X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/30cbe7a7d521a9c1806caba10fba69f7335a9757..cdb7e56a986c87b1ce82c69c871f2bc6d0447eb8:/mpbarrett.c diff --git a/mpbarrett.c b/mpbarrett.c index 0af753e..b38aa91 100644 --- a/mpbarrett.c +++ b/mpbarrett.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpbarrett.c,v 1.6 2000/10/08 12:03:44 mdw Exp $ + * $Id: mpbarrett.c,v 1.8 2001/06/16 13:00:20 mdw Exp $ * * Barrett modular reduction * @@ -30,6 +30,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpbarrett.c,v $ + * 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. * @@ -58,6 +64,7 @@ #include "mp.h" #include "mpbarrett.h" +#include "mpbarrett-exp.h" /*----- Main code ---------------------------------------------------------*/ @@ -198,60 +205,18 @@ mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *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++; - } - } - /* --- 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); + MP_SHRINK(e); + if (!MP_LEN(e)) + ; + else if (MP_LEN(e) < EXP_THRESH) + EXP_SIMPLE(x, a, e); + else + EXP_WINDOW(x, a, e); + mp_drop(d); + mp_drop(spare); return (x); }