From 419d33ed2f44bd5f00b0c78a81aaa1abc37f2056 Mon Sep 17 00:00:00 2001 From: mdw Date: Sat, 29 Jul 2000 17:04:33 +0000 Subject: [PATCH] Change to use left-to-right bitwise exponentiation. This will improve performance when the base is small. --- mpbarrett.c | 66 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/mpbarrett.c b/mpbarrett.c index aaa1bb7..affbc65 100644 --- a/mpbarrett.c +++ b/mpbarrett.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpbarrett.c,v 1.4 2000/06/17 11:45:09 mdw Exp $ + * $Id: mpbarrett.c,v 1.5 2000/07/29 17:04:33 mdw Exp $ * * Barrett modular reduction * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpbarrett.c,v $ + * 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. + * * 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 @@ -187,33 +191,53 @@ mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e) { mpscan sc; mp *x = MP_ONE; - mp *spare = MP_NEW; + mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; + unsigned sq = 0; a = MP_COPY(a); - mp_scan(&sc, e); - if (e->f & MP_BURN) - spare = MP_NEWSEC; - if (MP_STEP(&sc)) { - size_t sq = 0; + 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 (;;) { - mp *dd; - if (MP_BIT(&sc)) { - while (sq) { - dd = mp_sqr(spare, a); - dd = mpbarrett_reduce(mb, dd, dd); - spare = a; a = dd; - sq--; - } - dd = mp_mul(spare, x, a); - dd = mpbarrett_reduce(mb, dd, dd); - spare = x; x = dd; - } + if (!MP_RSTEP(&sc)) + goto done; + if (MP_RBIT(&sc)) + break; sq++; - if (!MP_STEP(&sc)) - break; } } + /* --- 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); -- 2.11.0