From 99df8f7b25c7013ac4bee8807986074154b41a03 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 19 Oct 2019 18:33:39 +0200 Subject: [PATCH 1/4] mp_log_u32: remove obsolete todo --- mp_log_u32.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/mp_log_u32.c b/mp_log_u32.c index fb9dd5a0f..0523f7beb 100644 --- a/mp_log_u32.c +++ b/mp_log_u32.c @@ -66,10 +66,6 @@ static mp_digit s_digit_ilogb(mp_digit base, mp_digit n) return ret; } -/* TODO: output could be "int" because the output of mp_radix_size is int, too, - 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 err; From 5c6391dd7cec7d91036cbff23a12b936dc39329a Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 19 Oct 2019 18:35:57 +0200 Subject: [PATCH 2/4] remove some MP_8BIT remnants --- mp_prime_is_prime.c | 5 ----- mp_prime_strong_lucas_selfridge.c | 1 - tommath.h | 12 ------------ tommath_private.h | 8 +------- 4 files changed, 1 insertion(+), 25 deletions(-) diff --git a/mp_prime_is_prime.c b/mp_prime_is_prime.c index 75d44c5f2..1a61bb6a1 100644 --- a/mp_prime_is_prime.c +++ b/mp_prime_is_prime.c @@ -100,11 +100,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) */ #ifndef LTM_USE_ONLY_MR if (t >= 0) { - /* - * Use a Frobenius-Underwood test instead of the Lucas-Selfridge test for - * MP_8BIT (It is unknown if the Lucas-Selfridge test works with 16-bit - * integers but the necesssary analysis is on the todo-list). - */ #ifdef LTM_USE_FROBENIUS_TEST err = mp_prime_frobenius_underwood(a, &res); if ((err != MP_OKAY) && (err != MP_ITER)) { diff --git a/mp_prime_strong_lucas_selfridge.c b/mp_prime_strong_lucas_selfridge.c index 693433627..895a48122 100644 --- a/mp_prime_strong_lucas_selfridge.c +++ b/mp_prime_strong_lucas_selfridge.c @@ -52,7 +52,6 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) { /* CZ TODO: choose better variable names! */ mp_int Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz; - /* CZ TODO: Some of them need the full 32 bit, hence the (temporary) exclusion of MP_8BIT */ int32_t D, Ds, J, sign, P, Q, r, s, u, Nbits; mp_err err; mp_bool oddness; diff --git a/tommath.h b/tommath.h index 29e9b207a..0aac377ce 100644 --- a/tommath.h +++ b/tommath.h @@ -6,23 +6,11 @@ #include #include -#ifdef MP_8BIT -# error "Support of 8-bit architectures has been dropped in this version of LTM." -#endif - #ifndef MP_NO_FILE # include #endif -#ifdef MP_8BIT -# ifdef _MSC_VER -# pragma message("8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version.") -# else -# warning "8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version." -# endif -#endif - #ifdef __cplusplus extern "C" { #endif diff --git a/tommath_private.h b/tommath_private.h index c44c72adb..45e0930f5 100644 --- a/tommath_private.h +++ b/tommath_private.h @@ -168,8 +168,6 @@ typedef private_mp_word mp_word; #ifndef MP_PREC # ifndef MP_LOW_MEM # define MP_PREC 32 /* default digits of precision */ -# elif defined(MP_8BIT) -# define MP_PREC 16 /* default digits of precision */ # else # define MP_PREC 8 /* default digits of precision */ # endif @@ -219,11 +217,7 @@ extern MP_PRIVATE const uint8_t s_mp_rmap_reverse[]; extern MP_PRIVATE const mp_digit s_mp_prime_tab[]; /* number of primes */ -#ifdef MP_8BIT -# define MP_PRIME_TAB_SIZE 31 -#else -# define MP_PRIME_TAB_SIZE 256 -#endif +#define MP_PRIME_TAB_SIZE 256 #define MP_GET_ENDIANNESS(x) \ do{\ From f0c83aea6c397115f3ec3c28c4c1b35eeee5c80e Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 19 Oct 2019 19:25:55 +0200 Subject: [PATCH 3/4] split mp_log_u32 for more configurability --- libtommath_VS2008.vcproj | 8 +++ makefile | 7 +- makefile.mingw | 7 +- makefile.msvc | 7 +- makefile.shared | 7 +- makefile.unix | 7 +- mp_log_u32.c | 145 ++------------------------------------- s_mp_log.c | 82 ++++++++++++++++++++++ s_mp_log_d.c | 68 ++++++++++++++++++ tommath_class.h | 30 +++++--- tommath_private.h | 2 + 11 files changed, 206 insertions(+), 164 deletions(-) create mode 100644 s_mp_log.c create mode 100644 s_mp_log_d.c diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj index ccbd1cc7f..d59f71cbd 100644 --- a/libtommath_VS2008.vcproj +++ b/libtommath_VS2008.vcproj @@ -880,6 +880,14 @@ RelativePath="s_mp_karatsuba_sqr.c" > + + + + diff --git a/makefile b/makefile index 117c84664..fb9c3910c 100644 --- a/makefile +++ b/makefile @@ -46,9 +46,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \ mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_exptmod.o \ s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \ -s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \ -s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \ -s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o +s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \ +s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \ +s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \ +s_mp_toom_mul.o s_mp_toom_sqr.o #END_INS diff --git a/makefile.mingw b/makefile.mingw index 1a222f02b..c3a680fd8 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -49,9 +49,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \ mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_exptmod.o \ s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \ -s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \ -s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \ -s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o +s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \ +s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \ +s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \ +s_mp_toom_mul.o s_mp_toom_sqr.o HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) diff --git a/makefile.msvc b/makefile.msvc index 35215b25b..84dce858b 100644 --- a/makefile.msvc +++ b/makefile.msvc @@ -41,9 +41,10 @@ mp_set_ll.obj mp_set_u32.obj mp_set_u64.obj mp_set_ul.obj mp_set_ull.obj mp_shri mp_sqrmod.obj mp_sqrt.obj mp_sqrtmod_prime.obj mp_sub.obj mp_sub_d.obj mp_submod.obj mp_to_radix.obj mp_to_sbin.obj \ mp_to_ubin.obj mp_ubin_size.obj mp_unpack.obj mp_xor.obj mp_zero.obj s_mp_add.obj s_mp_balance_mul.obj s_mp_exptmod.obj \ s_mp_exptmod_fast.obj s_mp_get_bit.obj s_mp_invmod_fast.obj s_mp_invmod_slow.obj s_mp_karatsuba_mul.obj \ -s_mp_karatsuba_sqr.obj s_mp_montgomery_reduce_fast.obj s_mp_mul_digs.obj s_mp_mul_digs_fast.obj \ -s_mp_mul_high_digs.obj s_mp_mul_high_digs_fast.obj s_mp_prime_is_divisible.obj s_mp_rand_jenkins.obj \ -s_mp_rand_platform.obj s_mp_reverse.obj s_mp_sqr.obj s_mp_sqr_fast.obj s_mp_sub.obj s_mp_toom_mul.obj s_mp_toom_sqr.obj +s_mp_karatsuba_sqr.obj s_mp_log.obj s_mp_log_d.obj s_mp_montgomery_reduce_fast.obj s_mp_mul_digs.obj \ +s_mp_mul_digs_fast.obj s_mp_mul_high_digs.obj s_mp_mul_high_digs_fast.obj s_mp_prime_is_divisible.obj \ +s_mp_rand_jenkins.obj s_mp_rand_platform.obj s_mp_reverse.obj s_mp_sqr.obj s_mp_sqr_fast.obj s_mp_sub.obj \ +s_mp_toom_mul.obj s_mp_toom_sqr.obj HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) diff --git a/makefile.shared b/makefile.shared index 4bfa0192e..336d648d9 100644 --- a/makefile.shared +++ b/makefile.shared @@ -43,9 +43,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \ mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_exptmod.o \ s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \ -s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \ -s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \ -s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o +s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \ +s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \ +s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \ +s_mp_toom_mul.o s_mp_toom_sqr.o #END_INS diff --git a/makefile.unix b/makefile.unix index 4b7c49f31..6cad21092 100644 --- a/makefile.unix +++ b/makefile.unix @@ -50,9 +50,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \ mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_exptmod.o \ s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \ -s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \ -s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \ -s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o +s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \ +s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \ +s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \ +s_mp_toom_mul.o s_mp_toom_sqr.o HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) diff --git a/mp_log_u32.c b/mp_log_u32.c index 0523f7beb..43748e533 100644 --- a/mp_log_u32.c +++ b/mp_log_u32.c @@ -3,78 +3,8 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* Compute log_{base}(a) */ -static mp_word s_pow(mp_word base, mp_word exponent) -{ - mp_word result = 1uLL; - while (exponent != 0u) { - if ((exponent & 1u) == 1u) { - result *= base; - } - exponent >>= 1; - base *= base; - } - - return result; -} - -static mp_digit s_digit_ilogb(mp_digit base, mp_digit n) -{ - mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N; - mp_digit ret, high = 1uL, low = 0uL, mid; - - if (n < base) { - return 0uL; - } - if (n == base) { - return 1uL; - } - - bracket_high = (mp_word) base ; - N = (mp_word) n; - - while (bracket_high < N) { - low = high; - bracket_low = bracket_high; - high <<= 1; - bracket_high *= bracket_high; - } - - while (((mp_digit)(high - low)) > 1uL) { - mid = (low + high) >> 1; - bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low)); - - if (N < bracket_mid) { - high = mid ; - bracket_high = bracket_mid ; - } - if (N > bracket_mid) { - low = mid ; - bracket_low = bracket_mid ; - } - if (N == bracket_mid) { - return (mp_digit) mid; - } - } - - if (bracket_high == N) { - ret = high; - } else { - ret = low; - } - - return ret; -} - mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) { - mp_err err; - mp_ord cmp; - uint32_t high, low, mid; - mp_int bracket_low, bracket_high, bracket_mid, t, bi_base; - - err = MP_OKAY; - if (a->sign == MP_NEG) { return MP_VAL; } @@ -98,79 +28,16 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) return MP_OKAY; } - if (a->used == 1) { - *c = (uint32_t)s_digit_ilogb(base, a->dp[0]); - return err; - } - - cmp = mp_cmp_d(a, base); - if ((cmp == MP_LT) || (cmp == MP_EQ)) { - *c = cmp == MP_EQ; - return err; - } - - if ((err = - mp_init_multi(&bracket_low, &bracket_high, - &bracket_mid, &t, &bi_base, NULL)) != MP_OKAY) { - return err; - } - - low = 0u; - mp_set(&bracket_low, 1uL); - high = 1u; - - mp_set(&bracket_high, base); - - /* - A kind of Giant-step/baby-step algorithm. - Idea shamelessly stolen from https://programmingpraxis.com/2010/05/07/integer-logarithms/2/ - The effect is asymptotic, hence needs benchmarks to test if the Giant-step should be skipped - for small n. - */ - while (mp_cmp(&bracket_high, a) == MP_LT) { - low = high; - if ((err = mp_copy(&bracket_high, &bracket_low)) != MP_OKAY) { - goto LBL_ERR; - } - high <<= 1; - if ((err = mp_sqr(&bracket_high, &bracket_high)) != MP_OKAY) { - goto LBL_ERR; - } + if (MP_HAS(S_MP_LOG_D) && a->used == 1) { + *c = (uint32_t)s_mp_log_d(base, a->dp[0]); + return MP_OKAY; } - mp_set(&bi_base, base); - while ((high - low) > 1u) { - mid = (high + low) >> 1; - - if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) { - goto LBL_ERR; - } - cmp = mp_cmp(a, &bracket_mid); - if (cmp == MP_LT) { - high = mid; - mp_exch(&bracket_mid, &bracket_high); - } - if (cmp == MP_GT) { - low = mid; - mp_exch(&bracket_mid, &bracket_low); - } - if (cmp == MP_EQ) { - *c = mid; - goto LBL_END; - } + if (MP_HAS(S_MP_LOG)) { + return s_mp_log(a, base, c); } - *c = (mp_cmp(&bracket_high, a) == MP_EQ) ? high : low; - -LBL_END: -LBL_ERR: - mp_clear_multi(&bracket_low, &bracket_high, &bracket_mid, - &t, &bi_base, NULL); - return err; + return MP_VAL; } - #endif diff --git a/s_mp_log.c b/s_mp_log.c new file mode 100644 index 000000000..eba279ef7 --- /dev/null +++ b/s_mp_log.c @@ -0,0 +1,82 @@ +#include "tommath_private.h" +#ifdef S_MP_LOG_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c) +{ + mp_err err; + mp_ord cmp; + uint32_t high, low, mid; + mp_int bracket_low, bracket_high, bracket_mid, t, bi_base; + + cmp = mp_cmp_d(a, base); + if ((cmp == MP_LT) || (cmp == MP_EQ)) { + *c = cmp == MP_EQ; + return MP_OKAY; + } + + if ((err = + mp_init_multi(&bracket_low, &bracket_high, + &bracket_mid, &t, &bi_base, NULL)) != MP_OKAY) { + return err; + } + + low = 0u; + mp_set(&bracket_low, 1uL); + high = 1u; + + mp_set(&bracket_high, base); + + /* + A kind of Giant-step/baby-step algorithm. + Idea shamelessly stolen from https://programmingpraxis.com/2010/05/07/integer-logarithms/2/ + The effect is asymptotic, hence needs benchmarks to test if the Giant-step should be skipped + for small n. + */ + while (mp_cmp(&bracket_high, a) == MP_LT) { + low = high; + if ((err = mp_copy(&bracket_high, &bracket_low)) != MP_OKAY) { + goto LBL_END; + } + high <<= 1; + if ((err = mp_sqr(&bracket_high, &bracket_high)) != MP_OKAY) { + goto LBL_END; + } + } + mp_set(&bi_base, base); + + while ((high - low) > 1u) { + mid = (high + low) >> 1; + + if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) { + goto LBL_END; + } + if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) { + goto LBL_END; + } + cmp = mp_cmp(a, &bracket_mid); + if (cmp == MP_LT) { + high = mid; + mp_exch(&bracket_mid, &bracket_high); + } + if (cmp == MP_GT) { + low = mid; + mp_exch(&bracket_mid, &bracket_low); + } + if (cmp == MP_EQ) { + *c = mid; + goto LBL_END; + } + } + + *c = (mp_cmp(&bracket_high, a) == MP_EQ) ? high : low; + +LBL_END: + mp_clear_multi(&bracket_low, &bracket_high, &bracket_mid, + &t, &bi_base, NULL); + return err; +} + + +#endif diff --git a/s_mp_log_d.c b/s_mp_log_d.c new file mode 100644 index 000000000..44edd0755 --- /dev/null +++ b/s_mp_log_d.c @@ -0,0 +1,68 @@ +#include "tommath_private.h" +#ifdef S_MP_LOG_D_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +static mp_word s_pow(mp_word base, mp_word exponent) +{ + mp_word result = 1uLL; + while (exponent != 0u) { + if ((exponent & 1u) == 1u) { + result *= base; + } + exponent >>= 1; + base *= base; + } + + return result; +} + +mp_digit s_mp_log_d(mp_digit base, mp_digit n) +{ + mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N; + mp_digit ret, high = 1uL, low = 0uL, mid; + + if (n < base) { + return 0uL; + } + if (n == base) { + return 1uL; + } + + bracket_high = (mp_word) base ; + N = (mp_word) n; + + while (bracket_high < N) { + low = high; + bracket_low = bracket_high; + high <<= 1; + bracket_high *= bracket_high; + } + + while (((mp_digit)(high - low)) > 1uL) { + mid = (low + high) >> 1; + bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low)); + + if (N < bracket_mid) { + high = mid ; + bracket_high = bracket_mid ; + } + if (N > bracket_mid) { + low = mid ; + bracket_low = bracket_mid ; + } + if (N == bracket_mid) { + return (mp_digit) mid; + } + } + + if (bracket_high == N) { + ret = high; + } else { + ret = low; + } + + return ret; +} + +#endif diff --git a/tommath_class.h b/tommath_class.h index 52c5e45b5..9b7f07541 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -153,6 +153,8 @@ # define S_MP_INVMOD_SLOW_C # define S_MP_KARATSUBA_MUL_C # define S_MP_KARATSUBA_SQR_C +# define S_MP_LOG_C +# define S_MP_LOG_D_C # define S_MP_MONTGOMERY_REDUCE_FAST_C # define S_MP_MUL_DIGS_C # define S_MP_MUL_DIGS_FAST_C @@ -529,17 +531,9 @@ #endif #if defined(MP_LOG_U32_C) -# define MP_CLEAR_MULTI_C -# define MP_CMP_C -# define MP_CMP_D_C -# define MP_COPY_C # define MP_COUNT_BITS_C -# define MP_EXCH_C -# define MP_EXPT_U32_C -# define MP_INIT_MULTI_C -# define MP_MUL_C -# define MP_SET_C -# define MP_SQR_C +# define S_MP_LOG_C +# define S_MP_LOG_D_C #endif #if defined(MP_LSHD_C) @@ -1121,6 +1115,22 @@ # define S_MP_SUB_C #endif +#if defined(S_MP_LOG_C) +# define MP_CLEAR_MULTI_C +# define MP_CMP_C +# define MP_CMP_D_C +# define MP_COPY_C +# define MP_EXCH_C +# define MP_EXPT_U32_C +# define MP_INIT_MULTI_C +# define MP_MUL_C +# define MP_SET_C +# define MP_SQR_C +#endif + +#if defined(S_MP_LOG_D_C) +#endif + #if defined(S_MP_MONTGOMERY_REDUCE_FAST_C) # define MP_CLAMP_C # define MP_CMP_MAG_C diff --git a/tommath_private.h b/tommath_private.h index 45e0930f5..e285abc16 100644 --- a/tommath_private.h +++ b/tommath_private.h @@ -206,6 +206,8 @@ typedef int mp_prime_callback(unsigned char *dst, int len, void *dat); MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_callback cb, void *dat); MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len); MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result); +MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n); +MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c); /* 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; From d8da85a9a4bf90cabbe8528391b3bd30506c22cd Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 19 Oct 2019 19:54:20 +0200 Subject: [PATCH 4/4] mp_sqrtmod_prime: use mp_set --- mp_sqrtmod_prime.c | 4 ++-- tommath_class.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mp_sqrtmod_prime.c b/mp_sqrtmod_prime.c index bf72005b3..03ebf8ae4 100644 --- a/mp_sqrtmod_prime.c +++ b/mp_sqrtmod_prime.c @@ -59,7 +59,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) } /* find a Z such that the Legendre symbol (Z|prime) == -1 */ - mp_set_u32(&Z, 2u); + mp_set(&Z, 2u); /* Z = 2 */ for (;;) { if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY) goto cleanup; @@ -79,7 +79,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) /* T = n ^ Q mod prime */ if ((err = mp_copy(&S, &M)) != MP_OKAY) goto cleanup; /* M = S */ - mp_set_u32(&two, 2u); + mp_set(&two, 2u); for (;;) { if ((err = mp_copy(&T, &t1)) != MP_OKAY) goto cleanup; diff --git a/tommath_class.h b/tommath_class.h index 9b7f07541..f7812da01 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -945,7 +945,6 @@ # define MP_MOD_D_C # define MP_MULMOD_C # define MP_SET_C -# define MP_SET_U32_C # define MP_SQRMOD_C # define MP_SUB_D_C # define MP_ZERO_C