2
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* high level multiplication (handles sign) */ |
|
18 int mp_mul (mp_int * a, mp_int * b, mp_int * c) |
|
19 { |
|
20 int res, neg; |
|
21 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; |
|
22 |
|
23 /* use Toom-Cook? */ |
|
24 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { |
|
25 res = mp_toom_mul(a, b, c); |
|
26 /* use Karatsuba? */ |
|
27 } else if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { |
|
28 res = mp_karatsuba_mul (a, b, c); |
|
29 } else { |
|
30 /* can we use the fast multiplier? |
|
31 * |
|
32 * The fast multiplier can be used if the output will |
|
33 * have less than MP_WARRAY digits and the number of |
|
34 * digits won't affect carry propagation |
|
35 */ |
|
36 int digs = a->used + b->used + 1; |
|
37 |
|
38 if ((digs < MP_WARRAY) && |
|
39 MIN(a->used, b->used) <= |
|
40 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
41 res = fast_s_mp_mul_digs (a, b, c, digs); |
|
42 } else { |
|
43 res = s_mp_mul (a, b, c); |
|
44 } |
|
45 } |
|
46 c->sign = neg; |
|
47 return res; |
|
48 } |