or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdbitwise.mdcomparison.mdconversion.mdindex.mdreduction.md
tile.json

arithmetic.mddocs/

Arithmetic Operations

Comprehensive arithmetic operations for big numbers including basic arithmetic (addition, subtraction, multiplication, division), advanced operations (exponentiation, modular arithmetic), and specialized functions.

Capabilities

Addition

Add two big numbers together with support for both BN instances and JavaScript numbers.

/**
 * Add another BN to this number
 * @param {BN} num - Number to add
 * @returns {BN} New BN instance with the sum
 */
add(num: BN): BN;

/**
 * Add another BN to this number in-place
 * @param {BN} num - Number to add
 * @returns {BN} This BN instance modified with the sum
 */
iadd(num: BN): BN;

/**
 * Add a JavaScript number to this BN
 * @param {number} num - JavaScript number to add (must be < 0x4000000)
 * @returns {BN} New BN instance with the sum
 */
addn(num: number): BN;

/**
 * Add a JavaScript number to this BN in-place
 * @param {number} num - JavaScript number to add (must be < 0x4000000) 
 * @returns {BN} This BN instance modified with the sum
 */
iaddn(num: number): BN;

Usage Examples:

const BN = require('bn.js');

const a = new BN('123456789');
const b = new BN('987654321');
const small = 42;

// Basic addition
const sum1 = a.add(b);           // New instance
const sum2 = a.clone().iadd(b);  // In-place (on clone)

// Add JavaScript number
const sum3 = a.addn(small);      // New instance  
const sum4 = a.clone().iaddn(small); // In-place (on clone)

console.log(sum1.toString());    // "1111111110"

Subtraction

Subtract one big number from another with proper sign handling.

/**
 * Subtract another BN from this number
 * @param {BN} num - Number to subtract
 * @returns {BN} New BN instance with the difference
 */
sub(num: BN): BN;

/**
 * Subtract another BN from this number in-place
 * @param {BN} num - Number to subtract
 * @returns {BN} This BN instance modified with the difference
 */
isub(num: BN): BN;

/**
 * Subtract a JavaScript number from this BN
 * @param {number} num - JavaScript number to subtract (must be < 0x4000000)
 * @returns {BN} New BN instance with the difference
 */
subn(num: number): BN;

/**
 * Subtract a JavaScript number from this BN in-place
 * @param {number} num - JavaScript number to subtract (must be < 0x4000000)
 * @returns {BN} This BN instance modified with the difference
 */
isubn(num: number): BN;

Multiplication

Multiply big numbers with various optimization strategies based on number size.

/**
 * Multiply this BN by another BN
 * @param {BN} num - Number to multiply by
 * @returns {BN} New BN instance with the product
 */
mul(num: BN): BN;

/**
 * Multiply this BN by another BN in-place
 * @param {BN} num - Number to multiply by
 * @returns {BN} This BN instance modified with the product
 */
imul(num: BN): BN;

/**
 * Multiply using FFT algorithm (for very large numbers)
 * @param {BN} num - Number to multiply by
 * @returns {BN} New BN instance with the product
 */
mulf(num: BN): BN;

/**
 * Multiply this BN by a JavaScript number
 * @param {number} num - JavaScript number to multiply by (must be < 0x4000000)
 * @returns {BN} New BN instance with the product
 */
muln(num: number): BN;

/**
 * Multiply this BN by a JavaScript number in-place
 * @param {number} num - JavaScript number to multiply by (must be < 0x4000000)
 * @returns {BN} This BN instance modified with the product
 */
imuln(num: number): BN;

Usage Examples:

const BN = require('bn.js');

const a = new BN('12345');
const b = new BN('67890'); 
const small = 100;

// Basic multiplication
const product1 = a.mul(b);       // Standard algorithm
const product2 = a.mulf(b);      // FFT algorithm (same result)

// Multiply by JavaScript number
const product3 = a.muln(small);  // Efficient for small numbers

console.log(product1.toString()); // "838102050"

Squaring

Efficiently compute the square of a number (optimized compared to multiplication).

/**
 * Square this number (multiply by itself)
 * @returns {BN} New BN instance with the square
 */
sqr(): BN;

/**
 * Square this number in-place
 * @returns {BN} This BN instance modified with the square
 */
isqr(): BN;

Exponentiation

Raise a number to a power using efficient exponentiation algorithms.

/**
 * Raise this number to the power of another number
 * @param {BN} exponent - The exponent
 * @returns {BN} New BN instance with the result
 */
pow(exponent: BN): BN;

Usage Examples:

const BN = require('bn.js');

const base = new BN('2');
const exponent = new BN('10');

const result = base.pow(exponent);
console.log(result.toString()); // "1024" (2^10)

Division and Modulo

Division operations with quotient, remainder, and combined operations.

/**
 * Divide this number by another number
 * @param {BN} divisor - Number to divide by
 * @returns {BN} New BN instance with the quotient
 */
div(divisor: BN): BN;

/**
 * Get the remainder of dividing this number by another
 * @param {BN} divisor - Number to divide by
 * @returns {BN} New BN instance with the remainder
 */
mod(divisor: BN): BN;

/**
 * Get unsigned modulo (always positive result)
 * @param {BN} divisor - Number to divide by
 * @returns {BN} New BN instance with the unsigned remainder
 */
umod(divisor: BN): BN;

/**
 * Perform division and modulo in one operation
 * @param {BN} divisor - Number to divide by
 * @param {string} [mode] - 'div' for quotient only, 'mod' for remainder only
 * @param {boolean} [positive] - Whether to force positive remainder
 * @returns {{div: BN, mod: BN}} Object with both quotient and remainder
 */
divmod(divisor: BN, mode?: string, positive?: boolean): {div: BN, mod: BN};

/**
 * Rounded division (rounds to nearest integer)
 * @param {BN} divisor - Number to divide by
 * @returns {BN} New BN instance with the rounded quotient
 */
divRound(divisor: BN): BN;

Division by JavaScript Numbers

Efficient division operations when the divisor is a JavaScript number.

/**
 * Divide this BN by a JavaScript number
 * @param {number} divisor - JavaScript number to divide by (must be < 0x4000000)
 * @returns {BN} New BN instance with the quotient
 */
divn(divisor: number): BN;

/**
 * Divide this BN by a JavaScript number in-place
 * @param {number} divisor - JavaScript number to divide by (must be < 0x4000000)
 * @returns {BN} This BN instance modified with the quotient
 */
idivn(divisor: number): BN;

/**
 * Get remainder of dividing this BN by a JavaScript number
 * @param {number} divisor - JavaScript number to divide by (must be < 0x4000000)
 * @returns {number} Remainder as JavaScript number
 */
modrn(divisor: number): number;

/**
 * Get remainder of dividing this BN by a JavaScript number (deprecated)
 * @param {number} divisor - JavaScript number to divide by
 * @returns {number} Remainder as JavaScript number
 * @deprecated Use modrn instead
 */
modn(divisor: number): number;

Usage Examples:

const BN = require('bn.js');

const dividend = new BN('1000000');
const divisor = new BN('7');
const smallDivisor = 13;

// Division operations
const quotient = dividend.div(divisor);
const remainder = dividend.mod(divisor);
const both = dividend.divmod(divisor);

console.log(quotient.toString());  // "142857"
console.log(remainder.toString()); // "1"
console.log(both.div.toString());  // "142857"
console.log(both.mod.toString());  // "1"

// Division by JavaScript number
const quotient2 = dividend.divn(smallDivisor);
const remainder2 = dividend.modrn(smallDivisor);

console.log(quotient2.toString()); // "76923"
console.log(remainder2);           // 1

Sign Operations

Operations for handling and manipulating the sign of numbers.

/**
 * Get the negation of this number
 * @returns {BN} New BN instance with opposite sign
 */
neg(): BN;

/**
 * Negate this number in-place
 * @returns {BN} This BN instance with sign flipped
 */  
ineg(): BN;

/**
 * Get the absolute value of this number
 * @returns {BN} New BN instance with positive value
 */
abs(): BN;

/**
 * Make this number positive in-place
 * @returns {BN} This BN instance made positive
 */
iabs(): BN;

Greatest Common Divisor

Mathematical functions for finding greatest common divisors and related operations.

/**
 * Find the greatest common divisor of this number and another
 * @param {BN} num - Other number
 * @returns {BN} New BN instance with the GCD
 */
gcd(num: BN): BN;

/**
 * Extended Euclidean algorithm
 * @param {BN} p - Other number
 * @returns {{a: BN, b: BN, gcd: BN}} Coefficients a, b and GCD such that a*this + b*p = gcd
 */
egcd(p: BN): {a: BN, b: BN, gcd: BN};

/**
 * Modular inverse of this number modulo another
 * @param {BN} modulus - The modulus
 * @returns {BN} New BN instance with the modular inverse
 */
invm(modulus: BN): BN;

Usage Examples:

const BN = require('bn.js');

const a = new BN('48');
const b = new BN('18');

// Greatest common divisor
const gcd = a.gcd(b);
console.log(gcd.toString()); // "6"

// Extended GCD
const extended = a.egcd(b);
console.log(extended.gcd.toString()); // "6"
console.log(extended.a.toString());   // "-1" 
console.log(extended.b.toString());   // "3"
// Verify: (-1)*48 + 3*18 = -48 + 54 = 6

// Modular inverse  
const x = new BN('7');
const mod = new BN('11');
const inverse = x.invm(mod);
console.log(inverse.toString()); // "8" (because 7*8 ≡ 1 (mod 11))