|
| 1 | +#include "tommath_private.h" |
| 2 | +#ifdef S_MP_FASTER_TO_RADIX_C |
| 3 | +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
| 4 | +/* SPDX-License-Identifier: Unlicense */ |
| 5 | + |
| 6 | +/* Portable integer log of two with small footprint */ |
| 7 | +static int32_t s_floor_ilog2(int32_t value) |
| 8 | +{ |
| 9 | + int r = 0; |
| 10 | + while ((value /= 2) != 0) { |
| 11 | + r++; |
| 12 | + } |
| 13 | + return r; |
| 14 | +} |
| 15 | + |
| 16 | +/* Exponentiation with small footprint */ |
| 17 | +static int32_t s_pow(int32_t base, int32_t exponent) |
| 18 | +{ |
| 19 | + int32_t result = 1; |
| 20 | + while (exponent != 0) { |
| 21 | + if ((exponent % 2) == 1) { |
| 22 | + result *= base; |
| 23 | + } |
| 24 | + exponent /= 2; |
| 25 | + base *= base; |
| 26 | + } |
| 27 | + return result; |
| 28 | +} |
| 29 | + |
| 30 | +static mp_err s_mp_to_radix_recursive(const mp_int *a, char **str, size_t *part_maxlen, size_t *part_written, |
| 31 | + int radix, int32_t k, int32_t t, bool pad, mp_int *P, mp_int *R) |
| 32 | +{ |
| 33 | + |
| 34 | + mp_int r, q, a1; |
| 35 | + mp_err err; |
| 36 | + int Beta; |
| 37 | + |
| 38 | + if (t < 0) { |
| 39 | + /* Print the string from the number given*/ |
| 40 | + if ((err = s_mp_slower_to_radix(a, str, part_maxlen, part_written, radix, pad)) != MP_OKAY) goto LTM_ERR; |
| 41 | + |
| 42 | + } else { |
| 43 | + if ((err = mp_init_multi(&q, &r, &a1, NULL)) != MP_OKAY) goto LTM_ERR; |
| 44 | + /* |
| 45 | + Barrett reduction. A step by step proof can be found at |
| 46 | + https://www.nayuki.io/page/barrett-reduction-algorithm |
| 47 | +
|
| 48 | + See also: Modern Computer Arithmetic, version 0.5.9, page 59 |
| 49 | + */ |
| 50 | + |
| 51 | + /* If this cast-feast looks familiar: it is the numerator from computing the reciprocal*/ |
| 52 | + Beta = (int)((int32_t)((uint32_t)1 << (t+1)) * k); |
| 53 | + |
| 54 | + /* Q = floor(A1 * I / 2^Beta) */ |
| 55 | + /* I = floor( (2^(2*Beta)) / B) Here we have R[t] = I, P[t] = B */ |
| 56 | + if ((err = mp_mul(a, &R[t], &q)) != MP_OKAY) goto LTM_ERR; |
| 57 | + if ((err = mp_div_2d(&q, Beta, &q, NULL)) != MP_OKAY) goto LTM_ERR; |
| 58 | + |
| 59 | + /* R = A - Q*B */ |
| 60 | + if ((err = mp_mul(&q, &P[t], &r)) != MP_OKAY) goto LTM_ERR; |
| 61 | + if ((err = mp_sub(a, &r, &r)) != MP_OKAY) goto LTM_ERR; |
| 62 | + |
| 63 | + /* We can use this simple correction because of the way we computed the reciprocal */ |
| 64 | + if (r.sign == MP_NEG) { |
| 65 | + if ((err = mp_decr(&q)) != MP_OKAY) goto LTM_ERR; |
| 66 | + if ((err = mp_add(&r, &P[t], &r)) != MP_OKAY) goto LTM_ERR; |
| 67 | + } |
| 68 | + |
| 69 | + /* Go down the lists while climbing up the tree. */ |
| 70 | + t--; |
| 71 | + |
| 72 | + /* Follow branches */ |
| 73 | + if (mp_iszero(&q) && (!pad)) { |
| 74 | + if ((err = s_mp_to_radix_recursive(&r, str, part_maxlen, part_written, radix, |
| 75 | + k, t, false, P, R)) != MP_OKAY) goto LTM_ERR; |
| 76 | + } else { |
| 77 | + if ((err = s_mp_to_radix_recursive(&q, str, part_maxlen, part_written, radix, |
| 78 | + k, t, pad, P, R)) != MP_OKAY) goto LTM_ERR; |
| 79 | + if ((err = s_mp_to_radix_recursive(&r, str, part_maxlen, part_written, radix, |
| 80 | + k, t, true, P, R)) != MP_OKAY) goto LTM_ERR; |
| 81 | + } |
| 82 | + mp_clear_multi(&q, &r, &a1, NULL); |
| 83 | + } |
| 84 | + |
| 85 | + err = MP_OKAY; |
| 86 | +LTM_ERR: |
| 87 | + return err; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) |
| 92 | +{ |
| 93 | + mp_err err; |
| 94 | + int32_t n = 0, k, t = 0, steps; |
| 95 | + int ilog2a; |
| 96 | + |
| 97 | + /* Use given buffer directly, no temporary buffers for the individual chunks */ |
| 98 | + char **sptr = &str; |
| 99 | + /* Size of the chunk */ |
| 100 | + size_t part_written = 0; |
| 101 | + size_t part_maxlen = maxlen; |
| 102 | + |
| 103 | + /* List of reciprocals */ |
| 104 | + mp_int *R = NULL; |
| 105 | + /* List of moduli */ |
| 106 | + mp_int *P = NULL; |
| 107 | + |
| 108 | + /* Denominator for the reciprocal: b^y */ |
| 109 | + n = s_pow((int32_t)radix, (int32_t)s_mp_radix_exponent_y[radix]); |
| 110 | + |
| 111 | + /* Numerator of the reciprocal: ceil(log_2(n)) */ |
| 112 | + k = s_floor_ilog2(n) + 1; |
| 113 | + |
| 114 | + /* steps = floor(log_2(floor(log_2(a))))*/ |
| 115 | + ilog2a = mp_count_bits(a) - 1; |
| 116 | + |
| 117 | + /* Cutoff at about twice the size of P[0]. Interestingly far below Karatsuba cut-off. */ |
| 118 | + if (ilog2a < (2 * k * MP_RADIX_BARRETT_START_MULTIPLICATOR)) { |
| 119 | + if ((err = s_mp_slower_to_radix(a, sptr, &part_maxlen, &part_written, radix, false)) != MP_OKAY) goto LTM_ERR; |
| 120 | + /* part_written does not count EOS */ |
| 121 | + *written = part_written + 1; |
| 122 | + return err; |
| 123 | + } |
| 124 | + /* |
| 125 | + floor(log_2(floor(log_2(a)))) is not enough but we check for |
| 126 | + the end inside the loop and the list is just a list of pointers, |
| 127 | + not much memory wasted here. |
| 128 | + */ |
| 129 | + steps = s_floor_ilog2((int32_t)ilog2a) + 2; |
| 130 | + |
| 131 | + /* Allocate memory for list of reciprocals */ |
| 132 | + R = (mp_int *) MP_MALLOC((size_t) steps * sizeof(mp_int)); |
| 133 | + if (R == NULL) { |
| 134 | + return MP_MEM; |
| 135 | + } |
| 136 | + /* Allocate memory for list of moduli */ |
| 137 | + P = (mp_int *) MP_MALLOC((size_t) steps * sizeof(mp_int)); |
| 138 | + if (P == NULL) { |
| 139 | + MP_FREE_BUF(R, (size_t) steps * sizeof(mp_int)); |
| 140 | + return MP_MEM; |
| 141 | + } |
| 142 | + |
| 143 | + /* |
| 144 | + The approximation to the reciprocal used in Barrett's method is |
| 145 | + R_t = ceil(2^((2^t)*k)/n^(2^t)) |
| 146 | + with R_0 = (2^(2*k))/b^y and k = ceil(log_2(n)) as computed above. |
| 147 | + */ |
| 148 | + |
| 149 | + /* To get the tree a bit flatter. Alternative: do it iteratively instead of recursively */ |
| 150 | + k = k * MP_RADIX_BARRETT_START_MULTIPLICATOR; |
| 151 | + |
| 152 | + /* Compute initial reciprocal R[0] and expand it (R[0]^(2^k) */ |
| 153 | + if ((err = mp_init_i32(&P[0], n)) != MP_OKAY) goto LTM_ERR; |
| 154 | + if ((err = mp_expt_n(&P[0], MP_RADIX_BARRETT_START_MULTIPLICATOR, &P[0])) != MP_OKAY) goto LTM_ERR; |
| 155 | + |
| 156 | + if ((err = mp_init(&R[0])) != MP_OKAY) goto LTM_ERR; |
| 157 | + if ((err = mp_2expt(&R[0], 2*k)) != MP_OKAY) goto LTM_ERR; |
| 158 | + |
| 159 | + if ((err = mp_div(&R[0], &P[0], &R[0], NULL)) != MP_OKAY) goto LTM_ERR; |
| 160 | + if ((err = mp_incr(&R[0])) != MP_OKAY) goto LTM_ERR; |
| 161 | + |
| 162 | + /* Compute the rest of the reciprocals if as needed */ |
| 163 | + for (t = 1; t < steps; t++) { |
| 164 | + /* P_t = (b^y)^(2^t) = n^(2^t) */ |
| 165 | + /* |
| 166 | + We cannot just square because it can |
| 167 | + a) overflow MP_MAX_DIGIT_COUNT |
| 168 | + b) it can get bigger than "a" which it shouldn't |
| 169 | + which also means that |
| 170 | + c) if it gets bigger than "a" we have all necessary |
| 171 | + reciprocals and can break out of the loop |
| 172 | + */ |
| 173 | + /* Check for overflow of 2^((2^t)*k) i.e. bigger than 2^MP_MAX_DIGIT_COUNT */ |
| 174 | + if (((int)(1u << t)*k) > MP_MAX_DIGIT_COUNT) { |
| 175 | + /* TODO: This can only happen near MP_MAX_DIGIT_COUNT and we can use |
| 176 | + the reciprocal R[t-1] to do the division but R[t] != R[t-1]^2 |
| 177 | + so we cannot just divide by R[t-1] twice. |
| 178 | + */ |
| 179 | + err = MP_OVF; |
| 180 | + goto LTM_ERR; |
| 181 | + } |
| 182 | + |
| 183 | + /* P[t-1]^2 > a at most likely more than just a bit or too, so check if we |
| 184 | + can bail out early without actually computing the square. The |
| 185 | + constant "10" is comprised of unity plus some angst-allowance */ |
| 186 | + if ((2 * mp_count_bits(&P[t-1]) - 10) > ilog2a) { |
| 187 | + /* Correct index */ |
| 188 | + t--; |
| 189 | + break; |
| 190 | + } |
| 191 | + |
| 192 | + /* Compute denominator */ |
| 193 | + if ((err = mp_init(&P[t])) != MP_OKAY) goto LTM_ERR; |
| 194 | + /* P[t] = P[t-1]^2 */ |
| 195 | + if ((err = mp_sqr(&P[t-1], &P[t])) != MP_OKAY) goto LTM_ERR; |
| 196 | + /* Check if P[t]^2 > a */ |
| 197 | + if (mp_cmp(&P[t],a) == MP_GT) { |
| 198 | + /* We don't need P[t] anymore */ |
| 199 | + mp_clear(&P[t]); |
| 200 | + /* Correct index */ |
| 201 | + t--; |
| 202 | + break; |
| 203 | + } |
| 204 | + /* Compute numerator */ |
| 205 | + if ((err = mp_init(&R[t])) != MP_OKAY) goto LTM_ERR; |
| 206 | + |
| 207 | + /* R[t] = R[t] << (2^t * k) The factor cannot overflow, we checked that above */ |
| 208 | + /* TODO: these are more castings than in the ER in Mayrhofen at New Year's Eve! */ |
| 209 | + if ((err = mp_2expt(&(R[t]), (int)((int32_t)((uint32_t)1 << (t+1)) * k))) != MP_OKAY) goto LTM_ERR; |
| 210 | + |
| 211 | + /* Compute reciprocal */ |
| 212 | + /* R[t] = floor(2^(2^t * k) / P[t] */ |
| 213 | + if ((err = mp_div(&R[t], &P[t], &R[t], NULL)) != MP_OKAY) goto LTM_ERR; |
| 214 | + /* Ceiling if P[t] is not a power of two but it is not a problem if P[t] is a power of two. */ |
| 215 | + if ((err = mp_incr(&R[t])) != MP_OKAY) goto LTM_ERR; |
| 216 | + } |
| 217 | + |
| 218 | + /* And finally: start the recursion. */ |
| 219 | + if ((err = s_mp_to_radix_recursive(a, sptr, &part_maxlen, &part_written, radix, |
| 220 | + k, t, false, P, R)) != MP_OKAY) goto LTM_ERR; |
| 221 | + /* part_written does not account for EOS */ |
| 222 | + *written = part_written + 1; |
| 223 | + |
| 224 | + err = MP_OKAY; |
| 225 | +LTM_ERR: |
| 226 | + do { |
| 227 | + mp_clear(&P[t]); |
| 228 | + mp_clear(&R[t]); |
| 229 | + } while (t--); |
| 230 | + MP_FREE_BUF(P, (size_t) steps * sizeof(mp_int)); |
| 231 | + MP_FREE_BUF(R, (size_t) steps * sizeof(mp_int)); |
| 232 | + return err; |
| 233 | +} |
| 234 | + |
| 235 | +#endif |
0 commit comments