Skip to content

Commit 8c1b296

Browse files
committed
add feature detection macro MP_HAS
1 parent 584405f commit 8c1b296

13 files changed

+191
-234
lines changed

bn_mp_div_d.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
4343
return MP_OKAY;
4444
}
4545

46-
#ifdef BN_MP_DIV_3_C
4746
/* three? */
48-
if (b == 3u) {
47+
if (MP_HAS(MP_DIV_3) && b == 3u) {
4948
return mp_div_3(a, c, d);
5049
}
51-
#endif
5250

5351
/* no easy answer [c'est la vie]. Just division */
5452
if ((err = mp_init_size(&q, a->used)) != MP_OKAY) {

bn_mp_exptmod.c

+12-27
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
1919

2020
/* if exponent X is negative we have to recurse */
2121
if (X->sign == MP_NEG) {
22-
#ifdef BN_MP_INVMOD_C
2322
mp_int tmpG, tmpX;
2423
mp_err err;
2524

25+
if (!MP_HAS(MP_INVMOD)) {
26+
return MP_VAL;
27+
}
28+
2629
/* first compute 1/G mod P */
2730
if ((err = mp_init(&tmpG)) != MP_OKAY) {
2831
return err;
@@ -46,50 +49,32 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
4649
err = mp_exptmod(&tmpG, &tmpX, P, Y);
4750
mp_clear_multi(&tmpG, &tmpX, NULL);
4851
return err;
49-
#else
50-
/* no invmod */
51-
return MP_VAL;
52-
#endif
5352
}
5453

5554
/* modified diminished radix reduction */
56-
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
57-
if (mp_reduce_is_2k_l(P) == MP_YES) {
55+
if (MP_HAS(MP_REDUCE_IS_2K_L) && MP_HAS(MP_REDUCE_2K_L) && MP_HAS(S_MP_EXPTMOD) &&
56+
mp_reduce_is_2k_l(P) == MP_YES) {
5857
return s_mp_exptmod(G, X, P, Y, 1);
5958
}
60-
#endif
6159

62-
#ifdef BN_MP_DR_IS_MODULUS_C
63-
/* is it a DR modulus? */
64-
dr = (mp_dr_is_modulus(P) == MP_YES) ? 1 : 0;
65-
#else
66-
/* default to no */
67-
dr = 0;
68-
#endif
60+
/* is it a DR modulus? default to no */
61+
dr = MP_HAS(MP_DR_IS_MODULUS) && mp_dr_is_modulus(P) == MP_YES ? 1 : 0;
6962

70-
#ifdef BN_MP_REDUCE_IS_2K_C
7163
/* if not, is it a unrestricted DR modulus? */
72-
if (dr == 0) {
64+
if (MP_HAS(MP_REDUCE_IS_2K) && dr == 0) {
7365
dr = (mp_reduce_is_2k(P) == MP_YES) ? 2 : 0;
7466
}
75-
#endif
7667

7768
/* if the modulus is odd or dr != 0 use the montgomery method */
78-
#ifdef BN_S_MP_EXPTMOD_FAST_C
79-
if (MP_IS_ODD(P) || (dr != 0)) {
69+
if (MP_HAS(S_MP_EXPTMOD_FAST) && (MP_IS_ODD(P) || (dr != 0))) {
8070
return s_mp_exptmod_fast(G, X, P, Y, dr);
81-
} else {
82-
#endif
83-
#ifdef BN_S_MP_EXPTMOD_C
71+
} else if (MP_HAS(S_MP_EXPTMOD)) {
8472
/* otherwise use the generic Barrett reduction technique */
8573
return s_mp_exptmod(G, X, P, Y, 0);
86-
#else
74+
} else {
8775
/* no exptmod for evens */
8876
return MP_VAL;
89-
#endif
90-
#ifdef BN_S_MP_EXPTMOD_FAST_C
9177
}
92-
#endif
9378
}
9479

9580
#endif

bn_mp_invmod.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
1111
return MP_VAL;
1212
}
1313

14-
#ifdef BN_S_MP_INVMOD_FAST_C
1514
/* if the modulus is odd we can use a faster routine instead */
16-
if (MP_IS_ODD(b)) {
15+
if (MP_HAS(S_MP_INVMOD_FAST) && MP_IS_ODD(b)) {
1716
return s_mp_invmod_fast(a, b, c);
1817
}
19-
#endif
2018

21-
#ifdef BN_S_MP_INVMOD_SLOW_C
22-
return s_mp_invmod_slow(a, b, c);
23-
#else
24-
return MP_VAL;
25-
#endif
19+
return MP_HAS(S_MP_INVMOD_SLOW)
20+
? s_mp_invmod_slow(a, b, c)
21+
: MP_VAL;
2622
}
2723
#endif

bn_mp_mul.c

+38-72
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,46 @@
66
/* high level multiplication (handles sign) */
77
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
88
{
9-
mp_err err;
10-
mp_sign neg;
11-
#ifdef BN_S_MP_BALANCE_MUL_C
12-
int len_b, len_a;
13-
#endif
14-
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
15-
#ifdef BN_S_MP_BALANCE_MUL_C
16-
len_a = a->used;
17-
len_b = b->used;
18-
19-
if (len_a == len_b) {
20-
goto GO_ON;
21-
}
22-
/*
23-
* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off.
24-
* The bigger one needs to be at least about one KARATSUBA_MUL_CUTOFF bigger
25-
* to make some sense, but it depends on architecture, OS, position of the
26-
* stars... so YMMV.
27-
* Using it to cut the input into slices small enough for fast_s_mp_mul_digs
28-
* was actually slower on the author's machine, but YMMV.
29-
*/
30-
if ((MP_MIN(len_a, len_b) < MP_KARATSUBA_MUL_CUTOFF)
31-
|| ((MP_MAX(len_a, len_b) / 2) < MP_KARATSUBA_MUL_CUTOFF)) {
32-
goto GO_ON;
33-
}
34-
/*
35-
* Not much effect was observed below a ratio of 1:2, but again: YMMV.
36-
*/
37-
if ((MP_MAX(len_a, len_b) / MP_MIN(len_a, len_b)) < 2) {
38-
goto GO_ON;
39-
}
40-
41-
err = s_mp_balance_mul(a,b,c);
42-
goto END;
43-
44-
GO_ON:
45-
#endif
9+
mp_err err;
10+
int min_len = MP_MIN(a->used, b->used),
11+
max_len = MP_MAX(a->used, b->used),
12+
digs = a->used + b->used + 1;
13+
mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
4614

47-
/* use Toom-Cook? */
48-
#ifdef BN_S_MP_TOOM_MUL_C
49-
if (MP_MIN(a->used, b->used) >= MP_TOOM_MUL_CUTOFF) {
15+
if (MP_HAS(S_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 MP_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 >= MP_KARATSUBA_MUL_CUTOFF) &&
24+
(max_len / 2 >= MP_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+
err = s_mp_balance_mul(a,b,c);
28+
} else if (MP_HAS(S_MP_TOOM_MUL) &&
29+
(min_len >= MP_TOOM_MUL_CUTOFF)) {
5030
err = s_mp_toom_mul(a, b, c);
51-
} else
52-
#endif
53-
#ifdef BN_S_MP_KARATSUBA_MUL_C
54-
/* use Karatsuba? */
55-
if (MP_MIN(a->used, b->used) >= MP_KARATSUBA_MUL_CUTOFF) {
56-
err = s_mp_karatsuba_mul(a, b, c);
57-
} else
58-
#endif
59-
{
60-
/* can we use the fast multiplier?
61-
*
62-
* The fast multiplier can be used if the output will
63-
* have less than MP_WARRAY digits and the number of
64-
* digits won't affect carry propagation
65-
*/
66-
int digs = a->used + b->used + 1;
67-
68-
#ifdef BN_S_MP_MUL_DIGS_FAST_C
69-
if ((digs < MP_WARRAY) &&
70-
(MP_MIN(a->used, b->used) <= MP_MAXFAST)) {
71-
err = s_mp_mul_digs_fast(a, b, c, digs);
72-
} else
73-
#endif
74-
{
75-
#ifdef BN_S_MP_MUL_DIGS_C
76-
err = s_mp_mul_digs(a, b, c, a->used + b->used + 1);
77-
#else
78-
err = MP_VAL;
79-
#endif
80-
}
81-
}
82-
END:
31+
} else if (MP_HAS(S_MP_KARATSUBA_MUL) &&
32+
(min_len >= MP_KARATSUBA_MUL_CUTOFF)) {
33+
err = s_mp_karatsuba_mul(a, b, c);
34+
} else if (MP_HAS(S_MP_MUL_DIGS_FAST) &&
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 < MP_WARRAY) &&
42+
(min_len <= MP_MAXFAST)) {
43+
err = s_mp_mul_digs_fast(a, b, c, digs);
44+
} else if (MP_HAS(S_MP_MUL_DIGS)) {
45+
err = s_mp_mul_digs(a, b, c, digs);
46+
} else {
47+
err = MP_VAL;
48+
}
8349
c->sign = (c->used > 0) ? neg : MP_ZPOS;
8450
return err;
8551
}

bn_mp_reduce.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,17 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
2626
if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) {
2727
goto CLEANUP;
2828
}
29-
} else {
30-
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
29+
} else if (MP_HAS(S_MP_MUL_HIGH_DIGS)) {
3130
if ((err = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
3231
goto CLEANUP;
3332
}
34-
#elif defined(BN_S_MP_MUL_HIGH_DIGS_FAST_C)
33+
} else if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)) {
3534
if ((err = s_mp_mul_high_digs_fast(&q, mu, &q, um)) != MP_OKAY) {
3635
goto CLEANUP;
3736
}
38-
#else
39-
{
40-
err = MP_VAL;
41-
goto CLEANUP;
42-
}
43-
#endif
37+
} else {
38+
err = MP_VAL;
39+
goto CLEANUP;
4440
}
4541

4642
/* q3 = q2 / b**(k+1) */

bn_mp_sqr.c

+14-28
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,21 @@
77
mp_err mp_sqr(const mp_int *a, mp_int *b)
88
{
99
mp_err err;
10-
11-
#ifdef BN_S_MP_TOOM_SQR_C
12-
/* use Toom-Cook? */
13-
if (a->used >= MP_TOOM_SQR_CUTOFF) {
10+
if (MP_HAS(S_MP_TOOM_SQR) && /* use Toom-Cook? */
11+
a->used >= MP_TOOM_SQR_CUTOFF) {
1412
err = s_mp_toom_sqr(a, b);
15-
/* Karatsuba? */
16-
} else
17-
#endif
18-
#ifdef BN_S_MP_KARATSUBA_SQR_C
19-
if (a->used >= MP_KARATSUBA_SQR_CUTOFF) {
20-
err = s_mp_karatsuba_sqr(a, b);
21-
} else
22-
#endif
23-
{
24-
#ifdef BN_S_MP_SQR_FAST_C
25-
/* can we use the fast comba multiplier? */
26-
if ((((a->used * 2) + 1) < MP_WARRAY) &&
27-
(a->used < (MP_MAXFAST / 2))) {
28-
err = s_mp_sqr_fast(a, b);
29-
} else
30-
#endif
31-
{
32-
#ifdef BN_S_MP_SQR_C
33-
err = s_mp_sqr(a, b);
34-
#else
35-
err = MP_VAL;
36-
#endif
37-
}
38-
}
13+
} else if (MP_HAS(S_MP_KARATSUBA_SQR) && /* Karatsuba? */
14+
a->used >= MP_KARATSUBA_SQR_CUTOFF) {
15+
err = s_mp_karatsuba_sqr(a, b);
16+
} else if (MP_HAS(S_MP_SQR_FAST) && /* can we use the fast comba multiplier? */
17+
(((a->used * 2) + 1) < MP_WARRAY) &&
18+
(a->used < (MP_MAXFAST / 2))) {
19+
err = s_mp_sqr_fast(a, b);
20+
} else if (MP_HAS(S_MP_SQR)) {
21+
err = s_mp_sqr(a, b);
22+
} else {
23+
err = MP_VAL;
24+
}
3925
b->sign = MP_ZPOS;
4026
return err;
4127
}

bn_s_mp_exptmod.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
#ifdef MP_LOW_MEM
77
# define TAB_SIZE 32
8+
# define MAX_WINSIZE 5
89
#else
910
# define TAB_SIZE 256
11+
# define MAX_WINSIZE 0
1012
#endif
1113

1214
mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
@@ -35,11 +37,7 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y
3537
winsize = 8;
3638
}
3739

38-
#ifdef MP_LOW_MEM
39-
if (winsize > 5) {
40-
winsize = 5;
41-
}
42-
#endif
40+
winsize = MAX_WINSIZE ? MP_MIN(MAX_WINSIZE, winsize) : winsize;
4341

4442
/* init M array */
4543
/* init first cell */

0 commit comments

Comments
 (0)