diff --git a/dist/bundle.esm.js b/dist/bundle.esm.js new file mode 100644 index 0000000..14d0a19 --- /dev/null +++ b/dist/bundle.esm.js @@ -0,0 +1,146 @@ +function abs(a) { + return (a >= 0) ? a : -a; +} + +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; +} + +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'); + 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, + y + }; +} + +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); + return aAbs << shift; +} + +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 / gcd(a, b)) * b); +} + +function max(a, b) { + return (a >= b) ? a : b; +} + +function min(a, b) { + return (a >= b) ? b : a; +} + +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; +} + +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()}`); + } + else { + return toZn(egcd.x, 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; +} + +export { abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn }; diff --git a/dist/bundle.esm.min.js b/dist/bundle.esm.min.js new file mode 100644 index 0000000..1e96a87 --- /dev/null +++ b/dist/bundle.esm.min.js @@ -0,0 +1 @@ +function n(n){return n>=0?n:-n}function t(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,u=0n;for(;0n!==n;){const i=t/n,f=t%n,g=r-o*i,b=e-u*i;t=n,n=f,r=o,e=u,o=g,u=b}return{g:t,x:r,y:e}}function e(t,r){let e="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof r?BigInt(n(r)):n(r);if(0n===e)return o;if(0n===o)return e;let u=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,u++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e<=t?n:t}function i(n,t){return n>=t?t:n}function f(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)throw new RangeError("n must be > 0");const r=n%t;return r<0n?r+t:r}function g(n,t){const e=r(f(n,t),t);if(1n!==e.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return f(e.x,t)}function b(t,r,e){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");if(1n===e)return 0n;if(t=f(t,e),r<0n)return g(b(t,n(r),e),e);let o=1n;for(;r>0;)r%2n===1n&&(o=o*t%e),r/=2n,t=t**2n%e;return o}export{n as abs,t as bitLength,r as eGcd,e as gcd,o as lcm,u as max,i as min,g as modInv,b as modPow,f as toZn}; diff --git a/dist/bundle.iife.js b/dist/bundle.iife.js new file mode 100644 index 0000000..8d3d12c --- /dev/null +++ b/dist/bundle.iife.js @@ -0,0 +1 @@ +var bigintModArith=function(n){"use strict";function t(n){return n>=0?n:-n}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,f=t%n,g=r-o*u,m=e-i*u;t=n,n=f,r=o,e=i,o=g,i=m}return{g:t,x:r,y:e}}function e(n,r){let e="number"==typeof n?BigInt(t(n)):t(n),o="number"==typeof r?BigInt(t(r)):t(r);if(0n===e)return o;if(0n===o)return e;let i=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,i++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e< 0");const r=n%t;return r<0n?r+t:r}function i(n,t){const e=r(o(n,t),t);if(1n!==e.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return o(e.x,t)}return n.abs=t,n.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t},n.eGcd=r,n.gcd=e,n.lcm=function(n,r){return"number"==typeof n&&(n=BigInt(n)),"number"==typeof r&&(r=BigInt(r)),0n===n&&0n===r?BigInt(0):t(n/e(n,r)*r)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modInv=i,n.modPow=function n(r,e,u){if("number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof u&&(u=BigInt(u)),u<=0n)throw new RangeError("n must be > 0");if(1n===u)return 0n;if(r=o(r,u),e<0n)return i(n(r,t(e),u),u);let f=1n;for(;e>0;)e%2n===1n&&(f=f*r%u),e/=2n,r=r**2n%u;return f},n.toZn=o,n}({}); diff --git a/dist/bundle.umd.js b/dist/bundle.umd.js new file mode 100644 index 0000000..8973ac7 --- /dev/null +++ b/dist/bundle.umd.js @@ -0,0 +1 @@ +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).bigintModArith={})}(this,(function(n){"use strict";function t(n){return n>=0?n:-n}function e(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const f=t/n,u=t%n,g=e-o*f,b=r-i*f;t=n,n=u,e=o,r=i,o=g,i=b}return{g:t,x:e,y:r}}function r(n,e){let r="number"==typeof n?BigInt(t(n)):t(n),o="number"==typeof e?BigInt(t(e)):t(e);if(0n===r)return o;if(0n===o)return r;let i=0n;for(;0n===(1n&(r|o));)r>>=1n,o>>=1n,i++;for(;0n===(1n&r);)r>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(r>o){const n=r;r=o,o=n}o-=r}while(0n!==o);return r< 0");const e=n%t;return e<0n?e+t:e}function i(n,t){const r=e(o(n,t),t);if(1n!==r.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return o(r.x,t)}n.abs=t,n.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t},n.eGcd=e,n.gcd=r,n.lcm=function(n,e){return"number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),0n===n&&0n===e?BigInt(0):t(n/r(n,e)*e)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modInv=i,n.modPow=function n(e,r,f){if("number"==typeof e&&(e=BigInt(e)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof f&&(f=BigInt(f)),f<=0n)throw new RangeError("n must be > 0");if(1n===f)return 0n;if(e=o(e,f),r<0n)return i(n(e,t(r),f),f);let u=1n;for(;r>0;)r%2n===1n&&(u=u*e%f),r/=2n,e=e**2n%f;return u},n.toZn=o})); diff --git a/dist/index.browser.esm.js b/dist/index.browser.esm.js new file mode 100644 index 0000000..6e5131c --- /dev/null +++ b/dist/index.browser.esm.js @@ -0,0 +1,2 @@ +function n(n){return n>=0?n:-n}function t(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,u=0n;for(;0n!==n;){const i=t/n,f=t%n,g=r-o*i,b=e-u*i;t=n,n=f,r=o,e=u,o=g,u=b}return{g:t,x:r,y:e}}function e(t,r){let e="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof r?BigInt(n(r)):n(r);if(0n===e)return o;if(0n===o)return e;let u=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,u++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e<=t?n:t}function i(n,t){return n>=t?t:n}function f(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)throw new RangeError("n must be > 0");const r=n%t;return r<0n?r+t:r}function g(n,t){const e=r(f(n,t),t);if(1n!==e.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return f(e.x,t)}function b(t,r,e){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");if(1n===e)return 0n;if(t=f(t,e),r<0n)return g(b(t,n(r),e),e);let o=1n;for(;r>0;)r%2n===1n&&(o=o*t%e),r/=2n,t=t**2n%e;return o}export{n as abs,t as bitLength,r as eGcd,e as gcd,o as lcm,u as max,i as min,g as modInv,b as modPow,f as toZn}; +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguYnJvd3Nlci5lc20uanMiLCJzb3VyY2VzIjpbIi4uL3NyYy90cy9hYnMudHMiLCIuLi9zcmMvdHMvYml0TGVuZ3RoLnRzIiwiLi4vc3JjL3RzL2VnY2QudHMiLCIuLi9zcmMvdHMvZ2NkLnRzIiwiLi4vc3JjL3RzL2xjbS50cyIsIi4uL3NyYy90cy9tYXgudHMiLCIuLi9zcmMvdHMvbWluLnRzIiwiLi4vc3JjL3RzL3RvWm4udHMiLCIuLi9zcmMvdHMvbW9kSW52LnRzIiwiLi4vc3JjL3RzL21vZFBvdy50cyJdLCJzb3VyY2VzQ29udGVudCI6bnVsbCwibmFtZXMiOlsiYWJzIiwiYSIsImJpdExlbmd0aCIsIkJpZ0ludCIsImJpdHMiLCJlR2NkIiwiYiIsIlJhbmdlRXJyb3IiLCJ4IiwieSIsInUiLCJ2IiwicSIsInIiLCJtIiwibiIsImciLCJnY2QiLCJhQWJzIiwiYkFicyIsInNoaWZ0IiwibGNtIiwibWF4IiwibWluIiwidG9abiIsImFabiIsIm1vZEludiIsImVnY2QiLCJ0b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BQUljLEVBQ2hCLE9BQVFVLEVBQU0sR0FBTUEsRUFBTVYsRUFBSVUsQ0FDaEMsQ0NYZ0IsU0FBQUMsRUFBUXpCLEVBQW9CYyxHQUMxQyxNQUFNWSxFQUFPdEIsRUFBS21CLEVBQUt2QixFQUFHYyxHQUFJQSxHQUM5QixHQUFlLEtBQVhZLEVBQUtYLEVBQ1AsTUFBTSxJQUFJVCxXQUFXLEdBQUdOLEVBQUUyQiwyQ0FBMkNiLEVBQUVhLGNBRXZFLE9BQU9KLEVBQUtHLEVBQUtuQixFQUFHTyxFQUV4QixVQ0xnQmMsRUFBUXZCLEVBQW9Cd0IsRUFBb0JmLEdBSzlELEdBSmlCLGlCQUFOVCxJQUFnQkEsRUFBSUgsT0FBT0csSUFDckIsaUJBQU53QixJQUFnQkEsRUFBSTNCLE9BQU8yQixJQUNyQixpQkFBTmYsSUFBZ0JBLEVBQUlaLE9BQU9ZLElBRWxDQSxHQUFLLEdBQ1AsTUFBTSxJQUFJUixXQUFXLGlCQUNoQixHQUFVLEtBQU5RLEVBQ1QsT0FBTyxHQUtULEdBRkFULEVBQUlrQixFQUFLbEIsRUFBR1MsR0FFUmUsRUFBSSxHQUNOLE9BQU9KLEVBQU9HLEVBQU92QixFQUFHTixFQUFJOEIsR0FBSWYsR0FBSUEsR0FHdEMsSUFBSUYsRUFBSSxHQUNSLEtBQU9pQixFQUFJLEdBQ0pBLEVBQUksS0FBUSxLQUNmakIsRUFBSUEsRUFBSVAsRUFBSVMsR0FFZGUsR0FBUSxHQUNSeEIsRUFBSUEsR0FBSyxHQUFLUyxFQUVoQixPQUFPRixDQUNUIn0= diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..39c5a6f --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,26 @@ +declare function abs(a: number | bigint): number | bigint; + +declare function bitLength(a: number | bigint): number; + +interface Egcd { + g: bigint; + x: bigint; + y: bigint; +} +declare function eGcd(a: number | bigint, b: number | bigint): Egcd; + +declare function gcd(a: number | bigint, b: number | bigint): bigint; + +declare function lcm(a: number | bigint, b: number | bigint): bigint; + +declare function max(a: number | bigint, b: number | bigint): number | bigint; + +declare function min(a: number | bigint, b: number | bigint): number | bigint; + +declare function modInv(a: number | bigint, n: number | bigint): bigint; + +declare function modPow(b: number | bigint, e: number | bigint, n: number | bigint): bigint; + +declare function toZn(a: number | bigint, n: number | bigint): bigint; + +export { Egcd, abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn }; diff --git a/dist/index.node.cjs b/dist/index.node.cjs new file mode 100644 index 0000000..5a5ad99 --- /dev/null +++ b/dist/index.node.cjs @@ -0,0 +1,2 @@ +"use strict";function n(n){return n>=0?n:-n}function t(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,f=t%n,g=r-o*u,p=e-i*u;t=n,n=f,r=o,e=i,o=g,i=p}return{g:t,x:r,y:e}}function r(t,r){let e="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof r?BigInt(n(r)):n(r);if(0n===e)return o;if(0n===o)return e;let i=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,i++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e< 0");const r=n%t;return r<0n?r+t:r}function o(n,r){const o=t(e(n,r),r);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${r.toString()}`);return e(o.x,r)}exports.abs=n,exports.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t},exports.eGcd=t,exports.gcd=r,exports.lcm=function(t,e){return"number"==typeof t&&(t=BigInt(t)),"number"==typeof e&&(e=BigInt(e)),0n===t&&0n===e?BigInt(0):n(t/r(t,e)*e)},exports.max=function(n,t){return n>=t?n:t},exports.min=function(n,t){return n>=t?t:n},exports.modInv=o,exports.modPow=function t(r,i,u){if("number"==typeof r&&(r=BigInt(r)),"number"==typeof i&&(i=BigInt(i)),"number"==typeof u&&(u=BigInt(u)),u<=0n)throw new RangeError("n must be > 0");if(1n===u)return 0n;if(r=e(r,u),i<0n)return o(t(r,n(i),u),u);let f=1n;for(;i>0;)i%2n===1n&&(f=f*r%u),i/=2n,r=r**2n%u;return f},exports.toZn=e; +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5janMiLCJzb3VyY2VzIjpbIi4uL3NyYy90cy9hYnMudHMiLCIuLi9zcmMvdHMvZWdjZC50cyIsIi4uL3NyYy90cy9nY2QudHMiLCIuLi9zcmMvdHMvdG9abi50cyIsIi4uL3NyYy90cy9tb2RJbnYudHMiLCIuLi9zcmMvdHMvYml0TGVuZ3RoLnRzIiwiLi4vc3JjL3RzL2xjbS50cyIsIi4uL3NyYy90cy9tYXgudHMiLCIuLi9zcmMvdHMvbWluLnRzIiwiLi4vc3JjL3RzL21vZFBvdy50cyJdLCJzb3VyY2VzQ29udGVudCI6bnVsbCwibmFtZXMiOlsiYWJzIiwiYSIsImVHY2QiLCJiIiwiQmlnSW50IiwiUmFuZ2VFcnJvciIsIngiLCJ5IiwidSIsInYiLCJxIiwiciIsIm0iLCJuIiwiZyIsImdjZCIsImFBYnMiLCJiQWJzIiwic2hpZnQiLCJ0b1puIiwiYVpuIiwibW9kSW52IiwiZWdjZCIsInRvU3RyaW5nIiwiYml0cyIsIm1vZFBvdyIsImUiXSwibWFwcGluZ3MiOiJhQU9NLFNBQVVBLEVBQUtDLEdBQ25CLE9BQVFBLEdBQUssRUFBS0EsR0FBS0EsQ0FDekIsQ0NPZ0IsU0FBQUMsRUFBTUQsRUFBb0JFLEdBSXhDLEdBSGlCLGlCQUFORixJQUFnQkEsRUFBSUcsT0FBT0gsSUFDckIsa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JFLEdBQ3ZDLE9BQVFGLEdBQUtFLEVBQUtBLEVBQUlGLENBQ3hCLDJDQ0lnQndCLEVBQVF0QixFQUFvQnVCLEVBQW9CYixHQUs5RCxHQUppQixpQkFBTlYsSUFBZ0JBLEVBQUlDLE9BQU9ELElBQ3JCLGlCQUFOdUIsSUFBZ0JBLEVBQUl0QixPQUFPc0IsSUFDckIsaUJBQU5iLElBQWdCQSxFQUFJVCxPQUFPUyxJQUVsQ0EsR0FBSyxHQUNQLE1BQU0sSUFBSVIsV0FBVyxpQkFDaEIsR0FBVSxLQUFOUSxFQUNULE9BQU8sR0FLVCxHQUZBVixFQUFJZ0IsRUFBS2hCLEVBQUdVLEdBRVJhLEVBQUksR0FDTixPQUFPTCxFQUFPSSxFQUFPdEIsRUFBR0gsRUFBSTBCLEdBQUliLEdBQUlBLEdBR3RDLElBQUlGLEVBQUksR0FDUixLQUFPZSxFQUFJLEdBQ0pBLEVBQUksS0FBUSxLQUNmZixFQUFJQSxFQUFJUixFQUFJVSxHQUVkYSxHQUFRLEdBQ1J2QixFQUFJQSxHQUFLLEdBQUtVLEVBRWhCLE9BQU9GLENBQ1QifQ== diff --git a/dist/index.node.esm.js b/dist/index.node.esm.js new file mode 100644 index 0000000..c0704d6 --- /dev/null +++ b/dist/index.node.esm.js @@ -0,0 +1,2 @@ +function n(n){return n>=0?n:-n}function t(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,u=0n;for(;0n!==n;){const i=t/n,f=t%n,g=r-o*i,b=e-u*i;t=n,n=f,r=o,e=u,o=g,u=b}return{g:t,x:r,y:e}}function e(t,r){let e="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof r?BigInt(n(r)):n(r);if(0n===e)return o;if(0n===o)return e;let u=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,u++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e<=t?n:t}function i(n,t){return n>=t?t:n}function f(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)throw new RangeError("n must be > 0");const r=n%t;return r<0n?r+t:r}function g(n,t){const e=r(f(n,t),t);if(1n!==e.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return f(e.x,t)}function b(t,r,e){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");if(1n===e)return 0n;if(t=f(t,e),r<0n)return g(b(t,n(r),e),e);let o=1n;for(;r>0;)r%2n===1n&&(o=o*t%e),r/=2n,t=t**2n%e;return o}export{n as abs,t as bitLength,r as eGcd,e as gcd,o as lcm,u as max,i as min,g as modInv,b as modPow,f as toZn}; +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5lc20uanMiLCJzb3VyY2VzIjpbIi4uL3NyYy90cy9hYnMudHMiLCIuLi9zcmMvdHMvYml0TGVuZ3RoLnRzIiwiLi4vc3JjL3RzL2VnY2QudHMiLCIuLi9zcmMvdHMvZ2NkLnRzIiwiLi4vc3JjL3RzL2xjbS50cyIsIi4uL3NyYy90cy9tYXgudHMiLCIuLi9zcmMvdHMvbWluLnRzIiwiLi4vc3JjL3RzL3RvWm4udHMiLCIuLi9zcmMvdHMvbW9kSW52LnRzIiwiLi4vc3JjL3RzL21vZFBvdy50cyJdLCJzb3VyY2VzQ29udGVudCI6bnVsbCwibmFtZXMiOlsiYWJzIiwiYSIsImJpdExlbmd0aCIsIkJpZ0ludCIsImJpdHMiLCJlR2NkIiwiYiIsIlJhbmdlRXJyb3IiLCJ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null +++ b/docs/.nojekyll @@ -0,0 +1 @@ +TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false. \ No newline at end of file diff --git a/docs/API.md b/docs/API.md new file mode 100644 index 0000000..16bb64f --- /dev/null +++ b/docs/API.md @@ -0,0 +1,292 @@ +# bigint-mod-arith - v3.2.1 + +Some common functions for modular arithmetic using native JS implementation of BigInt + +## Table of contents + +### Interfaces + +- [Egcd](interfaces/Egcd.md) + +### Functions + +- [abs](API.md#abs) +- [bitLength](API.md#bitlength) +- [eGcd](API.md#egcd) +- [gcd](API.md#gcd) +- [lcm](API.md#lcm) +- [max](API.md#max) +- [min](API.md#min) +- [modInv](API.md#modinv) +- [modPow](API.md#modpow) +- [toZn](API.md#tozn) + +## Functions + +### abs + +▸ **abs**(`a`): `number` \| `bigint` + +Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `a` | `number` \| `bigint` | + +#### Returns + +`number` \| `bigint` + +The absolute value of a + +#### Defined in + +[abs.ts:8](https://github.com/juanelas/bigint-mod-arith/blob/84ebe88/src/ts/abs.ts#L8) + +___ + +### bitLength + +▸ **bitLength**(`a`): `number` + +Returns the (minimum) length of a number expressed in bits. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `a` | `number` \| `bigint` | + +#### Returns + +`number` + +The bit length + +#### Defined in + +[bitLength.ts:7](https://github.com/juanelas/bigint-mod-arith/blob/84ebe88/src/ts/bitLength.ts#L7) + +___ + +### eGcd + +▸ **eGcd**(`a`, `b`): [`Egcd`](interfaces/Egcd.md) + +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). + +**`Throws`** + +RangeError if a or b are <= 0 + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `a` | `number` \| `bigint` | +| `b` | `number` \| `bigint` | + +#### Returns + +[`Egcd`](interfaces/Egcd.md) + +A triple (g, x, y), such that ax + by = g = gcd(a, b). + +#### Defined in + +[egcd.ts:17](https://github.com/juanelas/bigint-mod-arith/blob/84ebe88/src/ts/egcd.ts#L17) + +___ + +### gcd + +▸ **gcd**(`a`, `b`): `bigint` + +Greatest common divisor of two integers based on the iterative binary algorithm. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `a` | `number` \| `bigint` | +| `b` | `number` \| `bigint` | + +#### Returns + +`bigint` + +The greatest common divisor of a and b + +#### Defined in + +[gcd.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/84ebe88/src/ts/gcd.ts#L10) + +___ + +### lcm + +▸ **lcm**(`a`, `b`): `bigint` + +The least common multiple computed as abs(a*b)/gcd(a,b) + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `a` | `number` \| `bigint` | +| `b` | `number` \| `bigint` | + +#### Returns + +`bigint` + +The least common multiple of a and b + +#### Defined in + +[lcm.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/84ebe88/src/ts/lcm.ts#L10) + +___ + +### max + +▸ **max**(`a`, `b`): `number` \| `bigint` + +Maximum. max(a,b)==a if a>=b. max(a,b)==b if a=b. min(a,b)==a if a