142
|
1 #include <tommath.h> |
|
2 #ifdef BN_S_MP_EXPTMOD_C |
2
|
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4 * |
|
5 * LibTomMath is a library that provides multiple-precision |
|
6 * integer arithmetic as well as number theoretic functionality. |
|
7 * |
|
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. |
|
11 * |
|
12 * The library is free for all purposes without any express |
|
13 * guarantee it works. |
|
14 * |
|
15 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
16 */ |
|
17 |
|
18 #ifdef MP_LOW_MEM |
|
19 #define TAB_SIZE 32 |
|
20 #else |
|
21 #define TAB_SIZE 256 |
|
22 #endif |
|
23 |
|
24 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) |
|
25 { |
|
26 mp_int M[TAB_SIZE], res, mu; |
|
27 mp_digit buf; |
|
28 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; |
|
29 |
|
30 /* find window size */ |
|
31 x = mp_count_bits (X); |
|
32 if (x <= 7) { |
|
33 winsize = 2; |
|
34 } else if (x <= 36) { |
|
35 winsize = 3; |
|
36 } else if (x <= 140) { |
|
37 winsize = 4; |
|
38 } else if (x <= 450) { |
|
39 winsize = 5; |
|
40 } else if (x <= 1303) { |
|
41 winsize = 6; |
|
42 } else if (x <= 3529) { |
|
43 winsize = 7; |
|
44 } else { |
|
45 winsize = 8; |
|
46 } |
|
47 |
|
48 #ifdef MP_LOW_MEM |
|
49 if (winsize > 5) { |
|
50 winsize = 5; |
|
51 } |
|
52 #endif |
|
53 |
|
54 /* init M array */ |
|
55 /* init first cell */ |
|
56 if ((err = mp_init(&M[1])) != MP_OKAY) { |
|
57 return err; |
|
58 } |
|
59 |
|
60 /* now init the second half of the array */ |
|
61 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
|
62 if ((err = mp_init(&M[x])) != MP_OKAY) { |
|
63 for (y = 1<<(winsize-1); y < x; y++) { |
|
64 mp_clear (&M[y]); |
|
65 } |
|
66 mp_clear(&M[1]); |
|
67 return err; |
|
68 } |
|
69 } |
|
70 |
|
71 /* create mu, used for Barrett reduction */ |
|
72 if ((err = mp_init (&mu)) != MP_OKAY) { |
|
73 goto __M; |
|
74 } |
|
75 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { |
|
76 goto __MU; |
|
77 } |
|
78 |
|
79 /* create M table |
|
80 * |
|
81 * The M table contains powers of the base, |
|
82 * e.g. M[x] = G**x mod P |
|
83 * |
|
84 * The first half of the table is not |
|
85 * computed though accept for M[0] and M[1] |
|
86 */ |
|
87 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { |
|
88 goto __MU; |
|
89 } |
|
90 |
|
91 /* compute the value at M[1<<(winsize-1)] by squaring |
|
92 * M[1] (winsize-1) times |
|
93 */ |
|
94 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |
|
95 goto __MU; |
|
96 } |
|
97 |
|
98 for (x = 0; x < (winsize - 1); x++) { |
|
99 if ((err = mp_sqr (&M[1 << (winsize - 1)], |
|
100 &M[1 << (winsize - 1)])) != MP_OKAY) { |
|
101 goto __MU; |
|
102 } |
|
103 if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) { |
|
104 goto __MU; |
|
105 } |
|
106 } |
|
107 |
|
108 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P) |
|
109 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1) |
|
110 */ |
|
111 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { |
|
112 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { |
|
113 goto __MU; |
|
114 } |
|
115 if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) { |
|
116 goto __MU; |
|
117 } |
|
118 } |
|
119 |
|
120 /* setup result */ |
|
121 if ((err = mp_init (&res)) != MP_OKAY) { |
|
122 goto __MU; |
|
123 } |
|
124 mp_set (&res, 1); |
|
125 |
|
126 /* set initial mode and bit cnt */ |
|
127 mode = 0; |
|
128 bitcnt = 1; |
|
129 buf = 0; |
|
130 digidx = X->used - 1; |
|
131 bitcpy = 0; |
|
132 bitbuf = 0; |
|
133 |
|
134 for (;;) { |
|
135 /* grab next digit as required */ |
|
136 if (--bitcnt == 0) { |
|
137 /* if digidx == -1 we are out of digits */ |
|
138 if (digidx == -1) { |
|
139 break; |
|
140 } |
|
141 /* read next digit and reset the bitcnt */ |
|
142 buf = X->dp[digidx--]; |
|
143 bitcnt = (int) DIGIT_BIT; |
|
144 } |
|
145 |
|
146 /* grab the next msb from the exponent */ |
|
147 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1; |
|
148 buf <<= (mp_digit)1; |
|
149 |
|
150 /* if the bit is zero and mode == 0 then we ignore it |
|
151 * These represent the leading zero bits before the first 1 bit |
|
152 * in the exponent. Technically this opt is not required but it |
|
153 * does lower the # of trivial squaring/reductions used |
|
154 */ |
|
155 if (mode == 0 && y == 0) { |
|
156 continue; |
|
157 } |
|
158 |
|
159 /* if the bit is zero and mode == 1 then we square */ |
|
160 if (mode == 1 && y == 0) { |
|
161 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
162 goto __RES; |
|
163 } |
|
164 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
165 goto __RES; |
|
166 } |
|
167 continue; |
|
168 } |
|
169 |
|
170 /* else we add it to the window */ |
|
171 bitbuf |= (y << (winsize - ++bitcpy)); |
|
172 mode = 2; |
|
173 |
|
174 if (bitcpy == winsize) { |
|
175 /* ok window is filled so square as required and multiply */ |
|
176 /* square first */ |
|
177 for (x = 0; x < winsize; x++) { |
|
178 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
179 goto __RES; |
|
180 } |
|
181 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
182 goto __RES; |
|
183 } |
|
184 } |
|
185 |
|
186 /* then multiply */ |
|
187 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { |
|
188 goto __RES; |
|
189 } |
|
190 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
191 goto __RES; |
|
192 } |
|
193 |
|
194 /* empty window and reset */ |
|
195 bitcpy = 0; |
|
196 bitbuf = 0; |
|
197 mode = 1; |
|
198 } |
|
199 } |
|
200 |
|
201 /* if bits remain then square/multiply */ |
|
202 if (mode == 2 && bitcpy > 0) { |
|
203 /* square then multiply if the bit is set */ |
|
204 for (x = 0; x < bitcpy; x++) { |
|
205 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
206 goto __RES; |
|
207 } |
|
208 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
209 goto __RES; |
|
210 } |
|
211 |
|
212 bitbuf <<= 1; |
|
213 if ((bitbuf & (1 << winsize)) != 0) { |
|
214 /* then multiply */ |
|
215 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { |
|
216 goto __RES; |
|
217 } |
|
218 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
219 goto __RES; |
|
220 } |
|
221 } |
|
222 } |
|
223 } |
|
224 |
|
225 mp_exch (&res, Y); |
|
226 err = MP_OKAY; |
|
227 __RES:mp_clear (&res); |
|
228 __MU:mp_clear (&mu); |
|
229 __M: |
|
230 mp_clear(&M[1]); |
|
231 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
|
232 mp_clear (&M[x]); |
|
233 } |
|
234 return err; |
|
235 } |
142
|
236 #endif |