X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/4cc02d06a57cc386ba445d34d9b83329bb29f89c..d34decd2b2b88240cf4ca68a2a5feb7bf36de6e7:/mpmont-mexp.c diff --git a/mpmont-mexp.c b/mpmont-mexp.c index 4180fbc..0e5da91 100644 --- a/mpmont-mexp.c +++ b/mpmont-mexp.c @@ -1,8 +1,8 @@ /* -*-c-*- * - * $Id: mpmont-mexp.c,v 1.1 1999/11/19 13:19:29 mdw Exp $ + * $Id: mpmont-mexp.c,v 1.4 2000/06/17 11:45:09 mdw Exp $ * - * Multiplle simultaneous exponentiations + * Multiple simultaneous exponentiations * * (c) 1999 Straylight/Edgeware */ @@ -30,6 +30,18 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpmont-mexp.c,v $ + * Revision 1.4 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.3 1999/12/10 23:18:39 mdw + * Change interface for suggested destinations. + * + * Revision 1.2 1999/11/21 11:35:10 mdw + * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of + * @mpmont_mul@ for squaring in exponentiation. + * * Revision 1.1 1999/11/19 13:19:29 mdw * Simultaneous exponentiation support. * @@ -45,6 +57,7 @@ /* --- @mpmont_mexpr@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context + * @mp *d@ = fake destination * @mpmont_factor *f@ = pointer to array of factors * @size_t n@ = number of factors supplied * @@ -60,7 +73,7 @@ typedef struct scan { mpw w; } scan; -mp *mpmont_mexpr(mpmont *mm, mpmont_factor *f, size_t n) +mp *mpmont_mexpr(mpmont *mm, mp *d, mpmont_factor *f, size_t n) { size_t vn = 1 << n; mp **v = xmalloc(vn * sizeof(mp *)); @@ -113,6 +126,9 @@ mp *mpmont_mexpr(mpmont *mm, mpmont_factor *f, size_t n) * * I must scan the exponents from left to right, which is a shame. It * means that I can't use the standard @mpscan@ stuff, in particular. + * + * If any of the exponents are considered secret then make the accumulator + * automatically set the secret bit. */ { @@ -124,6 +140,8 @@ mp *mpmont_mexpr(mpmont *mm, mpmont_factor *f, size_t n) s[i].len = MP_LEN(f[i].exp); if (s[i].len > o) o = s[i].len; + if (f[i].exp->f & MP_BURN) + spare = MP_NEWSEC; } b = 0; } @@ -157,7 +175,8 @@ mp *mpmont_mexpr(mpmont *mm, mpmont_factor *f, size_t n) /* --- Accumulate the result --- */ if (spare) { - dd = mpmont_mul(mm, spare, a, a); + dd = mp_sqr(spare, a); + dd = mpmont_reduce(mm, dd, dd); spare = a; a = dd; } @@ -181,12 +200,16 @@ mp *mpmont_mexpr(mpmont *mm, mpmont_factor *f, size_t n) free(s); } + if (d != MP_NEW) + MP_DROP(d); + return (a); } /* --- @mpmont_mexp@ --- * * * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context + * @mp *d@ = fake destination * @mpmont_factor *f@ = pointer to array of factors * @size_t n@ = number of factors supplied * @@ -195,9 +218,9 @@ mp *mpmont_mexpr(mpmont *mm, mpmont_factor *f, size_t n) * Use: Convenient interface over @mpmont_mexpr@. */ -mp *mpmont_mexp(mpmont *mm, mpmont_factor *f, size_t n) +mp *mpmont_mexp(mpmont *mm, mp *d, mpmont_factor *f, size_t n) { - mp *d = mpmont_mexpr(mm, f, n); + d = mpmont_mexpr(mm, d, f, n); d = mpmont_reduce(mm, d, d); return (d); } @@ -225,7 +248,7 @@ static int verify(size_t n, dstr *v) rr = *(mp **)v[j].buf; mpmont_create(&mm, m); - r = mpmont_mexp(&mm, f, n); + r = mpmont_mexp(&mm, MP_NEW, f, n); if (MP_CMP(r, !=, rr)) { fputs("\n*** mexp failed\n", stderr); fputs("m = ", stderr); mp_writefile(m, stderr, 10); @@ -249,6 +272,7 @@ static int verify(size_t n, dstr *v) MP_DROP(r); MP_DROP(rr); mpmont_destroy(&mm); + assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); }