Miscellaneous constification.
[u/mdw/catacomb] / rabin.c
diff --git a/rabin.c b/rabin.c
index e6cd488..200ab0d 100644 (file)
--- a/rabin.c
+++ b/rabin.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: rabin.c,v 1.6 2001/06/16 12:56:38 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@.
  *
@@ -98,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
@@ -107,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;
@@ -123,8 +131,7 @@ int rabin_test(rabin *r, mp *g)
    * @y@ here has an extra factor of %$R$%.
    */
 
-  y = mpmont_mul(&r->mm, MP_NEW, g, r->mm.r2);
-  y = mpmont_expr(&r->mm, y, y, r->r);
+  y = mpmont_expr(&r->mm, MP_NEW, g, r->r);
   if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
     rc = PGEN_PASS;
     goto done;
@@ -157,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
@@ -169,7 +185,7 @@ done:
 
 int rabin_iters(unsigned len)
 {
-  static struct {
+  static const struct {
     unsigned b;
     int i;
   } *p, *q, tab[] = {