Skip to main content

createProvableBigInt

function createProvableBigInt(modulus: bigint, config?: BigIntParameter): typeof ProvableBigInt_

Defined in: index.ts:166

Creates a class representing a ProvableBigInt with modular arithmetic capabilities. This is particularly useful for implementing prime fields that don't fit into the native field.

const BigInt521 = createProvableBigInt(2n ** 521n - 1n); // creates a class for 521-bit integers

createProvableBigInt(modulus, config?) takes two parameters:

  • modulus: The modulus of the field (must be a prime)
  • config: Optional configuration for custom limb size and numbers

The returned class supports comprehensive arithmetic operations including:

  • Basic operations: addition, double, subtraction, multiplication, square, division
  • Advanced operations: inverse, negate, sqrt, power
  • Comparison operations: equals, assertEquals, greaterThan, lessthan, greaterThanOrEqual, lessThanOrEqual
  • Conversion methods: fromBigInt, toBigInt, fromFields, toFields, fromBits, toBits

Implementation details:

Internally, a ProvableBigInt is represented as an array of Field elements (limbs), where each limb holds 116 bits as default. The total size is determined by the configuration, with preset options supporting different bit lengths:

  • 348 bits (3 limbs)
  • 464 bits (4 limbs)
  • 580 bits (5 limbs)
  • 1044 bits (9 limbs)
  • 2088 bits (18 limbs)
  • 4176 bits (36 limbs)

Each arithmetic operation ensures the result is a valid element of the prime field.

Parameters

modulus

bigint

The modulus for the big integer arithmetic (must be prime)

config?

BigIntParameter

Optional configuration specifying a custom limb size and number

Returns

typeof ProvableBigInt_

A class representing ProvableBigInts with the specified modulus

Example

// Create a Provable BigInt class with modulus 2^521 - 1
const BigInt521 = createProvableBigInt(2n ** 521n - 1n);

// Create instances
const a = BigInt521.fromBigInt(123n);
const b = BigInt521.fromBigInt(456n);
const c = BigInt521.fromBigInt(1024n);

// Perform operations
const sum = a.add(b);
const double = a.double();
const diff = a.sub(b);
const product = a.mul(b);
const square = a.square();
const quotient = a.div(b);
const inverse = a.inverse();
const negation = a.negate();
const power = a.pow(b);
const sqrt = c.sqrt();

The class automatically handles modular reduction after arithmetic operations to maintain valid representations. All operations are designed to be provable and optimised for less constraints.

Throws

If the modulus is zero, negative, or exceeds the maximum supported size