2004-10-29 22:07:18 +00:00
|
|
|
#include <tommath.h>
|
|
|
|
#ifdef BN_S_MP_EXPTMOD_C
|
2003-07-02 15:39:39 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-07-02 15:39:39 +00:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-07-02 15:39:39 +00:00
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2015-10-30 21:55:29 +00:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
2003-07-02 15:39:39 +00:00
|
|
|
*/
|
|
|
|
#ifdef MP_LOW_MEM
|
|
|
|
#define TAB_SIZE 32
|
|
|
|
#else
|
|
|
|
#define TAB_SIZE 256
|
|
|
|
#endif
|
|
|
|
|
2005-02-12 08:40:15 +00:00
|
|
|
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
2003-07-02 15:39:39 +00:00
|
|
|
{
|
|
|
|
mp_int M[TAB_SIZE], res, mu;
|
|
|
|
mp_digit buf;
|
|
|
|
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
|
2005-02-12 08:40:15 +00:00
|
|
|
int (*redux)(mp_int*,mp_int*,mp_int*);
|
2003-07-02 15:39:39 +00:00
|
|
|
|
|
|
|
/* find window size */
|
|
|
|
x = mp_count_bits (X);
|
|
|
|
if (x <= 7) {
|
|
|
|
winsize = 2;
|
|
|
|
} else if (x <= 36) {
|
|
|
|
winsize = 3;
|
|
|
|
} else if (x <= 140) {
|
|
|
|
winsize = 4;
|
|
|
|
} else if (x <= 450) {
|
|
|
|
winsize = 5;
|
|
|
|
} else if (x <= 1303) {
|
|
|
|
winsize = 6;
|
|
|
|
} else if (x <= 3529) {
|
|
|
|
winsize = 7;
|
|
|
|
} else {
|
|
|
|
winsize = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MP_LOW_MEM
|
|
|
|
if (winsize > 5) {
|
|
|
|
winsize = 5;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* init M array */
|
|
|
|
/* init first cell */
|
|
|
|
if ((err = mp_init(&M[1])) != MP_OKAY) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now init the second half of the array */
|
|
|
|
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
|
|
|
if ((err = mp_init(&M[x])) != MP_OKAY) {
|
|
|
|
for (y = 1<<(winsize-1); y < x; y++) {
|
|
|
|
mp_clear (&M[y]);
|
|
|
|
}
|
|
|
|
mp_clear(&M[1]);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create mu, used for Barrett reduction */
|
|
|
|
if ((err = mp_init (&mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_M;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
|
|
|
|
if (redmode == 0) {
|
|
|
|
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
|
|
|
goto LBL_MU;
|
|
|
|
}
|
|
|
|
redux = mp_reduce;
|
|
|
|
} else {
|
|
|
|
if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
|
|
|
|
goto LBL_MU;
|
|
|
|
}
|
|
|
|
redux = mp_reduce_2k_l;
|
|
|
|
}
|
2003-07-02 15:39:39 +00:00
|
|
|
|
|
|
|
/* create M table
|
|
|
|
*
|
|
|
|
* The M table contains powers of the base,
|
|
|
|
* e.g. M[x] = G**x mod P
|
|
|
|
*
|
|
|
|
* The first half of the table is not
|
|
|
|
* computed though accept for M[0] and M[1]
|
|
|
|
*/
|
|
|
|
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the value at M[1<<(winsize-1)] by squaring
|
|
|
|
* M[1] (winsize-1) times
|
|
|
|
*/
|
|
|
|
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < (winsize - 1); x++) {
|
2005-02-12 08:40:15 +00:00
|
|
|
/* square it */
|
2003-07-02 15:39:39 +00:00
|
|
|
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
|
|
|
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
|
|
|
|
/* reduce modulo P */
|
|
|
|
if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 01:24:44 +00:00
|
|
|
/* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
|
|
|
|
* for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
|
|
|
|
*/
|
2003-07-02 15:39:39 +00:00
|
|
|
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
|
|
|
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup result */
|
|
|
|
if ((err = mp_init (&res)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_MU;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
mp_set (&res, 1);
|
|
|
|
|
|
|
|
/* set initial mode and bit cnt */
|
|
|
|
mode = 0;
|
|
|
|
bitcnt = 1;
|
|
|
|
buf = 0;
|
|
|
|
digidx = X->used - 1;
|
|
|
|
bitcpy = 0;
|
|
|
|
bitbuf = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* grab next digit as required */
|
|
|
|
if (--bitcnt == 0) {
|
2003-08-05 01:24:44 +00:00
|
|
|
/* if digidx == -1 we are out of digits */
|
2003-07-02 15:39:39 +00:00
|
|
|
if (digidx == -1) {
|
|
|
|
break;
|
|
|
|
}
|
2003-08-05 01:24:44 +00:00
|
|
|
/* read next digit and reset the bitcnt */
|
|
|
|
buf = X->dp[digidx--];
|
2003-07-02 15:39:39 +00:00
|
|
|
bitcnt = (int) DIGIT_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grab the next msb from the exponent */
|
2003-08-05 01:24:44 +00:00
|
|
|
y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
|
2003-07-02 15:39:39 +00:00
|
|
|
buf <<= (mp_digit)1;
|
|
|
|
|
|
|
|
/* if the bit is zero and mode == 0 then we ignore it
|
|
|
|
* These represent the leading zero bits before the first 1 bit
|
|
|
|
* in the exponent. Technically this opt is not required but it
|
|
|
|
* does lower the # of trivial squaring/reductions used
|
|
|
|
*/
|
2003-08-05 01:24:44 +00:00
|
|
|
if (mode == 0 && y == 0) {
|
2003-07-02 15:39:39 +00:00
|
|
|
continue;
|
2003-08-05 01:24:44 +00:00
|
|
|
}
|
2003-07-02 15:39:39 +00:00
|
|
|
|
|
|
|
/* if the bit is zero and mode == 1 then we square */
|
|
|
|
if (mode == 1 && y == 0) {
|
|
|
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else we add it to the window */
|
|
|
|
bitbuf |= (y << (winsize - ++bitcpy));
|
2003-08-05 01:24:44 +00:00
|
|
|
mode = 2;
|
2003-07-02 15:39:39 +00:00
|
|
|
|
|
|
|
if (bitcpy == winsize) {
|
|
|
|
/* ok window is filled so square as required and multiply */
|
|
|
|
/* square first */
|
|
|
|
for (x = 0; x < winsize; x++) {
|
|
|
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then multiply */
|
|
|
|
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* empty window and reset */
|
|
|
|
bitcpy = 0;
|
|
|
|
bitbuf = 0;
|
2003-08-05 01:24:44 +00:00
|
|
|
mode = 1;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if bits remain then square/multiply */
|
|
|
|
if (mode == 2 && bitcpy > 0) {
|
|
|
|
/* square then multiply if the bit is set */
|
|
|
|
for (x = 0; x < bitcpy; x++) {
|
|
|
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bitbuf <<= 1;
|
|
|
|
if ((bitbuf & (1 << winsize)) != 0) {
|
|
|
|
/* then multiply */
|
|
|
|
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2005-02-12 08:40:15 +00:00
|
|
|
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_RES;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_exch (&res, Y);
|
|
|
|
err = MP_OKAY;
|
2004-12-23 02:40:37 +00:00
|
|
|
LBL_RES:mp_clear (&res);
|
|
|
|
LBL_MU:mp_clear (&mu);
|
|
|
|
LBL_M:
|
2003-07-02 15:39:39 +00:00
|
|
|
mp_clear(&M[1]);
|
|
|
|
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
|
|
|
mp_clear (&M[x]);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|
2005-08-01 16:37:28 +00:00
|
|
|
|
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|