Big Number — arbitrary-precision integer arithmetic (SJCL-derived, BSD-2-Clause; see provenance) for ECDSA / RSA.
Module bn | Source packages/front/fw/src/crypto/utils/bn.js | Deps bitArray, random | Worker-safe yes
Representation: array of 24-bit limbs. Underlying ecc, rsa, rsaKeygen.
Resolve
const bn = runtime.resolve('bn');
// Returns: BN constructor + statics
API (excerpt)
| Method | Signature | Returns |
|---|---|---|
bn.fromBits(bits) / .toBits() |
— | bitArray ↔ BN conversion |
new bn(hex) / .toHex() |
(hex: string) / () => string |
Construct from hex string '0x…' (or number) ↔ hex serialisation '0x…' |
add / sub / mul / mod / mulmod / power / powermod |
— | Arithmetic |
inverseMod(p) |
— | Modular inverse (extended Euclidean) |
equals(other) |
— | Comparison (constant-time with same-limb-length) |
API note: the hex method is named
toHex()and nottoString().Object.prototype.toStringis frozen bysanity/base.js; assigningBn.prototype.toStringwould raiseTypeError: Cannot assign to read only property 'toString'. All BN ↔ hex conversions go throughtoHex/ the constructor.
Examples
const bn = fw.runtime.resolve('bn');
const a = new bn('0x1234');
const b = new bn('0xabcd');
const c = a.mulmod(b, new bn('0xffff')); // (a*b) mod 0xffff
Worker Usage
const worker = fw.createWorker(
function ({ libs, args }) {
const a = new libs.bn(args[0]);
self.postMessage(a.power(args[1]).toHex());
},
{ dependencies: ['bn'], args: ['0x1234', 5] }
);
Notes
- 24-bit limbs: trade-off inherited from the SJCL-derived design, for IEEE-754 precision + JS performance.
powermod: square-and-multiply with k-ary window (~O(bitLen) modmult).equals: constant-time when BNs have the same limb length (prefer.normalize()before security-critical comparisons).