bigint-mod-arith/dist/index.node.cjs

3 lines
8.7 KiB
JavaScript
Raw Normal View History

"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,c=r-o*u,p=e-i*u;t=n,n=f,r=o,e=i,o=c,i=p}return{g:t,x:r,y:e}}function r(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 e(n,e){const o=t(r(n,e),e);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${e.toString()}`);return r(o.x,e)}function o(n,t,o){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=o??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,o,u)=>{const f=i/o;return r(t+f*e(f,o)%i*n[u]%i,i)}),0n)}function i(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<<i}function u(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function f(t,i,c,p){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof i&&(i=BigInt(i)),"number"==typeof c&&(c=BigInt(c)),c<=0n)throw new RangeError("n must be > 0");if(1n===c)return 0n;if(t=r(t,c),i<0n)return e(f(t,n(i),c,p),c);if(void 0!==p)return function(n,t,r,e){const i=e.map((n=>n[0]**n[1])),c=e.map((n=>u([n]))),p=c.map(((r,e)=>f(n,t%r,i[e])));return o(p,i,r)}(t,i,c,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const r=String(n);void 0===t[r]?t[r]={p:BigInt(n),k:1n}:t[r].k+=1n}else{const r=String(n[0]);void 0===t[r]?t[r]={p:BigInt(n[0]),k:BigInt(n[1])}:t[r].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(p));let s=1n;for(;i>0;)i%2n===1n&&(s=s*t%c),i/=2n,t=t**2n%c;return s}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.crt=o,exports.eGcd=t,exports.gcd=i,exports.lcm=function(t,r){return"number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),0n===t&&0n===r?BigInt(0):n(t/i(t,r)*r)},exports.max=function(n,t){return n>=t?n:t},exports.min=function(n,t){return n>=t?t:n},exports.modAdd=function(n,t){const e=BigInt(t);return r(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n+t%e),0n),e)},exports.modInv=e,exports.modMultiply=function(n,t){const e=BigInt(t);return r(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n*t%e),1n),e)},exports.modPow=f,exports.phi=u,exports.toZn=r;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5janMiLCJzb3VyY2VzIjpbIi4uL3NyYy90cy9hYnMudHMiLCIuLi9zcmMvdHMvZWdjZC50cyIsIi4uL3NyYy90cy90b1puLnRzIiwiLi4vc3JjL3RzL21vZEludi50cyIsIi4uL3NyYy90cy9jcnQudHMiLCIuLi9zcmMvdHMvZ2NkLnRzIiwiLi4vc3JjL3RzL3BoaS50cyIsIi4uL3NyYy90cy9tb2RQb3cudHMiLCIuLi9zcmMvdHMvYml0TGVuZ3RoLnRzIiwiLi4vc3JjL3RzL2xjbS50cyIsIi4uL3NyYy90cy9tYXgudHMiLCIuLi9zcm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