math/mp-modsqrt.c: Zero has a square root. Return it correctly.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 22 Dec 2014 20:32:58 +0000 (20:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 27 Feb 2015 21:18:15 +0000 (21:18 +0000)
This causes `find' on prime curves to fail when given the x-coordinate
of a 2-torsion point.

math/mp-modsqrt.c
math/t/ec
math/t/mp

index c5aed1f..f7a7414 100644 (file)
@@ -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 --- */
index 92fb26a..acd16d6 100644 (file)
--- 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
index 7dae552..b50ea8d 100644 (file)
--- 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;