import{fileURLToPathasn}from"url";functiont(n){returnn>=0?n:-n}functione(n){if("number"==typeofn&&(n=BigInt(n)),1n===n)return1;lett=1;do{t++}while((n>>=1n)>1n);returnt}functionr(n,t){if("number"==typeofn&&(n=BigInt(n)),"number"==typeoft&&(t=BigInt(t)),n<=0n||t<=0n)thrownewRangeError("a and b MUST be > 0");lete=0n,r=1n,o=1n,i=0n;for(;0n!==n;){constu=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}}functiono(n,t){if("number"==typeofn&&(n=BigInt(n)),"number"==typeoft&&(t=BigInt(t)),t<=0n)thrownewRangeError("n must be > 0");conste=n%t;returne<0n?e+t:e}functioni(n,t){conste=r(o(n,t),t);if(1n!==e.g)thrownewRangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);returno(e.x,t)}functionu(n,t,e){if(n.length!==t.length)thrownewRangeError("The remainders and modulos arrays should have the same length");constr=e??t.reduce(((n,t)=>n*t),1n);returnt.reduce(((t,e,u)=>{consts=r/e;returno(t+s*i(s,e)%r*n[u]%r,r)}),0n)}functions(n,e){letr="number"==typeofn?BigInt(t(n)):t(n),o="number"==typeofe?BigInt(t(e)):t(e);if(0n===r)returno;if(0n===o)returnr;leti=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){constn=r;r=o,o=n}o-=r}while(0n!==o);returnr<<i}functiona(n,e){return"number"==typeofn&&(n=BigInt(n)),"number"==typeofe&&(e=BigInt(e)),0n===n&&0n===e?BigInt(0):t(n/s(n,e)*e)}functionc(n,t){returnn>=t?n:t}functionf(n,t){returnn>=t?t:n}functiong(n,e,r,s){if("number"==typeofn&&(n=BigInt(n)),"number"==typeofe&&(e=BigInt(e)),"number"==typeofr&&(r=BigInt(r)),r<=0n)thrownewRangeError("n must be > 0");if(1n===r)return0n;if(n=o(n,r),e<0n)returni(g(n,t(e),r,s),r);if(void0!==s)returnfunction(n,t,e,r){consto=r.map((n=>n[0]**n[1])),i=r.map((n=>function(n){returnn.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])));returnu(s,o,e)}(n,e,r,function(n){constt={};returnn.forEach((n=>{if("bigint"==typeofn||"number"==typeofn){conste=String(n);void0===t[e]?t[e]={p:BigInt(n),k:1n}:t[e].k+=1n}else{conste=String(n[0]);void0===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));leta=1n;for(;e>0;)e%2n===1n&&(a=a*n%r),e/=2n,n=n**2n%r;returna}functionm(n){lett=0n;for(consteofn.values()){t=(t<<8n)+BigInt(e)}returnt}varh=awaitimport("crypto");functionl(n,t=!1){if(n<1)thrownewRangeError("byteLength MUST be > 0");returnnewPromise((function(e,r){h.randomBytes(n,(function(n,o){null!==n&&r(n),t&&(o[0]=128|o[0]),e(o)}))}))}functionb(n,t=!1){if(n<1)thrownewRangeError("byteLength MUST be > 0");{conste=h.randomBytes(n);returnt&&(e[0]=128|e[0]),e}}functiond(n,t=!1){if(n<1)thrownewRangeError("bitLength MUST be > 0");conste=Math.ceil(n/8),r=n%8;returnnewPromise(((n,o)=>{l(e,!1).then((function(e){if(0!==r&&(e[0]=e[0]&2**r-1),t){constn=0!==r?2**(r-1):128;e[0]=e[0]|n}n(e)}))}))}functionw(n,t=!1){if(n<1)thrownewRangeError("bitLength MUST be > 0");conste=b(Math.ceil(n/8),!1),r=n%8;if(0!==r&&(e[0]=e[0]&2**r-1),t){constn=0!==r?2**(r-1):128;e[0]=e[0]|n}returne}functionp(n,t=1n){if(n<=t)thrownewRangeError("Arguments MUST be: max > min");constr=n-t,o=e(r);leti;do{i=m(w(o))}while(i>r);returni+t}lety=!1;try{awaitimport("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 ")}functionB(t,e=16,r=!1){if("number"==typeoft&&(t=BigInt(t)),t<0n)throwRangeError("w MUST be >= 0");returnnewPromise(!r&&y?(r,o)=>{consti=new_.Worker(n(import.meta.url));i.on("message",(n=>{void0!==n?._bcu?.isPrime&&(i.terminate().catch(o),r(n._bcu.isPrime))})),i.on("error",o);constu={_bcu:{rnd:t,iterations:e,id:0}};i.postMessage(u)}:n=>{n(I(t,e))})}functionI(n,t){if(2n==