X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/eab06f16ebf8a3c42b4a372e5fe5d14fefc05b24..3884054be9261b4607f5f3a2d0b79695f65b851f:/pgen.c 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; }