Skip to content

Commit 584405f

Browse files
authored
Merge pull request #304 from libtom/deprecate-expt-root
deprecate mp_expt_d and mp_n_root in favor of mp_expt and mp_root
2 parents 18c919f + ca89e9c commit 584405f

14 files changed

+182
-161
lines changed

bn_deprecated.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,38 @@ mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
195195
mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
196196
{
197197
(void)fast;
198-
return mp_expt_d(a, b, c);
198+
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
199+
return MP_VAL;
200+
}
201+
return mp_expt_u32(a, (uint32_t)b, c);
202+
}
203+
#endif
204+
#ifdef BN_MP_EXPT_D_C
205+
mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
206+
{
207+
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
208+
return MP_VAL;
209+
}
210+
return mp_expt_u32(a, (uint32_t)b, c);
199211
}
200212
#endif
201213
#ifdef BN_MP_N_ROOT_EX_C
202214
mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
203215
{
204216
(void)fast;
205-
return mp_n_root(a, b, c);
217+
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
218+
return MP_VAL;
219+
}
220+
return mp_root_u32(a, (uint32_t)b, c);
221+
}
222+
#endif
223+
#ifdef BN_MP_N_ROOT_C
224+
mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
225+
{
226+
if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
227+
return MP_VAL;
228+
}
229+
return mp_root_u32(a, (uint32_t)b, c);
206230
}
207231
#endif
208232
#endif

bn_mp_expt_d.c renamed to bn_mp_expt_u32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "tommath_private.h"
2-
#ifdef BN_MP_EXPT_D_C
2+
#ifdef BN_MP_EXPT_U32_C
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

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

bn_mp_ilogb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static mp_digit s_digit_ilogb(mp_digit base, mp_digit n)
7070
as is the output of mp_bitcount.
7171
With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only!
7272
*/
73-
mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
73+
mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c)
7474
{
7575
mp_err err;
7676
mp_ord cmp;
@@ -145,7 +145,7 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
145145
err = MP_VAL;
146146
goto LBL_ERR;
147147
}
148-
if ((err = mp_expt_d(&bi_base, (mp_digit)(mid - low), &t)) != MP_OKAY) {
148+
if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) {
149149
goto LBL_ERR;
150150
}
151151
if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) {

bn_mp_n_root.c renamed to bn_mp_root_u32.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "tommath_private.h"
2-
#ifdef BN_MP_N_ROOT_C
2+
#ifdef BN_MP_ROOT_U32_C
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

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

3838
/*
39-
GCC and clang do not understand the sizeof tests and complain,
40-
icc (the Intel compiler) seems to understand, at least it doesn't complain.
41-
2 of 3 say these macros are necessary, so there they are.
39+
If "b" is larger than INT_MAX it is also larger than
40+
log_2(n) because the bit-length of the "n" is measured
41+
with an int and hence the root is always < 2 (two).
4242
*/
43-
#if ( !(defined MP_8BIT) && !(defined MP_16BIT) )
44-
/*
45-
The type of mp_digit might be larger than an int.
46-
If "b" is larger than INT_MAX it is also larger than
47-
log_2(n) because the bit-length of the "n" is measured
48-
with an int and hence the root is always < 2 (two).
49-
*/
50-
if (sizeof(mp_digit) >= sizeof(int)) {
51-
if (b > (mp_digit)(INT_MAX/2)) {
52-
mp_set(c, 1uL);
53-
c->sign = a->sign;
54-
err = MP_OKAY;
55-
goto LBL_ERR;
56-
}
43+
if (b > (uint32_t)(INT_MAX/2)) {
44+
mp_set(c, 1uL);
45+
c->sign = a->sign;
46+
err = MP_OKAY;
47+
goto LBL_ERR;
5748
}
58-
#endif
49+
5950
/* "b" is smaller than INT_MAX, we can cast safely */
6051
if (ilog2 < (int)b) {
6152
mp_set(c, 1uL);
@@ -84,7 +75,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
8475
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
8576

8677
/* t3 = t1**(b-1) */
87-
if ((err = mp_expt_d(&t1, b - 1u, &t3)) != MP_OKAY) {
78+
if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) {
8879
goto LBL_ERR;
8980
}
9081
/* numerator */
@@ -124,7 +115,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
124115
/* result can be off by a few so check */
125116
/* Loop beneath can overshoot by one if found root is smaller than actual root */
126117
for (;;) {
127-
if ((err = mp_expt_d(&t1, b, &t2)) != MP_OKAY) {
118+
if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) {
128119
goto LBL_ERR;
129120
}
130121
cmp = mp_cmp(&t2, &a_);
@@ -142,7 +133,7 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
142133
}
143134
/* correct overshoot from above or from recurrence */
144135
for (;;) {
145-
if ((err = mp_expt_d(&t1, b, &t2)) != MP_OKAY) {
136+
if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) {
146137
goto LBL_ERR;
147138
}
148139
if (mp_cmp(&t2, &a_) == MP_GT) {

demo/test.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ static int test_mp_sqrt(void)
760760
printf("\nmp_sqrt() error!");
761761
goto LBL_ERR;
762762
}
763-
mp_n_root(&a, 2uL, &c);
763+
mp_root_u32(&a, 2uL, &c);
764764
if (mp_cmp_mag(&b, &c) != MP_EQ) {
765765
printf("mp_sqrt() bad result!\n");
766766
goto LBL_ERR;
@@ -1364,8 +1364,10 @@ static mp_err s_rs(const mp_int *a, int radix, int *size)
13641364
static int test_mp_ilogb(void)
13651365
{
13661366
mp_int a, lb;
1367-
mp_digit d, base;
1367+
mp_digit d;
1368+
uint32_t base;
13681369
int size;
1370+
const uint32_t max_base = MP_MIN(UINT32_MAX, MP_DIGIT_MAX);
13691371

13701372
if (mp_init_multi(&a, &lb, NULL) != MP_OKAY) {
13711373
goto LBL_ERR;
@@ -1377,11 +1379,11 @@ static int test_mp_ilogb(void)
13771379
1 x MP_VAL
13781380
*/
13791381
mp_set(&a, 42uL);
1380-
base = 0uL;
1382+
base = 0u;
13811383
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
13821384
goto LBL_ERR;
13831385
}
1384-
base = 1uL;
1386+
base = 1u;
13851387
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
13861388
goto LBL_ERR;
13871389
}
@@ -1392,7 +1394,7 @@ static int test_mp_ilogb(void)
13921394
2 2 1
13931395
2 3 1
13941396
*/
1395-
base = 2uL;
1397+
base = 2u;
13961398
mp_zero(&a);
13971399
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
13981400
goto LBL_ERR;
@@ -1414,7 +1416,7 @@ static int test_mp_ilogb(void)
14141416
3 2 0
14151417
3 3 1
14161418
*/
1417-
base = 3uL;
1419+
base = 3u;
14181420
mp_zero(&a);
14191421
if (mp_ilogb(&a, base, &lb) != MP_VAL) {
14201422
goto LBL_ERR;
@@ -1437,7 +1439,7 @@ static int test_mp_ilogb(void)
14371439
if (mp_rand(&a, 10) != MP_OKAY) {
14381440
goto LBL_ERR;
14391441
}
1440-
for (base = 2uL; base < 65uL; base++) {
1442+
for (base = 2u; base < 65u; base++) {
14411443
if (mp_ilogb(&a, base, &lb) != MP_OKAY) {
14421444
goto LBL_ERR;
14431445
}
@@ -1458,7 +1460,7 @@ static int test_mp_ilogb(void)
14581460
if (mp_rand(&a, 1) != MP_OKAY) {
14591461
goto LBL_ERR;
14601462
}
1461-
for (base = 2uL; base < 65uL; base++) {
1463+
for (base = 2u; base < 65u; base++) {
14621464
if (mp_ilogb(&a, base, &lb) != MP_OKAY) {
14631465
goto LBL_ERR;
14641466
}
@@ -1471,15 +1473,15 @@ static int test_mp_ilogb(void)
14711473
}
14721474
}
14731475

1474-
/*Test upper edgecase with base MP_MASK and number (MP_MASK/2)*MP_MASK^10 */
1475-
mp_set(&a, MP_MASK);
1476-
if (mp_expt_d(&a, 10uL, &a) != MP_OKAY) {
1476+
/*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10 */
1477+
mp_set(&a, max_base);
1478+
if (mp_expt_u32(&a, 10uL, &a) != MP_OKAY) {
14771479
goto LBL_ERR;
14781480
}
1479-
if (mp_add_d(&a, (MP_MASK>>1), &a) != MP_OKAY) {
1481+
if (mp_add_d(&a, max_base / 2, &a) != MP_OKAY) {
14801482
goto LBL_ERR;
14811483
}
1482-
if (mp_ilogb(&a, MP_MASK, &lb) != MP_OKAY) {
1484+
if (mp_ilogb(&a, max_base, &lb) != MP_OKAY) {
14831485
goto LBL_ERR;
14841486
}
14851487
if (mp_cmp_d(&lb, 10uL) != MP_EQ) {
@@ -1626,7 +1628,7 @@ static int test_mp_decr(void)
16261628
}
16271629

16281630
/*
1629-
Cannot test mp_exp(_d) without mp_n_root and vice versa.
1631+
Cannot test mp_exp(_d) without mp_root and vice versa.
16301632
So one of the two has to be tested from scratch.
16311633
16321634
Numbers generated by
@@ -1647,7 +1649,7 @@ static int test_mp_decr(void)
16471649
All numbers as strings to simplifiy things, especially for the
16481650
low-mp branch.
16491651
*/
1650-
static int test_mp_n_root(void)
1652+
static int test_mp_root_u32(void)
16511653
{
16521654
mp_int a, c, r;
16531655
mp_err e;
@@ -1850,10 +1852,10 @@ static int test_mp_n_root(void)
18501852
#else
18511853
for (j = 3; j < 100; j++) {
18521854
#endif
1853-
mp_n_root(&a, (mp_digit) j, &c);
1855+
mp_root_u32(&a, (uint32_t)j, &c);
18541856
mp_read_radix(&r, root[i][j-3], 10);
18551857
if (mp_cmp(&r, &c) != MP_EQ) {
1856-
fprintf(stderr, "mp_n_root failed at input #%d, root #%d\n", i, j);
1858+
fprintf(stderr, "mp_root_u32 failed at input #%d, root #%d\n", i, j);
18571859
goto LTM_ERR;
18581860
}
18591861
}
@@ -2063,7 +2065,7 @@ int unit_tests(int argc, char **argv)
20632065
T(mp_is_square),
20642066
T(mp_kronecker),
20652067
T(mp_montgomery_reduce),
2066-
T(mp_n_root),
2068+
T(mp_root_u32),
20672069
T(mp_or),
20682070
T(mp_prime_is_prime),
20692071
T(mp_prime_rand),

libtommath_VS2008.vcproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@
433433
>
434434
</File>
435435
<File
436-
RelativePath="bn_mp_expt_d.c"
436+
RelativePath="bn_mp_expt_u32.c"
437437
>
438438
</File>
439439
<File
@@ -632,10 +632,6 @@
632632
RelativePath="bn_mp_mulmod.c"
633633
>
634634
</File>
635-
<File
636-
RelativePath="bn_mp_n_root.c"
637-
>
638-
</File>
639635
<File
640636
RelativePath="bn_mp_neg.c"
641637
>
@@ -732,6 +728,10 @@
732728
RelativePath="bn_mp_reduce_setup.c"
733729
>
734730
</File>
731+
<File
732+
RelativePath="bn_mp_root_u32.c"
733+
>
734+
</File>
735735
<File
736736
RelativePath="bn_mp_rshd.c"
737737
>

makefile

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3030
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 \
3131
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 \
3232
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 \
33-
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 \
33+
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 \
3434
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 \
3535
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 \
3636
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 \
3737
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 \
3838
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 \
3939
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 \
4040
bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o \
41-
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 \
41+
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 \
4242
bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
4343
bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
4444
bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
4545
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 \
4646
bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
47-
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 \
48-
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 \
49-
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 \
50-
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 \
51-
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 \
52-
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 \
53-
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 \
54-
bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
55-
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 \
56-
bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o \
57-
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 \
58-
bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
47+
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 \
48+
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 \
49+
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 \
50+
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 \
51+
bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o \
52+
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 \
53+
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 \
54+
bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o \
55+
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 \
56+
bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o \
57+
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 \
58+
bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
5959

6060
#END_INS
6161

0 commit comments

Comments
 (0)