|
6 | 6 | /* SPDX-License-Identifier: Unlicense */ |
7 | 7 |
|
8 | 8 | /* store a bignum as a decimal ASCII string */ |
9 | | -mp_err mp_todecimal_fast_rec(mp_int *number, mp_int *nL, mp_int *shiftL, mp_int *mL, int precalc_array_index, int left, char **result) { |
10 | | - mp_int q, nLq, r; |
11 | | - mp_err err; |
12 | | - |
13 | | - if (precalc_array_index < 0) { |
14 | | - char *next_piece = calloc(4, sizeof(char)); |
15 | | - int s_s = left ? snprintf(next_piece, 4, "%u", mp_get_i32(number)) : snprintf(next_piece, 4, "%03u", mp_get_i32(number)); |
16 | | - size_t r_s = strlen(*result); |
17 | | - (*result) = realloc(*result, r_s + s_s + 2); |
18 | | - strcat(*result, next_piece); |
19 | | - return MP_OKAY; |
20 | | - } |
21 | | - |
22 | | - if ((err = mp_init_multi(&q, &nLq, &r, NULL)) != MP_OKAY) { |
23 | | - goto LBL_ERR; |
24 | | - } |
25 | | - if ((err = mp_mul(number, &mL[precalc_array_index], &q)) != MP_OKAY) { |
26 | | - goto LBL_ERR; |
27 | | - } |
28 | | - if ((err = mp_div_2d(&q, mp_get_i32(&shiftL[precalc_array_index]), &q, NULL)) != MP_OKAY) { |
29 | | - goto LBL_ERR; |
30 | | - } |
31 | | - |
32 | | - if ((err = mp_mul(&nL[precalc_array_index], &q, &nLq)) != MP_OKAY) { |
33 | | - goto LBL_ERR; |
34 | | - } |
35 | | - |
36 | | - if ((err = mp_sub(number, &nLq, &r)) != MP_OKAY) { |
37 | | - goto LBL_ERR; |
38 | | - } |
39 | | - |
40 | | - if (mp_isneg(&r)) { |
41 | | - if ((err = mp_sub_d(&q, 1, &q)) != MP_OKAY) { |
| 9 | +mp_err mp_todecimal_fast_rec(mp_int *number, mp_int *nL, mp_int *shiftL, mp_int *mL, int precalc_array_index, int left, |
| 10 | + char **result) |
| 11 | +{ |
| 12 | + mp_int q, nLq, r; |
| 13 | + mp_err err; |
| 14 | + |
| 15 | + if (precalc_array_index < 0) { |
| 16 | + char *next_piece = calloc(4, sizeof(char)); |
| 17 | + int s_s = left ? snprintf(next_piece, 4, "%u", mp_get_i32(number)) : snprintf(next_piece, 4, "%03u", |
| 18 | + mp_get_i32(number)); |
| 19 | + size_t r_s = strlen(*result); |
| 20 | + (*result) = realloc(*result, r_s + s_s + 2); |
| 21 | + strcat(*result, next_piece); |
| 22 | + return MP_OKAY; |
| 23 | + } |
| 24 | + |
| 25 | + if ((err = mp_init_multi(&q, &nLq, &r, NULL)) != MP_OKAY) { |
| 26 | + goto LBL_ERR; |
| 27 | + } |
| 28 | + if ((err = mp_mul(number, &mL[precalc_array_index], &q)) != MP_OKAY) { |
| 29 | + goto LBL_ERR; |
| 30 | + } |
| 31 | + if ((err = mp_div_2d(&q, mp_get_i32(&shiftL[precalc_array_index]), &q, NULL)) != MP_OKAY) { |
| 32 | + goto LBL_ERR; |
| 33 | + } |
| 34 | + |
| 35 | + if ((err = mp_mul(&nL[precalc_array_index], &q, &nLq)) != MP_OKAY) { |
| 36 | + goto LBL_ERR; |
| 37 | + } |
| 38 | + |
| 39 | + if ((err = mp_sub(number, &nLq, &r)) != MP_OKAY) { |
| 40 | + goto LBL_ERR; |
| 41 | + } |
| 42 | + |
| 43 | + if (mp_isneg(&r)) { |
| 44 | + if ((err = mp_sub_d(&q, 1, &q)) != MP_OKAY) { |
| 45 | + goto LBL_ERR; |
| 46 | + } |
| 47 | + if ((err = mp_add(&r, &nL[precalc_array_index], &r)) != MP_OKAY) { |
| 48 | + goto LBL_ERR; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + --precalc_array_index; |
| 53 | + if (left && mp_iszero(&q)) { |
| 54 | + if ((err = mp_todecimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 1, result)) != MP_OKAY) { |
| 55 | + goto LBL_ERR; |
| 56 | + } |
| 57 | + } else { |
| 58 | + if ((err = mp_todecimal_fast_rec(&q, nL, shiftL, mL, precalc_array_index, left, result)) != MP_OKAY) { |
| 59 | + goto LBL_ERR; |
| 60 | + } |
| 61 | + if ((err = mp_todecimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 0, result)) != MP_OKAY) { |
| 62 | + goto LBL_ERR; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + err = MP_OKAY; |
| 67 | + |
| 68 | +LBL_ERR: |
| 69 | + mp_clear_multi(&q, &nLq, &r, NULL); |
| 70 | + return err; |
| 71 | +} |
| 72 | + |
| 73 | +mp_err mp_todecimal_fast(mp_int *number, char **result) |
| 74 | +{ |
| 75 | + mp_int n, shift, M, M2, M22, M4, M44; |
| 76 | + mp_int *nL, *shiftL, *mL; |
| 77 | + mp_err err; |
| 78 | + int precalc_array_index = 1; |
| 79 | + |
| 80 | + if ((err = mp_init_multi(&M2, &M22, &M4, &M44, NULL)) != MP_OKAY) { |
| 81 | + goto LBL_ERR; |
| 82 | + } |
| 83 | + |
| 84 | + if (mp_isneg(number)) { |
| 85 | + if ((err = mp_neg(number, number)) != MP_OKAY) { |
| 86 | + goto LBL_ERR; |
| 87 | + } |
| 88 | + *result[0] = '-'; |
| 89 | + } |
| 90 | + if ((err = mp_init_set(&n, (mp_digit)1000)) != MP_OKAY) { |
| 91 | + goto LBL_ERR; |
| 92 | + } |
| 93 | + |
| 94 | + nL = malloc(20 * sizeof(mp_int)); |
| 95 | + if ((err = mp_init_copy(&nL[0], &n)) != MP_OKAY) { |
| 96 | + goto LBL_ERR; |
| 97 | + } |
| 98 | + |
| 99 | + if ((err = mp_init_set(&shift, (mp_digit)20)) != MP_OKAY) { |
| 100 | + goto LBL_ERR; |
| 101 | + } |
| 102 | + |
| 103 | + shiftL = malloc(20 * sizeof(mp_int)); |
| 104 | + if ((err = mp_init_copy(&shiftL[0], &shift)) != MP_OKAY) { |
| 105 | + goto LBL_ERR; |
| 106 | + } |
| 107 | + |
| 108 | + /* (8 * 2**$shift) / $n rounded up */ |
| 109 | + if ((err = mp_init_set(&M, (mp_digit)8389)) != MP_OKAY) { |
| 110 | + goto LBL_ERR; |
| 111 | + } |
| 112 | + |
| 113 | + /* $M / 8, rounded up */ |
| 114 | + mL = malloc(20 * sizeof(mp_int)); |
| 115 | + if ((err = mp_init_set(&mL[0], (mp_digit)1049)) != MP_OKAY) { |
| 116 | + goto LBL_ERR; |
| 117 | + } |
| 118 | + |
| 119 | + while (1) { |
| 120 | + if ((err = mp_sqr(&n, &n)) != MP_OKAY) { |
| 121 | + goto LBL_ERR; |
| 122 | + } |
| 123 | + if (mp_cmp(&n, number) == MP_GT) { |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + if ((err = mp_mul_2(&shift, &shift)) != MP_OKAY) { |
| 128 | + goto LBL_ERR; |
| 129 | + } |
| 130 | + |
| 131 | + /* The following is a Newton-Raphson step, to restore the invariant |
| 132 | + * that $M is (8 * 2**$shift) / $n, rounded up. */ |
| 133 | + { |
| 134 | + if ((err = mp_sqr(&M, &M2)) != MP_OKAY) { |
42 | 135 | goto LBL_ERR; |
43 | | - } |
44 | | - if ((err = mp_add(&r, &nL[precalc_array_index], &r)) != MP_OKAY) { |
| 136 | + } |
| 137 | + if ((err = mp_sqr(&M2, &M4)) != MP_OKAY) { |
45 | 138 | goto LBL_ERR; |
46 | | - } |
47 | | - } |
| 139 | + } |
48 | 140 |
|
49 | | - --precalc_array_index; |
50 | | - if (left && mp_iszero(&q)) { |
51 | | - if ((err = mp_todecimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 1, result)) != MP_OKAY) { |
| 141 | + if ((err = mp_mul(&M4, &n, &M4)) != MP_OKAY) { |
52 | 142 | goto LBL_ERR; |
53 | | - } |
54 | | - } else { |
55 | | - if ((err = mp_todecimal_fast_rec(&q, nL, shiftL, mL, precalc_array_index, left, result)) != MP_OKAY) { |
| 143 | + } |
| 144 | + if ((err = mp_div_2d(&M4, mp_get_l(&shift) + 6, &M4, NULL)) != MP_OKAY) { |
56 | 145 | goto LBL_ERR; |
57 | | - } |
58 | | - if ((err = mp_todecimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 0, result)) != MP_OKAY) { |
| 146 | + } |
| 147 | + if ((err = mp_mul_2(&M2, &M2)) != MP_OKAY) { |
59 | 148 | goto LBL_ERR; |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - err = MP_OKAY; |
64 | | - |
65 | | -LBL_ERR: |
66 | | - mp_clear_multi (&q, &nLq, &r, NULL); |
67 | | - return err; |
68 | | -} |
69 | | - |
70 | | -mp_err mp_todecimal_fast(mp_int *number, char **result) { |
71 | | - mp_int n, shift, M, M2, M22, M4, M44; |
72 | | - mp_int *nL, *shiftL, *mL; |
73 | | - mp_err err; |
74 | | - int precalc_array_index = 1; |
75 | | - |
76 | | - if ((err = mp_init_multi(&M2, &M22, &M4, &M44, NULL)) != MP_OKAY) { |
77 | | - goto LBL_ERR; |
78 | | - } |
79 | | - |
80 | | - if (mp_isneg(number)) { |
81 | | - if ((err = mp_neg(number, number)) != MP_OKAY) { |
| 149 | + } |
| 150 | + if ((err = mp_sub(&M4, &M2, &M4)) != MP_OKAY) { |
82 | 151 | goto LBL_ERR; |
83 | | - } |
84 | | - *result[0] = '-'; |
85 | | - } |
86 | | - if ((err = mp_init_set(&n, (mp_digit)1000)) != MP_OKAY) { |
87 | | - goto LBL_ERR; |
88 | | - } |
89 | | - |
90 | | - nL = malloc(20 * sizeof(mp_int)); |
91 | | - if ((err = mp_init_copy(&nL[0], &n)) != MP_OKAY) { |
92 | | - goto LBL_ERR; |
93 | | - } |
94 | | - |
95 | | - if ((err = mp_init_set(&shift, (mp_digit)20)) != MP_OKAY) { |
96 | | - goto LBL_ERR; |
97 | | - } |
98 | | - |
99 | | - shiftL = malloc(20 * sizeof(mp_int)); |
100 | | - if ((err = mp_init_copy(&shiftL[0], &shift)) != MP_OKAY) { |
101 | | - goto LBL_ERR; |
102 | | - } |
103 | | - |
104 | | - /* (8 * 2**$shift) / $n rounded up */ |
105 | | - if ((err = mp_init_set(&M, (mp_digit)8389)) != MP_OKAY) { |
106 | | - goto LBL_ERR; |
107 | | - } |
108 | | - |
109 | | - /* $M / 8, rounded up */ |
110 | | - mL = malloc(20 * sizeof(mp_int)); |
111 | | - if ((err = mp_init_set(&mL[0], (mp_digit)1049)) != MP_OKAY) { |
112 | | - goto LBL_ERR; |
113 | | - } |
114 | | - |
115 | | - while (1) { |
116 | | - if ((err = mp_sqr(&n, &n)) != MP_OKAY) { |
| 152 | + } |
| 153 | + if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) { |
117 | 154 | goto LBL_ERR; |
118 | | - } |
119 | | - if (mp_cmp(&n, number) == MP_GT) { |
120 | | - break; |
121 | | - } |
122 | | - |
123 | | - if ((err = mp_mul_2(&shift, &shift)) != MP_OKAY) { |
| 155 | + } |
| 156 | + if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) { |
| 157 | + goto LBL_ERR; |
| 158 | + } |
| 159 | + if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) { |
| 160 | + goto LBL_ERR; |
| 161 | + } |
| 162 | + if ((err = mp_neg(&M4, &M)) != MP_OKAY) { |
| 163 | + goto LBL_ERR; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if ((err = mp_init_copy(&nL[precalc_array_index], &n)) != MP_OKAY) { |
| 168 | + goto LBL_ERR; |
| 169 | + } |
| 170 | + if ((err = mp_init_copy(&shiftL[precalc_array_index], &shift)) != MP_OKAY) { |
| 171 | + goto LBL_ERR; |
| 172 | + } |
| 173 | + |
| 174 | + /* Divide by 8, round up */ |
| 175 | + { |
| 176 | + if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) { |
124 | 177 | goto LBL_ERR; |
125 | | - } |
126 | | - |
127 | | - /* The following is a Newton-Raphson step, to restore the invariant |
128 | | - * that $M is (8 * 2**$shift) / $n, rounded up. */ |
129 | | - { |
130 | | - if ((err = mp_sqr(&M, &M2)) != MP_OKAY) { |
131 | | - goto LBL_ERR; |
132 | | - } |
133 | | - if ((err = mp_sqr(&M2, &M4)) != MP_OKAY) { |
134 | | - goto LBL_ERR; |
135 | | - } |
136 | | - |
137 | | - if ((err = mp_mul(&M4, &n, &M4)) != MP_OKAY) { |
138 | | - goto LBL_ERR; |
139 | | - } |
140 | | - if ((err = mp_div_2d(&M4, mp_get_l(&shift) + 6, &M4, NULL)) != MP_OKAY) { |
141 | | - goto LBL_ERR; |
142 | | - } |
143 | | - if ((err = mp_mul_2(&M2, &M2)) != MP_OKAY) { |
144 | | - goto LBL_ERR; |
145 | | - } |
146 | | - if ((err = mp_sub(&M4, &M2, &M4)) != MP_OKAY) { |
147 | | - goto LBL_ERR; |
148 | | - } |
149 | | - if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) { |
150 | | - goto LBL_ERR; |
151 | | - } |
152 | | - if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) { |
153 | | - goto LBL_ERR; |
154 | | - } |
155 | | - if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) { |
156 | | - goto LBL_ERR; |
157 | | - } |
158 | | - if ((err = mp_neg(&M4, &M)) != MP_OKAY) { |
159 | | - goto LBL_ERR; |
160 | | - } |
161 | | - } |
162 | | - |
163 | | - if ((err = mp_init_copy(&nL[precalc_array_index], &n)) != MP_OKAY) { |
| 178 | + } |
| 179 | + if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) { |
164 | 180 | goto LBL_ERR; |
165 | | - } |
166 | | - if ((err = mp_init_copy(&shiftL[precalc_array_index], &shift)) != MP_OKAY) { |
| 181 | + } |
| 182 | + if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) { |
167 | 183 | goto LBL_ERR; |
168 | | - } |
169 | | - |
170 | | - /* Divide by 8, round up */ |
171 | | - { |
172 | | - if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) { |
173 | | - goto LBL_ERR; |
174 | | - } |
175 | | - if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) { |
176 | | - goto LBL_ERR; |
177 | | - } |
178 | | - if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) { |
179 | | - goto LBL_ERR; |
180 | | - } |
181 | | - if ((err = mp_neg(&M4, &M4)) != MP_OKAY) { |
182 | | - goto LBL_ERR; |
183 | | - } |
184 | | - } |
185 | | - if ((err = mp_init_copy(&mL[precalc_array_index], &M4)) != MP_OKAY) { |
| 184 | + } |
| 185 | + if ((err = mp_neg(&M4, &M4)) != MP_OKAY) { |
186 | 186 | goto LBL_ERR; |
187 | | - } |
188 | | - precalc_array_index++; |
189 | | - } |
| 187 | + } |
| 188 | + } |
| 189 | + if ((err = mp_init_copy(&mL[precalc_array_index], &M4)) != MP_OKAY) { |
| 190 | + goto LBL_ERR; |
| 191 | + } |
| 192 | + precalc_array_index++; |
| 193 | + } |
190 | 194 |
|
191 | | - if ((err = mp_todecimal_fast_rec(number, nL, shiftL, mL, precalc_array_index - 1, 1, result)) != MP_OKAY) { |
192 | | - goto LBL_ERR; |
193 | | - } |
| 195 | + if ((err = mp_todecimal_fast_rec(number, nL, shiftL, mL, precalc_array_index - 1, 1, result)) != MP_OKAY) { |
| 196 | + goto LBL_ERR; |
| 197 | + } |
194 | 198 |
|
195 | | - err = MP_OKAY; |
| 199 | + err = MP_OKAY; |
196 | 200 |
|
197 | 201 | LBL_ERR: |
198 | | - mp_clear_multi (&n, &shift, &M, &M2, &M22, &M4, &M44, NULL); |
199 | | - return err; |
| 202 | + mp_clear_multi(&n, &shift, &M, &M2, &M22, &M4, &M44, NULL); |
| 203 | + return err; |
200 | 204 | } |
201 | 205 |
|
202 | 206 | #endif |
0 commit comments