+/*
+ * Add a long to a bignum.
+ */
+Bignum bignum_add_long(Bignum number, unsigned long addend) {
+ Bignum ret = newbn(number[0]+1);
+ int i, maxspot = 0;
+ unsigned long carry = 0;
+
+ for (i = 1; i <= ret[0]; i++) {
+ carry += addend & 0xFFFF;
+ carry += (i <= number[0] ? number[i] : 0);
+ addend >>= 16;
+ ret[i] = (unsigned short) carry & 0xFFFF;
+ carry >>= 16;
+ if (ret[i] != 0)
+ maxspot = i;
+ }
+ ret[0] = maxspot;
+ return ret;
+}
+
+/*
+ * Compute the residue of a bignum, modulo a (max 16-bit) short.
+ */
+unsigned short bignum_mod_short(Bignum number, unsigned short modulus) {
+ unsigned long mod, r;
+ int i;
+
+ r = 0;
+ mod = modulus;
+ for (i = number[0]; i > 0; i--)
+ r = (r * 65536 + number[i]) % mod;
+ return (unsigned short) r;
+}
+
+void diagbn(char *prefix, Bignum md) {
+ int i, nibbles, morenibbles;
+ static const char hex[] = "0123456789ABCDEF";
+
+ debugprint(("%s0x", prefix ? prefix : ""));
+
+ nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
+ morenibbles = 4*md[0] - nibbles;
+ for (i=0; i<morenibbles; i++) debugprint(("-"));
+ for (i=nibbles; i-- ;)
+ debugprint(("%c",hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF]));
+
+ if (prefix) debugprint(("\n"));
+}
+
+/*
+ * Greatest common divisor.
+ */
+Bignum biggcd(Bignum av, Bignum bv) {
+ Bignum a = copybn(av);
+ Bignum b = copybn(bv);
+
+ diagbn("a = ", a);
+ diagbn("b = ", b);
+ while (bignum_cmp(b, Zero) != 0) {
+ Bignum t = newbn(b[0]);
+ bigmod(a, b, t, NULL);
+ diagbn("t = ", t);
+ while (t[0] > 1 && t[t[0]] == 0) t[0]--;
+ freebn(a);
+ a = b;
+ b = t;
+ }
+
+ freebn(b);
+ return a;
+}
+
+/*
+ * Modular inverse, using Euclid's extended algorithm.
+ */
+Bignum modinv(Bignum number, Bignum modulus) {
+ Bignum a = copybn(modulus);
+ Bignum b = copybn(number);
+ Bignum xp = copybn(Zero);
+ Bignum x = copybn(One);
+ int sign = +1;
+
+ while (bignum_cmp(b, One) != 0) {
+ Bignum t = newbn(b[0]);
+ Bignum q = newbn(a[0]);
+ bigmod(a, b, t, q);
+ while (t[0] > 1 && t[t[0]] == 0) t[0]--;
+ freebn(a);
+ a = b;
+ b = t;
+ t = xp;
+ xp = x;
+ x = bigmuladd(q, xp, t);
+ sign = -sign;
+ freebn(t);
+ }
+
+ freebn(b);
+ freebn(a);
+ freebn(xp);
+
+ /* now we know that sign * x == 1, and that x < modulus */
+ if (sign < 0) {
+ /* set a new x to be modulus - x */
+ Bignum newx = newbn(modulus[0]);
+ unsigned short carry = 0;
+ int maxspot = 1;
+ int i;
+
+ for (i = 1; i <= newx[0]; i++) {
+ unsigned short aword = (i <= modulus[0] ? modulus[i] : 0);
+ unsigned short bword = (i <= x[0] ? x[i] : 0);
+ newx[i] = aword - bword - carry;
+ bword = ~bword;
+ carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
+ if (newx[i] != 0)
+ maxspot = i;
+ }
+ newx[0] = maxspot;
+ freebn(x);
+ x = newx;
+ }
+
+ /* and return. */
+ return x;
+}
+
+/*
+ * Render a bignum into decimal. Return a malloced string holding
+ * the decimal representation.
+ */
+char *bignum_decimal(Bignum x) {
+ int ndigits, ndigit;
+ int i, iszero;
+ unsigned long carry;
+ char *ret;
+ unsigned short *workspace;
+
+ /*
+ * First, estimate the number of digits. Since log(10)/log(2)
+ * is just greater than 93/28 (the joys of continued fraction
+ * approximations...) we know that for every 93 bits, we need
+ * at most 28 digits. This will tell us how much to malloc.
+ *
+ * Formally: if x has i bits, that means x is strictly less
+ * than 2^i. Since 2 is less than 10^(28/93), this is less than
+ * 10^(28i/93). We need an integer power of ten, so we must
+ * round up (rounding down might make it less than x again).
+ * Therefore if we multiply the bit count by 28/93, rounding
+ * up, we will have enough digits.
+ */
+ i = ssh1_bignum_bitcount(x);
+ ndigits = (28*i + 92)/93; /* multiply by 28/93 and round up */
+ ndigits++; /* allow for trailing \0 */
+ ret = smalloc(ndigits);
+
+ /*
+ * Now allocate some workspace to hold the binary form as we
+ * repeatedly divide it by ten. Initialise this to the
+ * big-endian form of the number.
+ */
+ workspace = smalloc(sizeof(unsigned short) * x[0]);
+ for (i = 0; i < x[0]; i++)
+ workspace[i] = x[x[0] - i];
+
+ /*
+ * Next, write the decimal number starting with the last digit.
+ * We use ordinary short division, dividing 10 into the
+ * workspace.
+ */
+ ndigit = ndigits-1;
+ ret[ndigit] = '\0';
+ do {
+ iszero = 1;
+ carry = 0;
+ for (i = 0; i < x[0]; i++) {
+ carry = (carry << 16) + workspace[i];
+ workspace[i] = (unsigned short) (carry / 10);
+ if (workspace[i])
+ iszero = 0;
+ carry %= 10;
+ }
+ ret[--ndigit] = (char)(carry + '0');
+ } while (!iszero);
+
+ /*
+ * There's a chance we've fallen short of the start of the
+ * string. Correct if so.
+ */
+ if (ndigit > 0)
+ memmove(ret, ret+ndigit, ndigits-ndigit);
+
+ /*
+ * Done.
+ */
+ return ret;