bigint-crypto-utils/dist/bigint-crypto-utils-latest....

2 lines
6.5 KiB
JavaScript
Raw Normal View History

2020-03-24 21:00:29 +00:00
const _ZERO=BigInt(0),_ONE=BigInt(1),_TWO=BigInt(2);function abs(a){return(a=BigInt(a))>=_ZERO?a:-a}function bitLength(a){if((a=BigInt(a))===_ONE)return 1;let bits=1;do{bits++}while((a>>=_ONE)>_ONE);return bits}function eGcd(a,b){if(a=BigInt(a),b=BigInt(b),a<=_ZERO|b<=_ZERO)return NaN;let x=_ZERO,y=_ONE,u=_ONE,v=_ZERO;for(;a!==_ZERO;){let q=b/a,r=b%a,m=x-u*q,n=y-v*q;b=a,a=r,x=u,y=v,u=m,v=n}return{b:b,x:x,y:y}}function gcd(a,b){if(a=abs(a),b=abs(b),a===_ZERO)return b;if(b===_ZERO)return a;let shift=_ZERO;for(;!((a|b)&_ONE);)a>>=_ONE,b>>=_ONE,shift++;for(;!(a&_ONE);)a>>=_ONE;do{for(;!(b&_ONE);)b>>=_ONE;if(a>b){let x=a;a=b,b=x}b-=a}while(b);return a<<shift}async function isProbablyPrime(w,iterations=16){return"number"==typeof w&&(w=BigInt(w)),new Promise((resolve,reject)=>{const worker=new Worker(_isProbablyPrimeWorkerUrl());worker.onmessage=event=>{worker.terminate(),resolve(event.data.isPrime)},worker.onmessageerror=event=>{reject(event)},worker.postMessage({rnd:w,iterations:iterations,id:0})})}function lcm(a,b){return a=BigInt(a),b=BigInt(b),a===_ZERO&&b===_ZERO?_ZERO:abs(a*b)/gcd(a,b)}function max(a,b){return(a=BigInt(a))>=(b=BigInt(b))?a:b}function min(a,b){return(a=BigInt(a))>=(b=BigInt(b))?b:a}function modInv(a,n){if(a==_ZERO|n<=_ZERO)return NaN;let egcd=eGcd(toZn(a,n),n);return egcd.b!==_ONE?NaN:toZn(egcd.x,n)}function modPow(b,e,n){if((n=BigInt(n))===_ZERO)return NaN;if(n===_ONE)return _ZERO;if(b=toZn(b,n),(e=BigInt(e))<_ZERO)return modInv(modPow(b,abs(e),n),n);let r=_ONE;for(;e>0;)e%_TWO===_ONE&&(r=r*b%n),e/=_TWO,b=b**_TWO%n;return r}function prime(bitLength,iterations=16){if(bitLength<1)throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);return new Promise(resolve=>{let workerList=[];const _onmessage=(msg,newWorker)=>{if(msg.isPrime){for(let j=0;j<workerList.length;j++)workerList[j].terminate();for(;workerList.length;)workerList.pop();resolve(msg.value)}else{let rnd=fromBuffer(randBits(bitLength,!0));try{newWorker.postMessage({rnd:rnd,iterations:iterations,id:msg.id})}catch(error){}}};{let workerURL=_isProbablyPrimeWorkerUrl();for(let i=0;i<self.navigator.hardwareConcurrency;i++){let newWorker=new Worker(workerURL);newWorker.onmessage=event=>_onmessage(event.data,newWorker),workerList.push(newWorker)}}for(let i=0;i<workerList.length;i++){let rnd=fromBuffer(randBits(bitLength,!0));workerList[i].postMessage({rnd:rnd,iterations:iterations,id:i})}})}function primeSync(bitLength,iterations=16){if(bitLength<1)throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);let rnd=_ZERO;do{rnd=fromBuffer(randBytesSync(bitLength/8,!0))}while(!_isProbablyPrime(rnd,iterations));return rnd}function randBetween(max,min=_ONE){if(max<=min)throw new Error("max must be > min");const interval=max-min;let rnd,bitLen=bitLength(interval);do{rnd=fromBuffer(randBits(bitLen))}while(rnd>interval);return rnd+min}function randBits(bitLength,forceLength=!1){if(bitLength<1)throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);const rndBytes=randBytesSync(Math.ceil(bitLength/8),!1),bitLengthMod8=bitLength%8;if(bitLengthMod8&&(rndBytes[0]=rndBytes[0]&2**bitLengthMod8-1),forceLength){const mask=bitLengthMod8?2**(bitLengthMod8-1):128;rndBytes[0]=rndBytes[0]|mask}return rndBytes}function randBytes(byteLength,forceLength=!1){if(byteLength<1)throw new RangeError(`byteLength MUST be > 0 and it is ${byteLength}`);let buf;return new Promise((function(resolve){buf=new Uint8Array(byteLength),self.crypto.getRandomValues(buf),forceLength&&(buf[0]=128|buf[0]),resolve(buf)}))}function randBytesSync(byteLength,forceLength=!1){if(byteLength<1)throw new RangeError(`byteLength MUST be > 0 and it is ${byteLength}`);let buf;return buf=new Uint8Array(byteLength),self.crypto.getRandomValues(buf),forceLength&&(buf[0]=128|buf[0]),buf}function toZn(a,n){return(n=BigInt(n))<=0?NaN:(a=BigInt(a)%n)<0?a+n:a}function fromBuffer(buf){let ret=_ZERO;for(let i of buf.values()){let bi=BigInt(i);ret=(ret<<BigInt(8))+bi}return ret}function _isProbablyPrimeWorkerUrl(){let workerCode=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1)