Commit | Line | Data |
---|---|---|
e5574168 | 1 | /* |
2 | * Bignum routines for RSA and DH and stuff. | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
ed953b91 | 6 | #include <assert.h> |
e5574168 | 7 | #include <stdlib.h> |
8 | #include <string.h> | |
9 | ||
5c72ca61 | 10 | #include "misc.h" |
f85d8186 | 11 | #include "bn-internal.h" |
e5574168 | 12 | #include "ssh.h" |
13 | ||
a3412f52 | 14 | BignumInt bnZero[1] = { 0 }; |
15 | BignumInt bnOne[2] = { 1, 1 }; | |
e5574168 | 16 | |
7d6ee6ff | 17 | /* |
a3412f52 | 18 | * The Bignum format is an array of `BignumInt'. The first |
7d6ee6ff | 19 | * element of the array counts the remaining elements. The |
a3412f52 | 20 | * remaining elements express the actual number, base 2^BIGNUM_INT_BITS, _least_ |
7d6ee6ff | 21 | * significant digit first. (So it's trivial to extract the bit |
22 | * with value 2^n for any n.) | |
23 | * | |
24 | * All Bignums in this module are positive. Negative numbers must | |
25 | * be dealt with outside it. | |
26 | * | |
27 | * INVARIANT: the most significant word of any Bignum must be | |
28 | * nonzero. | |
29 | */ | |
30 | ||
7cca0d81 | 31 | Bignum Zero = bnZero, One = bnOne; |
e5574168 | 32 | |
32874aea | 33 | static Bignum newbn(int length) |
34 | { | |
a3412f52 | 35 | Bignum b = snewn(length + 1, BignumInt); |
e5574168 | 36 | if (!b) |
37 | abort(); /* FIXME */ | |
32874aea | 38 | memset(b, 0, (length + 1) * sizeof(*b)); |
e5574168 | 39 | b[0] = length; |
40 | return b; | |
41 | } | |
42 | ||
32874aea | 43 | void bn_restore_invariant(Bignum b) |
44 | { | |
45 | while (b[0] > 1 && b[b[0]] == 0) | |
46 | b[0]--; | |
3709bfe9 | 47 | } |
48 | ||
32874aea | 49 | Bignum copybn(Bignum orig) |
50 | { | |
a3412f52 | 51 | Bignum b = snewn(orig[0] + 1, BignumInt); |
7cca0d81 | 52 | if (!b) |
53 | abort(); /* FIXME */ | |
32874aea | 54 | memcpy(b, orig, (orig[0] + 1) * sizeof(*b)); |
7cca0d81 | 55 | return b; |
56 | } | |
57 | ||
32874aea | 58 | void freebn(Bignum b) |
59 | { | |
e5574168 | 60 | /* |
61 | * Burn the evidence, just in case. | |
62 | */ | |
dfb88efd | 63 | smemclr(b, sizeof(b[0]) * (b[0] + 1)); |
dcbde236 | 64 | sfree(b); |
e5574168 | 65 | } |
66 | ||
32874aea | 67 | Bignum bn_power_2(int n) |
68 | { | |
a3412f52 | 69 | Bignum ret = newbn(n / BIGNUM_INT_BITS + 1); |
3709bfe9 | 70 | bignum_set_bit(ret, n, 1); |
71 | return ret; | |
72 | } | |
73 | ||
e5574168 | 74 | /* |
0c431b2f | 75 | * Internal addition. Sets c = a - b, where 'a', 'b' and 'c' are all |
c40be1ad | 76 | * little-endian arrays of 'len' BignumInts. Returns a BignumInt carried |
0c431b2f | 77 | * off the top. |
78 | */ | |
79 | static BignumInt internal_add(const BignumInt *a, const BignumInt *b, | |
80 | BignumInt *c, int len) | |
81 | { | |
82 | int i; | |
83 | BignumDblInt carry = 0; | |
84 | ||
c40be1ad | 85 | for (i = 0; i < len; i++) { |
0c431b2f | 86 | carry += (BignumDblInt)a[i] + b[i]; |
87 | c[i] = (BignumInt)carry; | |
88 | carry >>= BIGNUM_INT_BITS; | |
89 | } | |
90 | ||
91 | return (BignumInt)carry; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Internal subtraction. Sets c = a - b, where 'a', 'b' and 'c' are | |
c40be1ad | 96 | * all little-endian arrays of 'len' BignumInts. Any borrow from the top |
0c431b2f | 97 | * is ignored. |
98 | */ | |
99 | static void internal_sub(const BignumInt *a, const BignumInt *b, | |
100 | BignumInt *c, int len) | |
101 | { | |
102 | int i; | |
103 | BignumDblInt carry = 1; | |
104 | ||
c40be1ad | 105 | for (i = 0; i < len; i++) { |
0c431b2f | 106 | carry += (BignumDblInt)a[i] + (b[i] ^ BIGNUM_INT_MASK); |
107 | c[i] = (BignumInt)carry; | |
108 | carry >>= BIGNUM_INT_BITS; | |
109 | } | |
110 | } | |
111 | ||
112 | /* | |
e5574168 | 113 | * Compute c = a * b. |
114 | * Input is in the first len words of a and b. | |
115 | * Result is returned in the first 2*len words of c. | |
5a502a19 | 116 | * |
117 | * 'scratch' must point to an array of BignumInt of size at least | |
118 | * mul_compute_scratch(len). (This covers the needs of internal_mul | |
119 | * and all its recursive calls to itself.) | |
e5574168 | 120 | */ |
0c431b2f | 121 | #define KARATSUBA_THRESHOLD 50 |
5a502a19 | 122 | static int mul_compute_scratch(int len) |
123 | { | |
124 | int ret = 0; | |
125 | while (len > KARATSUBA_THRESHOLD) { | |
126 | int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */ | |
127 | int midlen = botlen + 1; | |
128 | ret += 4*midlen; | |
129 | len = midlen; | |
130 | } | |
131 | return ret; | |
132 | } | |
132c534f | 133 | static void internal_mul(const BignumInt *a, const BignumInt *b, |
5a502a19 | 134 | BignumInt *c, int len, BignumInt *scratch) |
e5574168 | 135 | { |
0c431b2f | 136 | if (len > KARATSUBA_THRESHOLD) { |
757b0110 | 137 | int i; |
0c431b2f | 138 | |
139 | /* | |
140 | * Karatsuba divide-and-conquer algorithm. Cut each input in | |
141 | * half, so that it's expressed as two big 'digits' in a giant | |
142 | * base D: | |
143 | * | |
144 | * a = a_1 D + a_0 | |
145 | * b = b_1 D + b_0 | |
146 | * | |
147 | * Then the product is of course | |
148 | * | |
149 | * ab = a_1 b_1 D^2 + (a_1 b_0 + a_0 b_1) D + a_0 b_0 | |
150 | * | |
151 | * and we compute the three coefficients by recursively | |
152 | * calling ourself to do half-length multiplications. | |
153 | * | |
154 | * The clever bit that makes this worth doing is that we only | |
155 | * need _one_ half-length multiplication for the central | |
156 | * coefficient rather than the two that it obviouly looks | |
157 | * like, because we can use a single multiplication to compute | |
158 | * | |
159 | * (a_1 + a_0) (b_1 + b_0) = a_1 b_1 + a_1 b_0 + a_0 b_1 + a_0 b_0 | |
160 | * | |
161 | * and then we subtract the other two coefficients (a_1 b_1 | |
162 | * and a_0 b_0) which we were computing anyway. | |
163 | * | |
164 | * Hence we get to multiply two numbers of length N in about | |
165 | * three times as much work as it takes to multiply numbers of | |
166 | * length N/2, which is obviously better than the four times | |
167 | * as much work it would take if we just did a long | |
168 | * conventional multiply. | |
169 | */ | |
170 | ||
171 | int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */ | |
172 | int midlen = botlen + 1; | |
0c431b2f | 173 | BignumDblInt carry; |
174 | ||
175 | /* | |
176 | * The coefficients a_1 b_1 and a_0 b_0 just avoid overlapping | |
177 | * in the output array, so we can compute them immediately in | |
178 | * place. | |
179 | */ | |
180 | ||
f3c29e34 | 181 | #ifdef KARA_DEBUG |
182 | printf("a1,a0 = 0x"); | |
183 | for (i = 0; i < len; i++) { | |
184 | if (i == toplen) printf(", 0x"); | |
c40be1ad | 185 | printf("%0*x", BIGNUM_INT_BITS/4, a[len - 1 - i]); |
f3c29e34 | 186 | } |
187 | printf("\n"); | |
188 | printf("b1,b0 = 0x"); | |
189 | for (i = 0; i < len; i++) { | |
190 | if (i == toplen) printf(", 0x"); | |
c40be1ad | 191 | printf("%0*x", BIGNUM_INT_BITS/4, b[len - 1 - i]); |
f3c29e34 | 192 | } |
193 | printf("\n"); | |
194 | #endif | |
195 | ||
0c431b2f | 196 | /* a_1 b_1 */ |
c40be1ad | 197 | internal_mul(a + botlen, b + botlen, c + 2*botlen, toplen, scratch); |
f3c29e34 | 198 | #ifdef KARA_DEBUG |
199 | printf("a1b1 = 0x"); | |
200 | for (i = 0; i < 2*toplen; i++) { | |
c40be1ad | 201 | printf("%0*x", BIGNUM_INT_BITS/4, c[2*len - 1 - i]); |
f3c29e34 | 202 | } |
203 | printf("\n"); | |
204 | #endif | |
0c431b2f | 205 | |
206 | /* a_0 b_0 */ | |
c40be1ad | 207 | internal_mul(a, b, c, botlen, scratch); |
f3c29e34 | 208 | #ifdef KARA_DEBUG |
209 | printf("a0b0 = 0x"); | |
210 | for (i = 0; i < 2*botlen; i++) { | |
c40be1ad | 211 | printf("%0*x", BIGNUM_INT_BITS/4, c[2*botlen - 1 - i]); |
f3c29e34 | 212 | } |
213 | printf("\n"); | |
214 | #endif | |
0c431b2f | 215 | |
c40be1ad MW |
216 | /* Zero padding. botlen exceeds toplen by at most 1, and we'll set |
217 | * the extra carry explicitly below, so we only need to zero at most | |
218 | * one of the top words here. | |
219 | */ | |
220 | scratch[midlen - 2] = scratch[2*midlen - 2] = 0; | |
0c431b2f | 221 | |
757b0110 | 222 | for (i = 0; i < toplen; i++) { |
c40be1ad MW |
223 | scratch[i] = a[i + botlen]; /* a_1 */ |
224 | scratch[midlen + i] = b[i + botlen]; /* b_1 */ | |
0c431b2f | 225 | } |
226 | ||
227 | /* compute a_1 + a_0 */ | |
c40be1ad | 228 | scratch[midlen - 1] = internal_add(scratch, a, scratch, botlen); |
f3c29e34 | 229 | #ifdef KARA_DEBUG |
230 | printf("a1plusa0 = 0x"); | |
231 | for (i = 0; i < midlen; i++) { | |
c40be1ad | 232 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[midlen - 1 - i]); |
f3c29e34 | 233 | } |
234 | printf("\n"); | |
235 | #endif | |
0c431b2f | 236 | /* compute b_1 + b_0 */ |
c40be1ad MW |
237 | scratch[2*midlen - 1] = internal_add(scratch+midlen, b, |
238 | scratch+midlen, botlen); | |
f3c29e34 | 239 | #ifdef KARA_DEBUG |
240 | printf("b1plusb0 = 0x"); | |
241 | for (i = 0; i < midlen; i++) { | |
c40be1ad | 242 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[2*midlen - 1 - i]); |
f3c29e34 | 243 | } |
244 | printf("\n"); | |
245 | #endif | |
0c431b2f | 246 | |
247 | /* | |
248 | * Now we can do the third multiplication. | |
249 | */ | |
5a502a19 | 250 | internal_mul(scratch, scratch + midlen, scratch + 2*midlen, midlen, |
251 | scratch + 4*midlen); | |
f3c29e34 | 252 | #ifdef KARA_DEBUG |
253 | printf("a1plusa0timesb1plusb0 = 0x"); | |
254 | for (i = 0; i < 2*midlen; i++) { | |
c40be1ad | 255 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[4*midlen - 1 - i]); |
f3c29e34 | 256 | } |
257 | printf("\n"); | |
258 | #endif | |
0c431b2f | 259 | |
260 | /* | |
261 | * Now we can reuse the first half of 'scratch' to compute the | |
262 | * sum of the outer two coefficients, to subtract from that | |
263 | * product to obtain the middle one. | |
264 | */ | |
c40be1ad | 265 | scratch[2*botlen - 2] = scratch[2*botlen - 1] = 0; |
757b0110 | 266 | for (i = 0; i < 2*toplen; i++) |
c40be1ad MW |
267 | scratch[i] = c[2*botlen + i]; |
268 | scratch[2*botlen] = internal_add(scratch, c, scratch, 2*botlen); | |
269 | scratch[2*botlen + 1] = 0; | |
f3c29e34 | 270 | #ifdef KARA_DEBUG |
271 | printf("a1b1plusa0b0 = 0x"); | |
272 | for (i = 0; i < 2*midlen; i++) { | |
c40be1ad | 273 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[2*midlen - 1 - i]); |
f3c29e34 | 274 | } |
275 | printf("\n"); | |
276 | #endif | |
0c431b2f | 277 | |
c40be1ad | 278 | internal_sub(scratch + 2*midlen, scratch, scratch, 2*midlen); |
f3c29e34 | 279 | #ifdef KARA_DEBUG |
280 | printf("a1b0plusa0b1 = 0x"); | |
281 | for (i = 0; i < 2*midlen; i++) { | |
c40be1ad | 282 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[4*midlen - 1 - i]); |
f3c29e34 | 283 | } |
284 | printf("\n"); | |
285 | #endif | |
0c431b2f | 286 | |
287 | /* | |
288 | * And now all we need to do is to add that middle coefficient | |
289 | * back into the output. We may have to propagate a carry | |
290 | * further up the output, but we can be sure it won't | |
291 | * propagate right the way off the top. | |
292 | */ | |
c40be1ad MW |
293 | carry = internal_add(c + botlen, scratch, c + botlen, 2*midlen); |
294 | i = botlen + 2*midlen; | |
0c431b2f | 295 | while (carry) { |
c40be1ad | 296 | assert(i <= 2*len); |
757b0110 | 297 | carry += c[i]; |
298 | c[i] = (BignumInt)carry; | |
0c431b2f | 299 | carry >>= BIGNUM_INT_BITS; |
c40be1ad | 300 | i++; |
0c431b2f | 301 | } |
f3c29e34 | 302 | #ifdef KARA_DEBUG |
303 | printf("ab = 0x"); | |
304 | for (i = 0; i < 2*len; i++) { | |
c40be1ad | 305 | printf("%0*x", BIGNUM_INT_BITS/4, c[2*len - i]); |
f3c29e34 | 306 | } |
307 | printf("\n"); | |
308 | #endif | |
0c431b2f | 309 | |
0c431b2f | 310 | } else { |
757b0110 | 311 | int i; |
312 | BignumInt carry; | |
313 | BignumDblInt t; | |
c40be1ad | 314 | const BignumInt *ap, *alim = a + len, *bp, *blim = b + len; |
757b0110 | 315 | BignumInt *cp, *cps; |
0c431b2f | 316 | |
317 | /* | |
318 | * Multiply in the ordinary O(N^2) way. | |
319 | */ | |
320 | ||
757b0110 | 321 | for (i = 0; i < 2 * len; i++) |
322 | c[i] = 0; | |
0c431b2f | 323 | |
c40be1ad | 324 | for (cps = c, ap = a; ap < alim; ap++, cps++) { |
757b0110 | 325 | carry = 0; |
c40be1ad | 326 | for (cp = cps, bp = b, i = blim - bp; i--; bp++, cp++) { |
757b0110 | 327 | t = (MUL_WORD(*ap, *bp) + carry) + *cp; |
328 | *cp = (BignumInt) t; | |
08b5c9a2 | 329 | carry = (BignumInt)(t >> BIGNUM_INT_BITS); |
0c431b2f | 330 | } |
757b0110 | 331 | *cp = carry; |
0c431b2f | 332 | } |
e5574168 | 333 | } |
334 | } | |
335 | ||
132c534f | 336 | /* |
337 | * Variant form of internal_mul used for the initial step of | |
338 | * Montgomery reduction. Only bothers outputting 'len' words | |
339 | * (everything above that is thrown away). | |
340 | */ | |
341 | static void internal_mul_low(const BignumInt *a, const BignumInt *b, | |
5a502a19 | 342 | BignumInt *c, int len, BignumInt *scratch) |
132c534f | 343 | { |
132c534f | 344 | if (len > KARATSUBA_THRESHOLD) { |
757b0110 | 345 | int i; |
132c534f | 346 | |
347 | /* | |
348 | * Karatsuba-aware version of internal_mul_low. As before, we | |
349 | * express each input value as a shifted combination of two | |
350 | * halves: | |
351 | * | |
352 | * a = a_1 D + a_0 | |
353 | * b = b_1 D + b_0 | |
354 | * | |
355 | * Then the full product is, as before, | |
356 | * | |
357 | * ab = a_1 b_1 D^2 + (a_1 b_0 + a_0 b_1) D + a_0 b_0 | |
358 | * | |
359 | * Provided we choose D on the large side (so that a_0 and b_0 | |
360 | * are _at least_ as long as a_1 and b_1), we don't need the | |
361 | * topmost term at all, and we only need half of the middle | |
362 | * term. So there's no point in doing the proper Karatsuba | |
363 | * optimisation which computes the middle term using the top | |
364 | * one, because we'd take as long computing the top one as | |
365 | * just computing the middle one directly. | |
366 | * | |
367 | * So instead, we do a much more obvious thing: we call the | |
368 | * fully optimised internal_mul to compute a_0 b_0, and we | |
369 | * recursively call ourself to compute the _bottom halves_ of | |
370 | * a_1 b_0 and a_0 b_1, each of which we add into the result | |
371 | * in the obvious way. | |
372 | * | |
373 | * In other words, there's no actual Karatsuba _optimisation_ | |
374 | * in this function; the only benefit in doing it this way is | |
375 | * that we call internal_mul proper for a large part of the | |
376 | * work, and _that_ can optimise its operation. | |
377 | */ | |
378 | ||
379 | int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */ | |
132c534f | 380 | |
381 | /* | |
5a502a19 | 382 | * Scratch space for the various bits and pieces we're going |
383 | * to be adding together: we need botlen*2 words for a_0 b_0 | |
384 | * (though we may end up throwing away its topmost word), and | |
385 | * toplen words for each of a_1 b_0 and a_0 b_1. That adds up | |
386 | * to exactly 2*len. | |
132c534f | 387 | */ |
132c534f | 388 | |
389 | /* a_0 b_0 */ | |
c40be1ad | 390 | internal_mul(a, b, scratch + 2*toplen, botlen, scratch + 2*len); |
132c534f | 391 | |
392 | /* a_1 b_0 */ | |
c40be1ad | 393 | internal_mul_low(a + botlen, b, scratch + toplen, toplen, |
5a502a19 | 394 | scratch + 2*len); |
132c534f | 395 | |
396 | /* a_0 b_1 */ | |
c40be1ad | 397 | internal_mul_low(a, b + botlen, scratch, toplen, scratch + 2*len); |
132c534f | 398 | |
399 | /* Copy the bottom half of the big coefficient into place */ | |
757b0110 | 400 | for (i = 0; i < botlen; i++) |
c40be1ad | 401 | c[i] = scratch[2*toplen + i]; |
132c534f | 402 | |
403 | /* Add the two small coefficients, throwing away the returned carry */ | |
404 | internal_add(scratch, scratch + toplen, scratch, toplen); | |
405 | ||
406 | /* And add that to the large coefficient, leaving the result in c. */ | |
c40be1ad MW |
407 | internal_add(scratch, scratch + 2*toplen + botlen, |
408 | c + botlen, toplen); | |
132c534f | 409 | |
132c534f | 410 | } else { |
757b0110 | 411 | int i; |
412 | BignumInt carry; | |
413 | BignumDblInt t; | |
c40be1ad MW |
414 | const BignumInt *ap, *alim = a + len, *bp; |
415 | BignumInt *cp, *cps, *clim = c + len; | |
132c534f | 416 | |
757b0110 | 417 | /* |
418 | * Multiply in the ordinary O(N^2) way. | |
419 | */ | |
132c534f | 420 | |
757b0110 | 421 | for (i = 0; i < len; i++) |
422 | c[i] = 0; | |
423 | ||
c40be1ad | 424 | for (cps = c, ap = a; ap < alim; ap++, cps++) { |
757b0110 | 425 | carry = 0; |
c40be1ad | 426 | for (cp = cps, bp = b, i = clim - cp; i--; bp++, cp++) { |
757b0110 | 427 | t = (MUL_WORD(*ap, *bp) + carry) + *cp; |
428 | *cp = (BignumInt) t; | |
08b5c9a2 | 429 | carry = (BignumInt)(t >> BIGNUM_INT_BITS); |
132c534f | 430 | } |
431 | } | |
132c534f | 432 | } |
433 | } | |
434 | ||
435 | /* | |
c40be1ad | 436 | * Montgomery reduction. Expects x to be a little-endian array of 2*len |
132c534f | 437 | * BignumInts whose value satisfies 0 <= x < rn (where r = 2^(len * |
438 | * BIGNUM_INT_BITS) is the Montgomery base). Returns in the same array | |
439 | * a value x' which is congruent to xr^{-1} mod n, and satisfies 0 <= | |
440 | * x' < n. | |
441 | * | |
c40be1ad | 442 | * 'n' and 'mninv' should be little-endian arrays of 'len' BignumInts |
132c534f | 443 | * each, containing respectively n and the multiplicative inverse of |
444 | * -n mod r. | |
445 | * | |
5a502a19 | 446 | * 'tmp' is an array of BignumInt used as scratch space, of length at |
447 | * least 3*len + mul_compute_scratch(len). | |
132c534f | 448 | */ |
449 | static void monty_reduce(BignumInt *x, const BignumInt *n, | |
450 | const BignumInt *mninv, BignumInt *tmp, int len) | |
451 | { | |
452 | int i; | |
453 | BignumInt carry; | |
454 | ||
455 | /* | |
456 | * Multiply x by (-n)^{-1} mod r. This gives us a value m such | |
457 | * that mn is congruent to -x mod r. Hence, mn+x is an exact | |
458 | * multiple of r, and is also (obviously) congruent to x mod n. | |
459 | */ | |
c40be1ad | 460 | internal_mul_low(x, mninv, tmp, len, tmp + 3*len); |
132c534f | 461 | |
462 | /* | |
463 | * Compute t = (mn+x)/r in ordinary, non-modular, integer | |
464 | * arithmetic. By construction this is exact, and is congruent mod | |
465 | * n to x * r^{-1}, i.e. the answer we want. | |
466 | * | |
467 | * The following multiply leaves that answer in the _most_ | |
468 | * significant half of the 'x' array, so then we must shift it | |
469 | * down. | |
470 | */ | |
5a502a19 | 471 | internal_mul(tmp, n, tmp+len, len, tmp + 3*len); |
132c534f | 472 | carry = internal_add(x, tmp+len, x, 2*len); |
473 | for (i = 0; i < len; i++) | |
c40be1ad | 474 | x[i] = x[len + i], x[len + i] = 0; |
132c534f | 475 | |
476 | /* | |
477 | * Reduce t mod n. This doesn't require a full-on division by n, | |
478 | * but merely a test and single optional subtraction, since we can | |
479 | * show that 0 <= t < 2n. | |
480 | * | |
481 | * Proof: | |
482 | * + we computed m mod r, so 0 <= m < r. | |
483 | * + so 0 <= mn < rn, obviously | |
484 | * + hence we only need 0 <= x < rn to guarantee that 0 <= mn+x < 2rn | |
485 | * + yielding 0 <= (mn+x)/r < 2n as required. | |
486 | */ | |
487 | if (!carry) { | |
c40be1ad MW |
488 | for (i = len; i-- > 0; ) |
489 | if (x[i] != n[i]) | |
132c534f | 490 | break; |
491 | } | |
c40be1ad MW |
492 | if (carry || i < 0 || x[i] > n[i]) |
493 | internal_sub(x, n, x, len); | |
132c534f | 494 | } |
495 | ||
a3412f52 | 496 | static void internal_add_shifted(BignumInt *number, |
32874aea | 497 | unsigned n, int shift) |
498 | { | |
a3412f52 | 499 | int word = 1 + (shift / BIGNUM_INT_BITS); |
500 | int bshift = shift % BIGNUM_INT_BITS; | |
501 | BignumDblInt addend; | |
9400cf6f | 502 | |
3014da2b | 503 | addend = (BignumDblInt)n << bshift; |
9400cf6f | 504 | |
505 | while (addend) { | |
32874aea | 506 | addend += number[word]; |
a3412f52 | 507 | number[word] = (BignumInt) addend & BIGNUM_INT_MASK; |
508 | addend >>= BIGNUM_INT_BITS; | |
32874aea | 509 | word++; |
9400cf6f | 510 | } |
511 | } | |
512 | ||
e5574168 | 513 | /* |
514 | * Compute a = a % m. | |
9400cf6f | 515 | * Input in first alen words of a and first mlen words of m. |
516 | * Output in first alen words of a | |
c40be1ad | 517 | * (of which last alen-mlen words will be zero). |
e5574168 | 518 | * The MSW of m MUST have its high bit set. |
c40be1ad MW |
519 | * Quotient is accumulated in the `quotient' array. Quotient parts |
520 | * are shifted left by `qshift' before adding into quot. | |
e5574168 | 521 | */ |
a3412f52 | 522 | static void internal_mod(BignumInt *a, int alen, |
523 | BignumInt *m, int mlen, | |
524 | BignumInt *quot, int qshift) | |
e5574168 | 525 | { |
a3412f52 | 526 | BignumInt m0, m1; |
e5574168 | 527 | unsigned int h; |
c40be1ad | 528 | int i, j, k; |
e5574168 | 529 | |
c40be1ad | 530 | m0 = m[mlen - 1]; |
9400cf6f | 531 | if (mlen > 1) |
c40be1ad | 532 | m1 = m[mlen - 2]; |
9400cf6f | 533 | else |
32874aea | 534 | m1 = 0; |
e5574168 | 535 | |
c40be1ad | 536 | for (i = alen, h = 0; i-- >= mlen; ) { |
a3412f52 | 537 | BignumDblInt t; |
9400cf6f | 538 | unsigned int q, r, c, ai1; |
e5574168 | 539 | |
c40be1ad MW |
540 | if (i) |
541 | ai1 = a[i - 1]; | |
542 | else | |
543 | ai1 = 0; | |
9400cf6f | 544 | |
e5574168 | 545 | /* Find q = h:a[i] / m0 */ |
62ef3d44 | 546 | if (h >= m0) { |
547 | /* | |
548 | * Special case. | |
549 | * | |
550 | * To illustrate it, suppose a BignumInt is 8 bits, and | |
551 | * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then | |
552 | * our initial division will be 0xA123 / 0xA1, which | |
553 | * will give a quotient of 0x100 and a divide overflow. | |
554 | * However, the invariants in this division algorithm | |
555 | * are not violated, since the full number A1:23:... is | |
556 | * _less_ than the quotient prefix A1:B2:... and so the | |
557 | * following correction loop would have sorted it out. | |
558 | * | |
559 | * In this situation we set q to be the largest | |
560 | * quotient we _can_ stomach (0xFF, of course). | |
561 | */ | |
562 | q = BIGNUM_INT_MASK; | |
563 | } else { | |
819a22b3 | 564 | /* Macro doesn't want an array subscript expression passed |
565 | * into it (see definition), so use a temporary. */ | |
566 | BignumInt tmplo = a[i]; | |
567 | DIVMOD_WORD(q, r, h, tmplo, m0); | |
62ef3d44 | 568 | |
569 | /* Refine our estimate of q by looking at | |
c40be1ad | 570 | h:a[i]:a[i-1] / m0:m1 */ |
62ef3d44 | 571 | t = MUL_WORD(m1, q); |
572 | if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) { | |
573 | q--; | |
574 | t -= m1; | |
575 | r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */ | |
576 | if (r >= (BignumDblInt) m0 && | |
577 | t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--; | |
578 | } | |
e5574168 | 579 | } |
580 | ||
c40be1ad MW |
581 | j = i + 1 - mlen; |
582 | ||
9400cf6f | 583 | /* Subtract q * m from a[i...] */ |
e5574168 | 584 | c = 0; |
c40be1ad | 585 | for (k = 0; k < mlen; k++) { |
a47e8bba | 586 | t = MUL_WORD(q, m[k]); |
e5574168 | 587 | t += c; |
62ddb51e | 588 | c = (unsigned)(t >> BIGNUM_INT_BITS); |
c40be1ad | 589 | if ((BignumInt) t > a[j + k]) |
32874aea | 590 | c++; |
c40be1ad | 591 | a[j + k] -= (BignumInt) t; |
e5574168 | 592 | } |
593 | ||
594 | /* Add back m in case of borrow */ | |
595 | if (c != h) { | |
596 | t = 0; | |
c40be1ad | 597 | for (k = 0; k < mlen; k++) { |
e5574168 | 598 | t += m[k]; |
c40be1ad MW |
599 | t += a[j + k]; |
600 | a[j + k] = (BignumInt) t; | |
a3412f52 | 601 | t = t >> BIGNUM_INT_BITS; |
e5574168 | 602 | } |
32874aea | 603 | q--; |
e5574168 | 604 | } |
c40be1ad | 605 | |
32874aea | 606 | if (quot) |
c40be1ad MW |
607 | internal_add_shifted(quot, q, |
608 | qshift + BIGNUM_INT_BITS * (i + 1 - mlen)); | |
609 | ||
610 | if (i >= mlen) { | |
611 | h = a[i]; | |
612 | a[i] = 0; | |
613 | } | |
e5574168 | 614 | } |
615 | } | |
616 | ||
c40be1ad MW |
617 | static void shift_left(BignumInt *x, int xlen, int shift) |
618 | { | |
619 | int i; | |
620 | ||
621 | if (!shift) | |
622 | return; | |
623 | for (i = xlen; --i > 0; ) | |
624 | x[i] = (x[i] << shift) | (x[i - 1] >> (BIGNUM_INT_BITS - shift)); | |
625 | x[0] = x[0] << shift; | |
626 | } | |
627 | ||
628 | static void shift_right(BignumInt *x, int xlen, int shift) | |
629 | { | |
630 | int i; | |
631 | ||
632 | if (!shift || !xlen) | |
633 | return; | |
634 | xlen--; | |
635 | for (i = 0; i < xlen; i++) | |
636 | x[i] = (x[i] >> shift) | (x[i + 1] << (BIGNUM_INT_BITS - shift)); | |
637 | x[i] = x[i] >> shift; | |
638 | } | |
639 | ||
e5574168 | 640 | /* |
09095ac5 | 641 | * Compute (base ^ exp) % mod, the pedestrian way. |
e5574168 | 642 | */ |
09095ac5 | 643 | Bignum modpow_simple(Bignum base_in, Bignum exp, Bignum mod) |
e5574168 | 644 | { |
5a502a19 | 645 | BignumInt *a, *b, *n, *m, *scratch; |
09095ac5 | 646 | int mshift; |
5a502a19 | 647 | int mlen, scratchlen, i, j; |
09095ac5 | 648 | Bignum base, result; |
ed953b91 | 649 | |
650 | /* | |
651 | * The most significant word of mod needs to be non-zero. It | |
652 | * should already be, but let's make sure. | |
653 | */ | |
654 | assert(mod[mod[0]] != 0); | |
655 | ||
656 | /* | |
657 | * Make sure the base is smaller than the modulus, by reducing | |
658 | * it modulo the modulus if not. | |
659 | */ | |
660 | base = bigmod(base_in, mod); | |
e5574168 | 661 | |
09095ac5 | 662 | /* Allocate m of size mlen, copy mod to m */ |
09095ac5 | 663 | mlen = mod[0]; |
664 | m = snewn(mlen, BignumInt); | |
665 | for (j = 0; j < mlen; j++) | |
c40be1ad | 666 | m[j] = mod[j + 1]; |
09095ac5 | 667 | |
668 | /* Shift m left to make msb bit set */ | |
669 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) | |
c40be1ad | 670 | if ((m[mlen - 1] << mshift) & BIGNUM_TOP_BIT) |
09095ac5 | 671 | break; |
c40be1ad MW |
672 | if (mshift) |
673 | shift_left(m, mlen, mshift); | |
09095ac5 | 674 | |
675 | /* Allocate n of size mlen, copy base to n */ | |
676 | n = snewn(mlen, BignumInt); | |
c40be1ad MW |
677 | for (i = 0; i < (int)base[0]; i++) |
678 | n[i] = base[i + 1]; | |
679 | for (; i < mlen; i++) | |
680 | n[i] = 0; | |
09095ac5 | 681 | |
682 | /* Allocate a and b of size 2*mlen. Set a = 1 */ | |
683 | a = snewn(2 * mlen, BignumInt); | |
684 | b = snewn(2 * mlen, BignumInt); | |
c40be1ad MW |
685 | a[0] = 1; |
686 | for (i = 1; i < 2 * mlen; i++) | |
09095ac5 | 687 | a[i] = 0; |
09095ac5 | 688 | |
5a502a19 | 689 | /* Scratch space for multiplies */ |
690 | scratchlen = mul_compute_scratch(mlen); | |
691 | scratch = snewn(scratchlen, BignumInt); | |
692 | ||
09095ac5 | 693 | /* Skip leading zero bits of exp. */ |
694 | i = 0; | |
695 | j = BIGNUM_INT_BITS-1; | |
696 | while (i < (int)exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) { | |
697 | j--; | |
698 | if (j < 0) { | |
699 | i++; | |
700 | j = BIGNUM_INT_BITS-1; | |
701 | } | |
702 | } | |
703 | ||
704 | /* Main computation */ | |
705 | while (i < (int)exp[0]) { | |
706 | while (j >= 0) { | |
c40be1ad | 707 | internal_mul(a, a, b, mlen, scratch); |
09095ac5 | 708 | internal_mod(b, mlen * 2, m, mlen, NULL, 0); |
709 | if ((exp[exp[0] - i] & (1 << j)) != 0) { | |
c40be1ad | 710 | internal_mul(b, n, a, mlen, scratch); |
09095ac5 | 711 | internal_mod(a, mlen * 2, m, mlen, NULL, 0); |
712 | } else { | |
713 | BignumInt *t; | |
714 | t = a; | |
715 | a = b; | |
716 | b = t; | |
717 | } | |
718 | j--; | |
719 | } | |
720 | i++; | |
721 | j = BIGNUM_INT_BITS-1; | |
722 | } | |
723 | ||
724 | /* Fixup result in case the modulus was shifted */ | |
725 | if (mshift) { | |
c40be1ad MW |
726 | shift_left(a, mlen + 1, mshift); |
727 | internal_mod(a, mlen + 1, m, mlen, NULL, 0); | |
728 | shift_right(a, mlen, mshift); | |
09095ac5 | 729 | } |
730 | ||
731 | /* Copy result to buffer */ | |
732 | result = newbn(mod[0]); | |
733 | for (i = 0; i < mlen; i++) | |
c40be1ad | 734 | result[i + 1] = a[i]; |
09095ac5 | 735 | while (result[0] > 1 && result[result[0]] == 0) |
736 | result[0]--; | |
737 | ||
738 | /* Free temporary arrays */ | |
739 | for (i = 0; i < 2 * mlen; i++) | |
740 | a[i] = 0; | |
741 | sfree(a); | |
5a502a19 | 742 | for (i = 0; i < scratchlen; i++) |
743 | scratch[i] = 0; | |
744 | sfree(scratch); | |
09095ac5 | 745 | for (i = 0; i < 2 * mlen; i++) |
746 | b[i] = 0; | |
747 | sfree(b); | |
748 | for (i = 0; i < mlen; i++) | |
749 | m[i] = 0; | |
750 | sfree(m); | |
751 | for (i = 0; i < mlen; i++) | |
752 | n[i] = 0; | |
753 | sfree(n); | |
754 | ||
755 | freebn(base); | |
756 | ||
757 | return result; | |
758 | } | |
759 | ||
760 | /* | |
761 | * Compute (base ^ exp) % mod. Uses the Montgomery multiplication | |
762 | * technique where possible, falling back to modpow_simple otherwise. | |
763 | */ | |
764 | Bignum modpow(Bignum base_in, Bignum exp, Bignum mod) | |
765 | { | |
5a502a19 | 766 | BignumInt *a, *b, *x, *n, *mninv, *scratch; |
767 | int len, scratchlen, i, j; | |
09095ac5 | 768 | Bignum base, base2, r, rn, inv, result; |
769 | ||
770 | /* | |
771 | * The most significant word of mod needs to be non-zero. It | |
772 | * should already be, but let's make sure. | |
773 | */ | |
774 | assert(mod[mod[0]] != 0); | |
775 | ||
132c534f | 776 | /* |
777 | * mod had better be odd, or we can't do Montgomery multiplication | |
778 | * using a power of two at all. | |
779 | */ | |
09095ac5 | 780 | if (!(mod[1] & 1)) |
781 | return modpow_simple(base_in, exp, mod); | |
782 | ||
783 | /* | |
784 | * Make sure the base is smaller than the modulus, by reducing | |
785 | * it modulo the modulus if not. | |
786 | */ | |
787 | base = bigmod(base_in, mod); | |
e5574168 | 788 | |
132c534f | 789 | /* |
790 | * Compute the inverse of n mod r, for monty_reduce. (In fact we | |
791 | * want the inverse of _minus_ n mod r, but we'll sort that out | |
792 | * below.) | |
793 | */ | |
794 | len = mod[0]; | |
795 | r = bn_power_2(BIGNUM_INT_BITS * len); | |
796 | inv = modinv(mod, r); | |
e5574168 | 797 | |
132c534f | 798 | /* |
799 | * Multiply the base by r mod n, to get it into Montgomery | |
800 | * representation. | |
801 | */ | |
802 | base2 = modmul(base, r, mod); | |
803 | freebn(base); | |
804 | base = base2; | |
805 | ||
806 | rn = bigmod(r, mod); /* r mod n, i.e. Montgomerified 1 */ | |
807 | ||
808 | freebn(r); /* won't need this any more */ | |
809 | ||
810 | /* | |
c40be1ad MW |
811 | * Set up internal arrays of the right lengths containing the base, |
812 | * the modulus, and the modulus's inverse. | |
132c534f | 813 | */ |
814 | n = snewn(len, BignumInt); | |
815 | for (j = 0; j < len; j++) | |
c40be1ad | 816 | n[j] = mod[j + 1]; |
132c534f | 817 | |
818 | mninv = snewn(len, BignumInt); | |
819 | for (j = 0; j < len; j++) | |
c40be1ad | 820 | mninv[j] = (j < (int)inv[0] ? inv[j + 1] : 0); |
132c534f | 821 | freebn(inv); /* we don't need this copy of it any more */ |
822 | /* Now negate mninv mod r, so it's the inverse of -n rather than +n. */ | |
823 | x = snewn(len, BignumInt); | |
824 | for (j = 0; j < len; j++) | |
825 | x[j] = 0; | |
826 | internal_sub(x, mninv, mninv, len); | |
827 | ||
828 | /* x = snewn(len, BignumInt); */ /* already done above */ | |
829 | for (j = 0; j < len; j++) | |
c40be1ad | 830 | x[j] = (j < (int)base[0] ? base[j + 1] : 0); |
132c534f | 831 | freebn(base); /* we don't need this copy of it any more */ |
832 | ||
833 | a = snewn(2*len, BignumInt); | |
834 | b = snewn(2*len, BignumInt); | |
835 | for (j = 0; j < len; j++) | |
c40be1ad | 836 | a[j] = (j < (int)rn[0] ? rn[j + 1] : 0); |
132c534f | 837 | freebn(rn); |
838 | ||
5a502a19 | 839 | /* Scratch space for multiplies */ |
840 | scratchlen = 3*len + mul_compute_scratch(len); | |
841 | scratch = snewn(scratchlen, BignumInt); | |
e5574168 | 842 | |
843 | /* Skip leading zero bits of exp. */ | |
32874aea | 844 | i = 0; |
a3412f52 | 845 | j = BIGNUM_INT_BITS-1; |
62ddb51e | 846 | while (i < (int)exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) { |
e5574168 | 847 | j--; |
32874aea | 848 | if (j < 0) { |
849 | i++; | |
a3412f52 | 850 | j = BIGNUM_INT_BITS-1; |
32874aea | 851 | } |
e5574168 | 852 | } |
853 | ||
854 | /* Main computation */ | |
62ddb51e | 855 | while (i < (int)exp[0]) { |
e5574168 | 856 | while (j >= 0) { |
c40be1ad | 857 | internal_mul(a, a, b, len, scratch); |
5a502a19 | 858 | monty_reduce(b, n, mninv, scratch, len); |
e5574168 | 859 | if ((exp[exp[0] - i] & (1 << j)) != 0) { |
c40be1ad | 860 | internal_mul(b, x, a, len, scratch); |
5a502a19 | 861 | monty_reduce(a, n, mninv, scratch, len); |
e5574168 | 862 | } else { |
a3412f52 | 863 | BignumInt *t; |
32874aea | 864 | t = a; |
865 | a = b; | |
866 | b = t; | |
e5574168 | 867 | } |
868 | j--; | |
869 | } | |
32874aea | 870 | i++; |
a3412f52 | 871 | j = BIGNUM_INT_BITS-1; |
e5574168 | 872 | } |
873 | ||
132c534f | 874 | /* |
875 | * Final monty_reduce to get back from the adjusted Montgomery | |
876 | * representation. | |
877 | */ | |
5a502a19 | 878 | monty_reduce(a, n, mninv, scratch, len); |
e5574168 | 879 | |
880 | /* Copy result to buffer */ | |
59600f67 | 881 | result = newbn(mod[0]); |
132c534f | 882 | for (i = 0; i < len; i++) |
c40be1ad | 883 | result[i + 1] = a[i]; |
32874aea | 884 | while (result[0] > 1 && result[result[0]] == 0) |
885 | result[0]--; | |
e5574168 | 886 | |
887 | /* Free temporary arrays */ | |
5a502a19 | 888 | for (i = 0; i < scratchlen; i++) |
889 | scratch[i] = 0; | |
890 | sfree(scratch); | |
132c534f | 891 | for (i = 0; i < 2 * len; i++) |
32874aea | 892 | a[i] = 0; |
893 | sfree(a); | |
132c534f | 894 | for (i = 0; i < 2 * len; i++) |
32874aea | 895 | b[i] = 0; |
896 | sfree(b); | |
132c534f | 897 | for (i = 0; i < len; i++) |
898 | mninv[i] = 0; | |
899 | sfree(mninv); | |
900 | for (i = 0; i < len; i++) | |
32874aea | 901 | n[i] = 0; |
902 | sfree(n); | |
132c534f | 903 | for (i = 0; i < len; i++) |
904 | x[i] = 0; | |
905 | sfree(x); | |
ed953b91 | 906 | |
59600f67 | 907 | return result; |
e5574168 | 908 | } |
7cca0d81 | 909 | |
910 | /* | |
911 | * Compute (p * q) % mod. | |
912 | * The most significant word of mod MUST be non-zero. | |
913 | * We assume that the result array is the same size as the mod array. | |
914 | */ | |
59600f67 | 915 | Bignum modmul(Bignum p, Bignum q, Bignum mod) |
7cca0d81 | 916 | { |
5a502a19 | 917 | BignumInt *a, *n, *m, *o, *scratch; |
918 | int mshift, scratchlen; | |
80b10571 | 919 | int pqlen, mlen, rlen, i, j; |
59600f67 | 920 | Bignum result; |
7cca0d81 | 921 | |
922 | /* Allocate m of size mlen, copy mod to m */ | |
7cca0d81 | 923 | mlen = mod[0]; |
a3412f52 | 924 | m = snewn(mlen, BignumInt); |
32874aea | 925 | for (j = 0; j < mlen; j++) |
c40be1ad | 926 | m[j] = mod[j + 1]; |
7cca0d81 | 927 | |
928 | /* Shift m left to make msb bit set */ | |
a3412f52 | 929 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
c40be1ad | 930 | if ((m[mlen - 1] << mshift) & BIGNUM_TOP_BIT) |
32874aea | 931 | break; |
c40be1ad MW |
932 | if (mshift) |
933 | shift_left(m, mlen, mshift); | |
7cca0d81 | 934 | |
935 | pqlen = (p[0] > q[0] ? p[0] : q[0]); | |
936 | ||
aca5132b MW |
937 | /* Make sure that we're allowing enough space. The shifting below will |
938 | * underflow the vectors we allocate if `pqlen' is too small. | |
939 | */ | |
940 | if (2*pqlen <= mlen) | |
941 | pqlen = mlen/2 + 1; | |
942 | ||
7cca0d81 | 943 | /* Allocate n of size pqlen, copy p to n */ |
a3412f52 | 944 | n = snewn(pqlen, BignumInt); |
c40be1ad MW |
945 | for (i = 0; i < (int)p[0]; i++) |
946 | n[i] = p[i + 1]; | |
947 | for (; i < pqlen; i++) | |
948 | n[i] = 0; | |
7cca0d81 | 949 | |
950 | /* Allocate o of size pqlen, copy q to o */ | |
a3412f52 | 951 | o = snewn(pqlen, BignumInt); |
c40be1ad MW |
952 | for (i = 0; i < (int)q[0]; i++) |
953 | o[i] = q[i + 1]; | |
954 | for (; i < pqlen; i++) | |
955 | o[i] = 0; | |
7cca0d81 | 956 | |
957 | /* Allocate a of size 2*pqlen for result */ | |
a3412f52 | 958 | a = snewn(2 * pqlen, BignumInt); |
7cca0d81 | 959 | |
5a502a19 | 960 | /* Scratch space for multiplies */ |
961 | scratchlen = mul_compute_scratch(pqlen); | |
962 | scratch = snewn(scratchlen, BignumInt); | |
963 | ||
7cca0d81 | 964 | /* Main computation */ |
5a502a19 | 965 | internal_mul(n, o, a, pqlen, scratch); |
32874aea | 966 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
7cca0d81 | 967 | |
968 | /* Fixup result in case the modulus was shifted */ | |
969 | if (mshift) { | |
c40be1ad MW |
970 | shift_left(a, mlen + 1, mshift); |
971 | internal_mod(a, mlen + 1, m, mlen, NULL, 0); | |
972 | shift_right(a, mlen, mshift); | |
7cca0d81 | 973 | } |
974 | ||
975 | /* Copy result to buffer */ | |
32874aea | 976 | rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2); |
80b10571 | 977 | result = newbn(rlen); |
978 | for (i = 0; i < rlen; i++) | |
c40be1ad | 979 | result[i + 1] = a[i]; |
32874aea | 980 | while (result[0] > 1 && result[result[0]] == 0) |
981 | result[0]--; | |
7cca0d81 | 982 | |
983 | /* Free temporary arrays */ | |
5a502a19 | 984 | for (i = 0; i < scratchlen; i++) |
985 | scratch[i] = 0; | |
986 | sfree(scratch); | |
32874aea | 987 | for (i = 0; i < 2 * pqlen; i++) |
988 | a[i] = 0; | |
989 | sfree(a); | |
990 | for (i = 0; i < mlen; i++) | |
991 | m[i] = 0; | |
992 | sfree(m); | |
993 | for (i = 0; i < pqlen; i++) | |
994 | n[i] = 0; | |
995 | sfree(n); | |
996 | for (i = 0; i < pqlen; i++) | |
997 | o[i] = 0; | |
998 | sfree(o); | |
59600f67 | 999 | |
1000 | return result; | |
7cca0d81 | 1001 | } |
1002 | ||
1003 | /* | |
9400cf6f | 1004 | * Compute p % mod. |
1005 | * The most significant word of mod MUST be non-zero. | |
1006 | * We assume that the result array is the same size as the mod array. | |
5c72ca61 | 1007 | * We optionally write out a quotient if `quotient' is non-NULL. |
1008 | * We can avoid writing out the result if `result' is NULL. | |
9400cf6f | 1009 | */ |
f28753ab | 1010 | static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient) |
9400cf6f | 1011 | { |
a3412f52 | 1012 | BignumInt *n, *m; |
9400cf6f | 1013 | int mshift; |
1014 | int plen, mlen, i, j; | |
1015 | ||
1016 | /* Allocate m of size mlen, copy mod to m */ | |
9400cf6f | 1017 | mlen = mod[0]; |
a3412f52 | 1018 | m = snewn(mlen, BignumInt); |
32874aea | 1019 | for (j = 0; j < mlen; j++) |
c40be1ad | 1020 | m[j] = mod[j + 1]; |
9400cf6f | 1021 | |
1022 | /* Shift m left to make msb bit set */ | |
a3412f52 | 1023 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
c40be1ad | 1024 | if ((m[mlen - 1] << mshift) & BIGNUM_TOP_BIT) |
32874aea | 1025 | break; |
c40be1ad MW |
1026 | if (mshift) |
1027 | shift_left(m, mlen, mshift); | |
9400cf6f | 1028 | |
1029 | plen = p[0]; | |
1030 | /* Ensure plen > mlen */ | |
32874aea | 1031 | if (plen <= mlen) |
1032 | plen = mlen + 1; | |
9400cf6f | 1033 | |
1034 | /* Allocate n of size plen, copy p to n */ | |
a3412f52 | 1035 | n = snewn(plen, BignumInt); |
c40be1ad MW |
1036 | for (i = 0; i < (int)p[0]; i++) |
1037 | n[i] = p[i + 1]; | |
1038 | for (; i < plen; i++) | |
1039 | n[i] = 0; | |
9400cf6f | 1040 | |
1041 | /* Main computation */ | |
1042 | internal_mod(n, plen, m, mlen, quotient, mshift); | |
1043 | ||
1044 | /* Fixup result in case the modulus was shifted */ | |
1045 | if (mshift) { | |
c40be1ad | 1046 | shift_left(n, mlen + 1, mshift); |
9400cf6f | 1047 | internal_mod(n, plen, m, mlen, quotient, 0); |
c40be1ad | 1048 | shift_right(n, mlen, mshift); |
9400cf6f | 1049 | } |
1050 | ||
1051 | /* Copy result to buffer */ | |
5c72ca61 | 1052 | if (result) { |
c40be1ad MW |
1053 | for (i = 0; i < (int)result[0]; i++) |
1054 | result[i + 1] = i < plen ? n[i] : 0; | |
1055 | bn_restore_invariant(result); | |
9400cf6f | 1056 | } |
1057 | ||
1058 | /* Free temporary arrays */ | |
32874aea | 1059 | for (i = 0; i < mlen; i++) |
1060 | m[i] = 0; | |
1061 | sfree(m); | |
1062 | for (i = 0; i < plen; i++) | |
1063 | n[i] = 0; | |
1064 | sfree(n); | |
9400cf6f | 1065 | } |
1066 | ||
1067 | /* | |
7cca0d81 | 1068 | * Decrement a number. |
1069 | */ | |
32874aea | 1070 | void decbn(Bignum bn) |
1071 | { | |
7cca0d81 | 1072 | int i = 1; |
62ddb51e | 1073 | while (i < (int)bn[0] && bn[i] == 0) |
a3412f52 | 1074 | bn[i++] = BIGNUM_INT_MASK; |
7cca0d81 | 1075 | bn[i]--; |
1076 | } | |
1077 | ||
27cd7fc2 | 1078 | Bignum bignum_from_bytes(const unsigned char *data, int nbytes) |
32874aea | 1079 | { |
3709bfe9 | 1080 | Bignum result; |
1081 | int w, i; | |
1082 | ||
a3412f52 | 1083 | w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */ |
3709bfe9 | 1084 | |
1085 | result = newbn(w); | |
32874aea | 1086 | for (i = 1; i <= w; i++) |
1087 | result[i] = 0; | |
1088 | for (i = nbytes; i--;) { | |
1089 | unsigned char byte = *data++; | |
a3412f52 | 1090 | result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS); |
3709bfe9 | 1091 | } |
1092 | ||
32874aea | 1093 | while (result[0] > 1 && result[result[0]] == 0) |
1094 | result[0]--; | |
3709bfe9 | 1095 | return result; |
1096 | } | |
1097 | ||
7cca0d81 | 1098 | /* |
2e85c969 | 1099 | * Read an SSH-1-format bignum from a data buffer. Return the number |
0016d70b | 1100 | * of bytes consumed, or -1 if there wasn't enough data. |
7cca0d81 | 1101 | */ |
0016d70b | 1102 | int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result) |
32874aea | 1103 | { |
27cd7fc2 | 1104 | const unsigned char *p = data; |
7cca0d81 | 1105 | int i; |
1106 | int w, b; | |
1107 | ||
0016d70b | 1108 | if (len < 2) |
1109 | return -1; | |
1110 | ||
7cca0d81 | 1111 | w = 0; |
32874aea | 1112 | for (i = 0; i < 2; i++) |
1113 | w = (w << 8) + *p++; | |
1114 | b = (w + 7) / 8; /* bits -> bytes */ | |
7cca0d81 | 1115 | |
0016d70b | 1116 | if (len < b+2) |
1117 | return -1; | |
1118 | ||
32874aea | 1119 | if (!result) /* just return length */ |
1120 | return b + 2; | |
a52f067e | 1121 | |
3709bfe9 | 1122 | *result = bignum_from_bytes(p, b); |
7cca0d81 | 1123 | |
3709bfe9 | 1124 | return p + b - data; |
7cca0d81 | 1125 | } |
5c58ad2d | 1126 | |
1127 | /* | |
2e85c969 | 1128 | * Return the bit count of a bignum, for SSH-1 encoding. |
5c58ad2d | 1129 | */ |
32874aea | 1130 | int bignum_bitcount(Bignum bn) |
1131 | { | |
a3412f52 | 1132 | int bitcount = bn[0] * BIGNUM_INT_BITS - 1; |
32874aea | 1133 | while (bitcount >= 0 |
a3412f52 | 1134 | && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--; |
5c58ad2d | 1135 | return bitcount + 1; |
1136 | } | |
1137 | ||
1138 | /* | |
2e85c969 | 1139 | * Return the byte length of a bignum when SSH-1 encoded. |
5c58ad2d | 1140 | */ |
32874aea | 1141 | int ssh1_bignum_length(Bignum bn) |
1142 | { | |
1143 | return 2 + (bignum_bitcount(bn) + 7) / 8; | |
ddecd643 | 1144 | } |
1145 | ||
1146 | /* | |
2e85c969 | 1147 | * Return the byte length of a bignum when SSH-2 encoded. |
ddecd643 | 1148 | */ |
32874aea | 1149 | int ssh2_bignum_length(Bignum bn) |
1150 | { | |
1151 | return 4 + (bignum_bitcount(bn) + 8) / 8; | |
5c58ad2d | 1152 | } |
1153 | ||
1154 | /* | |
1155 | * Return a byte from a bignum; 0 is least significant, etc. | |
1156 | */ | |
32874aea | 1157 | int bignum_byte(Bignum bn, int i) |
1158 | { | |
62ddb51e | 1159 | if (i >= (int)(BIGNUM_INT_BYTES * bn[0])) |
32874aea | 1160 | return 0; /* beyond the end */ |
5c58ad2d | 1161 | else |
a3412f52 | 1162 | return (bn[i / BIGNUM_INT_BYTES + 1] >> |
1163 | ((i % BIGNUM_INT_BYTES)*8)) & 0xFF; | |
5c58ad2d | 1164 | } |
1165 | ||
1166 | /* | |
9400cf6f | 1167 | * Return a bit from a bignum; 0 is least significant, etc. |
1168 | */ | |
32874aea | 1169 | int bignum_bit(Bignum bn, int i) |
1170 | { | |
62ddb51e | 1171 | if (i >= (int)(BIGNUM_INT_BITS * bn[0])) |
32874aea | 1172 | return 0; /* beyond the end */ |
9400cf6f | 1173 | else |
a3412f52 | 1174 | return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1; |
9400cf6f | 1175 | } |
1176 | ||
1177 | /* | |
1178 | * Set a bit in a bignum; 0 is least significant, etc. | |
1179 | */ | |
32874aea | 1180 | void bignum_set_bit(Bignum bn, int bitnum, int value) |
1181 | { | |
62ddb51e | 1182 | if (bitnum >= (int)(BIGNUM_INT_BITS * bn[0])) |
32874aea | 1183 | abort(); /* beyond the end */ |
9400cf6f | 1184 | else { |
a3412f52 | 1185 | int v = bitnum / BIGNUM_INT_BITS + 1; |
1186 | int mask = 1 << (bitnum % BIGNUM_INT_BITS); | |
32874aea | 1187 | if (value) |
1188 | bn[v] |= mask; | |
1189 | else | |
1190 | bn[v] &= ~mask; | |
9400cf6f | 1191 | } |
1192 | } | |
1193 | ||
1194 | /* | |
2e85c969 | 1195 | * Write a SSH-1-format bignum into a buffer. It is assumed the |
5c58ad2d | 1196 | * buffer is big enough. Returns the number of bytes used. |
1197 | */ | |
32874aea | 1198 | int ssh1_write_bignum(void *data, Bignum bn) |
1199 | { | |
5c58ad2d | 1200 | unsigned char *p = data; |
1201 | int len = ssh1_bignum_length(bn); | |
1202 | int i; | |
ddecd643 | 1203 | int bitc = bignum_bitcount(bn); |
5c58ad2d | 1204 | |
1205 | *p++ = (bitc >> 8) & 0xFF; | |
32874aea | 1206 | *p++ = (bitc) & 0xFF; |
1207 | for (i = len - 2; i--;) | |
1208 | *p++ = bignum_byte(bn, i); | |
5c58ad2d | 1209 | return len; |
1210 | } | |
9400cf6f | 1211 | |
1212 | /* | |
1213 | * Compare two bignums. Returns like strcmp. | |
1214 | */ | |
32874aea | 1215 | int bignum_cmp(Bignum a, Bignum b) |
1216 | { | |
9400cf6f | 1217 | int amax = a[0], bmax = b[0]; |
1218 | int i = (amax > bmax ? amax : bmax); | |
1219 | while (i) { | |
a3412f52 | 1220 | BignumInt aval = (i > amax ? 0 : a[i]); |
1221 | BignumInt bval = (i > bmax ? 0 : b[i]); | |
32874aea | 1222 | if (aval < bval) |
1223 | return -1; | |
1224 | if (aval > bval) | |
1225 | return +1; | |
1226 | i--; | |
9400cf6f | 1227 | } |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | /* | |
1232 | * Right-shift one bignum to form another. | |
1233 | */ | |
32874aea | 1234 | Bignum bignum_rshift(Bignum a, int shift) |
1235 | { | |
9400cf6f | 1236 | Bignum ret; |
1237 | int i, shiftw, shiftb, shiftbb, bits; | |
a3412f52 | 1238 | BignumInt ai, ai1; |
9400cf6f | 1239 | |
ddecd643 | 1240 | bits = bignum_bitcount(a) - shift; |
a3412f52 | 1241 | ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS); |
9400cf6f | 1242 | |
1243 | if (ret) { | |
a3412f52 | 1244 | shiftw = shift / BIGNUM_INT_BITS; |
1245 | shiftb = shift % BIGNUM_INT_BITS; | |
1246 | shiftbb = BIGNUM_INT_BITS - shiftb; | |
32874aea | 1247 | |
1248 | ai1 = a[shiftw + 1]; | |
62ddb51e | 1249 | for (i = 1; i <= (int)ret[0]; i++) { |
32874aea | 1250 | ai = ai1; |
62ddb51e | 1251 | ai1 = (i + shiftw + 1 <= (int)a[0] ? a[i + shiftw + 1] : 0); |
a3412f52 | 1252 | ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK; |
32874aea | 1253 | } |
9400cf6f | 1254 | } |
1255 | ||
1256 | return ret; | |
1257 | } | |
1258 | ||
1259 | /* | |
1260 | * Non-modular multiplication and addition. | |
1261 | */ | |
32874aea | 1262 | Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) |
1263 | { | |
9400cf6f | 1264 | int alen = a[0], blen = b[0]; |
1265 | int mlen = (alen > blen ? alen : blen); | |
1266 | int rlen, i, maxspot; | |
5a502a19 | 1267 | int wslen; |
a3412f52 | 1268 | BignumInt *workspace; |
9400cf6f | 1269 | Bignum ret; |
1270 | ||
5a502a19 | 1271 | /* mlen space for a, mlen space for b, 2*mlen for result, |
1272 | * plus scratch space for multiplication */ | |
1273 | wslen = mlen * 4 + mul_compute_scratch(mlen); | |
1274 | workspace = snewn(wslen, BignumInt); | |
9400cf6f | 1275 | for (i = 0; i < mlen; i++) { |
c40be1ad MW |
1276 | workspace[0 * mlen + i] = i < (int)a[0] ? a[i + 1] : 0; |
1277 | workspace[1 * mlen + i] = i < (int)b[0] ? b[i + 1] : 0; | |
9400cf6f | 1278 | } |
1279 | ||
32874aea | 1280 | internal_mul(workspace + 0 * mlen, workspace + 1 * mlen, |
5a502a19 | 1281 | workspace + 2 * mlen, mlen, workspace + 4 * mlen); |
9400cf6f | 1282 | |
1283 | /* now just copy the result back */ | |
1284 | rlen = alen + blen + 1; | |
62ddb51e | 1285 | if (addend && rlen <= (int)addend[0]) |
32874aea | 1286 | rlen = addend[0] + 1; |
9400cf6f | 1287 | ret = newbn(rlen); |
1288 | maxspot = 0; | |
c40be1ad MW |
1289 | for (i = 0; i < (int)ret[0]; i++) { |
1290 | ret[i + 1] = (i < 2 * mlen ? workspace[2 * mlen + i] : 0); | |
1291 | if (ret[i + 1] != 0) | |
1292 | maxspot = i + 1; | |
9400cf6f | 1293 | } |
1294 | ret[0] = maxspot; | |
1295 | ||
1296 | /* now add in the addend, if any */ | |
1297 | if (addend) { | |
a3412f52 | 1298 | BignumDblInt carry = 0; |
32874aea | 1299 | for (i = 1; i <= rlen; i++) { |
62ddb51e | 1300 | carry += (i <= (int)ret[0] ? ret[i] : 0); |
1301 | carry += (i <= (int)addend[0] ? addend[i] : 0); | |
a3412f52 | 1302 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
1303 | carry >>= BIGNUM_INT_BITS; | |
32874aea | 1304 | if (ret[i] != 0 && i > maxspot) |
1305 | maxspot = i; | |
1306 | } | |
9400cf6f | 1307 | } |
1308 | ret[0] = maxspot; | |
1309 | ||
5a502a19 | 1310 | for (i = 0; i < wslen; i++) |
1311 | workspace[i] = 0; | |
c523f55f | 1312 | sfree(workspace); |
9400cf6f | 1313 | return ret; |
1314 | } | |
1315 | ||
1316 | /* | |
1317 | * Non-modular multiplication. | |
1318 | */ | |
32874aea | 1319 | Bignum bigmul(Bignum a, Bignum b) |
1320 | { | |
9400cf6f | 1321 | return bigmuladd(a, b, NULL); |
1322 | } | |
1323 | ||
1324 | /* | |
d737853b | 1325 | * Simple addition. |
1326 | */ | |
1327 | Bignum bigadd(Bignum a, Bignum b) | |
1328 | { | |
1329 | int alen = a[0], blen = b[0]; | |
1330 | int rlen = (alen > blen ? alen : blen) + 1; | |
1331 | int i, maxspot; | |
1332 | Bignum ret; | |
1333 | BignumDblInt carry; | |
1334 | ||
1335 | ret = newbn(rlen); | |
1336 | ||
1337 | carry = 0; | |
1338 | maxspot = 0; | |
1339 | for (i = 1; i <= rlen; i++) { | |
1340 | carry += (i <= (int)a[0] ? a[i] : 0); | |
1341 | carry += (i <= (int)b[0] ? b[i] : 0); | |
1342 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; | |
1343 | carry >>= BIGNUM_INT_BITS; | |
1344 | if (ret[i] != 0 && i > maxspot) | |
1345 | maxspot = i; | |
1346 | } | |
1347 | ret[0] = maxspot; | |
1348 | ||
1349 | return ret; | |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Subtraction. Returns a-b, or NULL if the result would come out | |
1354 | * negative (recall that this entire bignum module only handles | |
1355 | * positive numbers). | |
1356 | */ | |
1357 | Bignum bigsub(Bignum a, Bignum b) | |
1358 | { | |
1359 | int alen = a[0], blen = b[0]; | |
1360 | int rlen = (alen > blen ? alen : blen); | |
1361 | int i, maxspot; | |
1362 | Bignum ret; | |
1363 | BignumDblInt carry; | |
1364 | ||
1365 | ret = newbn(rlen); | |
1366 | ||
1367 | carry = 1; | |
1368 | maxspot = 0; | |
1369 | for (i = 1; i <= rlen; i++) { | |
1370 | carry += (i <= (int)a[0] ? a[i] : 0); | |
1371 | carry += (i <= (int)b[0] ? b[i] ^ BIGNUM_INT_MASK : BIGNUM_INT_MASK); | |
1372 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; | |
1373 | carry >>= BIGNUM_INT_BITS; | |
1374 | if (ret[i] != 0 && i > maxspot) | |
1375 | maxspot = i; | |
1376 | } | |
1377 | ret[0] = maxspot; | |
1378 | ||
1379 | if (!carry) { | |
1380 | freebn(ret); | |
1381 | return NULL; | |
1382 | } | |
1383 | ||
1384 | return ret; | |
1385 | } | |
1386 | ||
1387 | /* | |
7961438b MW |
1388 | * Return a bignum which is the result of shifting another left by N bits. |
1389 | * If N is negative then you get a right shift instead. | |
1390 | */ | |
1391 | Bignum biglsl(Bignum x, int n) | |
1392 | { | |
1393 | Bignum d; | |
1394 | unsigned o, i; | |
1395 | ||
1396 | if (!n || !x[0]) | |
1397 | return copybn(x); | |
1398 | else if (n < 0) | |
1399 | return biglsr(x, -n); | |
1400 | ||
1401 | o = n/BIGNUM_INT_BITS; | |
1402 | n %= BIGNUM_INT_BITS; | |
1403 | d = newbn(x[0] + o + !!n); | |
1404 | ||
1405 | for (i = 1; i <= o; i++) | |
1406 | d[i] = 0; | |
1407 | ||
1408 | if (!n) { | |
1409 | for (i = 1; i <= x[0]; i++) | |
1410 | d[o + i] = x[i]; | |
1411 | } else { | |
1412 | d[o + 1] = x[1] << n; | |
1413 | for (i = 2; i <= x[0]; i--) | |
1414 | d[o + i] = (x[i] << n) | (x[i - 1] >> (BIGNUM_INT_BITS - n)); | |
1415 | d[o + x[0] + 1] = x[x[0]] >> (BIGNUM_INT_BITS - n); | |
1416 | } | |
1417 | ||
1418 | bn_restore_invariant(d); | |
1419 | return d; | |
1420 | } | |
1421 | ||
1422 | /* | |
1423 | * Return a bignum which is the result of shifting another right by N bits | |
1424 | * (discarding the least significant N bits, and shifting zeroes in at the | |
1425 | * most significant end). If N is negative then you get a left shift | |
1426 | * instead. | |
1427 | */ | |
1428 | Bignum biglsr(Bignum x, int n) | |
1429 | { | |
1430 | Bignum d; | |
1431 | unsigned o, i; | |
1432 | ||
1433 | if (!n || !x[0]) | |
1434 | return copybn(x); | |
1435 | else if (n < 0) | |
1436 | return biglsl(x, -n); | |
1437 | ||
1438 | o = n/BIGNUM_INT_BITS; | |
1439 | n %= BIGNUM_INT_BITS; | |
1440 | d = newbn(x[0]); | |
1441 | ||
1442 | if (!n) { | |
1443 | for (i = o + 1; i <= x[0]; i++) | |
1444 | d[i - o] = x[i]; | |
1445 | } else { | |
1446 | d[1] = x[o + 1] >> n; | |
1447 | for (i = o + 2; i < x[0]; i++) | |
1448 | d[i - o] = x[ | |
1449 | d[o + x[0] + 1] = x[x[0]] >> (BIGNUM_INT_BITS - n); | |
1450 | for (i = x[0]; i > 1; i--) | |
1451 | d[o + i] = (x[i] << n) | (x[i - 1] >> (BIGNUM_INT_BITS - n)); | |
1452 | d[o + 1] = x[1] << n; | |
1453 | } | |
1454 | ||
1455 | bn_restore_invariant(d); | |
1456 | return d; | |
1457 | } | |
1458 | ||
1459 | /* | |
3709bfe9 | 1460 | * Create a bignum which is the bitmask covering another one. That |
1461 | * is, the smallest integer which is >= N and is also one less than | |
1462 | * a power of two. | |
1463 | */ | |
32874aea | 1464 | Bignum bignum_bitmask(Bignum n) |
1465 | { | |
3709bfe9 | 1466 | Bignum ret = copybn(n); |
1467 | int i; | |
a3412f52 | 1468 | BignumInt j; |
3709bfe9 | 1469 | |
1470 | i = ret[0]; | |
1471 | while (n[i] == 0 && i > 0) | |
32874aea | 1472 | i--; |
3709bfe9 | 1473 | if (i <= 0) |
32874aea | 1474 | return ret; /* input was zero */ |
3709bfe9 | 1475 | j = 1; |
1476 | while (j < n[i]) | |
32874aea | 1477 | j = 2 * j + 1; |
3709bfe9 | 1478 | ret[i] = j; |
1479 | while (--i > 0) | |
a3412f52 | 1480 | ret[i] = BIGNUM_INT_MASK; |
3709bfe9 | 1481 | return ret; |
1482 | } | |
1483 | ||
1484 | /* | |
5c72ca61 | 1485 | * Convert a (max 32-bit) long into a bignum. |
9400cf6f | 1486 | */ |
a3412f52 | 1487 | Bignum bignum_from_long(unsigned long nn) |
32874aea | 1488 | { |
9400cf6f | 1489 | Bignum ret; |
a3412f52 | 1490 | BignumDblInt n = nn; |
9400cf6f | 1491 | |
5c72ca61 | 1492 | ret = newbn(3); |
a3412f52 | 1493 | ret[1] = (BignumInt)(n & BIGNUM_INT_MASK); |
1494 | ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK); | |
5c72ca61 | 1495 | ret[3] = 0; |
1496 | ret[0] = (ret[2] ? 2 : 1); | |
32874aea | 1497 | return ret; |
9400cf6f | 1498 | } |
1499 | ||
1500 | /* | |
1501 | * Add a long to a bignum. | |
1502 | */ | |
a3412f52 | 1503 | Bignum bignum_add_long(Bignum number, unsigned long addendx) |
32874aea | 1504 | { |
1505 | Bignum ret = newbn(number[0] + 1); | |
9400cf6f | 1506 | int i, maxspot = 0; |
a3412f52 | 1507 | BignumDblInt carry = 0, addend = addendx; |
9400cf6f | 1508 | |
62ddb51e | 1509 | for (i = 1; i <= (int)ret[0]; i++) { |
a3412f52 | 1510 | carry += addend & BIGNUM_INT_MASK; |
62ddb51e | 1511 | carry += (i <= (int)number[0] ? number[i] : 0); |
a3412f52 | 1512 | addend >>= BIGNUM_INT_BITS; |
1513 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; | |
1514 | carry >>= BIGNUM_INT_BITS; | |
32874aea | 1515 | if (ret[i] != 0) |
1516 | maxspot = i; | |
9400cf6f | 1517 | } |
1518 | ret[0] = maxspot; | |
1519 | return ret; | |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * Compute the residue of a bignum, modulo a (max 16-bit) short. | |
1524 | */ | |
32874aea | 1525 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus) |
1526 | { | |
a3412f52 | 1527 | BignumDblInt mod, r; |
9400cf6f | 1528 | int i; |
1529 | ||
1530 | r = 0; | |
1531 | mod = modulus; | |
1532 | for (i = number[0]; i > 0; i--) | |
736cc6d1 | 1533 | r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod; |
6e522441 | 1534 | return (unsigned short) r; |
9400cf6f | 1535 | } |
1536 | ||
a3412f52 | 1537 | #ifdef DEBUG |
32874aea | 1538 | void diagbn(char *prefix, Bignum md) |
1539 | { | |
9400cf6f | 1540 | int i, nibbles, morenibbles; |
1541 | static const char hex[] = "0123456789ABCDEF"; | |
1542 | ||
5c72ca61 | 1543 | debug(("%s0x", prefix ? prefix : "")); |
9400cf6f | 1544 | |
32874aea | 1545 | nibbles = (3 + bignum_bitcount(md)) / 4; |
1546 | if (nibbles < 1) | |
1547 | nibbles = 1; | |
1548 | morenibbles = 4 * md[0] - nibbles; | |
1549 | for (i = 0; i < morenibbles; i++) | |
5c72ca61 | 1550 | debug(("-")); |
32874aea | 1551 | for (i = nibbles; i--;) |
5c72ca61 | 1552 | debug(("%c", |
1553 | hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF])); | |
9400cf6f | 1554 | |
32874aea | 1555 | if (prefix) |
5c72ca61 | 1556 | debug(("\n")); |
1557 | } | |
f28753ab | 1558 | #endif |
5c72ca61 | 1559 | |
1560 | /* | |
1561 | * Simple division. | |
1562 | */ | |
1563 | Bignum bigdiv(Bignum a, Bignum b) | |
1564 | { | |
1565 | Bignum q = newbn(a[0]); | |
1566 | bigdivmod(a, b, NULL, q); | |
1567 | return q; | |
1568 | } | |
1569 | ||
1570 | /* | |
1571 | * Simple remainder. | |
1572 | */ | |
1573 | Bignum bigmod(Bignum a, Bignum b) | |
1574 | { | |
1575 | Bignum r = newbn(b[0]); | |
1576 | bigdivmod(a, b, r, NULL); | |
1577 | return r; | |
9400cf6f | 1578 | } |
1579 | ||
1580 | /* | |
1581 | * Greatest common divisor. | |
1582 | */ | |
32874aea | 1583 | Bignum biggcd(Bignum av, Bignum bv) |
1584 | { | |
9400cf6f | 1585 | Bignum a = copybn(av); |
1586 | Bignum b = copybn(bv); | |
1587 | ||
9400cf6f | 1588 | while (bignum_cmp(b, Zero) != 0) { |
32874aea | 1589 | Bignum t = newbn(b[0]); |
5c72ca61 | 1590 | bigdivmod(a, b, t, NULL); |
32874aea | 1591 | while (t[0] > 1 && t[t[0]] == 0) |
1592 | t[0]--; | |
1593 | freebn(a); | |
1594 | a = b; | |
1595 | b = t; | |
9400cf6f | 1596 | } |
1597 | ||
1598 | freebn(b); | |
1599 | return a; | |
1600 | } | |
1601 | ||
1602 | /* | |
1603 | * Modular inverse, using Euclid's extended algorithm. | |
1604 | */ | |
32874aea | 1605 | Bignum modinv(Bignum number, Bignum modulus) |
1606 | { | |
9400cf6f | 1607 | Bignum a = copybn(modulus); |
1608 | Bignum b = copybn(number); | |
1609 | Bignum xp = copybn(Zero); | |
1610 | Bignum x = copybn(One); | |
1611 | int sign = +1; | |
1612 | ||
1613 | while (bignum_cmp(b, One) != 0) { | |
32874aea | 1614 | Bignum t = newbn(b[0]); |
1615 | Bignum q = newbn(a[0]); | |
5c72ca61 | 1616 | bigdivmod(a, b, t, q); |
32874aea | 1617 | while (t[0] > 1 && t[t[0]] == 0) |
1618 | t[0]--; | |
1619 | freebn(a); | |
1620 | a = b; | |
1621 | b = t; | |
1622 | t = xp; | |
1623 | xp = x; | |
1624 | x = bigmuladd(q, xp, t); | |
1625 | sign = -sign; | |
1626 | freebn(t); | |
75374b2f | 1627 | freebn(q); |
9400cf6f | 1628 | } |
1629 | ||
1630 | freebn(b); | |
1631 | freebn(a); | |
1632 | freebn(xp); | |
1633 | ||
1634 | /* now we know that sign * x == 1, and that x < modulus */ | |
1635 | if (sign < 0) { | |
32874aea | 1636 | /* set a new x to be modulus - x */ |
1637 | Bignum newx = newbn(modulus[0]); | |
a3412f52 | 1638 | BignumInt carry = 0; |
32874aea | 1639 | int maxspot = 1; |
1640 | int i; | |
1641 | ||
62ddb51e | 1642 | for (i = 1; i <= (int)newx[0]; i++) { |
1643 | BignumInt aword = (i <= (int)modulus[0] ? modulus[i] : 0); | |
1644 | BignumInt bword = (i <= (int)x[0] ? x[i] : 0); | |
32874aea | 1645 | newx[i] = aword - bword - carry; |
1646 | bword = ~bword; | |
1647 | carry = carry ? (newx[i] >= bword) : (newx[i] > bword); | |
1648 | if (newx[i] != 0) | |
1649 | maxspot = i; | |
1650 | } | |
1651 | newx[0] = maxspot; | |
1652 | freebn(x); | |
1653 | x = newx; | |
9400cf6f | 1654 | } |
1655 | ||
1656 | /* and return. */ | |
1657 | return x; | |
1658 | } | |
6e522441 | 1659 | |
1660 | /* | |
07cffb6a MW |
1661 | * Extract the largest power of 2 dividing x, storing it in p2, and returning |
1662 | * the product of the remaining factors. | |
1663 | */ | |
1664 | static Bignum extract_p2(Bignum x, unsigned *p2) | |
1665 | { | |
1666 | unsigned i, j, k, n; | |
1667 | Bignum y; | |
1668 | ||
1669 | /* If x is zero then the following won't work. And if x is odd then | |
1670 | * there's nothing very useful to do. | |
1671 | */ | |
1672 | if (!x[0] || (x[1] & 1)) { | |
1673 | *p2 = 0; | |
1674 | return copybn(x); | |
1675 | } | |
1676 | ||
1677 | /* Find the power of two. */ | |
1678 | for (i = 0; !x[i + 1]; i++); | |
1679 | for (j = 0; !((x[i + 1] >> j) & 1); j++); | |
1680 | *p2 = i*BIGNUM_INT_BITS + j; | |
1681 | ||
1682 | /* Work out how big the copy should be. */ | |
1683 | n = x[0] - i - 1; | |
1684 | if (x[x[0]] >> j) n++; | |
1685 | ||
1686 | /* Copy and shift down. */ | |
1687 | y = newbn(n); | |
1688 | for (k = 1; k <= n; k++) { | |
1689 | y[k] = x[k + i] >> j; | |
1690 | if (j && k < x[0]) y[k] |= x[k + i + 1] << (BIGNUM_INT_BITS - j); | |
1691 | } | |
1692 | ||
1693 | /* Done. */ | |
1694 | return y; | |
1695 | } | |
1696 | ||
1697 | /* | |
1698 | * Kronecker symbol (a|n). The result is always in { -1, 0, +1 }, and is | |
1699 | * zero if and only if a and n have a nontrivial common factor. Most | |
1700 | * usefully, if n is prime, this is the Legendre symbol, taking the value +1 | |
1701 | * if a is a quadratic residue mod n, and -1 otherwise; i.e., (a|p) == | |
1702 | * a^{(p-1)/2} (mod p). | |
1703 | */ | |
1704 | int kronecker(Bignum a, Bignum n) | |
1705 | { | |
1706 | unsigned s, nn; | |
1707 | int r = +1; | |
1708 | Bignum t; | |
1709 | ||
1710 | /* Special case for n = 0. This is the same convention PARI uses, | |
1711 | * except that we can't represent negative numbers. | |
1712 | */ | |
1713 | if (bignum_cmp(n, Zero) == 0) { | |
1714 | if (bignum_cmp(a, One) == 0) return +1; | |
1715 | else return 0; | |
1716 | } | |
1717 | ||
1718 | /* Write n = 2^s t, with t odd. If s > 0 and a is even, then the answer | |
1719 | * is zero; otherwise throw in a factor of (-1)^s if a == 3 or 5 (mod 8). | |
1720 | * | |
1721 | * At this point, we have a copy of n, and must remember to free it when | |
1722 | * we're done. It's convenient to take a copy of a at the same time. | |
1723 | */ | |
1724 | a = copybn(a); | |
1725 | n = extract_p2(n, &s); | |
1726 | ||
1727 | if (s && (!a[0] || !(a[1] & 1))) { r = 0; goto done; } | |
1728 | else if ((s & 1) && ((a[1] & 7) == 3 || (a[1] & 7) == 5)) r = -r; | |
1729 | ||
1730 | /* If n is (now) a unit then we're done. */ | |
1731 | if (bignum_cmp(n, One) == 0) goto done; | |
1732 | ||
1733 | /* Reduce a modulo n before we go any further. */ | |
1734 | if (bignum_cmp(a, n) >= 0) { t = bigmod(a, n); freebn(a); a = t; } | |
1735 | ||
1736 | /* Main loop. */ | |
1737 | for (;;) { | |
1738 | if (bignum_cmp(a, Zero) == 0) { r = 0; goto done; } | |
1739 | ||
1740 | /* Strip out and handle powers of two from a. */ | |
1741 | t = extract_p2(a, &s); freebn(a); a = t; | |
1742 | nn = n[1] & 7; | |
1743 | if ((s & 1) && (nn == 3 || nn == 5)) r = -r; | |
1744 | if (bignum_cmp(a, One) == 0) break; | |
1745 | ||
1746 | /* Swap, applying quadratic reciprocity. */ | |
1747 | if ((nn & 3) == 3 && (a[1] & 3) == 3) r = -r; | |
1748 | t = bigmod(n, a); freebn(n); n = a; a = t; | |
1749 | } | |
1750 | ||
1751 | /* Tidy up: we're done. */ | |
1752 | done: | |
1753 | freebn(a); freebn(n); | |
1754 | return r; | |
1755 | } | |
1756 | ||
1757 | /* | |
1758 | * Modular square root. We must have p prime: extracting square roots modulo | |
1759 | * composites is equivalent to factoring (but we don't check: you'll just get | |
1760 | * the wrong answer). Returns NULL if x is not a quadratic residue mod p. | |
1761 | */ | |
1762 | Bignum modsqrt(Bignum x, Bignum p) | |
1763 | { | |
1764 | Bignum xinv, b, c, r, t, z, X, mone; | |
1765 | unsigned i, j, s; | |
1766 | ||
1767 | /* If x is not a quadratic residue then we will not go to space today. */ | |
1768 | if (kronecker(x, p) != +1) return NULL; | |
1769 | ||
1770 | /* We need a quadratic nonresidue from somewhere. Exactly half of all | |
1771 | * units mod p are quadratic residues, but no efficient deterministic | |
1772 | * algorithm for finding one is known. So pick at random: we don't | |
1773 | * expect this to take long. | |
1774 | */ | |
1775 | z = newbn(p[0]); | |
1776 | do { | |
1777 | for (i = 1; i <= p[0]; i++) z[i] = rand(); | |
1778 | z[0] = p[0]; bn_restore_invariant(z); | |
1779 | } while (kronecker(z, p) != -1); | |
1780 | b = bigmod(z, p); freebn(z); | |
1781 | ||
1782 | /* We need to compute a few things before we really get started. */ | |
1783 | xinv = modinv(x, p); /* x^{-1} mod p */ | |
1784 | mone = bigsub(p, One); /* p - 1 == -1 (mod p) */ | |
1785 | t = extract_p2(mone, &s); /* 2^s t = p - 1 */ | |
1786 | c = modpow(b, t, p); /* b^t (mod p) */ | |
1787 | z = bigadd(t, One); freebn(t); t = z; /* (t + 1) */ | |
1788 | shift_right(t + 1, t[0], 1); if (!t[t[0]]) t[0]--; | |
1789 | r = modpow(x, t, p); /* x^{(t+1)/2} (mod p) */ | |
1790 | freebn(b); freebn(mone); freebn(t); | |
1791 | ||
1792 | /* OK, so how does this work anyway? | |
1793 | * | |
1794 | * We know that x^t is somewhere in the order-2^s subgroup of GF(p)^*; | |
1795 | * and g = c^{-1} is a generator for this subgroup (since we know that | |
1796 | * g^{2^{s-1}} = b^{(p-1)/2} = (b|p) = -1); so x^t = g^m for some m. In | |
1797 | * fact, we know that m is even because x is a square. Suppose we can | |
1798 | * determine m; then we know that x^t/g^m = 1, so x^{t+1}/c^m = x -- but | |
1799 | * both t + 1 and m are even, so x^{(t+1)/2}/g^{m/2} is a square root of | |
1800 | * x. | |
1801 | * | |
1802 | * Conveniently, finding the discrete log of an element X in a group of | |
1803 | * order 2^s is easy. Write X = g^m = g^{m_0+2k'}; then X^{2^{s-1}} = | |
1804 | * g^{m_0 2^{s-1}} c^{m' 2^s} = g^{m_0 2^{s-1}} is either -1 or +1, | |
1805 | * telling us that m_0 is 1 or 0 respectively. Then X/g^{m_0} = | |
1806 | * (g^2)^{m'} has order 2^{s-1} so we can continue inductively. What we | |
1807 | * end up with at the end is X/g^m. | |
1808 | * | |
1809 | * There are a few wrinkles. As we proceed through the induction, the | |
1810 | * generator for the subgroup will be c^{-2}, since we know that m is | |
1811 | * even. While we want the discrete log of X = x^t, we're actually going | |
1812 | * to keep track of r, which will eventually be x^{(t+1)/2}/g^{m/2} = | |
1813 | * x^{(t+1)/2} c^m, recovering X/g^m = r^2/x as we go. We don't actually | |
1814 | * form the discrete log explicitly, because the final result will | |
1815 | * actually be the square root we want. | |
1816 | */ | |
1817 | for (i = 1; i < s; i++) { | |
1818 | ||
1819 | /* Determine X. We could optimize this, only recomputing it when | |
1820 | * it's been invalidated, but that's fiddlier and this isn't | |
1821 | * performance critical. | |
1822 | */ | |
1823 | z = modmul(r, r, p); | |
1824 | X = modmul(z, xinv, p); | |
1825 | freebn(z); | |
1826 | ||
1827 | /* Determine X^{2^{s-1-i}}. */ | |
1828 | for (j = i + 1; j < s; j++) | |
1829 | z = modmul(X, X, p), freebn(X), X = z; | |
1830 | ||
1831 | /* Maybe accumulate a factor of c. */ | |
1832 | if (bignum_cmp(X, One) != 0) | |
1833 | z = modmul(r, c, p), freebn(r), r = z; | |
1834 | ||
1835 | /* Move on to the next smaller subgroup. */ | |
1836 | z = modmul(c, c, p), freebn(c), c = z; | |
1837 | freebn(X); | |
1838 | } | |
1839 | ||
1840 | /* Of course, there are two square roots of x. For predictability's sake | |
1841 | * we'll always return the one in [1..(p - 1)/2]. The other is, of | |
1842 | * course, p - r. | |
1843 | */ | |
1844 | z = bigsub(p, r); | |
1845 | if (bignum_cmp(r, z) < 0) | |
1846 | freebn(z); | |
1847 | else { | |
1848 | freebn(r); | |
1849 | r = z; | |
1850 | } | |
1851 | ||
1852 | /* We're done. */ | |
1853 | freebn(xinv); freebn(c); | |
1854 | return r; | |
1855 | } | |
1856 | ||
1857 | /* | |
6e522441 | 1858 | * Render a bignum into decimal. Return a malloced string holding |
1859 | * the decimal representation. | |
1860 | */ | |
32874aea | 1861 | char *bignum_decimal(Bignum x) |
1862 | { | |
6e522441 | 1863 | int ndigits, ndigit; |
1864 | int i, iszero; | |
a3412f52 | 1865 | BignumDblInt carry; |
6e522441 | 1866 | char *ret; |
a3412f52 | 1867 | BignumInt *workspace; |
6e522441 | 1868 | |
1869 | /* | |
1870 | * First, estimate the number of digits. Since log(10)/log(2) | |
1871 | * is just greater than 93/28 (the joys of continued fraction | |
1872 | * approximations...) we know that for every 93 bits, we need | |
1873 | * at most 28 digits. This will tell us how much to malloc. | |
1874 | * | |
1875 | * Formally: if x has i bits, that means x is strictly less | |
1876 | * than 2^i. Since 2 is less than 10^(28/93), this is less than | |
1877 | * 10^(28i/93). We need an integer power of ten, so we must | |
1878 | * round up (rounding down might make it less than x again). | |
1879 | * Therefore if we multiply the bit count by 28/93, rounding | |
1880 | * up, we will have enough digits. | |
74c79ce8 | 1881 | * |
1882 | * i=0 (i.e., x=0) is an irritating special case. | |
6e522441 | 1883 | */ |
ddecd643 | 1884 | i = bignum_bitcount(x); |
74c79ce8 | 1885 | if (!i) |
1886 | ndigits = 1; /* x = 0 */ | |
1887 | else | |
1888 | ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */ | |
32874aea | 1889 | ndigits++; /* allow for trailing \0 */ |
3d88e64d | 1890 | ret = snewn(ndigits, char); |
6e522441 | 1891 | |
1892 | /* | |
1893 | * Now allocate some workspace to hold the binary form as we | |
1894 | * repeatedly divide it by ten. Initialise this to the | |
1895 | * big-endian form of the number. | |
1896 | */ | |
a3412f52 | 1897 | workspace = snewn(x[0], BignumInt); |
62ddb51e | 1898 | for (i = 0; i < (int)x[0]; i++) |
32874aea | 1899 | workspace[i] = x[x[0] - i]; |
6e522441 | 1900 | |
1901 | /* | |
1902 | * Next, write the decimal number starting with the last digit. | |
1903 | * We use ordinary short division, dividing 10 into the | |
1904 | * workspace. | |
1905 | */ | |
32874aea | 1906 | ndigit = ndigits - 1; |
6e522441 | 1907 | ret[ndigit] = '\0'; |
1908 | do { | |
32874aea | 1909 | iszero = 1; |
1910 | carry = 0; | |
62ddb51e | 1911 | for (i = 0; i < (int)x[0]; i++) { |
a3412f52 | 1912 | carry = (carry << BIGNUM_INT_BITS) + workspace[i]; |
1913 | workspace[i] = (BignumInt) (carry / 10); | |
32874aea | 1914 | if (workspace[i]) |
1915 | iszero = 0; | |
1916 | carry %= 10; | |
1917 | } | |
1918 | ret[--ndigit] = (char) (carry + '0'); | |
6e522441 | 1919 | } while (!iszero); |
1920 | ||
1921 | /* | |
1922 | * There's a chance we've fallen short of the start of the | |
1923 | * string. Correct if so. | |
1924 | */ | |
1925 | if (ndigit > 0) | |
32874aea | 1926 | memmove(ret, ret + ndigit, ndigits - ndigit); |
6e522441 | 1927 | |
1928 | /* | |
1929 | * Done. | |
1930 | */ | |
c523f55f | 1931 | sfree(workspace); |
6e522441 | 1932 | return ret; |
1933 | } | |
f3c29e34 | 1934 | |
1935 | #ifdef TESTBN | |
1936 | ||
1937 | #include <stdio.h> | |
1938 | #include <stdlib.h> | |
1939 | #include <ctype.h> | |
1940 | ||
1941 | /* | |
4800a5e5 | 1942 | * gcc -Wall -g -O0 -DTESTBN -o testbn sshbn.c misc.c conf.c tree234.c unix/uxmisc.c -I. -I unix -I charset |
f84f1e46 | 1943 | * |
1944 | * Then feed to this program's standard input the output of | |
1945 | * testdata/bignum.py . | |
f3c29e34 | 1946 | */ |
1947 | ||
1948 | void modalfatalbox(char *p, ...) | |
1949 | { | |
1950 | va_list ap; | |
1951 | fprintf(stderr, "FATAL ERROR: "); | |
1952 | va_start(ap, p); | |
1953 | vfprintf(stderr, p, ap); | |
1954 | va_end(ap); | |
1955 | fputc('\n', stderr); | |
1956 | exit(1); | |
1957 | } | |
1958 | ||
1959 | #define fromxdigit(c) ( (c)>'9' ? ((c)&0xDF) - 'A' + 10 : (c) - '0' ) | |
1960 | ||
1961 | int main(int argc, char **argv) | |
1962 | { | |
1963 | char *buf; | |
1964 | int line = 0; | |
1965 | int passes = 0, fails = 0; | |
1966 | ||
1967 | while ((buf = fgetline(stdin)) != NULL) { | |
1968 | int maxlen = strlen(buf); | |
1969 | unsigned char *data = snewn(maxlen, unsigned char); | |
f84f1e46 | 1970 | unsigned char *ptrs[5], *q; |
f3c29e34 | 1971 | int ptrnum; |
1972 | char *bufp = buf; | |
1973 | ||
1974 | line++; | |
1975 | ||
1976 | q = data; | |
1977 | ptrnum = 0; | |
1978 | ||
f84f1e46 | 1979 | while (*bufp && !isspace((unsigned char)*bufp)) |
1980 | bufp++; | |
1981 | if (bufp) | |
1982 | *bufp++ = '\0'; | |
1983 | ||
f3c29e34 | 1984 | while (*bufp) { |
1985 | char *start, *end; | |
1986 | int i; | |
1987 | ||
1988 | while (*bufp && !isxdigit((unsigned char)*bufp)) | |
1989 | bufp++; | |
1990 | start = bufp; | |
1991 | ||
1992 | if (!*bufp) | |
1993 | break; | |
1994 | ||
1995 | while (*bufp && isxdigit((unsigned char)*bufp)) | |
1996 | bufp++; | |
1997 | end = bufp; | |
1998 | ||
1999 | if (ptrnum >= lenof(ptrs)) | |
2000 | break; | |
2001 | ptrs[ptrnum++] = q; | |
2002 | ||
2003 | for (i = -((end - start) & 1); i < end-start; i += 2) { | |
2004 | unsigned char val = (i < 0 ? 0 : fromxdigit(start[i])); | |
2005 | val = val * 16 + fromxdigit(start[i+1]); | |
2006 | *q++ = val; | |
2007 | } | |
2008 | ||
2009 | ptrs[ptrnum] = q; | |
2010 | } | |
2011 | ||
f84f1e46 | 2012 | if (!strcmp(buf, "mul")) { |
2013 | Bignum a, b, c, p; | |
2014 | ||
2015 | if (ptrnum != 3) { | |
f6939e2b | 2016 | printf("%d: mul with %d parameters, expected 3\n", line, ptrnum); |
f84f1e46 | 2017 | exit(1); |
2018 | } | |
2019 | a = bignum_from_bytes(ptrs[0], ptrs[1]-ptrs[0]); | |
2020 | b = bignum_from_bytes(ptrs[1], ptrs[2]-ptrs[1]); | |
2021 | c = bignum_from_bytes(ptrs[2], ptrs[3]-ptrs[2]); | |
2022 | p = bigmul(a, b); | |
f3c29e34 | 2023 | |
2024 | if (bignum_cmp(c, p) == 0) { | |
2025 | passes++; | |
2026 | } else { | |
2027 | char *as = bignum_decimal(a); | |
2028 | char *bs = bignum_decimal(b); | |
2029 | char *cs = bignum_decimal(c); | |
2030 | char *ps = bignum_decimal(p); | |
2031 | ||
2032 | printf("%d: fail: %s * %s gave %s expected %s\n", | |
2033 | line, as, bs, ps, cs); | |
2034 | fails++; | |
2035 | ||
2036 | sfree(as); | |
2037 | sfree(bs); | |
2038 | sfree(cs); | |
2039 | sfree(ps); | |
2040 | } | |
2041 | freebn(a); | |
2042 | freebn(b); | |
2043 | freebn(c); | |
2044 | freebn(p); | |
f84f1e46 | 2045 | } else if (!strcmp(buf, "pow")) { |
2046 | Bignum base, expt, modulus, expected, answer; | |
2047 | ||
2048 | if (ptrnum != 4) { | |
f6939e2b | 2049 | printf("%d: mul with %d parameters, expected 4\n", line, ptrnum); |
f84f1e46 | 2050 | exit(1); |
2051 | } | |
2052 | ||
2053 | base = bignum_from_bytes(ptrs[0], ptrs[1]-ptrs[0]); | |
2054 | expt = bignum_from_bytes(ptrs[1], ptrs[2]-ptrs[1]); | |
2055 | modulus = bignum_from_bytes(ptrs[2], ptrs[3]-ptrs[2]); | |
2056 | expected = bignum_from_bytes(ptrs[3], ptrs[4]-ptrs[3]); | |
2057 | answer = modpow(base, expt, modulus); | |
2058 | ||
2059 | if (bignum_cmp(expected, answer) == 0) { | |
2060 | passes++; | |
2061 | } else { | |
2062 | char *as = bignum_decimal(base); | |
2063 | char *bs = bignum_decimal(expt); | |
2064 | char *cs = bignum_decimal(modulus); | |
2065 | char *ds = bignum_decimal(answer); | |
2066 | char *ps = bignum_decimal(expected); | |
2067 | ||
2068 | printf("%d: fail: %s ^ %s mod %s gave %s expected %s\n", | |
2069 | line, as, bs, cs, ds, ps); | |
2070 | fails++; | |
2071 | ||
2072 | sfree(as); | |
2073 | sfree(bs); | |
2074 | sfree(cs); | |
2075 | sfree(ds); | |
2076 | sfree(ps); | |
2077 | } | |
2078 | freebn(base); | |
2079 | freebn(expt); | |
2080 | freebn(modulus); | |
2081 | freebn(expected); | |
2082 | freebn(answer); | |
07cffb6a MW |
2083 | } else if (!strcmp(buf, "modsqrt")) { |
2084 | Bignum x, p, expected, answer; | |
2085 | ||
2086 | if (ptrnum != 3) { | |
2087 | printf("%d: modsqrt with %d parameters, expected 3\n", line, ptrnum); | |
2088 | exit(1); | |
2089 | } | |
2090 | ||
2091 | x = bignum_from_bytes(ptrs[0], ptrs[1]-ptrs[0]); | |
2092 | p = bignum_from_bytes(ptrs[1], ptrs[2]-ptrs[1]); | |
2093 | expected = bignum_from_bytes(ptrs[2], ptrs[3]-ptrs[2]); | |
2094 | answer = modsqrt(x, p); | |
2095 | if (!answer) | |
2096 | answer = copybn(Zero); | |
2097 | ||
2098 | if (bignum_cmp(expected, answer) == 0) { | |
2099 | passes++; | |
2100 | } else { | |
2101 | char *xs = bignum_decimal(x); | |
2102 | char *ps = bignum_decimal(p); | |
2103 | char *qs = bignum_decimal(answer); | |
2104 | char *ws = bignum_decimal(expected); | |
2105 | ||
2106 | printf("%d: fail: sqrt(%s) mod %s gave %s expected %s\n", | |
2107 | line, xs, ps, qs, ws); | |
2108 | fails++; | |
2109 | ||
2110 | sfree(xs); | |
2111 | sfree(ps); | |
2112 | sfree(qs); | |
2113 | sfree(ws); | |
2114 | } | |
2115 | freebn(p); | |
2116 | freebn(x); | |
2117 | freebn(expected); | |
2118 | freebn(answer); | |
f84f1e46 | 2119 | } else { |
2120 | printf("%d: unrecognised test keyword: '%s'\n", line, buf); | |
2121 | exit(1); | |
f3c29e34 | 2122 | } |
f84f1e46 | 2123 | |
f3c29e34 | 2124 | sfree(buf); |
2125 | sfree(data); | |
2126 | } | |
2127 | ||
2128 | printf("passed %d failed %d total %d\n", passes, fails, passes+fails); | |
2129 | return fails != 0; | |
2130 | } | |
2131 | ||
2132 | #endif |