Skip to content

Commit f6a7bed

Browse files
minadsjaeckel
authored andcommitted
suffix _u32 -> _n of mp_(expt|log|root) functions, use int for now
1 parent 86648a0 commit f6a7bed

14 files changed

+135
-145
lines changed

demo/test.c

Lines changed: 31 additions & 31 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_n(&a, 2u, &c));
733733
if (mp_cmp_mag(&b, &c) != MP_EQ) {
734734
printf("mp_sqrt() bad result!\n");
735735
goto LBL_ERR;
@@ -1396,10 +1396,10 @@ static int test_mp_reduce_2k_l(void)
13961396
/* stripped down version of mp_radix_size. The faster version can be off by up t
13971397
o +3 */
13981398
/* TODO: This function should be removed, replaced by mp_radix_size, mp_radix_size_overestimate in 2.0 */
1399-
static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
1399+
static mp_err s_rs(const mp_int *a, int radix, int *size)
14001400
{
14011401
mp_err res;
1402-
uint32_t digs = 0u;
1402+
int digs = 0u;
14031403
mp_int t;
14041404
mp_digit d;
14051405
*size = 0u;
@@ -1408,7 +1408,7 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
14081408
return MP_OKAY;
14091409
}
14101410
if (radix == 2) {
1411-
*size = (uint32_t)mp_count_bits(a) + 1u;
1411+
*size = mp_count_bits(a) + 1;
14121412
return MP_OKAY;
14131413
}
14141414
DOR(mp_init_copy(&t, a));
@@ -1424,12 +1424,12 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
14241424
*size = digs + 1;
14251425
return MP_OKAY;
14261426
}
1427-
static int test_mp_log_u32(void)
1427+
static int test_mp_log_n(void)
14281428
{
14291429
mp_int a;
14301430
mp_digit d;
1431-
uint32_t base, lb, size;
1432-
const uint32_t max_base = MP_MIN(UINT32_MAX, MP_DIGIT_MAX);
1431+
int base, lb, size;
1432+
const int max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);
14331433

14341434
DOR(mp_init(&a));
14351435

@@ -1440,11 +1440,11 @@ static int test_mp_log_u32(void)
14401440
*/
14411441
mp_set(&a, 42u);
14421442
base = 0u;
1443-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1443+
if (mp_log_n(&a, base, &lb) != MP_VAL) {
14441444
goto LBL_ERR;
14451445
}
14461446
base = 1u;
1447-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1447+
if (mp_log_n(&a, base, &lb) != MP_VAL) {
14481448
goto LBL_ERR;
14491449
}
14501450
/*
@@ -1456,14 +1456,14 @@ static int test_mp_log_u32(void)
14561456
*/
14571457
base = 2u;
14581458
mp_zero(&a);
1459-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1459+
if (mp_log_n(&a, base, &lb) != MP_VAL) {
14601460
goto LBL_ERR;
14611461
}
14621462

14631463
for (d = 1; d < 4; d++) {
14641464
mp_set(&a, d);
1465-
DO(mp_log_u32(&a, base, &lb));
1466-
if (lb != ((d == 1)?0uL:1uL)) {
1465+
DO(mp_log_n(&a, base, &lb));
1466+
if (lb != ((d == 1)?0:1)) {
14671467
goto LBL_ERR;
14681468
}
14691469
}
@@ -1476,13 +1476,13 @@ static int test_mp_log_u32(void)
14761476
*/
14771477
base = 3u;
14781478
mp_zero(&a);
1479-
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
1479+
if (mp_log_n(&a, base, &lb) != MP_VAL) {
14801480
goto LBL_ERR;
14811481
}
14821482
for (d = 1; d < 4; d++) {
14831483
mp_set(&a, d);
1484-
DO(mp_log_u32(&a, base, &lb));
1485-
if (lb != ((d < base)?0uL:1uL)) {
1484+
DO(mp_log_n(&a, base, &lb));
1485+
if (lb != (((int)d < base)?0:1)) {
14861486
goto LBL_ERR;
14871487
}
14881488
}
@@ -1493,8 +1493,8 @@ static int test_mp_log_u32(void)
14931493
radix_size.
14941494
*/
14951495
DO(mp_rand(&a, 10));
1496-
for (base = 2u; base < 65u; base++) {
1497-
DO(mp_log_u32(&a, base, &lb));
1496+
for (base = 2; base < 65; base++) {
1497+
DO(mp_log_n(&a, base, &lb));
14981498
DO(s_rs(&a,(int)base, &size));
14991499
/* radix_size includes the memory needed for '\0', too*/
15001500
size -= 2;
@@ -1508,8 +1508,8 @@ static int test_mp_log_u32(void)
15081508
test the part of mp_ilogb that uses native types.
15091509
*/
15101510
DO(mp_rand(&a, 1));
1511-
for (base = 2u; base < 65u; base++) {
1512-
DO(mp_log_u32(&a, base, &lb));
1511+
for (base = 2; base < 65; base++) {
1512+
DO(mp_log_n(&a, base, &lb));
15131513
DO(s_rs(&a,(int)base, &size));
15141514
size -= 2;
15151515
if (lb != size) {
@@ -1519,9 +1519,9 @@ static int test_mp_log_u32(void)
15191519

15201520
/*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10 */
15211521
mp_set(&a, max_base);
1522-
DO(mp_expt_u32(&a, 10u, &a));
1523-
DO(mp_add_d(&a, max_base / 2u, &a));
1524-
DO(mp_log_u32(&a, max_base, &lb));
1522+
DO(mp_expt_n(&a, 10uL, &a));
1523+
DO(mp_add_d(&a, max_base / 2, &a));
1524+
DO(mp_log_n(&a, max_base, &lb));
15251525
if (lb != 10u) {
15261526
goto LBL_ERR;
15271527
}
@@ -1636,7 +1636,7 @@ static int test_mp_decr(void)
16361636
}
16371637

16381638
/*
1639-
Cannot test mp_exp(_d) without mp_root and vice versa.
1639+
Cannot test mp_exp(_d) without mp_root_n and vice versa.
16401640
So one of the two has to be tested from scratch.
16411641
16421642
Numbers generated by
@@ -1658,7 +1658,7 @@ static int test_mp_decr(void)
16581658
low-mp branch.
16591659
*/
16601660

1661-
static int test_mp_root_u32(void)
1661+
static int test_mp_root_n(void)
16621662
{
16631663
mp_int a, c, r;
16641664
int i, j;
@@ -1850,10 +1850,10 @@ static int test_mp_root_u32(void)
18501850
for (i = 0; i < 10; i++) {
18511851
DO(mp_read_radix(&a, input[i], 64));
18521852
for (j = 3; j < 100; j++) {
1853-
DO(mp_root_u32(&a, (uint32_t)j, &c));
1853+
DO(mp_root_n(&a, j, &c));
18541854
DO(mp_read_radix(&r, root[i][j-3], 10));
18551855
if (mp_cmp(&r, &c) != MP_EQ) {
1856-
fprintf(stderr, "mp_root_u32 failed at input #%d, root #%d\n", i, j);
1856+
fprintf(stderr, "mp_root_n failed at input #%d, root #%d\n", i, j);
18571857
goto LBL_ERR;
18581858
}
18591859
}
@@ -2037,8 +2037,8 @@ static int test_mp_radix_size(void)
20372037
DOR(mp_init(&a));
20382038

20392039
/* number to result in a different size for every base: 67^(4 * 67) */
2040-
mp_set(&a, 67u);
2041-
DO(mp_expt_u32(&a, 268u, &a));
2040+
mp_set(&a, 67);
2041+
DO(mp_expt_n(&a, 268, &a));
20422042

20432043
for (radix = 2; radix < 65; radix++) {
20442044
DO(mp_radix_size(&a, radix, &size));
@@ -2304,13 +2304,13 @@ static int unit_tests(int argc, char **argv)
23042304
T1(mp_get_u32, MP_GET_I32),
23052305
T1(mp_get_u64, MP_GET_I64),
23062306
T1(mp_get_ul, MP_GET_L),
2307-
T1(mp_log_u32, MP_LOG_U32),
2307+
T1(mp_log_n, MP_LOG_N),
23082308
T1(mp_incr, MP_ADD_D),
23092309
T1(mp_invmod, MP_INVMOD),
23102310
T1(mp_is_square, MP_IS_SQUARE),
23112311
T1(mp_kronecker, MP_KRONECKER),
23122312
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
2313-
T1(mp_root_u32, MP_ROOT_U32),
2313+
T1(mp_root_n, MP_ROOT_N),
23142314
T1(mp_or, MP_OR),
23152315
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
23162316
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
@@ -2326,7 +2326,7 @@ static int unit_tests(int argc, char **argv)
23262326
T1(mp_set_double, MP_SET_DOUBLE),
23272327
#endif
23282328
T1(mp_signed_rsh, MP_SIGNED_RSH),
2329-
T1(mp_sqrt, MP_SQRT),
2329+
T2(mp_sqrt, MP_SQRT, mp_root_n),
23302330
T1(mp_sqrtmod_prime, MP_SQRTMOD_PRIME),
23312331
T1(mp_xor, MP_XOR),
23322332
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
@@ -1911,9 +1911,9 @@ \section{Combined Modular Reduction}
19111911

19121912
\chapter{Exponentiation}
19131913
\section{Single Digit Exponentiation}
1914-
\index{mp\_expt\_u32}
1914+
\index{mp\_expt\_n}
19151915
\begin{alltt}
1916-
mp_err mp_expt_u32 (const mp_int *a, uint32_t b, mp_int *c)
1916+
mp_err mp_expt_n(const mp_int *a, int b, int *c)
19171917
\end{alltt}
19181918
This function computes $c = a^b$.
19191919

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

19421942
\section{Root Finding}
1943-
\index{mp\_root\_u32}
1943+
\index{mp\_root\_n}
19441944
\begin{alltt}
1945-
mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
1945+
mp_err mp_root_n(const mp_int *a, int b, mp_int *c)
19461946
\end{alltt}
19471947
This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. Will return a positive root
19481948
only for even roots and return a root with the sign of the input for odd roots. For example,
@@ -1964,9 +1964,9 @@ \section{Integer Logarithm}
19641964
A logarithm function for positive integer input \texttt{a, base} computing $\floor{\log_bx}$ such
19651965
that $(\log_b x)^b \le x$.
19661966

1967-
\index{mp\_log\_u32}
1967+
\index{mp\_log\_n}
19681968
\begin{alltt}
1969-
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
1969+
mp_err mp_log_n(const mp_int *a, int base, int *c)
19701970
\end{alltt}
19711971

19721972
\subsection{Example}
@@ -1981,7 +1981,7 @@ \subsection{Example}
19811981
int main(int argc, char **argv)
19821982
{
19831983
mp_int x, output;
1984-
uint32_t base;
1984+
int base;
19851985
mp_err e;
19861986
19871987
if (argc != 3) {
@@ -1994,12 +1994,8 @@ \subsection{Example}
19941994
exit(EXIT_FAILURE);
19951995
}
19961996
errno = 0;
1997-
#ifdef MP_64BIT
1998-
/* Check for overflow skipped */
1999-
base = (uint32_t)strtoull(argv[1], NULL, 10);
2000-
#else
2001-
base = (uint32_t)strtoul(argv[1], NULL, 10);
2002-
#endif
1997+
base = (int)strtoul(argv[1], NULL, 10);
1998+
20031999
if (errno == ERANGE) {
20042000
fprintf(stderr,"strtoul(l) failed: input out of range\textbackslash{}n");
20052001
exit(EXIT_FAILURE);
@@ -2009,8 +2005,8 @@ \subsection{Example}
20092005
mp_error_to_string(e));
20102006
exit(EXIT_FAILURE);
20112007
}
2012-
if ((e = mp_log_u32(&x, base, &output)) != MP_OKAY) {
2013-
fprintf(stderr,"mp_ilogb failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
2008+
if ((e = mp_log_n(&x, base, &output)) != MP_OKAY) {
2009+
fprintf(stderr,"mp_log_n failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
20142010
mp_error_to_string(e));
20152011
exit(EXIT_FAILURE);
20162012
}

mp_expt_u32.c renamed to mp_expt_n.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_N_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_n(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_n.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "tommath_private.h"
2+
#ifdef MP_LOG_N_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
mp_err mp_log_n(const mp_int *a, int base, int *c)
7+
{
8+
if (mp_isneg(a) || mp_iszero(a) || (base < 2) || (unsigned)base > (unsigned)MP_DIGIT_MAX) {
9+
return MP_VAL;
10+
}
11+
12+
if (MP_HAS(S_MP_LOG_2EXPT) && MP_IS_2EXPT((mp_digit)base)) {
13+
*c = s_mp_log_2expt(a, (mp_digit)base);
14+
return MP_OKAY;
15+
}
16+
17+
if (MP_HAS(S_MP_LOG_D) && (a->used == 1)) {
18+
*c = s_mp_log_d((mp_digit)base, a->dp[0]);
19+
return MP_OKAY;
20+
}
21+
22+
if (MP_HAS(S_MP_LOG)) {
23+
return s_mp_log(a, (mp_digit)base, c);
24+
}
25+
26+
return MP_VAL;
27+
}
28+
29+
#endif

mp_log_u32.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)