math/mp-gcd.c: Avoid clobbering constants during the sign fixup.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 27 Dec 2013 12:00:53 +0000 (12:00 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 27 Dec 2013 12:00:53 +0000 (12:00 +0000)
If the GCD computation was trivial, then the state still contains
references to the constants `MP_ZERO' and `MP_ONE'.  It would be Really Bad
to trash their signs -- in particular, this causes comparisons with
`MP_ONE' to fail, leading to an assertion failure from `mp_modinv'.

math/mp-gcd.c
math/t/mp

index 6e059e3..c36689c 100644 (file)
@@ -199,10 +199,8 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
 
     /* --- Twiddle the signs --- */
 
-    if (f & f_aneg)
-      x->f ^= MP_NEG;
-    if (f & f_bneg)
-      y->f ^= MP_NEG;
+    if (f & f_aneg) { MP_SPLIT(x); x->f ^= MP_NEG; }
+    if (f & f_bneg) { MP_SPLIT(y); y->f ^= MP_NEG; }
 
     /* --- Store the results --- */
 
index fe74f07..7dae552 100644 (file)
--- a/math/t/mp
+++ b/math/t/mp
@@ -199,6 +199,7 @@ modinv {
   5 9 2;
   15 64 47;
   564566436 546457643 408896426;
+  -1 257 -1;
 }
 
 jacobi {