232 lines
10 KiB
JavaScript
232 lines
10 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, '__esModule', { value: true });
|
||
|
|
||
|
/**
|
||
|
* Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
|
||
|
*
|
||
|
* @param a
|
||
|
*
|
||
|
* @returns The absolute value of a
|
||
|
*/
|
||
|
function abs(a) {
|
||
|
return (a >= 0) ? a : -a;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the bitlength of a number
|
||
|
*
|
||
|
* @param a
|
||
|
* @returns The bit length
|
||
|
*/
|
||
|
function bitLength(a) {
|
||
|
a = BigInt(a);
|
||
|
if (a === 1n) {
|
||
|
return 1;
|
||
|
}
|
||
|
let bits = 1;
|
||
|
do {
|
||
|
bits++;
|
||
|
} while ((a >>= 1n) > 1n);
|
||
|
return bits;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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).
|
||
|
*
|
||
|
* @param a
|
||
|
* @param b
|
||
|
*
|
||
|
* @returns A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
||
|
*/
|
||
|
function eGcd(a, b) {
|
||
|
let aBigint = BigInt(a);
|
||
|
let bBigInt = BigInt(b);
|
||
|
if (aBigint <= 0n || bBigInt <= 0n)
|
||
|
throw new RangeError('a and b MUST be > 0'); // a and b MUST be positive
|
||
|
let x = 0n;
|
||
|
let y = 1n;
|
||
|
let u = 1n;
|
||
|
let v = 0n;
|
||
|
while (aBigint !== 0n) {
|
||
|
const q = bBigInt / aBigint;
|
||
|
const r = bBigInt % aBigint;
|
||
|
const m = x - (u * q);
|
||
|
const n = y - (v * q);
|
||
|
bBigInt = aBigint;
|
||
|
aBigint = r;
|
||
|
x = u;
|
||
|
y = v;
|
||
|
u = m;
|
||
|
v = n;
|
||
|
}
|
||
|
return {
|
||
|
g: bBigInt,
|
||
|
x: x,
|
||
|
y: y
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Greatest-common divisor of two integers based on the iterative binary algorithm.
|
||
|
*
|
||
|
* @param a
|
||
|
* @param b
|
||
|
*
|
||
|
* @returns The greatest common divisor of a and b
|
||
|
*/
|
||
|
function gcd(a, b) {
|
||
|
let aAbs = BigInt(abs(a));
|
||
|
let bAbs = BigInt(abs(b));
|
||
|
if (aAbs === 0n) {
|
||
|
return bAbs;
|
||
|
}
|
||
|
else if (bAbs === 0n) {
|
||
|
return aAbs;
|
||
|
}
|
||
|
let shift = 0n;
|
||
|
while (((aAbs | bAbs) & 1n) === 0n) {
|
||
|
aAbs >>= 1n;
|
||
|
bAbs >>= 1n;
|
||
|
shift++;
|
||
|
}
|
||
|
while ((aAbs & 1n) === 0n)
|
||
|
aAbs >>= 1n;
|
||
|
do {
|
||
|
while ((bAbs & 1n) === 0n)
|
||
|
bAbs >>= 1n;
|
||
|
if (aAbs > bAbs) {
|
||
|
const x = aAbs;
|
||
|
aAbs = bAbs;
|
||
|
bAbs = x;
|
||
|
}
|
||
|
bAbs -= aAbs;
|
||
|
} while (bAbs !== 0n);
|
||
|
// rescale
|
||
|
return aAbs << shift;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* The least common multiple computed as abs(a*b)/gcd(a,b)
|
||
|
* @param a
|
||
|
* @param b
|
||
|
*
|
||
|
* @returns The least common multiple of a and b
|
||
|
*/
|
||
|
function lcm(a, b) {
|
||
|
const aBigInt = BigInt(a);
|
||
|
const bBigInt = BigInt(b);
|
||
|
if (aBigInt === 0n && bBigInt === 0n)
|
||
|
return BigInt(0);
|
||
|
return abs(aBigInt * bBigInt) / gcd(aBigInt, bBigInt);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
|
||
|
*
|
||
|
* @param a
|
||
|
* @param b
|
||
|
*
|
||
|
* @returns Maximum of numbers a and b
|
||
|
*/
|
||
|
function max(a, b) {
|
||
|
return (a >= b) ? a : b;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
|
||
|
*
|
||
|
* @param a
|
||
|
* @param b
|
||
|
*
|
||
|
* @returns Minimum of numbers a and b
|
||
|
*/
|
||
|
function min(a, b) {
|
||
|
return (a >= b) ? b : a;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds the smallest positive element that is congruent to a in modulo n
|
||
|
* @param {number|bigint} a An integer
|
||
|
* @param {number|bigint} n The modulo
|
||
|
*
|
||
|
* @returns The smallest positive representation of a in modulo n or number NaN if n < 0
|
||
|
*/
|
||
|
function toZn(a, n) {
|
||
|
const nBigInt = BigInt(n);
|
||
|
if (n <= 0) {
|
||
|
return NaN;
|
||
|
}
|
||
|
const aZn = BigInt(a) % nBigInt;
|
||
|
return (aZn < 0n) ? aZn + nBigInt : aZn;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Modular inverse.
|
||
|
*
|
||
|
* @param a The number to find an inverse for
|
||
|
* @param n The modulo
|
||
|
*
|
||
|
* @returns The inverse modulo n or number NaN if it does not exist
|
||
|
*/
|
||
|
function modInv(a, n) {
|
||
|
try {
|
||
|
const egcd = eGcd(toZn(a, n), n);
|
||
|
if (egcd.g !== 1n) {
|
||
|
return NaN; // modular inverse does not exist
|
||
|
}
|
||
|
else {
|
||
|
return toZn(egcd.x, n);
|
||
|
}
|
||
|
}
|
||
|
catch (error) {
|
||
|
return NaN;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
||
|
*
|
||
|
* @param b base
|
||
|
* @param e exponent
|
||
|
* @param n modulo
|
||
|
*
|
||
|
* @returns b**e mod n or number NaN if n <= 0
|
||
|
*/
|
||
|
function modPow(b, e, n) {
|
||
|
const nBigInt = BigInt(n);
|
||
|
if (nBigInt <= 0n) {
|
||
|
return NaN;
|
||
|
}
|
||
|
else if (nBigInt === 1n) {
|
||
|
return BigInt(0);
|
||
|
}
|
||
|
let bZn = toZn(b, nBigInt);
|
||
|
e = BigInt(e);
|
||
|
if (e < 0n) {
|
||
|
return modInv(modPow(bZn, abs(e), nBigInt), nBigInt);
|
||
|
}
|
||
|
let r = 1n;
|
||
|
while (e > 0) {
|
||
|
if ((e % 2n) === 1n) {
|
||
|
r = (r * bZn) % nBigInt;
|
||
|
}
|
||
|
e = e / 2n;
|
||
|
bZn = bZn ** 2n % nBigInt;
|
||
|
}
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
exports.abs = abs;
|
||
|
exports.bitLength = bitLength;
|
||
|
exports.eGcd = eGcd;
|
||
|
exports.gcd = gcd;
|
||
|
exports.lcm = lcm;
|
||
|
exports.max = max;
|
||
|
exports.min = min;
|
||
|
exports.modInv = modInv;
|
||
|
exports.modPow = modPow;
|
||
|
exports.toZn = toZn;
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5janMiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90cy9hYnMudHMiLCIuLi8uLi9zcmMvdHMvYml0TGVuZ3RoLnRzIiwiLi4vLi4vc3JjL3RzL2VnY2QudHMiLCIuLi8uLi9zcmMvdHMvZ2NkLnRzIiwiLi4vLi4vc3JjL3RzL2xjbS50cyIsIi4uLy4uL3NyYy90cy9tYXgudHMiLCIuLi8uLi9zcmMvdHMvbWluLnRzIiwiLi4vLi4vc3JjL3RzL3RvWm4udHMiLCIuLi8uLi9zcmMvdHMvbW9kSW52LnRzIiwiLi4vLi4vc3JjL3RzL21vZFBvdy50cyJdLCJzb3VyY2VzQ29udGVudCI6bnVsbCwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztBQUFBOzs7Ozs7O1NBT2dCLEdBQUcsQ0FBRSxDQUFnQjtJQUNuQyxPQUFPLENBQUMsQ0FBQyxJQUFJLENB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