X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/93feaa6e5b80dfa22365a13c94f8e6b6b33f3dcb..d34decd2b2b88240cf4ca68a2a5feb7bf36de6e7:/mpmont.c diff --git a/mpmont.c b/mpmont.c index 35bffc2..e7b9bb7 100644 --- a/mpmont.c +++ b/mpmont.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpmont.c,v 1.4 1999/11/21 12:27:06 mdw Exp $ + * $Id: mpmont.c,v 1.9 2000/06/17 11:45:09 mdw Exp $ * * Montgomery reduction * @@ -30,6 +30,24 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpmont.c,v $ + * Revision 1.9 2000/06/17 11:45:09 mdw + * Major memory management overhaul. Added arena support. Use the secure + * arena for secret integers. Replace and improve the MP management macros + * (e.g., replace MP_MODIFY by MP_DEST). + * + * Revision 1.8 1999/12/22 15:55:00 mdw + * Adjust Karatsuba parameters. + * + * Revision 1.7 1999/12/11 01:51:14 mdw + * Use a Karatsuba-based reduction for large moduli. + * + * Revision 1.6 1999/12/10 23:18:39 mdw + * Change interface for suggested destinations. + * + * Revision 1.5 1999/11/22 13:58:40 mdw + * Add an option to disable Montgomery reduction, so that performance + * comparisons can be done. + * * Revision 1.4 1999/11/21 12:27:06 mdw * Remove a division from the Montgomery setup by calculating * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of @@ -53,6 +71,16 @@ #include "mp.h" #include "mpmont.h" +/*----- Tweakables --------------------------------------------------------*/ + +/* --- @MPMONT_DISABLE@ --- * + * + * Replace all the clever Montgomery reduction with good old-fashioned long + * division. + */ + +/* #define MPMONT_DISABLE */ + /*----- Main code ---------------------------------------------------------*/ /* --- @mpmont_create@ --- * @@ -63,52 +91,62 @@ * Returns: --- * * Use: Initializes a Montgomery reduction context ready for use. + * The argument @m@ must be a positive odd integer. */ +#ifdef MPMONT_DISABLE + +void mpmont_create(mpmont *mm, mp *m) +{ + mp_shrink(m); + mm->m = MP_COPY(m); + mm->r = MP_ONE; + mm->r2 = MP_ONE; + mm->mi = MP_ONE; +} + +#else + void mpmont_create(mpmont *mm, mp *m) { + size_t n = MP_LEN(m); + mp *r2 = mp_new(2 * n + 1, 0); + mp r; + + /* --- Validate the arguments --- */ + + assert(((void)"Montgomery modulus must be positive", + (m->f & MP_NEG) == 0)); + assert(((void)"Montgomery modulus must be odd", m->v[0] & 1)); + /* --- Take a copy of the modulus --- */ mp_shrink(m); mm->m = MP_COPY(m); - /* --- Find the magic value @mi@ --- * - * - * This is a slightly grungy way of solving the problem, but it does work. - */ + /* --- Determine %$R^2$% --- */ - { - mpw av[2] = { 0, 1 }; - mp a, b; - mp *i; - mpw mi; + mm->n = n; + MPX_ZERO(r2->v, r2->vl - 1); + r2->vl[-1] = 1; - mp_build(&a, av, av + 2); - mp_build(&b, m->v, m->v + 1); - mp_gcd(0, 0, &i, &a, &b); - mi = i->v[0]; - if (!(i->f & MP_NEG)) - mi = MPW(-mi); - mm->mi = mi; - MP_DROP(i); - } + /* --- Find the magic value @mi@ --- */ + + mp_build(&r, r2->v + n, r2->vl); + mm->mi = MP_NEW; + mp_gcd(0, 0, &mm->mi, &r, m); + mm->mi = mp_sub(mm->mi, &r, mm->mi); /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */ - { - size_t l = MP_LEN(m); - mp *r = mp_create(2 * l + 1); - - mm->shift = l * MPW_BITS; - MPX_ZERO(r->v, r->vl - 1); - r->vl[-1] = 1; - mm->r2 = MP_NEW; - mp_div(0, &mm->r2, r, m); - mm->r = mpmont_reduce(mm, MP_NEW, mm->r2); - MP_DROP(r); - } + mm->r2 = MP_NEW; + mp_div(0, &mm->r2, r2, m); + mm->r = mpmont_reduce(mm, MP_NEW, mm->r2); + MP_DROP(r2); } +#endif + /* --- @mpmont_destroy@ --- * * * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context @@ -124,137 +162,199 @@ void mpmont_destroy(mpmont *mm) MP_DROP(mm->m); MP_DROP(mm->r); MP_DROP(mm->r2); + MP_DROP(mm->mi); } /* --- @mpmont_reduce@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context * @mp *d@ = destination - * @const mp *a@ = source, assumed positive + * @mp *a@ = source, assumed positive * * Returns: Result, %$a R^{-1} \bmod m$%. */ -mp *mpmont_reduce(mpmont *mm, mp *d, const mp *a) +#ifdef MPMONT_DISABLE + +mp *mpmont_reduce(mpmont *mm, mp *d, mp *a) { - mpw *dv, *dvl; - const mpw *mv, *mvl; - size_t n; + mp_div(0, &d, a, mm->m); + return (d); +} - /* --- Initial conditioning of the arguments --- */ +#else - n = MP_LEN(mm->m); +mp *mpmont_reduce(mpmont *mm, mp *d, mp *a) +{ + size_t n = mm->n; + + /* --- Check for serious Karatsuba reduction --- */ + + if (n > KARATSUBA_CUTOFF * 3) { + mp al; + mpw *vl; + mp *u; + + if (MP_LEN(a) >= n) + vl = a->v + n; + else + vl = a->vl; + mp_build(&al, a->v, vl); + u = mp_mul(MP_NEW, &al, mm->mi); + if (MP_LEN(u) > n) + u->vl = u->v + n; + u = mp_mul(u, u, mm->m); + d = mp_add(d, a, u); + mp_drop(u); + } + + /* --- Otherwise do it the hard way --- */ - if (d == a) - MP_MODIFY(d, 2 * n + 1); else { - MP_MODIFY(d, 2 * n + 1); - memcpy(d->v, a->v, MPWS(MP_LEN(a))); - memset(d->v + MP_LEN(a), 0, MPWS(MP_LEN(d) - MP_LEN(a))); - } - - dv = d->v; dvl = d->vl; - mv = mm->m->v; mvl = mm->m->vl; + mpw *dv, *dvl; + mpw *mv, *mvl; + mpw mi; + size_t k = n; + + /* --- Initial conditioning of the arguments --- */ - /* --- Let's go to work --- */ + a = MP_COPY(a); + if (d) + MP_DROP(d); + d = a; + MP_DEST(d, 2 * n + 1, a->f); - while (n--) { - mpw u = MPW(*dv * mm->mi); - MPX_UMLAN(dv, dvl, mv, mvl, u); - dv++; + dv = d->v; dvl = d->vl; + mv = mm->m->v; mvl = mm->m->vl; + + /* --- Let's go to work --- */ + + mi = mm->mi->v[0]; + while (k--) { + mpw u = MPW(*dv * mi); + MPX_UMLAN(dv, dvl, mv, mvl, u); + dv++; + } } - /* --- Done --- */ + /* --- Wrap everything up --- */ - memmove(d->v, dv, MPWS(dvl - dv)); - d->vl -= dv - d->v; - MP_SHRINK(d); - d->f = a->f & MP_BURN; + memmove(d->v, d->v + n, MPWS(MP_LEN(d) - n)); + d->vl -= n; if (MP_CMP(d, >=, mm->m)) d = mp_sub(d, d, mm->m); + MP_SHRINK(d); return (d); } +#endif + /* --- @mpmont_mul@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context * @mp *d@ = destination - * @const mp *a, *b@ = sources, assumed positive + * @mp *a, *b@ = sources, assumed positive * * Returns: Result, %$a b R^{-1} \bmod m$%. */ -mp *mpmont_mul(mpmont *mm, mp *d, const mp *a, const mp *b) +#ifdef MPMONT_DISABLE + +mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b) { - mpw *dv, *dvl; - const mpw *av, *avl; - const mpw *bv, *bvl; - const mpw *mv, *mvl; - mpw y; - size_t n, i; + d = mp_mul(d, a, b); + mp_div(0, &d, d, mm->m); + return (d); +} - /* --- Initial conditioning of the arguments --- */ +#else - if (MP_LEN(a) > MP_LEN(b)) { - const mp *t = a; a = b; b = t; - } - n = MP_LEN(mm->m); - - MP_MODIFY(d, 2 * n + 1); - dv = d->v; dvl = d->vl; - MPX_ZERO(dv, dvl); - av = a->v; avl = a->vl; - bv = b->v; bvl = b->vl; - mv = mm->m->v; mvl = mm->m->vl; - y = *bv; - - /* --- Montgomery multiplication phase --- */ - - i = 0; - while (i < n && av < avl) { - mpw x = *av++; - mpw u = MPW((*dv + x * y) * mm->mi); - MPX_UMLAN(dv, dvl, bv, bvl, x); - MPX_UMLAN(dv, dvl, mv, mvl, u); - dv++; - i++; - } +mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b) +{ + if (mm->n > KARATSUBA_CUTOFF * 3) { + d = mp_mul(d, a, b); + d = mpmont_reduce(mm, d, d); + } else { + mpw *dv, *dvl; + mpw *av, *avl; + mpw *bv, *bvl; + mpw *mv, *mvl; + mpw y; + size_t n, i; + mpw mi; - /* --- Simpler Montgomery reduction phase --- */ + /* --- Initial conditioning of the arguments --- */ - while (i < n) { - mpw u = MPW(*dv * mm->mi); - MPX_UMLAN(dv, dvl, mv, mvl, u); - dv++; - i++; - } + if (MP_LEN(a) > MP_LEN(b)) { + mp *t = a; a = b; b = t; + } + n = MP_LEN(mm->m); + + a = MP_COPY(a); + b = MP_COPY(b); + MP_DEST(d, 2 * n + 1, a->f | b->f | MP_UNDEF); + dv = d->v; dvl = d->vl; + MPX_ZERO(dv, dvl); + av = a->v; avl = a->vl; + bv = b->v; bvl = b->vl; + mv = mm->m->v; mvl = mm->m->vl; + y = *bv; + + /* --- Montgomery multiplication phase --- */ + + i = 0; + mi = mm->mi->v[0]; + while (i < n && av < avl) { + mpw x = *av++; + mpw u = MPW((*dv + x * y) * mi); + MPX_UMLAN(dv, dvl, bv, bvl, x); + MPX_UMLAN(dv, dvl, mv, mvl, u); + dv++; + i++; + } - /* --- Done --- */ + /* --- Simpler Montgomery reduction phase --- */ + + while (i < n) { + mpw u = MPW(*dv * mi); + MPX_UMLAN(dv, dvl, mv, mvl, u); + dv++; + i++; + } + + /* --- Done --- */ + + memmove(d->v, dv, MPWS(dvl - dv)); + d->vl -= dv - d->v; + MP_SHRINK(d); + d->f = (a->f | b->f) & MP_BURN; + if (MP_CMP(d, >=, mm->m)) + d = mp_sub(d, d, mm->m); + MP_DROP(a); + MP_DROP(b); + } - memmove(d->v, dv, MPWS(dvl - dv)); - d->vl -= dv - d->v; - MP_SHRINK(d); - d->f = (a->f | b->f) & MP_BURN; - if (MP_CMP(d, >=, mm->m)) - d = mp_sub(d, d, mm->m); return (d); } +#endif + /* --- @mpmont_expr@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context - * @const mp *a@ = base - * @const mp *e@ = exponent + * @mp *d@ = fake destination + * @mp *a@ = base + * @mp *e@ = exponent * * Returns: Result, %$a^e R \bmod m$%. */ -mp *mpmont_expr(mpmont *mm, const mp *a, const mp *e) +mp *mpmont_expr(mpmont *mm, mp *d, mp *a, mp *e) { mpscan sc; mp *ar = mpmont_mul(mm, MP_NEW, a, mm->r2); - mp *d = MP_COPY(mm->r); - mp *spare = MP_NEW; + mp *x = MP_COPY(mm->r); + mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; mp_scan(&sc, e); @@ -269,8 +369,8 @@ mp *mpmont_expr(mpmont *mm, const mp *a, const mp *e) spare = ar; ar = dd; sq--; } - dd = mpmont_mul(mm, spare, d, ar); - spare = d; d = dd; + dd = mpmont_mul(mm, spare, x, ar); + spare = x; x = dd; } sq++; if (!MP_STEP(&sc)) @@ -280,21 +380,24 @@ mp *mpmont_expr(mpmont *mm, const mp *a, const mp *e) MP_DROP(ar); if (spare != MP_NEW) MP_DROP(spare); - return (d); + if (d != MP_NEW) + MP_DROP(d); + return (x); } /* --- @mpmont_exp@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context - * @const mp *a@ = base - * @const mp *e@ = exponent + * @mp *d@ = fake destination + * @mp *a@ = base + * @mp *e@ = exponent * * Returns: Result, %$a^e \bmod m$%. */ -mp *mpmont_exp(mpmont *mm, const mp *a, const mp *e) +mp *mpmont_exp(mpmont *mm, mp *d, mp *a, mp *e) { - mp *d = mpmont_expr(mm, a, e); + d = mpmont_expr(mm, d, a, e); d = mpmont_reduce(mm, d, d); return (d); } @@ -315,9 +418,9 @@ static int tcreate(dstr *v) mpmont_create(&mm, m); - if (mm.mi != mi->v[0]) { + if (mm.mi->v[0] != mi->v[0]) { fprintf(stderr, "\n*** bad mi: found %lu, expected %lu", - (unsigned long)mm.mi, (unsigned long)mi->v[0]); + (unsigned long)mm.mi->v[0], (unsigned long)mi->v[0]); fputs("\nm = ", stderr); mp_writefile(m, stderr, 10); fputc('\n', stderr); ok = 0; @@ -346,6 +449,7 @@ static int tcreate(dstr *v) MP_DROP(r); MP_DROP(r2); mpmont_destroy(&mm); + assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); } @@ -403,6 +507,7 @@ static int tmul(dstr *v) MP_DROP(b); MP_DROP(r); mpmont_destroy(&mm); + assert(mparena_count(MPARENA_GLOBAL) == 0); return ok; } @@ -418,7 +523,7 @@ static int texp(dstr *v) mpmont mm; mpmont_create(&mm, m); - mr = mpmont_exp(&mm, a, b); + mr = mpmont_exp(&mm, MP_NEW, a, b); if (MP_CMP(mr, !=, r)) { fputs("\n*** montgomery modexp failed", stderr); @@ -437,14 +542,15 @@ static int texp(dstr *v) MP_DROP(r); MP_DROP(mr); mpmont_destroy(&mm); + assert(mparena_count(MPARENA_GLOBAL) == 0); return ok; } static test_chunk tests[] = { - { "create", tcreate, { &type_mp, &type_mp, &type_mp, &type_mp } }, - { "mul", tmul, { &type_mp, &type_mp, &type_mp, &type_mp } }, - { "exp", texp, { &type_mp, &type_mp, &type_mp, &type_mp } }, + { "create", tcreate, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, + { "mul", tmul, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, + { "exp", texp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, { 0, 0, { 0 } }, };