X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/8b021c3f89a78c3006ffc5d480feca6ef86d544e..2b645fb792c62ae0d38fcde4c39e1bd0889b0e06:/pgen.c diff --git a/pgen.c b/pgen.c index 0e67c76..dca61d9 100644 --- a/pgen.c +++ b/pgen.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: pgen.c,v 1.5 2000/06/17 11:52:36 mdw Exp $ + * $Id: pgen.c,v 1.8 2002/01/13 13:42:53 mdw Exp $ * * Prime generation glue * @@ -30,6 +30,17 @@ /*----- 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.6 2000/10/08 12:11:22 mdw + * Use @MP_EQ@ instead of @MP_CMP@. + * * Revision 1.5 2000/06/17 11:52:36 mdw * Signal a pgen abort if the jump and base share a common factor. * @@ -138,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; @@ -190,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 --- */ @@ -267,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; } @@ -314,8 +329,7 @@ mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx, ev.m = 0; } ev.r->ops->destroy(ev.r); - if (d != MP_NEW) - mp_drop(d); + mp_drop(d); return (ev.m); } @@ -339,7 +353,7 @@ static int verify(dstr *v) pf.step = 2; p = pgen("p", MP_NEW, m, pgen_evspin, 0, 0, pgen_filter, &pf, rabin_iters(mp_bits(m)), pgen_test, &r); - if (!p || MP_CMP(p, !=, q)) { + if (!p || !MP_EQ(p, q)) { fputs("\n*** pgen failed", stderr); fputs("\nm = ", stderr); mp_writefile(m, stderr, 10); fputs("\np = ", stderr); mp_writefile(p, stderr, 10); @@ -350,8 +364,7 @@ static int verify(dstr *v) mp_drop(m); mp_drop(q); - if (p) - mp_drop(p); + mp_drop(p); assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); }