Skip to content

Commit 4c356b8

Browse files
committed
fixed problem with overflow
1 parent e58a83d commit 4c356b8

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

s_mp_faster_to_radix.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static int32_t s_floor_ilog2(int32_t value)
1414
}
1515

1616
/* Exponentiation with small footprint */
17-
/*
17+
1818
static int32_t s_pow(int32_t base, int32_t exponent)
1919
{
2020
int32_t result = 1;
@@ -23,11 +23,14 @@ static int32_t s_pow(int32_t base, int32_t exponent)
2323
result *= base;
2424
}
2525
exponent /= 2;
26+
if (exponent == 0) {
27+
break;
28+
}
2629
base *= base;
2730
}
2831
return result;
2932
}
30-
*/
33+
3134
static mp_err s_mp_to_radix_recursive(const mp_int *a, char **str, size_t *part_maxlen, size_t *part_written,
3235
int radix, int32_t k, int32_t t, bool pad, mp_int *P, mp_int *R)
3336
{
@@ -101,20 +104,13 @@ mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *w
101104
size_t part_written = 0;
102105
size_t part_maxlen = maxlen;
103106

104-
mp_int N;
105-
106107
/* List of reciprocals */
107108
mp_int *R = NULL;
108109
/* List of moduli */
109110
mp_int *P = NULL;
110111

111112
/* Denominator for the reciprocal: b^y */
112-
/*n = s_pow((int32_t)radix, (int32_t)s_mp_radix_exponent_y[radix]);*/
113-
/* FIXME: either do it full in bigint or fix the problem with the sanitizer */
114-
if ((err = mp_init_i32(&N,(int32_t)radix)) != MP_OKAY) goto LTM_ERR;
115-
if ((err = mp_expt_n(&N, (int)s_mp_radix_exponent_y[radix], &N)) != MP_OKAY) goto LTM_ERR;
116-
n = mp_get_i32(&N);
117-
mp_clear(&N);
113+
n = s_pow((int32_t)radix, (int32_t)s_mp_radix_exponent_y[radix]);
118114

119115
/* Numerator of the reciprocal: ceil(log_2(n)) */
120116
k = s_floor_ilog2(n) + 1;

tommath_class.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,6 @@
10691069
# define MP_DIV_2D_C
10701070
# define MP_DIV_C
10711071
# define MP_EXPT_N_C
1072-
# define MP_GET_I32_C
10731072
# define MP_INIT_C
10741073
# define MP_INIT_I32_C
10751074
# define MP_INIT_MULTI_C

0 commit comments

Comments
 (0)