/* -*-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
*
/*----- 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.
*
#include "mp.h"
#include "mpbarrett.h"
+#include "mpbarrett-exp.h"
/*----- Main code ---------------------------------------------------------*/
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);
}