2 #ifdef BN_S_MP_EXPTMOD_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
24 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
26 mp_int M[TAB_SIZE], res, mu;
28 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
29 int (*redux)(mp_int*,mp_int*,mp_int*);
31 /* find window size */
32 x = mp_count_bits (X);
37 } else if (x <= 140) {
39 } else if (x <= 450) {
41 } else if (x <= 1303) {
43 } else if (x <= 3529) {
57 if ((err = mp_init(&M[1])) != MP_OKAY) {
61 /* now init the second half of the array */
62 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
63 if ((err = mp_init(&M[x])) != MP_OKAY) {
64 for (y = 1<<(winsize-1); y < x; y++) {
72 /* create mu, used for Barrett reduction */
73 if ((err = mp_init (&mu)) != MP_OKAY) {
78 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
83 if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
86 redux = mp_reduce_2k_l;
91 * The M table contains powers of the base,
92 * e.g. M[x] = G**x mod P
94 * The first half of the table is not
95 * computed though accept for M[0] and M[1]
97 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
101 /* compute the value at M[1<<(winsize-1)] by squaring
102 * M[1] (winsize-1) times
104 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
108 for (x = 0; x < (winsize - 1); x++) {
110 if ((err = mp_sqr (&M[1 << (winsize - 1)],
111 &M[1 << (winsize - 1)])) != MP_OKAY) {
115 /* reduce modulo P */
116 if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
121 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
122 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
124 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
125 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
128 if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
134 if ((err = mp_init (&res)) != MP_OKAY) {
139 /* set initial mode and bit cnt */
143 digidx = X->used - 1;
148 /* grab next digit as required */
150 /* if digidx == -1 we are out of digits */
154 /* read next digit and reset the bitcnt */
155 buf = X->dp[digidx--];
156 bitcnt = (int) DIGIT_BIT;
159 /* grab the next msb from the exponent */
160 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
163 /* if the bit is zero and mode == 0 then we ignore it
164 * These represent the leading zero bits before the first 1 bit
165 * in the exponent. Technically this opt is not required but it
166 * does lower the # of trivial squaring/reductions used
168 if (mode == 0 && y == 0) {
172 /* if the bit is zero and mode == 1 then we square */
173 if (mode == 1 && y == 0) {
174 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
177 if ((err = redux (&res, P, &mu)) != MP_OKAY) {
183 /* else we add it to the window */
184 bitbuf |= (y << (winsize - ++bitcpy));
187 if (bitcpy == winsize) {
188 /* ok window is filled so square as required and multiply */
190 for (x = 0; x < winsize; x++) {
191 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
194 if ((err = redux (&res, P, &mu)) != MP_OKAY) {
200 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
203 if ((err = redux (&res, P, &mu)) != MP_OKAY) {
207 /* empty window and reset */
214 /* if bits remain then square/multiply */
215 if (mode == 2 && bitcpy > 0) {
216 /* square then multiply if the bit is set */
217 for (x = 0; x < bitcpy; x++) {
218 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
221 if ((err = redux (&res, P, &mu)) != MP_OKAY) {
226 if ((bitbuf & (1 << winsize)) != 0) {
228 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
231 if ((err = redux (&res, P, &mu)) != MP_OKAY) {
240 LBL_RES:mp_clear (&res);
241 LBL_MU:mp_clear (&mu);
244 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {