More efficient Rabin-Miller test: with random witnesses, skip redundant
authormdw <mdw>
Sun, 13 Jan 2002 13:42:53 +0000 (13:42 +0000)
committermdw <mdw>
Sun, 13 Jan 2002 13:42:53 +0000 (13:42 +0000)
Montgomerization.  (Being bijective, it can't affect the distribution.)

pgen.c
rabin.c
rabin.h

diff --git a/pgen.c b/pgen.c
index 74a10bc..dca61d9 100644 (file)
--- a/pgen.c
+++ b/pgen.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: pgen.c,v 1.7 2001/02/03 16:05:32 mdw Exp $
+ * $Id: pgen.c,v 1.8 2002/01/13 13:42:53 mdw Exp $
  *
  * Prime generation glue
  *
  *
  * Prime generation glue
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: pgen.c,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: pgen.c,v $
+ * Revision 1.8  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.7  2001/02/03 16:05:32  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.7  2001/02/03 16:05:32  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.
@@ -145,11 +149,15 @@ int pgen_test(int rq, pgen_event *ev, void *p)
       rabin_create(r, ev->m);
       rc = PGEN_TRY;
       break;
       rabin_create(r, ev->m);
       rc = PGEN_TRY;
       break;
-    case PGEN_TRY: {
-      mp *a = mprand_range(MP_NEW, ev->m, ev->r, 0);
-      rc = rabin_test(r, a);
-      mp_drop(a);
-    } break;
+    case PGEN_TRY:
+      if (!ev->tests)
+       rc = rabin_rtest(r, MP_TWO);
+      else {
+       mp *a = mprand_range(MP_NEW, ev->m, ev->r, 0);
+       rc = rabin_rtest(r, a);
+       mp_drop(a);
+      }
+      break;
     case PGEN_DONE:
       rabin_destroy(r);
       rc = PGEN_DONE;
     case PGEN_DONE:
       rabin_destroy(r);
       rc = PGEN_DONE;
@@ -197,8 +205,8 @@ mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
     ev.m = MP_COPY(m);
   else
     ev.m = 0;
     ev.m = MP_COPY(m);
   else
     ev.m = 0;
-  ev.steps = steps;
-  ev.tests = tests;
+  ev.steps = 0;
+  ev.tests = 0;
   ev.r = fibrand_create(0);
 
   /* --- Tell the event handler we're under way --- */
   ev.r = fibrand_create(0);
 
   /* --- Tell the event handler we're under way --- */
@@ -274,17 +282,17 @@ mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
     /* --- If decrementing counters is requested, do that --- */
 
     if ((act & A_STEP) && steps) {
     /* --- If decrementing counters is requested, do that --- */
 
     if ((act & A_STEP) && steps) {
-      ev.steps--;
-      if (!ev.steps) {
+      ev.steps++;
+      if (ev.steps == steps) {
        act |= A_EVENT | A_ENDSTEP | A_DONE;
        rc = PGEN_ABORT;
       }
        act |= A_EVENT | A_ENDSTEP | A_DONE;
        rc = PGEN_ABORT;
       }
-      ev.tests = tests;
+      ev.tests = 0;
     }
 
     if ((act & A_TEST) && tests) {
     }
 
     if ((act & A_TEST) && tests) {
-      ev.tests--;
-      if (!ev.tests) {
+      ev.tests++;
+      if (ev.tests == tests) {
        act |= A_ENDTEST | A_ENDSTEP | A_DONE;
        rc = PGEN_DONE;
       }
        act |= A_ENDTEST | A_ENDSTEP | A_DONE;
        rc = PGEN_DONE;
       }
diff --git a/rabin.c b/rabin.c
index e6cd488..915dc14 100644 (file)
--- a/rabin.c
+++ b/rabin.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: rabin.c,v 1.6 2001/06/16 12:56:38 mdw Exp $
+ * $Id: rabin.c,v 1.7 2002/01/13 13:42:53 mdw Exp $
  *
  * Miller-Rabin primality test
  *
  *
  * Miller-Rabin primality test
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: rabin.c,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: rabin.c,v $
+ * 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.6  2001/06/16 12:56:38  mdw
  * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
  *
@@ -98,7 +102,7 @@ void rabin_destroy(rabin *r)
   mpmont_destroy(&r->mm);
 }
 
   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
  *
  * Arguments:  @rabin *r@ = pointer to Rabin-Miller context
  *             @mp *g@ = base to test the number against
@@ -107,10 +111,11 @@ void rabin_destroy(rabin *r)
  *             if it succeeded.
  *
  * Use:                Performs a single iteration of the Rabin-Miller primality
  *             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;
 {
   mp *y;
   mp *dd, *spare = MP_NEW;
@@ -123,8 +128,7 @@ int rabin_test(rabin *r, mp *g)
    * @y@ here has an extra factor of %$R$%.
    */
 
    * @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;
   if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
     rc = PGEN_PASS;
     goto done;
@@ -157,6 +161,15 @@ done:
   return (rc);
 }
 
   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
 /* --- @rabin_iters@ --- *
  *
  * Arguments:  @unsigned len@ = number of bits in value
diff --git a/rabin.h b/rabin.h
index d57efcd..d5ee9d3 100644 (file)
--- a/rabin.h
+++ b/rabin.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: rabin.h,v 1.5 2000/07/09 21:32:16 mdw Exp $
+ * $Id: rabin.h,v 1.6 2002/01/13 13:42:53 mdw Exp $
  *
  * Miller-Rabin primality test
  *
  *
  * Miller-Rabin primality test
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: rabin.h,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: rabin.h,v $
+ * Revision 1.6  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.5  2000/07/09 21:32:16  mdw
  * rabin_test: Correct error in comment.
  *
  * Revision 1.5  2000/07/09 21:32:16  mdw
  * rabin_test: Correct error in comment.
  *
@@ -105,7 +109,7 @@ extern void rabin_create(rabin */*r*/, mp */*m*/);
 
 extern void rabin_destroy(rabin */*r*/);
 
 
 extern void rabin_destroy(rabin */*r*/);
 
-/* --- @rabin_test@ --- *
+/* --- @rabin_test@, @rabin_rtest@ --- *
  *
  * Arguments:  @rabin *r@ = pointer to Rabin-Miller context
  *             @mp *g@ = base to test the number against
  *
  * Arguments:  @rabin *r@ = pointer to Rabin-Miller context
  *             @mp *g@ = base to test the number against
@@ -114,9 +118,11 @@ extern void rabin_destroy(rabin */*r*/);
  *             if it succeeded.
  *
  * Use:                Performs a single iteration of the Rabin-Miller primality
  *             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.
  */
 
  */
 
+extern int rabin_rtest(rabin */*r*/, mp */*g*/);
 extern int rabin_test(rabin */*r*/, mp */*g*/);
 
 /* --- @rabin_iters@ --- *
 extern int rabin_test(rabin */*r*/, mp */*g*/);
 
 /* --- @rabin_iters@ --- *