252 lines
12 KiB
JavaScript
252 lines
12 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 (minimum) length of a number expressed in bits.
|
|
*
|
|
* @param a
|
|
* @returns The bit length
|
|
*/
|
|
function bitLength(a) {
|
|
if (typeof a === 'number')
|
|
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
|
|
*
|
|
* @throws {@link RangeError} if a or b are <= 0
|
|
*
|
|
* @returns A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
|
*/
|
|
function eGcd(a, b) {
|
|
if (typeof a === 'number')
|
|
a = BigInt(a);
|
|
if (typeof b === 'number')
|
|
b = BigInt(b);
|
|
if (a <= 0n || b <= 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 (a !== 0n) {
|
|
const q = b / a;
|
|
const r = b % a;
|
|
const m = x - (u * q);
|
|
const n = y - (v * q);
|
|
b = a;
|
|
a = r;
|
|
x = u;
|
|
y = v;
|
|
u = m;
|
|
v = n;
|
|
}
|
|
return {
|
|
g: b,
|
|
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 = (typeof a === 'number') ? BigInt(abs(a)) : abs(a);
|
|
let bAbs = (typeof b === 'number') ? BigInt(abs(b)) : 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) {
|
|
if (typeof a === 'number')
|
|
a = BigInt(a);
|
|
if (typeof b === 'number')
|
|
b = BigInt(b);
|
|
if (a === 0n && b === 0n)
|
|
return BigInt(0);
|
|
// return abs(a * b) as bigint / gcd(a, b)
|
|
return abs((a / gcd(a, b)) * b);
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*
|
|
* @remarks
|
|
* a and b must be the same type, either number or bigint
|
|
*
|
|
* @param a - An integer
|
|
* @param n - The modulo
|
|
*
|
|
* @throws {@link RangeError} if n <= 0
|
|
*
|
|
* @returns A bigint with the smallest positive representation of a modulo n
|
|
*/
|
|
function toZn(a, n) {
|
|
if (typeof a === 'number')
|
|
a = BigInt(a);
|
|
if (typeof n === 'number')
|
|
n = BigInt(n);
|
|
if (n <= 0n) {
|
|
throw new RangeError('n must be > 0');
|
|
}
|
|
const aZn = a % n;
|
|
return (aZn < 0n) ? aZn + n : aZn;
|
|
}
|
|
|
|
/**
|
|
* Modular inverse.
|
|
*
|
|
* @param a The number to find an inverse for
|
|
* @param n The modulo
|
|
*
|
|
* @throws {@link RangeError} if a does not have inverse modulo n
|
|
*
|
|
* @returns The inverse modulo n
|
|
*/
|
|
function modInv(a, n) {
|
|
const egcd = eGcd(toZn(a, n), n);
|
|
if (egcd.g !== 1n) {
|
|
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
|
|
}
|
|
else {
|
|
return toZn(egcd.x, n);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
|
*
|
|
* @param b base
|
|
* @param e exponent
|
|
* @param n modulo
|
|
*
|
|
* @throws {@link RangeError} if n <= 0
|
|
*
|
|
* @returns b**e mod n
|
|
*/
|
|
function modPow(b, e, n) {
|
|
if (typeof b === 'number')
|
|
b = BigInt(b);
|
|
if (typeof e === 'number')
|
|
e = BigInt(e);
|
|
if (typeof n === 'number')
|
|
n = BigInt(n);
|
|
if (n <= 0n) {
|
|
throw new RangeError('n must be > 0');
|
|
}
|
|
else if (n === 1n) {
|
|
return 0n;
|
|
}
|
|
b = toZn(b, n);
|
|
if (e < 0n) {
|
|
return modInv(modPow(b, abs(e), n), n);
|
|
}
|
|
let r = 1n;
|
|
while (e > 0) {
|
|
if ((e % 2n) === 1n) {
|
|
r = r * b % n;
|
|
}
|
|
e = e / 2n;
|
|
b = b ** 2n % n;
|
|
}
|
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5janMiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90cy9hYnMudHMiLCIuLi8uLi9zcmMvdHMvYml0TGVuZ3RoLnRzIiwiLi4vLi4vc3JjL3RzL2VnY2QudHMiLCIuLi8uLi9zcmMvdHMvZ2NkLnRzIiwiLi4vLi4vc3JjL3RzL2xjbS50cyIsIi4uLy4uL3NyYy90cy9tYXgudHMiLCIuLi8uLi9zcmMvdHMvbWluLnRzIiwiLi4vLi4vc3JjL3RzL3RvWm4udHMiLCIuLi8uLi9zcmMvdHMvbW9kSW52LnRzIiwiLi4vLi4vc3JjL3RzL21vZFBvdy50cyJdLCJzb3VyY2VzQ29udGVudCI6bnVsbCwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztBQUFBOzs7Ozs7QUFNRztBQUNHLFNBQVUsR0FBRyxDQUFFLENBQWdCLEVBQUE7QUFDbkMsSUFBQSxPQUFPLENBQUMsQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUE7QUFDMUI7O0FDVEE7Ozs7O0FBS0c7QUFDRyxTQUFVLFNBQVMsQ0FBRSxDQUFnQixFQUFBO0lBQ3pDLElBQUksT0FBTyxDQUFDLEtBQUssUUFBUTtBQUFFLFFBQUEsQ0FBQyxHQUFHLE1BQU0sQ0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