From d032072ff0730e76e87821204c29c47331b13d99 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 22 Dec 2014 20:32:58 +0000 Subject: [PATCH] math/mp-modsqrt.c: Zero has a square root. Return it correctly. This causes `find' on prime curves to fail when given the x-coordinate of a 2-torsion point. --- math/mp-modsqrt.c | 8 +++++++- math/t/ec | 4 ++++ math/t/mp | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/math/mp-modsqrt.c b/math/mp-modsqrt.c index c5aed1fa..f7a74148 100644 --- a/math/mp-modsqrt.c +++ b/math/mp-modsqrt.c @@ -63,12 +63,18 @@ mp *mp_modsqrt(mp *d, mp *a, mp *p) mp *r, *A, *aa; mp *t; grand *gr; + int j; /* --- Cope if %$a \not\in Q_p$% --- */ - if (mp_jacobi(a, p) != 1) { + j = mp_jacobi(a, p); + if (j == -1) { mp_drop(d); return (0); + } else if (j == 0) { + if (d != a) mp_drop(d); + d = MP_COPY(a); + return (d); } /* --- Choose some quadratic non-residue --- */ diff --git a/math/t/ec b/math/t/ec index 92fb26a8..acd16d6e 100644 --- a/math/t/ec +++ b/math/t/ec @@ -139,6 +139,10 @@ find { "0x188da80eb03090f67cbf20eb43a18801f4ff0afd82ff1411, 0x2330e62c1895405fa2ad63f8a8a0ab35b05a0360c2133db9"; + "prime: 6277101735386680763835789423207666416102355444464034512659; prime: -3, 6" + 0xcdf65dc6c906c5c240dba0d156b911614abe806fa2b8dc0a + "0xcdf65dc6c906c5c240dba0d156b911614abe806fa2b8dc0a, 0"; + "binpoly: 0x800000000000000000000000000000000000000c9 bin: 1, 0x20a601907b8c953ca1481eb10512f78744a3205fd" 0x3f0eba16286a2d57ea0991168d4994637e8343e36 diff --git a/math/t/mp b/math/t/mp index 7dae5526..b50ea8dd 100644 --- a/math/t/mp +++ b/math/t/mp @@ -266,6 +266,7 @@ jacobi { } modsqrt { + 0 5 0; 1 3 1; 4 5 2; 9775592058107450692 13391974640168007623 3264570455655810730; -- 2.11.0