Miscellaneous constification.
[u/mdw/catacomb] / rabin.c
diff --git a/rabin.c b/rabin.c
index 677e233..200ab0d 100644 (file)
--- a/rabin.c
+++ b/rabin.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: rabin.c,v 1.4 2000/06/22 19:03:02 mdw Exp $
+ * $Id: rabin.c,v 1.8 2004/04/02 01:03:49 mdw Exp $
  *
  * Miller-Rabin primality test
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: rabin.c,v $
+ * Revision 1.8  2004/04/02 01:03:49  mdw
+ * Miscellaneous constification.
+ *
+ * Revision 1.7  2002/01/13 13:42:53  mdw
+ * More efficient Rabin-Miller test: with random witnesses, skip redundant
+ * Montgomerization.  (Being bijective, it can't affect the distribution.)
+ *
+ * Revision 1.6  2001/06/16 12:56:38  mdw
+ * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
+ *
+ * Revision 1.5  2000/10/08 12:11:22  mdw
+ * Use @MP_EQ@ instead of @MP_CMP@.
+ *
  * Revision 1.4  2000/06/22 19:03:02  mdw
  * Use the new @mp_odd@ function.
  *
@@ -92,7 +105,7 @@ void rabin_destroy(rabin *r)
   mpmont_destroy(&r->mm);
 }
 
-/* --- @rabin_test@ --- *
+/* --- @rabin_test@, @rabin_rtest@ --- *
  *
  * Arguments:  @rabin *r@ = pointer to Rabin-Miller context
  *             @mp *g@ = base to test the number against
@@ -101,10 +114,11 @@ void rabin_destroy(rabin *r)
  *             if it succeeded.
  *
  * Use:                Performs a single iteration of the Rabin-Miller primality
- *             test.
+ *             test.  The @rtest@ variant assumes that %$g$% is either
+ *             already in Montgomery representation, or you don't care.
  */
 
-int rabin_test(rabin *r, mp *g)
+int rabin_rtest(rabin *r, mp *g)
 {
   mp *y;
   mp *dd, *spare = MP_NEW;
@@ -118,7 +132,7 @@ int rabin_test(rabin *r, mp *g)
    */
 
   y = mpmont_expr(&r->mm, MP_NEW, g, r->r);
-  if (MP_CMP(y, ==, r->mm.r) || MP_CMP(y, ==, r->m1)) {
+  if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
     rc = PGEN_PASS;
     goto done;
   }
@@ -133,9 +147,9 @@ int rabin_test(rabin *r, mp *g)
     dd = mp_sqr(spare, y);
     dd = mpmont_reduce(&r->mm, dd, dd);
     spare = y; y = dd;
-    if (MP_CMP(y, ==, r->mm.r))
+    if (MP_EQ(y, r->mm.r))
       break;
-    if (MP_CMP(y, ==, r->m1)) {
+    if (MP_EQ(y, r->m1)) {
       rc = PGEN_PASS;
       break;
     }
@@ -150,6 +164,15 @@ done:
   return (rc);
 }
 
+int rabin_test(rabin *r, mp *g)
+{
+  int rc;
+  g = mpmont_mul(&r->mm, MP_NEW, g, r->mm.r2);
+  rc = rabin_rtest(r, g);
+  mp_drop(g);
+  return (rc);
+}
+
 /* --- @rabin_iters@ --- *
  *
  * Arguments:  @unsigned len@ = number of bits in value
@@ -162,7 +185,7 @@ done:
 
 int rabin_iters(unsigned len)
 {
-  static struct {
+  static const struct {
     unsigned b;
     int i;
   } *p, *q, tab[] = {