Skip to content

Commit

Permalink
remove _u32 suffix from mp_(expt|log|root) functions, use int for now
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Nov 5, 2019
1 parent c893d21 commit 2e345de
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 117 deletions.
60 changes: 30 additions & 30 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ static int test_mp_sqrt(void)
printf("\nmp_sqrt() error!");
goto LBL_ERR;
}
DO(mp_root_u32(&a, 2u, &c));
DO(mp_root(&a, 2u, &c));
if (mp_cmp_mag(&b, &c) != MP_EQ) {
printf("mp_sqrt() bad result!\n");
goto LBL_ERR;
Expand Down Expand Up @@ -1396,10 +1396,10 @@ static int test_mp_reduce_2k_l(void)
/* stripped down version of mp_radix_size. The faster version can be off by up t
o +3 */
/* TODO: This function should be removed, replaced by mp_radix_size, mp_radix_size_overestimate in 2.0 */
static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
static mp_err s_rs(const mp_int *a, int radix, int *size)
{
mp_err res;
uint32_t digs = 0u;
int digs = 0u;
mp_int t;
mp_digit d;
*size = 0u;
Expand All @@ -1408,7 +1408,7 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
return MP_OKAY;
}
if (radix == 2) {
*size = (uint32_t)mp_count_bits(a) + 1u;
*size = mp_count_bits(a) + 1;
return MP_OKAY;
}
DOR(mp_init_copy(&t, a));
Expand All @@ -1424,12 +1424,12 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
*size = digs + 1;
return MP_OKAY;
}
static int test_mp_log_u32(void)
static int test_mp_log(void)
{
mp_int a;
mp_digit d;
uint32_t base, lb, size;
const uint32_t max_base = MP_MIN(UINT32_MAX, MP_DIGIT_MAX);
int base, lb, size;
const int max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);

DOR(mp_init(&a));

Expand All @@ -1440,11 +1440,11 @@ static int test_mp_log_u32(void)
*/
mp_set(&a, 42u);
base = 0u;
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
if (mp_log(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
}
base = 1u;
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
if (mp_log(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
}
/*
Expand All @@ -1456,14 +1456,14 @@ static int test_mp_log_u32(void)
*/
base = 2u;
mp_zero(&a);
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
if (mp_log(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
}

for (d = 1; d < 4; d++) {
mp_set(&a, d);
DO(mp_log_u32(&a, base, &lb));
if (lb != ((d == 1)?0uL:1uL)) {
DO(mp_log(&a, base, &lb));
if (lb != ((d == 1)?0:1)) {
goto LBL_ERR;
}
}
Expand All @@ -1476,13 +1476,13 @@ static int test_mp_log_u32(void)
*/
base = 3u;
mp_zero(&a);
if (mp_log_u32(&a, base, &lb) != MP_VAL) {
if (mp_log(&a, base, &lb) != MP_VAL) {
goto LBL_ERR;
}
for (d = 1; d < 4; d++) {
mp_set(&a, d);
DO(mp_log_u32(&a, base, &lb));
if (lb != ((d < base)?0uL:1uL)) {
DO(mp_log(&a, base, &lb));
if (lb != (((int)d < base)?0:1)) {
goto LBL_ERR;
}
}
Expand All @@ -1493,8 +1493,8 @@ static int test_mp_log_u32(void)
radix_size.
*/
DO(mp_rand(&a, 10));
for (base = 2u; base < 65u; base++) {
DO(mp_log_u32(&a, base, &lb));
for (base = 2; base < 65; base++) {
DO(mp_log(&a, base, &lb));
DO(s_rs(&a,(int)base, &size));
/* radix_size includes the memory needed for '\0', too*/
size -= 2;
Expand All @@ -1508,8 +1508,8 @@ static int test_mp_log_u32(void)
test the part of mp_ilogb that uses native types.
*/
DO(mp_rand(&a, 1));
for (base = 2u; base < 65u; base++) {
DO(mp_log_u32(&a, base, &lb));
for (base = 2; base < 65; base++) {
DO(mp_log(&a, base, &lb));
DO(s_rs(&a,(int)base, &size));
size -= 2;
if (lb != size) {
Expand All @@ -1519,9 +1519,9 @@ static int test_mp_log_u32(void)

/*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10 */
mp_set(&a, max_base);
DO(mp_expt_u32(&a, 10u, &a));
DO(mp_add_d(&a, max_base / 2u, &a));
DO(mp_log_u32(&a, max_base, &lb));
DO(mp_expt(&a, 10uL, &a));
DO(mp_add_d(&a, max_base / 2, &a));
DO(mp_log(&a, max_base, &lb));
if (lb != 10u) {
goto LBL_ERR;
}
Expand Down Expand Up @@ -1658,7 +1658,7 @@ static int test_mp_decr(void)
low-mp branch.
*/

static int test_mp_root_u32(void)
static int test_mp_root(void)
{
mp_int a, c, r;
int i, j;
Expand Down Expand Up @@ -1850,10 +1850,10 @@ static int test_mp_root_u32(void)
for (i = 0; i < 10; i++) {
DO(mp_read_radix(&a, input[i], 64));
for (j = 3; j < 100; j++) {
DO(mp_root_u32(&a, (uint32_t)j, &c));
DO(mp_root(&a, j, &c));
DO(mp_read_radix(&r, root[i][j-3], 10));
if (mp_cmp(&r, &c) != MP_EQ) {
fprintf(stderr, "mp_root_u32 failed at input #%d, root #%d\n", i, j);
fprintf(stderr, "mp_root failed at input #%d, root #%d\n", i, j);
goto LBL_ERR;
}
}
Expand Down Expand Up @@ -2037,8 +2037,8 @@ static int test_mp_radix_size(void)
DOR(mp_init(&a));

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

for (radix = 2; radix < 65; radix++) {
DO(mp_radix_size(&a, radix, &size));
Expand Down Expand Up @@ -2304,13 +2304,13 @@ static int unit_tests(int argc, char **argv)
T1(mp_get_u32, MP_GET_I32),
T1(mp_get_u64, MP_GET_I64),
T1(mp_get_ul, MP_GET_L),
T1(mp_log_u32, MP_LOG_U32),
T1(mp_log, MP_LOG),
T1(mp_incr, MP_ADD_D),
T1(mp_invmod, MP_INVMOD),
T1(mp_is_square, MP_IS_SQUARE),
T1(mp_kronecker, MP_KRONECKER),
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
T1(mp_root_u32, MP_ROOT_U32),
T1(mp_root, MP_ROOT),
T1(mp_or, MP_OR),
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
Expand All @@ -2326,7 +2326,7 @@ static int unit_tests(int argc, char **argv)
T1(mp_set_double, MP_SET_DOUBLE),
#endif
T1(mp_signed_rsh, MP_SIGNED_RSH),
T1(mp_sqrt, MP_SQRT),
T2(mp_sqrt, MP_SQRT, MP_ROOT),
T1(mp_sqrtmod_prime, MP_SQRTMOD_PRIME),
T1(mp_xor, MP_XOR),
T2(s_mp_div_recursive, S_MP_DIV_RECURSIVE, S_MP_DIV_SCHOOL),
Expand Down
26 changes: 11 additions & 15 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1906,9 +1906,9 @@ \section{Combined Modular Reduction}

\chapter{Exponentiation}
\section{Single Digit Exponentiation}
\index{mp\_expt\_u32}
\index{mp\_expt}
\begin{alltt}
mp_err mp_expt_u32 (const mp_int *a, uint32_t b, mp_int *c)
mp_err mp_expt (const mp_int *a, int b, int *c)
\end{alltt}
This function computes $c = a^b$.

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

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

\index{mp\_log\_u32}
\index{mp\_log}
\begin{alltt}
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
mp_err mp_log(const mp_int *a, int base, int *c)
\end{alltt}

\subsection{Example}
Expand All @@ -1976,7 +1976,7 @@ \subsection{Example}
int main(int argc, char **argv)
{
mp_int x, output;
uint32_t base;
int base;
mp_err e;
if (argc != 3) {
Expand All @@ -1989,12 +1989,8 @@ \subsection{Example}
exit(EXIT_FAILURE);
}
errno = 0;
#ifdef MP_64BIT
/* Check for overflow skipped */
base = (uint32_t)strtoull(argv[1], NULL, 10);
#else
base = (uint32_t)strtoul(argv[1], NULL, 10);
#endif
base = (int)strtoul(argv[1], NULL, 10);
if (errno == ERANGE) {
fprintf(stderr,"strtoul(l) failed: input out of range\textbackslash{}n");
exit(EXIT_FAILURE);
Expand All @@ -2004,8 +2000,8 @@ \subsection{Example}
mp_error_to_string(e));
exit(EXIT_FAILURE);
}
if ((e = mp_log_u32(&x, base, &output)) != MP_OKAY) {
fprintf(stderr,"mp_ilogb failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
if ((e = mp_log(&x, base, &output)) != MP_OKAY) {
fprintf(stderr,"mp_log failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
mp_error_to_string(e));
exit(EXIT_FAILURE);
}
Expand Down
13 changes: 5 additions & 8 deletions mp_expt_u32.c → mp_expt.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "tommath_private.h"
#ifdef MP_EXPT_U32_C
#ifdef MP_EXPT_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_u32(const mp_int *a, uint32_t b, mp_int *c)
mp_err mp_expt(const mp_int *a, int b, mp_int *c)
{
mp_err err;

mp_int g;

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

while (b > 0u) {
while (b > 0) {
/* if the bit is set multiply */
if ((b & 1u) != 0u) {
if ((b & 1) != 0) {
if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
goto LBL_ERR;
}
}

/* square */
if (b > 1u) {
if (b > 1) {
if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
goto LBL_ERR;
}
Expand All @@ -36,8 +35,6 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
b >>= 1;
}

err = MP_OKAY;

LBL_ERR:
mp_clear(&g);
return err;
Expand Down
14 changes: 7 additions & 7 deletions mp_log_u32.c → mp_log.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "tommath_private.h"
#ifdef MP_LOG_U32_C
#ifdef MP_LOG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
mp_err mp_log(const mp_int *a, int base, int *c)
{
if (a->sign == MP_NEG) {
return MP_VAL;
Expand All @@ -13,22 +13,22 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
return MP_VAL;
}

if (base < 2u) {
if (base < 2 || (unsigned)base > (unsigned)MP_DIGIT_MAX) {
return MP_VAL;
}

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

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

if (MP_HAS(S_MP_LOG)) {
return s_mp_log(a, base, c);
return s_mp_log(a, (mp_digit)base, c);
}

return MP_VAL;
Expand Down
11 changes: 5 additions & 6 deletions mp_radix_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
{
mp_err err;
mp_int a_;
uint32_t b;
int b;

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

a_ = *a;
a_.sign = MP_ZPOS;
if ((err = mp_log_u32(&a_, (uint32_t)radix, &b)) != MP_OKAY) {
goto LBL_ERR;
if ((err = mp_log(&a_, radix, &b)) != MP_OKAY) {
return err;
}

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

LBL_ERR:
return err;
return MP_OKAY;
}
#endif
Loading

0 comments on commit 2e345de

Please sign in to comment.