Skip to content

BigInt

Source: std/bigint.mw.

Unsigned big integers on 32-bit limbs, enough for RSA and the elliptic curves used by TLS. Operations allocate their result.

Arbitrary-precision unsigned integers (little-endian 32-bit limbs) with the modular arithmetic needed for RSA and elliptic curves. Every function that returns a *Big returns a fresh value the caller releases with big_free. Reduction is shift-and-subtract: simple and constant-memory, adequate for signature *verification* (public data); it is not constant time.

struct Big {
d: *u32;
n: i64;
}
fn big_alloc(n: i64) -> *Big
fn big_free(b: *Big)
fn big_trim(b: *Big)
fn big_from_u64(v: u64) -> *Big
fn big_copy(a: *Big) -> *Big
fn big_from_bytes(p: *u8, len: i64) -> *Big
fn big_to_bytes(a: *Big, out: *u8, len: i64) -> bool

Writes a big-endian, left-padded with zeros to exactly len bytes. Returns false if it does not fit.

fn big_is_zero(a: *Big) -> bool
fn big_bits(a: *Big) -> i64
fn big_bit(a: *Big, i: i64) -> i64
fn big_is_odd(a: *Big) -> bool
fn big_cmp(a: *Big, b: *Big) -> i64
fn big_add(a: *Big, b: *Big) -> *Big
fn big_sub(a: *Big, b: *Big) -> *Big

a - b, requires a >= b.

fn big_mul(a: *Big, b: *Big) -> *Big
fn big_mod(a: *Big, m: *Big) -> *Big

a mod m (m > 0).

fn big_modmul(a: *Big, b: *Big, m: *Big) -> *Big
fn big_modadd(a: *Big, b: *Big, m: *Big) -> *Big
fn big_modsub(a: *Big, b: *Big, m: *Big) -> *Big

(a - b) mod m for a, b < m.

fn big_modexp(base: *Big, exp: *Big, m: *Big) -> *Big
fn big_modinv_prime(a: *Big, p: *Big) -> *Big

a^-1 mod p for prime p (Fermat).