bigint-crypto-utils/lib/index.browser.bundle.mod.js

2 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-04-21 00:41:32 +00:00
function n(n){return(n=BigInt(n))>=0n?n:-n}function t(n){if(1n===(n=BigInt(n)))return 1;let t=1;do{t++}while((n>>=1n)>1n);return t}function e(n,t){if((n=BigInt(n))<=0n|(t=BigInt(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,c=t%n,u=e-o*s,a=r-i*s;t=n,n=c,e=o,r=i,o=u,i=a}return{g:t,x:e,y:r}}function r(t,e){if(t=n(t),e=n(e),0n===t)return e;if(0n===e)return t;let r=0n;for(;!(1n&(t|e));)t>>=1n,e>>=1n,r++;for(;!(1n&t);)t>>=1n;do{for(;!(1n&e);)e>>=1n;if(t>e){const n=t;t=e,e=n}e-=t}while(e);return t<<r}function o(t,e){return t=BigInt(t),e=BigInt(e),0n===t&&0n===e?BigInt(0):n(t*e)/r(t,e)}function i(n,t){return(n=BigInt(n))>=(t=BigInt(t))?n:t}function s(n,t){return(n=BigInt(n))>=(t=BigInt(t))?t:n}function c(n,t){try{const r=e(a(n,t),t);return 1n!==r.g?NaN:a(r.x,t)}catch(n){return NaN}}function u(t,e,r){if(0n===(r=BigInt(r)))return NaN;if(1n===r)return BigInt(0);if(t=a(t,r),(e=BigInt(e))<0n)return c(u(t,n(e),r),r);let o=1n;for(;e>0;)e%2n===1n&&(o=o*t%r),e/=2n,t=t**2n%r;return o}function a(n,t){return(t=BigInt(t))<=0?NaN:(n=BigInt(n)%t)<0?n+t:n}function f(n,t=16,e=!1){return"number"==typeof n&&(n=BigInt(n)),new Promise((e,r)=>{const o=new Worker(I());o.onmessage=n=>{o.terminate(),e(n.data.isPrime)},o.onmessageerror=n=>{r(n)},o.postMessage({rnd:n,iterations:t,id:0})})}function g(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");if(!S){let e=0n;do{e=$(d(n,!0))}while(!b(e,t));return new Promise(n=>{n(e)})}return new Promise(e=>{const r=[],o=(o,i)=>{if(o.isPrime){for(let n=0;n<r.length;n++)r[n].terminate();for(;r.length;)r.pop();e(o.value)}else{const e=$(d(n,!0));try{i.postMessage({rnd:e,iterations:t,id:o.id})}catch(n){}}};{const n=I();for(let t=0;t<self.navigator.hardwareConcurrency-1;t++){const t=new Worker(n);t.onmessage=n=>o(n.data,t),r.push(t)}}for(let e=0;e<r.length;e++)h(n,!0).then((function(n){const o=$(n);r[e].postMessage({rnd:o,iterations:t,id:e})}))})}function l(n,t=16){if(n<1)throw new RangeError("bitLength MUST be > 0");let e=0n;do{e=$(d(n,!0))}while(!b(e,t));return e}function w(n,e=1n){if(n<=0n||e<0n||n<=e)throw new RangeError("inputs should be max > 0, min >= 0; max > min");const r=n-e,o=t(r);let i;do{i=$(d(o))}while(i>r);return i+e}function h(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=>{m(e,!1).then((function(e){if(r&&(e[0]=e[0]&2**r-1),t){const n=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=B(Math.ceil(n/8),!1),r=n%8;if(r&&(e[0]=e[0]&2**r-1),t){const n=r?2**(r-1):128;e[0]=e[0]|n}return e}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);self.crypto.getRandomValues(r),t&&(r[0]=128|r[0]),e(r)}}))}function B(n,t=!1){if(n<1)throw new RangeError("byteLength MUST be > 0");{const e=new Uint8Array(n);return self.crypto.getRandomValues(e),t&&(e[0]=128|e[0]),e}}function $(n){let t=0n;for(const e of n.values()){const n=BigInt(e);t=(t<<BigInt(8))+n}return t}function I(){let n=`'use strict';const ${e.name}=${e.toString()};const ${c.name}=${c.toString()};const ${u.name}=${u.toString()};const ${a.name}=${a.toString()};const ${d.name}=${d.toString()};const ${B.name}=${B.toString()};const ${w.name}=${w.toString()};const ${f.name}=${b.toString()};${t.toString()}${$.toString()}`;return n+=`onmessage = ${async function(n){const t=await f(n.data.rnd,n.data.iterations);postMessage({isPrime:t,value:n.data.rnd,id:n.data.id})}.toString()};`,function(n){n=`(() => {${n}})()`;const t=new Blob([n],{type:"text/javascript"});return window.URL.createObjectURL(t)}(n)}function b(n,t=16){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