bigint-crypto-utils/dist/index.node.esm.js

3 lines
20 KiB
JavaScript
Raw Normal View History

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<<i}function a(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)}function c(n,t){return n>=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==
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubm9kZS5lc20uanMiLCJzb3VyY2VzIjpbIi4uL25vZGVfbW9kdWxlcy9iaWdpbnQtbW9kLWFyaXRoL2Rpc3QvaW5kZXgubm9kZS5lc20uanMiLCIuLi9zcmMvdHMvZnJvbUJ1ZmZlci50cyIsIi4uL3NyYy90cy9yYW5kQnl0ZXMudHMiLCIuLi9zcmMvdHMvcmFuZEJpdHMudHMiLCIuLi9zcmMvdHMvcmFuZEJldHdlZW4udHMiLCIuLi9zcmMvdHMvd29ya2VyVXRpbHMudHMiLCIuLi9zcmMvdHMvaXNQcm9iYWJseVByaW1lLnRzIiwiLi4vc3JjL3RzL3ByaW1lLnRzIl0sInNvdXJjZXNDb250ZW50IjpudWxsLCJuYW1lcyI6WyJuIiwidCIsIkJpZ0ludCIsInIiLCJSYW5nZUVycm9yIiwiZSIsIm8iLCJpIiwidSIsImYiLCJnIiwiYyIsIngiLCJ5IiwidG9TdHJpbmciLCJsZW5ndGgiLCJyZWR1Y2UiLCJzIiwibWFwIiwiYSIsImZvckVhY2giLCJTdHJpbmciLCJwIiwiayIsIk9iamVjdCIsInZhbHVlcyIsImZyb21CdWZmZXIiLCJidWYiLCJyZXQiLCJj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