Merge pull request #289 from libtom/static-assert add MP_STATIC_ASSERT
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
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index 8b404e0..cf27ed9 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -6,10 +6,8 @@
/* shrink a bignum */
mp_err mp_shrink(mp_int *a)
{
- static int static_check[-(MP_PREC < MP_MIN_PREC)];
mp_digit *tmp;
int alloc = MP_MAX(MP_MIN_PREC, a->used);
- (void)static_check;
if (a->alloc != alloc) {
if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
(size_t)a->alloc * sizeof(mp_digit),
diff --git a/tommath_private.h b/tommath_private.h
index 67ac014..729392e 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -151,6 +151,9 @@ typedef private_mp_word mp_word;
#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
+/* Static assertion */
+#define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[-(!(cond))];
+
/* ---> Basic Manipulations <--- */
#define MP_IS_ZERO(a) ((a)->used == 0)
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
@@ -162,6 +165,8 @@ typedef private_mp_word mp_word;
/* 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)
+MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
+
/* random number source */
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);