Skip to content

Commit c962e52

Browse files
committed
addition of FFT multiplication/squaring
1 parent dad2509 commit c962e52

24 files changed

+52200
-19030
lines changed

bn_mp_mul.c

+37-31
Original file line numberDiff line numberDiff line change
@@ -43,54 +43,60 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
4343
GO_ON:
4444
#endif
4545

46+
#ifdef BN_S_MP_FFT_MUL_C
47+
if ((MP_MIN(a->used, b->used) >= FFT_MUL_LOWER_CO) && (MP_MAX(a->used, b->used) < FFT_MUL_UPPER_CO)) {
48+
res = s_mp_fft_mul(a, b, c);
49+
} else
50+
#endif
51+
4652
#ifdef BN_S_MP_TOOM_COOK_5_MUL_C
4753

48-
if (MP_MIN(a->used, b->used) >= TOOM_COOK_5_MUL_CO) {
49-
res = s_mp_toom_cook_5_mul(a, b, c);
50-
} else
54+
if (MP_MIN(a->used, b->used) >= TOOM_COOK_5_MUL_CO) {
55+
res = s_mp_toom_cook_5_mul(a, b, c);
56+
} else
5157
#endif
5258

5359
#ifdef BN_S_MP_TOOM_COOK_4_MUL_C
54-
if (MP_MIN(a->used, b->used) >= TOOM_COOK_4_MUL_CO) {
55-
res = s_mp_toom_cook_4_mul(a, b, c);
56-
} else
60+
if (MP_MIN(a->used, b->used) >= TOOM_COOK_4_MUL_CO) {
61+
res = s_mp_toom_cook_4_mul(a, b, c);
62+
} else
5763
#endif
58-
/* use Toom-Cook? */
64+
/* use Toom-Cook? */
5965
#ifdef BN_S_MP_TOOM_MUL_C
60-
if (MP_MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
61-
res = s_mp_toom_mul(a, b, c);
62-
} else
66+
if (MP_MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
67+
res = s_mp_toom_mul(a, b, c);
68+
} else
6369
#endif
6470
#ifdef BN_S_MP_KARATSUBA_MUL_C
65-
/* use Karatsuba? */
66-
if (MP_MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
67-
res = s_mp_karatsuba_mul(a, b, c);
68-
} else
71+
/* use Karatsuba? */
72+
if (MP_MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
73+
res = s_mp_karatsuba_mul(a, b, c);
74+
} else
6975
#endif
70-
{
71-
/* can we use the fast multiplier?
72-
*
73-
* The fast multiplier can be used if the output will
74-
* have less than MP_WARRAY digits and the number of
75-
* digits won't affect carry propagation
76-
*/
77-
int digs = a->used + b->used + 1;
76+
{
77+
/* can we use the fast multiplier?
78+
*
79+
* The fast multiplier can be used if the output will
80+
* have less than MP_WARRAY digits and the number of
81+
* digits won't affect carry propagation
82+
*/
83+
int digs = a->used + b->used + 1;
7884

7985
#ifdef BN_S_MP_MUL_DIGS_FAST_C
80-
if ((digs < (int)MP_WARRAY) &&
81-
(MP_MIN(a->used, b->used) <=
82-
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
83-
res = s_mp_mul_digs_fast(a, b, c, digs);
84-
} else
86+
if ((digs < (int)MP_WARRAY) &&
87+
(MP_MIN(a->used, b->used) <=
88+
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
89+
res = s_mp_mul_digs_fast(a, b, c, digs);
90+
} else
8591
#endif
86-
{
92+
{
8793
#ifdef BN_S_MP_MUL_DIGS_C
88-
res = s_mp_mul_digs(a, b, c, a->used + b->used + 1);
94+
res = s_mp_mul_digs(a, b, c, a->used + b->used + 1);
8995
#else
90-
res = MP_VAL;
96+
res = MP_VAL;
9197
#endif
98+
}
9299
}
93-
}
94100
END:
95101
c->sign = (c->used > 0) ? neg : MP_ZPOS;
96102
return res;

bn_mp_sqr.c

+30-24
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,53 @@ int mp_sqr(const mp_int *a, mp_int *b)
88
{
99
int res;
1010

11-
#ifdef BN_S_MP_TOOM_COOK_5_SQR_C
12-
if (a->used >= TOOM_COOK_5_SQR_CO) {
13-
res = s_mp_toom_cook_5_sqr(a, b);
11+
#ifdef BN_S_MP_FFT_SQR_C
12+
if ((a->used >= FFT_SQR_LOWER_CO) && (a->used < FFT_SQR_UPPER_CO)) {
13+
res = s_mp_fft_sqr(a, b);
1414
} else
1515
#endif
1616

17-
#ifdef BN_S_MP_TOOM_COOK_4_SQR_C
18-
if (a->used >= TOOM_COOK_4_SQR_CO) {
19-
res = s_mp_toom_cook_4_sqr(a, b);
17+
#ifdef BN_S_MP_TOOM_COOK_5_SQR_C
18+
if (a->used >= TOOM_COOK_5_SQR_CO) {
19+
res = s_mp_toom_cook_5_sqr(a, b);
2020
} else
2121
#endif
2222

23-
#ifdef BN_S_MP_TOOM_SQR_C
24-
/* use Toom-Cook? */
25-
if (a->used >= TOOM_SQR_CUTOFF) {
26-
res = s_mp_toom_sqr(a, b);
27-
/* Karatsuba? */
23+
#ifdef BN_S_MP_TOOM_COOK_4_SQR_C
24+
if (a->used >= TOOM_COOK_4_SQR_CO) {
25+
res = s_mp_toom_cook_4_sqr(a, b);
2826
} else
2927
#endif
30-
#ifdef BN_S_MP_KARATSUBA_SQR_C
31-
if (a->used >= KARATSUBA_SQR_CUTOFF) {
32-
res = s_mp_karatsuba_sqr(a, b);
28+
29+
#ifdef BN_S_MP_TOOM_SQR_C
30+
/* use Toom-Cook? */
31+
if (a->used >= TOOM_SQR_CUTOFF) {
32+
res = s_mp_toom_sqr(a, b);
33+
/* Karatsuba? */
3334
} else
3435
#endif
35-
{
36-
#ifdef BN_S_MP_SQR_FAST_C
37-
/* can we use the fast comba multiplier? */
38-
if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
39-
(a->used <
40-
(int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT)) - 1u)))) {
41-
res = s_mp_sqr_fast(a, b);
36+
#ifdef BN_S_MP_KARATSUBA_SQR_C
37+
if (a->used >= KARATSUBA_SQR_CUTOFF) {
38+
res = s_mp_karatsuba_sqr(a, b);
4239
} else
4340
#endif
4441
{
42+
#ifdef BN_S_MP_SQR_FAST_C
43+
/* can we use the fast comba multiplier? */
44+
if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
45+
(a->used <
46+
(int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT)) - 1u)))) {
47+
res = s_mp_sqr_fast(a, b);
48+
} else
49+
#endif
50+
{
4551
#ifdef BN_S_MP_SQR_C
46-
res = s_mp_sqr(a, b);
52+
res = s_mp_sqr(a, b);
4753
#else
48-
res = MP_VAL;
54+
res = MP_VAL;
4955
#endif
56+
}
5057
}
51-
}
5258
b->sign = MP_ZPOS;
5359
return res;
5460
}

0 commit comments

Comments
 (0)