Skip to content

Commit e6be74a

Browse files
committed
remove _u32 suffix from mp_(expt|log|root) functions, use int for now
1 parent 7e47ae6 commit e6be74a

File tree

12 files changed

+107
-117
lines changed

12 files changed

+107
-117
lines changed

demo/test.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ static int test_mp_sqrt(void)
729729
printf("\nmp_sqrt() error!");
730730
goto LBL_ERR;
731731
}
732-
DO(mp_root_u32(&a, 2u, &c));
732+
DO(mp_root(&a, 2u, &c));
733733
if (mp_cmp_mag(&b, &c) != MP_EQ) {
734734
printf("mp_sqrt() bad result!\n");
735735
goto LBL_ERR;
@@ -1397,10 +1397,10 @@ static int test_mp_reduce_2k_l(void)
13971397
/* stripped down version of mp_radix_size. The faster version can be off by up t
13981398
o +3 */
13991399
/* TODO: This function should be removed, replaced by mp_radix_size, mp_radix_size_overestimate in 2.0 */
1400-
static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
1400+
static mp_err s_rs(const mp_int *a, int radix, int *size)
14011401
{
14021402
mp_err res;
1403-
uint32_t digs = 0u;
1403+
int digs = 0u;
14041404
mp_int t;
14051405
mp_digit d;
14061406
*size = 0u;
@@ -1409,7 +1409,7 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
14091409
return MP_OKAY;
14101410
}
14111411
if (radix == 2) {
1412-
*size = (uint32_t)mp_count_bits(a) + 1u;
1412+
*size = mp_count_bits(a) + 1;
14131413
return MP_OKAY;
14141414
}
14151415
DOR(mp_init_copy(&t, a));
@@ -1425,12 +1425,12 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
14251425
*size = digs + 1;
14261426
return MP_OKAY;
14271427
}
1428-
static int test_mp_log_u32(void)
1428+
static int test_mp_log(void)
14291429
{
14301430
mp_int a;
14311431
mp_digit d;
1432-
uint32_t base, lb, size;
1433-
const uint32_t max_base = MP_MIN(UINT32_MAX, MP_DIGIT_MAX);
1432+
int base, lb, size;
1433+
const int max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);
14341434

14351435
DOR(mp_init(&a));
14361436

@@ -1441,11 +1441,11 @@ static int test_mp_log_u32(void)
14411441
*/
14421442
mp_set(&a, 42u);
14431443
base = 0u;
1444-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1444+
if (mp_log(&a, base, &lb) != MP_VAL) {
14451445
goto LBL_ERR;
14461446
}
14471447
base = 1u;
1448-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1448+
if (mp_log(&a, base, &lb) != MP_VAL) {
14491449
goto LBL_ERR;
14501450
}
14511451
/*
@@ -1457,14 +1457,14 @@ static int test_mp_log_u32(void)
14571457
*/
14581458
base = 2u;
14591459
mp_zero(&a);
1460-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1460+
if (mp_log(&a, base, &lb) != MP_VAL) {
14611461
goto LBL_ERR;
14621462
}
14631463

14641464
for (d = 1; d < 4; d++) {
14651465
mp_set(&a, d);
1466-
DO(mp_log_u32(&a, base, &lb));
1467-
if (lb != ((d == 1)?0uL:1uL)) {
1466+
DO(mp_log(&a, base, &lb));
1467+
if (lb != ((d == 1)?0:1)) {
14681468
goto LBL_ERR;
14691469
}
14701470
}
@@ -1477,13 +1477,13 @@ static int test_mp_log_u32(void)
14771477
*/
14781478
base = 3u;
14791479
mp_zero(&a);
1480-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1480+
if (mp_log(&a, base, &lb) != MP_VAL) {
14811481
goto LBL_ERR;
14821482
}
14831483
for (d = 1; d < 4; d++) {
14841484
mp_set(&a, d);
1485-
DO(mp_log_u32(&a, base, &lb));
1486-
if (lb != ((d < base)?0uL:1uL)) {
1485+
DO(mp_log(&a, base, &lb));
1486+
if (lb != (((int)d < base)?0:1)) {
14871487
goto LBL_ERR;
14881488
}
14891489
}
@@ -1494,8 +1494,8 @@ static int test_mp_log_u32(void)
14941494
radix_size.
14951495
*/
14961496
DO(mp_rand(&a, 10));
1497-
for (base = 2u; base < 65u; base++) {
1498-
DO(mp_log_u32(&a, base, &lb));
1497+
for (base = 2; base < 65; base++) {
1498+
DO(mp_log(&a, base, &lb));
14991499
DO(s_rs(&a,(int)base, &size));
15001500
/* radix_size includes the memory needed for '\0', too*/
15011501
size -= 2;
@@ -1509,8 +1509,8 @@ static int test_mp_log_u32(void)
15091509
test the part of mp_ilogb that uses native types.
15101510
*/
15111511
DO(mp_rand(&a, 1));
1512-
for (base = 2u; base < 65u; base++) {
1513-
DO(mp_log_u32(&a, base, &lb));
1512+
for (base = 2; base < 65; base++) {
1513+
DO(mp_log(&a, base, &lb));
15141514
DO(s_rs(&a,(int)base, &size));
15151515
size -= 2;
15161516
if (lb != size) {
@@ -1520,9 +1520,9 @@ static int test_mp_log_u32(void)
15201520

15211521
/*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10 */
15221522
mp_set(&a, max_base);
1523-
DO(mp_expt_u32(&a, 10u, &a));
1524-
DO(mp_add_d(&a, max_base / 2u, &a));
1525-
DO(mp_log_u32(&a, max_base, &lb));
1523+
DO(mp_expt(&a, 10uL, &a));
1524+
DO(mp_add_d(&a, max_base / 2, &a));
1525+
DO(mp_log(&a, max_base, &lb));
15261526
if (lb != 10u) {
15271527
goto LBL_ERR;
15281528
}
@@ -1659,7 +1659,7 @@ static int test_mp_decr(void)
16591659
low-mp branch.
16601660
*/
16611661

1662-
static int test_mp_root_u32(void)
1662+
static int test_mp_root(void)
16631663
{
16641664
mp_int a, c, r;
16651665
int i, j;
@@ -1851,10 +1851,10 @@ static int test_mp_root_u32(void)
18511851
for (i = 0; i < 10; i++) {
18521852
DO(mp_read_radix(&a, input[i], 64));
18531853
for (j = 3; j < 100; j++) {
1854-
DO(mp_root_u32(&a, (uint32_t)j, &c));
1854+
DO(mp_root(&a, j, &c));
18551855
DO(mp_read_radix(&r, root[i][j-3], 10));
18561856
if (mp_cmp(&r, &c) != MP_EQ) {
1857-
fprintf(stderr, "mp_root_u32 failed at input #%d, root #%d\n", i, j);
1857+
fprintf(stderr, "mp_root failed at input #%d, root #%d\n", i, j);
18581858
goto LBL_ERR;
18591859
}
18601860
}
@@ -2038,8 +2038,8 @@ static int test_mp_radix_size(void)
20382038
DOR(mp_init(&a));
20392039

20402040
/* number to result in a different size for every base: 67^(4 * 67) */
2041-
mp_set(&a, 67u);
2042-
DO(mp_expt_u32(&a, 268u, &a));
2041+
mp_set(&a, 67);
2042+
DO(mp_expt(&a, 268, &a));
20432043

20442044
for (radix = 2; radix < 65; radix++) {
20452045
DO(mp_radix_size(&a, radix, &size));
@@ -2305,13 +2305,13 @@ static int unit_tests(int argc, char **argv)
23052305
T1(mp_get_u32, MP_GET_I32),
23062306
T1(mp_get_u64, MP_GET_I64),
23072307
T1(mp_get_ul, MP_GET_L),
2308-
T1(mp_log_u32, MP_LOG_U32),
2308+
T1(mp_log, MP_LOG),
23092309
T1(mp_incr, MP_ADD_D),
23102310
T1(mp_invmod, MP_INVMOD),
23112311
T1(mp_is_square, MP_IS_SQUARE),
23122312
T1(mp_kronecker, MP_KRONECKER),
23132313
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
2314-
T1(mp_root_u32, MP_ROOT_U32),
2314+
T1(mp_root, MP_ROOT),
23152315
T1(mp_or, MP_OR),
23162316
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
23172317
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
@@ -2327,7 +2327,7 @@ static int unit_tests(int argc, char **argv)
23272327
T1(mp_set_double, MP_SET_DOUBLE),
23282328
#endif
23292329
T1(mp_signed_rsh, MP_SIGNED_RSH),
2330-
T1(mp_sqrt, MP_SQRT),
2330+
T2(mp_sqrt, MP_SQRT, MP_ROOT),
23312331
T1(mp_sqrtmod_prime, MP_SQRTMOD_PRIME),
23322332
T1(mp_xor, MP_XOR),
23332333
T2(s_mp_div_recursive, S_MP_DIV_RECURSIVE, S_MP_DIV_SCHOOL),

doc/bn.tex

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,9 +1906,9 @@ \section{Combined Modular Reduction}
19061906

19071907
\chapter{Exponentiation}
19081908
\section{Single Digit Exponentiation}
1909-
\index{mp\_expt\_u32}
1909+
\index{mp\_expt}
19101910
\begin{alltt}
1911-
mp_err mp_expt_u32 (const mp_int *a, uint32_t b, mp_int *c)
1911+
mp_err mp_expt (const mp_int *a, int b, int *c)
19121912
\end{alltt}
19131913
This function computes $c = a^b$.
19141914

@@ -1935,9 +1935,9 @@ \section{Modulus a Power of Two}
19351935
It calculates $c = a \mod 2^b$.
19361936

19371937
\section{Root Finding}
1938-
\index{mp\_root\_u32}
1938+
\index{mp\_root}
19391939
\begin{alltt}
1940-
mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
1940+
mp_err mp_root(const mp_int *a, int b, mp_int *c)
19411941
\end{alltt}
19421942
This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. Will return a positive root
19431943
only for even roots and return a root with the sign of the input for odd roots. For example,
@@ -1959,9 +1959,9 @@ \section{Integer Logarithm}
19591959
A logarithm function for positive integer input \texttt{a, base} computing $\floor{\log_bx}$ such
19601960
that $(\log_b x)^b \le x$.
19611961

1962-
\index{mp\_log\_u32}
1962+
\index{mp\_log}
19631963
\begin{alltt}
1964-
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
1964+
mp_err mp_log(const mp_int *a, int base, int *c)
19651965
\end{alltt}
19661966

19671967
\subsection{Example}
@@ -1976,7 +1976,7 @@ \subsection{Example}
19761976
int main(int argc, char **argv)
19771977
{
19781978
mp_int x, output;
1979-
uint32_t base;
1979+
int base;
19801980
mp_err e;
19811981
19821982
if (argc != 3) {
@@ -1989,12 +1989,8 @@ \subsection{Example}
19891989
exit(EXIT_FAILURE);
19901990
}
19911991
errno = 0;
1992-
#ifdef MP_64BIT
1993-
/* Check for overflow skipped */
1994-
base = (uint32_t)strtoull(argv[1], NULL, 10);
1995-
#else
1996-
base = (uint32_t)strtoul(argv[1], NULL, 10);
1997-
#endif
1992+
base = (int)strtoul(argv[1], NULL, 10);
1993+
19981994
if (errno == ERANGE) {
19991995
fprintf(stderr,"strtoul(l) failed: input out of range\textbackslash{}n");
20001996
exit(EXIT_FAILURE);
@@ -2004,8 +2000,8 @@ \subsection{Example}
20042000
mp_error_to_string(e));
20052001
exit(EXIT_FAILURE);
20062002
}
2007-
if ((e = mp_log_u32(&x, base, &output)) != MP_OKAY) {
2008-
fprintf(stderr,"mp_ilogb failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
2003+
if ((e = mp_log(&x, base, &output)) != MP_OKAY) {
2004+
fprintf(stderr,"mp_log failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
20092005
mp_error_to_string(e));
20102006
exit(EXIT_FAILURE);
20112007
}

mp_expt_u32.c renamed to mp_expt.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#include "tommath_private.h"
2-
#ifdef MP_EXPT_U32_C
2+
#ifdef MP_EXPT_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_u32(const mp_int *a, uint32_t b, mp_int *c)
7+
mp_err mp_expt(const mp_int *a, int b, mp_int *c)
88
{
99
mp_err err;
10-
1110
mp_int g;
1211

1312
if ((err = mp_init_copy(&g, a)) != MP_OKAY) {
@@ -17,16 +16,16 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
1716
/* set initial result */
1817
mp_set(c, 1uL);
1918

20-
while (b > 0u) {
19+
while (b > 0) {
2120
/* if the bit is set multiply */
22-
if ((b & 1u) != 0u) {
21+
if ((b & 1) != 0) {
2322
if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
2423
goto LBL_ERR;
2524
}
2625
}
2726

2827
/* square */
29-
if (b > 1u) {
28+
if (b > 1) {
3029
if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
3130
goto LBL_ERR;
3231
}
@@ -36,8 +35,6 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
3635
b >>= 1;
3736
}
3837

39-
err = MP_OKAY;
40-
4138
LBL_ERR:
4239
mp_clear(&g);
4340
return err;

mp_log_u32.c renamed to mp_log.c

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

6-
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
6+
mp_err mp_log(const mp_int *a, int base, int *c)
77
{
88
if (a->sign == MP_NEG) {
99
return MP_VAL;
@@ -13,22 +13,22 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
1313
return MP_VAL;
1414
}
1515

16-
if (base < 2u) {
16+
if (base < 2 || (unsigned)base > (unsigned)MP_DIGIT_MAX) {
1717
return MP_VAL;
1818
}
1919

20-
if (MP_HAS(S_MP_LOG_POW2) && ((base & (base - 1u)) == 0u)) {
21-
*c = s_mp_log_pow2(a, base);
20+
if (MP_HAS(S_MP_LOG_2EXPT) && ((base & (base - 1)) == 0u)) {
21+
*c = s_mp_log_2expt(a, (mp_digit)base);
2222
return MP_OKAY;
2323
}
2424

2525
if (MP_HAS(S_MP_LOG_D) && (a->used == 1)) {
26-
*c = (uint32_t)s_mp_log_d(base, a->dp[0]);
26+
*c = s_mp_log_d((mp_digit)base, a->dp[0]);
2727
return MP_OKAY;
2828
}
2929

3030
if (MP_HAS(S_MP_LOG)) {
31-
return s_mp_log(a, base, c);
31+
return s_mp_log(a, (mp_digit)base, c);
3232
}
3333

3434
return MP_VAL;

mp_radix_size.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
88
{
99
mp_err err;
1010
mp_int a_;
11-
uint32_t b;
11+
int b;
1212

1313
/* make sure the radix is in range */
1414
if ((radix < 2) || (radix > 64)) {
@@ -22,14 +22,13 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
2222

2323
a_ = *a;
2424
a_.sign = MP_ZPOS;
25-
if ((err = mp_log_u32(&a_, (uint32_t)radix, &b)) != MP_OKAY) {
26-
goto LBL_ERR;
25+
if ((err = mp_log(&a_, radix, &b)) != MP_OKAY) {
26+
return err;
2727
}
2828

2929
/* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
30-
*size = (size_t)b + 2U + ((a->sign == MP_NEG) ? 1U : 0U);
30+
*size = (size_t)(b + 2 + ((a->sign == MP_NEG) ? 1 : 0));
3131

32-
LBL_ERR:
33-
return err;
32+
return MP_OKAY;
3433
}
3534
#endif

0 commit comments

Comments
 (0)