X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/22bab86c9df047bdd258283c6567821319ba7a6f..2b645fb792c62ae0d38fcde4c39e1bd0889b0e06:/rabin.c diff --git a/rabin.c b/rabin.c index a5ebdcc..915dc14 100644 --- a/rabin.c +++ b/rabin.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: rabin.c,v 1.5 2000/10/08 12:11:22 mdw Exp $ + * $Id: rabin.c,v 1.7 2002/01/13 13:42:53 mdw Exp $ * * Miller-Rabin primality test * @@ -30,6 +30,13 @@ /*----- 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.5 2000/10/08 12:11:22 mdw * Use @MP_EQ@ instead of @MP_CMP@. * @@ -95,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 @@ -104,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; @@ -153,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