X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/2b2d07ca33cf57a82d416ee0aafa5842bf24e509..052b36d05a622a93733b735acce2de865b14627b:/strongprime.c diff --git a/strongprime.c b/strongprime.c index 4a7ddb8..a5f6052 100644 --- a/strongprime.c +++ b/strongprime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: strongprime.c,v 1.1 1999/12/22 15:51:22 mdw Exp $ + * $Id: strongprime.c,v 1.2 2000/02/12 18:21:03 mdw Exp $ * * Generate `strong' prime numbers * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: strongprime.c,v $ + * Revision 1.2 2000/02/12 18:21:03 mdw + * Overhaul of key management (again). + * * Revision 1.1 1999/12/22 15:51:22 mdw * Find `strong' RSA primes using Gordon's algorithm. * @@ -50,37 +53,34 @@ /*----- Main code ---------------------------------------------------------*/ -/* --- @strongprime@ --- * +/* --- @strongprime_setup@ --- * * * Arguments: @const char *name@ = pointer to name root - * @mp *d@ = destination integer + * @mp *d@ = destination for search start point + * @pfilt *f@ = where to store filter jump context * @unsigned nbits@ = number of bits wanted * @grand *r@ = random number source * @unsigned n@ = number of attempts to make * @pgen_proc *event@ = event handler function * @void *ectx@ = argument for the event handler * - * Returns: A `strong' prime, or zero. - * - * Use: Finds `strong' primes. A strong prime %$p$% is such that - * - * * %$p - 1$% has a large prime factor %$r$%, - * * %$p + 1$% has a large prime factor %$s$%, and - * * %$r - 1$% has a large prime factor %$t$%. + * Returns: A starting point for a `strong' prime search, or zero. * - * The numbers produced may be slightly larger than requested, - * by a few bits. + * Use: Sets up for a strong prime search, so that primes with + * particular properties can be found. It's probably important + * to note that the number left in the filter context @f@ is + * congruent to 2 (mod 4). */ -mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, - unsigned n, pgen_proc *event, void *ectx) +mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits, + grand *r, unsigned n, pgen_proc *event, void *ectx) { - mp *s, *t, *q, *p = 0; + mp *s, *t, *q; dstr dn = DSTR_INIT; - mp *rr = MP_NEW; + mp *rr = d; pgen_filterctx c; - pgen_jumpctx cj; + pgen_jumpctx j; rabin rb; /* --- The bitslop parameter --- * @@ -91,7 +91,7 @@ mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, * numbers around 10 seem to be good. */ -#define BITSLOP 10 +#define BITSLOP 12 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */ @@ -101,7 +101,7 @@ mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, rr = mprand(rr, nbits, r, 1); DRESET(&dn); dstr_putf(&dn, "%s [s]", name); if ((s = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_filter, &c, - rabin_iters(nbits), pgen_test, &rb)) == 0) + rabin_iters(nbits), pgen_test, &rb)) == 0) goto fail_s; mp_burn(s); @@ -119,12 +119,13 @@ mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, rr = mp_lsl(rr, rr, BITSLOP - 1); rr = mp_add(rr, rr, MP_ONE); DRESET(&dn); dstr_putf(&dn, "%s [r]", name); - cj.j = &c.f; + j.j = &c.f; nbits += BITSLOP; - if ((q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &cj, - rabin_iters(nbits), pgen_test, &rb)) == 0) - goto fail_r; + q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j, + rabin_iters(nbits), pgen_test, &rb); pfilt_destroy(&c.f); + if (!q) + goto fail_r; /* --- Select a suitable starting-point for finding %$p$% --- * * @@ -149,32 +150,75 @@ mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, mp *x; x = mp_mul(MP_NEW, q, s); x = mp_lsl(x, x, 1); - pfilt_create(&c.f, x); + pfilt_create(f, x); x = mp_lsl(x, x, BITSLOP - 1); rr = mp_add(rr, rr, x); mp_drop(x); } - if ((p = pgen(name, d, rr, event, ectx, n, pgen_jump, &cj, - rabin_iters(nbits * 2), pgen_test, &rb)) == 0) - goto fail_p; + /* --- Return the result --- */ - /* --- Tidy up because we've finished --- */ +#if 0 +fputs("r = ", stdout); mp_writefile(q, stdout, 10); putchar('\n'); +fputs("s = ", stdout); mp_writefile(s, stdout, 10); putchar('\n'); +fputs("t = ", stdout); mp_writefile(t, stdout, 10); putchar('\n'); +#endif -fail_p: mp_drop(q); + mp_drop(t); + mp_drop(s); + dstr_destroy(&dn); + return (rr); + + /* --- Tidy up if something failed --- */ + fail_r: - pfilt_destroy(&c.f); mp_drop(t); fail_t: mp_drop(s); fail_s: mp_drop(rr); dstr_destroy(&dn); - - return (p); + return (0); #undef BITSLOP } +/* --- @strongprime@ --- * + * + * Arguments: @const char *name@ = pointer to name root + * @mp *d@ = destination integer + * @unsigned nbits@ = number of bits wanted + * @grand *r@ = random number source + * @unsigned n@ = number of attempts to make + * @pgen_proc *event@ = event handler function + * @void *ectx@ = argument for the event handler + * + * Returns: A `strong' prime, or zero. + * + * Use: Finds `strong' primes. A strong prime %$p$% is such that + * + * * %$p - 1$% has a large prime factor %$r$%, + * * %$p + 1$% has a large prime factor %$s$%, and + * * %$r - 1$% has a large prime factor %$t$%. + * + * The numbers produced may be slightly larger than requested, + * by a few bits. + */ + +mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, + unsigned n, pgen_proc *event, void *ectx) +{ + pfilt f; + pgen_jumpctx j; + rabin rb; + + d = strongprime_setup(name, d, &f, nbits, r, n, event, ectx); + j.j = &f; + d = pgen(name, d, d, event, ectx, n, pgen_jump, &j, + rabin_iters(nbits), pgen_test, &rb); + pfilt_destroy(&f); + return (d); +} + /*----- That's all, folks -------------------------------------------------*/