Make tables of standard encryption schemes etc.
[u/mdw/catacomb] / mp-modsqrt.c
index 1c66c78..df60aba 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp-modsqrt.c,v 1.2 2000/10/08 12:02:21 mdw Exp $
+ * $Id: mp-modsqrt.c,v 1.4 2001/06/16 12:56:38 mdw Exp $
  *
  * Compute square roots modulo a prime
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp-modsqrt.c,v $
+ * Revision 1.4  2001/06/16 12:56:38  mdw
+ * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
+ *
+ * Revision 1.3  2001/02/03 12:00:29  mdw
+ * Now @mp_drop@ checks its argument is non-NULL before attempting to free
+ * it.  Note that the macro version @MP_DROP@ doesn't do this.
+ *
  * Revision 1.2  2000/10/08 12:02:21  mdw
  * Use @MP_EQ@ instead of @MP_CMP@.
  *
@@ -78,8 +85,7 @@ mp *mp_modsqrt(mp *d, mp *a, mp *p)
   /* --- Cope if %$a \not\in Q_p$% --- */
 
   if (mp_jacobi(a, p) != 1) {
-    if (d)
-      mp_drop(d);
+    mp_drop(d);
     return (0);
   }
 
@@ -108,10 +114,13 @@ mp *mp_modsqrt(mp *d, mp *a, mp *p)
   /* --- Now to really get going --- */
 
   mpmont_create(&mm, p);
+  b = mpmont_mul(&mm, b, b, mm.r2);
   c = mpmont_expr(&mm, b, b, t);
   t = mp_add(t, t, MP_ONE);
   t = mp_lsr(t, t, 1);
-  r = mpmont_expr(&mm, t, a, t);
+  dd = mpmont_mul(&mm, MP_NEW, a, mm.r2);
+  r = mpmont_expr(&mm, t, dd, t);
+  mp_drop(dd);
   ainv = mpmont_mul(&mm, ainv, ainv, mm.r2);
 
   mone = mp_sub(MP_NEW, p, mm.r);
@@ -146,8 +155,7 @@ mp *mp_modsqrt(mp *d, mp *a, mp *p)
   d = mpmont_reduce(&mm, d, r);
   mp_drop(ainv);
   mp_drop(r); mp_drop(c);
-  if (dd)
-    mp_drop(dd);
+  mp_drop(dd);
   mp_drop(mone);
   mpmont_destroy(&mm);
 
@@ -194,8 +202,7 @@ static int verify(dstr *v)
 
   mp_drop(a);
   mp_drop(p);
-  if (r)
-    mp_drop(r);
+  mp_drop(r);
   mp_drop(rr);
   assert(mparena_count(MPARENA_GLOBAL) == 0);
   return (ok);