Commit 5302b907ffe8df23ba9b5fec7f2b6d62d4e12081

Daniel Mendler 2019-06-06T21:06:06

use MP_SIZEOF_BITS

diff --git a/demo/test.c b/demo/test.c
index 748be4b..41e0394 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -726,7 +726,7 @@ static int test_mp_get_ul(void)
       return EXIT_FAILURE;
    }
 
-   for (i = 0; i < ((int)(sizeof(unsigned long)*CHAR_BIT) - 1); ++i) {
+   for (i = 0; i < ((int)MP_SIZEOF_BITS(unsigned long) - 1); ++i) {
       t = (1UL << (i+1)) - 1;
       if (!t)
          t = ~0UL;
@@ -759,7 +759,7 @@ static int test_mp_get_u64(void)
       return EXIT_FAILURE;
    }
 
-   for (i = 0; i < ((int)(sizeof(unsigned long long)*CHAR_BIT) - 1); ++i) {
+   for (i = 0; i < (int)(MP_SIZEOF_BITS(unsigned long long) - 1); ++i) {
       r = (1ULL << (i+1)) - 1;
       if (!r)
          r = ~0ULL;
diff --git a/tommath_private.h b/tommath_private.h
index b08fbd7..f37b541 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -169,7 +169,7 @@ typedef private_mp_word mp_word;
 #endif
 
 /* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
-#define MP_MIN_PREC ((((CHAR_BIT * (int)sizeof(long long)) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
+#define MP_MIN_PREC ((((int)MP_SIZEOF_BITS(long long) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
 
 MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
 
@@ -238,8 +238,8 @@ MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
         int i = 0;                                                                     \
         while (b != 0u) {                                                              \
             a->dp[i++] = ((mp_digit)b & MP_MASK);                                      \
-            if ((sizeof(type) * CHAR_BIT) <= MP_DIGIT_BIT) { break; }                  \
-            b >>= (((sizeof(type) * CHAR_BIT) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);    \
+            if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; }                       \
+            b >>= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);         \
         }                                                                              \
         a->used = i;                                                                   \
         a->sign = MP_ZPOS;                                                             \
@@ -267,12 +267,12 @@ MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
 #define MP_GET_MAG(type, name)                                                         \
     type name(const mp_int* a)                                                         \
     {                                                                                  \
-        unsigned i = MP_MIN((unsigned)a->used, (unsigned)(((sizeof(type) * CHAR_BIT) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
+        unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
         type res = 0u;                                                                 \
         while (i --> 0u) {                                                             \
-            res <<= (((sizeof(type) * CHAR_BIT) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);  \
+            res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);       \
             res |= (type)a->dp[i];                                                     \
-            if ((sizeof(type) * CHAR_BIT) <= MP_DIGIT_BIT) { break; }                  \
+            if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; }                       \
         }                                                                              \
         return res;                                                                    \
     }