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