Commit a36374578f855a26c3075c875cbf7bd9fc558b9a

Steffen Jaeckel 2019-06-06T11:48:42

change macros to contain types

diff --git a/bn_mp_get_i32.c b/bn_mp_get_i32.c
index 4dbfb32..4227a70 100644
--- a/bn_mp_get_i32.c
+++ b/bn_mp_get_i32.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_GET_SIGNED(mp_get_i32, mp_get_mag32, 32)
+MP_GET_SIGNED(int32_t, mp_get_i32, mp_get_mag32)
 #endif
diff --git a/bn_mp_get_i64.c b/bn_mp_get_i64.c
index c4c7362..cdc2fee 100644
--- a/bn_mp_get_i64.c
+++ b/bn_mp_get_i64.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_GET_SIGNED(mp_get_i64, mp_get_mag64, 64)
+MP_GET_SIGNED(int64_t, mp_get_i64, mp_get_mag64)
 #endif
diff --git a/bn_mp_get_mag32.c b/bn_mp_get_mag32.c
index 6877d01..46e8b29 100644
--- a/bn_mp_get_mag32.c
+++ b/bn_mp_get_mag32.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_GET_MAG(mp_get_mag32, 32)
+MP_GET_MAG(uint32_t, mp_get_mag32)
 #endif
diff --git a/bn_mp_get_mag64.c b/bn_mp_get_mag64.c
index 0e05c7f..6ff5e5d 100644
--- a/bn_mp_get_mag64.c
+++ b/bn_mp_get_mag64.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_GET_MAG(mp_get_mag64, 64)
+MP_GET_MAG(uint64_t, mp_get_mag64)
 #endif
diff --git a/bn_mp_init_i64.c b/bn_mp_init_i64.c
index 112a6d9..2fa1516 100644
--- a/bn_mp_init_i64.c
+++ b/bn_mp_init_i64.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
+MP_INIT_INT(mp_init_i64, mp_set_i64, int64_t)
 #endif
diff --git a/bn_mp_set_i32.c b/bn_mp_set_i32.c
index a181156..abf1f94 100644
--- a/bn_mp_set_i32.c
+++ b/bn_mp_set_i32.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_SET_SIGNED(mp_set_i32, mp_set_u32, 32)
+MP_SET_SIGNED(mp_set_i32, mp_set_u32, int32_t)
 #endif
diff --git a/bn_mp_set_i64.c b/bn_mp_set_i64.c
index 0b0fadc..6772dfe 100644
--- a/bn_mp_set_i64.c
+++ b/bn_mp_set_i64.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_SET_SIGNED(mp_set_i64, mp_set_u64, 64)
+MP_SET_SIGNED(mp_set_i64, mp_set_u64, int64_t)
 #endif
diff --git a/bn_mp_set_u32.c b/bn_mp_set_u32.c
index 8edbd2e..18ba5e1 100644
--- a/bn_mp_set_u32.c
+++ b/bn_mp_set_u32.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_SET_UNSIGNED(mp_set_u32, 32)
+MP_SET_UNSIGNED(mp_set_u32, uint32_t)
 #endif
diff --git a/bn_mp_set_u64.c b/bn_mp_set_u64.c
index f5beb76..88fab6c 100644
--- a/bn_mp_set_u64.c
+++ b/bn_mp_set_u64.c
@@ -3,5 +3,5 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-MP_SET_UNSIGNED(mp_set_u64, 64)
+MP_SET_UNSIGNED(mp_set_u64, uint64_t)
 #endif
diff --git a/tommath_private.h b/tommath_private.h
index 6495ce1..b08fbd7 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -232,26 +232,28 @@ MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
 MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
 
 /* code-generating macros */
-#define MP_SET_UNSIGNED(name, w)                                        \
-    void name(mp_int * a, uint##w##_t b)                                \
-    {                                                                   \
-        int i = 0;                                                      \
-        while (b != 0u) {                                               \
-            a->dp[i++] = ((mp_digit)b & MP_MASK);                       \
-            if (w <= MP_DIGIT_BIT) { break; }                           \
-            b >>= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);             \
-        }                                                               \
-        a->used = i;                                                    \
-        a->sign = MP_ZPOS;                                              \
-        MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);            \
+#define MP_SET_UNSIGNED(name, type)                                                    \
+    void name(mp_int * a, type b)                                                      \
+    {                                                                                  \
+        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);    \
+        }                                                                              \
+        a->used = i;                                                                   \
+        a->sign = MP_ZPOS;                                                             \
+        MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);                           \
     }
-#define MP_SET_SIGNED(name, uname, w)                                   \
-    void name(mp_int * a, int##w##_t b)                                 \
-    {                                                                   \
-        uname(a, (b < 0) ? -(uint##w##_t)b : (uint##w##_t)b);           \
-        if (b < 0) { a->sign = MP_NEG; }                                \
+
+#define MP_SET_SIGNED(name, uname, type)                 \
+    void name(mp_int * a, type b)                        \
+    {                                                    \
+        uname(a, (b < 0) ? -(u##type)b : (u##type)b);    \
+        if (b < 0) { a->sign = MP_NEG; }                 \
     }
-#define MP_INIT_INT(name , set, type)                     \
+
+#define MP_INIT_INT(name , set, type)                    \
     mp_err name(mp_int * a, type b)                      \
     {                                                    \
         mp_err err;                                      \
@@ -261,23 +263,25 @@ MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
         set(a, b);                                       \
         return MP_OKAY;                                  \
     }
-#define MP_GET_MAG(name, w)                                             \
-    uint##w##_t name(const mp_int* a)                                   \
-    {                                                                   \
-        unsigned i = MP_MIN((unsigned)a->used, (unsigned)((w + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
-        uint##w##_t res = 0u;                                           \
-        while (i --> 0u) {                                              \
-            res <<= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);           \
-            res |= (uint##w##_t)a->dp[i];                               \
-            if (w <= MP_DIGIT_BIT) { break; }                           \
-        }                                                               \
-        return res;                                                     \
+
+#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)); \
+        type res = 0u;                                                                 \
+        while (i --> 0u) {                                                             \
+            res <<= (((sizeof(type) * CHAR_BIT) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);  \
+            res |= (type)a->dp[i];                                                     \
+            if ((sizeof(type) * CHAR_BIT) <= MP_DIGIT_BIT) { break; }                  \
+        }                                                                              \
+        return res;                                                                    \
     }
-#define MP_GET_SIGNED(name, mag, w)                                     \
-    int##w##_t name(const mp_int* a)                                    \
-    {                                                                   \
-        uint64_t res = mag(a);                                          \
-        return (a->sign == MP_NEG) ? (int##w##_t)-res : (int##w##_t)res;\
+
+#define MP_GET_SIGNED(type, name, mag)                        \
+    type name(const mp_int* a)                                \
+    {                                                         \
+        uint64_t res = mag(a);                                \
+        return (a->sign == MP_NEG) ? (type)-res : (type)res;  \
     }
 
 #endif