Skip to content

deprecate mp_expt_d and mp_n_root in favor of mp_expt and mp_root #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions bn_deprecated.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,38 @@ mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
{
(void)fast;
return mp_expt_d(a, b, c);
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
return MP_VAL;
}
return mp_expt_u32(a, (uint32_t)b, c);
}
#endif
#ifdef BN_MP_EXPT_D_C
mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
{
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
return MP_VAL;
}
return mp_expt_u32(a, (uint32_t)b, c);
}
#endif
#ifdef BN_MP_N_ROOT_EX_C
mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
{
(void)fast;
return mp_n_root(a, b, c);
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
return MP_VAL;
}
return mp_root_u32(a, (uint32_t)b, c);
}
#endif
#ifdef BN_MP_N_ROOT_C
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);
}
#endif
#endif
4 changes: 2 additions & 2 deletions bn_mp_expt_d.c → bn_mp_expt_u32.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "tommath_private.h"
#ifdef BN_MP_EXPT_D_C
#ifdef BN_MP_EXPT_U32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* calculate c = a**b using a square-multiply algorithm */
mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
{
mp_err err;

Expand Down
4 changes: 2 additions & 2 deletions bn_mp_ilogb.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ 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_ilogb(const mp_int *a, mp_digit base, mp_int *c)
mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c)
{
mp_err err;
mp_ord cmp;
Expand Down Expand Up @@ -145,7 +145,7 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
err = MP_VAL;
goto LBL_ERR;
}
if ((err = mp_expt_d(&bi_base, (mp_digit)(mid - low), &t)) != MP_OKAY) {
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) {
Expand Down
37 changes: 14 additions & 23 deletions bn_mp_n_root.c → bn_mp_root_u32.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "tommath_private.h"
#ifdef BN_MP_N_ROOT_C
#ifdef BN_MP_ROOT_U32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

Expand All @@ -12,7 +12,7 @@
* which will find the root in log(N) time where
* each step involves a fair bit.
*/
mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
{
mp_int t1, t2, t3, a_;
mp_ord cmp;
Expand All @@ -36,26 +36,17 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
ilog2 = mp_count_bits(a);

/*
GCC and clang do not understand the sizeof tests and complain,
icc (the Intel compiler) seems to understand, at least it doesn't complain.
2 of 3 say these macros are necessary, so there they are.
If "b" is larger than INT_MAX it is also larger than
log_2(n) because the bit-length of the "n" is measured
with an int and hence the root is always < 2 (two).
*/
#if ( !(defined MP_8BIT) && !(defined MP_16BIT) )
/*
The type of mp_digit might be larger than an int.
If "b" is larger than INT_MAX it is also larger than
log_2(n) because the bit-length of the "n" is measured
with an int and hence the root is always < 2 (two).
*/
if (sizeof(mp_digit) >= sizeof(int)) {
if (b > (mp_digit)(INT_MAX/2)) {
mp_set(c, 1uL);
c->sign = a->sign;
err = MP_OKAY;
goto LBL_ERR;
}
if (b > (uint32_t)(INT_MAX/2)) {
mp_set(c, 1uL);
c->sign = a->sign;
err = MP_OKAY;
goto LBL_ERR;
}
#endif

/* "b" is smaller than INT_MAX, we can cast safely */
if (ilog2 < (int)b) {
mp_set(c, 1uL);
Expand Down Expand Up @@ -84,7 +75,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */

/* t3 = t1**(b-1) */
if ((err = mp_expt_d(&t1, b - 1u, &t3)) != MP_OKAY) {
if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) {
goto LBL_ERR;
}
/* numerator */
Expand Down Expand Up @@ -124,7 +115,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
/* result can be off by a few so check */
/* Loop beneath can overshoot by one if found root is smaller than actual root */
for (;;) {
if ((err = mp_expt_d(&t1, b, &t2)) != MP_OKAY) {
if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) {
goto LBL_ERR;
}
cmp = mp_cmp(&t2, &a_);
Expand All @@ -142,7 +133,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
}
/* correct overshoot from above or from recurrence */
for (;;) {
if ((err = mp_expt_d(&t1, b, &t2)) != MP_OKAY) {
if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp(&t2, &a_) == MP_GT) {
Expand Down
38 changes: 20 additions & 18 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ static int test_mp_sqrt(void)
printf("\nmp_sqrt() error!");
goto LBL_ERR;
}
mp_n_root(&a, 2uL, &c);
mp_root_u32(&a, 2uL, &c);
if (mp_cmp_mag(&b, &c) != MP_EQ) {
printf("mp_sqrt() bad result!\n");
goto LBL_ERR;
Expand Down Expand Up @@ -1364,8 +1364,10 @@ static mp_err s_rs(const mp_int *a, int radix, int *size)
static int test_mp_ilogb(void)
{
mp_int a, lb;
mp_digit d, base;
mp_digit d;
uint32_t base;
int size;
const uint32_t max_base = MP_MIN(UINT32_MAX, MP_DIGIT_MAX);

if (mp_init_multi(&a, &lb, NULL) != MP_OKAY) {
goto LBL_ERR;
Expand All @@ -1377,11 +1379,11 @@ static int test_mp_ilogb(void)
1 x MP_VAL
*/
mp_set(&a, 42uL);
base = 0uL;
base = 0u;
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
}
base = 1uL;
base = 1u;
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
}
Expand All @@ -1392,7 +1394,7 @@ static int test_mp_ilogb(void)
2 2 1
2 3 1
*/
base = 2uL;
base = 2u;
mp_zero(&a);
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
Expand All @@ -1414,7 +1416,7 @@ static int test_mp_ilogb(void)
3 2 0
3 3 1
*/
base = 3uL;
base = 3u;
mp_zero(&a);
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
Expand All @@ -1437,7 +1439,7 @@ static int test_mp_ilogb(void)
if (mp_rand(&a, 10) != MP_OKAY) {
goto LBL_ERR;
}
for (base = 2uL; base < 65uL; base++) {
for (base = 2u; base < 65u; base++) {
if (mp_ilogb(&a, base, &lb) != MP_OKAY) {
goto LBL_ERR;
}
Expand All @@ -1458,7 +1460,7 @@ static int test_mp_ilogb(void)
if (mp_rand(&a, 1) != MP_OKAY) {
goto LBL_ERR;
}
for (base = 2uL; base < 65uL; base++) {
for (base = 2u; base < 65u; base++) {
if (mp_ilogb(&a, base, &lb) != MP_OKAY) {
goto LBL_ERR;
}
Expand All @@ -1471,15 +1473,15 @@ static int test_mp_ilogb(void)
}
}

/*Test upper edgecase with base MP_MASK and number (MP_MASK/2)*MP_MASK^10 */
mp_set(&a, MP_MASK);
if (mp_expt_d(&a, 10uL, &a) != MP_OKAY) {
/*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10 */
mp_set(&a, max_base);
if (mp_expt_u32(&a, 10uL, &a) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_add_d(&a, (MP_MASK>>1), &a) != MP_OKAY) {
if (mp_add_d(&a, max_base / 2, &a) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_ilogb(&a, MP_MASK, &lb) != MP_OKAY) {
if (mp_ilogb(&a, max_base, &lb) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp_d(&lb, 10uL) != MP_EQ) {
Expand Down Expand Up @@ -1626,7 +1628,7 @@ static int test_mp_decr(void)
}

/*
Cannot test mp_exp(_d) without mp_n_root and vice versa.
Cannot test mp_exp(_d) without mp_root and vice versa.
So one of the two has to be tested from scratch.

Numbers generated by
Expand All @@ -1647,7 +1649,7 @@ static int test_mp_decr(void)
All numbers as strings to simplifiy things, especially for the
low-mp branch.
*/
static int test_mp_n_root(void)
static int test_mp_root_u32(void)
{
mp_int a, c, r;
mp_err e;
Expand Down Expand Up @@ -1850,10 +1852,10 @@ static int test_mp_n_root(void)
#else
for (j = 3; j < 100; j++) {
#endif
mp_n_root(&a, (mp_digit) j, &c);
mp_root_u32(&a, (uint32_t)j, &c);
mp_read_radix(&r, root[i][j-3], 10);
if (mp_cmp(&r, &c) != MP_EQ) {
fprintf(stderr, "mp_n_root failed at input #%d, root #%d\n", i, j);
fprintf(stderr, "mp_root_u32 failed at input #%d, root #%d\n", i, j);
goto LTM_ERR;
}
}
Expand Down Expand Up @@ -2063,7 +2065,7 @@ int unit_tests(int argc, char **argv)
T(mp_is_square),
T(mp_kronecker),
T(mp_montgomery_reduce),
T(mp_n_root),
T(mp_root_u32),
T(mp_or),
T(mp_prime_is_prime),
T(mp_prime_rand),
Expand Down
10 changes: 5 additions & 5 deletions libtommath_VS2008.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
>
</File>
<File
RelativePath="bn_mp_expt_d.c"
RelativePath="bn_mp_expt_u32.c"
>
</File>
<File
Expand Down Expand Up @@ -632,10 +632,6 @@
RelativePath="bn_mp_mulmod.c"
>
</File>
<File
RelativePath="bn_mp_n_root.c"
>
</File>
<File
RelativePath="bn_mp_neg.c"
>
Expand Down Expand Up @@ -732,6 +728,10 @@
RelativePath="bn_mp_reduce_setup.c"
>
</File>
<File
RelativePath="bn_mp_root_u32.c"
>
</File>
<File
RelativePath="bn_mp_rshd.c"
>
Expand Down
28 changes: 14 additions & 14 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp
bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \
bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \
bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \
bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o bn_mp_exptmod.o bn_mp_exteuclid.o \
bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o \
bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o \
bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_i32.o \
bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \
bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o bn_mp_invmod.o bn_mp_is_square.o \
bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o \
bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o \
bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_neg.o bn_mp_or.o \
bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o bn_mp_or.o \
bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o \
bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_bin_size.o bn_mp_signed_rsh.o \
bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o \
bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o \
bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o \
bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \
bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_bin_size.o \
bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o \
bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o \
bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o \
bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o \
bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o \
bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o \
bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o \
bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \
bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o

#END_INS

Expand Down
Loading