X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/419d33ed2f44bd5f00b0c78a81aaa1abc37f2056..34e4f738bcba58e6d8c4cabbb0b3232a65b42a9d:/mpbarrett.c diff --git a/mpbarrett.c b/mpbarrett.c index affbc65..934097d 100644 --- a/mpbarrett.c +++ b/mpbarrett.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpbarrett.c,v 1.5 2000/07/29 17:04:33 mdw Exp $ + * $Id: mpbarrett.c,v 1.9 2004/04/01 12:50:09 mdw Exp $ * * Barrett modular reduction * @@ -30,6 +30,22 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpbarrett.c,v $ + * Revision 1.9 2004/04/01 12:50:09 mdw + * Add cyclic group abstraction, with test code. Separate off exponentation + * functions for better static linking. Fix a buttload of bugs on the way. + * Generally ensure that negative exponents do inversion correctly. Add + * table of standard prime-field subgroups. (Binary field subgroups are + * currently unimplemented but easy to add if anyone ever finds a good one.) + * + * Revision 1.8 2001/06/16 13:00:20 mdw + * Use the generic exponentiation functions. + * + * Revision 1.7 2001/04/19 18:25:26 mdw + * Use sliding-window exponentiation. + * + * Revision 1.6 2000/10/08 12:03:44 mdw + * (mpbarrett_reduce): Cope with negative numbers. + * * Revision 1.5 2000/07/29 17:04:33 mdw * Change to use left-to-right bitwise exponentiation. This will improve * performance when the base is small. @@ -114,8 +130,7 @@ void mpbarrett_destroy(mpbarrett *mb) * Returns: The residue of @m@ modulo the number in the reduction * context. * - * Use: Performs an efficient modular reduction. The argument is - * assumed to be positive. + * Use: Performs an efficient modular reduction. */ mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m) @@ -161,7 +176,7 @@ mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m) mpx_umul(r->v, r->vl, q->v + k + 1, q->vl, mb->m->v, mb->m->vl); MP_DEST(d, k + 1, r->f); mpx_usub(d->v, d->vl, m->v, mvl, r->v, r->vl); - d->f = (m->f | r->f) & MP_BURN; + d->f = (m->f | r->f) & (MP_BURN | MP_NEG); MP_DROP(r); MP_DROP(q); MP_DROP(m); @@ -173,77 +188,15 @@ mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m) while (MPX_UCMP(d->v, d->vl, >=, mb->m->v, mb->m->vl)) mpx_usub(d->v, d->vl, d->v, d->vl, mb->m->v, mb->m->vl); - MP_SHRINK(d); - return (d); -} + /* --- Fix up the sign --- */ -/* --- @mpbarrett_exp@ --- * - * - * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context - * @mp *d@ = fake destination - * @mp *a@ = base - * @mp *e@ = exponent - * - * Returns: Result, %$a^e \bmod m$%. - */ - -mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e) -{ - mpscan sc; - mp *x = MP_ONE; - mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; - unsigned sq = 0; - - a = MP_COPY(a); - mp_rscan(&sc, e); - if (!MP_RSTEP(&sc)) - goto exit; - while (!MP_RBIT(&sc)) - MP_RSTEP(&sc); - - /* --- Do the main body of the work --- */ - - for (;;) { - sq++; - while (sq) { - mp *y; - y = mp_sqr(spare, x); - y = mpbarrett_reduce(mb, y, y); - spare = x; x = y; - sq--; - } - { - mp *y = mp_mul(spare, x, a); - y = mpbarrett_reduce(mb, y, y); - spare = x; x = y; - } - for (;;) { - if (!MP_RSTEP(&sc)) - goto done; - if (MP_RBIT(&sc)) - break; - sq++; - } + if (d->f & MP_NEG) { + mpx_usub(d->v, d->vl, mb->m->v, mb->m->vl, d->v, d->vl); + d->f &= ~MP_NEG; } - /* --- Do a final round of squaring --- */ - -done: - while (sq) { - mp *y; - y = mp_sqr(spare, x); - y = mpbarrett_reduce(mb, y, y); - spare = x; x = y; - sq--; - } - -exit: - MP_DROP(a); - if (spare != MP_NEW) - MP_DROP(spare); - if (d != MP_NEW) - MP_DROP(d); - return (x); + MP_SHRINK(d); + return (d); } /*----- Test rig ----------------------------------------------------------*/ @@ -262,7 +215,7 @@ static int vmod(dstr *v) mpbarrett_create(&mb, n); s = mpbarrett_reduce(&mb, MP_NEW, x); - if (MP_CMP(s, !=, r)) { + if (!MP_EQ(s, r)) { fputs("\n*** barrett reduction failure\n", stderr); fputs("x = ", stderr); mp_writefile(x, stderr, 10); fputc('\n', stderr); fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr); @@ -280,44 +233,8 @@ static int vmod(dstr *v) return (ok); } -static int vexp(dstr *v) -{ - mp *m = *(mp **)v[0].buf; - mp *a = *(mp **)v[1].buf; - mp *b = *(mp **)v[2].buf; - mp *r = *(mp **)v[3].buf; - mp *mr; - int ok = 1; - - mpbarrett mb; - mpbarrett_create(&mb, m); - - mr = mpbarrett_exp(&mb, MP_NEW, a, b); - - if (MP_CMP(mr, !=, r)) { - fputs("\n*** barrett modexp failed", stderr); - fputs("\n m = ", stderr); mp_writefile(m, stderr, 10); - fputs("\n a = ", stderr); mp_writefile(a, stderr, 10); - fputs("\n e = ", stderr); mp_writefile(b, stderr, 10); - fputs("\n r = ", stderr); mp_writefile(r, stderr, 10); - fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10); - fputc('\n', stderr); - ok = 0; - } - - mp_drop(m); - mp_drop(a); - mp_drop(b); - mp_drop(r); - mp_drop(mr); - mpbarrett_destroy(&mb); - assert(mparena_count(MPARENA_GLOBAL) == 0); - return ok; -} - static test_chunk tests[] = { { "mpbarrett-reduce", vmod, { &type_mp, &type_mp, &type_mp, 0 } }, - { "mpbarrett-exp", vexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, { 0, 0, { 0 } } };