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 | /* |
163 | * Compute c = a * b. |
164 | * Input is in the first len words of a and b. |
165 | * Result is returned in the first 2*len words of c. |
166 | */ |
a3412f52 |
167 | static void internal_mul(BignumInt *a, BignumInt *b, |
168 | BignumInt *c, int len) |
e5574168 |
169 | { |
170 | int i, j; |
a3412f52 |
171 | BignumDblInt t; |
e5574168 |
172 | |
32874aea |
173 | for (j = 0; j < 2 * len; j++) |
9400cf6f |
174 | c[j] = 0; |
e5574168 |
175 | |
176 | for (i = len - 1; i >= 0; i--) { |
e5574168 |
177 | t = 0; |
178 | for (j = len - 1; j >= 0; j--) { |
a3412f52 |
179 | t += MUL_WORD(a[i], (BignumDblInt) b[j]); |
180 | t += (BignumDblInt) c[i + j + 1]; |
181 | c[i + j + 1] = (BignumInt) t; |
182 | t = t >> BIGNUM_INT_BITS; |
e5574168 |
183 | } |
a3412f52 |
184 | c[i] = (BignumInt) t; |
e5574168 |
185 | } |
186 | } |
187 | |
a3412f52 |
188 | static void internal_add_shifted(BignumInt *number, |
32874aea |
189 | unsigned n, int shift) |
190 | { |
a3412f52 |
191 | int word = 1 + (shift / BIGNUM_INT_BITS); |
192 | int bshift = shift % BIGNUM_INT_BITS; |
193 | BignumDblInt addend; |
9400cf6f |
194 | |
3014da2b |
195 | addend = (BignumDblInt)n << bshift; |
9400cf6f |
196 | |
197 | while (addend) { |
32874aea |
198 | addend += number[word]; |
a3412f52 |
199 | number[word] = (BignumInt) addend & BIGNUM_INT_MASK; |
200 | addend >>= BIGNUM_INT_BITS; |
32874aea |
201 | word++; |
9400cf6f |
202 | } |
203 | } |
204 | |
e5574168 |
205 | /* |
206 | * Compute a = a % m. |
9400cf6f |
207 | * Input in first alen words of a and first mlen words of m. |
208 | * Output in first alen words of a |
209 | * (of which first alen-mlen words will be zero). |
e5574168 |
210 | * The MSW of m MUST have its high bit set. |
9400cf6f |
211 | * Quotient is accumulated in the `quotient' array, which is a Bignum |
212 | * rather than the internal bigendian format. Quotient parts are shifted |
213 | * left by `qshift' before adding into quot. |
e5574168 |
214 | */ |
a3412f52 |
215 | static void internal_mod(BignumInt *a, int alen, |
216 | BignumInt *m, int mlen, |
217 | BignumInt *quot, int qshift) |
e5574168 |
218 | { |
a3412f52 |
219 | BignumInt m0, m1; |
e5574168 |
220 | unsigned int h; |
221 | int i, k; |
222 | |
e5574168 |
223 | m0 = m[0]; |
9400cf6f |
224 | if (mlen > 1) |
32874aea |
225 | m1 = m[1]; |
9400cf6f |
226 | else |
32874aea |
227 | m1 = 0; |
e5574168 |
228 | |
32874aea |
229 | for (i = 0; i <= alen - mlen; i++) { |
a3412f52 |
230 | BignumDblInt t; |
9400cf6f |
231 | unsigned int q, r, c, ai1; |
e5574168 |
232 | |
233 | if (i == 0) { |
234 | h = 0; |
235 | } else { |
32874aea |
236 | h = a[i - 1]; |
237 | a[i - 1] = 0; |
e5574168 |
238 | } |
239 | |
32874aea |
240 | if (i == alen - 1) |
241 | ai1 = 0; |
242 | else |
243 | ai1 = a[i + 1]; |
9400cf6f |
244 | |
e5574168 |
245 | /* Find q = h:a[i] / m0 */ |
62ef3d44 |
246 | if (h >= m0) { |
247 | /* |
248 | * Special case. |
249 | * |
250 | * To illustrate it, suppose a BignumInt is 8 bits, and |
251 | * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then |
252 | * our initial division will be 0xA123 / 0xA1, which |
253 | * will give a quotient of 0x100 and a divide overflow. |
254 | * However, the invariants in this division algorithm |
255 | * are not violated, since the full number A1:23:... is |
256 | * _less_ than the quotient prefix A1:B2:... and so the |
257 | * following correction loop would have sorted it out. |
258 | * |
259 | * In this situation we set q to be the largest |
260 | * quotient we _can_ stomach (0xFF, of course). |
261 | */ |
262 | q = BIGNUM_INT_MASK; |
263 | } else { |
819a22b3 |
264 | /* Macro doesn't want an array subscript expression passed |
265 | * into it (see definition), so use a temporary. */ |
266 | BignumInt tmplo = a[i]; |
267 | DIVMOD_WORD(q, r, h, tmplo, m0); |
62ef3d44 |
268 | |
269 | /* Refine our estimate of q by looking at |
270 | h:a[i]:a[i+1] / m0:m1 */ |
271 | t = MUL_WORD(m1, q); |
272 | if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) { |
273 | q--; |
274 | t -= m1; |
275 | r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */ |
276 | if (r >= (BignumDblInt) m0 && |
277 | t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--; |
278 | } |
e5574168 |
279 | } |
280 | |
9400cf6f |
281 | /* Subtract q * m from a[i...] */ |
e5574168 |
282 | c = 0; |
9400cf6f |
283 | for (k = mlen - 1; k >= 0; k--) { |
a47e8bba |
284 | t = MUL_WORD(q, m[k]); |
e5574168 |
285 | t += c; |
62ddb51e |
286 | c = (unsigned)(t >> BIGNUM_INT_BITS); |
a3412f52 |
287 | if ((BignumInt) t > a[i + k]) |
32874aea |
288 | c++; |
a3412f52 |
289 | a[i + k] -= (BignumInt) t; |
e5574168 |
290 | } |
291 | |
292 | /* Add back m in case of borrow */ |
293 | if (c != h) { |
294 | t = 0; |
9400cf6f |
295 | for (k = mlen - 1; k >= 0; k--) { |
e5574168 |
296 | t += m[k]; |
32874aea |
297 | t += a[i + k]; |
a3412f52 |
298 | a[i + k] = (BignumInt) t; |
299 | t = t >> BIGNUM_INT_BITS; |
e5574168 |
300 | } |
32874aea |
301 | q--; |
e5574168 |
302 | } |
32874aea |
303 | if (quot) |
a3412f52 |
304 | internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i)); |
e5574168 |
305 | } |
306 | } |
307 | |
308 | /* |
309 | * Compute (base ^ exp) % mod. |
e5574168 |
310 | */ |
ed953b91 |
311 | Bignum modpow(Bignum base_in, Bignum exp, Bignum mod) |
e5574168 |
312 | { |
a3412f52 |
313 | BignumInt *a, *b, *n, *m; |
e5574168 |
314 | int mshift; |
315 | int mlen, i, j; |
ed953b91 |
316 | Bignum base, result; |
317 | |
318 | /* |
319 | * The most significant word of mod needs to be non-zero. It |
320 | * should already be, but let's make sure. |
321 | */ |
322 | assert(mod[mod[0]] != 0); |
323 | |
324 | /* |
325 | * Make sure the base is smaller than the modulus, by reducing |
326 | * it modulo the modulus if not. |
327 | */ |
328 | base = bigmod(base_in, mod); |
e5574168 |
329 | |
330 | /* Allocate m of size mlen, copy mod to m */ |
331 | /* We use big endian internally */ |
332 | mlen = mod[0]; |
a3412f52 |
333 | m = snewn(mlen, BignumInt); |
32874aea |
334 | for (j = 0; j < mlen; j++) |
335 | m[j] = mod[mod[0] - j]; |
e5574168 |
336 | |
337 | /* Shift m left to make msb bit set */ |
a3412f52 |
338 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
339 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
340 | break; |
e5574168 |
341 | if (mshift) { |
342 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
343 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
344 | m[mlen - 1] = m[mlen - 1] << mshift; |
e5574168 |
345 | } |
346 | |
347 | /* Allocate n of size mlen, copy base to n */ |
a3412f52 |
348 | n = snewn(mlen, BignumInt); |
e5574168 |
349 | i = mlen - base[0]; |
32874aea |
350 | for (j = 0; j < i; j++) |
351 | n[j] = 0; |
62ddb51e |
352 | for (j = 0; j < (int)base[0]; j++) |
32874aea |
353 | n[i + j] = base[base[0] - j]; |
e5574168 |
354 | |
355 | /* Allocate a and b of size 2*mlen. Set a = 1 */ |
a3412f52 |
356 | a = snewn(2 * mlen, BignumInt); |
357 | b = snewn(2 * mlen, BignumInt); |
32874aea |
358 | for (i = 0; i < 2 * mlen; i++) |
359 | a[i] = 0; |
360 | a[2 * mlen - 1] = 1; |
e5574168 |
361 | |
362 | /* Skip leading zero bits of exp. */ |
32874aea |
363 | i = 0; |
a3412f52 |
364 | j = BIGNUM_INT_BITS-1; |
62ddb51e |
365 | while (i < (int)exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) { |
e5574168 |
366 | j--; |
32874aea |
367 | if (j < 0) { |
368 | i++; |
a3412f52 |
369 | j = BIGNUM_INT_BITS-1; |
32874aea |
370 | } |
e5574168 |
371 | } |
372 | |
373 | /* Main computation */ |
62ddb51e |
374 | while (i < (int)exp[0]) { |
e5574168 |
375 | while (j >= 0) { |
9400cf6f |
376 | internal_mul(a + mlen, a + mlen, b, mlen); |
32874aea |
377 | internal_mod(b, mlen * 2, m, mlen, NULL, 0); |
e5574168 |
378 | if ((exp[exp[0] - i] & (1 << j)) != 0) { |
9400cf6f |
379 | internal_mul(b + mlen, n, a, mlen); |
32874aea |
380 | internal_mod(a, mlen * 2, m, mlen, NULL, 0); |
e5574168 |
381 | } else { |
a3412f52 |
382 | BignumInt *t; |
32874aea |
383 | t = a; |
384 | a = b; |
385 | b = t; |
e5574168 |
386 | } |
387 | j--; |
388 | } |
32874aea |
389 | i++; |
a3412f52 |
390 | j = BIGNUM_INT_BITS-1; |
e5574168 |
391 | } |
392 | |
393 | /* Fixup result in case the modulus was shifted */ |
394 | if (mshift) { |
32874aea |
395 | for (i = mlen - 1; i < 2 * mlen - 1; i++) |
a3412f52 |
396 | a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
397 | a[2 * mlen - 1] = a[2 * mlen - 1] << mshift; |
398 | internal_mod(a, mlen * 2, m, mlen, NULL, 0); |
399 | for (i = 2 * mlen - 1; i >= mlen; i--) |
a3412f52 |
400 | a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift)); |
e5574168 |
401 | } |
402 | |
403 | /* Copy result to buffer */ |
59600f67 |
404 | result = newbn(mod[0]); |
e5574168 |
405 | for (i = 0; i < mlen; i++) |
32874aea |
406 | result[result[0] - i] = a[i + mlen]; |
407 | while (result[0] > 1 && result[result[0]] == 0) |
408 | result[0]--; |
e5574168 |
409 | |
410 | /* Free temporary arrays */ |
32874aea |
411 | for (i = 0; i < 2 * mlen; i++) |
412 | a[i] = 0; |
413 | sfree(a); |
414 | for (i = 0; i < 2 * mlen; i++) |
415 | b[i] = 0; |
416 | sfree(b); |
417 | for (i = 0; i < mlen; i++) |
418 | m[i] = 0; |
419 | sfree(m); |
420 | for (i = 0; i < mlen; i++) |
421 | n[i] = 0; |
422 | sfree(n); |
59600f67 |
423 | |
ed953b91 |
424 | freebn(base); |
425 | |
59600f67 |
426 | return result; |
e5574168 |
427 | } |
7cca0d81 |
428 | |
429 | /* |
430 | * Compute (p * q) % mod. |
431 | * The most significant word of mod MUST be non-zero. |
432 | * We assume that the result array is the same size as the mod array. |
433 | */ |
59600f67 |
434 | Bignum modmul(Bignum p, Bignum q, Bignum mod) |
7cca0d81 |
435 | { |
a3412f52 |
436 | BignumInt *a, *n, *m, *o; |
7cca0d81 |
437 | int mshift; |
80b10571 |
438 | int pqlen, mlen, rlen, i, j; |
59600f67 |
439 | Bignum result; |
7cca0d81 |
440 | |
441 | /* Allocate m of size mlen, copy mod to m */ |
442 | /* We use big endian internally */ |
443 | mlen = mod[0]; |
a3412f52 |
444 | m = snewn(mlen, BignumInt); |
32874aea |
445 | for (j = 0; j < mlen; j++) |
446 | m[j] = mod[mod[0] - j]; |
7cca0d81 |
447 | |
448 | /* Shift m left to make msb bit set */ |
a3412f52 |
449 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
450 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
451 | break; |
7cca0d81 |
452 | if (mshift) { |
453 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
454 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
455 | m[mlen - 1] = m[mlen - 1] << mshift; |
7cca0d81 |
456 | } |
457 | |
458 | pqlen = (p[0] > q[0] ? p[0] : q[0]); |
459 | |
460 | /* Allocate n of size pqlen, copy p to n */ |
a3412f52 |
461 | n = snewn(pqlen, BignumInt); |
7cca0d81 |
462 | i = pqlen - p[0]; |
32874aea |
463 | for (j = 0; j < i; j++) |
464 | n[j] = 0; |
62ddb51e |
465 | for (j = 0; j < (int)p[0]; j++) |
32874aea |
466 | n[i + j] = p[p[0] - j]; |
7cca0d81 |
467 | |
468 | /* Allocate o of size pqlen, copy q to o */ |
a3412f52 |
469 | o = snewn(pqlen, BignumInt); |
7cca0d81 |
470 | i = pqlen - q[0]; |
32874aea |
471 | for (j = 0; j < i; j++) |
472 | o[j] = 0; |
62ddb51e |
473 | for (j = 0; j < (int)q[0]; j++) |
32874aea |
474 | o[i + j] = q[q[0] - j]; |
7cca0d81 |
475 | |
476 | /* Allocate a of size 2*pqlen for result */ |
a3412f52 |
477 | a = snewn(2 * pqlen, BignumInt); |
7cca0d81 |
478 | |
479 | /* Main computation */ |
9400cf6f |
480 | internal_mul(n, o, a, pqlen); |
32874aea |
481 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
7cca0d81 |
482 | |
483 | /* Fixup result in case the modulus was shifted */ |
484 | if (mshift) { |
32874aea |
485 | for (i = 2 * pqlen - mlen - 1; i < 2 * pqlen - 1; i++) |
a3412f52 |
486 | a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
487 | a[2 * pqlen - 1] = a[2 * pqlen - 1] << mshift; |
488 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
489 | for (i = 2 * pqlen - 1; i >= 2 * pqlen - mlen; i--) |
a3412f52 |
490 | a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift)); |
7cca0d81 |
491 | } |
492 | |
493 | /* Copy result to buffer */ |
32874aea |
494 | rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2); |
80b10571 |
495 | result = newbn(rlen); |
496 | for (i = 0; i < rlen; i++) |
32874aea |
497 | result[result[0] - i] = a[i + 2 * pqlen - rlen]; |
498 | while (result[0] > 1 && result[result[0]] == 0) |
499 | result[0]--; |
7cca0d81 |
500 | |
501 | /* Free temporary arrays */ |
32874aea |
502 | for (i = 0; i < 2 * pqlen; i++) |
503 | a[i] = 0; |
504 | sfree(a); |
505 | for (i = 0; i < mlen; i++) |
506 | m[i] = 0; |
507 | sfree(m); |
508 | for (i = 0; i < pqlen; i++) |
509 | n[i] = 0; |
510 | sfree(n); |
511 | for (i = 0; i < pqlen; i++) |
512 | o[i] = 0; |
513 | sfree(o); |
59600f67 |
514 | |
515 | return result; |
7cca0d81 |
516 | } |
517 | |
518 | /* |
9400cf6f |
519 | * Compute p % mod. |
520 | * The most significant word of mod MUST be non-zero. |
521 | * We assume that the result array is the same size as the mod array. |
5c72ca61 |
522 | * We optionally write out a quotient if `quotient' is non-NULL. |
523 | * We can avoid writing out the result if `result' is NULL. |
9400cf6f |
524 | */ |
f28753ab |
525 | static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient) |
9400cf6f |
526 | { |
a3412f52 |
527 | BignumInt *n, *m; |
9400cf6f |
528 | int mshift; |
529 | int plen, mlen, i, j; |
530 | |
531 | /* Allocate m of size mlen, copy mod to m */ |
532 | /* We use big endian internally */ |
533 | mlen = mod[0]; |
a3412f52 |
534 | m = snewn(mlen, BignumInt); |
32874aea |
535 | for (j = 0; j < mlen; j++) |
536 | m[j] = mod[mod[0] - j]; |
9400cf6f |
537 | |
538 | /* Shift m left to make msb bit set */ |
a3412f52 |
539 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
540 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
541 | break; |
9400cf6f |
542 | if (mshift) { |
543 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
544 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
545 | m[mlen - 1] = m[mlen - 1] << mshift; |
9400cf6f |
546 | } |
547 | |
548 | plen = p[0]; |
549 | /* Ensure plen > mlen */ |
32874aea |
550 | if (plen <= mlen) |
551 | plen = mlen + 1; |
9400cf6f |
552 | |
553 | /* Allocate n of size plen, copy p to n */ |
a3412f52 |
554 | n = snewn(plen, BignumInt); |
32874aea |
555 | for (j = 0; j < plen; j++) |
556 | n[j] = 0; |
62ddb51e |
557 | for (j = 1; j <= (int)p[0]; j++) |
32874aea |
558 | n[plen - j] = p[j]; |
9400cf6f |
559 | |
560 | /* Main computation */ |
561 | internal_mod(n, plen, m, mlen, quotient, mshift); |
562 | |
563 | /* Fixup result in case the modulus was shifted */ |
564 | if (mshift) { |
565 | for (i = plen - mlen - 1; i < plen - 1; i++) |
a3412f52 |
566 | n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
567 | n[plen - 1] = n[plen - 1] << mshift; |
9400cf6f |
568 | internal_mod(n, plen, m, mlen, quotient, 0); |
569 | for (i = plen - 1; i >= plen - mlen; i--) |
a3412f52 |
570 | n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift)); |
9400cf6f |
571 | } |
572 | |
573 | /* Copy result to buffer */ |
5c72ca61 |
574 | if (result) { |
62ddb51e |
575 | for (i = 1; i <= (int)result[0]; i++) { |
5c72ca61 |
576 | int j = plen - i; |
577 | result[i] = j >= 0 ? n[j] : 0; |
578 | } |
9400cf6f |
579 | } |
580 | |
581 | /* Free temporary arrays */ |
32874aea |
582 | for (i = 0; i < mlen; i++) |
583 | m[i] = 0; |
584 | sfree(m); |
585 | for (i = 0; i < plen; i++) |
586 | n[i] = 0; |
587 | sfree(n); |
9400cf6f |
588 | } |
589 | |
590 | /* |
7cca0d81 |
591 | * Decrement a number. |
592 | */ |
32874aea |
593 | void decbn(Bignum bn) |
594 | { |
7cca0d81 |
595 | int i = 1; |
62ddb51e |
596 | while (i < (int)bn[0] && bn[i] == 0) |
a3412f52 |
597 | bn[i++] = BIGNUM_INT_MASK; |
7cca0d81 |
598 | bn[i]--; |
599 | } |
600 | |
27cd7fc2 |
601 | Bignum bignum_from_bytes(const unsigned char *data, int nbytes) |
32874aea |
602 | { |
3709bfe9 |
603 | Bignum result; |
604 | int w, i; |
605 | |
a3412f52 |
606 | w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */ |
3709bfe9 |
607 | |
608 | result = newbn(w); |
32874aea |
609 | for (i = 1; i <= w; i++) |
610 | result[i] = 0; |
611 | for (i = nbytes; i--;) { |
612 | unsigned char byte = *data++; |
a3412f52 |
613 | result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS); |
3709bfe9 |
614 | } |
615 | |
32874aea |
616 | while (result[0] > 1 && result[result[0]] == 0) |
617 | result[0]--; |
3709bfe9 |
618 | return result; |
619 | } |
620 | |
7cca0d81 |
621 | /* |
2e85c969 |
622 | * Read an SSH-1-format bignum from a data buffer. Return the number |
0016d70b |
623 | * of bytes consumed, or -1 if there wasn't enough data. |
7cca0d81 |
624 | */ |
0016d70b |
625 | int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result) |
32874aea |
626 | { |
27cd7fc2 |
627 | const unsigned char *p = data; |
7cca0d81 |
628 | int i; |
629 | int w, b; |
630 | |
0016d70b |
631 | if (len < 2) |
632 | return -1; |
633 | |
7cca0d81 |
634 | w = 0; |
32874aea |
635 | for (i = 0; i < 2; i++) |
636 | w = (w << 8) + *p++; |
637 | b = (w + 7) / 8; /* bits -> bytes */ |
7cca0d81 |
638 | |
0016d70b |
639 | if (len < b+2) |
640 | return -1; |
641 | |
32874aea |
642 | if (!result) /* just return length */ |
643 | return b + 2; |
a52f067e |
644 | |
3709bfe9 |
645 | *result = bignum_from_bytes(p, b); |
7cca0d81 |
646 | |
3709bfe9 |
647 | return p + b - data; |
7cca0d81 |
648 | } |
5c58ad2d |
649 | |
650 | /* |
2e85c969 |
651 | * Return the bit count of a bignum, for SSH-1 encoding. |
5c58ad2d |
652 | */ |
32874aea |
653 | int bignum_bitcount(Bignum bn) |
654 | { |
a3412f52 |
655 | int bitcount = bn[0] * BIGNUM_INT_BITS - 1; |
32874aea |
656 | while (bitcount >= 0 |
a3412f52 |
657 | && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--; |
5c58ad2d |
658 | return bitcount + 1; |
659 | } |
660 | |
661 | /* |
2e85c969 |
662 | * Return the byte length of a bignum when SSH-1 encoded. |
5c58ad2d |
663 | */ |
32874aea |
664 | int ssh1_bignum_length(Bignum bn) |
665 | { |
666 | return 2 + (bignum_bitcount(bn) + 7) / 8; |
ddecd643 |
667 | } |
668 | |
669 | /* |
2e85c969 |
670 | * Return the byte length of a bignum when SSH-2 encoded. |
ddecd643 |
671 | */ |
32874aea |
672 | int ssh2_bignum_length(Bignum bn) |
673 | { |
674 | return 4 + (bignum_bitcount(bn) + 8) / 8; |
5c58ad2d |
675 | } |
676 | |
677 | /* |
678 | * Return a byte from a bignum; 0 is least significant, etc. |
679 | */ |
32874aea |
680 | int bignum_byte(Bignum bn, int i) |
681 | { |
62ddb51e |
682 | if (i >= (int)(BIGNUM_INT_BYTES * bn[0])) |
32874aea |
683 | return 0; /* beyond the end */ |
5c58ad2d |
684 | else |
a3412f52 |
685 | return (bn[i / BIGNUM_INT_BYTES + 1] >> |
686 | ((i % BIGNUM_INT_BYTES)*8)) & 0xFF; |
5c58ad2d |
687 | } |
688 | |
689 | /* |
9400cf6f |
690 | * Return a bit from a bignum; 0 is least significant, etc. |
691 | */ |
32874aea |
692 | int bignum_bit(Bignum bn, int i) |
693 | { |
62ddb51e |
694 | if (i >= (int)(BIGNUM_INT_BITS * bn[0])) |
32874aea |
695 | return 0; /* beyond the end */ |
9400cf6f |
696 | else |
a3412f52 |
697 | return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1; |
9400cf6f |
698 | } |
699 | |
700 | /* |
701 | * Set a bit in a bignum; 0 is least significant, etc. |
702 | */ |
32874aea |
703 | void bignum_set_bit(Bignum bn, int bitnum, int value) |
704 | { |
62ddb51e |
705 | if (bitnum >= (int)(BIGNUM_INT_BITS * bn[0])) |
32874aea |
706 | abort(); /* beyond the end */ |
9400cf6f |
707 | else { |
a3412f52 |
708 | int v = bitnum / BIGNUM_INT_BITS + 1; |
709 | int mask = 1 << (bitnum % BIGNUM_INT_BITS); |
32874aea |
710 | if (value) |
711 | bn[v] |= mask; |
712 | else |
713 | bn[v] &= ~mask; |
9400cf6f |
714 | } |
715 | } |
716 | |
717 | /* |
2e85c969 |
718 | * Write a SSH-1-format bignum into a buffer. It is assumed the |
5c58ad2d |
719 | * buffer is big enough. Returns the number of bytes used. |
720 | */ |
32874aea |
721 | int ssh1_write_bignum(void *data, Bignum bn) |
722 | { |
5c58ad2d |
723 | unsigned char *p = data; |
724 | int len = ssh1_bignum_length(bn); |
725 | int i; |
ddecd643 |
726 | int bitc = bignum_bitcount(bn); |
5c58ad2d |
727 | |
728 | *p++ = (bitc >> 8) & 0xFF; |
32874aea |
729 | *p++ = (bitc) & 0xFF; |
730 | for (i = len - 2; i--;) |
731 | *p++ = bignum_byte(bn, i); |
5c58ad2d |
732 | return len; |
733 | } |
9400cf6f |
734 | |
735 | /* |
736 | * Compare two bignums. Returns like strcmp. |
737 | */ |
32874aea |
738 | int bignum_cmp(Bignum a, Bignum b) |
739 | { |
9400cf6f |
740 | int amax = a[0], bmax = b[0]; |
741 | int i = (amax > bmax ? amax : bmax); |
742 | while (i) { |
a3412f52 |
743 | BignumInt aval = (i > amax ? 0 : a[i]); |
744 | BignumInt bval = (i > bmax ? 0 : b[i]); |
32874aea |
745 | if (aval < bval) |
746 | return -1; |
747 | if (aval > bval) |
748 | return +1; |
749 | i--; |
9400cf6f |
750 | } |
751 | return 0; |
752 | } |
753 | |
754 | /* |
755 | * Right-shift one bignum to form another. |
756 | */ |
32874aea |
757 | Bignum bignum_rshift(Bignum a, int shift) |
758 | { |
9400cf6f |
759 | Bignum ret; |
760 | int i, shiftw, shiftb, shiftbb, bits; |
a3412f52 |
761 | BignumInt ai, ai1; |
9400cf6f |
762 | |
ddecd643 |
763 | bits = bignum_bitcount(a) - shift; |
a3412f52 |
764 | ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS); |
9400cf6f |
765 | |
766 | if (ret) { |
a3412f52 |
767 | shiftw = shift / BIGNUM_INT_BITS; |
768 | shiftb = shift % BIGNUM_INT_BITS; |
769 | shiftbb = BIGNUM_INT_BITS - shiftb; |
32874aea |
770 | |
771 | ai1 = a[shiftw + 1]; |
62ddb51e |
772 | for (i = 1; i <= (int)ret[0]; i++) { |
32874aea |
773 | ai = ai1; |
62ddb51e |
774 | ai1 = (i + shiftw + 1 <= (int)a[0] ? a[i + shiftw + 1] : 0); |
a3412f52 |
775 | ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK; |
32874aea |
776 | } |
9400cf6f |
777 | } |
778 | |
779 | return ret; |
780 | } |
781 | |
782 | /* |
783 | * Non-modular multiplication and addition. |
784 | */ |
32874aea |
785 | Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) |
786 | { |
9400cf6f |
787 | int alen = a[0], blen = b[0]; |
788 | int mlen = (alen > blen ? alen : blen); |
789 | int rlen, i, maxspot; |
a3412f52 |
790 | BignumInt *workspace; |
9400cf6f |
791 | Bignum ret; |
792 | |
793 | /* mlen space for a, mlen space for b, 2*mlen for result */ |
a3412f52 |
794 | workspace = snewn(mlen * 4, BignumInt); |
9400cf6f |
795 | for (i = 0; i < mlen; i++) { |
62ddb51e |
796 | workspace[0 * mlen + i] = (mlen - i <= (int)a[0] ? a[mlen - i] : 0); |
797 | workspace[1 * mlen + i] = (mlen - i <= (int)b[0] ? b[mlen - i] : 0); |
9400cf6f |
798 | } |
799 | |
32874aea |
800 | internal_mul(workspace + 0 * mlen, workspace + 1 * mlen, |
801 | workspace + 2 * mlen, mlen); |
9400cf6f |
802 | |
803 | /* now just copy the result back */ |
804 | rlen = alen + blen + 1; |
62ddb51e |
805 | if (addend && rlen <= (int)addend[0]) |
32874aea |
806 | rlen = addend[0] + 1; |
9400cf6f |
807 | ret = newbn(rlen); |
808 | maxspot = 0; |
62ddb51e |
809 | for (i = 1; i <= (int)ret[0]; i++) { |
32874aea |
810 | ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0); |
811 | if (ret[i] != 0) |
812 | maxspot = i; |
9400cf6f |
813 | } |
814 | ret[0] = maxspot; |
815 | |
816 | /* now add in the addend, if any */ |
817 | if (addend) { |
a3412f52 |
818 | BignumDblInt carry = 0; |
32874aea |
819 | for (i = 1; i <= rlen; i++) { |
62ddb51e |
820 | carry += (i <= (int)ret[0] ? ret[i] : 0); |
821 | carry += (i <= (int)addend[0] ? addend[i] : 0); |
a3412f52 |
822 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
823 | carry >>= BIGNUM_INT_BITS; |
32874aea |
824 | if (ret[i] != 0 && i > maxspot) |
825 | maxspot = i; |
826 | } |
9400cf6f |
827 | } |
828 | ret[0] = maxspot; |
829 | |
c523f55f |
830 | sfree(workspace); |
9400cf6f |
831 | return ret; |
832 | } |
833 | |
834 | /* |
835 | * Non-modular multiplication. |
836 | */ |
32874aea |
837 | Bignum bigmul(Bignum a, Bignum b) |
838 | { |
9400cf6f |
839 | return bigmuladd(a, b, NULL); |
840 | } |
841 | |
842 | /* |
3709bfe9 |
843 | * Create a bignum which is the bitmask covering another one. That |
844 | * is, the smallest integer which is >= N and is also one less than |
845 | * a power of two. |
846 | */ |
32874aea |
847 | Bignum bignum_bitmask(Bignum n) |
848 | { |
3709bfe9 |
849 | Bignum ret = copybn(n); |
850 | int i; |
a3412f52 |
851 | BignumInt j; |
3709bfe9 |
852 | |
853 | i = ret[0]; |
854 | while (n[i] == 0 && i > 0) |
32874aea |
855 | i--; |
3709bfe9 |
856 | if (i <= 0) |
32874aea |
857 | return ret; /* input was zero */ |
3709bfe9 |
858 | j = 1; |
859 | while (j < n[i]) |
32874aea |
860 | j = 2 * j + 1; |
3709bfe9 |
861 | ret[i] = j; |
862 | while (--i > 0) |
a3412f52 |
863 | ret[i] = BIGNUM_INT_MASK; |
3709bfe9 |
864 | return ret; |
865 | } |
866 | |
867 | /* |
5c72ca61 |
868 | * Convert a (max 32-bit) long into a bignum. |
9400cf6f |
869 | */ |
a3412f52 |
870 | Bignum bignum_from_long(unsigned long nn) |
32874aea |
871 | { |
9400cf6f |
872 | Bignum ret; |
a3412f52 |
873 | BignumDblInt n = nn; |
9400cf6f |
874 | |
5c72ca61 |
875 | ret = newbn(3); |
a3412f52 |
876 | ret[1] = (BignumInt)(n & BIGNUM_INT_MASK); |
877 | ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK); |
5c72ca61 |
878 | ret[3] = 0; |
879 | ret[0] = (ret[2] ? 2 : 1); |
32874aea |
880 | return ret; |
9400cf6f |
881 | } |
882 | |
883 | /* |
884 | * Add a long to a bignum. |
885 | */ |
a3412f52 |
886 | Bignum bignum_add_long(Bignum number, unsigned long addendx) |
32874aea |
887 | { |
888 | Bignum ret = newbn(number[0] + 1); |
9400cf6f |
889 | int i, maxspot = 0; |
a3412f52 |
890 | BignumDblInt carry = 0, addend = addendx; |
9400cf6f |
891 | |
62ddb51e |
892 | for (i = 1; i <= (int)ret[0]; i++) { |
a3412f52 |
893 | carry += addend & BIGNUM_INT_MASK; |
62ddb51e |
894 | carry += (i <= (int)number[0] ? number[i] : 0); |
a3412f52 |
895 | addend >>= BIGNUM_INT_BITS; |
896 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
897 | carry >>= BIGNUM_INT_BITS; |
32874aea |
898 | if (ret[i] != 0) |
899 | maxspot = i; |
9400cf6f |
900 | } |
901 | ret[0] = maxspot; |
902 | return ret; |
903 | } |
904 | |
905 | /* |
906 | * Compute the residue of a bignum, modulo a (max 16-bit) short. |
907 | */ |
32874aea |
908 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus) |
909 | { |
a3412f52 |
910 | BignumDblInt mod, r; |
9400cf6f |
911 | int i; |
912 | |
913 | r = 0; |
914 | mod = modulus; |
915 | for (i = number[0]; i > 0; i--) |
736cc6d1 |
916 | r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod; |
6e522441 |
917 | return (unsigned short) r; |
9400cf6f |
918 | } |
919 | |
a3412f52 |
920 | #ifdef DEBUG |
32874aea |
921 | void diagbn(char *prefix, Bignum md) |
922 | { |
9400cf6f |
923 | int i, nibbles, morenibbles; |
924 | static const char hex[] = "0123456789ABCDEF"; |
925 | |
5c72ca61 |
926 | debug(("%s0x", prefix ? prefix : "")); |
9400cf6f |
927 | |
32874aea |
928 | nibbles = (3 + bignum_bitcount(md)) / 4; |
929 | if (nibbles < 1) |
930 | nibbles = 1; |
931 | morenibbles = 4 * md[0] - nibbles; |
932 | for (i = 0; i < morenibbles; i++) |
5c72ca61 |
933 | debug(("-")); |
32874aea |
934 | for (i = nibbles; i--;) |
5c72ca61 |
935 | debug(("%c", |
936 | hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF])); |
9400cf6f |
937 | |
32874aea |
938 | if (prefix) |
5c72ca61 |
939 | debug(("\n")); |
940 | } |
f28753ab |
941 | #endif |
5c72ca61 |
942 | |
943 | /* |
944 | * Simple division. |
945 | */ |
946 | Bignum bigdiv(Bignum a, Bignum b) |
947 | { |
948 | Bignum q = newbn(a[0]); |
949 | bigdivmod(a, b, NULL, q); |
950 | return q; |
951 | } |
952 | |
953 | /* |
954 | * Simple remainder. |
955 | */ |
956 | Bignum bigmod(Bignum a, Bignum b) |
957 | { |
958 | Bignum r = newbn(b[0]); |
959 | bigdivmod(a, b, r, NULL); |
960 | return r; |
9400cf6f |
961 | } |
962 | |
963 | /* |
964 | * Greatest common divisor. |
965 | */ |
32874aea |
966 | Bignum biggcd(Bignum av, Bignum bv) |
967 | { |
9400cf6f |
968 | Bignum a = copybn(av); |
969 | Bignum b = copybn(bv); |
970 | |
9400cf6f |
971 | while (bignum_cmp(b, Zero) != 0) { |
32874aea |
972 | Bignum t = newbn(b[0]); |
5c72ca61 |
973 | bigdivmod(a, b, t, NULL); |
32874aea |
974 | while (t[0] > 1 && t[t[0]] == 0) |
975 | t[0]--; |
976 | freebn(a); |
977 | a = b; |
978 | b = t; |
9400cf6f |
979 | } |
980 | |
981 | freebn(b); |
982 | return a; |
983 | } |
984 | |
985 | /* |
986 | * Modular inverse, using Euclid's extended algorithm. |
987 | */ |
32874aea |
988 | Bignum modinv(Bignum number, Bignum modulus) |
989 | { |
9400cf6f |
990 | Bignum a = copybn(modulus); |
991 | Bignum b = copybn(number); |
992 | Bignum xp = copybn(Zero); |
993 | Bignum x = copybn(One); |
994 | int sign = +1; |
995 | |
996 | while (bignum_cmp(b, One) != 0) { |
32874aea |
997 | Bignum t = newbn(b[0]); |
998 | Bignum q = newbn(a[0]); |
5c72ca61 |
999 | bigdivmod(a, b, t, q); |
32874aea |
1000 | while (t[0] > 1 && t[t[0]] == 0) |
1001 | t[0]--; |
1002 | freebn(a); |
1003 | a = b; |
1004 | b = t; |
1005 | t = xp; |
1006 | xp = x; |
1007 | x = bigmuladd(q, xp, t); |
1008 | sign = -sign; |
1009 | freebn(t); |
75374b2f |
1010 | freebn(q); |
9400cf6f |
1011 | } |
1012 | |
1013 | freebn(b); |
1014 | freebn(a); |
1015 | freebn(xp); |
1016 | |
1017 | /* now we know that sign * x == 1, and that x < modulus */ |
1018 | if (sign < 0) { |
32874aea |
1019 | /* set a new x to be modulus - x */ |
1020 | Bignum newx = newbn(modulus[0]); |
a3412f52 |
1021 | BignumInt carry = 0; |
32874aea |
1022 | int maxspot = 1; |
1023 | int i; |
1024 | |
62ddb51e |
1025 | for (i = 1; i <= (int)newx[0]; i++) { |
1026 | BignumInt aword = (i <= (int)modulus[0] ? modulus[i] : 0); |
1027 | BignumInt bword = (i <= (int)x[0] ? x[i] : 0); |
32874aea |
1028 | newx[i] = aword - bword - carry; |
1029 | bword = ~bword; |
1030 | carry = carry ? (newx[i] >= bword) : (newx[i] > bword); |
1031 | if (newx[i] != 0) |
1032 | maxspot = i; |
1033 | } |
1034 | newx[0] = maxspot; |
1035 | freebn(x); |
1036 | x = newx; |
9400cf6f |
1037 | } |
1038 | |
1039 | /* and return. */ |
1040 | return x; |
1041 | } |
6e522441 |
1042 | |
1043 | /* |
1044 | * Render a bignum into decimal. Return a malloced string holding |
1045 | * the decimal representation. |
1046 | */ |
32874aea |
1047 | char *bignum_decimal(Bignum x) |
1048 | { |
6e522441 |
1049 | int ndigits, ndigit; |
1050 | int i, iszero; |
a3412f52 |
1051 | BignumDblInt carry; |
6e522441 |
1052 | char *ret; |
a3412f52 |
1053 | BignumInt *workspace; |
6e522441 |
1054 | |
1055 | /* |
1056 | * First, estimate the number of digits. Since log(10)/log(2) |
1057 | * is just greater than 93/28 (the joys of continued fraction |
1058 | * approximations...) we know that for every 93 bits, we need |
1059 | * at most 28 digits. This will tell us how much to malloc. |
1060 | * |
1061 | * Formally: if x has i bits, that means x is strictly less |
1062 | * than 2^i. Since 2 is less than 10^(28/93), this is less than |
1063 | * 10^(28i/93). We need an integer power of ten, so we must |
1064 | * round up (rounding down might make it less than x again). |
1065 | * Therefore if we multiply the bit count by 28/93, rounding |
1066 | * up, we will have enough digits. |
74c79ce8 |
1067 | * |
1068 | * i=0 (i.e., x=0) is an irritating special case. |
6e522441 |
1069 | */ |
ddecd643 |
1070 | i = bignum_bitcount(x); |
74c79ce8 |
1071 | if (!i) |
1072 | ndigits = 1; /* x = 0 */ |
1073 | else |
1074 | ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */ |
32874aea |
1075 | ndigits++; /* allow for trailing \0 */ |
3d88e64d |
1076 | ret = snewn(ndigits, char); |
6e522441 |
1077 | |
1078 | /* |
1079 | * Now allocate some workspace to hold the binary form as we |
1080 | * repeatedly divide it by ten. Initialise this to the |
1081 | * big-endian form of the number. |
1082 | */ |
a3412f52 |
1083 | workspace = snewn(x[0], BignumInt); |
62ddb51e |
1084 | for (i = 0; i < (int)x[0]; i++) |
32874aea |
1085 | workspace[i] = x[x[0] - i]; |
6e522441 |
1086 | |
1087 | /* |
1088 | * Next, write the decimal number starting with the last digit. |
1089 | * We use ordinary short division, dividing 10 into the |
1090 | * workspace. |
1091 | */ |
32874aea |
1092 | ndigit = ndigits - 1; |
6e522441 |
1093 | ret[ndigit] = '\0'; |
1094 | do { |
32874aea |
1095 | iszero = 1; |
1096 | carry = 0; |
62ddb51e |
1097 | for (i = 0; i < (int)x[0]; i++) { |
a3412f52 |
1098 | carry = (carry << BIGNUM_INT_BITS) + workspace[i]; |
1099 | workspace[i] = (BignumInt) (carry / 10); |
32874aea |
1100 | if (workspace[i]) |
1101 | iszero = 0; |
1102 | carry %= 10; |
1103 | } |
1104 | ret[--ndigit] = (char) (carry + '0'); |
6e522441 |
1105 | } while (!iszero); |
1106 | |
1107 | /* |
1108 | * There's a chance we've fallen short of the start of the |
1109 | * string. Correct if so. |
1110 | */ |
1111 | if (ndigit > 0) |
32874aea |
1112 | memmove(ret, ret + ndigit, ndigits - ndigit); |
6e522441 |
1113 | |
1114 | /* |
1115 | * Done. |
1116 | */ |
c523f55f |
1117 | sfree(workspace); |
6e522441 |
1118 | return ret; |
1119 | } |