Commit 556219aa5bdacb10ab40b77735727eebec090c14

Francois Perrad 2019-05-09T22:07:13

refactor with new private macro MP_SIZEOF_BITS CHAR_BIT is no longer directly used

diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
index 04bea5c..4441d63 100644
--- a/bn_mp_get_long.c
+++ b/bn_mp_get_long.c
@@ -14,7 +14,7 @@ unsigned long mp_get_long(const mp_int *a)
    }
 
    /* get number of digits of the lsb we have to read */
-   i = MP_MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long)) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)) - 1;
+   i = MP_MIN(a->used, (((int)MP_SIZEOF_BITS(unsigned long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)) - 1;
 
    /* get most significant digit of result */
    res = (unsigned long)a->dp[i];
diff --git a/bn_mp_get_long_long.c b/bn_mp_get_long_long.c
index 88c5e0c..91dde4c 100644
--- a/bn_mp_get_long_long.c
+++ b/bn_mp_get_long_long.c
@@ -14,7 +14,7 @@ unsigned long long mp_get_long_long(const mp_int *a)
    }
 
    /* get number of digits of the lsb we have to read */
-   i = MP_MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long long)) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)) - 1;
+   i = MP_MIN(a->used, (((int)MP_SIZEOF_BITS(unsigned long long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)) - 1;
 
    /* get most significant digit of result */
    res = (unsigned long long)a->dp[i];
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index bf24cbe..41ff857 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -19,7 +19,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
    if ((digs < (int)MP_WARRAY) &&
        (x->used <= (int)MP_WARRAY) &&
        (n->used <
-        (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
+        (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
       return s_mp_montgomery_reduce_fast(x, n, rho);
    }
 
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index e206152..bcdba55 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -67,7 +67,7 @@ GO_ON:
 #ifdef BN_S_MP_MUL_DIGS_FAST_C
          if ((digs < (int)MP_WARRAY) &&
              (MP_MIN(a->used, b->used) <=
-              (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
+              (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
             res = s_mp_mul_digs_fast(a, b, c, digs);
          } else
 #endif
diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index 3a371e2..7c93020 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -287,11 +287,11 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result)
           * One 8-bit digit is too small, so concatenate two if the size of
           * unsigned int allows for it.
           */
-         if (((sizeof(unsigned int) * CHAR_BIT)/2) >= (sizeof(mp_digit) * CHAR_BIT)) {
+         if ((MP_SIZEOF_BITS(unsigned int)/2) >= MP_SIZEOF_BITS(mp_digit)) {
             if ((err = mp_rand(&b, 1)) != MP_OKAY) {
                goto LBL_B;
             }
-            fips_rand <<= CHAR_BIT * sizeof(mp_digit);
+            fips_rand <<= MP_SIZEOF_BITS(mp_digit);
             fips_rand |= (unsigned int) b.dp[0];
             fips_rand &= mask;
          }
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index da12929..9c3746a 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -25,7 +25,7 @@ int mp_sqr(const mp_int *a, mp_int *b)
          /* can we use the fast comba multiplier? */
          if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
              (a->used <
-              (int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT)) - 1u)))) {
+              (int)(1u << ((MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)) - 1u)))) {
             res = s_mp_sqr_fast(a, b);
          } else
 #endif
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index be94148..4e3ae02 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -55,13 +55,13 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
 
       /* subtract first digit */
       *tmpc    = *tmpa++ - b;
-      mu       = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
+      mu       = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
       *tmpc++ &= MP_MASK;
 
       /* handle rest of the digits */
       for (ix = 1; ix < a->used; ix++) {
          *tmpc    = *tmpa++ - mu;
-         mu       = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
+         mu       = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
          *tmpc++ &= MP_MASK;
       }
    }
diff --git a/bn_s_mp_exptmod_fast.c b/bn_s_mp_exptmod_fast.c
index af6ae57..8e40d0b 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 << ((CHAR_BIT * sizeof(mp_word)) - (2 * MP_DIGIT_BIT))))) {
+          (P->used < (1 << (MP_SIZEOF_BITS(mp_word) - (2 * MP_DIGIT_BIT))))) {
          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 9e925c7..7ed0406 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -18,7 +18,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 << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
+        (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
       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 416ac8c..3aff4c0 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 << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
+       && (MP_MIN(a->used, b->used) < (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
       return s_mp_mul_high_digs_fast(a, b, c, digs);
    }
 #endif
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index 19271a8..2bf0679 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -41,7 +41,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
           * if a carry does occur it will propagate all the way to the
           * MSB.  As a result a single shift is enough to get the carry
           */
-         u = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
+         u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
 
          /* Clear carry from T[i] */
          *tmpc++ &= MP_MASK;
@@ -53,7 +53,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
          *tmpc = *tmpa++ - u;
 
          /* U = carry bit of T[i] */
-         u = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
+         u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
 
          /* Clear carry from T[i] */
          *tmpc++ &= MP_MASK;
diff --git a/tommath_private.h b/tommath_private.h
index 1a28a09..8947822 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -66,6 +66,8 @@ extern void MP_FREE(void *mem, size_t size);
 #define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
 #define MP_IS_ODD(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
 
+#define MP_SIZEOF_BITS(type)    (CHAR_BIT * sizeof(type))
+
 /* random number source */
 extern int (*s_rand_source)(void *, size_t);
 
@@ -104,14 +106,14 @@ extern const size_t mp_s_rmap_reverse_sz;
 int func_name (mp_int * a, type b)                       \
 {                                                        \
    int x = 0;                                            \
-   int new_size = (((CHAR_BIT * sizeof(type)) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT; \
+   int new_size = ((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT; \
    int res = mp_grow(a, new_size);                       \
    if (res == MP_OKAY) {                                 \
      mp_zero(a);                                         \
      while (b != 0u) {                                   \
         a->dp[x++] = ((mp_digit)b & MP_MASK);            \
-        if ((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) { break; } \
-        b >>= (((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
+        if (MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) { break; } \
+        b >>= ((MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
      }                                                   \
      a->used = x;                                        \
    }                                                     \