Skip to content

Commit bf44351

Browse files
committed
add feature detection macros MP_DEFINED and MP_HAS
additionally, make bn_mp_mul private
1 parent 2033fb9 commit bf44351

File tree

7 files changed

+64
-99
lines changed

7 files changed

+64
-99
lines changed

bn_mp_invmod.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
1111
return MP_VAL;
1212
}
1313

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

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

bn_mp_mul.c

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,48 @@
66
/* high level multiplication (handles sign) */
77
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
88
{
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;
1313
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
4514

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) {
4930
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+
}
8350
c->sign = (c->used > 0) ? neg : MP_ZPOS;
8451
return res;
8552
}
8653
#endif
87-

bn_mp_reduce.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,15 @@ int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
2525
if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
2626
goto CLEANUP;
2727
}
28+
} else if (MP_HAS(S_MP_MUL_HIGH_DIGS) &&
29+
(res = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
30+
goto CLEANUP;
31+
} else if (MP_HAS(FAST_S_MP_MUL_HIGH_DIGS) &&
32+
(res = fast_s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
33+
goto CLEANUP;
2834
} else {
29-
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
30-
if ((res = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
31-
goto CLEANUP;
32-
}
33-
#elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
34-
if ((res = fast_s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
35-
goto CLEANUP;
36-
}
37-
#else
38-
{
39-
res = MP_VAL;
40-
goto CLEANUP;
41-
}
42-
#endif
35+
res = MP_VAL;
36+
goto CLEANUP;
4337
}
4438

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

bn_s_mp_exptmod.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, i
3434
winsize = 8;
3535
}
3636

37-
#ifdef MP_LOW_MEM
38-
if (winsize > 5) {
37+
if (MP_DEFINED(MP_LOW_MEM) && winsize > 5) {
3938
winsize = 5;
4039
}
41-
#endif
4240

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

demo/test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "shared.h"
22

3+
/* We can also test the private API here */
4+
#include "tommath_private.h"
5+
36
static int test_trivial_stuff(void)
47
{
58
mp_int a, b, c, d;
@@ -1624,7 +1627,7 @@ static int test_mp_balance_mul(void)
16241627
goto LTM_ERR;
16251628
}
16261629

1627-
if ((e = mp_mul(&a, &b, &c)) != MP_OKAY) {
1630+
if ((e = mp_balance_mul(&a, &b, &c)) != MP_OKAY) {
16281631
goto LTM_ERR;
16291632
}
16301633

tommath.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ int mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
335335

336336
/* c = a * b */
337337
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
338-
int mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
339338

340339
/* b = a*a */
341340
int mp_sqr(const mp_int *a, mp_int *b);

tommath_private.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ extern void *XCALLOC(size_t nmemb, size_t size);
3333
extern void XFREE(void *mem, size_t size);
3434
#endif
3535

36+
/* feature detection macro */
37+
#define MP_HAS(x) MP_DEFINED(BN_##x##_C)
38+
#define MP_DEFINED(x) _MP_DEFINED1(x)
39+
#define _MP_DEFINED1(x) _MP_DEFINED2(_MP_DEFINED_TEST##x)
40+
#define _MP_DEFINED2(x) _MP_DEFINED3(x 1, 0)
41+
#define _MP_DEFINED3(x, y, ...) y
42+
#define _MP_DEFINED_TEST ,
43+
3644
/* ---> Basic Manipulations <--- */
3745
#define IS_ZERO(a) ((a)->used == 0)
3846
#define IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
@@ -48,6 +56,7 @@ int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int dig
4856
int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
4957
int fast_s_mp_sqr(const mp_int *a, mp_int *b);
5058
int s_mp_sqr(const mp_int *a, mp_int *b);
59+
int mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
5160
int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
5261
int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
5362
int mp_karatsuba_sqr(const mp_int *a, mp_int *b);

0 commit comments

Comments
 (0)