Commit 7d4e8363e09517e6583c971a8af312d564792f25

Francois Perrad 2019-05-08T19:43:21

refactor with new private macro MP_MAXFAST

diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index 41ff857..c379675 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -18,8 +18,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
    digs = (n->used * 2) + 1;
    if ((digs < (int)MP_WARRAY) &&
        (x->used <= (int)MP_WARRAY) &&
-       (n->used <
-        (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
+       (n->used < MP_MAXFAST)) {
       return s_mp_montgomery_reduce_fast(x, n, rho);
    }
 
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index bcdba55..68d8fb8 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -66,8 +66,7 @@ GO_ON:
 
 #ifdef BN_S_MP_MUL_DIGS_FAST_C
          if ((digs < (int)MP_WARRAY) &&
-             (MP_MIN(a->used, b->used) <=
-              (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
+             (MP_MIN(a->used, b->used) <= MP_MAXFAST)) {
             res = s_mp_mul_digs_fast(a, b, c, digs);
          } else
 #endif
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index 9c3746a..5b93eab 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -24,8 +24,7 @@ int mp_sqr(const mp_int *a, mp_int *b)
 #ifdef BN_S_MP_SQR_FAST_C
          /* can we use the fast comba multiplier? */
          if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
-             (a->used <
-              (int)(1u << ((MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)) - 1u)))) {
+             (a->used < (MP_MAXFAST / 2))) {
             res = s_mp_sqr_fast(a, b);
          } else
 #endif
diff --git a/bn_s_mp_exptmod_fast.c b/bn_s_mp_exptmod_fast.c
index 8e40d0b..d6373ef 100644
--- a/bn_s_mp_exptmod_fast.c
+++ b/bn_s_mp_exptmod_fast.c
@@ -85,7 +85,7 @@ int s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int 
       /* automatically pick the comba one if available (saves quite a few calls/ifs) */
 #ifdef BN_S_MP_MONTGOMERY_REDUCE_FAST_C
       if ((((P->used * 2) + 1) < (int)MP_WARRAY) &&
-          (P->used < (1 << (MP_SIZEOF_BITS(mp_word) - (2 * MP_DIGIT_BIT))))) {
+          (P->used < MP_MAXFAST)) {
          redux = s_mp_montgomery_reduce_fast;
       } else
 #endif
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index 7ed0406..87b785c 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -17,8 +17,7 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 
    /* can we use the fast multiplier? */
    if ((digs < (int)MP_WARRAY) &&
-       (MP_MIN(a->used, b->used) <
-        (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
+       (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
       return s_mp_mul_digs_fast(a, b, c, digs);
    }
 
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index 3aff4c0..14b889e 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -17,7 +17,7 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
    /* can we use the fast multiplier? */
 #ifdef BN_S_MP_MUL_HIGH_DIGS_FAST_C
    if (((a->used + b->used + 1) < (int)MP_WARRAY)
-       && (MP_MIN(a->used, b->used) < (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
+       && (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
       return s_mp_mul_high_digs_fast(a, b, c, digs);
    }
 #endif
diff --git a/tommath_private.h b/tommath_private.h
index 8947822..fc836f2 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -67,6 +67,7 @@ extern void MP_FREE(void *mem, size_t size);
 #define MP_IS_ODD(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
 
 #define MP_SIZEOF_BITS(type)    (CHAR_BIT * sizeof(type))
+#define MP_MAXFAST              (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
 
 /* random number source */
 extern int (*s_rand_source)(void *, size_t);