From e70b0460ea2b6bec55c0bcda34181a1b28b9a1e4 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 17 Oct 2019 15:15:24 +0000 Subject: Put back MP_CALLOC, since it's going to stay in libtommath --- libtommath/bn_deprecated.c | 4 ++-- libtommath/bn_mp_init.c | 3 +-- libtommath/bn_mp_init_size.c | 3 +-- libtommath/bn_mp_log_u32.c | 10 +++++----- libtommath/bn_mp_root_u32.c | 4 ++-- libtommath/bn_mp_set_double.c | 6 +++--- libtommath/tommath.h | 10 +++------- libtommath/tommath_private.h | 2 ++ 8 files changed, 19 insertions(+), 23 deletions(-) diff --git a/libtommath/bn_deprecated.c b/libtommath/bn_deprecated.c index 2056b20..a4004f6 100644 --- a/libtommath/bn_deprecated.c +++ b/libtommath/bn_deprecated.c @@ -219,7 +219,7 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) { return MP_VAL; } - return mp_root_u32(a, (uint32_t)b, c); + return mp_root_u32(a, (unsigned int)b, c); } #endif #ifdef BN_MP_N_ROOT_C @@ -228,7 +228,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) { return MP_VAL; } - return mp_root_u32(a, (uint32_t)b, c); + return mp_root_u32(a, (unsigned int)b, c); } #endif #ifdef BN_MP_UNSIGNED_BIN_SIZE_C diff --git a/libtommath/bn_mp_init.c b/libtommath/bn_mp_init.c index a4c9175..2eb7924 100644 --- a/libtommath/bn_mp_init.c +++ b/libtommath/bn_mp_init.c @@ -7,8 +7,7 @@ mp_err mp_init(mp_int *a) { /* allocate memory required and clear it */ - a->dp = (mp_digit *) MP_MALLOC((size_t)MP_PREC * sizeof(mp_digit)); - MP_ZERO_DIGITS(a->dp, MP_PREC); + a->dp = (mp_digit *) MP_CALLOC((size_t)MP_PREC, sizeof(mp_digit)); if (a->dp == NULL) { return MP_MEM; } diff --git a/libtommath/bn_mp_init_size.c b/libtommath/bn_mp_init_size.c index 215de5a..d622687 100644 --- a/libtommath/bn_mp_init_size.c +++ b/libtommath/bn_mp_init_size.c @@ -9,8 +9,7 @@ mp_err mp_init_size(mp_int *a, int size) size = MP_MAX(MP_MIN_PREC, size); /* alloc mem */ - a->dp = (mp_digit *) MP_MALLOC((size_t)size * sizeof(mp_digit)); - MP_ZERO_DIGITS(a->dp, size); + a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit)); if (a->dp == NULL) { return MP_MEM; } diff --git a/libtommath/bn_mp_log_u32.c b/libtommath/bn_mp_log_u32.c index ddb78cf..f507b1d 100644 --- a/libtommath/bn_mp_log_u32.c +++ b/libtommath/bn_mp_log_u32.c @@ -70,11 +70,11 @@ static mp_digit s_digit_ilogb(mp_digit base, mp_digit n) as is the output of mp_bitcount. With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only! */ -mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) +mp_err mp_log_u32(const mp_int *a, unsigned int base, unsigned int *c) { mp_err err; mp_ord cmp; - uint32_t high, low, mid; + unsigned int high, low, mid; mp_int bracket_low, bracket_high, bracket_mid, t, bi_base; err = MP_OKAY; @@ -98,12 +98,12 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) base >>= 1; } bit_count = mp_count_bits(a) - 1; - *c = (uint32_t)(bit_count/y); + *c = (unsigned int)(bit_count/y); return MP_OKAY; } if (a->used == 1) { - *c = (uint32_t)s_digit_ilogb(base, a->dp[0]); + *c = (unsigned int)s_digit_ilogb(base, a->dp[0]); return err; } @@ -146,7 +146,7 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) while ((high - low) > 1u) { mid = (high + low) >> 1; - if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) { + if ((err = mp_expt_u32(&bi_base, mid - low, &t)) != MP_OKAY) { goto LBL_ERR; } if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) { diff --git a/libtommath/bn_mp_root_u32.c b/libtommath/bn_mp_root_u32.c index ba65549..b60cf26 100644 --- a/libtommath/bn_mp_root_u32.c +++ b/libtommath/bn_mp_root_u32.c @@ -12,7 +12,7 @@ * which will find the root in log(N) time where * each step involves a fair bit. */ -mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) +mp_err mp_root_u32(const mp_int *a, unsigned int b, mp_int *c) { mp_int t1, t2, t3, a_; mp_ord cmp; @@ -40,7 +40,7 @@ mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) log_2(n) because the bit-length of the "n" is measured with an int and hence the root is always < 2 (two). */ - if (b > (uint32_t)(INT_MAX/2)) { + if (b > (unsigned int)(INT_MAX/2)) { mp_set(c, 1uL); c->sign = a->sign; err = MP_OKAY; diff --git a/libtommath/bn_mp_set_double.c b/libtommath/bn_mp_set_double.c index fea5691..a42fc70 100644 --- a/libtommath/bn_mp_set_double.c +++ b/libtommath/bn_mp_set_double.c @@ -6,17 +6,17 @@ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) mp_err mp_set_double(mp_int *a, double b) { - unsigned long long frac; + uint64_t frac; int exp; mp_err err; union { double dbl; - unsigned long long bits; + uint64_t bits; } cast; cast.dbl = b; exp = (int)((unsigned)(cast.bits >> 52) & 0x7FFu); - frac = (cast.bits & ((1uLL << 52) - 1)) | (1uLL << 52); + frac = (cast.bits & ((1uLL << 52) - 1uLL)) | (1uLL << 52); if (exp == 0x7FF) { /* +-inf, NaN */ return MP_VAL; diff --git a/libtommath/tommath.h b/libtommath/tommath.h index e9e59be..3a9e93c 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -558,9 +558,7 @@ mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; * * returns error if a < 0 and b is even */ -#ifndef MP_NO_STDINT -mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR; -#endif +mp_err mp_root_u32(const mp_int *a, unsigned int b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; @@ -723,12 +721,10 @@ MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int siz mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR; /* Integer logarithm to integer base */ -#ifndef MP_NO_STDINT -mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR; +mp_err mp_log_u32(const mp_int *a, unsigned int base, unsigned int *c) MP_WUR; /* c = a**b */ -mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR; -#endif +mp_err mp_expt_u32(const mp_int *a, unsigned int b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index a3b4679..9276543 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -208,7 +208,9 @@ MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result); /* TODO: jenkins prng is not thread safe as of now */ MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR; +#ifndef MP_NO_STDINT MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed); +#endif extern MP_PRIVATE const char *const mp_s_rmap; extern MP_PRIVATE const unsigned char mp_s_rmap_reverse[]; -- cgit v0.12