From: mdw Date: Sun, 13 Jan 2002 13:42:53 +0000 (+0000) Subject: More efficient Rabin-Miller test: with random witnesses, skip redundant X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/283b9af095a5b24ae71b49a6d2dcbdcdaae47c40 More efficient Rabin-Miller test: with random witnesses, skip redundant Montgomerization. (Being bijective, it can't affect the distribution.) --- diff --git a/pgen.c b/pgen.c index 74a10bc..dca61d9 100644 --- a/pgen.c +++ b/pgen.c @@ -1,6 +1,6 @@ /* -*-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 * @@ -30,6 +30,10 @@ /*----- 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. @@ -145,11 +149,15 @@ int pgen_test(int rq, pgen_event *ev, void *p) 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; @@ -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.steps = steps; - ev.tests = tests; + ev.steps = 0; + ev.tests = 0; 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) { - ev.steps--; - if (!ev.steps) { + ev.steps++; + if (ev.steps == steps) { act |= A_EVENT | A_ENDSTEP | A_DONE; rc = PGEN_ABORT; } - ev.tests = tests; + ev.tests = 0; } 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; } diff --git a/rabin.c b/rabin.c index e6cd488..915dc14 100644 --- 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.7 2002/01/13 13:42:53 mdw Exp $ * * Miller-Rabin primality test * @@ -30,6 +30,10 @@ /*----- 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@. * @@ -98,7 +102,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 +111,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 +128,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 +161,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 diff --git a/rabin.h b/rabin.h index d57efcd..d5ee9d3 100644 --- a/rabin.h +++ b/rabin.h @@ -1,6 +1,6 @@ /* -*-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 * @@ -30,6 +30,10 @@ /*----- 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. * @@ -105,7 +109,7 @@ extern void rabin_create(rabin */*r*/, mp */*m*/); 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 @@ -114,9 +118,11 @@ extern 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. */ +extern int rabin_rtest(rabin */*r*/, mp */*g*/); extern int rabin_test(rabin */*r*/, mp */*g*/); /* --- @rabin_iters@ --- *