# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtommath / bn_mp_exptmod_fast.c
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2  *
3  * LibTomMath is library that provides for multiple-precision
4  * integer arithmetic as well as number theoretic functionality.
5  *
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.
9  *
10  * The library is free for all purposes without any express
11  * guarantee it works.
12  *
13  * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14  */
15 #include <tommath.h>
16
17 /* computes Y == G^X mod P, HAC pp.616, Algorithm 14.85
18  *
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.
21  *
22  * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
23  */
24 int
25 mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
26 {
27   mp_int  M[256], res;
28   mp_digit buf, mp;
29   int     err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
30   
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.  
34    */
35   int     (*redux)(mp_int*,mp_int*,mp_digit);
36
37   /* find window size */
38   x = mp_count_bits (X);
39   if (x <= 7) {
40     winsize = 2;
41   } else if (x <= 36) {
42     winsize = 3;
43   } else if (x <= 140) {
44     winsize = 4;
45   } else if (x <= 450) {
46     winsize = 5;
47   } else if (x <= 1303) {
48     winsize = 6;
49   } else if (x <= 3529) {
50     winsize = 7;
51   } else {
52     winsize = 8;
53   }
54
55 #ifdef MP_LOW_MEM
56   if (winsize > 5) {
57      winsize = 5;
58   }
59 #endif
60
61
62   /* init G array */
63   for (x = 0; x < (1 << winsize); x++) {
64     if ((err = mp_init (&M[x])) != MP_OKAY) {
65       for (y = 0; y < x; y++) {
66         mp_clear (&M[y]);
67       }
68       return err;
69     }
70   }
71
72   /* determine and setup reduction code */
73   if (redmode == 0) {
74      /* now setup montgomery  */
75      if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
76         goto __M;
77      }
78      
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;
83      } else {
84         /* use slower baselien method */
85         redux = mp_montgomery_reduce;
86      }
87   } else if (redmode == 1) {
88      /* setup DR reduction */
89      mp_dr_setup(P, &mp);
90      redux = mp_dr_reduce;
91   } else {
92      /* setup 2k reduction */
93      if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
94         goto __M;
95      }
96      redux = mp_reduce_2k;
97   }
98
99   /* setup result */
100   if ((err = mp_init (&res)) != MP_OKAY) {
101     goto __RES;
102   }
103
104   /* create M table
105    *
106    * The M table contains powers of the input base, e.g. M[x] = G^x mod P
107    *
108    * The first half of the table is not computed though accept for M[0] and M[1]
109    */
110
111   if (redmode == 0) {
112      /* now we need R mod m */
113      if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
114        goto __RES;
115      }
116
117      /* now set M[1] to G * R mod m */
118      if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
119        goto __RES;
120      }
121   } else {
122      mp_set(&res, 1);
123      if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
124         goto __RES;
125      }
126   }
127
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) {
130     goto __RES;
131   }
132
133   for (x = 0; x < (winsize - 1); x++) {
134     if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
135       goto __RES;
136     }
137     if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
138       goto __RES;
139     }
140   }
141
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) {
145       goto __RES;
146     }
147     if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
148       goto __RES;
149     }
150   }
151
152   /* set initial mode and bit cnt */
153   mode   = 0;
154   bitcnt = 1;
155   buf    = 0;
156   digidx = X->used - 1;
157   bitcpy = 0;
158   bitbuf = 0;
159
160   for (;;) {
161     /* grab next digit as required */
162     if (--bitcnt == 0) {
163       if (digidx == -1) {
164         break;
165       }
166       buf = X->dp[digidx--];
167       bitcnt = (int) DIGIT_BIT;
168     }
169
170     /* grab the next msb from the exponent */
171     y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
172     buf <<= (mp_digit)1;
173
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
178      */
179     if (mode == 0 && y == 0) {
180       continue;
181     }
182
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) {
186         goto __RES;
187       }
188       if ((err = redux (&res, P, mp)) != MP_OKAY) {
189         goto __RES;
190       }
191       continue;
192     }
193
194     /* else we add it to the window */
195     bitbuf |= (y << (winsize - ++bitcpy));
196     mode = 2;
197
198     if (bitcpy == winsize) {
199       /* ok window is filled so square as required and multiply  */
200       /* square first */
201       for (x = 0; x < winsize; x++) {
202         if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
203           goto __RES;
204         }
205         if ((err = redux (&res, P, mp)) != MP_OKAY) {
206           goto __RES;
207         }
208       }
209
210       /* then multiply */
211       if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
212         goto __RES;
213       }
214       if ((err = redux (&res, P, mp)) != MP_OKAY) {
215         goto __RES;
216       }
217
218       /* empty window and reset */
219       bitcpy = 0;
220       bitbuf = 0;
221       mode = 1;
222     }
223   }
224
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) {
230         goto __RES;
231       }
232       if ((err = redux (&res, P, mp)) != MP_OKAY) {
233         goto __RES;
234       }
235
236       bitbuf <<= 1;
237       if ((bitbuf & (1 << winsize)) != 0) {
238         /* then multiply */
239         if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
240           goto __RES;
241         }
242         if ((err = redux (&res, P, mp)) != MP_OKAY) {
243           goto __RES;
244         }
245       }
246     }
247   }
248
249   if (redmode == 0) {
250      /* fixup result if Montgomery reduction is used */
251      if ((err = mp_montgomery_reduce (&res, P, mp)) != MP_OKAY) {
252        goto __RES;
253      }
254   }
255
256   mp_exch (&res, Y);
257   err = MP_OKAY;
258 __RES:mp_clear (&res);
259 __M:
260   for (x = 0; x < (1 << winsize); x++) {
261     mp_clear (&M[x]);
262   }
263   return err;
264 }