Merge pull request #238 from fperrad/20190508_macros new macros
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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..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 << ((CHAR_BIT * sizeof(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 e206152..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 << ((CHAR_BIT * sizeof(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_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..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 << (((CHAR_BIT * sizeof(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_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..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 << ((CHAR_BIT * sizeof(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 9e925c7..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 << ((CHAR_BIT * sizeof(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 416ac8c..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 << ((CHAR_BIT * sizeof(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/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..fc836f2 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -66,6 +66,9 @@ 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))
+#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);
@@ -104,14 +107,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; \
} \