UNPKG

@zk2/ffjavascript

Version:
1 lines 86.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("wasmcurves"),require("web-worker"),require("wasmbuilder")):"function"==typeof define&&define.amd?define(["exports","wasmcurves","web-worker","wasmbuilder"],e):e((t||self).ffjavascript={},t.wasmcurves,t.webWorker,t.wasmbuilder)}(this,function(t,e,n,r){function i(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=/*#__PURE__*/i(n);const o=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4];function h(t,e){return e&&10!=e?16==e?"0x"==t.slice(0,2)?BigInt(t):BigInt("0x"+t):void 0:BigInt(t)}const u=h;function f(t){const e=t.toString(16);return 4*(e.length-1)+o[parseInt(e[0],16)]}function a(t){return BigInt(t)<BigInt(0)}function l(t){return!t}function c(t,e){return BigInt(t)<<BigInt(e)}function p(t,e){return BigInt(t)>>BigInt(e)}const m=c,g=p;function F(t){return(BigInt(t)&BigInt(1))==BigInt(1)}function d(t){let e=BigInt(t);const n=[];for(;e;){if(e&BigInt(1)){const t=2-Number(e%BigInt(4));n.push(t),e-=BigInt(t)}else n.push(0);e>>=BigInt(1)}return n}function b(t){let e=BigInt(t);const n=[];for(;e;)e&BigInt(1)?n.push(1):n.push(0),e>>=BigInt(1);return n}function y(t){if(t>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Number too big");return Number(t)}function v(t,e){return BigInt(t)+BigInt(e)}function w(t,e){return BigInt(t)-BigInt(e)}function L(t){return-BigInt(t)}function B(t,e){return BigInt(t)*BigInt(e)}function E(t,e){return BigInt(t)**BigInt(e)}function O(t,e){return BigInt(t)/BigInt(e)}function q(t,e){return BigInt(t)%BigInt(e)}function A(t,e){return BigInt(t)==BigInt(e)}function _(t,e){return BigInt(t)>BigInt(e)}function x(t,e){return BigInt(t)>=BigInt(e)}function R(t,e){return BigInt(t)&BigInt(e)}function M(t,e,n,r){const i="0000000"+n.toString(16),s=new Uint32Array(t.buffer,e,r/4),o=1+(4*(i.length-7)-1>>5);for(let t=0;t<o;t++)s[t]=parseInt(i.substring(i.length-8*t-8,i.length-8*t),16);for(let t=o;t<s.length;t++)s[t]=0;for(let e=4*s.length;e<r;e++)t[e]=y(R(p(n,8*e),255))}function S(t,e,n,r){const i="0000000"+n.toString(16),s=new DataView(t.buffer,t.byteOffset+e,r),o=1+(4*(i.length-7)-1>>5);for(let t=0;t<o;t++)s.setUint32(r-4*t-4,parseInt(i.substring(i.length-8*t-8,i.length-8*t),16),!1);for(let t=0;t<r/4-o;t++)s[t]=0}function z(t,e,n){n=n||t.byteLength,e=e||0;const r=new Uint32Array(t.buffer,e,n/4),i=new Array(n/4);return r.forEach((t,e)=>i[i.length-e-1]=t.toString(16).padStart(8,"0")),h(i.join(""),16)}function I(t,e,n){n=n||t.byteLength,e=e||0;const r=new DataView(t.buffer,t.byteOffset+e,n),i=new Array(n/4);for(let t=0;t<n/4;t++)i[t]=r.getUint32(4*t,!1).toString(16).padStart(8,"0");return h(i.join(""),16)}function P(t,e){return t.toString(e)}function G(t){const e=new Uint8Array(Math.floor((f(t)-1)/8)+1);return M(e,0,t,e.byteLength),e}const T=u(0),U=u(1);var j={__proto__:null,fromString:h,e:u,fromArray:function(t,e){let n=BigInt(0);e=BigInt(e);for(let r=0;r<t.length;r++)n=n*e+BigInt(t[r]);return n},bitLength:f,isNegative:a,isZero:l,shiftLeft:c,shiftRight:p,shl:m,shr:g,isOdd:F,naf:d,bits:b,toNumber:y,toArray:function(t,e){const n=[];let r=BigInt(t);for(e=BigInt(e);r;)n.unshift(Number(r%e)),r/=e;return n},add:v,sub:w,neg:L,mul:B,square:function(t){return BigInt(t)*BigInt(t)},pow:E,exp:function(t,e){return BigInt(t)**BigInt(e)},abs:function(t){return BigInt(t)>=0?BigInt(t):-BigInt(t)},div:O,mod:q,eq:A,neq:function(t,e){return BigInt(t)!=BigInt(e)},lt:function(t,e){return BigInt(t)<BigInt(e)},gt:_,leq:function(t,e){return BigInt(t)<=BigInt(e)},geq:x,band:R,bor:function(t,e){return BigInt(t)|BigInt(e)},bxor:function(t,e){return BigInt(t)^BigInt(e)},land:function(t,e){return BigInt(t)&&BigInt(e)},lor:function(t,e){return BigInt(t)||BigInt(e)},lnot:function(t){return!BigInt(t)},toRprLE:M,toRprBE:S,fromRprLE:z,fromRprBE:I,toString:P,toLEBuff:G,zero:T,one:U};function C(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 N(t,e,n,r,i){const s=1<<n;if(1==s)return[e[r]];if(2==s)return[t.F.add(e[r],e[r+i]),t.F.sub(e[r],e[r+i])];const o=s>>1,h=N(t,e,n-1,r,2*i),u=N(t,e,n-1,r+i,2*i),f=new Array(s);for(let e=0;e<o;e++)f[e]=t.F.add(h[e],t.F.mul(t.roots[n][e],u[e])),f[e+o]=t.F.sub(h[e],t.F.mul(t.roots[n][e],u[e]));return f}function $(t,e,n){const r=1<<n;if(1==r)return[e[0]];const i=r>>1,s=$(t,e.slice(0,i),n-1),o=$(t,e.slice(i),n-1),h=new Array(r);for(let e=0;e<i;e++)h[e]=t.F.add(s[e],t.F.mul(t.roots[n][e],o[e])),h[e+i]=t.F.sub(s[e],t.F.mul(t.roots[n][e],o[e]));return h}const k=[];for(let t=0;t<256;t++)k[t]=Z(t,8);function Z(t,e){let n=0,r=t;for(let t=0;t<e;t++)n<<=1,n|=1&r,r>>=1;return n}function J(t,e){return(k[t>>>24]|k[t>>>16&255]<<8|k[t>>>8&255]<<16|k[255&t]<<24)>>>32-e}function D(t,e){for(let n=0;n<t.length;n++){const r=J(n,e);if(r>n){const e=t[n];t[n]=t[r],t[r]=e}}}function Q(t,e,n){let r;if(l(n))return t.zero;const i=d(n);if(1==i[i.length-1])r=e;else{if(-1!=i[i.length-1])throw new Error("invlaud NAF");r=t.neg(e)}for(let n=i.length-2;n>=0;n--)r=t.double(r),1==i[n]?r=t.add(r,e):-1==i[n]&&(r=t.sub(r,e));return r}function X(t,e,n){if(l(n))return t.one;const r=b(n);if(0==r.length)return t.one;let i=e;for(let n=r.length-2;n>=0;n--)i=t.square(i),r[n]&&(i=t.mul(i,e));return i}function V(t){if(t.m%2==1)if(A(q(t.p,4),1))if(A(q(t.p,8),1))if(A(q(t.p,16),1))!function(t){for(t.sqrt_q=E(t.p,t.m),t.sqrt_s=0,t.sqrt_t=w(t.sqrt_q,1);!F(t.sqrt_t);)t.sqrt_s=t.sqrt_s+1,t.sqrt_t=O(t.sqrt_t,2);let e=t.one;for(;t.eq(e,t.one);){const n=t.random();t.sqrt_z=t.pow(n,t.sqrt_t),e=t.pow(t.sqrt_z,2**(t.sqrt_s-1))}t.sqrt_tm1d2=O(w(t.sqrt_t,1),2),t.sqrt=function(t){const e=this;if(e.isZero(t))return e.zero;let n=e.pow(t,e.sqrt_tm1d2);const r=e.pow(e.mul(e.square(n),t),2**(e.sqrt_s-1));if(e.eq(r,e.negone))return null;let i=e.sqrt_s,s=e.mul(t,n),o=e.mul(s,n),h=e.sqrt_z;for(;!e.eq(o,e.one);){let t=e.square(o),r=1;for(;!e.eq(t,e.one);)t=e.square(t),r++;n=h;for(let t=0;t<i-r-1;t++)n=e.square(n);h=e.square(n),o=e.mul(o,h),s=e.mul(s,n),i=r}return e.geq(s,e.zero)?s:e.neg(s)}}(t);else{if(!A(q(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(!A(q(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 A(q(t.p,4),3)&&function(t){t.sqrt_q=E(t.p,t.m),t.sqrt_e1=O(w(t.sqrt_q,3),4),t.sqrt=function(e){if(this.isZero(e))return this.zero;const n=this.pow(e,this.sqrt_e1),r=this.mul(this.square(n),e);if(this.eq(r,this.negone))return null;const i=this.mul(n,e);return t.geq(i,t.zero)?i:t.neg(i)}}(t);else{const e=q(E(t.p,t.m/2),4);1==e?function(t){t.sqrt=function(){throw new Error("Sqrt alg 10 not implemented")}}(t):3==e?function(t){t.sqrt_q=E(t.p,t.m/2),t.sqrt_e34=O(w(t.sqrt_q,3),4),t.sqrt_e12=O(w(t.sqrt_q,1),2),t.frobenius=function(e,n){return e%2==1?t.conjugate(n):n},t.sqrt=function(t){const e=this,n=e.pow(t,e.sqrt_e34),r=e.mul(e.square(n),t),i=e.mul(e.frobenius(1,r),r);if(e.eq(i,e.negone))return null;const s=e.mul(n,t);let o;if(e.eq(r,e.negone))o=e.mul(s,[e.F.zero,e.F.one]);else{const t=e.pow(e.add(e.one,r),e.sqrt_e12);o=e.mul(t,s)}return e.geq(o,e.zero)?o:e.neg(o)}}(t):function(t){t.sqrt=function(){throw new Error("Sqrt alg 8 not implemented")}}(t)}}function W(t,e,n,r,i){t[e]=t[e]+t[n]>>>0,t[i]=(t[i]^t[e])>>>0,t[i]=(t[i]<<16|t[i]>>>16&65535)>>>0,t[r]=t[r]+t[i]>>>0,t[n]=(t[n]^t[r])>>>0,t[n]=(t[n]<<12|t[n]>>>20&4095)>>>0,t[e]=t[e]+t[n]>>>0,t[i]=(t[i]^t[e])>>>0,t[i]=(t[i]<<8|t[i]>>>24&255)>>>0,t[r]=t[r]+t[i]>>>0,t[n]=(t[n]^t[r])>>>0,t[n]=(t[n]<<7|t[n]>>>25&127)>>>0}class K{constructor(t){this.state=[1634760805,857760878,2036477234,1797285236,(t=t||[0,0,0,0,0,0,0,0])[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],0,0,0,0],this.idx=16,this.buff=new Array(16)}nextU32(){return 16==this.idx&&this.update(),this.buff[this.idx++]}nextU64(){return v(B(this.nextU32(),4294967296),this.nextU32())}nextBool(){return 1==(1&this.nextU32())}update(){for(let t=0;t<16;t++)this.buff[t]=this.state[t];for(let e=0;e<10;e++)W(t=this.buff,0,4,8,12),W(t,1,5,9,13),W(t,2,6,10,14),W(t,3,7,11,15),W(t,0,5,10,15),W(t,1,6,11,12),W(t,2,7,8,13),W(t,3,4,9,14);var t;for(let t=0;t<16;t++)this.buff[t]=this.buff[t]+this.state[t]>>>0;this.idx=0,this.state[12]=this.state[12]+1>>>0,0==this.state[12]&&(this.state[13]=this.state[13]+1>>>0,0==this.state[13]&&(this.state[14]=this.state[14]+1>>>0,0==this.state[14]&&(this.state[15]=this.state[15]+1>>>0)))}}const H="undefined"!=typeof window&&void 0!==window.document,Y="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node,tt="object"==typeof self&&self.constructor&&"DedicatedWorkerGlobalScope"===self.constructor.name;"undefined"!=typeof window&&"nodejs"===window.name||"undefined"!=typeof navigator&&(navigator.userAgent.includes("Node.js")||navigator.userAgent.includes("jsdom")),"undefined"!=typeof Deno&&void 0!==Deno.version&&Deno;const et=(()=>{const t="undefined"!=typeof window&&(window.crypto||window.msCrypto)||"undefined"!=typeof require&&require("crypto")||null;return t&&"function"==typeof t.getRandomValues?function(e){return t.getRandomValues(e)}:t&&"function"==typeof t.randomFillSync?function(e){return t.randomFillSync(e)}:t&&"function"==typeof t.randomBytes?function(e){let n=t.randomBytes(e.length);for(let t=0,r=n.length;t<r;++t)e[t]=n[t]}:function(t){throw new Error("getRandomValues not supported for this browser or node version!")}})();function nt(t){const e=new Uint8Array(t);return et(e),e}let rt=null;function it(){return rt||(rt=new K(function(){const t=nt(32),e=new Uint32Array(t.buffer),n=[];for(let t=0;t<8;t++)n.push(e[t]);return n}()),rt)}class st{constructor(t,e,n){this.F=e,this.G=t,this.opMulGF=n;let r=e.sqrt_t||e.t,i=e.sqrt_s||e.s,s=e.one;for(;e.eq(e.pow(s,e.half),e.one);)s=e.add(s,e.one);this.w=new Array(i+1),this.wi=new Array(i+1),this.w[i]=this.F.pow(s,r),this.wi[i]=this.F.inv(this.w[i]);let o=i-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(i,15))}_setRoots(t){for(let e=t;e>=0&&!this.roots[e];e--){let t=this.F.one;const n=1<<e,r=new Array(n);for(let i=0;i<n;i++)r[i]=t,t=this.F.mul(t,this.w[e]);this.roots[e]=r}}fft(t){if(t.length<=1)return t;const e=ot(t.length-1)+1;if(this._setRoots(e),t.length!=1<<e)throw new Error("Size must be multiple of 2");return ht(this,t,e,0,1)}ifft(t){if(t.length<=1)return t;const e=ot(t.length-1)+1;this._setRoots(e);const n=1<<e;if(t.length!=n)throw new Error("Size must be multiple of 2");const r=ht(this,t,e,0,1),i=this.F.inv(this.F.mulScalar(this.F.one,n)),s=new Array(n);for(let t=0;t<n;t++)s[t]=this.opMulGF(r[(n-t)%n],i);return s}}function ot(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 ht(t,e,n,r,i){const s=1<<n;if(1==s)return[e[r]];if(2==s)return[t.G.add(e[r],e[r+i]),t.G.sub(e[r],e[r+i])];const o=s>>1,h=ht(t,e,n-1,r,2*i),u=ht(t,e,n-1,r+i,2*i),f=new Array(s);for(let e=0;e<o;e++)f[e]=t.G.add(h[e],t.opMulGF(u[e],t.roots[n][e])),f[e+o]=t.G.sub(h[e],t.opMulGF(u[e],t.roots[n][e]));return f}class ut{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=f(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 e=this.negone>>this.one;this.nqr=this.two;let n=this.pow(this.nqr,e);for(;!this.eq(n,this.negone);)this.nqr=this.nqr+this.one,n=this.pow(this.nqr,e);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),V(this),this.FFT=new st(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,e){let n;if(e?16==e&&(n=BigInt("0x"+t)):n=BigInt(t),n<0){let t=-n;return t>=this.p&&(t%=this.p),this.p-t}return n>=this.p?n%this.p:n}add(t,e){const n=t+e;return n>=this.p?n-this.p:n}sub(t,e){return t>=e?t-e:this.p-e+t}neg(t){return t?this.p-t:t}mul(t,e){return t*e%this.p}mulScalar(t,e){return t*this.e(e)%this.p}square(t){return t*t%this.p}eq(t,e){return t==e}neq(t,e){return t!=e}lt(t,e){return(t>this.half?t-this.p:t)<(e>this.half?e-this.p:e)}gt(t,e){return(t>this.half?t-this.p:t)>(e>this.half?e-this.p:e)}leq(t,e){return(t>this.half?t-this.p:t)<=(e>this.half?e-this.p:e)}geq(t,e){return(t>this.half?t-this.p:t)>=(e>this.half?e-this.p:e)}div(t,e){return this.mul(t,this.inv(e))}idiv(t,e){if(!e)throw new Error("Division by zero");return t/e}inv(t){if(!t)throw new Error("Division by zero");let e=this.zero,n=this.p,r=this.one,i=t%this.p;for(;i;){let t=n/i;[e,r]=[r,e-t*r],[n,i]=[i,n-t*i]}return e<this.zero&&(e+=this.p),e}mod(t,e){return t%e}pow(t,e){return X(this,t,e)}exp(t,e){return X(this,t,e)}band(t,e){const n=t&e&this.mask;return n>=this.p?n-this.p:n}bor(t,e){const n=(t|e)&this.mask;return n>=this.p?n-this.p:n}bxor(t,e){const n=(t^e)&this.mask;return n>=this.p?n-this.p:n}bnot(t){const e=t^this.mask;return e>=this.p?e-this.p:e}shl(t,e){if(Number(e)<this.bitLength){const n=t<<e&this.mask;return n>=this.p?n-this.p:n}{const n=this.p-e;return Number(n)<this.bitLength?t>>n:this.zero}}shr(t,e){if(Number(e)<this.bitLength)return t>>e;{const n=this.p-e;if(Number(n)<this.bitLength){const e=t<<n&this.mask;return e>=this.p?e-this.p:e}return 0}}land(t,e){return t&&e?this.one:this.zero}lor(t,e){return t||e?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 e=this.s,n=this.nqr_to_t,r=this.pow(t,this.t),i=this.pow(t,this.add(this.t,this.one)>>this.one);for(;r!=this.one;){let t=this.square(r),s=1;for(;t!=this.one;)s++,t=this.square(t);let o=n;for(let t=0;t<e-s-1;t++)o=this.square(o);e=s,n=this.square(o),r=this.mul(r,n),i=this.mul(i,o)}return i>this.p>>this.one&&(i=this.neg(i)),i}normalize(t,e){if((t=BigInt(t,e))<0){let e=-t;return e>=this.p&&(e%=this.p),this.p-e}return t>=this.p?t%this.p:t}random(){const t=2*this.bitLength/8;let e=this.zero;for(let n=0;n<t;n++)e=(e<<BigInt(8))+BigInt(nt(1)[0]);return e%this.p}toString(t,e){let n;return e=e||10,n=t>this.half&&10==e?"-"+(this.p-t).toString(e):t.toString(e),n}isZero(t){return t==this.zero}fromRng(t){let e;do{e=this.zero;for(let n=0;n<this.n64;n++)e+=t.nextU64()<<BigInt(64*n);e&=this.mask}while(e>=this.p);return e=e*this.Ri%this.p,e}fft(t){return this.FFT.fft(t)}ifft(t){return this.FFT.ifft(t)}toRprLE(t,e,n){M(t,e,n,8*this.n64)}toRprBE(t,e,n){S(t,e,n,8*this.n64)}toRprBEM(t,e,n){return this.toRprBE(t,e,this.mul(this.R,n))}toRprLEM(t,e,n){return this.toRprLE(t,e,this.mul(this.R,n))}fromRprLE(t,e){return z(t,e,this.n8)}fromRprBE(t,e){return I(t,e,this.n8)}fromRprLEM(t,e){return this.mul(this.fromRprLE(t,e),this.Ri)}fromRprBEM(t,e){return this.mul(this.fromRprBE(t,e),this.Ri)}toObject(t){return t}}function ft(t,e){if(Array.isArray(e)){for(let n=e.length-1;n>=0;n--)if(!t.F.isZero(e[n]))return ft(t.F,e[n]);return 0}return _(e,t.neg(e))}function at(t,e){let n=t;void 0===e&&0==(e=Math.floor((f(t)-1)/8)+1)&&(e=1);const r=new Uint8Array(e),i=new DataView(r.buffer);let s=0;for(;s<e;)s+4<=e?(i.setUint32(s,Number(n&BigInt(4294967295)),!0),s+=4,n>>=BigInt(32)):s+2<=e?(i.setUint16(s,Number(n&BigInt(65535)),!0),s+=2,n>>=BigInt(16)):(i.setUint8(s,Number(n&BigInt(255)),!0),s+=1,n>>=BigInt(8));if(n)throw new Error("Number does not fit in this length");return r}const lt=[];for(let t=0;t<256;t++)lt[t]=ct(t,8);function ct(t,e){let n=0,r=t;for(let t=0;t<e;t++)n<<=1,n|=1&r,r>>=1;return n}function pt(t,e){return(lt[t>>>24]|lt[t>>>16&255]<<8|lt[t>>>8&255]<<16|lt[255&t]<<24)>>>32-e}function mt(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 gt(t,e){const n=t.byteLength/e,r=mt(n);if(n!=1<<r)throw new Error("Invalid number of pointers");for(let i=0;i<n;i++){const n=pt(i,r);if(i>n){const r=t.slice(i*e,(i+1)*e);t.set(t.slice(n*e,(n+1)*e),i*e),t.set(r,n*e)}}}function Ft(t,e){const n=new Uint8Array(e*t.length);for(let r=0;r<t.length;r++)n.set(t[r],r*e);return n}function dt(t,e){const n=t.byteLength/e,r=new Array(n);for(let i=0;i<n;i++)r[i]=t.slice(i*e,i*e+e);return r}var bt={__proto__:null,stringifyBigInts:function t(e){if("bigint"==typeof e||void 0!==e.eq)return e.toString(10);if(e instanceof Uint8Array)return z(e,0);if(Array.isArray(e))return e.map(t);if("object"==typeof e){const n={};return Object.keys(e).forEach(r=>{n[r]=t(e[r])}),n}return e},unstringifyBigInts:function t(e){if("string"==typeof e&&/^[0-9]+$/.test(e))return BigInt(e);if("string"==typeof e&&/^0x[0-9a-fA-F]+$/.test(e))return BigInt(e);if(Array.isArray(e))return e.map(t);if("object"==typeof e){if(null===e)return null;const n={};return Object.keys(e).forEach(r=>{n[r]=t(e[r])}),n}return e},beBuff2int:function(t){let e=BigInt(0),n=t.length,r=0;const i=new DataView(t.buffer,t.byteOffset,t.byteLength);for(;n>0;)n>=4?(n-=4,e+=BigInt(i.getUint32(n))<<BigInt(8*r),r+=4):n>=2?(n-=2,e+=BigInt(i.getUint16(n))<<BigInt(8*r),r+=2):(n-=1,e+=BigInt(i.getUint8(n))<<BigInt(8*r),r+=1);return e},beInt2Buff:function(t,e){let n=t;const r=new Uint8Array(e),i=new DataView(r.buffer);let s=e;for(;s>0;)s-4>=0?(s-=4,i.setUint32(s,Number(n&BigInt(4294967295))),n>>=BigInt(32)):s-2>=0?(s-=2,i.setUint16(s,Number(n&BigInt(65535))),n>>=BigInt(16)):(s-=1,i.setUint8(s,Number(n&BigInt(255))),n>>=BigInt(8));if(n)throw new Error("Number does not fit in this length");return r},leBuff2int:function(t){let e=BigInt(0),n=0;const r=new DataView(t.buffer,t.byteOffset,t.byteLength);for(;n<t.length;)n+4<=t.length?(e+=BigInt(r.getUint32(n,!0))<<BigInt(8*n),n+=4):n+2<=t.length?(e+=BigInt(r.getUint16(n,!0))<<BigInt(8*n),n+=2):(e+=BigInt(r.getUint8(n,!0))<<BigInt(8*n),n+=1);return e},leInt2Buff:at,stringifyFElements:function t(e,n){if("bigint"==typeof n||void 0!==n.eq)return n.toString(10);if(n instanceof Uint8Array)return e.toString(e.e(n));if(Array.isArray(n))return n.map(t.bind(this,e));if("object"==typeof n){const r={};return Object.keys(n).forEach(i=>{r[i]=t(e,n[i])}),r}return n},unstringifyFElements:function t(e,n){if("string"==typeof n&&/^[0-9]+$/.test(n))return e.e(n);if("string"==typeof n&&/^0x[0-9a-fA-F]+$/.test(n))return e.e(n);if(Array.isArray(n))return n.map(t.bind(this,e));if("object"==typeof n){if(null===n)return null;const r={};return Object.keys(n).forEach(i=>{r[i]=t(e,n[i])}),r}return n},bitReverse:pt,log2:mt,buffReverseBits:gt,array2buffer:Ft,buffer2array:dt};const yt=1<<30;class vt{constructor(t){this.buffers=[],this.byteLength=t;for(let e=0;e<t;e+=yt){const n=Math.min(t-e,yt);this.buffers.push(new Uint8Array(n))}}slice(t,e){void 0===e&&(e=this.byteLength),void 0===t&&(t=0);const n=e-t,r=Math.floor(t/yt);if(r==Math.floor((t+n-1)/yt)||0==n)return this.buffers[r].slice(t%yt,t%yt+n);let i,s=r,o=t%yt,h=n;for(;h>0;){const t=o+h>yt?yt-o:h,e=new Uint8Array(this.buffers[s].buffer,this.buffers[s].byteOffset+o,t);if(t==n)return e.slice();i||(i=n<=yt?new Uint8Array(n):new vt(n)),i.set(e,n-h),h-=t,s++,o=0}return i}set(t,e){void 0===e&&(e=0);const n=t.byteLength;if(0==n)return;const r=Math.floor(e/yt);if(r==Math.floor((e+n-1)/yt))return this.buffers[r].set(t instanceof vt&&1==t.buffers.length?t.buffers[0]:t,e%yt);let i=r,s=e%yt,o=n;for(;o>0;){const e=s+o>yt?yt-s:o,r=t.slice(n-o,n-o+e);new Uint8Array(this.buffers[i].buffer,this.buffers[i].byteOffset+s,e).set(r),o-=e,i++,s=0}}}function wt(t,e,n,r){return function(i){try{const s=Math.floor(i.byteLength/n);if(s*n!==i.byteLength)throw new Error("Invalid buffer size");const o=Math.floor(s/t.concurrency),h=[];for(let u=0;u<t.concurrency;u++){let f;if(f=u<t.concurrency-1?o:s-u*o,0==f)continue;const a=i.slice(u*o*n,u*o*n+f*n);h.push(t.queueAction([{cmd:"ALLOCSET",var:0,buff:a},{cmd:"ALLOC",var:1,len:r*f},{cmd:"CALL",fnName:e,params:[{var:0},{val:f},{var:1}]},{cmd:"GET",out:0,var:1,len:r*f}]))}return Promise.resolve(Promise.all(h)).then(function(t){let e;e=i instanceof vt?new vt(s*r):new Uint8Array(s*r);let n=0;for(let r=0;r<t.length;r++)e.set(t[r][0],n),n+=t[r][0].byteLength;return e})}catch(t){return Promise.reject(t)}}}class Lt{constructor(t,e,n,r){if(this.tm=t,this.prefix=e,this.p=r,this.n8=n,this.type="F1",this.m=1,this.half=p(r,U),this.bitLength=f(r),this.mask=w(c(U,this.bitLength),U),this.pOp1=t.alloc(n),this.pOp2=t.alloc(n),this.pOp3=t.alloc(n),this.tm.instance.exports[e+"_zero"](this.pOp1),this.zero=this.tm.getBuff(this.pOp1,this.n8),this.tm.instance.exports[e+"_one"](this.pOp1),this.one=this.tm.getBuff(this.pOp1,this.n8),this.negone=this.neg(this.one),this.two=this.add(this.one,this.one),this.n64=Math.floor(n/8),this.n32=Math.floor(n/4),8*this.n64!=this.n8)throw new Error("n8 must be a multiple of 8");this.half=p(this.p,U),this.nqr=this.two;let i=this.exp(this.nqr,this.half);for(;!this.eq(i,this.negone);)this.nqr=this.add(this.nqr,this.one),i=this.exp(this.nqr,this.half);this.shift=this.mul(this.nqr,this.nqr),this.shiftInv=this.inv(this.shift),this.s=0;let s=w(this.p,U);for(;!F(s);)this.s=this.s+1,s=p(s,U);this.w=[],this.w[this.s]=this.exp(this.nqr,s);for(let t=this.s-1;t>=0;t--)this.w[t]=this.square(this.w[t+1]);if(!this.eq(this.w[0],this.one))throw new Error("Error calculating roots of unity");this.batchToMontgomery=wt(t,e+"_batchToMontgomery",this.n8,this.n8),this.batchFromMontgomery=wt(t,e+"_batchFromMontgomery",this.n8,this.n8)}op2(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op2Bool(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2)}op1(t,e){return this.tm.setBuff(this.pOp1,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op1Bool(t,e){return this.tm.setBuff(this.pOp1,e),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3)}add(t,e){return this.op2("_add",t,e)}eq(t,e){return this.op2Bool("_eq",t,e)}isZero(t){return this.op1Bool("_isZero",t)}sub(t,e){return this.op2("_sub",t,e)}neg(t){return this.op1("_neg",t)}inv(t){return this.op1("_inverse",t)}toMontgomery(t){return this.op1("_toMontgomery",t)}fromMontgomery(t){return this.op1("_fromMontgomery",t)}mul(t,e){return this.op2("_mul",t,e)}div(t,e){return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+"_inverse"](this.pOp2,this.pOp2),this.tm.instance.exports[this.prefix+"_mul"](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}square(t){return this.op1("_square",t)}isSquare(t){return this.op1Bool("_isSquare",t)}sqrt(t){return this.op1("_sqrt",t)}exp(t,e){return e instanceof Uint8Array||(e=G(u(e))),this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+"_exp"](this.pOp1,this.pOp2,e.byteLength,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}isNegative(t){return this.op1Bool("_isNegative",t)}e(t,e){if(t instanceof Uint8Array)return t;let n=u(t,e);a(n)?(n=L(n),_(n,this.p)&&(n=q(n,this.p)),n=w(this.p,n)):_(n,this.p)&&(n=q(n,this.p));const r=at(n,this.n8);return this.toMontgomery(r)}toString(t,e){return P(z(this.fromMontgomery(t),0),e)}fromRng(t){let e;const n=new Uint8Array(this.n8);do{e=T;for(let n=0;n<this.n64;n++)e=v(e,c(t.nextU64(),64*n));e=R(e,this.mask)}while(x(e,this.p));return M(n,0,e,this.n8),n}random(){return this.fromRng(it())}toObject(t){return z(this.fromMontgomery(t),0)}fromObject(t){const e=new Uint8Array(this.n8);return M(e,0,t,this.n8),this.toMontgomery(e)}toRprLE(t,e,n){t.set(this.fromMontgomery(n),e)}toRprBE(t,e,n){const r=this.fromMontgomery(n);for(let t=0;t<this.n8/2;t++){const e=r[t];r[t]=r[this.n8-1-t],r[this.n8-1-t]=e}t.set(r,e)}fromRprLE(t,e){const n=t.slice(e=e||0,e+this.n8);return this.toMontgomery(n)}batchInverse(t){try{const e=this;let n=!1;const r=e.n8,i=e.n8;Array.isArray(t)?(t=Ft(t,r),n=!0):t=t.slice(0,t.byteLength);const s=Math.floor(t.byteLength/r);if(s*r!==t.byteLength)throw new Error("Invalid buffer size");const o=Math.floor(s/e.tm.concurrency),h=[];for(let n=0;n<e.tm.concurrency;n++){let u;if(u=n<e.tm.concurrency-1?o:s-n*o,0==u)continue;const f=t.slice(n*o*r,n*o*r+u*r);h.push(e.tm.queueAction([{cmd:"ALLOCSET",var:0,buff:f},{cmd:"ALLOC",var:1,len:i*u},{cmd:"CALL",fnName:e.prefix+"_batchInverse",params:[{var:0},{val:r},{val:u},{var:1},{val:i}]},{cmd:"GET",out:0,var:1,len:i*u}]))}return Promise.resolve(Promise.all(h)).then(function(e){let r;r=t instanceof vt?new vt(s*i):new Uint8Array(s*i);let o=0;for(let t=0;t<e.length;t++)r.set(e[t][0],o),o+=e[t][0].byteLength;return n?dt(r,i):r})}catch(t){return Promise.reject(t)}}}class Bt{constructor(t,e,n){this.tm=t,this.prefix=e,this.F=n,this.type="F2",this.m=2*n.m,this.n8=2*this.F.n8,this.n32=2*this.F.n32,this.n64=2*this.F.n64,this.pOp1=t.alloc(2*n.n8),this.pOp2=t.alloc(2*n.n8),this.pOp3=t.alloc(2*n.n8),this.tm.instance.exports[e+"_zero"](this.pOp1),this.zero=t.getBuff(this.pOp1,this.n8),this.tm.instance.exports[e+"_one"](this.pOp1),this.one=t.getBuff(this.pOp1,this.n8),this.negone=this.neg(this.one),this.two=this.add(this.one,this.one)}op2(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op2Bool(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2)}op1(t,e){return this.tm.setBuff(this.pOp1,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op1Bool(t,e){return this.tm.setBuff(this.pOp1,e),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3)}add(t,e){return this.op2("_add",t,e)}eq(t,e){return this.op2Bool("_eq",t,e)}isZero(t){return this.op1Bool("_isZero",t)}sub(t,e){return this.op2("_sub",t,e)}neg(t){return this.op1("_neg",t)}inv(t){return this.op1("_inverse",t)}isNegative(t){return this.op1Bool("_isNegative",t)}toMontgomery(t){return this.op1("_toMontgomery",t)}fromMontgomery(t){return this.op1("_fromMontgomery",t)}mul(t,e){return this.op2("_mul",t,e)}mul1(t,e){return this.op2("_mul1",t,e)}div(t,e){return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+"_inverse"](this.pOp2,this.pOp2),this.tm.instance.exports[this.prefix+"_mul"](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}square(t){return this.op1("_square",t)}isSquare(t){return this.op1Bool("_isSquare",t)}sqrt(t){return this.op1("_sqrt",t)}exp(t,e){return e instanceof Uint8Array||(e=G(u(e))),this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+"_exp"](this.pOp1,this.pOp2,e.byteLength,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}e(t,e){if(t instanceof Uint8Array)return t;if(Array.isArray(t)&&2==t.length){const n=this.F.e(t[0],e),r=this.F.e(t[1],e),i=new Uint8Array(2*this.F.n8);return i.set(n),i.set(r,2*this.F.n8),i}throw new Error("invalid F2")}toString(t,e){return`[${this.F.toString(t.slice(0,this.F.n8),e)}, ${this.F.toString(t.slice(this.F.n8),e)}]`}fromRng(t){const e=this.F.fromRng(t),n=this.F.fromRng(t),r=new Uint8Array(2*this.F.n8);return r.set(e),r.set(n,this.F.n8),r}random(){return this.fromRng(it())}toObject(t){return[this.F.toObject(t.slice(0,this.F.n8)),this.F.toObject(t.slice(this.F.n8,2*this.F.n8))]}fromObject(t){const e=new Uint8Array(2*this.F.n8),n=this.F.fromObject(t[0]),r=this.F.fromObject(t[1]);return e.set(n),e.set(r,this.F.n8),e}c1(t){return t.slice(0,this.F.n8)}c2(t){return t.slice(this.F.n8)}}class Et{constructor(t,e,n){this.tm=t,this.prefix=e,this.F=n,this.type="F3",this.m=3*n.m,this.n8=3*this.F.n8,this.n32=3*this.F.n32,this.n64=3*this.F.n64,this.pOp1=t.alloc(3*n.n8),this.pOp2=t.alloc(3*n.n8),this.pOp3=t.alloc(3*n.n8),this.tm.instance.exports[e+"_zero"](this.pOp1),this.zero=t.getBuff(this.pOp1,this.n8),this.tm.instance.exports[e+"_one"](this.pOp1),this.one=t.getBuff(this.pOp1,this.n8),this.negone=this.neg(this.one),this.two=this.add(this.one,this.one)}op2(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op2Bool(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2)}op1(t,e){return this.tm.setBuff(this.pOp1,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op1Bool(t,e){return this.tm.setBuff(this.pOp1,e),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3)}eq(t,e){return this.op2Bool("_eq",t,e)}isZero(t){return this.op1Bool("_isZero",t)}add(t,e){return this.op2("_add",t,e)}sub(t,e){return this.op2("_sub",t,e)}neg(t){return this.op1("_neg",t)}inv(t){return this.op1("_inverse",t)}isNegative(t){return this.op1Bool("_isNegative",t)}toMontgomery(t){return this.op1("_toMontgomery",t)}fromMontgomery(t){return this.op1("_fromMontgomery",t)}mul(t,e){return this.op2("_mul",t,e)}div(t,e){return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+"_inverse"](this.pOp2,this.pOp2),this.tm.instance.exports[this.prefix+"_mul"](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}square(t){return this.op1("_square",t)}isSquare(t){return this.op1Bool("_isSquare",t)}sqrt(t){return this.op1("_sqrt",t)}exp(t,e){return e instanceof Uint8Array||(e=G(u(e))),this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+"_exp"](this.pOp1,this.pOp2,e.byteLength,this.pOp3),this.getBuff(this.pOp3,this.n8)}e(t,e){if(t instanceof Uint8Array)return t;if(Array.isArray(t)&&3==t.length){const n=this.F.e(t[0],e),r=this.F.e(t[1],e),i=this.F.e(t[2],e),s=new Uint8Array(3*this.F.n8);return s.set(n),s.set(r,this.F.n8),s.set(i,2*this.F.n8),s}throw new Error("invalid F3")}toString(t,e){return`[${this.F.toString(t.slice(0,this.F.n8),e)}, ${this.F.toString(t.slice(this.F.n8,2*this.F.n8),e)}, ${this.F.toString(t.slice(2*this.F.n8),e)}]`}fromRng(t){const e=this.F.fromRng(t),n=this.F.fromRng(t),r=this.F.fromRng(t),i=new Uint8Array(3*this.F.n8);return i.set(e),i.set(n,this.F.n8),i.set(r,2*this.F.n8),i}random(){return this.fromRng(it())}toObject(t){return[this.F.toObject(t.slice(0,this.F.n8)),this.F.toObject(t.slice(this.F.n8,2*this.F.n8)),this.F.toObject(t.slice(2*this.F.n8,3*this.F.n8))]}fromObject(t){const e=new Uint8Array(3*this.F.n8),n=this.F.fromObject(t[0]),r=this.F.fromObject(t[1]),i=this.F.fromObject(t[2]);return e.set(n),e.set(r,this.F.n8),e.set(i,2*this.F.n8),e}c1(t){return t.slice(0,this.F.n8)}c2(t){return t.slice(this.F.n8,2*this.F.n8)}c3(t){return t.slice(2*this.F.n8)}}class Ot{constructor(t,e,n,r,i,s){this.tm=t,this.prefix=e,this.F=n,this.pOp1=t.alloc(3*n.n8),this.pOp2=t.alloc(3*n.n8),this.pOp3=t.alloc(3*n.n8),this.tm.instance.exports[e+"_zero"](this.pOp1),this.zero=this.tm.getBuff(this.pOp1,3*n.n8),this.tm.instance.exports[e+"_zeroAffine"](this.pOp1),this.zeroAffine=this.tm.getBuff(this.pOp1,2*n.n8),this.one=this.tm.getBuff(r,3*n.n8),this.g=this.one,this.oneAffine=this.tm.getBuff(r,2*n.n8),this.gAffine=this.oneAffine,this.b=this.tm.getBuff(i,n.n8),s&&(this.cofactor=G(s)),this.negone=this.neg(this.one),this.two=this.add(this.one,this.one),this.batchLEMtoC=wt(t,e+"_batchLEMtoC",2*n.n8,n.n8),this.batchLEMtoU=wt(t,e+"_batchLEMtoU",2*n.n8,2*n.n8),this.batchCtoLEM=wt(t,e+"_batchCtoLEM",n.n8,2*n.n8),this.batchUtoLEM=wt(t,e+"_batchUtoLEM",2*n.n8,2*n.n8),this.batchToJacobian=wt(t,e+"_batchToJacobian",2*n.n8,3*n.n8),this.batchToAffine=wt(t,e+"_batchToAffine",3*n.n8,2*n.n8)}op2(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,3*this.F.n8)}op2bool(t,e,n){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,n),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3)}op1(t,e){return this.tm.setBuff(this.pOp1,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,3*this.F.n8)}op1Affine(t,e){return this.tm.setBuff(this.pOp1,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,2*this.F.n8)}op1Bool(t,e){return this.tm.setBuff(this.pOp1,e),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3)}add(t,e){if(t.byteLength==3*this.F.n8){if(e.byteLength==3*this.F.n8)return this.op2("_add",t,e);if(e.byteLength==2*this.F.n8)return this.op2("_addMixed",t,e);throw new Error("invalid point size")}if(t.byteLength==2*this.F.n8){if(e.byteLength==3*this.F.n8)return this.op2("_addMixed",e,t);if(e.byteLength==2*this.F.n8)return this.op2("_addAffine",t,e);throw new Error("invalid point size")}throw new Error("invalid point size")}sub(t,e){if(t.byteLength==3*this.F.n8){if(e.byteLength==3*this.F.n8)return this.op2("_sub",t,e);if(e.byteLength==2*this.F.n8)return this.op2("_subMixed",t,e);throw new Error("invalid point size")}if(t.byteLength==2*this.F.n8){if(e.byteLength==3*this.F.n8)return this.op2("_subMixed",e,t);if(e.byteLength==2*this.F.n8)return this.op2("_subAffine",t,e);throw new Error("invalid point size")}throw new Error("invalid point size")}neg(t){if(t.byteLength==3*this.F.n8)return this.op1("_neg",t);if(t.byteLength==2*this.F.n8)return this.op1Affine("_negAffine",t);throw new Error("invalid point size")}double(t){if(t.byteLength==3*this.F.n8)return this.op1("_double",t);if(t.byteLength==2*this.F.n8)return this.op1("_doubleAffine",t);throw new Error("invalid point size")}isZero(t){if(t.byteLength==3*this.F.n8)return this.op1Bool("_isZero",t);if(t.byteLength==2*this.F.n8)return this.op1Bool("_isZeroAffine",t);throw new Error("invalid point size")}timesScalar(t,e){let n;if(e instanceof Uint8Array||(e=G(u(e))),t.byteLength==3*this.F.n8)n=this.prefix+"_timesScalar";else{if(t.byteLength!=2*this.F.n8)throw new Error("invalid point size");n=this.prefix+"_timesScalarAffine"}return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[n](this.pOp1,this.pOp2,e.byteLength,this.pOp3),this.tm.getBuff(this.pOp3,3*this.F.n8)}timesFr(t,e){let n;if(t.byteLength==3*this.F.n8)n=this.prefix+"_timesFr";else{if(t.byteLength!=2*this.F.n8)throw new Error("invalid point size");n=this.prefix+"_timesFrAffine"}return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[n](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,3*this.F.n8)}eq(t,e){if(t.byteLength==3*this.F.n8){if(e.byteLength==3*this.F.n8)return this.op2bool("_eq",t,e);if(e.byteLength==2*this.F.n8)return this.op2bool("_eqMixed",t,e);throw new Error("invalid point size")}if(t.byteLength==2*this.F.n8){if(e.byteLength==3*this.F.n8)return this.op2bool("_eqMixed",e,t);if(e.byteLength==2*this.F.n8)return this.op2bool("_eqAffine",t,e);throw new Error("invalid point size")}throw new Error("invalid point size")}toAffine(t){if(t.byteLength==3*this.F.n8)return this.op1Affine("_toAffine",t);if(t.byteLength==2*this.F.n8)return t;throw new Error("invalid point size")}toJacobian(t){if(t.byteLength==3*this.F.n8)return t;if(t.byteLength==2*this.F.n8)return this.op1("_toJacobian",t);throw new Error("invalid point size")}toRprUncompressed(t,e,n){if(this.tm.setBuff(this.pOp1,n),n.byteLength==3*this.F.n8)this.tm.instance.exports[this.prefix+"_toAffine"](this.pOp1,this.pOp1);else if(n.byteLength!=2*this.F.n8)throw new Error("invalid point size");this.tm.instance.exports[this.prefix+"_LEMtoU"](this.pOp1,this.pOp1);const r=this.tm.getBuff(this.pOp1,2*this.F.n8);t.set(r,e)}fromRprUncompressed(t,e){const n=t.slice(e,e+2*this.F.n8);return this.tm.setBuff(this.pOp1,n),this.tm.instance.exports[this.prefix+"_UtoLEM"](this.pOp1,this.pOp1),this.tm.getBuff(this.pOp1,2*this.F.n8)}toRprCompressed(t,e,n){if(this.tm.setBuff(this.pOp1,n),n.byteLength==3*this.F.n8)this.tm.instance.exports[this.prefix+"_toAffine"](this.pOp1,this.pOp1);else if(n.byteLength!=2*this.F.n8)throw new Error("invalid point size");this.tm.instance.exports[this.prefix+"_LEMtoC"](this.pOp1,this.pOp1);const r=this.tm.getBuff(this.pOp1,this.F.n8);t.set(r,e)}fromRprCompressed(t,e){const n=t.slice(e,e+this.F.n8);return this.tm.setBuff(this.pOp1,n),this.tm.instance.exports[this.prefix+"_CtoLEM"](this.pOp1,this.pOp2),this.tm.getBuff(this.pOp2,2*this.F.n8)}toUncompressed(t){const e=new Uint8Array(2*this.F.n8);return this.toRprUncompressed(e,0,t),e}toRprLEM(t,e,n){if(n.byteLength!=2*this.F.n8){if(n.byteLength!=3*this.F.n8)throw new Error("invalid point size");{this.tm.setBuff(this.pOp1,n),this.tm.instance.exports[this.prefix+"_toAffine"](this.pOp1,this.pOp1);const r=this.tm.getBuff(this.pOp1,2*this.F.n8);t.set(r,e)}}else t.set(n,e)}fromRprLEM(t,e){return t.slice(e=e||0,e+2*this.F.n8)}toString(t,e){if(t.byteLength==3*this.F.n8)return`[ ${this.F.toString(t.slice(0,this.F.n8),e)}, ${this.F.toString(t.slice(this.F.n8,2*this.F.n8),e)}, ${this.F.toString(t.slice(2*this.F.n8),e)} ]`;if(t.byteLength==2*this.F.n8)return`[ ${this.F.toString(t.slice(0,this.F.n8),e)}, ${this.F.toString(t.slice(this.F.n8),e)} ]`;throw new Error("invalid point size")}isValid(t){if(this.isZero(t))return!0;const e=this.F,n=this.toAffine(t),r=n.slice(0,this.F.n8),i=n.slice(this.F.n8,2*this.F.n8),s=e.add(e.mul(e.square(r),r),this.b),o=e.square(i);return e.eq(s,o)}fromRng(t){const e=this.F;let n,r,i=[];do{i[0]=e.fromRng(t),n=t.nextBool(),r=e.add(e.mul(e.square(i[0]),i[0]),this.b)}while(!e.isSquare(r));i[1]=e.sqrt(r),n^e.isNegative(i[1])&&(i[1]=e.neg(i[1]));let s=new Uint8Array(2*this.F.n8);return s.set(i[0]),s.set(i[1],this.F.n8),this.cofactor&&(s=this.timesScalar(s,this.cofactor)),s}toObject(t){if(this.isZero(t))return[this.F.toObject(this.F.zero),this.F.toObject(this.F.one),this.F.toObject(this.F.zero)];const e=this.F.toObject(t.slice(0,this.F.n8)),n=this.F.toObject(t.slice(this.F.n8,2*this.F.n8));let r;return r=this.F.toObject(t.byteLength==3*this.F.n8?t.slice(2*this.F.n8,3*this.F.n8):this.F.one),[e,n,r]}fromObject(t){const e=this.F.fromObject(t[0]),n=this.F.fromObject(t[1]);let r;if(r=3==t.length?this.F.fromObject(t[2]):this.F.one,this.F.isZero(r,this.F.one))return this.zeroAffine;if(this.F.eq(r,this.F.one)){const t=new Uint8Array(2*this.F.n8);return t.set(e),t.set(n,this.F.n8),t}{const t=new Uint8Array(3*this.F.n8);return t.set(e),t.set(n,this.F.n8),t.set(r,2*this.F.n8),t}}e(t){return t instanceof Uint8Array?t:this.fromObject(t)}x(t){return this.toAffine(t).slice(0,this.F.n8)}y(t){return this.toAffine(t).slice(this.F.n8)}}function qt(t){const e=function(t){try{const e=new Uint8Array(t.code);return Promise.resolve(WebAssembly.compile(e)).then(function(e){return i=new WebAssembly.Memory({initial:t.init,maximum:n}),Promise.resolve(WebAssembly.instantiate(e,{env:{memory:i}})).then(function(t){r=t})})}catch(t){return Promise.reject(t)}},n=32767;let r,i;function s(t){const e=new Uint32Array(i.buffer,0,1);for(;3&e[0];)e[0]++;const r=e[0];if(e[0]+=t,e[0]+t>i.buffer.byteLength){const r=i.buffer.byteLength/65536;let s=Math.floor((e[0]+t)/65536)+1;s>n&&(s=n),i.grow(s-r)}return r}function o(t){const e=s(t.byteLength);return u(e,t),e}function h(t,e){const n=new Uint8Array(i.buffer);return new Uint8Array(n.buffer,n.byteOffset+t,e)}function u(t,e){new Uint8Array(i.buffer).set(new Uint8Array(e),t)}function f(t){if("INIT"==t[0].cmd)return e(t[0]);const n={vars:[],out:[]},f=new Uint32Array(i.buffer,0,1)[0];for(let e=0;e<t.length;e++)switch(t[e].cmd){case"ALLOCSET":n.vars[t[e].var]=o(t[e].buff);break;case"ALLOC":n.vars[t[e].var]=s(t[e].len);break;case"SET":u(n.vars[t[e].var],t[e].buff);break;case"CALL":{const i=[];for(let r=0;r<t[e].params.length;r++){const s=t[e].params[r];void 0!==s.var?i.push(n.vars[s.var]+(s.offset||0)):void 0!==s.val&&i.push(s.val)}r.exports[t[e].fnName](...i);break}case"GET":n.out[t[e].out]=h(n.vars[t[e].var],t[e].len).slice();break;default:throw new Error("Invalid cmd")}return new Uint32Array(i.buffer,0,1)[0]=f,n.out}return t&&(t.onmessage=function(n){let r;if(r=n.data?n.data:n,"INIT"==r[0].cmd)e(r[0]).then(function(){t.postMessage(r.result)});else if("TERMINATE"==r[0].cmd)t.close();else{const e=f(r);t.postMessage(e)}}),f}class At{constructor(){this.promise=new Promise((t,e)=>{this.reject=e,this.resolve=t})}}var _t;const xt="data:application/javascript;base64,"+(_t="("+qt.toString()+")(self)",H||tt?globalThis.btoa(_t):Buffer.from(_t).toString("base64"));class Rt{constructor(){this.actionQueue=[],this.oldPFree=0}startSyncOp(){if(0!=this.oldPFree)throw new Error("Sync operation in progress");this.oldPFree=this.u32[0]}endSyncOp(){if(0==this.oldPFree)throw new Error("No sync operation in progress");this.u32[0]=this.oldPFree,this.oldPFree=0}postAction(t,e,n,r){if(this.working[t])throw new Error("Posting a job t a working worker");return this.working[t]=!0,this.pendingDeferreds[t]=r||new At,this.workers[t].postMessage(e,n),this.pendingDeferreds[t].promise}processWorks(){for(let t=0;t<this.workers.length&&this.actionQueue.length>0;t++)if(0==this.working[t]){const e=this.actionQueue.shift();this.postAction(t,e.data,e.transfers,e.deferred)}}queueAction(t,e){const n=new At;if(this.singleThread){const e=this.taskManager(t);n.resolve(e)}else this.actionQueue.push({data:t,transfers:e,deferred:n}),this.processWorks();return n.promise}resetMemory(){this.u32[0]=this.initalPFree}allocBuff(t){const e=this.alloc(t.byteLength);return this.setBuff(e,t),e}getBuff(t,e){return this.u8.slice(t,t+e)}setBuff(t,e){this.u8.set(new Uint8Array(e),t)}alloc(t){for(;3&this.u32[0];)this.u32[0]++;const e=this.u32[0];return this.u32[0]+=t,e}terminate(){try{const t=this;for(let e=0;e<t.workers.length;e++)t.workers[e].postMessage([{cmd:"TERMINATE"}]);return Promise.resolve(new Promise(t=>setTimeout(t,200))).then(function(){})}catch(t){return Promise.reject(t)}}}function Mt(t,e){const n=t[e],r=t.Fr,i=t.tm;t[e].batchApplyKey=function(t,s,o,h,u){try{let f,a,l,c,p;if(h=h||"affine",u=u||"affine","G1"==e)"jacobian"==h?(l=3*n.F.n8,f="g1m_batchApplyKey"):(l=2*n.F.n8,f="g1m_batchApplyKeyMixed"),c=3*n.F.n8,"jacobian"==u?p=3*n.F.n8:(a="g1m_batchToAffine",p=2*n.F.n8);else if("G2"==e)"jacobian"==h?(l=3*n.F.n8,f="g2m_batchApplyKey"):(l=2*n.F.n8,f="g2m_batchApplyKeyMixed"),c=3*n.F.n8,"jacobian"==u?p=3*n.F.n8:(a="g2m_batchToAffine",p=2*n.F.n8);else{if("Fr"!=e)throw new Error("Invalid group: "+e);f="frm_batchApplyKey",l=n.n8,c=n.n8,p=n.n8}const m=Math.floor(t.byteLength/l),g=Math.floor(m/i.concurrency),F=[];o=r.e(o);let d=r.e(s);for(let e=0;e<i.concurrency;e++){let n;if(n=e<i.concurrency-1?g:m-e*g,0==n)continue;const s=[];s.push({cmd:"ALLOCSET",var:0,buff:t.slice(e*g*l,e*g*l+n*l)}),s.push({cmd:"ALLOCSET",var:1,buff:d}),s.push({cmd:"ALLOCSET",var:2,buff:o}),s.push({cmd:"ALLOC",var:3,len:n*Math.max(c,p)}),s.push({cmd:"CALL",fnName:f,params:[{var:0},{val:n},{var:1},{var:2},{var:3}]}),a&&s.push({cmd:"CALL",fnName:a,params:[{var:3},{val:n},{var:3}]}),s.push({cmd:"GET",out:0,var:3,len:n*p}),F.push(i.queueAction(s)),d=r.mul(d,r.exp(o,n))}return Promise.resolve(Promise.all(F)).then(function(e){let n;n=t instanceof vt?new vt(m*p):new Uint8Array(m*p);let r=0;for(let t=0;t<e.length;t++)n.set(e[t][0],r),r+=e[t][0].byteLength;return n})}catch(t){return Promise.reject(t)}}}const St=[1,1,1,1,2,3,4,5,6,7,7,8,9,10,11,12,13,13,14,15,16,16,17,17,17,17,17,17,17,17,17,17];function zt(t,e){const n=function(t,n,o,h,u){try{const f=1<<22,a=1024;let l;if("G1"==e)l="affine"==o?2*i.F.n8:3*i.F.n8;else{if("G2"!=e)throw new Error("Invalid group");l="affine"==o?2*i.F.n8:3*i.F.n8}const c=Math.floor(t.byteLength/l),p=Math.floor(n.byteLength/c);if(p*c!=n.byteLength)throw new Error("Scalar size does not match");const m=St[mt(c)],g=Math.floor((8*p-1)/m)+1;let F;F=Math.floor(c/(s.concurrency/g)),F>f&&(F=f),F<a&&(F=a);const d=[];for(let e=0;e<c;e+=F){h&&h.debug(`Multiexp start: ${u}: ${e}/${c}`);const i=Math.min(c-e,F),s=t.slice(e*l,(e+i)*l),f=n.slice(e*p,(e+i)*p);d.push(r(s,f,o,h,u).then(t=>(h&&h.debug(`Multiexp end: ${u}: ${e}/${c}`),t)))}return Promise.resolve(Promise.all(d)).then(function(t){let e=i.zero;for(let n=t.length-1;n>=0;n--)e=i.add(e,t[n]);return e})}catch(t){return Promise.reject(t)}},r=function(t,n,r,s,o){try{if(!(t instanceof Uint8Array))throw s&&s.error(`${o} _multiExpChunk buffBases is not Uint8Array`),new Error(`${o} _multiExpChunk buffBases is not Uint8Array`);if(!(n instanceof Uint8Array))throw s&&s.error(`${o} _multiExpChunk buffScalars is not Uint8Array`),new Error(`${o} _multiExpChunk buffScalars is not Uint8Array`);let h,u;if(r=r||"affine","G1"==e)"affine"==r?(u="g1m_multiexpAffine_chunk",h=2*i.F.n8):(u="g1m_multiexp_chunk",h=3*i.F.n8);else{if("G2"!=e)throw new Error("Invalid group");"affine"==r?(u="g2m_multiexpAffine_chunk",h=2*i.F.n8):(u="g2m_multiexp_chunk",h=3*i.F.n8)}const f=Math.floor(t.byteLength/h);if(0==f)return Promise.resolve(i.zero);const a=Math.floor(n.byteLength/f);if(a*f!=n.byteLength)throw new Error("Scalar size does not match");const l=St[mt(f)],c=Math.floor((8*a-1)/l)+1,p=[];for(let e=0;e<c;e++){const r=[{cmd:"ALLOCSET",var:0,buff:t},{cmd:"ALLOCSET",var:1,buff:n},{cmd:"ALLOC",var:2,len:3*i.F.n8},{cmd:"CALL",fnName:u,params:[{var:0},{var:1},{val:a},{val:f},{val:e*l},{val:Math.min(8*a-e*l,l)},{var:2}]},{cmd:"GET",out:0,var:2,len:3*i.F.n8}];p.push(i.tm.queueAction(r))}return Promise.resolve(Promise.all(p)).then(function(t){let e=i.zero;for(let n=t.length-1;n>=0;n--){if(!i.isZero(e))for(let t=0;t<l;t++)e=i.double(e);e=i.add(e,t[n][0])}return e})}catch(t){return Promise.reject(t)}},i=t[e],s=i.tm;i.multiExp=function(t,e,r,i){return Promise.resolve(n(t,e,"jacobian",r,i))},i.multiExpAffine=function(t,e,r,i){return Promise.resolve(n(t,e,"affine",r,i))}}function It(t,e,n){if(!t.s){if(n instanceof Pt){if(!n.s)return void(n.o=It.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(It.bind(null,t,e),It.bind(null,t,2));t.s=e,t.v=n;const r=t.o;r&&r(t)}}const Pt=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){const r=new t,i=this.s;if(i){const t=1&i?e:n;if(t){try{It(r,1,t(this.v))}catch(t){It(r,2,t)}return r}return this}return this.o=function(t){try{const i=t.v;1&t.s?It(r,1,e?e(i):i):n?It(r,1,n(i)):It(r,2,i)}catch(t){It(r,2,t)}},r},t}();function Gt(t){return t instanceof Pt&&1&t.s}function Tt(t,e,n){for(var r;;){var i=t();if(Gt(i)&&(i=i.v),!i)return s;if(i.then){r=0;break}var s=n();if(s&&s.then){if(!Gt(s)){r=1;break}s=s.s}if(e){var o=e();if(o&&o.then&&!Gt(o)){r=2;break}}}var h=new Pt,u=It.bind(null,h,2);return(0===r?i.then(a):1===r?s.then(f):o.then(l)).then(void 0,u),h;function f(r){s=r;do{if(e&&(o=e())&&o.then&&!Gt(o))return void o.then(l).then(void 0,u);if(!(i=t())||Gt(i)&&!i.v)return void It(h,1,s);if(i.then)return void i.then(a).then(void 0,u);Gt(s=n())&&(s=s.v)}while(!s||!s.then);s.then(f).then(void 0,u)}function a(t){t?(s=n())&&s.then?s.then(f).then(void 0,u):f(s):It(h,1,s)}function l(){(i=t())?i.then?i.then(a).then(void 0,u):a(i):It(h,1,s)}}function Ut(t,e){const n=function(t,n,r,h,u,f,a,l,c){try{const p=65536,m=16;let g,F,d,b,y,v;if("G1"==e)"affine"==f?(y=2*i.F.n8,F="g1m_batchToJacobian"):y=3*i.F.n8,v=3*i.F.n8,g="g1m_"+r,"affine"==a?(d="g1m_batchToAffine",b=2*i.F.n8):b=3*i.F.n8;else if("G2"==e)"affine"==f?(y=2*i.F.n8,F="g2m_batchToJacobian"):y=3*i.F.n8,g="g2m_"+r,v=3*i.F.n8,"affine"==a?(d="g2m_batchToAffine",b=2*i.F.n8):b=3*i.F.n8;else{if("Fr"!=e)throw new Error("Invalid group");y=s.n8,b=s.n8,v=s.n8,g="frm_"+r}if(t.byteLength!=n.byteLength)throw new Error("Invalid buffer size");const w=Math.floor(t.byteLength/y);if(w!=1<<mt(w))throw new Error("Invalid number of points");let L=Math.floor(w/o.concurrency);L<m&&(L=m),L>p&&(L=p);const B=[];for(let e=0;e<w;e+=L){l&&l.debug(`${c}: fftJoinExt Start: ${e}/${w}`);const r=Math.min(w-e,L),i=s.mul(h,s.exp(u,e)),f=[],a=t.slice(e*y,(e+r)*y),p=n.slice(e*y,(e+r)*y);f.push({cmd:"ALLOC",var:0,len:v*r}),f.push({cmd:"SET",var:0,buff:a}),f.push({cmd:"ALLOC",var:1,len:v*r}),f.push({cmd:"SET",var:1,buff:p}),f.push({cmd:"ALLOCSET",var:2,buff:i}),f.push({cmd:"ALLOCSET",var:3,buff:u}),F&&(f.push({cmd:"CALL",fnName:F,params:[{var:0},{val:r},{var:0}]}),f.push({cmd:"CALL",fnName:F,params:[{var:1},{val:r},{var:1}]})),f.push({cmd:"CALL",fnName:g,params:[{var:0},{var:1},{val:r},{var:2},{var:3},{val:s.s}]}),d&&(f.push({cmd:"CALL",fnName:d,params:[{var:0},{val:r},{var:0}]}),f.push({cmd:"CALL",fnName:d,params:[{var:1},{val:r},{var:1}]})),f.push({cmd:"GET",out:0,var:0,len:r*b}),f.push({cmd:"GET",out:1,var:1,len:r*b}),B.push(o.queueAction(f).then(t=>(l&&l.debug(`${c}: fftJoinExt End: ${e}/${w}`),t)))}return Promise.resolve(Promise.all(B)).then(function(t){let e,n;w*b>1<<28?(e=new vt(w*b),n=new vt(w*b)):(e=new Uint8Array(w*b),n=new Uint8Array(w*b));let r=0;for(let i=0;i<t.length;i++)e.set(t[i][0],r),n.set(t[i][1],r),r+=t[i][0].byteLength;return[e,n]})}catch(t){return Promise.reject(t)}},r=function(t,h,u,f,a,l){try{let p;function c(e){if(p)return e;let n,r,i;h&&(n=s.inv(s.e(E))),gt(t,g);let u=Math.min(1<<m,E),f=E/u;for(;f<o.concurrency&&u>=16;)f*=2,u/=2;const c=mt(u),q=[];for(let e=0;e<f;e++){a&&a.debug(`${l}: fft ${O} mix start: ${e}/${f}`);const r=[];r.push({cmd:"ALLOC",var:0,len:F*u});const i=t.slice(u*e*g,u*(e+1)*g);r.push({cmd:"SET",var:0,buff:i}),b&&r.push({cmd:"CALL",fnName:b,params:[{var:0},{val:u},{var:0}]});for(let t=1;t<=c;t++)r.push({cmd:"CALL",fnName:v,params:[{var:0},{val:u},{val:t}]});c==O?(L&&(r.push({cmd:"ALLOCSET",var:1,buff:n}),r.push({cmd:"CALL",fnName:L,params:[{var:0},{val:u},{var:1}]})),y&&r.push({cmd:"CALL",fnName:y,params:[{var:0},{val:u},{var:0}]}),r.push({cmd:"GET",out:0,var:0,len:u*d})):r.push({cmd:"GET",ou