+ result[1 + i / BIGNUM_INT_BYTES] |=
+ (BignumInt)byte << (8*i % BIGNUM_INT_BITS);
+ }
+
+ bn_restore_invariant(result);
+ return result;
+}
+
+Bignum bignum_from_bytes_le(const unsigned char *data, int nbytes)
+{
+ Bignum result;
+ int w, i;
+
+ assert(nbytes >= 0 && nbytes < INT_MAX/8);
+
+ w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */
+
+ result = newbn(w);
+ for (i = 1; i <= w; i++)
+ result[i] = 0;
+ for (i = 0; i < nbytes; ++i) {
+ unsigned char byte = *data++;
+ result[1 + i / BIGNUM_INT_BYTES] |=
+ (BignumInt)byte << (8*i % BIGNUM_INT_BITS);
+ }
+
+ bn_restore_invariant(result);
+ return result;
+}
+
+Bignum bignum_from_decimal(const char *decimal)
+{
+ Bignum result = copybn(Zero);
+
+ while (*decimal) {
+ Bignum tmp, tmp2;
+
+ if (!isdigit((unsigned char)*decimal)) {
+ freebn(result);
+ return 0;
+ }
+
+ tmp = bigmul(result, Ten);
+ tmp2 = bignum_from_long(*decimal - '0');
+ result = bigadd(tmp, tmp2);
+ freebn(tmp);
+ freebn(tmp2);
+
+ decimal++;