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