1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 * LibTomMath is library that provides for multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
6 * The library is designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
10 * The library is free for all purposes without any express
13 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
17 /* computes Y == G^X mod P, HAC pp.616, Algorithm 14.85
19 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
20 * The value of k changes based on the size of the exponent.
22 * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
25 mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
29 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
31 /* use a pointer to the reduction algorithm. This allows us to use
32 * one of many reduction algorithms without modding the guts of
33 * the code with if statements everywhere.
35 int (*redux)(mp_int*,mp_int*,mp_digit);
37 /* find window size */
38 x = mp_count_bits (X);
43 } else if (x <= 140) {
45 } else if (x <= 450) {
47 } else if (x <= 1303) {
49 } else if (x <= 3529) {
63 for (x = 0; x < (1 << winsize); x++) {
64 if ((err = mp_init (&M[x])) != MP_OKAY) {
65 for (y = 0; y < x; y++) {
72 /* determine and setup reduction code */
74 /* now setup montgomery */
75 if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
79 /* automatically pick the comba one if available (saves quite a few calls/ifs) */
80 if (((P->used * 2 + 1) < MP_WARRAY) &&
81 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
82 redux = fast_mp_montgomery_reduce;
84 /* use slower baselien method */
85 redux = mp_montgomery_reduce;
87 } else if (redmode == 1) {
88 /* setup DR reduction */
92 /* setup 2k reduction */
93 if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
100 if ((err = mp_init (&res)) != MP_OKAY) {
106 * The M table contains powers of the input base, e.g. M[x] = G^x mod P
108 * The first half of the table is not computed though accept for M[0] and M[1]
112 /* now we need R mod m */
113 if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
117 /* now set M[1] to G * R mod m */
118 if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
123 if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
128 /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
129 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
133 for (x = 0; x < (winsize - 1); x++) {
134 if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
137 if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
142 /* create upper table */
143 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
144 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
147 if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
152 /* set initial mode and bit cnt */
156 digidx = X->used - 1;
161 /* grab next digit as required */
166 buf = X->dp[digidx--];
167 bitcnt = (int) DIGIT_BIT;
170 /* grab the next msb from the exponent */
171 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
174 /* if the bit is zero and mode == 0 then we ignore it
175 * These represent the leading zero bits before the first 1 bit
176 * in the exponent. Technically this opt is not required but it
177 * does lower the # of trivial squaring/reductions used
179 if (mode == 0 && y == 0) {
183 /* if the bit is zero and mode == 1 then we square */
184 if (mode == 1 && y == 0) {
185 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
188 if ((err = redux (&res, P, mp)) != MP_OKAY) {
194 /* else we add it to the window */
195 bitbuf |= (y << (winsize - ++bitcpy));
198 if (bitcpy == winsize) {
199 /* ok window is filled so square as required and multiply */
201 for (x = 0; x < winsize; x++) {
202 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
205 if ((err = redux (&res, P, mp)) != MP_OKAY) {
211 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
214 if ((err = redux (&res, P, mp)) != MP_OKAY) {
218 /* empty window and reset */
225 /* if bits remain then square/multiply */
226 if (mode == 2 && bitcpy > 0) {
227 /* square then multiply if the bit is set */
228 for (x = 0; x < bitcpy; x++) {
229 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
232 if ((err = redux (&res, P, mp)) != MP_OKAY) {
237 if ((bitbuf & (1 << winsize)) != 0) {
239 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
242 if ((err = redux (&res, P, mp)) != MP_OKAY) {
250 /* fixup result if Montgomery reduction is used */
251 if ((err = mp_montgomery_reduce (&res, P, mp)) != MP_OKAY) {
258 __RES:mp_clear (&res);
260 for (x = 0; x < (1 << winsize); x++) {