2 * Bignum routines for RSA and DH and stuff.
12 #if defined __GNUC__ && defined __i386__
13 typedef unsigned long BignumInt;
14 typedef unsigned long long BignumDblInt;
15 #define BIGNUM_INT_MASK 0xFFFFFFFFUL
16 #define BIGNUM_TOP_BIT 0x80000000UL
17 #define BIGNUM_INT_BITS 32
18 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
19 #define DIVMOD_WORD(q, r, hi, lo, w) \
21 "=d" (r), "=a" (q) : \
22 "r" (w), "d" (hi), "a" (lo))
23 #elif defined _MSC_VER && defined _M_IX86
24 typedef unsigned __int32 BignumInt;
25 typedef unsigned __int64 BignumDblInt;
26 #define BIGNUM_INT_MASK 0xFFFFFFFFUL
27 #define BIGNUM_TOP_BIT 0x80000000UL
28 #define BIGNUM_INT_BITS 32
29 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
31 unsigned __int32 quot;
32 unsigned __int32 remd;
34 static __declspec(naked) msvc_quorem __stdcall msvc_divmod(
40 mov edx, dword ptr [esp+4]
41 mov eax, dword ptr [esp+8]
42 div dword ptr [esp+12]
46 #define DIVMOD_WORD(q, r, hi, lo, w) do { \
47 const msvc_quorem qr = msvc_divmod((hi), (lo), (w)); \
48 (q) = qr.quot; (r) = qr.remd; \
51 typedef unsigned short BignumInt;
52 typedef unsigned long BignumDblInt;
53 #define BIGNUM_INT_MASK 0xFFFFU
54 #define BIGNUM_TOP_BIT 0x8000U
55 #define BIGNUM_INT_BITS 16
56 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
57 #define DIVMOD_WORD(q, r, hi, lo, w) do { \
58 BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
64 #define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
66 #define BIGNUM_INTERNAL
67 typedef BignumInt *Bignum;
71 BignumInt bnZero[1] = { 0 };
72 BignumInt bnOne[2] = { 1, 1 };
75 * The Bignum format is an array of `BignumInt'. The first
76 * element of the array counts the remaining elements. The
77 * remaining elements express the actual number, base 2^BIGNUM_INT_BITS, _least_
78 * significant digit first. (So it's trivial to extract the bit
79 * with value 2^n for any n.)
81 * All Bignums in this module are positive. Negative numbers must
82 * be dealt with outside it.
84 * INVARIANT: the most significant word of any Bignum must be
88 Bignum Zero = bnZero, One = bnOne;
90 static Bignum newbn(int length)
92 Bignum b = snewn(length + 1, BignumInt);
95 memset(b, 0, (length + 1) * sizeof(*b));
100 void bn_restore_invariant(Bignum b)
102 while (b[0] > 1 && b[b[0]] == 0)
106 Bignum copybn(Bignum orig)
108 Bignum b = snewn(orig[0] + 1, BignumInt);
111 memcpy(b, orig, (orig[0] + 1) * sizeof(*b));
115 void freebn(Bignum b)
118 * Burn the evidence, just in case.
120 memset(b, 0, sizeof(b[0]) * (b[0] + 1));
124 Bignum bn_power_2(int n)
126 Bignum ret = newbn(n / BIGNUM_INT_BITS + 1);
127 bignum_set_bit(ret, n, 1);
133 * Input is in the first len words of a and b.
134 * Result is returned in the first 2*len words of c.
136 static void internal_mul(BignumInt *a, BignumInt *b,
137 BignumInt *c, int len)
142 for (j = 0; j < 2 * len; j++)
145 for (i = len - 1; i >= 0; i--) {
147 for (j = len - 1; j >= 0; j--) {
148 t += MUL_WORD(a[i], (BignumDblInt) b[j]);
149 t += (BignumDblInt) c[i + j + 1];
150 c[i + j + 1] = (BignumInt) t;
151 t = t >> BIGNUM_INT_BITS;
153 c[i] = (BignumInt) t;
157 static void internal_add_shifted(BignumInt *number,
158 unsigned n, int shift)
160 int word = 1 + (shift / BIGNUM_INT_BITS);
161 int bshift = shift % BIGNUM_INT_BITS;
164 addend = (BignumDblInt)n << bshift;
167 addend += number[word];
168 number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
169 addend >>= BIGNUM_INT_BITS;
176 * Input in first alen words of a and first mlen words of m.
177 * Output in first alen words of a
178 * (of which first alen-mlen words will be zero).
179 * The MSW of m MUST have its high bit set.
180 * Quotient is accumulated in the `quotient' array, which is a Bignum
181 * rather than the internal bigendian format. Quotient parts are shifted
182 * left by `qshift' before adding into quot.
184 static void internal_mod(BignumInt *a, int alen,
185 BignumInt *m, int mlen,
186 BignumInt *quot, int qshift)
198 for (i = 0; i <= alen - mlen; i++) {
200 unsigned int q, r, c, ai1;
214 /* Find q = h:a[i] / m0 */
219 * To illustrate it, suppose a BignumInt is 8 bits, and
220 * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
221 * our initial division will be 0xA123 / 0xA1, which
222 * will give a quotient of 0x100 and a divide overflow.
223 * However, the invariants in this division algorithm
224 * are not violated, since the full number A1:23:... is
225 * _less_ than the quotient prefix A1:B2:... and so the
226 * following correction loop would have sorted it out.
228 * In this situation we set q to be the largest
229 * quotient we _can_ stomach (0xFF, of course).
233 DIVMOD_WORD(q, r, h, a[i], m0);
235 /* Refine our estimate of q by looking at
236 h:a[i]:a[i+1] / m0:m1 */
238 if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
241 r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
242 if (r >= (BignumDblInt) m0 &&
243 t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
247 /* Subtract q * m from a[i...] */
249 for (k = mlen - 1; k >= 0; k--) {
250 t = MUL_WORD(q, m[k]);
252 c = t >> BIGNUM_INT_BITS;
253 if ((BignumInt) t > a[i + k])
255 a[i + k] -= (BignumInt) t;
258 /* Add back m in case of borrow */
261 for (k = mlen - 1; k >= 0; k--) {
264 a[i + k] = (BignumInt) t;
265 t = t >> BIGNUM_INT_BITS;
270 internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
275 * Compute (base ^ exp) % mod.
277 Bignum modpow(Bignum base_in, Bignum exp, Bignum mod)
279 BignumInt *a, *b, *n, *m;
285 * The most significant word of mod needs to be non-zero. It
286 * should already be, but let's make sure.
288 assert(mod[mod[0]] != 0);
291 * Make sure the base is smaller than the modulus, by reducing
292 * it modulo the modulus if not.
294 base = bigmod(base_in, mod);
296 /* Allocate m of size mlen, copy mod to m */
297 /* We use big endian internally */
299 m = snewn(mlen, BignumInt);
300 for (j = 0; j < mlen; j++)
301 m[j] = mod[mod[0] - j];
303 /* Shift m left to make msb bit set */
304 for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
305 if ((m[0] << mshift) & BIGNUM_TOP_BIT)
308 for (i = 0; i < mlen - 1; i++)
309 m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
310 m[mlen - 1] = m[mlen - 1] << mshift;
313 /* Allocate n of size mlen, copy base to n */
314 n = snewn(mlen, BignumInt);
316 for (j = 0; j < i; j++)
318 for (j = 0; j < base[0]; j++)
319 n[i + j] = base[base[0] - j];
321 /* Allocate a and b of size 2*mlen. Set a = 1 */
322 a = snewn(2 * mlen, BignumInt);
323 b = snewn(2 * mlen, BignumInt);
324 for (i = 0; i < 2 * mlen; i++)
328 /* Skip leading zero bits of exp. */
330 j = BIGNUM_INT_BITS-1;
331 while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
335 j = BIGNUM_INT_BITS-1;
339 /* Main computation */
342 internal_mul(a + mlen, a + mlen, b, mlen);
343 internal_mod(b, mlen * 2, m, mlen, NULL, 0);
344 if ((exp[exp[0] - i] & (1 << j)) != 0) {
345 internal_mul(b + mlen, n, a, mlen);
346 internal_mod(a, mlen * 2, m, mlen, NULL, 0);
356 j = BIGNUM_INT_BITS-1;
359 /* Fixup result in case the modulus was shifted */
361 for (i = mlen - 1; i < 2 * mlen - 1; i++)
362 a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift));
363 a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
364 internal_mod(a, mlen * 2, m, mlen, NULL, 0);
365 for (i = 2 * mlen - 1; i >= mlen; i--)
366 a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift));
369 /* Copy result to buffer */
370 result = newbn(mod[0]);
371 for (i = 0; i < mlen; i++)
372 result[result[0] - i] = a[i + mlen];
373 while (result[0] > 1 && result[result[0]] == 0)
376 /* Free temporary arrays */
377 for (i = 0; i < 2 * mlen; i++)
380 for (i = 0; i < 2 * mlen; i++)
383 for (i = 0; i < mlen; i++)
386 for (i = 0; i < mlen; i++)
396 * Compute (p * q) % mod.
397 * The most significant word of mod MUST be non-zero.
398 * We assume that the result array is the same size as the mod array.
400 Bignum modmul(Bignum p, Bignum q, Bignum mod)
402 BignumInt *a, *n, *m, *o;
404 int pqlen, mlen, rlen, i, j;
407 /* Allocate m of size mlen, copy mod to m */
408 /* We use big endian internally */
410 m = snewn(mlen, BignumInt);
411 for (j = 0; j < mlen; j++)
412 m[j] = mod[mod[0] - j];
414 /* Shift m left to make msb bit set */
415 for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
416 if ((m[0] << mshift) & BIGNUM_TOP_BIT)
419 for (i = 0; i < mlen - 1; i++)
420 m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
421 m[mlen - 1] = m[mlen - 1] << mshift;
424 pqlen = (p[0] > q[0] ? p[0] : q[0]);
426 /* Allocate n of size pqlen, copy p to n */
427 n = snewn(pqlen, BignumInt);
429 for (j = 0; j < i; j++)
431 for (j = 0; j < p[0]; j++)
432 n[i + j] = p[p[0] - j];
434 /* Allocate o of size pqlen, copy q to o */
435 o = snewn(pqlen, BignumInt);
437 for (j = 0; j < i; j++)
439 for (j = 0; j < q[0]; j++)
440 o[i + j] = q[q[0] - j];
442 /* Allocate a of size 2*pqlen for result */
443 a = snewn(2 * pqlen, BignumInt);
445 /* Main computation */
446 internal_mul(n, o, a, pqlen);
447 internal_mod(a, pqlen * 2, m, mlen, NULL, 0);
449 /* Fixup result in case the modulus was shifted */
451 for (i = 2 * pqlen - mlen - 1; i < 2 * pqlen - 1; i++)
452 a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift));
453 a[2 * pqlen - 1] = a[2 * pqlen - 1] << mshift;
454 internal_mod(a, pqlen * 2, m, mlen, NULL, 0);
455 for (i = 2 * pqlen - 1; i >= 2 * pqlen - mlen; i--)
456 a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift));
459 /* Copy result to buffer */
460 rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2);
461 result = newbn(rlen);
462 for (i = 0; i < rlen; i++)
463 result[result[0] - i] = a[i + 2 * pqlen - rlen];
464 while (result[0] > 1 && result[result[0]] == 0)
467 /* Free temporary arrays */
468 for (i = 0; i < 2 * pqlen; i++)
471 for (i = 0; i < mlen; i++)
474 for (i = 0; i < pqlen; i++)
477 for (i = 0; i < pqlen; i++)
486 * The most significant word of mod MUST be non-zero.
487 * We assume that the result array is the same size as the mod array.
488 * We optionally write out a quotient if `quotient' is non-NULL.
489 * We can avoid writing out the result if `result' is NULL.
491 static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
495 int plen, mlen, i, j;
497 /* Allocate m of size mlen, copy mod to m */
498 /* We use big endian internally */
500 m = snewn(mlen, BignumInt);
501 for (j = 0; j < mlen; j++)
502 m[j] = mod[mod[0] - j];
504 /* Shift m left to make msb bit set */
505 for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
506 if ((m[0] << mshift) & BIGNUM_TOP_BIT)
509 for (i = 0; i < mlen - 1; i++)
510 m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
511 m[mlen - 1] = m[mlen - 1] << mshift;
515 /* Ensure plen > mlen */
519 /* Allocate n of size plen, copy p to n */
520 n = snewn(plen, BignumInt);
521 for (j = 0; j < plen; j++)
523 for (j = 1; j <= p[0]; j++)
526 /* Main computation */
527 internal_mod(n, plen, m, mlen, quotient, mshift);
529 /* Fixup result in case the modulus was shifted */
531 for (i = plen - mlen - 1; i < plen - 1; i++)
532 n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
533 n[plen - 1] = n[plen - 1] << mshift;
534 internal_mod(n, plen, m, mlen, quotient, 0);
535 for (i = plen - 1; i >= plen - mlen; i--)
536 n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
539 /* Copy result to buffer */
541 for (i = 1; i <= result[0]; i++) {
543 result[i] = j >= 0 ? n[j] : 0;
547 /* Free temporary arrays */
548 for (i = 0; i < mlen; i++)
551 for (i = 0; i < plen; i++)
557 * Decrement a number.
559 void decbn(Bignum bn)
562 while (i < bn[0] && bn[i] == 0)
563 bn[i++] = BIGNUM_INT_MASK;
567 Bignum bignum_from_bytes(const unsigned char *data, int nbytes)
572 w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */
575 for (i = 1; i <= w; i++)
577 for (i = nbytes; i--;) {
578 unsigned char byte = *data++;
579 result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS);
582 while (result[0] > 1 && result[result[0]] == 0)
588 * Read an SSH-1-format bignum from a data buffer. Return the number
589 * of bytes consumed, or -1 if there wasn't enough data.
591 int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result)
593 const unsigned char *p = data;
601 for (i = 0; i < 2; i++)
603 b = (w + 7) / 8; /* bits -> bytes */
608 if (!result) /* just return length */
611 *result = bignum_from_bytes(p, b);
617 * Return the bit count of a bignum, for SSH-1 encoding.
619 int bignum_bitcount(Bignum bn)
621 int bitcount = bn[0] * BIGNUM_INT_BITS - 1;
623 && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--;
628 * Return the byte length of a bignum when SSH-1 encoded.
630 int ssh1_bignum_length(Bignum bn)
632 return 2 + (bignum_bitcount(bn) + 7) / 8;
636 * Return the byte length of a bignum when SSH-2 encoded.
638 int ssh2_bignum_length(Bignum bn)
640 return 4 + (bignum_bitcount(bn) + 8) / 8;
644 * Return a byte from a bignum; 0 is least significant, etc.
646 int bignum_byte(Bignum bn, int i)
648 if (i >= BIGNUM_INT_BYTES * bn[0])
649 return 0; /* beyond the end */
651 return (bn[i / BIGNUM_INT_BYTES + 1] >>
652 ((i % BIGNUM_INT_BYTES)*8)) & 0xFF;
656 * Return a bit from a bignum; 0 is least significant, etc.
658 int bignum_bit(Bignum bn, int i)
660 if (i >= BIGNUM_INT_BITS * bn[0])
661 return 0; /* beyond the end */
663 return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1;
667 * Set a bit in a bignum; 0 is least significant, etc.
669 void bignum_set_bit(Bignum bn, int bitnum, int value)
671 if (bitnum >= BIGNUM_INT_BITS * bn[0])
672 abort(); /* beyond the end */
674 int v = bitnum / BIGNUM_INT_BITS + 1;
675 int mask = 1 << (bitnum % BIGNUM_INT_BITS);
684 * Write a SSH-1-format bignum into a buffer. It is assumed the
685 * buffer is big enough. Returns the number of bytes used.
687 int ssh1_write_bignum(void *data, Bignum bn)
689 unsigned char *p = data;
690 int len = ssh1_bignum_length(bn);
692 int bitc = bignum_bitcount(bn);
694 *p++ = (bitc >> 8) & 0xFF;
695 *p++ = (bitc) & 0xFF;
696 for (i = len - 2; i--;)
697 *p++ = bignum_byte(bn, i);
702 * Compare two bignums. Returns like strcmp.
704 int bignum_cmp(Bignum a, Bignum b)
706 int amax = a[0], bmax = b[0];
707 int i = (amax > bmax ? amax : bmax);
709 BignumInt aval = (i > amax ? 0 : a[i]);
710 BignumInt bval = (i > bmax ? 0 : b[i]);
721 * Right-shift one bignum to form another.
723 Bignum bignum_rshift(Bignum a, int shift)
726 int i, shiftw, shiftb, shiftbb, bits;
729 bits = bignum_bitcount(a) - shift;
730 ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
733 shiftw = shift / BIGNUM_INT_BITS;
734 shiftb = shift % BIGNUM_INT_BITS;
735 shiftbb = BIGNUM_INT_BITS - shiftb;
738 for (i = 1; i <= ret[0]; i++) {
740 ai1 = (i + shiftw + 1 <= a[0] ? a[i + shiftw + 1] : 0);
741 ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK;
749 * Non-modular multiplication and addition.
751 Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
753 int alen = a[0], blen = b[0];
754 int mlen = (alen > blen ? alen : blen);
755 int rlen, i, maxspot;
756 BignumInt *workspace;
759 /* mlen space for a, mlen space for b, 2*mlen for result */
760 workspace = snewn(mlen * 4, BignumInt);
761 for (i = 0; i < mlen; i++) {
762 workspace[0 * mlen + i] = (mlen - i <= a[0] ? a[mlen - i] : 0);
763 workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0);
766 internal_mul(workspace + 0 * mlen, workspace + 1 * mlen,
767 workspace + 2 * mlen, mlen);
769 /* now just copy the result back */
770 rlen = alen + blen + 1;
771 if (addend && rlen <= addend[0])
772 rlen = addend[0] + 1;
775 for (i = 1; i <= ret[0]; i++) {
776 ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0);
782 /* now add in the addend, if any */
784 BignumDblInt carry = 0;
785 for (i = 1; i <= rlen; i++) {
786 carry += (i <= ret[0] ? ret[i] : 0);
787 carry += (i <= addend[0] ? addend[i] : 0);
788 ret[i] = (BignumInt) carry & BIGNUM_INT_MASK;
789 carry >>= BIGNUM_INT_BITS;
790 if (ret[i] != 0 && i > maxspot)
801 * Non-modular multiplication.
803 Bignum bigmul(Bignum a, Bignum b)
805 return bigmuladd(a, b, NULL);
809 * Create a bignum which is the bitmask covering another one. That
810 * is, the smallest integer which is >= N and is also one less than
813 Bignum bignum_bitmask(Bignum n)
815 Bignum ret = copybn(n);
820 while (n[i] == 0 && i > 0)
823 return ret; /* input was zero */
829 ret[i] = BIGNUM_INT_MASK;
834 * Convert a (max 32-bit) long into a bignum.
836 Bignum bignum_from_long(unsigned long nn)
842 ret[1] = (BignumInt)(n & BIGNUM_INT_MASK);
843 ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK);
845 ret[0] = (ret[2] ? 2 : 1);
850 * Add a long to a bignum.
852 Bignum bignum_add_long(Bignum number, unsigned long addendx)
854 Bignum ret = newbn(number[0] + 1);
856 BignumDblInt carry = 0, addend = addendx;
858 for (i = 1; i <= ret[0]; i++) {
859 carry += addend & BIGNUM_INT_MASK;
860 carry += (i <= number[0] ? number[i] : 0);
861 addend >>= BIGNUM_INT_BITS;
862 ret[i] = (BignumInt) carry & BIGNUM_INT_MASK;
863 carry >>= BIGNUM_INT_BITS;
872 * Compute the residue of a bignum, modulo a (max 16-bit) short.
874 unsigned short bignum_mod_short(Bignum number, unsigned short modulus)
881 for (i = number[0]; i > 0; i--)
882 r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod;
883 return (unsigned short) r;
887 void diagbn(char *prefix, Bignum md)
889 int i, nibbles, morenibbles;
890 static const char hex[] = "0123456789ABCDEF";
892 debug(("%s0x", prefix ? prefix : ""));
894 nibbles = (3 + bignum_bitcount(md)) / 4;
897 morenibbles = 4 * md[0] - nibbles;
898 for (i = 0; i < morenibbles; i++)
900 for (i = nibbles; i--;)
902 hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]));
912 Bignum bigdiv(Bignum a, Bignum b)
914 Bignum q = newbn(a[0]);
915 bigdivmod(a, b, NULL, q);
922 Bignum bigmod(Bignum a, Bignum b)
924 Bignum r = newbn(b[0]);
925 bigdivmod(a, b, r, NULL);
930 * Greatest common divisor.
932 Bignum biggcd(Bignum av, Bignum bv)
934 Bignum a = copybn(av);
935 Bignum b = copybn(bv);
937 while (bignum_cmp(b, Zero) != 0) {
938 Bignum t = newbn(b[0]);
939 bigdivmod(a, b, t, NULL);
940 while (t[0] > 1 && t[t[0]] == 0)
952 * Modular inverse, using Euclid's extended algorithm.
954 Bignum modinv(Bignum number, Bignum modulus)
956 Bignum a = copybn(modulus);
957 Bignum b = copybn(number);
958 Bignum xp = copybn(Zero);
959 Bignum x = copybn(One);
962 while (bignum_cmp(b, One) != 0) {
963 Bignum t = newbn(b[0]);
964 Bignum q = newbn(a[0]);
965 bigdivmod(a, b, t, q);
966 while (t[0] > 1 && t[t[0]] == 0)
973 x = bigmuladd(q, xp, t);
983 /* now we know that sign * x == 1, and that x < modulus */
985 /* set a new x to be modulus - x */
986 Bignum newx = newbn(modulus[0]);
991 for (i = 1; i <= newx[0]; i++) {
992 BignumInt aword = (i <= modulus[0] ? modulus[i] : 0);
993 BignumInt bword = (i <= x[0] ? x[i] : 0);
994 newx[i] = aword - bword - carry;
996 carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
1010 * Render a bignum into decimal. Return a malloced string holding
1011 * the decimal representation.
1013 char *bignum_decimal(Bignum x)
1015 int ndigits, ndigit;
1019 BignumInt *workspace;
1022 * First, estimate the number of digits. Since log(10)/log(2)
1023 * is just greater than 93/28 (the joys of continued fraction
1024 * approximations...) we know that for every 93 bits, we need
1025 * at most 28 digits. This will tell us how much to malloc.
1027 * Formally: if x has i bits, that means x is strictly less
1028 * than 2^i. Since 2 is less than 10^(28/93), this is less than
1029 * 10^(28i/93). We need an integer power of ten, so we must
1030 * round up (rounding down might make it less than x again).
1031 * Therefore if we multiply the bit count by 28/93, rounding
1032 * up, we will have enough digits.
1034 i = bignum_bitcount(x);
1035 ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */
1036 ndigits++; /* allow for trailing \0 */
1037 ret = snewn(ndigits, char);
1040 * Now allocate some workspace to hold the binary form as we
1041 * repeatedly divide it by ten. Initialise this to the
1042 * big-endian form of the number.
1044 workspace = snewn(x[0], BignumInt);
1045 for (i = 0; i < x[0]; i++)
1046 workspace[i] = x[x[0] - i];
1049 * Next, write the decimal number starting with the last digit.
1050 * We use ordinary short division, dividing 10 into the
1053 ndigit = ndigits - 1;
1058 for (i = 0; i < x[0]; i++) {
1059 carry = (carry << BIGNUM_INT_BITS) + workspace[i];
1060 workspace[i] = (BignumInt) (carry / 10);
1065 ret[--ndigit] = (char) (carry + '0');
1069 * There's a chance we've fallen short of the start of the
1070 * string. Correct if so.
1073 memmove(ret, ret + ndigit, ndigits - ndigit);