Skip to content

stdlib/bigintView source ↗

bigint

33 items · generated by scribe fromstdlib/bigint.fd· synced 2026-11-15

stdlib/bigint33 items

Contents

BigInt

A big integer: little-endian base-1e9 limbs + sign. Invariant: no trailing zero limbs (except the value 0 → limbs [0]).

Example: var a = bigint_from_str(“123456789012345678901234567890”); var b = bigint_mul(a, a); println(bigint_to_str(b)); assert_eq(bigint_to_str(b), “15241578753238836750495351562536198787501905199875019052100”);

pub struct BigInt {

Fields:

NameType
limbsVec
negbool

Defined at line 27


BigDiv

Result of bigint_divmod: quotient and remainder.

pub struct BigDiv {

Fields:

NameType
qBigInt
rBigInt

Defined at line 34


bigint_zero

The BigInt 0.

Example: assert(bigint_is_zero(bigint_zero()));

pub fn bigint_zero() -> BigInt {

Returns: BigInt

Defined at line 53


bigint_one

The BigInt 1.

Example: assert_eq(bigint_to_i64(bigint_one()), 1);

pub fn bigint_one() -> BigInt {

Returns: BigInt

Defined at line 65


bigint_from_i64

The BigInt holding x.

Example: assert_eq(bigint_to_str(bigint_from_i64(-42)), “-42”);

pub fn bigint_from_i64(x: i64) -> BigInt {

Parameters:

  • x: i64

Returns: BigInt

Defined at line 77


bigint_is_zero

Whether the value is 0.

Example: assert(bigint_is_zero(bigint_sub(bigint_from_i64(5), bigint_from_i64(5))));

pub fn bigint_is_zero(a: BigInt) -> bool {

Parameters:

  • a: BigInt

Returns: bool

Defined at line 108


bigint_from_str

Parse a decimal string (optional leading ’-’). Invalid input → 0.

Example: var n = bigint_from_str(“-1000000000000”); assert_eq(bigint_to_str(n), “-1000000000000”);

pub fn bigint_from_str(s: str) -> BigInt {

Parameters:

  • s: str

Returns: BigInt

Defined at line 119


bigint_to_str

Decimal string form.

Example: assert_eq(bigint_to_str(bigint_pow(bigint_from_i64(2), 64)), “18446744073709551616”);

pub fn bigint_to_str(a: BigInt) -> str {

Parameters:

  • a: BigInt

Returns: str

Defined at line 148


bigint_to_hex

Hex string form (“0x…” or “-0x…”).

Example: assert_eq(bigint_to_hex(bigint_from_i64(255)), “0xff”); assert_eq(bigint_to_hex(bigint_from_i64(-16)), “-0x10”);

pub fn bigint_to_hex(a: BigInt) -> str {

Parameters:

  • a: BigInt

Returns: str

Defined at line 170


bigint_to_i64

Lossy small-int conversion (clamps into i64 range naturally via mod).

Example: assert_eq(bigint_to_i64(bigint_from_str(“9000000000”)), 9000000000);

pub fn bigint_to_i64(a: BigInt) -> i64 {

Parameters:

  • a: BigInt

Returns: i64

Defined at line 195


bigint_cmp

Compare a and b: -1, 0, 1.

Example: assert_eq(bigint_cmp(bigint_from_i64(-5), bigint_from_i64(3)), -1); assert_eq(bigint_cmp(bigint_from_i64(7), bigint_from_i64(7)), 0);

pub fn bigint_cmp(a: BigInt, b: BigInt) -> i64 {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: i64

Defined at line 239


bigint_eq

Whether a == b.

Example: assert(bigint_eq(bigint_from_str(“10”), bigint_from_i64(10)));

pub fn bigint_eq(a: BigInt, b: BigInt) -> bool {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: bool

Defined at line 263


bigint_lt

Whether a < b.

Example: assert(bigint_lt(bigint_from_i64(-1), bigint_zero()));

pub fn bigint_lt(a: BigInt, b: BigInt) -> bool {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: bool

Defined at line 270


bigint_gt

Whether a > b.

Example: assert(bigint_gt(bigint_from_str(“100000000000000000000”), bigint_from_i64(1)));

pub fn bigint_gt(a: BigInt, b: BigInt) -> bool {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: bool

Defined at line 277


bigint_add

a + b

Example: var s = bigint_add(bigint_from_str(“999999999999999999”), bigint_one()); assert_eq(bigint_to_str(s), “1000000000000000000”);

pub fn bigint_add(a: BigInt, b: BigInt) -> BigInt {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: BigInt

Defined at line 350


bigint_sub

a - b

Example: assert_eq(bigint_to_str(bigint_sub(bigint_from_i64(3), bigint_from_i64(10))), “-7”);

pub fn bigint_sub(a: BigInt, b: BigInt) -> BigInt {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: BigInt

Defined at line 374


bigint_neg

-a

Example: assert_eq(bigint_to_str(bigint_neg(bigint_from_i64(4))), “-4”);

pub fn bigint_neg(a: BigInt) -> BigInt {

Parameters:

  • a: BigInt

Returns: BigInt

Defined at line 388


bigint_abs

|a|

Example: assert_eq(bigint_to_str(bigint_abs(bigint_from_i64(-4))), “4”);

pub fn bigint_abs(a: BigInt) -> BigInt {

Parameters:

  • a: BigInt

Returns: BigInt

Defined at line 401


bigint_mul

a * b (schoolbook — products stay < 10^18, safely in i64).

Example: assert_eq(bigint_to_str(bigint_mul(bigint_from_i64(-3), bigint_from_i64(4))), “-12”);

pub fn bigint_mul(a: BigInt, b: BigInt) -> BigInt {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: BigInt

Defined at line 411


bigint_mul_i64

a * small (|small| < 2^62 or so; products < 10^18 for |small| < 10^9 guaranteed, larger smalls work but multiply limb-wise).

Example: assert_eq(bigint_to_str(bigint_mul_i64(bigint_from_str(“500000000000”), 4)), “2000000000000”);

pub fn bigint_mul_i64(a: BigInt, small: i64) -> BigInt {

Parameters:

  • a: BigInt
  • small: i64

Returns: BigInt

Defined at line 455


bigint_add_i64

a + small

Example: assert_eq(bigint_to_str(bigint_add_i64(bigint_from_i64(1), 2)), “3”);

pub fn bigint_add_i64(a: BigInt, small: i64) -> BigInt {

Parameters:

  • a: BigInt
  • small: i64

Returns: BigInt

Defined at line 487


bigint_divmod

divmod: a = q*b + r, 0 <= |r| < |b| (r takes a’s sign). Division by zero → {0, 0}.

Example: var d = bigint_divmod(bigint_from_i64(17), bigint_from_i64(5)); assert_eq(bigint_to_i64(d.q), 3); assert_eq(bigint_to_i64(d.r), 2);

pub fn bigint_divmod(a: BigInt, b: BigInt) -> BigDiv {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: BigDiv

Defined at line 523


bigint_div

a / b (truncated toward zero).

Example: var q = bigint_div(bigint_from_str(“100000000000000000000”), bigint_from_i64(7)); assert_eq(bigint_to_str(q), “14285714285714285714”);

pub fn bigint_div(a: BigInt, b: BigInt) -> BigInt {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: BigInt

Defined at line 679


bigint_mod

a mod b — result takes a’s sign (truncated division).

Example: assert_eq(bigint_to_i64(bigint_mod(bigint_from_str(“100000000000000000000”), bigint_from_i64(7))), 2);

pub fn bigint_mod(a: BigInt, b: BigInt) -> BigInt {

Parameters:

  • a: BigInt
  • b: BigInt

Returns: BigInt

Defined at line 687


bigint_pow

a^e for e >= 0 (binary exponentiation).

Example: assert_eq(bigint_to_str(bigint_pow(bigint_from_i64(10), 20)), “100000000000000000000”);

pub fn bigint_pow(a: BigInt, e: i64) -> BigInt {

Parameters:

  • a: BigInt
  • e: i64

Returns: BigInt

Defined at line 697


bigint_modpow

(a^e) mod m — the workhorse for RSA-style crypto.

Example: var r = bigint_modpow(bigint_from_i64(4), bigint_from_i64(13), bigint_from_i64(497)); assert_eq(bigint_to_i64(r), 445);

pub fn bigint_modpow(base_in: BigInt, e_in: BigInt, m: BigInt) -> BigInt {

Parameters:

  • base_in: BigInt
  • e_in: BigInt
  • m: BigInt

Returns: BigInt

Defined at line 716


Generated by scribe, the Fides documentation generator