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