X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/79a340293970d5f9b9c000f82769179f9ba551bd..aa1082f28ddd05f3b946ca1a9c6bfaa17d18aca5:/mpmont.c diff --git a/mpmont.c b/mpmont.c index 66b7657..7522dba 100644 --- a/mpmont.c +++ b/mpmont.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpmont.c,v 1.3 1999/11/21 11:35:10 mdw Exp $ + * $Id: mpmont.c,v 1.5 1999/11/22 13:58:40 mdw Exp $ * * Montgomery reduction * @@ -30,6 +30,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpmont.c,v $ + * 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 + * %$R^2$%. + * * Revision 1.3 1999/11/21 11:35:10 mdw * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of * @mpmont_mul@ for squaring in exponentiation. @@ -48,6 +57,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@ --- * @@ -60,6 +79,18 @@ * Use: Initializes a Montgomery reduction context ready for use. */ +#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; +} + +#else + void mpmont_create(mpmont *mm, mp *m) { /* --- Take a copy of the modulus --- */ @@ -92,19 +123,20 @@ void mpmont_create(mpmont *mm, mp *m) { size_t l = MP_LEN(m); - mp *r = mp_create(l + 1); + mp *r = mp_create(2 * l + 1); mm->shift = l * MPW_BITS; MPX_ZERO(r->v, r->vl - 1); r->vl[-1] = 1; - mm->r = mm->r2 = MP_NEW; - mp_div(0, &mm->r, r, m); - r = mp_sqr(r, mm->r); + mm->r2 = MP_NEW; mp_div(0, &mm->r2, r, m); + mm->r = mpmont_reduce(mm, MP_NEW, mm->r2); MP_DROP(r); } } +#endif + /* --- @mpmont_destroy@ --- * * * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context @@ -131,6 +163,16 @@ void mpmont_destroy(mpmont *mm) * Returns: Result, %$a R^{-1} \bmod m$%. */ +#ifdef MPMONT_DISABLE + +mp *mpmont_reduce(mpmont *mm, mp *d, const mp *a) +{ + mp_div(0, &d, a, mm->m); + return (d); +} + +#else + mp *mpmont_reduce(mpmont *mm, mp *d, const mp *a) { mpw *dv, *dvl; @@ -171,6 +213,8 @@ mp *mpmont_reduce(mpmont *mm, mp *d, const mp *a) return (d); } +#endif + /* --- @mpmont_mul@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context @@ -180,6 +224,17 @@ mp *mpmont_reduce(mpmont *mm, mp *d, const mp *a) * Returns: Result, %$a b R^{-1} \bmod m$%. */ +#ifdef MPMONT_DISABLE + +mp *mpmont_mul(mpmont *mm, mp *d, const mp *a, const mp *b) +{ + d = mp_mul(d, a, b); + mp_div(0, &d, d, mm->m); + return (d); +} + +#else + mp *mpmont_mul(mpmont *mm, mp *d, const mp *a, const mp *b) { mpw *dv, *dvl; @@ -236,6 +291,8 @@ mp *mpmont_mul(mpmont *mm, mp *d, const mp *a, const mp *b) return (d); } +#endif + /* --- @mpmont_expr@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context