Skip to content

Commit 3ee4c5e

Browse files
committed
Replace the mp_to_decimal macro with a function...
that chooses a new internal function that uses Barrett reduction to speed up stringifying large integers to base 10 if it's available and the number is above a cutoff size, otherwise it just falls back to mp_to_radix.
1 parent c63799c commit 3ee4c5e

13 files changed

+385
-36
lines changed

bn_mp_to_decimal.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_TO_DECIMAL_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
/* stores a bignum as a decimal ASCII string, using Barrett
7+
* reduction if available.
8+
*/
9+
10+
mp_err mp_to_decimal(const mp_int *a, char *str, size_t maxlen)
11+
{
12+
mp_err err;
13+
14+
if (MP_HAS(S_MP_TO_DECIMAL_FAST) && (a->used > 10)) {
15+
err = s_mp_to_decimal_fast(a, str, maxlen);
16+
} else {
17+
err = mp_to_radix(a, str, maxlen, 10);
18+
}
19+
20+
return err;
21+
}
22+
23+
#endif

bn_s_mp_to_decimal_fast.c

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#include "tommath_private.h"
2+
#include <string.h>
3+
#ifdef BN_S_MP_TO_DECIMAL_FAST_C
4+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
5+
/* SPDX-License-Identifier: Unlicense */
6+
7+
/* store a bignum as a decimal ASCII string */
8+
mp_err s_mp_to_decimal_fast_rec(const mp_int *number, mp_int *nL, mp_int *shiftL, mp_int *mL, int precalc_array_index,
9+
int left,
10+
char **result,
11+
size_t *maxlen)
12+
{
13+
mp_int q, nLq, r;
14+
mp_err err;
15+
16+
if (precalc_array_index < 0) {
17+
int n = mp_get_i32(number), n2 = n, t = 0, c;
18+
char *i = *result;
19+
char s[4] = "000";
20+
21+
while (n) {
22+
s[2 - t] = mp_s_rmap[n % 10];
23+
t++;
24+
n /= 10;
25+
}
26+
27+
if (!left && n2 < 100) {
28+
t++;
29+
if (n2 < 10) {
30+
t++;
31+
}
32+
if (n2 == 0) {
33+
t++;
34+
}
35+
}
36+
37+
if (*maxlen < (size_t)t || (*maxlen -= (size_t)t) < 1) {
38+
/* no more room */
39+
return MP_VAL;
40+
}
41+
42+
for (c = 0; c < t; c++) {
43+
i[c] = s[3 - t + c];
44+
}
45+
46+
*result += t;
47+
48+
return MP_OKAY;
49+
}
50+
51+
if ((err = mp_init_multi(&q, &nLq, &r, NULL)) != MP_OKAY) {
52+
goto LBL_ERR;
53+
}
54+
if ((err = mp_mul(number, &mL[precalc_array_index], &q)) != MP_OKAY) {
55+
goto LBL_ERR;
56+
}
57+
if ((err = mp_div_2d(&q, mp_get_i32(&shiftL[precalc_array_index]), &q, NULL)) != MP_OKAY) {
58+
goto LBL_ERR;
59+
}
60+
61+
if ((err = mp_mul(&nL[precalc_array_index], &q, &nLq)) != MP_OKAY) {
62+
goto LBL_ERR;
63+
}
64+
65+
if ((err = mp_sub(number, &nLq, &r)) != MP_OKAY) {
66+
goto LBL_ERR;
67+
}
68+
69+
if (mp_isneg(&r)) {
70+
if ((err = mp_sub_d(&q, 1, &q)) != MP_OKAY) {
71+
goto LBL_ERR;
72+
}
73+
if ((err = mp_add(&r, &nL[precalc_array_index], &r)) != MP_OKAY) {
74+
goto LBL_ERR;
75+
}
76+
}
77+
78+
--precalc_array_index;
79+
if (left && mp_iszero(&q)) {
80+
if ((err = s_mp_to_decimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 1, result, maxlen)) != MP_OKAY) {
81+
goto LBL_ERR;
82+
}
83+
} else {
84+
if ((err = s_mp_to_decimal_fast_rec(&q, nL, shiftL, mL, precalc_array_index, left, result, maxlen)) != MP_OKAY) {
85+
goto LBL_ERR;
86+
}
87+
if ((err = s_mp_to_decimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 0, result, maxlen)) != MP_OKAY) {
88+
goto LBL_ERR;
89+
}
90+
}
91+
92+
err = MP_OKAY;
93+
94+
LBL_ERR:
95+
mp_clear_multi(&q, &nLq, &r, NULL);
96+
return err;
97+
}
98+
99+
mp_err s_mp_to_decimal_fast(const mp_int *a, char *result, size_t maxlen)
100+
{
101+
mp_int number, n, shift, M, M2, M22, M4, M44;
102+
mp_int nL[20], shiftL[20], mL[20];
103+
mp_err err;
104+
char **result_addr = &result;
105+
int precalc_array_index = 1, c;
106+
107+
if ((err = mp_init_multi(&n, &M, &M2, &M22, &M4, &M44, &mL[0], NULL)) != MP_OKAY) {
108+
goto LBL_ERR;
109+
}
110+
111+
if ((err = mp_init_copy(&number, a)) != MP_OKAY) {
112+
goto LBL_ERR;
113+
}
114+
if (mp_isneg(&number)) {
115+
if ((err = mp_neg(&number, &number)) != MP_OKAY) {
116+
goto LBL_ERR;
117+
}
118+
result[0] = '-';
119+
*result_addr += 1;
120+
maxlen -= 1;
121+
}
122+
mp_set_u32(&n, 1000);
123+
124+
if ((err = mp_init_copy(&nL[0], &n)) != MP_OKAY) {
125+
goto LBL_ERR;
126+
}
127+
128+
if ((err = mp_init_set(&shift, (mp_digit)20)) != MP_OKAY) {
129+
goto LBL_ERR;
130+
}
131+
132+
if ((err = mp_init_copy(&shiftL[0], &shift)) != MP_OKAY) {
133+
goto LBL_ERR;
134+
}
135+
136+
/* (8 * 2**$shift) / $n rounded up */
137+
mp_set_u32(&M, 8389);
138+
139+
/* $M / 8, rounded up */
140+
mp_set_u32(&mL[0], 1049);
141+
142+
while (1) {
143+
if ((err = mp_sqr(&n, &n)) != MP_OKAY) {
144+
goto LBL_ERR;
145+
}
146+
if (mp_cmp(&n, &number) == MP_GT) {
147+
break;
148+
}
149+
150+
if ((err = mp_mul_2(&shift, &shift)) != MP_OKAY) {
151+
goto LBL_ERR;
152+
}
153+
154+
/* The following is a Newton-Raphson step, to restore the invariant
155+
* that $M is (8 * 2**$shift) / $n, rounded up. */
156+
{
157+
if ((err = mp_sqr(&M, &M2)) != MP_OKAY) {
158+
goto LBL_ERR;
159+
}
160+
if ((err = mp_sqr(&M2, &M4)) != MP_OKAY) {
161+
goto LBL_ERR;
162+
}
163+
164+
if ((err = mp_mul(&M4, &n, &M4)) != MP_OKAY) {
165+
goto LBL_ERR;
166+
}
167+
if ((err = mp_div_2d(&M4, mp_get_i32(&shift) + 6, &M4, NULL)) != MP_OKAY) {
168+
goto LBL_ERR;
169+
}
170+
if ((err = mp_mul_2(&M2, &M2)) != MP_OKAY) {
171+
goto LBL_ERR;
172+
}
173+
if ((err = mp_sub(&M4, &M2, &M4)) != MP_OKAY) {
174+
goto LBL_ERR;
175+
}
176+
if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) {
177+
goto LBL_ERR;
178+
}
179+
if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) {
180+
goto LBL_ERR;
181+
}
182+
if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) {
183+
goto LBL_ERR;
184+
}
185+
if ((err = mp_neg(&M4, &M)) != MP_OKAY) {
186+
goto LBL_ERR;
187+
}
188+
}
189+
190+
if ((err = mp_init_copy(&nL[precalc_array_index], &n)) != MP_OKAY) {
191+
goto LBL_ERR;
192+
}
193+
if ((err = mp_init_copy(&shiftL[precalc_array_index], &shift)) != MP_OKAY) {
194+
goto LBL_ERR;
195+
}
196+
197+
/* Divide by 8, round up */
198+
{
199+
if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) {
200+
goto LBL_ERR;
201+
}
202+
if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) {
203+
goto LBL_ERR;
204+
}
205+
if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) {
206+
goto LBL_ERR;
207+
}
208+
if ((err = mp_neg(&M4, &M4)) != MP_OKAY) {
209+
goto LBL_ERR;
210+
}
211+
}
212+
if ((err = mp_init_copy(&mL[precalc_array_index], &M4)) != MP_OKAY) {
213+
goto LBL_ERR;
214+
}
215+
precalc_array_index++;
216+
}
217+
218+
if ((err = s_mp_to_decimal_fast_rec(&number, nL, shiftL, mL, precalc_array_index - 1, 1, result_addr,
219+
&maxlen)) != MP_OKAY) {
220+
goto LBL_ERR;
221+
}
222+
*result_addr[0] = '\0';
223+
224+
err = MP_OKAY;
225+
226+
LBL_ERR:
227+
mp_clear_multi(&number, &n, &shift, &M, &M2, &M22, &M4, &M44, NULL);
228+
for (c = 0; c < precalc_array_index; c++) {
229+
mp_clear_multi(&nL[c], &shiftL[c], &mL[c], NULL);
230+
}
231+
return err;
232+
}
233+
234+
#endif

demo/test.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,54 @@ static int test_s_mp_toom_sqr(void)
22142214
return EXIT_FAILURE;
22152215
}
22162216

2217+
static int test_mp_to_decimal(void)
2218+
{
2219+
mp_int a, b;
2220+
int size, err, strlength;
2221+
char *str;
2222+
2223+
if ((err = mp_init_multi(&a, &b, NULL)) != MP_OKAY) {
2224+
goto LTM_ERR;
2225+
}
2226+
for (size = 1; size < 1000; size += 10) {
2227+
int times;
2228+
printf("Testing mp_to_decimal: %5d bits \r", size);
2229+
fflush(stdout);
2230+
for (times = 0; times < 5; times++) {
2231+
if ((err = mp_rand(&a, size)) != MP_OKAY) {
2232+
goto LTM_ERR;
2233+
}
2234+
if (times % 2) {
2235+
/* also test some negative numbers */
2236+
if ((err = mp_neg(&a, &a)) != MP_OKAY) {
2237+
goto LTM_ERR;
2238+
}
2239+
}
2240+
if ((err = mp_radix_size(&a, 10, &strlength)) != MP_OKAY) {
2241+
goto LTM_ERR;
2242+
}
2243+
str = (char *)malloc((size_t)strlength);
2244+
if ((err = mp_to_decimal(&a, str, (size_t)strlength)) != MP_OKAY) {
2245+
goto LTM_ERR;
2246+
}
2247+
if ((err = mp_read_radix(&b, str, 10)) != MP_OKAY) {
2248+
goto LTM_ERR;
2249+
}
2250+
free(str);
2251+
if (mp_cmp(&a, &b) != MP_EQ) {
2252+
fprintf(stderr, "s_mp_to_decimal_fast failed at size %d\n", size);
2253+
goto LTM_ERR;
2254+
}
2255+
}
2256+
}
2257+
2258+
mp_clear_multi(&a, &b, NULL);
2259+
return EXIT_SUCCESS;
2260+
LTM_ERR:
2261+
mp_clear_multi(&a, &b, NULL);
2262+
return EXIT_FAILURE;
2263+
}
2264+
22172265
int unit_tests(int argc, char **argv)
22182266
{
22192267
static const struct {
@@ -2264,8 +2312,10 @@ int unit_tests(int argc, char **argv)
22642312
T1(s_mp_karatsuba_sqr, S_MP_KARATSUBA_SQR),
22652313
T1(s_mp_toom_mul, S_MP_TOOM_MUL),
22662314
T1(s_mp_toom_sqr, S_MP_TOOM_SQR),
2315+
T1(mp_to_decimal, S_MP_TO_DECIMAL_FAST)
22672316
#undef T2
22682317
#undef T1
2318+
#undef T
22692319
};
22702320
unsigned long i, ok, fail, nop;
22712321
uint64_t t;

libtommath_VS2008.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,10 @@
816816
RelativePath="bn_mp_submod.c"
817817
>
818818
</File>
819+
<File
820+
RelativePath="bn_mp_to_decimal.c"
821+
>
822+
</File>
819823
<File
820824
RelativePath="bn_mp_to_radix.c"
821825
>
@@ -936,6 +940,10 @@
936940
RelativePath="bn_s_mp_sub.c"
937941
>
938942
</File>
943+
<File
944+
RelativePath="bn_s_mp_to_decimal_fast.c"
945+
>
946+
</File>
939947
<File
940948
RelativePath="bn_s_mp_toom_mul.c"
941949
>

makefile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.
4848
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 \
4949
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 \
5050
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_radix.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_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \
53-
bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o \
54-
bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
55-
bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
56-
bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
57-
bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
51+
bn_mp_submod.o bn_mp_to_decimal.o bn_mp_to_radix.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
52+
bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o \
53+
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_to_decimal_fast.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
5859

5960
#END_INS
6061

makefile.mingw

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.
5151
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 \
5252
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 \
5353
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 \
54-
bn_mp_submod.o bn_mp_to_radix.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o \
55-
bn_mp_to_unsigned_bin_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \
56-
bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o \
57-
bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
58-
bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
59-
bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
60-
bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
54+
bn_mp_submod.o bn_mp_to_decimal.o bn_mp_to_radix.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
55+
bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o \
56+
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 \
57+
bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o \
58+
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 \
59+
bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o \
60+
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 \
61+
bn_s_mp_sub.o bn_s_mp_to_decimal_fast.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
6162

6263
HEADERS_PUB=tommath.h
6364
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)

0 commit comments

Comments
 (0)