@toruslabs/ffjavascript
Advanced tools
Comparing version
@@ -1,3 +0,1 @@ | ||
import crypto from 'crypto'; | ||
/* global BigInt */ | ||
@@ -521,16 +519,11 @@ const hexLen = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4]; | ||
let array = new Uint8Array(n); | ||
if (process.browser) { | ||
// Browser | ||
if (typeof globalThis.crypto !== "undefined") { | ||
// Supported | ||
globalThis.crypto.getRandomValues(array); | ||
} else { | ||
// fallback | ||
for (let i = 0; i < n; i++) { | ||
array[i] = (Math.random() * 4294967296) >>> 0; | ||
} | ||
// Browser & Node | ||
if (typeof globalThis.crypto !== "undefined") { | ||
// Supported | ||
globalThis.crypto.getRandomValues(array); | ||
} else { | ||
// fallback | ||
for (let i = 0; i < n; i++) { | ||
array[i] = (Math.random() * 4294967296) >>> 0; | ||
} | ||
} else { | ||
// NodeJS | ||
crypto.randomFillSync(array); | ||
} | ||
@@ -537,0 +530,0 @@ return array; |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("crypto")):"function"==typeof define&&define.amd?define(["exports","crypto"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).Ffjavascript={},t.crypto)}(this,(function(t,n){"use strict";const i=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4];function r(t,n){return n&&10!=n?16==n?"0x"==t.slice(0,2)?BigInt(t):BigInt("0x"+t):void 0:BigInt(t)}const s=r;function e(t){const n=t.toString(16);return 4*(n.length-1)+i[parseInt(n[0],16)]}function o(t){return!t}function h(t,n){return BigInt(t)<<BigInt(n)}function u(t,n){return BigInt(t)>>BigInt(n)}const f=h,l=u;function g(t){return(BigInt(t)&BigInt(1))==BigInt(1)}function p(t){let n=BigInt(t);const i=[];for(;n;)n&BigInt(1)?i.push(1):i.push(0),n>>=BigInt(1);return i}function c(t){if(t>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Number too big");return Number(t)}function a(t,n){return BigInt(t)-BigInt(n)}function q(t,n){return BigInt(t)**BigInt(n)}function B(t,n){return BigInt(t)/BigInt(n)}function m(t,n){return BigInt(t)%BigInt(n)}function I(t,n){return BigInt(t)==BigInt(n)}function w(t,n){return BigInt(t)&BigInt(n)}function b(t,n,i,r){const s="0000000"+i.toString(16),e=new Uint32Array(t.buffer,n,r/4),o=1+(4*(s.length-7)-1>>5);for(let t=0;t<o;t++)e[t]=parseInt(s.substring(s.length-8*t-8,s.length-8*t),16);for(let t=o;t<e.length;t++)e[t]=0;for(let n=4*e.length;n<r;n++)t[n]=c(w(u(i,8*n),255))}function d(t,n,i,r){const s="0000000"+i.toString(16),e=new DataView(t.buffer,t.byteOffset+n,r),o=1+(4*(s.length-7)-1>>5);for(let t=0;t<o;t++)e.setUint32(r-4*t-4,parseInt(s.substring(s.length-8*t-8,s.length-8*t),16),!1);for(let t=0;t<r/4-o;t++)e[t]=0}function _(t,n,i){i=i||t.byteLength,n=n||0;const s=new Uint32Array(t.buffer,n,i/4),e=new Array(i/4);return s.forEach(((t,n)=>e[e.length-n-1]=t.toString(16).padStart(8,"0"))),r(e.join(""),16)}function F(t,n,i){i=i||t.byteLength,n=n||0;const s=new DataView(t.buffer,t.byteOffset+n,i),e=new Array(i/4);for(let t=0;t<i/4;t++)e[t]=s.getUint32(4*t,!1).toString(16).padStart(8,"0");return r(e.join(""),16)}const E=s(0),R=s(1);var y=Object.freeze({__proto__:null,abs:function(t){return BigInt(t)>=0?BigInt(t):-BigInt(t)},add:function(t,n){return BigInt(t)+BigInt(n)},band:w,bitLength:e,bits:p,bor:function(t,n){return BigInt(t)|BigInt(n)},bxor:function(t,n){return BigInt(t)^BigInt(n)},div:B,e:s,eq:I,exp:function(t,n){return BigInt(t)**BigInt(n)},fromArray:function(t,n){let i=BigInt(0);n=BigInt(n);for(let r=0;r<t.length;r++)i=i*n+BigInt(t[r]);return i},fromRprBE:F,fromRprLE:_,fromString:r,geq:function(t,n){return BigInt(t)>=BigInt(n)},gt:function(t,n){return BigInt(t)>BigInt(n)},isNegative:function(t){return BigInt(t)<BigInt(0)},isOdd:g,isZero:o,land:function(t,n){return BigInt(t)&&BigInt(n)},leq:function(t,n){return BigInt(t)<=BigInt(n)},lnot:function(t){return!BigInt(t)},lor:function(t,n){return BigInt(t)||BigInt(n)},lt:function(t,n){return BigInt(t)<BigInt(n)},mod:m,mul:function(t,n){return BigInt(t)*BigInt(n)},naf:function(t){let n=BigInt(t);const i=[];for(;n;){if(n&BigInt(1)){const t=2-Number(n%BigInt(4));i.push(t),n-=BigInt(t)}else i.push(0);n>>=BigInt(1)}return i},neg:function(t){return-BigInt(t)},neq:function(t,n){return BigInt(t)!=BigInt(n)},one:R,pow:q,shiftLeft:h,shiftRight:u,shl:f,shr:l,square:function(t){return BigInt(t)*BigInt(t)},sub:a,toArray:function(t,n){const i=[];let r=BigInt(t);for(n=BigInt(n);r;)i.unshift(Number(r%n)),r/=n;return i},toLEBuff:function(t){const n=new Uint8Array(Math.floor((e(t)-1)/8)+1);return b(n,0,t,n.byteLength),n},toNumber:c,toRprBE:d,toRprLE:b,toString:function(t,n){return t.toString(n)},zero:E});function z(t,n,i){if(o(i))return t.one;const r=p(i);if(0==r.length)return t.one;let s=n;for(let i=r.length-2;i>=0;i--)s=t.square(s),r[i]&&(s=t.mul(s,n));return s}function S(t){if(t.m%2==1)if(I(m(t.p,4),1))if(I(m(t.p,8),1))if(I(m(t.p,16),1))!function(t){t.sqrt_q=q(t.p,t.m),t.sqrt_s=0,t.sqrt_t=a(t.sqrt_q,1);for(;!g(t.sqrt_t);)t.sqrt_s=t.sqrt_s+1,t.sqrt_t=B(t.sqrt_t,2);let n=t.one;for(;t.eq(n,t.one);){const i=t.random();t.sqrt_z=t.pow(i,t.sqrt_t),n=t.pow(t.sqrt_z,2**(t.sqrt_s-1))}t.sqrt_tm1d2=B(a(t.sqrt_t,1),2),t.sqrt=function(t){const n=this;if(n.isZero(t))return n.zero;let i=n.pow(t,n.sqrt_tm1d2);const r=n.pow(n.mul(n.square(i),t),2**(n.sqrt_s-1));if(n.eq(r,n.negone))return null;let s=n.sqrt_s,e=n.mul(t,i),o=n.mul(e,i),h=n.sqrt_z;for(;!n.eq(o,n.one);){let t=n.square(o),r=1;for(;!n.eq(t,n.one);)t=n.square(t),r++;i=h;for(let t=0;t<s-r-1;t++)i=n.square(i);h=n.square(i),o=n.mul(o,h),e=n.mul(e,i),s=r}return n.geq(e,n.zero)?e:n.neg(e)}}(t);else{if(!I(m(t.p,16),9))throw new Error("Field withot sqrt");!function(t){t.sqrt=function(){throw new Error("Sqrt alg 4 not implemented")}}(t)}else{if(!I(m(t.p,8),5))throw new Error("Field withot sqrt");!function(t){t.sqrt=function(){throw new Error("Sqrt alg 3 not implemented")}}(t)}else I(m(t.p,4),3)&&function(t){t.sqrt_q=q(t.p,t.m),t.sqrt_e1=B(a(t.sqrt_q,3),4),t.sqrt=function(n){if(this.isZero(n))return this.zero;const i=this.pow(n,this.sqrt_e1),r=this.mul(this.square(i),n);if(this.eq(r,this.negone))return null;const s=this.mul(i,n);return t.geq(s,t.zero)?s:t.neg(s)}}(t);else{const n=m(q(t.p,t.m/2),4);1==n?function(t){t.sqrt=function(){throw new Error("Sqrt alg 10 not implemented")}}(t):3==n?function(t){t.sqrt_q=q(t.p,t.m/2),t.sqrt_e34=B(a(t.sqrt_q,3),4),t.sqrt_e12=B(a(t.sqrt_q,1),2),t.frobenius=function(n,i){return n%2==1?t.conjugate(i):i},t.sqrt=function(t){const n=this,i=n.pow(t,n.sqrt_e34),r=n.mul(n.square(i),t),s=n.mul(n.frobenius(1,r),r);if(n.eq(s,n.negone))return null;const e=n.mul(i,t);let o;if(n.eq(r,n.negone))o=n.mul(e,[n.F.zero,n.F.one]);else{const t=n.pow(n.add(n.one,r),n.sqrt_e12);o=n.mul(t,e)}return n.geq(o,n.zero)?o:n.neg(o)}}(t):function(t){t.sqrt=function(){throw new Error("Sqrt alg 8 not implemented")}}(t)}}function L(t){let i=new Uint8Array(t);if(process.browser)if(void 0!==globalThis.crypto)globalThis.crypto.getRandomValues(i);else for(let n=0;n<t;n++)i[n]=4294967296*Math.random()>>>0;else n.randomFillSync(i);return i}class v{constructor(t,n,i){this.F=n,this.G=t,this.opMulGF=i;let r=n.sqrt_t||n.t,s=n.sqrt_s||n.s,e=n.one;for(;n.eq(n.pow(e,n.half),n.one);)e=n.add(e,n.one);this.w=new Array(s+1),this.wi=new Array(s+1),this.w[s]=this.F.pow(e,r),this.wi[s]=this.F.inv(this.w[s]);let o=s-1;for(;o>=0;)this.w[o]=this.F.square(this.w[o+1]),this.wi[o]=this.F.square(this.wi[o+1]),o--;this.roots=[],this._setRoots(Math.min(s,15))}_setRoots(t){for(let n=t;n>=0&&!this.roots[n];n--){let t=this.F.one;const i=1<<n,r=new Array(i);for(let s=0;s<i;s++)r[s]=t,t=this.F.mul(t,this.w[n]);this.roots[n]=r}}fft(t){if(t.length<=1)return t;const n=A(t.length-1)+1;this._setRoots(n);const i=1<<n;if(t.length!=i)throw new Error("Size must be multiple of 2");return T(this,t,n,0,1)}ifft(t){if(t.length<=1)return t;const n=A(t.length-1)+1;this._setRoots(n);const i=1<<n;if(t.length!=i)throw new Error("Size must be multiple of 2");const r=T(this,t,n,0,1),s=this.F.inv(this.F.mulScalar(this.F.one,i)),e=new Array(i);for(let t=0;t<i;t++)e[t]=this.opMulGF(r[(i-t)%i],s);return e}}function A(t){return(0!=(4294901760&t)?(t&=4294901760,16):0)|(0!=(4278255360&t)?(t&=4278255360,8):0)|(0!=(4042322160&t)?(t&=4042322160,4):0)|(0!=(3435973836&t)?(t&=3435973836,2):0)|0!=(2863311530&t)}function T(t,n,i,r,s){const e=1<<i;if(1==e)return[n[r]];if(2==e)return[t.G.add(n[r],n[r+s]),t.G.sub(n[r],n[r+s])];const o=e>>1,h=T(t,n,i-1,r,2*s),u=T(t,n,i-1,r+s,2*s),f=new Array(e);for(let n=0;n<o;n++)f[n]=t.G.add(h[n],t.opMulGF(u[n],t.roots[i][n])),f[n+o]=t.G.sub(h[n],t.opMulGF(u[n],t.roots[i][n]));return f}class M{constructor(t){this.type="F1",this.one=BigInt(1),this.zero=BigInt(0),this.p=BigInt(t),this.m=1,this.negone=this.p-this.one,this.two=BigInt(2),this.half=this.p>>this.one,this.bitLength=e(this.p),this.mask=(this.one<<BigInt(this.bitLength))-this.one,this.n64=Math.floor((this.bitLength-1)/64)+1,this.n32=2*this.n64,this.n8=8*this.n64,this.R=this.e(this.one<<BigInt(64*this.n64)),this.Ri=this.inv(this.R);const n=this.negone>>this.one;this.nqr=this.two;let i=this.pow(this.nqr,n);for(;!this.eq(i,this.negone);)this.nqr=this.nqr+this.one,i=this.pow(this.nqr,n);for(this.s=0,this.t=this.negone;(this.t&this.one)==this.zero;)this.s=this.s+1,this.t=this.t>>this.one;this.nqr_to_t=this.pow(this.nqr,this.t),S(this),this.FFT=new v(this,this,this.mul.bind(this)),this.fft=this.FFT.fft.bind(this.FFT),this.ifft=this.FFT.ifft.bind(this.FFT),this.w=this.FFT.w,this.wi=this.FFT.wi,this.shift=this.square(this.nqr),this.k=this.exp(this.nqr,2**this.s)}e(t,n){let i;if(n?16==n&&(i=BigInt("0x"+t)):i=BigInt(t),i<0){let t=-i;return t>=this.p&&(t%=this.p),this.p-t}return i>=this.p?i%this.p:i}add(t,n){const i=t+n;return i>=this.p?i-this.p:i}sub(t,n){return t>=n?t-n:this.p-n+t}neg(t){return t?this.p-t:t}mul(t,n){return t*n%this.p}mulScalar(t,n){return t*this.e(n)%this.p}square(t){return t*t%this.p}eq(t,n){return t==n}neq(t,n){return t!=n}lt(t,n){return(t>this.half?t-this.p:t)<(n>this.half?n-this.p:n)}gt(t,n){return(t>this.half?t-this.p:t)>(n>this.half?n-this.p:n)}leq(t,n){return(t>this.half?t-this.p:t)<=(n>this.half?n-this.p:n)}geq(t,n){return(t>this.half?t-this.p:t)>=(n>this.half?n-this.p:n)}div(t,n){return this.mul(t,this.inv(n))}idiv(t,n){if(!n)throw new Error("Division by zero");return t/n}inv(t){if(!t)throw new Error("Division by zero");let n=this.zero,i=this.p,r=this.one,s=t%this.p;for(;s;){let t=i/s;[n,r]=[r,n-t*r],[i,s]=[s,i-t*s]}return n<this.zero&&(n+=this.p),n}mod(t,n){return t%n}pow(t,n){return z(this,t,n)}exp(t,n){return z(this,t,n)}band(t,n){const i=t&n&this.mask;return i>=this.p?i-this.p:i}bor(t,n){const i=(t|n)&this.mask;return i>=this.p?i-this.p:i}bxor(t,n){const i=(t^n)&this.mask;return i>=this.p?i-this.p:i}bnot(t){const n=t^this.mask;return n>=this.p?n-this.p:n}shl(t,n){if(Number(n)<this.bitLength){const i=t<<n&this.mask;return i>=this.p?i-this.p:i}{const i=this.p-n;return Number(i)<this.bitLength?t>>i:this.zero}}shr(t,n){if(Number(n)<this.bitLength)return t>>n;{const i=this.p-n;if(Number(i)<this.bitLength){const n=t<<i&this.mask;return n>=this.p?n-this.p:n}return 0}}land(t,n){return t&&n?this.one:this.zero}lor(t,n){return t||n?this.one:this.zero}lnot(t){return t?this.zero:this.one}sqrt_old(t){if(t==this.zero)return this.zero;if(this.pow(t,this.negone>>this.one)!=this.one)return null;let n=this.s,i=this.nqr_to_t,r=this.pow(t,this.t),s=this.pow(t,this.add(this.t,this.one)>>this.one);for(;r!=this.one;){let t=this.square(r),e=1;for(;t!=this.one;)e++,t=this.square(t);let o=i;for(let t=0;t<n-e-1;t++)o=this.square(o);n=e,i=this.square(o),r=this.mul(r,i),s=this.mul(s,o)}return s>this.p>>this.one&&(s=this.neg(s)),s}normalize(t,n){if((t=BigInt(t,n))<0){let n=-t;return n>=this.p&&(n%=this.p),this.p-n}return t>=this.p?t%this.p:t}random(){const t=2*this.bitLength/8;let n=this.zero;for(let i=0;i<t;i++)n=(n<<BigInt(8))+BigInt(L(1)[0]);return n%this.p}toString(t,n){let i;if(n=n||10,t>this.half&&10==n){i="-"+(this.p-t).toString(n)}else i=t.toString(n);return i}isZero(t){return t==this.zero}fromRng(t){let n;do{n=this.zero;for(let i=0;i<this.n64;i++)n+=t.nextU64()<<BigInt(64*i);n&=this.mask}while(n>=this.p);return n=n*this.Ri%this.p,n}fft(t){return this.FFT.fft(t)}ifft(t){return this.FFT.ifft(t)}toRprLE(t,n,i){b(t,n,i,8*this.n64)}toRprBE(t,n,i){d(t,n,i,8*this.n64)}toRprBEM(t,n,i){return this.toRprBE(t,n,this.mul(this.R,i))}toRprLEM(t,n,i){return this.toRprLE(t,n,this.mul(this.R,i))}fromRprLE(t,n){return _(t,n,this.n8)}fromRprBE(t,n){return F(t,n,this.n8)}fromRprLEM(t,n){return this.mul(this.fromRprLE(t,n),this.Ri)}fromRprBEM(t,n){return this.mul(this.fromRprBE(t,n),this.Ri)}toObject(t){return t}}const x=y;t.F1Field=M,t.Scalar=x,t.ZqField=M})); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).Ffjavascript={})}(this,(function(t){"use strict";const n=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4];function i(t,n){return n&&10!=n?16==n?"0x"==t.slice(0,2)?BigInt(t):BigInt("0x"+t):void 0:BigInt(t)}const r=i;function s(t){const i=t.toString(16);return 4*(i.length-1)+n[parseInt(i[0],16)]}function e(t){return!t}function o(t,n){return BigInt(t)<<BigInt(n)}function h(t,n){return BigInt(t)>>BigInt(n)}const u=o,f=h;function l(t){return(BigInt(t)&BigInt(1))==BigInt(1)}function g(t){let n=BigInt(t);const i=[];for(;n;)n&BigInt(1)?i.push(1):i.push(0),n>>=BigInt(1);return i}function p(t){if(t>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Number too big");return Number(t)}function c(t,n){return BigInt(t)-BigInt(n)}function a(t,n){return BigInt(t)**BigInt(n)}function q(t,n){return BigInt(t)/BigInt(n)}function B(t,n){return BigInt(t)%BigInt(n)}function m(t,n){return BigInt(t)==BigInt(n)}function I(t,n){return BigInt(t)&BigInt(n)}function w(t,n,i,r){const s="0000000"+i.toString(16),e=new Uint32Array(t.buffer,n,r/4),o=1+(4*(s.length-7)-1>>5);for(let t=0;t<o;t++)e[t]=parseInt(s.substring(s.length-8*t-8,s.length-8*t),16);for(let t=o;t<e.length;t++)e[t]=0;for(let n=4*e.length;n<r;n++)t[n]=p(I(h(i,8*n),255))}function b(t,n,i,r){const s="0000000"+i.toString(16),e=new DataView(t.buffer,t.byteOffset+n,r),o=1+(4*(s.length-7)-1>>5);for(let t=0;t<o;t++)e.setUint32(r-4*t-4,parseInt(s.substring(s.length-8*t-8,s.length-8*t),16),!1);for(let t=0;t<r/4-o;t++)e[t]=0}function d(t,n,r){r=r||t.byteLength,n=n||0;const s=new Uint32Array(t.buffer,n,r/4),e=new Array(r/4);return s.forEach(((t,n)=>e[e.length-n-1]=t.toString(16).padStart(8,"0"))),i(e.join(""),16)}function _(t,n,r){r=r||t.byteLength,n=n||0;const s=new DataView(t.buffer,t.byteOffset+n,r),e=new Array(r/4);for(let t=0;t<r/4;t++)e[t]=s.getUint32(4*t,!1).toString(16).padStart(8,"0");return i(e.join(""),16)}const F=r(0),E=r(1);var R=Object.freeze({__proto__:null,abs:function(t){return BigInt(t)>=0?BigInt(t):-BigInt(t)},add:function(t,n){return BigInt(t)+BigInt(n)},band:I,bitLength:s,bits:g,bor:function(t,n){return BigInt(t)|BigInt(n)},bxor:function(t,n){return BigInt(t)^BigInt(n)},div:q,e:r,eq:m,exp:function(t,n){return BigInt(t)**BigInt(n)},fromArray:function(t,n){let i=BigInt(0);n=BigInt(n);for(let r=0;r<t.length;r++)i=i*n+BigInt(t[r]);return i},fromRprBE:_,fromRprLE:d,fromString:i,geq:function(t,n){return BigInt(t)>=BigInt(n)},gt:function(t,n){return BigInt(t)>BigInt(n)},isNegative:function(t){return BigInt(t)<BigInt(0)},isOdd:l,isZero:e,land:function(t,n){return BigInt(t)&&BigInt(n)},leq:function(t,n){return BigInt(t)<=BigInt(n)},lnot:function(t){return!BigInt(t)},lor:function(t,n){return BigInt(t)||BigInt(n)},lt:function(t,n){return BigInt(t)<BigInt(n)},mod:B,mul:function(t,n){return BigInt(t)*BigInt(n)},naf:function(t){let n=BigInt(t);const i=[];for(;n;){if(n&BigInt(1)){const t=2-Number(n%BigInt(4));i.push(t),n-=BigInt(t)}else i.push(0);n>>=BigInt(1)}return i},neg:function(t){return-BigInt(t)},neq:function(t,n){return BigInt(t)!=BigInt(n)},one:E,pow:a,shiftLeft:o,shiftRight:h,shl:u,shr:f,square:function(t){return BigInt(t)*BigInt(t)},sub:c,toArray:function(t,n){const i=[];let r=BigInt(t);for(n=BigInt(n);r;)i.unshift(Number(r%n)),r/=n;return i},toLEBuff:function(t){const n=new Uint8Array(Math.floor((s(t)-1)/8)+1);return w(n,0,t,n.byteLength),n},toNumber:p,toRprBE:b,toRprLE:w,toString:function(t,n){return t.toString(n)},zero:F});function z(t,n,i){if(e(i))return t.one;const r=g(i);if(0==r.length)return t.one;let s=n;for(let i=r.length-2;i>=0;i--)s=t.square(s),r[i]&&(s=t.mul(s,n));return s}function y(t){if(t.m%2==1)if(m(B(t.p,4),1))if(m(B(t.p,8),1))if(m(B(t.p,16),1))!function(t){t.sqrt_q=a(t.p,t.m),t.sqrt_s=0,t.sqrt_t=c(t.sqrt_q,1);for(;!l(t.sqrt_t);)t.sqrt_s=t.sqrt_s+1,t.sqrt_t=q(t.sqrt_t,2);let n=t.one;for(;t.eq(n,t.one);){const i=t.random();t.sqrt_z=t.pow(i,t.sqrt_t),n=t.pow(t.sqrt_z,2**(t.sqrt_s-1))}t.sqrt_tm1d2=q(c(t.sqrt_t,1),2),t.sqrt=function(t){const n=this;if(n.isZero(t))return n.zero;let i=n.pow(t,n.sqrt_tm1d2);const r=n.pow(n.mul(n.square(i),t),2**(n.sqrt_s-1));if(n.eq(r,n.negone))return null;let s=n.sqrt_s,e=n.mul(t,i),o=n.mul(e,i),h=n.sqrt_z;for(;!n.eq(o,n.one);){let t=n.square(o),r=1;for(;!n.eq(t,n.one);)t=n.square(t),r++;i=h;for(let t=0;t<s-r-1;t++)i=n.square(i);h=n.square(i),o=n.mul(o,h),e=n.mul(e,i),s=r}return n.geq(e,n.zero)?e:n.neg(e)}}(t);else{if(!m(B(t.p,16),9))throw new Error("Field withot sqrt");!function(t){t.sqrt=function(){throw new Error("Sqrt alg 4 not implemented")}}(t)}else{if(!m(B(t.p,8),5))throw new Error("Field withot sqrt");!function(t){t.sqrt=function(){throw new Error("Sqrt alg 3 not implemented")}}(t)}else m(B(t.p,4),3)&&function(t){t.sqrt_q=a(t.p,t.m),t.sqrt_e1=q(c(t.sqrt_q,3),4),t.sqrt=function(n){if(this.isZero(n))return this.zero;const i=this.pow(n,this.sqrt_e1),r=this.mul(this.square(i),n);if(this.eq(r,this.negone))return null;const s=this.mul(i,n);return t.geq(s,t.zero)?s:t.neg(s)}}(t);else{const n=B(a(t.p,t.m/2),4);1==n?function(t){t.sqrt=function(){throw new Error("Sqrt alg 10 not implemented")}}(t):3==n?function(t){t.sqrt_q=a(t.p,t.m/2),t.sqrt_e34=q(c(t.sqrt_q,3),4),t.sqrt_e12=q(c(t.sqrt_q,1),2),t.frobenius=function(n,i){return n%2==1?t.conjugate(i):i},t.sqrt=function(t){const n=this,i=n.pow(t,n.sqrt_e34),r=n.mul(n.square(i),t),s=n.mul(n.frobenius(1,r),r);if(n.eq(s,n.negone))return null;const e=n.mul(i,t);let o;if(n.eq(r,n.negone))o=n.mul(e,[n.F.zero,n.F.one]);else{const t=n.pow(n.add(n.one,r),n.sqrt_e12);o=n.mul(t,e)}return n.geq(o,n.zero)?o:n.neg(o)}}(t):function(t){t.sqrt=function(){throw new Error("Sqrt alg 8 not implemented")}}(t)}}function S(t){let n=new Uint8Array(t);if(void 0!==globalThis.crypto)globalThis.crypto.getRandomValues(n);else for(let i=0;i<t;i++)n[i]=4294967296*Math.random()>>>0;return n}class L{constructor(t,n,i){this.F=n,this.G=t,this.opMulGF=i;let r=n.sqrt_t||n.t,s=n.sqrt_s||n.s,e=n.one;for(;n.eq(n.pow(e,n.half),n.one);)e=n.add(e,n.one);this.w=new Array(s+1),this.wi=new Array(s+1),this.w[s]=this.F.pow(e,r),this.wi[s]=this.F.inv(this.w[s]);let o=s-1;for(;o>=0;)this.w[o]=this.F.square(this.w[o+1]),this.wi[o]=this.F.square(this.wi[o+1]),o--;this.roots=[],this._setRoots(Math.min(s,15))}_setRoots(t){for(let n=t;n>=0&&!this.roots[n];n--){let t=this.F.one;const i=1<<n,r=new Array(i);for(let s=0;s<i;s++)r[s]=t,t=this.F.mul(t,this.w[n]);this.roots[n]=r}}fft(t){if(t.length<=1)return t;const n=v(t.length-1)+1;this._setRoots(n);const i=1<<n;if(t.length!=i)throw new Error("Size must be multiple of 2");return A(this,t,n,0,1)}ifft(t){if(t.length<=1)return t;const n=v(t.length-1)+1;this._setRoots(n);const i=1<<n;if(t.length!=i)throw new Error("Size must be multiple of 2");const r=A(this,t,n,0,1),s=this.F.inv(this.F.mulScalar(this.F.one,i)),e=new Array(i);for(let t=0;t<i;t++)e[t]=this.opMulGF(r[(i-t)%i],s);return e}}function v(t){return(4294901760&t?(t&=4294901760,16):0)|(4278255360&t?(t&=4278255360,8):0)|(4042322160&t?(t&=4042322160,4):0)|(3435973836&t?(t&=3435973836,2):0)|!!(2863311530&t)}function A(t,n,i,r,s){const e=1<<i;if(1==e)return[n[r]];if(2==e)return[t.G.add(n[r],n[r+s]),t.G.sub(n[r],n[r+s])];const o=e>>1,h=A(t,n,i-1,r,2*s),u=A(t,n,i-1,r+s,2*s),f=new Array(e);for(let n=0;n<o;n++)f[n]=t.G.add(h[n],t.opMulGF(u[n],t.roots[i][n])),f[n+o]=t.G.sub(h[n],t.opMulGF(u[n],t.roots[i][n]));return f}class T{constructor(t){this.type="F1",this.one=BigInt(1),this.zero=BigInt(0),this.p=BigInt(t),this.m=1,this.negone=this.p-this.one,this.two=BigInt(2),this.half=this.p>>this.one,this.bitLength=s(this.p),this.mask=(this.one<<BigInt(this.bitLength))-this.one,this.n64=Math.floor((this.bitLength-1)/64)+1,this.n32=2*this.n64,this.n8=8*this.n64,this.R=this.e(this.one<<BigInt(64*this.n64)),this.Ri=this.inv(this.R);const n=this.negone>>this.one;this.nqr=this.two;let i=this.pow(this.nqr,n);for(;!this.eq(i,this.negone);)this.nqr=this.nqr+this.one,i=this.pow(this.nqr,n);for(this.s=0,this.t=this.negone;(this.t&this.one)==this.zero;)this.s=this.s+1,this.t=this.t>>this.one;this.nqr_to_t=this.pow(this.nqr,this.t),y(this),this.FFT=new L(this,this,this.mul.bind(this)),this.fft=this.FFT.fft.bind(this.FFT),this.ifft=this.FFT.ifft.bind(this.FFT),this.w=this.FFT.w,this.wi=this.FFT.wi,this.shift=this.square(this.nqr),this.k=this.exp(this.nqr,2**this.s)}e(t,n){let i;if(n?16==n&&(i=BigInt("0x"+t)):i=BigInt(t),i<0){let t=-i;return t>=this.p&&(t%=this.p),this.p-t}return i>=this.p?i%this.p:i}add(t,n){const i=t+n;return i>=this.p?i-this.p:i}sub(t,n){return t>=n?t-n:this.p-n+t}neg(t){return t?this.p-t:t}mul(t,n){return t*n%this.p}mulScalar(t,n){return t*this.e(n)%this.p}square(t){return t*t%this.p}eq(t,n){return t==n}neq(t,n){return t!=n}lt(t,n){return(t>this.half?t-this.p:t)<(n>this.half?n-this.p:n)}gt(t,n){return(t>this.half?t-this.p:t)>(n>this.half?n-this.p:n)}leq(t,n){return(t>this.half?t-this.p:t)<=(n>this.half?n-this.p:n)}geq(t,n){return(t>this.half?t-this.p:t)>=(n>this.half?n-this.p:n)}div(t,n){return this.mul(t,this.inv(n))}idiv(t,n){if(!n)throw new Error("Division by zero");return t/n}inv(t){if(!t)throw new Error("Division by zero");let n=this.zero,i=this.p,r=this.one,s=t%this.p;for(;s;){let t=i/s;[n,r]=[r,n-t*r],[i,s]=[s,i-t*s]}return n<this.zero&&(n+=this.p),n}mod(t,n){return t%n}pow(t,n){return z(this,t,n)}exp(t,n){return z(this,t,n)}band(t,n){const i=t&n&this.mask;return i>=this.p?i-this.p:i}bor(t,n){const i=(t|n)&this.mask;return i>=this.p?i-this.p:i}bxor(t,n){const i=(t^n)&this.mask;return i>=this.p?i-this.p:i}bnot(t){const n=t^this.mask;return n>=this.p?n-this.p:n}shl(t,n){if(Number(n)<this.bitLength){const i=t<<n&this.mask;return i>=this.p?i-this.p:i}{const i=this.p-n;return Number(i)<this.bitLength?t>>i:this.zero}}shr(t,n){if(Number(n)<this.bitLength)return t>>n;{const i=this.p-n;if(Number(i)<this.bitLength){const n=t<<i&this.mask;return n>=this.p?n-this.p:n}return 0}}land(t,n){return t&&n?this.one:this.zero}lor(t,n){return t||n?this.one:this.zero}lnot(t){return t?this.zero:this.one}sqrt_old(t){if(t==this.zero)return this.zero;if(this.pow(t,this.negone>>this.one)!=this.one)return null;let n=this.s,i=this.nqr_to_t,r=this.pow(t,this.t),s=this.pow(t,this.add(this.t,this.one)>>this.one);for(;r!=this.one;){let t=this.square(r),e=1;for(;t!=this.one;)e++,t=this.square(t);let o=i;for(let t=0;t<n-e-1;t++)o=this.square(o);n=e,i=this.square(o),r=this.mul(r,i),s=this.mul(s,o)}return s>this.p>>this.one&&(s=this.neg(s)),s}normalize(t,n){if((t=BigInt(t,n))<0){let n=-t;return n>=this.p&&(n%=this.p),this.p-n}return t>=this.p?t%this.p:t}random(){const t=2*this.bitLength/8;let n=this.zero;for(let i=0;i<t;i++)n=(n<<BigInt(8))+BigInt(S(1)[0]);return n%this.p}toString(t,n){let i;if(n=n||10,t>this.half&&10==n){i="-"+(this.p-t).toString(n)}else i=t.toString(n);return i}isZero(t){return t==this.zero}fromRng(t){let n;do{n=this.zero;for(let i=0;i<this.n64;i++)n+=t.nextU64()<<BigInt(64*i);n&=this.mask}while(n>=this.p);return n=n*this.Ri%this.p,n}fft(t){return this.FFT.fft(t)}ifft(t){return this.FFT.ifft(t)}toRprLE(t,n,i){w(t,n,i,8*this.n64)}toRprBE(t,n,i){b(t,n,i,8*this.n64)}toRprBEM(t,n,i){return this.toRprBE(t,n,this.mul(this.R,i))}toRprLEM(t,n,i){return this.toRprLE(t,n,this.mul(this.R,i))}fromRprLE(t,n){return d(t,n,this.n8)}fromRprBE(t,n){return _(t,n,this.n8)}fromRprLEM(t,n){return this.mul(this.fromRprLE(t,n),this.Ri)}fromRprBEM(t,n){return this.mul(this.fromRprBE(t,n),this.Ri)}toObject(t){return t}}const M=R;t.F1Field=T,t.Scalar=M,t.ZqField=T})); |
{ | ||
"name": "@toruslabs/ffjavascript", | ||
"type": "module", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Finite Field Library in Javascript", | ||
"main": "./build/main.cjs", | ||
"module": "./build/main.esm.js", | ||
"main": "./build/lib.cjs/main.js", | ||
"module": "./build/lib.esm/main.js", | ||
"unpkg": "./build/main.umd.min.js", | ||
@@ -13,2 +13,3 @@ "jsdelivr": "./build/main.umd.min.js", | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
@@ -37,8 +38,8 @@ "test": "mocha", | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"chai": "^4.3.7", | ||
"eslint": "^9.0.0", | ||
"mocha": "^10.4.0", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^4.14.1" | ||
"eslint": "^8.46.0", | ||
"mocha": "^10.2.0", | ||
"rimraf": "^5.0.1", | ||
"rollup": "^3.27.2" | ||
}, | ||
@@ -45,0 +46,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
137498
48.59%20
233.33%3472
95.28%0
-100%3
50%