|
6 | 6 | /* high level multiplication (handles sign) */
|
7 | 7 | int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
8 | 8 | {
|
9 |
| - int res, neg; |
10 |
| -#ifdef BN_MP_BALANCE_MUL_C |
11 |
| - int len_b, len_a; |
12 |
| -#endif |
| 9 | + int res, neg, min_len, max_len, digs; |
| 10 | + min_len = MIN(a->used, b->used); |
| 11 | + max_len = MAX(a->used, b->used); |
| 12 | + digs = a->used + b->used + 1; |
13 | 13 | neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
14 |
| -#ifdef BN_MP_BALANCE_MUL_C |
15 |
| - len_a = a->used; |
16 |
| - len_b = b->used; |
17 |
| - |
18 |
| - if (len_a == len_b) { |
19 |
| - goto GO_ON; |
20 |
| - } |
21 |
| - /* |
22 |
| - * Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. |
23 |
| - * The bigger one needs to be at least about one KARATSUBA_MUL_CUTOFF bigger |
24 |
| - * to make some sense, but it depends on architecture, OS, position of the |
25 |
| - * stars... so YMMV. |
26 |
| - * Using it to cut the input into slices small enough for fast_s_mp_mul_digs |
27 |
| - * was actually slower on the author's machine, but YMMV. |
28 |
| - */ |
29 |
| - if ((MIN(len_a, len_b) < KARATSUBA_MUL_CUTOFF) |
30 |
| - || ((MAX(len_a, len_b)) / 2 < KARATSUBA_MUL_CUTOFF)) { |
31 |
| - goto GO_ON; |
32 |
| - } |
33 |
| - /* |
34 |
| - * Not much effect was observed below a ratio of 1:2, but again: YMMV. |
35 |
| - */ |
36 |
| - if ((MAX(len_a, len_b) / MIN(len_a, len_b)) < 2) { |
37 |
| - goto GO_ON; |
38 |
| - } |
39 |
| - |
40 |
| - res = mp_balance_mul(a,b,c); |
41 |
| - goto END; |
42 |
| - |
43 |
| -GO_ON: |
44 |
| -#endif |
45 | 14 |
|
46 |
| - /* use Toom-Cook? */ |
47 |
| -#ifdef BN_MP_TOOM_MUL_C |
48 |
| - if (MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) { |
| 15 | + if (MP_HAS(MP_BALANCE_MUL) && |
| 16 | + /* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. |
| 17 | + * The bigger one needs to be at least about one KARATSUBA_MUL_CUTOFF bigger |
| 18 | + * to make some sense, but it depends on architecture, OS, position of the |
| 19 | + * stars... so YMMV. |
| 20 | + * Using it to cut the input into slices small enough for fast_s_mp_mul_digs |
| 21 | + * was actually slower on the author's machine, but YMMV. |
| 22 | + */ |
| 23 | + (min_len >= KARATSUBA_MUL_CUTOFF) && |
| 24 | + (max_len / 2 >= KARATSUBA_MUL_CUTOFF) && |
| 25 | + /* Not much effect was observed below a ratio of 1:2, but again: YMMV. */ |
| 26 | + (max_len >= (2 * min_len))) { |
| 27 | + res = mp_balance_mul(a,b,c); |
| 28 | + } else if (MP_HAS(MP_TOOM_MUL) && |
| 29 | + min_len >= TOOM_MUL_CUTOFF) { |
49 | 30 | res = mp_toom_mul(a, b, c);
|
50 |
| - } else |
51 |
| -#endif |
52 |
| -#ifdef BN_MP_KARATSUBA_MUL_C |
53 |
| - /* use Karatsuba? */ |
54 |
| - if (MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { |
55 |
| - res = mp_karatsuba_mul(a, b, c); |
56 |
| - } else |
57 |
| -#endif |
58 |
| - { |
59 |
| - /* can we use the fast multiplier? |
60 |
| - * |
61 |
| - * The fast multiplier can be used if the output will |
62 |
| - * have less than MP_WARRAY digits and the number of |
63 |
| - * digits won't affect carry propagation |
64 |
| - */ |
65 |
| - int digs = a->used + b->used + 1; |
66 |
| - |
67 |
| -#ifdef BN_FAST_S_MP_MUL_DIGS_C |
68 |
| - if ((digs < (int)MP_WARRAY) && |
69 |
| - (MIN(a->used, b->used) <= |
70 |
| - (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { |
71 |
| - res = fast_s_mp_mul_digs(a, b, c, digs); |
72 |
| - } else |
73 |
| -#endif |
74 |
| - { |
75 |
| -#ifdef BN_S_MP_MUL_DIGS_C |
76 |
| - res = s_mp_mul(a, b, c); /* uses s_mp_mul_digs */ |
77 |
| -#else |
78 |
| - res = MP_VAL; |
79 |
| -#endif |
80 |
| - } |
81 |
| - } |
82 |
| -END: |
| 31 | + } else if (MP_HAS(MP_KARATSUBA_MUL) && |
| 32 | + min_len >= KARATSUBA_MUL_CUTOFF) { |
| 33 | + res = mp_karatsuba_mul(a, b, c); |
| 34 | + } else if (MP_HAS(FAST_S_MP_MUL_DIGS) && |
| 35 | + /* can we use the fast multiplier? |
| 36 | + * |
| 37 | + * The fast multiplier can be used if the output will |
| 38 | + * have less than MP_WARRAY digits and the number of |
| 39 | + * digits won't affect carry propagation |
| 40 | + */ |
| 41 | + (digs < (int)MP_WARRAY) && |
| 42 | + (min_len <= |
| 43 | + (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { |
| 44 | + res = fast_s_mp_mul_digs(a, b, c, digs); |
| 45 | + } else if (MP_HAS(S_MP_MUL_DIGS)) { |
| 46 | + res = s_mp_mul(a, b, c); /* uses s_mp_mul_digs */ |
| 47 | + } else { |
| 48 | + res = MP_VAL; |
| 49 | + } |
83 | 50 | c->sign = (c->used > 0) ? neg : MP_ZPOS;
|
84 | 51 | return res;
|
85 | 52 | }
|
86 | 53 | #endif
|
87 |
| - |
0 commit comments