2 #ifdef BN_MP_EXPTMOD_FAST_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@gmail.com, http://math.libtomcrypt.com
18 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
20 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
21 * The value of k changes based on the size of the exponent.
23 * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
32 int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
34 mp_int M[TAB_SIZE], res;
36 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
38 /* use a pointer to the reduction algorithm. This allows us to use
39 * one of many reduction algorithms without modding the guts of
40 * the code with if statements everywhere.
42 int (*redux)(mp_int*,mp_int*,mp_digit);
44 /* find window size */
45 x = mp_count_bits (X);
50 } else if (x <= 140) {
52 } else if (x <= 450) {
54 } else if (x <= 1303) {
56 } else if (x <= 3529) {
70 if ((err = mp_init(&M[1])) != MP_OKAY) {
74 /* now init the second half of the array */
75 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
76 if ((err = mp_init(&M[x])) != MP_OKAY) {
77 for (y = 1<<(winsize-1); y < x; y++) {
85 /* determine and setup reduction code */
87 #ifdef BN_MP_MONTGOMERY_SETUP_C
88 /* now setup montgomery */
89 if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
97 /* automatically pick the comba one if available (saves quite a few calls/ifs) */
98 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
99 if (((P->used * 2 + 1) < MP_WARRAY) &&
100 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
101 redux = fast_mp_montgomery_reduce;
105 #ifdef BN_MP_MONTGOMERY_REDUCE_C
106 /* use slower baseline Montgomery method */
107 redux = mp_montgomery_reduce;
113 } else if (redmode == 1) {
114 #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
115 /* setup DR reduction for moduli of the form B**k - b */
117 redux = mp_dr_reduce;
123 #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
124 /* setup DR reduction for moduli of the form 2**k - b */
125 if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
128 redux = mp_reduce_2k;
136 if ((err = mp_init (&res)) != MP_OKAY) {
144 * The first half of the table is not computed though accept for M[0] and M[1]
148 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
149 /* now we need R mod m */
150 if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
158 /* now set M[1] to G * R mod m */
159 if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
164 if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
169 /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
170 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
174 for (x = 0; x < (winsize - 1); x++) {
175 if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
178 if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
183 /* create upper table */
184 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
185 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
188 if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
193 /* set initial mode and bit cnt */
197 digidx = X->used - 1;
202 /* grab next digit as required */
204 /* if digidx == -1 we are out of digits so break */
208 /* read next digit and reset bitcnt */
209 buf = X->dp[digidx--];
210 bitcnt = (int)DIGIT_BIT;
213 /* grab the next msb from the exponent */
214 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
217 /* if the bit is zero and mode == 0 then we ignore it
218 * These represent the leading zero bits before the first 1 bit
219 * in the exponent. Technically this opt is not required but it
220 * does lower the # of trivial squaring/reductions used
222 if (mode == 0 && y == 0) {
226 /* if the bit is zero and mode == 1 then we square */
227 if (mode == 1 && y == 0) {
228 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
231 if ((err = redux (&res, P, mp)) != MP_OKAY) {
237 /* else we add it to the window */
238 bitbuf |= (y << (winsize - ++bitcpy));
241 if (bitcpy == winsize) {
242 /* ok window is filled so square as required and multiply */
244 for (x = 0; x < winsize; x++) {
245 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
248 if ((err = redux (&res, P, mp)) != MP_OKAY) {
254 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
257 if ((err = redux (&res, P, mp)) != MP_OKAY) {
261 /* empty window and reset */
268 /* if bits remain then square/multiply */
269 if (mode == 2 && bitcpy > 0) {
270 /* square then multiply if the bit is set */
271 for (x = 0; x < bitcpy; x++) {
272 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
275 if ((err = redux (&res, P, mp)) != MP_OKAY) {
279 /* get next bit of the window */
281 if ((bitbuf & (1 << winsize)) != 0) {
283 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
286 if ((err = redux (&res, P, mp)) != MP_OKAY) {
294 /* fixup result if Montgomery reduction is used
295 * recall that any value in a Montgomery system is
296 * actually multiplied by R mod n. So we have
297 * to reduce one more time to cancel out the factor
300 if ((err = redux(&res, P, mp)) != MP_OKAY) {
305 /* swap res with Y */
308 LBL_RES:mp_clear (&res);
311 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
319 /* $Source: /cvs/libtom/libtommath/bn_mp_exptmod_fast.c,v $ */
320 /* $Revision: 1.3 $ */
321 /* $Date: 2006/03/31 14:18:44 $ */