3
|
1 /* Start: bn_error.c */ |
143
|
2 #include <ltc_tommath.h> |
|
3 #ifdef BN_ERROR_C |
|
4 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5 * |
|
6 * LibTomMath is a library that provides multiple-precision |
|
7 * integer arithmetic as well as number theoretic functionality. |
|
8 * |
|
9 * The library was designed directly after the MPI library by |
|
10 * Michael Fromberger but has been written from scratch with |
|
11 * additional optimizations in place. |
|
12 * |
|
13 * The library is free for all purposes without any express |
|
14 * guarantee it works. |
|
15 * |
|
16 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
17 */ |
3
|
18 |
|
19 static const struct { |
|
20 int code; |
|
21 char *msg; |
|
22 } msgs[] = { |
|
23 { MP_OKAY, "Successful" }, |
|
24 { MP_MEM, "Out of heap" }, |
|
25 { MP_VAL, "Value out of range" } |
|
26 }; |
|
27 |
|
28 /* return a char * string for a given code */ |
|
29 char *mp_error_to_string(int code) |
|
30 { |
|
31 int x; |
|
32 |
|
33 /* scan the lookup table for the given message */ |
|
34 for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) { |
|
35 if (msgs[x].code == code) { |
|
36 return msgs[x].msg; |
|
37 } |
|
38 } |
|
39 |
|
40 /* generic reply for invalid code */ |
|
41 return "Invalid error code"; |
|
42 } |
|
43 |
143
|
44 #endif |
3
|
45 |
|
46 /* End: bn_error.c */ |
|
47 |
|
48 /* Start: bn_fast_mp_invmod.c */ |
143
|
49 #include <ltc_tommath.h> |
|
50 #ifdef BN_FAST_MP_INVMOD_C |
|
51 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
52 * |
|
53 * LibTomMath is a library that provides multiple-precision |
|
54 * integer arithmetic as well as number theoretic functionality. |
|
55 * |
|
56 * The library was designed directly after the MPI library by |
|
57 * Michael Fromberger but has been written from scratch with |
|
58 * additional optimizations in place. |
|
59 * |
|
60 * The library is free for all purposes without any express |
|
61 * guarantee it works. |
|
62 * |
|
63 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
64 */ |
3
|
65 |
|
66 /* computes the modular inverse via binary extended euclidean algorithm, |
|
67 * that is c = 1/a mod b |
|
68 * |
143
|
69 * Based on slow invmod except this is optimized for the case where b is |
3
|
70 * odd as per HAC Note 14.64 on pp. 610 |
|
71 */ |
|
72 int |
|
73 fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) |
|
74 { |
|
75 mp_int x, y, u, v, B, D; |
|
76 int res, neg; |
|
77 |
|
78 /* 2. [modified] b must be odd */ |
|
79 if (mp_iseven (b) == 1) { |
|
80 return MP_VAL; |
|
81 } |
|
82 |
|
83 /* init all our temps */ |
|
84 if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) { |
|
85 return res; |
|
86 } |
|
87 |
|
88 /* x == modulus, y == value to invert */ |
|
89 if ((res = mp_copy (b, &x)) != MP_OKAY) { |
|
90 goto __ERR; |
|
91 } |
|
92 |
|
93 /* we need y = |a| */ |
|
94 if ((res = mp_abs (a, &y)) != MP_OKAY) { |
|
95 goto __ERR; |
|
96 } |
|
97 |
|
98 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ |
|
99 if ((res = mp_copy (&x, &u)) != MP_OKAY) { |
|
100 goto __ERR; |
|
101 } |
|
102 if ((res = mp_copy (&y, &v)) != MP_OKAY) { |
|
103 goto __ERR; |
|
104 } |
|
105 mp_set (&D, 1); |
|
106 |
|
107 top: |
|
108 /* 4. while u is even do */ |
|
109 while (mp_iseven (&u) == 1) { |
|
110 /* 4.1 u = u/2 */ |
|
111 if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { |
|
112 goto __ERR; |
|
113 } |
|
114 /* 4.2 if B is odd then */ |
|
115 if (mp_isodd (&B) == 1) { |
|
116 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { |
|
117 goto __ERR; |
|
118 } |
|
119 } |
|
120 /* B = B/2 */ |
|
121 if ((res = mp_div_2 (&B, &B)) != MP_OKAY) { |
|
122 goto __ERR; |
|
123 } |
|
124 } |
|
125 |
|
126 /* 5. while v is even do */ |
|
127 while (mp_iseven (&v) == 1) { |
|
128 /* 5.1 v = v/2 */ |
|
129 if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { |
|
130 goto __ERR; |
|
131 } |
|
132 /* 5.2 if D is odd then */ |
|
133 if (mp_isodd (&D) == 1) { |
|
134 /* D = (D-x)/2 */ |
|
135 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { |
|
136 goto __ERR; |
|
137 } |
|
138 } |
|
139 /* D = D/2 */ |
|
140 if ((res = mp_div_2 (&D, &D)) != MP_OKAY) { |
|
141 goto __ERR; |
|
142 } |
|
143 } |
|
144 |
|
145 /* 6. if u >= v then */ |
|
146 if (mp_cmp (&u, &v) != MP_LT) { |
|
147 /* u = u - v, B = B - D */ |
|
148 if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) { |
|
149 goto __ERR; |
|
150 } |
|
151 |
|
152 if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) { |
|
153 goto __ERR; |
|
154 } |
|
155 } else { |
|
156 /* v - v - u, D = D - B */ |
|
157 if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) { |
|
158 goto __ERR; |
|
159 } |
|
160 |
|
161 if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) { |
|
162 goto __ERR; |
|
163 } |
|
164 } |
|
165 |
|
166 /* if not zero goto step 4 */ |
|
167 if (mp_iszero (&u) == 0) { |
|
168 goto top; |
|
169 } |
|
170 |
|
171 /* now a = C, b = D, gcd == g*v */ |
|
172 |
|
173 /* if v != 1 then there is no inverse */ |
|
174 if (mp_cmp_d (&v, 1) != MP_EQ) { |
|
175 res = MP_VAL; |
|
176 goto __ERR; |
|
177 } |
|
178 |
|
179 /* b is now the inverse */ |
|
180 neg = a->sign; |
|
181 while (D.sign == MP_NEG) { |
|
182 if ((res = mp_add (&D, b, &D)) != MP_OKAY) { |
|
183 goto __ERR; |
|
184 } |
|
185 } |
|
186 mp_exch (&D, c); |
|
187 c->sign = neg; |
|
188 res = MP_OKAY; |
|
189 |
|
190 __ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); |
|
191 return res; |
|
192 } |
143
|
193 #endif |
3
|
194 |
|
195 /* End: bn_fast_mp_invmod.c */ |
|
196 |
|
197 /* Start: bn_fast_mp_montgomery_reduce.c */ |
143
|
198 #include <ltc_tommath.h> |
|
199 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C |
|
200 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
201 * |
|
202 * LibTomMath is a library that provides multiple-precision |
|
203 * integer arithmetic as well as number theoretic functionality. |
|
204 * |
|
205 * The library was designed directly after the MPI library by |
|
206 * Michael Fromberger but has been written from scratch with |
|
207 * additional optimizations in place. |
|
208 * |
|
209 * The library is free for all purposes without any express |
|
210 * guarantee it works. |
|
211 * |
|
212 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
213 */ |
3
|
214 |
|
215 /* computes xR**-1 == x (mod N) via Montgomery Reduction |
|
216 * |
143
|
217 * This is an optimized implementation of montgomery_reduce |
3
|
218 * which uses the comba method to quickly calculate the columns of the |
|
219 * reduction. |
|
220 * |
|
221 * Based on Algorithm 14.32 on pp.601 of HAC. |
|
222 */ |
|
223 int |
|
224 fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) |
|
225 { |
|
226 int ix, res, olduse; |
|
227 mp_word W[MP_WARRAY]; |
|
228 |
|
229 /* get old used count */ |
|
230 olduse = x->used; |
|
231 |
|
232 /* grow a as required */ |
|
233 if (x->alloc < n->used + 1) { |
|
234 if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) { |
|
235 return res; |
|
236 } |
|
237 } |
|
238 |
|
239 /* first we have to get the digits of the input into |
|
240 * an array of double precision words W[...] |
|
241 */ |
|
242 { |
|
243 register mp_word *_W; |
|
244 register mp_digit *tmpx; |
|
245 |
|
246 /* alias for the W[] array */ |
|
247 _W = W; |
|
248 |
|
249 /* alias for the digits of x*/ |
|
250 tmpx = x->dp; |
|
251 |
|
252 /* copy the digits of a into W[0..a->used-1] */ |
|
253 for (ix = 0; ix < x->used; ix++) { |
|
254 *_W++ = *tmpx++; |
|
255 } |
|
256 |
|
257 /* zero the high words of W[a->used..m->used*2] */ |
|
258 for (; ix < n->used * 2 + 1; ix++) { |
|
259 *_W++ = 0; |
|
260 } |
|
261 } |
|
262 |
|
263 /* now we proceed to zero successive digits |
|
264 * from the least significant upwards |
|
265 */ |
|
266 for (ix = 0; ix < n->used; ix++) { |
|
267 /* mu = ai * m' mod b |
|
268 * |
|
269 * We avoid a double precision multiplication (which isn't required) |
|
270 * by casting the value down to a mp_digit. Note this requires |
|
271 * that W[ix-1] have the carry cleared (see after the inner loop) |
|
272 */ |
|
273 register mp_digit mu; |
|
274 mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK); |
|
275 |
|
276 /* a = a + mu * m * b**i |
|
277 * |
|
278 * This is computed in place and on the fly. The multiplication |
|
279 * by b**i is handled by offseting which columns the results |
|
280 * are added to. |
|
281 * |
|
282 * Note the comba method normally doesn't handle carries in the |
|
283 * inner loop In this case we fix the carry from the previous |
|
284 * column since the Montgomery reduction requires digits of the |
|
285 * result (so far) [see above] to work. This is |
|
286 * handled by fixing up one carry after the inner loop. The |
|
287 * carry fixups are done in order so after these loops the |
|
288 * first m->used words of W[] have the carries fixed |
|
289 */ |
|
290 { |
|
291 register int iy; |
|
292 register mp_digit *tmpn; |
|
293 register mp_word *_W; |
|
294 |
|
295 /* alias for the digits of the modulus */ |
|
296 tmpn = n->dp; |
|
297 |
|
298 /* Alias for the columns set by an offset of ix */ |
|
299 _W = W + ix; |
|
300 |
|
301 /* inner loop */ |
|
302 for (iy = 0; iy < n->used; iy++) { |
|
303 *_W++ += ((mp_word)mu) * ((mp_word)*tmpn++); |
|
304 } |
|
305 } |
|
306 |
|
307 /* now fix carry for next digit, W[ix+1] */ |
|
308 W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT); |
|
309 } |
|
310 |
|
311 /* now we have to propagate the carries and |
|
312 * shift the words downward [all those least |
|
313 * significant digits we zeroed]. |
|
314 */ |
|
315 { |
|
316 register mp_digit *tmpx; |
|
317 register mp_word *_W, *_W1; |
|
318 |
|
319 /* nox fix rest of carries */ |
|
320 |
|
321 /* alias for current word */ |
|
322 _W1 = W + ix; |
|
323 |
|
324 /* alias for next word, where the carry goes */ |
|
325 _W = W + ++ix; |
|
326 |
|
327 for (; ix <= n->used * 2 + 1; ix++) { |
|
328 *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT); |
|
329 } |
|
330 |
|
331 /* copy out, A = A/b**n |
|
332 * |
|
333 * The result is A/b**n but instead of converting from an |
|
334 * array of mp_word to mp_digit than calling mp_rshd |
|
335 * we just copy them in the right order |
|
336 */ |
|
337 |
|
338 /* alias for destination word */ |
|
339 tmpx = x->dp; |
|
340 |
|
341 /* alias for shifted double precision result */ |
|
342 _W = W + n->used; |
|
343 |
|
344 for (ix = 0; ix < n->used + 1; ix++) { |
|
345 *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK)); |
|
346 } |
|
347 |
|
348 /* zero oldused digits, if the input a was larger than |
|
349 * m->used+1 we'll have to clear the digits |
|
350 */ |
|
351 for (; ix < olduse; ix++) { |
|
352 *tmpx++ = 0; |
|
353 } |
|
354 } |
|
355 |
|
356 /* set the max used and clamp */ |
|
357 x->used = n->used + 1; |
|
358 mp_clamp (x); |
|
359 |
|
360 /* if A >= m then A = A - m */ |
|
361 if (mp_cmp_mag (x, n) != MP_LT) { |
|
362 return s_mp_sub (x, n, x); |
|
363 } |
|
364 return MP_OKAY; |
|
365 } |
143
|
366 #endif |
3
|
367 |
|
368 /* End: bn_fast_mp_montgomery_reduce.c */ |
|
369 |
|
370 /* Start: bn_fast_s_mp_mul_digs.c */ |
143
|
371 #include <ltc_tommath.h> |
|
372 #ifdef BN_FAST_S_MP_MUL_DIGS_C |
|
373 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
374 * |
|
375 * LibTomMath is a library that provides multiple-precision |
|
376 * integer arithmetic as well as number theoretic functionality. |
|
377 * |
|
378 * The library was designed directly after the MPI library by |
|
379 * Michael Fromberger but has been written from scratch with |
|
380 * additional optimizations in place. |
|
381 * |
|
382 * The library is free for all purposes without any express |
|
383 * guarantee it works. |
|
384 * |
|
385 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
386 */ |
3
|
387 |
|
388 /* Fast (comba) multiplier |
|
389 * |
|
390 * This is the fast column-array [comba] multiplier. It is |
|
391 * designed to compute the columns of the product first |
|
392 * then handle the carries afterwards. This has the effect |
|
393 * of making the nested loops that compute the columns very |
|
394 * simple and schedulable on super-scalar processors. |
|
395 * |
|
396 * This has been modified to produce a variable number of |
|
397 * digits of output so if say only a half-product is required |
|
398 * you don't have to compute the upper half (a feature |
|
399 * required for fast Barrett reduction). |
|
400 * |
|
401 * Based on Algorithm 14.12 on pp.595 of HAC. |
|
402 * |
|
403 */ |
|
404 int |
|
405 fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
406 { |
143
|
407 int olduse, res, pa, ix, iz; |
|
408 mp_digit W[MP_WARRAY]; |
|
409 register mp_word _W; |
3
|
410 |
|
411 /* grow the destination as required */ |
|
412 if (c->alloc < digs) { |
|
413 if ((res = mp_grow (c, digs)) != MP_OKAY) { |
|
414 return res; |
|
415 } |
|
416 } |
|
417 |
143
|
418 /* number of output digits to produce */ |
|
419 pa = MIN(digs, a->used + b->used); |
|
420 |
|
421 /* clear the carry */ |
|
422 _W = 0; |
|
423 for (ix = 0; ix <= pa; ix++) { |
|
424 int tx, ty; |
|
425 int iy; |
|
426 mp_digit *tmpx, *tmpy; |
|
427 |
|
428 /* get offsets into the two bignums */ |
|
429 ty = MIN(b->used-1, ix); |
|
430 tx = ix - ty; |
|
431 |
|
432 /* setup temp aliases */ |
|
433 tmpx = a->dp + tx; |
|
434 tmpy = b->dp + ty; |
|
435 |
|
436 /* this is the number of times the loop will iterrate, essentially its |
|
437 while (tx++ < a->used && ty-- >= 0) { ... } |
3
|
438 */ |
143
|
439 iy = MIN(a->used-tx, ty+1); |
|
440 |
|
441 /* execute loop */ |
|
442 for (iz = 0; iz < iy; ++iz) { |
|
443 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
3
|
444 } |
143
|
445 |
|
446 /* store term */ |
|
447 W[ix] = ((mp_digit)_W) & MP_MASK; |
|
448 |
|
449 /* make next carry */ |
|
450 _W = _W >> ((mp_word)DIGIT_BIT); |
3
|
451 } |
|
452 |
|
453 /* setup dest */ |
15
|
454 olduse = c->used; |
3
|
455 c->used = digs; |
|
456 |
|
457 { |
|
458 register mp_digit *tmpc; |
|
459 tmpc = c->dp; |
143
|
460 for (ix = 0; ix < digs; ix++) { |
3
|
461 /* now extract the previous digit [below the carry] */ |
143
|
462 *tmpc++ = W[ix]; |
|
463 } |
3
|
464 |
|
465 /* clear unused digits [that existed in the old copy of c] */ |
|
466 for (; ix < olduse; ix++) { |
|
467 *tmpc++ = 0; |
|
468 } |
|
469 } |
|
470 mp_clamp (c); |
|
471 return MP_OKAY; |
|
472 } |
143
|
473 #endif |
3
|
474 |
|
475 /* End: bn_fast_s_mp_mul_digs.c */ |
|
476 |
|
477 /* Start: bn_fast_s_mp_mul_high_digs.c */ |
143
|
478 #include <ltc_tommath.h> |
|
479 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C |
|
480 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
481 * |
|
482 * LibTomMath is a library that provides multiple-precision |
|
483 * integer arithmetic as well as number theoretic functionality. |
|
484 * |
|
485 * The library was designed directly after the MPI library by |
|
486 * Michael Fromberger but has been written from scratch with |
|
487 * additional optimizations in place. |
|
488 * |
|
489 * The library is free for all purposes without any express |
|
490 * guarantee it works. |
|
491 * |
|
492 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
493 */ |
|
494 |
|
495 /* this is a modified version of fast_s_mul_digs that only produces |
|
496 * output digits *above* digs. See the comments for fast_s_mul_digs |
3
|
497 * to see how it works. |
|
498 * |
|
499 * This is used in the Barrett reduction since for one of the multiplications |
|
500 * only the higher digits were needed. This essentially halves the work. |
|
501 * |
|
502 * Based on Algorithm 14.12 on pp.595 of HAC. |
|
503 */ |
|
504 int |
|
505 fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
506 { |
143
|
507 int olduse, res, pa, ix, iz; |
|
508 mp_digit W[MP_WARRAY]; |
|
509 mp_word _W; |
|
510 |
|
511 /* grow the destination as required */ |
|
512 pa = a->used + b->used; |
|
513 if (c->alloc < pa) { |
|
514 if ((res = mp_grow (c, pa)) != MP_OKAY) { |
3
|
515 return res; |
|
516 } |
|
517 } |
|
518 |
143
|
519 /* number of output digits to produce */ |
|
520 pa = a->used + b->used; |
|
521 _W = 0; |
|
522 for (ix = digs; ix <= pa; ix++) { |
|
523 int tx, ty, iy; |
|
524 mp_digit *tmpx, *tmpy; |
|
525 |
|
526 /* get offsets into the two bignums */ |
|
527 ty = MIN(b->used-1, ix); |
|
528 tx = ix - ty; |
|
529 |
|
530 /* setup temp aliases */ |
|
531 tmpx = a->dp + tx; |
|
532 tmpy = b->dp + ty; |
|
533 |
|
534 /* this is the number of times the loop will iterrate, essentially its |
|
535 while (tx++ < a->used && ty-- >= 0) { ... } |
3
|
536 */ |
143
|
537 iy = MIN(a->used-tx, ty+1); |
|
538 |
|
539 /* execute loop */ |
|
540 for (iz = 0; iz < iy; iz++) { |
|
541 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
3
|
542 } |
|
543 |
143
|
544 /* store term */ |
|
545 W[ix] = ((mp_digit)_W) & MP_MASK; |
|
546 |
|
547 /* make next carry */ |
|
548 _W = _W >> ((mp_word)DIGIT_BIT); |
3
|
549 } |
|
550 |
|
551 /* setup dest */ |
143
|
552 olduse = c->used; |
|
553 c->used = pa; |
|
554 |
|
555 { |
|
556 register mp_digit *tmpc; |
|
557 |
|
558 tmpc = c->dp + digs; |
|
559 for (ix = digs; ix <= pa; ix++) { |
|
560 /* now extract the previous digit [below the carry] */ |
|
561 *tmpc++ = W[ix]; |
|
562 } |
|
563 |
|
564 /* clear unused digits [that existed in the old copy of c] */ |
|
565 for (; ix < olduse; ix++) { |
|
566 *tmpc++ = 0; |
|
567 } |
3
|
568 } |
|
569 mp_clamp (c); |
|
570 return MP_OKAY; |
|
571 } |
143
|
572 #endif |
3
|
573 |
|
574 /* End: bn_fast_s_mp_mul_high_digs.c */ |
|
575 |
|
576 /* Start: bn_fast_s_mp_sqr.c */ |
143
|
577 #include <ltc_tommath.h> |
|
578 #ifdef BN_FAST_S_MP_SQR_C |
|
579 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
580 * |
|
581 * LibTomMath is a library that provides multiple-precision |
|
582 * integer arithmetic as well as number theoretic functionality. |
|
583 * |
|
584 * The library was designed directly after the MPI library by |
|
585 * Michael Fromberger but has been written from scratch with |
|
586 * additional optimizations in place. |
|
587 * |
|
588 * The library is free for all purposes without any express |
|
589 * guarantee it works. |
|
590 * |
|
591 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
592 */ |
3
|
593 |
|
594 /* fast squaring |
|
595 * |
|
596 * This is the comba method where the columns of the product |
|
597 * are computed first then the carries are computed. This |
|
598 * has the effect of making a very simple inner loop that |
|
599 * is executed the most |
|
600 * |
|
601 * W2 represents the outer products and W the inner. |
|
602 * |
|
603 * A further optimizations is made because the inner |
|
604 * products are of the form "A * B * 2". The *2 part does |
|
605 * not need to be computed until the end which is good |
|
606 * because 64-bit shifts are slow! |
|
607 * |
|
608 * Based on Algorithm 14.16 on pp.597 of HAC. |
|
609 * |
|
610 */ |
143
|
611 /* the jist of squaring... |
|
612 |
|
613 you do like mult except the offset of the tmpx [one that starts closer to zero] |
|
614 can't equal the offset of tmpy. So basically you set up iy like before then you min it with |
|
615 (ty-tx) so that it never happens. You double all those you add in the inner loop |
|
616 |
|
617 After that loop you do the squares and add them in. |
|
618 |
|
619 Remove W2 and don't memset W |
|
620 |
|
621 */ |
|
622 |
3
|
623 int fast_s_mp_sqr (mp_int * a, mp_int * b) |
|
624 { |
143
|
625 int olduse, res, pa, ix, iz; |
|
626 mp_digit W[MP_WARRAY], *tmpx; |
|
627 mp_word W1; |
|
628 |
|
629 /* grow the destination as required */ |
|
630 pa = a->used + a->used; |
|
631 if (b->alloc < pa) { |
|
632 if ((res = mp_grow (b, pa)) != MP_OKAY) { |
3
|
633 return res; |
|
634 } |
|
635 } |
|
636 |
143
|
637 /* number of output digits to produce */ |
|
638 W1 = 0; |
|
639 for (ix = 0; ix <= pa; ix++) { |
|
640 int tx, ty, iy; |
|
641 mp_word _W; |
|
642 mp_digit *tmpy; |
|
643 |
|
644 /* clear counter */ |
|
645 _W = 0; |
|
646 |
|
647 /* get offsets into the two bignums */ |
|
648 ty = MIN(a->used-1, ix); |
|
649 tx = ix - ty; |
|
650 |
|
651 /* setup temp aliases */ |
|
652 tmpx = a->dp + tx; |
|
653 tmpy = a->dp + ty; |
|
654 |
|
655 /* this is the number of times the loop will iterrate, essentially its |
|
656 while (tx++ < a->used && ty-- >= 0) { ... } |
|
657 */ |
|
658 iy = MIN(a->used-tx, ty+1); |
|
659 |
|
660 /* now for squaring tx can never equal ty |
|
661 * we halve the distance since they approach at a rate of 2x |
|
662 * and we have to round because odd cases need to be executed |
|
663 */ |
|
664 iy = MIN(iy, (ty-tx+1)>>1); |
|
665 |
|
666 /* execute loop */ |
|
667 for (iz = 0; iz < iy; iz++) { |
|
668 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
3
|
669 } |
143
|
670 |
|
671 /* double the inner product and add carry */ |
|
672 _W = _W + _W + W1; |
|
673 |
|
674 /* even columns have the square term in them */ |
|
675 if ((ix&1) == 0) { |
|
676 _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]); |
|
677 } |
|
678 |
|
679 /* store it */ |
|
680 W[ix] = _W; |
|
681 |
|
682 /* make next carry */ |
|
683 W1 = _W >> ((mp_word)DIGIT_BIT); |
3
|
684 } |
|
685 |
|
686 /* setup dest */ |
|
687 olduse = b->used; |
143
|
688 b->used = a->used+a->used; |
|
689 |
3
|
690 { |
143
|
691 mp_digit *tmpb; |
3
|
692 tmpb = b->dp; |
143
|
693 for (ix = 0; ix < pa; ix++) { |
|
694 *tmpb++ = W[ix] & MP_MASK; |
|
695 } |
|
696 |
|
697 /* clear unused digits [that existed in the old copy of c] */ |
3
|
698 for (; ix < olduse; ix++) { |
|
699 *tmpb++ = 0; |
|
700 } |
|
701 } |
|
702 mp_clamp (b); |
|
703 return MP_OKAY; |
|
704 } |
143
|
705 #endif |
3
|
706 |
|
707 /* End: bn_fast_s_mp_sqr.c */ |
|
708 |
|
709 /* Start: bn_mp_2expt.c */ |
143
|
710 #include <ltc_tommath.h> |
|
711 #ifdef BN_MP_2EXPT_C |
|
712 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
713 * |
|
714 * LibTomMath is a library that provides multiple-precision |
|
715 * integer arithmetic as well as number theoretic functionality. |
|
716 * |
|
717 * The library was designed directly after the MPI library by |
|
718 * Michael Fromberger but has been written from scratch with |
|
719 * additional optimizations in place. |
|
720 * |
|
721 * The library is free for all purposes without any express |
|
722 * guarantee it works. |
|
723 * |
|
724 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
725 */ |
3
|
726 |
|
727 /* computes a = 2**b |
|
728 * |
|
729 * Simple algorithm which zeroes the int, grows it then just sets one bit |
|
730 * as required. |
|
731 */ |
|
732 int |
|
733 mp_2expt (mp_int * a, int b) |
|
734 { |
|
735 int res; |
|
736 |
|
737 /* zero a as per default */ |
|
738 mp_zero (a); |
|
739 |
|
740 /* grow a to accomodate the single bit */ |
|
741 if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) { |
|
742 return res; |
|
743 } |
|
744 |
|
745 /* set the used count of where the bit will go */ |
|
746 a->used = b / DIGIT_BIT + 1; |
|
747 |
|
748 /* put the single bit in its place */ |
143
|
749 a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT); |
3
|
750 |
|
751 return MP_OKAY; |
|
752 } |
143
|
753 #endif |
3
|
754 |
|
755 /* End: bn_mp_2expt.c */ |
|
756 |
|
757 /* Start: bn_mp_abs.c */ |
143
|
758 #include <ltc_tommath.h> |
|
759 #ifdef BN_MP_ABS_C |
|
760 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
761 * |
|
762 * LibTomMath is a library that provides multiple-precision |
|
763 * integer arithmetic as well as number theoretic functionality. |
|
764 * |
|
765 * The library was designed directly after the MPI library by |
|
766 * Michael Fromberger but has been written from scratch with |
|
767 * additional optimizations in place. |
|
768 * |
|
769 * The library is free for all purposes without any express |
|
770 * guarantee it works. |
|
771 * |
|
772 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
773 */ |
3
|
774 |
|
775 /* b = |a| |
|
776 * |
|
777 * Simple function copies the input and fixes the sign to positive |
|
778 */ |
|
779 int |
|
780 mp_abs (mp_int * a, mp_int * b) |
|
781 { |
|
782 int res; |
|
783 |
|
784 /* copy a to b */ |
|
785 if (a != b) { |
|
786 if ((res = mp_copy (a, b)) != MP_OKAY) { |
|
787 return res; |
|
788 } |
|
789 } |
|
790 |
|
791 /* force the sign of b to positive */ |
|
792 b->sign = MP_ZPOS; |
|
793 |
|
794 return MP_OKAY; |
|
795 } |
143
|
796 #endif |
3
|
797 |
|
798 /* End: bn_mp_abs.c */ |
|
799 |
|
800 /* Start: bn_mp_add.c */ |
143
|
801 #include <ltc_tommath.h> |
|
802 #ifdef BN_MP_ADD_C |
|
803 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
804 * |
|
805 * LibTomMath is a library that provides multiple-precision |
|
806 * integer arithmetic as well as number theoretic functionality. |
|
807 * |
|
808 * The library was designed directly after the MPI library by |
|
809 * Michael Fromberger but has been written from scratch with |
|
810 * additional optimizations in place. |
|
811 * |
|
812 * The library is free for all purposes without any express |
|
813 * guarantee it works. |
|
814 * |
|
815 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
816 */ |
3
|
817 |
|
818 /* high level addition (handles signs) */ |
|
819 int mp_add (mp_int * a, mp_int * b, mp_int * c) |
|
820 { |
|
821 int sa, sb, res; |
|
822 |
|
823 /* get sign of both inputs */ |
|
824 sa = a->sign; |
|
825 sb = b->sign; |
|
826 |
|
827 /* handle two cases, not four */ |
|
828 if (sa == sb) { |
|
829 /* both positive or both negative */ |
|
830 /* add their magnitudes, copy the sign */ |
|
831 c->sign = sa; |
|
832 res = s_mp_add (a, b, c); |
|
833 } else { |
|
834 /* one positive, the other negative */ |
|
835 /* subtract the one with the greater magnitude from */ |
|
836 /* the one of the lesser magnitude. The result gets */ |
|
837 /* the sign of the one with the greater magnitude. */ |
|
838 if (mp_cmp_mag (a, b) == MP_LT) { |
|
839 c->sign = sb; |
|
840 res = s_mp_sub (b, a, c); |
|
841 } else { |
|
842 c->sign = sa; |
|
843 res = s_mp_sub (a, b, c); |
|
844 } |
|
845 } |
|
846 return res; |
|
847 } |
|
848 |
143
|
849 #endif |
3
|
850 |
|
851 /* End: bn_mp_add.c */ |
|
852 |
|
853 /* Start: bn_mp_add_d.c */ |
143
|
854 #include <ltc_tommath.h> |
|
855 #ifdef BN_MP_ADD_D_C |
|
856 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
857 * |
|
858 * LibTomMath is a library that provides multiple-precision |
|
859 * integer arithmetic as well as number theoretic functionality. |
|
860 * |
|
861 * The library was designed directly after the MPI library by |
|
862 * Michael Fromberger but has been written from scratch with |
|
863 * additional optimizations in place. |
|
864 * |
|
865 * The library is free for all purposes without any express |
|
866 * guarantee it works. |
|
867 * |
|
868 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
869 */ |
3
|
870 |
|
871 /* single digit addition */ |
|
872 int |
|
873 mp_add_d (mp_int * a, mp_digit b, mp_int * c) |
|
874 { |
|
875 int res, ix, oldused; |
|
876 mp_digit *tmpa, *tmpc, mu; |
|
877 |
|
878 /* grow c as required */ |
|
879 if (c->alloc < a->used + 1) { |
|
880 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { |
|
881 return res; |
|
882 } |
|
883 } |
|
884 |
|
885 /* if a is negative and |a| >= b, call c = |a| - b */ |
|
886 if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) { |
|
887 /* temporarily fix sign of a */ |
|
888 a->sign = MP_ZPOS; |
|
889 |
|
890 /* c = |a| - b */ |
|
891 res = mp_sub_d(a, b, c); |
|
892 |
|
893 /* fix sign */ |
|
894 a->sign = c->sign = MP_NEG; |
|
895 |
|
896 return res; |
|
897 } |
|
898 |
|
899 /* old number of used digits in c */ |
|
900 oldused = c->used; |
|
901 |
|
902 /* sign always positive */ |
|
903 c->sign = MP_ZPOS; |
|
904 |
|
905 /* source alias */ |
|
906 tmpa = a->dp; |
|
907 |
|
908 /* destination alias */ |
|
909 tmpc = c->dp; |
|
910 |
|
911 /* if a is positive */ |
|
912 if (a->sign == MP_ZPOS) { |
|
913 /* add digit, after this we're propagating |
|
914 * the carry. |
|
915 */ |
|
916 *tmpc = *tmpa++ + b; |
|
917 mu = *tmpc >> DIGIT_BIT; |
|
918 *tmpc++ &= MP_MASK; |
|
919 |
|
920 /* now handle rest of the digits */ |
|
921 for (ix = 1; ix < a->used; ix++) { |
|
922 *tmpc = *tmpa++ + mu; |
|
923 mu = *tmpc >> DIGIT_BIT; |
|
924 *tmpc++ &= MP_MASK; |
|
925 } |
|
926 /* set final carry */ |
|
927 ix++; |
|
928 *tmpc++ = mu; |
|
929 |
|
930 /* setup size */ |
|
931 c->used = a->used + 1; |
|
932 } else { |
|
933 /* a was negative and |a| < b */ |
|
934 c->used = 1; |
|
935 |
|
936 /* the result is a single digit */ |
|
937 if (a->used == 1) { |
|
938 *tmpc++ = b - a->dp[0]; |
|
939 } else { |
|
940 *tmpc++ = b; |
|
941 } |
|
942 |
|
943 /* setup count so the clearing of oldused |
|
944 * can fall through correctly |
|
945 */ |
|
946 ix = 1; |
|
947 } |
|
948 |
|
949 /* now zero to oldused */ |
|
950 while (ix++ < oldused) { |
|
951 *tmpc++ = 0; |
|
952 } |
|
953 mp_clamp(c); |
|
954 |
|
955 return MP_OKAY; |
|
956 } |
|
957 |
143
|
958 #endif |
3
|
959 |
|
960 /* End: bn_mp_add_d.c */ |
|
961 |
|
962 /* Start: bn_mp_addmod.c */ |
143
|
963 #include <ltc_tommath.h> |
|
964 #ifdef BN_MP_ADDMOD_C |
|
965 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
966 * |
|
967 * LibTomMath is a library that provides multiple-precision |
|
968 * integer arithmetic as well as number theoretic functionality. |
|
969 * |
|
970 * The library was designed directly after the MPI library by |
|
971 * Michael Fromberger but has been written from scratch with |
|
972 * additional optimizations in place. |
|
973 * |
|
974 * The library is free for all purposes without any express |
|
975 * guarantee it works. |
|
976 * |
|
977 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
978 */ |
3
|
979 |
|
980 /* d = a + b (mod c) */ |
|
981 int |
|
982 mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
|
983 { |
|
984 int res; |
|
985 mp_int t; |
|
986 |
|
987 if ((res = mp_init (&t)) != MP_OKAY) { |
|
988 return res; |
|
989 } |
|
990 |
|
991 if ((res = mp_add (a, b, &t)) != MP_OKAY) { |
|
992 mp_clear (&t); |
|
993 return res; |
|
994 } |
|
995 res = mp_mod (&t, c, d); |
|
996 mp_clear (&t); |
|
997 return res; |
|
998 } |
143
|
999 #endif |
3
|
1000 |
|
1001 /* End: bn_mp_addmod.c */ |
|
1002 |
|
1003 /* Start: bn_mp_and.c */ |
143
|
1004 #include <ltc_tommath.h> |
|
1005 #ifdef BN_MP_AND_C |
|
1006 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1007 * |
|
1008 * LibTomMath is a library that provides multiple-precision |
|
1009 * integer arithmetic as well as number theoretic functionality. |
|
1010 * |
|
1011 * The library was designed directly after the MPI library by |
|
1012 * Michael Fromberger but has been written from scratch with |
|
1013 * additional optimizations in place. |
|
1014 * |
|
1015 * The library is free for all purposes without any express |
|
1016 * guarantee it works. |
|
1017 * |
|
1018 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1019 */ |
3
|
1020 |
|
1021 /* AND two ints together */ |
|
1022 int |
|
1023 mp_and (mp_int * a, mp_int * b, mp_int * c) |
|
1024 { |
|
1025 int res, ix, px; |
|
1026 mp_int t, *x; |
|
1027 |
|
1028 if (a->used > b->used) { |
|
1029 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
1030 return res; |
|
1031 } |
|
1032 px = b->used; |
|
1033 x = b; |
|
1034 } else { |
|
1035 if ((res = mp_init_copy (&t, b)) != MP_OKAY) { |
|
1036 return res; |
|
1037 } |
|
1038 px = a->used; |
|
1039 x = a; |
|
1040 } |
|
1041 |
|
1042 for (ix = 0; ix < px; ix++) { |
|
1043 t.dp[ix] &= x->dp[ix]; |
|
1044 } |
|
1045 |
|
1046 /* zero digits above the last from the smallest mp_int */ |
|
1047 for (; ix < t.used; ix++) { |
|
1048 t.dp[ix] = 0; |
|
1049 } |
|
1050 |
|
1051 mp_clamp (&t); |
|
1052 mp_exch (c, &t); |
|
1053 mp_clear (&t); |
|
1054 return MP_OKAY; |
|
1055 } |
143
|
1056 #endif |
3
|
1057 |
|
1058 /* End: bn_mp_and.c */ |
|
1059 |
|
1060 /* Start: bn_mp_clamp.c */ |
143
|
1061 #include <ltc_tommath.h> |
|
1062 #ifdef BN_MP_CLAMP_C |
|
1063 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1064 * |
|
1065 * LibTomMath is a library that provides multiple-precision |
|
1066 * integer arithmetic as well as number theoretic functionality. |
|
1067 * |
|
1068 * The library was designed directly after the MPI library by |
|
1069 * Michael Fromberger but has been written from scratch with |
|
1070 * additional optimizations in place. |
|
1071 * |
|
1072 * The library is free for all purposes without any express |
|
1073 * guarantee it works. |
|
1074 * |
|
1075 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1076 */ |
3
|
1077 |
|
1078 /* trim unused digits |
|
1079 * |
|
1080 * This is used to ensure that leading zero digits are |
|
1081 * trimed and the leading "used" digit will be non-zero |
|
1082 * Typically very fast. Also fixes the sign if there |
|
1083 * are no more leading digits |
|
1084 */ |
|
1085 void |
|
1086 mp_clamp (mp_int * a) |
|
1087 { |
|
1088 /* decrease used while the most significant digit is |
|
1089 * zero. |
|
1090 */ |
|
1091 while (a->used > 0 && a->dp[a->used - 1] == 0) { |
|
1092 --(a->used); |
|
1093 } |
|
1094 |
|
1095 /* reset the sign flag if used == 0 */ |
|
1096 if (a->used == 0) { |
|
1097 a->sign = MP_ZPOS; |
|
1098 } |
|
1099 } |
143
|
1100 #endif |
3
|
1101 |
|
1102 /* End: bn_mp_clamp.c */ |
|
1103 |
|
1104 /* Start: bn_mp_clear.c */ |
143
|
1105 #include <ltc_tommath.h> |
|
1106 #ifdef BN_MP_CLEAR_C |
|
1107 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1108 * |
|
1109 * LibTomMath is a library that provides multiple-precision |
|
1110 * integer arithmetic as well as number theoretic functionality. |
|
1111 * |
|
1112 * The library was designed directly after the MPI library by |
|
1113 * Michael Fromberger but has been written from scratch with |
|
1114 * additional optimizations in place. |
|
1115 * |
|
1116 * The library is free for all purposes without any express |
|
1117 * guarantee it works. |
|
1118 * |
|
1119 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1120 */ |
3
|
1121 |
|
1122 /* clear one (frees) */ |
|
1123 void |
|
1124 mp_clear (mp_int * a) |
|
1125 { |
143
|
1126 int i; |
|
1127 |
3
|
1128 /* only do anything if a hasn't been freed previously */ |
|
1129 if (a->dp != NULL) { |
|
1130 /* first zero the digits */ |
143
|
1131 for (i = 0; i < a->used; i++) { |
|
1132 a->dp[i] = 0; |
|
1133 } |
3
|
1134 |
|
1135 /* free ram */ |
|
1136 XFREE(a->dp); |
|
1137 |
|
1138 /* reset members to make debugging easier */ |
|
1139 a->dp = NULL; |
|
1140 a->alloc = a->used = 0; |
|
1141 a->sign = MP_ZPOS; |
|
1142 } |
|
1143 } |
143
|
1144 #endif |
3
|
1145 |
|
1146 /* End: bn_mp_clear.c */ |
|
1147 |
|
1148 /* Start: bn_mp_clear_multi.c */ |
143
|
1149 #include <ltc_tommath.h> |
|
1150 #ifdef BN_MP_CLEAR_MULTI_C |
|
1151 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1152 * |
|
1153 * LibTomMath is a library that provides multiple-precision |
|
1154 * integer arithmetic as well as number theoretic functionality. |
|
1155 * |
|
1156 * The library was designed directly after the MPI library by |
|
1157 * Michael Fromberger but has been written from scratch with |
|
1158 * additional optimizations in place. |
|
1159 * |
|
1160 * The library is free for all purposes without any express |
|
1161 * guarantee it works. |
|
1162 * |
|
1163 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1164 */ |
3
|
1165 #include <stdarg.h> |
|
1166 |
|
1167 void mp_clear_multi(mp_int *mp, ...) |
|
1168 { |
|
1169 mp_int* next_mp = mp; |
|
1170 va_list args; |
|
1171 va_start(args, mp); |
|
1172 while (next_mp != NULL) { |
|
1173 mp_clear(next_mp); |
|
1174 next_mp = va_arg(args, mp_int*); |
|
1175 } |
|
1176 va_end(args); |
|
1177 } |
143
|
1178 #endif |
3
|
1179 |
|
1180 /* End: bn_mp_clear_multi.c */ |
|
1181 |
|
1182 /* Start: bn_mp_cmp.c */ |
143
|
1183 #include <ltc_tommath.h> |
|
1184 #ifdef BN_MP_CMP_C |
|
1185 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1186 * |
|
1187 * LibTomMath is a library that provides multiple-precision |
|
1188 * integer arithmetic as well as number theoretic functionality. |
|
1189 * |
|
1190 * The library was designed directly after the MPI library by |
|
1191 * Michael Fromberger but has been written from scratch with |
|
1192 * additional optimizations in place. |
|
1193 * |
|
1194 * The library is free for all purposes without any express |
|
1195 * guarantee it works. |
|
1196 * |
|
1197 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1198 */ |
3
|
1199 |
|
1200 /* compare two ints (signed)*/ |
|
1201 int |
|
1202 mp_cmp (mp_int * a, mp_int * b) |
|
1203 { |
|
1204 /* compare based on sign */ |
|
1205 if (a->sign != b->sign) { |
|
1206 if (a->sign == MP_NEG) { |
|
1207 return MP_LT; |
|
1208 } else { |
|
1209 return MP_GT; |
|
1210 } |
|
1211 } |
|
1212 |
|
1213 /* compare digits */ |
|
1214 if (a->sign == MP_NEG) { |
|
1215 /* if negative compare opposite direction */ |
|
1216 return mp_cmp_mag(b, a); |
|
1217 } else { |
|
1218 return mp_cmp_mag(a, b); |
|
1219 } |
|
1220 } |
143
|
1221 #endif |
3
|
1222 |
|
1223 /* End: bn_mp_cmp.c */ |
|
1224 |
|
1225 /* Start: bn_mp_cmp_d.c */ |
143
|
1226 #include <ltc_tommath.h> |
|
1227 #ifdef BN_MP_CMP_D_C |
|
1228 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1229 * |
|
1230 * LibTomMath is a library that provides multiple-precision |
|
1231 * integer arithmetic as well as number theoretic functionality. |
|
1232 * |
|
1233 * The library was designed directly after the MPI library by |
|
1234 * Michael Fromberger but has been written from scratch with |
|
1235 * additional optimizations in place. |
|
1236 * |
|
1237 * The library is free for all purposes without any express |
|
1238 * guarantee it works. |
|
1239 * |
|
1240 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1241 */ |
3
|
1242 |
|
1243 /* compare a digit */ |
|
1244 int mp_cmp_d(mp_int * a, mp_digit b) |
|
1245 { |
|
1246 /* compare based on sign */ |
|
1247 if (a->sign == MP_NEG) { |
|
1248 return MP_LT; |
|
1249 } |
|
1250 |
|
1251 /* compare based on magnitude */ |
|
1252 if (a->used > 1) { |
|
1253 return MP_GT; |
|
1254 } |
|
1255 |
|
1256 /* compare the only digit of a to b */ |
|
1257 if (a->dp[0] > b) { |
|
1258 return MP_GT; |
|
1259 } else if (a->dp[0] < b) { |
|
1260 return MP_LT; |
|
1261 } else { |
|
1262 return MP_EQ; |
|
1263 } |
|
1264 } |
143
|
1265 #endif |
3
|
1266 |
|
1267 /* End: bn_mp_cmp_d.c */ |
|
1268 |
|
1269 /* Start: bn_mp_cmp_mag.c */ |
143
|
1270 #include <ltc_tommath.h> |
|
1271 #ifdef BN_MP_CMP_MAG_C |
|
1272 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1273 * |
|
1274 * LibTomMath is a library that provides multiple-precision |
|
1275 * integer arithmetic as well as number theoretic functionality. |
|
1276 * |
|
1277 * The library was designed directly after the MPI library by |
|
1278 * Michael Fromberger but has been written from scratch with |
|
1279 * additional optimizations in place. |
|
1280 * |
|
1281 * The library is free for all purposes without any express |
|
1282 * guarantee it works. |
|
1283 * |
|
1284 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1285 */ |
3
|
1286 |
|
1287 /* compare maginitude of two ints (unsigned) */ |
|
1288 int mp_cmp_mag (mp_int * a, mp_int * b) |
|
1289 { |
|
1290 int n; |
|
1291 mp_digit *tmpa, *tmpb; |
|
1292 |
|
1293 /* compare based on # of non-zero digits */ |
|
1294 if (a->used > b->used) { |
|
1295 return MP_GT; |
|
1296 } |
|
1297 |
|
1298 if (a->used < b->used) { |
|
1299 return MP_LT; |
|
1300 } |
|
1301 |
|
1302 /* alias for a */ |
|
1303 tmpa = a->dp + (a->used - 1); |
|
1304 |
|
1305 /* alias for b */ |
|
1306 tmpb = b->dp + (a->used - 1); |
|
1307 |
|
1308 /* compare based on digits */ |
|
1309 for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { |
|
1310 if (*tmpa > *tmpb) { |
|
1311 return MP_GT; |
|
1312 } |
|
1313 |
|
1314 if (*tmpa < *tmpb) { |
|
1315 return MP_LT; |
|
1316 } |
|
1317 } |
|
1318 return MP_EQ; |
|
1319 } |
143
|
1320 #endif |
3
|
1321 |
|
1322 /* End: bn_mp_cmp_mag.c */ |
|
1323 |
|
1324 /* Start: bn_mp_cnt_lsb.c */ |
143
|
1325 #include <ltc_tommath.h> |
|
1326 #ifdef BN_MP_CNT_LSB_C |
|
1327 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1328 * |
|
1329 * LibTomMath is a library that provides multiple-precision |
|
1330 * integer arithmetic as well as number theoretic functionality. |
|
1331 * |
|
1332 * The library was designed directly after the MPI library by |
|
1333 * Michael Fromberger but has been written from scratch with |
|
1334 * additional optimizations in place. |
|
1335 * |
|
1336 * The library is free for all purposes without any express |
|
1337 * guarantee it works. |
|
1338 * |
|
1339 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1340 */ |
3
|
1341 |
|
1342 static const int lnz[16] = { |
|
1343 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 |
|
1344 }; |
|
1345 |
|
1346 /* Counts the number of lsbs which are zero before the first zero bit */ |
|
1347 int mp_cnt_lsb(mp_int *a) |
|
1348 { |
|
1349 int x; |
|
1350 mp_digit q, qq; |
|
1351 |
|
1352 /* easy out */ |
|
1353 if (mp_iszero(a) == 1) { |
|
1354 return 0; |
|
1355 } |
|
1356 |
|
1357 /* scan lower digits until non-zero */ |
|
1358 for (x = 0; x < a->used && a->dp[x] == 0; x++); |
|
1359 q = a->dp[x]; |
|
1360 x *= DIGIT_BIT; |
|
1361 |
|
1362 /* now scan this digit until a 1 is found */ |
|
1363 if ((q & 1) == 0) { |
|
1364 do { |
|
1365 qq = q & 15; |
|
1366 x += lnz[qq]; |
|
1367 q >>= 4; |
|
1368 } while (qq == 0); |
|
1369 } |
|
1370 return x; |
|
1371 } |
|
1372 |
143
|
1373 #endif |
3
|
1374 |
|
1375 /* End: bn_mp_cnt_lsb.c */ |
|
1376 |
|
1377 /* Start: bn_mp_copy.c */ |
143
|
1378 #include <ltc_tommath.h> |
|
1379 #ifdef BN_MP_COPY_C |
|
1380 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1381 * |
|
1382 * LibTomMath is a library that provides multiple-precision |
|
1383 * integer arithmetic as well as number theoretic functionality. |
|
1384 * |
|
1385 * The library was designed directly after the MPI library by |
|
1386 * Michael Fromberger but has been written from scratch with |
|
1387 * additional optimizations in place. |
|
1388 * |
|
1389 * The library is free for all purposes without any express |
|
1390 * guarantee it works. |
|
1391 * |
|
1392 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1393 */ |
3
|
1394 |
|
1395 /* copy, b = a */ |
|
1396 int |
|
1397 mp_copy (mp_int * a, mp_int * b) |
|
1398 { |
|
1399 int res, n; |
|
1400 |
|
1401 /* if dst == src do nothing */ |
|
1402 if (a == b) { |
|
1403 return MP_OKAY; |
|
1404 } |
|
1405 |
|
1406 /* grow dest */ |
|
1407 if (b->alloc < a->used) { |
|
1408 if ((res = mp_grow (b, a->used)) != MP_OKAY) { |
|
1409 return res; |
|
1410 } |
|
1411 } |
|
1412 |
|
1413 /* zero b and copy the parameters over */ |
|
1414 { |
|
1415 register mp_digit *tmpa, *tmpb; |
|
1416 |
|
1417 /* pointer aliases */ |
|
1418 |
|
1419 /* source */ |
|
1420 tmpa = a->dp; |
|
1421 |
|
1422 /* destination */ |
|
1423 tmpb = b->dp; |
|
1424 |
|
1425 /* copy all the digits */ |
|
1426 for (n = 0; n < a->used; n++) { |
|
1427 *tmpb++ = *tmpa++; |
|
1428 } |
|
1429 |
|
1430 /* clear high digits */ |
|
1431 for (; n < b->used; n++) { |
|
1432 *tmpb++ = 0; |
|
1433 } |
|
1434 } |
|
1435 |
|
1436 /* copy used count and sign */ |
|
1437 b->used = a->used; |
|
1438 b->sign = a->sign; |
|
1439 return MP_OKAY; |
|
1440 } |
143
|
1441 #endif |
3
|
1442 |
|
1443 /* End: bn_mp_copy.c */ |
|
1444 |
|
1445 /* Start: bn_mp_count_bits.c */ |
143
|
1446 #include <ltc_tommath.h> |
|
1447 #ifdef BN_MP_COUNT_BITS_C |
|
1448 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1449 * |
|
1450 * LibTomMath is a library that provides multiple-precision |
|
1451 * integer arithmetic as well as number theoretic functionality. |
|
1452 * |
|
1453 * The library was designed directly after the MPI library by |
|
1454 * Michael Fromberger but has been written from scratch with |
|
1455 * additional optimizations in place. |
|
1456 * |
|
1457 * The library is free for all purposes without any express |
|
1458 * guarantee it works. |
|
1459 * |
|
1460 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1461 */ |
3
|
1462 |
|
1463 /* returns the number of bits in an int */ |
|
1464 int |
|
1465 mp_count_bits (mp_int * a) |
|
1466 { |
|
1467 int r; |
|
1468 mp_digit q; |
|
1469 |
|
1470 /* shortcut */ |
|
1471 if (a->used == 0) { |
|
1472 return 0; |
|
1473 } |
|
1474 |
|
1475 /* get number of digits and add that */ |
|
1476 r = (a->used - 1) * DIGIT_BIT; |
|
1477 |
|
1478 /* take the last digit and count the bits in it */ |
|
1479 q = a->dp[a->used - 1]; |
|
1480 while (q > ((mp_digit) 0)) { |
|
1481 ++r; |
|
1482 q >>= ((mp_digit) 1); |
|
1483 } |
|
1484 return r; |
|
1485 } |
143
|
1486 #endif |
3
|
1487 |
|
1488 /* End: bn_mp_count_bits.c */ |
|
1489 |
|
1490 /* Start: bn_mp_div.c */ |
143
|
1491 #include <ltc_tommath.h> |
|
1492 #ifdef BN_MP_DIV_C |
|
1493 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1494 * |
|
1495 * LibTomMath is a library that provides multiple-precision |
|
1496 * integer arithmetic as well as number theoretic functionality. |
|
1497 * |
|
1498 * The library was designed directly after the MPI library by |
|
1499 * Michael Fromberger but has been written from scratch with |
|
1500 * additional optimizations in place. |
|
1501 * |
|
1502 * The library is free for all purposes without any express |
|
1503 * guarantee it works. |
|
1504 * |
|
1505 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1506 */ |
|
1507 |
|
1508 #ifdef BN_MP_DIV_SMALL |
|
1509 |
|
1510 /* slower bit-bang division... also smaller */ |
|
1511 int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
|
1512 { |
|
1513 mp_int ta, tb, tq, q; |
|
1514 int res, n, n2; |
|
1515 |
|
1516 /* is divisor zero ? */ |
|
1517 if (mp_iszero (b) == 1) { |
|
1518 return MP_VAL; |
|
1519 } |
|
1520 |
|
1521 /* if a < b then q=0, r = a */ |
|
1522 if (mp_cmp_mag (a, b) == MP_LT) { |
|
1523 if (d != NULL) { |
|
1524 res = mp_copy (a, d); |
|
1525 } else { |
|
1526 res = MP_OKAY; |
|
1527 } |
|
1528 if (c != NULL) { |
|
1529 mp_zero (c); |
|
1530 } |
|
1531 return res; |
|
1532 } |
|
1533 |
|
1534 /* init our temps */ |
|
1535 if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != MP_OKAY)) { |
|
1536 return res; |
|
1537 } |
|
1538 |
|
1539 |
|
1540 mp_set(&tq, 1); |
|
1541 n = mp_count_bits(a) - mp_count_bits(b); |
|
1542 if (((res = mp_copy(a, &ta)) != MP_OKAY) || |
|
1543 ((res = mp_copy(b, &tb)) != MP_OKAY) || |
|
1544 ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || |
|
1545 ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) { |
|
1546 goto __ERR; |
|
1547 } |
|
1548 |
|
1549 while (n-- >= 0) { |
|
1550 if (mp_cmp(&tb, &ta) != MP_GT) { |
|
1551 if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) || |
|
1552 ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) { |
|
1553 goto __ERR; |
|
1554 } |
|
1555 } |
|
1556 if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) || |
|
1557 ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) { |
|
1558 goto __ERR; |
|
1559 } |
|
1560 } |
|
1561 |
|
1562 /* now q == quotient and ta == remainder */ |
|
1563 n = a->sign; |
|
1564 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); |
|
1565 if (c != NULL) { |
|
1566 mp_exch(c, &q); |
|
1567 c->sign = n2; |
|
1568 } |
|
1569 if (d != NULL) { |
|
1570 mp_exch(d, &ta); |
|
1571 d->sign = n; |
|
1572 } |
|
1573 __ERR: |
|
1574 mp_clear_multi(&ta, &tb, &tq, &q, NULL); |
|
1575 return res; |
|
1576 } |
|
1577 |
|
1578 #else |
3
|
1579 |
|
1580 /* integer signed division. |
|
1581 * c*b + d == a [e.g. a/b, c=quotient, d=remainder] |
|
1582 * HAC pp.598 Algorithm 14.20 |
|
1583 * |
|
1584 * Note that the description in HAC is horribly |
|
1585 * incomplete. For example, it doesn't consider |
|
1586 * the case where digits are removed from 'x' in |
|
1587 * the inner loop. It also doesn't consider the |
|
1588 * case that y has fewer than three digits, etc.. |
|
1589 * |
|
1590 * The overall algorithm is as described as |
|
1591 * 14.20 from HAC but fixed to treat these cases. |
|
1592 */ |
|
1593 int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
|
1594 { |
|
1595 mp_int q, x, y, t1, t2; |
|
1596 int res, n, t, i, norm, neg; |
|
1597 |
|
1598 /* is divisor zero ? */ |
|
1599 if (mp_iszero (b) == 1) { |
|
1600 return MP_VAL; |
|
1601 } |
|
1602 |
|
1603 /* if a < b then q=0, r = a */ |
|
1604 if (mp_cmp_mag (a, b) == MP_LT) { |
|
1605 if (d != NULL) { |
|
1606 res = mp_copy (a, d); |
|
1607 } else { |
|
1608 res = MP_OKAY; |
|
1609 } |
|
1610 if (c != NULL) { |
|
1611 mp_zero (c); |
|
1612 } |
|
1613 return res; |
|
1614 } |
|
1615 |
|
1616 if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) { |
|
1617 return res; |
|
1618 } |
|
1619 q.used = a->used + 2; |
|
1620 |
|
1621 if ((res = mp_init (&t1)) != MP_OKAY) { |
|
1622 goto __Q; |
|
1623 } |
|
1624 |
|
1625 if ((res = mp_init (&t2)) != MP_OKAY) { |
|
1626 goto __T1; |
|
1627 } |
|
1628 |
|
1629 if ((res = mp_init_copy (&x, a)) != MP_OKAY) { |
|
1630 goto __T2; |
|
1631 } |
|
1632 |
|
1633 if ((res = mp_init_copy (&y, b)) != MP_OKAY) { |
|
1634 goto __X; |
|
1635 } |
|
1636 |
|
1637 /* fix the sign */ |
|
1638 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; |
|
1639 x.sign = y.sign = MP_ZPOS; |
|
1640 |
|
1641 /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ |
|
1642 norm = mp_count_bits(&y) % DIGIT_BIT; |
|
1643 if (norm < (int)(DIGIT_BIT-1)) { |
|
1644 norm = (DIGIT_BIT-1) - norm; |
|
1645 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) { |
|
1646 goto __Y; |
|
1647 } |
|
1648 if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) { |
|
1649 goto __Y; |
|
1650 } |
|
1651 } else { |
|
1652 norm = 0; |
|
1653 } |
|
1654 |
|
1655 /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */ |
|
1656 n = x.used - 1; |
|
1657 t = y.used - 1; |
|
1658 |
|
1659 /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ |
|
1660 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */ |
|
1661 goto __Y; |
|
1662 } |
|
1663 |
|
1664 while (mp_cmp (&x, &y) != MP_LT) { |
|
1665 ++(q.dp[n - t]); |
|
1666 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) { |
|
1667 goto __Y; |
|
1668 } |
|
1669 } |
|
1670 |
|
1671 /* reset y by shifting it back down */ |
|
1672 mp_rshd (&y, n - t); |
|
1673 |
|
1674 /* step 3. for i from n down to (t + 1) */ |
|
1675 for (i = n; i >= (t + 1); i--) { |
|
1676 if (i > x.used) { |
|
1677 continue; |
|
1678 } |
|
1679 |
|
1680 /* step 3.1 if xi == yt then set q{i-t-1} to b-1, |
|
1681 * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ |
|
1682 if (x.dp[i] == y.dp[t]) { |
|
1683 q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); |
|
1684 } else { |
|
1685 mp_word tmp; |
|
1686 tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); |
|
1687 tmp |= ((mp_word) x.dp[i - 1]); |
|
1688 tmp /= ((mp_word) y.dp[t]); |
|
1689 if (tmp > (mp_word) MP_MASK) |
|
1690 tmp = MP_MASK; |
|
1691 q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); |
|
1692 } |
|
1693 |
|
1694 /* while (q{i-t-1} * (yt * b + y{t-1})) > |
|
1695 xi * b**2 + xi-1 * b + xi-2 |
|
1696 |
|
1697 do q{i-t-1} -= 1; |
|
1698 */ |
|
1699 q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; |
|
1700 do { |
|
1701 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK; |
|
1702 |
|
1703 /* find left hand */ |
|
1704 mp_zero (&t1); |
|
1705 t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1]; |
|
1706 t1.dp[1] = y.dp[t]; |
|
1707 t1.used = 2; |
|
1708 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) { |
|
1709 goto __Y; |
|
1710 } |
|
1711 |
|
1712 /* find right hand */ |
|
1713 t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2]; |
|
1714 t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1]; |
|
1715 t2.dp[2] = x.dp[i]; |
|
1716 t2.used = 3; |
|
1717 } while (mp_cmp_mag(&t1, &t2) == MP_GT); |
|
1718 |
|
1719 /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ |
|
1720 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) { |
|
1721 goto __Y; |
|
1722 } |
|
1723 |
|
1724 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { |
|
1725 goto __Y; |
|
1726 } |
|
1727 |
|
1728 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) { |
|
1729 goto __Y; |
|
1730 } |
|
1731 |
|
1732 /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ |
|
1733 if (x.sign == MP_NEG) { |
|
1734 if ((res = mp_copy (&y, &t1)) != MP_OKAY) { |
|
1735 goto __Y; |
|
1736 } |
|
1737 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { |
|
1738 goto __Y; |
|
1739 } |
|
1740 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { |
|
1741 goto __Y; |
|
1742 } |
|
1743 |
|
1744 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; |
|
1745 } |
|
1746 } |
|
1747 |
|
1748 /* now q is the quotient and x is the remainder |
|
1749 * [which we have to normalize] |
|
1750 */ |
|
1751 |
|
1752 /* get sign before writing to c */ |
143
|
1753 x.sign = x.used == 0 ? MP_ZPOS : a->sign; |
3
|
1754 |
|
1755 if (c != NULL) { |
|
1756 mp_clamp (&q); |
|
1757 mp_exch (&q, c); |
|
1758 c->sign = neg; |
|
1759 } |
|
1760 |
|
1761 if (d != NULL) { |
|
1762 mp_div_2d (&x, norm, &x, NULL); |
|
1763 mp_exch (&x, d); |
|
1764 } |
|
1765 |
|
1766 res = MP_OKAY; |
|
1767 |
|
1768 __Y:mp_clear (&y); |
|
1769 __X:mp_clear (&x); |
|
1770 __T2:mp_clear (&t2); |
|
1771 __T1:mp_clear (&t1); |
|
1772 __Q:mp_clear (&q); |
|
1773 return res; |
|
1774 } |
|
1775 |
143
|
1776 #endif |
|
1777 |
|
1778 #endif |
|
1779 |
3
|
1780 /* End: bn_mp_div.c */ |
|
1781 |
|
1782 /* Start: bn_mp_div_2.c */ |
143
|
1783 #include <ltc_tommath.h> |
|
1784 #ifdef BN_MP_DIV_2_C |
|
1785 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1786 * |
|
1787 * LibTomMath is a library that provides multiple-precision |
|
1788 * integer arithmetic as well as number theoretic functionality. |
|
1789 * |
|
1790 * The library was designed directly after the MPI library by |
|
1791 * Michael Fromberger but has been written from scratch with |
|
1792 * additional optimizations in place. |
|
1793 * |
|
1794 * The library is free for all purposes without any express |
|
1795 * guarantee it works. |
|
1796 * |
|
1797 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1798 */ |
3
|
1799 |
|
1800 /* b = a/2 */ |
|
1801 int mp_div_2(mp_int * a, mp_int * b) |
|
1802 { |
|
1803 int x, res, oldused; |
|
1804 |
|
1805 /* copy */ |
|
1806 if (b->alloc < a->used) { |
|
1807 if ((res = mp_grow (b, a->used)) != MP_OKAY) { |
|
1808 return res; |
|
1809 } |
|
1810 } |
|
1811 |
|
1812 oldused = b->used; |
|
1813 b->used = a->used; |
|
1814 { |
|
1815 register mp_digit r, rr, *tmpa, *tmpb; |
|
1816 |
|
1817 /* source alias */ |
|
1818 tmpa = a->dp + b->used - 1; |
|
1819 |
|
1820 /* dest alias */ |
|
1821 tmpb = b->dp + b->used - 1; |
|
1822 |
|
1823 /* carry */ |
|
1824 r = 0; |
|
1825 for (x = b->used - 1; x >= 0; x--) { |
|
1826 /* get the carry for the next iteration */ |
|
1827 rr = *tmpa & 1; |
|
1828 |
|
1829 /* shift the current digit, add in carry and store */ |
|
1830 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); |
|
1831 |
|
1832 /* forward carry to next iteration */ |
|
1833 r = rr; |
|
1834 } |
|
1835 |
|
1836 /* zero excess digits */ |
|
1837 tmpb = b->dp + b->used; |
|
1838 for (x = b->used; x < oldused; x++) { |
|
1839 *tmpb++ = 0; |
|
1840 } |
|
1841 } |
|
1842 b->sign = a->sign; |
|
1843 mp_clamp (b); |
|
1844 return MP_OKAY; |
|
1845 } |
143
|
1846 #endif |
3
|
1847 |
|
1848 /* End: bn_mp_div_2.c */ |
|
1849 |
|
1850 /* Start: bn_mp_div_2d.c */ |
143
|
1851 #include <ltc_tommath.h> |
|
1852 #ifdef BN_MP_DIV_2D_C |
|
1853 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1854 * |
|
1855 * LibTomMath is a library that provides multiple-precision |
|
1856 * integer arithmetic as well as number theoretic functionality. |
|
1857 * |
|
1858 * The library was designed directly after the MPI library by |
|
1859 * Michael Fromberger but has been written from scratch with |
|
1860 * additional optimizations in place. |
|
1861 * |
|
1862 * The library is free for all purposes without any express |
|
1863 * guarantee it works. |
|
1864 * |
|
1865 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1866 */ |
3
|
1867 |
|
1868 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ |
|
1869 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d) |
|
1870 { |
|
1871 mp_digit D, r, rr; |
|
1872 int x, res; |
|
1873 mp_int t; |
|
1874 |
|
1875 |
|
1876 /* if the shift count is <= 0 then we do no work */ |
|
1877 if (b <= 0) { |
|
1878 res = mp_copy (a, c); |
|
1879 if (d != NULL) { |
|
1880 mp_zero (d); |
|
1881 } |
|
1882 return res; |
|
1883 } |
|
1884 |
|
1885 if ((res = mp_init (&t)) != MP_OKAY) { |
|
1886 return res; |
|
1887 } |
|
1888 |
|
1889 /* get the remainder */ |
|
1890 if (d != NULL) { |
|
1891 if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) { |
|
1892 mp_clear (&t); |
|
1893 return res; |
|
1894 } |
|
1895 } |
|
1896 |
|
1897 /* copy */ |
|
1898 if ((res = mp_copy (a, c)) != MP_OKAY) { |
|
1899 mp_clear (&t); |
|
1900 return res; |
|
1901 } |
|
1902 |
|
1903 /* shift by as many digits in the bit count */ |
|
1904 if (b >= (int)DIGIT_BIT) { |
|
1905 mp_rshd (c, b / DIGIT_BIT); |
|
1906 } |
|
1907 |
|
1908 /* shift any bit count < DIGIT_BIT */ |
|
1909 D = (mp_digit) (b % DIGIT_BIT); |
|
1910 if (D != 0) { |
|
1911 register mp_digit *tmpc, mask, shift; |
|
1912 |
|
1913 /* mask */ |
|
1914 mask = (((mp_digit)1) << D) - 1; |
|
1915 |
|
1916 /* shift for lsb */ |
|
1917 shift = DIGIT_BIT - D; |
|
1918 |
|
1919 /* alias */ |
|
1920 tmpc = c->dp + (c->used - 1); |
|
1921 |
|
1922 /* carry */ |
|
1923 r = 0; |
|
1924 for (x = c->used - 1; x >= 0; x--) { |
|
1925 /* get the lower bits of this word in a temp */ |
|
1926 rr = *tmpc & mask; |
|
1927 |
|
1928 /* shift the current word and mix in the carry bits from the previous word */ |
|
1929 *tmpc = (*tmpc >> D) | (r << shift); |
|
1930 --tmpc; |
|
1931 |
|
1932 /* set the carry to the carry bits of the current word found above */ |
|
1933 r = rr; |
|
1934 } |
|
1935 } |
|
1936 mp_clamp (c); |
|
1937 if (d != NULL) { |
|
1938 mp_exch (&t, d); |
|
1939 } |
|
1940 mp_clear (&t); |
|
1941 return MP_OKAY; |
|
1942 } |
143
|
1943 #endif |
3
|
1944 |
|
1945 /* End: bn_mp_div_2d.c */ |
|
1946 |
|
1947 /* Start: bn_mp_div_3.c */ |
143
|
1948 #include <ltc_tommath.h> |
|
1949 #ifdef BN_MP_DIV_3_C |
|
1950 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
1951 * |
|
1952 * LibTomMath is a library that provides multiple-precision |
|
1953 * integer arithmetic as well as number theoretic functionality. |
|
1954 * |
|
1955 * The library was designed directly after the MPI library by |
|
1956 * Michael Fromberger but has been written from scratch with |
|
1957 * additional optimizations in place. |
|
1958 * |
|
1959 * The library is free for all purposes without any express |
|
1960 * guarantee it works. |
|
1961 * |
|
1962 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
1963 */ |
3
|
1964 |
|
1965 /* divide by three (based on routine from MPI and the GMP manual) */ |
|
1966 int |
|
1967 mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) |
|
1968 { |
|
1969 mp_int q; |
|
1970 mp_word w, t; |
|
1971 mp_digit b; |
|
1972 int res, ix; |
|
1973 |
|
1974 /* b = 2**DIGIT_BIT / 3 */ |
|
1975 b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); |
|
1976 |
|
1977 if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |
|
1978 return res; |
|
1979 } |
|
1980 |
|
1981 q.used = a->used; |
|
1982 q.sign = a->sign; |
|
1983 w = 0; |
|
1984 for (ix = a->used - 1; ix >= 0; ix--) { |
|
1985 w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |
|
1986 |
|
1987 if (w >= 3) { |
|
1988 /* multiply w by [1/3] */ |
|
1989 t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT); |
|
1990 |
|
1991 /* now subtract 3 * [w/3] from w, to get the remainder */ |
|
1992 w -= t+t+t; |
|
1993 |
|
1994 /* fixup the remainder as required since |
|
1995 * the optimization is not exact. |
|
1996 */ |
|
1997 while (w >= 3) { |
|
1998 t += 1; |
|
1999 w -= 3; |
|
2000 } |
|
2001 } else { |
|
2002 t = 0; |
|
2003 } |
|
2004 q.dp[ix] = (mp_digit)t; |
|
2005 } |
|
2006 |
|
2007 /* [optional] store the remainder */ |
|
2008 if (d != NULL) { |
|
2009 *d = (mp_digit)w; |
|
2010 } |
|
2011 |
|
2012 /* [optional] store the quotient */ |
|
2013 if (c != NULL) { |
|
2014 mp_clamp(&q); |
|
2015 mp_exch(&q, c); |
|
2016 } |
|
2017 mp_clear(&q); |
|
2018 |
|
2019 return res; |
|
2020 } |
|
2021 |
143
|
2022 #endif |
3
|
2023 |
|
2024 /* End: bn_mp_div_3.c */ |
|
2025 |
|
2026 /* Start: bn_mp_div_d.c */ |
143
|
2027 #include <ltc_tommath.h> |
|
2028 #ifdef BN_MP_DIV_D_C |
|
2029 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2030 * |
|
2031 * LibTomMath is a library that provides multiple-precision |
|
2032 * integer arithmetic as well as number theoretic functionality. |
|
2033 * |
|
2034 * The library was designed directly after the MPI library by |
|
2035 * Michael Fromberger but has been written from scratch with |
|
2036 * additional optimizations in place. |
|
2037 * |
|
2038 * The library is free for all purposes without any express |
|
2039 * guarantee it works. |
|
2040 * |
|
2041 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2042 */ |
3
|
2043 |
|
2044 static int s_is_power_of_two(mp_digit b, int *p) |
|
2045 { |
|
2046 int x; |
|
2047 |
|
2048 for (x = 1; x < DIGIT_BIT; x++) { |
|
2049 if (b == (((mp_digit)1)<<x)) { |
|
2050 *p = x; |
|
2051 return 1; |
|
2052 } |
|
2053 } |
|
2054 return 0; |
|
2055 } |
|
2056 |
|
2057 /* single digit division (based on routine from MPI) */ |
|
2058 int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) |
|
2059 { |
|
2060 mp_int q; |
|
2061 mp_word w; |
|
2062 mp_digit t; |
|
2063 int res, ix; |
|
2064 |
|
2065 /* cannot divide by zero */ |
|
2066 if (b == 0) { |
|
2067 return MP_VAL; |
|
2068 } |
|
2069 |
|
2070 /* quick outs */ |
|
2071 if (b == 1 || mp_iszero(a) == 1) { |
|
2072 if (d != NULL) { |
|
2073 *d = 0; |
|
2074 } |
|
2075 if (c != NULL) { |
|
2076 return mp_copy(a, c); |
|
2077 } |
|
2078 return MP_OKAY; |
|
2079 } |
|
2080 |
|
2081 /* power of two ? */ |
|
2082 if (s_is_power_of_two(b, &ix) == 1) { |
|
2083 if (d != NULL) { |
143
|
2084 *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1); |
3
|
2085 } |
|
2086 if (c != NULL) { |
|
2087 return mp_div_2d(a, ix, c, NULL); |
|
2088 } |
|
2089 return MP_OKAY; |
|
2090 } |
|
2091 |
143
|
2092 #ifdef BN_MP_DIV_3_C |
3
|
2093 /* three? */ |
|
2094 if (b == 3) { |
|
2095 return mp_div_3(a, c, d); |
|
2096 } |
143
|
2097 #endif |
3
|
2098 |
|
2099 /* no easy answer [c'est la vie]. Just division */ |
|
2100 if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |
|
2101 return res; |
|
2102 } |
|
2103 |
|
2104 q.used = a->used; |
|
2105 q.sign = a->sign; |
|
2106 w = 0; |
|
2107 for (ix = a->used - 1; ix >= 0; ix--) { |
|
2108 w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |
|
2109 |
|
2110 if (w >= b) { |
|
2111 t = (mp_digit)(w / b); |
|
2112 w -= ((mp_word)t) * ((mp_word)b); |
|
2113 } else { |
|
2114 t = 0; |
|
2115 } |
|
2116 q.dp[ix] = (mp_digit)t; |
|
2117 } |
|
2118 |
|
2119 if (d != NULL) { |
|
2120 *d = (mp_digit)w; |
|
2121 } |
|
2122 |
|
2123 if (c != NULL) { |
|
2124 mp_clamp(&q); |
|
2125 mp_exch(&q, c); |
|
2126 } |
|
2127 mp_clear(&q); |
|
2128 |
|
2129 return res; |
|
2130 } |
|
2131 |
143
|
2132 #endif |
3
|
2133 |
|
2134 /* End: bn_mp_div_d.c */ |
|
2135 |
|
2136 /* Start: bn_mp_dr_is_modulus.c */ |
143
|
2137 #include <ltc_tommath.h> |
|
2138 #ifdef BN_MP_DR_IS_MODULUS_C |
|
2139 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2140 * |
|
2141 * LibTomMath is a library that provides multiple-precision |
|
2142 * integer arithmetic as well as number theoretic functionality. |
|
2143 * |
|
2144 * The library was designed directly after the MPI library by |
|
2145 * Michael Fromberger but has been written from scratch with |
|
2146 * additional optimizations in place. |
|
2147 * |
|
2148 * The library is free for all purposes without any express |
|
2149 * guarantee it works. |
|
2150 * |
|
2151 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2152 */ |
3
|
2153 |
|
2154 /* determines if a number is a valid DR modulus */ |
|
2155 int mp_dr_is_modulus(mp_int *a) |
|
2156 { |
|
2157 int ix; |
|
2158 |
|
2159 /* must be at least two digits */ |
|
2160 if (a->used < 2) { |
|
2161 return 0; |
|
2162 } |
|
2163 |
|
2164 /* must be of the form b**k - a [a <= b] so all |
|
2165 * but the first digit must be equal to -1 (mod b). |
|
2166 */ |
|
2167 for (ix = 1; ix < a->used; ix++) { |
|
2168 if (a->dp[ix] != MP_MASK) { |
|
2169 return 0; |
|
2170 } |
|
2171 } |
|
2172 return 1; |
|
2173 } |
|
2174 |
143
|
2175 #endif |
3
|
2176 |
|
2177 /* End: bn_mp_dr_is_modulus.c */ |
|
2178 |
|
2179 /* Start: bn_mp_dr_reduce.c */ |
143
|
2180 #include <ltc_tommath.h> |
|
2181 #ifdef BN_MP_DR_REDUCE_C |
|
2182 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2183 * |
|
2184 * LibTomMath is a library that provides multiple-precision |
|
2185 * integer arithmetic as well as number theoretic functionality. |
|
2186 * |
|
2187 * The library was designed directly after the MPI library by |
|
2188 * Michael Fromberger but has been written from scratch with |
|
2189 * additional optimizations in place. |
|
2190 * |
|
2191 * The library is free for all purposes without any express |
|
2192 * guarantee it works. |
|
2193 * |
|
2194 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2195 */ |
3
|
2196 |
|
2197 /* reduce "x" in place modulo "n" using the Diminished Radix algorithm. |
|
2198 * |
|
2199 * Based on algorithm from the paper |
|
2200 * |
|
2201 * "Generating Efficient Primes for Discrete Log Cryptosystems" |
|
2202 * Chae Hoon Lim, Pil Loong Lee, |
|
2203 * POSTECH Information Research Laboratories |
|
2204 * |
|
2205 * The modulus must be of a special format [see manual] |
|
2206 * |
|
2207 * Has been modified to use algorithm 7.10 from the LTM book instead |
|
2208 * |
|
2209 * Input x must be in the range 0 <= x <= (n-1)**2 |
|
2210 */ |
|
2211 int |
|
2212 mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k) |
|
2213 { |
|
2214 int err, i, m; |
|
2215 mp_word r; |
|
2216 mp_digit mu, *tmpx1, *tmpx2; |
|
2217 |
|
2218 /* m = digits in modulus */ |
|
2219 m = n->used; |
|
2220 |
|
2221 /* ensure that "x" has at least 2m digits */ |
|
2222 if (x->alloc < m + m) { |
|
2223 if ((err = mp_grow (x, m + m)) != MP_OKAY) { |
|
2224 return err; |
|
2225 } |
|
2226 } |
|
2227 |
|
2228 /* top of loop, this is where the code resumes if |
|
2229 * another reduction pass is required. |
|
2230 */ |
|
2231 top: |
|
2232 /* aliases for digits */ |
|
2233 /* alias for lower half of x */ |
|
2234 tmpx1 = x->dp; |
|
2235 |
|
2236 /* alias for upper half of x, or x/B**m */ |
|
2237 tmpx2 = x->dp + m; |
|
2238 |
|
2239 /* set carry to zero */ |
|
2240 mu = 0; |
|
2241 |
|
2242 /* compute (x mod B**m) + k * [x/B**m] inline and inplace */ |
|
2243 for (i = 0; i < m; i++) { |
|
2244 r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu; |
|
2245 *tmpx1++ = (mp_digit)(r & MP_MASK); |
|
2246 mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); |
|
2247 } |
|
2248 |
|
2249 /* set final carry */ |
|
2250 *tmpx1++ = mu; |
|
2251 |
|
2252 /* zero words above m */ |
|
2253 for (i = m + 1; i < x->used; i++) { |
|
2254 *tmpx1++ = 0; |
|
2255 } |
|
2256 |
|
2257 /* clamp, sub and return */ |
|
2258 mp_clamp (x); |
|
2259 |
|
2260 /* if x >= n then subtract and reduce again |
|
2261 * Each successive "recursion" makes the input smaller and smaller. |
|
2262 */ |
|
2263 if (mp_cmp_mag (x, n) != MP_LT) { |
|
2264 s_mp_sub(x, n, x); |
|
2265 goto top; |
|
2266 } |
|
2267 return MP_OKAY; |
|
2268 } |
143
|
2269 #endif |
3
|
2270 |
|
2271 /* End: bn_mp_dr_reduce.c */ |
|
2272 |
|
2273 /* Start: bn_mp_dr_setup.c */ |
143
|
2274 #include <ltc_tommath.h> |
|
2275 #ifdef BN_MP_DR_SETUP_C |
|
2276 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2277 * |
|
2278 * LibTomMath is a library that provides multiple-precision |
|
2279 * integer arithmetic as well as number theoretic functionality. |
|
2280 * |
|
2281 * The library was designed directly after the MPI library by |
|
2282 * Michael Fromberger but has been written from scratch with |
|
2283 * additional optimizations in place. |
|
2284 * |
|
2285 * The library is free for all purposes without any express |
|
2286 * guarantee it works. |
|
2287 * |
|
2288 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2289 */ |
3
|
2290 |
|
2291 /* determines the setup value */ |
|
2292 void mp_dr_setup(mp_int *a, mp_digit *d) |
|
2293 { |
|
2294 /* the casts are required if DIGIT_BIT is one less than |
|
2295 * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] |
|
2296 */ |
|
2297 *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - |
|
2298 ((mp_word)a->dp[0])); |
|
2299 } |
|
2300 |
143
|
2301 #endif |
3
|
2302 |
|
2303 /* End: bn_mp_dr_setup.c */ |
|
2304 |
|
2305 /* Start: bn_mp_exch.c */ |
143
|
2306 #include <ltc_tommath.h> |
|
2307 #ifdef BN_MP_EXCH_C |
|
2308 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2309 * |
|
2310 * LibTomMath is a library that provides multiple-precision |
|
2311 * integer arithmetic as well as number theoretic functionality. |
|
2312 * |
|
2313 * The library was designed directly after the MPI library by |
|
2314 * Michael Fromberger but has been written from scratch with |
|
2315 * additional optimizations in place. |
|
2316 * |
|
2317 * The library is free for all purposes without any express |
|
2318 * guarantee it works. |
|
2319 * |
|
2320 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2321 */ |
3
|
2322 |
|
2323 /* swap the elements of two integers, for cases where you can't simply swap the |
|
2324 * mp_int pointers around |
|
2325 */ |
|
2326 void |
|
2327 mp_exch (mp_int * a, mp_int * b) |
|
2328 { |
|
2329 mp_int t; |
|
2330 |
|
2331 t = *a; |
|
2332 *a = *b; |
|
2333 *b = t; |
|
2334 } |
143
|
2335 #endif |
3
|
2336 |
|
2337 /* End: bn_mp_exch.c */ |
|
2338 |
|
2339 /* Start: bn_mp_expt_d.c */ |
143
|
2340 #include <ltc_tommath.h> |
|
2341 #ifdef BN_MP_EXPT_D_C |
|
2342 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2343 * |
|
2344 * LibTomMath is a library that provides multiple-precision |
|
2345 * integer arithmetic as well as number theoretic functionality. |
|
2346 * |
|
2347 * The library was designed directly after the MPI library by |
|
2348 * Michael Fromberger but has been written from scratch with |
|
2349 * additional optimizations in place. |
|
2350 * |
|
2351 * The library is free for all purposes without any express |
|
2352 * guarantee it works. |
|
2353 * |
|
2354 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2355 */ |
3
|
2356 |
|
2357 /* calculate c = a**b using a square-multiply algorithm */ |
|
2358 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c) |
|
2359 { |
|
2360 int res, x; |
|
2361 mp_int g; |
|
2362 |
|
2363 if ((res = mp_init_copy (&g, a)) != MP_OKAY) { |
|
2364 return res; |
|
2365 } |
|
2366 |
|
2367 /* set initial result */ |
|
2368 mp_set (c, 1); |
|
2369 |
|
2370 for (x = 0; x < (int) DIGIT_BIT; x++) { |
|
2371 /* square */ |
|
2372 if ((res = mp_sqr (c, c)) != MP_OKAY) { |
|
2373 mp_clear (&g); |
|
2374 return res; |
|
2375 } |
|
2376 |
|
2377 /* if the bit is set multiply */ |
|
2378 if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) { |
|
2379 if ((res = mp_mul (c, &g, c)) != MP_OKAY) { |
|
2380 mp_clear (&g); |
|
2381 return res; |
|
2382 } |
|
2383 } |
|
2384 |
|
2385 /* shift to next bit */ |
|
2386 b <<= 1; |
|
2387 } |
|
2388 |
|
2389 mp_clear (&g); |
|
2390 return MP_OKAY; |
|
2391 } |
143
|
2392 #endif |
3
|
2393 |
|
2394 /* End: bn_mp_expt_d.c */ |
|
2395 |
|
2396 /* Start: bn_mp_exptmod.c */ |
143
|
2397 #include <ltc_tommath.h> |
|
2398 #ifdef BN_MP_EXPTMOD_C |
|
2399 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2400 * |
|
2401 * LibTomMath is a library that provides multiple-precision |
|
2402 * integer arithmetic as well as number theoretic functionality. |
|
2403 * |
|
2404 * The library was designed directly after the MPI library by |
|
2405 * Michael Fromberger but has been written from scratch with |
|
2406 * additional optimizations in place. |
|
2407 * |
|
2408 * The library is free for all purposes without any express |
|
2409 * guarantee it works. |
|
2410 * |
|
2411 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2412 */ |
3
|
2413 |
|
2414 |
|
2415 /* this is a shell function that calls either the normal or Montgomery |
|
2416 * exptmod functions. Originally the call to the montgomery code was |
|
2417 * embedded in the normal function but that wasted alot of stack space |
|
2418 * for nothing (since 99% of the time the Montgomery code would be called) |
|
2419 */ |
|
2420 int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) |
|
2421 { |
|
2422 int dr; |
|
2423 |
|
2424 /* modulus P must be positive */ |
|
2425 if (P->sign == MP_NEG) { |
|
2426 return MP_VAL; |
|
2427 } |
|
2428 |
|
2429 /* if exponent X is negative we have to recurse */ |
|
2430 if (X->sign == MP_NEG) { |
143
|
2431 #ifdef BN_MP_INVMOD_C |
3
|
2432 mp_int tmpG, tmpX; |
|
2433 int err; |
|
2434 |
|
2435 /* first compute 1/G mod P */ |
|
2436 if ((err = mp_init(&tmpG)) != MP_OKAY) { |
|
2437 return err; |
|
2438 } |
|
2439 if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) { |
|
2440 mp_clear(&tmpG); |
|
2441 return err; |
|
2442 } |
|
2443 |
|
2444 /* now get |X| */ |
|
2445 if ((err = mp_init(&tmpX)) != MP_OKAY) { |
|
2446 mp_clear(&tmpG); |
|
2447 return err; |
|
2448 } |
|
2449 if ((err = mp_abs(X, &tmpX)) != MP_OKAY) { |
|
2450 mp_clear_multi(&tmpG, &tmpX, NULL); |
|
2451 return err; |
|
2452 } |
|
2453 |
|
2454 /* and now compute (1/G)**|X| instead of G**X [X < 0] */ |
|
2455 err = mp_exptmod(&tmpG, &tmpX, P, Y); |
|
2456 mp_clear_multi(&tmpG, &tmpX, NULL); |
|
2457 return err; |
143
|
2458 #else |
|
2459 /* no invmod */ |
|
2460 return MP_VAL |
|
2461 #endif |
|
2462 } |
|
2463 |
|
2464 #ifdef BN_MP_DR_IS_MODULUS_C |
3
|
2465 /* is it a DR modulus? */ |
|
2466 dr = mp_dr_is_modulus(P); |
143
|
2467 #else |
|
2468 dr = 0; |
|
2469 #endif |
|
2470 |
|
2471 #ifdef BN_MP_REDUCE_IS_2K_C |
3
|
2472 /* if not, is it a uDR modulus? */ |
|
2473 if (dr == 0) { |
|
2474 dr = mp_reduce_is_2k(P) << 1; |
|
2475 } |
143
|
2476 #endif |
3
|
2477 |
|
2478 /* if the modulus is odd or dr != 0 use the fast method */ |
143
|
2479 #ifdef BN_MP_EXPTMOD_FAST_C |
3
|
2480 if (mp_isodd (P) == 1 || dr != 0) { |
|
2481 return mp_exptmod_fast (G, X, P, Y, dr); |
|
2482 } else { |
143
|
2483 #endif |
|
2484 #ifdef BN_S_MP_EXPTMOD_C |
3
|
2485 /* otherwise use the generic Barrett reduction technique */ |
|
2486 return s_mp_exptmod (G, X, P, Y); |
143
|
2487 #else |
|
2488 /* no exptmod for evens */ |
|
2489 return MP_VAL; |
|
2490 #endif |
|
2491 #ifdef BN_MP_EXPTMOD_FAST_C |
|
2492 } |
|
2493 #endif |
3
|
2494 } |
|
2495 |
143
|
2496 #endif |
3
|
2497 |
|
2498 /* End: bn_mp_exptmod.c */ |
|
2499 |
|
2500 /* Start: bn_mp_exptmod_fast.c */ |
143
|
2501 #include <ltc_tommath.h> |
|
2502 #ifdef BN_MP_EXPTMOD_FAST_C |
|
2503 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2504 * |
|
2505 * LibTomMath is a library that provides multiple-precision |
|
2506 * integer arithmetic as well as number theoretic functionality. |
|
2507 * |
|
2508 * The library was designed directly after the MPI library by |
|
2509 * Michael Fromberger but has been written from scratch with |
|
2510 * additional optimizations in place. |
|
2511 * |
|
2512 * The library is free for all purposes without any express |
|
2513 * guarantee it works. |
|
2514 * |
|
2515 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2516 */ |
3
|
2517 |
|
2518 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85 |
|
2519 * |
|
2520 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation. |
|
2521 * The value of k changes based on the size of the exponent. |
|
2522 * |
|
2523 * Uses Montgomery or Diminished Radix reduction [whichever appropriate] |
|
2524 */ |
|
2525 |
|
2526 #ifdef MP_LOW_MEM |
|
2527 #define TAB_SIZE 32 |
|
2528 #else |
|
2529 #define TAB_SIZE 256 |
|
2530 #endif |
|
2531 |
|
2532 int |
|
2533 mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) |
|
2534 { |
|
2535 mp_int M[TAB_SIZE], res; |
|
2536 mp_digit buf, mp; |
|
2537 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; |
|
2538 |
|
2539 /* use a pointer to the reduction algorithm. This allows us to use |
|
2540 * one of many reduction algorithms without modding the guts of |
|
2541 * the code with if statements everywhere. |
|
2542 */ |
|
2543 int (*redux)(mp_int*,mp_int*,mp_digit); |
|
2544 |
|
2545 /* find window size */ |
|
2546 x = mp_count_bits (X); |
|
2547 if (x <= 7) { |
|
2548 winsize = 2; |
|
2549 } else if (x <= 36) { |
|
2550 winsize = 3; |
|
2551 } else if (x <= 140) { |
|
2552 winsize = 4; |
|
2553 } else if (x <= 450) { |
|
2554 winsize = 5; |
|
2555 } else if (x <= 1303) { |
|
2556 winsize = 6; |
|
2557 } else if (x <= 3529) { |
|
2558 winsize = 7; |
|
2559 } else { |
|
2560 winsize = 8; |
|
2561 } |
|
2562 |
|
2563 #ifdef MP_LOW_MEM |
|
2564 if (winsize > 5) { |
|
2565 winsize = 5; |
|
2566 } |
|
2567 #endif |
|
2568 |
|
2569 /* init M array */ |
|
2570 /* init first cell */ |
|
2571 if ((err = mp_init(&M[1])) != MP_OKAY) { |
|
2572 return err; |
|
2573 } |
|
2574 |
|
2575 /* now init the second half of the array */ |
|
2576 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
|
2577 if ((err = mp_init(&M[x])) != MP_OKAY) { |
|
2578 for (y = 1<<(winsize-1); y < x; y++) { |
|
2579 mp_clear (&M[y]); |
|
2580 } |
|
2581 mp_clear(&M[1]); |
|
2582 return err; |
|
2583 } |
|
2584 } |
|
2585 |
|
2586 /* determine and setup reduction code */ |
|
2587 if (redmode == 0) { |
143
|
2588 #ifdef BN_MP_MONTGOMERY_SETUP_C |
3
|
2589 /* now setup montgomery */ |
|
2590 if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { |
|
2591 goto __M; |
|
2592 } |
143
|
2593 #else |
|
2594 err = MP_VAL; |
|
2595 goto __M; |
|
2596 #endif |
3
|
2597 |
|
2598 /* automatically pick the comba one if available (saves quite a few calls/ifs) */ |
143
|
2599 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C |
3
|
2600 if (((P->used * 2 + 1) < MP_WARRAY) && |
|
2601 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
2602 redux = fast_mp_montgomery_reduce; |
143
|
2603 } else |
|
2604 #endif |
|
2605 { |
|
2606 #ifdef BN_MP_MONTGOMERY_REDUCE_C |
3
|
2607 /* use slower baseline Montgomery method */ |
|
2608 redux = mp_montgomery_reduce; |
143
|
2609 #else |
|
2610 err = MP_VAL; |
|
2611 goto __M; |
|
2612 #endif |
3
|
2613 } |
|
2614 } else if (redmode == 1) { |
143
|
2615 #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C) |
3
|
2616 /* setup DR reduction for moduli of the form B**k - b */ |
|
2617 mp_dr_setup(P, &mp); |
|
2618 redux = mp_dr_reduce; |
143
|
2619 #else |
|
2620 err = MP_VAL; |
|
2621 goto __M; |
|
2622 #endif |
3
|
2623 } else { |
143
|
2624 #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C) |
3
|
2625 /* setup DR reduction for moduli of the form 2**k - b */ |
|
2626 if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) { |
|
2627 goto __M; |
|
2628 } |
|
2629 redux = mp_reduce_2k; |
143
|
2630 #else |
|
2631 err = MP_VAL; |
|
2632 goto __M; |
|
2633 #endif |
3
|
2634 } |
|
2635 |
|
2636 /* setup result */ |
|
2637 if ((err = mp_init (&res)) != MP_OKAY) { |
|
2638 goto __M; |
|
2639 } |
|
2640 |
|
2641 /* create M table |
|
2642 * |
143
|
2643 |
3
|
2644 * |
|
2645 * The first half of the table is not computed though accept for M[0] and M[1] |
|
2646 */ |
|
2647 |
|
2648 if (redmode == 0) { |
143
|
2649 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C |
3
|
2650 /* now we need R mod m */ |
|
2651 if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) { |
|
2652 goto __RES; |
|
2653 } |
143
|
2654 #else |
|
2655 err = MP_VAL; |
|
2656 goto __RES; |
|
2657 #endif |
3
|
2658 |
|
2659 /* now set M[1] to G * R mod m */ |
|
2660 if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) { |
|
2661 goto __RES; |
|
2662 } |
|
2663 } else { |
|
2664 mp_set(&res, 1); |
|
2665 if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { |
|
2666 goto __RES; |
|
2667 } |
|
2668 } |
|
2669 |
|
2670 /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */ |
|
2671 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |
|
2672 goto __RES; |
|
2673 } |
|
2674 |
|
2675 for (x = 0; x < (winsize - 1); x++) { |
|
2676 if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { |
|
2677 goto __RES; |
|
2678 } |
|
2679 if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) { |
|
2680 goto __RES; |
|
2681 } |
|
2682 } |
|
2683 |
|
2684 /* create upper table */ |
|
2685 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { |
|
2686 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { |
|
2687 goto __RES; |
|
2688 } |
|
2689 if ((err = redux (&M[x], P, mp)) != MP_OKAY) { |
|
2690 goto __RES; |
|
2691 } |
|
2692 } |
|
2693 |
|
2694 /* set initial mode and bit cnt */ |
|
2695 mode = 0; |
|
2696 bitcnt = 1; |
|
2697 buf = 0; |
|
2698 digidx = X->used - 1; |
|
2699 bitcpy = 0; |
|
2700 bitbuf = 0; |
|
2701 |
|
2702 for (;;) { |
|
2703 /* grab next digit as required */ |
|
2704 if (--bitcnt == 0) { |
|
2705 /* if digidx == -1 we are out of digits so break */ |
|
2706 if (digidx == -1) { |
|
2707 break; |
|
2708 } |
|
2709 /* read next digit and reset bitcnt */ |
|
2710 buf = X->dp[digidx--]; |
|
2711 bitcnt = (int)DIGIT_BIT; |
|
2712 } |
|
2713 |
|
2714 /* grab the next msb from the exponent */ |
|
2715 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1; |
|
2716 buf <<= (mp_digit)1; |
|
2717 |
|
2718 /* if the bit is zero and mode == 0 then we ignore it |
|
2719 * These represent the leading zero bits before the first 1 bit |
|
2720 * in the exponent. Technically this opt is not required but it |
|
2721 * does lower the # of trivial squaring/reductions used |
|
2722 */ |
|
2723 if (mode == 0 && y == 0) { |
|
2724 continue; |
|
2725 } |
|
2726 |
|
2727 /* if the bit is zero and mode == 1 then we square */ |
|
2728 if (mode == 1 && y == 0) { |
|
2729 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
2730 goto __RES; |
|
2731 } |
|
2732 if ((err = redux (&res, P, mp)) != MP_OKAY) { |
|
2733 goto __RES; |
|
2734 } |
|
2735 continue; |
|
2736 } |
|
2737 |
|
2738 /* else we add it to the window */ |
|
2739 bitbuf |= (y << (winsize - ++bitcpy)); |
|
2740 mode = 2; |
|
2741 |
|
2742 if (bitcpy == winsize) { |
|
2743 /* ok window is filled so square as required and multiply */ |
|
2744 /* square first */ |
|
2745 for (x = 0; x < winsize; x++) { |
|
2746 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
2747 goto __RES; |
|
2748 } |
|
2749 if ((err = redux (&res, P, mp)) != MP_OKAY) { |
|
2750 goto __RES; |
|
2751 } |
|
2752 } |
|
2753 |
|
2754 /* then multiply */ |
|
2755 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { |
|
2756 goto __RES; |
|
2757 } |
|
2758 if ((err = redux (&res, P, mp)) != MP_OKAY) { |
|
2759 goto __RES; |
|
2760 } |
|
2761 |
|
2762 /* empty window and reset */ |
|
2763 bitcpy = 0; |
|
2764 bitbuf = 0; |
|
2765 mode = 1; |
|
2766 } |
|
2767 } |
|
2768 |
|
2769 /* if bits remain then square/multiply */ |
|
2770 if (mode == 2 && bitcpy > 0) { |
|
2771 /* square then multiply if the bit is set */ |
|
2772 for (x = 0; x < bitcpy; x++) { |
|
2773 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
2774 goto __RES; |
|
2775 } |
|
2776 if ((err = redux (&res, P, mp)) != MP_OKAY) { |
|
2777 goto __RES; |
|
2778 } |
|
2779 |
|
2780 /* get next bit of the window */ |
|
2781 bitbuf <<= 1; |
|
2782 if ((bitbuf & (1 << winsize)) != 0) { |
|
2783 /* then multiply */ |
|
2784 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { |
|
2785 goto __RES; |
|
2786 } |
|
2787 if ((err = redux (&res, P, mp)) != MP_OKAY) { |
|
2788 goto __RES; |
|
2789 } |
|
2790 } |
|
2791 } |
|
2792 } |
|
2793 |
|
2794 if (redmode == 0) { |
|
2795 /* fixup result if Montgomery reduction is used |
|
2796 * recall that any value in a Montgomery system is |
|
2797 * actually multiplied by R mod n. So we have |
|
2798 * to reduce one more time to cancel out the factor |
|
2799 * of R. |
|
2800 */ |
143
|
2801 if ((err = redux(&res, P, mp)) != MP_OKAY) { |
3
|
2802 goto __RES; |
|
2803 } |
|
2804 } |
|
2805 |
|
2806 /* swap res with Y */ |
|
2807 mp_exch (&res, Y); |
|
2808 err = MP_OKAY; |
|
2809 __RES:mp_clear (&res); |
|
2810 __M: |
|
2811 mp_clear(&M[1]); |
|
2812 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
|
2813 mp_clear (&M[x]); |
|
2814 } |
|
2815 return err; |
|
2816 } |
143
|
2817 #endif |
|
2818 |
3
|
2819 |
|
2820 /* End: bn_mp_exptmod_fast.c */ |
|
2821 |
|
2822 /* Start: bn_mp_exteuclid.c */ |
143
|
2823 #include <ltc_tommath.h> |
|
2824 #ifdef BN_MP_EXTEUCLID_C |
|
2825 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2826 * |
|
2827 * LibTomMath is a library that provides multiple-precision |
|
2828 * integer arithmetic as well as number theoretic functionality. |
|
2829 * |
|
2830 * The library was designed directly after the MPI library by |
|
2831 * Michael Fromberger but has been written from scratch with |
|
2832 * additional optimizations in place. |
|
2833 * |
|
2834 * The library is free for all purposes without any express |
|
2835 * guarantee it works. |
|
2836 * |
|
2837 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2838 */ |
3
|
2839 |
|
2840 /* Extended euclidean algorithm of (a, b) produces |
|
2841 a*u1 + b*u2 = u3 |
|
2842 */ |
|
2843 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) |
|
2844 { |
|
2845 mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp; |
|
2846 int err; |
|
2847 |
|
2848 if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) { |
|
2849 return err; |
|
2850 } |
|
2851 |
|
2852 /* initialize, (u1,u2,u3) = (1,0,a) */ |
|
2853 mp_set(&u1, 1); |
|
2854 if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto _ERR; } |
|
2855 |
|
2856 /* initialize, (v1,v2,v3) = (0,1,b) */ |
|
2857 mp_set(&v2, 1); |
|
2858 if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto _ERR; } |
|
2859 |
|
2860 /* loop while v3 != 0 */ |
|
2861 while (mp_iszero(&v3) == MP_NO) { |
|
2862 /* q = u3/v3 */ |
|
2863 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto _ERR; } |
|
2864 |
|
2865 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ |
|
2866 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto _ERR; } |
|
2867 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto _ERR; } |
|
2868 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto _ERR; } |
|
2869 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto _ERR; } |
|
2870 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto _ERR; } |
|
2871 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto _ERR; } |
|
2872 |
|
2873 /* (u1,u2,u3) = (v1,v2,v3) */ |
|
2874 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto _ERR; } |
|
2875 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto _ERR; } |
|
2876 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto _ERR; } |
|
2877 |
|
2878 /* (v1,v2,v3) = (t1,t2,t3) */ |
|
2879 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto _ERR; } |
|
2880 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto _ERR; } |
|
2881 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto _ERR; } |
|
2882 } |
|
2883 |
|
2884 /* copy result out */ |
|
2885 if (U1 != NULL) { mp_exch(U1, &u1); } |
|
2886 if (U2 != NULL) { mp_exch(U2, &u2); } |
|
2887 if (U3 != NULL) { mp_exch(U3, &u3); } |
|
2888 |
|
2889 err = MP_OKAY; |
|
2890 _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); |
|
2891 return err; |
|
2892 } |
143
|
2893 #endif |
3
|
2894 |
|
2895 /* End: bn_mp_exteuclid.c */ |
|
2896 |
|
2897 /* Start: bn_mp_fread.c */ |
143
|
2898 #include <ltc_tommath.h> |
|
2899 #ifdef BN_MP_FREAD_C |
|
2900 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2901 * |
|
2902 * LibTomMath is a library that provides multiple-precision |
|
2903 * integer arithmetic as well as number theoretic functionality. |
|
2904 * |
|
2905 * The library was designed directly after the MPI library by |
|
2906 * Michael Fromberger but has been written from scratch with |
|
2907 * additional optimizations in place. |
|
2908 * |
|
2909 * The library is free for all purposes without any express |
|
2910 * guarantee it works. |
|
2911 * |
|
2912 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2913 */ |
3
|
2914 |
|
2915 /* read a bigint from a file stream in ASCII */ |
|
2916 int mp_fread(mp_int *a, int radix, FILE *stream) |
|
2917 { |
|
2918 int err, ch, neg, y; |
|
2919 |
|
2920 /* clear a */ |
|
2921 mp_zero(a); |
|
2922 |
|
2923 /* if first digit is - then set negative */ |
|
2924 ch = fgetc(stream); |
|
2925 if (ch == '-') { |
|
2926 neg = MP_NEG; |
|
2927 ch = fgetc(stream); |
|
2928 } else { |
|
2929 neg = MP_ZPOS; |
|
2930 } |
|
2931 |
|
2932 for (;;) { |
|
2933 /* find y in the radix map */ |
|
2934 for (y = 0; y < radix; y++) { |
|
2935 if (mp_s_rmap[y] == ch) { |
|
2936 break; |
|
2937 } |
|
2938 } |
|
2939 if (y == radix) { |
|
2940 break; |
|
2941 } |
|
2942 |
|
2943 /* shift up and add */ |
|
2944 if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { |
|
2945 return err; |
|
2946 } |
|
2947 if ((err = mp_add_d(a, y, a)) != MP_OKAY) { |
|
2948 return err; |
|
2949 } |
|
2950 |
|
2951 ch = fgetc(stream); |
|
2952 } |
|
2953 if (mp_cmp_d(a, 0) != MP_EQ) { |
|
2954 a->sign = neg; |
|
2955 } |
|
2956 |
|
2957 return MP_OKAY; |
|
2958 } |
|
2959 |
143
|
2960 #endif |
3
|
2961 |
|
2962 /* End: bn_mp_fread.c */ |
|
2963 |
|
2964 /* Start: bn_mp_fwrite.c */ |
143
|
2965 #include <ltc_tommath.h> |
|
2966 #ifdef BN_MP_FWRITE_C |
|
2967 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2968 * |
|
2969 * LibTomMath is a library that provides multiple-precision |
|
2970 * integer arithmetic as well as number theoretic functionality. |
|
2971 * |
|
2972 * The library was designed directly after the MPI library by |
|
2973 * Michael Fromberger but has been written from scratch with |
|
2974 * additional optimizations in place. |
|
2975 * |
|
2976 * The library is free for all purposes without any express |
|
2977 * guarantee it works. |
|
2978 * |
|
2979 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
2980 */ |
3
|
2981 |
|
2982 int mp_fwrite(mp_int *a, int radix, FILE *stream) |
|
2983 { |
|
2984 char *buf; |
|
2985 int err, len, x; |
|
2986 |
|
2987 if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { |
|
2988 return err; |
|
2989 } |
|
2990 |
|
2991 buf = OPT_CAST(char) XMALLOC (len); |
|
2992 if (buf == NULL) { |
|
2993 return MP_MEM; |
|
2994 } |
|
2995 |
|
2996 if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { |
|
2997 XFREE (buf); |
|
2998 return err; |
|
2999 } |
|
3000 |
|
3001 for (x = 0; x < len; x++) { |
|
3002 if (fputc(buf[x], stream) == EOF) { |
|
3003 XFREE (buf); |
|
3004 return MP_VAL; |
|
3005 } |
|
3006 } |
|
3007 |
|
3008 XFREE (buf); |
|
3009 return MP_OKAY; |
|
3010 } |
|
3011 |
143
|
3012 #endif |
3
|
3013 |
|
3014 /* End: bn_mp_fwrite.c */ |
|
3015 |
|
3016 /* Start: bn_mp_gcd.c */ |
143
|
3017 #include <ltc_tommath.h> |
|
3018 #ifdef BN_MP_GCD_C |
|
3019 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3020 * |
|
3021 * LibTomMath is a library that provides multiple-precision |
|
3022 * integer arithmetic as well as number theoretic functionality. |
|
3023 * |
|
3024 * The library was designed directly after the MPI library by |
|
3025 * Michael Fromberger but has been written from scratch with |
|
3026 * additional optimizations in place. |
|
3027 * |
|
3028 * The library is free for all purposes without any express |
|
3029 * guarantee it works. |
|
3030 * |
|
3031 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3032 */ |
3
|
3033 |
|
3034 /* Greatest Common Divisor using the binary method */ |
|
3035 int mp_gcd (mp_int * a, mp_int * b, mp_int * c) |
|
3036 { |
|
3037 mp_int u, v; |
|
3038 int k, u_lsb, v_lsb, res; |
|
3039 |
|
3040 /* either zero than gcd is the largest */ |
|
3041 if (mp_iszero (a) == 1 && mp_iszero (b) == 0) { |
|
3042 return mp_abs (b, c); |
|
3043 } |
|
3044 if (mp_iszero (a) == 0 && mp_iszero (b) == 1) { |
|
3045 return mp_abs (a, c); |
|
3046 } |
|
3047 |
|
3048 /* optimized. At this point if a == 0 then |
|
3049 * b must equal zero too |
|
3050 */ |
|
3051 if (mp_iszero (a) == 1) { |
|
3052 mp_zero(c); |
|
3053 return MP_OKAY; |
|
3054 } |
|
3055 |
|
3056 /* get copies of a and b we can modify */ |
|
3057 if ((res = mp_init_copy (&u, a)) != MP_OKAY) { |
|
3058 return res; |
|
3059 } |
|
3060 |
|
3061 if ((res = mp_init_copy (&v, b)) != MP_OKAY) { |
|
3062 goto __U; |
|
3063 } |
|
3064 |
|
3065 /* must be positive for the remainder of the algorithm */ |
|
3066 u.sign = v.sign = MP_ZPOS; |
|
3067 |
|
3068 /* B1. Find the common power of two for u and v */ |
|
3069 u_lsb = mp_cnt_lsb(&u); |
|
3070 v_lsb = mp_cnt_lsb(&v); |
|
3071 k = MIN(u_lsb, v_lsb); |
|
3072 |
|
3073 if (k > 0) { |
|
3074 /* divide the power of two out */ |
|
3075 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) { |
|
3076 goto __V; |
|
3077 } |
|
3078 |
|
3079 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) { |
|
3080 goto __V; |
|
3081 } |
|
3082 } |
|
3083 |
|
3084 /* divide any remaining factors of two out */ |
|
3085 if (u_lsb != k) { |
|
3086 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { |
|
3087 goto __V; |
|
3088 } |
|
3089 } |
|
3090 |
|
3091 if (v_lsb != k) { |
|
3092 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { |
|
3093 goto __V; |
|
3094 } |
|
3095 } |
|
3096 |
|
3097 while (mp_iszero(&v) == 0) { |
|
3098 /* make sure v is the largest */ |
|
3099 if (mp_cmp_mag(&u, &v) == MP_GT) { |
|
3100 /* swap u and v to make sure v is >= u */ |
|
3101 mp_exch(&u, &v); |
|
3102 } |
|
3103 |
|
3104 /* subtract smallest from largest */ |
|
3105 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { |
|
3106 goto __V; |
|
3107 } |
|
3108 |
|
3109 /* Divide out all factors of two */ |
|
3110 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { |
|
3111 goto __V; |
|
3112 } |
|
3113 } |
|
3114 |
|
3115 /* multiply by 2**k which we divided out at the beginning */ |
|
3116 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { |
|
3117 goto __V; |
|
3118 } |
|
3119 c->sign = MP_ZPOS; |
|
3120 res = MP_OKAY; |
|
3121 __V:mp_clear (&u); |
|
3122 __U:mp_clear (&v); |
|
3123 return res; |
|
3124 } |
143
|
3125 #endif |
3
|
3126 |
|
3127 /* End: bn_mp_gcd.c */ |
|
3128 |
|
3129 /* Start: bn_mp_get_int.c */ |
143
|
3130 #include <ltc_tommath.h> |
|
3131 #ifdef BN_MP_GET_INT_C |
|
3132 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3133 * |
|
3134 * LibTomMath is a library that provides multiple-precision |
|
3135 * integer arithmetic as well as number theoretic functionality. |
|
3136 * |
|
3137 * The library was designed directly after the MPI library by |
|
3138 * Michael Fromberger but has been written from scratch with |
|
3139 * additional optimizations in place. |
|
3140 * |
|
3141 * The library is free for all purposes without any express |
|
3142 * guarantee it works. |
|
3143 * |
|
3144 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3145 */ |
3
|
3146 |
|
3147 /* get the lower 32-bits of an mp_int */ |
|
3148 unsigned long mp_get_int(mp_int * a) |
|
3149 { |
|
3150 int i; |
|
3151 unsigned long res; |
|
3152 |
|
3153 if (a->used == 0) { |
|
3154 return 0; |
|
3155 } |
|
3156 |
|
3157 /* get number of digits of the lsb we have to read */ |
|
3158 i = MIN(a->used,(int)((sizeof(unsigned long)*CHAR_BIT+DIGIT_BIT-1)/DIGIT_BIT))-1; |
|
3159 |
|
3160 /* get most significant digit of result */ |
|
3161 res = DIGIT(a,i); |
|
3162 |
|
3163 while (--i >= 0) { |
|
3164 res = (res << DIGIT_BIT) | DIGIT(a,i); |
|
3165 } |
|
3166 |
|
3167 /* force result to 32-bits always so it is consistent on non 32-bit platforms */ |
|
3168 return res & 0xFFFFFFFFUL; |
|
3169 } |
143
|
3170 #endif |
3
|
3171 |
|
3172 /* End: bn_mp_get_int.c */ |
|
3173 |
|
3174 /* Start: bn_mp_grow.c */ |
143
|
3175 #include <ltc_tommath.h> |
|
3176 #ifdef BN_MP_GROW_C |
|
3177 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3178 * |
|
3179 * LibTomMath is a library that provides multiple-precision |
|
3180 * integer arithmetic as well as number theoretic functionality. |
|
3181 * |
|
3182 * The library was designed directly after the MPI library by |
|
3183 * Michael Fromberger but has been written from scratch with |
|
3184 * additional optimizations in place. |
|
3185 * |
|
3186 * The library is free for all purposes without any express |
|
3187 * guarantee it works. |
|
3188 * |
|
3189 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3190 */ |
3
|
3191 |
|
3192 /* grow as required */ |
|
3193 int mp_grow (mp_int * a, int size) |
|
3194 { |
|
3195 int i; |
|
3196 mp_digit *tmp; |
|
3197 |
|
3198 /* if the alloc size is smaller alloc more ram */ |
|
3199 if (a->alloc < size) { |
|
3200 /* ensure there are always at least MP_PREC digits extra on top */ |
|
3201 size += (MP_PREC * 2) - (size % MP_PREC); |
|
3202 |
|
3203 /* reallocate the array a->dp |
|
3204 * |
|
3205 * We store the return in a temporary variable |
|
3206 * in case the operation failed we don't want |
|
3207 * to overwrite the dp member of a. |
|
3208 */ |
|
3209 tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size); |
|
3210 if (tmp == NULL) { |
|
3211 /* reallocation failed but "a" is still valid [can be freed] */ |
|
3212 return MP_MEM; |
|
3213 } |
|
3214 |
|
3215 /* reallocation succeeded so set a->dp */ |
|
3216 a->dp = tmp; |
|
3217 |
|
3218 /* zero excess digits */ |
|
3219 i = a->alloc; |
|
3220 a->alloc = size; |
|
3221 for (; i < a->alloc; i++) { |
|
3222 a->dp[i] = 0; |
|
3223 } |
|
3224 } |
|
3225 return MP_OKAY; |
|
3226 } |
143
|
3227 #endif |
3
|
3228 |
|
3229 /* End: bn_mp_grow.c */ |
|
3230 |
|
3231 /* Start: bn_mp_init.c */ |
143
|
3232 #include <ltc_tommath.h> |
|
3233 #ifdef BN_MP_INIT_C |
|
3234 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3235 * |
|
3236 * LibTomMath is a library that provides multiple-precision |
|
3237 * integer arithmetic as well as number theoretic functionality. |
|
3238 * |
|
3239 * The library was designed directly after the MPI library by |
|
3240 * Michael Fromberger but has been written from scratch with |
|
3241 * additional optimizations in place. |
|
3242 * |
|
3243 * The library is free for all purposes without any express |
|
3244 * guarantee it works. |
|
3245 * |
|
3246 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3247 */ |
|
3248 |
|
3249 /* init a new mp_int */ |
3
|
3250 int mp_init (mp_int * a) |
|
3251 { |
143
|
3252 int i; |
|
3253 |
3
|
3254 /* allocate memory required and clear it */ |
143
|
3255 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC); |
3
|
3256 if (a->dp == NULL) { |
|
3257 return MP_MEM; |
|
3258 } |
|
3259 |
143
|
3260 /* set the digits to zero */ |
|
3261 for (i = 0; i < MP_PREC; i++) { |
|
3262 a->dp[i] = 0; |
|
3263 } |
|
3264 |
3
|
3265 /* set the used to zero, allocated digits to the default precision |
|
3266 * and sign to positive */ |
|
3267 a->used = 0; |
|
3268 a->alloc = MP_PREC; |
|
3269 a->sign = MP_ZPOS; |
|
3270 |
|
3271 return MP_OKAY; |
|
3272 } |
143
|
3273 #endif |
3
|
3274 |
|
3275 /* End: bn_mp_init.c */ |
|
3276 |
|
3277 /* Start: bn_mp_init_copy.c */ |
143
|
3278 #include <ltc_tommath.h> |
|
3279 #ifdef BN_MP_INIT_COPY_C |
|
3280 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3281 * |
|
3282 * LibTomMath is a library that provides multiple-precision |
|
3283 * integer arithmetic as well as number theoretic functionality. |
|
3284 * |
|
3285 * The library was designed directly after the MPI library by |
|
3286 * Michael Fromberger but has been written from scratch with |
|
3287 * additional optimizations in place. |
|
3288 * |
|
3289 * The library is free for all purposes without any express |
|
3290 * guarantee it works. |
|
3291 * |
|
3292 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3293 */ |
3
|
3294 |
|
3295 /* creates "a" then copies b into it */ |
|
3296 int mp_init_copy (mp_int * a, mp_int * b) |
|
3297 { |
|
3298 int res; |
|
3299 |
|
3300 if ((res = mp_init (a)) != MP_OKAY) { |
|
3301 return res; |
|
3302 } |
|
3303 return mp_copy (b, a); |
|
3304 } |
143
|
3305 #endif |
3
|
3306 |
|
3307 /* End: bn_mp_init_copy.c */ |
|
3308 |
|
3309 /* Start: bn_mp_init_multi.c */ |
143
|
3310 #include <ltc_tommath.h> |
|
3311 #ifdef BN_MP_INIT_MULTI_C |
|
3312 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3313 * |
|
3314 * LibTomMath is a library that provides multiple-precision |
|
3315 * integer arithmetic as well as number theoretic functionality. |
|
3316 * |
|
3317 * The library was designed directly after the MPI library by |
|
3318 * Michael Fromberger but has been written from scratch with |
|
3319 * additional optimizations in place. |
|
3320 * |
|
3321 * The library is free for all purposes without any express |
|
3322 * guarantee it works. |
|
3323 * |
|
3324 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3325 */ |
3
|
3326 #include <stdarg.h> |
|
3327 |
|
3328 int mp_init_multi(mp_int *mp, ...) |
|
3329 { |
|
3330 mp_err res = MP_OKAY; /* Assume ok until proven otherwise */ |
|
3331 int n = 0; /* Number of ok inits */ |
|
3332 mp_int* cur_arg = mp; |
|
3333 va_list args; |
|
3334 |
|
3335 va_start(args, mp); /* init args to next argument from caller */ |
|
3336 while (cur_arg != NULL) { |
|
3337 if (mp_init(cur_arg) != MP_OKAY) { |
|
3338 /* Oops - error! Back-track and mp_clear what we already |
|
3339 succeeded in init-ing, then return error. |
|
3340 */ |
|
3341 va_list clean_args; |
|
3342 |
|
3343 /* end the current list */ |
|
3344 va_end(args); |
|
3345 |
|
3346 /* now start cleaning up */ |
|
3347 cur_arg = mp; |
|
3348 va_start(clean_args, mp); |
|
3349 while (n--) { |
|
3350 mp_clear(cur_arg); |
|
3351 cur_arg = va_arg(clean_args, mp_int*); |
|
3352 } |
|
3353 va_end(clean_args); |
|
3354 res = MP_MEM; |
|
3355 break; |
|
3356 } |
|
3357 n++; |
|
3358 cur_arg = va_arg(args, mp_int*); |
|
3359 } |
|
3360 va_end(args); |
|
3361 return res; /* Assumed ok, if error flagged above. */ |
|
3362 } |
|
3363 |
143
|
3364 #endif |
3
|
3365 |
|
3366 /* End: bn_mp_init_multi.c */ |
|
3367 |
|
3368 /* Start: bn_mp_init_set.c */ |
143
|
3369 #include <ltc_tommath.h> |
|
3370 #ifdef BN_MP_INIT_SET_C |
|
3371 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3372 * |
|
3373 * LibTomMath is a library that provides multiple-precision |
|
3374 * integer arithmetic as well as number theoretic functionality. |
|
3375 * |
|
3376 * The library was designed directly after the MPI library by |
|
3377 * Michael Fromberger but has been written from scratch with |
|
3378 * additional optimizations in place. |
|
3379 * |
|
3380 * The library is free for all purposes without any express |
|
3381 * guarantee it works. |
|
3382 * |
|
3383 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3384 */ |
3
|
3385 |
|
3386 /* initialize and set a digit */ |
|
3387 int mp_init_set (mp_int * a, mp_digit b) |
|
3388 { |
|
3389 int err; |
|
3390 if ((err = mp_init(a)) != MP_OKAY) { |
|
3391 return err; |
|
3392 } |
|
3393 mp_set(a, b); |
|
3394 return err; |
|
3395 } |
143
|
3396 #endif |
3
|
3397 |
|
3398 /* End: bn_mp_init_set.c */ |
|
3399 |
|
3400 /* Start: bn_mp_init_set_int.c */ |
143
|
3401 #include <ltc_tommath.h> |
|
3402 #ifdef BN_MP_INIT_SET_INT_C |
|
3403 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3404 * |
|
3405 * LibTomMath is a library that provides multiple-precision |
|
3406 * integer arithmetic as well as number theoretic functionality. |
|
3407 * |
|
3408 * The library was designed directly after the MPI library by |
|
3409 * Michael Fromberger but has been written from scratch with |
|
3410 * additional optimizations in place. |
|
3411 * |
|
3412 * The library is free for all purposes without any express |
|
3413 * guarantee it works. |
|
3414 * |
|
3415 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3416 */ |
3
|
3417 |
|
3418 /* initialize and set a digit */ |
|
3419 int mp_init_set_int (mp_int * a, unsigned long b) |
|
3420 { |
|
3421 int err; |
|
3422 if ((err = mp_init(a)) != MP_OKAY) { |
|
3423 return err; |
|
3424 } |
|
3425 return mp_set_int(a, b); |
|
3426 } |
143
|
3427 #endif |
3
|
3428 |
|
3429 /* End: bn_mp_init_set_int.c */ |
|
3430 |
|
3431 /* Start: bn_mp_init_size.c */ |
143
|
3432 #include <ltc_tommath.h> |
|
3433 #ifdef BN_MP_INIT_SIZE_C |
|
3434 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3435 * |
|
3436 * LibTomMath is a library that provides multiple-precision |
|
3437 * integer arithmetic as well as number theoretic functionality. |
|
3438 * |
|
3439 * The library was designed directly after the MPI library by |
|
3440 * Michael Fromberger but has been written from scratch with |
|
3441 * additional optimizations in place. |
|
3442 * |
|
3443 * The library is free for all purposes without any express |
|
3444 * guarantee it works. |
|
3445 * |
|
3446 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3447 */ |
3
|
3448 |
|
3449 /* init an mp_init for a given size */ |
|
3450 int mp_init_size (mp_int * a, int size) |
|
3451 { |
143
|
3452 int x; |
|
3453 |
3
|
3454 /* pad size so there are always extra digits */ |
|
3455 size += (MP_PREC * 2) - (size % MP_PREC); |
|
3456 |
|
3457 /* alloc mem */ |
143
|
3458 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size); |
3
|
3459 if (a->dp == NULL) { |
|
3460 return MP_MEM; |
|
3461 } |
143
|
3462 |
|
3463 /* set the members */ |
3
|
3464 a->used = 0; |
|
3465 a->alloc = size; |
|
3466 a->sign = MP_ZPOS; |
|
3467 |
143
|
3468 /* zero the digits */ |
|
3469 for (x = 0; x < size; x++) { |
|
3470 a->dp[x] = 0; |
|
3471 } |
|
3472 |
3
|
3473 return MP_OKAY; |
|
3474 } |
143
|
3475 #endif |
3
|
3476 |
|
3477 /* End: bn_mp_init_size.c */ |
|
3478 |
|
3479 /* Start: bn_mp_invmod.c */ |
143
|
3480 #include <ltc_tommath.h> |
|
3481 #ifdef BN_MP_INVMOD_C |
|
3482 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3483 * |
|
3484 * LibTomMath is a library that provides multiple-precision |
|
3485 * integer arithmetic as well as number theoretic functionality. |
|
3486 * |
|
3487 * The library was designed directly after the MPI library by |
|
3488 * Michael Fromberger but has been written from scratch with |
|
3489 * additional optimizations in place. |
|
3490 * |
|
3491 * The library is free for all purposes without any express |
|
3492 * guarantee it works. |
|
3493 * |
|
3494 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3495 */ |
3
|
3496 |
|
3497 /* hac 14.61, pp608 */ |
|
3498 int mp_invmod (mp_int * a, mp_int * b, mp_int * c) |
|
3499 { |
143
|
3500 /* b cannot be negative */ |
|
3501 if (b->sign == MP_NEG || mp_iszero(b) == 1) { |
|
3502 return MP_VAL; |
|
3503 } |
|
3504 |
|
3505 #ifdef BN_FAST_MP_INVMOD_C |
|
3506 /* if the modulus is odd we can use a faster routine instead */ |
|
3507 if (mp_isodd (b) == 1) { |
|
3508 return fast_mp_invmod (a, b, c); |
|
3509 } |
|
3510 #endif |
|
3511 |
|
3512 #ifdef BN_MP_INVMOD_SLOW_C |
|
3513 return mp_invmod_slow(a, b, c); |
|
3514 #endif |
|
3515 |
|
3516 return MP_VAL; |
|
3517 } |
|
3518 #endif |
|
3519 |
|
3520 /* End: bn_mp_invmod.c */ |
|
3521 |
|
3522 /* Start: bn_mp_invmod_slow.c */ |
|
3523 #include <ltc_tommath.h> |
|
3524 #ifdef BN_MP_INVMOD_SLOW_C |
|
3525 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3526 * |
|
3527 * LibTomMath is a library that provides multiple-precision |
|
3528 * integer arithmetic as well as number theoretic functionality. |
|
3529 * |
|
3530 * The library was designed directly after the MPI library by |
|
3531 * Michael Fromberger but has been written from scratch with |
|
3532 * additional optimizations in place. |
|
3533 * |
|
3534 * The library is free for all purposes without any express |
|
3535 * guarantee it works. |
|
3536 * |
|
3537 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3538 */ |
|
3539 |
|
3540 /* hac 14.61, pp608 */ |
|
3541 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) |
|
3542 { |
3
|
3543 mp_int x, y, u, v, A, B, C, D; |
|
3544 int res; |
|
3545 |
|
3546 /* b cannot be negative */ |
|
3547 if (b->sign == MP_NEG || mp_iszero(b) == 1) { |
|
3548 return MP_VAL; |
|
3549 } |
|
3550 |
|
3551 /* init temps */ |
|
3552 if ((res = mp_init_multi(&x, &y, &u, &v, |
|
3553 &A, &B, &C, &D, NULL)) != MP_OKAY) { |
|
3554 return res; |
|
3555 } |
|
3556 |
|
3557 /* x = a, y = b */ |
|
3558 if ((res = mp_copy (a, &x)) != MP_OKAY) { |
|
3559 goto __ERR; |
|
3560 } |
|
3561 if ((res = mp_copy (b, &y)) != MP_OKAY) { |
|
3562 goto __ERR; |
|
3563 } |
|
3564 |
|
3565 /* 2. [modified] if x,y are both even then return an error! */ |
|
3566 if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) { |
|
3567 res = MP_VAL; |
|
3568 goto __ERR; |
|
3569 } |
|
3570 |
|
3571 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ |
|
3572 if ((res = mp_copy (&x, &u)) != MP_OKAY) { |
|
3573 goto __ERR; |
|
3574 } |
|
3575 if ((res = mp_copy (&y, &v)) != MP_OKAY) { |
|
3576 goto __ERR; |
|
3577 } |
|
3578 mp_set (&A, 1); |
|
3579 mp_set (&D, 1); |
|
3580 |
|
3581 top: |
|
3582 /* 4. while u is even do */ |
|
3583 while (mp_iseven (&u) == 1) { |
|
3584 /* 4.1 u = u/2 */ |
|
3585 if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { |
|
3586 goto __ERR; |
|
3587 } |
|
3588 /* 4.2 if A or B is odd then */ |
|
3589 if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) { |
|
3590 /* A = (A+y)/2, B = (B-x)/2 */ |
|
3591 if ((res = mp_add (&A, &y, &A)) != MP_OKAY) { |
|
3592 goto __ERR; |
|
3593 } |
|
3594 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { |
|
3595 goto __ERR; |
|
3596 } |
|
3597 } |
|
3598 /* A = A/2, B = B/2 */ |
|
3599 if ((res = mp_div_2 (&A, &A)) != MP_OKAY) { |
|
3600 goto __ERR; |
|
3601 } |
|
3602 if ((res = mp_div_2 (&B, &B)) != MP_OKAY) { |
|
3603 goto __ERR; |
|
3604 } |
|
3605 } |
|
3606 |
|
3607 /* 5. while v is even do */ |
|
3608 while (mp_iseven (&v) == 1) { |
|
3609 /* 5.1 v = v/2 */ |
|
3610 if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { |
|
3611 goto __ERR; |
|
3612 } |
|
3613 /* 5.2 if C or D is odd then */ |
|
3614 if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) { |
|
3615 /* C = (C+y)/2, D = (D-x)/2 */ |
|
3616 if ((res = mp_add (&C, &y, &C)) != MP_OKAY) { |
|
3617 goto __ERR; |
|
3618 } |
|
3619 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { |
|
3620 goto __ERR; |
|
3621 } |
|
3622 } |
|
3623 /* C = C/2, D = D/2 */ |
|
3624 if ((res = mp_div_2 (&C, &C)) != MP_OKAY) { |
|
3625 goto __ERR; |
|
3626 } |
|
3627 if ((res = mp_div_2 (&D, &D)) != MP_OKAY) { |
|
3628 goto __ERR; |
|
3629 } |
|
3630 } |
|
3631 |
|
3632 /* 6. if u >= v then */ |
|
3633 if (mp_cmp (&u, &v) != MP_LT) { |
|
3634 /* u = u - v, A = A - C, B = B - D */ |
|
3635 if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) { |
|
3636 goto __ERR; |
|
3637 } |
|
3638 |
|
3639 if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) { |
|
3640 goto __ERR; |
|
3641 } |
|
3642 |
|
3643 if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) { |
|
3644 goto __ERR; |
|
3645 } |
|
3646 } else { |
|
3647 /* v - v - u, C = C - A, D = D - B */ |
|
3648 if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) { |
|
3649 goto __ERR; |
|
3650 } |
|
3651 |
|
3652 if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) { |
|
3653 goto __ERR; |
|
3654 } |
|
3655 |
|
3656 if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) { |
|
3657 goto __ERR; |
|
3658 } |
|
3659 } |
|
3660 |
|
3661 /* if not zero goto step 4 */ |
|
3662 if (mp_iszero (&u) == 0) |
|
3663 goto top; |
|
3664 |
|
3665 /* now a = C, b = D, gcd == g*v */ |
|
3666 |
|
3667 /* if v != 1 then there is no inverse */ |
|
3668 if (mp_cmp_d (&v, 1) != MP_EQ) { |
|
3669 res = MP_VAL; |
|
3670 goto __ERR; |
|
3671 } |
|
3672 |
|
3673 /* if its too low */ |
|
3674 while (mp_cmp_d(&C, 0) == MP_LT) { |
|
3675 if ((res = mp_add(&C, b, &C)) != MP_OKAY) { |
|
3676 goto __ERR; |
|
3677 } |
|
3678 } |
|
3679 |
|
3680 /* too big */ |
|
3681 while (mp_cmp_mag(&C, b) != MP_LT) { |
|
3682 if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { |
|
3683 goto __ERR; |
|
3684 } |
|
3685 } |
|
3686 |
|
3687 /* C is now the inverse */ |
|
3688 mp_exch (&C, c); |
|
3689 res = MP_OKAY; |
|
3690 __ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); |
|
3691 return res; |
|
3692 } |
143
|
3693 #endif |
|
3694 |
|
3695 /* End: bn_mp_invmod_slow.c */ |
3
|
3696 |
|
3697 /* Start: bn_mp_is_square.c */ |
143
|
3698 #include <ltc_tommath.h> |
|
3699 #ifdef BN_MP_IS_SQUARE_C |
|
3700 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3701 * |
|
3702 * LibTomMath is a library that provides multiple-precision |
|
3703 * integer arithmetic as well as number theoretic functionality. |
|
3704 * |
|
3705 * The library was designed directly after the MPI library by |
|
3706 * Michael Fromberger but has been written from scratch with |
|
3707 * additional optimizations in place. |
|
3708 * |
|
3709 * The library is free for all purposes without any express |
|
3710 * guarantee it works. |
|
3711 * |
|
3712 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3713 */ |
3
|
3714 |
|
3715 /* Check if remainders are possible squares - fast exclude non-squares */ |
|
3716 static const char rem_128[128] = { |
|
3717 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3718 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3719 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3720 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3721 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3722 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3723 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, |
|
3724 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 |
|
3725 }; |
|
3726 |
|
3727 static const char rem_105[105] = { |
|
3728 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, |
|
3729 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, |
|
3730 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, |
|
3731 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, |
|
3732 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, |
|
3733 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, |
|
3734 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1 |
|
3735 }; |
|
3736 |
|
3737 /* Store non-zero to ret if arg is square, and zero if not */ |
|
3738 int mp_is_square(mp_int *arg,int *ret) |
|
3739 { |
|
3740 int res; |
|
3741 mp_digit c; |
|
3742 mp_int t; |
|
3743 unsigned long r; |
|
3744 |
|
3745 /* Default to Non-square :) */ |
|
3746 *ret = MP_NO; |
|
3747 |
|
3748 if (arg->sign == MP_NEG) { |
|
3749 return MP_VAL; |
|
3750 } |
|
3751 |
|
3752 /* digits used? (TSD) */ |
|
3753 if (arg->used == 0) { |
|
3754 return MP_OKAY; |
|
3755 } |
|
3756 |
|
3757 /* First check mod 128 (suppose that DIGIT_BIT is at least 7) */ |
|
3758 if (rem_128[127 & DIGIT(arg,0)] == 1) { |
|
3759 return MP_OKAY; |
|
3760 } |
|
3761 |
|
3762 /* Next check mod 105 (3*5*7) */ |
|
3763 if ((res = mp_mod_d(arg,105,&c)) != MP_OKAY) { |
|
3764 return res; |
|
3765 } |
|
3766 if (rem_105[c] == 1) { |
|
3767 return MP_OKAY; |
|
3768 } |
|
3769 |
143
|
3770 |
3
|
3771 if ((res = mp_init_set_int(&t,11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) { |
|
3772 return res; |
|
3773 } |
|
3774 if ((res = mp_mod(arg,&t,&t)) != MP_OKAY) { |
|
3775 goto ERR; |
|
3776 } |
|
3777 r = mp_get_int(&t); |
|
3778 /* Check for other prime modules, note it's not an ERROR but we must |
|
3779 * free "t" so the easiest way is to goto ERR. We know that res |
|
3780 * is already equal to MP_OKAY from the mp_mod call |
|
3781 */ |
|
3782 if ( (1L<<(r%11)) & 0x5C4L ) goto ERR; |
|
3783 if ( (1L<<(r%13)) & 0x9E4L ) goto ERR; |
|
3784 if ( (1L<<(r%17)) & 0x5CE8L ) goto ERR; |
|
3785 if ( (1L<<(r%19)) & 0x4F50CL ) goto ERR; |
|
3786 if ( (1L<<(r%23)) & 0x7ACCA0L ) goto ERR; |
|
3787 if ( (1L<<(r%29)) & 0xC2EDD0CL ) goto ERR; |
|
3788 if ( (1L<<(r%31)) & 0x6DE2B848L ) goto ERR; |
|
3789 |
|
3790 /* Final check - is sqr(sqrt(arg)) == arg ? */ |
|
3791 if ((res = mp_sqrt(arg,&t)) != MP_OKAY) { |
|
3792 goto ERR; |
|
3793 } |
|
3794 if ((res = mp_sqr(&t,&t)) != MP_OKAY) { |
|
3795 goto ERR; |
|
3796 } |
|
3797 |
|
3798 *ret = (mp_cmp_mag(&t,arg) == MP_EQ) ? MP_YES : MP_NO; |
|
3799 ERR:mp_clear(&t); |
|
3800 return res; |
|
3801 } |
143
|
3802 #endif |
3
|
3803 |
|
3804 /* End: bn_mp_is_square.c */ |
|
3805 |
|
3806 /* Start: bn_mp_jacobi.c */ |
143
|
3807 #include <ltc_tommath.h> |
|
3808 #ifdef BN_MP_JACOBI_C |
|
3809 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3810 * |
|
3811 * LibTomMath is a library that provides multiple-precision |
|
3812 * integer arithmetic as well as number theoretic functionality. |
|
3813 * |
|
3814 * The library was designed directly after the MPI library by |
|
3815 * Michael Fromberger but has been written from scratch with |
|
3816 * additional optimizations in place. |
|
3817 * |
|
3818 * The library is free for all purposes without any express |
|
3819 * guarantee it works. |
|
3820 * |
|
3821 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3822 */ |
3
|
3823 |
|
3824 /* computes the jacobi c = (a | n) (or Legendre if n is prime) |
|
3825 * HAC pp. 73 Algorithm 2.149 |
|
3826 */ |
|
3827 int mp_jacobi (mp_int * a, mp_int * p, int *c) |
|
3828 { |
|
3829 mp_int a1, p1; |
|
3830 int k, s, r, res; |
|
3831 mp_digit residue; |
|
3832 |
|
3833 /* if p <= 0 return MP_VAL */ |
|
3834 if (mp_cmp_d(p, 0) != MP_GT) { |
|
3835 return MP_VAL; |
|
3836 } |
|
3837 |
|
3838 /* step 1. if a == 0, return 0 */ |
|
3839 if (mp_iszero (a) == 1) { |
|
3840 *c = 0; |
|
3841 return MP_OKAY; |
|
3842 } |
|
3843 |
|
3844 /* step 2. if a == 1, return 1 */ |
|
3845 if (mp_cmp_d (a, 1) == MP_EQ) { |
|
3846 *c = 1; |
|
3847 return MP_OKAY; |
|
3848 } |
|
3849 |
|
3850 /* default */ |
|
3851 s = 0; |
|
3852 |
|
3853 /* step 3. write a = a1 * 2**k */ |
|
3854 if ((res = mp_init_copy (&a1, a)) != MP_OKAY) { |
|
3855 return res; |
|
3856 } |
|
3857 |
|
3858 if ((res = mp_init (&p1)) != MP_OKAY) { |
|
3859 goto __A1; |
|
3860 } |
|
3861 |
|
3862 /* divide out larger power of two */ |
|
3863 k = mp_cnt_lsb(&a1); |
|
3864 if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) { |
|
3865 goto __P1; |
|
3866 } |
|
3867 |
|
3868 /* step 4. if e is even set s=1 */ |
|
3869 if ((k & 1) == 0) { |
|
3870 s = 1; |
|
3871 } else { |
|
3872 /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */ |
|
3873 residue = p->dp[0] & 7; |
|
3874 |
|
3875 if (residue == 1 || residue == 7) { |
|
3876 s = 1; |
|
3877 } else if (residue == 3 || residue == 5) { |
|
3878 s = -1; |
|
3879 } |
|
3880 } |
|
3881 |
|
3882 /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */ |
|
3883 if ( ((p->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) { |
|
3884 s = -s; |
|
3885 } |
|
3886 |
|
3887 /* if a1 == 1 we're done */ |
|
3888 if (mp_cmp_d (&a1, 1) == MP_EQ) { |
|
3889 *c = s; |
|
3890 } else { |
|
3891 /* n1 = n mod a1 */ |
|
3892 if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) { |
|
3893 goto __P1; |
|
3894 } |
|
3895 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) { |
|
3896 goto __P1; |
|
3897 } |
|
3898 *c = s * r; |
|
3899 } |
|
3900 |
|
3901 /* done */ |
|
3902 res = MP_OKAY; |
|
3903 __P1:mp_clear (&p1); |
|
3904 __A1:mp_clear (&a1); |
|
3905 return res; |
|
3906 } |
143
|
3907 #endif |
3
|
3908 |
|
3909 /* End: bn_mp_jacobi.c */ |
|
3910 |
|
3911 /* Start: bn_mp_karatsuba_mul.c */ |
143
|
3912 #include <ltc_tommath.h> |
|
3913 #ifdef BN_MP_KARATSUBA_MUL_C |
|
3914 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
3915 * |
|
3916 * LibTomMath is a library that provides multiple-precision |
|
3917 * integer arithmetic as well as number theoretic functionality. |
|
3918 * |
|
3919 * The library was designed directly after the MPI library by |
|
3920 * Michael Fromberger but has been written from scratch with |
|
3921 * additional optimizations in place. |
|
3922 * |
|
3923 * The library is free for all purposes without any express |
|
3924 * guarantee it works. |
|
3925 * |
|
3926 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
3927 */ |
3
|
3928 |
|
3929 /* c = |a| * |b| using Karatsuba Multiplication using |
|
3930 * three half size multiplications |
|
3931 * |
|
3932 * Let B represent the radix [e.g. 2**DIGIT_BIT] and |
|
3933 * let n represent half of the number of digits in |
|
3934 * the min(a,b) |
|
3935 * |
|
3936 * a = a1 * B**n + a0 |
|
3937 * b = b1 * B**n + b0 |
|
3938 * |
|
3939 * Then, a * b => |
|
3940 a1b1 * B**2n + ((a1 - a0)(b1 - b0) + a0b0 + a1b1) * B + a0b0 |
|
3941 * |
|
3942 * Note that a1b1 and a0b0 are used twice and only need to be |
|
3943 * computed once. So in total three half size (half # of |
|
3944 * digit) multiplications are performed, a0b0, a1b1 and |
|
3945 * (a1-b1)(a0-b0) |
|
3946 * |
|
3947 * Note that a multiplication of half the digits requires |
|
3948 * 1/4th the number of single precision multiplications so in |
|
3949 * total after one call 25% of the single precision multiplications |
|
3950 * are saved. Note also that the call to mp_mul can end up back |
|
3951 * in this function if the a0, a1, b0, or b1 are above the threshold. |
|
3952 * This is known as divide-and-conquer and leads to the famous |
|
3953 * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than |
|
3954 * the standard O(N**2) that the baseline/comba methods use. |
|
3955 * Generally though the overhead of this method doesn't pay off |
|
3956 * until a certain size (N ~ 80) is reached. |
|
3957 */ |
|
3958 int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) |
|
3959 { |
|
3960 mp_int x0, x1, y0, y1, t1, x0y0, x1y1; |
|
3961 int B, err; |
|
3962 |
|
3963 /* default the return code to an error */ |
|
3964 err = MP_MEM; |
|
3965 |
|
3966 /* min # of digits */ |
|
3967 B = MIN (a->used, b->used); |
|
3968 |
|
3969 /* now divide in two */ |
|
3970 B = B >> 1; |
|
3971 |
|
3972 /* init copy all the temps */ |
|
3973 if (mp_init_size (&x0, B) != MP_OKAY) |
|
3974 goto ERR; |
|
3975 if (mp_init_size (&x1, a->used - B) != MP_OKAY) |
|
3976 goto X0; |
|
3977 if (mp_init_size (&y0, B) != MP_OKAY) |
|
3978 goto X1; |
|
3979 if (mp_init_size (&y1, b->used - B) != MP_OKAY) |
|
3980 goto Y0; |
|
3981 |
|
3982 /* init temps */ |
|
3983 if (mp_init_size (&t1, B * 2) != MP_OKAY) |
|
3984 goto Y1; |
|
3985 if (mp_init_size (&x0y0, B * 2) != MP_OKAY) |
|
3986 goto T1; |
|
3987 if (mp_init_size (&x1y1, B * 2) != MP_OKAY) |
|
3988 goto X0Y0; |
|
3989 |
|
3990 /* now shift the digits */ |
|
3991 x0.used = y0.used = B; |
|
3992 x1.used = a->used - B; |
|
3993 y1.used = b->used - B; |
|
3994 |
|
3995 { |
|
3996 register int x; |
|
3997 register mp_digit *tmpa, *tmpb, *tmpx, *tmpy; |
|
3998 |
|
3999 /* we copy the digits directly instead of using higher level functions |
|
4000 * since we also need to shift the digits |
|
4001 */ |
|
4002 tmpa = a->dp; |
|
4003 tmpb = b->dp; |
|
4004 |
|
4005 tmpx = x0.dp; |
|
4006 tmpy = y0.dp; |
|
4007 for (x = 0; x < B; x++) { |
|
4008 *tmpx++ = *tmpa++; |
|
4009 *tmpy++ = *tmpb++; |
|
4010 } |
|
4011 |
|
4012 tmpx = x1.dp; |
|
4013 for (x = B; x < a->used; x++) { |
|
4014 *tmpx++ = *tmpa++; |
|
4015 } |
|
4016 |
|
4017 tmpy = y1.dp; |
|
4018 for (x = B; x < b->used; x++) { |
|
4019 *tmpy++ = *tmpb++; |
|
4020 } |
|
4021 } |
|
4022 |
|
4023 /* only need to clamp the lower words since by definition the |
|
4024 * upper words x1/y1 must have a known number of digits |
|
4025 */ |
|
4026 mp_clamp (&x0); |
|
4027 mp_clamp (&y0); |
|
4028 |
|
4029 /* now calc the products x0y0 and x1y1 */ |
|
4030 /* after this x0 is no longer required, free temp [x0==t2]! */ |
|
4031 if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY) |
|
4032 goto X1Y1; /* x0y0 = x0*y0 */ |
|
4033 if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY) |
|
4034 goto X1Y1; /* x1y1 = x1*y1 */ |
|
4035 |
|
4036 /* now calc x1-x0 and y1-y0 */ |
|
4037 if (mp_sub (&x1, &x0, &t1) != MP_OKAY) |
|
4038 goto X1Y1; /* t1 = x1 - x0 */ |
|
4039 if (mp_sub (&y1, &y0, &x0) != MP_OKAY) |
|
4040 goto X1Y1; /* t2 = y1 - y0 */ |
|
4041 if (mp_mul (&t1, &x0, &t1) != MP_OKAY) |
|
4042 goto X1Y1; /* t1 = (x1 - x0) * (y1 - y0) */ |
|
4043 |
|
4044 /* add x0y0 */ |
|
4045 if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY) |
|
4046 goto X1Y1; /* t2 = x0y0 + x1y1 */ |
|
4047 if (mp_sub (&x0, &t1, &t1) != MP_OKAY) |
|
4048 goto X1Y1; /* t1 = x0y0 + x1y1 - (x1-x0)*(y1-y0) */ |
|
4049 |
|
4050 /* shift by B */ |
|
4051 if (mp_lshd (&t1, B) != MP_OKAY) |
|
4052 goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */ |
|
4053 if (mp_lshd (&x1y1, B * 2) != MP_OKAY) |
|
4054 goto X1Y1; /* x1y1 = x1y1 << 2*B */ |
|
4055 |
|
4056 if (mp_add (&x0y0, &t1, &t1) != MP_OKAY) |
|
4057 goto X1Y1; /* t1 = x0y0 + t1 */ |
|
4058 if (mp_add (&t1, &x1y1, c) != MP_OKAY) |
|
4059 goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */ |
|
4060 |
|
4061 /* Algorithm succeeded set the return code to MP_OKAY */ |
|
4062 err = MP_OKAY; |
|
4063 |
|
4064 X1Y1:mp_clear (&x1y1); |
|
4065 X0Y0:mp_clear (&x0y0); |
|
4066 T1:mp_clear (&t1); |
|
4067 Y1:mp_clear (&y1); |
|
4068 Y0:mp_clear (&y0); |
|
4069 X1:mp_clear (&x1); |
|
4070 X0:mp_clear (&x0); |
|
4071 ERR: |
|
4072 return err; |
|
4073 } |
143
|
4074 #endif |
3
|
4075 |
|
4076 /* End: bn_mp_karatsuba_mul.c */ |
|
4077 |
|
4078 /* Start: bn_mp_karatsuba_sqr.c */ |
143
|
4079 #include <ltc_tommath.h> |
|
4080 #ifdef BN_MP_KARATSUBA_SQR_C |
|
4081 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4082 * |
|
4083 * LibTomMath is a library that provides multiple-precision |
|
4084 * integer arithmetic as well as number theoretic functionality. |
|
4085 * |
|
4086 * The library was designed directly after the MPI library by |
|
4087 * Michael Fromberger but has been written from scratch with |
|
4088 * additional optimizations in place. |
|
4089 * |
|
4090 * The library is free for all purposes without any express |
|
4091 * guarantee it works. |
|
4092 * |
|
4093 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4094 */ |
3
|
4095 |
|
4096 /* Karatsuba squaring, computes b = a*a using three |
|
4097 * half size squarings |
|
4098 * |
143
|
4099 * See comments of karatsuba_mul for details. It |
3
|
4100 * is essentially the same algorithm but merely |
|
4101 * tuned to perform recursive squarings. |
|
4102 */ |
|
4103 int mp_karatsuba_sqr (mp_int * a, mp_int * b) |
|
4104 { |
|
4105 mp_int x0, x1, t1, t2, x0x0, x1x1; |
|
4106 int B, err; |
|
4107 |
|
4108 err = MP_MEM; |
|
4109 |
|
4110 /* min # of digits */ |
|
4111 B = a->used; |
|
4112 |
|
4113 /* now divide in two */ |
|
4114 B = B >> 1; |
|
4115 |
|
4116 /* init copy all the temps */ |
|
4117 if (mp_init_size (&x0, B) != MP_OKAY) |
|
4118 goto ERR; |
|
4119 if (mp_init_size (&x1, a->used - B) != MP_OKAY) |
|
4120 goto X0; |
|
4121 |
|
4122 /* init temps */ |
|
4123 if (mp_init_size (&t1, a->used * 2) != MP_OKAY) |
|
4124 goto X1; |
|
4125 if (mp_init_size (&t2, a->used * 2) != MP_OKAY) |
|
4126 goto T1; |
|
4127 if (mp_init_size (&x0x0, B * 2) != MP_OKAY) |
|
4128 goto T2; |
|
4129 if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY) |
|
4130 goto X0X0; |
|
4131 |
|
4132 { |
|
4133 register int x; |
|
4134 register mp_digit *dst, *src; |
|
4135 |
|
4136 src = a->dp; |
|
4137 |
|
4138 /* now shift the digits */ |
|
4139 dst = x0.dp; |
|
4140 for (x = 0; x < B; x++) { |
|
4141 *dst++ = *src++; |
|
4142 } |
|
4143 |
|
4144 dst = x1.dp; |
|
4145 for (x = B; x < a->used; x++) { |
|
4146 *dst++ = *src++; |
|
4147 } |
|
4148 } |
|
4149 |
|
4150 x0.used = B; |
|
4151 x1.used = a->used - B; |
|
4152 |
|
4153 mp_clamp (&x0); |
|
4154 |
|
4155 /* now calc the products x0*x0 and x1*x1 */ |
|
4156 if (mp_sqr (&x0, &x0x0) != MP_OKAY) |
|
4157 goto X1X1; /* x0x0 = x0*x0 */ |
|
4158 if (mp_sqr (&x1, &x1x1) != MP_OKAY) |
|
4159 goto X1X1; /* x1x1 = x1*x1 */ |
|
4160 |
|
4161 /* now calc (x1-x0)**2 */ |
|
4162 if (mp_sub (&x1, &x0, &t1) != MP_OKAY) |
|
4163 goto X1X1; /* t1 = x1 - x0 */ |
|
4164 if (mp_sqr (&t1, &t1) != MP_OKAY) |
|
4165 goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */ |
|
4166 |
|
4167 /* add x0y0 */ |
|
4168 if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY) |
|
4169 goto X1X1; /* t2 = x0x0 + x1x1 */ |
|
4170 if (mp_sub (&t2, &t1, &t1) != MP_OKAY) |
|
4171 goto X1X1; /* t1 = x0x0 + x1x1 - (x1-x0)*(x1-x0) */ |
|
4172 |
|
4173 /* shift by B */ |
|
4174 if (mp_lshd (&t1, B) != MP_OKAY) |
|
4175 goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */ |
|
4176 if (mp_lshd (&x1x1, B * 2) != MP_OKAY) |
|
4177 goto X1X1; /* x1x1 = x1x1 << 2*B */ |
|
4178 |
|
4179 if (mp_add (&x0x0, &t1, &t1) != MP_OKAY) |
|
4180 goto X1X1; /* t1 = x0x0 + t1 */ |
|
4181 if (mp_add (&t1, &x1x1, b) != MP_OKAY) |
|
4182 goto X1X1; /* t1 = x0x0 + t1 + x1x1 */ |
|
4183 |
|
4184 err = MP_OKAY; |
|
4185 |
|
4186 X1X1:mp_clear (&x1x1); |
|
4187 X0X0:mp_clear (&x0x0); |
|
4188 T2:mp_clear (&t2); |
|
4189 T1:mp_clear (&t1); |
|
4190 X1:mp_clear (&x1); |
|
4191 X0:mp_clear (&x0); |
|
4192 ERR: |
|
4193 return err; |
|
4194 } |
143
|
4195 #endif |
3
|
4196 |
|
4197 /* End: bn_mp_karatsuba_sqr.c */ |
|
4198 |
|
4199 /* Start: bn_mp_lcm.c */ |
143
|
4200 #include <ltc_tommath.h> |
|
4201 #ifdef BN_MP_LCM_C |
|
4202 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4203 * |
|
4204 * LibTomMath is a library that provides multiple-precision |
|
4205 * integer arithmetic as well as number theoretic functionality. |
|
4206 * |
|
4207 * The library was designed directly after the MPI library by |
|
4208 * Michael Fromberger but has been written from scratch with |
|
4209 * additional optimizations in place. |
|
4210 * |
|
4211 * The library is free for all purposes without any express |
|
4212 * guarantee it works. |
|
4213 * |
|
4214 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4215 */ |
3
|
4216 |
|
4217 /* computes least common multiple as |a*b|/(a, b) */ |
|
4218 int mp_lcm (mp_int * a, mp_int * b, mp_int * c) |
|
4219 { |
|
4220 int res; |
|
4221 mp_int t1, t2; |
|
4222 |
|
4223 |
|
4224 if ((res = mp_init_multi (&t1, &t2, NULL)) != MP_OKAY) { |
|
4225 return res; |
|
4226 } |
|
4227 |
|
4228 /* t1 = get the GCD of the two inputs */ |
|
4229 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) { |
|
4230 goto __T; |
|
4231 } |
|
4232 |
|
4233 /* divide the smallest by the GCD */ |
|
4234 if (mp_cmp_mag(a, b) == MP_LT) { |
|
4235 /* store quotient in t2 such that t2 * b is the LCM */ |
|
4236 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { |
|
4237 goto __T; |
|
4238 } |
|
4239 res = mp_mul(b, &t2, c); |
|
4240 } else { |
|
4241 /* store quotient in t2 such that t2 * a is the LCM */ |
|
4242 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { |
|
4243 goto __T; |
|
4244 } |
|
4245 res = mp_mul(a, &t2, c); |
|
4246 } |
|
4247 |
|
4248 /* fix the sign to positive */ |
|
4249 c->sign = MP_ZPOS; |
|
4250 |
|
4251 __T: |
|
4252 mp_clear_multi (&t1, &t2, NULL); |
|
4253 return res; |
|
4254 } |
143
|
4255 #endif |
3
|
4256 |
|
4257 /* End: bn_mp_lcm.c */ |
|
4258 |
|
4259 /* Start: bn_mp_lshd.c */ |
143
|
4260 #include <ltc_tommath.h> |
|
4261 #ifdef BN_MP_LSHD_C |
|
4262 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4263 * |
|
4264 * LibTomMath is a library that provides multiple-precision |
|
4265 * integer arithmetic as well as number theoretic functionality. |
|
4266 * |
|
4267 * The library was designed directly after the MPI library by |
|
4268 * Michael Fromberger but has been written from scratch with |
|
4269 * additional optimizations in place. |
|
4270 * |
|
4271 * The library is free for all purposes without any express |
|
4272 * guarantee it works. |
|
4273 * |
|
4274 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4275 */ |
3
|
4276 |
|
4277 /* shift left a certain amount of digits */ |
|
4278 int mp_lshd (mp_int * a, int b) |
|
4279 { |
|
4280 int x, res; |
|
4281 |
|
4282 /* if its less than zero return */ |
|
4283 if (b <= 0) { |
|
4284 return MP_OKAY; |
|
4285 } |
|
4286 |
|
4287 /* grow to fit the new digits */ |
|
4288 if (a->alloc < a->used + b) { |
|
4289 if ((res = mp_grow (a, a->used + b)) != MP_OKAY) { |
|
4290 return res; |
|
4291 } |
|
4292 } |
|
4293 |
|
4294 { |
|
4295 register mp_digit *top, *bottom; |
|
4296 |
|
4297 /* increment the used by the shift amount then copy upwards */ |
|
4298 a->used += b; |
|
4299 |
|
4300 /* top */ |
|
4301 top = a->dp + a->used - 1; |
|
4302 |
|
4303 /* base */ |
|
4304 bottom = a->dp + a->used - 1 - b; |
|
4305 |
|
4306 /* much like mp_rshd this is implemented using a sliding window |
|
4307 * except the window goes the otherway around. Copying from |
|
4308 * the bottom to the top. see bn_mp_rshd.c for more info. |
|
4309 */ |
|
4310 for (x = a->used - 1; x >= b; x--) { |
|
4311 *top-- = *bottom--; |
|
4312 } |
|
4313 |
|
4314 /* zero the lower digits */ |
|
4315 top = a->dp; |
|
4316 for (x = 0; x < b; x++) { |
|
4317 *top++ = 0; |
|
4318 } |
|
4319 } |
|
4320 return MP_OKAY; |
|
4321 } |
143
|
4322 #endif |
3
|
4323 |
|
4324 /* End: bn_mp_lshd.c */ |
|
4325 |
|
4326 /* Start: bn_mp_mod.c */ |
143
|
4327 #include <ltc_tommath.h> |
|
4328 #ifdef BN_MP_MOD_C |
|
4329 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4330 * |
|
4331 * LibTomMath is a library that provides multiple-precision |
|
4332 * integer arithmetic as well as number theoretic functionality. |
|
4333 * |
|
4334 * The library was designed directly after the MPI library by |
|
4335 * Michael Fromberger but has been written from scratch with |
|
4336 * additional optimizations in place. |
|
4337 * |
|
4338 * The library is free for all purposes without any express |
|
4339 * guarantee it works. |
|
4340 * |
|
4341 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4342 */ |
3
|
4343 |
|
4344 /* c = a mod b, 0 <= c < b */ |
|
4345 int |
|
4346 mp_mod (mp_int * a, mp_int * b, mp_int * c) |
|
4347 { |
|
4348 mp_int t; |
|
4349 int res; |
|
4350 |
|
4351 if ((res = mp_init (&t)) != MP_OKAY) { |
|
4352 return res; |
|
4353 } |
|
4354 |
|
4355 if ((res = mp_div (a, b, NULL, &t)) != MP_OKAY) { |
|
4356 mp_clear (&t); |
|
4357 return res; |
|
4358 } |
|
4359 |
|
4360 if (t.sign != b->sign) { |
|
4361 res = mp_add (b, &t, c); |
|
4362 } else { |
|
4363 res = MP_OKAY; |
|
4364 mp_exch (&t, c); |
|
4365 } |
|
4366 |
|
4367 mp_clear (&t); |
|
4368 return res; |
|
4369 } |
143
|
4370 #endif |
3
|
4371 |
|
4372 /* End: bn_mp_mod.c */ |
|
4373 |
|
4374 /* Start: bn_mp_mod_2d.c */ |
143
|
4375 #include <ltc_tommath.h> |
|
4376 #ifdef BN_MP_MOD_2D_C |
|
4377 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4378 * |
|
4379 * LibTomMath is a library that provides multiple-precision |
|
4380 * integer arithmetic as well as number theoretic functionality. |
|
4381 * |
|
4382 * The library was designed directly after the MPI library by |
|
4383 * Michael Fromberger but has been written from scratch with |
|
4384 * additional optimizations in place. |
|
4385 * |
|
4386 * The library is free for all purposes without any express |
|
4387 * guarantee it works. |
|
4388 * |
|
4389 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4390 */ |
3
|
4391 |
|
4392 /* calc a value mod 2**b */ |
|
4393 int |
|
4394 mp_mod_2d (mp_int * a, int b, mp_int * c) |
|
4395 { |
|
4396 int x, res; |
|
4397 |
|
4398 /* if b is <= 0 then zero the int */ |
|
4399 if (b <= 0) { |
|
4400 mp_zero (c); |
|
4401 return MP_OKAY; |
|
4402 } |
|
4403 |
|
4404 /* if the modulus is larger than the value than return */ |
|
4405 if (b > (int) (a->used * DIGIT_BIT)) { |
|
4406 res = mp_copy (a, c); |
|
4407 return res; |
|
4408 } |
|
4409 |
|
4410 /* copy */ |
|
4411 if ((res = mp_copy (a, c)) != MP_OKAY) { |
|
4412 return res; |
|
4413 } |
|
4414 |
|
4415 /* zero digits above the last digit of the modulus */ |
|
4416 for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) { |
|
4417 c->dp[x] = 0; |
|
4418 } |
|
4419 /* clear the digit that is not completely outside/inside the modulus */ |
|
4420 c->dp[b / DIGIT_BIT] &= |
|
4421 (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); |
|
4422 mp_clamp (c); |
|
4423 return MP_OKAY; |
|
4424 } |
143
|
4425 #endif |
3
|
4426 |
|
4427 /* End: bn_mp_mod_2d.c */ |
|
4428 |
|
4429 /* Start: bn_mp_mod_d.c */ |
143
|
4430 #include <ltc_tommath.h> |
|
4431 #ifdef BN_MP_MOD_D_C |
|
4432 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4433 * |
|
4434 * LibTomMath is a library that provides multiple-precision |
|
4435 * integer arithmetic as well as number theoretic functionality. |
|
4436 * |
|
4437 * The library was designed directly after the MPI library by |
|
4438 * Michael Fromberger but has been written from scratch with |
|
4439 * additional optimizations in place. |
|
4440 * |
|
4441 * The library is free for all purposes without any express |
|
4442 * guarantee it works. |
|
4443 * |
|
4444 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4445 */ |
3
|
4446 |
|
4447 int |
|
4448 mp_mod_d (mp_int * a, mp_digit b, mp_digit * c) |
|
4449 { |
|
4450 return mp_div_d(a, b, NULL, c); |
|
4451 } |
143
|
4452 #endif |
3
|
4453 |
|
4454 /* End: bn_mp_mod_d.c */ |
|
4455 |
|
4456 /* Start: bn_mp_montgomery_calc_normalization.c */ |
143
|
4457 #include <ltc_tommath.h> |
|
4458 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C |
|
4459 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4460 * |
|
4461 * LibTomMath is a library that provides multiple-precision |
|
4462 * integer arithmetic as well as number theoretic functionality. |
|
4463 * |
|
4464 * The library was designed directly after the MPI library by |
|
4465 * Michael Fromberger but has been written from scratch with |
|
4466 * additional optimizations in place. |
|
4467 * |
|
4468 * The library is free for all purposes without any express |
|
4469 * guarantee it works. |
|
4470 * |
|
4471 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4472 */ |
|
4473 |
|
4474 /* |
3
|
4475 * shifts with subtractions when the result is greater than b. |
|
4476 * |
|
4477 * The method is slightly modified to shift B unconditionally upto just under |
|
4478 * the leading bit of b. This saves alot of multiple precision shifting. |
|
4479 */ |
143
|
4480 int mp_montgomery_calc_normalization (mp_int * a, mp_int * b) |
3
|
4481 { |
|
4482 int x, bits, res; |
|
4483 |
|
4484 /* how many bits of last digit does b use */ |
|
4485 bits = mp_count_bits (b) % DIGIT_BIT; |
|
4486 |
143
|
4487 |
|
4488 if (b->used > 1) { |
|
4489 if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) { |
|
4490 return res; |
|
4491 } |
|
4492 } else { |
|
4493 mp_set(a, 1); |
|
4494 bits = 1; |
|
4495 } |
|
4496 |
3
|
4497 |
|
4498 /* now compute C = A * B mod b */ |
|
4499 for (x = bits - 1; x < (int)DIGIT_BIT; x++) { |
|
4500 if ((res = mp_mul_2 (a, a)) != MP_OKAY) { |
|
4501 return res; |
|
4502 } |
|
4503 if (mp_cmp_mag (a, b) != MP_LT) { |
|
4504 if ((res = s_mp_sub (a, b, a)) != MP_OKAY) { |
|
4505 return res; |
|
4506 } |
|
4507 } |
|
4508 } |
|
4509 |
|
4510 return MP_OKAY; |
|
4511 } |
143
|
4512 #endif |
3
|
4513 |
|
4514 /* End: bn_mp_montgomery_calc_normalization.c */ |
|
4515 |
|
4516 /* Start: bn_mp_montgomery_reduce.c */ |
143
|
4517 #include <ltc_tommath.h> |
|
4518 #ifdef BN_MP_MONTGOMERY_REDUCE_C |
|
4519 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4520 * |
|
4521 * LibTomMath is a library that provides multiple-precision |
|
4522 * integer arithmetic as well as number theoretic functionality. |
|
4523 * |
|
4524 * The library was designed directly after the MPI library by |
|
4525 * Michael Fromberger but has been written from scratch with |
|
4526 * additional optimizations in place. |
|
4527 * |
|
4528 * The library is free for all purposes without any express |
|
4529 * guarantee it works. |
|
4530 * |
|
4531 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4532 */ |
3
|
4533 |
|
4534 /* computes xR**-1 == x (mod N) via Montgomery Reduction */ |
|
4535 int |
|
4536 mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) |
|
4537 { |
|
4538 int ix, res, digs; |
|
4539 mp_digit mu; |
|
4540 |
|
4541 /* can the fast reduction [comba] method be used? |
|
4542 * |
143
|
4543 * Note that unlike in mul you're safely allowed *less* |
3
|
4544 * than the available columns [255 per default] since carries |
|
4545 * are fixed up in the inner loop. |
|
4546 */ |
|
4547 digs = n->used * 2 + 1; |
|
4548 if ((digs < MP_WARRAY) && |
|
4549 n->used < |
|
4550 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
4551 return fast_mp_montgomery_reduce (x, n, rho); |
|
4552 } |
|
4553 |
|
4554 /* grow the input as required */ |
|
4555 if (x->alloc < digs) { |
|
4556 if ((res = mp_grow (x, digs)) != MP_OKAY) { |
|
4557 return res; |
|
4558 } |
|
4559 } |
|
4560 x->used = digs; |
|
4561 |
|
4562 for (ix = 0; ix < n->used; ix++) { |
|
4563 /* mu = ai * rho mod b |
|
4564 * |
|
4565 * The value of rho must be precalculated via |
143
|
4566 * montgomery_setup() such that |
3
|
4567 * it equals -1/n0 mod b this allows the |
|
4568 * following inner loop to reduce the |
|
4569 * input one digit at a time |
|
4570 */ |
|
4571 mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK); |
|
4572 |
|
4573 /* a = a + mu * m * b**i */ |
|
4574 { |
|
4575 register int iy; |
|
4576 register mp_digit *tmpn, *tmpx, u; |
|
4577 register mp_word r; |
|
4578 |
|
4579 /* alias for digits of the modulus */ |
|
4580 tmpn = n->dp; |
|
4581 |
|
4582 /* alias for the digits of x [the input] */ |
|
4583 tmpx = x->dp + ix; |
|
4584 |
|
4585 /* set the carry to zero */ |
|
4586 u = 0; |
|
4587 |
|
4588 /* Multiply and add in place */ |
|
4589 for (iy = 0; iy < n->used; iy++) { |
|
4590 /* compute product and sum */ |
|
4591 r = ((mp_word)mu) * ((mp_word)*tmpn++) + |
|
4592 ((mp_word) u) + ((mp_word) * tmpx); |
|
4593 |
|
4594 /* get carry */ |
|
4595 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
4596 |
|
4597 /* fix digit */ |
|
4598 *tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK)); |
|
4599 } |
|
4600 /* At this point the ix'th digit of x should be zero */ |
|
4601 |
|
4602 |
|
4603 /* propagate carries upwards as required*/ |
|
4604 while (u) { |
|
4605 *tmpx += u; |
|
4606 u = *tmpx >> DIGIT_BIT; |
|
4607 *tmpx++ &= MP_MASK; |
|
4608 } |
|
4609 } |
|
4610 } |
|
4611 |
|
4612 /* at this point the n.used'th least |
|
4613 * significant digits of x are all zero |
|
4614 * which means we can shift x to the |
|
4615 * right by n.used digits and the |
|
4616 * residue is unchanged. |
|
4617 */ |
|
4618 |
|
4619 /* x = x/b**n.used */ |
|
4620 mp_clamp(x); |
|
4621 mp_rshd (x, n->used); |
|
4622 |
|
4623 /* if x >= n then x = x - n */ |
|
4624 if (mp_cmp_mag (x, n) != MP_LT) { |
|
4625 return s_mp_sub (x, n, x); |
|
4626 } |
|
4627 |
|
4628 return MP_OKAY; |
|
4629 } |
143
|
4630 #endif |
3
|
4631 |
|
4632 /* End: bn_mp_montgomery_reduce.c */ |
|
4633 |
|
4634 /* Start: bn_mp_montgomery_setup.c */ |
143
|
4635 #include <ltc_tommath.h> |
|
4636 #ifdef BN_MP_MONTGOMERY_SETUP_C |
|
4637 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4638 * |
|
4639 * LibTomMath is a library that provides multiple-precision |
|
4640 * integer arithmetic as well as number theoretic functionality. |
|
4641 * |
|
4642 * The library was designed directly after the MPI library by |
|
4643 * Michael Fromberger but has been written from scratch with |
|
4644 * additional optimizations in place. |
|
4645 * |
|
4646 * The library is free for all purposes without any express |
|
4647 * guarantee it works. |
|
4648 * |
|
4649 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4650 */ |
3
|
4651 |
|
4652 /* setups the montgomery reduction stuff */ |
|
4653 int |
|
4654 mp_montgomery_setup (mp_int * n, mp_digit * rho) |
|
4655 { |
|
4656 mp_digit x, b; |
|
4657 |
|
4658 /* fast inversion mod 2**k |
|
4659 * |
|
4660 * Based on the fact that |
|
4661 * |
|
4662 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) |
|
4663 * => 2*X*A - X*X*A*A = 1 |
|
4664 * => 2*(1) - (1) = 1 |
|
4665 */ |
|
4666 b = n->dp[0]; |
|
4667 |
|
4668 if ((b & 1) == 0) { |
|
4669 return MP_VAL; |
|
4670 } |
|
4671 |
|
4672 x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ |
|
4673 x *= 2 - b * x; /* here x*a==1 mod 2**8 */ |
|
4674 #if !defined(MP_8BIT) |
|
4675 x *= 2 - b * x; /* here x*a==1 mod 2**16 */ |
|
4676 #endif |
|
4677 #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) |
|
4678 x *= 2 - b * x; /* here x*a==1 mod 2**32 */ |
|
4679 #endif |
|
4680 #ifdef MP_64BIT |
|
4681 x *= 2 - b * x; /* here x*a==1 mod 2**64 */ |
|
4682 #endif |
|
4683 |
|
4684 /* rho = -1/m mod b */ |
143
|
4685 *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; |
3
|
4686 |
|
4687 return MP_OKAY; |
|
4688 } |
143
|
4689 #endif |
3
|
4690 |
|
4691 /* End: bn_mp_montgomery_setup.c */ |
|
4692 |
|
4693 /* Start: bn_mp_mul.c */ |
143
|
4694 #include <ltc_tommath.h> |
|
4695 #ifdef BN_MP_MUL_C |
|
4696 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4697 * |
|
4698 * LibTomMath is a library that provides multiple-precision |
|
4699 * integer arithmetic as well as number theoretic functionality. |
|
4700 * |
|
4701 * The library was designed directly after the MPI library by |
|
4702 * Michael Fromberger but has been written from scratch with |
|
4703 * additional optimizations in place. |
|
4704 * |
|
4705 * The library is free for all purposes without any express |
|
4706 * guarantee it works. |
|
4707 * |
|
4708 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4709 */ |
3
|
4710 |
|
4711 /* high level multiplication (handles sign) */ |
|
4712 int mp_mul (mp_int * a, mp_int * b, mp_int * c) |
|
4713 { |
|
4714 int res, neg; |
|
4715 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; |
|
4716 |
|
4717 /* use Toom-Cook? */ |
143
|
4718 #ifdef BN_MP_TOOM_MUL_C |
3
|
4719 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { |
|
4720 res = mp_toom_mul(a, b, c); |
143
|
4721 } else |
|
4722 #endif |
|
4723 #ifdef BN_MP_KARATSUBA_MUL_C |
3
|
4724 /* use Karatsuba? */ |
143
|
4725 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { |
3
|
4726 res = mp_karatsuba_mul (a, b, c); |
143
|
4727 } else |
|
4728 #endif |
|
4729 { |
3
|
4730 /* can we use the fast multiplier? |
|
4731 * |
|
4732 * The fast multiplier can be used if the output will |
|
4733 * have less than MP_WARRAY digits and the number of |
|
4734 * digits won't affect carry propagation |
|
4735 */ |
|
4736 int digs = a->used + b->used + 1; |
|
4737 |
143
|
4738 #ifdef BN_FAST_S_MP_MUL_DIGS_C |
3
|
4739 if ((digs < MP_WARRAY) && |
|
4740 MIN(a->used, b->used) <= |
|
4741 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
4742 res = fast_s_mp_mul_digs (a, b, c, digs); |
143
|
4743 } else |
|
4744 #endif |
|
4745 #ifdef BN_S_MP_MUL_DIGS_C |
|
4746 res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */ |
|
4747 #else |
|
4748 res = MP_VAL; |
|
4749 #endif |
|
4750 |
|
4751 } |
|
4752 c->sign = (c->used > 0) ? neg : MP_ZPOS; |
3
|
4753 return res; |
|
4754 } |
143
|
4755 #endif |
3
|
4756 |
|
4757 /* End: bn_mp_mul.c */ |
|
4758 |
|
4759 /* Start: bn_mp_mul_2.c */ |
143
|
4760 #include <ltc_tommath.h> |
|
4761 #ifdef BN_MP_MUL_2_C |
|
4762 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4763 * |
|
4764 * LibTomMath is a library that provides multiple-precision |
|
4765 * integer arithmetic as well as number theoretic functionality. |
|
4766 * |
|
4767 * The library was designed directly after the MPI library by |
|
4768 * Michael Fromberger but has been written from scratch with |
|
4769 * additional optimizations in place. |
|
4770 * |
|
4771 * The library is free for all purposes without any express |
|
4772 * guarantee it works. |
|
4773 * |
|
4774 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4775 */ |
3
|
4776 |
|
4777 /* b = a*2 */ |
|
4778 int mp_mul_2(mp_int * a, mp_int * b) |
|
4779 { |
|
4780 int x, res, oldused; |
|
4781 |
|
4782 /* grow to accomodate result */ |
|
4783 if (b->alloc < a->used + 1) { |
|
4784 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { |
|
4785 return res; |
|
4786 } |
|
4787 } |
|
4788 |
|
4789 oldused = b->used; |
|
4790 b->used = a->used; |
|
4791 |
|
4792 { |
|
4793 register mp_digit r, rr, *tmpa, *tmpb; |
|
4794 |
|
4795 /* alias for source */ |
|
4796 tmpa = a->dp; |
|
4797 |
|
4798 /* alias for dest */ |
|
4799 tmpb = b->dp; |
|
4800 |
|
4801 /* carry */ |
|
4802 r = 0; |
|
4803 for (x = 0; x < a->used; x++) { |
|
4804 |
|
4805 /* get what will be the *next* carry bit from the |
|
4806 * MSB of the current digit |
|
4807 */ |
|
4808 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); |
|
4809 |
|
4810 /* now shift up this digit, add in the carry [from the previous] */ |
|
4811 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; |
|
4812 |
|
4813 /* copy the carry that would be from the source |
|
4814 * digit into the next iteration |
|
4815 */ |
|
4816 r = rr; |
|
4817 } |
|
4818 |
|
4819 /* new leading digit? */ |
|
4820 if (r != 0) { |
|
4821 /* add a MSB which is always 1 at this point */ |
|
4822 *tmpb = 1; |
|
4823 ++(b->used); |
|
4824 } |
|
4825 |
|
4826 /* now zero any excess digits on the destination |
|
4827 * that we didn't write to |
|
4828 */ |
|
4829 tmpb = b->dp + b->used; |
|
4830 for (x = b->used; x < oldused; x++) { |
|
4831 *tmpb++ = 0; |
|
4832 } |
|
4833 } |
|
4834 b->sign = a->sign; |
|
4835 return MP_OKAY; |
|
4836 } |
143
|
4837 #endif |
3
|
4838 |
|
4839 /* End: bn_mp_mul_2.c */ |
|
4840 |
|
4841 /* Start: bn_mp_mul_2d.c */ |
143
|
4842 #include <ltc_tommath.h> |
|
4843 #ifdef BN_MP_MUL_2D_C |
|
4844 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4845 * |
|
4846 * LibTomMath is a library that provides multiple-precision |
|
4847 * integer arithmetic as well as number theoretic functionality. |
|
4848 * |
|
4849 * The library was designed directly after the MPI library by |
|
4850 * Michael Fromberger but has been written from scratch with |
|
4851 * additional optimizations in place. |
|
4852 * |
|
4853 * The library is free for all purposes without any express |
|
4854 * guarantee it works. |
|
4855 * |
|
4856 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4857 */ |
3
|
4858 |
|
4859 /* shift left by a certain bit count */ |
|
4860 int mp_mul_2d (mp_int * a, int b, mp_int * c) |
|
4861 { |
|
4862 mp_digit d; |
|
4863 int res; |
|
4864 |
|
4865 /* copy */ |
|
4866 if (a != c) { |
|
4867 if ((res = mp_copy (a, c)) != MP_OKAY) { |
|
4868 return res; |
|
4869 } |
|
4870 } |
|
4871 |
|
4872 if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) { |
|
4873 if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) { |
|
4874 return res; |
|
4875 } |
|
4876 } |
|
4877 |
|
4878 /* shift by as many digits in the bit count */ |
|
4879 if (b >= (int)DIGIT_BIT) { |
|
4880 if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) { |
|
4881 return res; |
|
4882 } |
|
4883 } |
|
4884 |
|
4885 /* shift any bit count < DIGIT_BIT */ |
|
4886 d = (mp_digit) (b % DIGIT_BIT); |
|
4887 if (d != 0) { |
|
4888 register mp_digit *tmpc, shift, mask, r, rr; |
|
4889 register int x; |
|
4890 |
|
4891 /* bitmask for carries */ |
|
4892 mask = (((mp_digit)1) << d) - 1; |
|
4893 |
|
4894 /* shift for msbs */ |
|
4895 shift = DIGIT_BIT - d; |
|
4896 |
|
4897 /* alias */ |
|
4898 tmpc = c->dp; |
|
4899 |
|
4900 /* carry */ |
|
4901 r = 0; |
|
4902 for (x = 0; x < c->used; x++) { |
|
4903 /* get the higher bits of the current word */ |
|
4904 rr = (*tmpc >> shift) & mask; |
|
4905 |
|
4906 /* shift the current word and OR in the carry */ |
|
4907 *tmpc = ((*tmpc << d) | r) & MP_MASK; |
|
4908 ++tmpc; |
|
4909 |
|
4910 /* set the carry to the carry bits of the current word */ |
|
4911 r = rr; |
|
4912 } |
|
4913 |
|
4914 /* set final carry */ |
|
4915 if (r != 0) { |
|
4916 c->dp[(c->used)++] = r; |
|
4917 } |
|
4918 } |
|
4919 mp_clamp (c); |
|
4920 return MP_OKAY; |
|
4921 } |
143
|
4922 #endif |
3
|
4923 |
|
4924 /* End: bn_mp_mul_2d.c */ |
|
4925 |
|
4926 /* Start: bn_mp_mul_d.c */ |
143
|
4927 #include <ltc_tommath.h> |
|
4928 #ifdef BN_MP_MUL_D_C |
|
4929 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4930 * |
|
4931 * LibTomMath is a library that provides multiple-precision |
|
4932 * integer arithmetic as well as number theoretic functionality. |
|
4933 * |
|
4934 * The library was designed directly after the MPI library by |
|
4935 * Michael Fromberger but has been written from scratch with |
|
4936 * additional optimizations in place. |
|
4937 * |
|
4938 * The library is free for all purposes without any express |
|
4939 * guarantee it works. |
|
4940 * |
|
4941 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
4942 */ |
3
|
4943 |
|
4944 /* multiply by a digit */ |
|
4945 int |
|
4946 mp_mul_d (mp_int * a, mp_digit b, mp_int * c) |
|
4947 { |
|
4948 mp_digit u, *tmpa, *tmpc; |
|
4949 mp_word r; |
|
4950 int ix, res, olduse; |
|
4951 |
|
4952 /* make sure c is big enough to hold a*b */ |
|
4953 if (c->alloc < a->used + 1) { |
|
4954 if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) { |
|
4955 return res; |
|
4956 } |
|
4957 } |
|
4958 |
|
4959 /* get the original destinations used count */ |
|
4960 olduse = c->used; |
|
4961 |
|
4962 /* set the sign */ |
|
4963 c->sign = a->sign; |
|
4964 |
|
4965 /* alias for a->dp [source] */ |
|
4966 tmpa = a->dp; |
|
4967 |
|
4968 /* alias for c->dp [dest] */ |
|
4969 tmpc = c->dp; |
|
4970 |
|
4971 /* zero carry */ |
|
4972 u = 0; |
|
4973 |
|
4974 /* compute columns */ |
|
4975 for (ix = 0; ix < a->used; ix++) { |
|
4976 /* compute product and carry sum for this term */ |
|
4977 r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b); |
|
4978 |
|
4979 /* mask off higher bits to get a single digit */ |
|
4980 *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
4981 |
|
4982 /* send carry into next iteration */ |
|
4983 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
|
4984 } |
|
4985 |
|
4986 /* store final carry [if any] */ |
|
4987 *tmpc++ = u; |
|
4988 |
|
4989 /* now zero digits above the top */ |
|
4990 while (ix++ < olduse) { |
|
4991 *tmpc++ = 0; |
|
4992 } |
|
4993 |
|
4994 /* set used count */ |
|
4995 c->used = a->used + 1; |
|
4996 mp_clamp(c); |
|
4997 |
|
4998 return MP_OKAY; |
|
4999 } |
143
|
5000 #endif |
3
|
5001 |
|
5002 /* End: bn_mp_mul_d.c */ |
|
5003 |
|
5004 /* Start: bn_mp_mulmod.c */ |
143
|
5005 #include <ltc_tommath.h> |
|
5006 #ifdef BN_MP_MULMOD_C |
|
5007 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5008 * |
|
5009 * LibTomMath is a library that provides multiple-precision |
|
5010 * integer arithmetic as well as number theoretic functionality. |
|
5011 * |
|
5012 * The library was designed directly after the MPI library by |
|
5013 * Michael Fromberger but has been written from scratch with |
|
5014 * additional optimizations in place. |
|
5015 * |
|
5016 * The library is free for all purposes without any express |
|
5017 * guarantee it works. |
|
5018 * |
|
5019 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5020 */ |
3
|
5021 |
|
5022 /* d = a * b (mod c) */ |
|
5023 int |
|
5024 mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
|
5025 { |
|
5026 int res; |
|
5027 mp_int t; |
|
5028 |
|
5029 if ((res = mp_init (&t)) != MP_OKAY) { |
|
5030 return res; |
|
5031 } |
|
5032 |
|
5033 if ((res = mp_mul (a, b, &t)) != MP_OKAY) { |
|
5034 mp_clear (&t); |
|
5035 return res; |
|
5036 } |
|
5037 res = mp_mod (&t, c, d); |
|
5038 mp_clear (&t); |
|
5039 return res; |
|
5040 } |
143
|
5041 #endif |
3
|
5042 |
|
5043 /* End: bn_mp_mulmod.c */ |
|
5044 |
|
5045 /* Start: bn_mp_n_root.c */ |
143
|
5046 #include <ltc_tommath.h> |
|
5047 #ifdef BN_MP_N_ROOT_C |
|
5048 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5049 * |
|
5050 * LibTomMath is a library that provides multiple-precision |
|
5051 * integer arithmetic as well as number theoretic functionality. |
|
5052 * |
|
5053 * The library was designed directly after the MPI library by |
|
5054 * Michael Fromberger but has been written from scratch with |
|
5055 * additional optimizations in place. |
|
5056 * |
|
5057 * The library is free for all purposes without any express |
|
5058 * guarantee it works. |
|
5059 * |
|
5060 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5061 */ |
3
|
5062 |
|
5063 /* find the n'th root of an integer |
|
5064 * |
|
5065 * Result found such that (c)**b <= a and (c+1)**b > a |
|
5066 * |
|
5067 * This algorithm uses Newton's approximation |
|
5068 * x[i+1] = x[i] - f(x[i])/f'(x[i]) |
|
5069 * which will find the root in log(N) time where |
|
5070 * each step involves a fair bit. This is not meant to |
|
5071 * find huge roots [square and cube, etc]. |
|
5072 */ |
|
5073 int mp_n_root (mp_int * a, mp_digit b, mp_int * c) |
|
5074 { |
|
5075 mp_int t1, t2, t3; |
|
5076 int res, neg; |
|
5077 |
|
5078 /* input must be positive if b is even */ |
|
5079 if ((b & 1) == 0 && a->sign == MP_NEG) { |
|
5080 return MP_VAL; |
|
5081 } |
|
5082 |
|
5083 if ((res = mp_init (&t1)) != MP_OKAY) { |
|
5084 return res; |
|
5085 } |
|
5086 |
|
5087 if ((res = mp_init (&t2)) != MP_OKAY) { |
|
5088 goto __T1; |
|
5089 } |
|
5090 |
|
5091 if ((res = mp_init (&t3)) != MP_OKAY) { |
|
5092 goto __T2; |
|
5093 } |
|
5094 |
|
5095 /* if a is negative fudge the sign but keep track */ |
|
5096 neg = a->sign; |
|
5097 a->sign = MP_ZPOS; |
|
5098 |
|
5099 /* t2 = 2 */ |
|
5100 mp_set (&t2, 2); |
|
5101 |
|
5102 do { |
|
5103 /* t1 = t2 */ |
|
5104 if ((res = mp_copy (&t2, &t1)) != MP_OKAY) { |
|
5105 goto __T3; |
|
5106 } |
|
5107 |
|
5108 /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ |
|
5109 |
|
5110 /* t3 = t1**(b-1) */ |
|
5111 if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) { |
|
5112 goto __T3; |
|
5113 } |
|
5114 |
|
5115 /* numerator */ |
|
5116 /* t2 = t1**b */ |
|
5117 if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) { |
|
5118 goto __T3; |
|
5119 } |
|
5120 |
|
5121 /* t2 = t1**b - a */ |
|
5122 if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) { |
|
5123 goto __T3; |
|
5124 } |
|
5125 |
|
5126 /* denominator */ |
|
5127 /* t3 = t1**(b-1) * b */ |
|
5128 if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) { |
|
5129 goto __T3; |
|
5130 } |
|
5131 |
|
5132 /* t3 = (t1**b - a)/(b * t1**(b-1)) */ |
|
5133 if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) { |
|
5134 goto __T3; |
|
5135 } |
|
5136 |
|
5137 if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) { |
|
5138 goto __T3; |
|
5139 } |
|
5140 } while (mp_cmp (&t1, &t2) != MP_EQ); |
|
5141 |
|
5142 /* result can be off by a few so check */ |
|
5143 for (;;) { |
|
5144 if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) { |
|
5145 goto __T3; |
|
5146 } |
|
5147 |
|
5148 if (mp_cmp (&t2, a) == MP_GT) { |
|
5149 if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) { |
|
5150 goto __T3; |
|
5151 } |
|
5152 } else { |
|
5153 break; |
|
5154 } |
|
5155 } |
|
5156 |
|
5157 /* reset the sign of a first */ |
|
5158 a->sign = neg; |
|
5159 |
|
5160 /* set the result */ |
|
5161 mp_exch (&t1, c); |
|
5162 |
|
5163 /* set the sign of the result */ |
|
5164 c->sign = neg; |
|
5165 |
|
5166 res = MP_OKAY; |
|
5167 |
|
5168 __T3:mp_clear (&t3); |
|
5169 __T2:mp_clear (&t2); |
|
5170 __T1:mp_clear (&t1); |
|
5171 return res; |
|
5172 } |
143
|
5173 #endif |
3
|
5174 |
|
5175 /* End: bn_mp_n_root.c */ |
|
5176 |
|
5177 /* Start: bn_mp_neg.c */ |
143
|
5178 #include <ltc_tommath.h> |
|
5179 #ifdef BN_MP_NEG_C |
|
5180 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5181 * |
|
5182 * LibTomMath is a library that provides multiple-precision |
|
5183 * integer arithmetic as well as number theoretic functionality. |
|
5184 * |
|
5185 * The library was designed directly after the MPI library by |
|
5186 * Michael Fromberger but has been written from scratch with |
|
5187 * additional optimizations in place. |
|
5188 * |
|
5189 * The library is free for all purposes without any express |
|
5190 * guarantee it works. |
|
5191 * |
|
5192 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5193 */ |
3
|
5194 |
|
5195 /* b = -a */ |
|
5196 int mp_neg (mp_int * a, mp_int * b) |
|
5197 { |
|
5198 int res; |
|
5199 if ((res = mp_copy (a, b)) != MP_OKAY) { |
|
5200 return res; |
|
5201 } |
|
5202 if (mp_iszero(b) != MP_YES) { |
|
5203 b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS; |
|
5204 } |
|
5205 return MP_OKAY; |
|
5206 } |
143
|
5207 #endif |
3
|
5208 |
|
5209 /* End: bn_mp_neg.c */ |
|
5210 |
|
5211 /* Start: bn_mp_or.c */ |
143
|
5212 #include <ltc_tommath.h> |
|
5213 #ifdef BN_MP_OR_C |
|
5214 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5215 * |
|
5216 * LibTomMath is a library that provides multiple-precision |
|
5217 * integer arithmetic as well as number theoretic functionality. |
|
5218 * |
|
5219 * The library was designed directly after the MPI library by |
|
5220 * Michael Fromberger but has been written from scratch with |
|
5221 * additional optimizations in place. |
|
5222 * |
|
5223 * The library is free for all purposes without any express |
|
5224 * guarantee it works. |
|
5225 * |
|
5226 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5227 */ |
3
|
5228 |
|
5229 /* OR two ints together */ |
|
5230 int mp_or (mp_int * a, mp_int * b, mp_int * c) |
|
5231 { |
|
5232 int res, ix, px; |
|
5233 mp_int t, *x; |
|
5234 |
|
5235 if (a->used > b->used) { |
|
5236 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
5237 return res; |
|
5238 } |
|
5239 px = b->used; |
|
5240 x = b; |
|
5241 } else { |
|
5242 if ((res = mp_init_copy (&t, b)) != MP_OKAY) { |
|
5243 return res; |
|
5244 } |
|
5245 px = a->used; |
|
5246 x = a; |
|
5247 } |
|
5248 |
|
5249 for (ix = 0; ix < px; ix++) { |
|
5250 t.dp[ix] |= x->dp[ix]; |
|
5251 } |
|
5252 mp_clamp (&t); |
|
5253 mp_exch (c, &t); |
|
5254 mp_clear (&t); |
|
5255 return MP_OKAY; |
|
5256 } |
143
|
5257 #endif |
3
|
5258 |
|
5259 /* End: bn_mp_or.c */ |
|
5260 |
|
5261 /* Start: bn_mp_prime_fermat.c */ |
143
|
5262 #include <ltc_tommath.h> |
|
5263 #ifdef BN_MP_PRIME_FERMAT_C |
|
5264 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5265 * |
|
5266 * LibTomMath is a library that provides multiple-precision |
|
5267 * integer arithmetic as well as number theoretic functionality. |
|
5268 * |
|
5269 * The library was designed directly after the MPI library by |
|
5270 * Michael Fromberger but has been written from scratch with |
|
5271 * additional optimizations in place. |
|
5272 * |
|
5273 * The library is free for all purposes without any express |
|
5274 * guarantee it works. |
|
5275 * |
|
5276 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5277 */ |
3
|
5278 |
|
5279 /* performs one Fermat test. |
|
5280 * |
|
5281 * If "a" were prime then b**a == b (mod a) since the order of |
|
5282 * the multiplicative sub-group would be phi(a) = a-1. That means |
|
5283 * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). |
|
5284 * |
|
5285 * Sets result to 1 if the congruence holds, or zero otherwise. |
|
5286 */ |
|
5287 int mp_prime_fermat (mp_int * a, mp_int * b, int *result) |
|
5288 { |
|
5289 mp_int t; |
|
5290 int err; |
|
5291 |
|
5292 /* default to composite */ |
|
5293 *result = MP_NO; |
|
5294 |
|
5295 /* ensure b > 1 */ |
|
5296 if (mp_cmp_d(b, 1) != MP_GT) { |
|
5297 return MP_VAL; |
|
5298 } |
|
5299 |
|
5300 /* init t */ |
|
5301 if ((err = mp_init (&t)) != MP_OKAY) { |
|
5302 return err; |
|
5303 } |
|
5304 |
|
5305 /* compute t = b**a mod a */ |
|
5306 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) { |
|
5307 goto __T; |
|
5308 } |
|
5309 |
|
5310 /* is it equal to b? */ |
|
5311 if (mp_cmp (&t, b) == MP_EQ) { |
|
5312 *result = MP_YES; |
|
5313 } |
|
5314 |
|
5315 err = MP_OKAY; |
|
5316 __T:mp_clear (&t); |
|
5317 return err; |
|
5318 } |
143
|
5319 #endif |
3
|
5320 |
|
5321 /* End: bn_mp_prime_fermat.c */ |
|
5322 |
|
5323 /* Start: bn_mp_prime_is_divisible.c */ |
143
|
5324 #include <ltc_tommath.h> |
|
5325 #ifdef BN_MP_PRIME_IS_DIVISIBLE_C |
|
5326 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5327 * |
|
5328 * LibTomMath is a library that provides multiple-precision |
|
5329 * integer arithmetic as well as number theoretic functionality. |
|
5330 * |
|
5331 * The library was designed directly after the MPI library by |
|
5332 * Michael Fromberger but has been written from scratch with |
|
5333 * additional optimizations in place. |
|
5334 * |
|
5335 * The library is free for all purposes without any express |
|
5336 * guarantee it works. |
|
5337 * |
|
5338 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5339 */ |
3
|
5340 |
|
5341 /* determines if an integers is divisible by one |
|
5342 * of the first PRIME_SIZE primes or not |
|
5343 * |
|
5344 * sets result to 0 if not, 1 if yes |
|
5345 */ |
|
5346 int mp_prime_is_divisible (mp_int * a, int *result) |
|
5347 { |
|
5348 int err, ix; |
|
5349 mp_digit res; |
|
5350 |
|
5351 /* default to not */ |
|
5352 *result = MP_NO; |
|
5353 |
|
5354 for (ix = 0; ix < PRIME_SIZE; ix++) { |
|
5355 /* what is a mod __prime_tab[ix] */ |
|
5356 if ((err = mp_mod_d (a, __prime_tab[ix], &res)) != MP_OKAY) { |
|
5357 return err; |
|
5358 } |
|
5359 |
|
5360 /* is the residue zero? */ |
|
5361 if (res == 0) { |
|
5362 *result = MP_YES; |
|
5363 return MP_OKAY; |
|
5364 } |
|
5365 } |
|
5366 |
|
5367 return MP_OKAY; |
|
5368 } |
143
|
5369 #endif |
3
|
5370 |
|
5371 /* End: bn_mp_prime_is_divisible.c */ |
|
5372 |
|
5373 /* Start: bn_mp_prime_is_prime.c */ |
143
|
5374 #include <ltc_tommath.h> |
|
5375 #ifdef BN_MP_PRIME_IS_PRIME_C |
|
5376 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5377 * |
|
5378 * LibTomMath is a library that provides multiple-precision |
|
5379 * integer arithmetic as well as number theoretic functionality. |
|
5380 * |
|
5381 * The library was designed directly after the MPI library by |
|
5382 * Michael Fromberger but has been written from scratch with |
|
5383 * additional optimizations in place. |
|
5384 * |
|
5385 * The library is free for all purposes without any express |
|
5386 * guarantee it works. |
|
5387 * |
|
5388 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5389 */ |
3
|
5390 |
|
5391 /* performs a variable number of rounds of Miller-Rabin |
|
5392 * |
|
5393 * Probability of error after t rounds is no more than |
143
|
5394 |
3
|
5395 * |
|
5396 * Sets result to 1 if probably prime, 0 otherwise |
|
5397 */ |
|
5398 int mp_prime_is_prime (mp_int * a, int t, int *result) |
|
5399 { |
|
5400 mp_int b; |
|
5401 int ix, err, res; |
|
5402 |
|
5403 /* default to no */ |
|
5404 *result = MP_NO; |
|
5405 |
|
5406 /* valid value of t? */ |
|
5407 if (t <= 0 || t > PRIME_SIZE) { |
|
5408 return MP_VAL; |
|
5409 } |
|
5410 |
|
5411 /* is the input equal to one of the primes in the table? */ |
|
5412 for (ix = 0; ix < PRIME_SIZE; ix++) { |
|
5413 if (mp_cmp_d(a, __prime_tab[ix]) == MP_EQ) { |
|
5414 *result = 1; |
|
5415 return MP_OKAY; |
|
5416 } |
|
5417 } |
|
5418 |
|
5419 /* first perform trial division */ |
|
5420 if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) { |
|
5421 return err; |
|
5422 } |
|
5423 |
|
5424 /* return if it was trivially divisible */ |
|
5425 if (res == MP_YES) { |
|
5426 return MP_OKAY; |
|
5427 } |
|
5428 |
|
5429 /* now perform the miller-rabin rounds */ |
|
5430 if ((err = mp_init (&b)) != MP_OKAY) { |
|
5431 return err; |
|
5432 } |
|
5433 |
|
5434 for (ix = 0; ix < t; ix++) { |
|
5435 /* set the prime */ |
|
5436 mp_set (&b, __prime_tab[ix]); |
|
5437 |
|
5438 if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) { |
|
5439 goto __B; |
|
5440 } |
|
5441 |
|
5442 if (res == MP_NO) { |
|
5443 goto __B; |
|
5444 } |
|
5445 } |
|
5446 |
|
5447 /* passed the test */ |
|
5448 *result = MP_YES; |
|
5449 __B:mp_clear (&b); |
|
5450 return err; |
|
5451 } |
143
|
5452 #endif |
3
|
5453 |
|
5454 /* End: bn_mp_prime_is_prime.c */ |
|
5455 |
|
5456 /* Start: bn_mp_prime_miller_rabin.c */ |
143
|
5457 #include <ltc_tommath.h> |
|
5458 #ifdef BN_MP_PRIME_MILLER_RABIN_C |
|
5459 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5460 * |
|
5461 * LibTomMath is a library that provides multiple-precision |
|
5462 * integer arithmetic as well as number theoretic functionality. |
|
5463 * |
|
5464 * The library was designed directly after the MPI library by |
|
5465 * Michael Fromberger but has been written from scratch with |
|
5466 * additional optimizations in place. |
|
5467 * |
|
5468 * The library is free for all purposes without any express |
|
5469 * guarantee it works. |
|
5470 * |
|
5471 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5472 */ |
3
|
5473 |
|
5474 /* Miller-Rabin test of "a" to the base of "b" as described in |
|
5475 * HAC pp. 139 Algorithm 4.24 |
|
5476 * |
|
5477 * Sets result to 0 if definitely composite or 1 if probably prime. |
|
5478 * Randomly the chance of error is no more than 1/4 and often |
|
5479 * very much lower. |
|
5480 */ |
|
5481 int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) |
|
5482 { |
|
5483 mp_int n1, y, r; |
|
5484 int s, j, err; |
|
5485 |
|
5486 /* default */ |
|
5487 *result = MP_NO; |
|
5488 |
|
5489 /* ensure b > 1 */ |
|
5490 if (mp_cmp_d(b, 1) != MP_GT) { |
|
5491 return MP_VAL; |
|
5492 } |
|
5493 |
|
5494 /* get n1 = a - 1 */ |
|
5495 if ((err = mp_init_copy (&n1, a)) != MP_OKAY) { |
|
5496 return err; |
|
5497 } |
|
5498 if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) { |
|
5499 goto __N1; |
|
5500 } |
|
5501 |
|
5502 /* set 2**s * r = n1 */ |
|
5503 if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) { |
|
5504 goto __N1; |
|
5505 } |
|
5506 |
|
5507 /* count the number of least significant bits |
|
5508 * which are zero |
|
5509 */ |
|
5510 s = mp_cnt_lsb(&r); |
|
5511 |
|
5512 /* now divide n - 1 by 2**s */ |
|
5513 if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) { |
|
5514 goto __R; |
|
5515 } |
|
5516 |
|
5517 /* compute y = b**r mod a */ |
|
5518 if ((err = mp_init (&y)) != MP_OKAY) { |
|
5519 goto __R; |
|
5520 } |
|
5521 if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) { |
|
5522 goto __Y; |
|
5523 } |
|
5524 |
|
5525 /* if y != 1 and y != n1 do */ |
|
5526 if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) { |
|
5527 j = 1; |
|
5528 /* while j <= s-1 and y != n1 */ |
|
5529 while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) { |
|
5530 if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) { |
|
5531 goto __Y; |
|
5532 } |
|
5533 |
|
5534 /* if y == 1 then composite */ |
|
5535 if (mp_cmp_d (&y, 1) == MP_EQ) { |
|
5536 goto __Y; |
|
5537 } |
|
5538 |
|
5539 ++j; |
|
5540 } |
|
5541 |
|
5542 /* if y != n1 then composite */ |
|
5543 if (mp_cmp (&y, &n1) != MP_EQ) { |
|
5544 goto __Y; |
|
5545 } |
|
5546 } |
|
5547 |
|
5548 /* probably prime now */ |
|
5549 *result = MP_YES; |
|
5550 __Y:mp_clear (&y); |
|
5551 __R:mp_clear (&r); |
|
5552 __N1:mp_clear (&n1); |
|
5553 return err; |
|
5554 } |
143
|
5555 #endif |
3
|
5556 |
|
5557 /* End: bn_mp_prime_miller_rabin.c */ |
|
5558 |
|
5559 /* Start: bn_mp_prime_next_prime.c */ |
143
|
5560 #include <ltc_tommath.h> |
|
5561 #ifdef BN_MP_PRIME_NEXT_PRIME_C |
|
5562 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5563 * |
|
5564 * LibTomMath is a library that provides multiple-precision |
|
5565 * integer arithmetic as well as number theoretic functionality. |
|
5566 * |
|
5567 * The library was designed directly after the MPI library by |
|
5568 * Michael Fromberger but has been written from scratch with |
|
5569 * additional optimizations in place. |
|
5570 * |
|
5571 * The library is free for all purposes without any express |
|
5572 * guarantee it works. |
|
5573 * |
|
5574 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5575 */ |
3
|
5576 |
|
5577 /* finds the next prime after the number "a" using "t" trials |
|
5578 * of Miller-Rabin. |
|
5579 * |
|
5580 * bbs_style = 1 means the prime must be congruent to 3 mod 4 |
|
5581 */ |
|
5582 int mp_prime_next_prime(mp_int *a, int t, int bbs_style) |
|
5583 { |
|
5584 int err, res, x, y; |
|
5585 mp_digit res_tab[PRIME_SIZE], step, kstep; |
|
5586 mp_int b; |
|
5587 |
|
5588 /* ensure t is valid */ |
|
5589 if (t <= 0 || t > PRIME_SIZE) { |
|
5590 return MP_VAL; |
|
5591 } |
|
5592 |
|
5593 /* force positive */ |
|
5594 a->sign = MP_ZPOS; |
|
5595 |
|
5596 /* simple algo if a is less than the largest prime in the table */ |
|
5597 if (mp_cmp_d(a, __prime_tab[PRIME_SIZE-1]) == MP_LT) { |
|
5598 /* find which prime it is bigger than */ |
|
5599 for (x = PRIME_SIZE - 2; x >= 0; x--) { |
|
5600 if (mp_cmp_d(a, __prime_tab[x]) != MP_LT) { |
|
5601 if (bbs_style == 1) { |
|
5602 /* ok we found a prime smaller or |
|
5603 * equal [so the next is larger] |
|
5604 * |
|
5605 * however, the prime must be |
|
5606 * congruent to 3 mod 4 |
|
5607 */ |
|
5608 if ((__prime_tab[x + 1] & 3) != 3) { |
|
5609 /* scan upwards for a prime congruent to 3 mod 4 */ |
|
5610 for (y = x + 1; y < PRIME_SIZE; y++) { |
|
5611 if ((__prime_tab[y] & 3) == 3) { |
|
5612 mp_set(a, __prime_tab[y]); |
|
5613 return MP_OKAY; |
|
5614 } |
|
5615 } |
|
5616 } |
|
5617 } else { |
|
5618 mp_set(a, __prime_tab[x + 1]); |
|
5619 return MP_OKAY; |
|
5620 } |
|
5621 } |
|
5622 } |
|
5623 /* at this point a maybe 1 */ |
|
5624 if (mp_cmp_d(a, 1) == MP_EQ) { |
|
5625 mp_set(a, 2); |
|
5626 return MP_OKAY; |
|
5627 } |
|
5628 /* fall through to the sieve */ |
|
5629 } |
|
5630 |
|
5631 /* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */ |
|
5632 if (bbs_style == 1) { |
|
5633 kstep = 4; |
|
5634 } else { |
|
5635 kstep = 2; |
|
5636 } |
|
5637 |
|
5638 /* at this point we will use a combination of a sieve and Miller-Rabin */ |
|
5639 |
|
5640 if (bbs_style == 1) { |
|
5641 /* if a mod 4 != 3 subtract the correct value to make it so */ |
|
5642 if ((a->dp[0] & 3) != 3) { |
|
5643 if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { return err; }; |
|
5644 } |
|
5645 } else { |
|
5646 if (mp_iseven(a) == 1) { |
|
5647 /* force odd */ |
|
5648 if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { |
|
5649 return err; |
|
5650 } |
|
5651 } |
|
5652 } |
|
5653 |
|
5654 /* generate the restable */ |
|
5655 for (x = 1; x < PRIME_SIZE; x++) { |
|
5656 if ((err = mp_mod_d(a, __prime_tab[x], res_tab + x)) != MP_OKAY) { |
|
5657 return err; |
|
5658 } |
|
5659 } |
|
5660 |
|
5661 /* init temp used for Miller-Rabin Testing */ |
|
5662 if ((err = mp_init(&b)) != MP_OKAY) { |
|
5663 return err; |
|
5664 } |
|
5665 |
|
5666 for (;;) { |
|
5667 /* skip to the next non-trivially divisible candidate */ |
|
5668 step = 0; |
|
5669 do { |
|
5670 /* y == 1 if any residue was zero [e.g. cannot be prime] */ |
|
5671 y = 0; |
|
5672 |
|
5673 /* increase step to next candidate */ |
|
5674 step += kstep; |
|
5675 |
|
5676 /* compute the new residue without using division */ |
|
5677 for (x = 1; x < PRIME_SIZE; x++) { |
|
5678 /* add the step to each residue */ |
|
5679 res_tab[x] += kstep; |
|
5680 |
|
5681 /* subtract the modulus [instead of using division] */ |
|
5682 if (res_tab[x] >= __prime_tab[x]) { |
|
5683 res_tab[x] -= __prime_tab[x]; |
|
5684 } |
|
5685 |
|
5686 /* set flag if zero */ |
|
5687 if (res_tab[x] == 0) { |
|
5688 y = 1; |
|
5689 } |
|
5690 } |
|
5691 } while (y == 1 && step < ((((mp_digit)1)<<DIGIT_BIT) - kstep)); |
|
5692 |
|
5693 /* add the step */ |
|
5694 if ((err = mp_add_d(a, step, a)) != MP_OKAY) { |
|
5695 goto __ERR; |
|
5696 } |
|
5697 |
|
5698 /* if didn't pass sieve and step == MAX then skip test */ |
|
5699 if (y == 1 && step >= ((((mp_digit)1)<<DIGIT_BIT) - kstep)) { |
|
5700 continue; |
|
5701 } |
|
5702 |
|
5703 /* is this prime? */ |
|
5704 for (x = 0; x < t; x++) { |
|
5705 mp_set(&b, __prime_tab[t]); |
|
5706 if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { |
|
5707 goto __ERR; |
|
5708 } |
|
5709 if (res == MP_NO) { |
|
5710 break; |
|
5711 } |
|
5712 } |
|
5713 |
|
5714 if (res == MP_YES) { |
|
5715 break; |
|
5716 } |
|
5717 } |
|
5718 |
|
5719 err = MP_OKAY; |
|
5720 __ERR: |
|
5721 mp_clear(&b); |
|
5722 return err; |
|
5723 } |
|
5724 |
143
|
5725 #endif |
3
|
5726 |
|
5727 /* End: bn_mp_prime_next_prime.c */ |
|
5728 |
143
|
5729 /* Start: bn_mp_prime_rabin_miller_trials.c */ |
|
5730 #include <ltc_tommath.h> |
|
5731 #ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C |
|
5732 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5733 * |
|
5734 * LibTomMath is a library that provides multiple-precision |
|
5735 * integer arithmetic as well as number theoretic functionality. |
|
5736 * |
|
5737 * The library was designed directly after the MPI library by |
|
5738 * Michael Fromberger but has been written from scratch with |
|
5739 * additional optimizations in place. |
|
5740 * |
|
5741 * The library is free for all purposes without any express |
|
5742 * guarantee it works. |
|
5743 * |
|
5744 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5745 */ |
|
5746 |
|
5747 |
|
5748 static const struct { |
|
5749 int k, t; |
|
5750 } sizes[] = { |
|
5751 { 128, 28 }, |
|
5752 { 256, 16 }, |
|
5753 { 384, 10 }, |
|
5754 { 512, 7 }, |
|
5755 { 640, 6 }, |
|
5756 { 768, 5 }, |
|
5757 { 896, 4 }, |
|
5758 { 1024, 4 } |
|
5759 }; |
|
5760 |
|
5761 /* returns # of RM trials required for a given bit size */ |
|
5762 int mp_prime_rabin_miller_trials(int size) |
|
5763 { |
|
5764 int x; |
|
5765 |
|
5766 for (x = 0; x < (int)(sizeof(sizes)/(sizeof(sizes[0]))); x++) { |
|
5767 if (sizes[x].k == size) { |
|
5768 return sizes[x].t; |
|
5769 } else if (sizes[x].k > size) { |
|
5770 return (x == 0) ? sizes[0].t : sizes[x - 1].t; |
|
5771 } |
|
5772 } |
|
5773 return sizes[x-1].t + 1; |
|
5774 } |
|
5775 |
|
5776 |
|
5777 #endif |
|
5778 |
|
5779 /* End: bn_mp_prime_rabin_miller_trials.c */ |
|
5780 |
3
|
5781 /* Start: bn_mp_prime_random_ex.c */ |
143
|
5782 #include <ltc_tommath.h> |
|
5783 #ifdef BN_MP_PRIME_RANDOM_EX_C |
|
5784 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5785 * |
|
5786 * LibTomMath is a library that provides multiple-precision |
|
5787 * integer arithmetic as well as number theoretic functionality. |
|
5788 * |
|
5789 * The library was designed directly after the MPI library by |
|
5790 * Michael Fromberger but has been written from scratch with |
|
5791 * additional optimizations in place. |
|
5792 * |
|
5793 * The library is free for all purposes without any express |
|
5794 * guarantee it works. |
|
5795 * |
|
5796 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5797 */ |
3
|
5798 |
|
5799 /* makes a truly random prime of a given size (bits), |
|
5800 * |
|
5801 * Flags are as follows: |
|
5802 * |
|
5803 * LTM_PRIME_BBS - make prime congruent to 3 mod 4 |
|
5804 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) |
|
5805 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero |
|
5806 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one |
|
5807 * |
|
5808 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can |
|
5809 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself |
|
5810 * so it can be NULL |
|
5811 * |
|
5812 */ |
|
5813 |
|
5814 /* This is possibly the mother of all prime generation functions, muahahahahaha! */ |
|
5815 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat) |
|
5816 { |
|
5817 unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb; |
|
5818 int res, err, bsize, maskOR_msb_offset; |
|
5819 |
|
5820 /* sanity check the input */ |
|
5821 if (size <= 1 || t <= 0) { |
|
5822 return MP_VAL; |
|
5823 } |
|
5824 |
|
5825 /* LTM_PRIME_SAFE implies LTM_PRIME_BBS */ |
|
5826 if (flags & LTM_PRIME_SAFE) { |
|
5827 flags |= LTM_PRIME_BBS; |
|
5828 } |
|
5829 |
|
5830 /* calc the byte size */ |
|
5831 bsize = (size>>3)+(size&7?1:0); |
|
5832 |
|
5833 /* we need a buffer of bsize bytes */ |
|
5834 tmp = OPT_CAST(unsigned char) XMALLOC(bsize); |
|
5835 if (tmp == NULL) { |
|
5836 return MP_MEM; |
|
5837 } |
|
5838 |
|
5839 /* calc the maskAND value for the MSbyte*/ |
|
5840 maskAND = 0xFF >> (8 - (size & 7)); |
|
5841 |
|
5842 /* calc the maskOR_msb */ |
|
5843 maskOR_msb = 0; |
|
5844 maskOR_msb_offset = (size - 2) >> 3; |
|
5845 if (flags & LTM_PRIME_2MSB_ON) { |
|
5846 maskOR_msb |= 1 << ((size - 2) & 7); |
|
5847 } else if (flags & LTM_PRIME_2MSB_OFF) { |
|
5848 maskAND &= ~(1 << ((size - 2) & 7)); |
|
5849 } |
|
5850 |
|
5851 /* get the maskOR_lsb */ |
|
5852 maskOR_lsb = 0; |
|
5853 if (flags & LTM_PRIME_BBS) { |
|
5854 maskOR_lsb |= 3; |
|
5855 } |
|
5856 |
|
5857 do { |
|
5858 /* read the bytes */ |
|
5859 if (cb(tmp, bsize, dat) != bsize) { |
|
5860 err = MP_VAL; |
|
5861 goto error; |
|
5862 } |
|
5863 |
|
5864 /* work over the MSbyte */ |
|
5865 tmp[0] &= maskAND; |
|
5866 tmp[0] |= 1 << ((size - 1) & 7); |
|
5867 |
|
5868 /* mix in the maskORs */ |
|
5869 tmp[maskOR_msb_offset] |= maskOR_msb; |
|
5870 tmp[bsize-1] |= maskOR_lsb; |
|
5871 |
|
5872 /* read it in */ |
|
5873 if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) { goto error; } |
|
5874 |
|
5875 /* is it prime? */ |
|
5876 if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; } |
143
|
5877 if (res == MP_NO) { |
|
5878 continue; |
|
5879 } |
3
|
5880 |
|
5881 if (flags & LTM_PRIME_SAFE) { |
|
5882 /* see if (a-1)/2 is prime */ |
|
5883 if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; } |
|
5884 if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; } |
|
5885 |
|
5886 /* is it prime? */ |
|
5887 if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; } |
|
5888 } |
|
5889 } while (res == MP_NO); |
|
5890 |
|
5891 if (flags & LTM_PRIME_SAFE) { |
|
5892 /* restore a to the original value */ |
|
5893 if ((err = mp_mul_2(a, a)) != MP_OKAY) { goto error; } |
|
5894 if ((err = mp_add_d(a, 1, a)) != MP_OKAY) { goto error; } |
|
5895 } |
|
5896 |
|
5897 err = MP_OKAY; |
|
5898 error: |
|
5899 XFREE(tmp); |
|
5900 return err; |
|
5901 } |
|
5902 |
|
5903 |
143
|
5904 #endif |
3
|
5905 |
|
5906 /* End: bn_mp_prime_random_ex.c */ |
|
5907 |
|
5908 /* Start: bn_mp_radix_size.c */ |
143
|
5909 #include <ltc_tommath.h> |
|
5910 #ifdef BN_MP_RADIX_SIZE_C |
|
5911 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5912 * |
|
5913 * LibTomMath is a library that provides multiple-precision |
|
5914 * integer arithmetic as well as number theoretic functionality. |
|
5915 * |
|
5916 * The library was designed directly after the MPI library by |
|
5917 * Michael Fromberger but has been written from scratch with |
|
5918 * additional optimizations in place. |
|
5919 * |
|
5920 * The library is free for all purposes without any express |
|
5921 * guarantee it works. |
|
5922 * |
|
5923 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5924 */ |
3
|
5925 |
|
5926 /* returns size of ASCII reprensentation */ |
|
5927 int mp_radix_size (mp_int * a, int radix, int *size) |
|
5928 { |
|
5929 int res, digs; |
|
5930 mp_int t; |
|
5931 mp_digit d; |
|
5932 |
|
5933 *size = 0; |
|
5934 |
|
5935 /* special case for binary */ |
|
5936 if (radix == 2) { |
|
5937 *size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1; |
|
5938 return MP_OKAY; |
|
5939 } |
|
5940 |
|
5941 /* make sure the radix is in range */ |
|
5942 if (radix < 2 || radix > 64) { |
|
5943 return MP_VAL; |
|
5944 } |
|
5945 |
|
5946 /* init a copy of the input */ |
|
5947 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
5948 return res; |
|
5949 } |
|
5950 |
|
5951 /* digs is the digit count */ |
|
5952 digs = 0; |
|
5953 |
|
5954 /* if it's negative add one for the sign */ |
|
5955 if (t.sign == MP_NEG) { |
|
5956 ++digs; |
|
5957 t.sign = MP_ZPOS; |
|
5958 } |
|
5959 |
|
5960 /* fetch out all of the digits */ |
|
5961 while (mp_iszero (&t) == 0) { |
|
5962 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { |
|
5963 mp_clear (&t); |
|
5964 return res; |
|
5965 } |
|
5966 ++digs; |
|
5967 } |
|
5968 mp_clear (&t); |
|
5969 |
|
5970 /* return digs + 1, the 1 is for the NULL byte that would be required. */ |
|
5971 *size = digs + 1; |
|
5972 return MP_OKAY; |
|
5973 } |
|
5974 |
143
|
5975 #endif |
3
|
5976 |
|
5977 /* End: bn_mp_radix_size.c */ |
|
5978 |
|
5979 /* Start: bn_mp_radix_smap.c */ |
143
|
5980 #include <ltc_tommath.h> |
|
5981 #ifdef BN_MP_RADIX_SMAP_C |
|
5982 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
5983 * |
|
5984 * LibTomMath is a library that provides multiple-precision |
|
5985 * integer arithmetic as well as number theoretic functionality. |
|
5986 * |
|
5987 * The library was designed directly after the MPI library by |
|
5988 * Michael Fromberger but has been written from scratch with |
|
5989 * additional optimizations in place. |
|
5990 * |
|
5991 * The library is free for all purposes without any express |
|
5992 * guarantee it works. |
|
5993 * |
|
5994 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
5995 */ |
3
|
5996 |
|
5997 /* chars used in radix conversions */ |
|
5998 const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; |
143
|
5999 #endif |
3
|
6000 |
|
6001 /* End: bn_mp_radix_smap.c */ |
|
6002 |
|
6003 /* Start: bn_mp_rand.c */ |
143
|
6004 #include <ltc_tommath.h> |
|
6005 #ifdef BN_MP_RAND_C |
|
6006 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6007 * |
|
6008 * LibTomMath is a library that provides multiple-precision |
|
6009 * integer arithmetic as well as number theoretic functionality. |
|
6010 * |
|
6011 * The library was designed directly after the MPI library by |
|
6012 * Michael Fromberger but has been written from scratch with |
|
6013 * additional optimizations in place. |
|
6014 * |
|
6015 * The library is free for all purposes without any express |
|
6016 * guarantee it works. |
|
6017 * |
|
6018 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6019 */ |
3
|
6020 |
|
6021 /* makes a pseudo-random int of a given size */ |
|
6022 int |
|
6023 mp_rand (mp_int * a, int digits) |
|
6024 { |
|
6025 int res; |
|
6026 mp_digit d; |
|
6027 |
|
6028 mp_zero (a); |
|
6029 if (digits <= 0) { |
|
6030 return MP_OKAY; |
|
6031 } |
|
6032 |
|
6033 /* first place a random non-zero digit */ |
|
6034 do { |
|
6035 d = ((mp_digit) abs (rand ())); |
|
6036 } while (d == 0); |
|
6037 |
|
6038 if ((res = mp_add_d (a, d, a)) != MP_OKAY) { |
|
6039 return res; |
|
6040 } |
|
6041 |
|
6042 while (digits-- > 0) { |
|
6043 if ((res = mp_lshd (a, 1)) != MP_OKAY) { |
|
6044 return res; |
|
6045 } |
|
6046 |
|
6047 if ((res = mp_add_d (a, ((mp_digit) abs (rand ())), a)) != MP_OKAY) { |
|
6048 return res; |
|
6049 } |
|
6050 } |
|
6051 |
|
6052 return MP_OKAY; |
|
6053 } |
143
|
6054 #endif |
3
|
6055 |
|
6056 /* End: bn_mp_rand.c */ |
|
6057 |
|
6058 /* Start: bn_mp_read_radix.c */ |
143
|
6059 #include <ltc_tommath.h> |
|
6060 #ifdef BN_MP_READ_RADIX_C |
|
6061 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6062 * |
|
6063 * LibTomMath is a library that provides multiple-precision |
|
6064 * integer arithmetic as well as number theoretic functionality. |
|
6065 * |
|
6066 * The library was designed directly after the MPI library by |
|
6067 * Michael Fromberger but has been written from scratch with |
|
6068 * additional optimizations in place. |
|
6069 * |
|
6070 * The library is free for all purposes without any express |
|
6071 * guarantee it works. |
|
6072 * |
|
6073 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6074 */ |
3
|
6075 |
|
6076 /* read a string [ASCII] in a given radix */ |
|
6077 int mp_read_radix (mp_int * a, char *str, int radix) |
|
6078 { |
|
6079 int y, res, neg; |
|
6080 char ch; |
|
6081 |
|
6082 /* make sure the radix is ok */ |
|
6083 if (radix < 2 || radix > 64) { |
|
6084 return MP_VAL; |
|
6085 } |
|
6086 |
|
6087 /* if the leading digit is a |
|
6088 * minus set the sign to negative. |
|
6089 */ |
|
6090 if (*str == '-') { |
|
6091 ++str; |
|
6092 neg = MP_NEG; |
|
6093 } else { |
|
6094 neg = MP_ZPOS; |
|
6095 } |
|
6096 |
|
6097 /* set the integer to the default of zero */ |
|
6098 mp_zero (a); |
|
6099 |
|
6100 /* process each digit of the string */ |
|
6101 while (*str) { |
|
6102 /* if the radix < 36 the conversion is case insensitive |
|
6103 * this allows numbers like 1AB and 1ab to represent the same value |
|
6104 * [e.g. in hex] |
|
6105 */ |
|
6106 ch = (char) ((radix < 36) ? toupper (*str) : *str); |
|
6107 for (y = 0; y < 64; y++) { |
|
6108 if (ch == mp_s_rmap[y]) { |
|
6109 break; |
|
6110 } |
|
6111 } |
|
6112 |
|
6113 /* if the char was found in the map |
|
6114 * and is less than the given radix add it |
|
6115 * to the number, otherwise exit the loop. |
|
6116 */ |
|
6117 if (y < radix) { |
|
6118 if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { |
|
6119 return res; |
|
6120 } |
|
6121 if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) { |
|
6122 return res; |
|
6123 } |
|
6124 } else { |
|
6125 break; |
|
6126 } |
|
6127 ++str; |
|
6128 } |
|
6129 |
|
6130 /* set the sign only if a != 0 */ |
|
6131 if (mp_iszero(a) != 1) { |
|
6132 a->sign = neg; |
|
6133 } |
|
6134 return MP_OKAY; |
|
6135 } |
143
|
6136 #endif |
3
|
6137 |
|
6138 /* End: bn_mp_read_radix.c */ |
|
6139 |
|
6140 /* Start: bn_mp_read_signed_bin.c */ |
143
|
6141 #include <ltc_tommath.h> |
|
6142 #ifdef BN_MP_READ_SIGNED_BIN_C |
|
6143 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6144 * |
|
6145 * LibTomMath is a library that provides multiple-precision |
|
6146 * integer arithmetic as well as number theoretic functionality. |
|
6147 * |
|
6148 * The library was designed directly after the MPI library by |
|
6149 * Michael Fromberger but has been written from scratch with |
|
6150 * additional optimizations in place. |
|
6151 * |
|
6152 * The library is free for all purposes without any express |
|
6153 * guarantee it works. |
|
6154 * |
|
6155 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6156 */ |
3
|
6157 |
|
6158 /* read signed bin, big endian, first byte is 0==positive or 1==negative */ |
|
6159 int |
|
6160 mp_read_signed_bin (mp_int * a, unsigned char *b, int c) |
|
6161 { |
|
6162 int res; |
|
6163 |
|
6164 /* read magnitude */ |
|
6165 if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) { |
|
6166 return res; |
|
6167 } |
|
6168 |
|
6169 /* first byte is 0 for positive, non-zero for negative */ |
|
6170 if (b[0] == 0) { |
|
6171 a->sign = MP_ZPOS; |
|
6172 } else { |
|
6173 a->sign = MP_NEG; |
|
6174 } |
|
6175 |
|
6176 return MP_OKAY; |
|
6177 } |
143
|
6178 #endif |
3
|
6179 |
|
6180 /* End: bn_mp_read_signed_bin.c */ |
|
6181 |
|
6182 /* Start: bn_mp_read_unsigned_bin.c */ |
143
|
6183 #include <ltc_tommath.h> |
|
6184 #ifdef BN_MP_READ_UNSIGNED_BIN_C |
|
6185 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6186 * |
|
6187 * LibTomMath is a library that provides multiple-precision |
|
6188 * integer arithmetic as well as number theoretic functionality. |
|
6189 * |
|
6190 * The library was designed directly after the MPI library by |
|
6191 * Michael Fromberger but has been written from scratch with |
|
6192 * additional optimizations in place. |
|
6193 * |
|
6194 * The library is free for all purposes without any express |
|
6195 * guarantee it works. |
|
6196 * |
|
6197 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6198 */ |
3
|
6199 |
|
6200 /* reads a unsigned char array, assumes the msb is stored first [big endian] */ |
|
6201 int |
|
6202 mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c) |
|
6203 { |
|
6204 int res; |
|
6205 |
|
6206 /* make sure there are at least two digits */ |
|
6207 if (a->alloc < 2) { |
|
6208 if ((res = mp_grow(a, 2)) != MP_OKAY) { |
|
6209 return res; |
|
6210 } |
|
6211 } |
|
6212 |
|
6213 /* zero the int */ |
|
6214 mp_zero (a); |
|
6215 |
|
6216 /* read the bytes in */ |
|
6217 while (c-- > 0) { |
|
6218 if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) { |
|
6219 return res; |
|
6220 } |
|
6221 |
|
6222 #ifndef MP_8BIT |
|
6223 a->dp[0] |= *b++; |
|
6224 a->used += 1; |
|
6225 #else |
|
6226 a->dp[0] = (*b & MP_MASK); |
|
6227 a->dp[1] |= ((*b++ >> 7U) & 1); |
|
6228 a->used += 2; |
|
6229 #endif |
|
6230 } |
|
6231 mp_clamp (a); |
|
6232 return MP_OKAY; |
|
6233 } |
143
|
6234 #endif |
3
|
6235 |
|
6236 /* End: bn_mp_read_unsigned_bin.c */ |
|
6237 |
|
6238 /* Start: bn_mp_reduce.c */ |
143
|
6239 #include <ltc_tommath.h> |
|
6240 #ifdef BN_MP_REDUCE_C |
|
6241 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6242 * |
|
6243 * LibTomMath is a library that provides multiple-precision |
|
6244 * integer arithmetic as well as number theoretic functionality. |
|
6245 * |
|
6246 * The library was designed directly after the MPI library by |
|
6247 * Michael Fromberger but has been written from scratch with |
|
6248 * additional optimizations in place. |
|
6249 * |
|
6250 * The library is free for all purposes without any express |
|
6251 * guarantee it works. |
|
6252 * |
|
6253 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6254 */ |
3
|
6255 |
|
6256 /* reduces x mod m, assumes 0 < x < m**2, mu is |
|
6257 * precomputed via mp_reduce_setup. |
|
6258 * From HAC pp.604 Algorithm 14.42 |
|
6259 */ |
|
6260 int |
|
6261 mp_reduce (mp_int * x, mp_int * m, mp_int * mu) |
|
6262 { |
|
6263 mp_int q; |
|
6264 int res, um = m->used; |
|
6265 |
|
6266 /* q = x */ |
|
6267 if ((res = mp_init_copy (&q, x)) != MP_OKAY) { |
|
6268 return res; |
|
6269 } |
|
6270 |
|
6271 /* q1 = x / b**(k-1) */ |
|
6272 mp_rshd (&q, um - 1); |
|
6273 |
|
6274 /* according to HAC this optimization is ok */ |
|
6275 if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { |
|
6276 if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) { |
|
6277 goto CLEANUP; |
|
6278 } |
|
6279 } else { |
143
|
6280 #ifdef BN_S_MP_MUL_HIGH_DIGS_C |
3
|
6281 if ((res = s_mp_mul_high_digs (&q, mu, &q, um - 1)) != MP_OKAY) { |
|
6282 goto CLEANUP; |
|
6283 } |
143
|
6284 #elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C) |
|
6285 if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um - 1)) != MP_OKAY) { |
|
6286 goto CLEANUP; |
|
6287 } |
|
6288 #else |
|
6289 { |
|
6290 res = MP_VAL; |
|
6291 goto CLEANUP; |
|
6292 } |
|
6293 #endif |
3
|
6294 } |
|
6295 |
|
6296 /* q3 = q2 / b**(k+1) */ |
|
6297 mp_rshd (&q, um + 1); |
|
6298 |
|
6299 /* x = x mod b**(k+1), quick (no division) */ |
|
6300 if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) { |
|
6301 goto CLEANUP; |
|
6302 } |
|
6303 |
|
6304 /* q = q * m mod b**(k+1), quick (no division) */ |
|
6305 if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) { |
|
6306 goto CLEANUP; |
|
6307 } |
|
6308 |
|
6309 /* x = x - q */ |
|
6310 if ((res = mp_sub (x, &q, x)) != MP_OKAY) { |
|
6311 goto CLEANUP; |
|
6312 } |
|
6313 |
|
6314 /* If x < 0, add b**(k+1) to it */ |
|
6315 if (mp_cmp_d (x, 0) == MP_LT) { |
|
6316 mp_set (&q, 1); |
|
6317 if ((res = mp_lshd (&q, um + 1)) != MP_OKAY) |
|
6318 goto CLEANUP; |
|
6319 if ((res = mp_add (x, &q, x)) != MP_OKAY) |
|
6320 goto CLEANUP; |
|
6321 } |
|
6322 |
|
6323 /* Back off if it's too big */ |
|
6324 while (mp_cmp (x, m) != MP_LT) { |
|
6325 if ((res = s_mp_sub (x, m, x)) != MP_OKAY) { |
|
6326 goto CLEANUP; |
|
6327 } |
|
6328 } |
|
6329 |
|
6330 CLEANUP: |
|
6331 mp_clear (&q); |
|
6332 |
|
6333 return res; |
|
6334 } |
143
|
6335 #endif |
3
|
6336 |
|
6337 /* End: bn_mp_reduce.c */ |
|
6338 |
|
6339 /* Start: bn_mp_reduce_2k.c */ |
143
|
6340 #include <ltc_tommath.h> |
|
6341 #ifdef BN_MP_REDUCE_2K_C |
|
6342 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6343 * |
|
6344 * LibTomMath is a library that provides multiple-precision |
|
6345 * integer arithmetic as well as number theoretic functionality. |
|
6346 * |
|
6347 * The library was designed directly after the MPI library by |
|
6348 * Michael Fromberger but has been written from scratch with |
|
6349 * additional optimizations in place. |
|
6350 * |
|
6351 * The library is free for all purposes without any express |
|
6352 * guarantee it works. |
|
6353 * |
|
6354 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6355 */ |
3
|
6356 |
|
6357 /* reduces a modulo n where n is of the form 2**p - d */ |
|
6358 int |
|
6359 mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d) |
|
6360 { |
|
6361 mp_int q; |
|
6362 int p, res; |
|
6363 |
|
6364 if ((res = mp_init(&q)) != MP_OKAY) { |
|
6365 return res; |
|
6366 } |
|
6367 |
|
6368 p = mp_count_bits(n); |
|
6369 top: |
|
6370 /* q = a/2**p, a = a mod 2**p */ |
|
6371 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |
|
6372 goto ERR; |
|
6373 } |
|
6374 |
|
6375 if (d != 1) { |
|
6376 /* q = q * d */ |
|
6377 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { |
|
6378 goto ERR; |
|
6379 } |
|
6380 } |
|
6381 |
|
6382 /* a = a + q */ |
|
6383 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { |
|
6384 goto ERR; |
|
6385 } |
|
6386 |
|
6387 if (mp_cmp_mag(a, n) != MP_LT) { |
|
6388 s_mp_sub(a, n, a); |
|
6389 goto top; |
|
6390 } |
|
6391 |
|
6392 ERR: |
|
6393 mp_clear(&q); |
|
6394 return res; |
|
6395 } |
|
6396 |
143
|
6397 #endif |
3
|
6398 |
|
6399 /* End: bn_mp_reduce_2k.c */ |
|
6400 |
|
6401 /* Start: bn_mp_reduce_2k_setup.c */ |
143
|
6402 #include <ltc_tommath.h> |
|
6403 #ifdef BN_MP_REDUCE_2K_SETUP_C |
|
6404 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6405 * |
|
6406 * LibTomMath is a library that provides multiple-precision |
|
6407 * integer arithmetic as well as number theoretic functionality. |
|
6408 * |
|
6409 * The library was designed directly after the MPI library by |
|
6410 * Michael Fromberger but has been written from scratch with |
|
6411 * additional optimizations in place. |
|
6412 * |
|
6413 * The library is free for all purposes without any express |
|
6414 * guarantee it works. |
|
6415 * |
|
6416 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6417 */ |
3
|
6418 |
|
6419 /* determines the setup value */ |
|
6420 int |
|
6421 mp_reduce_2k_setup(mp_int *a, mp_digit *d) |
|
6422 { |
|
6423 int res, p; |
|
6424 mp_int tmp; |
|
6425 |
|
6426 if ((res = mp_init(&tmp)) != MP_OKAY) { |
|
6427 return res; |
|
6428 } |
|
6429 |
|
6430 p = mp_count_bits(a); |
|
6431 if ((res = mp_2expt(&tmp, p)) != MP_OKAY) { |
|
6432 mp_clear(&tmp); |
|
6433 return res; |
|
6434 } |
|
6435 |
|
6436 if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) { |
|
6437 mp_clear(&tmp); |
|
6438 return res; |
|
6439 } |
|
6440 |
|
6441 *d = tmp.dp[0]; |
|
6442 mp_clear(&tmp); |
|
6443 return MP_OKAY; |
|
6444 } |
143
|
6445 #endif |
3
|
6446 |
|
6447 /* End: bn_mp_reduce_2k_setup.c */ |
|
6448 |
|
6449 /* Start: bn_mp_reduce_is_2k.c */ |
143
|
6450 #include <ltc_tommath.h> |
|
6451 #ifdef BN_MP_REDUCE_IS_2K_C |
|
6452 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6453 * |
|
6454 * LibTomMath is a library that provides multiple-precision |
|
6455 * integer arithmetic as well as number theoretic functionality. |
|
6456 * |
|
6457 * The library was designed directly after the MPI library by |
|
6458 * Michael Fromberger but has been written from scratch with |
|
6459 * additional optimizations in place. |
|
6460 * |
|
6461 * The library is free for all purposes without any express |
|
6462 * guarantee it works. |
|
6463 * |
|
6464 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6465 */ |
3
|
6466 |
|
6467 /* determines if mp_reduce_2k can be used */ |
|
6468 int mp_reduce_is_2k(mp_int *a) |
|
6469 { |
143
|
6470 int ix, iy, iw; |
|
6471 mp_digit iz; |
3
|
6472 |
|
6473 if (a->used == 0) { |
|
6474 return 0; |
|
6475 } else if (a->used == 1) { |
|
6476 return 1; |
|
6477 } else if (a->used > 1) { |
|
6478 iy = mp_count_bits(a); |
|
6479 iz = 1; |
|
6480 iw = 1; |
|
6481 |
|
6482 /* Test every bit from the second digit up, must be 1 */ |
|
6483 for (ix = DIGIT_BIT; ix < iy; ix++) { |
|
6484 if ((a->dp[iw] & iz) == 0) { |
|
6485 return 0; |
|
6486 } |
|
6487 iz <<= 1; |
143
|
6488 if (iz > (mp_digit)MP_MASK) { |
3
|
6489 ++iw; |
|
6490 iz = 1; |
|
6491 } |
|
6492 } |
|
6493 } |
|
6494 return 1; |
|
6495 } |
|
6496 |
143
|
6497 #endif |
3
|
6498 |
|
6499 /* End: bn_mp_reduce_is_2k.c */ |
|
6500 |
|
6501 /* Start: bn_mp_reduce_setup.c */ |
143
|
6502 #include <ltc_tommath.h> |
|
6503 #ifdef BN_MP_REDUCE_SETUP_C |
|
6504 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6505 * |
|
6506 * LibTomMath is a library that provides multiple-precision |
|
6507 * integer arithmetic as well as number theoretic functionality. |
|
6508 * |
|
6509 * The library was designed directly after the MPI library by |
|
6510 * Michael Fromberger but has been written from scratch with |
|
6511 * additional optimizations in place. |
|
6512 * |
|
6513 * The library is free for all purposes without any express |
|
6514 * guarantee it works. |
|
6515 * |
|
6516 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6517 */ |
3
|
6518 |
|
6519 /* pre-calculate the value required for Barrett reduction |
|
6520 * For a given modulus "b" it calulates the value required in "a" |
|
6521 */ |
143
|
6522 int mp_reduce_setup (mp_int * a, mp_int * b) |
3
|
6523 { |
|
6524 int res; |
|
6525 |
|
6526 if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { |
|
6527 return res; |
|
6528 } |
|
6529 return mp_div (a, b, a, NULL); |
|
6530 } |
143
|
6531 #endif |
3
|
6532 |
|
6533 /* End: bn_mp_reduce_setup.c */ |
|
6534 |
|
6535 /* Start: bn_mp_rshd.c */ |
143
|
6536 #include <ltc_tommath.h> |
|
6537 #ifdef BN_MP_RSHD_C |
|
6538 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6539 * |
|
6540 * LibTomMath is a library that provides multiple-precision |
|
6541 * integer arithmetic as well as number theoretic functionality. |
|
6542 * |
|
6543 * The library was designed directly after the MPI library by |
|
6544 * Michael Fromberger but has been written from scratch with |
|
6545 * additional optimizations in place. |
|
6546 * |
|
6547 * The library is free for all purposes without any express |
|
6548 * guarantee it works. |
|
6549 * |
|
6550 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6551 */ |
3
|
6552 |
|
6553 /* shift right a certain amount of digits */ |
|
6554 void mp_rshd (mp_int * a, int b) |
|
6555 { |
|
6556 int x; |
|
6557 |
|
6558 /* if b <= 0 then ignore it */ |
|
6559 if (b <= 0) { |
|
6560 return; |
|
6561 } |
|
6562 |
|
6563 /* if b > used then simply zero it and return */ |
|
6564 if (a->used <= b) { |
|
6565 mp_zero (a); |
|
6566 return; |
|
6567 } |
|
6568 |
|
6569 { |
|
6570 register mp_digit *bottom, *top; |
|
6571 |
|
6572 /* shift the digits down */ |
|
6573 |
|
6574 /* bottom */ |
|
6575 bottom = a->dp; |
|
6576 |
|
6577 /* top [offset into digits] */ |
|
6578 top = a->dp + b; |
|
6579 |
|
6580 /* this is implemented as a sliding window where |
|
6581 * the window is b-digits long and digits from |
|
6582 * the top of the window are copied to the bottom |
|
6583 * |
|
6584 * e.g. |
|
6585 |
|
6586 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> |
|
6587 /\ | ----> |
|
6588 \-------------------/ ----> |
|
6589 */ |
|
6590 for (x = 0; x < (a->used - b); x++) { |
|
6591 *bottom++ = *top++; |
|
6592 } |
|
6593 |
|
6594 /* zero the top digits */ |
|
6595 for (; x < a->used; x++) { |
|
6596 *bottom++ = 0; |
|
6597 } |
|
6598 } |
|
6599 |
|
6600 /* remove excess digits */ |
|
6601 a->used -= b; |
|
6602 } |
143
|
6603 #endif |
3
|
6604 |
|
6605 /* End: bn_mp_rshd.c */ |
|
6606 |
|
6607 /* Start: bn_mp_set.c */ |
143
|
6608 #include <ltc_tommath.h> |
|
6609 #ifdef BN_MP_SET_C |
|
6610 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6611 * |
|
6612 * LibTomMath is a library that provides multiple-precision |
|
6613 * integer arithmetic as well as number theoretic functionality. |
|
6614 * |
|
6615 * The library was designed directly after the MPI library by |
|
6616 * Michael Fromberger but has been written from scratch with |
|
6617 * additional optimizations in place. |
|
6618 * |
|
6619 * The library is free for all purposes without any express |
|
6620 * guarantee it works. |
|
6621 * |
|
6622 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6623 */ |
3
|
6624 |
|
6625 /* set to a digit */ |
|
6626 void mp_set (mp_int * a, mp_digit b) |
|
6627 { |
|
6628 mp_zero (a); |
|
6629 a->dp[0] = b & MP_MASK; |
|
6630 a->used = (a->dp[0] != 0) ? 1 : 0; |
|
6631 } |
143
|
6632 #endif |
3
|
6633 |
|
6634 /* End: bn_mp_set.c */ |
|
6635 |
|
6636 /* Start: bn_mp_set_int.c */ |
143
|
6637 #include <ltc_tommath.h> |
|
6638 #ifdef BN_MP_SET_INT_C |
|
6639 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6640 * |
|
6641 * LibTomMath is a library that provides multiple-precision |
|
6642 * integer arithmetic as well as number theoretic functionality. |
|
6643 * |
|
6644 * The library was designed directly after the MPI library by |
|
6645 * Michael Fromberger but has been written from scratch with |
|
6646 * additional optimizations in place. |
|
6647 * |
|
6648 * The library is free for all purposes without any express |
|
6649 * guarantee it works. |
|
6650 * |
|
6651 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6652 */ |
3
|
6653 |
|
6654 /* set a 32-bit const */ |
|
6655 int mp_set_int (mp_int * a, unsigned long b) |
|
6656 { |
|
6657 int x, res; |
|
6658 |
|
6659 mp_zero (a); |
|
6660 |
|
6661 /* set four bits at a time */ |
|
6662 for (x = 0; x < 8; x++) { |
|
6663 /* shift the number up four bits */ |
|
6664 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { |
|
6665 return res; |
|
6666 } |
|
6667 |
|
6668 /* OR in the top four bits of the source */ |
|
6669 a->dp[0] |= (b >> 28) & 15; |
|
6670 |
|
6671 /* shift the source up to the next four bits */ |
|
6672 b <<= 4; |
|
6673 |
|
6674 /* ensure that digits are not clamped off */ |
|
6675 a->used += 1; |
|
6676 } |
|
6677 mp_clamp (a); |
|
6678 return MP_OKAY; |
|
6679 } |
143
|
6680 #endif |
3
|
6681 |
|
6682 /* End: bn_mp_set_int.c */ |
|
6683 |
|
6684 /* Start: bn_mp_shrink.c */ |
143
|
6685 #include <ltc_tommath.h> |
|
6686 #ifdef BN_MP_SHRINK_C |
|
6687 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6688 * |
|
6689 * LibTomMath is a library that provides multiple-precision |
|
6690 * integer arithmetic as well as number theoretic functionality. |
|
6691 * |
|
6692 * The library was designed directly after the MPI library by |
|
6693 * Michael Fromberger but has been written from scratch with |
|
6694 * additional optimizations in place. |
|
6695 * |
|
6696 * The library is free for all purposes without any express |
|
6697 * guarantee it works. |
|
6698 * |
|
6699 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6700 */ |
3
|
6701 |
|
6702 /* shrink a bignum */ |
|
6703 int mp_shrink (mp_int * a) |
|
6704 { |
|
6705 mp_digit *tmp; |
|
6706 if (a->alloc != a->used && a->used > 0) { |
|
6707 if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * a->used)) == NULL) { |
|
6708 return MP_MEM; |
|
6709 } |
|
6710 a->dp = tmp; |
|
6711 a->alloc = a->used; |
|
6712 } |
|
6713 return MP_OKAY; |
|
6714 } |
143
|
6715 #endif |
3
|
6716 |
|
6717 /* End: bn_mp_shrink.c */ |
|
6718 |
|
6719 /* Start: bn_mp_signed_bin_size.c */ |
143
|
6720 #include <ltc_tommath.h> |
|
6721 #ifdef BN_MP_SIGNED_BIN_SIZE_C |
|
6722 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6723 * |
|
6724 * LibTomMath is a library that provides multiple-precision |
|
6725 * integer arithmetic as well as number theoretic functionality. |
|
6726 * |
|
6727 * The library was designed directly after the MPI library by |
|
6728 * Michael Fromberger but has been written from scratch with |
|
6729 * additional optimizations in place. |
|
6730 * |
|
6731 * The library is free for all purposes without any express |
|
6732 * guarantee it works. |
|
6733 * |
|
6734 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6735 */ |
3
|
6736 |
|
6737 /* get the size for an signed equivalent */ |
|
6738 int mp_signed_bin_size (mp_int * a) |
|
6739 { |
|
6740 return 1 + mp_unsigned_bin_size (a); |
|
6741 } |
143
|
6742 #endif |
3
|
6743 |
|
6744 /* End: bn_mp_signed_bin_size.c */ |
|
6745 |
|
6746 /* Start: bn_mp_sqr.c */ |
143
|
6747 #include <ltc_tommath.h> |
|
6748 #ifdef BN_MP_SQR_C |
|
6749 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6750 * |
|
6751 * LibTomMath is a library that provides multiple-precision |
|
6752 * integer arithmetic as well as number theoretic functionality. |
|
6753 * |
|
6754 * The library was designed directly after the MPI library by |
|
6755 * Michael Fromberger but has been written from scratch with |
|
6756 * additional optimizations in place. |
|
6757 * |
|
6758 * The library is free for all purposes without any express |
|
6759 * guarantee it works. |
|
6760 * |
|
6761 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6762 */ |
3
|
6763 |
|
6764 /* computes b = a*a */ |
|
6765 int |
|
6766 mp_sqr (mp_int * a, mp_int * b) |
|
6767 { |
|
6768 int res; |
|
6769 |
143
|
6770 #ifdef BN_MP_TOOM_SQR_C |
3
|
6771 /* use Toom-Cook? */ |
|
6772 if (a->used >= TOOM_SQR_CUTOFF) { |
|
6773 res = mp_toom_sqr(a, b); |
|
6774 /* Karatsuba? */ |
143
|
6775 } else |
|
6776 #endif |
|
6777 #ifdef BN_MP_KARATSUBA_SQR_C |
|
6778 if (a->used >= KARATSUBA_SQR_CUTOFF) { |
3
|
6779 res = mp_karatsuba_sqr (a, b); |
143
|
6780 } else |
|
6781 #endif |
|
6782 { |
|
6783 #ifdef BN_FAST_S_MP_SQR_C |
3
|
6784 /* can we use the fast comba multiplier? */ |
|
6785 if ((a->used * 2 + 1) < MP_WARRAY && |
|
6786 a->used < |
|
6787 (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) { |
|
6788 res = fast_s_mp_sqr (a, b); |
143
|
6789 } else |
|
6790 #endif |
|
6791 #ifdef BN_S_MP_SQR_C |
3
|
6792 res = s_mp_sqr (a, b); |
143
|
6793 #else |
|
6794 res = MP_VAL; |
|
6795 #endif |
3
|
6796 } |
|
6797 b->sign = MP_ZPOS; |
|
6798 return res; |
|
6799 } |
143
|
6800 #endif |
3
|
6801 |
|
6802 /* End: bn_mp_sqr.c */ |
|
6803 |
|
6804 /* Start: bn_mp_sqrmod.c */ |
143
|
6805 #include <ltc_tommath.h> |
|
6806 #ifdef BN_MP_SQRMOD_C |
|
6807 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6808 * |
|
6809 * LibTomMath is a library that provides multiple-precision |
|
6810 * integer arithmetic as well as number theoretic functionality. |
|
6811 * |
|
6812 * The library was designed directly after the MPI library by |
|
6813 * Michael Fromberger but has been written from scratch with |
|
6814 * additional optimizations in place. |
|
6815 * |
|
6816 * The library is free for all purposes without any express |
|
6817 * guarantee it works. |
|
6818 * |
|
6819 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6820 */ |
3
|
6821 |
|
6822 /* c = a * a (mod b) */ |
|
6823 int |
|
6824 mp_sqrmod (mp_int * a, mp_int * b, mp_int * c) |
|
6825 { |
|
6826 int res; |
|
6827 mp_int t; |
|
6828 |
|
6829 if ((res = mp_init (&t)) != MP_OKAY) { |
|
6830 return res; |
|
6831 } |
|
6832 |
|
6833 if ((res = mp_sqr (a, &t)) != MP_OKAY) { |
|
6834 mp_clear (&t); |
|
6835 return res; |
|
6836 } |
|
6837 res = mp_mod (&t, b, c); |
|
6838 mp_clear (&t); |
|
6839 return res; |
|
6840 } |
143
|
6841 #endif |
3
|
6842 |
|
6843 /* End: bn_mp_sqrmod.c */ |
|
6844 |
|
6845 /* Start: bn_mp_sqrt.c */ |
143
|
6846 #include <ltc_tommath.h> |
|
6847 #ifdef BN_MP_SQRT_C |
|
6848 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6849 * |
|
6850 * LibTomMath is a library that provides multiple-precision |
|
6851 * integer arithmetic as well as number theoretic functionality. |
|
6852 * |
|
6853 * The library was designed directly after the MPI library by |
|
6854 * Michael Fromberger but has been written from scratch with |
|
6855 * additional optimizations in place. |
|
6856 * |
|
6857 * The library is free for all purposes without any express |
|
6858 * guarantee it works. |
|
6859 * |
|
6860 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6861 */ |
3
|
6862 |
|
6863 /* this function is less generic than mp_n_root, simpler and faster */ |
|
6864 int mp_sqrt(mp_int *arg, mp_int *ret) |
|
6865 { |
|
6866 int res; |
|
6867 mp_int t1,t2; |
|
6868 |
|
6869 /* must be positive */ |
|
6870 if (arg->sign == MP_NEG) { |
|
6871 return MP_VAL; |
|
6872 } |
|
6873 |
|
6874 /* easy out */ |
|
6875 if (mp_iszero(arg) == MP_YES) { |
|
6876 mp_zero(ret); |
|
6877 return MP_OKAY; |
|
6878 } |
|
6879 |
|
6880 if ((res = mp_init_copy(&t1, arg)) != MP_OKAY) { |
|
6881 return res; |
|
6882 } |
|
6883 |
|
6884 if ((res = mp_init(&t2)) != MP_OKAY) { |
|
6885 goto E2; |
|
6886 } |
|
6887 |
|
6888 /* First approx. (not very bad for large arg) */ |
|
6889 mp_rshd (&t1,t1.used/2); |
|
6890 |
|
6891 /* t1 > 0 */ |
|
6892 if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { |
|
6893 goto E1; |
|
6894 } |
|
6895 if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) { |
|
6896 goto E1; |
|
6897 } |
|
6898 if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) { |
|
6899 goto E1; |
|
6900 } |
|
6901 /* And now t1 > sqrt(arg) */ |
|
6902 do { |
|
6903 if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { |
|
6904 goto E1; |
|
6905 } |
|
6906 if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) { |
|
6907 goto E1; |
|
6908 } |
|
6909 if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) { |
|
6910 goto E1; |
|
6911 } |
|
6912 /* t1 >= sqrt(arg) >= t2 at this point */ |
|
6913 } while (mp_cmp_mag(&t1,&t2) == MP_GT); |
|
6914 |
|
6915 mp_exch(&t1,ret); |
|
6916 |
|
6917 E1: mp_clear(&t2); |
|
6918 E2: mp_clear(&t1); |
|
6919 return res; |
|
6920 } |
|
6921 |
143
|
6922 #endif |
3
|
6923 |
|
6924 /* End: bn_mp_sqrt.c */ |
|
6925 |
|
6926 /* Start: bn_mp_sub.c */ |
143
|
6927 #include <ltc_tommath.h> |
|
6928 #ifdef BN_MP_SUB_C |
|
6929 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6930 * |
|
6931 * LibTomMath is a library that provides multiple-precision |
|
6932 * integer arithmetic as well as number theoretic functionality. |
|
6933 * |
|
6934 * The library was designed directly after the MPI library by |
|
6935 * Michael Fromberger but has been written from scratch with |
|
6936 * additional optimizations in place. |
|
6937 * |
|
6938 * The library is free for all purposes without any express |
|
6939 * guarantee it works. |
|
6940 * |
|
6941 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
6942 */ |
3
|
6943 |
|
6944 /* high level subtraction (handles signs) */ |
|
6945 int |
|
6946 mp_sub (mp_int * a, mp_int * b, mp_int * c) |
|
6947 { |
|
6948 int sa, sb, res; |
|
6949 |
|
6950 sa = a->sign; |
|
6951 sb = b->sign; |
|
6952 |
|
6953 if (sa != sb) { |
|
6954 /* subtract a negative from a positive, OR */ |
|
6955 /* subtract a positive from a negative. */ |
|
6956 /* In either case, ADD their magnitudes, */ |
|
6957 /* and use the sign of the first number. */ |
|
6958 c->sign = sa; |
|
6959 res = s_mp_add (a, b, c); |
|
6960 } else { |
|
6961 /* subtract a positive from a positive, OR */ |
|
6962 /* subtract a negative from a negative. */ |
|
6963 /* First, take the difference between their */ |
|
6964 /* magnitudes, then... */ |
|
6965 if (mp_cmp_mag (a, b) != MP_LT) { |
|
6966 /* Copy the sign from the first */ |
|
6967 c->sign = sa; |
|
6968 /* The first has a larger or equal magnitude */ |
|
6969 res = s_mp_sub (a, b, c); |
|
6970 } else { |
|
6971 /* The result has the *opposite* sign from */ |
|
6972 /* the first number. */ |
|
6973 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; |
|
6974 /* The second has a larger magnitude */ |
|
6975 res = s_mp_sub (b, a, c); |
|
6976 } |
|
6977 } |
|
6978 return res; |
|
6979 } |
|
6980 |
143
|
6981 #endif |
3
|
6982 |
|
6983 /* End: bn_mp_sub.c */ |
|
6984 |
|
6985 /* Start: bn_mp_sub_d.c */ |
143
|
6986 #include <ltc_tommath.h> |
|
6987 #ifdef BN_MP_SUB_D_C |
|
6988 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
6989 * |
|
6990 * LibTomMath is a library that provides multiple-precision |
|
6991 * integer arithmetic as well as number theoretic functionality. |
|
6992 * |
|
6993 * The library was designed directly after the MPI library by |
|
6994 * Michael Fromberger but has been written from scratch with |
|
6995 * additional optimizations in place. |
|
6996 * |
|
6997 * The library is free for all purposes without any express |
|
6998 * guarantee it works. |
|
6999 * |
|
7000 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7001 */ |
3
|
7002 |
|
7003 /* single digit subtraction */ |
|
7004 int |
|
7005 mp_sub_d (mp_int * a, mp_digit b, mp_int * c) |
|
7006 { |
|
7007 mp_digit *tmpa, *tmpc, mu; |
|
7008 int res, ix, oldused; |
|
7009 |
|
7010 /* grow c as required */ |
|
7011 if (c->alloc < a->used + 1) { |
|
7012 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { |
|
7013 return res; |
|
7014 } |
|
7015 } |
|
7016 |
|
7017 /* if a is negative just do an unsigned |
|
7018 * addition [with fudged signs] |
|
7019 */ |
|
7020 if (a->sign == MP_NEG) { |
|
7021 a->sign = MP_ZPOS; |
|
7022 res = mp_add_d(a, b, c); |
|
7023 a->sign = c->sign = MP_NEG; |
|
7024 return res; |
|
7025 } |
|
7026 |
|
7027 /* setup regs */ |
|
7028 oldused = c->used; |
|
7029 tmpa = a->dp; |
|
7030 tmpc = c->dp; |
|
7031 |
|
7032 /* if a <= b simply fix the single digit */ |
|
7033 if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) { |
|
7034 if (a->used == 1) { |
|
7035 *tmpc++ = b - *tmpa; |
|
7036 } else { |
|
7037 *tmpc++ = b; |
|
7038 } |
|
7039 ix = 1; |
|
7040 |
|
7041 /* negative/1digit */ |
|
7042 c->sign = MP_NEG; |
|
7043 c->used = 1; |
|
7044 } else { |
|
7045 /* positive/size */ |
|
7046 c->sign = MP_ZPOS; |
|
7047 c->used = a->used; |
|
7048 |
|
7049 /* subtract first digit */ |
|
7050 *tmpc = *tmpa++ - b; |
|
7051 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); |
|
7052 *tmpc++ &= MP_MASK; |
|
7053 |
|
7054 /* handle rest of the digits */ |
|
7055 for (ix = 1; ix < a->used; ix++) { |
|
7056 *tmpc = *tmpa++ - mu; |
|
7057 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); |
|
7058 *tmpc++ &= MP_MASK; |
|
7059 } |
|
7060 } |
|
7061 |
|
7062 /* zero excess digits */ |
|
7063 while (ix++ < oldused) { |
|
7064 *tmpc++ = 0; |
|
7065 } |
|
7066 mp_clamp(c); |
|
7067 return MP_OKAY; |
|
7068 } |
|
7069 |
143
|
7070 #endif |
3
|
7071 |
|
7072 /* End: bn_mp_sub_d.c */ |
|
7073 |
|
7074 /* Start: bn_mp_submod.c */ |
143
|
7075 #include <ltc_tommath.h> |
|
7076 #ifdef BN_MP_SUBMOD_C |
|
7077 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7078 * |
|
7079 * LibTomMath is a library that provides multiple-precision |
|
7080 * integer arithmetic as well as number theoretic functionality. |
|
7081 * |
|
7082 * The library was designed directly after the MPI library by |
|
7083 * Michael Fromberger but has been written from scratch with |
|
7084 * additional optimizations in place. |
|
7085 * |
|
7086 * The library is free for all purposes without any express |
|
7087 * guarantee it works. |
|
7088 * |
|
7089 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7090 */ |
3
|
7091 |
|
7092 /* d = a - b (mod c) */ |
|
7093 int |
|
7094 mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
|
7095 { |
|
7096 int res; |
|
7097 mp_int t; |
|
7098 |
|
7099 |
|
7100 if ((res = mp_init (&t)) != MP_OKAY) { |
|
7101 return res; |
|
7102 } |
|
7103 |
|
7104 if ((res = mp_sub (a, b, &t)) != MP_OKAY) { |
|
7105 mp_clear (&t); |
|
7106 return res; |
|
7107 } |
|
7108 res = mp_mod (&t, c, d); |
|
7109 mp_clear (&t); |
|
7110 return res; |
|
7111 } |
143
|
7112 #endif |
3
|
7113 |
|
7114 /* End: bn_mp_submod.c */ |
|
7115 |
|
7116 /* Start: bn_mp_to_signed_bin.c */ |
143
|
7117 #include <ltc_tommath.h> |
|
7118 #ifdef BN_MP_TO_SIGNED_BIN_C |
|
7119 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7120 * |
|
7121 * LibTomMath is a library that provides multiple-precision |
|
7122 * integer arithmetic as well as number theoretic functionality. |
|
7123 * |
|
7124 * The library was designed directly after the MPI library by |
|
7125 * Michael Fromberger but has been written from scratch with |
|
7126 * additional optimizations in place. |
|
7127 * |
|
7128 * The library is free for all purposes without any express |
|
7129 * guarantee it works. |
|
7130 * |
|
7131 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7132 */ |
3
|
7133 |
|
7134 /* store in signed [big endian] format */ |
|
7135 int |
|
7136 mp_to_signed_bin (mp_int * a, unsigned char *b) |
|
7137 { |
|
7138 int res; |
|
7139 |
|
7140 if ((res = mp_to_unsigned_bin (a, b + 1)) != MP_OKAY) { |
|
7141 return res; |
|
7142 } |
|
7143 b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1); |
|
7144 return MP_OKAY; |
|
7145 } |
143
|
7146 #endif |
3
|
7147 |
|
7148 /* End: bn_mp_to_signed_bin.c */ |
|
7149 |
|
7150 /* Start: bn_mp_to_unsigned_bin.c */ |
143
|
7151 #include <ltc_tommath.h> |
|
7152 #ifdef BN_MP_TO_UNSIGNED_BIN_C |
|
7153 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7154 * |
|
7155 * LibTomMath is a library that provides multiple-precision |
|
7156 * integer arithmetic as well as number theoretic functionality. |
|
7157 * |
|
7158 * The library was designed directly after the MPI library by |
|
7159 * Michael Fromberger but has been written from scratch with |
|
7160 * additional optimizations in place. |
|
7161 * |
|
7162 * The library is free for all purposes without any express |
|
7163 * guarantee it works. |
|
7164 * |
|
7165 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7166 */ |
3
|
7167 |
|
7168 /* store in unsigned [big endian] format */ |
|
7169 int |
|
7170 mp_to_unsigned_bin (mp_int * a, unsigned char *b) |
|
7171 { |
|
7172 int x, res; |
|
7173 mp_int t; |
|
7174 |
|
7175 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
7176 return res; |
|
7177 } |
|
7178 |
|
7179 x = 0; |
|
7180 while (mp_iszero (&t) == 0) { |
|
7181 #ifndef MP_8BIT |
|
7182 b[x++] = (unsigned char) (t.dp[0] & 255); |
|
7183 #else |
|
7184 b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7)); |
|
7185 #endif |
|
7186 if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) { |
|
7187 mp_clear (&t); |
|
7188 return res; |
|
7189 } |
|
7190 } |
|
7191 bn_reverse (b, x); |
|
7192 mp_clear (&t); |
|
7193 return MP_OKAY; |
|
7194 } |
143
|
7195 #endif |
3
|
7196 |
|
7197 /* End: bn_mp_to_unsigned_bin.c */ |
|
7198 |
|
7199 /* Start: bn_mp_toom_mul.c */ |
143
|
7200 #include <ltc_tommath.h> |
|
7201 #ifdef BN_MP_TOOM_MUL_C |
|
7202 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7203 * |
|
7204 * LibTomMath is a library that provides multiple-precision |
|
7205 * integer arithmetic as well as number theoretic functionality. |
|
7206 * |
|
7207 * The library was designed directly after the MPI library by |
|
7208 * Michael Fromberger but has been written from scratch with |
|
7209 * additional optimizations in place. |
|
7210 * |
|
7211 * The library is free for all purposes without any express |
|
7212 * guarantee it works. |
|
7213 * |
|
7214 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7215 */ |
|
7216 |
|
7217 /* multiplication using the Toom-Cook 3-way algorithm |
|
7218 * |
|
7219 * Much more complicated than Karatsuba but has a lower asymptotic running time of |
|
7220 * O(N**1.464). This algorithm is only particularly useful on VERY large |
|
7221 * inputs (we're talking 1000s of digits here...). |
|
7222 */ |
3
|
7223 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) |
|
7224 { |
|
7225 mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2; |
|
7226 int res, B; |
|
7227 |
|
7228 /* init temps */ |
|
7229 if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, |
|
7230 &a0, &a1, &a2, &b0, &b1, |
|
7231 &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) { |
|
7232 return res; |
|
7233 } |
|
7234 |
|
7235 /* B */ |
|
7236 B = MIN(a->used, b->used) / 3; |
|
7237 |
|
7238 /* a = a2 * B**2 + a1 * B + a0 */ |
|
7239 if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) { |
|
7240 goto ERR; |
|
7241 } |
|
7242 |
|
7243 if ((res = mp_copy(a, &a1)) != MP_OKAY) { |
|
7244 goto ERR; |
|
7245 } |
|
7246 mp_rshd(&a1, B); |
|
7247 mp_mod_2d(&a1, DIGIT_BIT * B, &a1); |
|
7248 |
|
7249 if ((res = mp_copy(a, &a2)) != MP_OKAY) { |
|
7250 goto ERR; |
|
7251 } |
|
7252 mp_rshd(&a2, B*2); |
|
7253 |
|
7254 /* b = b2 * B**2 + b1 * B + b0 */ |
|
7255 if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) { |
|
7256 goto ERR; |
|
7257 } |
|
7258 |
|
7259 if ((res = mp_copy(b, &b1)) != MP_OKAY) { |
|
7260 goto ERR; |
|
7261 } |
|
7262 mp_rshd(&b1, B); |
|
7263 mp_mod_2d(&b1, DIGIT_BIT * B, &b1); |
|
7264 |
|
7265 if ((res = mp_copy(b, &b2)) != MP_OKAY) { |
|
7266 goto ERR; |
|
7267 } |
|
7268 mp_rshd(&b2, B*2); |
|
7269 |
|
7270 /* w0 = a0*b0 */ |
|
7271 if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) { |
|
7272 goto ERR; |
|
7273 } |
|
7274 |
|
7275 /* w4 = a2 * b2 */ |
|
7276 if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) { |
|
7277 goto ERR; |
|
7278 } |
|
7279 |
|
7280 /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */ |
|
7281 if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) { |
|
7282 goto ERR; |
|
7283 } |
|
7284 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) { |
|
7285 goto ERR; |
|
7286 } |
|
7287 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) { |
|
7288 goto ERR; |
|
7289 } |
|
7290 if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) { |
|
7291 goto ERR; |
|
7292 } |
|
7293 |
|
7294 if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) { |
|
7295 goto ERR; |
|
7296 } |
|
7297 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) { |
|
7298 goto ERR; |
|
7299 } |
|
7300 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) { |
|
7301 goto ERR; |
|
7302 } |
|
7303 if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) { |
|
7304 goto ERR; |
|
7305 } |
|
7306 |
|
7307 if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) { |
|
7308 goto ERR; |
|
7309 } |
|
7310 |
|
7311 /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */ |
|
7312 if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) { |
|
7313 goto ERR; |
|
7314 } |
|
7315 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) { |
|
7316 goto ERR; |
|
7317 } |
|
7318 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) { |
|
7319 goto ERR; |
|
7320 } |
|
7321 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) { |
|
7322 goto ERR; |
|
7323 } |
|
7324 |
|
7325 if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) { |
|
7326 goto ERR; |
|
7327 } |
|
7328 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) { |
|
7329 goto ERR; |
|
7330 } |
|
7331 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) { |
|
7332 goto ERR; |
|
7333 } |
|
7334 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) { |
|
7335 goto ERR; |
|
7336 } |
|
7337 |
|
7338 if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) { |
|
7339 goto ERR; |
|
7340 } |
|
7341 |
|
7342 |
|
7343 /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */ |
|
7344 if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) { |
|
7345 goto ERR; |
|
7346 } |
|
7347 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) { |
|
7348 goto ERR; |
|
7349 } |
|
7350 if ((res = mp_add(&b2, &b1, &tmp2)) != MP_OKAY) { |
|
7351 goto ERR; |
|
7352 } |
|
7353 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) { |
|
7354 goto ERR; |
|
7355 } |
|
7356 if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) { |
|
7357 goto ERR; |
|
7358 } |
|
7359 |
|
7360 /* now solve the matrix |
|
7361 |
|
7362 0 0 0 0 1 |
|
7363 1 2 4 8 16 |
|
7364 1 1 1 1 1 |
|
7365 16 8 4 2 1 |
|
7366 1 0 0 0 0 |
|
7367 |
|
7368 using 12 subtractions, 4 shifts, |
|
7369 2 small divisions and 1 small multiplication |
|
7370 */ |
|
7371 |
|
7372 /* r1 - r4 */ |
|
7373 if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { |
|
7374 goto ERR; |
|
7375 } |
|
7376 /* r3 - r0 */ |
|
7377 if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) { |
|
7378 goto ERR; |
|
7379 } |
|
7380 /* r1/2 */ |
|
7381 if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) { |
|
7382 goto ERR; |
|
7383 } |
|
7384 /* r3/2 */ |
|
7385 if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) { |
|
7386 goto ERR; |
|
7387 } |
|
7388 /* r2 - r0 - r4 */ |
|
7389 if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) { |
|
7390 goto ERR; |
|
7391 } |
|
7392 if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) { |
|
7393 goto ERR; |
|
7394 } |
|
7395 /* r1 - r2 */ |
|
7396 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { |
|
7397 goto ERR; |
|
7398 } |
|
7399 /* r3 - r2 */ |
|
7400 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { |
|
7401 goto ERR; |
|
7402 } |
|
7403 /* r1 - 8r0 */ |
|
7404 if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) { |
|
7405 goto ERR; |
|
7406 } |
|
7407 if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) { |
|
7408 goto ERR; |
|
7409 } |
|
7410 /* r3 - 8r4 */ |
|
7411 if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) { |
|
7412 goto ERR; |
|
7413 } |
|
7414 if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) { |
|
7415 goto ERR; |
|
7416 } |
|
7417 /* 3r2 - r1 - r3 */ |
|
7418 if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) { |
|
7419 goto ERR; |
|
7420 } |
|
7421 if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) { |
|
7422 goto ERR; |
|
7423 } |
|
7424 if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) { |
|
7425 goto ERR; |
|
7426 } |
|
7427 /* r1 - r2 */ |
|
7428 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { |
|
7429 goto ERR; |
|
7430 } |
|
7431 /* r3 - r2 */ |
|
7432 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { |
|
7433 goto ERR; |
|
7434 } |
|
7435 /* r1/3 */ |
|
7436 if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) { |
|
7437 goto ERR; |
|
7438 } |
|
7439 /* r3/3 */ |
|
7440 if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { |
|
7441 goto ERR; |
|
7442 } |
|
7443 |
|
7444 /* at this point shift W[n] by B*n */ |
|
7445 if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { |
|
7446 goto ERR; |
|
7447 } |
|
7448 if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) { |
|
7449 goto ERR; |
|
7450 } |
|
7451 if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) { |
|
7452 goto ERR; |
|
7453 } |
|
7454 if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { |
|
7455 goto ERR; |
|
7456 } |
|
7457 |
|
7458 if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) { |
|
7459 goto ERR; |
|
7460 } |
|
7461 if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) { |
|
7462 goto ERR; |
|
7463 } |
|
7464 if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) { |
|
7465 goto ERR; |
|
7466 } |
|
7467 if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) { |
|
7468 goto ERR; |
|
7469 } |
|
7470 |
|
7471 ERR: |
|
7472 mp_clear_multi(&w0, &w1, &w2, &w3, &w4, |
|
7473 &a0, &a1, &a2, &b0, &b1, |
|
7474 &b2, &tmp1, &tmp2, NULL); |
|
7475 return res; |
|
7476 } |
|
7477 |
143
|
7478 #endif |
3
|
7479 |
|
7480 /* End: bn_mp_toom_mul.c */ |
|
7481 |
|
7482 /* Start: bn_mp_toom_sqr.c */ |
143
|
7483 #include <ltc_tommath.h> |
|
7484 #ifdef BN_MP_TOOM_SQR_C |
|
7485 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7486 * |
|
7487 * LibTomMath is a library that provides multiple-precision |
|
7488 * integer arithmetic as well as number theoretic functionality. |
|
7489 * |
|
7490 * The library was designed directly after the MPI library by |
|
7491 * Michael Fromberger but has been written from scratch with |
|
7492 * additional optimizations in place. |
|
7493 * |
|
7494 * The library is free for all purposes without any express |
|
7495 * guarantee it works. |
|
7496 * |
|
7497 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7498 */ |
3
|
7499 |
|
7500 /* squaring using Toom-Cook 3-way algorithm */ |
|
7501 int |
|
7502 mp_toom_sqr(mp_int *a, mp_int *b) |
|
7503 { |
|
7504 mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2; |
|
7505 int res, B; |
|
7506 |
|
7507 /* init temps */ |
|
7508 if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != MP_OKAY) { |
|
7509 return res; |
|
7510 } |
|
7511 |
|
7512 /* B */ |
|
7513 B = a->used / 3; |
|
7514 |
|
7515 /* a = a2 * B**2 + a1 * B + a0 */ |
|
7516 if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) { |
|
7517 goto ERR; |
|
7518 } |
|
7519 |
|
7520 if ((res = mp_copy(a, &a1)) != MP_OKAY) { |
|
7521 goto ERR; |
|
7522 } |
|
7523 mp_rshd(&a1, B); |
|
7524 mp_mod_2d(&a1, DIGIT_BIT * B, &a1); |
|
7525 |
|
7526 if ((res = mp_copy(a, &a2)) != MP_OKAY) { |
|
7527 goto ERR; |
|
7528 } |
|
7529 mp_rshd(&a2, B*2); |
|
7530 |
|
7531 /* w0 = a0*a0 */ |
|
7532 if ((res = mp_sqr(&a0, &w0)) != MP_OKAY) { |
|
7533 goto ERR; |
|
7534 } |
|
7535 |
|
7536 /* w4 = a2 * a2 */ |
|
7537 if ((res = mp_sqr(&a2, &w4)) != MP_OKAY) { |
|
7538 goto ERR; |
|
7539 } |
|
7540 |
|
7541 /* w1 = (a2 + 2(a1 + 2a0))**2 */ |
|
7542 if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) { |
|
7543 goto ERR; |
|
7544 } |
|
7545 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) { |
|
7546 goto ERR; |
|
7547 } |
|
7548 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) { |
|
7549 goto ERR; |
|
7550 } |
|
7551 if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) { |
|
7552 goto ERR; |
|
7553 } |
|
7554 |
|
7555 if ((res = mp_sqr(&tmp1, &w1)) != MP_OKAY) { |
|
7556 goto ERR; |
|
7557 } |
|
7558 |
|
7559 /* w3 = (a0 + 2(a1 + 2a2))**2 */ |
|
7560 if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) { |
|
7561 goto ERR; |
|
7562 } |
|
7563 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) { |
|
7564 goto ERR; |
|
7565 } |
|
7566 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) { |
|
7567 goto ERR; |
|
7568 } |
|
7569 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) { |
|
7570 goto ERR; |
|
7571 } |
|
7572 |
|
7573 if ((res = mp_sqr(&tmp1, &w3)) != MP_OKAY) { |
|
7574 goto ERR; |
|
7575 } |
|
7576 |
|
7577 |
|
7578 /* w2 = (a2 + a1 + a0)**2 */ |
|
7579 if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) { |
|
7580 goto ERR; |
|
7581 } |
|
7582 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) { |
|
7583 goto ERR; |
|
7584 } |
|
7585 if ((res = mp_sqr(&tmp1, &w2)) != MP_OKAY) { |
|
7586 goto ERR; |
|
7587 } |
|
7588 |
|
7589 /* now solve the matrix |
|
7590 |
|
7591 0 0 0 0 1 |
|
7592 1 2 4 8 16 |
|
7593 1 1 1 1 1 |
|
7594 16 8 4 2 1 |
|
7595 1 0 0 0 0 |
|
7596 |
|
7597 using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication. |
|
7598 */ |
|
7599 |
|
7600 /* r1 - r4 */ |
|
7601 if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { |
|
7602 goto ERR; |
|
7603 } |
|
7604 /* r3 - r0 */ |
|
7605 if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) { |
|
7606 goto ERR; |
|
7607 } |
|
7608 /* r1/2 */ |
|
7609 if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) { |
|
7610 goto ERR; |
|
7611 } |
|
7612 /* r3/2 */ |
|
7613 if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) { |
|
7614 goto ERR; |
|
7615 } |
|
7616 /* r2 - r0 - r4 */ |
|
7617 if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) { |
|
7618 goto ERR; |
|
7619 } |
|
7620 if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) { |
|
7621 goto ERR; |
|
7622 } |
|
7623 /* r1 - r2 */ |
|
7624 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { |
|
7625 goto ERR; |
|
7626 } |
|
7627 /* r3 - r2 */ |
|
7628 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { |
|
7629 goto ERR; |
|
7630 } |
|
7631 /* r1 - 8r0 */ |
|
7632 if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) { |
|
7633 goto ERR; |
|
7634 } |
|
7635 if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) { |
|
7636 goto ERR; |
|
7637 } |
|
7638 /* r3 - 8r4 */ |
|
7639 if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) { |
|
7640 goto ERR; |
|
7641 } |
|
7642 if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) { |
|
7643 goto ERR; |
|
7644 } |
|
7645 /* 3r2 - r1 - r3 */ |
|
7646 if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) { |
|
7647 goto ERR; |
|
7648 } |
|
7649 if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) { |
|
7650 goto ERR; |
|
7651 } |
|
7652 if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) { |
|
7653 goto ERR; |
|
7654 } |
|
7655 /* r1 - r2 */ |
|
7656 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { |
|
7657 goto ERR; |
|
7658 } |
|
7659 /* r3 - r2 */ |
|
7660 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { |
|
7661 goto ERR; |
|
7662 } |
|
7663 /* r1/3 */ |
|
7664 if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) { |
|
7665 goto ERR; |
|
7666 } |
|
7667 /* r3/3 */ |
|
7668 if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { |
|
7669 goto ERR; |
|
7670 } |
|
7671 |
|
7672 /* at this point shift W[n] by B*n */ |
|
7673 if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { |
|
7674 goto ERR; |
|
7675 } |
|
7676 if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) { |
|
7677 goto ERR; |
|
7678 } |
|
7679 if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) { |
|
7680 goto ERR; |
|
7681 } |
|
7682 if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { |
|
7683 goto ERR; |
|
7684 } |
|
7685 |
|
7686 if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) { |
|
7687 goto ERR; |
|
7688 } |
|
7689 if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) { |
|
7690 goto ERR; |
|
7691 } |
|
7692 if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) { |
|
7693 goto ERR; |
|
7694 } |
|
7695 if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) { |
|
7696 goto ERR; |
|
7697 } |
|
7698 |
|
7699 ERR: |
|
7700 mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL); |
|
7701 return res; |
|
7702 } |
|
7703 |
143
|
7704 #endif |
3
|
7705 |
|
7706 /* End: bn_mp_toom_sqr.c */ |
|
7707 |
|
7708 /* Start: bn_mp_toradix.c */ |
143
|
7709 #include <ltc_tommath.h> |
|
7710 #ifdef BN_MP_TORADIX_C |
|
7711 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7712 * |
|
7713 * LibTomMath is a library that provides multiple-precision |
|
7714 * integer arithmetic as well as number theoretic functionality. |
|
7715 * |
|
7716 * The library was designed directly after the MPI library by |
|
7717 * Michael Fromberger but has been written from scratch with |
|
7718 * additional optimizations in place. |
|
7719 * |
|
7720 * The library is free for all purposes without any express |
|
7721 * guarantee it works. |
|
7722 * |
|
7723 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7724 */ |
3
|
7725 |
|
7726 /* stores a bignum as a ASCII string in a given radix (2..64) */ |
|
7727 int mp_toradix (mp_int * a, char *str, int radix) |
|
7728 { |
|
7729 int res, digs; |
|
7730 mp_int t; |
|
7731 mp_digit d; |
|
7732 char *_s = str; |
|
7733 |
|
7734 /* check range of the radix */ |
|
7735 if (radix < 2 || radix > 64) { |
|
7736 return MP_VAL; |
|
7737 } |
|
7738 |
|
7739 /* quick out if its zero */ |
|
7740 if (mp_iszero(a) == 1) { |
|
7741 *str++ = '0'; |
|
7742 *str = '\0'; |
|
7743 return MP_OKAY; |
|
7744 } |
|
7745 |
|
7746 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
7747 return res; |
|
7748 } |
|
7749 |
|
7750 /* if it is negative output a - */ |
|
7751 if (t.sign == MP_NEG) { |
|
7752 ++_s; |
|
7753 *str++ = '-'; |
|
7754 t.sign = MP_ZPOS; |
|
7755 } |
|
7756 |
|
7757 digs = 0; |
|
7758 while (mp_iszero (&t) == 0) { |
|
7759 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { |
|
7760 mp_clear (&t); |
|
7761 return res; |
|
7762 } |
|
7763 *str++ = mp_s_rmap[d]; |
|
7764 ++digs; |
|
7765 } |
|
7766 |
|
7767 /* reverse the digits of the string. In this case _s points |
|
7768 * to the first digit [exluding the sign] of the number] |
|
7769 */ |
|
7770 bn_reverse ((unsigned char *)_s, digs); |
|
7771 |
|
7772 /* append a NULL so the string is properly terminated */ |
|
7773 *str = '\0'; |
|
7774 |
|
7775 mp_clear (&t); |
|
7776 return MP_OKAY; |
|
7777 } |
|
7778 |
143
|
7779 #endif |
3
|
7780 |
|
7781 /* End: bn_mp_toradix.c */ |
|
7782 |
|
7783 /* Start: bn_mp_toradix_n.c */ |
143
|
7784 #include <ltc_tommath.h> |
|
7785 #ifdef BN_MP_TORADIX_N_C |
|
7786 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7787 * |
|
7788 * LibTomMath is a library that provides multiple-precision |
|
7789 * integer arithmetic as well as number theoretic functionality. |
|
7790 * |
|
7791 * The library was designed directly after the MPI library by |
|
7792 * Michael Fromberger but has been written from scratch with |
|
7793 * additional optimizations in place. |
|
7794 * |
|
7795 * The library is free for all purposes without any express |
|
7796 * guarantee it works. |
|
7797 * |
|
7798 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7799 */ |
3
|
7800 |
|
7801 /* stores a bignum as a ASCII string in a given radix (2..64) |
|
7802 * |
|
7803 * Stores upto maxlen-1 chars and always a NULL byte |
|
7804 */ |
|
7805 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) |
|
7806 { |
|
7807 int res, digs; |
|
7808 mp_int t; |
|
7809 mp_digit d; |
|
7810 char *_s = str; |
|
7811 |
|
7812 /* check range of the maxlen, radix */ |
|
7813 if (maxlen < 3 || radix < 2 || radix > 64) { |
|
7814 return MP_VAL; |
|
7815 } |
|
7816 |
|
7817 /* quick out if its zero */ |
|
7818 if (mp_iszero(a) == 1) { |
|
7819 *str++ = '0'; |
|
7820 *str = '\0'; |
|
7821 return MP_OKAY; |
|
7822 } |
|
7823 |
|
7824 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
7825 return res; |
|
7826 } |
|
7827 |
|
7828 /* if it is negative output a - */ |
|
7829 if (t.sign == MP_NEG) { |
|
7830 /* we have to reverse our digits later... but not the - sign!! */ |
|
7831 ++_s; |
|
7832 |
|
7833 /* store the flag and mark the number as positive */ |
|
7834 *str++ = '-'; |
|
7835 t.sign = MP_ZPOS; |
|
7836 |
|
7837 /* subtract a char */ |
|
7838 --maxlen; |
|
7839 } |
|
7840 |
|
7841 digs = 0; |
|
7842 while (mp_iszero (&t) == 0) { |
|
7843 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { |
|
7844 mp_clear (&t); |
|
7845 return res; |
|
7846 } |
|
7847 *str++ = mp_s_rmap[d]; |
|
7848 ++digs; |
|
7849 |
|
7850 if (--maxlen == 1) { |
|
7851 /* no more room */ |
|
7852 break; |
|
7853 } |
|
7854 } |
|
7855 |
|
7856 /* reverse the digits of the string. In this case _s points |
|
7857 * to the first digit [exluding the sign] of the number] |
|
7858 */ |
|
7859 bn_reverse ((unsigned char *)_s, digs); |
|
7860 |
|
7861 /* append a NULL so the string is properly terminated */ |
|
7862 *str = '\0'; |
|
7863 |
|
7864 mp_clear (&t); |
|
7865 return MP_OKAY; |
|
7866 } |
|
7867 |
143
|
7868 #endif |
3
|
7869 |
|
7870 /* End: bn_mp_toradix_n.c */ |
|
7871 |
|
7872 /* Start: bn_mp_unsigned_bin_size.c */ |
143
|
7873 #include <ltc_tommath.h> |
|
7874 #ifdef BN_MP_UNSIGNED_BIN_SIZE_C |
|
7875 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7876 * |
|
7877 * LibTomMath is a library that provides multiple-precision |
|
7878 * integer arithmetic as well as number theoretic functionality. |
|
7879 * |
|
7880 * The library was designed directly after the MPI library by |
|
7881 * Michael Fromberger but has been written from scratch with |
|
7882 * additional optimizations in place. |
|
7883 * |
|
7884 * The library is free for all purposes without any express |
|
7885 * guarantee it works. |
|
7886 * |
|
7887 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7888 */ |
3
|
7889 |
|
7890 /* get the size for an unsigned equivalent */ |
|
7891 int |
|
7892 mp_unsigned_bin_size (mp_int * a) |
|
7893 { |
|
7894 int size = mp_count_bits (a); |
|
7895 return (size / 8 + ((size & 7) != 0 ? 1 : 0)); |
|
7896 } |
143
|
7897 #endif |
3
|
7898 |
|
7899 /* End: bn_mp_unsigned_bin_size.c */ |
|
7900 |
|
7901 /* Start: bn_mp_xor.c */ |
143
|
7902 #include <ltc_tommath.h> |
|
7903 #ifdef BN_MP_XOR_C |
|
7904 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7905 * |
|
7906 * LibTomMath is a library that provides multiple-precision |
|
7907 * integer arithmetic as well as number theoretic functionality. |
|
7908 * |
|
7909 * The library was designed directly after the MPI library by |
|
7910 * Michael Fromberger but has been written from scratch with |
|
7911 * additional optimizations in place. |
|
7912 * |
|
7913 * The library is free for all purposes without any express |
|
7914 * guarantee it works. |
|
7915 * |
|
7916 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7917 */ |
3
|
7918 |
|
7919 /* XOR two ints together */ |
|
7920 int |
|
7921 mp_xor (mp_int * a, mp_int * b, mp_int * c) |
|
7922 { |
|
7923 int res, ix, px; |
|
7924 mp_int t, *x; |
|
7925 |
|
7926 if (a->used > b->used) { |
|
7927 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
7928 return res; |
|
7929 } |
|
7930 px = b->used; |
|
7931 x = b; |
|
7932 } else { |
|
7933 if ((res = mp_init_copy (&t, b)) != MP_OKAY) { |
|
7934 return res; |
|
7935 } |
|
7936 px = a->used; |
|
7937 x = a; |
|
7938 } |
|
7939 |
|
7940 for (ix = 0; ix < px; ix++) { |
143
|
7941 |
3
|
7942 } |
|
7943 mp_clamp (&t); |
|
7944 mp_exch (c, &t); |
|
7945 mp_clear (&t); |
|
7946 return MP_OKAY; |
|
7947 } |
143
|
7948 #endif |
3
|
7949 |
|
7950 /* End: bn_mp_xor.c */ |
|
7951 |
|
7952 /* Start: bn_mp_zero.c */ |
143
|
7953 #include <ltc_tommath.h> |
|
7954 #ifdef BN_MP_ZERO_C |
|
7955 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7956 * |
|
7957 * LibTomMath is a library that provides multiple-precision |
|
7958 * integer arithmetic as well as number theoretic functionality. |
|
7959 * |
|
7960 * The library was designed directly after the MPI library by |
|
7961 * Michael Fromberger but has been written from scratch with |
|
7962 * additional optimizations in place. |
|
7963 * |
|
7964 * The library is free for all purposes without any express |
|
7965 * guarantee it works. |
|
7966 * |
|
7967 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7968 */ |
3
|
7969 |
|
7970 /* set to zero */ |
|
7971 void |
|
7972 mp_zero (mp_int * a) |
|
7973 { |
|
7974 a->sign = MP_ZPOS; |
|
7975 a->used = 0; |
|
7976 memset (a->dp, 0, sizeof (mp_digit) * a->alloc); |
|
7977 } |
143
|
7978 #endif |
3
|
7979 |
|
7980 /* End: bn_mp_zero.c */ |
|
7981 |
|
7982 /* Start: bn_prime_tab.c */ |
143
|
7983 #include <ltc_tommath.h> |
|
7984 #ifdef BN_PRIME_TAB_C |
|
7985 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
7986 * |
|
7987 * LibTomMath is a library that provides multiple-precision |
|
7988 * integer arithmetic as well as number theoretic functionality. |
|
7989 * |
|
7990 * The library was designed directly after the MPI library by |
|
7991 * Michael Fromberger but has been written from scratch with |
|
7992 * additional optimizations in place. |
|
7993 * |
|
7994 * The library is free for all purposes without any express |
|
7995 * guarantee it works. |
|
7996 * |
|
7997 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
7998 */ |
3
|
7999 const mp_digit __prime_tab[] = { |
|
8000 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013, |
|
8001 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035, |
|
8002 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059, |
|
8003 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, |
|
8004 #ifndef MP_8BIT |
|
8005 0x0083, |
|
8006 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD, |
|
8007 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF, |
|
8008 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107, |
|
8009 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137, |
|
8010 |
|
8011 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167, |
|
8012 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199, |
|
8013 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9, |
|
8014 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7, |
|
8015 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239, |
|
8016 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265, |
|
8017 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293, |
|
8018 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF, |
|
8019 |
|
8020 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301, |
|
8021 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B, |
|
8022 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371, |
|
8023 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD, |
|
8024 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5, |
|
8025 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419, |
|
8026 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449, |
|
8027 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B, |
|
8028 |
|
8029 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7, |
|
8030 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503, |
|
8031 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529, |
|
8032 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F, |
|
8033 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3, |
|
8034 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7, |
|
8035 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623, |
|
8036 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653 |
|
8037 #endif |
|
8038 }; |
143
|
8039 #endif |
3
|
8040 |
|
8041 /* End: bn_prime_tab.c */ |
|
8042 |
|
8043 /* Start: bn_reverse.c */ |
143
|
8044 #include <ltc_tommath.h> |
|
8045 #ifdef BN_REVERSE_C |
|
8046 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8047 * |
|
8048 * LibTomMath is a library that provides multiple-precision |
|
8049 * integer arithmetic as well as number theoretic functionality. |
|
8050 * |
|
8051 * The library was designed directly after the MPI library by |
|
8052 * Michael Fromberger but has been written from scratch with |
|
8053 * additional optimizations in place. |
|
8054 * |
|
8055 * The library is free for all purposes without any express |
|
8056 * guarantee it works. |
|
8057 * |
|
8058 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8059 */ |
3
|
8060 |
|
8061 /* reverse an array, used for radix code */ |
|
8062 void |
|
8063 bn_reverse (unsigned char *s, int len) |
|
8064 { |
|
8065 int ix, iy; |
|
8066 unsigned char t; |
|
8067 |
|
8068 ix = 0; |
|
8069 iy = len - 1; |
|
8070 while (ix < iy) { |
|
8071 t = s[ix]; |
|
8072 s[ix] = s[iy]; |
|
8073 s[iy] = t; |
|
8074 ++ix; |
|
8075 --iy; |
|
8076 } |
|
8077 } |
143
|
8078 #endif |
3
|
8079 |
|
8080 /* End: bn_reverse.c */ |
|
8081 |
|
8082 /* Start: bn_s_mp_add.c */ |
143
|
8083 #include <ltc_tommath.h> |
|
8084 #ifdef BN_S_MP_ADD_C |
|
8085 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8086 * |
|
8087 * LibTomMath is a library that provides multiple-precision |
|
8088 * integer arithmetic as well as number theoretic functionality. |
|
8089 * |
|
8090 * The library was designed directly after the MPI library by |
|
8091 * Michael Fromberger but has been written from scratch with |
|
8092 * additional optimizations in place. |
|
8093 * |
|
8094 * The library is free for all purposes without any express |
|
8095 * guarantee it works. |
|
8096 * |
|
8097 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8098 */ |
3
|
8099 |
|
8100 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ |
|
8101 int |
|
8102 s_mp_add (mp_int * a, mp_int * b, mp_int * c) |
|
8103 { |
|
8104 mp_int *x; |
|
8105 int olduse, res, min, max; |
|
8106 |
|
8107 /* find sizes, we let |a| <= |b| which means we have to sort |
|
8108 * them. "x" will point to the input with the most digits |
|
8109 */ |
|
8110 if (a->used > b->used) { |
|
8111 min = b->used; |
|
8112 max = a->used; |
|
8113 x = a; |
|
8114 } else { |
|
8115 min = a->used; |
|
8116 max = b->used; |
|
8117 x = b; |
|
8118 } |
|
8119 |
|
8120 /* init result */ |
|
8121 if (c->alloc < max + 1) { |
|
8122 if ((res = mp_grow (c, max + 1)) != MP_OKAY) { |
|
8123 return res; |
|
8124 } |
|
8125 } |
|
8126 |
|
8127 /* get old used digit count and set new one */ |
|
8128 olduse = c->used; |
|
8129 c->used = max + 1; |
|
8130 |
|
8131 { |
|
8132 register mp_digit u, *tmpa, *tmpb, *tmpc; |
|
8133 register int i; |
|
8134 |
|
8135 /* alias for digit pointers */ |
|
8136 |
|
8137 /* first input */ |
|
8138 tmpa = a->dp; |
|
8139 |
|
8140 /* second input */ |
|
8141 tmpb = b->dp; |
|
8142 |
|
8143 /* destination */ |
|
8144 tmpc = c->dp; |
|
8145 |
|
8146 /* zero the carry */ |
|
8147 u = 0; |
|
8148 for (i = 0; i < min; i++) { |
|
8149 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */ |
|
8150 *tmpc = *tmpa++ + *tmpb++ + u; |
|
8151 |
|
8152 /* U = carry bit of T[i] */ |
|
8153 u = *tmpc >> ((mp_digit)DIGIT_BIT); |
|
8154 |
|
8155 /* take away carry bit from T[i] */ |
|
8156 *tmpc++ &= MP_MASK; |
|
8157 } |
|
8158 |
|
8159 /* now copy higher words if any, that is in A+B |
|
8160 * if A or B has more digits add those in |
|
8161 */ |
|
8162 if (min != max) { |
|
8163 for (; i < max; i++) { |
|
8164 /* T[i] = X[i] + U */ |
|
8165 *tmpc = x->dp[i] + u; |
|
8166 |
|
8167 /* U = carry bit of T[i] */ |
|
8168 u = *tmpc >> ((mp_digit)DIGIT_BIT); |
|
8169 |
|
8170 /* take away carry bit from T[i] */ |
|
8171 *tmpc++ &= MP_MASK; |
|
8172 } |
|
8173 } |
|
8174 |
|
8175 /* add carry */ |
|
8176 *tmpc++ = u; |
|
8177 |
|
8178 /* clear digits above oldused */ |
|
8179 for (i = c->used; i < olduse; i++) { |
|
8180 *tmpc++ = 0; |
|
8181 } |
|
8182 } |
|
8183 |
|
8184 mp_clamp (c); |
|
8185 return MP_OKAY; |
|
8186 } |
143
|
8187 #endif |
3
|
8188 |
|
8189 /* End: bn_s_mp_add.c */ |
|
8190 |
|
8191 /* Start: bn_s_mp_exptmod.c */ |
143
|
8192 #include <ltc_tommath.h> |
|
8193 #ifdef BN_S_MP_EXPTMOD_C |
|
8194 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8195 * |
|
8196 * LibTomMath is a library that provides multiple-precision |
|
8197 * integer arithmetic as well as number theoretic functionality. |
|
8198 * |
|
8199 * The library was designed directly after the MPI library by |
|
8200 * Michael Fromberger but has been written from scratch with |
|
8201 * additional optimizations in place. |
|
8202 * |
|
8203 * The library is free for all purposes without any express |
|
8204 * guarantee it works. |
|
8205 * |
|
8206 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8207 */ |
3
|
8208 |
|
8209 #ifdef MP_LOW_MEM |
|
8210 #define TAB_SIZE 32 |
|
8211 #else |
|
8212 #define TAB_SIZE 256 |
|
8213 #endif |
|
8214 |
|
8215 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) |
|
8216 { |
|
8217 mp_int M[TAB_SIZE], res, mu; |
|
8218 mp_digit buf; |
|
8219 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; |
|
8220 |
|
8221 /* find window size */ |
|
8222 x = mp_count_bits (X); |
|
8223 if (x <= 7) { |
|
8224 winsize = 2; |
|
8225 } else if (x <= 36) { |
|
8226 winsize = 3; |
|
8227 } else if (x <= 140) { |
|
8228 winsize = 4; |
|
8229 } else if (x <= 450) { |
|
8230 winsize = 5; |
|
8231 } else if (x <= 1303) { |
|
8232 winsize = 6; |
|
8233 } else if (x <= 3529) { |
|
8234 winsize = 7; |
|
8235 } else { |
|
8236 winsize = 8; |
|
8237 } |
|
8238 |
|
8239 #ifdef MP_LOW_MEM |
|
8240 if (winsize > 5) { |
|
8241 winsize = 5; |
|
8242 } |
|
8243 #endif |
|
8244 |
|
8245 /* init M array */ |
|
8246 /* init first cell */ |
|
8247 if ((err = mp_init(&M[1])) != MP_OKAY) { |
|
8248 return err; |
|
8249 } |
|
8250 |
|
8251 /* now init the second half of the array */ |
|
8252 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
|
8253 if ((err = mp_init(&M[x])) != MP_OKAY) { |
|
8254 for (y = 1<<(winsize-1); y < x; y++) { |
|
8255 mp_clear (&M[y]); |
|
8256 } |
|
8257 mp_clear(&M[1]); |
|
8258 return err; |
|
8259 } |
|
8260 } |
|
8261 |
|
8262 /* create mu, used for Barrett reduction */ |
|
8263 if ((err = mp_init (&mu)) != MP_OKAY) { |
|
8264 goto __M; |
|
8265 } |
|
8266 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { |
|
8267 goto __MU; |
|
8268 } |
|
8269 |
|
8270 /* create M table |
|
8271 * |
|
8272 * The M table contains powers of the base, |
|
8273 * e.g. M[x] = G**x mod P |
|
8274 * |
|
8275 * The first half of the table is not |
|
8276 * computed though accept for M[0] and M[1] |
|
8277 */ |
|
8278 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { |
|
8279 goto __MU; |
|
8280 } |
|
8281 |
|
8282 /* compute the value at M[1<<(winsize-1)] by squaring |
|
8283 * M[1] (winsize-1) times |
|
8284 */ |
|
8285 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |
|
8286 goto __MU; |
|
8287 } |
|
8288 |
|
8289 for (x = 0; x < (winsize - 1); x++) { |
|
8290 if ((err = mp_sqr (&M[1 << (winsize - 1)], |
|
8291 &M[1 << (winsize - 1)])) != MP_OKAY) { |
|
8292 goto __MU; |
|
8293 } |
|
8294 if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) { |
|
8295 goto __MU; |
|
8296 } |
|
8297 } |
|
8298 |
|
8299 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P) |
|
8300 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1) |
|
8301 */ |
|
8302 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { |
|
8303 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { |
|
8304 goto __MU; |
|
8305 } |
|
8306 if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) { |
|
8307 goto __MU; |
|
8308 } |
|
8309 } |
|
8310 |
|
8311 /* setup result */ |
|
8312 if ((err = mp_init (&res)) != MP_OKAY) { |
|
8313 goto __MU; |
|
8314 } |
|
8315 mp_set (&res, 1); |
|
8316 |
|
8317 /* set initial mode and bit cnt */ |
|
8318 mode = 0; |
|
8319 bitcnt = 1; |
|
8320 buf = 0; |
|
8321 digidx = X->used - 1; |
|
8322 bitcpy = 0; |
|
8323 bitbuf = 0; |
|
8324 |
|
8325 for (;;) { |
|
8326 /* grab next digit as required */ |
|
8327 if (--bitcnt == 0) { |
|
8328 /* if digidx == -1 we are out of digits */ |
|
8329 if (digidx == -1) { |
|
8330 break; |
|
8331 } |
|
8332 /* read next digit and reset the bitcnt */ |
|
8333 buf = X->dp[digidx--]; |
|
8334 bitcnt = (int) DIGIT_BIT; |
|
8335 } |
|
8336 |
|
8337 /* grab the next msb from the exponent */ |
|
8338 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1; |
|
8339 buf <<= (mp_digit)1; |
|
8340 |
|
8341 /* if the bit is zero and mode == 0 then we ignore it |
|
8342 * These represent the leading zero bits before the first 1 bit |
|
8343 * in the exponent. Technically this opt is not required but it |
|
8344 * does lower the # of trivial squaring/reductions used |
|
8345 */ |
|
8346 if (mode == 0 && y == 0) { |
|
8347 continue; |
|
8348 } |
|
8349 |
|
8350 /* if the bit is zero and mode == 1 then we square */ |
|
8351 if (mode == 1 && y == 0) { |
|
8352 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
8353 goto __RES; |
|
8354 } |
|
8355 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
8356 goto __RES; |
|
8357 } |
|
8358 continue; |
|
8359 } |
|
8360 |
|
8361 /* else we add it to the window */ |
|
8362 bitbuf |= (y << (winsize - ++bitcpy)); |
|
8363 mode = 2; |
|
8364 |
|
8365 if (bitcpy == winsize) { |
|
8366 /* ok window is filled so square as required and multiply */ |
|
8367 /* square first */ |
|
8368 for (x = 0; x < winsize; x++) { |
|
8369 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
8370 goto __RES; |
|
8371 } |
|
8372 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
8373 goto __RES; |
|
8374 } |
|
8375 } |
|
8376 |
|
8377 /* then multiply */ |
|
8378 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { |
|
8379 goto __RES; |
|
8380 } |
|
8381 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
8382 goto __RES; |
|
8383 } |
|
8384 |
|
8385 /* empty window and reset */ |
|
8386 bitcpy = 0; |
|
8387 bitbuf = 0; |
|
8388 mode = 1; |
|
8389 } |
|
8390 } |
|
8391 |
|
8392 /* if bits remain then square/multiply */ |
|
8393 if (mode == 2 && bitcpy > 0) { |
|
8394 /* square then multiply if the bit is set */ |
|
8395 for (x = 0; x < bitcpy; x++) { |
|
8396 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
|
8397 goto __RES; |
|
8398 } |
|
8399 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
8400 goto __RES; |
|
8401 } |
|
8402 |
|
8403 bitbuf <<= 1; |
|
8404 if ((bitbuf & (1 << winsize)) != 0) { |
|
8405 /* then multiply */ |
|
8406 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { |
|
8407 goto __RES; |
|
8408 } |
|
8409 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { |
|
8410 goto __RES; |
|
8411 } |
|
8412 } |
|
8413 } |
|
8414 } |
|
8415 |
|
8416 mp_exch (&res, Y); |
|
8417 err = MP_OKAY; |
|
8418 __RES:mp_clear (&res); |
|
8419 __MU:mp_clear (&mu); |
|
8420 __M: |
|
8421 mp_clear(&M[1]); |
|
8422 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
|
8423 mp_clear (&M[x]); |
|
8424 } |
|
8425 return err; |
|
8426 } |
143
|
8427 #endif |
3
|
8428 |
|
8429 /* End: bn_s_mp_exptmod.c */ |
|
8430 |
|
8431 /* Start: bn_s_mp_mul_digs.c */ |
143
|
8432 #include <ltc_tommath.h> |
|
8433 #ifdef BN_S_MP_MUL_DIGS_C |
|
8434 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8435 * |
|
8436 * LibTomMath is a library that provides multiple-precision |
|
8437 * integer arithmetic as well as number theoretic functionality. |
|
8438 * |
|
8439 * The library was designed directly after the MPI library by |
|
8440 * Michael Fromberger but has been written from scratch with |
|
8441 * additional optimizations in place. |
|
8442 * |
|
8443 * The library is free for all purposes without any express |
|
8444 * guarantee it works. |
|
8445 * |
|
8446 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8447 */ |
3
|
8448 |
|
8449 /* multiplies |a| * |b| and only computes upto digs digits of result |
|
8450 * HAC pp. 595, Algorithm 14.12 Modified so you can control how |
|
8451 * many digits of output are created. |
|
8452 */ |
|
8453 int |
|
8454 s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
8455 { |
|
8456 mp_int t; |
|
8457 int res, pa, pb, ix, iy; |
|
8458 mp_digit u; |
|
8459 mp_word r; |
|
8460 mp_digit tmpx, *tmpt, *tmpy; |
|
8461 |
|
8462 /* can we use the fast multiplier? */ |
|
8463 if (((digs) < MP_WARRAY) && |
|
8464 MIN (a->used, b->used) < |
|
8465 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
8466 return fast_s_mp_mul_digs (a, b, c, digs); |
|
8467 } |
|
8468 |
|
8469 if ((res = mp_init_size (&t, digs)) != MP_OKAY) { |
|
8470 return res; |
|
8471 } |
|
8472 t.used = digs; |
|
8473 |
|
8474 /* compute the digits of the product directly */ |
|
8475 pa = a->used; |
|
8476 for (ix = 0; ix < pa; ix++) { |
|
8477 /* set the carry to zero */ |
|
8478 u = 0; |
|
8479 |
|
8480 /* limit ourselves to making digs digits of output */ |
|
8481 pb = MIN (b->used, digs - ix); |
|
8482 |
|
8483 /* setup some aliases */ |
|
8484 /* copy of the digit from a used within the nested loop */ |
|
8485 tmpx = a->dp[ix]; |
|
8486 |
|
8487 /* an alias for the destination shifted ix places */ |
|
8488 tmpt = t.dp + ix; |
|
8489 |
|
8490 /* an alias for the digits of b */ |
|
8491 tmpy = b->dp; |
|
8492 |
|
8493 /* compute the columns of the output and propagate the carry */ |
|
8494 for (iy = 0; iy < pb; iy++) { |
|
8495 /* compute the column as a mp_word */ |
|
8496 r = ((mp_word)*tmpt) + |
|
8497 ((mp_word)tmpx) * ((mp_word)*tmpy++) + |
|
8498 ((mp_word) u); |
|
8499 |
|
8500 /* the new column is the lower part of the result */ |
|
8501 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
8502 |
|
8503 /* get the carry word from the result */ |
|
8504 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
|
8505 } |
|
8506 /* set carry if it is placed below digs */ |
|
8507 if (ix + iy < digs) { |
|
8508 *tmpt = u; |
|
8509 } |
|
8510 } |
|
8511 |
|
8512 mp_clamp (&t); |
|
8513 mp_exch (&t, c); |
|
8514 |
|
8515 mp_clear (&t); |
|
8516 return MP_OKAY; |
|
8517 } |
143
|
8518 #endif |
3
|
8519 |
|
8520 /* End: bn_s_mp_mul_digs.c */ |
|
8521 |
|
8522 /* Start: bn_s_mp_mul_high_digs.c */ |
143
|
8523 #include <ltc_tommath.h> |
|
8524 #ifdef BN_S_MP_MUL_HIGH_DIGS_C |
|
8525 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8526 * |
|
8527 * LibTomMath is a library that provides multiple-precision |
|
8528 * integer arithmetic as well as number theoretic functionality. |
|
8529 * |
|
8530 * The library was designed directly after the MPI library by |
|
8531 * Michael Fromberger but has been written from scratch with |
|
8532 * additional optimizations in place. |
|
8533 * |
|
8534 * The library is free for all purposes without any express |
|
8535 * guarantee it works. |
|
8536 * |
|
8537 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8538 */ |
3
|
8539 |
|
8540 /* multiplies |a| * |b| and does not compute the lower digs digits |
|
8541 * [meant to get the higher part of the product] |
|
8542 */ |
|
8543 int |
|
8544 s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
8545 { |
|
8546 mp_int t; |
|
8547 int res, pa, pb, ix, iy; |
|
8548 mp_digit u; |
|
8549 mp_word r; |
|
8550 mp_digit tmpx, *tmpt, *tmpy; |
|
8551 |
|
8552 /* can we use the fast multiplier? */ |
143
|
8553 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C |
3
|
8554 if (((a->used + b->used + 1) < MP_WARRAY) |
|
8555 && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
8556 return fast_s_mp_mul_high_digs (a, b, c, digs); |
|
8557 } |
143
|
8558 #endif |
3
|
8559 |
|
8560 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { |
|
8561 return res; |
|
8562 } |
|
8563 t.used = a->used + b->used + 1; |
|
8564 |
|
8565 pa = a->used; |
|
8566 pb = b->used; |
|
8567 for (ix = 0; ix < pa; ix++) { |
|
8568 /* clear the carry */ |
|
8569 u = 0; |
|
8570 |
|
8571 /* left hand side of A[ix] * B[iy] */ |
|
8572 tmpx = a->dp[ix]; |
|
8573 |
|
8574 /* alias to the address of where the digits will be stored */ |
|
8575 tmpt = &(t.dp[digs]); |
|
8576 |
|
8577 /* alias for where to read the right hand side from */ |
|
8578 tmpy = b->dp + (digs - ix); |
|
8579 |
|
8580 for (iy = digs - ix; iy < pb; iy++) { |
|
8581 /* calculate the double precision result */ |
|
8582 r = ((mp_word)*tmpt) + |
|
8583 ((mp_word)tmpx) * ((mp_word)*tmpy++) + |
|
8584 ((mp_word) u); |
|
8585 |
|
8586 /* get the lower part */ |
|
8587 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
8588 |
|
8589 /* carry the carry */ |
|
8590 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
|
8591 } |
|
8592 *tmpt = u; |
|
8593 } |
|
8594 mp_clamp (&t); |
|
8595 mp_exch (&t, c); |
|
8596 mp_clear (&t); |
|
8597 return MP_OKAY; |
|
8598 } |
143
|
8599 #endif |
3
|
8600 |
|
8601 /* End: bn_s_mp_mul_high_digs.c */ |
|
8602 |
|
8603 /* Start: bn_s_mp_sqr.c */ |
143
|
8604 #include <ltc_tommath.h> |
|
8605 #ifdef BN_S_MP_SQR_C |
|
8606 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8607 * |
|
8608 * LibTomMath is a library that provides multiple-precision |
|
8609 * integer arithmetic as well as number theoretic functionality. |
|
8610 * |
|
8611 * The library was designed directly after the MPI library by |
|
8612 * Michael Fromberger but has been written from scratch with |
|
8613 * additional optimizations in place. |
|
8614 * |
|
8615 * The library is free for all purposes without any express |
|
8616 * guarantee it works. |
|
8617 * |
|
8618 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8619 */ |
3
|
8620 |
|
8621 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ |
|
8622 int |
|
8623 s_mp_sqr (mp_int * a, mp_int * b) |
|
8624 { |
|
8625 mp_int t; |
|
8626 int res, ix, iy, pa; |
|
8627 mp_word r; |
|
8628 mp_digit u, tmpx, *tmpt; |
|
8629 |
|
8630 pa = a->used; |
|
8631 if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) { |
|
8632 return res; |
|
8633 } |
|
8634 |
|
8635 /* default used is maximum possible size */ |
|
8636 t.used = 2*pa + 1; |
|
8637 |
|
8638 for (ix = 0; ix < pa; ix++) { |
|
8639 /* first calculate the digit at 2*ix */ |
|
8640 /* calculate double precision result */ |
|
8641 r = ((mp_word) t.dp[2*ix]) + |
|
8642 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); |
|
8643 |
|
8644 /* store lower part in result */ |
|
8645 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
8646 |
|
8647 /* get the carry */ |
|
8648 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
8649 |
|
8650 /* left hand side of A[ix] * A[iy] */ |
|
8651 tmpx = a->dp[ix]; |
|
8652 |
|
8653 /* alias for where to store the results */ |
|
8654 tmpt = t.dp + (2*ix + 1); |
|
8655 |
|
8656 for (iy = ix + 1; iy < pa; iy++) { |
|
8657 /* first calculate the product */ |
|
8658 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); |
|
8659 |
|
8660 /* now calculate the double precision result, note we use |
|
8661 * addition instead of *2 since it's easier to optimize |
|
8662 */ |
|
8663 r = ((mp_word) *tmpt) + r + r + ((mp_word) u); |
|
8664 |
|
8665 /* store lower part */ |
|
8666 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
8667 |
|
8668 /* get carry */ |
|
8669 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
8670 } |
|
8671 /* propagate upwards */ |
|
8672 while (u != ((mp_digit) 0)) { |
|
8673 r = ((mp_word) *tmpt) + ((mp_word) u); |
|
8674 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
8675 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
8676 } |
|
8677 } |
|
8678 |
|
8679 mp_clamp (&t); |
|
8680 mp_exch (&t, b); |
|
8681 mp_clear (&t); |
|
8682 return MP_OKAY; |
|
8683 } |
143
|
8684 #endif |
3
|
8685 |
|
8686 /* End: bn_s_mp_sqr.c */ |
|
8687 |
|
8688 /* Start: bn_s_mp_sub.c */ |
143
|
8689 #include <ltc_tommath.h> |
|
8690 #ifdef BN_S_MP_SUB_C |
|
8691 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8692 * |
|
8693 * LibTomMath is a library that provides multiple-precision |
|
8694 * integer arithmetic as well as number theoretic functionality. |
|
8695 * |
|
8696 * The library was designed directly after the MPI library by |
|
8697 * Michael Fromberger but has been written from scratch with |
|
8698 * additional optimizations in place. |
|
8699 * |
|
8700 * The library is free for all purposes without any express |
|
8701 * guarantee it works. |
|
8702 * |
|
8703 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8704 */ |
3
|
8705 |
|
8706 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ |
|
8707 int |
|
8708 s_mp_sub (mp_int * a, mp_int * b, mp_int * c) |
|
8709 { |
|
8710 int olduse, res, min, max; |
|
8711 |
|
8712 /* find sizes */ |
|
8713 min = b->used; |
|
8714 max = a->used; |
|
8715 |
|
8716 /* init result */ |
|
8717 if (c->alloc < max) { |
|
8718 if ((res = mp_grow (c, max)) != MP_OKAY) { |
|
8719 return res; |
|
8720 } |
|
8721 } |
|
8722 olduse = c->used; |
|
8723 c->used = max; |
|
8724 |
|
8725 { |
|
8726 register mp_digit u, *tmpa, *tmpb, *tmpc; |
|
8727 register int i; |
|
8728 |
|
8729 /* alias for digit pointers */ |
|
8730 tmpa = a->dp; |
|
8731 tmpb = b->dp; |
|
8732 tmpc = c->dp; |
|
8733 |
|
8734 /* set carry to zero */ |
|
8735 u = 0; |
|
8736 for (i = 0; i < min; i++) { |
|
8737 /* T[i] = A[i] - B[i] - U */ |
|
8738 *tmpc = *tmpa++ - *tmpb++ - u; |
|
8739 |
|
8740 /* U = carry bit of T[i] |
|
8741 * Note this saves performing an AND operation since |
|
8742 * if a carry does occur it will propagate all the way to the |
|
8743 * MSB. As a result a single shift is enough to get the carry |
|
8744 */ |
|
8745 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); |
|
8746 |
|
8747 /* Clear carry from T[i] */ |
|
8748 *tmpc++ &= MP_MASK; |
|
8749 } |
|
8750 |
|
8751 /* now copy higher words if any, e.g. if A has more digits than B */ |
|
8752 for (; i < max; i++) { |
|
8753 /* T[i] = A[i] - U */ |
|
8754 *tmpc = *tmpa++ - u; |
|
8755 |
|
8756 /* U = carry bit of T[i] */ |
|
8757 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); |
|
8758 |
|
8759 /* Clear carry from T[i] */ |
|
8760 *tmpc++ &= MP_MASK; |
|
8761 } |
|
8762 |
|
8763 /* clear digits above used (since we may not have grown result above) */ |
|
8764 for (i = c->used; i < olduse; i++) { |
|
8765 *tmpc++ = 0; |
|
8766 } |
|
8767 } |
|
8768 |
|
8769 mp_clamp (c); |
|
8770 return MP_OKAY; |
|
8771 } |
|
8772 |
143
|
8773 #endif |
3
|
8774 |
|
8775 /* End: bn_s_mp_sub.c */ |
|
8776 |
|
8777 /* Start: bncore.c */ |
143
|
8778 #include <ltc_tommath.h> |
|
8779 #ifdef BNCORE_C |
|
8780 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
8781 * |
|
8782 * LibTomMath is a library that provides multiple-precision |
|
8783 * integer arithmetic as well as number theoretic functionality. |
|
8784 * |
|
8785 * The library was designed directly after the MPI library by |
|
8786 * Michael Fromberger but has been written from scratch with |
|
8787 * additional optimizations in place. |
|
8788 * |
|
8789 * The library is free for all purposes without any express |
|
8790 * guarantee it works. |
|
8791 * |
|
8792 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
8793 */ |
3
|
8794 |
|
8795 /* Known optimal configurations |
|
8796 |
|
8797 CPU /Compiler /MUL CUTOFF/SQR CUTOFF |
|
8798 ------------------------------------------------------------- |
143
|
8799 Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-) |
15
|
8800 |
3
|
8801 */ |
|
8802 |
143
|
8803 int KARATSUBA_MUL_CUTOFF = 88, /* Min. number of digits before Karatsuba multiplication is used. */ |
|
8804 KARATSUBA_SQR_CUTOFF = 128, /* Min. number of digits before Karatsuba squaring is used. */ |
3
|
8805 |
|
8806 TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */ |
|
8807 TOOM_SQR_CUTOFF = 400; |
143
|
8808 #endif |
3
|
8809 |
|
8810 /* End: bncore.c */ |
|
8811 |
|
8812 |
|
8813 /* EOF */ |