From e015a51c8054cf0cb705eed2b3b1f4734a3c7f86 Mon Sep 17 00:00:00 2001 From: Juanelas Date: Wed, 28 Jun 2023 19:38:17 +0200 Subject: [PATCH] modpow with crt; crt; modAdd; modMultiply; phi --- dist/bundle.esm.js | 4 +- dist/bundle.esm.min.js | 2 +- dist/bundle.iife.js | 2 +- dist/bundle.umd.js | 2 +- dist/index.browser.esm.js | 2 +- dist/index.d.ts | 41 +++++++++++- dist/index.node.cjs | 3 +- dist/index.node.esm.js | 3 +- docs/API.md | 127 +++++++++++++++++++++++++++++++++++++- src/ts/index.ts | 2 +- 10 files changed, 173 insertions(+), 15 deletions(-) diff --git a/dist/bundle.esm.js b/dist/bundle.esm.js index c93de14..b19af12 100644 --- a/dist/bundle.esm.js +++ b/dist/bundle.esm.js @@ -1,4 +1,4 @@ -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,i=0n;for(;0n!==n;){const u=t/n,f=t%n,g=r-o*u,c=e-i*u;t=n,n=f,r=o,e=i,o=g,i=c;}return {g:t,x:r,y:e}}function e(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 o(n,t){const o=r(e(n,t),t);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return e(o.x,t)}function i(n,t,r){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=r??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,r,u)=>{const f=i/r;return e(t+f*o(f,r)%i*n[u]%i,i)}),0n)}function u(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<=t?n:t}function c(n,t){return n>=t?t:n}function a(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function s(t,r,u,f){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof u&&(u=BigInt(u)),u<=0n)throw new RangeError("n must be > 0");if(1n===u)return 0n;if(t=e(t,u),r<0n)return o(s(t,n(r),u,f),u);if(void 0!==f)return function(n,t,r,e){const o=e.map((n=>n[0]**n[1])),u=e.map((n=>a([n]))),f=u.map(((r,e)=>s(n,t%r,o[e])));return i(f,o,r)}(t,r,u,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]))}(f));let g=1n;for(;r>0;)r%2n===1n&&(g=g*t%u),r/=2n,t=t**2n%u;return g} +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,i=0n;for(;0n!==n;){const u=t/n,f=t%n,g=r-o*u,c=e-i*u;t=n,n=f,r=o,e=i,o=g,i=c;}return {g:t,x:r,y:e}}function e(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 o(n,t){const o=r(e(n,t),t);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return e(o.x,t)}function i(n,t,r){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=r??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,r,u)=>{const f=i/r;return e(t+f*o(f,r)%i*n[u]%i,i)}),0n)}function u(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<=t?n:t}function c(n,t){return n>=t?t:n}function m(n,t){const r=BigInt(t);return e(n.map((n=>BigInt(n)%r)).reduce(((n,t)=>n+t%r),0n),r)}function p(n,t){const r=BigInt(t);return e(n.map((n=>BigInt(n)%r)).reduce(((n,t)=>n*t%r),1n),r)}function a(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function s(t,r,u,f){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof u&&(u=BigInt(u)),u<=0n)throw new RangeError("n must be > 0");if(1n===u)return 0n;if(t=e(t,u),r<0n)return o(s(t,n(r),u,f),u);if(void 0!==f)return function(n,t,r,e){const o=e.map((n=>n[0]**n[1])),u=e.map((n=>a([n]))),f=u.map(((r,e)=>s(n,t%r,o[e])));return i(f,o,r)}(t,r,u,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]))}(f));let g=1n;for(;r>0;)r%2n===1n&&(g=g*t%u),r/=2n,t=t**2n%u;return g} function fromBuffer(buf) { let ret = 0n; @@ -530,4 +530,4 @@ function primeSync(bitLength, iterations = 16) { return rnd; } -export { n as abs, t as bitLength, r as eGcd, u as gcd, isProbablyPrime, f as lcm, g as max, c as min, o as modInv, s as modPow, prime, primeSync, randBetween, randBits, randBitsSync, randBytes, randBytesSync, e as toZn }; +export { n as abs, t as bitLength, i as crt, r as eGcd, u as gcd, isProbablyPrime, f as lcm, g as max, c as min, m as modAdd, o as modInv, p as modMultiply, s as modPow, a as phi, prime, primeSync, randBetween, randBits, randBitsSync, randBytes, randBytesSync, e as toZn }; diff --git a/dist/bundle.esm.min.js b/dist/bundle.esm.min.js index a9f955c..6387d69 100644 --- a/dist/bundle.esm.min.js +++ b/dist/bundle.esm.min.js @@ -1 +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 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 s=t/n,u=t%n,a=e-o*s,c=r-i*s;t=n,n=u,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}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 e=n%t;return e<0n?e+t:e}function o(n,t){const o=e(r(n,t),t);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return r(o.x,t)}function i(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,s)=>{const u=i/e;return r(t+u*o(u,e)%i*n[s]%i,i)}),0n)}function s(t,e){let r="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof e?BigInt(n(e)):n(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<=t?n:t}function c(n,t){return n>=t?t:n}function f(t,e,s,u){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof s&&(s=BigInt(s)),s<=0n)throw new RangeError("n must be > 0");if(1n===s)return 0n;if(t=r(t,s),e<0n)return o(f(t,n(e),s,u),s);if(void 0!==u)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),s=r.map((n=>function(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}([n]))),u=s.map(((e,r)=>f(n,t%e,o[r])));return i(u,o,e)}(t,e,s,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(u));let a=1n;for(;e>0;)e%2n===1n&&(a=a*t%s),e/=2n,t=t**2n%s;return a}function g(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function m(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{m(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function b(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=d(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function h(n,e=1n){if(n<=e)throw new RangeError("Arguments MUST be: max > min");const r=n-e,o=t(r);let i;do{i=g(b(o))}while(i>r);return i+e}let w=!1;function p(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker(S());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function y(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}function $(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!w){let e=0n;do{e=g(b(n,!0))}while(!y(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=g(b(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=S();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=g(b(n,!0))}while(!y(e,t));return e}void 0!==self.Worker&&(w=!0);export{n as abs,t as bitLength,e as eGcd,s as gcd,p as isProbablyPrime,u as lcm,a as max,c as min,o as modInv,f as modPow,$ as prime,B as primeSync,h as randBetween,l as randBits,b as randBitsSync,m as randBytes,d as randBytesSync,r as toZn}; +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 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 s=t/n,u=t%n,a=e-o*s,c=r-i*s;t=n,n=u,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}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 e=n%t;return e<0n?e+t:e}function o(n,t){const o=e(r(n,t),t);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return r(o.x,t)}function i(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,s)=>{const u=i/e;return r(t+u*o(u,e)%i*n[s]%i,i)}),0n)}function s(t,e){let r="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof e?BigInt(n(e)):n(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<=t?n:t}function c(n,t){return n>=t?t:n}function f(n,t){const e=BigInt(t);return r(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n+t%e),0n),e)}function g(n,t){const e=BigInt(t);return r(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n*t%e),1n),e)}function m(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function d(t,e,s,u){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof s&&(s=BigInt(s)),s<=0n)throw new RangeError("n must be > 0");if(1n===s)return 0n;if(t=r(t,s),e<0n)return o(d(t,n(e),s,u),s);if(void 0!==u)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),s=r.map((n=>m([n]))),u=s.map(((e,r)=>d(n,t%e,o[r])));return i(u,o,e)}(t,e,s,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(u));let a=1n;for(;e>0;)e%2n===1n&&(a=a*t%s),e/=2n,t=t**2n%s;return a}function l(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function b(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{b(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function p(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=h(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function y(n,e=1n){if(n<=e)throw new RangeError("Arguments MUST be: max > min");const r=n-e,o=t(r);let i;do{i=l(p(o))}while(i>r);return i+e}let B=!1;function I(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker($());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function S(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}function R(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!B){let e=0n;do{e=l(p(n,!0))}while(!S(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=l(p(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=$();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=l(p(n,!0))}while(!S(e,t));return e}void 0!==self.Worker&&(B=!0);export{n as abs,t as bitLength,i as crt,e as eGcd,s as gcd,I as isProbablyPrime,u as lcm,a as max,c as min,f as modAdd,o as modInv,g as modMultiply,d as modPow,m as phi,R as prime,M as primeSync,y as randBetween,w as randBits,p as randBitsSync,b as randBytes,h as randBytesSync,r as toZn}; diff --git a/dist/bundle.iife.js b/dist/bundle.iife.js index 89e8812..2377ed2 100644 --- a/dist/bundle.iife.js +++ b/dist/bundle.iife.js @@ -1 +1 @@ -var bigintCryptoUtils=function(n){"use strict";function t(n){return n>=0?n:-n}function e(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 e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,s=t%n,a=e-o*u,c=r-i*u;t=n,n=s,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}function o(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 e=n%t;return e<0n?e+t:e}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)}function u(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const r=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,u)=>{const s=r/e;return o(t+s*i(s,e)%r*n[u]%r,r)}),0n)}function s(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");if(1n===r)return 0n;if(n=o(n,r),e<0n)return i(a(n,t(e),r,s),r);if(void 0!==s)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),i=r.map((n=>function(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}([n]))),s=i.map(((e,r)=>a(n,t%e,o[r])));return u(s,o,e)}(n,e,r,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(s));let c=1n;for(;e>0;)e%2n===1n&&(c=c*n%r),e/=2n,n=n**2n%r;return c}function c(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function f(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{f(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function d(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=g(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function l(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=c(d(o))}while(i>r);return i+t}let b=!1;function h(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker(p());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function w(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}return void 0!==self.Worker&&(b=!0),n.abs=t,n.bitLength=e,n.eGcd=r,n.gcd=s,n.isProbablyPrime=h,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/s(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=a,n.prime=function(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!b){let e=0n;do{e=c(d(n,!0))}while(!w(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=c(d(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=p();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=c(d(n,!0))}while(!w(e,t));return e},n.randBetween=l,n.randBits=m,n.randBitsSync=d,n.randBytes=f,n.randBytesSync=g,n.toZn=o,n}({}); +var bigintCryptoUtils=function(n){"use strict";function t(n){return n>=0?n:-n}function e(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 e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,s=t%n,c=e-o*u,a=r-i*u;t=n,n=s,e=o,r=i,o=c,i=a}return{g:t,x:e,y:r}}function o(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 e=n%t;return e<0n?e+t:e}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)}function u(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const r=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,u)=>{const s=r/e;return o(t+s*i(s,e)%r*n[u]%r,r)}),0n)}function s(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<n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function a(n,e,r,s){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof r&&(r=BigInt(r)),r<=0n)throw new RangeError("n must be > 0");if(1n===r)return 0n;if(n=o(n,r),e<0n)return i(a(n,t(e),r,s),r);if(void 0!==s)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),i=r.map((n=>c([n]))),s=i.map(((e,r)=>a(n,t%e,o[r])));return u(s,o,e)}(n,e,r,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(s));let f=1n;for(;e>0;)e%2n===1n&&(f=f*n%r),e/=2n,n=n**2n%r;return f}function f(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function g(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{g(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function l(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=m(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function b(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=f(l(o))}while(i>r);return i+t}let h=!1;function w(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker(y());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function p(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}return void 0!==self.Worker&&(h=!0),n.abs=t,n.bitLength=e,n.crt=u,n.eGcd=r,n.gcd=s,n.isProbablyPrime=w,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/s(n,e)*e)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modAdd=function(n,t){const e=BigInt(t);return o(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n+t%e),0n),e)},n.modInv=i,n.modMultiply=function(n,t){const e=BigInt(t);return o(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n*t%e),1n),e)},n.modPow=a,n.phi=c,n.prime=function(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!h){let e=0n;do{e=f(l(n,!0))}while(!p(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=f(l(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=y();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=f(l(n,!0))}while(!p(e,t));return e},n.randBetween=b,n.randBits=d,n.randBitsSync=l,n.randBytes=g,n.randBytesSync=m,n.toZn=o,n}({}); diff --git a/dist/bundle.umd.js b/dist/bundle.umd.js index a0d12ed..e745cb6 100644 --- a/dist/bundle.umd.js +++ b/dist/bundle.umd.js @@ -1 +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).bigintCryptoUtils={})}(this,(function(n){"use strict";function t(n){return n>=0?n:-n}function e(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 e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const s=t/n,u=t%n,c=e-o*s,a=r-i*s;t=n,n=u,e=o,r=i,o=c,i=a}return{g:t,x:e,y:r}}function o(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 e=n%t;return e<0n?e+t:e}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)}function s(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const r=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,s)=>{const u=r/e;return o(t+u*i(u,e)%r*n[s]%r,r)}),0n)}function u(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");if(1n===r)return 0n;if(n=o(n,r),e<0n)return i(c(n,t(e),r,u),r);if(void 0!==u)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),i=r.map((n=>function(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}([n]))),u=i.map(((e,r)=>c(n,t%e,o[r])));return s(u,o,e)}(n,e,r,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(u));let a=1n;for(;e>0;)e%2n===1n&&(a=a*n%r),e/=2n,n=n**2n%r;return a}function a(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function f(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{f(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function m(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=g(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function l(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=a(m(o))}while(i>r);return i+t}let b=!1;function h(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker(p());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function w(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}void 0!==self.Worker&&(b=!0),n.abs=t,n.bitLength=e,n.eGcd=r,n.gcd=u,n.isProbablyPrime=h,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/u(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=c,n.prime=function(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!b){let e=0n;do{e=a(m(n,!0))}while(!w(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=a(m(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=p();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=a(m(n,!0))}while(!w(e,t));return e},n.randBetween=l,n.randBits=d,n.randBitsSync=m,n.randBytes=f,n.randBytesSync=g,n.toZn=o})); +!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).bigintCryptoUtils={})}(this,(function(n){"use strict";function t(n){return n>=0?n:-n}function e(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 e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,s=t%n,c=e-o*u,a=r-i*u;t=n,n=s,e=o,r=i,o=c,i=a}return{g:t,x:e,y:r}}function o(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 e=n%t;return e<0n?e+t:e}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)}function u(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const r=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,u)=>{const s=r/e;return o(t+s*i(s,e)%r*n[u]%r,r)}),0n)}function s(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<n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function a(n,e,r,s){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof r&&(r=BigInt(r)),r<=0n)throw new RangeError("n must be > 0");if(1n===r)return 0n;if(n=o(n,r),e<0n)return i(a(n,t(e),r,s),r);if(void 0!==s)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),i=r.map((n=>c([n]))),s=i.map(((e,r)=>a(n,t%e,o[r])));return u(s,o,e)}(n,e,r,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(s));let f=1n;for(;e>0;)e%2n===1n&&(f=f*n%r),e/=2n,n=n**2n%r;return f}function f(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function g(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{g(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function l(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=d(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function b(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=f(l(o))}while(i>r);return i+t}let h=!1;function p(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker(y());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function w(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}void 0!==self.Worker&&(h=!0),n.abs=t,n.bitLength=e,n.crt=u,n.eGcd=r,n.gcd=s,n.isProbablyPrime=p,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/s(n,e)*e)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modAdd=function(n,t){const e=BigInt(t);return o(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n+t%e),0n),e)},n.modInv=i,n.modMultiply=function(n,t){const e=BigInt(t);return o(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n*t%e),1n),e)},n.modPow=a,n.phi=c,n.prime=function(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!h){let e=0n;do{e=f(l(n,!0))}while(!w(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=f(l(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=y();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=f(l(n,!0))}while(!w(e,t));return e},n.randBetween=b,n.randBits=m,n.randBitsSync=l,n.randBytes=g,n.randBytesSync=d,n.toZn=o})); diff --git a/dist/index.browser.esm.js b/dist/index.browser.esm.js index a9f955c..6387d69 100644 --- a/dist/index.browser.esm.js +++ b/dist/index.browser.esm.js @@ -1 +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 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 s=t/n,u=t%n,a=e-o*s,c=r-i*s;t=n,n=u,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}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 e=n%t;return e<0n?e+t:e}function o(n,t){const o=e(r(n,t),t);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return r(o.x,t)}function i(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,s)=>{const u=i/e;return r(t+u*o(u,e)%i*n[s]%i,i)}),0n)}function s(t,e){let r="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof e?BigInt(n(e)):n(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<=t?n:t}function c(n,t){return n>=t?t:n}function f(t,e,s,u){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof s&&(s=BigInt(s)),s<=0n)throw new RangeError("n must be > 0");if(1n===s)return 0n;if(t=r(t,s),e<0n)return o(f(t,n(e),s,u),s);if(void 0!==u)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),s=r.map((n=>function(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}([n]))),u=s.map(((e,r)=>f(n,t%e,o[r])));return i(u,o,e)}(t,e,s,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(u));let a=1n;for(;e>0;)e%2n===1n&&(a=a*t%s),e/=2n,t=t**2n%s;return a}function g(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function m(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{m(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function b(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=d(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function h(n,e=1n){if(n<=e)throw new RangeError("Arguments MUST be: max > min");const r=n-e,o=t(r);let i;do{i=g(b(o))}while(i>r);return i+e}let w=!1;function p(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker(S());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function y(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}function $(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!w){let e=0n;do{e=g(b(n,!0))}while(!y(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=g(b(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=S();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=g(b(n,!0))}while(!y(e,t));return e}void 0!==self.Worker&&(w=!0);export{n as abs,t as bitLength,e as eGcd,s as gcd,p as isProbablyPrime,u as lcm,a as max,c as min,o as modInv,f as modPow,$ as prime,B as primeSync,h as randBetween,l as randBits,b as randBitsSync,m as randBytes,d as randBytesSync,r as toZn}; +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 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 s=t/n,u=t%n,a=e-o*s,c=r-i*s;t=n,n=u,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}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 e=n%t;return e<0n?e+t:e}function o(n,t){const o=e(r(n,t),t);if(1n!==o.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return r(o.x,t)}function i(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,s)=>{const u=i/e;return r(t+u*o(u,e)%i*n[s]%i,i)}),0n)}function s(t,e){let r="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof e?BigInt(n(e)):n(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<=t?n:t}function c(n,t){return n>=t?t:n}function f(n,t){const e=BigInt(t);return r(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n+t%e),0n),e)}function g(n,t){const e=BigInt(t);return r(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n*t%e),1n),e)}function m(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function d(t,e,s,u){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof s&&(s=BigInt(s)),s<=0n)throw new RangeError("n must be > 0");if(1n===s)return 0n;if(t=r(t,s),e<0n)return o(d(t,n(e),s,u),s);if(void 0!==u)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),s=r.map((n=>m([n]))),u=s.map(((e,r)=>d(n,t%e,o[r])));return i(u,o,e)}(t,e,s,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(u));let a=1n;for(;e>0;)e%2n===1n&&(a=a*t%s),e/=2n,t=t**2n%s;return a}function l(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}function b(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){{const r=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(r);else for(let t=0;t 0");{const e=new Uint8Array(n);if(n<=65536)self.crypto.getRandomValues(e);else for(let t=0;t 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{b(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function p(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=h(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function y(n,e=1n){if(n<=e)throw new RangeError("Arguments MUST be: max > min");const r=n-e,o=t(r);let i;do{i=l(p(o))}while(i>r);return i+e}let B=!1;function I(n,t=16,e=!1){if("number"==typeof n&&(n=BigInt(n)),n<0n)throw RangeError("w MUST be >= 0");return new Promise(((e,r)=>{const o=new Worker($());o.onmessage=n=>{void 0!==n?.data?._bcu?.isPrime&&(o.terminate(),e(n.data._bcu.isPrime))},o.onmessageerror=n=>{r(n)};const i={_bcu:{rnd:n,iterations:t,id:0}};o.postMessage(i)}))}function S(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}function R(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!B){let e=0n;do{e=l(p(n,!0))}while(!S(e,t));return new Promise((n=>{n(e)}))}return new Promise(((e,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();e(r._bcu.value)}else{const e=l(p(n,!0));try{const n={_bcu:{rnd:e,iterations:t,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};{const n=$();for(let t=0;ti(n.data,t),o.push(t)}}for(let e=0;e 0");let e=0n;do{e=l(p(n,!0))}while(!S(e,t));return e}void 0!==self.Worker&&(B=!0);export{n as abs,t as bitLength,i as crt,e as eGcd,s as gcd,I as isProbablyPrime,u as lcm,a as max,c as min,f as modAdd,o as modInv,g as modMultiply,d as modPow,m as phi,R as prime,M as primeSync,y as randBetween,w as randBits,p as randBitsSync,b as randBytes,h as randBytesSync,r as toZn}; diff --git a/dist/index.d.ts b/dist/index.d.ts index 3e5f7a4..9ca045f 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -16,6 +16,20 @@ declare function abs(a: number | bigint): number | bigint; */ declare function bitLength(a: number | bigint): number; +/** + * Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer n by several integers, then one can determine uniquely the remainder of the division of n by the product of these integers, under the condition that the divisors are pairwise coprime (no two divisors share a common factor other than 1). Provided that n_i are pairwise coprime, and a_i any integers, this function returns a solution for the following system of equations: + x ≡ a_1 mod n_1 + x ≡ a_2 mod n_2 + ⋮ + x ≡ a_k mod n_k + * + * @param remainders the array of remainders a_i. For example [17n, 243n, 344n] + * @param modulos the array of modulos n_i. For example [769n, 2017n, 47701n] + * @param modulo the product of all modulos. Provided here just to save some operations if it is already known + * @returns x + */ +declare function crt(remainders: bigint[], modulos: bigint[], modulo?: bigint): bigint; + interface Egcd { g: bigint; x: bigint; @@ -73,6 +87,14 @@ declare function max(a: number | bigint, b: number | bigint): number | bigint; */ declare function min(a: number | bigint, b: number | bigint): number | bigint; +/** + * Modular addition of (a_1 + ... + a_r) mod n + * @param addends an array of the numbers a_i to add. For example [3, 12353251235n, 1243, -12341232545990n] + * @param n the modulo + * @returns The smallest positive integer that is congruent with (a_1 + ... + a_r) mod n + */ +declare function modAdd(addends: Array, n: number | bigint): bigint; + /** * Modular inverse. * @@ -85,6 +107,14 @@ declare function min(a: number | bigint, b: number | bigint): number | bigint; */ declare function modInv(a: number | bigint, n: number | bigint): bigint; +/** +* Modular addition of (a_1 * ... * a_r) mod n + * @param factors an array of the numbers a_i to multiply. For example [3, 12353251235n, 1243, -12341232545990n] + * @param n the modulo + * @returns The smallest positive integer that is congruent with (a_1 * ... * a_r) mod n + */ +declare function modMultiply(factors: Array, n: number | bigint): bigint; + type PrimePower = [number | bigint, number | bigint]; type PrimeFactor = number | bigint | PrimePower; /** @@ -101,6 +131,15 @@ type PrimeFactor = number | bigint | PrimePower; */ declare function modPow(b: number | bigint, e: number | bigint, n: number | bigint, primeFactorization?: PrimeFactor[]): bigint; +type PrimeFactorization = Array<[bigint, bigint]>; +/** + * A function that computes the Euler's totien function of a number n, whose prime power factorization is known + * + * @param primeFactorization an array of arrays containing the prime power factorization of a number n. For example, for n = (p1**k1)*(p2**k2)*...*(pr**kr), one should provide [[p1, k1], [p2, k2], ... , [pr, kr]] + * @returns phi((p1**k1)*(p2**k2)*...*(pr**kr)) + */ +declare function phi(primeFactorization: PrimeFactorization): bigint; + /** * Finds the smallest positive element that is congruent to a in modulo n * @@ -215,4 +254,4 @@ declare function randBytes(byteLength: number, forceLength?: boolean): Promise=0?n:-n}function e(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let e=1;do{e++}while((n>>=1n)>1n);return e}function t(n,e){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),n<=0n||e<=0n)throw new RangeError("a and b MUST be > 0");let t=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=e/n,s=e%n,c=t-o*u,f=r-i*u;e=n,n=s,t=o,r=i,o=c,i=f}return{g:e,x:t,y:r}}function r(n,e){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");const t=n%e;return t<0n?t+e:t}function o(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 i(n,e,t){if(n.length!==e.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=t??e.reduce(((n,e)=>n*e),1n);return e.reduce(((e,t,u)=>{const s=i/t;return r(e+s*o(s,t)%i*n[u]%i,i)}),0n)}function u(e,t){let r="number"==typeof e?BigInt(n(e)):n(e),o="number"==typeof t?BigInt(n(t)):n(t);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");if(1n===u)return 0n;if(e=r(e,u),t<0n)return o(s(e,n(t),u,c),u);if(void 0!==c)return function(n,e,t,r){const o=r.map((n=>n[0]**n[1])),u=r.map((n=>function(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,e)=>e*n),1n)}([n]))),c=u.map(((t,r)=>s(n,e%t,o[r])));return i(c,o,t)}(e,t,u,function(n){const e={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const t=String(n);void 0===e[t]?e[t]={p:BigInt(n),k:1n}:e[t].k+=1n}else{const t=String(n[0]);void 0===e[t]?e[t]={p:BigInt(n[0]),k:BigInt(n[1])}:e[t].k+=BigInt(n[1])}})),Object.values(e).map((n=>[n.p,n.k]))}(c));let f=1n;for(;t>0;)t%2n===1n&&(f=f*e%u),t/=2n,e=e**2n%u;return f}function c(n){let e=0n;for(const t of n.values()){e=(e<<8n)+BigInt(t)}return e}var f=require("crypto");function a(n,e=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(t,r){f.randomBytes(n,(function(n,o){null!==n&&r(n),e&&(o[0]=128|o[0]),t(o)}))}))}function g(n,e=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");{const t=f.randomBytes(n);return e&&(t[0]=128|t[0]),t}}function h(n,e=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const t=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{a(t,!1).then((function(t){if(0!==r&&(t[0]=t[0]&2**r-1),e){const n=0!==r?2**(r-1):128;t[0]=t[0]|n}n(t)}))}))}function d(n,e=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const t=g(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(t[0]=t[0]&2**r-1),e){const n=0!==r?2**(r-1):128;t[0]=t[0]|n}return t}function p(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=c(d(o))}while(i>r);return i+t}let l=!1;try{require("worker_threads"),l=!0}catch(r){console.log("[bigint-crypto-utils] WARNING:\nThis node version doesn't support worker_threads. You should enable them in order to greatly speedup the generation of big prime numbers.\n · With Node >=11 it is enabled by default (consider upgrading).\n · With Node 10, starting with 10.5.0, you can enable worker_threads at runtime executing node --experimental-worker ")}function b(n,e){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const t=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let e=0;e= 0");return new Promise(!t&&l?(t,r)=>{const o=new m.Worker(__filename);o.on("message",(n=>{void 0!==n?._bcu?.isPrime&&(o.terminate().catch(r),t(n._bcu.isPrime))})),o.on("error",r);const i={_bcu:{rnd:n,iterations:e,id:0}};o.postMessage(i)}:t=>{t(b(n,e))})},exports.lcm=function(e,t){return"number"==typeof e&&(e=BigInt(e)),"number"==typeof t&&(t=BigInt(t)),0n===e&&0n===t?BigInt(0):n(e/u(e,t)*t)},exports.max=function(n,e){return n>=e?n:e},exports.min=function(n,e){return n>=e?e:n},exports.modInv=o,exports.modPow=s,exports.prime=function(n,e=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!l){let t=0n;do{t=c(d(n,!0))}while(!b(t,e));return new Promise((n=>{n(t)}))}return new Promise(((t,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();t(r._bcu.value)}else{const t=c(d(n,!0));try{const n={_bcu:{rnd:t,iterations:e,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};for(let n=0;ni(e,n))),o.push(n)}for(let t=0;t 0");let t=0n;do{t=c(d(n,!0))}while(!b(t,e));return t},exports.randBetween=p,exports.randBits=h,exports.randBitsSync=d,exports.randBytes=a,exports.randBytesSync=g,exports.toZn=r; -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5janMiLCJzb3VyY2VzIjpbIi4uL25vZGVfbW9kdWxlcy9iaWdpbnQtbW9kLWFyaXRoL2Rpc3QvaW5kZXgubm9kZS5lc20uanMiLCIuLi9zcmMvdHMvZnJvbUJ1ZmZlci50cyIsIi4uL3NyYy90cy9yYW5kQnl0ZXMudHMiLCIuLi9zcmMvdHMvcmFuZEJpdHMudHMiLCIuLi9zcmMvdHMvcmFuZEJldHdlZW4udHMiLCIuLi9zcmMvdHMvd29ya2VyVXRpbHMudHMiLCIuLi9zcmMvdHMvaXNQcm9iYWJseVByaW1lLnRzIiwi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UsWUFBWSxDQUN4QkwsS0FBTSxDQUNKbkIsTUFDQU0sYUFDQWlCLEdBQUl0RSxJQUdWLElBQUcrRSxNQUFNbEQsRUFDVixHQUVMLDZCQWEyQkssRUFBbUJtQixFQUFxQixJQUNqRSxHQUFJbkIsRUFBWSxFQUFHLE1BQU0sSUFBSXJDLFdBQVcseUJBQ3hDLElBQUlrRCxFQUFNLEdBQ1YsR0FDRUEsRUFBTTVCLEVBQVdzQixFQUFhUCxHQUFXLFdBQ2pDaUIsRUFBaUJKLEVBQUtNLElBQ2hDLE9BQU9OLENBQ1QiLCJ4X2dvb2dsZV9pZ25vcmVMaXN0IjpbMF19 +"use strict";function n(n){return n>=0?n:-n}function e(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let e=1;do{e++}while((n>>=1n)>1n);return e}function t(n,e){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),n<=0n||e<=0n)throw new RangeError("a and b MUST be > 0");let t=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=e/n,s=e%n,c=t-o*u,f=r-i*u;e=n,n=s,t=o,r=i,o=c,i=f}return{g:e,x:t,y:r}}function r(n,e){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");const t=n%e;return t<0n?t+e:t}function o(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 i(n,e,t){if(n.length!==e.length)throw new RangeError("The remainders and modulos arrays should have the same length");const i=t??e.reduce(((n,e)=>n*e),1n);return e.reduce(((e,t,u)=>{const s=i/t;return r(e+s*o(s,t)%i*n[u]%i,i)}),0n)}function u(e,t){let r="number"==typeof e?BigInt(n(e)):n(e),o="number"==typeof t?BigInt(n(t)):n(t);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<n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,e)=>e*n),1n)}function c(e,t,u,f){if("number"==typeof e&&(e=BigInt(e)),"number"==typeof t&&(t=BigInt(t)),"number"==typeof u&&(u=BigInt(u)),u<=0n)throw new RangeError("n must be > 0");if(1n===u)return 0n;if(e=r(e,u),t<0n)return o(c(e,n(t),u,f),u);if(void 0!==f)return function(n,e,t,r){const o=r.map((n=>n[0]**n[1])),u=r.map((n=>s([n]))),f=u.map(((t,r)=>c(n,e%t,o[r])));return i(f,o,t)}(e,t,u,function(n){const e={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const t=String(n);void 0===e[t]?e[t]={p:BigInt(n),k:1n}:e[t].k+=1n}else{const t=String(n[0]);void 0===e[t]?e[t]={p:BigInt(n[0]),k:BigInt(n[1])}:e[t].k+=BigInt(n[1])}})),Object.values(e).map((n=>[n.p,n.k]))}(f));let a=1n;for(;t>0;)t%2n===1n&&(a=a*e%u),t/=2n,e=e**2n%u;return a}function f(n){let e=0n;for(const t of n.values()){e=(e<<8n)+BigInt(t)}return e}var a=require("crypto");function g(n,e=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(t,r){a.randomBytes(n,(function(n,o){null!==n&&r(n),e&&(o[0]=128|o[0]),t(o)}))}))}function p(n,e=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");{const t=a.randomBytes(n);return e&&(t[0]=128|t[0]),t}}function d(n,e=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const t=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{g(t,!1).then((function(t){if(0!==r&&(t[0]=t[0]&2**r-1),e){const n=0!==r?2**(r-1):128;t[0]=t[0]|n}n(t)}))}))}function h(n,e=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const t=p(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(t[0]=t[0]&2**r-1),e){const n=0!==r?2**(r-1):128;t[0]=t[0]|n}return t}function l(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=f(h(o))}while(i>r);return i+t}let m=!1;try{require("worker_threads"),m=!0}catch(r){console.log("[bigint-crypto-utils] WARNING:\nThis node version doesn't support worker_threads. You should enable them in order to greatly speedup the generation of big prime numbers.\n · With Node >=11 it is enabled by default (consider upgrading).\n · With Node 10, starting with 10.5.0, you can enable worker_threads at runtime executing node --experimental-worker ")}function b(n,e){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const t=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let e=0;e= 0");return new Promise(!t&&m?(t,r)=>{const o=new w.Worker(__filename);o.on("message",(n=>{void 0!==n?._bcu?.isPrime&&(o.terminate().catch(r),t(n._bcu.isPrime))})),o.on("error",r);const i={_bcu:{rnd:n,iterations:e,id:0}};o.postMessage(i)}:t=>{t(b(n,e))})},exports.lcm=function(e,t){return"number"==typeof e&&(e=BigInt(e)),"number"==typeof t&&(t=BigInt(t)),0n===e&&0n===t?BigInt(0):n(e/u(e,t)*t)},exports.max=function(n,e){return n>=e?n:e},exports.min=function(n,e){return n>=e?e:n},exports.modAdd=function(n,e){const t=BigInt(e);return r(n.map((n=>BigInt(n)%t)).reduce(((n,e)=>n+e%t),0n),t)},exports.modInv=o,exports.modMultiply=function(n,e){const t=BigInt(e);return r(n.map((n=>BigInt(n)%t)).reduce(((n,e)=>n*e%t),1n),t)},exports.modPow=c,exports.phi=s,exports.prime=function(n,e=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!m){let t=0n;do{t=f(h(n,!0))}while(!b(t,e));return new Promise((n=>{n(t)}))}return new Promise(((t,r)=>{const o=[],i=(r,i)=>{if(r._bcu.isPrime){for(let n=0;n0;)o.pop();t(r._bcu.value)}else{const t=f(h(n,!0));try{const n={_bcu:{rnd:t,iterations:e,id:r._bcu.id}};i.postMessage(n)}catch(n){}}};for(let n=0;ni(e,n))),o.push(n)}for(let t=0;t 0");let t=0n;do{t=f(h(n,!0))}while(!b(t,e));return t},exports.randBetween=l,exports.randBits=d,exports.randBitsSync=h,exports.randBytes=g,exports.randBytesSync=p,exports.toZn=r; diff --git a/dist/index.node.esm.js b/dist/index.node.esm.js index 7b96b95..e1e2052 100644 --- a/dist/index.node.esm.js +++ b/dist/index.node.esm.js @@ -1,2 +1 @@ -import{fileURLToPath as n}from"url";function t(n){return n>=0?n:-n}function e(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 e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,s=t%n,a=e-o*u,c=r-i*u;t=n,n=s,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}function o(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 e=n%t;return e<0n?e+t:e}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)}function u(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const r=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,u)=>{const s=r/e;return o(t+s*i(s,e)%r*n[u]%r,r)}),0n)}function s(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<=t?n:t}function f(n,t){return n>=t?t:n}function g(n,e,r,s){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof r&&(r=BigInt(r)),r<=0n)throw new RangeError("n must be > 0");if(1n===r)return 0n;if(n=o(n,r),e<0n)return i(g(n,t(e),r,s),r);if(void 0!==s)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),i=r.map((n=>function(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}([n]))),s=i.map(((e,r)=>g(n,t%e,o[r])));return u(s,o,e)}(n,e,r,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(s));let a=1n;for(;e>0;)e%2n===1n&&(a=a*n%r),e/=2n,n=n**2n%r;return a}function m(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}var h=await import("crypto");function l(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){h.randomBytes(n,(function(n,o){null!==n&&r(n),t&&(o[0]=128|o[0]),e(o)}))}))}function b(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");{const e=h.randomBytes(n);return t&&(e[0]=128|e[0]),e}}function d(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{l(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function w(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=b(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function p(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=m(w(o))}while(i>r);return i+t}let y=!1;try{await import("worker_threads"),y=!0}catch(o){console.log("[bigint-crypto-utils] WARNING:\nThis node version doesn't support worker_threads. You should enable them in order to greatly speedup the generation of big prime numbers.\n · With Node >=11 it is enabled by default (consider upgrading).\n · With Node 10, starting with 10.5.0, you can enable worker_threads at runtime executing node --experimental-worker ")}function B(t,e=16,r=!1){if("number"==typeof t&&(t=BigInt(t)),t<0n)throw RangeError("w MUST be >= 0");return new Promise(!r&&y?(r,o)=>{const i=new _.Worker(n(import.meta.url));i.on("message",(n=>{void 0!==n?._bcu?.isPrime&&(i.terminate().catch(o),r(n._bcu.isPrime))})),i.on("error",o);const u={_bcu:{rnd:t,iterations:e,id:0}};i.postMessage(u)}:n=>{n(I(t,e))})}function I(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t 0");if(!y){let n=0n;do{n=m(w(t,!0))}while(!I(n,e));return new Promise((t=>{t(n)}))}return new Promise(((r,o)=>{const i=[],u=(n,o)=>{if(n._bcu.isPrime){for(let n=0;n0;)i.pop();r(n._bcu.value)}else{const r=m(w(t,!0));try{const t={_bcu:{rnd:r,iterations:e,id:n._bcu.id}};o.postMessage(t)}catch(n){}}};for(let t=0;tu(n,t))),i.push(t)}for(let n=0;n 0");let e=0n;do{e=m(w(n,!0))}while(!I(e,t));return e}export{t as abs,e as bitLength,r as eGcd,s as gcd,B as isProbablyPrime,a as lcm,c as max,f as min,i as modInv,g as modPow,E as prime,R as primeSync,p as randBetween,d as randBits,w as randBitsSync,l as randBytes,b as randBytesSync,o as toZn}; -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5lc20uanMiLCJzb3VyY2VzIjpbIi4uL25vZGVfbW9kdWxlcy9iaWdpbnQtbW9kLWFyaXRoL2Rpc3QvaW5kZXgubm9kZS5lc20uanMiLCIuLi9zcmMvdHMvZnJvbUJ1ZmZlci50cyIsIi4uL3NyYy90cy9yYW5kQnl0ZXMudHMiLCIuLi9zcmMvdHMvcmFuZEJpdHMudHMiLCIuLi9zcmMvdHMvcmFuZEJldHdlZW4udHMiLCIuLi9zcmMvdHMvd29ya2VyVXRpbHMudHMiLCIuLi9zcmMvdHMvaXNQcm9iYWJseVByaW1lLnRzIiwiLi4vc3JjL3RzL3ByaW1lLnRzIl0sInNvdXJjZXNDb250ZW50IjpudWxsLCJuYW1lcyI6WyJuIiwidCIsIkJpZ0ludCIsInIiLCJSYW5nZUVycm9yIiwiZSIsIm8iLCJpIiwidSIsImYiLCJnIiwiYyIsIngiLCJ5IiwidG9TdHJpbmciLCJsZW5ndGgiLCJyZWR1Y2UiLCJzIiwibWFwIiwiYSIsImZv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LENBQ0poQixNQUNBTSxhQUNBZSxHQUFJcEUsSUFHVixJQUFHa0UsTUFBTXJDLEVBQ1YsR0FFTCxVQWFnQjZELEVBQVd4RCxFQUFtQm1CLEVBQXFCLElBQ2pFLEdBQUluQixFQUFZLEVBQUcsTUFBTSxJQUFJckMsV0FBVyx5QkFDeEMsSUFBSWtELEVBQU0sR0FDVixHQUNFQSxFQUFNNUIsRUFBV3NCLEVBQWFQLEdBQVcsV0FDakNvQyxFQUFpQnZCLEVBQUtNLElBQ2hDLE9BQU9OLENBQ1QiLCJ4X2dvb2dsZV9pZ25vcmVMaXN0IjpbMF19 +import{fileURLToPath as n}from"url";function t(n){return n>=0?n:-n}function e(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 e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,s=t%n,a=e-o*u,c=r-i*u;t=n,n=s,e=o,r=i,o=a,i=c}return{g:t,x:e,y:r}}function o(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 e=n%t;return e<0n?e+t:e}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)}function u(n,t,e){if(n.length!==t.length)throw new RangeError("The remainders and modulos arrays should have the same length");const r=e??t.reduce(((n,t)=>n*t),1n);return t.reduce(((t,e,u)=>{const s=r/e;return o(t+s*i(s,e)%r*n[u]%r,r)}),0n)}function s(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<=t?n:t}function f(n,t){return n>=t?t:n}function g(n,t){const e=BigInt(t);return o(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n+t%e),0n),e)}function m(n,t){const e=BigInt(t);return o(n.map((n=>BigInt(n)%e)).reduce(((n,t)=>n*t%e),1n),e)}function h(n){return n.map((n=>n[0]**(n[1]-1n)*(n[0]-1n))).reduce(((n,t)=>t*n),1n)}function d(n,e,r,s){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof r&&(r=BigInt(r)),r<=0n)throw new RangeError("n must be > 0");if(1n===r)return 0n;if(n=o(n,r),e<0n)return i(d(n,t(e),r,s),r);if(void 0!==s)return function(n,t,e,r){const o=r.map((n=>n[0]**n[1])),i=r.map((n=>h([n]))),s=i.map(((e,r)=>d(n,t%e,o[r])));return u(s,o,e)}(n,e,r,function(n){const t={};return n.forEach((n=>{if("bigint"==typeof n||"number"==typeof n){const e=String(n);void 0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{const e=String(n[0]);void 0===t[e]?t[e]={p:BigInt(n[0]),k:BigInt(n[1])}:t[e].k+=BigInt(n[1])}})),Object.values(t).map((n=>[n.p,n.k]))}(s));let a=1n;for(;e>0;)e%2n===1n&&(a=a*n%r),e/=2n,n=n**2n%r;return a}function l(n){let t=0n;for(const e of n.values()){t=(t<<8n)+BigInt(e)}return t}var b=await import("crypto");function p(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");return new Promise((function(e,r){b.randomBytes(n,(function(n,o){null!==n&&r(n),t&&(o[0]=128|o[0]),e(o)}))}))}function w(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");{const e=b.randomBytes(n);return t&&(e[0]=128|e[0]),e}}function y(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=Math.ceil(n/8),r=n%8;return new Promise(((n,o)=>{p(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}function B(n,t=!1){if(n<1)throw new RangeError("bitLength MUST be > 0");const e=w(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){const n=0!==r?2**(r-1):128;e[0]=e[0]|n}return e}function I(n,t=1n){if(n<=t)throw new RangeError("Arguments MUST be: max > min");const r=n-t,o=e(r);let i;do{i=l(B(o))}while(i>r);return i+t}let _=!1;try{await import("worker_threads"),_=!0}catch(o){console.log("[bigint-crypto-utils] WARNING:\nThis node version doesn't support worker_threads. You should enable them in order to greatly speedup the generation of big prime numbers.\n · With Node >=11 it is enabled by default (consider upgrading).\n · With Node 10, starting with 10.5.0, you can enable worker_threads at runtime executing node --experimental-worker ")}function v(t,e=16,r=!1){if("number"==typeof t&&(t=BigInt(t)),t<0n)throw RangeError("w MUST be >= 0");return new Promise(!r&&_?(r,o)=>{const i=new E.Worker(n(import.meta.url));i.on("message",(n=>{void 0!==n?._bcu?.isPrime&&(i.terminate().catch(o),r(n._bcu.isPrime))})),i.on("error",o);const u={_bcu:{rnd:t,iterations:e,id:0}};i.postMessage(u)}:n=>{n(M(t,e))})}function M(n,t){if(2n===n)return!0;if(0n===(1n&n)||1n===n)return!1;const e=[3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n,103n,107n,109n,113n,127n,131n,137n,139n,149n,151n,157n,163n,167n,173n,179n,181n,191n,193n,197n,199n,211n,223n,227n,229n,233n,239n,241n,251n,257n,263n,269n,271n,277n,281n,283n,293n,307n,311n,313n,317n,331n,337n,347n,349n,353n,359n,367n,373n,379n,383n,389n,397n,401n,409n,419n,421n,431n,433n,439n,443n,449n,457n,461n,463n,467n,479n,487n,491n,499n,503n,509n,521n,523n,541n,547n,557n,563n,569n,571n,577n,587n,593n,599n,601n,607n,613n,617n,619n,631n,641n,643n,647n,653n,659n,661n,673n,677n,683n,691n,701n,709n,719n,727n,733n,739n,743n,751n,757n,761n,769n,773n,787n,797n,809n,811n,821n,823n,827n,829n,839n,853n,857n,859n,863n,877n,881n,883n,887n,907n,911n,919n,929n,937n,941n,947n,953n,967n,971n,977n,983n,991n,997n,1009n,1013n,1019n,1021n,1031n,1033n,1039n,1049n,1051n,1061n,1063n,1069n,1087n,1091n,1093n,1097n,1103n,1109n,1117n,1123n,1129n,1151n,1153n,1163n,1171n,1181n,1187n,1193n,1201n,1213n,1217n,1223n,1229n,1231n,1237n,1249n,1259n,1277n,1279n,1283n,1289n,1291n,1297n,1301n,1303n,1307n,1319n,1321n,1327n,1361n,1367n,1373n,1381n,1399n,1409n,1423n,1427n,1429n,1433n,1439n,1447n,1451n,1453n,1459n,1471n,1481n,1483n,1487n,1489n,1493n,1499n,1511n,1523n,1531n,1543n,1549n,1553n,1559n,1567n,1571n,1579n,1583n,1597n];for(let t=0;t 0");if(!_){let n=0n;do{n=l(B(t,!0))}while(!M(n,e));return new Promise((t=>{t(n)}))}return new Promise(((r,o)=>{const i=[],u=(n,o)=>{if(n._bcu.isPrime){for(let n=0;n0;)i.pop();r(n._bcu.value)}else{const r=l(B(t,!0));try{const t={_bcu:{rnd:r,iterations:e,id:n._bcu.id}};o.postMessage(t)}catch(n){}}};for(let t=0;tu(n,t))),i.push(t)}for(let n=0;n 0");let e=0n;do{e=l(B(n,!0))}while(!M(e,t));return e}export{t as abs,e as bitLength,u as crt,r as eGcd,s as gcd,v as isProbablyPrime,a as lcm,c as max,f as min,g as modAdd,i as modInv,m as modMultiply,d as modPow,h as phi,S as prime,P as primeSync,I as randBetween,y as randBits,B as randBitsSync,p as randBytes,w as randBytesSync,o as toZn}; diff --git a/docs/API.md b/docs/API.md index e42499b..6fb5546 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2,18 +2,32 @@ ## Table of contents +### Interfaces + +- [Egcd](interfaces/Egcd.md) + +### Type Aliases + +- [PrimeFactor](API.md#primefactor) +- [PrimeFactorization](API.md#primefactorization) +- [PrimePower](API.md#primepower) + ### Functions - [abs](API.md#abs) - [bitLength](API.md#bitlength) +- [crt](API.md#crt) - [eGcd](API.md#egcd) - [gcd](API.md#gcd) - [isProbablyPrime](API.md#isprobablyprime) - [lcm](API.md#lcm) - [max](API.md#max) - [min](API.md#min) +- [modAdd](API.md#modadd) - [modInv](API.md#modinv) +- [modMultiply](API.md#modmultiply) - [modPow](API.md#modpow) +- [phi](API.md#phi) - [prime](API.md#prime) - [primeSync](API.md#primesync) - [randBetween](API.md#randbetween) @@ -23,6 +37,24 @@ - [randBytesSync](API.md#randbytessync) - [toZn](API.md#tozn) +## Type Aliases + +### PrimeFactor + +Ƭ **PrimeFactor**: `number` \| `bigint` \| [`PrimePower`](API.md#primepower) + +___ + +### PrimeFactorization + +Ƭ **PrimeFactorization**: [`bigint`, `bigint`][] + +___ + +### PrimePower + +Ƭ **PrimePower**: [`number` \| `bigint`, `number` \| `bigint`] + ## Functions ### abs @@ -65,9 +97,35 @@ The bit length ___ +### crt + +▸ **crt**(`remainders`, `modulos`, `modulo?`): `bigint` + +Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer n by several integers, then one can determine uniquely the remainder of the division of n by the product of these integers, under the condition that the divisors are pairwise coprime (no two divisors share a common factor other than 1). Provided that n_i are pairwise coprime, and a_i any integers, this function returns a solution for the following system of equations: + x ≡ a_1 mod n_1 + x ≡ a_2 mod n_2 + ⋮ + x ≡ a_k mod n_k + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `remainders` | `bigint`[] | the array of remainders a_i. For example [17n, 243n, 344n] | +| `modulos` | `bigint`[] | the array of modulos n_i. For example [769n, 2017n, 47701n] | +| `modulo?` | `bigint` | the product of all modulos. Provided here just to save some operations if it is already known | + +#### Returns + +`bigint` + +x + +___ + ### eGcd -▸ **eGcd**(`a`, `b`): `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). @@ -85,7 +143,7 @@ RangeError if a or b are <= 0 #### Returns -`Egcd` +[`Egcd`](interfaces/Egcd.md) A triple (g, x, y), such that ax + by = g = gcd(a, b). @@ -202,6 +260,27 @@ Minimum of numbers a and b ___ +### modAdd + +▸ **modAdd**(`addends`, `n`): `bigint` + +Modular addition of (a_1 + ... + a_r) mod n + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `addends` | (`number` \| `bigint`)[] | an array of the numbers a_i to add. For example [3, 12353251235n, 1243, -12341232545990n] | +| `n` | `number` \| `bigint` | the modulo | + +#### Returns + +`bigint` + +The smallest positive integer that is congruent with (a_1 + ... + a_r) mod n + +___ + ### modInv ▸ **modInv**(`a`, `n`): `bigint` @@ -227,6 +306,28 @@ The inverse modulo n ___ +### modMultiply + +▸ **modMultiply**(`factors`, `n`): `bigint` + +Modular addition of (a_1 * ... * a_r) mod n + * + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `factors` | (`number` \| `bigint`)[] | an array of the numbers a_i to multiply. For example [3, 12353251235n, 1243, -12341232545990n] * | +| `n` | `number` \| `bigint` | the modulo * | + +#### Returns + +`bigint` + +The smallest positive integer that is congruent with (a_1 * ... * a_r) mod n + +___ + ### modPow ▸ **modPow**(`b`, `e`, `n`, `primeFactorization?`): `bigint` @@ -244,7 +345,7 @@ RangeError if n <= 0 | `b` | `number` \| `bigint` | base | | `e` | `number` \| `bigint` | exponent | | `n` | `number` \| `bigint` | modulo | -| `primeFactorization?` | `PrimeFactor`[] | an array of the prime factors, for example [5n, 5n, 13n, 27n], or prime powers as [p, k], for instance [[5, 2], [13, 1], [27, 1]]. If the prime factorization is provided the chinese remainder theorem is used to greatly speed up the exponentiation. | +| `primeFactorization?` | [`PrimeFactor`](API.md#primefactor)[] | an array of the prime factors, for example [5n, 5n, 13n, 27n], or prime powers as [p, k], for instance [[5, 2], [13, 1], [27, 1]]. If the prime factorization is provided the chinese remainder theorem is used to greatly speed up the exponentiation. | #### Returns @@ -254,6 +355,26 @@ b**e mod n ___ +### phi + +▸ **phi**(`primeFactorization`): `bigint` + +A function that computes the Euler's totien function of a number n, whose prime power factorization is known + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `primeFactorization` | [`PrimeFactorization`](API.md#primefactorization) | an array of arrays containing the prime power factorization of a number n. For example, for n = (p1**k1)*(p2**k2)*...*(pr**kr), one should provide [[p1, k1], [p2, k2], ... , [pr, kr]] | + +#### Returns + +`bigint` + +phi((p1**k1)*(p2**k2)*...*(pr**kr)) + +___ + ### prime ▸ **prime**(`bitLength`, `iterations?`): `Promise`<`bigint`\> diff --git a/src/ts/index.ts b/src/ts/index.ts index 3157d7e..29e543f 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -1,4 +1,4 @@ -export { abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn } from 'bigint-mod-arith' +export * from 'bigint-mod-arith' export { isProbablyPrime } from './isProbablyPrime.js' export { prime, primeSync } from './prime.js'