bigint-mod-arith/README.md

4.6 KiB

bigint-mod-arith

Some extra functions to work with modular arithmetics using native JS (stage 3) implementation of BigInt.

Usage examples

const modArith = require('bigint-mod-arith');

let a = 5n;
let b = 2n;
let n = 19n;

console.log(modArith.modPow(a, b, n)); // prints 13

console.log(modArith.modInv(2n, 5n)); // prints 3

console.log(modArith.modInv(3n, 5n)); // prints 2

Functions

abs(a)bigint

Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0

gcd(a, b)bigint

Greatest-common divisor of two integers based on the iterative binary algorithm.

lcm(a, b)bigint

The least common multiple computed as abs(a*b)/gcd(a,b)

toZn(a, n)bigint

Finds the smallest positive element that is congruent to a in modulo n

eGcd(a, b)egcdReturn

An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).

modInv(a, n)bigint

Modular inverse.

modPow(a, b, n)bigint

Modular exponentiation a**b mod n

Typedefs

egcdReturn : Object

A triple (g, x, y), such that ax + by = g = gcd(a, b).

abs(a) ⇒ bigint

Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0

Kind: global function
Returns: bigint - the absolute value of a

Param Type
a number | bigint

gcd(a, b) ⇒ bigint

Greatest-common divisor of two integers based on the iterative binary algorithm.

Kind: global function
Returns: bigint - The greatest common divisor of a and b

Param Type
a number | bigint
b number | bigint

lcm(a, b) ⇒ bigint

The least common multiple computed as abs(a*b)/gcd(a,b)

Kind: global function
Returns: bigint - The least common multiple of a and b

Param Type
a number | bigint
b number | bigint

toZn(a, n) ⇒ bigint

Finds the smallest positive element that is congruent to a in modulo n

Kind: global function
Returns: bigint - The smallest positive representation of a in modulo n

Param Type Description
a number | bigint An integer
n number | bigint The modulo

eGcd(a, b) ⇒ egcdReturn

An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).

Kind: global function

Param Type
a number | bigint
b number | bigint

modInv(a, n) ⇒ bigint

Modular inverse.

Kind: global function
Returns: bigint - the inverse modulo n

Param Type Description
a number | bigint The number to find an inverse for
n number | bigint The modulo

modPow(a, b, n) ⇒ bigint

Modular exponentiation a**b mod n

Kind: global function
Returns: bigint - a**b mod n

Param Type Description
a number | bigint base
b number | bigint exponent
n number | bigint modulo

egcdReturn : Object

A triple (g, x, y), such that ax + by = g = gcd(a, b).

Kind: global typedef
Properties

Name Type
g bigint
x bigint
y bigint