From 0aea419a892f20424edb3179dfa9c3fb6d63eb60 Mon Sep 17 00:00:00 2001 From: 0xmad <0xmad@users.noreply.github.com> Date: Thu, 19 Oct 2023 14:28:39 -0500 Subject: [PATCH] chore: update with upstream --- README.md | 2 +- build/cli.cjs | 14 +- build/snarkjs.js | 19823 ++++++++++++++-- build/snarkjs.min.js | 4 +- package-lock.json | 38 +- package.json | 2 +- smart_contract_tests/package-lock.json | 51 +- smart_contract_tests/package.json | 2 +- .../test/smart_contracts.test.js | 2 +- 9 files changed, 17783 insertions(+), 2155 deletions(-) diff --git a/README.md b/README.md index 1c9dfe7..501583c 100644 --- a/README.md +++ b/README.md @@ -512,7 +512,7 @@ npm install snarkjs ``` ```js -const snarkjs = require("snarkjs"); +const snarkjs = require("@cryptkeeperzk/snarkjs"); const fs = require("fs"); async function run() { diff --git a/build/cli.cjs b/build/cli.cjs index 0f66924..23423b0 100755 --- a/build/cli.cjs +++ b/build/cli.cjs @@ -6,7 +6,7 @@ var fs = require('fs'); var url = require('url'); var r1csfile = require('r1csfile'); var fastFile = require('fastfile'); -var ffjavascript = require('ffjavascript'); +var ffjavascript = require('@cryptkeeperzk/ffjavascript'); var Blake2b = require('blake2b-wasm'); var readline = require('readline'); var crypto = require('crypto'); @@ -13077,13 +13077,13 @@ async function zkeyExportSolidityVerifier(params, options) { const templates = {}; if (await fileExists(path__default["default"].join(__dirname$1, "templates"))) { - templates.groth16 = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "templates", "verifier_groth16.sol.@cryptkeeperzk/ejs"), "utf8"); - templates.plonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "templates", "verifier_plonk.sol.@cryptkeeperzk/ejs"), "utf8"); - templates.fflonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "templates", "verifier_fflonk.sol.@cryptkeeperzk/ejs"), "utf8"); + templates.groth16 = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "templates", "verifier_groth16.sol.ejs"), "utf8"); + templates.plonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "templates", "verifier_plonk.sol.ejs"), "utf8"); + templates.fflonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "templates", "verifier_fflonk.sol.ejs"), "utf8"); } else { - templates.groth16 = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "..", "templates", "verifier_groth16.sol.@cryptkeeperzk/ejs"), "utf8"); - templates.plonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "..", "templates", "verifier_plonk.sol.@cryptkeeperzk/ejs"), "utf8"); - templates.fflonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "..", "templates", "verifier_fflonk.sol.@cryptkeeperzk/ejs"), "utf8"); + templates.groth16 = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "..", "templates", "verifier_groth16.sol.ejs"), "utf8"); + templates.plonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "..", "templates", "verifier_plonk.sol.ejs"), "utf8"); + templates.fflonk = await fs__default["default"].promises.readFile(path__default["default"].join(__dirname$1, "..", "templates", "verifier_fflonk.sol.ejs"), "utf8"); } const verifierCode = await exportSolidityVerifier(zkeyName, templates, logger); diff --git a/build/snarkjs.js b/build/snarkjs.js index fde2fdd..7742344 100644 --- a/build/snarkjs.js +++ b/build/snarkjs.js @@ -1,6 +1,17501 @@ var snarkjs = (function (exports) { 'use strict'; + /* global BigInt */ + const hexLen$1 = [ 0, 1, 2, 2, 3, 3, 3, 3, 4 ,4 ,4 ,4 ,4 ,4 ,4 ,4]; + + function fromString$1(s, radix) { + if ((!radix)||(radix==10)) { + return BigInt(s); + } else if (radix==16) { + if (s.slice(0,2) == "0x") { + return BigInt(s); + } else { + return BigInt("0x"+s); + } + } + } + + const e$1 = fromString$1; + + function fromArray$1(a, radix) { + let acc =BigInt(0); + radix = BigInt(radix); + for (let i=0; i> BigInt(n); + } + + const shl$1 = shiftLeft$1; + const shr$1 = shiftRight$1; + + function isOdd$b(a) { + return (BigInt(a) & BigInt(1)) == BigInt(1); + } + + + function naf$1(n) { + let E = BigInt(n); + const res = []; + while (E) { + if (E & BigInt(1)) { + const z = 2 - Number(E % BigInt(4)); + res.push( z ); + E = E - BigInt(z); + } else { + res.push( 0 ); + } + E = E >> BigInt(1); + } + return res; + } + + + function bits$1(n) { + let E = BigInt(n); + const res = []; + while (E) { + if (E & BigInt(1)) { + res.push(1); + } else { + res.push( 0 ); + } + E = E >> BigInt(1); + } + return res; + } + + function toNumber$2(s) { + if (s>BigInt(Number.MAX_SAFE_INTEGER )) { + throw new Error("Number too big"); + } + return Number(s); + } + + function toArray$1(s, radix) { + const res = []; + let rem = BigInt(s); + radix = BigInt(radix); + while (rem) { + res.unshift( Number(rem % radix)); + rem = rem / radix; + } + return res; + } + + + function add$1(a, b) { + return BigInt(a) + BigInt(b); + } + + function sub$1(a, b) { + return BigInt(a) - BigInt(b); + } + + function neg$1(a) { + return -BigInt(a); + } + + function mul$1(a, b) { + return BigInt(a) * BigInt(b); + } + + function square$5(a) { + return BigInt(a) * BigInt(a); + } + + function pow$1(a, b) { + return BigInt(a) ** BigInt(b); + } + + function exp$3(a, b) { + return BigInt(a) ** BigInt(b); + } + + function abs$3(a) { + return BigInt(a) >= 0 ? BigInt(a) : -BigInt(a); + } + + function div$1(a, b) { + return BigInt(a) / BigInt(b); + } + + function mod$1(a, b) { + return BigInt(a) % BigInt(b); + } + + function eq$1(a, b) { + return BigInt(a) == BigInt(b); + } + + function neq$1(a, b) { + return BigInt(a) != BigInt(b); + } + + function lt$1(a, b) { + return BigInt(a) < BigInt(b); + } + + function gt$1(a, b) { + return BigInt(a) > BigInt(b); + } + + function leq$1(a, b) { + return BigInt(a) <= BigInt(b); + } + + function geq$1(a, b) { + return BigInt(a) >= BigInt(b); + } + + function band$1(a, b) { + return BigInt(a) & BigInt(b); + } + + function bor$1(a, b) { + return BigInt(a) | BigInt(b); + } + + function bxor$1(a, b) { + return BigInt(a) ^ BigInt(b); + } + + function land$1(a, b) { + return BigInt(a) && BigInt(b); + } + + function lor$1(a, b) { + return BigInt(a) || BigInt(b); + } + + function lnot$1(a) { + return !BigInt(a); + } + + // Returns a buffer with Little Endian Representation + function toRprLE$1(buff, o, e, n8) { + const s = "0000000" + e.toString(16); + const v = new Uint32Array(buff.buffer, buff.byteOffset + o, n8/4); + const l = (((s.length-7)*4 - 1) >> 5)+1; // Number of 32bit words; + for (let i=0; i> 5)+1; // Number of 32bit words; + for (let i=0; i a[a.length-i-1] = ch.toString(16).padStart(8,"0") ); + return fromString$1(a.join(""), 16); + } + + // Pases a buffer with Big Endian Representation + function fromRprBE$1(buff, o, n8) { + n8 = n8 || buff.byteLength; + o = o || 0; + const v = new DataView(buff.buffer, buff.byteOffset + o, n8); + const a = new Array(n8/4); + for (let i=0; i. + */ + + + /* + exports.mulScalar = (F, base, e) =>{ + let res = F.zero; + let rem = bigInt(e); + let exp = base; + + while (! rem.eq(bigInt.zero)) { + if (rem.and(bigInt.one).eq(bigInt.one)) { + res = F.add(res, exp); + } + exp = F.double(exp); + rem = rem.shiftRight(1); + } + + return res; + }; + */ + + + function exp$2(F, base, e) { + + if (isZero$2(e)) return F.one; + + const n = bits$1(e); + + if (n.length==0) return F.one; + + let res = base; + + for (let i=n.length-2; i>=0; i--) { + + res = F.square(res); + + if (n[i]) { + res = F.mul(res, base); + } + } + + return res; + } + + // Check here: https://eprint.iacr.org/2012/685.pdf + + function buildSqrt$1 (F) { + if ((F.m % 2) == 1) { + if (eq$1(mod$1(F.p, 4), 1 )) { + if (eq$1(mod$1(F.p, 8), 1 )) { + if (eq$1(mod$1(F.p, 16), 1 )) { + // alg7_muller(F); + alg5_tonelliShanks$1(F); + } else if (eq$1(mod$1(F.p, 16), 9 )) { + alg4_kong$1(F); + } else { + throw new Error("Field withot sqrt"); + } + } else if (eq$1(mod$1(F.p, 8), 5 )) { + alg3_atkin$1(F); + } else { + throw new Error("Field withot sqrt"); + } + } else if (eq$1(mod$1(F.p, 4), 3 )) { + alg2_shanks$1(F); + } + } else { + const pm2mod4 = mod$1(pow$1(F.p, F.m/2), 4); + if (pm2mod4 == 1) { + alg10_adj$1(F); + } else if (pm2mod4 == 3) { + alg9_adj$1(F); + } else { + alg8_complex$1(F); + } + + } + } + + + function alg5_tonelliShanks$1(F) { + F.sqrt_q = pow$1(F.p, F.m); + + F.sqrt_s = 0; + F.sqrt_t = sub$1(F.sqrt_q, 1); + + while (!isOdd$b(F.sqrt_t)) { + F.sqrt_s = F.sqrt_s + 1; + F.sqrt_t = div$1(F.sqrt_t, 2); + } + + let c0 = F.one; + + while (F.eq(c0, F.one)) { + const c = F.random(); + F.sqrt_z = F.pow(c, F.sqrt_t); + c0 = F.pow(F.sqrt_z, 2 ** (F.sqrt_s-1) ); + } + + F.sqrt_tm1d2 = div$1(sub$1(F.sqrt_t, 1),2); + + F.sqrt = function(a) { + const F=this; + if (F.isZero(a)) return F.zero; + let w = F.pow(a, F.sqrt_tm1d2); + const a0 = F.pow( F.mul(F.square(w), a), 2 ** (F.sqrt_s-1) ); + if (F.eq(a0, F.negone)) return null; + + let v = F.sqrt_s; + let x = F.mul(a, w); + let b = F.mul(x, w); + let z = F.sqrt_z; + while (!F.eq(b, F.one)) { + let b2k = F.square(b); + let k=1; + while (!F.eq(b2k, F.one)) { + b2k = F.square(b2k); + k++; + } + + w = z; + for (let i=0; i>> 0; + st[d] = (st[d] ^ st[a]) >>> 0; + st[d] = ((st[d] << 16) | ((st[d]>>>16) & 0xFFFF)) >>> 0; + + st[c] = (st[c] + st[d]) >>> 0; + st[b] = (st[b] ^ st[c]) >>> 0; + st[b] = ((st[b] << 12) | ((st[b]>>>20) & 0xFFF)) >>> 0; + + st[a] = (st[a] + st[b]) >>> 0; + st[d] = (st[d] ^ st[a]) >>> 0; + st[d] = ((st[d] << 8) | ((st[d]>>>24) & 0xFF)) >>> 0; + + st[c] = (st[c] + st[d]) >>> 0; + st[b] = (st[b] ^ st[c]) >>> 0; + st[b] = ((st[b] << 7) | ((st[b]>>>25) & 0x7F)) >>> 0; + } + + function doubleRound$1(st) { + quarterRound$1(st, 0, 4, 8,12); + quarterRound$1(st, 1, 5, 9,13); + quarterRound$1(st, 2, 6,10,14); + quarterRound$1(st, 3, 7,11,15); + + quarterRound$1(st, 0, 5,10,15); + quarterRound$1(st, 1, 6,11,12); + quarterRound$1(st, 2, 7, 8,13); + quarterRound$1(st, 3, 4, 9,14); + } + + class ChaCha$1 { + + constructor(seed) { + seed = seed || [0,0,0,0,0,0,0,0]; + this.state = [ + 0x61707865, + 0x3320646E, + 0x79622D32, + 0x6B206574, + seed[0], + seed[1], + seed[2], + seed[3], + seed[4], + seed[5], + seed[6], + seed[7], + 0, + 0, + 0, + 0 + ]; + this.idx = 16; + this.buff = new Array(16); + } + + nextU32() { + if (this.idx == 16) this.update(); + return this.buff[this.idx++]; + } + + nextU64() { + return add$1(mul$1(this.nextU32(), 0x100000000), this.nextU32()); + } + + nextBool() { + return (this.nextU32() & 1) == 1; + } + + update() { + // Copy the state + for (let i=0; i<16; i++) this.buff[i] = this.state[i]; + + // Apply the rounds + for (let i=0; i<10; i++) doubleRound$1(this.buff); + + // Add to the initial + for (let i=0; i<16; i++) this.buff[i] = (this.buff[i] + this.state[i]) >>> 0; + + this.idx = 0; + + this.state[12] = (this.state[12] + 1) >>> 0; + if (this.state[12] != 0) return; + this.state[13] = (this.state[13] + 1) >>> 0; + if (this.state[13] != 0) return; + this.state[14] = (this.state[14] + 1) >>> 0; + if (this.state[14] != 0) return; + this.state[15] = (this.state[15] + 1) >>> 0; + } + } + + var crypto = {}; + + function getRandomBytes$2(n) { + let array = new Uint8Array(n); + { // Browser + if (typeof globalThis.crypto !== "undefined") { // Supported + globalThis.crypto.getRandomValues(array); + } else { // fallback + for (let i=0; i>>0; + } + } + } + return array; + } + + function getRandomSeed$1() { + const arr = getRandomBytes$2(32); + const arrV = new Uint32Array(arr.buffer); + const seed = []; + for (let i=0; i<8; i++) { + seed.push(arrV[i]); + } + return seed; + } + + let threadRng$1 = null; + + function getThreadRng$1() { + if (threadRng$1) return threadRng$1; + threadRng$1 = new ChaCha$1(getRandomSeed$1()); + return threadRng$1; + } + + /* + Copyright 2018 0kims association. + + This file is part of snarkjs. + + snarkjs is a free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your option) + any later version. + + snarkjs is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along with + snarkjs. If not, see . + */ + + /* + This library does operations on polynomials with coefficients in a field F. + + A polynomial P(x) = p0 + p1 * x + p2 * x^2 + ... + pn * x^n is represented + by the array [ p0, p1, p2, ... , pn ]. + */ + + class FFT$1 { + constructor (G, F, opMulGF) { + this.F = F; + this.G = G; + this.opMulGF = opMulGF; + + let rem = F.sqrt_t || F.t; + let s = F.sqrt_s || F.s; + + let nqr = F.one; + while (F.eq(F.pow(nqr, F.half), F.one)) nqr = F.add(nqr, F.one); + + this.w = new Array(s+1); + this.wi = new Array(s+1); + this.w[s] = this.F.pow(nqr, rem); + this.wi[s] = this.F.inv(this.w[s]); + + let n=s-1; + while (n>=0) { + this.w[n] = this.F.square(this.w[n+1]); + this.wi[n] = this.F.square(this.wi[n+1]); + n--; + } + + + this.roots = []; + /* + for (let i=0; i<16; i++) { + let r = this.F.one; + n = 1 << i; + const rootsi = new Array(n); + for (let j=0; j=0) && (!this.roots[i]); i--) { + let r = this.F.one; + const nroots = 1 << i; + const rootsi = new Array(nroots); + for (let j=0; j> 1; + const p1 = __fft$1(PF, pall, bits-1, offset, step*2); + const p2 = __fft$1(PF, pall, bits-1, offset+step, step*2); + + const out = new Array(n); + + for (let i=0; i> this.one; + this.bitLength = bitLength$c(this.p); + this.mask = (this.one << BigInt(this.bitLength)) - this.one; + + this.n64 = Math.floor((this.bitLength - 1) / 64)+1; + this.n32 = this.n64*2; + this.n8 = this.n64*8; + this.R = this.e(this.one << BigInt(this.n64*64)); + this.Ri = this.inv(this.R); + + const e = this.negone >> this.one; + this.nqr = this.two; + let r = this.pow(this.nqr, e); + while (!this.eq(r, this.negone)) { + this.nqr = this.nqr + this.one; + r = this.pow(this.nqr, e); + } + + + this.s = 0; + this.t = this.negone; + + while ((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); + + buildSqrt$1(this); + + this.FFT = new FFT$1(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(a,b) { + let res; + if (!b) { + res = BigInt(a); + } else if (b==16) { + res = BigInt("0x"+a); + } + if (res < 0) { + let nres = -res; + if (nres >= this.p) nres = nres % this.p; + return this.p - nres; + } else { + return (res>= this.p) ? res%this.p : res; + } + + } + + add(a, b) { + const res = a + b; + return res >= this.p ? res-this.p : res; + } + + sub(a, b) { + return (a >= b) ? a-b : this.p-b+a; + } + + neg(a) { + return a ? this.p-a : a; + } + + mul(a, b) { + return (a*b)%this.p; + } + + mulScalar(base, s) { + return (base * this.e(s)) % this.p; + } + + square(a) { + return (a*a) % this.p; + } + + eq(a, b) { + return a==b; + } + + neq(a, b) { + return a!=b; + } + + lt(a, b) { + const aa = (a > this.half) ? a - this.p : a; + const bb = (b > this.half) ? b - this.p : b; + return aa < bb; + } + + gt(a, b) { + const aa = (a > this.half) ? a - this.p : a; + const bb = (b > this.half) ? b - this.p : b; + return aa > bb; + } + + leq(a, b) { + const aa = (a > this.half) ? a - this.p : a; + const bb = (b > this.half) ? b - this.p : b; + return aa <= bb; + } + + geq(a, b) { + const aa = (a > this.half) ? a - this.p : a; + const bb = (b > this.half) ? b - this.p : b; + return aa >= bb; + } + + div(a, b) { + return this.mul(a, this.inv(b)); + } + + idiv(a, b) { + if (!b) throw new Error("Division by zero"); + return a / b; + } + + inv(a) { + if (!a) throw new Error("Division by zero"); + + let t = this.zero; + let r = this.p; + let newt = this.one; + let newr = a % this.p; + while (newr) { + let q = r/newr; + [t, newt] = [newt, t-q*newt]; + [r, newr] = [newr, r-q*newr]; + } + if (t= this.p ? res-this.p : res; + } + + bor(a, b) { + const res = ((a | b) & this.mask); + return res >= this.p ? res-this.p : res; + } + + bxor(a, b) { + const res = ((a ^ b) & this.mask); + return res >= this.p ? res-this.p : res; + } + + bnot(a) { + const res = a ^ this.mask; + return res >= this.p ? res-this.p : res; + } + + shl(a, b) { + if (Number(b) < this.bitLength) { + const res = (a << b) & this.mask; + return res >= this.p ? res-this.p : res; + } else { + const nb = this.p - b; + if (Number(nb) < this.bitLength) { + return a >> nb; + } else { + return this.zero; + } + } + } + + shr(a, b) { + if (Number(b) < this.bitLength) { + return a >> b; + } else { + const nb = this.p - b; + if (Number(nb) < this.bitLength) { + const res = (a << nb) & this.mask; + return res >= this.p ? res-this.p : res; + } else { + return 0; + } + } + } + + land(a, b) { + return (a && b) ? this.one : this.zero; + } + + lor(a, b) { + return (a || b) ? this.one : this.zero; + } + + lnot(a) { + return (a) ? this.zero : this.one; + } + + sqrt_old(n) { + + if (n == this.zero) return this.zero; + + // Test that have solution + const res = this.pow(n, this.negone >> this.one); + if ( res != this.one ) return null; + + let m = this.s; + let c = this.nqr_to_t; + let t = this.pow(n, this.t); + let r = this.pow(n, this.add(this.t, this.one) >> this.one ); + + while ( t != this.one ) { + let sq = this.square(t); + let i = 1; + while (sq != this.one ) { + i++; + sq = this.square(sq); + } + + // b = c ^ m-i-1 + let b = c; + for (let j=0; j< m-i-1; j ++) b = this.square(b); + + m = i; + c = this.square(b); + t = this.mul(t, c); + r = this.mul(r, b); + } + + if (r > (this.p >> this.one)) { + r = this.neg(r); + } + + return r; + } + + normalize(a, b) { + a = BigInt(a,b); + if (a < 0) { + let na = -a; + if (na >= this.p) na = na % this.p; + return this.p - na; + } else { + return (a>= this.p) ? a%this.p : a; + } + } + + random() { + const nBytes = (this.bitLength*2 / 8); + let res =this.zero; + for (let i=0; i this.half)&&(base == 10)) { + const v = this.p-a; + vs = "-"+v.toString(base); + } else { + vs = a.toString(base); + } + return vs; + } + + isZero(a) { + return a == this.zero; + } + + fromRng(rng) { + let v; + do { + v=this.zero; + for (let i=0; i= this.p); + v = (v * this.Ri) % this.p; // Convert from montgomery + return v; + } + + fft(a) { + return this.FFT.fft(a); + } + + ifft(a) { + return this.FFT.ifft(a); + } + + // Returns a buffer with Little Endian Representation + toRprLE(buff, o, e) { + toRprLE$1(buff, o, e, this.n64*8); + } + + // Returns a buffer with Big Endian Representation + toRprBE(buff, o, e) { + toRprBE$1(buff, o, e, this.n64*8); + } + + // Returns a buffer with Big Endian Montgomery Representation + toRprBEM(buff, o, e) { + return this.toRprBE(buff, o, this.mul(this.R, e)); + } + + toRprLEM(buff, o, e) { + return this.toRprLE(buff, o, this.mul(this.R, e)); + } + + + // Pases a buffer with Little Endian Representation + fromRprLE(buff, o) { + return fromRprLE$1(buff, o, this.n8); + } + + // Pases a buffer with Big Endian Representation + fromRprBE(buff, o) { + return fromRprBE$1(buff, o, this.n8); + } + + fromRprLEM(buff, o) { + return this.mul(this.fromRprLE(buff, o), this.Ri); + } + + fromRprBEM(buff, o) { + return this.mul(this.fromRprBE(buff, o), this.Ri); + } + + toObject(a) { + return a; + } + } + + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; + + var utils$c = {}; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + utils$c.bigInt2BytesLE = function bigInt2BytesLE(_a, len) { + const b = Array(len); + let v = BigInt(_a); + for (let i=0; i> 8n; + } + return b; + }; + + utils$c.bigInt2U32LE = function bigInt2BytesLE(_a, len) { + const b = Array(len); + let v = BigInt(_a); + for (let i=0; i> 32n; + } + return b; + }; + + utils$c.isOcamNum = function(a) { + if (!Array.isArray(a)) return false; + if (a.length != 3) return false; + if (typeof a[0] !== "number") return false; + if (typeof a[1] !== "number") return false; + if (!Array.isArray(a[2])) return false; + return true; + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + var build_int$1 = function buildInt(module, n64, _prefix) { + + const prefix = _prefix || "int"; + if (module.modules[prefix]) return prefix; // already builded + module.modules[prefix] = {}; + + const n32 = n64*2; + const n8 = n64*8; + + function buildCopy() { + const f = module.addFunction(prefix+"_copy"); + f.addParam("px", "i32"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + for (let i=0; i>1) )&&(i>1, k>>1) + ) + ) + ); + + f.addCode( + c.setLocal(c1, + c.i64_add( + c.getLocal(c1), + c.i64_shr_u( + c.getLocal(c0), + c.i64_const(32) + ) + ) + ) + ); + } + + // Add the old carry + + if (k>0) { + f.addCode( + c.setLocal(c0, + c.i64_add( + c.i64_and( + c.getLocal(c0), + c.i64_const(0xFFFFFFFF) + ), + c.i64_and( + c.getLocal(c0_old), + c.i64_const(0xFFFFFFFF) + ), + ) + ) + ); + + f.addCode( + c.setLocal(c1, + c.i64_add( + c.i64_add( + c.getLocal(c1), + c.i64_shr_u( + c.getLocal(c0), + c.i64_const(32) + ) + ), + c.getLocal(c1_old) + ) + ) + ); + } + + f.addCode( + c.i64_store32( + c.getLocal("r"), + k*4, + c.getLocal(c0) + ) + ); + + f.addCode( + c.setLocal( + c0_old, + c.getLocal(c1) + ), + c.setLocal( + c1_old, + c.i64_shr_u( + c.getLocal(c0_old), + c.i64_const(32) + ) + ) + ); + + } + f.addCode( + c.i64_store32( + c.getLocal("r"), + n32*4*2-4, + c.getLocal(c0_old) + ) + ); + + } + + + function buildSquareOld() { + const f = module.addFunction(prefix+"_squareOld"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode(c.call(prefix + "_mul", c.getLocal("x"), c.getLocal("x"), c.getLocal("r"))); + } + + function _buildMul1() { + const f = module.addFunction(prefix+"__mul1"); + f.addParam("px", "i32"); + f.addParam("y", "i64"); + f.addParam("pr", "i32"); + f.addLocal("c", "i64"); + + const c = f.getCodeBuilder(); + + f.addCode(c.setLocal( + "c", + c.i64_mul( + c.i64_load32_u(c.getLocal("px"), 0, 0), + c.getLocal("y") + ) + )); + + f.addCode(c.i64_store32( + c.getLocal("pr"), + 0, + 0, + c.getLocal("c"), + )); + + for (let i=1; i3)&&(Y[eY]==0) ey--; + f.addCode(c.block(c.loop( + c.br_if( + 1, + c.i32_or( + c.i32_load8_u( + c.i32_add(Y , c.getLocal("eY")), + 0, + 0 + ), + c.i32_eq( + c.getLocal("eY"), + c.i32_const(3) + ) + ) + ), + c.setLocal("eY", c.i32_sub(c.getLocal("eY"), c.i32_const(1))), + c.br(0) + ))); + + f.addCode( + c.setLocal( + "sy", + c.i64_add( + c.i64_load32_u( + c.i32_sub( + c.i32_add( Y, c.getLocal("eY")), + c.i32_const(3) + ), + 0, + 0 + ), + c.i64_const(1) + ) + ) + ); + + // Force a divide by 0 if quotien is 0 + f.addCode( + c.if( + c.i64_eq( + c.getLocal("sy"), + c.i64_const(1) + ), + c.drop(c.i64_div_u(c.i64_const(0), c.i64_const(0))) + ) + ); + + f.addCode(c.block(c.loop( + + // while (eX>7)&&(Y[eX]==0) ex--; + c.block(c.loop( + c.br_if( + 1, + c.i32_or( + c.i32_load8_u( + c.i32_add(R , c.getLocal("eX")), + 0, + 0 + ), + c.i32_eq( + c.getLocal("eX"), + c.i32_const(7) + ) + ) + ), + c.setLocal("eX", c.i32_sub(c.getLocal("eX"), c.i32_const(1))), + c.br(0) + )), + + c.setLocal( + "sx", + c.i64_load( + c.i32_sub( + c.i32_add( R, c.getLocal("eX")), + c.i32_const(7) + ), + 0, + 0 + ) + ), + + c.setLocal( + "sx", + c.i64_div_u( + c.getLocal("sx"), + c.getLocal("sy") + ) + ), + c.setLocal( + "ec", + c.i32_sub( + c.i32_sub( + c.getLocal("eX"), + c.getLocal("eY") + ), + c.i32_const(4) + ) + ), + + // While greater than 32 bits or ec is neg, shr and inc exp + c.block(c.loop( + c.br_if( + 1, + c.i32_and( + c.i64_eqz( + c.i64_and( + c.getLocal("sx"), + c.i64_const("0xFFFFFFFF00000000") + ) + ), + c.i32_ge_s( + c.getLocal("ec"), + c.i32_const(0) + ) + ) + ), + + c.setLocal( + "sx", + c.i64_shr_u( + c.getLocal("sx"), + c.i64_const(8) + ) + ), + + c.setLocal( + "ec", + c.i32_add( + c.getLocal("ec"), + c.i32_const(1) + ) + ), + c.br(0) + )), + + c.if( + c.i64_eqz(c.getLocal("sx")), + [ + ...c.br_if( + 2, + c.i32_eqz(c.call(prefix + "_gte", R, Y)) + ), + ...c.setLocal("sx", c.i64_const(1)), + ...c.setLocal("ec", c.i32_const(0)) + ] + ), + + c.call(prefix + "__mul1", Y, c.getLocal("sx"), R2), + c.drop(c.call( + prefix + "_sub", + R, + c.i32_sub(R2, c.getLocal("ec")), + R + )), + c.call( + prefix + "__add1", + c.i32_add(C, c.getLocal("ec")), + c.getLocal("sx") + ), + c.br(0) + ))); + } + + function buildInverseMod() { + + const f = module.addFunction(prefix+"_inverseMod"); + f.addParam("px", "i32"); + f.addParam("pm", "i32"); + f.addParam("pr", "i32"); + f.addLocal("t", "i32"); + f.addLocal("newt", "i32"); + f.addLocal("r", "i32"); + f.addLocal("qq", "i32"); + f.addLocal("qr", "i32"); + f.addLocal("newr", "i32"); + f.addLocal("swp", "i32"); + f.addLocal("x", "i32"); + f.addLocal("signt", "i32"); + f.addLocal("signnewt", "i32"); + f.addLocal("signx", "i32"); + + const c = f.getCodeBuilder(); + + const aux1 = c.i32_const(module.alloc(n8)); + const aux2 = c.i32_const(module.alloc(n8)); + const aux3 = c.i32_const(module.alloc(n8)); + const aux4 = c.i32_const(module.alloc(n8)); + const aux5 = c.i32_const(module.alloc(n8)); + const aux6 = c.i32_const(module.alloc(n8)); + const mulBuff = c.i32_const(module.alloc(n8*2)); + const aux7 = c.i32_const(module.alloc(n8)); + + f.addCode( + c.setLocal("t", aux1), + c.call(prefix + "_zero", aux1), + c.setLocal("signt", c.i32_const(0)), + ); + + f.addCode( + c.setLocal("r", aux2), + c.call(prefix + "_copy", c.getLocal("pm"), aux2) + ); + + f.addCode( + c.setLocal("newt", aux3), + c.call(prefix + "_one", aux3), + c.setLocal("signnewt", c.i32_const(0)), + ); + + f.addCode( + c.setLocal("newr", aux4), + c.call(prefix + "_copy", c.getLocal("px"), aux4) + ); + + + + + f.addCode(c.setLocal("qq", aux5)); + f.addCode(c.setLocal("qr", aux6)); + f.addCode(c.setLocal("x", aux7)); + + f.addCode(c.block(c.loop( + c.br_if( + 1, + c.call(prefix + "_isZero", c.getLocal("newr") ) + ), + c.call(prefix + "_div", c.getLocal("r"), c.getLocal("newr"), c.getLocal("qq"), c.getLocal("qr")), + + c.call(prefix + "_mul", c.getLocal("qq"), c.getLocal("newt"), mulBuff), + + c.if( + c.getLocal("signt"), + c.if( + c.getLocal("signnewt"), + c.if ( + c.call(prefix + "_gte", mulBuff, c.getLocal("t")), + [ + ...c.drop(c.call(prefix + "_sub", mulBuff, c.getLocal("t"), c.getLocal("x"))), + ...c.setLocal("signx", c.i32_const(0)) + ], + [ + ...c.drop(c.call(prefix + "_sub", c.getLocal("t"), mulBuff, c.getLocal("x"))), + ...c.setLocal("signx", c.i32_const(1)) + ], + ), + [ + ...c.drop(c.call(prefix + "_add", mulBuff, c.getLocal("t"), c.getLocal("x"))), + ...c.setLocal("signx", c.i32_const(1)) + ] + ), + c.if( + c.getLocal("signnewt"), + [ + ...c.drop(c.call(prefix + "_add", mulBuff, c.getLocal("t"), c.getLocal("x"))), + ...c.setLocal("signx", c.i32_const(0)) + ], + c.if ( + c.call(prefix + "_gte", c.getLocal("t"), mulBuff), + [ + ...c.drop(c.call(prefix + "_sub", c.getLocal("t"), mulBuff, c.getLocal("x"))), + ...c.setLocal("signx", c.i32_const(0)) + ], + [ + ...c.drop(c.call(prefix + "_sub", mulBuff, c.getLocal("t"), c.getLocal("x"))), + ...c.setLocal("signx", c.i32_const(1)) + ] + ) + ) + ), + + c.setLocal("swp", c.getLocal("t")), + c.setLocal("t", c.getLocal("newt")), + c.setLocal("newt", c.getLocal("x")), + c.setLocal("x", c.getLocal("swp")), + + c.setLocal("signt", c.getLocal("signnewt")), + c.setLocal("signnewt", c.getLocal("signx")), + + c.setLocal("swp", c.getLocal("r")), + c.setLocal("r", c.getLocal("newr")), + c.setLocal("newr", c.getLocal("qr")), + c.setLocal("qr", c.getLocal("swp")), + + c.br(0) + ))); + + f.addCode(c.if( + c.getLocal("signt"), + c.drop(c.call(prefix + "_sub", c.getLocal("pm"), c.getLocal("t"), c.getLocal("pr"))), + c.call(prefix + "_copy", c.getLocal("t"), c.getLocal("pr")) + )); + } + + + buildCopy(); + buildZero(); + buildIsZero(); + buildOne(); + buildEq(); + buildGte(); + buildAdd(); + buildSub(); + buildMul(); + buildSquare(); + buildSquareOld(); + buildDiv(); + buildInverseMod(); + module.exportFunction(prefix+"_copy"); + module.exportFunction(prefix+"_zero"); + module.exportFunction(prefix+"_one"); + module.exportFunction(prefix+"_isZero"); + module.exportFunction(prefix+"_eq"); + module.exportFunction(prefix+"_gte"); + module.exportFunction(prefix+"_add"); + module.exportFunction(prefix+"_sub"); + module.exportFunction(prefix+"_mul"); + module.exportFunction(prefix+"_square"); + module.exportFunction(prefix+"_squareOld"); + module.exportFunction(prefix+"_div"); + module.exportFunction(prefix+"_inverseMod"); + + return prefix; + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + var build_timesscalar$1 = function buildTimesScalar(module, fnName, elementLen, opAB, opAA, opCopy, opInit) { + + const f = module.addFunction(fnName); + f.addParam("base", "i32"); + f.addParam("scalar", "i32"); + f.addParam("scalarLength", "i32"); + f.addParam("r", "i32"); + f.addLocal("i", "i32"); + f.addLocal("b", "i32"); + + const c = f.getCodeBuilder(); + + const aux = c.i32_const(module.alloc(elementLen)); + + f.addCode( + c.if( + c.i32_eqz(c.getLocal("scalarLength")), + [ + ...c.call(opInit, c.getLocal("r")), + ...c.ret([]) + ] + ) + ); + f.addCode(c.call(opCopy, c.getLocal("base"), aux)); + f.addCode(c.call(opInit, c.getLocal("r"))); + f.addCode(c.setLocal("i", c.getLocal("scalarLength"))); + f.addCode(c.block(c.loop( + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + + c.setLocal( + "b", + c.i32_load8_u( + c.i32_add( + c.getLocal("scalar"), + c.getLocal("i") + ) + ) + ), + ...innerLoop(), + c.br_if(1, c.i32_eqz ( c.getLocal("i") )), + c.br(0) + ))); + + + function innerLoop() { + const code = []; + for (let i=0; i<8; i++) { + code.push( + ...c.call(opAA, c.getLocal("r"), c.getLocal("r")), + ...c.if( + c.i32_ge_u( c.getLocal("b"), c.i32_const(0x80 >> i)), + [ + ...c.setLocal( + "b", + c.i32_sub( + c.getLocal("b"), + c.i32_const(0x80 >> i) + ) + ), + ...c.call(opAB, c.getLocal("r"),aux, c.getLocal("r")) + ] + ) + ); + } + return code; + } + + }; + + var build_batchinverse$1 = buildBatchInverse$7; + + function buildBatchInverse$7(module, prefix) { + + + const n8 = module.modules[prefix].n64*8; + + const f = module.addFunction(prefix+"_batchInverse"); + f.addParam("pIn", "i32"); + f.addParam("inStep", "i32"); + f.addParam("n", "i32"); + f.addParam("pOut", "i32"); + f.addParam("outStep", "i32"); + f.addLocal("itAux", "i32"); + f.addLocal("itIn", "i32"); + f.addLocal("itOut","i32"); + f.addLocal("i","i32"); + + const c = f.getCodeBuilder(); + + const AUX = c.i32_const(module.alloc(n8)); + + + // Alloc Working space for accumulated umltiplications + f.addCode( + c.setLocal("itAux", c.i32_load( c.i32_const(0) )), + c.i32_store( + c.i32_const(0), + c.i32_add( + c.getLocal("itAux"), + c.i32_mul( + c.i32_add( + c.getLocal("n"), + c.i32_const(1) + ), + c.i32_const(n8) + ) + ) + ) + ); + + f.addCode( + + // aux[0] = a; + c.call(prefix+"_one", c.getLocal("itAux")), + // for (i=0;i b ? 1 : -1; + } + + function square$4(n) { + return n * n; + } + + function isOdd$a(n) { + return n % 2n !== 0n; + } + + function isEven$1(n) { + return n % 2n === 0n; + } + + function isNegative$7(n) { + return n < 0n; + } + + function isPositive$1(n) { + return n > 0n; + } + + function bitLength$b(n) { + if (isNegative$7(n)) { + return n.toString(2).length - 1; // discard the - sign + } else { + return n.toString(2).length; + } + } + + function abs$2(n) { + return n < 0n ? -n : n; + } + + function isUnit$1(n) { + return abs$2(n) === 1n; + } + + function modInv$7(a, n) { + var t = 0n, newT = 1n, r = n, newR = abs$2(a), q, lastT, lastR; + while (newR !== 0n) { + q = r / newR; + lastT = t; + lastR = r; + t = newT; + r = newR; + newT = lastT - (q * newT); + newR = lastR - (q * newR); + } + if (!isUnit$1(r)) throw new Error(a.toString() + " and " + n.toString() + " are not co-prime"); + if (compare$1(t, 0n) === -1) { + t = t + n; + } + if (isNegative$7(a)) { + return -t; + } + return t; + } + + function modPow$5(n, exp, mod) { + if (mod === 0n) throw new Error("Cannot take modPow with modulus 0"); + var r = 1n, + base = n % mod; + if (isNegative$7(exp)) { + exp = exp * -1n; + base = modInv$7(base, mod); + } + while (isPositive$1(exp)) { + if (base === 0n) return 0n; + if (isOdd$a(exp)) r = r * base % mod; + exp = exp / 2n; + base = square$4(base) % mod; + } + return r; + } + + function compareAbs$1(a, b) { + a = a >= 0n ? a : -a; + b = b >= 0n ? b : -b; + return a === b ? 0 : a > b ? 1 : -1; + } + + function isDivisibleBy$1(a, n) { + if (n === 0n) return false; + if (isUnit$1(n)) return true; + if (compareAbs$1(n, 2n) === 0) return isEven$1(a); + return a % n === 0n; + } + + function isBasicPrime$1(v) { + var n = abs$2(v); + if (isUnit$1(n)) return false; + if (n === 2n || n === 3n || n === 5n) return true; + if (isEven$1(n) || isDivisibleBy$1(n, 3n) || isDivisibleBy$1(n, 5n)) return false; + if (n < 49n) return true; + // we don't know if it's prime: let the other functions figure it out + } + + function prev$1(n) { + return n - 1n; + } + + function millerRabinTest$1(n, a) { + var nPrev = prev$1(n), + b = nPrev, + r = 0, + d, i, x; + while (isEven$1(b)) b = b / 2n, r++; + next: for (i = 0; i < a.length; i++) { + if (n < a[i]) continue; + x = modPow$5(BigInt(a[i]), b, n); + if (isUnit$1(x) || x === nPrev) continue; + for (d = r - 1; d != 0; d--) { + x = square$4(x) % n; + if (isUnit$1(x)) return false; + if (x === nPrev) continue next; + } + return false; + } + return true; + } + + function isPrime$3(p) { + var isPrime = isBasicPrime$1(p); + if (isPrime !== undefined) return isPrime; + var n = abs$2(p); + var bits = bitLength$b(n); + if (bits <= 64) + return millerRabinTest$1(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]); + var logN = Math.log(2) * Number(bits); + var t = Math.ceil(logN); + for (var a = [], i = 0; i < t; i++) { + a.push(BigInt(i + 2)); + } + return millerRabinTest$1(n, a); + } + + bigint$1.bitLength = bitLength$b; + bigint$1.isOdd = isOdd$a; + bigint$1.isNegative = isNegative$7; + bigint$1.abs = abs$2; + bigint$1.isUnit = isUnit$1; + bigint$1.compare = compare$1; + bigint$1.modInv = modInv$7; + bigint$1.modPow = modPow$5; + bigint$1.isPrime = isPrime$3; + bigint$1.square = square$4; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + const buildInt$1 = build_int$1; + const utils$b = utils$c; + const buildExp$5 = build_timesscalar$1; + const buildBatchInverse$6 = build_batchinverse$1; + const buildBatchConvertion$5 = build_batchconvertion$1; + const buildBatchOp$1 = build_batchop$1; + const { bitLength: bitLength$a, modInv: modInv$6, modPow: modPow$4, isPrime: isPrime$2, isOdd: isOdd$9, square: square$3 } = bigint$1; + + var build_f1m$1 = function buildF1m(module, _q, _prefix, _intPrefix) { + const q = BigInt(_q); + const n64 = Math.floor((bitLength$a(q - 1n) - 1)/64) +1; + const n32 = n64*2; + const n8 = n64*8; + + const prefix = _prefix || "f1m"; + if (module.modules[prefix]) return prefix; // already builded + + const intPrefix = buildInt$1(module, n64, _intPrefix); + const pq = module.alloc(n8, utils$b.bigInt2BytesLE(q, n8)); + + const pR2 = module.alloc(utils$b.bigInt2BytesLE(square$3(1n << BigInt(n64*64)) % q, n8)); + const pOne = module.alloc(utils$b.bigInt2BytesLE((1n << BigInt(n64*64)) % q, n8)); + const pZero = module.alloc(utils$b.bigInt2BytesLE(0n, n8)); + const _minusOne = q - 1n; + const _e = _minusOne >> 1n; // e = (p-1)/2 + const pe = module.alloc(n8, utils$b.bigInt2BytesLE(_e, n8)); + + const _ePlusOne = _e + 1n; // e = (p-1)/2 + const pePlusOne = module.alloc(n8, utils$b.bigInt2BytesLE(_ePlusOne, n8)); + + module.modules[prefix] = { + pq: pq, + pR2: pR2, + n64: n64, + q: q, + pOne: pOne, + pZero: pZero, + pePlusOne: pePlusOne + }; + + function buildOne() { + const f = module.addFunction(prefix+"_one"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode(c.call(intPrefix + "_copy", c.i32_const(pOne), c.getLocal("pr"))); + } + + function buildAdd() { + const f = module.addFunction(prefix+"_add"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.if( + c.call(intPrefix+"_add", c.getLocal("x"), c.getLocal("y"), c.getLocal("r")), + c.drop(c.call(intPrefix+"_sub", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))), + c.if( + c.call(intPrefix+"_gte", c.getLocal("r"), c.i32_const(pq) ), + c.drop(c.call(intPrefix+"_sub", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))), + ) + ) + ); + } + + function buildSub() { + const f = module.addFunction(prefix+"_sub"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.if( + c.call(intPrefix+"_sub", c.getLocal("x"), c.getLocal("y"), c.getLocal("r")), + c.drop(c.call(intPrefix+"_add", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))) + ) + ); + } + + function buildNeg() { + const f = module.addFunction(prefix+"_neg"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call(prefix + "_sub", c.i32_const(pZero), c.getLocal("x"), c.getLocal("r")) + ); + } + + + function buildIsNegative() { + const f = module.addFunction(prefix+"_isNegative"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const AUX = c.i32_const(module.alloc(n8)); + + f.addCode( + c.call(prefix + "_fromMontgomery", c.getLocal("x"), AUX), + c.call(intPrefix + "_gte", AUX, c.i32_const(pePlusOne) ) + ); + } + + function buildSign() { + const f = module.addFunction(prefix+"_sign"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const AUX = c.i32_const(module.alloc(n8)); + + f.addCode( + c.if ( + c.call(intPrefix + "_isZero", c.getLocal("x")), + c.ret(c.i32_const(0)) + ), + c.call(prefix + "_fromMontgomery", c.getLocal("x"), AUX), + c.if( + c.call(intPrefix + "_gte", AUX, c.i32_const(pePlusOne)), + c.ret(c.i32_const(-1)) + ), + c.ret(c.i32_const(1)) + ); + } + + + function buildMReduct() { + const carries = module.alloc(n32*n32*8); + + const f = module.addFunction(prefix+"_mReduct"); + f.addParam("t", "i32"); + f.addParam("r", "i32"); + f.addLocal("np32", "i64"); + f.addLocal("c", "i64"); + f.addLocal("m", "i64"); + + const c = f.getCodeBuilder(); + + const np32 = Number(0x100000000n - modInv$6(q, 0x100000000n)); + + f.addCode(c.setLocal("np32", c.i64_const(np32))); + + for (let i=0; i=n32) { + f.addCode( + c.i64_store32( + c.getLocal("r"), + (k-n32)*4, + c.getLocal(c0) + ) + ); + } + [c0, c1] = [c1, c0]; + f.addCode( + c.setLocal(c1, + c.i64_shr_u( + c.getLocal(c0), + c.i64_const(32) + ) + ) + ); + } + f.addCode( + c.i64_store32( + c.getLocal("r"), + n32*4-4, + c.getLocal(c0) + ) + ); + + f.addCode( + c.if( + c.i32_wrap_i64(c.getLocal(c1)), + c.drop(c.call(intPrefix+"_sub", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))), + c.if( + c.call(intPrefix+"_gte", c.getLocal("r"), c.i32_const(pq) ), + c.drop(c.call(intPrefix+"_sub", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))), + ) + ) + ); + } + + + function buildSquare() { + + const f = module.addFunction(prefix+"_square"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + f.addLocal("c0", "i64"); + f.addLocal("c1", "i64"); + f.addLocal("c0_old", "i64"); + f.addLocal("c1_old", "i64"); + f.addLocal("np32", "i64"); + + + for (let i=0;i>1) )&&(i>1, k>>1) + ) + ) + ); + + f.addCode( + c.setLocal(c1, + c.i64_add( + c.getLocal(c1), + c.i64_shr_u( + c.getLocal(c0), + c.i64_const(32) + ) + ) + ) + ); + } + + // Add the old carry + + if (k>0) { + f.addCode( + c.setLocal(c0, + c.i64_add( + c.i64_and( + c.getLocal(c0), + c.i64_const(0xFFFFFFFF) + ), + c.i64_and( + c.getLocal(c0_old), + c.i64_const(0xFFFFFFFF) + ), + ) + ) + ); + + f.addCode( + c.setLocal(c1, + c.i64_add( + c.i64_add( + c.getLocal(c1), + c.i64_shr_u( + c.getLocal(c0), + c.i64_const(32) + ) + ), + c.getLocal(c1_old) + ) + ) + ); + } + + + for (let i=Math.max(1, k-n32+1); (i<=k)&&(i=n32) { + f.addCode( + c.i64_store32( + c.getLocal("r"), + (k-n32)*4, + c.getLocal(c0) + ) + ); + } + f.addCode( + c.setLocal( + c0_old, + c.getLocal(c1) + ), + c.setLocal( + c1_old, + c.i64_shr_u( + c.getLocal(c0_old), + c.i64_const(32) + ) + ) + ); + } + f.addCode( + c.i64_store32( + c.getLocal("r"), + n32*4-4, + c.getLocal(c0_old) + ) + ); + + f.addCode( + c.if( + c.i32_wrap_i64(c.getLocal(c1_old)), + c.drop(c.call(intPrefix+"_sub", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))), + c.if( + c.call(intPrefix+"_gte", c.getLocal("r"), c.i32_const(pq) ), + c.drop(c.call(intPrefix+"_sub", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))), + ) + ) + ); + } + + + function buildSquareOld() { + const f = module.addFunction(prefix+"_squareOld"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode(c.call(prefix + "_mul", c.getLocal("x"), c.getLocal("x"), c.getLocal("r"))); + } + + function buildToMontgomery() { + const f = module.addFunction(prefix+"_toMontgomery"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + f.addCode(c.call(prefix+"_mul", c.getLocal("x"), c.i32_const(pR2), c.getLocal("r"))); + } + + function buildFromMontgomery() { + + const pAux2 = module.alloc(n8*2); + + const f = module.addFunction(prefix+"_fromMontgomery"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + f.addCode(c.call(intPrefix + "_copy", c.getLocal("x"), c.i32_const(pAux2) )); + f.addCode(c.call(intPrefix + "_zero", c.i32_const(pAux2 + n8) )); + f.addCode(c.call(prefix+"_mReduct", c.i32_const(pAux2), c.getLocal("r"))); + } + + function buildInverse() { + + const f = module.addFunction(prefix+ "_inverse"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + f.addCode(c.call(prefix + "_fromMontgomery", c.getLocal("x"), c.getLocal("r"))); + f.addCode(c.call(intPrefix + "_inverseMod", c.getLocal("r"), c.i32_const(pq), c.getLocal("r"))); + f.addCode(c.call(prefix + "_toMontgomery", c.getLocal("r"), c.getLocal("r"))); + } + + // Calculate various valuse needed for sqrt + + + let _nqr = 2n; + if (isPrime$2(q)) { + while (modPow$4(_nqr, _e, q) !== _minusOne) _nqr = _nqr + 1n; + } + + let s2 = 0; + let _t = _minusOne; + + while ((!isOdd$9(_t))&&(_t !== 0n)) { + s2++; + _t = _t >> 1n; + } + const pt = module.alloc(n8, utils$b.bigInt2BytesLE(_t, n8)); + + const _nqrToT = modPow$4(_nqr, _t, q); + const pNqrToT = module.alloc(utils$b.bigInt2BytesLE((_nqrToT << BigInt(n64*64)) % q, n8)); + + const _tPlusOneOver2 = (_t + 1n) >> 1n; + const ptPlusOneOver2 = module.alloc(n8, utils$b.bigInt2BytesLE(_tPlusOneOver2, n8)); + + function buildSqrt() { + + const f = module.addFunction(prefix+ "_sqrt"); + f.addParam("n", "i32"); + f.addParam("r", "i32"); + f.addLocal("m", "i32"); + f.addLocal("i", "i32"); + f.addLocal("j", "i32"); + + const c = f.getCodeBuilder(); + + const ONE = c.i32_const(pOne); + const C = c.i32_const(module.alloc(n8)); + const T = c.i32_const(module.alloc(n8)); + const R = c.i32_const(module.alloc(n8)); + const SQ = c.i32_const(module.alloc(n8)); + const B = c.i32_const(module.alloc(n8)); + + f.addCode( + + // If (n==0) return 0 + c.if( + c.call(prefix + "_isZero", c.getLocal("n")), + c.ret( + c.call(prefix + "_zero", c.getLocal("r")) + ) + ), + + c.setLocal("m", c.i32_const(s2)), + c.call(prefix + "_copy", c.i32_const(pNqrToT), C), + c.call(prefix + "_exp", c.getLocal("n"), c.i32_const(pt), c.i32_const(n8), T), + c.call(prefix + "_exp", c.getLocal("n"), c.i32_const(ptPlusOneOver2), c.i32_const(n8), R), + + c.block(c.loop( + c.br_if(1, c.call(prefix + "_eq", T, ONE)), + + c.call(prefix + "_square", T, SQ), + c.setLocal("i", c.i32_const(1)), + c.block(c.loop( + c.br_if(1, c.call(prefix + "_eq", SQ, ONE)), + c.call(prefix + "_square", SQ, SQ), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )), + + c.call(prefix + "_copy", C, B), + c.setLocal("j", c.i32_sub(c.i32_sub( c.getLocal("m"), c.getLocal("i")), c.i32_const(1)) ), + c.block(c.loop( + c.br_if(1, c.i32_eqz(c.getLocal("j"))), + c.call(prefix + "_square", B, B), + c.setLocal("j", c.i32_sub(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )), + + c.setLocal("m", c.getLocal("i")), + c.call(prefix + "_square", B, C), + c.call(prefix + "_mul", T, C, T), + c.call(prefix + "_mul", R, B, R), + + c.br(0) + )), + + c.if( + c.call(prefix + "_isNegative", R), + c.call(prefix + "_neg", R, c.getLocal("r")), + c.call(prefix + "_copy", R, c.getLocal("r")), + ) + ); + } + + function buildIsSquare() { + const f = module.addFunction(prefix+"_isSquare"); + f.addParam("n", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const ONE = c.i32_const(pOne); + const AUX = c.i32_const(module.alloc(n8)); + + f.addCode( + c.if( + c.call(prefix + "_isZero", c.getLocal("n")), + c.ret(c.i32_const(1)) + ), + c.call(prefix + "_exp", c.getLocal("n"), c.i32_const(pe), c.i32_const(n8), AUX), + c.call(prefix + "_eq", AUX, ONE) + ); + } + + + function buildLoad() { + const f = module.addFunction(prefix+"_load"); + f.addParam("scalar", "i32"); + f.addParam("scalarLen", "i32"); + f.addParam("r", "i32"); + f.addLocal("p", "i32"); + f.addLocal("l", "i32"); + f.addLocal("i", "i32"); + f.addLocal("j", "i32"); + const c = f.getCodeBuilder(); + + const R = c.i32_const(module.alloc(n8)); + const pAux = module.alloc(n8); + const AUX = c.i32_const(pAux); + + f.addCode( + c.call(intPrefix + "_zero", c.getLocal("r")), + c.setLocal("i", c.i32_const(n8)), + c.setLocal("p", c.getLocal("scalar")), + c.block(c.loop( + c.br_if(1, c.i32_gt_u(c.getLocal("i"), c.getLocal("scalarLen"))), + + c.if( + c.i32_eq(c.getLocal("i"), c.i32_const(n8)), + c.call(prefix + "_one", R), + c.call(prefix + "_mul", R, c.i32_const(pR2), R) + ), + c.call(prefix + "_mul", c.getLocal("p"), R, AUX), + c.call(prefix + "_add", c.getLocal("r"), AUX, c.getLocal("r")), + + c.setLocal("p", c.i32_add(c.getLocal("p"), c.i32_const(n8))), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(n8))), + c.br(0) + )), + + c.setLocal("l", c.i32_rem_u( c.getLocal("scalarLen"), c.i32_const(n8))), + c.if(c.i32_eqz(c.getLocal("l")), c.ret([])), + c.call(intPrefix + "_zero", AUX), + c.setLocal("j", c.i32_const(0)), + c.block(c.loop( + c.br_if(1, c.i32_eq(c.getLocal("j"), c.getLocal("l"))), + + c.i32_store8( + c.getLocal("j"), + pAux, + c.i32_load8_u(c.getLocal("p")), + ), + c.setLocal("p", c.i32_add(c.getLocal("p"), c.i32_const(1))), + c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )), + + c.if( + c.i32_eq(c.getLocal("i"), c.i32_const(n8)), + c.call(prefix + "_one", R), + c.call(prefix + "_mul", R, c.i32_const(pR2), R) + ), + c.call(prefix + "_mul", AUX, R, AUX), + c.call(prefix + "_add", c.getLocal("r"), AUX, c.getLocal("r")), + ); + } + + function buildTimesScalar() { + const f = module.addFunction(prefix+"_timesScalar"); + f.addParam("x", "i32"); + f.addParam("scalar", "i32"); + f.addParam("scalarLen", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const AUX = c.i32_const(module.alloc(n8)); + + f.addCode( + c.call(prefix + "_load", c.getLocal("scalar"), c.getLocal("scalarLen"), AUX), + c.call(prefix + "_toMontgomery", AUX, AUX), + c.call(prefix + "_mul", c.getLocal("x"), AUX, c.getLocal("r")), + ); + } + + function buildIsOne() { + const f = module.addFunction(prefix+"_isOne"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + f.addCode( + c.ret(c.call(intPrefix + "_eq", c.getLocal("x"), c.i32_const(pOne))) + ); + } + + + module.exportFunction(intPrefix + "_copy", prefix+"_copy"); + module.exportFunction(intPrefix + "_zero", prefix+"_zero"); + module.exportFunction(intPrefix + "_isZero", prefix+"_isZero"); + module.exportFunction(intPrefix + "_eq", prefix+"_eq"); + + buildIsOne(); + buildAdd(); + buildSub(); + buildNeg(); + buildMReduct(); + buildMul(); + buildSquare(); + buildSquareOld(); + buildToMontgomery(); + buildFromMontgomery(); + buildIsNegative(); + buildSign(); + buildInverse(); + buildOne(); + buildLoad(); + buildTimesScalar(); + buildBatchInverse$6(module, prefix); + buildBatchConvertion$5(module, prefix + "_batchToMontgomery", prefix + "_toMontgomery", n8, n8); + buildBatchConvertion$5(module, prefix + "_batchFromMontgomery", prefix + "_fromMontgomery", n8, n8); + buildBatchConvertion$5(module, prefix + "_batchNeg", prefix + "_neg", n8, n8); + buildBatchOp$1(module, prefix + "_batchAdd", prefix + "_add", n8, n8); + buildBatchOp$1(module, prefix + "_batchSub", prefix + "_sub", n8, n8); + buildBatchOp$1(module, prefix + "_batchMul", prefix + "_mul", n8, n8); + + module.exportFunction(prefix + "_add"); + module.exportFunction(prefix + "_sub"); + module.exportFunction(prefix + "_neg"); + module.exportFunction(prefix + "_isNegative"); + module.exportFunction(prefix + "_isOne"); + module.exportFunction(prefix + "_sign"); + module.exportFunction(prefix + "_mReduct"); + module.exportFunction(prefix + "_mul"); + module.exportFunction(prefix + "_square"); + module.exportFunction(prefix + "_squareOld"); + module.exportFunction(prefix + "_fromMontgomery"); + module.exportFunction(prefix + "_toMontgomery"); + module.exportFunction(prefix + "_inverse"); + module.exportFunction(prefix + "_one"); + module.exportFunction(prefix + "_load"); + module.exportFunction(prefix + "_timesScalar"); + buildExp$5( + module, + prefix + "_exp", + n8, + prefix + "_mul", + prefix + "_square", + intPrefix + "_copy", + prefix + "_one", + ); + module.exportFunction(prefix + "_exp"); + module.exportFunction(prefix + "_batchInverse"); + if (isPrime$2(q)) { + buildSqrt(); + buildIsSquare(); + module.exportFunction(prefix + "_sqrt"); + module.exportFunction(prefix + "_isSquare"); + } + module.exportFunction(prefix + "_batchToMontgomery"); + module.exportFunction(prefix + "_batchFromMontgomery"); + // console.log(module.functionIdxByName); + + return prefix; + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + const buildF1m$5 =build_f1m$1; + const { bitLength: bitLength$9 } = bigint$1; + + var build_f1$1 = function buildF1(module, _q, _prefix, _f1mPrefix, _intPrefix) { + + const q = BigInt(_q); + const n64 = Math.floor((bitLength$9(q - 1n) - 1)/64) +1; + const n8 = n64*8; + + const prefix = _prefix || "f1"; + if (module.modules[prefix]) return prefix; // already builded + module.modules[prefix] = { + n64: n64 + }; + + const intPrefix = _intPrefix || "int"; + const f1mPrefix = buildF1m$5(module, q, _f1mPrefix, intPrefix); + + + const pR2 = module.modules[f1mPrefix].pR2; + const pq = module.modules[f1mPrefix].pq; + const pePlusOne = module.modules[f1mPrefix].pePlusOne; + + function buildMul() { + const pAux1 = module.alloc(n8); + + const f = module.addFunction(prefix+ "_mul"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + f.addCode(c.call(f1mPrefix + "_mul", c.getLocal("x"), c.getLocal("y"), c.i32_const(pAux1))); + f.addCode(c.call(f1mPrefix + "_mul", c.i32_const(pAux1), c.i32_const(pR2), c.getLocal("r"))); + } + + function buildSquare() { + const f = module.addFunction(prefix+"_square"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode(c.call(prefix + "_mul", c.getLocal("x"), c.getLocal("x"), c.getLocal("r"))); + } + + + function buildInverse() { + + const f = module.addFunction(prefix+ "_inverse"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + f.addCode(c.call(intPrefix + "_inverseMod", c.getLocal("x"), c.i32_const(pq), c.getLocal("r"))); + } + + function buildIsNegative() { + const f = module.addFunction(prefix+"_isNegative"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call(intPrefix + "_gte", c.getLocal("x"), c.i32_const(pePlusOne) ) + ); + } + + + buildMul(); + buildSquare(); + buildInverse(); + buildIsNegative(); + module.exportFunction(f1mPrefix + "_add", prefix + "_add"); + module.exportFunction(f1mPrefix + "_sub", prefix + "_sub"); + module.exportFunction(f1mPrefix + "_neg", prefix + "_neg"); + module.exportFunction(prefix + "_mul"); + module.exportFunction(prefix + "_square"); + module.exportFunction(prefix + "_inverse"); + module.exportFunction(prefix + "_isNegative"); + module.exportFunction(f1mPrefix + "_copy", prefix+"_copy"); + module.exportFunction(f1mPrefix + "_zero", prefix+"_zero"); + module.exportFunction(f1mPrefix + "_one", prefix+"_one"); + module.exportFunction(f1mPrefix + "_isZero", prefix+"_isZero"); + module.exportFunction(f1mPrefix + "_eq", prefix+"_eq"); + + return prefix; + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + const buildExp$4 = build_timesscalar$1; + const buildBatchInverse$5 = build_batchinverse$1; + const utils$a = utils$c; + + var build_f2m$1 = function buildF2m(module, mulNonResidueFn, prefix, f1mPrefix) { + + if (module.modules[prefix]) return prefix; // already builded + + const f1n8 = module.modules[f1mPrefix].n64*8; + const q = module.modules[f1mPrefix].q; + + module.modules[prefix] = { + n64: module.modules[f1mPrefix].n64*2 + }; + + function buildAdd() { + const f = module.addFunction(prefix+"_add"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_add", x0, y0, r0), + c.call(f1mPrefix+"_add", x1, y1, r1), + ); + } + + function buildTimesScalar() { + const f = module.addFunction(prefix+"_timesScalar"); + f.addParam("x", "i32"); + f.addParam("scalar", "i32"); + f.addParam("scalarLen", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_timesScalar", x0, c.getLocal("scalar"), c.getLocal("scalarLen"), r0), + c.call(f1mPrefix+"_timesScalar", x1, c.getLocal("scalar"), c.getLocal("scalarLen"), r1), + ); + } + + function buildSub() { + const f = module.addFunction(prefix+"_sub"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_sub", x0, y0, r0), + c.call(f1mPrefix+"_sub", x1, y1, r1), + ); + } + + function buildNeg() { + const f = module.addFunction(prefix+"_neg"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_neg", x0, r0), + c.call(f1mPrefix+"_neg", x1, r1), + ); + } + + function buildConjugate() { + const f = module.addFunction(prefix+"_conjugate"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_copy", x0, r0), + c.call(f1mPrefix+"_neg", x1, r1), + ); + } + + + function buildIsNegative() { + const f = module.addFunction(prefix+"_isNegative"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + + f.addCode( + c.if( + c.call(f1mPrefix+"_isZero", x1), + c.ret(c.call(f1mPrefix+"_isNegative", x0)) + ), + c.ret(c.call(f1mPrefix+"_isNegative", x1)) + ); + } + + function buildMul() { + const f = module.addFunction(prefix+"_mul"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + const A = c.i32_const(module.alloc(f1n8)); + const B = c.i32_const(module.alloc(f1n8)); + const C = c.i32_const(module.alloc(f1n8)); + const D = c.i32_const(module.alloc(f1n8)); + + + f.addCode( + c.call(f1mPrefix + "_mul", x0, y0, A), // A = x0*y0 + c.call(f1mPrefix + "_mul", x1, y1, B), // B = x1*y1 + + c.call(f1mPrefix + "_add", x0, x1, C), // C = x0 + x1 + c.call(f1mPrefix + "_add", y0, y1, D), // D = y0 + y1 + c.call(f1mPrefix + "_mul", C, D, C), // C = (x0 + x1)*(y0 + y1) = x0*y0+x0*y1+x1*y0+x1*y1 + + // c.call(f1mPrefix + "_mul", B, c.i32_const(pNonResidue), r0), // r0 = nr*(x1*y1) + c.call(mulNonResidueFn, B, r0), // r0 = nr*(x1*y1) + c.call(f1mPrefix + "_add", A, r0, r0), // r0 = x0*y0 + nr*(x1*y1) + c.call(f1mPrefix + "_add", A, B, r1), // r1 = x0*y0+x1*y1 + c.call(f1mPrefix + "_sub", C, r1, r1) // r1 = x0*y0+x0*y1+x1*y0+x1*y1 - x0*y0+x1*y1 = x0*y1+x1*y0 + ); + + } + + function buildMul1() { + const f = module.addFunction(prefix+"_mul1"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const y = c.getLocal("y"); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + + f.addCode( + c.call(f1mPrefix + "_mul", x0, y, r0), // A = x0*y + c.call(f1mPrefix + "_mul", x1, y, r1), // B = x1*y + ); + } + + function buildSquare() { + const f = module.addFunction(prefix+"_square"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + const AB = c.i32_const(module.alloc(f1n8)); + const APB = c.i32_const(module.alloc(f1n8)); + const APNB = c.i32_const(module.alloc(f1n8)); + const ABPNAB = c.i32_const(module.alloc(f1n8)); + + + f.addCode( + // AB = x0*y1 + c.call(f1mPrefix + "_mul", x0, x1, AB), + + // APB = x0+y1 + c.call(f1mPrefix + "_add", x0, x1, APB), + + // APBN0 = x0 + nr*x1 + c.call(mulNonResidueFn, x1, APNB), + c.call(f1mPrefix + "_add", x0, APNB, APNB), + + // ABPNAB = ab + nr*ab + c.call(mulNonResidueFn, AB, ABPNAB), + c.call(f1mPrefix + "_add", ABPNAB, AB, ABPNAB), + + // r0 = APB * APNB - ABPNAB + c.call(f1mPrefix + "_mul", APB, APNB, r0), + c.call(f1mPrefix + "_sub", r0, ABPNAB, r0), + + // r1 = AB + AB + c.call(f1mPrefix + "_add", AB, AB, r1), + ); + + } + + + function buildToMontgomery() { + const f = module.addFunction(prefix+"_toMontgomery"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_toMontgomery", x0, r0), + c.call(f1mPrefix+"_toMontgomery", x1, r1) + ); + } + + function buildFromMontgomery() { + const f = module.addFunction(prefix+"_fromMontgomery"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_fromMontgomery", x0, r0), + c.call(f1mPrefix+"_fromMontgomery", x1, r1) + ); + } + + function buildCopy() { + const f = module.addFunction(prefix+"_copy"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_copy", x0, r0), + c.call(f1mPrefix+"_copy", x1, r1) + ); + } + + function buildZero() { + const f = module.addFunction(prefix+"_zero"); + f.addParam("x", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_zero", x0), + c.call(f1mPrefix+"_zero", x1) + ); + } + + function buildOne() { + const f = module.addFunction(prefix+"_one"); + f.addParam("x", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_one", x0), + c.call(f1mPrefix+"_zero", x1) + ); + } + + function buildEq() { + const f = module.addFunction(prefix+"_eq"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + + f.addCode( + c.i32_and( + c.call(f1mPrefix+"_eq", x0, y0), + c.call(f1mPrefix+"_eq", x1, y1) + ) + ); + } + + function buildIsZero() { + const f = module.addFunction(prefix+"_isZero"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + + f.addCode( + c.i32_and( + c.call(f1mPrefix+"_isZero", x0), + c.call(f1mPrefix+"_isZero", x1) + ) + ); + } + + function buildInverse() { + const f = module.addFunction(prefix+"_inverse"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + + const t0 = c.i32_const(module.alloc(f1n8)); + const t1 = c.i32_const(module.alloc(f1n8)); + const t2 = c.i32_const(module.alloc(f1n8)); + const t3 = c.i32_const(module.alloc(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_square", x0, t0), + c.call(f1mPrefix+"_square", x1, t1), + // c.call(f1mPrefix+"_mul", t1, c.i32_const(pNonResidue), t2), + c.call(mulNonResidueFn, t1, t2), + + c.call(f1mPrefix+"_sub", t0, t2, t2), + c.call(f1mPrefix+"_inverse", t2, t3), + + c.call(f1mPrefix+"_mul", x0, t3, r0), + c.call(f1mPrefix+"_mul", x1, t3, r1), + c.call(f1mPrefix+"_neg", r1, r1), + ); + } + + + function buildSign() { + const f = module.addFunction(prefix+"_sign"); + f.addParam("x", "i32"); + f.addLocal("s", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + + f.addCode( + c.setLocal("s" , c.call( f1mPrefix + "_sign", x1)), + c.if( + c.getLocal("s"), + c.ret(c.getLocal("s")) + ), + c.ret(c.call( f1mPrefix + "_sign", x0)) + ); + } + + function buildIsOne() { + const f = module.addFunction(prefix+"_isOne"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + + f.addCode( + c.ret(c.i32_and( + c.call(f1mPrefix + "_isOne", x0), + c.call(f1mPrefix + "_isZero", x1), + )) + ); + } + + + // Check here: https://eprint.iacr.org/2012/685.pdf + // Alg 9adj + function buildSqrt() { + + const f = module.addFunction(prefix+"_sqrt"); + f.addParam("a", "i32"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + // BigInt can't take `undefined` so we use `|| 0` + const e34 = c.i32_const(module.alloc(utils$a.bigInt2BytesLE((BigInt(q || 0) - 3n) / 4n, f1n8 ))); + // BigInt can't take `undefined` so we use `|| 0` + const e12 = c.i32_const(module.alloc(utils$a.bigInt2BytesLE((BigInt(q || 0) - 1n) / 2n, f1n8 ))); + + const a = c.getLocal("a"); + const a1 = c.i32_const(module.alloc(f1n8*2)); + const alpha = c.i32_const(module.alloc(f1n8*2)); + const a0 = c.i32_const(module.alloc(f1n8*2)); + const pn1 = module.alloc(f1n8*2); + const n1 = c.i32_const(pn1); + const n1a = c.i32_const(pn1); + const n1b = c.i32_const(pn1+f1n8); + const x0 = c.i32_const(module.alloc(f1n8*2)); + const b = c.i32_const(module.alloc(f1n8*2)); + + f.addCode( + + c.call(prefix + "_one", n1), + c.call(prefix + "_neg", n1, n1), + + // const a1 = F.pow(a, F.sqrt_e34); + c.call(prefix + "_exp", a, e34, c.i32_const(f1n8), a1), + + // const a1 = F.pow(a, F.sqrt_e34); + c.call(prefix + "_square", a1, alpha), + c.call(prefix + "_mul", a, alpha, alpha), + + // const a0 = F.mul(F.frobenius(1, alfa), alfa); + c.call(prefix + "_conjugate", alpha, a0), + c.call(prefix + "_mul", a0, alpha, a0), + + // if (F.eq(a0, F.negone)) return null; + c.if(c.call(prefix + "_eq",a0,n1), c.unreachable() ), + + // const x0 = F.mul(a1, a); + c.call(prefix + "_mul", a1, a, x0), + + // if (F.eq(alfa, F.negone)) { + c.if( + c.call(prefix + "_eq", alpha, n1), + [ + // x = F.mul(x0, [F.F.zero, F.F.one]); + ...c.call(f1mPrefix + "_zero", n1a), + ...c.call(f1mPrefix + "_one", n1b), + ...c.call(prefix + "_mul", n1, x0, c.getLocal("pr")), + ], + [ + // const b = F.pow(F.add(F.one, alfa), F.sqrt_e12); + ...c.call(prefix + "_one", b), + ...c.call(prefix + "_add", b, alpha, b), + ...c.call(prefix + "_exp", b, e12, c.i32_const(f1n8), b), + + // x = F.mul(b, x0); + ...c.call(prefix + "_mul", b, x0, c.getLocal("pr")), + ] + ) + ); + + } + + + function buildIsSquare() { + + const f = module.addFunction(prefix+"_isSquare"); + f.addParam("a", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + // BigInt can't take `undefined` so we use `|| 0` + const e34 = c.i32_const(module.alloc(utils$a.bigInt2BytesLE((BigInt(q || 0) - 3n) / 4n, f1n8 ))); + + const a = c.getLocal("a"); + const a1 = c.i32_const(module.alloc(f1n8*2)); + const alpha = c.i32_const(module.alloc(f1n8*2)); + const a0 = c.i32_const(module.alloc(f1n8*2)); + const pn1 = module.alloc(f1n8*2); + const n1 = c.i32_const(pn1); + + f.addCode( + + c.call(prefix + "_one", n1), + c.call(prefix + "_neg", n1, n1), + + // const a1 = F.pow(a, F.sqrt_e34); + c.call(prefix + "_exp", a, e34, c.i32_const(f1n8), a1), + + // const a1 = F.pow(a, F.sqrt_e34); + c.call(prefix + "_square", a1, alpha), + c.call(prefix + "_mul", a, alpha, alpha), + + // const a0 = F.mul(F.frobenius(1, alfa), alfa); + c.call(prefix + "_conjugate", alpha, a0), + c.call(prefix + "_mul", a0, alpha, a0), + + // if (F.eq(a0, F.negone)) return null; + c.if( + c.call( + prefix + "_eq", + a0, + n1 + ), + c.ret(c.i32_const(0)) + ), + c.ret(c.i32_const(1)) + ); + + } + + + buildIsZero(); + buildIsOne(); + buildZero(); + buildOne(); + buildCopy(); + buildMul(); + buildMul1(); + buildSquare(); + buildAdd(); + buildSub(); + buildNeg(); + buildConjugate(); + buildToMontgomery(); + buildFromMontgomery(); + buildEq(); + buildInverse(); + buildTimesScalar(); + buildSign(); + buildIsNegative(); + + module.exportFunction(prefix + "_isZero"); + module.exportFunction(prefix + "_isOne"); + module.exportFunction(prefix + "_zero"); + module.exportFunction(prefix + "_one"); + module.exportFunction(prefix + "_copy"); + module.exportFunction(prefix + "_mul"); + module.exportFunction(prefix + "_mul1"); + module.exportFunction(prefix + "_square"); + module.exportFunction(prefix + "_add"); + module.exportFunction(prefix + "_sub"); + module.exportFunction(prefix + "_neg"); + module.exportFunction(prefix + "_sign"); + module.exportFunction(prefix + "_conjugate"); + module.exportFunction(prefix + "_fromMontgomery"); + module.exportFunction(prefix + "_toMontgomery"); + module.exportFunction(prefix + "_eq"); + module.exportFunction(prefix + "_inverse"); + buildBatchInverse$5(module, prefix); + buildExp$4( + module, + prefix + "_exp", + f1n8*2, + prefix + "_mul", + prefix + "_square", + prefix + "_copy", + prefix + "_one", + ); + buildSqrt(); + buildIsSquare(); + + module.exportFunction(prefix + "_exp"); + module.exportFunction(prefix + "_timesScalar"); + module.exportFunction(prefix + "_batchInverse"); + module.exportFunction(prefix + "_sqrt"); + module.exportFunction(prefix + "_isSquare"); + module.exportFunction(prefix + "_isNegative"); + + + return prefix; + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + const buildExp$3 = build_timesscalar$1; + const buildBatchInverse$4 = build_batchinverse$1; + + var build_f3m$1 = function buildF3m(module, mulNonResidueFn, prefix, f1mPrefix) { + + if (module.modules[prefix]) return prefix; // already builded + + const f1n8 = module.modules[f1mPrefix].n64*8; + module.modules[prefix] = { + n64: module.modules[f1mPrefix].n64*3 + }; + + function buildAdd() { + const f = module.addFunction(prefix+"_add"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + const y2 = c.i32_add(c.getLocal("y"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_add", x0, y0, r0), + c.call(f1mPrefix+"_add", x1, y1, r1), + c.call(f1mPrefix+"_add", x2, y2, r2), + ); + } + + function buildTimesScalar() { + const f = module.addFunction(prefix+"_timesScalar"); + f.addParam("x", "i32"); + f.addParam("scalar", "i32"); + f.addParam("scalarLen", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_timesScalar", x0, c.getLocal("scalar"), c.getLocal("scalarLen"), r0), + c.call(f1mPrefix+"_timesScalar", x1, c.getLocal("scalar"), c.getLocal("scalarLen"), r1), + c.call(f1mPrefix+"_timesScalar", x2, c.getLocal("scalar"), c.getLocal("scalarLen"), r2), + ); + } + + + function buildSub() { + const f = module.addFunction(prefix+"_sub"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + const y2 = c.i32_add(c.getLocal("y"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_sub", x0, y0, r0), + c.call(f1mPrefix+"_sub", x1, y1, r1), + c.call(f1mPrefix+"_sub", x2, y2, r2), + ); + } + + function buildNeg() { + const f = module.addFunction(prefix+"_neg"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_neg", x0, r0), + c.call(f1mPrefix+"_neg", x1, r1), + c.call(f1mPrefix+"_neg", x2, r2), + ); + } + + function buildIsNegative() { + const f = module.addFunction(prefix+"_isNegative"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + + f.addCode( + c.if( + c.call(f1mPrefix+"_isZero", x2), + c.if( + c.call(f1mPrefix+"_isZero", x1), + c.ret(c.call(f1mPrefix+"_isNegative", x0)), + c.ret(c.call(f1mPrefix+"_isNegative", x1)) + ) + ), + c.ret(c.call(f1mPrefix+"_isNegative", x2)) + ); + } + + + function buildMul() { + const f = module.addFunction(prefix+"_mul"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.addParam("r", "i32"); + + const cd = f.getCodeBuilder(); + + const a = cd.getLocal("x"); + const b = cd.i32_add(cd.getLocal("x"), cd.i32_const(f1n8)); + const c = cd.i32_add(cd.getLocal("x"), cd.i32_const(2*f1n8)); + const A = cd.getLocal("y"); + const B = cd.i32_add(cd.getLocal("y"), cd.i32_const(f1n8)); + const C = cd.i32_add(cd.getLocal("y"), cd.i32_const(2*f1n8)); + const r0 = cd.getLocal("r"); + const r1 = cd.i32_add(cd.getLocal("r"), cd.i32_const(f1n8)); + const r2 = cd.i32_add(cd.getLocal("r"), cd.i32_const(2*f1n8)); + + const aA = cd.i32_const(module.alloc(f1n8)); + const bB = cd.i32_const(module.alloc(f1n8)); + const cC = cd.i32_const(module.alloc(f1n8)); + const a_b = cd.i32_const(module.alloc(f1n8)); + const A_B = cd.i32_const(module.alloc(f1n8)); + const a_c = cd.i32_const(module.alloc(f1n8)); + const A_C = cd.i32_const(module.alloc(f1n8)); + const b_c = cd.i32_const(module.alloc(f1n8)); + const B_C = cd.i32_const(module.alloc(f1n8)); + const aA_bB = cd.i32_const(module.alloc(f1n8)); + const aA_cC = cd.i32_const(module.alloc(f1n8)); + const bB_cC = cd.i32_const(module.alloc(f1n8)); + const AUX = cd.i32_const(module.alloc(f1n8)); + + + f.addCode( + cd.call(f1mPrefix + "_mul", a, A, aA), + cd.call(f1mPrefix + "_mul", b, B, bB), + cd.call(f1mPrefix + "_mul", c, C, cC), + + cd.call(f1mPrefix + "_add", a, b, a_b), + cd.call(f1mPrefix + "_add", A, B, A_B), + cd.call(f1mPrefix + "_add", a, c, a_c), + cd.call(f1mPrefix + "_add", A, C, A_C), + cd.call(f1mPrefix + "_add", b, c, b_c), + cd.call(f1mPrefix + "_add", B, C, B_C), + + cd.call(f1mPrefix + "_add", aA, bB, aA_bB), + cd.call(f1mPrefix + "_add", aA, cC, aA_cC), + cd.call(f1mPrefix + "_add", bB, cC, bB_cC), + + cd.call(f1mPrefix + "_mul", b_c, B_C, r0), + cd.call(f1mPrefix + "_sub", r0, bB_cC, r0), + cd.call(mulNonResidueFn, r0, r0), + cd.call(f1mPrefix + "_add", aA, r0, r0), + + cd.call(f1mPrefix + "_mul", a_b, A_B, r1), + cd.call(f1mPrefix + "_sub", r1, aA_bB, r1), + cd.call(mulNonResidueFn, cC, AUX), + cd.call(f1mPrefix + "_add", r1, AUX, r1), + + cd.call(f1mPrefix + "_mul", a_c, A_C, r2), + cd.call(f1mPrefix + "_sub", r2, aA_cC, r2), + cd.call(f1mPrefix + "_add", r2, bB, r2), + ); + + } + + function buildSquare() { + const f = module.addFunction(prefix+"_square"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const A = c.getLocal("x"); + const B = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const C = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + const s0 = c.i32_const(module.alloc(f1n8)); + const ab = c.i32_const(module.alloc(f1n8)); + const s1 = c.i32_const(module.alloc(f1n8)); + const s2 = c.i32_const(module.alloc(f1n8)); + const bc = c.i32_const(module.alloc(f1n8)); + const s3 = c.i32_const(module.alloc(f1n8)); + const s4 = c.i32_const(module.alloc(f1n8)); + + + f.addCode( + + c.call(f1mPrefix + "_square", A, s0), + c.call(f1mPrefix + "_mul", A, B, ab), + c.call(f1mPrefix + "_add", ab, ab, s1), + + c.call(f1mPrefix + "_sub", A, B, s2), + c.call(f1mPrefix + "_add", s2, C, s2), + c.call(f1mPrefix + "_square", s2, s2), + + c.call(f1mPrefix + "_mul", B, C, bc), + c.call(f1mPrefix + "_add", bc, bc, s3), + + c.call(f1mPrefix + "_square", C, s4), + + c.call(mulNonResidueFn, s3, r0), + c.call(f1mPrefix + "_add", s0, r0, r0), + + c.call(mulNonResidueFn, s4, r1), + c.call(f1mPrefix + "_add", s1, r1, r1), + + c.call(f1mPrefix + "_add", s0, s4, r2), + c.call(f1mPrefix + "_sub", s3, r2, r2), + c.call(f1mPrefix + "_add", s2, r2, r2), + c.call(f1mPrefix + "_add", s1, r2, r2), + ); + + } + + + function buildToMontgomery() { + const f = module.addFunction(prefix+"_toMontgomery"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_toMontgomery", x0, r0), + c.call(f1mPrefix+"_toMontgomery", x1, r1), + c.call(f1mPrefix+"_toMontgomery", x2, r2) + ); + } + + function buildFromMontgomery() { + const f = module.addFunction(prefix+"_fromMontgomery"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_fromMontgomery", x0, r0), + c.call(f1mPrefix+"_fromMontgomery", x1, r1), + c.call(f1mPrefix+"_fromMontgomery", x2, r2) + ); + } + + function buildCopy() { + const f = module.addFunction(prefix+"_copy"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_copy", x0, r0), + c.call(f1mPrefix+"_copy", x1, r1), + c.call(f1mPrefix+"_copy", x2, r2), + ); + } + + function buildZero() { + const f = module.addFunction(prefix+"_zero"); + f.addParam("x", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_zero", x0), + c.call(f1mPrefix+"_zero", x1), + c.call(f1mPrefix+"_zero", x2), + ); + } + + function buildOne() { + const f = module.addFunction(prefix+"_one"); + f.addParam("x", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + + f.addCode( + c.call(f1mPrefix+"_one", x0), + c.call(f1mPrefix+"_zero", x1), + c.call(f1mPrefix+"_zero", x2), + ); + } + + function buildEq() { + const f = module.addFunction(prefix+"_eq"); + f.addParam("x", "i32"); + f.addParam("y", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const y0 = c.getLocal("y"); + const y1 = c.i32_add(c.getLocal("y"), c.i32_const(f1n8)); + const y2 = c.i32_add(c.getLocal("y"), c.i32_const(2*f1n8)); + + f.addCode( + c.i32_and( + c.i32_and( + c.call(f1mPrefix+"_eq", x0, y0), + c.call(f1mPrefix+"_eq", x1, y1), + ), + c.call(f1mPrefix+"_eq", x2, y2) + ) + ); + } + + function buildIsZero() { + const f = module.addFunction(prefix+"_isZero"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + + f.addCode( + c.i32_and( + c.i32_and( + c.call(f1mPrefix+"_isZero", x0), + c.call(f1mPrefix+"_isZero", x1) + ), + c.call(f1mPrefix+"_isZero", x2) + ) + ); + } + + function buildInverse() { + const f = module.addFunction(prefix+"_inverse"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + const r0 = c.getLocal("r"); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(f1n8)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(2*f1n8)); + + const t0 = c.i32_const(module.alloc(f1n8)); + const t1 = c.i32_const(module.alloc(f1n8)); + const t2 = c.i32_const(module.alloc(f1n8)); + const t3 = c.i32_const(module.alloc(f1n8)); + const t4 = c.i32_const(module.alloc(f1n8)); + const t5 = c.i32_const(module.alloc(f1n8)); + const c0 = c.i32_const(module.alloc(f1n8)); + const c1 = c.i32_const(module.alloc(f1n8)); + const c2 = c.i32_const(module.alloc(f1n8)); + const t6 = c.i32_const(module.alloc(f1n8)); + const AUX = c.i32_const(module.alloc(f1n8)); + + f.addCode( + c.call(f1mPrefix+"_square", x0, t0), + c.call(f1mPrefix+"_square", x1, t1), + c.call(f1mPrefix+"_square", x2, t2), + c.call(f1mPrefix+"_mul", x0, x1, t3), + c.call(f1mPrefix+"_mul", x0, x2, t4), + c.call(f1mPrefix+"_mul", x1, x2, t5), + + c.call(mulNonResidueFn, t5, c0), + c.call(f1mPrefix+"_sub", t0, c0, c0), + + c.call(mulNonResidueFn, t2, c1), + c.call(f1mPrefix+"_sub", c1, t3, c1), + + c.call(f1mPrefix+"_sub", t1, t4, c2), + + c.call(f1mPrefix+"_mul", x2, c1, t6), + c.call(f1mPrefix+"_mul", x1, c2, AUX), + c.call(f1mPrefix+"_add", t6, AUX, t6), + c.call(mulNonResidueFn, t6, t6), + c.call(f1mPrefix+"_mul", x0, c0, AUX), + c.call(f1mPrefix+"_add", AUX, t6, t6), + + c.call(f1mPrefix+"_inverse", t6, t6), + + c.call(f1mPrefix+"_mul", t6, c0, r0), + c.call(f1mPrefix+"_mul", t6, c1, r1), + c.call(f1mPrefix+"_mul", t6, c2, r2) + ); + } + + + function buildSign() { + const f = module.addFunction(prefix+"_sign"); + f.addParam("x", "i32"); + f.addLocal("s", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(2*f1n8)); + + f.addCode( + c.setLocal("s" , c.call( f1mPrefix + "_sign", x2)), + c.if( + c.getLocal("s"), + c.ret(c.getLocal("s")) + ), + c.setLocal("s" , c.call( f1mPrefix + "_sign", x1)), + c.if( + c.getLocal("s"), + c.ret(c.getLocal("s")) + ), + c.ret(c.call( f1mPrefix + "_sign", x0)) + ); + } + + function buildIsOne() { + const f = module.addFunction(prefix+"_isOne"); + f.addParam("x", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(f1n8*2)); + + f.addCode( + c.ret( + c.i32_and( + c.i32_and( + c.call(f1mPrefix + "_isOne", x0), + c.call(f1mPrefix + "_isZero", x1) + ), + c.call(f1mPrefix + "_isZero", x2) + ) + ) + ); + } + + buildIsZero(); + buildIsOne(); + buildZero(); + buildOne(); + buildCopy(); + buildMul(); + buildSquare(); + buildAdd(); + buildSub(); + buildNeg(); + buildSign(); + buildToMontgomery(); + buildFromMontgomery(); + buildEq(); + buildInverse(); + buildTimesScalar(); + buildIsNegative(); + + module.exportFunction(prefix + "_isZero"); + module.exportFunction(prefix + "_isOne"); + module.exportFunction(prefix + "_zero"); + module.exportFunction(prefix + "_one"); + module.exportFunction(prefix + "_copy"); + module.exportFunction(prefix + "_mul"); + module.exportFunction(prefix + "_square"); + module.exportFunction(prefix + "_add"); + module.exportFunction(prefix + "_sub"); + module.exportFunction(prefix + "_neg"); + module.exportFunction(prefix + "_sign"); + module.exportFunction(prefix + "_fromMontgomery"); + module.exportFunction(prefix + "_toMontgomery"); + module.exportFunction(prefix + "_eq"); + module.exportFunction(prefix + "_inverse"); + buildBatchInverse$4(module, prefix); + buildExp$3( + module, + prefix + "_exp", + f1n8*3, + prefix + "_mul", + prefix + "_square", + prefix + "_copy", + prefix + "_one" + ); + module.exportFunction(prefix + "_exp"); + module.exportFunction(prefix + "_timesScalar"); + module.exportFunction(prefix + "_batchInverse"); + module.exportFunction(prefix + "_isNegative"); + + return prefix; + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + var build_timesscalarnaf$1 = function buildTimesScalarNAF(module, fnName, elementLen, opAB, opAA, opAmB, opCopy, opInit) { + + const f = module.addFunction(fnName); + f.addParam("base", "i32"); + f.addParam("scalar", "i32"); + f.addParam("scalarLength", "i32"); + f.addParam("r", "i32"); + f.addLocal("old0", "i32"); + f.addLocal("nbits", "i32"); + f.addLocal("i", "i32"); + f.addLocal("last", "i32"); + f.addLocal("cur", "i32"); + f.addLocal("carry", "i32"); + f.addLocal("p", "i32"); + + const c = f.getCodeBuilder(); + + const aux = c.i32_const(module.alloc(elementLen)); + + function getBit(IDX) { + return c.i32_and( + c.i32_shr_u( + c.i32_load( + c.i32_add( + c.getLocal("scalar"), + c.i32_and( + c.i32_shr_u( + IDX, + c.i32_const(3) + ), + c.i32_const(0xFFFFFFFC) + ) + ) + ), + c.i32_and( + IDX, + c.i32_const(0x1F) + ) + ), + c.i32_const(1) + ); + } + + function pushBit(b) { + return [ + ...c.i32_store8( + c.getLocal("p"), + c.i32_const(b) + ), + ...c.setLocal( + "p", + c.i32_add( + c.getLocal("p"), + c.i32_const(1) + ) + ) + ]; + } + + f.addCode( + c.if( + c.i32_eqz(c.getLocal("scalarLength")), + [ + ...c.call(opInit, c.getLocal("r")), + ...c.ret([]) + ] + ), + c.setLocal("nbits", c.i32_shl(c.getLocal("scalarLength"), c.i32_const(3))), + c.setLocal("old0", c.i32_load(c.i32_const(0))), + c.setLocal("p", c.getLocal("old0")), + c.i32_store( + c.i32_const(0), + c.i32_and( + c.i32_add( + c.i32_add( + c.getLocal("old0"), + c.i32_const(32) + ), + c.getLocal("nbits") + ), + c.i32_const(0xFFFFFFF8) + ) + ), + c.setLocal("i", c.i32_const(1)), + + c.setLocal("last",getBit(c.i32_const(0))), + c.setLocal("carry",c.i32_const(0)), + + c.block(c.loop( + c.br_if(1, c.i32_eq( c.getLocal("i"), c.getLocal("nbits"))), + + c.setLocal("cur", getBit(c.getLocal("i"))), + c.if( c.getLocal("last"), + c.if( c.getLocal("cur"), + c.if(c.getLocal("carry"), + [ + ...c.setLocal("last", c.i32_const(0)), + ...c.setLocal("carry", c.i32_const(1)), + ...pushBit(1) + ] + , + [ + ...c.setLocal("last", c.i32_const(0)), + ...c.setLocal("carry", c.i32_const(1)), + ...pushBit(255) + ], + ), + c.if(c.getLocal("carry"), + [ + ...c.setLocal("last", c.i32_const(0)), + ...c.setLocal("carry", c.i32_const(1)), + ...pushBit(255) + ] + , + [ + ...c.setLocal("last", c.i32_const(0)), + ...c.setLocal("carry", c.i32_const(0)), + ...pushBit(1) + ], + ), + ), + c.if( c.getLocal("cur"), + c.if(c.getLocal("carry"), + [ + ...c.setLocal("last", c.i32_const(0)), + ...c.setLocal("carry", c.i32_const(1)), + ...pushBit(0) + ] + , + [ + ...c.setLocal("last", c.i32_const(1)), + ...c.setLocal("carry", c.i32_const(0)), + ...pushBit(0) + ], + ), + c.if(c.getLocal("carry"), + [ + ...c.setLocal("last", c.i32_const(1)), + ...c.setLocal("carry", c.i32_const(0)), + ...pushBit(0) + ] + , + [ + ...c.setLocal("last", c.i32_const(0)), + ...c.setLocal("carry", c.i32_const(0)), + ...pushBit(0) + ], + ), + ) + ), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )), + + c.if( c.getLocal("last"), + c.if(c.getLocal("carry"), + [ + ...pushBit(255), + ...pushBit(0), + ...pushBit(1) + ] + , + [ + ...pushBit(1) + ], + ), + c.if(c.getLocal("carry"), + [ + ...pushBit(0), + ...pushBit(1) + ] + ), + ), + + c.setLocal("p", c.i32_sub(c.getLocal("p"), c.i32_const(1))), + + // p already points to the last bit + + c.call(opCopy, c.getLocal("base"), aux), + + c.call(opInit, c.getLocal("r")), + + c.block(c.loop( + + + c.call(opAA, c.getLocal("r"), c.getLocal("r")), + + + c.setLocal("cur", + c.i32_load8_u( + c.getLocal("p") + ) + ), + + c.if( + c.getLocal("cur"), + c.if( + c.i32_eq(c.getLocal("cur"), c.i32_const(1)), + c.call(opAB, c.getLocal("r"), aux, c.getLocal("r")), + c.call(opAmB, c.getLocal("r"), aux, c.getLocal("r")), + ) + ), + + c.br_if(1, c.i32_eq( c.getLocal("old0"), c.getLocal("p"))), + c.setLocal("p", c.i32_sub(c.getLocal("p"), c.i32_const(1))), + c.br(0) + + )), + + c.i32_store( c.i32_const(0), c.getLocal("old0")) + + ); + + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + var build_multiexp$1 = function buildMultiexp(module, prefix, fnName, opAdd, n8b) { + + const n64g = module.modules[prefix].n64; + const n8g = n64g*8; + + function buildGetChunk() { + const f = module.addFunction(fnName + "_getChunk"); + f.addParam("pScalar", "i32"); + f.addParam("scalarSize", "i32"); // Number of bytes of the scalar + f.addParam("startBit", "i32"); // Bit to start extract + f.addParam("chunkSize", "i32"); // Chunk size in bits + f.addLocal("bitsToEnd", "i32"); + f.addLocal("mask", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.setLocal("bitsToEnd", + c.i32_sub( + c.i32_mul( + c.getLocal("scalarSize"), + c.i32_const(8) + ), + c.getLocal("startBit") + ) + ), + c.if( + c.i32_gt_s( + c.getLocal("chunkSize"), + c.getLocal("bitsToEnd") + ), + c.setLocal( + "mask", + c.i32_sub( + c.i32_shl( + c.i32_const(1), + c.getLocal("bitsToEnd") + ), + c.i32_const(1) + ) + ), + c.setLocal( + "mask", + c.i32_sub( + c.i32_shl( + c.i32_const(1), + c.getLocal("chunkSize") + ), + c.i32_const(1) + ) + ) + ), + c.i32_and( + c.i32_shr_u( + c.i32_load( + c.i32_add( + c.getLocal("pScalar"), + c.i32_shr_u( + c.getLocal("startBit"), + c.i32_const(3) + ) + ), + 0, // offset + 0 // align to byte. + ), + c.i32_and( + c.getLocal("startBit"), + c.i32_const(0x7) + ) + ), + c.getLocal("mask") + ) + ); + } + + function buildMutiexpChunk() { + const f = module.addFunction(fnName + "_chunk"); + f.addParam("pBases", "i32"); + f.addParam("pScalars", "i32"); + f.addParam("scalarSize", "i32"); // Number of points + f.addParam("n", "i32"); // Number of points + f.addParam("startBit", "i32"); // bit where it starts the chunk + f.addParam("chunkSize", "i32"); // bit where it starts the chunk + f.addParam("pr", "i32"); + f.addLocal("nChunks", "i32"); + f.addLocal("itScalar", "i32"); + f.addLocal("endScalar", "i32"); + f.addLocal("itBase", "i32"); + f.addLocal("i", "i32"); + f.addLocal("j", "i32"); + f.addLocal("nTable", "i32"); + f.addLocal("pTable", "i32"); + f.addLocal("idx", "i32"); + f.addLocal("pIdxTable", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.if( + c.i32_eqz(c.getLocal("n")), + [ + ...c.call(prefix + "_zero", c.getLocal("pr")), + ...c.ret([]) + ] + ), + + // Allocate memory + + c.setLocal( + "nTable", + c.i32_shl( + c.i32_const(1), + c.getLocal("chunkSize") + ) + ), + c.setLocal("pTable", c.i32_load( c.i32_const(0) )), + c.i32_store( + c.i32_const(0), + c.i32_add( + c.getLocal("pTable"), + c.i32_mul( + c.getLocal("nTable"), + c.i32_const(n8g) + ) + ) + ), + + // Reset Table + c.setLocal("j", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("j"), + c.getLocal("nTable") + ) + ), + + c.call( + prefix + "_zero", + c.i32_add( + c.getLocal("pTable"), + c.i32_mul( + c.getLocal("j"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )), + + // Distribute elements + c.setLocal("itBase", c.getLocal("pBases")), + c.setLocal("itScalar", c.getLocal("pScalars")), + c.setLocal("endScalar", + c.i32_add( + c.getLocal("pScalars"), + c.i32_mul( + c.getLocal("n"), + c.getLocal("scalarSize") + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("itScalar"), + c.getLocal("endScalar") + ) + ), + + c.setLocal( + "idx", + c.call(fnName + "_getChunk", + c.getLocal("itScalar"), + c.getLocal("scalarSize"), + c.getLocal("startBit"), + c.getLocal("chunkSize") + ) + ), + + c.if( + c.getLocal("idx"), + [ + ...c.setLocal( + "pIdxTable", + c.i32_add( + c.getLocal("pTable"), + c.i32_mul( + c.i32_sub( + c.getLocal("idx"), + c.i32_const(1) + ), + c.i32_const(n8g) + ) + ) + ), + ...c.call( + opAdd, + c.getLocal("pIdxTable"), + c.getLocal("itBase"), + c.getLocal("pIdxTable"), + ) + ] + ), + + c.setLocal("itScalar", c.i32_add(c.getLocal("itScalar"), c.getLocal("scalarSize"))), + c.setLocal("itBase", c.i32_add(c.getLocal("itBase"), c.i32_const(n8b))), + c.br(0) + )), + + c.call(fnName + "_reduceTable", c.getLocal("pTable"), c.getLocal("chunkSize")), + c.call( + prefix + "_copy", + c.getLocal("pTable"), + c.getLocal("pr") + ), + + + c.i32_store( + c.i32_const(0), + c.getLocal("pTable") + ) + + ); + } + + function buildMultiexp() { + const f = module.addFunction(fnName); + f.addParam("pBases", "i32"); + f.addParam("pScalars", "i32"); + f.addParam("scalarSize", "i32"); // Number of points + f.addParam("n", "i32"); // Number of points + f.addParam("pr", "i32"); + f.addLocal("chunkSize", "i32"); + f.addLocal("nChunks", "i32"); + f.addLocal("itScalar", "i32"); + f.addLocal("endScalar", "i32"); + f.addLocal("itBase", "i32"); + f.addLocal("itBit", "i32"); + f.addLocal("i", "i32"); + f.addLocal("j", "i32"); + f.addLocal("nTable", "i32"); + f.addLocal("pTable", "i32"); + f.addLocal("idx", "i32"); + f.addLocal("pIdxTable", "i32"); + + const c = f.getCodeBuilder(); + + const aux = c.i32_const(module.alloc(n8g)); + + const pTSizes = module.alloc([ + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 16, 16, 15, 14, 13, 13, + 12, 11, 10, 9, 8, 7, 7, 6, + 5 , 4, 3, 2, 1, 1, 1, 1 + ]); + + f.addCode( + c.call(prefix + "_zero", c.getLocal("pr")), + c.if( + c.i32_eqz(c.getLocal("n")), + c.ret([]) + ), + c.setLocal("chunkSize", c.i32_load8_u( c.i32_clz(c.getLocal("n")), pTSizes )), + c.setLocal( + "nChunks", + c.i32_add( + c.i32_div_u( + c.i32_sub( + c.i32_shl( + c.getLocal("scalarSize"), + c.i32_const(3) + ), + c.i32_const(1) + ), + c.getLocal("chunkSize") + ), + c.i32_const(1) + ) + ), + + + // Allocate memory + + c.setLocal( + "itBit", + c.i32_mul( + c.i32_sub( + c.getLocal("nChunks"), + c.i32_const(1) + ), + c.getLocal("chunkSize") + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_lt_s( + c.getLocal("itBit"), + c.i32_const(0) + ) + ), + + // Double nChunk times + c.if( + c.i32_eqz(c.call(prefix + "_isZero", c.getLocal("pr"))), + [ + ...c.setLocal("j", c.i32_const(0)), + ...c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("j"), + c.getLocal("chunkSize") + ) + ), + + c.call(prefix + "_double", c.getLocal("pr"), c.getLocal("pr")), + + c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )) + ] + ), + + c.call( + fnName + "_chunk", + c.getLocal("pBases"), + c.getLocal("pScalars"), + c.getLocal("scalarSize"), + c.getLocal("n"), + c.getLocal("itBit"), + c.getLocal("chunkSize"), + aux + ), + + c.call( + prefix + "_add", + c.getLocal("pr"), + aux, + c.getLocal("pr") + ), + c.setLocal("itBit", c.i32_sub(c.getLocal("itBit"), c.getLocal("chunkSize"))), + c.br(0) + )) + ); + } + + function buildReduceTable() { + const f = module.addFunction(fnName + "_reduceTable"); + f.addParam("pTable", "i32"); + f.addParam("p", "i32"); // Number of bits of the table + f.addLocal("half", "i32"); + f.addLocal("it1", "i32"); + f.addLocal("it2", "i32"); + f.addLocal("pAcc", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.if( + c.i32_eq(c.getLocal("p"), c.i32_const(1)), + c.ret([]) + ), + c.setLocal( + "half", + c.i32_shl( + c.i32_const(1), + c.i32_sub( + c.getLocal("p"), + c.i32_const(1) + ) + ) + ), + + c.setLocal("it1", c.getLocal("pTable")), + c.setLocal( + "it2", + c.i32_add( + c.getLocal("pTable"), + c.i32_mul( + c.getLocal("half"), + c.i32_const(n8g) + ) + ) + ), + c.setLocal("pAcc", + c.i32_sub( + c.getLocal("it2"), + c.i32_const(n8g) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("it1"), + c.getLocal("pAcc") + ) + ), + c.call( + prefix + "_add", + c.getLocal("it1"), + c.getLocal("it2"), + c.getLocal("it1") + ), + c.call( + prefix + "_add", + c.getLocal("pAcc"), + c.getLocal("it2"), + c.getLocal("pAcc") + ), + c.setLocal("it1", c.i32_add(c.getLocal("it1"), c.i32_const(n8g))), + c.setLocal("it2", c.i32_add(c.getLocal("it2"), c.i32_const(n8g))), + c.br(0) + )), + + c.call( + fnName + "_reduceTable", + c.getLocal("pTable"), + c.i32_sub( + c.getLocal("p"), + c.i32_const(1) + ) + ), + + c.setLocal("p", c.i32_sub(c.getLocal("p"), c.i32_const(1))), + c.block(c.loop( + c.br_if(1, c.i32_eqz(c.getLocal("p"))), + c.call(prefix + "_double", c.getLocal("pAcc"), c.getLocal("pAcc")), + c.setLocal("p", c.i32_sub(c.getLocal("p"), c.i32_const(1))), + c.br(0) + )), + + c.call(prefix + "_add", c.getLocal("pTable"), c.getLocal("pAcc"), c.getLocal("pTable")) + ); + } + + buildGetChunk(); + buildReduceTable(); + buildMutiexpChunk(); + buildMultiexp(); + + module.exportFunction(fnName); + module.exportFunction(fnName +"_chunk"); + + + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + const buildTimesScalarNAF$1 = build_timesscalarnaf$1; + //const buildTimesScalar = require("./build_timesscalar"); + const buildBatchConvertion$4 = build_batchconvertion$1; + const buildMultiexp$3 = build_multiexp$1; + + var build_curve_jacobian_a0$1 = function buildCurve(module, prefix, prefixField, pB) { + + + const n64 = module.modules[prefixField].n64; + const n8 = n64*8; + + if (module.modules[prefix]) return prefix; // already builded + module.modules[prefix] = { + n64: n64*3 + }; + + function buildIsZero() { + const f = module.addFunction(prefix + "_isZero"); + f.addParam("p1", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + f.addCode(c.call( + prefixField + "_isZero", + c.i32_add( + c.getLocal("p1"), + c.i32_const(n8*2) + ) + )); + } + function buildIsZeroAffine() { + const f = module.addFunction(prefix + "_isZeroAffine"); + f.addParam("p1", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.i32_and( + c.call( + prefixField + "_isZero", + c.getLocal("p1") + ), + c.call( + prefixField + "_isZero", + c.i32_add( + c.getLocal("p1"), + c.i32_const(n8) + ) + ) + ) + ); + } + + function buildCopy() { + const f = module.addFunction(prefix + "_copy"); + f.addParam("ps", "i32"); + f.addParam("pd", "i32"); + + const c = f.getCodeBuilder(); + + for (let i=0; i. + */ + + const { isOdd: isOdd$8, modInv: modInv$5, modPow: modPow$3 } = bigint$1; + const utils$9 = utils$c; + + var build_fft$1 = function buildFFT(module, prefix, gPrefix, fPrefix, opGtimesF) { + + const n64f = module.modules[fPrefix].n64; + const n8f = n64f*8; + + const n64g = module.modules[gPrefix].n64; + const n8g = n64g*8; + + const q = module.modules[fPrefix].q; + + let rem = q - 1n; + let maxBits = 0; + while (!isOdd$8(rem)) { + maxBits ++; + rem = rem >> 1n; + } + + let nr = 2n; + + while ( modPow$3(nr, q >> 1n, q) === 1n ) nr = nr + 1n; + + // console.log(nr); + + const w = new Array(maxBits+1); + w[maxBits] = modPow$3(nr, rem, q); + + let n=maxBits-1; + while (n>=0) { + w[n] = modPow$3(w[n+1], 2n, q); + n--; + } + + const bytes = []; + const R = (1n << BigInt(n8f*8)) % q; + + for (let i=0; i> i); + } + } + return r; + } + + const rtable = Array(256); + for (let i=0; i<256; i++) { + rtable[i] = rev(i); + } + + const REVTABLE = module.alloc(rtable); + + + function buildLog2() { + const f = module.addFunction(prefix+"__log2"); + f.addParam("n", "i32"); + f.setReturnType("i32"); + f.addLocal("bits", "i32"); + f.addLocal("aux", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.setLocal( + "aux", + c.i32_shr_u( + c.getLocal("n"), + c.i32_const(1) + ) + ) + ); + f.addCode(c.setLocal("bits", c.i32_const(0))); + + f.addCode(c.block(c.loop( + c.br_if( + 1, + c.i32_eqz(c.getLocal("aux")) + ), + + c.setLocal( + "aux", + c.i32_shr_u( + c.getLocal("aux"), + c.i32_const(1) + ) + ), + + c.setLocal( + "bits", + c.i32_add( + c.getLocal("bits"), + c.i32_const(1) + ) + ), + + c.br(0) + ))); + + f.addCode(c.if( + c.i32_ne( + c.getLocal("n"), + c.i32_shl( + c.i32_const(1), + c.getLocal("bits") + ) + ), + c.unreachable() + )); + + f.addCode(c.if( + c.i32_gt_u( + c.getLocal("bits"), + c.i32_const(maxBits) + ), + c.unreachable() + )); + + f.addCode(c.getLocal("bits")); + } + + function buildFFT() { + const f = module.addFunction(prefix+"_fft"); + f.addParam("px", "i32"); + f.addParam("n", "i32"); + + f.addLocal("bits", "i32"); + + const c = f.getCodeBuilder(); + + const One = c.i32_const(module.alloc(n8f)); + + f.addCode( + c.setLocal( + "bits", + c.call( + prefix + "__log2", + c.getLocal("n") + ) + ), + c.call(fPrefix + "_one", One), + c.call( + prefix+"_rawfft", + c.getLocal("px"), + c.getLocal("bits"), + c.i32_const(0), + One + ) + ); + + } + + function buildIFFT() { + const f = module.addFunction(prefix+"_ifft"); + f.addParam("px", "i32"); + f.addParam("n", "i32"); + f.addLocal("bits", "i32"); + f.addLocal("pInv2", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.setLocal( + "bits", + c.call( + prefix + "__log2", + c.getLocal("n") + ) + ), + c.setLocal( + "pInv2", + c.i32_add( + c.i32_const(INV2), + c.i32_mul( + c.getLocal("bits"), + c.i32_const(n8f) + ) + ) + ), + + c.call( + prefix+"_rawfft", + c.getLocal("px"), + c.getLocal("bits"), + c.i32_const(1), + c.getLocal("pInv2") + ), + ); + } + + function buildRawFFT() { + const f = module.addFunction(prefix+"_rawfft"); + f.addParam("px", "i32"); + f.addParam("bits", "i32"); // 2 power + f.addParam("reverse", "i32"); + f.addParam("mulFactor", "i32"); + + f.addLocal("s", "i32"); + f.addLocal("k", "i32"); + f.addLocal("j", "i32"); + f.addLocal("m", "i32"); + f.addLocal("mdiv2", "i32"); + f.addLocal("n", "i32"); + f.addLocal("pwm", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + + const c = f.getCodeBuilder(); + + const W = c.i32_const(module.alloc(n8f)); + const T = c.i32_const(module.alloc(n8g)); + const U = c.i32_const(module.alloc(n8g)); + + f.addCode( + c.call(prefix + "__reversePermutation", c.getLocal("px"), c.getLocal("bits")), + c.setLocal("n", c.i32_shl(c.i32_const(1), c.getLocal("bits"))), + c.setLocal("s", c.i32_const(1)), + c.block(c.loop( + c.br_if( + 1, + c.i32_gt_u( + c.getLocal("s"), + c.getLocal("bits") + ) + ), + c.setLocal("m", c.i32_shl(c.i32_const(1), c.getLocal("s"))), + c.setLocal("pwm", + c.i32_add( + c.i32_const(ROOTs), + c.i32_mul( + c.getLocal("s"), + c.i32_const(n8f) + ) + ) + ), + c.setLocal("k", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_ge_u( + c.getLocal("k"), + c.getLocal("n") + ) + ), + + c.call(fPrefix + "_one", W), + + c.setLocal("mdiv2", c.i32_shr_u(c.getLocal("m"), c.i32_const(1)) ), + c.setLocal("j", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_ge_u( + c.getLocal("j"), + c.getLocal("mdiv2") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.i32_add( + c.getLocal("k"), + c.getLocal("j") + ), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("idx1"), + c.i32_mul( + c.getLocal("mdiv2"), + c.i32_const(n8g) + ) + ) + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + W, + T + ), + + c.call( + gPrefix + "_copy", + c.getLocal("idx1"), + U + ), + + c.call( + gPrefix + "_add", + U, + T, + c.getLocal("idx1"), + ), + + c.call( + gPrefix + "_sub", + U, + T, + c.getLocal("idx2"), + ), + + c.call( + fPrefix + "_mul", + W, + c.getLocal("pwm"), + W, + ), + + c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )), + + c.setLocal("k", c.i32_add(c.getLocal("k"), c.getLocal("m"))), + c.br(0) + )), + + c.setLocal("s", c.i32_add(c.getLocal("s"), c.i32_const(1))), + c.br(0) + )), + c.call( + prefix + "__fftFinal", + c.getLocal("px"), + c.getLocal("bits"), + c.getLocal("reverse"), + c.getLocal("mulFactor") + ) + ); + } + + + function buildFinalInverse() { + const f = module.addFunction(prefix+"__fftFinal"); + f.addParam("px", "i32"); + f.addParam("bits", "i32"); + f.addParam("reverse", "i32"); + f.addParam("mulFactor", "i32"); + f.addLocal("n", "i32"); + f.addLocal("ndiv2", "i32"); + f.addLocal("pInv2", "i32"); + f.addLocal("i", "i32"); + f.addLocal("mask", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + + const c = f.getCodeBuilder(); + + const T = c.i32_const(module.alloc(n8g)); + + f.addCode( + c.if( + c.i32_and( + c.i32_eqz(c.getLocal("reverse")), + c.call(fPrefix + "_isOne", c.getLocal("mulFactor")) + ), + c.ret([]) + ), + c.setLocal("n", c.i32_shl( c.i32_const(1), c.getLocal("bits"))), + + c.setLocal("mask", c.i32_sub( c.getLocal("n") , c.i32_const(1))), + c.setLocal("i", c.i32_const(1)), + c.setLocal( + "ndiv2", + c.i32_shr_u( + c.getLocal("n"), + c.i32_const(1) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_ge_u( + c.getLocal("i"), + c.getLocal("ndiv2") + ) + ), + + c.setLocal("idx1", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal("idx2", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.i32_sub( + c.getLocal("n"), + c.getLocal("i") + ), + c.i32_const(n8g) + ) + ) + ), + + c.if( + c.getLocal("reverse"), + c.if( + c.call(fPrefix + "_isOne", c.getLocal("mulFactor")), + [ + ...c.call(gPrefix + "_copy", c.getLocal("idx1"), T), + ...c.call(gPrefix + "_copy", c.getLocal("idx2") , c.getLocal("idx1") ), + ...c.call(gPrefix + "_copy", T , c.getLocal("idx2")), + ], + [ + ...c.call(gPrefix + "_copy", c.getLocal("idx1"), T), + ...c.call(opGtimesF , c.getLocal("idx2") , c.getLocal("mulFactor"), c.getLocal("idx1") ), + ...c.call(opGtimesF , T , c.getLocal("mulFactor"), c.getLocal("idx2")), + ] + ), + c.if( + c.call(fPrefix + "_isOne", c.getLocal("mulFactor")), + [ + // Do nothing (It should not be here) + ], + [ + ...c.call(opGtimesF , c.getLocal("idx1") , c.getLocal("mulFactor"), c.getLocal("idx1") ), + ...c.call(opGtimesF , c.getLocal("idx2") , c.getLocal("mulFactor"), c.getLocal("idx2")), + ] + ) + ), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + + c.br(0) + )), + + c.if( + c.call(fPrefix + "_isOne", c.getLocal("mulFactor")), + [ + // Do nothing (It should not be here) + ], + [ + ...c.call(opGtimesF, c.getLocal("px") , c.getLocal("mulFactor"), c.getLocal("px")), + ...c.setLocal("idx2", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.getLocal("ndiv2"), + c.i32_const(n8g) + ) + ) + ), + ...c.call(opGtimesF, c.getLocal("idx2"),c.getLocal("mulFactor"), c.getLocal("idx2")) + ] + ) + ); + } + + function buildReversePermutation() { + const f = module.addFunction(prefix+"__reversePermutation"); + f.addParam("px", "i32"); + f.addParam("bits", "i32"); + f.addLocal("n", "i32"); + f.addLocal("i", "i32"); + f.addLocal("ri", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + + const c = f.getCodeBuilder(); + + const T = c.i32_const(module.alloc(n8g)); + + f.addCode( + c.setLocal("n", c.i32_shl( c.i32_const(1), c.getLocal("bits"))), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("n") + ) + ), + + c.setLocal("idx1", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal("ri", c.call(prefix + "__rev", c.getLocal("i"), c.getLocal("bits"))), + + c.setLocal("idx2", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.getLocal("ri"), + c.i32_const(n8g) + ) + ) + ), + + c.if( + c.i32_lt_u( + c.getLocal("i"), + c.getLocal("ri") + ), + [ + ...c.call(gPrefix + "_copy", c.getLocal("idx1"), T), + ...c.call(gPrefix + "_copy", c.getLocal("idx2") , c.getLocal("idx1")), + ...c.call(gPrefix + "_copy", T , c.getLocal("idx2")) + ] + ), + + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + + c.br(0) + )) + ); + } + + function buildRev() { + const f = module.addFunction(prefix+"__rev"); + f.addParam("x", "i32"); + f.addParam("bits", "i32"); + f.setReturnType("i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.i32_rotl( + c.i32_add( + c.i32_add( + c.i32_shl( + c.i32_load8_u( + c.i32_and( + c.getLocal("x"), + c.i32_const(0xFF) + ), + REVTABLE, + 0 + ), + c.i32_const(24) + ), + c.i32_shl( + c.i32_load8_u( + c.i32_and( + c.i32_shr_u( + c.getLocal("x"), + c.i32_const(8) + ), + c.i32_const(0xFF) + ), + REVTABLE, + 0 + ), + c.i32_const(16) + ), + ), + c.i32_add( + c.i32_shl( + c.i32_load8_u( + c.i32_and( + c.i32_shr_u( + c.getLocal("x"), + c.i32_const(16) + ), + c.i32_const(0xFF) + ), + REVTABLE, + 0 + ), + c.i32_const(8) + ), + c.i32_load8_u( + c.i32_and( + c.i32_shr_u( + c.getLocal("x"), + c.i32_const(24) + ), + c.i32_const(0xFF) + ), + REVTABLE, + 0 + ), + ) + ), + c.getLocal("bits") + ) + ); + } + + + function buildFFTJoin() { + const f = module.addFunction(prefix+"_fftJoin"); + f.addParam("pBuff1", "i32"); + f.addParam("pBuff2", "i32"); + f.addParam("n", "i32"); + f.addParam("first", "i32"); + f.addParam("inc", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + const W = c.i32_const(module.alloc(n8f)); + const T = c.i32_const(module.alloc(n8g)); + const U = c.i32_const(module.alloc(n8g)); + + f.addCode( + c.call( fPrefix + "_copy", c.getLocal("first"), W), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("n") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("pBuff1"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("pBuff2"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + W, + T + ), + + c.call( + gPrefix + "_copy", + c.getLocal("idx1"), + U + ), + + c.call( + gPrefix + "_add", + U, + T, + c.getLocal("idx1"), + ), + + c.call( + gPrefix + "_sub", + U, + T, + c.getLocal("idx2"), + ), + + c.call( + fPrefix + "_mul", + W, + c.getLocal("inc"), + W, + ), + + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + + function buildFFTJoinExt() { + const f = module.addFunction(prefix+"_fftJoinExt"); + f.addParam("pBuff1", "i32"); + f.addParam("pBuff2", "i32"); + f.addParam("n", "i32"); + f.addParam("first", "i32"); + f.addParam("inc", "i32"); + f.addParam("totalBits", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + f.addLocal("i", "i32"); + f.addLocal("pShiftToM", "i32"); + + const c = f.getCodeBuilder(); + + const W = c.i32_const(module.alloc(n8f)); + const U = c.i32_const(module.alloc(n8g)); + + f.addCode( + + c.setLocal("pShiftToM", + c.i32_add( + c.i32_const(SHIFT_TO_M), + c.i32_mul( + c.getLocal("totalBits"), + c.i32_const(n8f) + ) + ) + ), + + + c.call( fPrefix + "_copy", c.getLocal("first"), W), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("n") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("pBuff1"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("pBuff2"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.call( + gPrefix + "_add", + c.getLocal("idx1"), + c.getLocal("idx2"), + U + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + c.getLocal("pShiftToM"), + c.getLocal("idx2") + ), + + c.call( + gPrefix + "_add", + c.getLocal("idx1"), + c.getLocal("idx2"), + c.getLocal("idx2") + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + W, + c.getLocal("idx2"), + ), + + c.call( + gPrefix + "_copy", + U, + c.getLocal("idx1") + ), + + c.call( + fPrefix + "_mul", + W, + c.getLocal("inc"), + W + ), + + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + function buildFFTJoinExtInv() { + const f = module.addFunction(prefix+"_fftJoinExtInv"); + f.addParam("pBuff1", "i32"); + f.addParam("pBuff2", "i32"); + f.addParam("n", "i32"); + f.addParam("first", "i32"); + f.addParam("inc", "i32"); + f.addParam("totalBits", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + f.addLocal("i", "i32"); + f.addLocal("pShiftToM", "i32"); + f.addLocal("pSConst", "i32"); + + const c = f.getCodeBuilder(); + + const W = c.i32_const(module.alloc(n8f)); + const U = c.i32_const(module.alloc(n8g)); + + f.addCode( + + c.setLocal("pShiftToM", + c.i32_add( + c.i32_const(SHIFT_TO_M), + c.i32_mul( + c.getLocal("totalBits"), + c.i32_const(n8f) + ) + ) + ), + c.setLocal("pSConst", + c.i32_add( + c.i32_const(SCONST), + c.i32_mul( + c.getLocal("totalBits"), + c.i32_const(n8f) + ) + ) + ), + + + c.call( fPrefix + "_copy", c.getLocal("first"), W), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("n") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("pBuff1"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("pBuff2"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + W, + U + ), + + c.call( + gPrefix + "_sub", + c.getLocal("idx1"), + U, + c.getLocal("idx2"), + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + c.getLocal("pSConst"), + c.getLocal("idx2") + ), + + c.call( + opGtimesF, + c.getLocal("idx1"), + c.getLocal("pShiftToM"), + c.getLocal("idx1") + ), + + c.call( + gPrefix + "_sub", + U, + c.getLocal("idx1"), + c.getLocal("idx1") + ), + + c.call( + opGtimesF, + c.getLocal("idx1"), + c.getLocal("pSConst"), + c.getLocal("idx1") + ), + + c.call( + fPrefix + "_mul", + W, + c.getLocal("inc"), + W + ), + + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + + + function buildPrepareLagrangeEvaluation() { + const f = module.addFunction(prefix+"_prepareLagrangeEvaluation"); + f.addParam("pBuff1", "i32"); + f.addParam("pBuff2", "i32"); + f.addParam("n", "i32"); + f.addParam("first", "i32"); + f.addParam("inc", "i32"); + f.addParam("totalBits", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + f.addLocal("i", "i32"); + f.addLocal("pShiftToM", "i32"); + f.addLocal("pSConst", "i32"); + + const c = f.getCodeBuilder(); + + const W = c.i32_const(module.alloc(n8f)); + const U = c.i32_const(module.alloc(n8g)); + + f.addCode( + + c.setLocal("pShiftToM", + c.i32_add( + c.i32_const(SHIFT_TO_M), + c.i32_mul( + c.getLocal("totalBits"), + c.i32_const(n8f) + ) + ) + ), + c.setLocal("pSConst", + c.i32_add( + c.i32_const(SCONST), + c.i32_mul( + c.getLocal("totalBits"), + c.i32_const(n8f) + ) + ) + ), + + + c.call( fPrefix + "_copy", c.getLocal("first"), W), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("n") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("pBuff1"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("pBuff2"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + + c.call( + opGtimesF, + c.getLocal("idx1"), + c.getLocal("pShiftToM"), + U + ), + + c.call( + gPrefix + "_sub", + c.getLocal("idx2"), + U, + U + ), + + c.call( + gPrefix + "_sub", + c.getLocal("idx1"), + c.getLocal("idx2"), + c.getLocal("idx2"), + ), + + c.call( + opGtimesF, + U, + c.getLocal("pSConst"), + c.getLocal("idx1"), + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + W, + c.getLocal("idx2"), + ), + + c.call( + fPrefix + "_mul", + W, + c.getLocal("inc"), + W + ), + + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + function buildFFTMix() { + const f = module.addFunction(prefix+"_fftMix"); + f.addParam("pBuff", "i32"); + f.addParam("n", "i32"); + f.addParam("exp", "i32"); + f.addLocal("nGroups", "i32"); + f.addLocal("nPerGroup", "i32"); + f.addLocal("nPerGroupDiv2", "i32"); + f.addLocal("pairOffset", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + f.addLocal("i", "i32"); + f.addLocal("j", "i32"); + f.addLocal("pwm", "i32"); + + const c = f.getCodeBuilder(); + + const W = c.i32_const(module.alloc(n8f)); + const T = c.i32_const(module.alloc(n8g)); + const U = c.i32_const(module.alloc(n8g)); + + f.addCode( + c.setLocal("nPerGroup", c.i32_shl(c.i32_const(1), c.getLocal("exp"))), + c.setLocal("nPerGroupDiv2", c.i32_shr_u(c.getLocal("nPerGroup"), c.i32_const(1))), + c.setLocal("nGroups", c.i32_shr_u(c.getLocal("n"), c.getLocal("exp"))), + c.setLocal("pairOffset", c.i32_mul(c.getLocal("nPerGroupDiv2"), c.i32_const(n8g))), + c.setLocal("pwm", + c.i32_add( + c.i32_const(ROOTs), + c.i32_mul( + c.getLocal("exp"), + c.i32_const(n8f) + ) + ) + ), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("nGroups") + ) + ), + c.call( fPrefix + "_one", W), + c.setLocal("j", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("j"), + c.getLocal("nPerGroupDiv2") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("pBuff"), + c.i32_mul( + c.i32_add( + c.i32_mul( + c.getLocal("i"), + c.getLocal("nPerGroup") + ), + c.getLocal("j") + ), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("idx1"), + c.getLocal("pairOffset") + ) + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + W, + T + ), + + c.call( + gPrefix + "_copy", + c.getLocal("idx1"), + U + ), + + c.call( + gPrefix + "_add", + U, + T, + c.getLocal("idx1"), + ), + + c.call( + gPrefix + "_sub", + U, + T, + c.getLocal("idx2"), + ), + + c.call( + fPrefix + "_mul", + W, + c.getLocal("pwm"), + W, + ), + c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + + // Reverse all and multiply by factor + function buildFFTFinal() { + const f = module.addFunction(prefix+"_fftFinal"); + f.addParam("pBuff", "i32"); + f.addParam("n", "i32"); + f.addParam("factor", "i32"); + f.addLocal("idx1", "i32"); + f.addLocal("idx2", "i32"); + f.addLocal("i", "i32"); + f.addLocal("ndiv2", "i32"); + + const c = f.getCodeBuilder(); + + const T = c.i32_const(module.alloc(n8g)); + + f.addCode( + c.setLocal("ndiv2", c.i32_shr_u(c.getLocal("n"), c.i32_const(1))), + c.if( + c.i32_and( + c.getLocal("n"), + c.i32_const(1) + ), + c.call( + opGtimesF, + c.i32_add( + c.getLocal("pBuff"), + c.i32_mul( + c.getLocal("ndiv2"), + c.i32_const(n8g) + ) + ), + c.getLocal("factor"), + c.i32_add( + c.getLocal("pBuff"), + c.i32_mul( + c.getLocal("ndiv2"), + c.i32_const(n8g) + ) + ), + ), + ), + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_ge_u( + c.getLocal("i"), + c.getLocal("ndiv2") + ) + ), + + c.setLocal( + "idx1", + c.i32_add( + c.getLocal("pBuff"), + c.i32_mul( + c.getLocal("i"), + c.i32_const(n8g) + ) + ) + ), + + c.setLocal( + "idx2", + c.i32_add( + c.getLocal("pBuff"), + c.i32_mul( + c.i32_sub( + c.i32_sub( + c.getLocal("n"), + c.i32_const(1) + ), + c.getLocal("i") + ), + c.i32_const(n8g) + ) + ) + ), + + c.call( + opGtimesF, + c.getLocal("idx2"), + c.getLocal("factor"), + T + ), + + c.call( + opGtimesF, + c.getLocal("idx1"), + c.getLocal("factor"), + c.getLocal("idx2"), + ), + + c.call( + gPrefix + "_copy", + T, + c.getLocal("idx1"), + ), + + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + buildRev(); + buildReversePermutation(); + buildFinalInverse(); + buildRawFFT(); + buildLog2(); + buildFFT(); + buildIFFT(); + buildFFTJoin(); + buildFFTJoinExt(); + buildFFTJoinExtInv(); + buildFFTMix(); + buildFFTFinal(); + buildPrepareLagrangeEvaluation(); + + module.exportFunction(prefix+"_fft"); + module.exportFunction(prefix+"_ifft"); + module.exportFunction(prefix+"_rawfft"); + module.exportFunction(prefix+"_fftJoin"); + module.exportFunction(prefix+"_fftJoinExt"); + module.exportFunction(prefix+"_fftJoinExtInv"); + module.exportFunction(prefix+"_fftMix"); + module.exportFunction(prefix+"_fftFinal"); + module.exportFunction(prefix+"_prepareLagrangeEvaluation"); + + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + var build_pol$1 = function buildPol(module, prefix, prefixField) { + + const n64 = module.modules[prefixField].n64; + const n8 = n64*8; + + + function buildZero() { + const f = module.addFunction(prefix+"_zero"); + f.addParam("px", "i32"); + f.addParam("n", "i32"); + f.addLocal("lastp", "i32"); + f.addLocal("p", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.setLocal("p", c.getLocal("px")), + c.setLocal( + "lastp", + c.i32_add( + c.getLocal("px"), + c.i32_mul( + c.getLocal("n"), + c.i32_const(n8) + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("p"), + c.getLocal("lastp") + ) + ), + c.call(prefixField + "_zero", c.getLocal("p")), + c.setLocal("p", c.i32_add(c.getLocal("p"), c.i32_const(n8))), + c.br(0) + )) + ); + } + + function buildConstructLC() { + const f = module.addFunction(prefix+"_constructLC"); + f.addParam("ppolynomials", "i32"); + f.addParam("psignals", "i32"); + f.addParam("nSignals", "i32"); + f.addParam("pres", "i32"); + f.addLocal("i", "i32"); + f.addLocal("j", "i32"); + f.addLocal("pp", "i32"); + f.addLocal("ps", "i32"); + f.addLocal("pd", "i32"); + f.addLocal("ncoefs", "i32"); + + const c = f.getCodeBuilder(); + + const aux = c.i32_const(module.alloc(n8)); + + f.addCode( + c.setLocal("i", c.i32_const(0)), + c.setLocal("pp", c.getLocal("ppolynomials")), + c.setLocal("ps", c.getLocal("psignals")), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("i"), + c.getLocal("nSignals") + ) + ), + + c.setLocal("ncoefs", c.i32_load(c.getLocal("pp"))), + c.setLocal("pp", c.i32_add(c.getLocal("pp"), c.i32_const(4))), + + c.setLocal("j", c.i32_const(0)), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("j"), + c.getLocal("ncoefs") + ) + ), + + c.setLocal( + "pd", + c.i32_add( + c.getLocal("pres"), + c.i32_mul( + c.i32_load(c.getLocal("pp")), + c.i32_const(n8) + ) + ) + ), + + c.setLocal("pp", c.i32_add(c.getLocal("pp"), c.i32_const(4))), + + + c.call( + prefixField + "_mul", + c.getLocal("ps"), + c.getLocal("pp"), + aux + ), + + c.call( + prefixField + "_add", + aux, + c.getLocal("pd"), + c.getLocal("pd") + ), + + c.setLocal("pp", c.i32_add(c.getLocal("pp"), c.i32_const(n8))), + c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), + c.br(0) + )), + + c.setLocal("ps", c.i32_add(c.getLocal("ps"), c.i32_const(n8))), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + + } + + buildZero(); + buildConstructLC(); + + + module.exportFunction(prefix + "_zero"); + module.exportFunction(prefix + "_constructLC"); + + return prefix; + + + + + }; + + var build_qap$1 = function buildQAP(module, prefix, prefixField) { + + const n64 = module.modules[prefixField].n64; + const n8 = n64*8; + + + function buildBuildABC() { + const f = module.addFunction(prefix+"_buildABC"); + f.addParam("pCoefs", "i32"); + f.addParam("nCoefs", "i32"); + f.addParam("pWitness", "i32"); + f.addParam("pA", "i32"); + f.addParam("pB", "i32"); + f.addParam("pC", "i32"); + f.addParam("offsetOut", "i32"); + f.addParam("nOut", "i32"); + f.addParam("offsetWitness", "i32"); + f.addParam("nWitness", "i32"); + f.addLocal("it", "i32"); + f.addLocal("ita", "i32"); + f.addLocal("itb", "i32"); + f.addLocal("last", "i32"); + f.addLocal("m", "i32"); + f.addLocal("c", "i32"); + f.addLocal("s", "i32"); + f.addLocal("pOut", "i32"); + + const c = f.getCodeBuilder(); + + const aux = c.i32_const(module.alloc(n8)); + + f.addCode( + + // Set output a and b to 0 + c.setLocal("ita", c.getLocal("pA")), + c.setLocal("itb", c.getLocal("pB")), + c.setLocal( + "last", + c.i32_add( + c.getLocal("pA"), + c.i32_mul( + c.getLocal("nOut"), + c.i32_const(n8) + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("ita"), + c.getLocal("last") + ) + ), + c.call(prefixField + "_zero", c.getLocal("ita")), + c.call(prefixField + "_zero", c.getLocal("itb")), + c.setLocal("ita", c.i32_add(c.getLocal("ita"), c.i32_const(n8))), + c.setLocal("itb", c.i32_add(c.getLocal("itb"), c.i32_const(n8))), + c.br(0) + )), + + + c.setLocal("it", c.getLocal("pCoefs")), + c.setLocal( + "last", + c.i32_add( + c.getLocal("pCoefs"), + c.i32_mul( + c.getLocal("nCoefs"), + c.i32_const(n8+12) + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("it"), + c.getLocal("last") + ) + ), + c.setLocal( + "s", + c.i32_load(c.getLocal("it"), 8) + ), + c.if( + c.i32_or( + c.i32_lt_u( + c.getLocal("s"), + c.getLocal("offsetWitness"), + ), + c.i32_ge_u( + c.getLocal("s"), + c.i32_add( + c.getLocal("offsetWitness"), + c.getLocal("nWitness"), + ) + ) + ), + [ + ...c.setLocal("it", c.i32_add(c.getLocal("it"), c.i32_const(n8+12))), + ...c.br(1) + ] + ), + + c.setLocal( + "m", + c.i32_load(c.getLocal("it")) + ), + c.if( + c.i32_eq(c.getLocal("m"), c.i32_const(0)), + c.setLocal("pOut", c.getLocal("pA")), + c.if( + c.i32_eq(c.getLocal("m"), c.i32_const(1)), + c.setLocal("pOut", c.getLocal("pB")), + [ + ...c.setLocal("it", c.i32_add(c.getLocal("it"), c.i32_const(n8+12))), + ...c.br(1) + ] + ) + ), + c.setLocal( + "c", + c.i32_load(c.getLocal("it"), 4) + ), + c.if( + c.i32_or( + c.i32_lt_u( + c.getLocal("c"), + c.getLocal("offsetOut"), + ), + c.i32_ge_u( + c.getLocal("c"), + c.i32_add( + c.getLocal("offsetOut"), + c.getLocal("nOut"), + ) + ) + ), + [ + ...c.setLocal("it", c.i32_add(c.getLocal("it"), c.i32_const(n8+12))), + ...c.br(1) + ] + ), + c.setLocal( + "pOut", + c.i32_add( + c.getLocal("pOut"), + c.i32_mul( + c.i32_sub( + c.getLocal("c"), + c.getLocal("offsetOut") + ), + c.i32_const(n8) + ) + ) + ), + c.call( + prefixField + "_mul", + c.i32_add( + c.getLocal("pWitness"), + c.i32_mul( + c.i32_sub(c.getLocal("s"), c.getLocal("offsetWitness")), + c.i32_const(n8) + ) + ), + c.i32_add( c.getLocal("it"), c.i32_const(12)), + aux + ), + c.call( + prefixField + "_add", + c.getLocal("pOut"), + aux, + c.getLocal("pOut"), + ), + c.setLocal("it", c.i32_add(c.getLocal("it"), c.i32_const(n8+12))), + c.br(0) + )), + + c.setLocal("ita", c.getLocal("pA")), + c.setLocal("itb", c.getLocal("pB")), + c.setLocal("it", c.getLocal("pC")), + c.setLocal( + "last", + c.i32_add( + c.getLocal("pA"), + c.i32_mul( + c.getLocal("nOut"), + c.i32_const(n8) + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("ita"), + c.getLocal("last") + ) + ), + c.call( + prefixField + "_mul", + c.getLocal("ita"), + c.getLocal("itb"), + c.getLocal("it") + ), + c.setLocal("ita", c.i32_add(c.getLocal("ita"), c.i32_const(n8))), + c.setLocal("itb", c.i32_add(c.getLocal("itb"), c.i32_const(n8))), + c.setLocal("it", c.i32_add(c.getLocal("it"), c.i32_const(n8))), + c.br(0) + )), + + ); + } + + function buildJoinABC() { + const f = module.addFunction(prefix+"_joinABC"); + f.addParam("pA", "i32"); + f.addParam("pB", "i32"); + f.addParam("pC", "i32"); + f.addParam("n", "i32"); + f.addParam("pP", "i32"); + f.addLocal("ita", "i32"); + f.addLocal("itb", "i32"); + f.addLocal("itc", "i32"); + f.addLocal("itp", "i32"); + f.addLocal("last", "i32"); + + const c = f.getCodeBuilder(); + + const aux = c.i32_const(module.alloc(n8)); + + f.addCode( + c.setLocal("ita", c.getLocal("pA")), + c.setLocal("itb", c.getLocal("pB")), + c.setLocal("itc", c.getLocal("pC")), + c.setLocal("itp", c.getLocal("pP")), + c.setLocal( + "last", + c.i32_add( + c.getLocal("pA"), + c.i32_mul( + c.getLocal("n"), + c.i32_const(n8) + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("ita"), + c.getLocal("last") + ) + ), + c.call( + prefixField + "_mul", + c.getLocal("ita"), + c.getLocal("itb"), + aux + ), + c.call( + prefixField + "_sub", + aux, + c.getLocal("itc"), + c.getLocal("itp"), + ), + c.setLocal("ita", c.i32_add(c.getLocal("ita"), c.i32_const(n8))), + c.setLocal("itb", c.i32_add(c.getLocal("itb"), c.i32_const(n8))), + c.setLocal("itc", c.i32_add(c.getLocal("itc"), c.i32_const(n8))), + c.setLocal("itp", c.i32_add(c.getLocal("itp"), c.i32_const(n8))), + c.br(0) + )) + ); + } + + function buildBatchAdd() { + const f = module.addFunction(prefix+"_batchAdd"); + f.addParam("pa", "i32"); + f.addParam("pb", "i32"); + f.addParam("n", "i32"); + f.addParam("pr", "i32"); + f.addLocal("ita", "i32"); + f.addLocal("itb", "i32"); + f.addLocal("itr", "i32"); + f.addLocal("last", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.setLocal("ita", c.getLocal("pa")), + c.setLocal("itb", c.getLocal("pb")), + c.setLocal("itr", c.getLocal("pr")), + c.setLocal( + "last", + c.i32_add( + c.getLocal("pa"), + c.i32_mul( + c.getLocal("n"), + c.i32_const(n8) + ) + ) + ), + c.block(c.loop( + c.br_if( + 1, + c.i32_eq( + c.getLocal("ita"), + c.getLocal("last") + ) + ), + c.call( + prefixField + "_add", + c.getLocal("ita"), + c.getLocal("itb"), + c.getLocal("itr"), + ), + c.setLocal("ita", c.i32_add(c.getLocal("ita"), c.i32_const(n8))), + c.setLocal("itb", c.i32_add(c.getLocal("itb"), c.i32_const(n8))), + c.setLocal("itr", c.i32_add(c.getLocal("itr"), c.i32_const(n8))), + c.br(0) + )) + ); + } + + buildBuildABC(); + buildJoinABC(); + buildBatchAdd(); + + module.exportFunction(prefix + "_buildABC"); + module.exportFunction(prefix + "_joinABC"); + module.exportFunction(prefix + "_batchAdd"); + + return prefix; + + }; + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmsnark (Web Assembly zkSnark Prover). + + wasmsnark is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmsnark is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmsnark. If not, see . + */ + + var build_applykey$1 = function buildApplyKey(module, fnName, gPrefix, frPrefix, sizeGIn, sizeGOut, sizeF, opGtimesF) { + + const f = module.addFunction(fnName); + f.addParam("pIn", "i32"); + f.addParam("n", "i32"); + f.addParam("pFirst", "i32"); + f.addParam("pInc", "i32"); + f.addParam("pOut", "i32"); + f.addLocal("pOldFree", "i32"); + f.addLocal("i", "i32"); + f.addLocal("pFrom", "i32"); + f.addLocal("pTo", "i32"); + + const c = f.getCodeBuilder(); + + const t = c.i32_const(module.alloc(sizeF)); + + f.addCode( + c.setLocal("pFrom", c.getLocal("pIn")), + c.setLocal("pTo", c.getLocal("pOut")), + ); + + // t = first + f.addCode( + c.call( + frPrefix + "_copy", + c.getLocal("pFirst"), + t + ) + ); + f.addCode( + c.setLocal("i", c.i32_const(0)), + c.block(c.loop( + c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("n") )), + + c.call( + opGtimesF, + c.getLocal("pFrom"), + t, + c.getLocal("pTo") + ), + c.setLocal("pFrom", c.i32_add(c.getLocal("pFrom"), c.i32_const(sizeGIn))), + c.setLocal("pTo", c.i32_add(c.getLocal("pTo"), c.i32_const(sizeGOut))), + + // t = t* inc + c.call( + frPrefix + "_mul", + t, + c.getLocal("pInc"), + t + ), + c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + + module.exportFunction(fnName); + + }; + + const utils$8 = utils$c; + + const buildF1m$4 =build_f1m$1; + const buildF1$3 =build_f1$1; + const buildF2m$3 =build_f2m$1; + const buildF3m$3 =build_f3m$1; + const buildCurve$3 =build_curve_jacobian_a0$1; + const buildFFT$5 = build_fft$1; + const buildPol$3 = build_pol$1; + const buildQAP$3 = build_qap$1; + const buildApplyKey$3 = build_applykey$1; + const { bitLength: bitLength$8, modInv: modInv$4, isOdd: isOdd$7, isNegative: isNegative$6 } = bigint$1; + + var build_bn128$1 = function buildBN128(module, _prefix) { + + const prefix = _prefix || "bn128"; + + if (module.modules[prefix]) return prefix; // already builded + + const q = 21888242871839275222246405745257275088696311157297823662689037894645226208583n; + const r = 21888242871839275222246405745257275088548364400416034343698204186575808495617n; + + + const n64 = Math.floor((bitLength$8(q - 1n) - 1)/64) +1; + const n8 = n64*8; + const frsize = n8; + const f1size = n8; + const f2size = f1size * 2; + const ftsize = f1size * 12; + + const pr = module.alloc(utils$8.bigInt2BytesLE( r, frsize )); + + const f1mPrefix = buildF1m$4(module, q, "f1m"); + buildF1$3(module, r, "fr", "frm"); + + const pG1b = module.alloc(utils$8.bigInt2BytesLE( toMontgomery(3n), f1size )); + const g1mPrefix = buildCurve$3(module, "g1m", "f1m", pG1b); + + buildFFT$5(module, "frm", "frm", "frm", "frm_mul"); + + buildPol$3(module, "pol", "frm"); + buildQAP$3(module, "qap", "frm"); + + const f2mPrefix = buildF2m$3(module, "f1m_neg", "f2m", "f1m"); + const pG2b = module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery(19485874751759354771024239261021720505790618469301721065564631296452457478373n), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(266929791119991161246907387137283842545076965332900288569378510910307636690n), f1size ) + ]); + const g2mPrefix = buildCurve$3(module, "g2m", "f2m", pG2b); + + + function buildGTimesFr(fnName, opMul) { + const f = module.addFunction(fnName); + f.addParam("pG", "i32"); + f.addParam("pFr", "i32"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + const AUX = c.i32_const(module.alloc(n8)); + + f.addCode( + c.call("frm_fromMontgomery", c.getLocal("pFr"), AUX), + c.call( + opMul, + c.getLocal("pG"), + AUX, + c.i32_const(n8), + c.getLocal("pr") + ) + ); + + module.exportFunction(fnName); + } + buildGTimesFr("g1m_timesFr", "g1m_timesScalar"); + buildFFT$5(module, "g1m", "g1m", "frm", "g1m_timesFr"); + + buildGTimesFr("g2m_timesFr", "g2m_timesScalar"); + buildFFT$5(module, "g2m", "g2m", "frm", "g2m_timesFr"); + + buildGTimesFr("g1m_timesFrAffine", "g1m_timesScalarAffine"); + buildGTimesFr("g2m_timesFrAffine", "g2m_timesScalarAffine"); + + buildApplyKey$3(module, "frm_batchApplyKey", "fmr", "frm", n8, n8, n8, "frm_mul"); + buildApplyKey$3(module, "g1m_batchApplyKey", "g1m", "frm", n8*3, n8*3, n8, "g1m_timesFr"); + buildApplyKey$3(module, "g1m_batchApplyKeyMixed", "g1m", "frm", n8*2, n8*3, n8, "g1m_timesFrAffine"); + buildApplyKey$3(module, "g2m_batchApplyKey", "g2m", "frm", n8*2*3, n8*3*2, n8, "g2m_timesFr"); + buildApplyKey$3(module, "g2m_batchApplyKeyMixed", "g2m", "frm", n8*2*2, n8*3*2, n8, "g2m_timesFrAffine"); + + function toMontgomery(a) { + return BigInt(a) * ( 1n << BigInt(f1size*8)) % q; + } + + const G1gen = [ + 1n, + 2n, + 1n + ]; + + const pG1gen = module.alloc( + [ + ...utils$8.bigInt2BytesLE( toMontgomery(G1gen[0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G1gen[1]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G1gen[2]), f1size ), + ] + ); + + const G1zero = [ + 0n, + 1n, + 0n + ]; + + const pG1zero = module.alloc( + [ + ...utils$8.bigInt2BytesLE( toMontgomery(G1zero[0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G1zero[1]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G1zero[2]), f1size ) + ] + ); + + const G2gen = [ + [ + 10857046999023057135944570762232829481370756359578518086990519993285655852781n, + 11559732032986387107991004021392285783925812861821192530917403151452391805634n, + ],[ + 8495653923123431417604973247489272438418190587263600148770280649306958101930n, + 4082367875863433681332203403145435568316851327593401208105741076214120093531n, + ],[ + 1n, + 0n, + ] + ]; + + const pG2gen = module.alloc( + [ + ...utils$8.bigInt2BytesLE( toMontgomery(G2gen[0][0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2gen[0][1]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2gen[1][0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2gen[1][1]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2gen[2][0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2gen[2][1]), f1size ), + ] + ); + + const G2zero = [ + [ + 0n, + 0n, + ],[ + 1n, + 0n, + ],[ + 0n, + 0n, + ] + ]; + + const pG2zero = module.alloc( + [ + ...utils$8.bigInt2BytesLE( toMontgomery(G2zero[0][0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2zero[0][1]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2zero[1][0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2zero[1][1]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2zero[2][0]), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(G2zero[2][1]), f1size ), + ] + ); + + const pOneT = module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery(1), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(0), f1size ), + ]); + + const pNonResidueF6 = module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery(9), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(1), f1size ), + ]); + + const pTwoInv = module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery( modInv$4(2n, q)), f1size ), + ...utils$8.bigInt2BytesLE( 0n, f1size ) + ]); + + const pAltBn128Twist = pNonResidueF6; + + const pTwistCoefB = module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery(19485874751759354771024239261021720505790618469301721065564631296452457478373n), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery(266929791119991161246907387137283842545076965332900288569378510910307636690n), f1size ), + ]); + + function build_mulNR6() { + const f = module.addFunction(prefix + "_mulNR6"); + f.addParam("x", "i32"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call( + f2mPrefix + "_mul", + c.i32_const(pNonResidueF6), + c.getLocal("x"), + c.getLocal("pr") + ) + ); + } + build_mulNR6(); + + const f6mPrefix = buildF3m$3(module, prefix+"_mulNR6", "f6m", "f2m"); + + function build_mulNR12() { + const f = module.addFunction(prefix + "_mulNR12"); + f.addParam("x", "i32"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call( + f2mPrefix + "_mul", + c.i32_const(pNonResidueF6), + c.i32_add(c.getLocal("x"), c.i32_const(n8*4)), + c.getLocal("pr") + ), + c.call( + f2mPrefix + "_copy", + c.getLocal("x"), + c.i32_add(c.getLocal("pr"), c.i32_const(n8*2)), + ), + c.call( + f2mPrefix + "_copy", + c.i32_add(c.getLocal("x"), c.i32_const(n8*2)), + c.i32_add(c.getLocal("pr"), c.i32_const(n8*4)), + ) + ); + } + build_mulNR12(); + + const ftmPrefix = buildF2m$3(module, prefix+"_mulNR12", "ftm", f6mPrefix); + + + const ateLoopCount = 29793968203157093288n; + const ateLoopBitBytes = bits(ateLoopCount); + const pAteLoopBitBytes = module.alloc(ateLoopBitBytes); + + const ateCoefSize = 3 * f2size; + const ateNDblCoefs = ateLoopBitBytes.length-1; + const ateNAddCoefs = ateLoopBitBytes.reduce((acc, b) => acc + ( b!=0 ? 1 : 0) ,0); + const ateNCoefs = ateNAddCoefs + ateNDblCoefs + 1; + const prePSize = 3*2*n8; + const preQSize = 3*n8*2 + ateNCoefs*ateCoefSize; + + + module.modules[prefix] = { + n64: n64, + pG1gen: pG1gen, + pG1zero: pG1zero, + pG1b: pG1b, + pG2gen: pG2gen, + pG2zero: pG2zero, + pG2b: pG2b, + pq: module.modules["f1m"].pq, + pr: pr, + pOneT: pOneT, + prePSize: prePSize, + preQSize: preQSize, + r: r.toString(), + q: q.toString() + }; + + // console.log("PrePSize: " +prePSize); + // console.log("PreQSize: " +preQSize); + + const finalExpZ = 4965661367192848881n; + + function naf(n) { + let E = n; + const res = []; + while (E > 0n) { + if (isOdd$7(E)) { + const z = 2 - Number(E % 4n); + res.push( z ); + E = E - BigInt(z); + } else { + res.push( 0 ); + } + E = E >> 1n; + } + return res; + } + + function bits(n) { + let E = n; + const res = []; + while (E > 0n) { + if (isOdd$7(E)) { + res.push( 1 ); + } else { + res.push( 0 ); + } + E = E >> 1n; + } + return res; + } + + function buildPrepareG1() { + const f = module.addFunction(prefix+ "_prepareG1"); + f.addParam("pP", "i32"); + f.addParam("ppreP", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call(g1mPrefix + "_normalize", c.getLocal("pP"), c.getLocal("ppreP")), // TODO Remove if already in affine + ); + } + + function buildPrepAddStep() { + const f = module.addFunction(prefix+ "_prepAddStep"); + f.addParam("pQ", "i32"); + f.addParam("pR", "i32"); + f.addParam("pCoef", "i32"); + + const c = f.getCodeBuilder(); + + const X2 = c.getLocal("pQ"); + const Y2 = c.i32_add(c.getLocal("pQ"), c.i32_const(f2size)); + + const X1 = c.getLocal("pR"); + const Y1 = c.i32_add(c.getLocal("pR"), c.i32_const(f2size)); + const Z1 = c.i32_add(c.getLocal("pR"), c.i32_const(2*f2size)); + + const ELL_0 = c.getLocal("pCoef"); + const ELL_VW = c.i32_add(c.getLocal("pCoef"), c.i32_const(f2size)); + const ELL_VV = c.i32_add(c.getLocal("pCoef"), c.i32_const(2*f2size)); + + const D = ELL_VW; + const E = c.i32_const(module.alloc(f2size)); + const F = c.i32_const(module.alloc(f2size)); + const G = c.i32_const(module.alloc(f2size)); + const H = c.i32_const(module.alloc(f2size)); + const I = c.i32_const(module.alloc(f2size)); + const J = c.i32_const(module.alloc(f2size)); + const AUX = c.i32_const(module.alloc(f2size)); + + f.addCode( + // D = X1 - X2*Z1 + c.call(f2mPrefix + "_mul", X2, Z1, D), + c.call(f2mPrefix + "_sub", X1, D, D), + + // E = Y1 - Y2*Z1 + c.call(f2mPrefix + "_mul", Y2, Z1, E), + c.call(f2mPrefix + "_sub", Y1, E, E), + + // F = D^2 + c.call(f2mPrefix + "_square", D, F), + + // G = E^2 + c.call(f2mPrefix + "_square", E, G), + + // H = D*F + c.call(f2mPrefix + "_mul", D, F, H), + + // I = X1 * F + c.call(f2mPrefix + "_mul", X1, F, I), + + // J = H + Z1*G - (I+I) + c.call(f2mPrefix + "_add", I, I, AUX), + c.call(f2mPrefix + "_mul", Z1, G, J), + c.call(f2mPrefix + "_add", H, J, J), + c.call(f2mPrefix + "_sub", J, AUX, J), + + + // X3 (X1) = D*J + c.call(f2mPrefix + "_mul", D, J, X1), + + // Y3 (Y1) = E*(I-J)-(H*Y1) + c.call(f2mPrefix + "_mul", H, Y1, Y1), + c.call(f2mPrefix + "_sub", I, J, AUX), + c.call(f2mPrefix + "_mul", E, AUX, AUX), + c.call(f2mPrefix + "_sub", AUX, Y1, Y1), + + // Z3 (Z1) = Z1*H + c.call(f2mPrefix + "_mul", Z1, H, Z1), + + // ell_0 = xi * (E * X2 - D * Y2) + c.call(f2mPrefix + "_mul", D, Y2, AUX), + c.call(f2mPrefix + "_mul", E, X2, ELL_0), + c.call(f2mPrefix + "_sub", ELL_0, AUX, ELL_0), + c.call(f2mPrefix + "_mul", ELL_0, c.i32_const(pAltBn128Twist), ELL_0), + + + // ell_VV = - E (later: * xP) + c.call(f2mPrefix + "_neg", E, ELL_VV), + + // ell_VW = D (later: * yP ) + // Already assigned + + ); + } + + + + function buildPrepDoubleStep() { + const f = module.addFunction(prefix+ "_prepDblStep"); + f.addParam("pR", "i32"); + f.addParam("pCoef", "i32"); + + const c = f.getCodeBuilder(); + + const X1 = c.getLocal("pR"); + const Y1 = c.i32_add(c.getLocal("pR"), c.i32_const(f2size)); + const Z1 = c.i32_add(c.getLocal("pR"), c.i32_const(2*f2size)); + + const ELL_0 = c.getLocal("pCoef"); + const ELL_VW = c.i32_add(c.getLocal("pCoef"), c.i32_const(f2size)); + const ELL_VV = c.i32_add(c.getLocal("pCoef"), c.i32_const(2*f2size)); + + const A = c.i32_const(module.alloc(f2size)); + const B = c.i32_const(module.alloc(f2size)); + const C = c.i32_const(module.alloc(f2size)); + const D = c.i32_const(module.alloc(f2size)); + const E = c.i32_const(module.alloc(f2size)); + const F = c.i32_const(module.alloc(f2size)); + const G = c.i32_const(module.alloc(f2size)); + const H = c.i32_const(module.alloc(f2size)); + const I = c.i32_const(module.alloc(f2size)); + const J = c.i32_const(module.alloc(f2size)); + const E2 = c.i32_const(module.alloc(f2size)); + const AUX = c.i32_const(module.alloc(f2size)); + + f.addCode( + + // A = X1 * Y1 / 2 + c.call(f2mPrefix + "_mul", Y1, c.i32_const(pTwoInv), A), + c.call(f2mPrefix + "_mul", X1, A, A), + + // B = Y1^2 + c.call(f2mPrefix + "_square", Y1, B), + + // C = Z1^2 + c.call(f2mPrefix + "_square", Z1, C), + + // D = 3 * C + c.call(f2mPrefix + "_add", C, C, D), + c.call(f2mPrefix + "_add", D, C, D), + + // E = twist_b * D + c.call(f2mPrefix + "_mul", c.i32_const(pTwistCoefB), D, E), + + // F = 3 * E + c.call(f2mPrefix + "_add", E, E, F), + c.call(f2mPrefix + "_add", E, F, F), + + // G = (B+F)/2 + c.call(f2mPrefix + "_add", B, F, G), + c.call(f2mPrefix + "_mul", G, c.i32_const(pTwoInv), G), + + // H = (Y1+Z1)^2-(B+C) + c.call(f2mPrefix + "_add", B, C, AUX), + c.call(f2mPrefix + "_add", Y1, Z1, H), + c.call(f2mPrefix + "_square", H, H), + c.call(f2mPrefix + "_sub", H, AUX, H), + + // I = E-B + c.call(f2mPrefix + "_sub", E, B, I), + + // J = X1^2 + c.call(f2mPrefix + "_square", X1, J), + + // E_squared = E^2 + c.call(f2mPrefix + "_square", E, E2), + + // X3 (X1) = A * (B-F) + c.call(f2mPrefix + "_sub", B, F, AUX), + c.call(f2mPrefix + "_mul", A, AUX, X1), + + // Y3 (Y1) = G^2 - 3*E^2 + c.call(f2mPrefix + "_add", E2, E2, AUX), + c.call(f2mPrefix + "_add", E2, AUX, AUX), + c.call(f2mPrefix + "_square", G, Y1), + c.call(f2mPrefix + "_sub", Y1, AUX, Y1), + + // Z3 (Z1) = B * H + c.call(f2mPrefix + "_mul", B, H, Z1), + + // ell_0 = xi * I + c.call(f2mPrefix + "_mul", c.i32_const(pAltBn128Twist), I, ELL_0), + + // ell_VW = - H (later: * yP) + c.call(f2mPrefix + "_neg", H, ELL_VW), + + // ell_VV = 3*J (later: * xP) + c.call(f2mPrefix + "_add", J, J, ELL_VV), + c.call(f2mPrefix + "_add", J, ELL_VV, ELL_VV), + + ); + } + + function buildMulByQ() { + const f = module.addFunction(prefix + "_mulByQ"); + f.addParam("p1", "i32"); + f.addParam("pr", "i32"); + + const c = f.getCodeBuilder(); + + const x = c.getLocal("p1"); + const y = c.i32_add(c.getLocal("p1"), c.i32_const(f2size)); + const z = c.i32_add(c.getLocal("p1"), c.i32_const(f2size*2)); + const x3 = c.getLocal("pr"); + const y3 = c.i32_add(c.getLocal("pr"), c.i32_const(f2size)); + const z3 = c.i32_add(c.getLocal("pr"), c.i32_const(f2size*2)); + + const MulByQX = c.i32_const(module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery("21575463638280843010398324269430826099269044274347216827212613867836435027261"), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery("10307601595873709700152284273816112264069230130616436755625194854815875713954"), f1size ), + ])); + + const MulByQY = c.i32_const(module.alloc([ + ...utils$8.bigInt2BytesLE( toMontgomery("2821565182194536844548159561693502659359617185244120367078079554186484126554"), f1size ), + ...utils$8.bigInt2BytesLE( toMontgomery("3505843767911556378687030309984248845540243509899259641013678093033130930403"), f1size ), + ])); + + f.addCode( + // The frobeniusMap(1) in this field, is the conjugate + c.call(f2mPrefix + "_conjugate", x, x3), + c.call(f2mPrefix + "_mul", MulByQX, x3, x3), + c.call(f2mPrefix + "_conjugate", y, y3), + c.call(f2mPrefix + "_mul", MulByQY, y3, y3), + c.call(f2mPrefix + "_conjugate", z, z3), + ); + } + + + function buildPrepareG2() { + buildMulByQ(); + const f = module.addFunction(prefix+ "_prepareG2"); + f.addParam("pQ", "i32"); + f.addParam("ppreQ", "i32"); + f.addLocal("pCoef", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + const QX = c.getLocal("pQ"); + + const pR = module.alloc(f2size*3); + const R = c.i32_const(pR); + const RX = c.i32_const(pR); + const RY = c.i32_const(pR+f2size); + const RZ = c.i32_const(pR+2*f2size); + + const cQX = c.i32_add( c.getLocal("ppreQ"), c.i32_const(0)); + const cQY = c.i32_add( c.getLocal("ppreQ"), c.i32_const(f2size)); + + const pQ1 = module.alloc(f2size*3); + const Q1 = c.i32_const(pQ1); + + const pQ2 = module.alloc(f2size*3); + const Q2 = c.i32_const(pQ2); + const Q2Y = c.i32_const(pQ2 + f2size); + + f.addCode( + c.call(g2mPrefix + "_normalize", QX, cQX), // TODO Remove if already in affine + c.call(f2mPrefix + "_copy", cQX, RX), + c.call(f2mPrefix + "_copy", cQY, RY), + c.call(f2mPrefix + "_one", RZ), + ); + + f.addCode( + c.setLocal("pCoef", c.i32_add( c.getLocal("ppreQ"), c.i32_const(f2size*3))), + c.setLocal("i", c.i32_const(ateLoopBitBytes.length-2)), + c.block(c.loop( + + c.call(prefix + "_prepDblStep", R, c.getLocal("pCoef")), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + c.if( + c.i32_load8_s(c.getLocal("i"), pAteLoopBitBytes), + [ + ...c.call(prefix + "_prepAddStep", cQX, R, c.getLocal("pCoef")), + ...c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + ] + ), + c.br_if(1, c.i32_eqz ( c.getLocal("i") )), + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + + f.addCode( + c.call(prefix + "_mulByQ", cQX, Q1), + c.call(prefix + "_mulByQ", Q1, Q2) + ); + + f.addCode( + c.call(f2mPrefix + "_neg", Q2Y, Q2Y), + + c.call(prefix + "_prepAddStep", Q1, R, c.getLocal("pCoef")), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + c.call(prefix + "_prepAddStep", Q2, R, c.getLocal("pCoef")), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + ); + } + + function buildMulBy024Old() { + const f = module.addFunction(prefix+ "__mulBy024Old"); + f.addParam("pEll0", "i32"); + f.addParam("pEllVW", "i32"); + f.addParam("pEllVV", "i32"); + f.addParam("pR", "i32"); // Result in F12 + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("pEll0"); + const x2 = c.getLocal("pEllVV"); + const x4 = c.getLocal("pEllVW"); + + const z0 = c.getLocal("pR"); + + const pAUX12 = module.alloc(ftsize); + const AUX12 = c.i32_const(pAUX12); + const AUX12_0 = c.i32_const(pAUX12); + const AUX12_2 = c.i32_const(pAUX12+f2size); + const AUX12_4 = c.i32_const(pAUX12+f2size*2); + const AUX12_6 = c.i32_const(pAUX12+f2size*3); + const AUX12_8 = c.i32_const(pAUX12+f2size*4); + const AUX12_10 = c.i32_const(pAUX12+f2size*5); + + f.addCode( + + c.call(f2mPrefix + "_copy", x0, AUX12_0), + c.call(f2mPrefix + "_zero", AUX12_2), + c.call(f2mPrefix + "_copy", x2, AUX12_4), + c.call(f2mPrefix + "_zero", AUX12_6), + c.call(f2mPrefix + "_copy", x4, AUX12_8), + c.call(f2mPrefix + "_zero", AUX12_10), + c.call(ftmPrefix + "_mul", AUX12, z0, z0), + ); + } + + function buildMulBy024() { + const f = module.addFunction(prefix+ "__mulBy024"); + f.addParam("pEll0", "i32"); + f.addParam("pEllVW", "i32"); + f.addParam("pEllVV", "i32"); + f.addParam("pR", "i32"); // Result in F12 + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("pEll0"); + const x2 = c.getLocal("pEllVV"); + const x4 = c.getLocal("pEllVW"); + + const z0 = c.getLocal("pR"); + const z1 = c.i32_add(c.getLocal("pR"), c.i32_const(2*n8)); + const z2 = c.i32_add(c.getLocal("pR"), c.i32_const(4*n8)); + const z3 = c.i32_add(c.getLocal("pR"), c.i32_const(6*n8)); + const z4 = c.i32_add(c.getLocal("pR"), c.i32_const(8*n8)); + const z5 = c.i32_add(c.getLocal("pR"), c.i32_const(10*n8)); + + const t0 = c.i32_const(module.alloc(f2size)); + const t1 = c.i32_const(module.alloc(f2size)); + const t2 = c.i32_const(module.alloc(f2size)); + const s0 = c.i32_const(module.alloc(f2size)); + const T3 = c.i32_const(module.alloc(f2size)); + const T4 = c.i32_const(module.alloc(f2size)); + const D0 = c.i32_const(module.alloc(f2size)); + const D2 = c.i32_const(module.alloc(f2size)); + const D4 = c.i32_const(module.alloc(f2size)); + const S1 = c.i32_const(module.alloc(f2size)); + const AUX = c.i32_const(module.alloc(f2size)); + + f.addCode( + + // D0 = z0 * x0; + c.call(f2mPrefix + "_mul", z0, x0, D0), + // D2 = z2 * x2; + c.call(f2mPrefix + "_mul", z2, x2, D2), + // D4 = z4 * x4; + c.call(f2mPrefix + "_mul", z4, x4, D4), + // t2 = z0 + z4; + c.call(f2mPrefix + "_add", z0, z4, t2), + // t1 = z0 + z2; + c.call(f2mPrefix + "_add", z0, z2, t1), + // s0 = z1 + z3 + z5; + c.call(f2mPrefix + "_add", z1, z3, s0), + c.call(f2mPrefix + "_add", s0, z5, s0), + + + // For z.a_.a_ = z0. + // S1 = z1 * x2; + c.call(f2mPrefix + "_mul", z1, x2, S1), + // T3 = S1 + D4; + c.call(f2mPrefix + "_add", S1, D4, T3), + // T4 = my_Fp6::non_residue * T3 + D0; + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), T3, T4), + c.call(f2mPrefix + "_add", T4, D0, z0), + // z0 = T4; + + // For z.a_.b_ = z1 + // T3 = z5 * x4; + c.call(f2mPrefix + "_mul", z5, x4, T3), + // S1 = S1 + T3; + c.call(f2mPrefix + "_add", S1, T3, S1), + // T3 = T3 + D2; + c.call(f2mPrefix + "_add", T3, D2, T3), + // T4 = my_Fp6::non_residue * T3; + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), T3, T4), + // T3 = z1 * x0; + c.call(f2mPrefix + "_mul", z1, x0, T3), + // S1 = S1 + T3; + c.call(f2mPrefix + "_add", S1, T3, S1), + // T4 = T4 + T3; + c.call(f2mPrefix + "_add", T4, T3, z1), + // z1 = T4; + + + + // For z.a_.c_ = z2 + // t0 = x0 + x2; + c.call(f2mPrefix + "_add", x0, x2, t0), + // T3 = t1 * t0 - D0 - D2; + c.call(f2mPrefix + "_mul", t1, t0, T3), + c.call(f2mPrefix + "_add", D0, D2, AUX), + c.call(f2mPrefix + "_sub", T3, AUX, T3), + // T4 = z3 * x4; + c.call(f2mPrefix + "_mul", z3, x4, T4), + // S1 = S1 + T4; + c.call(f2mPrefix + "_add", S1, T4, S1), + + + // For z.b_.a_ = z3 (z3 needs z2) + // t0 = z2 + z4; + c.call(f2mPrefix + "_add", z2, z4, t0), + // T3 = T3 + T4; + // z2 = T3; + c.call(f2mPrefix + "_add", T3, T4, z2), + // t1 = x2 + x4; + c.call(f2mPrefix + "_add", x2, x4, t1), + // T3 = t0 * t1 - D2 - D4; + c.call(f2mPrefix + "_mul", t1, t0, T3), + c.call(f2mPrefix + "_add", D2, D4, AUX), + c.call(f2mPrefix + "_sub", T3, AUX, T3), + // T4 = my_Fp6::non_residue * T3; + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), T3, T4), + // T3 = z3 * x0; + c.call(f2mPrefix + "_mul", z3, x0, T3), + // S1 = S1 + T3; + c.call(f2mPrefix + "_add", S1, T3, S1), + // T4 = T4 + T3; + c.call(f2mPrefix + "_add", T4, T3, z3), + // z3 = T4; + + // For z.b_.b_ = z4 + // T3 = z5 * x2; + c.call(f2mPrefix + "_mul", z5, x2, T3), + // S1 = S1 + T3; + c.call(f2mPrefix + "_add", S1, T3, S1), + // T4 = my_Fp6::non_residue * T3; + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), T3, T4), + // t0 = x0 + x4; + c.call(f2mPrefix + "_add", x0, x4, t0), + // T3 = t2 * t0 - D0 - D4; + c.call(f2mPrefix + "_mul", t2, t0, T3), + c.call(f2mPrefix + "_add", D0, D4, AUX), + c.call(f2mPrefix + "_sub", T3, AUX, T3), + // T4 = T4 + T3; + c.call(f2mPrefix + "_add", T4, T3, z4), + // z4 = T4; + + // For z.b_.c_ = z5. + // t0 = x0 + x2 + x4; + c.call(f2mPrefix + "_add", x0, x2, t0), + c.call(f2mPrefix + "_add", t0, x4, t0), + // T3 = s0 * t0 - S1; + c.call(f2mPrefix + "_mul", s0, t0, T3), + c.call(f2mPrefix + "_sub", T3, S1, z5), + // z5 = T3; + + ); + } + + + function buildMillerLoop() { + const f = module.addFunction(prefix+ "_millerLoop"); + f.addParam("ppreP", "i32"); + f.addParam("ppreQ", "i32"); + f.addParam("r", "i32"); + f.addLocal("pCoef", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + const preP_PX = c.getLocal("ppreP"); + const preP_PY = c.i32_add(c.getLocal("ppreP"), c.i32_const(f1size)); + + const ELL_0 = c.getLocal("pCoef"); + const ELL_VW = c.i32_add(c.getLocal("pCoef"), c.i32_const(f2size)); + const ELL_VV = c.i32_add(c.getLocal("pCoef"), c.i32_const(2*f2size)); + + + const pVW = module.alloc(f2size); + const VW = c.i32_const(pVW); + const pVV = module.alloc(f2size); + const VV = c.i32_const(pVV); + + const F = c.getLocal("r"); + + + f.addCode( + c.call(ftmPrefix + "_one", F), + + c.setLocal("pCoef", c.i32_add( c.getLocal("ppreQ"), c.i32_const(f2size*3))), + + c.setLocal("i", c.i32_const(ateLoopBitBytes.length-2)), + c.block(c.loop( + + + c.call(ftmPrefix + "_square", F, F), + + c.call(f2mPrefix + "_mul1", ELL_VW,preP_PY, VW), + c.call(f2mPrefix + "_mul1", ELL_VV, preP_PX, VV), + c.call(prefix + "__mulBy024", ELL_0, VW, VV, F), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + c.if( + c.i32_load8_s(c.getLocal("i"), pAteLoopBitBytes), + [ + ...c.call(f2mPrefix + "_mul1", ELL_VW, preP_PY, VW), + ...c.call(f2mPrefix + "_mul1", ELL_VV, preP_PX, VV), + + ...c.call(prefix + "__mulBy024", ELL_0, VW, VV, F), + ...c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + ] + ), + c.br_if(1, c.i32_eqz ( c.getLocal("i") )), + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + + ); + + f.addCode( + c.call(f2mPrefix + "_mul1", ELL_VW, preP_PY, VW), + c.call(f2mPrefix + "_mul1", ELL_VV, preP_PX, VV), + c.call(prefix + "__mulBy024", ELL_0, VW, VV, F), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + c.call(f2mPrefix + "_mul1", ELL_VW, preP_PY, VW), + c.call(f2mPrefix + "_mul1", ELL_VV, preP_PX, VV), + c.call(prefix + "__mulBy024", ELL_0, VW, VV, F), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + ); + + } + + + function buildFrobeniusMap(n) { + const F12 = [ + [ + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + ], + [ + [1n, 0n], + [8376118865763821496583973867626364092589906065868298776909617916018768340080n, 16469823323077808223889137241176536799009286646108169935659301613961712198316n], + [21888242871839275220042445260109153167277707414472061641714758635765020556617n, 0n], + [11697423496358154304825782922584725312912383441159505038794027105778954184319n, 303847389135065887422783454877609941456349188919719272345083954437860409601n], + [21888242871839275220042445260109153167277707414472061641714758635765020556616n, 0n], + [3321304630594332808241809054958361220322477375291206261884409189760185844239n, 5722266937896532885780051958958348231143373700109372999374820235121374419868n], + [21888242871839275222246405745257275088696311157297823662689037894645226208582n, 0n], + [13512124006075453725662431877630910996106405091429524885779419978626457868503n, 5418419548761466998357268504080738289687024511189653727029736280683514010267n], + [2203960485148121921418603742825762020974279258880205651966n, 0n], + [10190819375481120917420622822672549775783927716138318623895010788866272024264n, 21584395482704209334823622290379665147239961968378104390343953940207365798982n], + [2203960485148121921418603742825762020974279258880205651967n, 0n], + [18566938241244942414004596690298913868373833782006617400804628704885040364344n, 16165975933942742336466353786298926857552937457188450663314217659523851788715n], + ] + ]; + + const F6 = [ + [ + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + ], + [ + [1n, 0n], + [21575463638280843010398324269430826099269044274347216827212613867836435027261n, 10307601595873709700152284273816112264069230130616436755625194854815875713954n], + [21888242871839275220042445260109153167277707414472061641714758635765020556616n, 0n], + [3772000881919853776433695186713858239009073593817195771773381919316419345261n, 2236595495967245188281701248203181795121068902605861227855261137820944008926n], + [2203960485148121921418603742825762020974279258880205651966n, 0n], + [18429021223477853657660792034369865839114504446431234726392080002137598044644n, 9344045779998320333812420223237981029506012124075525679208581902008406485703n], + ], + [ + [1n, 0n], + [2581911344467009335267311115468803099551665605076196740867805258568234346338n, 19937756971775647987995932169929341994314640652964949448313374472400716661030n], + [2203960485148121921418603742825762020974279258880205651966n, 0n], + [5324479202449903542726783395506214481928257762400643279780343368557297135718n, 16208900380737693084919495127334387981393726419856888799917914180988844123039n], + [21888242871839275220042445260109153167277707414472061641714758635765020556616n, 0n], + [13981852324922362344252311234282257507216387789820983642040889267519694726527n, 7629828391165209371577384193250820201684255241773809077146787135900891633097n], + ] + ]; + + const f = module.addFunction(prefix+ "__frobeniusMap"+n); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + for (let i=0; i<6; i++) { + const X = (i==0) ? c.getLocal("x") : c.i32_add(c.getLocal("x"), c.i32_const(i*f2size)); + const Xc0 = X; + const Xc1 = c.i32_add(c.getLocal("x"), c.i32_const(i*f2size + f1size)); + const R = (i==0) ? c.getLocal("r") : c.i32_add(c.getLocal("r"), c.i32_const(i*f2size)); + const Rc0 = R; + const Rc1 = c.i32_add(c.getLocal("r"), c.i32_const(i*f2size + f1size)); + const coef = mul2(F12[Math.floor(i/3)][n%12] , F6[i%3][n%6]); + const pCoef = module.alloc([ + ...utils$8.bigInt2BytesLE(toMontgomery(coef[0]), 32), + ...utils$8.bigInt2BytesLE(toMontgomery(coef[1]), 32), + ]); + if (n%2 == 1) { + f.addCode( + c.call(f1mPrefix + "_copy", Xc0, Rc0), + c.call(f1mPrefix + "_neg", Xc1, Rc1), + c.call(f2mPrefix + "_mul", R, c.i32_const(pCoef), R), + ); + } else { + f.addCode(c.call(f2mPrefix + "_mul", X, c.i32_const(pCoef), R)); + } + } + + function mul2(a, b) { + const ac0 = BigInt(a[0]); + const ac1 = BigInt(a[1]); + const bc0 = BigInt(b[0]); + const bc1 = BigInt(b[1]); + const res = [ + (ac0 * bc0 - ( ac1 * bc1) ) % q, + (ac0 * bc1 + ( ac1 * bc0) ) % q, + ]; + if (isNegative$6(res[0])) res[0] = res[0] + q; + return res; + } + + } + + + + function buildFinalExponentiationFirstChunk() { + + const f = module.addFunction(prefix+ "__finalExponentiationFirstChunk"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const elt = c.getLocal("x"); + const eltC0 = elt; + const eltC1 = c.i32_add(elt, c.i32_const(n8*6)); + const r = c.getLocal("r"); + const pA = module.alloc(ftsize); + const A = c.i32_const(pA); + const Ac0 = A; + const Ac1 = c.i32_const(pA + n8*6); + const B = c.i32_const(module.alloc(ftsize)); + const C = c.i32_const(module.alloc(ftsize)); + const D = c.i32_const(module.alloc(ftsize)); + + f.addCode( + // const alt_bn128_Fq12 A = alt_bn128_Fq12(elt.c0,-elt.c1); + c.call(f6mPrefix + "_copy", eltC0, Ac0), + c.call(f6mPrefix + "_neg", eltC1, Ac1), + + // const alt_bn128_Fq12 B = elt.inverse(); + c.call(ftmPrefix + "_inverse", elt, B), + + // const alt_bn128_Fq12 C = A * B; + c.call(ftmPrefix + "_mul", A, B, C), + // const alt_bn128_Fq12 D = C.Frobenius_map(2); + c.call(prefix + "__frobeniusMap2", C, D), + // const alt_bn128_Fq12 result = D * C; + c.call(ftmPrefix + "_mul", C, D, r), + ); + } + + function buildCyclotomicSquare() { + const f = module.addFunction(prefix+ "__cyclotomicSquare"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x4 = c.i32_add(c.getLocal("x"), c.i32_const(f2size)); + const x3 = c.i32_add(c.getLocal("x"), c.i32_const(2*f2size)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(3*f2size)); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(4*f2size)); + const x5 = c.i32_add(c.getLocal("x"), c.i32_const(5*f2size)); + + const r0 = c.getLocal("r"); + const r4 = c.i32_add(c.getLocal("r"), c.i32_const(f2size)); + const r3 = c.i32_add(c.getLocal("r"), c.i32_const(2*f2size)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(3*f2size)); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(4*f2size)); + const r5 = c.i32_add(c.getLocal("r"), c.i32_const(5*f2size)); + + const t0 = c.i32_const(module.alloc(f2size)); + const t1 = c.i32_const(module.alloc(f2size)); + const t2 = c.i32_const(module.alloc(f2size)); + const t3 = c.i32_const(module.alloc(f2size)); + const t4 = c.i32_const(module.alloc(f2size)); + const t5 = c.i32_const(module.alloc(f2size)); + const tmp = c.i32_const(module.alloc(f2size)); + const AUX = c.i32_const(module.alloc(f2size)); + + + f.addCode( + // // t0 + t1*y = (z0 + z1*y)^2 = a^2 + // tmp = z0 * z1; + // t0 = (z0 + z1) * (z0 + my_Fp6::non_residue * z1) - tmp - my_Fp6::non_residue * tmp; + // t1 = tmp + tmp; + c.call(f2mPrefix + "_mul", x0, x1, tmp), + c.call(f2mPrefix + "_mul", x1, c.i32_const(pNonResidueF6), t0), + c.call(f2mPrefix + "_add", x0, t0, t0), + c.call(f2mPrefix + "_add", x0, x1, AUX), + c.call(f2mPrefix + "_mul", AUX, t0, t0), + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), tmp, AUX), + c.call(f2mPrefix + "_add", tmp, AUX, AUX), + c.call(f2mPrefix + "_sub", t0, AUX, t0), + c.call(f2mPrefix + "_add", tmp, tmp, t1), + + // // t2 + t3*y = (z2 + z3*y)^2 = b^2 + // tmp = z2 * z3; + // t2 = (z2 + z3) * (z2 + my_Fp6::non_residue * z3) - tmp - my_Fp6::non_residue * tmp; + // t3 = tmp + tmp; + c.call(f2mPrefix + "_mul", x2, x3, tmp), + c.call(f2mPrefix + "_mul", x3, c.i32_const(pNonResidueF6), t2), + c.call(f2mPrefix + "_add", x2, t2, t2), + c.call(f2mPrefix + "_add", x2, x3, AUX), + c.call(f2mPrefix + "_mul", AUX, t2, t2), + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), tmp, AUX), + c.call(f2mPrefix + "_add", tmp, AUX, AUX), + c.call(f2mPrefix + "_sub", t2, AUX, t2), + c.call(f2mPrefix + "_add", tmp, tmp, t3), + + // // t4 + t5*y = (z4 + z5*y)^2 = c^2 + // tmp = z4 * z5; + // t4 = (z4 + z5) * (z4 + my_Fp6::non_residue * z5) - tmp - my_Fp6::non_residue * tmp; + // t5 = tmp + tmp; + c.call(f2mPrefix + "_mul", x4, x5, tmp), + c.call(f2mPrefix + "_mul", x5, c.i32_const(pNonResidueF6), t4), + c.call(f2mPrefix + "_add", x4, t4, t4), + c.call(f2mPrefix + "_add", x4, x5, AUX), + c.call(f2mPrefix + "_mul", AUX, t4, t4), + c.call(f2mPrefix + "_mul", c.i32_const(pNonResidueF6), tmp, AUX), + c.call(f2mPrefix + "_add", tmp, AUX, AUX), + c.call(f2mPrefix + "_sub", t4, AUX, t4), + c.call(f2mPrefix + "_add", tmp, tmp, t5), + + // For A + // z0 = 3 * t0 - 2 * z0 + c.call(f2mPrefix + "_sub", t0, x0, r0), + c.call(f2mPrefix + "_add", r0, r0, r0), + c.call(f2mPrefix + "_add", t0, r0, r0), + // z1 = 3 * t1 + 2 * z1 + c.call(f2mPrefix + "_add", t1, x1, r1), + c.call(f2mPrefix + "_add", r1, r1, r1), + c.call(f2mPrefix + "_add", t1, r1, r1), + + // For B + // z2 = 3 * (xi * t5) + 2 * z2 + c.call(f2mPrefix + "_mul", t5, c.i32_const(pAltBn128Twist), AUX), + c.call(f2mPrefix + "_add", AUX, x2, r2), + c.call(f2mPrefix + "_add", r2, r2, r2), + c.call(f2mPrefix + "_add", AUX, r2, r2), + // z3 = 3 * t4 - 2 * z3 + c.call(f2mPrefix + "_sub", t4, x3, r3), + c.call(f2mPrefix + "_add", r3, r3, r3), + c.call(f2mPrefix + "_add", t4, r3, r3), + + // For C + // z4 = 3 * t2 - 2 * z4 + c.call(f2mPrefix + "_sub", t2, x4, r4), + c.call(f2mPrefix + "_add", r4, r4, r4), + c.call(f2mPrefix + "_add", t2, r4, r4), + // z5 = 3 * t3 + 2 * z5 + c.call(f2mPrefix + "_add", t3, x5, r5), + c.call(f2mPrefix + "_add", r5, r5, r5), + c.call(f2mPrefix + "_add", t3, r5, r5), + + ); + } + + + function buildCyclotomicExp(exponent, fnName) { + const exponentNafBytes = naf(exponent).map( (b) => (b==-1 ? 0xFF: b) ); + const pExponentNafBytes = module.alloc(exponentNafBytes); + + const f = module.addFunction(prefix+ "__cyclotomicExp_"+fnName); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + f.addLocal("bit", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + const x = c.getLocal("x"); + + const res = c.getLocal("r"); + + const inverse = c.i32_const(module.alloc(ftsize)); + + + f.addCode( + c.call(ftmPrefix + "_conjugate", x, inverse), + c.call(ftmPrefix + "_one", res), + + c.if( + c.teeLocal("bit", c.i32_load8_s(c.i32_const(exponentNafBytes.length-1), pExponentNafBytes)), + c.if( + c.i32_eq( + c.getLocal("bit"), + c.i32_const(1) + ), + c.call(ftmPrefix + "_mul", res, x, res), + c.call(ftmPrefix + "_mul", res, inverse, res), + ) + ), + + c.setLocal("i", c.i32_const(exponentNafBytes.length-2)), + c.block(c.loop( + c.call(prefix + "__cyclotomicSquare", res, res), + c.if( + c.teeLocal("bit", c.i32_load8_s(c.getLocal("i"), pExponentNafBytes)), + c.if( + c.i32_eq( + c.getLocal("bit"), + c.i32_const(1) + ), + c.call(ftmPrefix + "_mul", res, x, res), + c.call(ftmPrefix + "_mul", res, inverse, res), + ) + ), + c.br_if(1, c.i32_eqz ( c.getLocal("i") )), + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + + + function buildFinalExponentiationLastChunk() { + buildCyclotomicSquare(); + buildCyclotomicExp(finalExpZ, "w0"); + + const f = module.addFunction(prefix+ "__finalExponentiationLastChunk"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const elt = c.getLocal("x"); + const result = c.getLocal("r"); + const A = c.i32_const(module.alloc(ftsize)); + const B = c.i32_const(module.alloc(ftsize)); + const C = c.i32_const(module.alloc(ftsize)); + const D = c.i32_const(module.alloc(ftsize)); + const E = c.i32_const(module.alloc(ftsize)); + const F = c.i32_const(module.alloc(ftsize)); + const G = c.i32_const(module.alloc(ftsize)); + const H = c.i32_const(module.alloc(ftsize)); + const I = c.i32_const(module.alloc(ftsize)); + const J = c.i32_const(module.alloc(ftsize)); + const K = c.i32_const(module.alloc(ftsize)); + const L = c.i32_const(module.alloc(ftsize)); + const M = c.i32_const(module.alloc(ftsize)); + const N = c.i32_const(module.alloc(ftsize)); + const O = c.i32_const(module.alloc(ftsize)); + const P = c.i32_const(module.alloc(ftsize)); + const Q = c.i32_const(module.alloc(ftsize)); + const R = c.i32_const(module.alloc(ftsize)); + const S = c.i32_const(module.alloc(ftsize)); + const T = c.i32_const(module.alloc(ftsize)); + const U = c.i32_const(module.alloc(ftsize)); + + f.addCode( + + + // A = exp_by_neg_z(elt) // = elt^(-z) + c.call(prefix + "__cyclotomicExp_w0", elt, A), + c.call(ftmPrefix + "_conjugate", A, A), + // B = A^2 // = elt^(-2*z) + c.call(prefix + "__cyclotomicSquare", A, B), + // C = B^2 // = elt^(-4*z) + c.call(prefix + "__cyclotomicSquare", B, C), + // D = C * B // = elt^(-6*z) + c.call(ftmPrefix + "_mul", C, B, D), + // E = exp_by_neg_z(D) // = elt^(6*z^2) + c.call(prefix + "__cyclotomicExp_w0", D, E), + c.call(ftmPrefix + "_conjugate", E, E), + // F = E^2 // = elt^(12*z^2) + c.call(prefix + "__cyclotomicSquare", E, F), + // G = epx_by_neg_z(F) // = elt^(-12*z^3) + c.call(prefix + "__cyclotomicExp_w0", F, G), + c.call(ftmPrefix + "_conjugate", G, G), + // H = conj(D) // = elt^(6*z) + c.call(ftmPrefix + "_conjugate", D, H), + // I = conj(G) // = elt^(12*z^3) + c.call(ftmPrefix + "_conjugate", G, I), + // J = I * E // = elt^(12*z^3 + 6*z^2) + c.call(ftmPrefix + "_mul", I, E, J), + // K = J * H // = elt^(12*z^3 + 6*z^2 + 6*z) + c.call(ftmPrefix + "_mul", J, H, K), + // L = K * B // = elt^(12*z^3 + 6*z^2 + 4*z) + c.call(ftmPrefix + "_mul", K, B, L), + // M = K * E // = elt^(12*z^3 + 12*z^2 + 6*z) + c.call(ftmPrefix + "_mul", K, E, M), + + // N = M * elt // = elt^(12*z^3 + 12*z^2 + 6*z + 1) + c.call(ftmPrefix + "_mul", M, elt, N), + + // O = L.Frobenius_map(1) // = elt^(q*(12*z^3 + 6*z^2 + 4*z)) + c.call(prefix + "__frobeniusMap1", L, O), + // P = O * N // = elt^(q*(12*z^3 + 6*z^2 + 4*z) * (12*z^3 + 12*z^2 + 6*z + 1)) + c.call(ftmPrefix + "_mul", O, N, P), + // Q = K.Frobenius_map(2) // = elt^(q^2 * (12*z^3 + 6*z^2 + 6*z)) + c.call(prefix + "__frobeniusMap2", K, Q), + // R = Q * P // = elt^(q^2 * (12*z^3 + 6*z^2 + 6*z) + q*(12*z^3 + 6*z^2 + 4*z) * (12*z^3 + 12*z^2 + 6*z + 1)) + c.call(ftmPrefix + "_mul", Q, P, R), + // S = conj(elt) // = elt^(-1) + c.call(ftmPrefix + "_conjugate", elt, S), + // T = S * L // = elt^(12*z^3 + 6*z^2 + 4*z - 1) + c.call(ftmPrefix + "_mul", S, L, T), + // U = T.Frobenius_map(3) // = elt^(q^3(12*z^3 + 6*z^2 + 4*z - 1)) + c.call(prefix + "__frobeniusMap3", T, U), + // V = U * R // = elt^(q^3(12*z^3 + 6*z^2 + 4*z - 1) + q^2 * (12*z^3 + 6*z^2 + 6*z) + q*(12*z^3 + 6*z^2 + 4*z) * (12*z^3 + 12*z^2 + 6*z + 1)) + c.call(ftmPrefix + "_mul", U, R, result), + // result = V + ); + } + + + function buildFinalExponentiation() { + buildFinalExponentiationFirstChunk(); + buildFinalExponentiationLastChunk(); + const f = module.addFunction(prefix+ "_finalExponentiation"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const elt = c.getLocal("x"); + const result = c.getLocal("r"); + const eltToFirstChunk = c.i32_const(module.alloc(ftsize)); + + f.addCode( + c.call(prefix + "__finalExponentiationFirstChunk", elt, eltToFirstChunk ), + c.call(prefix + "__finalExponentiationLastChunk", eltToFirstChunk, result ) + ); + } + + + function buildFinalExponentiationOld() { + const f = module.addFunction(prefix+ "_finalExponentiationOld"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const exponent = 552484233613224096312617126783173147097382103762957654188882734314196910839907541213974502761540629817009608548654680343627701153829446747810907373256841551006201639677726139946029199968412598804882391702273019083653272047566316584365559776493027495458238373902875937659943504873220554161550525926302303331747463515644711876653177129578303191095900909191624817826566688241804408081892785725967931714097716709526092261278071952560171111444072049229123565057483750161460024353346284167282452756217662335528813519139808291170539072125381230815729071544861602750936964829313608137325426383735122175229541155376346436093930287402089517426973178917569713384748081827255472576937471496195752727188261435633271238710131736096299798168852925540549342330775279877006784354801422249722573783561685179618816480037695005515426162362431072245638324744480n; + + const pExponent = module.alloc(utils$8.bigInt2BytesLE( exponent, 352 )); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call(ftmPrefix + "_exp", c.getLocal("x"), c.i32_const(pExponent), c.i32_const(352), c.getLocal("r")), + ); + } + + + + + const pPreP = module.alloc(prePSize); + const pPreQ = module.alloc(preQSize); + + function buildPairingEquation(nPairings) { + + const f = module.addFunction(prefix+ "_pairingEq"+nPairings); + for (let i=0; i acc + ( b!=0 ? 1 : 0) ,0); + const ateNCoefs = ateNAddCoefs + ateNDblCoefs + 1; + const prePSize = 3*2*n8q; + const preQSize = 3*n8q*2 + ateNCoefs*ateCoefSize; + const finalExpIsNegative = true; + + const finalExpZ = 15132376222941642752n; + + + module.modules[prefix] = { + n64q: n64q, + n64r: n64r, + n8q: n8q, + n8r: n8r, + pG1gen: pG1gen, + pG1zero: pG1zero, + pG1b: pG1b, + pG2gen: pG2gen, + pG2zero: pG2zero, + pG2b: pG2b, + pq: module.modules["f1m"].pq, + pr: pr, + pOneT: pOneT, + r: r, + q: q, + prePSize: prePSize, + preQSize: preQSize + }; + + + function naf(n) { + let E = n; + const res = []; + while (E > 0n) { + if (isOdd$6(E)) { + const z = 2 - Number(E % 4n); + res.push( z ); + E = E - BigInt(z); + } else { + res.push( 0 ); + } + E = E >> 1n; + } + return res; + } + + function bits(n) { + let E = n; + const res = []; + while (E > 0n) { + if (isOdd$6(E)) { + res.push( 1 ); + } else { + res.push( 0 ); + } + E = E >> 1n; + } + return res; + } + + function buildPrepareG1() { + const f = module.addFunction(prefix+ "_prepareG1"); + f.addParam("pP", "i32"); + f.addParam("ppreP", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call(g1mPrefix + "_normalize", c.getLocal("pP"), c.getLocal("ppreP")), // TODO Remove if already in affine + ); + } + + + + function buildPrepDoubleStep() { + const f = module.addFunction(prefix+ "_prepDblStep"); + f.addParam("R", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const Rx = c.getLocal("R"); + const Ry = c.i32_add(c.getLocal("R"), c.i32_const(2*n8q)); + const Rz = c.i32_add(c.getLocal("R"), c.i32_const(4*n8q)); + + const t0 = c.getLocal("r"); + const t3 = c.i32_add(c.getLocal("r"), c.i32_const(2*n8q)); + const t6 = c.i32_add(c.getLocal("r"), c.i32_const(4*n8q)); + + + const zsquared = c.i32_const(module.alloc(f2size)); + const t1 = c.i32_const(module.alloc(f2size)); + const t2 = c.i32_const(module.alloc(f2size)); + const t4 = c.i32_const(module.alloc(f2size)); + const t5 = c.i32_const(module.alloc(f2size)); + + f.addCode( + + // tmp0 = r.x.square(); + c.call(f2mPrefix + "_square", Rx, t0), + + // tmp1 = r.y.square(); + c.call(f2mPrefix + "_square", Ry, t1), + + // tmp2 = tmp1.square(); + c.call(f2mPrefix + "_square", t1, t2), + + // tmp3 = (tmp1 + r.x).square() - tmp0 - tmp2; + c.call(f2mPrefix + "_add", t1, Rx, t3), + c.call(f2mPrefix + "_square", t3, t3), + c.call(f2mPrefix + "_sub", t3, t0, t3), + c.call(f2mPrefix + "_sub", t3, t2, t3), + + // tmp3 = tmp3 + tmp3; + c.call(f2mPrefix + "_add", t3, t3, t3), + + // tmp4 = tmp0 + tmp0 + tmp0; + c.call(f2mPrefix + "_add", t0, t0, t4), + c.call(f2mPrefix + "_add", t4, t0, t4), + + // tmp6 = r.x + tmp4; + c.call(f2mPrefix + "_add", Rx, t4, t6), + + // tmp5 = tmp4.square(); + c.call(f2mPrefix + "_square", t4, t5), + + // zsquared = r.z.square(); + c.call(f2mPrefix + "_square", Rz, zsquared), + + // r.x = tmp5 - tmp3 - tmp3; + c.call(f2mPrefix + "_sub", t5, t3, Rx), + c.call(f2mPrefix + "_sub", Rx, t3, Rx), + + // r.z = (r.z + r.y).square() - tmp1 - zsquared; + c.call(f2mPrefix + "_add", Rz, Ry, Rz), + c.call(f2mPrefix + "_square", Rz, Rz), + c.call(f2mPrefix + "_sub", Rz, t1, Rz), + c.call(f2mPrefix + "_sub", Rz, zsquared, Rz), + + // r.y = (tmp3 - r.x) * tmp4; + c.call(f2mPrefix + "_sub", t3, Rx, Ry), + c.call(f2mPrefix + "_mul", Ry, t4, Ry), + + // tmp2 = tmp2 + tmp2; + c.call(f2mPrefix + "_add", t2, t2, t2), + + // tmp2 = tmp2 + tmp2; + c.call(f2mPrefix + "_add", t2, t2, t2), + + // tmp2 = tmp2 + tmp2; + c.call(f2mPrefix + "_add", t2, t2, t2), + + // r.y -= tmp2; + c.call(f2mPrefix + "_sub", Ry, t2, Ry), + + // tmp3 = tmp4 * zsquared; + c.call(f2mPrefix + "_mul", t4, zsquared, t3), + + // tmp3 = tmp3 + tmp3; + c.call(f2mPrefix + "_add", t3, t3, t3), + + // tmp3 = -tmp3; + c.call(f2mPrefix + "_neg", t3, t3), + + // tmp6 = tmp6.square() - tmp0 - tmp5; + c.call(f2mPrefix + "_square", t6, t6), + c.call(f2mPrefix + "_sub", t6, t0, t6), + c.call(f2mPrefix + "_sub", t6, t5, t6), + + // tmp1 = tmp1 + tmp1; + c.call(f2mPrefix + "_add", t1, t1, t1), + + // tmp1 = tmp1 + tmp1; + c.call(f2mPrefix + "_add", t1, t1, t1), + + // tmp6 = tmp6 - tmp1; + c.call(f2mPrefix + "_sub", t6, t1, t6), + + // tmp0 = r.z * zsquared; + c.call(f2mPrefix + "_mul", Rz, zsquared, t0), + + // tmp0 = tmp0 + tmp0; + c.call(f2mPrefix + "_add", t0, t0, t0), + + ); + } + + function buildPrepAddStep() { + const f = module.addFunction(prefix+ "_prepAddStep"); + f.addParam("R", "i32"); + f.addParam("Q", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const Rx = c.getLocal("R"); + const Ry = c.i32_add(c.getLocal("R"), c.i32_const(2*n8q)); + const Rz = c.i32_add(c.getLocal("R"), c.i32_const(4*n8q)); + + const Qx = c.getLocal("Q"); + const Qy = c.i32_add(c.getLocal("Q"), c.i32_const(2*n8q)); + + const t10 = c.getLocal("r"); + const t1 = c.i32_add(c.getLocal("r"), c.i32_const(2*n8q)); + const t9 = c.i32_add(c.getLocal("r"), c.i32_const(4*n8q)); + + const zsquared = c.i32_const(module.alloc(f2size)); + const ysquared = c.i32_const(module.alloc(f2size)); + const ztsquared = c.i32_const(module.alloc(f2size)); + const t0 = c.i32_const(module.alloc(f2size)); + const t2 = c.i32_const(module.alloc(f2size)); + const t3 = c.i32_const(module.alloc(f2size)); + const t4 = c.i32_const(module.alloc(f2size)); + const t5 = c.i32_const(module.alloc(f2size)); + const t6 = c.i32_const(module.alloc(f2size)); + const t7 = c.i32_const(module.alloc(f2size)); + const t8 = c.i32_const(module.alloc(f2size)); + + f.addCode( + + // zsquared = r.z.square(); + c.call(f2mPrefix + "_square", Rz, zsquared), + + // ysquared = q.y.square(); + c.call(f2mPrefix + "_square", Qy, ysquared), + + // t0 = zsquared * q.x; + c.call(f2mPrefix + "_mul", zsquared, Qx, t0), + + // t1 = ((q.y + r.z).square() - ysquared - zsquared) * zsquared; + c.call(f2mPrefix + "_add", Qy, Rz, t1), + c.call(f2mPrefix + "_square", t1, t1), + c.call(f2mPrefix + "_sub", t1, ysquared, t1), + c.call(f2mPrefix + "_sub", t1, zsquared, t1), + c.call(f2mPrefix + "_mul", t1, zsquared, t1), + + // t2 = t0 - r.x; + c.call(f2mPrefix + "_sub", t0, Rx, t2), + + // t3 = t2.square(); + c.call(f2mPrefix + "_square", t2, t3), + + // t4 = t3 + t3; + c.call(f2mPrefix + "_add", t3, t3, t4), + + // t4 = t4 + t4; + c.call(f2mPrefix + "_add", t4, t4, t4), + + // t5 = t4 * t2; + c.call(f2mPrefix + "_mul", t4, t2, t5), + + // t6 = t1 - r.y - r.y; + c.call(f2mPrefix + "_sub", t1, Ry, t6), + c.call(f2mPrefix + "_sub", t6, Ry, t6), + + // t9 = t6 * q.x; + c.call(f2mPrefix + "_mul", t6, Qx, t9), + + // t7 = t4 * r.x; + c.call(f2mPrefix + "_mul", t4, Rx, t7), + + // r.x = t6.square() - t5 - t7 - t7; + c.call(f2mPrefix + "_square", t6, Rx), + c.call(f2mPrefix + "_sub", Rx, t5, Rx), + c.call(f2mPrefix + "_sub", Rx, t7, Rx), + c.call(f2mPrefix + "_sub", Rx, t7, Rx), + + // r.z = (r.z + t2).square() - zsquared - t3; + c.call(f2mPrefix + "_add", Rz, t2, Rz), + c.call(f2mPrefix + "_square", Rz, Rz), + c.call(f2mPrefix + "_sub", Rz, zsquared, Rz), + c.call(f2mPrefix + "_sub", Rz, t3, Rz), + + // t10 = q.y + r.z; + c.call(f2mPrefix + "_add", Qy, Rz, t10), + + // t8 = (t7 - r.x) * t6; + c.call(f2mPrefix + "_sub", t7, Rx, t8), + c.call(f2mPrefix + "_mul", t8, t6, t8), + + // t0 = r.y * t5; + c.call(f2mPrefix + "_mul", Ry, t5, t0), + + // t0 = t0 + t0; + c.call(f2mPrefix + "_add", t0, t0, t0), + + // r.y = t8 - t0; + c.call(f2mPrefix + "_sub", t8, t0, Ry), + + // t10 = t10.square() - ysquared; + c.call(f2mPrefix + "_square", t10, t10), + c.call(f2mPrefix + "_sub", t10, ysquared, t10), + + // ztsquared = r.z.square(); + c.call(f2mPrefix + "_square", Rz, ztsquared), + + // t10 = t10 - ztsquared; + c.call(f2mPrefix + "_sub", t10, ztsquared, t10), + + // t9 = t9 + t9 - t10; + c.call(f2mPrefix + "_add", t9, t9, t9), + c.call(f2mPrefix + "_sub", t9, t10, t9), + + // t10 = r.z + r.z; + c.call(f2mPrefix + "_add", Rz, Rz, t10), + + // t6 = -t6; + c.call(f2mPrefix + "_neg", t6, t6), + + // t1 = t6 + t6; + c.call(f2mPrefix + "_add", t6, t6, t1), + ); + } + + + function buildPrepareG2() { + const f = module.addFunction(prefix+ "_prepareG2"); + f.addParam("pQ", "i32"); + f.addParam("ppreQ", "i32"); + f.addLocal("pCoef", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + + const Q = c.getLocal("pQ"); + + const pR = module.alloc(f2size*3); + const R = c.i32_const(pR); + + const base = c.getLocal("ppreQ"); + + f.addCode( + c.call(g2mPrefix + "_normalize", Q, base), + c.if( + c.call(g2mPrefix + "_isZero", base), + c.ret([]) + ), + c.call(g2mPrefix + "_copy", base, R), + c.setLocal("pCoef", c.i32_add(c.getLocal("ppreQ"), c.i32_const(f2size*3))), + ); + + f.addCode( + c.setLocal("i", c.i32_const(ateLoopBitBytes.length-2)), + c.block(c.loop( + + c.call(prefix + "_prepDblStep", R, c.getLocal("pCoef")), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + c.if( + c.i32_load8_s(c.getLocal("i"), pAteLoopBitBytes), + [ + ...c.call(prefix + "_prepAddStep", R, base, c.getLocal("pCoef")), + ...c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + ] + ), + c.br_if(1, c.i32_eqz ( c.getLocal("i") )), + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + } + + + function buildF6Mul1() { + const f = module.addFunction(f6mPrefix+ "_mul1"); + f.addParam("pA", "i32"); // F6 + f.addParam("pC1", "i32"); // F2 + f.addParam("pR", "i32"); // F6 + + const c = f.getCodeBuilder(); + + const A_c0 = c.getLocal("pA"); + const A_c1 = c.i32_add(c.getLocal("pA"), c.i32_const(f1size*2)); + const A_c2 = c.i32_add(c.getLocal("pA"), c.i32_const(f1size*4)); + + const c1 = c.getLocal("pC1"); + + const t1 = c.getLocal("pR"); + const t2 = c.i32_add(c.getLocal("pR"), c.i32_const(f1size*2)); + const b_b = c.i32_add(c.getLocal("pR"), c.i32_const(f1size*4)); + + const Ac0_Ac1 = c.i32_const(module.alloc(f1size*2)); + const Ac1_Ac2 = c.i32_const(module.alloc(f1size*2)); + + f.addCode( + + c.call(f2mPrefix + "_add", A_c0, A_c1, Ac0_Ac1), + c.call(f2mPrefix + "_add", A_c1, A_c2, Ac1_Ac2), + + // let b_b = self.c1 * c1; + c.call(f2mPrefix + "_mul", A_c1, c1, b_b), + + // let t1 = (self.c1 + self.c2) * c1 - b_b; + c.call(f2mPrefix + "_mul", Ac1_Ac2, c1, t1), + c.call(f2mPrefix + "_sub", t1, b_b, t1), + + // let t1 = t1.mul_by_nonresidue(); + c.call(f2mPrefix + "_mulNR", t1, t1), + + // let t2 = (self.c0 + self.c1) * c1 - b_b; + c.call(f2mPrefix + "_mul", Ac0_Ac1, c1, t2), + c.call(f2mPrefix + "_sub", t2, b_b, t2), + ); + } + buildF6Mul1(); + + function buildF6Mul01() { + const f = module.addFunction(f6mPrefix+ "_mul01"); + f.addParam("pA", "i32"); // F6 + f.addParam("pC0", "i32"); // F2 + f.addParam("pC1", "i32"); // F2 + f.addParam("pR", "i32"); // F6 + + const c = f.getCodeBuilder(); + + const A_c0 = c.getLocal("pA"); + const A_c1 = c.i32_add(c.getLocal("pA"), c.i32_const(f1size*2)); + const A_c2 = c.i32_add(c.getLocal("pA"), c.i32_const(f1size*4)); + + const c0 = c.getLocal("pC0"); + const c1 = c.getLocal("pC1"); + + const t1 = c.getLocal("pR"); + const t2 = c.i32_add(c.getLocal("pR"), c.i32_const(f1size*2)); + const t3 = c.i32_add(c.getLocal("pR"), c.i32_const(f1size*4)); + + const a_a = c.i32_const(module.alloc(f1size*2)); + const b_b = c.i32_const(module.alloc(f1size*2)); + const Ac0_Ac1 = c.i32_const(module.alloc(f1size*2)); + const Ac0_Ac2 = c.i32_const(module.alloc(f1size*2)); + + f.addCode( + // let a_a = self.c0 * c0; + c.call(f2mPrefix + "_mul", A_c0, c0, a_a), + + // let b_b = self.c1 * c1; + c.call(f2mPrefix + "_mul", A_c1, c1, b_b), + + + c.call(f2mPrefix + "_add", A_c0, A_c1, Ac0_Ac1), + c.call(f2mPrefix + "_add", A_c0, A_c2, Ac0_Ac2), + + // let t1 = (self.c1 + self.c2) * c1 - b_b; + c.call(f2mPrefix + "_add", A_c1, A_c2, t1), + c.call(f2mPrefix + "_mul", t1, c1, t1), + c.call(f2mPrefix + "_sub", t1, b_b, t1), + + // let t1 = t1.mul_by_nonresidue() + a_a; + c.call(f2mPrefix + "_mulNR", t1, t1), + c.call(f2mPrefix + "_add", t1, a_a, t1), + + // let t2 = (c0 + c1) * (self.c0 + self.c1) - a_a - b_b; + c.call(f2mPrefix + "_add", c0, c1, t2), + c.call(f2mPrefix + "_mul", t2, Ac0_Ac1, t2), + c.call(f2mPrefix + "_sub", t2, a_a, t2), + c.call(f2mPrefix + "_sub", t2, b_b, t2), + + // let t3 = (self.c0 + self.c2) * c0 - a_a + b_b; + c.call(f2mPrefix + "_mul", Ac0_Ac2, c0, t3), + c.call(f2mPrefix + "_sub", t3, a_a, t3), + c.call(f2mPrefix + "_add", t3, b_b, t3), + + + ); + } + buildF6Mul01(); + + + function buildF12Mul014() { + + const f = module.addFunction(ftmPrefix+ "_mul014"); + f.addParam("pA", "i32"); // F12 + f.addParam("pC0", "i32"); // F2 + f.addParam("pC1", "i32"); // F2 + f.addParam("pC4", "i32"); // F2 + f.addParam("pR", "i32"); // F12 + + const c = f.getCodeBuilder(); + + + const A_c0 = c.getLocal("pA"); + const A_c1 = c.i32_add(c.getLocal("pA"), c.i32_const(f1size*6)); + + const c0 = c.getLocal("pC0"); + const c1 = c.getLocal("pC1"); + const c4 = c.getLocal("pC4"); + + const aa = c.i32_const(module.alloc(f1size*6)); + const bb = c.i32_const(module.alloc(f1size*6)); + const o = c.i32_const(module.alloc(f1size*2)); + + const R_c0 = c.getLocal("pR"); + const R_c1 = c.i32_add(c.getLocal("pR"), c.i32_const(f1size*6)); + + f.addCode( + // let aa = self.c0.mul_by_01(c0, c1); + c.call(f6mPrefix + "_mul01", A_c0, c0, c1, aa), + + // let bb = self.c1.mul_by_1(c4); + c.call(f6mPrefix + "_mul1", A_c1, c4, bb), + + // let o = c1 + c4; + c.call(f2mPrefix + "_add", c1, c4, o), + + // let c1 = self.c1 + self.c0; + c.call(f6mPrefix + "_add", A_c1, A_c0, R_c1), + + // let c1 = c1.mul_by_01(c0, &o); + c.call(f6mPrefix + "_mul01", R_c1, c0, o, R_c1), + + // let c1 = c1 - aa - bb; + c.call(f6mPrefix + "_sub", R_c1, aa, R_c1), + c.call(f6mPrefix + "_sub", R_c1, bb, R_c1), + + // let c0 = bb; + c.call(f6mPrefix + "_copy", bb, R_c0), + + // let c0 = c0.mul_by_nonresidue(); + c.call(f6mPrefix + "_mulNR", R_c0, R_c0), + + // let c0 = c0 + aa; + c.call(f6mPrefix + "_add", R_c0, aa, R_c0), + ); + } + buildF12Mul014(); + + + function buildELL() { + const f = module.addFunction(prefix+ "_ell"); + f.addParam("pP", "i32"); + f.addParam("pCoefs", "i32"); + f.addParam("pF", "i32"); + + const c = f.getCodeBuilder(); + + const Px = c.getLocal("pP"); + const Py = c.i32_add(c.getLocal("pP"), c.i32_const(n8q)); + + const F = c.getLocal("pF"); + + const coef0_0 = c.getLocal("pCoefs"); + const coef0_1 = c.i32_add(c.getLocal("pCoefs"), c.i32_const(f1size)); + const coef1_0 = c.i32_add(c.getLocal("pCoefs"), c.i32_const(f1size*2)); + const coef1_1 = c.i32_add(c.getLocal("pCoefs"), c.i32_const(f1size*3)); + const coef2 = c.i32_add(c.getLocal("pCoefs"), c.i32_const(f1size*4)); + + const pc0 = module.alloc(f1size*2); + const c0 = c.i32_const(pc0); + const c0_c0 = c.i32_const(pc0); + const c0_c1 = c.i32_const(pc0+f1size); + + const pc1 = module.alloc(f1size*2); + const c1 = c.i32_const(pc1); + const c1_c0 = c.i32_const(pc1); + const c1_c1 = c.i32_const(pc1+f1size); + f.addCode( + // let mut c0 = coeffs.0; + // let mut c1 = coeffs.1; + // + // c0.c0 *= p.y; + // c0.c1 *= p.y; + // + // c1.c0 *= p.x; + // c1.c1 *= p.x; + // + // f.mul_by_014(&coeffs.2, &c1, &c0) + + c.call(f1mPrefix + "_mul", coef0_0, Py, c0_c0), + c.call(f1mPrefix + "_mul", coef0_1, Py, c0_c1), + c.call(f1mPrefix + "_mul", coef1_0, Px, c1_c0), + c.call(f1mPrefix + "_mul", coef1_1, Px, c1_c1), + + c.call(ftmPrefix + "_mul014", F, coef2, c1, c0, F), + + ); + + } + buildELL(); + + function buildMillerLoop() { + const f = module.addFunction(prefix+ "_millerLoop"); + f.addParam("ppreP", "i32"); + f.addParam("ppreQ", "i32"); + f.addParam("r", "i32"); + f.addLocal("pCoef", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + const preP = c.getLocal("ppreP"); + + const coefs = c.getLocal("pCoef"); + + const F = c.getLocal("r"); + + + f.addCode( + c.call(ftmPrefix + "_one", F), + + c.if( + c.call(g1mPrefix + "_isZero", preP), + c.ret([]) + ), + c.if( + c.call(g1mPrefix + "_isZero", c.getLocal("ppreQ")), + c.ret([]) + ), + c.setLocal("pCoef", c.i32_add( c.getLocal("ppreQ"), c.i32_const(f2size*3))), + + c.setLocal("i", c.i32_const(ateLoopBitBytes.length-2)), + c.block(c.loop( + + + c.call(prefix + "_ell", preP, coefs, F), + c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + + c.if( + c.i32_load8_s(c.getLocal("i"), pAteLoopBitBytes), + [ + ...c.call(prefix + "_ell", preP, coefs, F), + ...c.setLocal("pCoef", c.i32_add(c.getLocal("pCoef"), c.i32_const(ateCoefSize))), + ] + ), + c.call(ftmPrefix + "_square", F, F), + + c.br_if(1, c.i32_eq ( c.getLocal("i"), c.i32_const(1) )), + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )), + c.call(prefix + "_ell", preP, coefs, F), + + ); + + + { + f.addCode( + c.call(ftmPrefix + "_conjugate", F, F), + ); + } + } + + + function buildFrobeniusMap(n) { + const F12 = [ + [ + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + ], + [ + [1n, 0n], + [3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n, 151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n], + [793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n, 0n], + [2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n, 1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n], + [793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n, 0n], + [3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n, 877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n], + [4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n, 0n], + [151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n, 3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n], + [4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n, 0n], + [1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n, 2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n], + [4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n, 0n], + [877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n, 3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n], + ] + ]; + + const F6 = [ + [ + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + [1n, 0n], + ], + [ + [1n, 0n], + [0n, 4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n], + [793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n, 0n], + [0n, 1n], + [4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n, 0n], + [0n, 793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n], + ], + [ + [1n, 0n], + [4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n, 0n], + [4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n, 0n], + [4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n, 0n], + [793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n, 0n], + [793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n, 0n], + ] + ]; + + const f = module.addFunction(ftmPrefix + "_frobeniusMap"+n); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + for (let i=0; i<6; i++) { + const X = (i==0) ? c.getLocal("x") : c.i32_add(c.getLocal("x"), c.i32_const(i*f2size)); + const Xc0 = X; + const Xc1 = c.i32_add(c.getLocal("x"), c.i32_const(i*f2size + f1size)); + const R = (i==0) ? c.getLocal("r") : c.i32_add(c.getLocal("r"), c.i32_const(i*f2size)); + const Rc0 = R; + const Rc1 = c.i32_add(c.getLocal("r"), c.i32_const(i*f2size + f1size)); + const coef = mul2(F12[Math.floor(i/3)][n%12] , F6[i%3][n%6]); + const pCoef = module.alloc([ + ...utils$7.bigInt2BytesLE(toMontgomery(coef[0]), n8q), + ...utils$7.bigInt2BytesLE(toMontgomery(coef[1]), n8q), + ]); + if (n%2 == 1) { + f.addCode( + c.call(f1mPrefix + "_copy", Xc0, Rc0), + c.call(f1mPrefix + "_neg", Xc1, Rc1), + c.call(f2mPrefix + "_mul", R, c.i32_const(pCoef), R), + ); + } else { + f.addCode(c.call(f2mPrefix + "_mul", X, c.i32_const(pCoef), R)); + } + } + + function mul2(a, b) { + const ac0 = a[0]; + const ac1 = a[1]; + const bc0 = b[0]; + const bc1 = b[1]; + const res = [ + (ac0 * bc0 - (ac1 * bc1)) % q, + (ac0 * bc1 + (ac1 * bc0)) % q, + ]; + if (isNegative$5(res[0])) res[0] = res[0] + q; + return res; + } + + } + + + function buildCyclotomicSquare() { + const f = module.addFunction(prefix+ "__cyclotomicSquare"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const x0 = c.getLocal("x"); + const x4 = c.i32_add(c.getLocal("x"), c.i32_const(f2size)); + const x3 = c.i32_add(c.getLocal("x"), c.i32_const(2*f2size)); + const x2 = c.i32_add(c.getLocal("x"), c.i32_const(3*f2size)); + const x1 = c.i32_add(c.getLocal("x"), c.i32_const(4*f2size)); + const x5 = c.i32_add(c.getLocal("x"), c.i32_const(5*f2size)); + + const r0 = c.getLocal("r"); + const r4 = c.i32_add(c.getLocal("r"), c.i32_const(f2size)); + const r3 = c.i32_add(c.getLocal("r"), c.i32_const(2*f2size)); + const r2 = c.i32_add(c.getLocal("r"), c.i32_const(3*f2size)); + const r1 = c.i32_add(c.getLocal("r"), c.i32_const(4*f2size)); + const r5 = c.i32_add(c.getLocal("r"), c.i32_const(5*f2size)); + + const t0 = c.i32_const(module.alloc(f2size)); + const t1 = c.i32_const(module.alloc(f2size)); + const t2 = c.i32_const(module.alloc(f2size)); + const t3 = c.i32_const(module.alloc(f2size)); + const t4 = c.i32_const(module.alloc(f2size)); + const t5 = c.i32_const(module.alloc(f2size)); + const tmp = c.i32_const(module.alloc(f2size)); + const AUX = c.i32_const(module.alloc(f2size)); + + + f.addCode( + // // t0 + t1*y = (z0 + z1*y)^2 = a^2 + // tmp = z0 * z1; + // t0 = (z0 + z1) * (z0 + my_Fp6::non_residue * z1) - tmp - my_Fp6::non_residue * tmp; + // t1 = tmp + tmp; + c.call(f2mPrefix + "_mul", x0, x1, tmp), + c.call(f2mPrefix + "_mulNR", x1, t0), + c.call(f2mPrefix + "_add", x0, t0, t0), + c.call(f2mPrefix + "_add", x0, x1, AUX), + c.call(f2mPrefix + "_mul", AUX, t0, t0), + c.call(f2mPrefix + "_mulNR", tmp, AUX), + c.call(f2mPrefix + "_add", tmp, AUX, AUX), + c.call(f2mPrefix + "_sub", t0, AUX, t0), + c.call(f2mPrefix + "_add", tmp, tmp, t1), + + // // t2 + t3*y = (z2 + z3*y)^2 = b^2 + // tmp = z2 * z3; + // t2 = (z2 + z3) * (z2 + my_Fp6::non_residue * z3) - tmp - my_Fp6::non_residue * tmp; + // t3 = tmp + tmp; + c.call(f2mPrefix + "_mul", x2, x3, tmp), + c.call(f2mPrefix + "_mulNR", x3, t2), + c.call(f2mPrefix + "_add", x2, t2, t2), + c.call(f2mPrefix + "_add", x2, x3, AUX), + c.call(f2mPrefix + "_mul", AUX, t2, t2), + c.call(f2mPrefix + "_mulNR", tmp, AUX), + c.call(f2mPrefix + "_add", tmp, AUX, AUX), + c.call(f2mPrefix + "_sub", t2, AUX, t2), + c.call(f2mPrefix + "_add", tmp, tmp, t3), + + // // t4 + t5*y = (z4 + z5*y)^2 = c^2 + // tmp = z4 * z5; + // t4 = (z4 + z5) * (z4 + my_Fp6::non_residue * z5) - tmp - my_Fp6::non_residue * tmp; + // t5 = tmp + tmp; + c.call(f2mPrefix + "_mul", x4, x5, tmp), + c.call(f2mPrefix + "_mulNR", x5, t4), + c.call(f2mPrefix + "_add", x4, t4, t4), + c.call(f2mPrefix + "_add", x4, x5, AUX), + c.call(f2mPrefix + "_mul", AUX, t4, t4), + c.call(f2mPrefix + "_mulNR", tmp, AUX), + c.call(f2mPrefix + "_add", tmp, AUX, AUX), + c.call(f2mPrefix + "_sub", t4, AUX, t4), + c.call(f2mPrefix + "_add", tmp, tmp, t5), + + // For A + // z0 = 3 * t0 - 2 * z0 + c.call(f2mPrefix + "_sub", t0, x0, r0), + c.call(f2mPrefix + "_add", r0, r0, r0), + c.call(f2mPrefix + "_add", t0, r0, r0), + // z1 = 3 * t1 + 2 * z1 + c.call(f2mPrefix + "_add", t1, x1, r1), + c.call(f2mPrefix + "_add", r1, r1, r1), + c.call(f2mPrefix + "_add", t1, r1, r1), + + // For B + // z2 = 3 * (xi * t5) + 2 * z2 + c.call(f2mPrefix + "_mul", t5, c.i32_const(pBls12381Twist), AUX), + c.call(f2mPrefix + "_add", AUX, x2, r2), + c.call(f2mPrefix + "_add", r2, r2, r2), + c.call(f2mPrefix + "_add", AUX, r2, r2), + // z3 = 3 * t4 - 2 * z3 + c.call(f2mPrefix + "_sub", t4, x3, r3), + c.call(f2mPrefix + "_add", r3, r3, r3), + c.call(f2mPrefix + "_add", t4, r3, r3), + + // For C + // z4 = 3 * t2 - 2 * z4 + c.call(f2mPrefix + "_sub", t2, x4, r4), + c.call(f2mPrefix + "_add", r4, r4, r4), + c.call(f2mPrefix + "_add", t2, r4, r4), + // z5 = 3 * t3 + 2 * z5 + c.call(f2mPrefix + "_add", t3, x5, r5), + c.call(f2mPrefix + "_add", r5, r5, r5), + c.call(f2mPrefix + "_add", t3, r5, r5), + + ); + } + + + function buildCyclotomicExp(exponent, isExpNegative, fnName) { + const exponentNafBytes = naf(exponent).map( (b) => (b==-1 ? 0xFF: b) ); + const pExponentNafBytes = module.alloc(exponentNafBytes); + // const pExponent = module.alloc(utils.bigInt2BytesLE(exponent, n8)); + + const f = module.addFunction(prefix+ "__cyclotomicExp_"+fnName); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + f.addLocal("bit", "i32"); + f.addLocal("i", "i32"); + + const c = f.getCodeBuilder(); + + const x = c.getLocal("x"); + + const res = c.getLocal("r"); + + const inverse = c.i32_const(module.alloc(ftsize)); + + + f.addCode( + c.call(ftmPrefix + "_conjugate", x, inverse), + c.call(ftmPrefix + "_one", res), + + c.if( + c.teeLocal("bit", c.i32_load8_s(c.i32_const(exponentNafBytes.length-1), pExponentNafBytes)), + c.if( + c.i32_eq( + c.getLocal("bit"), + c.i32_const(1) + ), + c.call(ftmPrefix + "_mul", res, x, res), + c.call(ftmPrefix + "_mul", res, inverse, res), + ) + ), + + c.setLocal("i", c.i32_const(exponentNafBytes.length-2)), + c.block(c.loop( + c.call(prefix + "__cyclotomicSquare", res, res), + c.if( + c.teeLocal("bit", c.i32_load8_s(c.getLocal("i"), pExponentNafBytes)), + c.if( + c.i32_eq( + c.getLocal("bit"), + c.i32_const(1) + ), + c.call(ftmPrefix + "_mul", res, x, res), + c.call(ftmPrefix + "_mul", res, inverse, res), + ) + ), + c.br_if(1, c.i32_eqz ( c.getLocal("i") )), + c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))), + c.br(0) + )) + ); + + if (isExpNegative) { + f.addCode( + c.call(ftmPrefix + "_conjugate", res, res), + ); + } + + } + + function buildFinalExponentiation() { + buildCyclotomicSquare(); + buildCyclotomicExp(finalExpZ, finalExpIsNegative, "w0"); + + const f = module.addFunction(prefix+ "_finalExponentiation"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const c = f.getCodeBuilder(); + + const elt = c.getLocal("x"); + const res = c.getLocal("r"); + const t0 = c.i32_const(module.alloc(ftsize)); + const t1 = c.i32_const(module.alloc(ftsize)); + const t2 = c.i32_const(module.alloc(ftsize)); + const t3 = c.i32_const(module.alloc(ftsize)); + const t4 = c.i32_const(module.alloc(ftsize)); + const t5 = c.i32_const(module.alloc(ftsize)); + const t6 = c.i32_const(module.alloc(ftsize)); + + f.addCode( + + // let mut t0 = f.frobenius_map(6) + c.call(ftmPrefix + "_frobeniusMap6", elt, t0), + + // let t1 = f.invert() + c.call(ftmPrefix + "_inverse", elt, t1), + + // let mut t2 = t0 * t1; + c.call(ftmPrefix + "_mul", t0, t1, t2), + + // t1 = t2.clone(); + c.call(ftmPrefix + "_copy", t2, t1), + + // t2 = t2.frobenius_map().frobenius_map(); + c.call(ftmPrefix + "_frobeniusMap2", t2, t2), + + // t2 *= t1; + c.call(ftmPrefix + "_mul", t2, t1, t2), + + + // t1 = cyclotomic_square(t2).conjugate(); + c.call(prefix + "__cyclotomicSquare", t2, t1), + c.call(ftmPrefix + "_conjugate", t1, t1), + + // let mut t3 = cycolotomic_exp(t2); + c.call(prefix + "__cyclotomicExp_w0", t2, t3), + + // let mut t4 = cyclotomic_square(t3); + c.call(prefix + "__cyclotomicSquare", t3, t4), + + // let mut t5 = t1 * t3; + c.call(ftmPrefix + "_mul", t1, t3, t5), + + // t1 = cycolotomic_exp(t5); + c.call(prefix + "__cyclotomicExp_w0", t5, t1), + + // t0 = cycolotomic_exp(t1); + c.call(prefix + "__cyclotomicExp_w0", t1, t0), + + // let mut t6 = cycolotomic_exp(t0); + c.call(prefix + "__cyclotomicExp_w0", t0, t6), + + // t6 *= t4; + c.call(ftmPrefix + "_mul", t6, t4, t6), + + // t4 = cycolotomic_exp(t6); + c.call(prefix + "__cyclotomicExp_w0", t6, t4), + + // t5 = t5.conjugate(); + c.call(ftmPrefix + "_conjugate", t5, t5), + + // t4 *= t5 * t2; + c.call(ftmPrefix + "_mul", t4, t5, t4), + c.call(ftmPrefix + "_mul", t4, t2, t4), + + // t5 = t2.conjugate(); + c.call(ftmPrefix + "_conjugate", t2, t5), + + // t1 *= t2; + c.call(ftmPrefix + "_mul", t1, t2, t1), + + // t1 = t1.frobenius_map().frobenius_map().frobenius_map(); + c.call(ftmPrefix + "_frobeniusMap3", t1, t1), + + // t6 *= t5; + c.call(ftmPrefix + "_mul", t6, t5, t6), + + // t6 = t6.frobenius_map(); + c.call(ftmPrefix + "_frobeniusMap1", t6, t6), + + // t3 *= t0; + c.call(ftmPrefix + "_mul", t3, t0, t3), + + // t3 = t3.frobenius_map().frobenius_map(); + c.call(ftmPrefix + "_frobeniusMap2", t3, t3), + + // t3 *= t1; + c.call(ftmPrefix + "_mul", t3, t1, t3), + + // t3 *= t6; + c.call(ftmPrefix + "_mul", t3, t6, t3), + + // f = t3 * t4; + c.call(ftmPrefix + "_mul", t3, t4, res), + + ); + } + + + function buildFinalExponentiationOld() { + const f = module.addFunction(prefix+ "_finalExponentiationOld"); + f.addParam("x", "i32"); + f.addParam("r", "i32"); + + const exponent = 322277361516934140462891564586510139908379969514828494218366688025288661041104682794998680497580008899973249814104447692778988208376779573819485263026159588510513834876303014016798809919343532899164848730280942609956670917565618115867287399623286813270357901731510188149934363360381614501334086825442271920079363289954510565375378443704372994881406797882676971082200626541916413184642520269678897559532260949334760604962086348898118982248842634379637598665468817769075878555493752214492790122785850202957575200176084204422751485957336465472324810982833638490904279282696134323072515220044451592646885410572234451732790590013479358343841220074174848221722017083597872017638514103174122784843925578370430843522959600095676285723737049438346544753168912974976791528535276317256904336520179281145394686565050419250614107803233314658825463117900250701199181529205942363159325765991819433914303908860460720581408201373164047773794825411011922305820065611121544561808414055302212057471395719432072209245600258134364584636810093520285711072578721435517884103526483832733289802426157301542744476740008494780363354305116978805620671467071400711358839553375340724899735460480144599782014906586543813292157922220645089192130209334926661588737007768565838519456601560804957985667880395221049249803753582637708560n; + + const pExponent = module.alloc(utils$7.bigInt2BytesLE( exponent, 544 )); + + const c = f.getCodeBuilder(); + + f.addCode( + c.call(ftmPrefix + "_exp", c.getLocal("x"), c.i32_const(pExponent), c.i32_const(544), c.getLocal("r")), + ); + } + + + const pPreP = module.alloc(prePSize); + const pPreQ = module.alloc(preQSize); + + function buildPairingEquation(nPairings) { + + const f = module.addFunction(prefix+ "_pairingEq"+nPairings); + for (let i=0; i. + */ + + // module.exports.bn128_wasm = require("./build/bn128_wasm.js"); + // module.exports.bls12381_wasm = require("./build/bls12381_wasm.js"); + // module.exports.mnt6753_wasm = require("./build/mnt6753_wasm.js"); + + var buildBn128$3 = build_bn128$1; + var buildBls12381$3 = build_bls12381$1; + + /* global BigInt */ + + function leInt2Buff$1(n, len) { + let r = n; + if (typeof len === "undefined") { + len = Math.floor((bitLength$c(n) - 1) / 8) + 1; + if (len == 0) len = 1; + } + const buff = new Uint8Array(len); + const buffV = new DataView(buff.buffer); + let o = 0; + while (o < len) { + if (o + 4 <= len) { + buffV.setUint32(o, Number(r & BigInt(0xffffffff)), true); + o += 4; + r = r >> BigInt(32); + } else if (o + 2 <= len) { + buffV.setUint16(o, Number(r & BigInt(0xffff)), true); + o += 2; + r = r >> BigInt(16); + } else { + buffV.setUint8(o, Number(r & BigInt(0xff)), true); + o += 1; + r = r >> BigInt(8); + } + } + if (r) { + throw new Error("Number does not fit in this length"); + } + return buff; + } + + const _revTable$1 = []; + for (let i = 0; i < 256; i++) { + _revTable$1[i] = _revSlow$1(i, 8); + } + + function _revSlow$1(idx, bits) { + let res = 0; + let a = idx; + for (let i = 0; i < bits; i++) { + res <<= 1; + res = res | (a & 1); + a >>= 1; + } + return res; + } + + function bitReverse$1(idx, bits) { + return ( + (_revTable$1[idx >>> 24] | + (_revTable$1[(idx >>> 16) & 0xff] << 8) | + (_revTable$1[(idx >>> 8) & 0xff] << 16) | + (_revTable$1[idx & 0xff] << 24)) >>> + (32 - bits) + ); + } + + function log2$3(V) { + return ( + ((V & 0xffff0000) !== 0 ? ((V &= 0xffff0000), 16) : 0) | + ((V & 0xff00ff00) !== 0 ? ((V &= 0xff00ff00), 8) : 0) | + ((V & 0xf0f0f0f0) !== 0 ? ((V &= 0xf0f0f0f0), 4) : 0) | + ((V & 0xcccccccc) !== 0 ? ((V &= 0xcccccccc), 2) : 0) | + ((V & 0xaaaaaaaa) !== 0) + ); + } + + function buffReverseBits$1(buff, eSize) { + const n = buff.byteLength / eSize; + const bits = log2$3(n); + if (n != 1 << bits) { + throw new Error("Invalid number of pointers"); + } + for (let i = 0; i < n; i++) { + const r = bitReverse$1(i, bits); + if (i > r) { + const tmp = buff.slice(i * eSize, (i + 1) * eSize); + buff.set(buff.slice(r * eSize, (r + 1) * eSize), i * eSize); + buff.set(tmp, r * eSize); + } + } + } + + function array2buffer$1(arr, sG) { + const buff = new Uint8Array(sG * arr.length); + + for (let i = 0; i < arr.length; i++) { + buff.set(arr[i], i * sG); + } + + return buff; + } + + function buffer2array$1(buff, sG) { + const n = buff.byteLength / sG; + const arr = new Array(n); + for (let i = 0; i < n; i++) { + arr[i] = buff.slice(i * sG, i * sG + sG); + } + return arr; + } + + const PAGE_SIZE$2 = 1<<30; + + class BigBuffer$1 { + + constructor(size) { + this.buffers = []; + this.byteLength = size; + for (let i=0; i0) { + // bytes to copy from this page + const l = (o+r > PAGE_SIZE$2) ? (PAGE_SIZE$2 -o) : r; + const srcView = new Uint8Array(this.buffers[p].buffer, this.buffers[p].byteOffset+o, l); + if (l == len) return srcView.slice(); + if (!buff) { + if (len <= PAGE_SIZE$2) { + buff = new Uint8Array(len); + } else { + buff = new BigBuffer$1(len); + } + } + buff.set(srcView, len-r); + r = r-l; + p ++; + o = 0; + } + + return buff; + } + + set(buff, offset) { + if (offset === undefined) offset = 0; + + const len = buff.byteLength; + + if (len==0) return; + + const firstPage = Math.floor(offset / PAGE_SIZE$2); + const lastPage = Math.floor((offset+len-1) / PAGE_SIZE$2); + + if (firstPage == lastPage) { + if ((buff instanceof BigBuffer$1)&&(buff.buffers.length==1)) { + return this.buffers[firstPage].set(buff.buffers[0], offset % PAGE_SIZE$2); + } else { + return this.buffers[firstPage].set(buff, offset % PAGE_SIZE$2); + } + + } + + + let p = firstPage; + let o = offset % PAGE_SIZE$2; + let r = len; + while (r>0) { + const l = (o+r > PAGE_SIZE$2) ? (PAGE_SIZE$2 -o) : r; + const srcView = buff.slice( len -r, len -r+l); + const dstView = new Uint8Array(this.buffers[p].buffer, this.buffers[p].byteOffset + o, l); + dstView.set(srcView); + r = r-l; + p ++; + o = 0; + } + + } + } + + function buildBatchConvert$1(tm, fnName, sIn, sOut) { + return async function batchConvert(buffIn) { + const nPoints = Math.floor(buffIn.byteLength / sIn); + if ( nPoints * sIn !== buffIn.byteLength) { + throw new Error("Invalid buffer size"); + } + const pointsPerChunk = Math.floor(nPoints/tm.concurrency); + const opPromises = []; + for (let i=0; i=0; i--) { + this.w[i] = this.square(this.w[i+1]); + } + + if (!this.eq(this.w[0], this.one)) { + throw new Error("Error calculating roots of unity"); + } + + this.batchToMontgomery = buildBatchConvert$1(tm, prefix + "_batchToMontgomery", this.n8, this.n8); + this.batchFromMontgomery = buildBatchConvert$1(tm, prefix + "_batchFromMontgomery", this.n8, this.n8); + } + + + op2(opName, a, b) { + this.tm.setBuff(this.pOp1, a); + this.tm.setBuff(this.pOp2, b); + this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2, this.pOp3); + return this.tm.getBuff(this.pOp3, this.n8); + } + + op2Bool(opName, a, b) { + this.tm.setBuff(this.pOp1, a); + this.tm.setBuff(this.pOp2, b); + return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2); + } + + op1(opName, a) { + this.tm.setBuff(this.pOp1, a); + this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3); + return this.tm.getBuff(this.pOp3, this.n8); + } + + op1Bool(opName, a) { + this.tm.setBuff(this.pOp1, a); + return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3); + } + + add(a,b) { + return this.op2("_add", a, b); + } + + + eq(a,b) { + return this.op2Bool("_eq", a, b); + } + + isZero(a) { + return this.op1Bool("_isZero", a); + } + + sub(a,b) { + return this.op2("_sub", a, b); + } + + neg(a) { + return this.op1("_neg", a); + } + + inv(a) { + return this.op1("_inverse", a); + } + + toMontgomery(a) { + return this.op1("_toMontgomery", a); + } + + fromMontgomery(a) { + return this.op1("_fromMontgomery", a); + } + + mul(a,b) { + return this.op2("_mul", a, b); + } + + div(a, b) { + this.tm.setBuff(this.pOp1, a); + this.tm.setBuff(this.pOp2, b); + this.tm.instance.exports[this.prefix + "_inverse"](this.pOp2, this.pOp2); + this.tm.instance.exports[this.prefix + "_mul"](this.pOp1, this.pOp2, this.pOp3); + return this.tm.getBuff(this.pOp3, this.n8); + } + + square(a) { + return this.op1("_square", a); + } + + isSquare(a) { + return this.op1Bool("_isSquare", a); + } + + sqrt(a) { + return this.op1("_sqrt", a); + } + + exp(a, b) { + if (!(b instanceof Uint8Array)) { + b = toLEBuff$1(e$1(b)); + } + this.tm.setBuff(this.pOp1, a); + this.tm.setBuff(this.pOp2, b); + this.tm.instance.exports[this.prefix + "_exp"](this.pOp1, this.pOp2, b.byteLength, this.pOp3); + return this.tm.getBuff(this.pOp3, this.n8); + } + + isNegative(a) { + return this.op1Bool("_isNegative", a); + } + + e(a, b) { + if (a instanceof Uint8Array) return a; + let ra = e$1(a, b); + if (isNegative$8(ra)) { + ra = neg$1(ra); + if (gt$1(ra, this.p)) { + ra = mod$1(ra, this.p); + } + ra = sub$1(this.p, ra); + } else { + if (gt$1(ra, this.p)) { + ra = mod$1(ra, this.p); + } + } + const buff = leInt2Buff$1(ra, this.n8); + return this.toMontgomery(buff); + } + + toString(a, radix) { + const an = this.fromMontgomery(a); + const s = fromRprLE$1(an, 0); + return toString$6(s, radix); + } + + fromRng(rng) { + let v; + const buff = new Uint8Array(this.n8); + do { + v = zero$1; + for (let i=0; i memory.buffer.byteLength) { + const currentPages = memory.buffer.byteLength / 0x10000; + let requiredPages = Math.floor((u32[0] + length) / 0x10000)+1; + if (requiredPages>MAXMEM) requiredPages=MAXMEM; + memory.grow(requiredPages-currentPages); + } + return res; + } + + function allocBuffer(buffer) { + const p = alloc(buffer.byteLength); + setBuffer(p, buffer); + return p; + } + + function getBuffer(pointer, length) { + const u8 = new Uint8Array(memory.buffer); + return new Uint8Array(u8.buffer, u8.byteOffset + pointer, length); + } + + function setBuffer(pointer, buffer) { + const u8 = new Uint8Array(memory.buffer); + u8.set(new Uint8Array(buffer), pointer); + } + + function runTask(task) { + if (task[0].cmd == "INIT") { + return init(task[0]); + } + const ctx = { + vars: [], + out: [] + }; + const u32a = new Uint32Array(memory.buffer, 0, 1); + const oldAlloc = u32a[0]; + for (let i=0; i. + */ + + // const MEM_SIZE = 1000; // Memory size in 64K Pakes (512Mb) + const MEM_SIZE$1 = 25; // Memory size in 64K Pakes (1600Kb) + + class Deferred$1 { + constructor() { + this.promise = new Promise((resolve, reject)=> { + this.reject = reject; + this.resolve = resolve; + }); + } + } + + function sleep$1(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + + function stringToBase64$1(str) { + { + return globalThis.btoa(str); + } + } + + const threadSource$1 = stringToBase64$1("(" + thread$1.toString() + ")(self)"); + const workerSource$1 = "data:application/javascript;base64," + threadSource$1; + + + + async function buildThreadManager$1(wasm, singleThread) { + const tm = new ThreadManager$1(); + + tm.memory = new WebAssembly.Memory({initial:MEM_SIZE$1}); + tm.u8 = new Uint8Array(tm.memory.buffer); + tm.u32 = new Uint32Array(tm.memory.buffer); + + const wasmModule = await WebAssembly.compile(wasm.code); + + tm.instance = await WebAssembly.instantiate(wasmModule, { + env: { + "memory": tm.memory + } + }); + + tm.singleThread = singleThread; + tm.initalPFree = tm.u32[0]; // Save the Pointer to free space. + tm.pq = wasm.pq; + tm.pr = wasm.pr; + tm.pG1gen = wasm.pG1gen; + tm.pG1zero = wasm.pG1zero; + tm.pG2gen = wasm.pG2gen; + tm.pG2zero = wasm.pG2zero; + tm.pOneT = wasm.pOneT; + + // tm.pTmp0 = tm.alloc(curve.G2.F.n8*3); + // tm.pTmp1 = tm.alloc(curve.G2.F.n8*3); + + + if (singleThread) { + tm.code = wasm.code; + tm.taskManager = thread$1(); + await tm.taskManager([{ + cmd: "INIT", + init: MEM_SIZE$1, + code: tm.code.slice() + }]); + tm.concurrency = 1; + } else { + tm.workers = []; + tm.pendingDeferreds = []; + tm.working = []; + + let concurrency; + + if ((typeof(navigator) === "object") && navigator.hardwareConcurrency) { + concurrency = navigator.hardwareConcurrency; + } else { + concurrency = os.cpus().length; + } + + if(concurrency == 0){ + concurrency = 2; + } + + // Limit to 64 threads for memory reasons. + if (concurrency>64) concurrency=64; + tm.concurrency = concurrency; + + for (let i = 0; i 0); i++) { + if (this.working[i] == false) { + const work = this.actionQueue.shift(); + this.postAction(i, work.data, work.transfers, work.deferred); + } + } + } + + queueAction(actionData, transfers) { + const d = new Deferred$1(); + + if (this.singleThread) { + const res = this.taskManager(actionData); + d.resolve(res); + } else { + this.actionQueue.push({ + data: actionData, + transfers: transfers, + deferred: d + }); + this.processWorks(); + } + return d.promise; + } + + resetMemory() { + this.u32[0] = this.initalPFree; + } + + allocBuff(buff) { + const pointer = this.alloc(buff.byteLength); + this.setBuff(pointer, buff); + return pointer; + } + + getBuff(pointer, length) { + return this.u8.slice(pointer, pointer+ length); + } + + setBuff(pointer, buffer) { + this.u8.set(new Uint8Array(buffer), pointer); + } + + alloc(length) { + while (this.u32[0] & 3) this.u32[0]++; // Return always aligned pointers + const res = this.u32[0]; + this.u32[0] += length; + return res; + } + + async terminate() { + for (let i=0; i=0; i--) { + if (!G.isZero(res)) { + for (let j=0; jMAX_CHUNK_SIZE) chunkSize = MAX_CHUNK_SIZE; + if (chunkSize { + if (logger) logger.debug(`Multiexp end: ${logText}: ${i}/${nPoints}`); + return r; + })); + } + + const result = await Promise.all(opPromises); + + let res = G.zero; + for (let i=result.length-1; i>=0; i--) { + res = G.add(res, result[i]); + } + + return res; + } + + G.multiExp = async function multiExpAffine(buffBases, buffScalars, logger, logText) { + return await _multiExp(buffBases, buffScalars, "jacobian", logger, logText); + }; + G.multiExpAffine = async function multiExpAffine(buffBases, buffScalars, logger, logText) { + return await _multiExp(buffBases, buffScalars, "affine", logger, logText); + }; + } + + function buildFFT$3(curve, groupName) { + const G = curve[groupName]; + const Fr = curve.Fr; + const tm = G.tm; + async function _fft(buff, inverse, inType, outType, logger, loggerTxt) { + + inType = inType || "affine"; + outType = outType || "affine"; + const MAX_BITS_THREAD = 14; + + let sIn, sMid, sOut, fnIn2Mid, fnMid2Out, fnFFTMix, fnFFTJoin, fnFFTFinal; + if (groupName == "G1") { + if (inType == "affine") { + sIn = G.F.n8*2; + fnIn2Mid = "g1m_batchToJacobian"; + } else { + sIn = G.F.n8*3; + } + sMid = G.F.n8*3; + if (inverse) { + fnFFTFinal = "g1m_fftFinal"; + } + fnFFTJoin = "g1m_fftJoin"; + fnFFTMix = "g1m_fftMix"; + + if (outType == "affine") { + sOut = G.F.n8*2; + fnMid2Out = "g1m_batchToAffine"; + } else { + sOut = G.F.n8*3; + } + + } else if (groupName == "G2") { + if (inType == "affine") { + sIn = G.F.n8*2; + fnIn2Mid = "g2m_batchToJacobian"; + } else { + sIn = G.F.n8*3; + } + sMid = G.F.n8*3; + if (inverse) { + fnFFTFinal = "g2m_fftFinal"; + } + fnFFTJoin = "g2m_fftJoin"; + fnFFTMix = "g2m_fftMix"; + if (outType == "affine") { + sOut = G.F.n8*2; + fnMid2Out = "g2m_batchToAffine"; + } else { + sOut = G.F.n8*3; + } + } else if (groupName == "Fr") { + sIn = G.n8; + sMid = G.n8; + sOut = G.n8; + if (inverse) { + fnFFTFinal = "frm_fftFinal"; + } + fnFFTMix = "frm_fftMix"; + fnFFTJoin = "frm_fftJoin"; + } + + + let returnArray = false; + if (Array.isArray(buff)) { + buff = array2buffer$1(buff, sIn); + returnArray = true; + } else { + buff = buff.slice(0, buff.byteLength); + } + + const nPoints = buff.byteLength / sIn; + const bits = log2$3(nPoints); + + if ((1 << bits) != nPoints) { + throw new Error("fft must be multiple of 2" ); + } + + if (bits == Fr.s +1) { + let buffOut; + + if (inverse) { + buffOut = await _fftExtInv(buff, inType, outType, logger, loggerTxt); + } else { + buffOut = await _fftExt(buff, inType, outType, logger, loggerTxt); + } + + if (returnArray) { + return buffer2array$1(buffOut, sOut); + } else { + return buffOut; + } + } + + let inv; + if (inverse) { + inv = Fr.inv(Fr.e(nPoints)); + } + + let buffOut; + + buffReverseBits$1(buff, sIn); + + let chunks; + let pointsInChunk = Math.min(1 << MAX_BITS_THREAD, nPoints); + let nChunks = nPoints / pointsInChunk; + + while ((nChunks < tm.concurrency)&&(pointsInChunk>=16)) { + nChunks *= 2; + pointsInChunk /= 2; + } + + const l2Chunk = log2$3(pointsInChunk); + + const promises = []; + for (let i = 0; i< nChunks; i++) { + if (logger) logger.debug(`${loggerTxt}: fft ${bits} mix start: ${i}/${nChunks}`); + const task = []; + task.push({cmd: "ALLOC", var: 0, len: sMid*pointsInChunk}); + const buffChunk = buff.slice( (pointsInChunk * i)*sIn, (pointsInChunk * (i+1))*sIn); + task.push({cmd: "SET", var: 0, buff: buffChunk}); + if (fnIn2Mid) { + task.push({cmd: "CALL", fnName:fnIn2Mid, params: [{var:0}, {val: pointsInChunk}, {var: 0}]}); + } + for (let j=1; j<=l2Chunk;j++) { + task.push({cmd: "CALL", fnName:fnFFTMix, params: [{var:0}, {val: pointsInChunk}, {val: j}]}); + } + + if (l2Chunk==bits) { + if (fnFFTFinal) { + task.push({cmd: "ALLOCSET", var: 1, buff: inv}); + task.push({cmd: "CALL", fnName: fnFFTFinal, params:[ + {var: 0}, + {val: pointsInChunk}, + {var: 1}, + ]}); + } + if (fnMid2Out) { + task.push({cmd: "CALL", fnName:fnMid2Out, params: [{var:0}, {val: pointsInChunk}, {var: 0}]}); + } + task.push({cmd: "GET", out: 0, var: 0, len: pointsInChunk*sOut}); + } else { + task.push({cmd: "GET", out:0, var: 0, len: sMid*pointsInChunk}); + } + promises.push(tm.queueAction(task).then( (r) => { + if (logger) logger.debug(`${loggerTxt}: fft ${bits} mix end: ${i}/${nChunks}`); + return r; + })); + } + + chunks = await Promise.all(promises); + for (let i = 0; i< nChunks; i++) chunks[i] = chunks[i][0]; + + for (let i = l2Chunk+1; i<=bits; i++) { + if (logger) logger.debug(`${loggerTxt}: fft ${bits} join: ${i}/${bits}`); + const nGroups = 1 << (bits - i); + const nChunksPerGroup = nChunks / nGroups; + const opPromises = []; + for (let j=0; j { + if (logger) logger.debug(`${loggerTxt}: fft ${bits} join ${i}/${bits} ${j+1}/${nGroups} ${k}/${nChunksPerGroup/2}`); + return r; + })); + } + } + + const res = await Promise.all(opPromises); + for (let j=0; j0; i--) { + buffOut.set(chunks[i], p); + p += pointsInChunk*sOut; + delete chunks[i]; // Liberate mem + } + buffOut.set(chunks[0].slice(0, (pointsInChunk-1)*sOut), p); + delete chunks[0]; + } else { + for (let i=0; i (1<<28)) { + buffOut = new BigBuffer$1(res1[0].byteLength*2); + } else { + buffOut = new Uint8Array(res1[0].byteLength*2); + } + + buffOut.set(res1[0]); + buffOut.set(res1[1], res1[0].byteLength); + + return buffOut; + } + + async function _fftExtInv(buff, inType, outType, logger, loggerTxt) { + let b1, b2; + b1 = buff.slice( 0 , buff.byteLength/2); + b2 = buff.slice( buff.byteLength/2, buff.byteLength); + + const promises = []; + + promises.push( _fft(b1, true, inType, "jacobian", logger, loggerTxt)); + promises.push( _fft(b2, true, inType, "jacobian", logger, loggerTxt)); + + [b1, b2] = await Promise.all(promises); + + const res1 = await _fftJoinExt(b1, b2, "fftJoinExtInv", Fr.one, Fr.shiftInv, "jacobian", outType, logger, loggerTxt); + + let buffOut; + if (res1[0].byteLength > (1<<28)) { + buffOut = new BigBuffer$1(res1[0].byteLength*2); + } else { + buffOut = new Uint8Array(res1[0].byteLength*2); + } + + buffOut.set(res1[0]); + buffOut.set(res1[1], res1[0].byteLength); + + return buffOut; + } + + + async function _fftJoinExt(buff1, buff2, fn, first, inc, inType, outType, logger, loggerTxt) { + const MAX_CHUNK_SIZE = 1<<16; + const MIN_CHUNK_SIZE = 1<<4; + + let fnName; + let fnIn2Mid, fnMid2Out; + let sOut, sIn, sMid; + + if (groupName == "G1") { + if (inType == "affine") { + sIn = G.F.n8*2; + fnIn2Mid = "g1m_batchToJacobian"; + } else { + sIn = G.F.n8*3; + } + sMid = G.F.n8*3; + fnName = "g1m_"+fn; + if (outType == "affine") { + fnMid2Out = "g1m_batchToAffine"; + sOut = G.F.n8*2; + } else { + sOut = G.F.n8*3; + } + } else if (groupName == "G2") { + if (inType == "affine") { + sIn = G.F.n8*2; + fnIn2Mid = "g2m_batchToJacobian"; + } else { + sIn = G.F.n8*3; + } + fnName = "g2m_"+fn; + sMid = G.F.n8*3; + if (outType == "affine") { + fnMid2Out = "g2m_batchToAffine"; + sOut = G.F.n8*2; + } else { + sOut = G.F.n8*3; + } + } else if (groupName == "Fr") { + sIn = Fr.n8; + sOut = Fr.n8; + sMid = Fr.n8; + fnName = "frm_" + fn; + } else { + throw new Error("Invalid group"); + } + + if (buff1.byteLength != buff2.byteLength) { + throw new Error("Invalid buffer size"); + } + const nPoints = Math.floor(buff1.byteLength / sIn); + if (nPoints != 1 << log2$3(nPoints)) { + throw new Error("Invalid number of points"); + } + + let chunkSize = Math.floor(nPoints /tm.concurrency); + if (chunkSize < MIN_CHUNK_SIZE) chunkSize = MIN_CHUNK_SIZE; + if (chunkSize > MAX_CHUNK_SIZE) chunkSize = MAX_CHUNK_SIZE; + + const opPromises = []; + + for (let i=0; i { + if (logger) logger.debug(`${loggerTxt}: fftJoinExt End: ${i}/${nPoints}`); + return r; + }) + ); + } + + const result = await Promise.all(opPromises); + + let fullBuffOut1; + let fullBuffOut2; + if (nPoints * sOut > 1<<28) { + fullBuffOut1 = new BigBuffer$1(nPoints*sOut); + fullBuffOut2 = new BigBuffer$1(nPoints*sOut); + } else { + fullBuffOut1 = new Uint8Array(nPoints*sOut); + fullBuffOut2 = new Uint8Array(nPoints*sOut); + } + + let p =0; + for (let i=0; i Fr.s+1) { + if (logger) logger.error("lagrangeEvaluations input too big"); + throw new Error("lagrangeEvaluations input too big"); + } + + let t0 = buff.slice(0, buff.byteLength/2); + let t1 = buff.slice(buff.byteLength/2, buff.byteLength); + + + const shiftToSmallM = Fr.exp(Fr.shift, nPoints/2); + const sConst = Fr.inv( Fr.sub(Fr.one, shiftToSmallM)); + + [t0, t1] = await _fftJoinExt(t0, t1, "prepareLagrangeEvaluation", sConst, Fr.shiftInv, inType, "jacobian", logger, loggerTxt + " prep"); + + const promises = []; + + promises.push( _fft(t0, true, "jacobian", outType, logger, loggerTxt + " t0")); + promises.push( _fft(t1, true, "jacobian", outType, logger, loggerTxt + " t1")); + + [t0, t1] = await Promise.all(promises); + + let buffOut; + if (t0.byteLength > (1<<28)) { + buffOut = new BigBuffer$1(t0.byteLength*2); + } else { + buffOut = new Uint8Array(t0.byteLength*2); + } + + buffOut.set(t0); + buffOut.set(t1, t0.byteLength); + + return buffOut; + }; + + G.fftMix = async function fftMix(buff) { + const sG = G.F.n8*3; + let fnName, fnFFTJoin; + if (groupName == "G1") { + fnName = "g1m_fftMix"; + fnFFTJoin = "g1m_fftJoin"; + } else if (groupName == "G2") { + fnName = "g2m_fftMix"; + fnFFTJoin = "g2m_fftJoin"; + } else if (groupName == "Fr") { + fnName = "frm_fftMix"; + fnFFTJoin = "frm_fftJoin"; + } else { + throw new Error("Invalid group"); + } + + const nPoints = Math.floor(buff.byteLength / sG); + const power = log2$3(nPoints); + + let nChunks = 1 << log2$3(tm.concurrency); + + if (nPoints <= nChunks*2) nChunks = 1; + + const pointsPerChunk = nPoints / nChunks; + + const powerChunk = log2$3(pointsPerChunk); + + const opPromises = []; + for (let i=0; i=0; i--) { + fullBuffOut.set(result[i][0], p); + p+=result[i][0].byteLength; + } + + return fullBuffOut; + }; + } + + async function buildEngine$1(params) { + + const tm = await buildThreadManager$1(params.wasm, params.singleThread); + + + const curve = {}; + + curve.q = e$1(params.wasm.q.toString()); + curve.r = e$1(params.wasm.r.toString()); + curve.name = params.name; + curve.tm = tm; + curve.prePSize = params.wasm.prePSize; + curve.preQSize = params.wasm.preQSize; + curve.Fr = new WasmField1$1(tm, "frm", params.n8r, params.r); + curve.F1 = new WasmField1$1(tm, "f1m", params.n8q, params.q); + curve.F2 = new WasmField2$1(tm, "f2m", curve.F1); + curve.G1 = new WasmCurve$1(tm, "g1m", curve.F1, params.wasm.pG1gen, params.wasm.pG1b, params.cofactorG1); + curve.G2 = new WasmCurve$1(tm, "g2m", curve.F2, params.wasm.pG2gen, params.wasm.pG2b, params.cofactorG2); + curve.F6 = new WasmField3$1(tm, "f6m", curve.F2); + curve.F12 = new WasmField2$1(tm, "ftm", curve.F6); + + curve.Gt = curve.F12; + + buildBatchApplyKey$1(curve, "G1"); + buildBatchApplyKey$1(curve, "G2"); + buildBatchApplyKey$1(curve, "Fr"); + + buildMultiexp$2(curve, "G1"); + buildMultiexp$2(curve, "G2"); + + buildFFT$3(curve, "G1"); + buildFFT$3(curve, "G2"); + buildFFT$3(curve, "Fr"); + + buildPairing$1(curve); + + curve.array2buffer = function(arr, sG) { + const buff = new Uint8Array(sG*arr.length); + + for (let i=0; i. + */ + + function toNumber$1(n) { + return BigInt(n); + } + + function isNegative$4(n) { + return n < 0n; + } + + function isZero$1(n) { + return n === 0n; + } + + function bitLength$6(n) { + if (isNegative$4(n)) { + return n.toString(2).length - 1; // discard the - sign + } else { + return n.toString(2).length; + } + } + + function u32(n) { + const b = []; + const v = toNumber$1(n); + b.push(Number(v & 0xFFn)); + b.push(Number(v >> 8n & 0xFFn)); + b.push(Number(v >> 16n & 0xFFn)); + b.push(Number(v >> 24n & 0xFFn)); + return b; + } + + function toUTF8Array(str) { + var utf8 = []; + for (var i=0; i < str.length; i++) { + var charcode = str.charCodeAt(i); + if (charcode < 0x80) utf8.push(charcode); + else if (charcode < 0x800) { + utf8.push(0xc0 | (charcode >> 6), + 0x80 | (charcode & 0x3f)); + } + else if (charcode < 0xd800 || charcode >= 0xe000) { + utf8.push(0xe0 | (charcode >> 12), + 0x80 | ((charcode>>6) & 0x3f), + 0x80 | (charcode & 0x3f)); + } + // surrogate pair + else { + i++; + // UTF-16 encodes 0x10000-0x10FFFF by + // subtracting 0x10000 and splitting the + // 20 bits of 0x0-0xFFFFF into two halves + charcode = 0x10000 + (((charcode & 0x3ff)<<10) + | (str.charCodeAt(i) & 0x3ff)); + utf8.push(0xf0 | (charcode >>18), + 0x80 | ((charcode>>12) & 0x3f), + 0x80 | ((charcode>>6) & 0x3f), + 0x80 | (charcode & 0x3f)); + } + } + return utf8; + } + + function string(str) { + const bytes = toUTF8Array(str); + return [ ...varuint32(bytes.length), ...bytes ]; + } + + function varuint(n) { + const code = []; + let v = toNumber$1(n); + if (isNegative$4(v)) throw new Error("Number cannot be negative"); + while (!isZero$1(v)) { + code.push(Number(v & 0x7Fn)); + v = v >> 7n; + } + if (code.length==0) code.push(0); + for (let i=0; i 0xFFFFFFFFn) throw new Error("Number too big"); + if (v > 0x7FFFFFFFn) v = v - 0x100000000n; + // bigInt("-80000000", 16) as base10 + if (v < -2147483648n) throw new Error("Number too small"); + return varint(v); + } + + function varint64(n) { + let v = toNumber$1(n); + if (v > 0xFFFFFFFFFFFFFFFFn) throw new Error("Number too big"); + if (v > 0x7FFFFFFFFFFFFFFFn) v = v - 0x10000000000000000n; + // bigInt("-8000000000000000", 16) as base10 + if (v < -9223372036854775808n) throw new Error("Number too small"); + return varint(v); + } + + function varuint32(n) { + let v = toNumber$1(n); + if (v > 0xFFFFFFFFn) throw new Error("Number too big"); + return varuint(v); + } + + function toHexString(byteArray) { + return Array.from(byteArray, function(byte) { + return ("0" + (byte & 0xFF).toString(16)).slice(-2); + }).join(""); + } + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmbuilder + + wasmbuilder is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmbuilder is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmbuilder. If not, see . + */ + + class CodeBuilder { + constructor(func) { + this.func = func; + this.functionName = func.functionName; + this.module = func.module; + } + + setLocal(localName, valCode) { + const idx = this.func.localIdxByName[localName]; + if (idx === undefined) + throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${localName} `); + return [...valCode, 0x21, ...varuint32( idx )]; + } + + teeLocal(localName, valCode) { + const idx = this.func.localIdxByName[localName]; + if (idx === undefined) + throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${localName} `); + return [...valCode, 0x22, ...varuint32( idx )]; + } + + getLocal(localName) { + const idx = this.func.localIdxByName[localName]; + if (idx === undefined) + throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${localName} `); + return [0x20, ...varuint32( idx )]; + } + + i64_load8_s(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 0 : _align; // 8 bits alignment by default + return [...idxCode, 0x30, align, ...varuint32(offset)]; + } + + i64_load8_u(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 0 : _align; // 8 bits alignment by default + return [...idxCode, 0x31, align, ...varuint32(offset)]; + } + + i64_load16_s(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 1 : _align; // 16 bits alignment by default + return [...idxCode, 0x32, align, ...varuint32(offset)]; + } + + i64_load16_u(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 1 : _align; // 16 bits alignment by default + return [...idxCode, 0x33, align, ...varuint32(offset)]; + } + + i64_load32_s(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 2 : _align; // 32 bits alignment by default + return [...idxCode, 0x34, align, ...varuint32(offset)]; + } + + i64_load32_u(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 2 : _align; // 32 bits alignment by default + return [...idxCode, 0x35, align, ...varuint32(offset)]; + } + + i64_load(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 3 : _align; // 64 bits alignment by default + return [...idxCode, 0x29, align, ...varuint32(offset)]; + } + + + i64_store(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 3; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 3; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x37, align, ...varuint32(offset)]; + } + + i64_store32(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 2; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 2; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x3e, align, ...varuint32(offset)]; + } + + + i64_store16(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 1; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 1; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x3d, align, ...varuint32(offset)]; + } + + + i64_store8(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 0; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 0; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x3c, align, ...varuint32(offset)]; + } + + i32_load8_s(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 0 : _align; // 32 bits alignment by default + return [...idxCode, 0x2c, align, ...varuint32(offset)]; + } + + i32_load8_u(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 0 : _align; // 32 bits alignment by default + return [...idxCode, 0x2d, align, ...varuint32(offset)]; + } + + i32_load16_s(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 1 : _align; // 32 bits alignment by default + return [...idxCode, 0x2e, align, ...varuint32(offset)]; + } + + i32_load16_u(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 1 : _align; // 32 bits alignment by default + return [...idxCode, 0x2f, align, ...varuint32(offset)]; + } + + i32_load(idxCode, _offset, _align) { + const offset = _offset || 0; + const align = (_align === undefined) ? 2 : _align; // 32 bits alignment by default + return [...idxCode, 0x28, align, ...varuint32(offset)]; + } + + i32_store(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 2; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 2; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x36, align, ...varuint32(offset)]; + } + + + i32_store16(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 1; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 1; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x3b, align, ...varuint32(offset)]; + } + + i32_store8(idxCode, _offset, _align, _codeVal) { + let offset, align, codeVal; + if (Array.isArray(_offset)) { + offset = 0; + align = 0; + codeVal = _offset; + } else if (Array.isArray(_align)) { + offset = _offset; + align = 0; + codeVal = _align; + } else if (Array.isArray(_codeVal)) { + offset = _offset; + align = _align; + codeVal = _codeVal; + } + return [...idxCode, ...codeVal, 0x3a, align, ...varuint32(offset)]; + } + + call(fnName, ...args) { + const idx = this.module.functionIdxByName[fnName]; + if (idx === undefined) + throw new Error(`Function not defined: Function: ${fnName}`); + return [...[].concat(...args), 0x10, ...varuint32(idx)]; + } + + call_indirect(fnIdx, ...args) { + return [...[].concat(...args), ...fnIdx, 0x11, 0, 0]; + } + + if(condCode, thenCode, elseCode) { + if (elseCode) { + return [...condCode, 0x04, 0x40, ...thenCode, 0x05, ...elseCode, 0x0b]; + } else { + return [...condCode, 0x04, 0x40, ...thenCode, 0x0b]; + } + } + + block(bCode) { return [0x02, 0x40, ...bCode, 0x0b]; } + loop(...args) { + return [0x03, 0x40, ...[].concat(...[...args]), 0x0b]; + } + br_if(relPath, condCode) { return [...condCode, 0x0d, ...varuint32(relPath)]; } + br(relPath) { return [0x0c, ...varuint32(relPath)]; } + ret(rCode) { return [...rCode, 0x0f]; } + drop(dCode) { return [...dCode, 0x1a]; } + + i64_const(num) { return [0x42, ...varint64(num)]; } + i32_const(num) { return [0x41, ...varint32(num)]; } + + + i64_eqz(opcode) { return [...opcode, 0x50]; } + i64_eq(op1code, op2code) { return [...op1code, ...op2code, 0x51]; } + i64_ne(op1code, op2code) { return [...op1code, ...op2code, 0x52]; } + i64_lt_s(op1code, op2code) { return [...op1code, ...op2code, 0x53]; } + i64_lt_u(op1code, op2code) { return [...op1code, ...op2code, 0x54]; } + i64_gt_s(op1code, op2code) { return [...op1code, ...op2code, 0x55]; } + i64_gt_u(op1code, op2code) { return [...op1code, ...op2code, 0x56]; } + i64_le_s(op1code, op2code) { return [...op1code, ...op2code, 0x57]; } + i64_le_u(op1code, op2code) { return [...op1code, ...op2code, 0x58]; } + i64_ge_s(op1code, op2code) { return [...op1code, ...op2code, 0x59]; } + i64_ge_u(op1code, op2code) { return [...op1code, ...op2code, 0x5a]; } + i64_add(op1code, op2code) { return [...op1code, ...op2code, 0x7c]; } + i64_sub(op1code, op2code) { return [...op1code, ...op2code, 0x7d]; } + i64_mul(op1code, op2code) { return [...op1code, ...op2code, 0x7e]; } + i64_div_s(op1code, op2code) { return [...op1code, ...op2code, 0x7f]; } + i64_div_u(op1code, op2code) { return [...op1code, ...op2code, 0x80]; } + i64_rem_s(op1code, op2code) { return [...op1code, ...op2code, 0x81]; } + i64_rem_u(op1code, op2code) { return [...op1code, ...op2code, 0x82]; } + i64_and(op1code, op2code) { return [...op1code, ...op2code, 0x83]; } + i64_or(op1code, op2code) { return [...op1code, ...op2code, 0x84]; } + i64_xor(op1code, op2code) { return [...op1code, ...op2code, 0x85]; } + i64_shl(op1code, op2code) { return [...op1code, ...op2code, 0x86]; } + i64_shr_s(op1code, op2code) { return [...op1code, ...op2code, 0x87]; } + i64_shr_u(op1code, op2code) { return [...op1code, ...op2code, 0x88]; } + i64_extend_i32_s(op1code) { return [...op1code, 0xac]; } + i64_extend_i32_u(op1code) { return [...op1code, 0xad]; } + i64_clz(op1code) { return [...op1code, 0x79]; } + i64_ctz(op1code) { return [...op1code, 0x7a]; } + + i32_eqz(op1code) { return [...op1code, 0x45]; } + i32_eq(op1code, op2code) { return [...op1code, ...op2code, 0x46]; } + i32_ne(op1code, op2code) { return [...op1code, ...op2code, 0x47]; } + i32_lt_s(op1code, op2code) { return [...op1code, ...op2code, 0x48]; } + i32_lt_u(op1code, op2code) { return [...op1code, ...op2code, 0x49]; } + i32_gt_s(op1code, op2code) { return [...op1code, ...op2code, 0x4a]; } + i32_gt_u(op1code, op2code) { return [...op1code, ...op2code, 0x4b]; } + i32_le_s(op1code, op2code) { return [...op1code, ...op2code, 0x4c]; } + i32_le_u(op1code, op2code) { return [...op1code, ...op2code, 0x4d]; } + i32_ge_s(op1code, op2code) { return [...op1code, ...op2code, 0x4e]; } + i32_ge_u(op1code, op2code) { return [...op1code, ...op2code, 0x4f]; } + i32_add(op1code, op2code) { return [...op1code, ...op2code, 0x6a]; } + i32_sub(op1code, op2code) { return [...op1code, ...op2code, 0x6b]; } + i32_mul(op1code, op2code) { return [...op1code, ...op2code, 0x6c]; } + i32_div_s(op1code, op2code) { return [...op1code, ...op2code, 0x6d]; } + i32_div_u(op1code, op2code) { return [...op1code, ...op2code, 0x6e]; } + i32_rem_s(op1code, op2code) { return [...op1code, ...op2code, 0x6f]; } + i32_rem_u(op1code, op2code) { return [...op1code, ...op2code, 0x70]; } + i32_and(op1code, op2code) { return [...op1code, ...op2code, 0x71]; } + i32_or(op1code, op2code) { return [...op1code, ...op2code, 0x72]; } + i32_xor(op1code, op2code) { return [...op1code, ...op2code, 0x73]; } + i32_shl(op1code, op2code) { return [...op1code, ...op2code, 0x74]; } + i32_shr_s(op1code, op2code) { return [...op1code, ...op2code, 0x75]; } + i32_shr_u(op1code, op2code) { return [...op1code, ...op2code, 0x76]; } + i32_rotl(op1code, op2code) { return [...op1code, ...op2code, 0x77]; } + i32_rotr(op1code, op2code) { return [...op1code, ...op2code, 0x78]; } + i32_wrap_i64(op1code) { return [...op1code, 0xa7]; } + i32_clz(op1code) { return [...op1code, 0x67]; } + i32_ctz(op1code) { return [...op1code, 0x68]; } + + unreachable() { return [ 0x0 ]; } + + current_memory() { return [ 0x3f, 0]; } + + comment() { return []; } + } + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmbuilder + + wasmbuilder is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmbuilder is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmbuilder. If not, see . + */ + + const typeCodes = { + "i32": 0x7f, + "i64": 0x7e, + "f32": 0x7d, + "f64": 0x7c, + "anyfunc": 0x70, + "func": 0x60, + "emptyblock": 0x40 + }; + + + class FunctionBuilder { + + constructor (module, fnName, fnType, moduleName, fieldName) { + if (fnType == "import") { + this.fnType = "import"; + this.moduleName = moduleName; + this.fieldName = fieldName; + } else if (fnType == "internal") { + this.fnType = "internal"; + } else { + throw new Error("Invalid function fnType: " + fnType); + } + this.module = module; + this.fnName = fnName; + this.params = []; + this.locals = []; + this.localIdxByName = {}; + this.code = []; + this.returnType = null; + this.nextLocal =0; + } + + addParam(paramName, paramType) { + if (this.localIdxByName[paramName]) + throw new Error(`param already exists. Function: ${this.fnName}, Param: ${paramName} `); + const idx = this.nextLocal++; + this.localIdxByName[paramName] = idx; + this.params.push({ + type: paramType + }); + } + + addLocal(localName, localType, _length) { + const length = _length || 1; + if (this.localIdxByName[localName]) + throw new Error(`local already exists. Function: ${this.fnName}, Param: ${localName} `); + const idx = this.nextLocal++; + this.localIdxByName[localName] = idx; + this.locals.push({ + type: localType, + length: length + }); + } + + setReturnType(returnType) { + if (this.returnType) + throw new Error(`returnType already defined. Function: ${this.fnName}`); + this.returnType = returnType; + } + + getSignature() { + const params = [...varuint32(this.params.length), ...this.params.map((p) => typeCodes[p.type])]; + const returns = this.returnType ? [0x01, typeCodes[this.returnType]] : [0]; + return [0x60, ...params, ...returns]; + } + + getBody() { + const locals = this.locals.map((l) => [ + ...varuint32(l.length), + typeCodes[l.type] + ]); + + const body = [ + ...varuint32(this.locals.length), + ...[].concat(...locals), + ...this.code, + 0x0b + ]; + return [ + ...varuint32(body.length), + ...body + ]; + } + + addCode(...code) { + this.code.push(...[].concat(...[...code])); + } + + getCodeBuilder() { + return new CodeBuilder(this); + } + } + + /* + Copyright 2019 0KIMS association. + + This file is part of wasmbuilder + + wasmbuilder is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + wasmbuilder is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with wasmbuilder. If not, see . + */ + + class ModuleBuilder { + + constructor() { + this.functions = []; + this.functionIdxByName = {}; + this.nImportFunctions = 0; + this.nInternalFunctions =0; + this.memory = { + pagesSize: 1, + moduleName: "env", + fieldName: "memory" + }; + this.free = 8; + this.datas = []; + this.modules = {}; + this.exports = []; + this.functionsTable = []; + } + + build() { + this._setSignatures(); + return new Uint8Array([ + ...u32(0x6d736100), + ...u32(1), + ...this._buildType(), + ...this._buildImport(), + ...this._buildFunctionDeclarations(), + ...this._buildFunctionsTable(), + ...this._buildExports(), + ...this._buildElements(), + ...this._buildCode(), + ...this._buildData() + ]); + } + + addFunction(fnName) { + if (typeof(this.functionIdxByName[fnName]) !== "undefined") + throw new Error(`Function already defined: ${fnName}`); + + const idx = this.functions.length; + this.functionIdxByName[fnName] = idx; + + this.functions.push(new FunctionBuilder(this, fnName, "internal")); + + this.nInternalFunctions++; + return this.functions[idx]; + } + + addIimportFunction(fnName, moduleName, _fieldName) { + if (typeof(this.functionIdxByName[fnName]) !== "undefined") + throw new Error(`Function already defined: ${fnName}`); + + if ( (this.functions.length>0) + &&(this.functions[this.functions.length-1].type == "internal")) + throw new Error(`Import functions must be declared before internal: ${fnName}`); + + let fieldName = _fieldName || fnName; + + const idx = this.functions.length; + this.functionIdxByName[fnName] = idx; + + this.functions.push(new FunctionBuilder(this, fnName, "import", moduleName, fieldName)); + + this.nImportFunctions ++; + return this.functions[idx]; + } + + setMemory(pagesSize, moduleName, fieldName) { + this.memory = { + pagesSize: pagesSize, + moduleName: moduleName || "env", + fieldName: fieldName || "memory" + }; + } + + exportFunction(fnName, _exportName) { + const exportName = _exportName || fnName; + if (typeof(this.functionIdxByName[fnName]) === "undefined") + throw new Error(`Function not defined: ${fnName}`); + const idx = this.functionIdxByName[fnName]; + if (exportName != fnName) { + this.functionIdxByName[exportName] = idx; + } + this.exports.push({ + exportName: exportName, + idx: idx + }); + } + + addFunctionToTable(fnName) { + const idx = this.functionIdxByName[fnName]; + this.functionsTable.push(idx); + } + + addData(offset, bytes) { + this.datas.push({ + offset: offset, + bytes: bytes + }); + } + + alloc(a, b) { + let size; + let bytes; + if ((Array.isArray(a) || ArrayBuffer.isView(a)) && (typeof(b) === "undefined")) { + size = a.length; + bytes = a; + } else { + size = a; + bytes = b; + } + size = (((size-1)>>3) +1)<<3; // Align to 64 bits. + const p = this.free; + this.free += size; + if (bytes) { + this.addData(p, bytes); + } + return p; + } + + allocString(s) { + const encoder = new globalThis.TextEncoder(); + const uint8array = encoder.encode(s); + return this.alloc([...uint8array, 0]); + } + + _setSignatures() { + this.signatures = []; + const signatureIdxByName = {}; + if (this.functionsTable.length>0) { + const signature = this.functions[this.functionsTable[0]].getSignature(); + const signatureName = "s_"+toHexString(signature); + signatureIdxByName[signatureName] = 0; + this.signatures.push(signature); + } + for (let i=0; i { + self.pendingLoads.push({ + page: p, + resolve: resolve, + reject: reject + }); + }); + self.__statusPage("After Load request: ", p); + return P; + } + + __statusPage(s, p) { + const logEntry = []; + const self=this; + if (!self.logHistory) return; + logEntry.push("==" + s+ " " +p); + let S = ""; + for (let i=0; i " + self.history[p][i][j]); + } + } + } + + + + _triggerLoad() { + const self = this; + + if (self.reading) return; + if (self.pendingLoads.length==0) return; + + const pageIdxs = Object.keys(self.pages); + + const deletablePages = []; + for (let i=0; i0) && + ( (typeof self.pages[self.pendingLoads[0].page] != "undefined" ) + ||( (freePages>0) + ||(deletablePages.length>0)))) { + const load = self.pendingLoads.shift(); + if (typeof self.pages[load.page] != "undefined") { + self.pages[load.page].pendingOps ++; + const idx = deletablePages.indexOf(load.page); + if (idx>=0) deletablePages.splice(idx, 1); + if (self.pages[load.page].loading) { + self.pages[load.page].loading.push(load); + } else { + load.resolve(); + } + self.__statusPage("After Load (cached): ", load.page); + + } else { + if (freePages) { + freePages--; + } else { + const fp = deletablePages.shift(); + self.__statusPage("Before Unload: ", fp); + self.avBuffs.unshift(self.pages[fp]); + delete self.pages[fp]; + self.__statusPage("After Unload: ", fp); + } + + if (load.page>=self.totalPages) { + self.pages[load.page] = getNewPage(); + load.resolve(); + self.__statusPage("After Load (new): ", load.page); + } else { + self.reading = true; + self.pages[load.page] = getNewPage(); + self.pages[load.page].loading = [load]; + ops.push(self.fd.read(self.pages[load.page].buff, 0, self.pageSize, load.page*self.pageSize).then((res)=> { + self.pages[load.page].size = res.bytesRead; + const loading = self.pages[load.page].loading; + delete self.pages[load.page].loading; + for (let i=0; i { + load.reject(err); + })); + self.__statusPage("After Load (loading): ", load.page); + } + } + } + // if (ops.length>1) console.log(ops.length); + + Promise.all(ops).then( () => { + self.reading = false; + if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self)); + self._tryClose(); + }); + + function getNewPage() { + if (self.avBuffs.length>0) { + const p = self.avBuffs.shift(); + p.dirty = false; + p.pendingOps = 1; + p.size =0; + return p; + } else { + return { + dirty: false, + buff: new Uint8Array(self.pageSize), + pendingOps: 1, + size: 0 + }; + } + } + + } + + + _triggerWrite() { + const self = this; + if (self.writing) return; + + const pageIdxs = Object.keys(self.pages); + + const ops = []; + + for (let i=0; i { + page.writing = false; + return; + }, (err) => { + console.log("ERROR Writing: "+err); + self.error = err; + self._tryClose(); + })); + } + } + + if (self.writing) { + Promise.all(ops).then( () => { + self.writing = false; + setImmediate(self._triggerWrite.bind(self)); + self._tryClose(); + if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self)); + }); + } + } + + _getDirtyPage() { + for (let p in this.pages) { + if (this.pages[p].dirty) return p; + } + return -1; + } + + async write(buff, pos) { + if (buff.byteLength == 0) return; + const self = this; + /* + if (buff.byteLength > self.pageSize*self.maxPagesLoaded*0.8) { + const cacheSize = Math.floor(buff.byteLength * 1.1); + this.maxPagesLoaded = Math.floor( cacheSize / self.pageSize)+1; + } + */ + if (typeof pos == "undefined") pos = self.pos; + self.pos = pos+buff.byteLength; + if (self.totalSize < pos + buff.byteLength) self.totalSize = pos + buff.byteLength; + if (self.pendingClose) + throw new Error("Writing a closing file"); + const firstPage = Math.floor(pos / self.pageSize); + const lastPage = Math.floor((pos + buff.byteLength -1) / self.pageSize); + + const pagePromises = []; + for (let i=firstPage; i<=lastPage; i++) pagePromises.push(self._loadPage(i)); + self._triggerLoad(); + + let p = firstPage; + let o = pos % self.pageSize; + let r = buff.byteLength; + while (r>0) { + await pagePromises[p-firstPage]; + const l = (o+r > self.pageSize) ? (self.pageSize -o) : r; + const srcView = buff.slice( buff.byteLength - r, buff.byteLength - r + l); + const dstView = new Uint8Array(self.pages[p].buff.buffer, o, l); + dstView.set(srcView); + self.pages[p].dirty = true; + self.pages[p].pendingOps --; + self.pages[p].size = Math.max(o+l, self.pages[p].size); + if (p>=self.totalPages) { + self.totalPages = p+1; + } + r = r-l; + p ++; + o = 0; + if (!self.writing) setImmediate(self._triggerWrite.bind(self)); + } + } + + async read(len, pos) { + const self = this; + let buff = new Uint8Array(len); + await self.readToBuffer(buff, 0, len, pos); + + return buff; + } + + async readToBuffer(buffDst, offset, len, pos) { + if (len == 0) { + return; + } + const self = this; + if (len > self.pageSize*self.maxPagesLoaded*0.8) { + const cacheSize = Math.floor(len * 1.1); + this.maxPagesLoaded = Math.floor( cacheSize / self.pageSize)+1; + } + if (typeof pos == "undefined") pos = self.pos; + self.pos = pos+len; + if (self.pendingClose) + throw new Error("Reading a closing file"); + const firstPage = Math.floor(pos / self.pageSize); + const lastPage = Math.floor((pos + len -1) / self.pageSize); + + const pagePromises = []; + for (let i=firstPage; i<=lastPage; i++) pagePromises.push(self._loadPage(i)); + + self._triggerLoad(); + + let p = firstPage; + let o = pos % self.pageSize; + // Remaining bytes to read + let r = pos + len > self.totalSize ? len - (pos + len - self.totalSize): len; + while (r>0) { + await pagePromises[p - firstPage]; + self.__statusPage("After Await (read): ", p); + + // bytes to copy from this page + const l = (o+r > self.pageSize) ? (self.pageSize -o) : r; + const srcView = new Uint8Array(self.pages[p].buff.buffer, self.pages[p].buff.byteOffset + o, l); + buffDst.set(srcView, offset+len-r); + self.pages[p].pendingOps --; + + self.__statusPage("After Op done: ", p); + + r = r-l; + p ++; + o = 0; + if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self)); + } + + this.pos = pos + len; + + } + + + _tryClose() { + const self = this; + if (!self.pendingClose) return; + if (self.error) { + self.pendingCloseReject(self.error); + } + const p = self._getDirtyPage(); + if ((p>=0) || (self.writing) || (self.reading) || (self.pendingLoads.length>0)) return; + self.pendingClose(); + } + + close() { + const self = this; + if (self.pendingClose) + throw new Error("Closing the file twice"); + return new Promise((resolve, reject) => { + self.pendingClose = resolve; + self.pendingCloseReject = reject; + self._tryClose(); + }).then(()=> { + self.fd.close(); + }, (err) => { + self.fd.close(); + throw (err); + }); + } + + async discard() { + const self = this; + await self.close(); + await fs.promises.unlink(this.fileName); + } + + async writeULE32(v, pos) { + const self = this; + const tmpBuff32 = new Uint8Array(4); + const tmpBuff32v = new DataView(tmpBuff32.buffer); + + tmpBuff32v.setUint32(0, v, true); + + await self.write(tmpBuff32, pos); + } + + async writeUBE32(v, pos) { + const self = this; + + const tmpBuff32 = new Uint8Array(4); + const tmpBuff32v = new DataView(tmpBuff32.buffer); + + tmpBuff32v.setUint32(0, v, false); + + await self.write(tmpBuff32, pos); + } + + + async writeULE64(v, pos) { + const self = this; + + const tmpBuff64 = new Uint8Array(8); + const tmpBuff64v = new DataView(tmpBuff64.buffer); + + tmpBuff64v.setUint32(0, v & 0xFFFFFFFF, true); + tmpBuff64v.setUint32(4, Math.floor(v / 0x100000000) , true); + + await self.write(tmpBuff64, pos); + } + + async readULE32(pos) { + const self = this; + const b = await self.read(4, pos); + + const view = new Uint32Array(b.buffer); + + return view[0]; + } + + async readUBE32(pos) { + const self = this; + const b = await self.read(4, pos); + + const view = new DataView(b.buffer); + + return view.getUint32(0, false); + } + + async readULE64(pos) { + const self = this; + const b = await self.read(8, pos); + + const view = new Uint32Array(b.buffer); + + return view[1] * 0x100000000 + view[0]; + } + + async readString(pos) { + const self = this; + + if (self.pendingClose) { + throw new Error("Reading a closing file"); + } + + let currentPosition = typeof pos == "undefined" ? self.pos : pos; + let currentPage = Math.floor(currentPosition / self.pageSize); + + let endOfStringFound = false; + let str = ""; + + while (!endOfStringFound) { + //Read page + let pagePromise = self._loadPage(currentPage); + self._triggerLoad(); + await pagePromise; + self.__statusPage("After Await (read): ", currentPage); + + let offsetOnPage = currentPosition % self.pageSize; + + const dataArray = new Uint8Array( + self.pages[currentPage].buff.buffer, + self.pages[currentPage].buff.byteOffset + offsetOnPage, + self.pageSize - offsetOnPage + ); + + let indexEndOfString = dataArray.findIndex(element => element === 0); + endOfStringFound = indexEndOfString !== -1; + + if (endOfStringFound) { + str += new TextDecoder().decode(dataArray.slice(0, indexEndOfString)); + self.pos = currentPage * this.pageSize + offsetOnPage + indexEndOfString + 1; + } else { + str += new TextDecoder().decode(dataArray); + self.pos = currentPage * this.pageSize + offsetOnPage + dataArray.length; + } + + self.pages[currentPage].pendingOps--; + self.__statusPage("After Op done: ", currentPage); + + currentPosition = self.pos; + currentPage++; + + if (self.pendingLoads.length > 0) setImmediate(self._triggerLoad.bind(self)); + } + + return str; + } + } + + function createNew$1(o) { + const initialSize = o.initialSize || 1<<20; + const fd = new MemFile(); + fd.o = o; + fd.o.data = new Uint8Array(initialSize); + fd.allocSize = initialSize; + fd.totalSize = 0; + fd.readOnly = false; + fd.pos = 0; + return fd; + } + + function readExisting$2(o) { + const fd = new MemFile(); + fd.o = o; + fd.allocSize = o.data.byteLength; + fd.totalSize = o.data.byteLength; + fd.readOnly = true; + fd.pos = 0; + return fd; + } + + const tmpBuff32$1 = new Uint8Array(4); + const tmpBuff32v$1 = new DataView(tmpBuff32$1.buffer); + const tmpBuff64$1 = new Uint8Array(8); + const tmpBuff64v$1 = new DataView(tmpBuff64$1.buffer); + + class MemFile { + + constructor() { + this.pageSize = 1 << 14; // for compatibility + } + + _resizeIfNeeded(newLen) { + if (newLen > this.allocSize) { + const newAllocSize = Math.max( + this.allocSize + (1 << 20), + Math.floor(this.allocSize * 1.1), + newLen + ); + const newData = new Uint8Array(newAllocSize); + newData.set(this.o.data); + this.o.data = newData; + this.allocSize = newAllocSize; + } + } + + async write(buff, pos) { + const self =this; + if (typeof pos == "undefined") pos = self.pos; + if (this.readOnly) throw new Error("Writing a read only file"); + + this._resizeIfNeeded(pos + buff.byteLength); + + this.o.data.set(buff.slice(), pos); + + if (pos + buff.byteLength > this.totalSize) this.totalSize = pos + buff.byteLength; + + this.pos = pos + buff.byteLength; + } + + async readToBuffer(buffDest, offset, len, pos) { + const self = this; + if (typeof pos == "undefined") pos = self.pos; + if (this.readOnly) { + if (pos + len > this.totalSize) throw new Error("Reading out of bounds"); + } + this._resizeIfNeeded(pos + len); + + const buffSrc = new Uint8Array(this.o.data.buffer, this.o.data.byteOffset + pos, len); + + buffDest.set(buffSrc, offset); + + this.pos = pos + len; + } + + async read(len, pos) { + const self = this; + + const buff = new Uint8Array(len); + await self.readToBuffer(buff, 0, len, pos); + + return buff; + } + + close() { + if (this.o.data.byteLength != this.totalSize) { + this.o.data = this.o.data.slice(0, this.totalSize); + } + } + + async discard() { + } + + + async writeULE32(v, pos) { + const self = this; + + tmpBuff32v$1.setUint32(0, v, true); + + await self.write(tmpBuff32$1, pos); + } + + async writeUBE32(v, pos) { + const self = this; + + tmpBuff32v$1.setUint32(0, v, false); + + await self.write(tmpBuff32$1, pos); + } + + + async writeULE64(v, pos) { + const self = this; + + tmpBuff64v$1.setUint32(0, v & 0xFFFFFFFF, true); + tmpBuff64v$1.setUint32(4, Math.floor(v / 0x100000000) , true); + + await self.write(tmpBuff64$1, pos); + } + + + async readULE32(pos) { + const self = this; + const b = await self.read(4, pos); + + const view = new Uint32Array(b.buffer); + + return view[0]; + } + + async readUBE32(pos) { + const self = this; + const b = await self.read(4, pos); + + const view = new DataView(b.buffer); + + return view.getUint32(0, false); + } + + async readULE64(pos) { + const self = this; + const b = await self.read(8, pos); + + const view = new Uint32Array(b.buffer); + + return view[1] * 0x100000000 + view[0]; + } + + async readString(pos) { + const self = this; + + let currentPosition = typeof pos == "undefined" ? self.pos : pos; + + if (currentPosition > this.totalSize) { + if (this.readOnly) { + throw new Error("Reading out of bounds"); + } + this._resizeIfNeeded(pos); + } + const dataArray = new Uint8Array( + self.o.data.buffer, + currentPosition, + this.totalSize - currentPosition + ); + + let indexEndOfString = dataArray.findIndex(element => element === 0); + let endOfStringFound = indexEndOfString !== -1; + + let str = ""; + if (endOfStringFound) { + str = new TextDecoder().decode(dataArray.slice(0, indexEndOfString)); + self.pos = currentPosition + indexEndOfString + 1; + } else { + self.pos = currentPosition; + } + return str; + } + } + + const PAGE_SIZE$1 = 1<<22; + + function createNew(o) { + const initialSize = o.initialSize || 0; + const fd = new BigMemFile(); + fd.o = o; + const nPages = initialSize ? Math.floor((initialSize - 1) / PAGE_SIZE$1)+1 : 0; + fd.o.data = []; + for (let i=0; i0) { + const l = (o+r > PAGE_SIZE$1) ? (PAGE_SIZE$1 -o) : r; + const srcView = buff.slice(buff.byteLength - r, buff.byteLength - r + l); + const dstView = new Uint8Array(self.o.data[p].buffer, o, l); + dstView.set(srcView); + r = r-l; + p ++; + o = 0; + } + + this.pos = pos + buff.byteLength; + } + + async readToBuffer(buffDst, offset, len, pos) { + const self = this; + if (typeof pos == "undefined") pos = self.pos; + if (this.readOnly) { + if (pos + len > this.totalSize) throw new Error("Reading out of bounds"); + } + this._resizeIfNeeded(pos + len); + + const firstPage = Math.floor(pos / PAGE_SIZE$1); + + let p = firstPage; + let o = pos % PAGE_SIZE$1; + // Remaining bytes to read + let r = len; + while (r>0) { + // bytes to copy from this page + const l = (o+r > PAGE_SIZE$1) ? (PAGE_SIZE$1 -o) : r; + const srcView = new Uint8Array(self.o.data[p].buffer, o, l); + buffDst.set(srcView, offset+len-r); + r = r-l; + p ++; + o = 0; + } + + this.pos = pos + len; + } + + async read(len, pos) { + const self = this; + const buff = new Uint8Array(len); + + await self.readToBuffer(buff, 0, len, pos); + + return buff; + } + + close() { + } + + async discard() { + } + + + async writeULE32(v, pos) { + const self = this; + + tmpBuff32v.setUint32(0, v, true); + + await self.write(tmpBuff32, pos); + } + + async writeUBE32(v, pos) { + const self = this; + + tmpBuff32v.setUint32(0, v, false); + + await self.write(tmpBuff32, pos); + } + + + async writeULE64(v, pos) { + const self = this; + + tmpBuff64v.setUint32(0, v & 0xFFFFFFFF, true); + tmpBuff64v.setUint32(4, Math.floor(v / 0x100000000) , true); + + await self.write(tmpBuff64, pos); + } + + + async readULE32(pos) { + const self = this; + const b = await self.read(4, pos); + + const view = new Uint32Array(b.buffer); + + return view[0]; + } + + async readUBE32(pos) { + const self = this; + const b = await self.read(4, pos); + + const view = new DataView(b.buffer); + + return view.getUint32(0, false); + } + + async readULE64(pos) { + const self = this; + const b = await self.read(8, pos); + + const view = new Uint32Array(b.buffer); + + return view[1] * 0x100000000 + view[0]; + } + + async readString(pos) { + const self = this; + const fixedSize = 2048; + + let currentPosition = typeof pos == "undefined" ? self.pos : pos; + + if (currentPosition > this.totalSize) { + if (this.readOnly) { + throw new Error("Reading out of bounds"); + } + this._resizeIfNeeded(pos); + } + + let endOfStringFound = false; + let str = ""; + + while (!endOfStringFound) { + let currentPage = Math.floor(currentPosition / PAGE_SIZE$1); + let offsetOnPage = currentPosition % PAGE_SIZE$1; + + if (self.o.data[currentPage] === undefined) { + throw new Error("ERROR"); + } + + let readLength = Math.min(fixedSize, self.o.data[currentPage].length - offsetOnPage); + const dataArray = new Uint8Array(self.o.data[currentPage].buffer, offsetOnPage, readLength); + + let indexEndOfString = dataArray.findIndex(element => element === 0); + endOfStringFound = indexEndOfString !== -1; + + if (endOfStringFound) { + str += new TextDecoder().decode(dataArray.slice(0, indexEndOfString)); + self.pos = currentPage * PAGE_SIZE$1 + offsetOnPage + indexEndOfString + 1; + } else { + str += new TextDecoder().decode(dataArray); + self.pos = currentPage * PAGE_SIZE$1 + offsetOnPage + dataArray.length; + } + + currentPosition = self.pos; + } + return str; + } + } + + const O_TRUNC = 512; + const O_CREAT = 64; + const O_RDWR = 2; + const O_RDONLY = 0; + + /* global fetch */ + + const DEFAULT_CACHE_SIZE = (1 << 16); + const DEFAULT_PAGE_SIZE = (1 << 13); + + + async function createOverride(o, b, c) { + if (typeof o === "string") { + o = { + type: "file", + fileName: o, + cacheSize: b || DEFAULT_CACHE_SIZE, + pageSize: c || DEFAULT_PAGE_SIZE + }; + } + if (o.type == "file") { + return await open(o.fileName, O_TRUNC | O_CREAT | O_RDWR, o.cacheSize, o.pageSize); + } else if (o.type == "mem") { + return createNew$1(o); + } else if (o.type == "bigMem") { + return createNew(o); + } else { + throw new Error("Invalid FastFile type: "+o.type); + } + } + + async function readExisting(o, b, c) { + if (o instanceof Uint8Array) { + o = { + type: "mem", + data: o + }; + } + { + if (typeof o === "string") { + const buff = await fetch(o).then( function(res) { + return res.arrayBuffer(); + }).then(function (ab) { + return new Uint8Array(ab); + }); + o = { + type: "mem", + data: buff + }; + } + } + if (o.type == "file") { + return await open(o.fileName, O_RDONLY, o.cacheSize, o.pageSize); + } else if (o.type == "mem") { + return await readExisting$2(o); + } else if (o.type == "bigMem") { + return await readExisting$1(o); + } else { + throw new Error("Invalid FastFile type: "+o.type); + } + } + + async function readBinFile(fileName, type, maxVersion, cacheSize, pageSize) { + + const fd = await readExisting(fileName); + + const b = await fd.read(4); + let readedType = ""; + for (let i=0; i<4; i++) readedType += String.fromCharCode(b[i]); + + if (readedType != type) throw new Error(fileName + ": Invalid File format"); + + let v = await fd.readULE32(); + + if (v>maxVersion) throw new Error("Version not supported"); + + const nSections = await fd.readULE32(); + + // Scan sections + let sections = []; + for (let i=0; i1) throw new Error(fd.fileName +": Section Duplicated " +idSection); + + fd.pos = sections[idSection][0].p; + + fd.readingSection = sections[idSection][0]; + } + + async function endReadSection(fd, noCheck) { + if (typeof fd.readingSection === "undefined") throw new Error("Not reading a section"); + if (!noCheck) { + if (fd.pos-fd.readingSection.p != fd.readingSection.size) throw new Error("Invalid section size reading"); + } + delete fd.readingSection; + } + + async function writeBigInt(fd, n, n8, pos) { + const buff = new Uint8Array(n8); + Scalar$1.toRprLE(buff, 0, n, n8); + await fd.write(buff, pos); + } + + async function readBigInt(fd, n8, pos) { + const buff = await fd.read(n8, pos); + return Scalar$1.fromRprLE(buff, 0, n8); + } + + async function copySection(fdFrom, sections, fdTo, sectionId, size) { + if (typeof size === "undefined") { + size = sections[sectionId][0].size; + } + const chunkSize = fdFrom.pageSize; + await startReadUniqueSection(fdFrom, sections, sectionId); + await startWriteSection(fdTo, sectionId); + for (let p=0; p sections[idSection][0].size) { + throw new Error("Reading out of the range of the section"); + } + + let buff; + if (length < (1 << 30) ) { + buff = new Uint8Array(length); + } else { + buff = new BigBuffer$1(length); + } + + await fd.readToBuffer(buff, 0, length, sections[idSection][0].p + offset); + return buff; + } + + async function sectionIsEqual(fd1, sections1, fd2, sections2, idSection) { + const MAX_BUFF_SIZE = fd1.pageSize * 16; + await startReadUniqueSection(fd1, sections1, idSection); + await startReadUniqueSection(fd2, sections2, idSection); + if (sections1[idSection][0].size != sections2[idSection][0].size) return false; + const totalBytes=sections1[idSection][0].size; + for (let i=0; iBigInt(Number.MAX_SAFE_INTEGER )) { throw new Error("Number too big"); } @@ -201,7 +17696,7 @@ var snarkjs = (function (exports) { const l = (((s.length-7)*4 - 1) >> 5)+1; // Number of 32bit words; for (let i=0; i> this.one; - this.bitLength = bitLength$6(this.p); + this.bitLength = bitLength$5(this.p); this.mask = (this.one << BigInt(this.bitLength)) - this.one; this.n64 = Math.floor((this.bitLength - 1) / 64)+1; @@ -1160,8 +18653,6 @@ var snarkjs = (function (exports) { } } - var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; - var utils$6 = {}; /* @@ -2720,7 +20211,7 @@ var snarkjs = (function (exports) { return n % 2n === 0n; } - function isNegative$3(n) { + function isNegative$2(n) { return n < 0n; } @@ -2728,8 +20219,8 @@ var snarkjs = (function (exports) { return n > 0n; } - function bitLength$5(n) { - if (isNegative$3(n)) { + function bitLength$4(n) { + if (isNegative$2(n)) { return n.toString(2).length - 1; // discard the - sign } else { return n.toString(2).length; @@ -2759,7 +20250,7 @@ var snarkjs = (function (exports) { if (compare(t, 0n) === -1) { t = t + n; } - if (isNegative$3(a)) { + if (isNegative$2(a)) { return -t; } return t; @@ -2769,7 +20260,7 @@ var snarkjs = (function (exports) { if (mod === 0n) throw new Error("Cannot take modPow with modulus 0"); var r = 1n, base = n % mod; - if (isNegative$3(exp)) { + if (isNegative$2(exp)) { exp = exp * -1n; base = modInv$3(base, mod); } @@ -2832,7 +20323,7 @@ var snarkjs = (function (exports) { var isPrime = isBasicPrime(p); if (isPrime !== undefined) return isPrime; var n = abs(p); - var bits = bitLength$5(n); + var bits = bitLength$4(n); if (bits <= 64) return millerRabinTest(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]); var logN = Math.log(2) * Number(bits); @@ -2843,9 +20334,9 @@ var snarkjs = (function (exports) { return millerRabinTest(n, a); } - bigint.bitLength = bitLength$5; + bigint.bitLength = bitLength$4; bigint.isOdd = isOdd$4; - bigint.isNegative = isNegative$3; + bigint.isNegative = isNegative$2; bigint.abs = abs; bigint.isUnit = isUnit; bigint.compare = compare; @@ -2879,11 +20370,11 @@ var snarkjs = (function (exports) { const buildBatchInverse$2 = build_batchinverse; const buildBatchConvertion$1 = build_batchconvertion; const buildBatchOp = build_batchop; - const { bitLength: bitLength$4, modInv: modInv$2, modPow: modPow$1, isPrime, isOdd: isOdd$3, square } = bigint; + const { bitLength: bitLength$3, modInv: modInv$2, modPow: modPow$1, isPrime, isOdd: isOdd$3, square } = bigint; var build_f1m = function buildF1m(module, _q, _prefix, _intPrefix) { const q = BigInt(_q); - const n64 = Math.floor((bitLength$4(q - 1n) - 1)/64) +1; + const n64 = Math.floor((bitLength$3(q - 1n) - 1)/64) +1; const n32 = n64*2; const n8 = n64*8; @@ -3936,12 +21427,12 @@ var snarkjs = (function (exports) { */ const buildF1m$2 =build_f1m; - const { bitLength: bitLength$3 } = bigint; + const { bitLength: bitLength$2 } = bigint; var build_f1 = function buildF1(module, _q, _prefix, _f1mPrefix, _intPrefix) { const q = BigInt(_q); - const n64 = Math.floor((bitLength$3(q - 1n) - 1)/64) +1; + const n64 = Math.floor((bitLength$2(q - 1n) - 1)/64) +1; const n8 = n64*8; const prefix = _prefix || "f1"; @@ -7114,7 +24605,7 @@ var snarkjs = (function (exports) { f.addCode( c.if( - c.call(prefix + "_isZeroAffine", c.getLocal("pIn")), + c.call(prefix + "_isZero", c.getLocal("pIn")), [ ...c.call(prefixField + "_zero", c.getLocal("pOut")), ...c.i32_store8( @@ -9406,7 +26897,7 @@ var snarkjs = (function (exports) { const buildPol$1 = build_pol; const buildQAP$1 = build_qap; const buildApplyKey$1 = build_applykey; - const { bitLength: bitLength$2, modInv, isOdd: isOdd$1, isNegative: isNegative$2 } = bigint; + const { bitLength: bitLength$1, modInv, isOdd: isOdd$1, isNegative: isNegative$1 } = bigint; var build_bn128 = function buildBN128(module, _prefix) { @@ -9418,7 +26909,7 @@ var snarkjs = (function (exports) { const r = 21888242871839275222246405745257275088548364400416034343698204186575808495617n; - const n64 = Math.floor((bitLength$2(q - 1n) - 1)/64) +1; + const n64 = Math.floor((bitLength$1(q - 1n) - 1)/64) +1; const n8 = n64*8; const frsize = n8; const f1size = n8; @@ -10364,7 +27855,7 @@ var snarkjs = (function (exports) { (ac0 * bc0 - ( ac1 * bc1) ) % q, (ac0 * bc1 + ( ac1 * bc0) ) % q, ]; - if (isNegative$2(res[0])) res[0] = res[0] + q; + if (isNegative$1(res[0])) res[0] = res[0] + q; return res; } @@ -10813,7 +28304,7 @@ var snarkjs = (function (exports) { const buildPol = build_pol; const buildQAP = build_qap; const buildApplyKey = build_applykey; - const { bitLength: bitLength$1, isOdd, isNegative: isNegative$1 } = bigint; + const { bitLength, isOdd, isNegative } = bigint; // Definition here: https://electriccoin.co/blog/new-snark-curve/ @@ -10826,13 +28317,13 @@ var snarkjs = (function (exports) { const q = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn; const r = 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n; - const n64q = Math.floor((bitLength$1(q - 1n) - 1)/64) +1; + const n64q = Math.floor((bitLength(q - 1n) - 1)/64) +1; const n8q = n64q*8; const f1size = n8q; const f2size = f1size * 2; const ftsize = f1size * 12; - const n64r = Math.floor((bitLength$1(r - 1n) - 1)/64) +1; + const n64r = Math.floor((bitLength(r - 1n) - 1)/64) +1; const n8r = n64r*8; const frsize = n8r; @@ -11801,7 +29292,7 @@ var snarkjs = (function (exports) { (ac0 * bc0 - (ac1 * bc1)) % q, (ac0 * bc1 + (ac1 * bc0)) % q, ]; - if (isNegative$1(res[0])) res[0] = res[0] + q; + if (isNegative(res[0])) res[0] = res[0] + q; return res; } @@ -12558,7 +30049,7 @@ var snarkjs = (function (exports) { function leInt2Buff(n, len) { let r = n; if (typeof len === "undefined") { - len = Math.floor((bitLength$6(n) - 1) / 8) + 1; + len = Math.floor((bitLength$5(n) - 1) / 8) + 1; if (len == 0) len = 1; } const buff = new Uint8Array(len); @@ -12712,15 +30203,15 @@ var snarkjs = (function (exports) { buffer2array: buffer2array }); - const PAGE_SIZE$1 = 1<<30; + const PAGE_SIZE = 1<<30; class BigBuffer { constructor(size) { this.buffers = []; this.byteLength = size; - for (let i=0; i0) { // bytes to copy from this page - const l = (o+r > PAGE_SIZE$1) ? (PAGE_SIZE$1 -o) : r; + const l = (o+r > PAGE_SIZE) ? (PAGE_SIZE -o) : r; const srcView = new Uint8Array(this.buffers[p].buffer, this.buffers[p].byteOffset+o, l); if (l == len) return srcView.slice(); if (!buff) { - if (len <= PAGE_SIZE$1) { + if (len <= PAGE_SIZE) { buff = new Uint8Array(len); } else { buff = new BigBuffer(len); @@ -12771,24 +30262,24 @@ var snarkjs = (function (exports) { if (len==0) return; - const firstPage = Math.floor(offset / PAGE_SIZE$1); - const lastPage = Math.floor((offset+len-1) / PAGE_SIZE$1); + const firstPage = Math.floor(offset / PAGE_SIZE); + const lastPage = Math.floor((offset+len-1) / PAGE_SIZE); if (firstPage == lastPage) { if ((buff instanceof BigBuffer)&&(buff.buffers.length==1)) { - return this.buffers[firstPage].set(buff.buffers[0], offset % PAGE_SIZE$1); + return this.buffers[firstPage].set(buff.buffers[0], offset % PAGE_SIZE); } else { - return this.buffers[firstPage].set(buff, offset % PAGE_SIZE$1); + return this.buffers[firstPage].set(buff, offset % PAGE_SIZE); } } let p = firstPage; - let o = offset % PAGE_SIZE$1; + let o = offset % PAGE_SIZE; let r = len; while (r>0) { - const l = (o+r > PAGE_SIZE$1) ? (PAGE_SIZE$1 -o) : r; + const l = (o+r > PAGE_SIZE) ? (PAGE_SIZE -o) : r; const srcView = buff.slice( len -r, len -r+l); const dstView = new Uint8Array(this.buffers[p].buffer, this.buffers[p].byteOffset + o, l); dstView.set(srcView); @@ -12864,7 +30355,7 @@ var snarkjs = (function (exports) { this.m = 1; this.half = shiftRight(p, one); - this.bitLength = bitLength$6(p); + this.bitLength = bitLength$5(p); this.mask = sub(shiftLeft(one, this.bitLength), one); this.pOp1 = tm.alloc(n8); @@ -13018,7 +30509,7 @@ var snarkjs = (function (exports) { e(a, b) { if (a instanceof Uint8Array) return a; let ra = e(a, b); - if (isNegative$4(ra)) { + if (isNegative$3(ra)) { ra = neg(ra); if (gt(ra, this.p)) { ra = mod(ra, this.p); @@ -14047,25 +31538,156 @@ var snarkjs = (function (exports) { return runTask; } - var os = {}; + var base64Js = {}; - /** - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ + base64Js.byteLength = byteLength$5; + base64Js.toByteArray = toByteArray; + base64Js.fromByteArray = fromByteArray; - var browser$1 = Worker; + var lookup = []; + var revLookup = []; + var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array; + + var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i]; + revLookup[code.charCodeAt(i)] = i; + } + + // Support decoding URL-safe base64 strings, as Node.js does. + // See: https://en.wikipedia.org/wiki/Base64#URL_applications + revLookup['-'.charCodeAt(0)] = 62; + revLookup['_'.charCodeAt(0)] = 63; + + function getLens (b64) { + var len = b64.length; + + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // Trim off extra bytes after placeholder bytes are found + // See: https://github.com/beatgammit/base64-js/issues/42 + var validLen = b64.indexOf('='); + if (validLen === -1) validLen = len; + + var placeHoldersLen = validLen === len + ? 0 + : 4 - (validLen % 4); + + return [validLen, placeHoldersLen] + } + + // base64 is 4/3 + up to two characters of the original data + function byteLength$5 (b64) { + var lens = getLens(b64); + var validLen = lens[0]; + var placeHoldersLen = lens[1]; + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen + } + + function _byteLength (b64, validLen, placeHoldersLen) { + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen + } + + function toByteArray (b64) { + var tmp; + var lens = getLens(b64); + var validLen = lens[0]; + var placeHoldersLen = lens[1]; + + var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)); + + var curByte = 0; + + // if there are placeholders, only get up to the last complete 4 chars + var len = placeHoldersLen > 0 + ? validLen - 4 + : validLen; + + var i; + for (i = 0; i < len; i += 4) { + tmp = + (revLookup[b64.charCodeAt(i)] << 18) | + (revLookup[b64.charCodeAt(i + 1)] << 12) | + (revLookup[b64.charCodeAt(i + 2)] << 6) | + revLookup[b64.charCodeAt(i + 3)]; + arr[curByte++] = (tmp >> 16) & 0xFF; + arr[curByte++] = (tmp >> 8) & 0xFF; + arr[curByte++] = tmp & 0xFF; + } + + if (placeHoldersLen === 2) { + tmp = + (revLookup[b64.charCodeAt(i)] << 2) | + (revLookup[b64.charCodeAt(i + 1)] >> 4); + arr[curByte++] = tmp & 0xFF; + } + + if (placeHoldersLen === 1) { + tmp = + (revLookup[b64.charCodeAt(i)] << 10) | + (revLookup[b64.charCodeAt(i + 1)] << 4) | + (revLookup[b64.charCodeAt(i + 2)] >> 2); + arr[curByte++] = (tmp >> 8) & 0xFF; + arr[curByte++] = tmp & 0xFF; + } + + return arr + } + + function tripletToBase64 (num) { + return lookup[num >> 18 & 0x3F] + + lookup[num >> 12 & 0x3F] + + lookup[num >> 6 & 0x3F] + + lookup[num & 0x3F] + } + + function encodeChunk (uint8, start, end) { + var tmp; + var output = []; + for (var i = start; i < end; i += 3) { + tmp = + ((uint8[i] << 16) & 0xFF0000) + + ((uint8[i + 1] << 8) & 0xFF00) + + (uint8[i + 2] & 0xFF); + output.push(tripletToBase64(tmp)); + } + return output.join('') + } + + function fromByteArray (uint8) { + var tmp; + var len = uint8.length; + var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes + var parts = []; + var maxChunkLength = 16383; // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))); + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1]; + parts.push( + lookup[tmp >> 2] + + lookup[(tmp << 4) & 0x3F] + + '==' + ); + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + uint8[len - 1]; + parts.push( + lookup[tmp >> 10] + + lookup[(tmp >> 4) & 0x3F] + + lookup[(tmp << 2) & 0x3F] + + '=' + ); + } + + return parts.join('') + } /* global navigator, WebAssembly */ /* @@ -14174,8 +31796,21 @@ var snarkjs = (function (exports) { tm.concurrency = concurrency; for (let i = 0; i. - */ - - function toNumber(n) { - return BigInt(n); - } - - function isNegative(n) { - return n < 0n; - } - - function isZero(n) { - return n === 0n; - } - - function bitLength(n) { - if (isNegative(n)) { - return n.toString(2).length - 1; // discard the - sign - } else { - return n.toString(2).length; - } - } - - function u32(n) { - const b = []; - const v = toNumber(n); - b.push(Number(v & 0xFFn)); - b.push(Number(v >> 8n & 0xFFn)); - b.push(Number(v >> 16n & 0xFFn)); - b.push(Number(v >> 24n & 0xFFn)); - return b; - } - - function toUTF8Array(str) { - var utf8 = []; - for (var i=0; i < str.length; i++) { - var charcode = str.charCodeAt(i); - if (charcode < 0x80) utf8.push(charcode); - else if (charcode < 0x800) { - utf8.push(0xc0 | (charcode >> 6), - 0x80 | (charcode & 0x3f)); - } - else if (charcode < 0xd800 || charcode >= 0xe000) { - utf8.push(0xe0 | (charcode >> 12), - 0x80 | ((charcode>>6) & 0x3f), - 0x80 | (charcode & 0x3f)); - } - // surrogate pair - else { - i++; - // UTF-16 encodes 0x10000-0x10FFFF by - // subtracting 0x10000 and splitting the - // 20 bits of 0x0-0xFFFFF into two halves - charcode = 0x10000 + (((charcode & 0x3ff)<<10) - | (str.charCodeAt(i) & 0x3ff)); - utf8.push(0xf0 | (charcode >>18), - 0x80 | ((charcode>>12) & 0x3f), - 0x80 | ((charcode>>6) & 0x3f), - 0x80 | (charcode & 0x3f)); - } - } - return utf8; - } - - function string(str) { - const bytes = toUTF8Array(str); - return [ ...varuint32(bytes.length), ...bytes ]; - } - - function varuint(n) { - const code = []; - let v = toNumber(n); - if (isNegative(v)) throw new Error("Number cannot be negative"); - while (!isZero(v)) { - code.push(Number(v & 0x7Fn)); - v = v >> 7n; - } - if (code.length==0) code.push(0); - for (let i=0; i 0xFFFFFFFFn) throw new Error("Number too big"); - if (v > 0x7FFFFFFFn) v = v - 0x100000000n; - // bigInt("-80000000", 16) as base10 - if (v < -2147483648n) throw new Error("Number too small"); - return varint(v); - } - - function varint64(n) { - let v = toNumber(n); - if (v > 0xFFFFFFFFFFFFFFFFn) throw new Error("Number too big"); - if (v > 0x7FFFFFFFFFFFFFFFn) v = v - 0x10000000000000000n; - // bigInt("-8000000000000000", 16) as base10 - if (v < -9223372036854775808n) throw new Error("Number too small"); - return varint(v); - } - - function varuint32(n) { - let v = toNumber(n); - if (v > 0xFFFFFFFFn) throw new Error("Number too big"); - return varuint(v); - } - - function toHexString(byteArray) { - return Array.from(byteArray, function(byte) { - return ("0" + (byte & 0xFF).toString(16)).slice(-2); - }).join(""); - } - - /* - Copyright 2019 0KIMS association. - - This file is part of wasmbuilder - - wasmbuilder is a free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - wasmbuilder is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public - License for more details. - - You should have received a copy of the GNU General Public License - along with wasmbuilder. If not, see . - */ - - class CodeBuilder { - constructor(func) { - this.func = func; - this.functionName = func.functionName; - this.module = func.module; - } - - setLocal(localName, valCode) { - const idx = this.func.localIdxByName[localName]; - if (idx === undefined) - throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${localName} `); - return [...valCode, 0x21, ...varuint32( idx )]; - } - - teeLocal(localName, valCode) { - const idx = this.func.localIdxByName[localName]; - if (idx === undefined) - throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${localName} `); - return [...valCode, 0x22, ...varuint32( idx )]; - } - - getLocal(localName) { - const idx = this.func.localIdxByName[localName]; - if (idx === undefined) - throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${localName} `); - return [0x20, ...varuint32( idx )]; - } - - i64_load8_s(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 0 : _align; // 8 bits alignment by default - return [...idxCode, 0x30, align, ...varuint32(offset)]; - } - - i64_load8_u(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 0 : _align; // 8 bits alignment by default - return [...idxCode, 0x31, align, ...varuint32(offset)]; - } - - i64_load16_s(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 1 : _align; // 16 bits alignment by default - return [...idxCode, 0x32, align, ...varuint32(offset)]; - } - - i64_load16_u(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 1 : _align; // 16 bits alignment by default - return [...idxCode, 0x33, align, ...varuint32(offset)]; - } - - i64_load32_s(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 2 : _align; // 32 bits alignment by default - return [...idxCode, 0x34, align, ...varuint32(offset)]; - } - - i64_load32_u(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 2 : _align; // 32 bits alignment by default - return [...idxCode, 0x35, align, ...varuint32(offset)]; - } - - i64_load(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 3 : _align; // 64 bits alignment by default - return [...idxCode, 0x29, align, ...varuint32(offset)]; - } - - - i64_store(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 3; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 3; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x37, align, ...varuint32(offset)]; - } - - i64_store32(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 2; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 2; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x3e, align, ...varuint32(offset)]; - } - - - i64_store16(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 1; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 1; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x3d, align, ...varuint32(offset)]; - } - - - i64_store8(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 0; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 0; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x3c, align, ...varuint32(offset)]; - } - - i32_load8_s(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 0 : _align; // 32 bits alignment by default - return [...idxCode, 0x2c, align, ...varuint32(offset)]; - } - - i32_load8_u(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 0 : _align; // 32 bits alignment by default - return [...idxCode, 0x2d, align, ...varuint32(offset)]; - } - - i32_load16_s(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 1 : _align; // 32 bits alignment by default - return [...idxCode, 0x2e, align, ...varuint32(offset)]; - } - - i32_load16_u(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 1 : _align; // 32 bits alignment by default - return [...idxCode, 0x2f, align, ...varuint32(offset)]; - } - - i32_load(idxCode, _offset, _align) { - const offset = _offset || 0; - const align = (_align === undefined) ? 2 : _align; // 32 bits alignment by default - return [...idxCode, 0x28, align, ...varuint32(offset)]; - } - - i32_store(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 2; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 2; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x36, align, ...varuint32(offset)]; - } - - - i32_store16(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 1; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 1; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x3b, align, ...varuint32(offset)]; - } - - i32_store8(idxCode, _offset, _align, _codeVal) { - let offset, align, codeVal; - if (Array.isArray(_offset)) { - offset = 0; - align = 0; - codeVal = _offset; - } else if (Array.isArray(_align)) { - offset = _offset; - align = 0; - codeVal = _align; - } else if (Array.isArray(_codeVal)) { - offset = _offset; - align = _align; - codeVal = _codeVal; - } - return [...idxCode, ...codeVal, 0x3a, align, ...varuint32(offset)]; - } - - call(fnName, ...args) { - const idx = this.module.functionIdxByName[fnName]; - if (idx === undefined) - throw new Error(`Function not defined: Function: ${fnName}`); - return [...[].concat(...args), 0x10, ...varuint32(idx)]; - } - - call_indirect(fnIdx, ...args) { - return [...[].concat(...args), ...fnIdx, 0x11, 0, 0]; - } - - if(condCode, thenCode, elseCode) { - if (elseCode) { - return [...condCode, 0x04, 0x40, ...thenCode, 0x05, ...elseCode, 0x0b]; - } else { - return [...condCode, 0x04, 0x40, ...thenCode, 0x0b]; - } - } - - block(bCode) { return [0x02, 0x40, ...bCode, 0x0b]; } - loop(...args) { - return [0x03, 0x40, ...[].concat(...[...args]), 0x0b]; - } - br_if(relPath, condCode) { return [...condCode, 0x0d, ...varuint32(relPath)]; } - br(relPath) { return [0x0c, ...varuint32(relPath)]; } - ret(rCode) { return [...rCode, 0x0f]; } - drop(dCode) { return [...dCode, 0x1a]; } - - i64_const(num) { return [0x42, ...varint64(num)]; } - i32_const(num) { return [0x41, ...varint32(num)]; } - - - i64_eqz(opcode) { return [...opcode, 0x50]; } - i64_eq(op1code, op2code) { return [...op1code, ...op2code, 0x51]; } - i64_ne(op1code, op2code) { return [...op1code, ...op2code, 0x52]; } - i64_lt_s(op1code, op2code) { return [...op1code, ...op2code, 0x53]; } - i64_lt_u(op1code, op2code) { return [...op1code, ...op2code, 0x54]; } - i64_gt_s(op1code, op2code) { return [...op1code, ...op2code, 0x55]; } - i64_gt_u(op1code, op2code) { return [...op1code, ...op2code, 0x56]; } - i64_le_s(op1code, op2code) { return [...op1code, ...op2code, 0x57]; } - i64_le_u(op1code, op2code) { return [...op1code, ...op2code, 0x58]; } - i64_ge_s(op1code, op2code) { return [...op1code, ...op2code, 0x59]; } - i64_ge_u(op1code, op2code) { return [...op1code, ...op2code, 0x5a]; } - i64_add(op1code, op2code) { return [...op1code, ...op2code, 0x7c]; } - i64_sub(op1code, op2code) { return [...op1code, ...op2code, 0x7d]; } - i64_mul(op1code, op2code) { return [...op1code, ...op2code, 0x7e]; } - i64_div_s(op1code, op2code) { return [...op1code, ...op2code, 0x7f]; } - i64_div_u(op1code, op2code) { return [...op1code, ...op2code, 0x80]; } - i64_rem_s(op1code, op2code) { return [...op1code, ...op2code, 0x81]; } - i64_rem_u(op1code, op2code) { return [...op1code, ...op2code, 0x82]; } - i64_and(op1code, op2code) { return [...op1code, ...op2code, 0x83]; } - i64_or(op1code, op2code) { return [...op1code, ...op2code, 0x84]; } - i64_xor(op1code, op2code) { return [...op1code, ...op2code, 0x85]; } - i64_shl(op1code, op2code) { return [...op1code, ...op2code, 0x86]; } - i64_shr_s(op1code, op2code) { return [...op1code, ...op2code, 0x87]; } - i64_shr_u(op1code, op2code) { return [...op1code, ...op2code, 0x88]; } - i64_extend_i32_s(op1code) { return [...op1code, 0xac]; } - i64_extend_i32_u(op1code) { return [...op1code, 0xad]; } - i64_clz(op1code) { return [...op1code, 0x79]; } - i64_ctz(op1code) { return [...op1code, 0x7a]; } - - i32_eqz(op1code) { return [...op1code, 0x45]; } - i32_eq(op1code, op2code) { return [...op1code, ...op2code, 0x46]; } - i32_ne(op1code, op2code) { return [...op1code, ...op2code, 0x47]; } - i32_lt_s(op1code, op2code) { return [...op1code, ...op2code, 0x48]; } - i32_lt_u(op1code, op2code) { return [...op1code, ...op2code, 0x49]; } - i32_gt_s(op1code, op2code) { return [...op1code, ...op2code, 0x4a]; } - i32_gt_u(op1code, op2code) { return [...op1code, ...op2code, 0x4b]; } - i32_le_s(op1code, op2code) { return [...op1code, ...op2code, 0x4c]; } - i32_le_u(op1code, op2code) { return [...op1code, ...op2code, 0x4d]; } - i32_ge_s(op1code, op2code) { return [...op1code, ...op2code, 0x4e]; } - i32_ge_u(op1code, op2code) { return [...op1code, ...op2code, 0x4f]; } - i32_add(op1code, op2code) { return [...op1code, ...op2code, 0x6a]; } - i32_sub(op1code, op2code) { return [...op1code, ...op2code, 0x6b]; } - i32_mul(op1code, op2code) { return [...op1code, ...op2code, 0x6c]; } - i32_div_s(op1code, op2code) { return [...op1code, ...op2code, 0x6d]; } - i32_div_u(op1code, op2code) { return [...op1code, ...op2code, 0x6e]; } - i32_rem_s(op1code, op2code) { return [...op1code, ...op2code, 0x6f]; } - i32_rem_u(op1code, op2code) { return [...op1code, ...op2code, 0x70]; } - i32_and(op1code, op2code) { return [...op1code, ...op2code, 0x71]; } - i32_or(op1code, op2code) { return [...op1code, ...op2code, 0x72]; } - i32_xor(op1code, op2code) { return [...op1code, ...op2code, 0x73]; } - i32_shl(op1code, op2code) { return [...op1code, ...op2code, 0x74]; } - i32_shr_s(op1code, op2code) { return [...op1code, ...op2code, 0x75]; } - i32_shr_u(op1code, op2code) { return [...op1code, ...op2code, 0x76]; } - i32_rotl(op1code, op2code) { return [...op1code, ...op2code, 0x77]; } - i32_rotr(op1code, op2code) { return [...op1code, ...op2code, 0x78]; } - i32_wrap_i64(op1code) { return [...op1code, 0xa7]; } - i32_clz(op1code) { return [...op1code, 0x67]; } - i32_ctz(op1code) { return [...op1code, 0x68]; } - - unreachable() { return [ 0x0 ]; } - - current_memory() { return [ 0x3f, 0]; } - - comment() { return []; } - } - - /* - Copyright 2019 0KIMS association. - - This file is part of wasmbuilder - - wasmbuilder is a free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - wasmbuilder is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public - License for more details. - - You should have received a copy of the GNU General Public License - along with wasmbuilder. If not, see . - */ - - const typeCodes = { - "i32": 0x7f, - "i64": 0x7e, - "f32": 0x7d, - "f64": 0x7c, - "anyfunc": 0x70, - "func": 0x60, - "emptyblock": 0x40 - }; - - - class FunctionBuilder { - - constructor (module, fnName, fnType, moduleName, fieldName) { - if (fnType == "import") { - this.fnType = "import"; - this.moduleName = moduleName; - this.fieldName = fieldName; - } else if (fnType == "internal") { - this.fnType = "internal"; - } else { - throw new Error("Invalid function fnType: " + fnType); - } - this.module = module; - this.fnName = fnName; - this.params = []; - this.locals = []; - this.localIdxByName = {}; - this.code = []; - this.returnType = null; - this.nextLocal =0; - } - - addParam(paramName, paramType) { - if (this.localIdxByName[paramName]) - throw new Error(`param already exists. Function: ${this.fnName}, Param: ${paramName} `); - const idx = this.nextLocal++; - this.localIdxByName[paramName] = idx; - this.params.push({ - type: paramType - }); - } - - addLocal(localName, localType, _length) { - const length = _length || 1; - if (this.localIdxByName[localName]) - throw new Error(`local already exists. Function: ${this.fnName}, Param: ${localName} `); - const idx = this.nextLocal++; - this.localIdxByName[localName] = idx; - this.locals.push({ - type: localType, - length: length - }); - } - - setReturnType(returnType) { - if (this.returnType) - throw new Error(`returnType already defined. Function: ${this.fnName}`); - this.returnType = returnType; - } - - getSignature() { - const params = [...varuint32(this.params.length), ...this.params.map((p) => typeCodes[p.type])]; - const returns = this.returnType ? [0x01, typeCodes[this.returnType]] : [0]; - return [0x60, ...params, ...returns]; - } - - getBody() { - const locals = this.locals.map((l) => [ - ...varuint32(l.length), - typeCodes[l.type] - ]); - - const body = [ - ...varuint32(this.locals.length), - ...[].concat(...locals), - ...this.code, - 0x0b - ]; - return [ - ...varuint32(body.length), - ...body - ]; - } - - addCode(...code) { - this.code.push(...[].concat(...[...code])); - } - - getCodeBuilder() { - return new CodeBuilder(this); - } - } - - /* - Copyright 2019 0KIMS association. - - This file is part of wasmbuilder - - wasmbuilder is a free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - wasmbuilder is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public - License for more details. - - You should have received a copy of the GNU General Public License - along with wasmbuilder. If not, see . - */ - - class ModuleBuilder { - - constructor() { - this.functions = []; - this.functionIdxByName = {}; - this.nImportFunctions = 0; - this.nInternalFunctions =0; - this.memory = { - pagesSize: 1, - moduleName: "env", - fieldName: "memory" - }; - this.free = 8; - this.datas = []; - this.modules = {}; - this.exports = []; - this.functionsTable = []; - } - - build() { - this._setSignatures(); - return new Uint8Array([ - ...u32(0x6d736100), - ...u32(1), - ...this._buildType(), - ...this._buildImport(), - ...this._buildFunctionDeclarations(), - ...this._buildFunctionsTable(), - ...this._buildExports(), - ...this._buildElements(), - ...this._buildCode(), - ...this._buildData() - ]); - } - - addFunction(fnName) { - if (typeof(this.functionIdxByName[fnName]) !== "undefined") - throw new Error(`Function already defined: ${fnName}`); - - const idx = this.functions.length; - this.functionIdxByName[fnName] = idx; - - this.functions.push(new FunctionBuilder(this, fnName, "internal")); - - this.nInternalFunctions++; - return this.functions[idx]; - } - - addIimportFunction(fnName, moduleName, _fieldName) { - if (typeof(this.functionIdxByName[fnName]) !== "undefined") - throw new Error(`Function already defined: ${fnName}`); - - if ( (this.functions.length>0) - &&(this.functions[this.functions.length-1].type == "internal")) - throw new Error(`Import functions must be declared before internal: ${fnName}`); - - let fieldName = _fieldName || fnName; - - const idx = this.functions.length; - this.functionIdxByName[fnName] = idx; - - this.functions.push(new FunctionBuilder(this, fnName, "import", moduleName, fieldName)); - - this.nImportFunctions ++; - return this.functions[idx]; - } - - setMemory(pagesSize, moduleName, fieldName) { - this.memory = { - pagesSize: pagesSize, - moduleName: moduleName || "env", - fieldName: fieldName || "memory" - }; - } - - exportFunction(fnName, _exportName) { - const exportName = _exportName || fnName; - if (typeof(this.functionIdxByName[fnName]) === "undefined") - throw new Error(`Function not defined: ${fnName}`); - const idx = this.functionIdxByName[fnName]; - if (exportName != fnName) { - this.functionIdxByName[exportName] = idx; - } - this.exports.push({ - exportName: exportName, - idx: idx - }); - } - - addFunctionToTable(fnName) { - const idx = this.functionIdxByName[fnName]; - this.functionsTable.push(idx); - } - - addData(offset, bytes) { - this.datas.push({ - offset: offset, - bytes: bytes - }); - } - - alloc(a, b) { - let size; - let bytes; - if ((Array.isArray(a) || ArrayBuffer.isView(a)) && (typeof(b) === "undefined")) { - size = a.length; - bytes = a; - } else { - size = a; - bytes = b; - } - size = (((size-1)>>3) +1)<<3; // Align to 64 bits. - const p = this.free; - this.free += size; - if (bytes) { - this.addData(p, bytes); - } - return p; - } - - allocString(s) { - const encoder = new globalThis.TextEncoder(); - const uint8array = encoder.encode(s); - return this.alloc([...uint8array, 0]); - } - - _setSignatures() { - this.signatures = []; - const signatureIdxByName = {}; - if (this.functionsTable.length>0) { - const signature = this.functions[this.functionsTable[0]].getSignature(); - const signatureName = "s_"+toHexString(signature); - signatureIdxByName[signatureName] = 0; - this.signatures.push(signature); - } - for (let i=0; i { - self.pendingLoads.push({ - page: p, - resolve: resolve, - reject: reject - }); - }); - self.__statusPage("After Load request: ", p); - return P; - } - - __statusPage(s, p) { - const logEntry = []; - const self=this; - if (!self.logHistory) return; - logEntry.push("==" + s+ " " +p); - let S = ""; - for (let i=0; i " + self.history[p][i][j]); - } - } - } - - - - _triggerLoad() { - const self = this; - - if (self.reading) return; - if (self.pendingLoads.length==0) return; - - const pageIdxs = Object.keys(self.pages); - - const deletablePages = []; - for (let i=0; i0) && - ( (typeof self.pages[self.pendingLoads[0].page] != "undefined" ) - ||( (freePages>0) - ||(deletablePages.length>0)))) { - const load = self.pendingLoads.shift(); - if (typeof self.pages[load.page] != "undefined") { - self.pages[load.page].pendingOps ++; - const idx = deletablePages.indexOf(load.page); - if (idx>=0) deletablePages.splice(idx, 1); - if (self.pages[load.page].loading) { - self.pages[load.page].loading.push(load); - } else { - load.resolve(); - } - self.__statusPage("After Load (cached): ", load.page); - - } else { - if (freePages) { - freePages--; - } else { - const fp = deletablePages.shift(); - self.__statusPage("Before Unload: ", fp); - self.avBuffs.unshift(self.pages[fp]); - delete self.pages[fp]; - self.__statusPage("After Unload: ", fp); - } - - if (load.page>=self.totalPages) { - self.pages[load.page] = getNewPage(); - load.resolve(); - self.__statusPage("After Load (new): ", load.page); - } else { - self.reading = true; - self.pages[load.page] = getNewPage(); - self.pages[load.page].loading = [load]; - ops.push(self.fd.read(self.pages[load.page].buff, 0, self.pageSize, load.page*self.pageSize).then((res)=> { - self.pages[load.page].size = res.bytesRead; - const loading = self.pages[load.page].loading; - delete self.pages[load.page].loading; - for (let i=0; i { - load.reject(err); - })); - self.__statusPage("After Load (loading): ", load.page); - } - } - } - // if (ops.length>1) console.log(ops.length); - - Promise.all(ops).then( () => { - self.reading = false; - if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self)); - self._tryClose(); - }); - - function getNewPage() { - if (self.avBuffs.length>0) { - const p = self.avBuffs.shift(); - p.dirty = false; - p.pendingOps = 1; - p.size =0; - return p; - } else { - return { - dirty: false, - buff: new Uint8Array(self.pageSize), - pendingOps: 1, - size: 0 - }; - } - } - - } - - - _triggerWrite() { - const self = this; - if (self.writing) return; - - const pageIdxs = Object.keys(self.pages); - - const ops = []; - - for (let i=0; i { - page.writing = false; - return; - }, (err) => { - console.log("ERROR Writing: "+err); - self.error = err; - self._tryClose(); - })); - } - } - - if (self.writing) { - Promise.all(ops).then( () => { - self.writing = false; - setImmediate(self._triggerWrite.bind(self)); - self._tryClose(); - if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self)); - }); - } - } - - _getDirtyPage() { - for (let p in this.pages) { - if (this.pages[p].dirty) return p; - } - return -1; - } - - async write(buff, pos) { - if (buff.byteLength == 0) return; - const self = this; - /* - if (buff.byteLength > self.pageSize*self.maxPagesLoaded*0.8) { - const cacheSize = Math.floor(buff.byteLength * 1.1); - this.maxPagesLoaded = Math.floor( cacheSize / self.pageSize)+1; - } - */ - if (typeof pos == "undefined") pos = self.pos; - self.pos = pos+buff.byteLength; - if (self.totalSize < pos + buff.byteLength) self.totalSize = pos + buff.byteLength; - if (self.pendingClose) - throw new Error("Writing a closing file"); - const firstPage = Math.floor(pos / self.pageSize); - const lastPage = Math.floor((pos + buff.byteLength -1) / self.pageSize); - - const pagePromises = []; - for (let i=firstPage; i<=lastPage; i++) pagePromises.push(self._loadPage(i)); - self._triggerLoad(); - - let p = firstPage; - let o = pos % self.pageSize; - let r = buff.byteLength; - while (r>0) { - await pagePromises[p-firstPage]; - const l = (o+r > self.pageSize) ? (self.pageSize -o) : r; - const srcView = buff.slice( buff.byteLength - r, buff.byteLength - r + l); - const dstView = new Uint8Array(self.pages[p].buff.buffer, o, l); - dstView.set(srcView); - self.pages[p].dirty = true; - self.pages[p].pendingOps --; - self.pages[p].size = Math.max(o+l, self.pages[p].size); - if (p>=self.totalPages) { - self.totalPages = p+1; - } - r = r-l; - p ++; - o = 0; - if (!self.writing) setImmediate(self._triggerWrite.bind(self)); - } - } - - async read(len, pos) { - const self = this; - let buff = new Uint8Array(len); - await self.readToBuffer(buff, 0, len, pos); - - return buff; - } - - async readToBuffer(buffDst, offset, len, pos) { - if (len == 0) { - return; - } - const self = this; - if (len > self.pageSize*self.maxPagesLoaded*0.8) { - const cacheSize = Math.floor(len * 1.1); - this.maxPagesLoaded = Math.floor( cacheSize / self.pageSize)+1; - } - if (typeof pos == "undefined") pos = self.pos; - self.pos = pos+len; - if (self.pendingClose) - throw new Error("Reading a closing file"); - const firstPage = Math.floor(pos / self.pageSize); - const lastPage = Math.floor((pos + len -1) / self.pageSize); - - const pagePromises = []; - for (let i=firstPage; i<=lastPage; i++) pagePromises.push(self._loadPage(i)); - - self._triggerLoad(); - - let p = firstPage; - let o = pos % self.pageSize; - // Remaining bytes to read - let r = pos + len > self.totalSize ? len - (pos + len - self.totalSize): len; - while (r>0) { - await pagePromises[p - firstPage]; - self.__statusPage("After Await (read): ", p); - - // bytes to copy from this page - const l = (o+r > self.pageSize) ? (self.pageSize -o) : r; - const srcView = new Uint8Array(self.pages[p].buff.buffer, self.pages[p].buff.byteOffset + o, l); - buffDst.set(srcView, offset+len-r); - self.pages[p].pendingOps --; - - self.__statusPage("After Op done: ", p); - - r = r-l; - p ++; - o = 0; - if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self)); - } - - this.pos = pos + len; - - } - - - _tryClose() { - const self = this; - if (!self.pendingClose) return; - if (self.error) { - self.pendingCloseReject(self.error); - } - const p = self._getDirtyPage(); - if ((p>=0) || (self.writing) || (self.reading) || (self.pendingLoads.length>0)) return; - self.pendingClose(); - } - - close() { - const self = this; - if (self.pendingClose) - throw new Error("Closing the file twice"); - return new Promise((resolve, reject) => { - self.pendingClose = resolve; - self.pendingCloseReject = reject; - self._tryClose(); - }).then(()=> { - self.fd.close(); - }, (err) => { - self.fd.close(); - throw (err); - }); - } - - async discard() { - const self = this; - await self.close(); - await fs.promises.unlink(this.fileName); - } - - async writeULE32(v, pos) { - const self = this; - const tmpBuff32 = new Uint8Array(4); - const tmpBuff32v = new DataView(tmpBuff32.buffer); - - tmpBuff32v.setUint32(0, v, true); - - await self.write(tmpBuff32, pos); - } - - async writeUBE32(v, pos) { - const self = this; - - const tmpBuff32 = new Uint8Array(4); - const tmpBuff32v = new DataView(tmpBuff32.buffer); - - tmpBuff32v.setUint32(0, v, false); - - await self.write(tmpBuff32, pos); - } - - - async writeULE64(v, pos) { - const self = this; - - const tmpBuff64 = new Uint8Array(8); - const tmpBuff64v = new DataView(tmpBuff64.buffer); - - tmpBuff64v.setUint32(0, v & 0xFFFFFFFF, true); - tmpBuff64v.setUint32(4, Math.floor(v / 0x100000000) , true); - - await self.write(tmpBuff64, pos); - } - - async readULE32(pos) { - const self = this; - const b = await self.read(4, pos); - - const view = new Uint32Array(b.buffer); - - return view[0]; - } - - async readUBE32(pos) { - const self = this; - const b = await self.read(4, pos); - - const view = new DataView(b.buffer); - - return view.getUint32(0, false); - } - - async readULE64(pos) { - const self = this; - const b = await self.read(8, pos); - - const view = new Uint32Array(b.buffer); - - return view[1] * 0x100000000 + view[0]; - } - - async readString(pos) { - const self = this; - - if (self.pendingClose) { - throw new Error("Reading a closing file"); - } - - let currentPosition = typeof pos == "undefined" ? self.pos : pos; - let currentPage = Math.floor(currentPosition / self.pageSize); - - let endOfStringFound = false; - let str = ""; - - while (!endOfStringFound) { - //Read page - let pagePromise = self._loadPage(currentPage); - self._triggerLoad(); - await pagePromise; - self.__statusPage("After Await (read): ", currentPage); - - let offsetOnPage = currentPosition % self.pageSize; - - const dataArray = new Uint8Array( - self.pages[currentPage].buff.buffer, - self.pages[currentPage].buff.byteOffset + offsetOnPage, - self.pageSize - offsetOnPage - ); - - let indexEndOfString = dataArray.findIndex(element => element === 0); - endOfStringFound = indexEndOfString !== -1; - - if (endOfStringFound) { - str += new TextDecoder().decode(dataArray.slice(0, indexEndOfString)); - self.pos = currentPage * this.pageSize + offsetOnPage + indexEndOfString + 1; - } else { - str += new TextDecoder().decode(dataArray); - self.pos = currentPage * this.pageSize + offsetOnPage + dataArray.length; - } - - self.pages[currentPage].pendingOps--; - self.__statusPage("After Op done: ", currentPage); - - currentPosition = self.pos; - currentPage++; - - if (self.pendingLoads.length > 0) setImmediate(self._triggerLoad.bind(self)); - } - - return str; - } - } - - function createNew$1(o) { - const initialSize = o.initialSize || 1<<20; - const fd = new MemFile(); - fd.o = o; - fd.o.data = new Uint8Array(initialSize); - fd.allocSize = initialSize; - fd.totalSize = 0; - fd.readOnly = false; - fd.pos = 0; - return fd; - } - - function readExisting$2(o) { - const fd = new MemFile(); - fd.o = o; - fd.allocSize = o.data.byteLength; - fd.totalSize = o.data.byteLength; - fd.readOnly = true; - fd.pos = 0; - return fd; - } - - const tmpBuff32$1 = new Uint8Array(4); - const tmpBuff32v$1 = new DataView(tmpBuff32$1.buffer); - const tmpBuff64$1 = new Uint8Array(8); - const tmpBuff64v$1 = new DataView(tmpBuff64$1.buffer); - - class MemFile { - - constructor() { - this.pageSize = 1 << 14; // for compatibility - } - - _resizeIfNeeded(newLen) { - if (newLen > this.allocSize) { - const newAllocSize = Math.max( - this.allocSize + (1 << 20), - Math.floor(this.allocSize * 1.1), - newLen - ); - const newData = new Uint8Array(newAllocSize); - newData.set(this.o.data); - this.o.data = newData; - this.allocSize = newAllocSize; - } - } - - async write(buff, pos) { - const self =this; - if (typeof pos == "undefined") pos = self.pos; - if (this.readOnly) throw new Error("Writing a read only file"); - - this._resizeIfNeeded(pos + buff.byteLength); - - this.o.data.set(buff.slice(), pos); - - if (pos + buff.byteLength > this.totalSize) this.totalSize = pos + buff.byteLength; - - this.pos = pos + buff.byteLength; - } - - async readToBuffer(buffDest, offset, len, pos) { - const self = this; - if (typeof pos == "undefined") pos = self.pos; - if (this.readOnly) { - if (pos + len > this.totalSize) throw new Error("Reading out of bounds"); - } - this._resizeIfNeeded(pos + len); - - const buffSrc = new Uint8Array(this.o.data.buffer, this.o.data.byteOffset + pos, len); - - buffDest.set(buffSrc, offset); - - this.pos = pos + len; - } - - async read(len, pos) { - const self = this; - - const buff = new Uint8Array(len); - await self.readToBuffer(buff, 0, len, pos); - - return buff; - } - - close() { - if (this.o.data.byteLength != this.totalSize) { - this.o.data = this.o.data.slice(0, this.totalSize); - } - } - - async discard() { - } - - - async writeULE32(v, pos) { - const self = this; - - tmpBuff32v$1.setUint32(0, v, true); - - await self.write(tmpBuff32$1, pos); - } - - async writeUBE32(v, pos) { - const self = this; - - tmpBuff32v$1.setUint32(0, v, false); - - await self.write(tmpBuff32$1, pos); - } - - - async writeULE64(v, pos) { - const self = this; - - tmpBuff64v$1.setUint32(0, v & 0xFFFFFFFF, true); - tmpBuff64v$1.setUint32(4, Math.floor(v / 0x100000000) , true); - - await self.write(tmpBuff64$1, pos); - } - - - async readULE32(pos) { - const self = this; - const b = await self.read(4, pos); - - const view = new Uint32Array(b.buffer); - - return view[0]; - } - - async readUBE32(pos) { - const self = this; - const b = await self.read(4, pos); - - const view = new DataView(b.buffer); - - return view.getUint32(0, false); - } - - async readULE64(pos) { - const self = this; - const b = await self.read(8, pos); - - const view = new Uint32Array(b.buffer); - - return view[1] * 0x100000000 + view[0]; - } - - async readString(pos) { - const self = this; - - let currentPosition = typeof pos == "undefined" ? self.pos : pos; - - if (currentPosition > this.totalSize) { - if (this.readOnly) { - throw new Error("Reading out of bounds"); - } - this._resizeIfNeeded(pos); - } - const dataArray = new Uint8Array( - self.o.data.buffer, - currentPosition, - this.totalSize - currentPosition - ); - - let indexEndOfString = dataArray.findIndex(element => element === 0); - let endOfStringFound = indexEndOfString !== -1; - - let str = ""; - if (endOfStringFound) { - str = new TextDecoder().decode(dataArray.slice(0, indexEndOfString)); - self.pos = currentPosition + indexEndOfString + 1; - } else { - self.pos = currentPosition; - } - return str; - } - } - - const PAGE_SIZE = 1<<22; - - function createNew(o) { - const initialSize = o.initialSize || 0; - const fd = new BigMemFile(); - fd.o = o; - const nPages = initialSize ? Math.floor((initialSize - 1) / PAGE_SIZE)+1 : 0; - fd.o.data = []; - for (let i=0; i0) { - const l = (o+r > PAGE_SIZE) ? (PAGE_SIZE -o) : r; - const srcView = buff.slice(buff.byteLength - r, buff.byteLength - r + l); - const dstView = new Uint8Array(self.o.data[p].buffer, o, l); - dstView.set(srcView); - r = r-l; - p ++; - o = 0; - } - - this.pos = pos + buff.byteLength; - } - - async readToBuffer(buffDst, offset, len, pos) { - const self = this; - if (typeof pos == "undefined") pos = self.pos; - if (this.readOnly) { - if (pos + len > this.totalSize) throw new Error("Reading out of bounds"); - } - this._resizeIfNeeded(pos + len); - - const firstPage = Math.floor(pos / PAGE_SIZE); - - let p = firstPage; - let o = pos % PAGE_SIZE; - // Remaining bytes to read - let r = len; - while (r>0) { - // bytes to copy from this page - const l = (o+r > PAGE_SIZE) ? (PAGE_SIZE -o) : r; - const srcView = new Uint8Array(self.o.data[p].buffer, o, l); - buffDst.set(srcView, offset+len-r); - r = r-l; - p ++; - o = 0; - } - - this.pos = pos + len; - } - - async read(len, pos) { - const self = this; - const buff = new Uint8Array(len); - - await self.readToBuffer(buff, 0, len, pos); - - return buff; - } - - close() { - } - - async discard() { - } - - - async writeULE32(v, pos) { - const self = this; - - tmpBuff32v.setUint32(0, v, true); - - await self.write(tmpBuff32, pos); - } - - async writeUBE32(v, pos) { - const self = this; - - tmpBuff32v.setUint32(0, v, false); - - await self.write(tmpBuff32, pos); - } - - - async writeULE64(v, pos) { - const self = this; - - tmpBuff64v.setUint32(0, v & 0xFFFFFFFF, true); - tmpBuff64v.setUint32(4, Math.floor(v / 0x100000000) , true); - - await self.write(tmpBuff64, pos); - } - - - async readULE32(pos) { - const self = this; - const b = await self.read(4, pos); - - const view = new Uint32Array(b.buffer); - - return view[0]; - } - - async readUBE32(pos) { - const self = this; - const b = await self.read(4, pos); - - const view = new DataView(b.buffer); - - return view.getUint32(0, false); - } - - async readULE64(pos) { - const self = this; - const b = await self.read(8, pos); - - const view = new Uint32Array(b.buffer); - - return view[1] * 0x100000000 + view[0]; - } - - async readString(pos) { - const self = this; - const fixedSize = 2048; - - let currentPosition = typeof pos == "undefined" ? self.pos : pos; - - if (currentPosition > this.totalSize) { - if (this.readOnly) { - throw new Error("Reading out of bounds"); - } - this._resizeIfNeeded(pos); - } - - let endOfStringFound = false; - let str = ""; - - while (!endOfStringFound) { - let currentPage = Math.floor(currentPosition / PAGE_SIZE); - let offsetOnPage = currentPosition % PAGE_SIZE; - - if (self.o.data[currentPage] === undefined) { - throw new Error("ERROR"); - } - - let readLength = Math.min(fixedSize, self.o.data[currentPage].length - offsetOnPage); - const dataArray = new Uint8Array(self.o.data[currentPage].buffer, offsetOnPage, readLength); - - let indexEndOfString = dataArray.findIndex(element => element === 0); - endOfStringFound = indexEndOfString !== -1; - - if (endOfStringFound) { - str += new TextDecoder().decode(dataArray.slice(0, indexEndOfString)); - self.pos = currentPage * PAGE_SIZE + offsetOnPage + indexEndOfString + 1; - } else { - str += new TextDecoder().decode(dataArray); - self.pos = currentPage * PAGE_SIZE + offsetOnPage + dataArray.length; - } - - currentPosition = self.pos; - } - return str; - } - } - - const O_TRUNC = 1024; - const O_CREAT = 512; - const O_RDWR = 2; - const O_RDONLY = 0; - - /* global fetch */ - - const DEFAULT_CACHE_SIZE = (1 << 16); - const DEFAULT_PAGE_SIZE = (1 << 13); - - - async function createOverride(o, b, c) { - if (typeof o === "string") { - o = { - type: "file", - fileName: o, - cacheSize: b || DEFAULT_CACHE_SIZE, - pageSize: c || DEFAULT_PAGE_SIZE - }; - } - if (o.type == "file") { - return await open(o.fileName, O_TRUNC | O_CREAT | O_RDWR, o.cacheSize, o.pageSize); - } else if (o.type == "mem") { - return createNew$1(o); - } else if (o.type == "bigMem") { - return createNew(o); - } else { - throw new Error("Invalid FastFile type: "+o.type); - } - } - - async function readExisting(o, b, c) { - if (o instanceof Uint8Array) { - o = { - type: "mem", - data: o - }; - } - { - if (typeof o === "string") { - const buff = await fetch(o).then( function(res) { - return res.arrayBuffer(); - }).then(function (ab) { - return new Uint8Array(ab); - }); - o = { - type: "mem", - data: buff - }; - } - } - if (o.type == "file") { - return await open(o.fileName, O_RDONLY, o.cacheSize, o.pageSize); - } else if (o.type == "mem") { - return await readExisting$2(o); - } else if (o.type == "bigMem") { - return await readExisting$1(o); - } else { - throw new Error("Invalid FastFile type: "+o.type); - } - } - - async function readBinFile(fileName, type, maxVersion, cacheSize, pageSize) { - - const fd = await readExisting(fileName); - - const b = await fd.read(4); - let readedType = ""; - for (let i=0; i<4; i++) readedType += String.fromCharCode(b[i]); - - if (readedType != type) throw new Error(fileName + ": Invalid File format"); - - let v = await fd.readULE32(); - - if (v>maxVersion) throw new Error("Version not supported"); - - const nSections = await fd.readULE32(); - - // Scan sections - let sections = []; - for (let i=0; i1) throw new Error(fd.fileName +": Section Duplicated " +idSection); - - fd.pos = sections[idSection][0].p; - - fd.readingSection = sections[idSection][0]; - } - - async function endReadSection(fd, noCheck) { - if (typeof fd.readingSection === "undefined") throw new Error("Not reading a section"); - if (!noCheck) { - if (fd.pos-fd.readingSection.p != fd.readingSection.size) throw new Error("Invalid section size reading"); - } - delete fd.readingSection; - } - - async function writeBigInt(fd, n, n8, pos) { - const buff = new Uint8Array(n8); - Scalar.toRprLE(buff, 0, n, n8); - await fd.write(buff, pos); - } - - async function readBigInt(fd, n8, pos) { - const buff = await fd.read(n8, pos); - return Scalar.fromRprLE(buff, 0, n8); - } - - async function copySection(fdFrom, sections, fdTo, sectionId, size) { - if (typeof size === "undefined") { - size = sections[sectionId][0].size; - } - const chunkSize = fdFrom.pageSize; - await startReadUniqueSection(fdFrom, sections, sectionId); - await startWriteSection(fdTo, sectionId); - for (let p=0; p sections[idSection][0].size) { - throw new Error("Reading out of the range of the section"); - } - - let buff; - if (length < (1 << 30) ) { - buff = new Uint8Array(length); - } else { - buff = new BigBuffer(length); - } - - await fd.readToBuffer(buff, 0, length, sections[idSection][0].p + offset); - return buff; - } - - async function sectionIsEqual(fd1, sections1, fd2, sections2, idSection) { - const MAX_BUFF_SIZE = fd1.pageSize * 16; - await startReadUniqueSection(fd1, sections1, idSection); - await startReadUniqueSection(fd2, sections2, idSection); - if (sections1[idSection][0].size != sections2[idSection][0].size) return false; - const totalBytes=sections1[idSection][0].size; - for (let i=0; i> 2) + i]; } - this.prime = Scalar.fromArray(arr, 0x100000000); + this.prime = Scalar$1.fromArray(arr, 0x100000000); - this.Fr = new ZqField(this.prime); + this.Fr = new ZqField$1(this.prime); - this.mask32 = Scalar.fromString("FFFFFFFF", 16); + this.mask32 = Scalar$1.fromString("FFFFFFFF", 16); this.NVars = this.instance.exports.getNVars(); this.n64 = Math.floor((this.Fr.bitLength - 1) / 64)+1; - this.R = this.Fr.e( Scalar.shiftLeft(1 , this.n64*64)); + this.R = this.Fr.e( Scalar$1.shiftLeft(1 , this.n64*64)); this.RInv = this.Fr.inv(this.R); this.sanityCheck = sanityCheck; } @@ -20246,7 +35869,7 @@ var snarkjs = (function (exports) { for (let i=0; i> 2)] = 0; self.i32[(p >> 2) + 1] = 0x80000000; - const arr = Scalar.toArray(v, 0x100000000); + const arr = Scalar$1.toArray(v, 0x100000000); for (let i=0; i>BigInt(e)}const c=r,d=s;function u(t){return(BigInt(t)&BigInt(1))==BigInt(1)}function g(t){let e=BigInt(t);const a=[];for(;e;)e&BigInt(1)?a.push(1):a.push(0),e>>=BigInt(1);return a}function f(t){if(t>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Number too big");return Number(t)}function h(t,e){return BigInt(t)+BigInt(e)}function _(t,e){return BigInt(t)-BigInt(e)}function p(t){return-BigInt(t)}function m(t,e){return BigInt(t)*BigInt(e)}function w(t,e){return BigInt(t)**BigInt(e)}function L(t,e){return BigInt(t)/BigInt(e)}function b(t,e){return BigInt(t)%BigInt(e)}function A(t,e){return BigInt(t)==BigInt(e)}function y(t,e){return BigInt(t)>BigInt(e)}function C(t,e){return BigInt(t)>=BigInt(e)}function I(t,e){return BigInt(t)&BigInt(e)}function F(t,e,a,i){const o="0000000"+a.toString(16),n=new Uint32Array(t.buffer,t.byteOffset+e,i/4),l=1+(4*(o.length-7)-1>>5);for(let t=0;t>5);for(let t=0;tn[n.length-e-1]=t.toString(16).padStart(8,"0"))),a(n.join(""),16)}function v(t,e,i){i=i||t.byteLength,e=e||0;const o=new DataView(t.buffer,t.byteOffset+e,i),n=new Array(i/4);for(let t=0;t>=BigInt(1)}return a},bits:g,toNumber:f,toArray:function(t,e){const a=[];let i=BigInt(t);for(e=BigInt(e);i;)a.unshift(Number(i%e)),i/=e;return a},add:h,sub:_,neg:p,mul:m,square:function(t){return BigInt(t)*BigInt(t)},pow:w,exp:function(t,e){return BigInt(t)**BigInt(e)},abs:function(t){return BigInt(t)>=0?BigInt(t):-BigInt(t)},div:L,mod:b,eq:A,neq:function(t,e){return BigInt(t)!=BigInt(e)},lt:function(t,e){return BigInt(t)=0;a--)o=t.square(o),i[a]&&(o=t.mul(o,e));return o}function z(t){if(t.m%2==1)if(A(b(t.p,4),1))if(A(b(t.p,8),1))if(A(b(t.p,16),1))!function(t){t.sqrt_q=w(t.p,t.m),t.sqrt_s=0,t.sqrt_t=_(t.sqrt_q,1);for(;!u(t.sqrt_t);)t.sqrt_s=t.sqrt_s+1,t.sqrt_t=L(t.sqrt_t,2);let e=t.one;for(;t.eq(e,t.one);){const a=t.random();t.sqrt_z=t.pow(a,t.sqrt_t),e=t.pow(t.sqrt_z,2**(t.sqrt_s-1))}t.sqrt_tm1d2=L(_(t.sqrt_t,1),2),t.sqrt=function(t){const e=this;if(e.isZero(t))return e.zero;let a=e.pow(t,e.sqrt_tm1d2);const i=e.pow(e.mul(e.square(a),t),2**(e.sqrt_s-1));if(e.eq(i,e.negone))return null;let o=e.sqrt_s,n=e.mul(t,a),l=e.mul(n,a),r=e.sqrt_z;for(;!e.eq(l,e.one);){let t=e.square(l),i=1;for(;!e.eq(t,e.one);)t=e.square(t),i++;a=r;for(let t=0;t>>0,t[o]=(t[o]^t[e])>>>0,t[o]=(t[o]<<16|t[o]>>>16&65535)>>>0,t[i]=t[i]+t[o]>>>0,t[a]=(t[a]^t[i])>>>0,t[a]=(t[a]<<12|t[a]>>>20&4095)>>>0,t[e]=t[e]+t[a]>>>0,t[o]=(t[o]^t[e])>>>0,t[o]=(t[o]<<8|t[o]>>>24&255)>>>0,t[i]=t[i]+t[o]>>>0,t[a]=(t[a]^t[i])>>>0,t[a]=(t[a]<<7|t[a]>>>25&127)>>>0}class Q{constructor(t){t=t||[0,0,0,0,0,0,0,0],this.state=[1634760805,857760878,2036477234,1797285236,t[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 h(m(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++)U(t=this.buff,0,4,8,12),U(t,1,5,9,13),U(t,2,6,10,14),U(t,3,7,11,15),U(t,0,5,10,15),U(t,1,6,11,12),U(t,2,7,8,13),U(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)))}}var q={};function M(t){let e=new Uint8Array(t);if(void 0!==globalThis.crypto)globalThis.crypto.getRandomValues(e);else for(let a=0;a>>0;return e}let k=null;function R(){return k||(k=new Q(function(){const t=M(32),e=new Uint32Array(t.buffer),a=[];for(let t=0;t<8;t++)a.push(e[t]);return a}()),k)}class D{constructor(t,e,a){this.F=e,this.G=t,this.opMulGF=a;let i=e.sqrt_t||e.t,o=e.sqrt_s||e.s,n=e.one;for(;e.eq(e.pow(n,e.half),e.one);)n=e.add(n,e.one);this.w=new Array(o+1),this.wi=new Array(o+1),this.w[o]=this.F.pow(n,i),this.wi[o]=this.F.inv(this.w[o]);let l=o-1;for(;l>=0;)this.w[l]=this.F.square(this.w[l+1]),this.wi[l]=this.F.square(this.wi[l+1]),l--;this.roots=[],this._setRoots(Math.min(o,15))}_setRoots(t){for(let e=t;e>=0&&!this.roots[e];e--){let t=this.F.one;const a=1<>1,r=$(t,e,a-1,i,2*o),s=$(t,e,a-1,i+o,2*o),c=new Array(n);for(let e=0;e>this.one,this.bitLength=o(this.p),this.mask=(this.one<>this.one;this.nqr=this.two;let a=this.pow(this.nqr,e);for(;!this.eq(a,this.negone);)this.nqr=this.nqr+this.one,a=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),z(this),this.FFT=new D(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 a;if(e?16==e&&(a=BigInt("0x"+t)):a=BigInt(t),a<0){let t=-a;return t>=this.p&&(t%=this.p),this.p-t}return a>=this.p?a%this.p:a}add(t,e){const a=t+e;return a>=this.p?a-this.p:a}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,a=this.p,i=this.one,o=t%this.p;for(;o;){let t=a/o;[e,i]=[i,e-t*i],[a,o]=[o,a-t*o]}return e=this.p?a-this.p:a}bor(t,e){const a=(t|e)&this.mask;return a>=this.p?a-this.p:a}bxor(t,e){const a=(t^e)&this.mask;return a>=this.p?a-this.p:a}bnot(t){const e=t^this.mask;return e>=this.p?e-this.p:e}shl(t,e){if(Number(e)=this.p?a-this.p:a}{const a=this.p-e;return Number(a)>a:this.zero}}shr(t,e){if(Number(e)>e;{const a=this.p-e;if(Number(a)=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,a=this.nqr_to_t,i=this.pow(t,this.t),o=this.pow(t,this.add(this.t,this.one)>>this.one);for(;i!=this.one;){let t=this.square(i),n=1;for(;t!=this.one;)n++,t=this.square(t);let l=a;for(let t=0;tthis.p>>this.one&&(o=this.neg(o)),o}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 a=0;athis.half&&10==e){a="-"+(this.p-t).toString(e)}else a=t.toString(e);return a}isZero(t){return t==this.zero}fromRng(t){let e;do{e=this.zero;for(let a=0;a=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,a){F(t,e,a,8*this.n64)}toRprBE(t,e,a){x(t,e,a,8*this.n64)}toRprBEM(t,e,a){return this.toRprBE(t,e,this.mul(this.R,a))}toRprLEM(t,e,a){return this.toRprLE(t,e,this.mul(this.R,a))}fromRprLE(t,e){return E(t,e,this.n8)}fromRprBE(t,e){return v(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}}var K="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},j={bigInt2BytesLE:function(t,e){const a=Array(e);let i=BigInt(t);for(let t=0;t>=8n;return a},bigInt2U32LE:function(t,e){const a=Array(e);let i=BigInt(t);for(let t=0;t>=32n;return a},isOcamNum:function(t){return!!Array.isArray(t)&&(3==t.length&&("number"==typeof t[0]&&("number"==typeof t[1]&&!!Array.isArray(t[2]))))}},H=function(t,e,a,i,o,n,l){const r=t.addFunction(e);r.addParam("base","i32"),r.addParam("scalar","i32"),r.addParam("scalarLength","i32"),r.addParam("r","i32"),r.addLocal("i","i32"),r.addLocal("b","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(a));r.addCode(s.if(s.i32_eqz(s.getLocal("scalarLength")),[...s.call(l,s.getLocal("r")),...s.ret([])])),r.addCode(s.call(n,s.getLocal("base"),c)),r.addCode(s.call(l,s.getLocal("r"))),r.addCode(s.setLocal("i",s.getLocal("scalarLength"))),r.addCode(s.block(s.loop(s.setLocal("i",s.i32_sub(s.getLocal("i"),s.i32_const(1))),s.setLocal("b",s.i32_load8_u(s.i32_add(s.getLocal("scalar"),s.getLocal("i")))),...function(){const t=[];for(let e=0;e<8;e++)t.push(...s.call(o,s.getLocal("r"),s.getLocal("r")),...s.if(s.i32_ge_u(s.getLocal("b"),s.i32_const(128>>e)),[...s.setLocal("b",s.i32_sub(s.getLocal("b"),s.i32_const(128>>e))),...s.call(i,s.getLocal("r"),c,s.getLocal("r"))]));return t}(),s.br_if(1,s.i32_eqz(s.getLocal("i"))),s.br(0))))},Z=function(t,e){const a=8*t.modules[e].n64,i=t.addFunction(e+"_batchInverse");i.addParam("pIn","i32"),i.addParam("inStep","i32"),i.addParam("n","i32"),i.addParam("pOut","i32"),i.addParam("outStep","i32"),i.addLocal("itAux","i32"),i.addLocal("itIn","i32"),i.addLocal("itOut","i32"),i.addLocal("i","i32");const o=i.getCodeBuilder(),n=o.i32_const(t.alloc(a));i.addCode(o.setLocal("itAux",o.i32_load(o.i32_const(0))),o.i32_store(o.i32_const(0),o.i32_add(o.getLocal("itAux"),o.i32_mul(o.i32_add(o.getLocal("n"),o.i32_const(1)),o.i32_const(a))))),i.addCode(o.call(e+"_one",o.getLocal("itAux")),o.setLocal("itIn",o.getLocal("pIn")),o.setLocal("itAux",o.i32_add(o.getLocal("itAux"),o.i32_const(a))),o.setLocal("i",o.i32_const(0)),o.block(o.loop(o.br_if(1,o.i32_eq(o.getLocal("i"),o.getLocal("n"))),o.if(o.call(e+"_isZero",o.getLocal("itIn")),o.call(e+"_copy",o.i32_sub(o.getLocal("itAux"),o.i32_const(a)),o.getLocal("itAux")),o.call(e+"_mul",o.getLocal("itIn"),o.i32_sub(o.getLocal("itAux"),o.i32_const(a)),o.getLocal("itAux"))),o.setLocal("itIn",o.i32_add(o.getLocal("itIn"),o.getLocal("inStep"))),o.setLocal("itAux",o.i32_add(o.getLocal("itAux"),o.i32_const(a))),o.setLocal("i",o.i32_add(o.getLocal("i"),o.i32_const(1))),o.br(0))),o.setLocal("itIn",o.i32_sub(o.getLocal("itIn"),o.getLocal("inStep"))),o.setLocal("itAux",o.i32_sub(o.getLocal("itAux"),o.i32_const(a))),o.setLocal("itOut",o.i32_add(o.getLocal("pOut"),o.i32_mul(o.i32_sub(o.getLocal("n"),o.i32_const(1)),o.getLocal("outStep")))),o.call(e+"_inverse",o.getLocal("itAux"),o.getLocal("itAux")),o.block(o.loop(o.br_if(1,o.i32_eqz(o.getLocal("i"))),o.if(o.call(e+"_isZero",o.getLocal("itIn")),[...o.call(e+"_copy",o.getLocal("itAux"),o.i32_sub(o.getLocal("itAux"),o.i32_const(a))),...o.call(e+"_zero",o.getLocal("itOut"))],[...o.call(e+"_copy",o.i32_sub(o.getLocal("itAux"),o.i32_const(a)),n),...o.call(e+"_mul",o.getLocal("itAux"),o.getLocal("itIn"),o.i32_sub(o.getLocal("itAux"),o.i32_const(a))),...o.call(e+"_mul",o.getLocal("itAux"),n,o.getLocal("itOut"))]),o.setLocal("itIn",o.i32_sub(o.getLocal("itIn"),o.getLocal("inStep"))),o.setLocal("itOut",o.i32_sub(o.getLocal("itOut"),o.getLocal("outStep"))),o.setLocal("itAux",o.i32_sub(o.getLocal("itAux"),o.i32_const(a))),o.setLocal("i",o.i32_sub(o.getLocal("i"),o.i32_const(1))),o.br(0)))),i.addCode(o.i32_store(o.i32_const(0),o.getLocal("itAux")))};var W=function(t,e,a,i,o,n){void 0===n&&(n=ie?1:-1}function tt(t){return t*t}function et(t){return t%2n!==0n}function at(t){return t%2n===0n}function it(t){return t<0n}function ot(t){return t>0n}function nt(t){return it(t)?t.toString(2).length-1:t.toString(2).length}function lt(t){return t<0n?-t:t}function rt(t){return 1n===lt(t)}function st(t,e){for(var a,i,o,n=0n,l=1n,r=e,s=lt(t);0n!==s;)a=r/s,i=n,o=r,n=l,r=s,l=i-a*l,s=o-a*s;if(!rt(r))throw new Error(t.toString()+" and "+e.toString()+" are not co-prime");return-1===X(n,0n)&&(n+=e),it(t)?-n:n}function ct(t,e,a){if(0n===a)throw new Error("Cannot take modPow with modulus 0");var i=1n,o=t%a;for(it(e)&&(e*=-1n,o=st(o,a));ot(e);){if(0n===o)return 0n;et(e)&&(i=i*o%a),e/=2n,o=tt(o)%a}return i}function dt(t,e){return 0n!==e&&(!!rt(e)||(0===function(t,e){return(t=t>=0n?t:-t)===(e=e>=0n?e:-e)?0:t>e?1:-1}(e,2n)?at(t):t%e===0n))}function ut(t,e){for(var a,i,o,n=function(t){return t-1n}(t),l=n,r=0;at(l);)l/=2n,r++;t:for(i=0;i>1&&i>1,t>>1)))),e.addCode(a.setLocal(s,a.i64_add(a.getLocal(s),a.i64_shr_u(a.getLocal(r),a.i64_const(32)))))),t>0&&(e.addCode(a.setLocal(r,a.i64_add(a.i64_and(a.getLocal(r),a.i64_const(4294967295)),a.i64_and(a.getLocal(c),a.i64_const(4294967295))))),e.addCode(a.setLocal(s,a.i64_add(a.i64_add(a.getLocal(s),a.i64_shr_u(a.getLocal(r),a.i64_const(32))),a.getLocal(d))))),e.addCode(a.i64_store32(a.getLocal("r"),4*t,a.getLocal(r))),e.addCode(a.setLocal(c,a.getLocal(s)),a.setLocal(d,a.i64_shr_u(a.getLocal(c),a.i64_const(32))))}e.addCode(a.i64_store32(a.getLocal("r"),4*o*2-4,a.getLocal(c)))}(),function(){const e=t.addFunction(i+"_squareOld");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(i+"_mul",a.getLocal("x"),a.getLocal("x"),a.getLocal("r")))}(),function(){!function(){const e=t.addFunction(i+"__mul1");e.addParam("px","i32"),e.addParam("y","i64"),e.addParam("pr","i32"),e.addLocal("c","i64");const a=e.getCodeBuilder();e.addCode(a.setLocal("c",a.i64_mul(a.i64_load32_u(a.getLocal("px"),0,0),a.getLocal("y")))),e.addCode(a.i64_store32(a.getLocal("pr"),0,0,a.getLocal("c")));for(let t=1;t>1n,p=t.alloc(r,ft.bigInt2BytesLE(_,r)),m=_+1n,w=t.alloc(r,ft.bigInt2BytesLE(m,r));t.modules[s]={pq:d,pR2:u,n64:n,q:o,pOne:g,pZero:f,pePlusOne:w};let L=2n;if(At(o))for(;bt(L,_,o)!==h;)L+=1n;let b=0,A=h;for(;!yt(A)&&0n!==A;)b++,A>>=1n;const y=t.alloc(r,ft.bigInt2BytesLE(A,r)),C=bt(L,A,o),I=t.alloc(ft.bigInt2BytesLE((C<>1n,x=t.alloc(r,ft.bigInt2BytesLE(F,r));return t.exportFunction(c+"_copy",s+"_copy"),t.exportFunction(c+"_zero",s+"_zero"),t.exportFunction(c+"_isZero",s+"_isZero"),t.exportFunction(c+"_eq",s+"_eq"),function(){const e=t.addFunction(s+"_isOne");e.addParam("x","i32"),e.setReturnType("i32");const a=e.getCodeBuilder();e.addCode(a.ret(a.call(c+"_eq",a.getLocal("x"),a.i32_const(g))))}(),function(){const e=t.addFunction(s+"_add");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.if(a.call(c+"_add",a.getLocal("x"),a.getLocal("y"),a.getLocal("r")),a.drop(a.call(c+"_sub",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))),a.if(a.call(c+"_gte",a.getLocal("r"),a.i32_const(d)),a.drop(a.call(c+"_sub",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))))))}(),function(){const e=t.addFunction(s+"_sub");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.if(a.call(c+"_sub",a.getLocal("x"),a.getLocal("y"),a.getLocal("r")),a.drop(a.call(c+"_add",a.getLocal("r"),a.i32_const(d),a.getLocal("r")))))}(),function(){const e=t.addFunction(s+"_neg");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(s+"_sub",a.i32_const(f),a.getLocal("x"),a.getLocal("r")))}(),function(){const e=t.alloc(l*l*8),a=t.addFunction(s+"_mReduct");a.addParam("t","i32"),a.addParam("r","i32"),a.addLocal("np32","i64"),a.addLocal("c","i64"),a.addLocal("m","i64");const i=a.getCodeBuilder(),n=Number(0x100000000n-Lt(o,0x100000000n));a.addCode(i.setLocal("np32",i.i64_const(n)));for(let t=0;t=l&&e.addCode(a.i64_store32(a.getLocal("r"),4*(t-l),a.getLocal(h))),[h,_]=[_,h],e.addCode(a.setLocal(_,a.i64_shr_u(a.getLocal(h),a.i64_const(32))))}e.addCode(a.i64_store32(a.getLocal("r"),4*l-4,a.getLocal(h))),e.addCode(a.if(a.i32_wrap_i64(a.getLocal(_)),a.drop(a.call(c+"_sub",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))),a.if(a.call(c+"_gte",a.getLocal("r"),a.i32_const(d)),a.drop(a.call(c+"_sub",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))))))}(),function(){const e=t.addFunction(s+"_square");e.addParam("x","i32"),e.addParam("r","i32"),e.addLocal("c0","i64"),e.addLocal("c1","i64"),e.addLocal("c0_old","i64"),e.addLocal("c1_old","i64"),e.addLocal("np32","i64");for(let t=0;t>1&&i>1,t>>1)))),e.addCode(a.setLocal(h,a.i64_add(a.getLocal(h),a.i64_shr_u(a.getLocal(f),a.i64_const(32)))))),t>0&&(e.addCode(a.setLocal(f,a.i64_add(a.i64_and(a.getLocal(f),a.i64_const(4294967295)),a.i64_and(a.getLocal(_),a.i64_const(4294967295))))),e.addCode(a.setLocal(h,a.i64_add(a.i64_add(a.getLocal(h),a.i64_shr_u(a.getLocal(f),a.i64_const(32))),a.getLocal(p)))));for(let i=Math.max(1,t-l+1);i<=t&&i=l&&e.addCode(a.i64_store32(a.getLocal("r"),4*(t-l),a.getLocal(f))),e.addCode(a.setLocal(_,a.getLocal(h)),a.setLocal(p,a.i64_shr_u(a.getLocal(_),a.i64_const(32))))}e.addCode(a.i64_store32(a.getLocal("r"),4*l-4,a.getLocal(_))),e.addCode(a.if(a.i32_wrap_i64(a.getLocal(p)),a.drop(a.call(c+"_sub",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))),a.if(a.call(c+"_gte",a.getLocal("r"),a.i32_const(d)),a.drop(a.call(c+"_sub",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))))))}(),function(){const e=t.addFunction(s+"_squareOld");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(s+"_mul",a.getLocal("x"),a.getLocal("x"),a.getLocal("r")))}(),function(){const e=t.addFunction(s+"_toMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(s+"_mul",a.getLocal("x"),a.i32_const(u),a.getLocal("r")))}(),function(){const e=t.alloc(2*r),a=t.addFunction(s+"_fromMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const i=a.getCodeBuilder();a.addCode(i.call(c+"_copy",i.getLocal("x"),i.i32_const(e))),a.addCode(i.call(c+"_zero",i.i32_const(e+r))),a.addCode(i.call(s+"_mReduct",i.i32_const(e),i.getLocal("r")))}(),function(){const e=t.addFunction(s+"_isNegative");e.addParam("x","i32"),e.setReturnType("i32");const a=e.getCodeBuilder(),i=a.i32_const(t.alloc(r));e.addCode(a.call(s+"_fromMontgomery",a.getLocal("x"),i),a.call(c+"_gte",i,a.i32_const(w)))}(),function(){const e=t.addFunction(s+"_sign");e.addParam("x","i32"),e.setReturnType("i32");const a=e.getCodeBuilder(),i=a.i32_const(t.alloc(r));e.addCode(a.if(a.call(c+"_isZero",a.getLocal("x")),a.ret(a.i32_const(0))),a.call(s+"_fromMontgomery",a.getLocal("x"),i),a.if(a.call(c+"_gte",i,a.i32_const(w)),a.ret(a.i32_const(-1))),a.ret(a.i32_const(1)))}(),function(){const e=t.addFunction(s+"_inverse");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(s+"_fromMontgomery",a.getLocal("x"),a.getLocal("r"))),e.addCode(a.call(c+"_inverseMod",a.getLocal("r"),a.i32_const(d),a.getLocal("r"))),e.addCode(a.call(s+"_toMontgomery",a.getLocal("r"),a.getLocal("r")))}(),function(){const e=t.addFunction(s+"_one");e.addParam("pr","i32");const a=e.getCodeBuilder();e.addCode(a.call(c+"_copy",a.i32_const(g),a.getLocal("pr")))}(),function(){const e=t.addFunction(s+"_load");e.addParam("scalar","i32"),e.addParam("scalarLen","i32"),e.addParam("r","i32"),e.addLocal("p","i32"),e.addLocal("l","i32"),e.addLocal("i","i32"),e.addLocal("j","i32");const a=e.getCodeBuilder(),i=a.i32_const(t.alloc(r)),o=t.alloc(r),n=a.i32_const(o);e.addCode(a.call(c+"_zero",a.getLocal("r")),a.setLocal("i",a.i32_const(r)),a.setLocal("p",a.getLocal("scalar")),a.block(a.loop(a.br_if(1,a.i32_gt_u(a.getLocal("i"),a.getLocal("scalarLen"))),a.if(a.i32_eq(a.getLocal("i"),a.i32_const(r)),a.call(s+"_one",i),a.call(s+"_mul",i,a.i32_const(u),i)),a.call(s+"_mul",a.getLocal("p"),i,n),a.call(s+"_add",a.getLocal("r"),n,a.getLocal("r")),a.setLocal("p",a.i32_add(a.getLocal("p"),a.i32_const(r))),a.setLocal("i",a.i32_add(a.getLocal("i"),a.i32_const(r))),a.br(0))),a.setLocal("l",a.i32_rem_u(a.getLocal("scalarLen"),a.i32_const(r))),a.if(a.i32_eqz(a.getLocal("l")),a.ret([])),a.call(c+"_zero",n),a.setLocal("j",a.i32_const(0)),a.block(a.loop(a.br_if(1,a.i32_eq(a.getLocal("j"),a.getLocal("l"))),a.i32_store8(a.getLocal("j"),o,a.i32_load8_u(a.getLocal("p"))),a.setLocal("p",a.i32_add(a.getLocal("p"),a.i32_const(1))),a.setLocal("j",a.i32_add(a.getLocal("j"),a.i32_const(1))),a.br(0))),a.if(a.i32_eq(a.getLocal("i"),a.i32_const(r)),a.call(s+"_one",i),a.call(s+"_mul",i,a.i32_const(u),i)),a.call(s+"_mul",n,i,n),a.call(s+"_add",a.getLocal("r"),n,a.getLocal("r")))}(),function(){const e=t.addFunction(s+"_timesScalar");e.addParam("x","i32"),e.addParam("scalar","i32"),e.addParam("scalarLen","i32"),e.addParam("r","i32");const a=e.getCodeBuilder(),i=a.i32_const(t.alloc(r));e.addCode(a.call(s+"_load",a.getLocal("scalar"),a.getLocal("scalarLen"),i),a.call(s+"_toMontgomery",i,i),a.call(s+"_mul",a.getLocal("x"),i,a.getLocal("r")))}(),_t(t,s),pt(t,s+"_batchToMontgomery",s+"_toMontgomery",r,r),pt(t,s+"_batchFromMontgomery",s+"_fromMontgomery",r,r),pt(t,s+"_batchNeg",s+"_neg",r,r),mt(t,s+"_batchAdd",s+"_add",r,r),mt(t,s+"_batchSub",s+"_sub",r,r),mt(t,s+"_batchMul",s+"_mul",r,r),t.exportFunction(s+"_add"),t.exportFunction(s+"_sub"),t.exportFunction(s+"_neg"),t.exportFunction(s+"_isNegative"),t.exportFunction(s+"_isOne"),t.exportFunction(s+"_sign"),t.exportFunction(s+"_mReduct"),t.exportFunction(s+"_mul"),t.exportFunction(s+"_square"),t.exportFunction(s+"_squareOld"),t.exportFunction(s+"_fromMontgomery"),t.exportFunction(s+"_toMontgomery"),t.exportFunction(s+"_inverse"),t.exportFunction(s+"_one"),t.exportFunction(s+"_load"),t.exportFunction(s+"_timesScalar"),ht(t,s+"_exp",r,s+"_mul",s+"_square",c+"_copy",s+"_one"),t.exportFunction(s+"_exp"),t.exportFunction(s+"_batchInverse"),At(o)&&(!function(){const e=t.addFunction(s+"_sqrt");e.addParam("n","i32"),e.addParam("r","i32"),e.addLocal("m","i32"),e.addLocal("i","i32"),e.addLocal("j","i32");const a=e.getCodeBuilder(),i=a.i32_const(g),o=a.i32_const(t.alloc(r)),n=a.i32_const(t.alloc(r)),l=a.i32_const(t.alloc(r)),c=a.i32_const(t.alloc(r)),d=a.i32_const(t.alloc(r));e.addCode(a.if(a.call(s+"_isZero",a.getLocal("n")),a.ret(a.call(s+"_zero",a.getLocal("r")))),a.setLocal("m",a.i32_const(b)),a.call(s+"_copy",a.i32_const(I),o),a.call(s+"_exp",a.getLocal("n"),a.i32_const(y),a.i32_const(r),n),a.call(s+"_exp",a.getLocal("n"),a.i32_const(x),a.i32_const(r),l),a.block(a.loop(a.br_if(1,a.call(s+"_eq",n,i)),a.call(s+"_square",n,c),a.setLocal("i",a.i32_const(1)),a.block(a.loop(a.br_if(1,a.call(s+"_eq",c,i)),a.call(s+"_square",c,c),a.setLocal("i",a.i32_add(a.getLocal("i"),a.i32_const(1))),a.br(0))),a.call(s+"_copy",o,d),a.setLocal("j",a.i32_sub(a.i32_sub(a.getLocal("m"),a.getLocal("i")),a.i32_const(1))),a.block(a.loop(a.br_if(1,a.i32_eqz(a.getLocal("j"))),a.call(s+"_square",d,d),a.setLocal("j",a.i32_sub(a.getLocal("j"),a.i32_const(1))),a.br(0))),a.setLocal("m",a.getLocal("i")),a.call(s+"_square",d,o),a.call(s+"_mul",n,o,n),a.call(s+"_mul",l,d,l),a.br(0))),a.if(a.call(s+"_isNegative",l),a.call(s+"_neg",l,a.getLocal("r")),a.call(s+"_copy",l,a.getLocal("r"))))}(),function(){const e=t.addFunction(s+"_isSquare");e.addParam("n","i32"),e.setReturnType("i32");const a=e.getCodeBuilder(),i=a.i32_const(g),o=a.i32_const(t.alloc(r));e.addCode(a.if(a.call(s+"_isZero",a.getLocal("n")),a.ret(a.i32_const(1))),a.call(s+"_exp",a.getLocal("n"),a.i32_const(p),a.i32_const(r),o),a.call(s+"_eq",o,i))}(),t.exportFunction(s+"_sqrt"),t.exportFunction(s+"_isSquare")),t.exportFunction(s+"_batchToMontgomery"),t.exportFunction(s+"_batchFromMontgomery"),s};const Ft=It,{bitLength:xt}=J;var Et=function(t,e,a,i,o){const n=BigInt(e),l=Math.floor((xt(n-1n)-1)/64)+1,r=8*l,s=a||"f1";if(t.modules[s])return s;t.modules[s]={n64:l};const c=o||"int",d=Ft(t,n,i,c),u=t.modules[d].pR2,g=t.modules[d].pq,f=t.modules[d].pePlusOne;return function(){const e=t.alloc(r),a=t.addFunction(s+"_mul");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const i=a.getCodeBuilder();a.addCode(i.call(d+"_mul",i.getLocal("x"),i.getLocal("y"),i.i32_const(e))),a.addCode(i.call(d+"_mul",i.i32_const(e),i.i32_const(u),i.getLocal("r")))}(),function(){const e=t.addFunction(s+"_square");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(s+"_mul",a.getLocal("x"),a.getLocal("x"),a.getLocal("r")))}(),function(){const e=t.addFunction(s+"_inverse");e.addParam("x","i32"),e.addParam("r","i32");const a=e.getCodeBuilder();e.addCode(a.call(c+"_inverseMod",a.getLocal("x"),a.i32_const(g),a.getLocal("r")))}(),function(){const e=t.addFunction(s+"_isNegative");e.addParam("x","i32"),e.setReturnType("i32");const a=e.getCodeBuilder();e.addCode(a.call(c+"_gte",a.getLocal("x"),a.i32_const(f)))}(),t.exportFunction(d+"_add",s+"_add"),t.exportFunction(d+"_sub",s+"_sub"),t.exportFunction(d+"_neg",s+"_neg"),t.exportFunction(s+"_mul"),t.exportFunction(s+"_square"),t.exportFunction(s+"_inverse"),t.exportFunction(s+"_isNegative"),t.exportFunction(d+"_copy",s+"_copy"),t.exportFunction(d+"_zero",s+"_zero"),t.exportFunction(d+"_one",s+"_one"),t.exportFunction(d+"_isZero",s+"_isZero"),t.exportFunction(d+"_eq",s+"_eq"),s};const vt=H,Bt=Z,St=j;var Pt=function(t,e,a,i){if(t.modules[a])return a;const o=8*t.modules[i].n64,n=t.modules[i].q;return t.modules[a]={n64:2*t.modules[i].n64},function(){const e=t.addFunction(a+"_isZero");e.addParam("x","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o));e.addCode(n.i32_and(n.call(i+"_isZero",l),n.call(i+"_isZero",r)))}(),function(){const e=t.addFunction(a+"_isOne");e.addParam("x","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o));e.addCode(n.ret(n.i32_and(n.call(i+"_isOne",l),n.call(i+"_isZero",r))))}(),function(){const e=t.addFunction(a+"_zero");e.addParam("x","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o));e.addCode(n.call(i+"_zero",l),n.call(i+"_zero",r))}(),function(){const e=t.addFunction(a+"_one");e.addParam("x","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o));e.addCode(n.call(i+"_one",l),n.call(i+"_zero",r))}(),function(){const e=t.addFunction(a+"_copy");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("r"),c=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_copy",l,s),n.call(i+"_copy",r,c))}(),function(){const n=t.addFunction(a+"_mul");n.addParam("x","i32"),n.addParam("y","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),r=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(o)),c=l.getLocal("y"),d=l.i32_add(l.getLocal("y"),l.i32_const(o)),u=l.getLocal("r"),g=l.i32_add(l.getLocal("r"),l.i32_const(o)),f=l.i32_const(t.alloc(o)),h=l.i32_const(t.alloc(o)),_=l.i32_const(t.alloc(o)),p=l.i32_const(t.alloc(o));n.addCode(l.call(i+"_mul",r,c,f),l.call(i+"_mul",s,d,h),l.call(i+"_add",r,s,_),l.call(i+"_add",c,d,p),l.call(i+"_mul",_,p,_),l.call(e,h,u),l.call(i+"_add",f,u,u),l.call(i+"_add",f,h,g),l.call(i+"_sub",_,g,g))}(),function(){const e=t.addFunction(a+"_mul1");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("y"),c=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_mul",l,s,c),n.call(i+"_mul",r,s,d))}(),function(){const n=t.addFunction(a+"_square");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),r=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(o)),c=l.getLocal("r"),d=l.i32_add(l.getLocal("r"),l.i32_const(o)),u=l.i32_const(t.alloc(o)),g=l.i32_const(t.alloc(o)),f=l.i32_const(t.alloc(o)),h=l.i32_const(t.alloc(o));n.addCode(l.call(i+"_mul",r,s,u),l.call(i+"_add",r,s,g),l.call(e,s,f),l.call(i+"_add",r,f,f),l.call(e,u,h),l.call(i+"_add",h,u,h),l.call(i+"_mul",g,f,c),l.call(i+"_sub",c,h,c),l.call(i+"_add",u,u,d))}(),function(){const e=t.addFunction(a+"_add");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("y"),c=n.i32_add(n.getLocal("y"),n.i32_const(o)),d=n.getLocal("r"),u=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_add",l,s,d),n.call(i+"_add",r,c,u))}(),function(){const e=t.addFunction(a+"_sub");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("y"),c=n.i32_add(n.getLocal("y"),n.i32_const(o)),d=n.getLocal("r"),u=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_sub",l,s,d),n.call(i+"_sub",r,c,u))}(),function(){const e=t.addFunction(a+"_neg");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("r"),c=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_neg",l,s),n.call(i+"_neg",r,c))}(),function(){const e=t.addFunction(a+"_conjugate");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("r"),c=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_copy",l,s),n.call(i+"_neg",r,c))}(),function(){const e=t.addFunction(a+"_toMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("r"),c=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_toMontgomery",l,s),n.call(i+"_toMontgomery",r,c))}(),function(){const e=t.addFunction(a+"_fromMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("r"),c=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_fromMontgomery",l,s),n.call(i+"_fromMontgomery",r,c))}(),function(){const e=t.addFunction(a+"_eq");e.addParam("x","i32"),e.addParam("y","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("y"),c=n.i32_add(n.getLocal("y"),n.i32_const(o));e.addCode(n.i32_and(n.call(i+"_eq",l,s),n.call(i+"_eq",r,c)))}(),function(){const n=t.addFunction(a+"_inverse");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),r=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(o)),c=l.getLocal("r"),d=l.i32_add(l.getLocal("r"),l.i32_const(o)),u=l.i32_const(t.alloc(o)),g=l.i32_const(t.alloc(o)),f=l.i32_const(t.alloc(o)),h=l.i32_const(t.alloc(o));n.addCode(l.call(i+"_square",r,u),l.call(i+"_square",s,g),l.call(e,g,f),l.call(i+"_sub",u,f,f),l.call(i+"_inverse",f,h),l.call(i+"_mul",r,h,c),l.call(i+"_mul",s,h,d),l.call(i+"_neg",d,d))}(),function(){const e=t.addFunction(a+"_timesScalar");e.addParam("x","i32"),e.addParam("scalar","i32"),e.addParam("scalarLen","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.getLocal("r"),c=n.i32_add(n.getLocal("r"),n.i32_const(o));e.addCode(n.call(i+"_timesScalar",l,n.getLocal("scalar"),n.getLocal("scalarLen"),s),n.call(i+"_timesScalar",r,n.getLocal("scalar"),n.getLocal("scalarLen"),c))}(),function(){const e=t.addFunction(a+"_sign");e.addParam("x","i32"),e.addLocal("s","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o));e.addCode(n.setLocal("s",n.call(i+"_sign",r)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.ret(n.call(i+"_sign",l)))}(),function(){const e=t.addFunction(a+"_isNegative");e.addParam("x","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o));e.addCode(n.if(n.call(i+"_isZero",r),n.ret(n.call(i+"_isNegative",l))),n.ret(n.call(i+"_isNegative",r)))}(),t.exportFunction(a+"_isZero"),t.exportFunction(a+"_isOne"),t.exportFunction(a+"_zero"),t.exportFunction(a+"_one"),t.exportFunction(a+"_copy"),t.exportFunction(a+"_mul"),t.exportFunction(a+"_mul1"),t.exportFunction(a+"_square"),t.exportFunction(a+"_add"),t.exportFunction(a+"_sub"),t.exportFunction(a+"_neg"),t.exportFunction(a+"_sign"),t.exportFunction(a+"_conjugate"),t.exportFunction(a+"_fromMontgomery"),t.exportFunction(a+"_toMontgomery"),t.exportFunction(a+"_eq"),t.exportFunction(a+"_inverse"),Bt(t,a),vt(t,a+"_exp",2*o,a+"_mul",a+"_square",a+"_copy",a+"_one"),function(){const e=t.addFunction(a+"_sqrt");e.addParam("a","i32"),e.addParam("pr","i32");const l=e.getCodeBuilder(),r=l.i32_const(t.alloc(St.bigInt2BytesLE((BigInt(n||0)-3n)/4n,o))),s=l.i32_const(t.alloc(St.bigInt2BytesLE((BigInt(n||0)-1n)/2n,o))),c=l.getLocal("a"),d=l.i32_const(t.alloc(2*o)),u=l.i32_const(t.alloc(2*o)),g=l.i32_const(t.alloc(2*o)),f=t.alloc(2*o),h=l.i32_const(f),_=l.i32_const(f),p=l.i32_const(f+o),m=l.i32_const(t.alloc(2*o)),w=l.i32_const(t.alloc(2*o));e.addCode(l.call(a+"_one",h),l.call(a+"_neg",h,h),l.call(a+"_exp",c,r,l.i32_const(o),d),l.call(a+"_square",d,u),l.call(a+"_mul",c,u,u),l.call(a+"_conjugate",u,g),l.call(a+"_mul",g,u,g),l.if(l.call(a+"_eq",g,h),l.unreachable()),l.call(a+"_mul",d,c,m),l.if(l.call(a+"_eq",u,h),[...l.call(i+"_zero",_),...l.call(i+"_one",p),...l.call(a+"_mul",h,m,l.getLocal("pr"))],[...l.call(a+"_one",w),...l.call(a+"_add",w,u,w),...l.call(a+"_exp",w,s,l.i32_const(o),w),...l.call(a+"_mul",w,m,l.getLocal("pr"))]))}(),function(){const e=t.addFunction(a+"_isSquare");e.addParam("a","i32"),e.setReturnType("i32");const i=e.getCodeBuilder(),l=i.i32_const(t.alloc(St.bigInt2BytesLE((BigInt(n||0)-3n)/4n,o))),r=i.getLocal("a"),s=i.i32_const(t.alloc(2*o)),c=i.i32_const(t.alloc(2*o)),d=i.i32_const(t.alloc(2*o)),u=t.alloc(2*o),g=i.i32_const(u);e.addCode(i.call(a+"_one",g),i.call(a+"_neg",g,g),i.call(a+"_exp",r,l,i.i32_const(o),s),i.call(a+"_square",s,c),i.call(a+"_mul",r,c,c),i.call(a+"_conjugate",c,d),i.call(a+"_mul",d,c,d),i.if(i.call(a+"_eq",d,g),i.ret(i.i32_const(0))),i.ret(i.i32_const(1)))}(),t.exportFunction(a+"_exp"),t.exportFunction(a+"_timesScalar"),t.exportFunction(a+"_batchInverse"),t.exportFunction(a+"_sqrt"),t.exportFunction(a+"_isSquare"),t.exportFunction(a+"_isNegative"),a};const Gt=H,Ot=Z;var Tt=function(t,e,a,i){if(t.modules[a])return a;const o=8*t.modules[i].n64;return t.modules[a]={n64:3*t.modules[i].n64},function(){const e=t.addFunction(a+"_isZero");e.addParam("x","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o));e.addCode(n.i32_and(n.i32_and(n.call(i+"_isZero",l),n.call(i+"_isZero",r)),n.call(i+"_isZero",s)))}(),function(){const e=t.addFunction(a+"_isOne");e.addParam("x","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o));e.addCode(n.ret(n.i32_and(n.i32_and(n.call(i+"_isOne",l),n.call(i+"_isZero",r)),n.call(i+"_isZero",s))))}(),function(){const e=t.addFunction(a+"_zero");e.addParam("x","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o));e.addCode(n.call(i+"_zero",l),n.call(i+"_zero",r),n.call(i+"_zero",s))}(),function(){const e=t.addFunction(a+"_one");e.addParam("x","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o));e.addCode(n.call(i+"_one",l),n.call(i+"_zero",r),n.call(i+"_zero",s))}(),function(){const e=t.addFunction(a+"_copy");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(o)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_copy",l,c),n.call(i+"_copy",r,d),n.call(i+"_copy",s,u))}(),function(){const n=t.addFunction(a+"_mul");n.addParam("x","i32"),n.addParam("y","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),r=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(o)),c=l.i32_add(l.getLocal("x"),l.i32_const(2*o)),d=l.getLocal("y"),u=l.i32_add(l.getLocal("y"),l.i32_const(o)),g=l.i32_add(l.getLocal("y"),l.i32_const(2*o)),f=l.getLocal("r"),h=l.i32_add(l.getLocal("r"),l.i32_const(o)),_=l.i32_add(l.getLocal("r"),l.i32_const(2*o)),p=l.i32_const(t.alloc(o)),m=l.i32_const(t.alloc(o)),w=l.i32_const(t.alloc(o)),L=l.i32_const(t.alloc(o)),b=l.i32_const(t.alloc(o)),A=l.i32_const(t.alloc(o)),y=l.i32_const(t.alloc(o)),C=l.i32_const(t.alloc(o)),I=l.i32_const(t.alloc(o)),F=l.i32_const(t.alloc(o)),x=l.i32_const(t.alloc(o)),E=l.i32_const(t.alloc(o)),v=l.i32_const(t.alloc(o));n.addCode(l.call(i+"_mul",r,d,p),l.call(i+"_mul",s,u,m),l.call(i+"_mul",c,g,w),l.call(i+"_add",r,s,L),l.call(i+"_add",d,u,b),l.call(i+"_add",r,c,A),l.call(i+"_add",d,g,y),l.call(i+"_add",s,c,C),l.call(i+"_add",u,g,I),l.call(i+"_add",p,m,F),l.call(i+"_add",p,w,x),l.call(i+"_add",m,w,E),l.call(i+"_mul",C,I,f),l.call(i+"_sub",f,E,f),l.call(e,f,f),l.call(i+"_add",p,f,f),l.call(i+"_mul",L,b,h),l.call(i+"_sub",h,F,h),l.call(e,w,v),l.call(i+"_add",h,v,h),l.call(i+"_mul",A,y,_),l.call(i+"_sub",_,x,_),l.call(i+"_add",_,m,_))}(),function(){const n=t.addFunction(a+"_square");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),r=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(o)),c=l.i32_add(l.getLocal("x"),l.i32_const(2*o)),d=l.getLocal("r"),u=l.i32_add(l.getLocal("r"),l.i32_const(o)),g=l.i32_add(l.getLocal("r"),l.i32_const(2*o)),f=l.i32_const(t.alloc(o)),h=l.i32_const(t.alloc(o)),_=l.i32_const(t.alloc(o)),p=l.i32_const(t.alloc(o)),m=l.i32_const(t.alloc(o)),w=l.i32_const(t.alloc(o)),L=l.i32_const(t.alloc(o));n.addCode(l.call(i+"_square",r,f),l.call(i+"_mul",r,s,h),l.call(i+"_add",h,h,_),l.call(i+"_sub",r,s,p),l.call(i+"_add",p,c,p),l.call(i+"_square",p,p),l.call(i+"_mul",s,c,m),l.call(i+"_add",m,m,w),l.call(i+"_square",c,L),l.call(e,w,d),l.call(i+"_add",f,d,d),l.call(e,L,u),l.call(i+"_add",_,u,u),l.call(i+"_add",f,L,g),l.call(i+"_sub",w,g,g),l.call(i+"_add",p,g,g),l.call(i+"_add",_,g,g))}(),function(){const e=t.addFunction(a+"_add");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(o)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*o)),g=n.getLocal("r"),f=n.i32_add(n.getLocal("r"),n.i32_const(o)),h=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_add",l,c,g),n.call(i+"_add",r,d,f),n.call(i+"_add",s,u,h))}(),function(){const e=t.addFunction(a+"_sub");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(o)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*o)),g=n.getLocal("r"),f=n.i32_add(n.getLocal("r"),n.i32_const(o)),h=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_sub",l,c,g),n.call(i+"_sub",r,d,f),n.call(i+"_sub",s,u,h))}(),function(){const e=t.addFunction(a+"_neg");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(o)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_neg",l,c),n.call(i+"_neg",r,d),n.call(i+"_neg",s,u))}(),function(){const e=t.addFunction(a+"_sign");e.addParam("x","i32"),e.addLocal("s","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o));e.addCode(n.setLocal("s",n.call(i+"_sign",s)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.setLocal("s",n.call(i+"_sign",r)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.ret(n.call(i+"_sign",l)))}(),function(){const e=t.addFunction(a+"_toMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(o)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_toMontgomery",l,c),n.call(i+"_toMontgomery",r,d),n.call(i+"_toMontgomery",s,u))}(),function(){const e=t.addFunction(a+"_fromMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(o)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_fromMontgomery",l,c),n.call(i+"_fromMontgomery",r,d),n.call(i+"_fromMontgomery",s,u))}(),function(){const e=t.addFunction(a+"_eq");e.addParam("x","i32"),e.addParam("y","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(o)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*o));e.addCode(n.i32_and(n.i32_and(n.call(i+"_eq",l,c),n.call(i+"_eq",r,d)),n.call(i+"_eq",s,u)))}(),function(){const n=t.addFunction(a+"_inverse");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),r=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(o)),c=l.i32_add(l.getLocal("x"),l.i32_const(2*o)),d=l.getLocal("r"),u=l.i32_add(l.getLocal("r"),l.i32_const(o)),g=l.i32_add(l.getLocal("r"),l.i32_const(2*o)),f=l.i32_const(t.alloc(o)),h=l.i32_const(t.alloc(o)),_=l.i32_const(t.alloc(o)),p=l.i32_const(t.alloc(o)),m=l.i32_const(t.alloc(o)),w=l.i32_const(t.alloc(o)),L=l.i32_const(t.alloc(o)),b=l.i32_const(t.alloc(o)),A=l.i32_const(t.alloc(o)),y=l.i32_const(t.alloc(o)),C=l.i32_const(t.alloc(o));n.addCode(l.call(i+"_square",r,f),l.call(i+"_square",s,h),l.call(i+"_square",c,_),l.call(i+"_mul",r,s,p),l.call(i+"_mul",r,c,m),l.call(i+"_mul",s,c,w),l.call(e,w,L),l.call(i+"_sub",f,L,L),l.call(e,_,b),l.call(i+"_sub",b,p,b),l.call(i+"_sub",h,m,A),l.call(i+"_mul",c,b,y),l.call(i+"_mul",s,A,C),l.call(i+"_add",y,C,y),l.call(e,y,y),l.call(i+"_mul",r,L,C),l.call(i+"_add",C,y,y),l.call(i+"_inverse",y,y),l.call(i+"_mul",y,L,d),l.call(i+"_mul",y,b,u),l.call(i+"_mul",y,A,g))}(),function(){const e=t.addFunction(a+"_timesScalar");e.addParam("x","i32"),e.addParam("scalar","i32"),e.addParam("scalarLen","i32"),e.addParam("r","i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o)),c=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(o)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*o));e.addCode(n.call(i+"_timesScalar",l,n.getLocal("scalar"),n.getLocal("scalarLen"),c),n.call(i+"_timesScalar",r,n.getLocal("scalar"),n.getLocal("scalarLen"),d),n.call(i+"_timesScalar",s,n.getLocal("scalar"),n.getLocal("scalarLen"),u))}(),function(){const e=t.addFunction(a+"_isNegative");e.addParam("x","i32"),e.setReturnType("i32");const n=e.getCodeBuilder(),l=n.getLocal("x"),r=n.i32_add(n.getLocal("x"),n.i32_const(o)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*o));e.addCode(n.if(n.call(i+"_isZero",s),n.if(n.call(i+"_isZero",r),n.ret(n.call(i+"_isNegative",l)),n.ret(n.call(i+"_isNegative",r)))),n.ret(n.call(i+"_isNegative",s)))}(),t.exportFunction(a+"_isZero"),t.exportFunction(a+"_isOne"),t.exportFunction(a+"_zero"),t.exportFunction(a+"_one"),t.exportFunction(a+"_copy"),t.exportFunction(a+"_mul"),t.exportFunction(a+"_square"),t.exportFunction(a+"_add"),t.exportFunction(a+"_sub"),t.exportFunction(a+"_neg"),t.exportFunction(a+"_sign"),t.exportFunction(a+"_fromMontgomery"),t.exportFunction(a+"_toMontgomery"),t.exportFunction(a+"_eq"),t.exportFunction(a+"_inverse"),Ot(t,a),Gt(t,a+"_exp",3*o,a+"_mul",a+"_square",a+"_copy",a+"_one"),t.exportFunction(a+"_exp"),t.exportFunction(a+"_timesScalar"),t.exportFunction(a+"_batchInverse"),t.exportFunction(a+"_isNegative"),a};const zt=function(t,e,a,i,o,n,l,r){const s=t.addFunction(e);s.addParam("base","i32"),s.addParam("scalar","i32"),s.addParam("scalarLength","i32"),s.addParam("r","i32"),s.addLocal("old0","i32"),s.addLocal("nbits","i32"),s.addLocal("i","i32"),s.addLocal("last","i32"),s.addLocal("cur","i32"),s.addLocal("carry","i32"),s.addLocal("p","i32");const c=s.getCodeBuilder(),d=c.i32_const(t.alloc(a));function u(t){return c.i32_and(c.i32_shr_u(c.i32_load(c.i32_add(c.getLocal("scalar"),c.i32_and(c.i32_shr_u(t,c.i32_const(3)),c.i32_const(4294967292)))),c.i32_and(t,c.i32_const(31))),c.i32_const(1))}function g(t){return[...c.i32_store8(c.getLocal("p"),c.i32_const(t)),...c.setLocal("p",c.i32_add(c.getLocal("p"),c.i32_const(1)))]}s.addCode(c.if(c.i32_eqz(c.getLocal("scalarLength")),[...c.call(r,c.getLocal("r")),...c.ret([])]),c.setLocal("nbits",c.i32_shl(c.getLocal("scalarLength"),c.i32_const(3))),c.setLocal("old0",c.i32_load(c.i32_const(0))),c.setLocal("p",c.getLocal("old0")),c.i32_store(c.i32_const(0),c.i32_and(c.i32_add(c.i32_add(c.getLocal("old0"),c.i32_const(32)),c.getLocal("nbits")),c.i32_const(4294967288))),c.setLocal("i",c.i32_const(1)),c.setLocal("last",u(c.i32_const(0))),c.setLocal("carry",c.i32_const(0)),c.block(c.loop(c.br_if(1,c.i32_eq(c.getLocal("i"),c.getLocal("nbits"))),c.setLocal("cur",u(c.getLocal("i"))),c.if(c.getLocal("last"),c.if(c.getLocal("cur"),c.if(c.getLocal("carry"),[...c.setLocal("last",c.i32_const(0)),...c.setLocal("carry",c.i32_const(1)),...g(1)],[...c.setLocal("last",c.i32_const(0)),...c.setLocal("carry",c.i32_const(1)),...g(255)]),c.if(c.getLocal("carry"),[...c.setLocal("last",c.i32_const(0)),...c.setLocal("carry",c.i32_const(1)),...g(255)],[...c.setLocal("last",c.i32_const(0)),...c.setLocal("carry",c.i32_const(0)),...g(1)])),c.if(c.getLocal("cur"),c.if(c.getLocal("carry"),[...c.setLocal("last",c.i32_const(0)),...c.setLocal("carry",c.i32_const(1)),...g(0)],[...c.setLocal("last",c.i32_const(1)),...c.setLocal("carry",c.i32_const(0)),...g(0)]),c.if(c.getLocal("carry"),[...c.setLocal("last",c.i32_const(1)),...c.setLocal("carry",c.i32_const(0)),...g(0)],[...c.setLocal("last",c.i32_const(0)),...c.setLocal("carry",c.i32_const(0)),...g(0)]))),c.setLocal("i",c.i32_add(c.getLocal("i"),c.i32_const(1))),c.br(0))),c.if(c.getLocal("last"),c.if(c.getLocal("carry"),[...g(255),...g(0),...g(1)],[...g(1)]),c.if(c.getLocal("carry"),[...g(0),...g(1)])),c.setLocal("p",c.i32_sub(c.getLocal("p"),c.i32_const(1))),c.call(l,c.getLocal("base"),d),c.call(r,c.getLocal("r")),c.block(c.loop(c.call(o,c.getLocal("r"),c.getLocal("r")),c.setLocal("cur",c.i32_load8_u(c.getLocal("p"))),c.if(c.getLocal("cur"),c.if(c.i32_eq(c.getLocal("cur"),c.i32_const(1)),c.call(i,c.getLocal("r"),d,c.getLocal("r")),c.call(n,c.getLocal("r"),d,c.getLocal("r")))),c.br_if(1,c.i32_eq(c.getLocal("old0"),c.getLocal("p"))),c.setLocal("p",c.i32_sub(c.getLocal("p"),c.i32_const(1))),c.br(0))),c.i32_store(c.i32_const(0),c.getLocal("old0")))},Ut=W,Qt=function(t,e,a,i,o){const n=8*t.modules[e].n64;function l(){const i=t.addFunction(a);i.addParam("pBases","i32"),i.addParam("pScalars","i32"),i.addParam("scalarSize","i32"),i.addParam("n","i32"),i.addParam("pr","i32"),i.addLocal("chunkSize","i32"),i.addLocal("nChunks","i32"),i.addLocal("itScalar","i32"),i.addLocal("endScalar","i32"),i.addLocal("itBase","i32"),i.addLocal("itBit","i32"),i.addLocal("i","i32"),i.addLocal("j","i32"),i.addLocal("nTable","i32"),i.addLocal("pTable","i32"),i.addLocal("idx","i32"),i.addLocal("pIdxTable","i32");const o=i.getCodeBuilder(),l=o.i32_const(t.alloc(n)),r=t.alloc([17,17,17,17,17,17,17,17,17,17,16,16,15,14,13,13,12,11,10,9,8,7,7,6,5,4,3,2,1,1,1,1]);i.addCode(o.call(e+"_zero",o.getLocal("pr")),o.if(o.i32_eqz(o.getLocal("n")),o.ret([])),o.setLocal("chunkSize",o.i32_load8_u(o.i32_clz(o.getLocal("n")),r)),o.setLocal("nChunks",o.i32_add(o.i32_div_u(o.i32_sub(o.i32_shl(o.getLocal("scalarSize"),o.i32_const(3)),o.i32_const(1)),o.getLocal("chunkSize")),o.i32_const(1))),o.setLocal("itBit",o.i32_mul(o.i32_sub(o.getLocal("nChunks"),o.i32_const(1)),o.getLocal("chunkSize"))),o.block(o.loop(o.br_if(1,o.i32_lt_s(o.getLocal("itBit"),o.i32_const(0))),o.if(o.i32_eqz(o.call(e+"_isZero",o.getLocal("pr"))),[...o.setLocal("j",o.i32_const(0)),...o.block(o.loop(o.br_if(1,o.i32_eq(o.getLocal("j"),o.getLocal("chunkSize"))),o.call(e+"_double",o.getLocal("pr"),o.getLocal("pr")),o.setLocal("j",o.i32_add(o.getLocal("j"),o.i32_const(1))),o.br(0)))]),o.call(a+"_chunk",o.getLocal("pBases"),o.getLocal("pScalars"),o.getLocal("scalarSize"),o.getLocal("n"),o.getLocal("itBit"),o.getLocal("chunkSize"),l),o.call(e+"_add",o.getLocal("pr"),l,o.getLocal("pr")),o.setLocal("itBit",o.i32_sub(o.getLocal("itBit"),o.getLocal("chunkSize"))),o.br(0))))}!function(){const e=t.addFunction(a+"_getChunk");e.addParam("pScalar","i32"),e.addParam("scalarSize","i32"),e.addParam("startBit","i32"),e.addParam("chunkSize","i32"),e.addLocal("bitsToEnd","i32"),e.addLocal("mask","i32"),e.setReturnType("i32");const i=e.getCodeBuilder();e.addCode(i.setLocal("bitsToEnd",i.i32_sub(i.i32_mul(i.getLocal("scalarSize"),i.i32_const(8)),i.getLocal("startBit"))),i.if(i.i32_gt_s(i.getLocal("chunkSize"),i.getLocal("bitsToEnd")),i.setLocal("mask",i.i32_sub(i.i32_shl(i.i32_const(1),i.getLocal("bitsToEnd")),i.i32_const(1))),i.setLocal("mask",i.i32_sub(i.i32_shl(i.i32_const(1),i.getLocal("chunkSize")),i.i32_const(1)))),i.i32_and(i.i32_shr_u(i.i32_load(i.i32_add(i.getLocal("pScalar"),i.i32_shr_u(i.getLocal("startBit"),i.i32_const(3))),0,0),i.i32_and(i.getLocal("startBit"),i.i32_const(7))),i.getLocal("mask")))}(),function(){const i=t.addFunction(a+"_reduceTable");i.addParam("pTable","i32"),i.addParam("p","i32"),i.addLocal("half","i32"),i.addLocal("it1","i32"),i.addLocal("it2","i32"),i.addLocal("pAcc","i32");const o=i.getCodeBuilder();i.addCode(o.if(o.i32_eq(o.getLocal("p"),o.i32_const(1)),o.ret([])),o.setLocal("half",o.i32_shl(o.i32_const(1),o.i32_sub(o.getLocal("p"),o.i32_const(1)))),o.setLocal("it1",o.getLocal("pTable")),o.setLocal("it2",o.i32_add(o.getLocal("pTable"),o.i32_mul(o.getLocal("half"),o.i32_const(n)))),o.setLocal("pAcc",o.i32_sub(o.getLocal("it2"),o.i32_const(n))),o.block(o.loop(o.br_if(1,o.i32_eq(o.getLocal("it1"),o.getLocal("pAcc"))),o.call(e+"_add",o.getLocal("it1"),o.getLocal("it2"),o.getLocal("it1")),o.call(e+"_add",o.getLocal("pAcc"),o.getLocal("it2"),o.getLocal("pAcc")),o.setLocal("it1",o.i32_add(o.getLocal("it1"),o.i32_const(n))),o.setLocal("it2",o.i32_add(o.getLocal("it2"),o.i32_const(n))),o.br(0))),o.call(a+"_reduceTable",o.getLocal("pTable"),o.i32_sub(o.getLocal("p"),o.i32_const(1))),o.setLocal("p",o.i32_sub(o.getLocal("p"),o.i32_const(1))),o.block(o.loop(o.br_if(1,o.i32_eqz(o.getLocal("p"))),o.call(e+"_double",o.getLocal("pAcc"),o.getLocal("pAcc")),o.setLocal("p",o.i32_sub(o.getLocal("p"),o.i32_const(1))),o.br(0))),o.call(e+"_add",o.getLocal("pTable"),o.getLocal("pAcc"),o.getLocal("pTable")))}(),function(){const l=t.addFunction(a+"_chunk");l.addParam("pBases","i32"),l.addParam("pScalars","i32"),l.addParam("scalarSize","i32"),l.addParam("n","i32"),l.addParam("startBit","i32"),l.addParam("chunkSize","i32"),l.addParam("pr","i32"),l.addLocal("nChunks","i32"),l.addLocal("itScalar","i32"),l.addLocal("endScalar","i32"),l.addLocal("itBase","i32"),l.addLocal("i","i32"),l.addLocal("j","i32"),l.addLocal("nTable","i32"),l.addLocal("pTable","i32"),l.addLocal("idx","i32"),l.addLocal("pIdxTable","i32");const r=l.getCodeBuilder();l.addCode(r.if(r.i32_eqz(r.getLocal("n")),[...r.call(e+"_zero",r.getLocal("pr")),...r.ret([])]),r.setLocal("nTable",r.i32_shl(r.i32_const(1),r.getLocal("chunkSize"))),r.setLocal("pTable",r.i32_load(r.i32_const(0))),r.i32_store(r.i32_const(0),r.i32_add(r.getLocal("pTable"),r.i32_mul(r.getLocal("nTable"),r.i32_const(n)))),r.setLocal("j",r.i32_const(0)),r.block(r.loop(r.br_if(1,r.i32_eq(r.getLocal("j"),r.getLocal("nTable"))),r.call(e+"_zero",r.i32_add(r.getLocal("pTable"),r.i32_mul(r.getLocal("j"),r.i32_const(n)))),r.setLocal("j",r.i32_add(r.getLocal("j"),r.i32_const(1))),r.br(0))),r.setLocal("itBase",r.getLocal("pBases")),r.setLocal("itScalar",r.getLocal("pScalars")),r.setLocal("endScalar",r.i32_add(r.getLocal("pScalars"),r.i32_mul(r.getLocal("n"),r.getLocal("scalarSize")))),r.block(r.loop(r.br_if(1,r.i32_eq(r.getLocal("itScalar"),r.getLocal("endScalar"))),r.setLocal("idx",r.call(a+"_getChunk",r.getLocal("itScalar"),r.getLocal("scalarSize"),r.getLocal("startBit"),r.getLocal("chunkSize"))),r.if(r.getLocal("idx"),[...r.setLocal("pIdxTable",r.i32_add(r.getLocal("pTable"),r.i32_mul(r.i32_sub(r.getLocal("idx"),r.i32_const(1)),r.i32_const(n)))),...r.call(i,r.getLocal("pIdxTable"),r.getLocal("itBase"),r.getLocal("pIdxTable"))]),r.setLocal("itScalar",r.i32_add(r.getLocal("itScalar"),r.getLocal("scalarSize"))),r.setLocal("itBase",r.i32_add(r.getLocal("itBase"),r.i32_const(o))),r.br(0))),r.call(a+"_reduceTable",r.getLocal("pTable"),r.getLocal("chunkSize")),r.call(e+"_copy",r.getLocal("pTable"),r.getLocal("pr")),r.i32_store(r.i32_const(0),r.getLocal("pTable")))}(),l(),t.exportFunction(a),t.exportFunction(a+"_chunk")};var qt=function(t,e,a,i){const o=t.modules[a].n64,n=8*o;if(t.modules[e])return e;return t.modules[e]={n64:3*o},function(){const i=t.addFunction(e+"_isZeroAffine");i.addParam("p1","i32"),i.setReturnType("i32");const o=i.getCodeBuilder();i.addCode(o.i32_and(o.call(a+"_isZero",o.getLocal("p1")),o.call(a+"_isZero",o.i32_add(o.getLocal("p1"),o.i32_const(n)))))}(),function(){const i=t.addFunction(e+"_isZero");i.addParam("p1","i32"),i.setReturnType("i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_isZero",o.i32_add(o.getLocal("p1"),o.i32_const(2*n))))}(),function(){const i=t.addFunction(e+"_zeroAffine");i.addParam("pr","i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_zero",o.getLocal("pr"))),i.addCode(o.call(a+"_zero",o.i32_add(o.getLocal("pr"),o.i32_const(n))))}(),function(){const i=t.addFunction(e+"_zero");i.addParam("pr","i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_zero",o.getLocal("pr"))),i.addCode(o.call(a+"_one",o.i32_add(o.getLocal("pr"),o.i32_const(n)))),i.addCode(o.call(a+"_zero",o.i32_add(o.getLocal("pr"),o.i32_const(2*n))))}(),function(){const a=t.addFunction(e+"_copyAffine");a.addParam("ps","i32"),a.addParam("pd","i32");const i=a.getCodeBuilder();for(let t=0;t<2*o;t++)a.addCode(i.i64_store(i.getLocal("pd"),8*t,i.i64_load(i.getLocal("ps"),8*t)))}(),function(){const a=t.addFunction(e+"_copy");a.addParam("ps","i32"),a.addParam("pd","i32");const i=a.getCodeBuilder();for(let t=0;t<3*o;t++)a.addCode(i.i64_store(i.getLocal("pd"),8*t,i.i64_load(i.getLocal("ps"),8*t)))}(),function(){const i=t.addFunction(e+"_toJacobian");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.getLocal("pr"),c=o.i32_add(o.getLocal("pr"),o.i32_const(n)),d=o.i32_add(o.getLocal("pr"),o.i32_const(2*n));i.addCode(o.if(o.call(e+"_isZeroAffine",o.getLocal("p1")),o.call(e+"_zero",o.getLocal("pr")),[...o.call(a+"_one",d),...o.call(a+"_copy",r,c),...o.call(a+"_copy",l,s)]))}(),function(){const i=t.addFunction(e+"_eqAffine");i.addParam("p1","i32"),i.addParam("p2","i32"),i.setReturnType("i32"),i.addLocal("z1","i32");const o=i.getCodeBuilder();i.addCode(o.ret(o.i32_and(o.call(a+"_eq",o.getLocal("p1"),o.getLocal("p2")),o.call(a+"_eq",o.i32_add(o.getLocal("p1"),o.i32_const(n)),o.i32_add(o.getLocal("p2"),o.i32_const(n))))))}(),function(){const i=t.addFunction(e+"_eqMixed");i.addParam("p1","i32"),i.addParam("p2","i32"),i.setReturnType("i32"),i.addLocal("z1","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n));i.addCode(o.setLocal("z1",o.i32_add(o.getLocal("p1"),o.i32_const(2*n))));const s=o.getLocal("z1"),c=o.getLocal("p2"),d=o.i32_add(o.getLocal("p2"),o.i32_const(n)),u=o.i32_const(t.alloc(n)),g=o.i32_const(t.alloc(n)),f=o.i32_const(t.alloc(n)),h=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),o.ret(o.call(e+"_isZeroAffine",o.getLocal("p2")))),o.if(o.call(e+"_isZeroAffine",o.getLocal("p2")),o.ret(o.i32_const(0))),o.if(o.call(a+"_isOne",s),o.ret(o.call(e+"_eqAffine",o.getLocal("p1"),o.getLocal("p2")))),o.call(a+"_square",s,u),o.call(a+"_mul",c,u,g),o.call(a+"_mul",s,u,f),o.call(a+"_mul",d,f,h),o.if(o.call(a+"_eq",l,g),o.if(o.call(a+"_eq",r,h),o.ret(o.i32_const(1)))),o.ret(o.i32_const(0)))}(),function(){const i=t.addFunction(e+"_eq");i.addParam("p1","i32"),i.addParam("p2","i32"),i.setReturnType("i32"),i.addLocal("z1","i32"),i.addLocal("z2","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n));i.addCode(o.setLocal("z1",o.i32_add(o.getLocal("p1"),o.i32_const(2*n))));const s=o.getLocal("z1"),c=o.getLocal("p2"),d=o.i32_add(o.getLocal("p2"),o.i32_const(n));i.addCode(o.setLocal("z2",o.i32_add(o.getLocal("p2"),o.i32_const(2*n))));const u=o.getLocal("z2"),g=o.i32_const(t.alloc(n)),f=o.i32_const(t.alloc(n)),h=o.i32_const(t.alloc(n)),_=o.i32_const(t.alloc(n)),p=o.i32_const(t.alloc(n)),m=o.i32_const(t.alloc(n)),w=o.i32_const(t.alloc(n)),L=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),o.ret(o.call(e+"_isZero",o.getLocal("p2")))),o.if(o.call(e+"_isZero",o.getLocal("p2")),o.ret(o.i32_const(0))),o.if(o.call(a+"_isOne",s),o.ret(o.call(e+"_eqMixed",o.getLocal("p2"),o.getLocal("p1")))),o.if(o.call(a+"_isOne",u),o.ret(o.call(e+"_eqMixed",o.getLocal("p1"),o.getLocal("p2")))),o.call(a+"_square",s,g),o.call(a+"_square",u,f),o.call(a+"_mul",l,f,h),o.call(a+"_mul",c,g,_),o.call(a+"_mul",s,g,p),o.call(a+"_mul",u,f,m),o.call(a+"_mul",r,m,w),o.call(a+"_mul",d,p,L),o.if(o.call(a+"_eq",h,_),o.if(o.call(a+"_eq",w,L),o.ret(o.i32_const(1)))),o.ret(o.i32_const(0)))}(),function(){const i=t.addFunction(e+"_doubleAffine");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.getLocal("pr"),c=o.i32_add(o.getLocal("pr"),o.i32_const(n)),d=o.i32_add(o.getLocal("pr"),o.i32_const(2*n)),u=o.i32_const(t.alloc(n)),g=o.i32_const(t.alloc(n)),f=o.i32_const(t.alloc(n)),h=o.i32_const(t.alloc(n)),_=o.i32_const(t.alloc(n)),p=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZeroAffine",o.getLocal("p1")),[...o.call(e+"_toJacobian",o.getLocal("p1"),o.getLocal("pr")),...o.ret([])]),o.call(a+"_square",l,u),o.call(a+"_square",r,g),o.call(a+"_square",g,f),o.call(a+"_add",l,g,h),o.call(a+"_square",h,h),o.call(a+"_sub",h,u,h),o.call(a+"_sub",h,f,h),o.call(a+"_add",h,h,h),o.call(a+"_add",u,u,_),o.call(a+"_add",_,u,_),o.call(a+"_add",r,r,d),o.call(a+"_square",_,s),o.call(a+"_sub",s,h,s),o.call(a+"_sub",s,h,s),o.call(a+"_add",f,f,p),o.call(a+"_add",p,p,p),o.call(a+"_add",p,p,p),o.call(a+"_sub",h,s,c),o.call(a+"_mul",c,_,c),o.call(a+"_sub",c,p,c))}(),function(){const i=t.addFunction(e+"_double");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.i32_add(o.getLocal("p1"),o.i32_const(2*n)),c=o.getLocal("pr"),d=o.i32_add(o.getLocal("pr"),o.i32_const(n)),u=o.i32_add(o.getLocal("pr"),o.i32_const(2*n)),g=o.i32_const(t.alloc(n)),f=o.i32_const(t.alloc(n)),h=o.i32_const(t.alloc(n)),_=o.i32_const(t.alloc(n)),p=o.i32_const(t.alloc(n)),m=o.i32_const(t.alloc(n)),w=o.i32_const(t.alloc(n)),L=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),[...o.call(e+"_copy",o.getLocal("p1"),o.getLocal("pr")),...o.ret([])]),o.if(o.call(a+"_isOne",s),[...o.ret(o.call(e+"_doubleAffine",o.getLocal("p1"),o.getLocal("pr"))),...o.ret([])]),o.call(a+"_square",l,g),o.call(a+"_square",r,f),o.call(a+"_square",f,h),o.call(a+"_add",l,f,_),o.call(a+"_square",_,_),o.call(a+"_sub",_,g,_),o.call(a+"_sub",_,h,_),o.call(a+"_add",_,_,_),o.call(a+"_add",g,g,p),o.call(a+"_add",p,g,p),o.call(a+"_square",p,m),o.call(a+"_mul",r,s,w),o.call(a+"_add",_,_,c),o.call(a+"_sub",m,c,c),o.call(a+"_add",h,h,L),o.call(a+"_add",L,L,L),o.call(a+"_add",L,L,L),o.call(a+"_sub",_,c,d),o.call(a+"_mul",d,p,d),o.call(a+"_sub",d,L,d),o.call(a+"_add",w,w,u))}(),function(){const i=t.addFunction(e+"_addAffine");i.addParam("p1","i32"),i.addParam("p2","i32"),i.addParam("pr","i32"),i.addLocal("z1","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n));i.addCode(o.setLocal("z1",o.i32_add(o.getLocal("p1"),o.i32_const(2*n))));const s=o.getLocal("p2"),c=o.i32_add(o.getLocal("p2"),o.i32_const(n)),d=o.getLocal("pr"),u=o.i32_add(o.getLocal("pr"),o.i32_const(n)),g=o.i32_add(o.getLocal("pr"),o.i32_const(2*n)),f=o.i32_const(t.alloc(n)),h=o.i32_const(t.alloc(n)),_=o.i32_const(t.alloc(n)),p=o.i32_const(t.alloc(n)),m=o.i32_const(t.alloc(n)),w=o.i32_const(t.alloc(n)),L=o.i32_const(t.alloc(n)),b=o.i32_const(t.alloc(n)),A=o.i32_const(t.alloc(n)),y=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZeroAffine",o.getLocal("p1")),[...o.call(e+"_copyAffine",o.getLocal("p2"),o.getLocal("pr")),...o.call(a+"_one",o.i32_add(o.getLocal("pr"),o.i32_const(2*n))),...o.ret([])]),o.if(o.call(e+"_isZeroAffine",o.getLocal("p2")),[...o.call(e+"_copyAffine",o.getLocal("p1"),o.getLocal("pr")),...o.call(a+"_one",o.i32_add(o.getLocal("pr"),o.i32_const(2*n))),...o.ret([])]),o.if(o.call(a+"_eq",l,s),o.if(o.call(a+"_eq",r,c),[...o.call(e+"_doubleAffine",o.getLocal("p2"),o.getLocal("pr")),...o.ret([])])),o.call(a+"_sub",s,l,f),o.call(a+"_sub",c,r,_),o.call(a+"_square",f,h),o.call(a+"_add",h,h,p),o.call(a+"_add",p,p,p),o.call(a+"_mul",f,p,m),o.call(a+"_add",_,_,w),o.call(a+"_mul",l,p,b),o.call(a+"_square",w,L),o.call(a+"_add",b,b,A),o.call(a+"_sub",L,m,d),o.call(a+"_sub",d,A,d),o.call(a+"_mul",r,m,y),o.call(a+"_add",y,y,y),o.call(a+"_sub",b,d,u),o.call(a+"_mul",u,w,u),o.call(a+"_sub",u,y,u),o.call(a+"_add",f,f,g))}(),function(){const i=t.addFunction(e+"_addMixed");i.addParam("p1","i32"),i.addParam("p2","i32"),i.addParam("pr","i32"),i.addLocal("z1","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n));i.addCode(o.setLocal("z1",o.i32_add(o.getLocal("p1"),o.i32_const(2*n))));const s=o.getLocal("z1"),c=o.getLocal("p2"),d=o.i32_add(o.getLocal("p2"),o.i32_const(n)),u=o.getLocal("pr"),g=o.i32_add(o.getLocal("pr"),o.i32_const(n)),f=o.i32_add(o.getLocal("pr"),o.i32_const(2*n)),h=o.i32_const(t.alloc(n)),_=o.i32_const(t.alloc(n)),p=o.i32_const(t.alloc(n)),m=o.i32_const(t.alloc(n)),w=o.i32_const(t.alloc(n)),L=o.i32_const(t.alloc(n)),b=o.i32_const(t.alloc(n)),A=o.i32_const(t.alloc(n)),y=o.i32_const(t.alloc(n)),C=o.i32_const(t.alloc(n)),I=o.i32_const(t.alloc(n)),F=o.i32_const(t.alloc(n)),x=o.i32_const(t.alloc(n)),E=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),[...o.call(e+"_copyAffine",o.getLocal("p2"),o.getLocal("pr")),...o.call(a+"_one",o.i32_add(o.getLocal("pr"),o.i32_const(2*n))),...o.ret([])]),o.if(o.call(e+"_isZeroAffine",o.getLocal("p2")),[...o.call(e+"_copy",o.getLocal("p1"),o.getLocal("pr")),...o.ret([])]),o.if(o.call(a+"_isOne",s),[...o.call(e+"_addAffine",l,c,u),...o.ret([])]),o.call(a+"_square",s,h),o.call(a+"_mul",c,h,_),o.call(a+"_mul",s,h,p),o.call(a+"_mul",d,p,m),o.if(o.call(a+"_eq",l,_),o.if(o.call(a+"_eq",r,m),[...o.call(e+"_doubleAffine",o.getLocal("p2"),o.getLocal("pr")),...o.ret([])])),o.call(a+"_sub",_,l,w),o.call(a+"_sub",m,r,b),o.call(a+"_square",w,L),o.call(a+"_add",L,L,A),o.call(a+"_add",A,A,A),o.call(a+"_mul",w,A,y),o.call(a+"_add",b,b,C),o.call(a+"_mul",l,A,F),o.call(a+"_square",C,I),o.call(a+"_add",F,F,x),o.call(a+"_sub",I,y,u),o.call(a+"_sub",u,x,u),o.call(a+"_mul",r,y,E),o.call(a+"_add",E,E,E),o.call(a+"_sub",F,u,g),o.call(a+"_mul",g,C,g),o.call(a+"_sub",g,E,g),o.call(a+"_add",s,w,f),o.call(a+"_square",f,f),o.call(a+"_sub",f,h,f),o.call(a+"_sub",f,L,f))}(),function(){const i=t.addFunction(e+"_add");i.addParam("p1","i32"),i.addParam("p2","i32"),i.addParam("pr","i32"),i.addLocal("z1","i32"),i.addLocal("z2","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n));i.addCode(o.setLocal("z1",o.i32_add(o.getLocal("p1"),o.i32_const(2*n))));const s=o.getLocal("z1"),c=o.getLocal("p2"),d=o.i32_add(o.getLocal("p2"),o.i32_const(n));i.addCode(o.setLocal("z2",o.i32_add(o.getLocal("p2"),o.i32_const(2*n))));const u=o.getLocal("z2"),g=o.getLocal("pr"),f=o.i32_add(o.getLocal("pr"),o.i32_const(n)),h=o.i32_add(o.getLocal("pr"),o.i32_const(2*n)),_=o.i32_const(t.alloc(n)),p=o.i32_const(t.alloc(n)),m=o.i32_const(t.alloc(n)),w=o.i32_const(t.alloc(n)),L=o.i32_const(t.alloc(n)),b=o.i32_const(t.alloc(n)),A=o.i32_const(t.alloc(n)),y=o.i32_const(t.alloc(n)),C=o.i32_const(t.alloc(n)),I=o.i32_const(t.alloc(n)),F=o.i32_const(t.alloc(n)),x=o.i32_const(t.alloc(n)),E=o.i32_const(t.alloc(n)),v=o.i32_const(t.alloc(n)),B=o.i32_const(t.alloc(n)),S=o.i32_const(t.alloc(n)),P=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),[...o.call(e+"_copy",o.getLocal("p2"),o.getLocal("pr")),...o.ret([])]),o.if(o.call(e+"_isZero",o.getLocal("p2")),[...o.call(e+"_copy",o.getLocal("p1"),o.getLocal("pr")),...o.ret([])]),o.if(o.call(a+"_isOne",s),[...o.call(e+"_addMixed",c,l,g),...o.ret([])]),o.if(o.call(a+"_isOne",u),[...o.call(e+"_addMixed",l,c,g),...o.ret([])]),o.call(a+"_square",s,_),o.call(a+"_square",u,p),o.call(a+"_mul",l,p,m),o.call(a+"_mul",c,_,w),o.call(a+"_mul",s,_,L),o.call(a+"_mul",u,p,b),o.call(a+"_mul",r,b,A),o.call(a+"_mul",d,L,y),o.if(o.call(a+"_eq",m,w),o.if(o.call(a+"_eq",A,y),[...o.call(e+"_double",o.getLocal("p1"),o.getLocal("pr")),...o.ret([])])),o.call(a+"_sub",w,m,C),o.call(a+"_sub",y,A,I),o.call(a+"_add",C,C,F),o.call(a+"_square",F,F),o.call(a+"_mul",C,F,x),o.call(a+"_add",I,I,E),o.call(a+"_mul",m,F,B),o.call(a+"_square",E,v),o.call(a+"_add",B,B,S),o.call(a+"_sub",v,x,g),o.call(a+"_sub",g,S,g),o.call(a+"_mul",A,x,P),o.call(a+"_add",P,P,P),o.call(a+"_sub",B,g,f),o.call(a+"_mul",f,E,f),o.call(a+"_sub",f,P,f),o.call(a+"_add",s,u,h),o.call(a+"_square",h,h),o.call(a+"_sub",h,_,h),o.call(a+"_sub",h,p,h),o.call(a+"_mul",h,C,h))}(),function(){const i=t.addFunction(e+"_negAffine");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.getLocal("pr"),c=o.i32_add(o.getLocal("pr"),o.i32_const(n));i.addCode(o.call(a+"_copy",l,s),o.call(a+"_neg",r,c))}(),function(){const i=t.addFunction(e+"_neg");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.i32_add(o.getLocal("p1"),o.i32_const(2*n)),c=o.getLocal("pr"),d=o.i32_add(o.getLocal("pr"),o.i32_const(n)),u=o.i32_add(o.getLocal("pr"),o.i32_const(2*n));i.addCode(o.call(a+"_copy",l,c),o.call(a+"_neg",r,d),o.call(a+"_copy",s,u))}(),function(){const a=t.addFunction(e+"_subAffine");a.addParam("p1","i32"),a.addParam("p2","i32"),a.addParam("pr","i32");const i=a.getCodeBuilder(),o=i.i32_const(t.alloc(3*n));a.addCode(i.call(e+"_negAffine",i.getLocal("p2"),o),i.call(e+"_addAffine",i.getLocal("p1"),o,i.getLocal("pr")))}(),function(){const a=t.addFunction(e+"_subMixed");a.addParam("p1","i32"),a.addParam("p2","i32"),a.addParam("pr","i32");const i=a.getCodeBuilder(),o=i.i32_const(t.alloc(3*n));a.addCode(i.call(e+"_negAffine",i.getLocal("p2"),o),i.call(e+"_addMixed",i.getLocal("p1"),o,i.getLocal("pr")))}(),function(){const a=t.addFunction(e+"_sub");a.addParam("p1","i32"),a.addParam("p2","i32"),a.addParam("pr","i32");const i=a.getCodeBuilder(),o=i.i32_const(t.alloc(3*n));a.addCode(i.call(e+"_neg",i.getLocal("p2"),o),i.call(e+"_add",i.getLocal("p1"),o,i.getLocal("pr")))}(),function(){const i=t.addFunction(e+"_fromMontgomeryAffine");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_fromMontgomery",o.getLocal("p1"),o.getLocal("pr")));for(let t=1;t<2;t++)i.addCode(o.call(a+"_fromMontgomery",o.i32_add(o.getLocal("p1"),o.i32_const(t*n)),o.i32_add(o.getLocal("pr"),o.i32_const(t*n))))}(),function(){const i=t.addFunction(e+"_fromMontgomery");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_fromMontgomery",o.getLocal("p1"),o.getLocal("pr")));for(let t=1;t<3;t++)i.addCode(o.call(a+"_fromMontgomery",o.i32_add(o.getLocal("p1"),o.i32_const(t*n)),o.i32_add(o.getLocal("pr"),o.i32_const(t*n))))}(),function(){const i=t.addFunction(e+"_toMontgomeryAffine");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_toMontgomery",o.getLocal("p1"),o.getLocal("pr")));for(let t=1;t<2;t++)i.addCode(o.call(a+"_toMontgomery",o.i32_add(o.getLocal("p1"),o.i32_const(t*n)),o.i32_add(o.getLocal("pr"),o.i32_const(t*n))))}(),function(){const i=t.addFunction(e+"_toMontgomery");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder();i.addCode(o.call(a+"_toMontgomery",o.getLocal("p1"),o.getLocal("pr")));for(let t=1;t<3;t++)i.addCode(o.call(a+"_toMontgomery",o.i32_add(o.getLocal("p1"),o.i32_const(t*n)),o.i32_add(o.getLocal("pr"),o.i32_const(t*n))))}(),function(){const i=t.addFunction(e+"_toAffine");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.i32_add(o.getLocal("p1"),o.i32_const(2*n)),c=o.getLocal("pr"),d=o.i32_add(o.getLocal("pr"),o.i32_const(n)),u=o.i32_const(t.alloc(n)),g=o.i32_const(t.alloc(n)),f=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),[...o.call(a+"_zero",c),...o.call(a+"_zero",d)],[...o.call(a+"_inverse",s,u),...o.call(a+"_square",u,g),...o.call(a+"_mul",u,g,f),...o.call(a+"_mul",l,g,c),...o.call(a+"_mul",r,f,d)]))}(),function(){const o=t.addFunction(e+"_inCurveAffine");o.addParam("pIn","i32"),o.setReturnType("i32");const l=o.getCodeBuilder(),r=l.getLocal("pIn"),s=l.i32_add(l.getLocal("pIn"),l.i32_const(n)),c=l.i32_const(t.alloc(n)),d=l.i32_const(t.alloc(n));o.addCode(l.call(a+"_square",s,c),l.call(a+"_square",r,d),l.call(a+"_mul",r,d,d),l.call(a+"_add",d,l.i32_const(i),d),l.ret(l.call(a+"_eq",c,d)))}(),function(){const a=t.addFunction(e+"_inCurve");a.addParam("pIn","i32"),a.setReturnType("i32");const i=a.getCodeBuilder(),o=i.i32_const(t.alloc(2*n));a.addCode(i.call(e+"_toAffine",i.getLocal("pIn"),o),i.ret(i.call(e+"_inCurveAffine",o)))}(),function(){const i=t.addFunction(e+"_batchToAffine");i.addParam("pIn","i32"),i.addParam("n","i32"),i.addParam("pOut","i32"),i.addLocal("pAux","i32"),i.addLocal("itIn","i32"),i.addLocal("itAux","i32"),i.addLocal("itOut","i32"),i.addLocal("i","i32");const o=i.getCodeBuilder(),l=o.i32_const(t.alloc(n));i.addCode(o.setLocal("pAux",o.i32_load(o.i32_const(0))),o.i32_store(o.i32_const(0),o.i32_add(o.getLocal("pAux"),o.i32_mul(o.getLocal("n"),o.i32_const(n)))),o.call(a+"_batchInverse",o.i32_add(o.getLocal("pIn"),o.i32_const(2*n)),o.i32_const(3*n),o.getLocal("n"),o.getLocal("pAux"),o.i32_const(n)),o.setLocal("itIn",o.getLocal("pIn")),o.setLocal("itAux",o.getLocal("pAux")),o.setLocal("itOut",o.getLocal("pOut")),o.setLocal("i",o.i32_const(0)),o.block(o.loop(o.br_if(1,o.i32_eq(o.getLocal("i"),o.getLocal("n"))),o.if(o.call(a+"_isZero",o.getLocal("itAux")),[...o.call(a+"_zero",o.getLocal("itOut")),...o.call(a+"_zero",o.i32_add(o.getLocal("itOut"),o.i32_const(n)))],[...o.call(a+"_mul",o.getLocal("itAux"),o.i32_add(o.getLocal("itIn"),o.i32_const(n)),l),...o.call(a+"_square",o.getLocal("itAux"),o.getLocal("itAux")),...o.call(a+"_mul",o.getLocal("itAux"),o.getLocal("itIn"),o.getLocal("itOut")),...o.call(a+"_mul",o.getLocal("itAux"),l,o.i32_add(o.getLocal("itOut"),o.i32_const(n)))]),o.setLocal("itIn",o.i32_add(o.getLocal("itIn"),o.i32_const(3*n))),o.setLocal("itOut",o.i32_add(o.getLocal("itOut"),o.i32_const(2*n))),o.setLocal("itAux",o.i32_add(o.getLocal("itAux"),o.i32_const(n))),o.setLocal("i",o.i32_add(o.getLocal("i"),o.i32_const(1))),o.br(0))),o.i32_store(o.i32_const(0),o.getLocal("pAux")))}(),function(){const i=t.addFunction(e+"_normalize");i.addParam("p1","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),l=o.getLocal("p1"),r=o.i32_add(o.getLocal("p1"),o.i32_const(n)),s=o.i32_add(o.getLocal("p1"),o.i32_const(2*n)),c=o.getLocal("pr"),d=o.i32_add(o.getLocal("pr"),o.i32_const(n)),u=o.i32_add(o.getLocal("pr"),o.i32_const(2*n)),g=o.i32_const(t.alloc(n)),f=o.i32_const(t.alloc(n)),h=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZero",o.getLocal("p1")),o.call(e+"_zero",o.getLocal("pr")),[...o.call(a+"_inverse",s,g),...o.call(a+"_square",g,f),...o.call(a+"_mul",g,f,h),...o.call(a+"_mul",l,f,c),...o.call(a+"_mul",r,h,d),...o.call(a+"_one",u)]))}(),function(){const a=t.addFunction(e+"__reverseBytes");a.addParam("pIn","i32"),a.addParam("n","i32"),a.addParam("pOut","i32"),a.addLocal("itOut","i32"),a.addLocal("itIn","i32");const i=a.getCodeBuilder();a.addCode(i.setLocal("itOut",i.i32_sub(i.i32_add(i.getLocal("pOut"),i.getLocal("n")),i.i32_const(1))),i.setLocal("itIn",i.getLocal("pIn")),i.block(i.loop(i.br_if(1,i.i32_lt_s(i.getLocal("itOut"),i.getLocal("pOut"))),i.i32_store8(i.getLocal("itOut"),i.i32_load8_u(i.getLocal("itIn"))),i.setLocal("itOut",i.i32_sub(i.getLocal("itOut"),i.i32_const(1))),i.setLocal("itIn",i.i32_add(i.getLocal("itIn"),i.i32_const(1))),i.br(0))))}(),function(){const a=t.addFunction(e+"_LEMtoU");a.addParam("pIn","i32"),a.addParam("pOut","i32");const i=a.getCodeBuilder(),o=t.alloc(2*n),l=i.i32_const(o),r=i.i32_const(o),s=i.i32_const(o+n);a.addCode(i.if(i.call(e+"_isZeroAffine",i.getLocal("pIn")),[...i.call(e+"_zeroAffine",i.getLocal("pOut")),...i.ret([])]),i.call(e+"_fromMontgomeryAffine",i.getLocal("pIn"),l),i.call(e+"__reverseBytes",r,i.i32_const(n),i.getLocal("pOut")),i.call(e+"__reverseBytes",s,i.i32_const(n),i.i32_add(i.getLocal("pOut"),i.i32_const(n))))}(),function(){const i=t.addFunction(e+"_LEMtoC");i.addParam("pIn","i32"),i.addParam("pOut","i32");const o=i.getCodeBuilder(),l=o.i32_const(t.alloc(n));i.addCode(o.if(o.call(e+"_isZeroAffine",o.getLocal("pIn")),[...o.call(a+"_zero",o.getLocal("pOut")),...o.i32_store8(o.getLocal("pOut"),o.i32_const(64)),...o.ret([])]),o.call(a+"_fromMontgomery",o.getLocal("pIn"),l),o.call(e+"__reverseBytes",l,o.i32_const(n),o.getLocal("pOut")),o.if(o.i32_eq(o.call(a+"_sign",o.i32_add(o.getLocal("pIn"),o.i32_const(n))),o.i32_const(-1)),o.i32_store8(o.getLocal("pOut"),o.i32_or(o.i32_load8_u(o.getLocal("pOut")),o.i32_const(128)))))}(),function(){const a=t.addFunction(e+"_UtoLEM");a.addParam("pIn","i32"),a.addParam("pOut","i32");const i=a.getCodeBuilder(),o=t.alloc(2*n),l=i.i32_const(o),r=i.i32_const(o),s=i.i32_const(o+n);a.addCode(i.if(i.i32_and(i.i32_load8_u(i.getLocal("pIn")),i.i32_const(64)),[...i.call(e+"_zeroAffine",i.getLocal("pOut")),...i.ret([])]),i.call(e+"__reverseBytes",i.getLocal("pIn"),i.i32_const(n),r),i.call(e+"__reverseBytes",i.i32_add(i.getLocal("pIn"),i.i32_const(n)),i.i32_const(n),s),i.call(e+"_toMontgomeryAffine",l,i.getLocal("pOut")))}(),function(){const o=t.addFunction(e+"_CtoLEM");o.addParam("pIn","i32"),o.addParam("pOut","i32"),o.addLocal("firstByte","i32"),o.addLocal("greatest","i32");const l=o.getCodeBuilder(),r=t.alloc(2*n),s=l.i32_const(r),c=l.i32_const(r+n);o.addCode(l.setLocal("firstByte",l.i32_load8_u(l.getLocal("pIn"))),l.if(l.i32_and(l.getLocal("firstByte"),l.i32_const(64)),[...l.call(e+"_zeroAffine",l.getLocal("pOut")),...l.ret([])]),l.setLocal("greatest",l.i32_and(l.getLocal("firstByte"),l.i32_const(128))),l.call(a+"_copy",l.getLocal("pIn"),c),l.i32_store8(c,l.i32_and(l.getLocal("firstByte"),l.i32_const(63))),l.call(e+"__reverseBytes",c,l.i32_const(n),s),l.call(a+"_toMontgomery",s,l.getLocal("pOut")),l.call(a+"_square",l.getLocal("pOut"),c),l.call(a+"_mul",l.getLocal("pOut"),c,c),l.call(a+"_add",c,l.i32_const(i),c),l.call(a+"_sqrt",c,c),l.call(a+"_neg",c,s),l.if(l.i32_eq(l.call(a+"_sign",c),l.i32_const(-1)),l.if(l.getLocal("greatest"),l.call(a+"_copy",c,l.i32_add(l.getLocal("pOut"),l.i32_const(n))),l.call(a+"_neg",c,l.i32_add(l.getLocal("pOut"),l.i32_const(n)))),l.if(l.getLocal("greatest"),l.call(a+"_neg",c,l.i32_add(l.getLocal("pOut"),l.i32_const(n))),l.call(a+"_copy",c,l.i32_add(l.getLocal("pOut"),l.i32_const(n))))))}(),Ut(t,e+"_batchLEMtoU",e+"_LEMtoU",2*n,2*n),Ut(t,e+"_batchLEMtoC",e+"_LEMtoC",2*n,n),Ut(t,e+"_batchUtoLEM",e+"_UtoLEM",2*n,2*n),Ut(t,e+"_batchCtoLEM",e+"_CtoLEM",n,2*n,!0),Ut(t,e+"_batchToJacobian",e+"_toJacobian",2*n,3*n,!0),Qt(t,e,e+"_multiexp",e+"_add",3*n),Qt(t,e,e+"_multiexpAffine",e+"_addMixed",2*n),zt(t,e+"_timesScalar",3*n,e+"_add",e+"_double",e+"_sub",e+"_copy",e+"_zero"),zt(t,e+"_timesScalarAffine",2*n,e+"_addMixed",e+"_double",e+"_subMixed",e+"_copyAffine",e+"_zero"),t.exportFunction(e+"_isZero"),t.exportFunction(e+"_isZeroAffine"),t.exportFunction(e+"_eq"),t.exportFunction(e+"_eqMixed"),t.exportFunction(e+"_eqAffine"),t.exportFunction(e+"_copy"),t.exportFunction(e+"_copyAffine"),t.exportFunction(e+"_zero"),t.exportFunction(e+"_zeroAffine"),t.exportFunction(e+"_double"),t.exportFunction(e+"_doubleAffine"),t.exportFunction(e+"_add"),t.exportFunction(e+"_addMixed"),t.exportFunction(e+"_addAffine"),t.exportFunction(e+"_neg"),t.exportFunction(e+"_negAffine"),t.exportFunction(e+"_sub"),t.exportFunction(e+"_subMixed"),t.exportFunction(e+"_subAffine"),t.exportFunction(e+"_fromMontgomery"),t.exportFunction(e+"_fromMontgomeryAffine"),t.exportFunction(e+"_toMontgomery"),t.exportFunction(e+"_toMontgomeryAffine"),t.exportFunction(e+"_timesScalar"),t.exportFunction(e+"_timesScalarAffine"),t.exportFunction(e+"_normalize"),t.exportFunction(e+"_LEMtoU"),t.exportFunction(e+"_LEMtoC"),t.exportFunction(e+"_UtoLEM"),t.exportFunction(e+"_CtoLEM"),t.exportFunction(e+"_batchLEMtoU"),t.exportFunction(e+"_batchLEMtoC"),t.exportFunction(e+"_batchUtoLEM"),t.exportFunction(e+"_batchCtoLEM"),t.exportFunction(e+"_toAffine"),t.exportFunction(e+"_toJacobian"),t.exportFunction(e+"_batchToAffine"),t.exportFunction(e+"_batchToJacobian"),t.exportFunction(e+"_inCurve"),t.exportFunction(e+"_inCurveAffine"),e};const{isOdd:Mt,modInv:kt,modPow:Rt}=J,Dt=j;var Nt=function(t,e,a,i,o){const n=8*t.modules[i].n64,l=8*t.modules[a].n64,r=t.modules[i].q;let s=r-1n,c=0;for(;!Mt(s);)c++,s>>=1n;let d=2n;for(;1n===Rt(d,r>>1n,r);)d+=1n;const u=new Array(c+1);u[c]=Rt(d,s,r);let g=c-1;for(;g>=0;)u[g]=Rt(u[g+1],2n,r),g--;const f=[],h=(1n<>a);return e}const F=Array(256);for(let t=0;t<256;t++)F[t]=I(t);const x=t.alloc(F);function E(){const a=t.addFunction(e+"_fft");a.addParam("px","i32"),a.addParam("n","i32"),a.addLocal("bits","i32");const o=a.getCodeBuilder(),l=o.i32_const(t.alloc(n));a.addCode(o.setLocal("bits",o.call(e+"__log2",o.getLocal("n"))),o.call(i+"_one",l),o.call(e+"_rawfft",o.getLocal("px"),o.getLocal("bits"),o.i32_const(0),l))}!function(){const a=t.addFunction(e+"__rev");a.addParam("x","i32"),a.addParam("bits","i32"),a.setReturnType("i32");const i=a.getCodeBuilder();a.addCode(i.i32_rotl(i.i32_add(i.i32_add(i.i32_shl(i.i32_load8_u(i.i32_and(i.getLocal("x"),i.i32_const(255)),x,0),i.i32_const(24)),i.i32_shl(i.i32_load8_u(i.i32_and(i.i32_shr_u(i.getLocal("x"),i.i32_const(8)),i.i32_const(255)),x,0),i.i32_const(16))),i.i32_add(i.i32_shl(i.i32_load8_u(i.i32_and(i.i32_shr_u(i.getLocal("x"),i.i32_const(16)),i.i32_const(255)),x,0),i.i32_const(8)),i.i32_load8_u(i.i32_and(i.i32_shr_u(i.getLocal("x"),i.i32_const(24)),i.i32_const(255)),x,0))),i.getLocal("bits")))}(),function(){const i=t.addFunction(e+"__reversePermutation");i.addParam("px","i32"),i.addParam("bits","i32"),i.addLocal("n","i32"),i.addLocal("i","i32"),i.addLocal("ri","i32"),i.addLocal("idx1","i32"),i.addLocal("idx2","i32");const o=i.getCodeBuilder(),n=o.i32_const(t.alloc(l));i.addCode(o.setLocal("n",o.i32_shl(o.i32_const(1),o.getLocal("bits"))),o.setLocal("i",o.i32_const(0)),o.block(o.loop(o.br_if(1,o.i32_eq(o.getLocal("i"),o.getLocal("n"))),o.setLocal("idx1",o.i32_add(o.getLocal("px"),o.i32_mul(o.getLocal("i"),o.i32_const(l)))),o.setLocal("ri",o.call(e+"__rev",o.getLocal("i"),o.getLocal("bits"))),o.setLocal("idx2",o.i32_add(o.getLocal("px"),o.i32_mul(o.getLocal("ri"),o.i32_const(l)))),o.if(o.i32_lt_u(o.getLocal("i"),o.getLocal("ri")),[...o.call(a+"_copy",o.getLocal("idx1"),n),...o.call(a+"_copy",o.getLocal("idx2"),o.getLocal("idx1")),...o.call(a+"_copy",n,o.getLocal("idx2"))]),o.setLocal("i",o.i32_add(o.getLocal("i"),o.i32_const(1))),o.br(0))))}(),function(){const n=t.addFunction(e+"__fftFinal");n.addParam("px","i32"),n.addParam("bits","i32"),n.addParam("reverse","i32"),n.addParam("mulFactor","i32"),n.addLocal("n","i32"),n.addLocal("ndiv2","i32"),n.addLocal("pInv2","i32"),n.addLocal("i","i32"),n.addLocal("mask","i32"),n.addLocal("idx1","i32"),n.addLocal("idx2","i32");const r=n.getCodeBuilder(),s=r.i32_const(t.alloc(l));n.addCode(r.if(r.i32_and(r.i32_eqz(r.getLocal("reverse")),r.call(i+"_isOne",r.getLocal("mulFactor"))),r.ret([])),r.setLocal("n",r.i32_shl(r.i32_const(1),r.getLocal("bits"))),r.setLocal("mask",r.i32_sub(r.getLocal("n"),r.i32_const(1))),r.setLocal("i",r.i32_const(1)),r.setLocal("ndiv2",r.i32_shr_u(r.getLocal("n"),r.i32_const(1))),r.block(r.loop(r.br_if(1,r.i32_ge_u(r.getLocal("i"),r.getLocal("ndiv2"))),r.setLocal("idx1",r.i32_add(r.getLocal("px"),r.i32_mul(r.getLocal("i"),r.i32_const(l)))),r.setLocal("idx2",r.i32_add(r.getLocal("px"),r.i32_mul(r.i32_sub(r.getLocal("n"),r.getLocal("i")),r.i32_const(l)))),r.if(r.getLocal("reverse"),r.if(r.call(i+"_isOne",r.getLocal("mulFactor")),[...r.call(a+"_copy",r.getLocal("idx1"),s),...r.call(a+"_copy",r.getLocal("idx2"),r.getLocal("idx1")),...r.call(a+"_copy",s,r.getLocal("idx2"))],[...r.call(a+"_copy",r.getLocal("idx1"),s),...r.call(o,r.getLocal("idx2"),r.getLocal("mulFactor"),r.getLocal("idx1")),...r.call(o,s,r.getLocal("mulFactor"),r.getLocal("idx2"))]),r.if(r.call(i+"_isOne",r.getLocal("mulFactor")),[],[...r.call(o,r.getLocal("idx1"),r.getLocal("mulFactor"),r.getLocal("idx1")),...r.call(o,r.getLocal("idx2"),r.getLocal("mulFactor"),r.getLocal("idx2"))])),r.setLocal("i",r.i32_add(r.getLocal("i"),r.i32_const(1))),r.br(0))),r.if(r.call(i+"_isOne",r.getLocal("mulFactor")),[],[...r.call(o,r.getLocal("px"),r.getLocal("mulFactor"),r.getLocal("px")),...r.setLocal("idx2",r.i32_add(r.getLocal("px"),r.i32_mul(r.getLocal("ndiv2"),r.i32_const(l)))),...r.call(o,r.getLocal("idx2"),r.getLocal("mulFactor"),r.getLocal("idx2"))]))}(),function(){const r=t.addFunction(e+"_rawfft");r.addParam("px","i32"),r.addParam("bits","i32"),r.addParam("reverse","i32"),r.addParam("mulFactor","i32"),r.addLocal("s","i32"),r.addLocal("k","i32"),r.addLocal("j","i32"),r.addLocal("m","i32"),r.addLocal("mdiv2","i32"),r.addLocal("n","i32"),r.addLocal("pwm","i32"),r.addLocal("idx1","i32"),r.addLocal("idx2","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));r.addCode(s.call(e+"__reversePermutation",s.getLocal("px"),s.getLocal("bits")),s.setLocal("n",s.i32_shl(s.i32_const(1),s.getLocal("bits"))),s.setLocal("s",s.i32_const(1)),s.block(s.loop(s.br_if(1,s.i32_gt_u(s.getLocal("s"),s.getLocal("bits"))),s.setLocal("m",s.i32_shl(s.i32_const(1),s.getLocal("s"))),s.setLocal("pwm",s.i32_add(s.i32_const(_),s.i32_mul(s.getLocal("s"),s.i32_const(n)))),s.setLocal("k",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_ge_u(s.getLocal("k"),s.getLocal("n"))),s.call(i+"_one",c),s.setLocal("mdiv2",s.i32_shr_u(s.getLocal("m"),s.i32_const(1))),s.setLocal("j",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_ge_u(s.getLocal("j"),s.getLocal("mdiv2"))),s.setLocal("idx1",s.i32_add(s.getLocal("px"),s.i32_mul(s.i32_add(s.getLocal("k"),s.getLocal("j")),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("idx1"),s.i32_mul(s.getLocal("mdiv2"),s.i32_const(l)))),s.call(o,s.getLocal("idx2"),c,d),s.call(a+"_copy",s.getLocal("idx1"),u),s.call(a+"_add",u,d,s.getLocal("idx1")),s.call(a+"_sub",u,d,s.getLocal("idx2")),s.call(i+"_mul",c,s.getLocal("pwm"),c),s.setLocal("j",s.i32_add(s.getLocal("j"),s.i32_const(1))),s.br(0))),s.setLocal("k",s.i32_add(s.getLocal("k"),s.getLocal("m"))),s.br(0))),s.setLocal("s",s.i32_add(s.getLocal("s"),s.i32_const(1))),s.br(0))),s.call(e+"__fftFinal",s.getLocal("px"),s.getLocal("bits"),s.getLocal("reverse"),s.getLocal("mulFactor")))}(),function(){const a=t.addFunction(e+"__log2");a.addParam("n","i32"),a.setReturnType("i32"),a.addLocal("bits","i32"),a.addLocal("aux","i32");const i=a.getCodeBuilder();a.addCode(i.setLocal("aux",i.i32_shr_u(i.getLocal("n"),i.i32_const(1)))),a.addCode(i.setLocal("bits",i.i32_const(0))),a.addCode(i.block(i.loop(i.br_if(1,i.i32_eqz(i.getLocal("aux"))),i.setLocal("aux",i.i32_shr_u(i.getLocal("aux"),i.i32_const(1))),i.setLocal("bits",i.i32_add(i.getLocal("bits"),i.i32_const(1))),i.br(0)))),a.addCode(i.if(i.i32_ne(i.getLocal("n"),i.i32_shl(i.i32_const(1),i.getLocal("bits"))),i.unreachable())),a.addCode(i.if(i.i32_gt_u(i.getLocal("bits"),i.i32_const(c)),i.unreachable())),a.addCode(i.getLocal("bits"))}(),E(),function(){const a=t.addFunction(e+"_ifft");a.addParam("px","i32"),a.addParam("n","i32"),a.addLocal("bits","i32"),a.addLocal("pInv2","i32");const i=a.getCodeBuilder();a.addCode(i.setLocal("bits",i.call(e+"__log2",i.getLocal("n"))),i.setLocal("pInv2",i.i32_add(i.i32_const(w),i.i32_mul(i.getLocal("bits"),i.i32_const(n)))),i.call(e+"_rawfft",i.getLocal("px"),i.getLocal("bits"),i.i32_const(1),i.getLocal("pInv2")))}(),function(){const r=t.addFunction(e+"_fftJoin");r.addParam("pBuff1","i32"),r.addParam("pBuff2","i32"),r.addParam("n","i32"),r.addParam("first","i32"),r.addParam("inc","i32"),r.addLocal("idx1","i32"),r.addLocal("idx2","i32"),r.addLocal("i","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));r.addCode(s.call(i+"_copy",s.getLocal("first"),c),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(o,s.getLocal("idx2"),c,d),s.call(a+"_copy",s.getLocal("idx1"),u),s.call(a+"_add",u,d,s.getLocal("idx1")),s.call(a+"_sub",u,d,s.getLocal("idx2")),s.call(i+"_mul",c,s.getLocal("inc"),c),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const r=t.addFunction(e+"_fftJoinExt");r.addParam("pBuff1","i32"),r.addParam("pBuff2","i32"),r.addParam("n","i32"),r.addParam("first","i32"),r.addParam("inc","i32"),r.addParam("totalBits","i32"),r.addLocal("idx1","i32"),r.addLocal("idx2","i32"),r.addLocal("i","i32"),r.addLocal("pShiftToM","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));r.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(y),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(i+"_copy",s.getLocal("first"),c),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(a+"_add",s.getLocal("idx1"),s.getLocal("idx2"),d),s.call(o,s.getLocal("idx2"),s.getLocal("pShiftToM"),s.getLocal("idx2")),s.call(a+"_add",s.getLocal("idx1"),s.getLocal("idx2"),s.getLocal("idx2")),s.call(o,s.getLocal("idx2"),c,s.getLocal("idx2")),s.call(a+"_copy",d,s.getLocal("idx1")),s.call(i+"_mul",c,s.getLocal("inc"),c),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const r=t.addFunction(e+"_fftJoinExtInv");r.addParam("pBuff1","i32"),r.addParam("pBuff2","i32"),r.addParam("n","i32"),r.addParam("first","i32"),r.addParam("inc","i32"),r.addParam("totalBits","i32"),r.addLocal("idx1","i32"),r.addLocal("idx2","i32"),r.addLocal("i","i32"),r.addLocal("pShiftToM","i32"),r.addLocal("pSConst","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));r.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(y),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.setLocal("pSConst",s.i32_add(s.i32_const(C),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(i+"_copy",s.getLocal("first"),c),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(o,s.getLocal("idx2"),c,d),s.call(a+"_sub",s.getLocal("idx1"),d,s.getLocal("idx2")),s.call(o,s.getLocal("idx2"),s.getLocal("pSConst"),s.getLocal("idx2")),s.call(o,s.getLocal("idx1"),s.getLocal("pShiftToM"),s.getLocal("idx1")),s.call(a+"_sub",d,s.getLocal("idx1"),s.getLocal("idx1")),s.call(o,s.getLocal("idx1"),s.getLocal("pSConst"),s.getLocal("idx1")),s.call(i+"_mul",c,s.getLocal("inc"),c),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const r=t.addFunction(e+"_fftMix");r.addParam("pBuff","i32"),r.addParam("n","i32"),r.addParam("exp","i32"),r.addLocal("nGroups","i32"),r.addLocal("nPerGroup","i32"),r.addLocal("nPerGroupDiv2","i32"),r.addLocal("pairOffset","i32"),r.addLocal("idx1","i32"),r.addLocal("idx2","i32"),r.addLocal("i","i32"),r.addLocal("j","i32"),r.addLocal("pwm","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));r.addCode(s.setLocal("nPerGroup",s.i32_shl(s.i32_const(1),s.getLocal("exp"))),s.setLocal("nPerGroupDiv2",s.i32_shr_u(s.getLocal("nPerGroup"),s.i32_const(1))),s.setLocal("nGroups",s.i32_shr_u(s.getLocal("n"),s.getLocal("exp"))),s.setLocal("pairOffset",s.i32_mul(s.getLocal("nPerGroupDiv2"),s.i32_const(l))),s.setLocal("pwm",s.i32_add(s.i32_const(_),s.i32_mul(s.getLocal("exp"),s.i32_const(n)))),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("nGroups"))),s.call(i+"_one",c),s.setLocal("j",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("j"),s.getLocal("nPerGroupDiv2"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff"),s.i32_mul(s.i32_add(s.i32_mul(s.getLocal("i"),s.getLocal("nPerGroup")),s.getLocal("j")),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("idx1"),s.getLocal("pairOffset"))),s.call(o,s.getLocal("idx2"),c,d),s.call(a+"_copy",s.getLocal("idx1"),u),s.call(a+"_add",u,d,s.getLocal("idx1")),s.call(a+"_sub",u,d,s.getLocal("idx2")),s.call(i+"_mul",c,s.getLocal("pwm"),c),s.setLocal("j",s.i32_add(s.getLocal("j"),s.i32_const(1))),s.br(0))),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const i=t.addFunction(e+"_fftFinal");i.addParam("pBuff","i32"),i.addParam("n","i32"),i.addParam("factor","i32"),i.addLocal("idx1","i32"),i.addLocal("idx2","i32"),i.addLocal("i","i32"),i.addLocal("ndiv2","i32");const n=i.getCodeBuilder(),r=n.i32_const(t.alloc(l));i.addCode(n.setLocal("ndiv2",n.i32_shr_u(n.getLocal("n"),n.i32_const(1))),n.if(n.i32_and(n.getLocal("n"),n.i32_const(1)),n.call(o,n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("ndiv2"),n.i32_const(l))),n.getLocal("factor"),n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("ndiv2"),n.i32_const(l))))),n.setLocal("i",n.i32_const(0)),n.block(n.loop(n.br_if(1,n.i32_ge_u(n.getLocal("i"),n.getLocal("ndiv2"))),n.setLocal("idx1",n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("i"),n.i32_const(l)))),n.setLocal("idx2",n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.i32_sub(n.i32_sub(n.getLocal("n"),n.i32_const(1)),n.getLocal("i")),n.i32_const(l)))),n.call(o,n.getLocal("idx2"),n.getLocal("factor"),r),n.call(o,n.getLocal("idx1"),n.getLocal("factor"),n.getLocal("idx2")),n.call(a+"_copy",r,n.getLocal("idx1")),n.setLocal("i",n.i32_add(n.getLocal("i"),n.i32_const(1))),n.br(0))))}(),function(){const r=t.addFunction(e+"_prepareLagrangeEvaluation");r.addParam("pBuff1","i32"),r.addParam("pBuff2","i32"),r.addParam("n","i32"),r.addParam("first","i32"),r.addParam("inc","i32"),r.addParam("totalBits","i32"),r.addLocal("idx1","i32"),r.addLocal("idx2","i32"),r.addLocal("i","i32"),r.addLocal("pShiftToM","i32"),r.addLocal("pSConst","i32");const s=r.getCodeBuilder(),c=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));r.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(y),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.setLocal("pSConst",s.i32_add(s.i32_const(C),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(i+"_copy",s.getLocal("first"),c),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(o,s.getLocal("idx1"),s.getLocal("pShiftToM"),d),s.call(a+"_sub",s.getLocal("idx2"),d,d),s.call(a+"_sub",s.getLocal("idx1"),s.getLocal("idx2"),s.getLocal("idx2")),s.call(o,d,s.getLocal("pSConst"),s.getLocal("idx1")),s.call(o,s.getLocal("idx2"),c,s.getLocal("idx2")),s.call(i+"_mul",c,s.getLocal("inc"),c),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),t.exportFunction(e+"_fft"),t.exportFunction(e+"_ifft"),t.exportFunction(e+"_rawfft"),t.exportFunction(e+"_fftJoin"),t.exportFunction(e+"_fftJoinExt"),t.exportFunction(e+"_fftJoinExtInv"),t.exportFunction(e+"_fftMix"),t.exportFunction(e+"_fftFinal"),t.exportFunction(e+"_prepareLagrangeEvaluation")},$t=function(t,e,a){const i=8*t.modules[a].n64;return function(){const o=t.addFunction(e+"_zero");o.addParam("px","i32"),o.addParam("n","i32"),o.addLocal("lastp","i32"),o.addLocal("p","i32");const n=o.getCodeBuilder();o.addCode(n.setLocal("p",n.getLocal("px")),n.setLocal("lastp",n.i32_add(n.getLocal("px"),n.i32_mul(n.getLocal("n"),n.i32_const(i)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("p"),n.getLocal("lastp"))),n.call(a+"_zero",n.getLocal("p")),n.setLocal("p",n.i32_add(n.getLocal("p"),n.i32_const(i))),n.br(0))))}(),function(){const o=t.addFunction(e+"_constructLC");o.addParam("ppolynomials","i32"),o.addParam("psignals","i32"),o.addParam("nSignals","i32"),o.addParam("pres","i32"),o.addLocal("i","i32"),o.addLocal("j","i32"),o.addLocal("pp","i32"),o.addLocal("ps","i32"),o.addLocal("pd","i32"),o.addLocal("ncoefs","i32");const n=o.getCodeBuilder(),l=n.i32_const(t.alloc(i));o.addCode(n.setLocal("i",n.i32_const(0)),n.setLocal("pp",n.getLocal("ppolynomials")),n.setLocal("ps",n.getLocal("psignals")),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("i"),n.getLocal("nSignals"))),n.setLocal("ncoefs",n.i32_load(n.getLocal("pp"))),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(4))),n.setLocal("j",n.i32_const(0)),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("j"),n.getLocal("ncoefs"))),n.setLocal("pd",n.i32_add(n.getLocal("pres"),n.i32_mul(n.i32_load(n.getLocal("pp")),n.i32_const(i)))),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(4))),n.call(a+"_mul",n.getLocal("ps"),n.getLocal("pp"),l),n.call(a+"_add",l,n.getLocal("pd"),n.getLocal("pd")),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(i))),n.setLocal("j",n.i32_add(n.getLocal("j"),n.i32_const(1))),n.br(0))),n.setLocal("ps",n.i32_add(n.getLocal("ps"),n.i32_const(i))),n.setLocal("i",n.i32_add(n.getLocal("i"),n.i32_const(1))),n.br(0))))}(),t.exportFunction(e+"_zero"),t.exportFunction(e+"_constructLC"),e},Vt=function(t,e,a){const i=8*t.modules[a].n64;return function(){const o=t.addFunction(e+"_buildABC");o.addParam("pCoefs","i32"),o.addParam("nCoefs","i32"),o.addParam("pWitness","i32"),o.addParam("pA","i32"),o.addParam("pB","i32"),o.addParam("pC","i32"),o.addParam("offsetOut","i32"),o.addParam("nOut","i32"),o.addParam("offsetWitness","i32"),o.addParam("nWitness","i32"),o.addLocal("it","i32"),o.addLocal("ita","i32"),o.addLocal("itb","i32"),o.addLocal("last","i32"),o.addLocal("m","i32"),o.addLocal("c","i32"),o.addLocal("s","i32"),o.addLocal("pOut","i32");const n=o.getCodeBuilder(),l=n.i32_const(t.alloc(i));o.addCode(n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("nOut"),n.i32_const(i)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(a+"_zero",n.getLocal("ita")),n.call(a+"_zero",n.getLocal("itb")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(i))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(i))),n.br(0))),n.setLocal("it",n.getLocal("pCoefs")),n.setLocal("last",n.i32_add(n.getLocal("pCoefs"),n.i32_mul(n.getLocal("nCoefs"),n.i32_const(i+12)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("it"),n.getLocal("last"))),n.setLocal("s",n.i32_load(n.getLocal("it"),8)),n.if(n.i32_or(n.i32_lt_u(n.getLocal("s"),n.getLocal("offsetWitness")),n.i32_ge_u(n.getLocal("s"),n.i32_add(n.getLocal("offsetWitness"),n.getLocal("nWitness")))),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(i+12))),...n.br(1)]),n.setLocal("m",n.i32_load(n.getLocal("it"))),n.if(n.i32_eq(n.getLocal("m"),n.i32_const(0)),n.setLocal("pOut",n.getLocal("pA")),n.if(n.i32_eq(n.getLocal("m"),n.i32_const(1)),n.setLocal("pOut",n.getLocal("pB")),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(i+12))),...n.br(1)])),n.setLocal("c",n.i32_load(n.getLocal("it"),4)),n.if(n.i32_or(n.i32_lt_u(n.getLocal("c"),n.getLocal("offsetOut")),n.i32_ge_u(n.getLocal("c"),n.i32_add(n.getLocal("offsetOut"),n.getLocal("nOut")))),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(i+12))),...n.br(1)]),n.setLocal("pOut",n.i32_add(n.getLocal("pOut"),n.i32_mul(n.i32_sub(n.getLocal("c"),n.getLocal("offsetOut")),n.i32_const(i)))),n.call(a+"_mul",n.i32_add(n.getLocal("pWitness"),n.i32_mul(n.i32_sub(n.getLocal("s"),n.getLocal("offsetWitness")),n.i32_const(i))),n.i32_add(n.getLocal("it"),n.i32_const(12)),l),n.call(a+"_add",n.getLocal("pOut"),l,n.getLocal("pOut")),n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(i+12))),n.br(0))),n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("it",n.getLocal("pC")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("nOut"),n.i32_const(i)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(a+"_mul",n.getLocal("ita"),n.getLocal("itb"),n.getLocal("it")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(i))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(i))),n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(i))),n.br(0))))}(),function(){const o=t.addFunction(e+"_joinABC");o.addParam("pA","i32"),o.addParam("pB","i32"),o.addParam("pC","i32"),o.addParam("n","i32"),o.addParam("pP","i32"),o.addLocal("ita","i32"),o.addLocal("itb","i32"),o.addLocal("itc","i32"),o.addLocal("itp","i32"),o.addLocal("last","i32");const n=o.getCodeBuilder(),l=n.i32_const(t.alloc(i));o.addCode(n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("itc",n.getLocal("pC")),n.setLocal("itp",n.getLocal("pP")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("n"),n.i32_const(i)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(a+"_mul",n.getLocal("ita"),n.getLocal("itb"),l),n.call(a+"_sub",l,n.getLocal("itc"),n.getLocal("itp")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(i))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(i))),n.setLocal("itc",n.i32_add(n.getLocal("itc"),n.i32_const(i))),n.setLocal("itp",n.i32_add(n.getLocal("itp"),n.i32_const(i))),n.br(0))))}(),function(){const o=t.addFunction(e+"_batchAdd");o.addParam("pa","i32"),o.addParam("pb","i32"),o.addParam("n","i32"),o.addParam("pr","i32"),o.addLocal("ita","i32"),o.addLocal("itb","i32"),o.addLocal("itr","i32"),o.addLocal("last","i32");const n=o.getCodeBuilder();o.addCode(n.setLocal("ita",n.getLocal("pa")),n.setLocal("itb",n.getLocal("pb")),n.setLocal("itr",n.getLocal("pr")),n.setLocal("last",n.i32_add(n.getLocal("pa"),n.i32_mul(n.getLocal("n"),n.i32_const(i)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(a+"_add",n.getLocal("ita"),n.getLocal("itb"),n.getLocal("itr")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(i))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(i))),n.setLocal("itr",n.i32_add(n.getLocal("itr"),n.i32_const(i))),n.br(0))))}(),t.exportFunction(e+"_buildABC"),t.exportFunction(e+"_joinABC"),t.exportFunction(e+"_batchAdd"),e},Kt=function(t,e,a,i,o,n,l,r){const s=t.addFunction(e);s.addParam("pIn","i32"),s.addParam("n","i32"),s.addParam("pFirst","i32"),s.addParam("pInc","i32"),s.addParam("pOut","i32"),s.addLocal("pOldFree","i32"),s.addLocal("i","i32"),s.addLocal("pFrom","i32"),s.addLocal("pTo","i32");const c=s.getCodeBuilder(),d=c.i32_const(t.alloc(l));s.addCode(c.setLocal("pFrom",c.getLocal("pIn")),c.setLocal("pTo",c.getLocal("pOut"))),s.addCode(c.call(i+"_copy",c.getLocal("pFirst"),d)),s.addCode(c.setLocal("i",c.i32_const(0)),c.block(c.loop(c.br_if(1,c.i32_eq(c.getLocal("i"),c.getLocal("n"))),c.call(r,c.getLocal("pFrom"),d,c.getLocal("pTo")),c.setLocal("pFrom",c.i32_add(c.getLocal("pFrom"),c.i32_const(o))),c.setLocal("pTo",c.i32_add(c.getLocal("pTo"),c.i32_const(n))),c.call(i+"_mul",d,c.getLocal("pInc"),d),c.setLocal("i",c.i32_add(c.getLocal("i"),c.i32_const(1))),c.br(0)))),t.exportFunction(e)};const jt=j,Ht=It,Zt=Et,Wt=Pt,Yt=Tt,Jt=qt,Xt=Nt,te=$t,ee=Vt,ae=Kt,{bitLength:ie,modInv:oe,isOdd:ne,isNegative:le}=J;const re=j,se=It,ce=Et,de=Pt,ue=Tt,ge=qt,fe=Nt,he=$t,_e=Vt,pe=Kt,{bitLength:me,isOdd:we,isNegative:Le}=J;var be=function(t,e){const a=e||"bn128";if(t.modules[a])return a;const i=21888242871839275222246405745257275088696311157297823662689037894645226208583n,o=21888242871839275222246405745257275088548364400416034343698204186575808495617n,n=Math.floor((ie(i-1n)-1)/64)+1,l=8*n,r=l,s=l,c=2*s,d=12*s,u=t.alloc(jt.bigInt2BytesLE(o,r)),g=Ht(t,i,"f1m");Zt(t,o,"fr","frm");const f=t.alloc(jt.bigInt2BytesLE(L(3n),s)),h=Jt(t,"g1m","f1m",f);Xt(t,"frm","frm","frm","frm_mul"),te(t,"pol","frm"),ee(t,"qap","frm");const _=Wt(t,"f1m_neg","f2m","f1m"),p=t.alloc([...jt.bigInt2BytesLE(L(19485874751759354771024239261021720505790618469301721065564631296452457478373n),s),...jt.bigInt2BytesLE(L(266929791119991161246907387137283842545076965332900288569378510910307636690n),s)]),m=Jt(t,"g2m","f2m",p);function w(e,a){const i=t.addFunction(e);i.addParam("pG","i32"),i.addParam("pFr","i32"),i.addParam("pr","i32");const o=i.getCodeBuilder(),n=o.i32_const(t.alloc(l));i.addCode(o.call("frm_fromMontgomery",o.getLocal("pFr"),n),o.call(a,o.getLocal("pG"),n,o.i32_const(l),o.getLocal("pr"))),t.exportFunction(e)}function L(t){return BigInt(t)*(1n<0n;)ne(e)?a.push(1):a.push(0),e>>=1n;return a}(29793968203157093288n),U=t.alloc(z),Q=3*c,q=z.length-1,M=z.reduce(((t,e)=>t+(0!=e?1:0)),0),k=6*l,R=3*l*2+(M+q+1)*Q;t.modules[a]={n64:n,pG1gen:A,pG1zero:C,pG1b:f,pG2gen:F,pG2zero:E,pG2b:p,pq:t.modules.f1m.pq,pr:u,pOneT:v,prePSize:k,preQSize:R,r:o.toString(),q:i.toString()};const D=4965661367192848881n;function N(e){const o=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[8376118865763821496583973867626364092589906065868298776909617916018768340080n,16469823323077808223889137241176536799009286646108169935659301613961712198316n],[21888242871839275220042445260109153167277707414472061641714758635765020556617n,0n],[11697423496358154304825782922584725312912383441159505038794027105778954184319n,303847389135065887422783454877609941456349188919719272345083954437860409601n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[3321304630594332808241809054958361220322477375291206261884409189760185844239n,5722266937896532885780051958958348231143373700109372999374820235121374419868n],[21888242871839275222246405745257275088696311157297823662689037894645226208582n,0n],[13512124006075453725662431877630910996106405091429524885779419978626457868503n,5418419548761466998357268504080738289687024511189653727029736280683514010267n],[2203960485148121921418603742825762020974279258880205651966n,0n],[10190819375481120917420622822672549775783927716138318623895010788866272024264n,21584395482704209334823622290379665147239961968378104390343953940207365798982n],[2203960485148121921418603742825762020974279258880205651967n,0n],[18566938241244942414004596690298913868373833782006617400804628704885040364344n,16165975933942742336466353786298926857552937457188450663314217659523851788715n]]],n=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[21575463638280843010398324269430826099269044274347216827212613867836435027261n,10307601595873709700152284273816112264069230130616436755625194854815875713954n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[3772000881919853776433695186713858239009073593817195771773381919316419345261n,2236595495967245188281701248203181795121068902605861227855261137820944008926n],[2203960485148121921418603742825762020974279258880205651966n,0n],[18429021223477853657660792034369865839114504446431234726392080002137598044644n,9344045779998320333812420223237981029506012124075525679208581902008406485703n]],[[1n,0n],[2581911344467009335267311115468803099551665605076196740867805258568234346338n,19937756971775647987995932169929341994314640652964949448313374472400716661030n],[2203960485148121921418603742825762020974279258880205651966n,0n],[5324479202449903542726783395506214481928257762400643279780343368557297135718n,16208900380737693084919495127334387981393726419856888799917914180988844123039n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[13981852324922362344252311234282257507216387789820983642040889267519694726527n,7629828391165209371577384193250820201684255241773809077146787135900891633097n]]],l=t.addFunction(a+"__frobeniusMap"+e);l.addParam("x","i32"),l.addParam("r","i32");const r=l.getCodeBuilder();for(let a=0;a<6;a++){const i=0==a?r.getLocal("x"):r.i32_add(r.getLocal("x"),r.i32_const(a*c)),u=i,f=r.i32_add(r.getLocal("x"),r.i32_const(a*c+s)),h=0==a?r.getLocal("r"):r.i32_add(r.getLocal("r"),r.i32_const(a*c)),p=h,m=r.i32_add(r.getLocal("r"),r.i32_const(a*c+s)),w=d(o[Math.floor(a/3)][e%12],n[a%3][e%6]),b=t.alloc([...jt.bigInt2BytesLE(L(w[0]),32),...jt.bigInt2BytesLE(L(w[1]),32)]);e%2==1?l.addCode(r.call(g+"_copy",u,p),r.call(g+"_neg",f,m),r.call(_+"_mul",h,r.i32_const(b),h)):l.addCode(r.call(_+"_mul",i,r.i32_const(b),h))}function d(t,e){const a=BigInt(t[0]),o=BigInt(t[1]),n=BigInt(e[0]),l=BigInt(e[1]),r=[(a*n-o*l)%i,(a*l+o*n)%i];return le(r[0])&&(r[0]=r[0]+i),r}}function $(e,i){const o=function(t){let e=t;const a=[];for(;e>0n;){if(ne(e)){const t=2-Number(e%4n);a.push(t),e-=BigInt(t)}else a.push(0);e>>=1n}return a}(e).map((t=>-1==t?255:t)),n=t.alloc(o),l=t.addFunction(a+"__cyclotomicExp_"+i);l.addParam("x","i32"),l.addParam("r","i32"),l.addLocal("bit","i32"),l.addLocal("i","i32");const r=l.getCodeBuilder(),s=r.getLocal("x"),c=r.getLocal("r"),u=r.i32_const(t.alloc(d));l.addCode(r.call(T+"_conjugate",s,u),r.call(T+"_one",c),r.if(r.teeLocal("bit",r.i32_load8_s(r.i32_const(o.length-1),n)),r.if(r.i32_eq(r.getLocal("bit"),r.i32_const(1)),r.call(T+"_mul",c,s,c),r.call(T+"_mul",c,u,c))),r.setLocal("i",r.i32_const(o.length-2)),r.block(r.loop(r.call(a+"__cyclotomicSquare",c,c),r.if(r.teeLocal("bit",r.i32_load8_s(r.getLocal("i"),n)),r.if(r.i32_eq(r.getLocal("bit"),r.i32_const(1)),r.call(T+"_mul",c,s,c),r.call(T+"_mul",c,u,c))),r.br_if(1,r.i32_eqz(r.getLocal("i"))),r.setLocal("i",r.i32_sub(r.getLocal("i"),r.i32_const(1))),r.br(0))))}function V(){!function(){const e=t.addFunction(a+"__cyclotomicSquare");e.addParam("x","i32"),e.addParam("r","i32");const i=e.getCodeBuilder(),o=i.getLocal("x"),n=i.i32_add(i.getLocal("x"),i.i32_const(c)),l=i.i32_add(i.getLocal("x"),i.i32_const(2*c)),r=i.i32_add(i.getLocal("x"),i.i32_const(3*c)),s=i.i32_add(i.getLocal("x"),i.i32_const(4*c)),d=i.i32_add(i.getLocal("x"),i.i32_const(5*c)),u=i.getLocal("r"),g=i.i32_add(i.getLocal("r"),i.i32_const(c)),f=i.i32_add(i.getLocal("r"),i.i32_const(2*c)),h=i.i32_add(i.getLocal("r"),i.i32_const(3*c)),p=i.i32_add(i.getLocal("r"),i.i32_const(4*c)),m=i.i32_add(i.getLocal("r"),i.i32_const(5*c)),w=i.i32_const(t.alloc(c)),L=i.i32_const(t.alloc(c)),b=i.i32_const(t.alloc(c)),A=i.i32_const(t.alloc(c)),y=i.i32_const(t.alloc(c)),C=i.i32_const(t.alloc(c)),I=i.i32_const(t.alloc(c)),F=i.i32_const(t.alloc(c));e.addCode(i.call(_+"_mul",o,s,I),i.call(_+"_mul",s,i.i32_const(B),w),i.call(_+"_add",o,w,w),i.call(_+"_add",o,s,F),i.call(_+"_mul",F,w,w),i.call(_+"_mul",i.i32_const(B),I,F),i.call(_+"_add",I,F,F),i.call(_+"_sub",w,F,w),i.call(_+"_add",I,I,L),i.call(_+"_mul",r,l,I),i.call(_+"_mul",l,i.i32_const(B),b),i.call(_+"_add",r,b,b),i.call(_+"_add",r,l,F),i.call(_+"_mul",F,b,b),i.call(_+"_mul",i.i32_const(B),I,F),i.call(_+"_add",I,F,F),i.call(_+"_sub",b,F,b),i.call(_+"_add",I,I,A),i.call(_+"_mul",n,d,I),i.call(_+"_mul",d,i.i32_const(B),y),i.call(_+"_add",n,y,y),i.call(_+"_add",n,d,F),i.call(_+"_mul",F,y,y),i.call(_+"_mul",i.i32_const(B),I,F),i.call(_+"_add",I,F,F),i.call(_+"_sub",y,F,y),i.call(_+"_add",I,I,C),i.call(_+"_sub",w,o,u),i.call(_+"_add",u,u,u),i.call(_+"_add",w,u,u),i.call(_+"_add",L,s,p),i.call(_+"_add",p,p,p),i.call(_+"_add",L,p,p),i.call(_+"_mul",C,i.i32_const(P),F),i.call(_+"_add",F,r,h),i.call(_+"_add",h,h,h),i.call(_+"_add",F,h,h),i.call(_+"_sub",y,l,f),i.call(_+"_add",f,f,f),i.call(_+"_add",y,f,f),i.call(_+"_sub",b,n,g),i.call(_+"_add",g,g,g),i.call(_+"_add",b,g,g),i.call(_+"_add",A,d,m),i.call(_+"_add",m,m,m),i.call(_+"_add",A,m,m))}(),$(D,"w0");const e=t.addFunction(a+"__finalExponentiationLastChunk");e.addParam("x","i32"),e.addParam("r","i32");const i=e.getCodeBuilder(),o=i.getLocal("x"),n=i.getLocal("r"),l=i.i32_const(t.alloc(d)),r=i.i32_const(t.alloc(d)),s=i.i32_const(t.alloc(d)),u=i.i32_const(t.alloc(d)),g=i.i32_const(t.alloc(d)),f=i.i32_const(t.alloc(d)),h=i.i32_const(t.alloc(d)),p=i.i32_const(t.alloc(d)),m=i.i32_const(t.alloc(d)),w=i.i32_const(t.alloc(d)),L=i.i32_const(t.alloc(d)),b=i.i32_const(t.alloc(d)),A=i.i32_const(t.alloc(d)),y=i.i32_const(t.alloc(d)),C=i.i32_const(t.alloc(d)),I=i.i32_const(t.alloc(d)),F=i.i32_const(t.alloc(d)),x=i.i32_const(t.alloc(d)),E=i.i32_const(t.alloc(d)),v=i.i32_const(t.alloc(d)),S=i.i32_const(t.alloc(d));e.addCode(i.call(a+"__cyclotomicExp_w0",o,l),i.call(T+"_conjugate",l,l),i.call(a+"__cyclotomicSquare",l,r),i.call(a+"__cyclotomicSquare",r,s),i.call(T+"_mul",s,r,u),i.call(a+"__cyclotomicExp_w0",u,g),i.call(T+"_conjugate",g,g),i.call(a+"__cyclotomicSquare",g,f),i.call(a+"__cyclotomicExp_w0",f,h),i.call(T+"_conjugate",h,h),i.call(T+"_conjugate",u,p),i.call(T+"_conjugate",h,m),i.call(T+"_mul",m,g,w),i.call(T+"_mul",w,p,L),i.call(T+"_mul",L,r,b),i.call(T+"_mul",L,g,A),i.call(T+"_mul",A,o,y),i.call(a+"__frobeniusMap1",b,C),i.call(T+"_mul",C,y,I),i.call(a+"__frobeniusMap2",L,F),i.call(T+"_mul",F,I,x),i.call(T+"_conjugate",o,E),i.call(T+"_mul",E,b,v),i.call(a+"__frobeniusMap3",v,S),i.call(T+"_mul",S,x,n))}const K=t.alloc(k),j=t.alloc(R);function H(e){const i=t.addFunction(a+"_pairingEq"+e);for(let t=0;t0n;)we(e)?a.push(1):a.push(0),e>>=1n;return a}(0xd201000000010000n),z=t.alloc(T),U=3*s,Q=T.length-1,q=T.reduce(((t,e)=>t+(0!=e?1:0)),0),M=6*l,k=3*l*2+(q+Q+1)*U,R=!0,D=15132376222941642752n;function N(e){const a=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n,151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n,0n],[2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n,1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n,877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n],[4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n,0n],[151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n,3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n,2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n,0n],[877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n,3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n]]],o=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[0n,4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[0n,1n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[0n,793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n]],[[1n,0n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n,0n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n,0n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n,0n]]],n=t.addFunction(O+"_frobeniusMap"+e);n.addParam("x","i32"),n.addParam("r","i32");const c=n.getCodeBuilder();for(let i=0;i<6;i++){const u=0==i?c.getLocal("x"):c.i32_add(c.getLocal("x"),c.i32_const(i*s)),g=u,f=c.i32_add(c.getLocal("x"),c.i32_const(i*s+r)),_=0==i?c.getLocal("r"):c.i32_add(c.getLocal("r"),c.i32_const(i*s)),p=_,w=c.i32_add(c.getLocal("r"),c.i32_const(i*s+r)),L=d(a[Math.floor(i/3)][e%12],o[i%3][e%6]),b=t.alloc([...re.bigInt2BytesLE(A(L[0]),l),...re.bigInt2BytesLE(A(L[1]),l)]);e%2==1?n.addCode(c.call(h+"_copy",g,p),c.call(h+"_neg",f,w),c.call(m+"_mul",_,c.i32_const(b),_)):n.addCode(c.call(m+"_mul",u,c.i32_const(b),_))}function d(t,e){const a=t[0],o=t[1],n=e[0],l=e[1],r=[(a*n-o*l)%i,(a*l+o*n)%i];return Le(r[0])&&(r[0]=r[0]+i),r}}function $(e,i,o){const n=function(t){let e=t;const a=[];for(;e>0n;){if(we(e)){const t=2-Number(e%4n);a.push(t),e-=BigInt(t)}else a.push(0);e>>=1n}return a}(e).map((t=>-1==t?255:t)),l=t.alloc(n),r=t.addFunction(a+"__cyclotomicExp_"+o);r.addParam("x","i32"),r.addParam("r","i32"),r.addLocal("bit","i32"),r.addLocal("i","i32");const s=r.getCodeBuilder(),d=s.getLocal("x"),u=s.getLocal("r"),g=s.i32_const(t.alloc(c));r.addCode(s.call(O+"_conjugate",d,g),s.call(O+"_one",u),s.if(s.teeLocal("bit",s.i32_load8_s(s.i32_const(n.length-1),l)),s.if(s.i32_eq(s.getLocal("bit"),s.i32_const(1)),s.call(O+"_mul",u,d,u),s.call(O+"_mul",u,g,u))),s.setLocal("i",s.i32_const(n.length-2)),s.block(s.loop(s.call(a+"__cyclotomicSquare",u,u),s.if(s.teeLocal("bit",s.i32_load8_s(s.getLocal("i"),l)),s.if(s.i32_eq(s.getLocal("bit"),s.i32_const(1)),s.call(O+"_mul",u,d,u),s.call(O+"_mul",u,g,u))),s.br_if(1,s.i32_eqz(s.getLocal("i"))),s.setLocal("i",s.i32_sub(s.getLocal("i"),s.i32_const(1))),s.br(0)))),i&&r.addCode(s.call(O+"_conjugate",u,u))}t.modules[a]={n64q:n,n64r:d,n8q:l,n8r:u,pG1gen:C,pG1zero:F,pG1b:_,pG2gen:E,pG2zero:B,pG2b:w,pq:t.modules.f1m.pq,pr:f,pOneT:S,r:o,q:i,prePSize:M,preQSize:k},function(){const e=t.addFunction(G+"_mul1");e.addParam("pA","i32"),e.addParam("pC1","i32"),e.addParam("pR","i32");const a=e.getCodeBuilder(),i=a.getLocal("pA"),o=a.i32_add(a.getLocal("pA"),a.i32_const(2*r)),n=a.i32_add(a.getLocal("pA"),a.i32_const(4*r)),l=a.getLocal("pC1"),s=a.getLocal("pR"),c=a.i32_add(a.getLocal("pR"),a.i32_const(2*r)),d=a.i32_add(a.getLocal("pR"),a.i32_const(4*r)),u=a.i32_const(t.alloc(2*r)),g=a.i32_const(t.alloc(2*r));e.addCode(a.call(m+"_add",i,o,u),a.call(m+"_add",o,n,g),a.call(m+"_mul",o,l,d),a.call(m+"_mul",g,l,s),a.call(m+"_sub",s,d,s),a.call(m+"_mulNR",s,s),a.call(m+"_mul",u,l,c),a.call(m+"_sub",c,d,c))}(),function(){const e=t.addFunction(G+"_mul01");e.addParam("pA","i32"),e.addParam("pC0","i32"),e.addParam("pC1","i32"),e.addParam("pR","i32");const a=e.getCodeBuilder(),i=a.getLocal("pA"),o=a.i32_add(a.getLocal("pA"),a.i32_const(2*r)),n=a.i32_add(a.getLocal("pA"),a.i32_const(4*r)),l=a.getLocal("pC0"),s=a.getLocal("pC1"),c=a.getLocal("pR"),d=a.i32_add(a.getLocal("pR"),a.i32_const(2*r)),u=a.i32_add(a.getLocal("pR"),a.i32_const(4*r)),g=a.i32_const(t.alloc(2*r)),f=a.i32_const(t.alloc(2*r)),h=a.i32_const(t.alloc(2*r)),_=a.i32_const(t.alloc(2*r));e.addCode(a.call(m+"_mul",i,l,g),a.call(m+"_mul",o,s,f),a.call(m+"_add",i,o,h),a.call(m+"_add",i,n,_),a.call(m+"_add",o,n,c),a.call(m+"_mul",c,s,c),a.call(m+"_sub",c,f,c),a.call(m+"_mulNR",c,c),a.call(m+"_add",c,g,c),a.call(m+"_add",l,s,d),a.call(m+"_mul",d,h,d),a.call(m+"_sub",d,g,d),a.call(m+"_sub",d,f,d),a.call(m+"_mul",_,l,u),a.call(m+"_sub",u,g,u),a.call(m+"_add",u,f,u))}(),function(){const e=t.addFunction(O+"_mul014");e.addParam("pA","i32"),e.addParam("pC0","i32"),e.addParam("pC1","i32"),e.addParam("pC4","i32"),e.addParam("pR","i32");const a=e.getCodeBuilder(),i=a.getLocal("pA"),o=a.i32_add(a.getLocal("pA"),a.i32_const(6*r)),n=a.getLocal("pC0"),l=a.getLocal("pC1"),s=a.getLocal("pC4"),c=a.i32_const(t.alloc(6*r)),d=a.i32_const(t.alloc(6*r)),u=a.i32_const(t.alloc(2*r)),g=a.getLocal("pR"),f=a.i32_add(a.getLocal("pR"),a.i32_const(6*r));e.addCode(a.call(G+"_mul01",i,n,l,c),a.call(G+"_mul1",o,s,d),a.call(m+"_add",l,s,u),a.call(G+"_add",o,i,f),a.call(G+"_mul01",f,n,u,f),a.call(G+"_sub",f,c,f),a.call(G+"_sub",f,d,f),a.call(G+"_copy",d,g),a.call(G+"_mulNR",g,g),a.call(G+"_add",g,c,g))}(),function(){const e=t.addFunction(a+"_ell");e.addParam("pP","i32"),e.addParam("pCoefs","i32"),e.addParam("pF","i32");const i=e.getCodeBuilder(),o=i.getLocal("pP"),n=i.i32_add(i.getLocal("pP"),i.i32_const(l)),s=i.getLocal("pF"),c=i.getLocal("pCoefs"),d=i.i32_add(i.getLocal("pCoefs"),i.i32_const(r)),u=i.i32_add(i.getLocal("pCoefs"),i.i32_const(2*r)),g=i.i32_add(i.getLocal("pCoefs"),i.i32_const(3*r)),f=i.i32_add(i.getLocal("pCoefs"),i.i32_const(4*r)),_=t.alloc(2*r),p=i.i32_const(_),m=i.i32_const(_),w=i.i32_const(_+r),L=t.alloc(2*r),b=i.i32_const(L),A=i.i32_const(L),y=i.i32_const(L+r);e.addCode(i.call(h+"_mul",c,n,m),i.call(h+"_mul",d,n,w),i.call(h+"_mul",u,o,A),i.call(h+"_mul",g,o,y),i.call(O+"_mul014",s,f,b,p,s))}();const V=t.alloc(M),K=t.alloc(k);function j(e){const i=t.addFunction(a+"_pairingEq"+e);for(let t=0;t>=BigInt(32)):l+2<=e?(n.setUint16(l,Number(a&BigInt(65535)),!0),l+=2,a>>=BigInt(16)):(n.setUint8(l,Number(a&BigInt(255)),!0),l+=1,a>>=BigInt(8));if(a)throw new Error("Number does not fit in this length");return i}const Ce=[];for(let t=0;t<256;t++)Ce[t]=Ie(t,8);function Ie(t,e){let a=0,i=t;for(let t=0;t>=1;return a}function Fe(t,e){return(Ce[t>>>24]|Ce[t>>>16&255]<<8|Ce[t>>>8&255]<<16|Ce[255&t]<<24)>>>32-e}function xe(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 Ee(t,e){const a=t.byteLength/e,i=xe(a);if(a!=1<a){const i=t.slice(o*e,(o+1)*e);t.set(t.slice(a*e,(a+1)*e),o*e),t.set(i,a*e)}}}function ve(t,e){const a=new Uint8Array(e*t.length);for(let i=0;i{a[i]=t(e[i])})),a}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 a={};return Object.keys(e).forEach((i=>{a[i]=t(e[i])})),a}return e},beBuff2int:function(t){let e=BigInt(0),a=t.length,i=0;const o=new DataView(t.buffer,t.byteOffset,t.byteLength);for(;a>0;)a>=4?(a-=4,e+=BigInt(o.getUint32(a))<=2?(a-=2,e+=BigInt(o.getUint16(a))<0;)n-4>=0?(n-=4,o.setUint32(n,Number(a&BigInt(4294967295))),a>>=BigInt(32)):n-2>=0?(n-=2,o.setUint16(n,Number(a&BigInt(65535))),a>>=BigInt(16)):(n-=1,o.setUint8(n,Number(a&BigInt(255))),a>>=BigInt(8));if(a)throw new Error("Number does not fit in this length");return i},leBuff2int:function(t){let e=BigInt(0),a=0;const i=new DataView(t.buffer,t.byteOffset,t.byteLength);for(;a{i[o]=t(e,a[o])})),i}return a},unstringifyFElements:function t(e,a){if("string"==typeof a&&/^[0-9]+$/.test(a))return e.e(a);if("string"==typeof a&&/^0x[0-9a-fA-F]+$/.test(a))return e.e(a);if(Array.isArray(a))return a.map(t.bind(this,e));if("object"==typeof a){if(null===a)return null;const i={};return Object.keys(a).forEach((o=>{i[o]=t(e,a[o])})),i}return a},bitReverse:Fe,log2:xe,buffReverseBits:Ee,array2buffer:ve,buffer2array:Be});const Pe=1<<30;class Ge{constructor(t){this.buffers=[],this.byteLength=t;for(let e=0;e0;){const t=l+r>Pe?Pe-l:r,e=new Uint8Array(this.buffers[n].buffer,this.buffers[n].byteOffset+l,t);if(t==a)return e.slice();o||(o=a<=Pe?new Uint8Array(a):new Ge(a)),o.set(e,a-r),r-=t,n++,l=0}return o}set(t,e){void 0===e&&(e=0);const a=t.byteLength;if(0==a)return;const i=Math.floor(e/Pe);if(i==Math.floor((e+a-1)/Pe))return t instanceof Ge&&1==t.buffers.length?this.buffers[i].set(t.buffers[0],e%Pe):this.buffers[i].set(t,e%Pe);let o=i,n=e%Pe,l=a;for(;l>0;){const e=n+l>Pe?Pe-n:l,i=t.slice(a-l,a-l+e);new Uint8Array(this.buffers[o].buffer,this.buffers[o].byteOffset+n,e).set(i),l-=e,o++,n=0}}}function Oe(t,e,a,i){return async function(o){const n=Math.floor(o.byteLength/a);if(n*a!==o.byteLength)throw new Error("Invalid buffer size");const l=Math.floor(n/t.concurrency),r=[];for(let s=0;s=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=Oe(t,e+"_batchToMontgomery",this.n8,this.n8),this.batchFromMontgomery=Oe(t,e+"_batchFromMontgomery",this.n8,this.n8)}op2(t,e,a){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,a),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op2Bool(t,e,a){return this.tm.setBuff(this.pOp1,e),this.tm.setBuff(this.pOp2,a),!!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=S(i(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 a=i(t,e);n(a)?(a=p(a),y(a,this.p)&&(a=b(a,this.p)),a=_(this.p,a)):y(a,this.p)&&(a=b(a,this.p));const o=ye(a,this.n8);return this.toMontgomery(o)}toString(t,e){return B(E(this.fromMontgomery(t),0),e)}fromRng(t){let e;const a=new Uint8Array(this.n8);do{e=P;for(let a=0;ai.buffer.byteLength){const o=i.buffer.byteLength/65536;let n=Math.floor((a[0]+t)/65536)+1;n>e&&(n=e),i.grow(n-o)}return o}function l(t){const e=n(t.byteLength);return s(e,t),e}function r(t,e){const a=new Uint8Array(i.buffer);return new Uint8Array(a.buffer,a.byteOffset+t,e)}function s(t,e){new Uint8Array(i.buffer).set(new Uint8Array(e),t)}function c(t){if("INIT"==t[0].cmd)return o(t[0]);const e={vars:[],out:[]},c=new Uint32Array(i.buffer,0,1)[0];for(let i=0;i{this.reject=e,this.resolve=t}))}}var Ne;const $e="data:application/javascript;base64,"+(Ne="("+qe.toString()+")(self)",globalThis.btoa(Ne));class Ve{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,a,i){if(this.working[t])throw new Error("Posting a job t a working worker");return this.working[t]=!0,this.pendingDeferreds[t]=i||new De,this.workers[t].postMessage(e,a),this.pendingDeferreds[t].promise}processWorks(){for(let t=0;t0;t++)if(0==this.working[t]){const e=this.actionQueue.shift();this.postAction(t,e.data,e.transfers,e.deferred)}}queueAction(t,e){const a=new De;if(this.singleThread){const e=this.taskManager(t);a.resolve(e)}else this.actionQueue.push({data:t,transfers:e,deferred:a}),this.processWorks();return a.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}async terminate(){for(let t=0;tsetTimeout(e,t))))}}function Ke(t,e){const a=t[e],i=t.Fr,o=t.tm;t[e].batchApplyKey=async function(t,n,l,r,s){let c,d,u,g,f;if(r=r||"affine",s=s||"affine","G1"==e)"jacobian"==r?(u=3*a.F.n8,c="g1m_batchApplyKey"):(u=2*a.F.n8,c="g1m_batchApplyKeyMixed"),g=3*a.F.n8,"jacobian"==s?f=3*a.F.n8:(d="g1m_batchToAffine",f=2*a.F.n8);else if("G2"==e)"jacobian"==r?(u=3*a.F.n8,c="g2m_batchApplyKey"):(u=2*a.F.n8,c="g2m_batchApplyKeyMixed"),g=3*a.F.n8,"jacobian"==s?f=3*a.F.n8:(d="g2m_batchToAffine",f=2*a.F.n8);else{if("Fr"!=e)throw new Error("Invalid group: "+e);c="frm_batchApplyKey",u=a.n8,g=a.n8,f=a.n8}const h=Math.floor(t.byteLength/u),_=Math.floor(h/o.concurrency),p=[];l=i.e(l);let m=i.e(n);for(let e=0;e=0;t--){if(!a.isZero(_))for(let t=0;tc&&(_=c),_<1024&&(_=1024);const p=[];for(let e=0;e(r&&r.debug(`Multiexp end: ${s}: ${e}/${u}`),t))))}const m=await Promise.all(p);let w=a.zero;for(let t=m.length-1;t>=0;t--)w=a.add(w,m[t]);return w}a.multiExp=async function(t,e,a,i){return await n(t,e,"jacobian",a,i)},a.multiExpAffine=async function(t,e,a,i){return await n(t,e,"affine",a,i)}}function Ze(t,e){const a=t[e],i=t.Fr,o=a.tm;async function n(t,r,s,c,d,u){s=s||"affine",c=c||"affine";let g,f,h,_,p,m,w,L;"G1"==e?("affine"==s?(g=2*a.F.n8,_="g1m_batchToJacobian"):g=3*a.F.n8,f=3*a.F.n8,r&&(L="g1m_fftFinal"),w="g1m_fftJoin",m="g1m_fftMix","affine"==c?(h=2*a.F.n8,p="g1m_batchToAffine"):h=3*a.F.n8):"G2"==e?("affine"==s?(g=2*a.F.n8,_="g2m_batchToJacobian"):g=3*a.F.n8,f=3*a.F.n8,r&&(L="g2m_fftFinal"),w="g2m_fftJoin",m="g2m_fftMix","affine"==c?(h=2*a.F.n8,p="g2m_batchToAffine"):h=3*a.F.n8):"Fr"==e&&(g=a.n8,f=a.n8,h=a.n8,r&&(L="frm_fftFinal"),m="frm_fftMix",w="frm_fftJoin");let b=!1;Array.isArray(t)?(t=ve(t,g),b=!0):t=t.slice(0,t.byteLength);const A=t.byteLength/g,y=xe(A);if(1<1<<28?new Ge(2*u[0].byteLength):new Uint8Array(2*u[0].byteLength);return g.set(u[0]),g.set(u[1],u[0].byteLength),g}(t,s,c,d,u):await async function(t,e,a,o,r){let s,c;s=t.slice(0,t.byteLength/2),c=t.slice(t.byteLength/2,t.byteLength);const d=[];[s,c]=await l(s,c,"fftJoinExt",i.one,i.shift,e,"jacobian",o,r),d.push(n(s,!1,"jacobian",a,o,r)),d.push(n(c,!1,"jacobian",a,o,r));const u=await Promise.all(d);let g;g=u[0].byteLength>1<<28?new Ge(2*u[0].byteLength):new Uint8Array(2*u[0].byteLength);return g.set(u[0]),g.set(u[1],u[0].byteLength),g}(t,s,c,d,u),b?Be(e,h):e}let C,I,F;r&&(C=i.inv(i.e(A))),Ee(t,g);let x=Math.min(16384,A),E=A/x;for(;E=16;)E*=2,x/=2;const v=xe(x),B=[];for(let e=0;e(d&&d.debug(`${u}: fft ${y} mix end: ${e}/${E}`),t))))}F=await Promise.all(B);for(let t=0;t(d&&d.debug(`${u}: fft ${y} join ${t}/${y} ${l+1}/${e} ${r}/${a/2}`),i))))}const l=await Promise.all(n);for(let t=0;t0;e--)I.set(F[e],t),t+=x*h,delete F[e];I.set(F[0].slice(0,(x-1)*h),t),delete F[0]}else for(let t=0;t65536&&(b=65536);const A=[];for(let e=0;e(u&&u.debug(`${g}: fftJoinExt End: ${e}/${L}`),t))))}const y=await Promise.all(A);let C,I;L*p>1<<28?(C=new Ge(L*p),I=new Ge(L*p)):(C=new Uint8Array(L*p),I=new Uint8Array(L*p));let F=0;for(let t=0;ti.s+1)throw s&&s.error("lagrangeEvaluations input too big"),new Error("lagrangeEvaluations input too big");let f=t.slice(0,t.byteLength/2),h=t.slice(t.byteLength/2,t.byteLength);const _=i.exp(i.shift,u/2),p=i.inv(i.sub(i.one,_));[f,h]=await l(f,h,"prepareLagrangeEvaluation",p,i.shiftInv,o,"jacobian",s,c+" prep");const m=[];let w;return m.push(n(f,!0,"jacobian",r,s,c+" t0")),m.push(n(h,!0,"jacobian",r,s,c+" t1")),[f,h]=await Promise.all(m),w=f.byteLength>1<<28?new Ge(2*f.byteLength):new Uint8Array(2*f.byteLength),w.set(f),w.set(h,f.byteLength),w},a.fftMix=async function(t){const n=3*a.F.n8;let l,r;if("G1"==e)l="g1m_fftMix",r="g1m_fftJoin";else if("G2"==e)l="g2m_fftMix",r="g2m_fftJoin";else{if("Fr"!=e)throw new Error("Invalid group");l="frm_fftMix",r="frm_fftJoin"}const s=Math.floor(t.byteLength/n),c=xe(s);let d=1<=0;t--)f.set(g[t][0],h),h+=g[t][0].byteLength;return f}}async function We(t){const e=await async function(t,e){const a=new Ve;a.memory=new WebAssembly.Memory({initial:Re}),a.u8=new Uint8Array(a.memory.buffer),a.u32=new Uint32Array(a.memory.buffer);const i=await WebAssembly.compile(t.code);if(a.instance=await WebAssembly.instantiate(i,{env:{memory:a.memory}}),a.singleThread=e,a.initalPFree=a.u32[0],a.pq=t.pq,a.pr=t.pr,a.pG1gen=t.pG1gen,a.pG1zero=t.pG1zero,a.pG2gen=t.pG2gen,a.pG2zero=t.pG2zero,a.pOneT=t.pOneT,e)a.code=t.code,a.taskManager=qe(),await a.taskManager([{cmd:"INIT",init:Re,code:a.code.slice()}]),a.concurrency=1;else{let e;a.workers=[],a.pendingDeferreds=[],a.working=[],e="object"==typeof navigator&&navigator.hardwareConcurrency?navigator.hardwareConcurrency:Me.cpus().length,0==e&&(e=2),e>64&&(e=64),a.concurrency=e;for(let t=0;t>8n&0xFFn)),e.push(Number(a>>16n&0xFFn)),e.push(Number(a>>24n&0xFFn)),e}function ea(t){const e=function(t){for(var e=[],a=0;a>6,128|63&i):i<55296||i>=57344?e.push(224|i>>12,128|i>>6&63,128|63&i):(a++,i=65536+((1023&i)<<10|1023&t.charCodeAt(a)),e.push(240|i>>18,128|i>>12&63,128|i>>6&63,128|63&i))}return e}(t);return[...la(e.length),...e]}function aa(t){const e=[];let a=Ye(t);if(Je(a))throw new Error("Number cannot be negative");for(;!Xe(a);)e.push(Number(0x7Fn&a)),a>>=7n;0==e.length&&e.push(0);for(let t=0;t0xFFFFFFFFn)throw new Error("Number too big");if(e>0x7FFFFFFFn&&(e-=0x100000000n),e<-2147483648n)throw new Error("Number too small");return ia(e)}function na(t){let e=Ye(t);if(e>0xFFFFFFFFFFFFFFFFn)throw new Error("Number too big");if(e>0x7FFFFFFFFFFFFFFFn&&(e-=0x10000000000000000n),e<-9223372036854775808n)throw new Error("Number too small");return ia(e)}function la(t){let e=Ye(t);if(e>0xFFFFFFFFn)throw new Error("Number too big");return aa(e)}function ra(t){return Array.from(t,(function(t){return("0"+(255&t).toString(16)).slice(-2)})).join("")}class sa{constructor(t){this.func=t,this.functionName=t.functionName,this.module=t.module}setLocal(t,e){const a=this.func.localIdxByName[t];if(void 0===a)throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${t} `);return[...e,33,...la(a)]}teeLocal(t,e){const a=this.func.localIdxByName[t];if(void 0===a)throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${t} `);return[...e,34,...la(a)]}getLocal(t){const e=this.func.localIdxByName[t];if(void 0===e)throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${t} `);return[32,...la(e)]}i64_load8_s(t,e,a){return[...t,48,void 0===a?0:a,...la(e||0)]}i64_load8_u(t,e,a){return[...t,49,void 0===a?0:a,...la(e||0)]}i64_load16_s(t,e,a){return[...t,50,void 0===a?1:a,...la(e||0)]}i64_load16_u(t,e,a){return[...t,51,void 0===a?1:a,...la(e||0)]}i64_load32_s(t,e,a){return[...t,52,void 0===a?2:a,...la(e||0)]}i64_load32_u(t,e,a){return[...t,53,void 0===a?2:a,...la(e||0)]}i64_load(t,e,a){return[...t,41,void 0===a?3:a,...la(e||0)]}i64_store(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=3,l=e):Array.isArray(a)?(o=e,n=3,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,55,n,...la(o)]}i64_store32(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=2,l=e):Array.isArray(a)?(o=e,n=2,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,62,n,...la(o)]}i64_store16(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=1,l=e):Array.isArray(a)?(o=e,n=1,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,61,n,...la(o)]}i64_store8(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=0,l=e):Array.isArray(a)?(o=e,n=0,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,60,n,...la(o)]}i32_load8_s(t,e,a){return[...t,44,void 0===a?0:a,...la(e||0)]}i32_load8_u(t,e,a){return[...t,45,void 0===a?0:a,...la(e||0)]}i32_load16_s(t,e,a){return[...t,46,void 0===a?1:a,...la(e||0)]}i32_load16_u(t,e,a){return[...t,47,void 0===a?1:a,...la(e||0)]}i32_load(t,e,a){return[...t,40,void 0===a?2:a,...la(e||0)]}i32_store(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=2,l=e):Array.isArray(a)?(o=e,n=2,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,54,n,...la(o)]}i32_store16(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=1,l=e):Array.isArray(a)?(o=e,n=1,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,59,n,...la(o)]}i32_store8(t,e,a,i){let o,n,l;return Array.isArray(e)?(o=0,n=0,l=e):Array.isArray(a)?(o=e,n=0,l=a):Array.isArray(i)&&(o=e,n=a,l=i),[...t,...l,58,n,...la(o)]}call(t,...e){const a=this.module.functionIdxByName[t];if(void 0===a)throw new Error(`Function not defined: Function: ${t}`);return[...[].concat(...e),16,...la(a)]}call_indirect(t,...e){return[...[].concat(...e),...t,17,0,0]}if(t,e,a){return a?[...t,4,64,...e,5,...a,11]:[...t,4,64,...e,11]}block(t){return[2,64,...t,11]}loop(...t){return[3,64,...[].concat(...t),11]}br_if(t,e){return[...e,13,...la(t)]}br(t){return[12,...la(t)]}ret(t){return[...t,15]}drop(t){return[...t,26]}i64_const(t){return[66,...na(t)]}i32_const(t){return[65,...oa(t)]}i64_eqz(t){return[...t,80]}i64_eq(t,e){return[...t,...e,81]}i64_ne(t,e){return[...t,...e,82]}i64_lt_s(t,e){return[...t,...e,83]}i64_lt_u(t,e){return[...t,...e,84]}i64_gt_s(t,e){return[...t,...e,85]}i64_gt_u(t,e){return[...t,...e,86]}i64_le_s(t,e){return[...t,...e,87]}i64_le_u(t,e){return[...t,...e,88]}i64_ge_s(t,e){return[...t,...e,89]}i64_ge_u(t,e){return[...t,...e,90]}i64_add(t,e){return[...t,...e,124]}i64_sub(t,e){return[...t,...e,125]}i64_mul(t,e){return[...t,...e,126]}i64_div_s(t,e){return[...t,...e,127]}i64_div_u(t,e){return[...t,...e,128]}i64_rem_s(t,e){return[...t,...e,129]}i64_rem_u(t,e){return[...t,...e,130]}i64_and(t,e){return[...t,...e,131]}i64_or(t,e){return[...t,...e,132]}i64_xor(t,e){return[...t,...e,133]}i64_shl(t,e){return[...t,...e,134]}i64_shr_s(t,e){return[...t,...e,135]}i64_shr_u(t,e){return[...t,...e,136]}i64_extend_i32_s(t){return[...t,172]}i64_extend_i32_u(t){return[...t,173]}i64_clz(t){return[...t,121]}i64_ctz(t){return[...t,122]}i32_eqz(t){return[...t,69]}i32_eq(t,e){return[...t,...e,70]}i32_ne(t,e){return[...t,...e,71]}i32_lt_s(t,e){return[...t,...e,72]}i32_lt_u(t,e){return[...t,...e,73]}i32_gt_s(t,e){return[...t,...e,74]}i32_gt_u(t,e){return[...t,...e,75]}i32_le_s(t,e){return[...t,...e,76]}i32_le_u(t,e){return[...t,...e,77]}i32_ge_s(t,e){return[...t,...e,78]}i32_ge_u(t,e){return[...t,...e,79]}i32_add(t,e){return[...t,...e,106]}i32_sub(t,e){return[...t,...e,107]}i32_mul(t,e){return[...t,...e,108]}i32_div_s(t,e){return[...t,...e,109]}i32_div_u(t,e){return[...t,...e,110]}i32_rem_s(t,e){return[...t,...e,111]}i32_rem_u(t,e){return[...t,...e,112]}i32_and(t,e){return[...t,...e,113]}i32_or(t,e){return[...t,...e,114]}i32_xor(t,e){return[...t,...e,115]}i32_shl(t,e){return[...t,...e,116]}i32_shr_s(t,e){return[...t,...e,117]}i32_shr_u(t,e){return[...t,...e,118]}i32_rotl(t,e){return[...t,...e,119]}i32_rotr(t,e){return[...t,...e,120]}i32_wrap_i64(t){return[...t,167]}i32_clz(t){return[...t,103]}i32_ctz(t){return[...t,104]}unreachable(){return[0]}current_memory(){return[63,0]}comment(){return[]}}const ca={i32:127,i64:126,f32:125,f64:124,anyfunc:112,func:96,emptyblock:64};class da{constructor(t,e,a,i,o){if("import"==a)this.fnType="import",this.moduleName=i,this.fieldName=o;else{if("internal"!=a)throw new Error("Invalid function fnType: "+a);this.fnType="internal"}this.module=t,this.fnName=e,this.params=[],this.locals=[],this.localIdxByName={},this.code=[],this.returnType=null,this.nextLocal=0}addParam(t,e){if(this.localIdxByName[t])throw new Error(`param already exists. Function: ${this.fnName}, Param: ${t} `);const a=this.nextLocal++;this.localIdxByName[t]=a,this.params.push({type:e})}addLocal(t,e,a){const i=a||1;if(this.localIdxByName[t])throw new Error(`local already exists. Function: ${this.fnName}, Param: ${t} `);const o=this.nextLocal++;this.localIdxByName[t]=o,this.locals.push({type:e,length:i})}setReturnType(t){if(this.returnType)throw new Error(`returnType already defined. Function: ${this.fnName}`);this.returnType=t}getSignature(){return[96,...[...la(this.params.length),...this.params.map((t=>ca[t.type]))],...this.returnType?[1,ca[this.returnType]]:[0]]}getBody(){const t=this.locals.map((t=>[...la(t.length),ca[t.type]])),e=[...la(this.locals.length),...[].concat(...t),...this.code,11];return[...la(e.length),...e]}addCode(...t){this.code.push(...[].concat(...t))}getCodeBuilder(){return new sa(this)}}class ua{constructor(){this.functions=[],this.functionIdxByName={},this.nImportFunctions=0,this.nInternalFunctions=0,this.memory={pagesSize:1,moduleName:"env",fieldName:"memory"},this.free=8,this.datas=[],this.modules={},this.exports=[],this.functionsTable=[]}build(){return this._setSignatures(),new Uint8Array([...ta(1836278016),...ta(1),...this._buildType(),...this._buildImport(),...this._buildFunctionDeclarations(),...this._buildFunctionsTable(),...this._buildExports(),...this._buildElements(),...this._buildCode(),...this._buildData()])}addFunction(t){if(void 0!==this.functionIdxByName[t])throw new Error(`Function already defined: ${t}`);const e=this.functions.length;return this.functionIdxByName[t]=e,this.functions.push(new da(this,t,"internal")),this.nInternalFunctions++,this.functions[e]}addIimportFunction(t,e,a){if(void 0!==this.functionIdxByName[t])throw new Error(`Function already defined: ${t}`);if(this.functions.length>0&&"internal"==this.functions[this.functions.length-1].type)throw new Error(`Import functions must be declared before internal: ${t}`);let i=a||t;const o=this.functions.length;return this.functionIdxByName[t]=o,this.functions.push(new da(this,t,"import",e,i)),this.nImportFunctions++,this.functions[o]}setMemory(t,e,a){this.memory={pagesSize:t,moduleName:e||"env",fieldName:a||"memory"}}exportFunction(t,e){const a=e||t;if(void 0===this.functionIdxByName[t])throw new Error(`Function not defined: ${t}`);const i=this.functionIdxByName[t];a!=t&&(this.functionIdxByName[a]=i),this.exports.push({exportName:a,idx:i})}addFunctionToTable(t){const e=this.functionIdxByName[t];this.functionsTable.push(e)}addData(t,e){this.datas.push({offset:t,bytes:e})}alloc(t,e){let a,i;(Array.isArray(t)||ArrayBuffer.isView(t))&&void 0===e?(a=t.length,i=t):(a=t,i=e),a=1+(a-1>>3)<<3;const o=this.free;return this.free+=a,i&&this.addData(o,i),o}allocString(t){const e=(new globalThis.TextEncoder).encode(t);return this.alloc([...e,0])}_setSignatures(){this.signatures=[];const t={};if(this.functionsTable.length>0){const e=this.functions[this.functionsTable[0]].getSignature();t["s_"+ra(e)]=0,this.signatures.push(e)}for(let e=0;e{e.pendingLoads.push({page:t,resolve:a,reject:i})}));return e.__statusPage("After Load request: ",t),a}__statusPage(t,e){const a=[],i=this;if(!i.logHistory)return;a.push("=="+t+" "+e);let o="";for(let t=0;t "+e.history[t][a][i])}_triggerLoad(){const t=this;if(t.reading)return;if(0==t.pendingLoads.length)return;const e=Object.keys(t.pages),a=[];for(let i=0;i0&&(void 0!==t.pages[t.pendingLoads[0].page]||i>0||a.length>0);){const e=t.pendingLoads.shift();if(void 0!==t.pages[e.page]){t.pages[e.page].pendingOps++;const i=a.indexOf(e.page);i>=0&&a.splice(i,1),t.pages[e.page].loading?t.pages[e.page].loading.push(e):e.resolve(),t.__statusPage("After Load (cached): ",e.page)}else{if(i)i--;else{const e=a.shift();t.__statusPage("Before Unload: ",e),t.avBuffs.unshift(t.pages[e]),delete t.pages[e],t.__statusPage("After Unload: ",e)}e.page>=t.totalPages?(t.pages[e.page]=n(),e.resolve(),t.__statusPage("After Load (new): ",e.page)):(t.reading=!0,t.pages[e.page]=n(),t.pages[e.page].loading=[e],o.push(t.fd.read(t.pages[e.page].buff,0,t.pageSize,e.page*t.pageSize).then((a=>{t.pages[e.page].size=a.bytesRead;const i=t.pages[e.page].loading;delete t.pages[e.page].loading;for(let t=0;t{e.reject(t)}))),t.__statusPage("After Load (loading): ",e.page))}}function n(){if(t.avBuffs.length>0){const e=t.avBuffs.shift();return e.dirty=!1,e.pendingOps=1,e.size=0,e}return{dirty:!1,buff:new Uint8Array(t.pageSize),pendingOps:1,size:0}}Promise.all(o).then((()=>{t.reading=!1,t.pendingLoads.length>0&&setImmediate(t._triggerLoad.bind(t)),t._tryClose()}))}_triggerWrite(){const t=this;if(t.writing)return;const e=Object.keys(t.pages),a=[];for(let i=0;i{o.writing=!1}),(e=>{console.log("ERROR Writing: "+e),t.error=e,t._tryClose()}))))}t.writing&&Promise.all(a).then((()=>{t.writing=!1,setImmediate(t._triggerWrite.bind(t)),t._tryClose(),t.pendingLoads.length>0&&setImmediate(t._triggerLoad.bind(t))}))}_getDirtyPage(){for(let t in this.pages)if(this.pages[t].dirty)return t;return-1}async write(t,e){if(0==t.byteLength)return;const a=this;if(void 0===e&&(e=a.pos),a.pos=e+t.byteLength,a.totalSize0;){await n[l-i];const e=r+s>a.pageSize?a.pageSize-r:s,o=t.slice(t.byteLength-s,t.byteLength-s+e);new Uint8Array(a.pages[l].buff.buffer,r,e).set(o),a.pages[l].dirty=!0,a.pages[l].pendingOps--,a.pages[l].size=Math.max(r+e,a.pages[l].size),l>=a.totalPages&&(a.totalPages=l+1),s-=e,l++,r=0,a.writing||setImmediate(a._triggerWrite.bind(a))}}async read(t,e){let a=new Uint8Array(t);return await this.readToBuffer(a,0,t,e),a}async readToBuffer(t,e,a,i){if(0==a)return;const o=this;if(a>o.pageSize*o.maxPagesLoaded*.8){const t=Math.floor(1.1*a);this.maxPagesLoaded=Math.floor(t/o.pageSize)+1}if(void 0===i&&(i=o.pos),o.pos=i+a,o.pendingClose)throw new Error("Reading a closing file");const n=Math.floor(i/o.pageSize),l=Math.floor((i+a-1)/o.pageSize),r=[];for(let t=n;t<=l;t++)r.push(o._loadPage(t));o._triggerLoad();let s=n,c=i%o.pageSize,d=i+a>o.totalSize?a-(i+a-o.totalSize):a;for(;d>0;){await r[s-n],o.__statusPage("After Await (read): ",s);const i=c+d>o.pageSize?o.pageSize-c:d,l=new Uint8Array(o.pages[s].buff.buffer,o.pages[s].buff.byteOffset+c,i);t.set(l,e+a-d),o.pages[s].pendingOps--,o.__statusPage("After Op done: ",s),d-=i,s++,c=0,o.pendingLoads.length>0&&setImmediate(o._triggerLoad.bind(o))}this.pos=i+a}_tryClose(){const t=this;if(!t.pendingClose)return;t.error&&t.pendingCloseReject(t.error);t._getDirtyPage()>=0||t.writing||t.reading||t.pendingLoads.length>0||t.pendingClose()}close(){const t=this;if(t.pendingClose)throw new Error("Closing the file twice");return new Promise(((e,a)=>{t.pendingClose=e,t.pendingCloseReject=a,t._tryClose()})).then((()=>{t.fd.close()}),(e=>{throw t.fd.close(),e}))}async discard(){await this.close(),await wa.promises.unlink(this.fileName)}async writeULE32(t,e){const a=new Uint8Array(4);new DataView(a.buffer).setUint32(0,t,!0),await this.write(a,e)}async writeUBE32(t,e){const a=new Uint8Array(4);new DataView(a.buffer).setUint32(0,t,!1),await this.write(a,e)}async writeULE64(t,e){const a=new Uint8Array(8),i=new DataView(a.buffer);i.setUint32(0,4294967295&t,!0),i.setUint32(4,Math.floor(t/4294967296),!0),await this.write(a,e)}async readULE32(t){const e=await this.read(4,t);return new Uint32Array(e.buffer)[0]}async readUBE32(t){const e=await this.read(4,t);return new DataView(e.buffer).getUint32(0,!1)}async readULE64(t){const e=await this.read(8,t),a=new Uint32Array(e.buffer);return 4294967296*a[1]+a[0]}async readString(t){const e=this;if(e.pendingClose)throw new Error("Reading a closing file");let a=void 0===t?e.pos:t,i=Math.floor(a/e.pageSize),o=!1,n="";for(;!o;){let t=e._loadPage(i);e._triggerLoad(),await t,e.__statusPage("After Await (read): ",i);let l=a%e.pageSize;const r=new Uint8Array(e.pages[i].buff.buffer,e.pages[i].buff.byteOffset+l,e.pageSize-l);let s=r.findIndex((t=>0===t));o=-1!==s,o?(n+=(new TextDecoder).decode(r.slice(0,s)),e.pos=i*this.pageSize+l+s+1):(n+=(new TextDecoder).decode(r),e.pos=i*this.pageSize+l+r.length),e.pages[i].pendingOps--,e.__statusPage("After Op done: ",i),a=e.pos,i++,e.pendingLoads.length>0&&setImmediate(e._triggerLoad.bind(e))}return n}}const Aa=new Uint8Array(4),ya=new DataView(Aa.buffer),Ca=new Uint8Array(8),Ia=new DataView(Ca.buffer);class Fa{constructor(){this.pageSize=16384}_resizeIfNeeded(t){if(t>this.allocSize){const e=Math.max(this.allocSize+(1<<20),Math.floor(1.1*this.allocSize),t),a=new Uint8Array(e);a.set(this.o.data),this.o.data=a,this.allocSize=e}}async write(t,e){if(void 0===e&&(e=this.pos),this.readOnly)throw new Error("Writing a read only file");this._resizeIfNeeded(e+t.byteLength),this.o.data.set(t.slice(),e),e+t.byteLength>this.totalSize&&(this.totalSize=e+t.byteLength),this.pos=e+t.byteLength}async readToBuffer(t,e,a,i){if(void 0===i&&(i=this.pos),this.readOnly&&i+a>this.totalSize)throw new Error("Reading out of bounds");this._resizeIfNeeded(i+a);const o=new Uint8Array(this.o.data.buffer,this.o.data.byteOffset+i,a);t.set(o,e),this.pos=i+a}async read(t,e){const a=new Uint8Array(t);return await this.readToBuffer(a,0,t,e),a}close(){this.o.data.byteLength!=this.totalSize&&(this.o.data=this.o.data.slice(0,this.totalSize))}async discard(){}async writeULE32(t,e){ya.setUint32(0,t,!0),await this.write(Aa,e)}async writeUBE32(t,e){ya.setUint32(0,t,!1),await this.write(Aa,e)}async writeULE64(t,e){Ia.setUint32(0,4294967295&t,!0),Ia.setUint32(4,Math.floor(t/4294967296),!0),await this.write(Ca,e)}async readULE32(t){const e=await this.read(4,t);return new Uint32Array(e.buffer)[0]}async readUBE32(t){const e=await this.read(4,t);return new DataView(e.buffer).getUint32(0,!1)}async readULE64(t){const e=await this.read(8,t),a=new Uint32Array(e.buffer);return 4294967296*a[1]+a[0]}async readString(t){const e=this;let a=void 0===t?e.pos:t;if(a>this.totalSize){if(this.readOnly)throw new Error("Reading out of bounds");this._resizeIfNeeded(t)}const i=new Uint8Array(e.o.data.buffer,a,this.totalSize-a);let o=i.findIndex((t=>0===t)),n="";return-1!==o?(n=(new TextDecoder).decode(i.slice(0,o)),e.pos=a+o+1):e.pos=a,n}}const xa=1<<22;const Ea=new Uint8Array(4),va=new DataView(Ea.buffer),Ba=new Uint8Array(8),Sa=new DataView(Ba.buffer);class Pa{constructor(){this.pageSize=16384}_resizeIfNeeded(t){if(t<=this.totalSize)return;if(this.readOnly)throw new Error("Reading out of file bounds");const e=Math.floor((t-1)/xa)+1;for(let a=Math.max(this.o.data.length-1,0);a0;){const e=o+n>xa?xa-o:n,l=t.slice(t.byteLength-n,t.byteLength-n+e);new Uint8Array(a.o.data[i].buffer,o,e).set(l),n-=e,i++,o=0}this.pos=e+t.byteLength}async readToBuffer(t,e,a,i){const o=this;if(void 0===i&&(i=o.pos),this.readOnly&&i+a>this.totalSize)throw new Error("Reading out of bounds");this._resizeIfNeeded(i+a);let n=Math.floor(i/xa),l=i%xa,r=a;for(;r>0;){const i=l+r>xa?xa-l:r,s=new Uint8Array(o.o.data[n].buffer,l,i);t.set(s,e+a-r),r-=i,n++,l=0}this.pos=i+a}async read(t,e){const a=new Uint8Array(t);return await this.readToBuffer(a,0,t,e),a}close(){}async discard(){}async writeULE32(t,e){va.setUint32(0,t,!0),await this.write(Ea,e)}async writeUBE32(t,e){va.setUint32(0,t,!1),await this.write(Ea,e)}async writeULE64(t,e){Sa.setUint32(0,4294967295&t,!0),Sa.setUint32(4,Math.floor(t/4294967296),!0),await this.write(Ba,e)}async readULE32(t){const e=await this.read(4,t);return new Uint32Array(e.buffer)[0]}async readUBE32(t){const e=await this.read(4,t);return new DataView(e.buffer).getUint32(0,!1)}async readULE64(t){const e=await this.read(8,t),a=new Uint32Array(e.buffer);return 4294967296*a[1]+a[0]}async readString(t){const e=this;let a=void 0===t?e.pos:t;if(a>this.totalSize){if(this.readOnly)throw new Error("Reading out of bounds");this._resizeIfNeeded(t)}let i=!1,o="";for(;!i;){let t=Math.floor(a/xa),n=a%xa;if(void 0===e.o.data[t])throw new Error("ERROR");let l=Math.min(2048,e.o.data[t].length-n);const r=new Uint8Array(e.o.data[t].buffer,n,l);let s=r.findIndex((t=>0===t));i=-1!==s,i?(o+=(new TextDecoder).decode(r.slice(0,s)),e.pos=t*xa+n+s+1):(o+=(new TextDecoder).decode(r),e.pos=t*xa+n+r.length),a=e.pos}return o}}const Ga=1024,Oa=512,Ta=2,za=0,Ua=65536,Qa=8192;async function qa(t,e,a){if("string"==typeof t&&(t={type:"file",fileName:t,cacheSize:e||Ua,pageSize:a||Qa}),"file"==t.type)return await La(t.fileName,Ga|Oa|Ta,t.cacheSize,t.pageSize);if("mem"==t.type)return function(t){const e=t.initialSize||1<<20,a=new Fa;return a.o=t,a.o.data=new Uint8Array(e),a.allocSize=e,a.totalSize=0,a.readOnly=!1,a.pos=0,a}(t);if("bigMem"==t.type)return function(t){const e=t.initialSize||0,a=new Pa;a.o=t;const i=e?Math.floor((e-1)/xa)+1:0;a.o.data=[];for(let t=0;ta)throw new Error("Version not supported");const s=await n.readULE32();let c=[];for(let t=0;t1)throw new Error(t.fileName+": Section Duplicated "+a);t.pos=e[a][0].p,t.readingSection=e[a][0]}async function Va(t,e){if(void 0===t.readingSection)throw new Error("Not reading a section");if(!e&&t.pos-t.readingSection.p!=t.readingSection.size)throw new Error("Invalid section size reading");delete t.readingSection}async function Ka(t,e,a,i){const o=new Uint8Array(a);pa.toRprLE(o,0,e,a),await t.write(o,i)}async function ja(t,e,a){const i=await t.read(e,a);return pa.fromRprLE(i,0,e)}async function Ha(t,e,a,i,o){void 0===o&&(o=e[i][0].size);const n=t.pageSize;await $a(t,e,i),await Da(a,i);for(let e=0;ee[a][0].size)throw new Error("Reading out of the range of the section");let n;return n=o<1<<30?new Uint8Array(o):new Ge(o),await t.readToBuffer(n,0,o,e[a][0].p+i),n}async function Wa(t,e,a,i,o){const n=16*t.pageSize;if(await $a(t,e,o),await $a(a,i,o),e[o][0].size!=i[o][0].size)return!1;const l=e[o][0].size;for(let e=0;e=0)e=await ga();else{if(!(["BLS12381"].indexOf(a)>=0))throw new Error(`Curve not supported: ${t}`);e=await fa()}return e}var ii={exports:{}},oi=function t(e,a){if(!e){var i=new ni(a);throw Error.captureStackTrace&&Error.captureStackTrace(i,t),i}};class ni extends Error{}ni.prototype.name="AssertionError";var li={exports:{}};function ri(t){return t.length}var si={byteLength:ri,toString:function(t){const e=t.byteLength;let a="";for(let i=0;i1&&61===t.charCodeAt(e-1)&&e--,3*e>>>2}di[45]=62,di[95]=63;var gi={byteLength:ui,toString:function(t){const e=t.byteLength;let a="";for(let i=0;i>2]+ci[(3&t[i])<<4|t[i+1]>>4]+ci[(15&t[i+1])<<2|t[i+2]>>6]+ci[63&t[i+2]];return e%3==2?a=a.substring(0,a.length-1)+"=":e%3==1&&(a=a.substring(0,a.length-2)+"=="),a},write:function(t,e,a=0,i=ui(e)){const o=Math.min(i,t.byteLength-a);for(let a=0,i=0;i>4,t[i++]=(15&n)<<4|l>>2,t[i++]=(3&l)<<6|63&r}return o}};function fi(t){return t.length>>>1}var hi={byteLength:fi,toString:function(t){const e=t.byteLength;t=new DataView(t.buffer,t.byteOffset,e);let a="",i=0;for(let o=e-e%4;i=48&&t<=57?t-48:t>=65&&t<=70?t-65+10:t>=97&&t<=102?t-97+10:void 0}function pi(t){let e=0;for(let a=0,i=t.length;a=55296&&o<=56319&&a+1=56320&&i<=57343){e+=4,a++;continue}}e+=o<=127?1:o<=2047?2:3}return e}let mi,wi;if("undefined"!=typeof TextDecoder){const t=new TextDecoder;mi=function(e){return t.decode(e)}}else mi=function(t){const e=t.byteLength;let a="",i=0;for(;i0){let e=0;for(;e>i,i-=6;i>=0;)t[l++]=128|a>>i&63,i-=6;n+=a>=65536?2:1}return o};var Li={byteLength:pi,toString:mi,write:wi};function bi(t){return 2*t.length}var Ai,yi,Ci={byteLength:bi,toString:function(t){const e=t.byteLength;let a="";for(let i=0;i>8,l=o%256;t[a+2*i]=l,t[a+2*i+1]=n}return o}};!function(t,e){const a=si,i=gi,o=hi,n=Li,l=Ci,r=255===new Uint8Array(Uint16Array.of(255).buffer)[0];function s(t){switch(t){case"ascii":return a;case"base64":return i;case"hex":return o;case"utf8":case"utf-8":case void 0:return n;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return l;default:throw new Error(`Unknown encoding: ${t}`)}}function c(t){return t instanceof Uint8Array}function d(t,e,a){return"string"==typeof t?function(t,e){const a=s(e),i=new Uint8Array(a.byteLength(t));return a.write(i,t,0,i.byteLength),i}(t,e):Array.isArray(t)?function(t){const e=new Uint8Array(t.length);return e.set(t),e}(t):ArrayBuffer.isView(t)?function(t){const e=new Uint8Array(t.byteLength);return e.set(t),e}(t):function(t,e,a){return new Uint8Array(t,e,a)}(t,e,a)}function u(t,e,a,i,o){if(0===t.byteLength)return-1;if("string"==typeof a?(i=a,a=0):void 0===a?a=o?0:t.length-1:a<0&&(a+=t.byteLength),a>=t.byteLength){if(o)return-1;a=t.byteLength-1}else if(a<0){if(!o)return-1;a=0}if("string"==typeof e)e=d(e,i);else if("number"==typeof e)return e&=255,o?t.indexOf(e,a):t.lastIndexOf(e,a);if(0===e.byteLength)return-1;if(o){let i=-1;for(let o=a;ot.byteLength&&(a=t.byteLength-e.byteLength);for(let i=a;i>=0;i--){let a=!0;for(let o=0;oo)return 1}return t.byteLength>e.byteLength?1:t.byteLengtht+e.byteLength),0));const a=new Uint8Array(e);return t.reduce(((t,e)=>(a.set(e,t),t+e.byteLength)),0),a},copy:function(t,e,a=0,i=0,o=t.byteLength){if(o>0&&o=t.byteLength)throw new RangeError("sourceStart is out of range");if(o<0)throw new RangeError("sourceEnd is out of range");a>=e.byteLength&&(a=e.byteLength),o>t.byteLength&&(o=t.byteLength),e.byteLength-a=o||i<=a?"":(a<0&&(a=0),i>o&&(i=o),(0!==a||i{for(var t=new Uint8Array(128),e=0;e<64;e++)t[e<26?e+65:e<52?e+71:e<62?e-4:4*e-205]=e;return e=>{for(var a=e.length,i=new Uint8Array(3*(a-("="==e[a-1])-("="==e[a-2]))/4|0),o=0,n=0;o>4,i[n++]=r<<4|s>>2,i[n++]=s<<6|c}return i}})(),e=((t,e)=>function(){return e||(0,t[Object.keys(t)[0]])((e={exports:{}}).exports,e),e.exports})({"wasm-binary:./blake2b.wat"(e,a){a.exports=t("AGFzbQEAAAABEANgAn9/AGADf39/AGABfwADBQQAAQICBQUBAQroBwdNBQZtZW1vcnkCAAxibGFrZTJiX2luaXQAAA5ibGFrZTJiX3VwZGF0ZQABDWJsYWtlMmJfZmluYWwAAhBibGFrZTJiX2NvbXByZXNzAAMKvz8EwAIAIABCADcDACAAQgA3AwggAEIANwMQIABCADcDGCAAQgA3AyAgAEIANwMoIABCADcDMCAAQgA3AzggAEIANwNAIABCADcDSCAAQgA3A1AgAEIANwNYIABCADcDYCAAQgA3A2ggAEIANwNwIABCADcDeCAAQoiS853/zPmE6gBBACkDAIU3A4ABIABCu86qptjQ67O7f0EIKQMAhTcDiAEgAEKr8NP0r+68tzxBECkDAIU3A5ABIABC8e30+KWn/aelf0EYKQMAhTcDmAEgAELRhZrv+s+Uh9EAQSApAwCFNwOgASAAQp/Y+dnCkdqCm39BKCkDAIU3A6gBIABC6/qG2r+19sEfQTApAwCFNwOwASAAQvnC+JuRo7Pw2wBBOCkDAIU3A7gBIABCADcDwAEgAEIANwPIASAAQgA3A9ABC20BA38gAEHAAWohAyAAQcgBaiEEIAQpAwCnIQUCQANAIAEgAkYNASAFQYABRgRAIAMgAykDACAFrXw3AwBBACEFIAAQAwsgACAFaiABLQAAOgAAIAVBAWohBSABQQFqIQEMAAsLIAQgBa03AwALYQEDfyAAQcABaiEBIABByAFqIQIgASABKQMAIAIpAwB8NwMAIABCfzcD0AEgAikDAKchAwJAA0AgA0GAAUYNASAAIANqQQA6AAAgA0EBaiEDDAALCyACIAOtNwMAIAAQAwuqOwIgfgl/IABBgAFqISEgAEGIAWohIiAAQZABaiEjIABBmAFqISQgAEGgAWohJSAAQagBaiEmIABBsAFqIScgAEG4AWohKCAhKQMAIQEgIikDACECICMpAwAhAyAkKQMAIQQgJSkDACEFICYpAwAhBiAnKQMAIQcgKCkDACEIQoiS853/zPmE6gAhCUK7zqqm2NDrs7t/IQpCq/DT9K/uvLc8IQtC8e30+KWn/aelfyEMQtGFmu/6z5SH0QAhDUKf2PnZwpHagpt/IQ5C6/qG2r+19sEfIQ9C+cL4m5Gjs/DbACEQIAApAwAhESAAKQMIIRIgACkDECETIAApAxghFCAAKQMgIRUgACkDKCEWIAApAzAhFyAAKQM4IRggACkDQCEZIAApA0ghGiAAKQNQIRsgACkDWCEcIAApA2AhHSAAKQNoIR4gACkDcCEfIAApA3ghICANIAApA8ABhSENIA8gACkD0AGFIQ8gASAFIBF8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSASfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgE3x8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBR8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAVfHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgFnx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBd8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAYfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgGXx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBp8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAbfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgHHx8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIB18fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCAefHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgH3x8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFICB8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAffHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgG3x8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBV8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAZfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgGnx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHICB8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAefHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggF3x8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBJ8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiAdfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgEXx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBN8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAcfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggGHx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBZ8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAUfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgHHx8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIBl8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAdfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgEXx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBZ8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByATfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggIHx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIB58fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAbfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgH3x8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBR8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAXfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggGHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBJ8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAafHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFXx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIBh8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSAafHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgFHx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBJ8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAefHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgHXx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBx8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAffHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgE3x8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBd8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAWfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgG3x8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIBV8fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCARfHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgIHx8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFIBl8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAafHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgEXx8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBZ8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAYfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgE3x8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHIBV8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAbfHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggIHx8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIB98fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiASfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgHHx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIB18fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAXfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggGXx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBR8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAefHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgE3x8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIB18fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAXfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgG3x8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBF8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAcfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggGXx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBR8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAVfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgHnx8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBh8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAWfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggIHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIB98fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSASfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgGnx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIB18fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSAWfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgEnx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGICB8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAffHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgHnx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBV8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAbfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgEXx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBh8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAXfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgFHx8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIBp8fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCATfHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgGXx8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFIBx8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAefHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgHHx8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBh8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAffHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgHXx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHIBJ8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAUfHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggGnx8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBZ8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiARfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgIHx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBV8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAZfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggF3x8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBN8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAbfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgF3x8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFICB8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAffHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgGnx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBx8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAUfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggEXx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBl8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAdfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgE3x8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIB58fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAYfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggEnx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBV8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAbfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFnx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIBt8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSATfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgGXx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBV8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAYfHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgF3x8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBJ8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAWfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgIHx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBx8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAafHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgH3x8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIBR8fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCAdfHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgHnx8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFIBF8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSARfHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgEnx8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBN8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAUfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgFXx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHIBZ8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAXfHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggGHx8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBl8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiAafHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgG3x8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBx8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAdfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggHnx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIB98fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAgfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgH3x8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIBt8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAVfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgGXx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBp8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAgfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggHnx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBd8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiASfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgHXx8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBF8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByATfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggHHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBh8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAWfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFHx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgISAhKQMAIAEgCYWFNwMAICIgIikDACACIAqFhTcDACAjICMpAwAgAyALhYU3AwAgJCAkKQMAIAQgDIWFNwMAICUgJSkDACAFIA2FhTcDACAmICYpAwAgBiAOhYU3AwAgJyAnKQMAIAcgD4WFNwMAICggKCkDACAIIBCFhTcDAAs=")}}),a=e(),i=WebAssembly.compile(a);return Ai=async t=>(await WebAssembly.instantiate(await i,t)).exports}()().then((t=>{xi=t})),vi=64,Bi=[];ii.exports=Ui;var Si=ii.exports.BYTES_MIN=16,Pi=ii.exports.BYTES_MAX=64;ii.exports.BYTES=32;var Gi=ii.exports.KEYBYTES_MIN=16,Oi=ii.exports.KEYBYTES_MAX=64;ii.exports.KEYBYTES=32;var Ti=ii.exports.SALTBYTES=16,zi=ii.exports.PERSONALBYTES=16;function Ui(t,e,a,i,o){if(!(this instanceof Ui))return new Ui(t,e,a,i,o);if(!xi)throw new Error("WASM not loaded. Wait for Blake2b.ready(cb)");t||(t=32),!0!==o&&(Ii(t>=Si,"digestLength must be at least "+Si+", was given "+t),Ii(t<=Pi,"digestLength must be at most "+Pi+", was given "+t),null!=e&&(Ii(e instanceof Uint8Array,"key must be Uint8Array or Buffer"),Ii(e.length>=Gi,"key must be at least "+Gi+", was given "+e.length),Ii(e.length<=Oi,"key must be at least "+Oi+", was given "+e.length)),null!=a&&(Ii(a instanceof Uint8Array,"salt must be Uint8Array or Buffer"),Ii(a.length===Ti,"salt must be exactly "+Ti+", was given "+a.length)),null!=i&&(Ii(i instanceof Uint8Array,"personal must be Uint8Array or Buffer"),Ii(i.length===zi,"personal must be exactly "+zi+", was given "+i.length))),Bi.length||(Bi.push(vi),vi+=216),this.digestLength=t,this.finalized=!1,this.pointer=Bi.pop(),this._memory=new Uint8Array(xi.memory.buffer),this._memory.fill(0,0,64),this._memory[0]=this.digestLength,this._memory[1]=e?e.length:0,this._memory[2]=1,this._memory[3]=1,a&&this._memory.set(a,32),i&&this._memory.set(i,48),this.pointer+216>this._memory.length&&this._realloc(this.pointer+216),xi.blake2b_init(this.pointer,this.digestLength),e&&(this.update(e),this._memory.fill(0,vi,vi+e.length),this._memory[this.pointer+200]=128)}function Qi(){}function qi(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 Mi(t,e){const a=new DataView(t.buffer,t.byteOffset,t.byteLength);let i="";for(let t=0;t<4;t++){t>0&&(i+="\n"),i+="\t\t";for(let e=0;e<4;e++)e>0&&(i+=" "),i+=a.getUint32(16*t+4*e).toString(16).padStart(8,"0")}return e&&(i=e+"\n"+i),i}function ki(t,e){if(t.byteLength!=e.byteLength)return!1;for(var a=new Int8Array(t),i=new Int8Array(e),o=0;o!=t.byteLength;o++)if(a[o]!=i[o])return!1;return!0}function Ri(t){const e=t.getPartialHash(),a=ii.exports(64);return a.setPartialHash(e),a}async function Di(t,e,a,i,o){if(t.G1.isZero(e))return!1;if(t.G1.isZero(a))return!1;if(t.G2.isZero(i))return!1;if(t.G2.isZero(o))return!1;return await t.pairingEq(e,o,t.G1.neg(a),i)}function Ni(t){let e=new Uint8Array(t);return void 0!==globalThis.crypto?globalThis.crypto.getRandomValues(e):q.randomFillSync(e),e}async function $i(t){if(void 0!==globalThis.crypto&&void 0!==globalThis.crypto.subtle){const e=await globalThis.crypto.subtle.digest("SHA-256",t.buffer);return new Uint8Array(e)}return q.createHash("sha256").update(t).digest()}function Vi(t,e){return new DataView(t.buffer).getUint32(e,!1)}async function Ki(t){for(;!t;)t=await window.prompt("Enter a random text. (Entropy): ","");const e=ii.exports(64);e.update(Ni(64));const a=new TextEncoder;e.update(a.encode(t));const i=e.digest(),o=[];for(let t=0;t<8;t++)o[t]=Vi(i,4*t);return new Q(o)}async function ji(t,e){let a,i;e<32?(a=1<>>0,i=1):(a=4294967296,i=1<>>0);let o=t;for(let t=0;t{a[i]=Wi(t,e[i])})),a}return"bigint"==typeof e||void 0!==e.eq?e.toString(10):e}Ui.prototype._realloc=function(t){xi.memory.grow(Math.max(0,Math.ceil(Math.abs(t-this._memory.length)/65536))),this._memory=new Uint8Array(xi.memory.buffer)},Ui.prototype.update=function(t){return Ii(!1===this.finalized,"Hash instance finalized"),Ii(t instanceof Uint8Array,"input must be Uint8Array or Buffer"),vi+t.length>this._memory.length&&this._realloc(vi+t.length),this._memory.set(t,vi),xi.blake2b_update(this.pointer,vi,vi+t.length),this},Ui.prototype.digest=function(t){if(Ii(!1===this.finalized,"Hash instance finalized"),this.finalized=!0,Bi.push(this.pointer),xi.blake2b_final(this.pointer),!t||"binary"===t)return this._memory.slice(this.pointer+128,this.pointer+128+this.digestLength);if("string"==typeof t)return Fi.toString(this._memory,t,this.pointer+128,this.pointer+128+this.digestLength);Ii(t instanceof Uint8Array&&t.length>=this.digestLength,"input must be Uint8Array or Buffer");for(var e=0;et()),t):t(new Error("WebAssembly not supported"))},Ui.prototype.ready=Ui.ready,Ui.prototype.getPartialHash=function(){return this._memory.slice(this.pointer,this.pointer+216)},Ui.prototype.setPartialHash=function(t){this._memory.set(t,this.pointer)};const Yi=1,Ji=1,Xi=2,to=10,eo=3,ao=17,io=2,oo=3,no=4,lo=5,ro=6,so=7,co=8,uo=9,go=10,fo=11,ho=12,_o=13,po=14,mo=15,wo=16,Lo=17;async function bo(t,e){await Da(t,1),await t.writeULE32(1),await Na(t);const a=await ei(e.q);await Da(t,2);const i=a.q,o=8*(Math.floor((pa.bitLength(i)-1)/64)+1),n=a.r,l=8*(Math.floor((pa.bitLength(n)-1)/64)+1);await t.writeULE32(o),await Ka(t,i,o),await t.writeULE32(l),await Ka(t,n,l),await t.writeULE32(e.nVars),await t.writeULE32(e.nPublic),await t.writeULE32(e.domainSize),await Ao(t,a,e.vk_alpha_1),await Ao(t,a,e.vk_beta_1),await yo(t,a,e.vk_beta_2),await yo(t,a,e.vk_gamma_2),await Ao(t,a,e.vk_delta_1),await yo(t,a,e.vk_delta_2),await Na(t)}async function Ao(t,e,a){const i=new Uint8Array(2*e.G1.F.n8);e.G1.toRprLEM(i,0,a),await t.write(i)}async function yo(t,e,a){const i=new Uint8Array(2*e.G2.F.n8);e.G2.toRprLEM(i,0,a),await t.write(i)}async function Co(t,e,a){const i=await t.read(2*e.G1.F.n8),o=e.G1.fromRprLEM(i,0);return a?e.G1.toObject(o):o}async function Io(t,e,a){const i=await t.read(2*e.G2.F.n8),o=e.G2.fromRprLEM(i,0);return a?e.G2.toObject(o):o}async function Fo(t,e,a){await $a(t,e,1);const i=await t.readULE32();if(await Va(t),i===Ji)return await async function(t,e,a){const i={protocol:"groth16"};await $a(t,e,2);const o=await t.readULE32();i.n8q=o,i.q=await ja(t,o);const n=await t.readULE32();return i.n8r=n,i.r=await ja(t,n),i.curve=await ei(i.q),i.nVars=await t.readULE32(),i.nPublic=await t.readULE32(),i.domainSize=await t.readULE32(),i.power=qi(i.domainSize),i.vk_alpha_1=await Co(t,i.curve,a),i.vk_beta_1=await Co(t,i.curve,a),i.vk_beta_2=await Io(t,i.curve,a),i.vk_gamma_2=await Io(t,i.curve,a),i.vk_delta_1=await Co(t,i.curve,a),i.vk_delta_2=await Io(t,i.curve,a),await Va(t),i}(t,e,a);if(i===Xi)return await async function(t,e,a){const i={protocol:"plonk"};await $a(t,e,2);const o=await t.readULE32();i.n8q=o,i.q=await ja(t,o);const n=await t.readULE32();return i.n8r=n,i.r=await ja(t,n),i.curve=await ei(i.q),i.nVars=await t.readULE32(),i.nPublic=await t.readULE32(),i.domainSize=await t.readULE32(),i.power=qi(i.domainSize),i.nAdditions=await t.readULE32(),i.nConstraints=await t.readULE32(),i.k1=await t.read(n),i.k2=await t.read(n),i.Qm=await Co(t,i.curve,a),i.Ql=await Co(t,i.curve,a),i.Qr=await Co(t,i.curve,a),i.Qo=await Co(t,i.curve,a),i.Qc=await Co(t,i.curve,a),i.S1=await Co(t,i.curve,a),i.S2=await Co(t,i.curve,a),i.S3=await Co(t,i.curve,a),i.X_2=await Io(t,i.curve,a),await Va(t),i}(t,e,a);if(i===to)return await async function(t,e,a){const i={protocol:"fflonk"};i.protocolId=to,await $a(t,e,io);const o=await t.readULE32();i.n8q=o,i.q=await ja(t,o),i.curve=await ei(i.q);const n=await t.readULE32();return i.n8r=n,i.r=await ja(t,n),i.nVars=await t.readULE32(),i.nPublic=await t.readULE32(),i.domainSize=await t.readULE32(),i.power=qi(i.domainSize),i.nAdditions=await t.readULE32(),i.nConstraints=await t.readULE32(),i.k1=await t.read(n),i.k2=await t.read(n),i.w3=await t.read(n),i.w4=await t.read(n),i.w8=await t.read(n),i.wr=await t.read(n),i.X_2=await Io(t,i.curve,a),i.C0=await Co(t,i.curve,a),await Va(t),i}(t,e,a);throw new Error("Protocol not supported: ")}async function xo(t,e,a){const i={delta:{}};i.deltaAfter=await Co(t,e,a),i.delta.g1_s=await Co(t,e,a),i.delta.g1_sx=await Co(t,e,a),i.delta.g2_spx=await Io(t,e,a),i.transcript=await t.read(64),i.type=await t.readULE32();const o=await t.readULE32(),n=t.pos;let l=0;for(;t.pos-n0){const e=new Uint8Array(i);await t.writeULE32(e.byteLength),await t.write(e)}else await t.writeULE32(0)}async function Bo(t,e,a){await Da(t,10),await t.write(a.csHash),await t.writeULE32(a.contributions.length);for(let i=0;i0;)a.unshift(0),n--;return a}async function qo(t,e){e=e||{};let a,i=32767,o=!1;for(;!o;)try{a=new WebAssembly.Memory({initial:i}),o=!0}catch(t){if(1===i)throw t;console.warn("Could not allocate "+1024*i*64+" bytes. This may cause severe instability. Trying with "+1024*i*64/2+" bytes"),i=Math.floor(i/2)}const n=await WebAssembly.compile(t);let l,r="",s="",c=1,d=0,u=0;const g=await WebAssembly.instantiate(n,{env:{memory:a},runtime:{exceptionHandler:function(t){let e;throw e=1==t?"Signal not found. ":2==t?"Too many signals set. ":3==t?"Signal already set. ":4==t?"Assert Failed. ":5==t?"Not enough memory. ":6==t?"Input signal array access exceeds the size. ":"Unknown error. ",console.error("ERROR: ",t,r),new Error(e+r)},printErrorMessage:function(){r+=h()+"\n"},writeBufferMessage:function(){const t=h();"\n"===t?(console.log(s),s=""):(""!==s&&(s+=" "),s+=t)},showSharedRWMemory:function(){const t=g.exports.getFieldNumLen32(),e=new Uint32Array(t);for(let a=0;a=2&&(d>=1||u>=7)){""!==s&&(s+=" ");const t=pa.fromArray(e,4294967296).toString();s+=t}else console.log(pa.fromArray(e,4294967296))},error:function(t,a,i,o,n,r){let s;throw s=7==t?_(a)+" "+l.getFr(o).toString()+" != "+l.getFr(n).toString()+" "+_(r):9==t?_(a)+" "+l.getFr(o).toString()+" "+_(n):5==t&&e.sym?_(a)+" "+e.sym.labelIdx2Name[n]:_(a)+" "+i+" "+o+" "+n+" "+r,console.log("ERROR: ",t,s),new Error(s)},log:function(t){console.log(l.getFr(t).toString())},logGetSignal:function(t,a){e.logGetSignal&&e.logGetSignal(t,l.getFr(a))},logSetSignal:function(t,a){e.logSetSignal&&e.logSetSignal(t,l.getFr(a))},logStartComponent:function(t){e.logStartComponent&&e.logStartComponent(t)},logFinishComponent:function(t){e.logFinishComponent&&e.logFinishComponent(t)}}});"function"==typeof g.exports.getVersion&&(c=g.exports.getVersion()),"function"==typeof g.exports.getMinorVersion&&(d=g.exports.getMinorVersion()),"function"==typeof g.exports.getPatchVersion&&(u=g.exports.getPatchVersion());const f=e&&(e.sanityCheck||e.logGetSignal||e.logSetSignal||e.logStartComponent||e.logFinishComponent);return l=2===c?new ko(g,f):new Mo(a,g,f),l;function h(){for(var t="",e=g.exports.getMessageChar();0!=e;)t+=String.fromCharCode(e),e=g.exports.getMessageChar();return t}function _(t){const e=new Uint8Array(a.buffer),i=[];for(let a=0;e[t+a]>0;a++)i.push(e[t+a]);return String.fromCharCode.apply(null,i)}}class Mo{constructor(t,e,a){this.memory=t,this.i32=new Uint32Array(t.buffer),this.instance=e,this.n32=(this.instance.exports.getFrLen()>>2)-2;const i=this.instance.exports.getPRawPrime(),o=new Array(this.n32);for(let t=0;t>2)+t];this.prime=pa.fromArray(o,4294967296),this.Fr=new V(this.prime),this.mask32=pa.fromString("FFFFFFFF",16),this.NVars=this.instance.exports.getNVars(),this.n64=Math.floor((this.Fr.bitLength-1)/64)+1,this.R=this.Fr.e(pa.shiftLeft(1,64*this.n64)),this.RInv=this.Fr.inv(this.R),this.sanityCheck=a}circom_version(){return 1}async _doCalculateWitness(t,e){this.instance.exports.init(this.sanityCheck||e?1:0);const a=this.allocInt(),i=this.allocFr();Object.keys(t).forEach((e=>{const o=Uo(e),n=parseInt(o.slice(0,8),16),l=parseInt(o.slice(8,16),16);try{this.instance.exports.getSignalOffset32(a,0,n,l)}catch(t){throw new Error(`Signal ${e} is not an input of the circuit.`)}const r=this.getInt(a),s=zo(t[e]);for(let t=0;t>2]}setInt(t,e){this.i32[t>>2]=e}getFr(t){const e=this,a=t>>2;if(2147483648&e.i32[a+1]){const t=new Array(e.n32);for(let i=0;i>2]=o,void(a.i32[1+(t>>2)]=0)}a.i32[t>>2]=0,a.i32[1+(t>>2)]=2147483648;const n=pa.toArray(e,4294967296);for(let e=0;e>2)+e]=i>=0?n[i]:0}}}class ko{constructor(t,e){this.instance=t,this.version=this.instance.exports.getVersion(),this.n32=this.instance.exports.getFieldNumLen32(),this.instance.exports.getRawPrime();const a=new Array(this.n32);for(let t=0;t{const a=Uo(e),o=parseInt(a.slice(0,8),16),n=parseInt(a.slice(8,16),16),l=zo(t[e]);for(let t=0;t1)throw new Error(t.fileName+": File has more than one header");t.pos=e[1][0].p;const a=await t.readULE32(),i=await t.read(a),o=pa.fromRprLE(i),n=await ei(o);if(8*n.F1.n64!=a)throw new Error(t.fileName+": Invalid size");const l=await t.readULE32(),r=await t.readULE32();if(t.pos-e[1][0].p!=e[1][0].size)throw new Error("Invalid PTau header size");return{curve:n,power:l,ceremonyPower:r}}function tn(t,e,a,i){const o={tau:{},alpha:{},beta:{}};return o.tau.g1_s=n(),o.tau.g1_sx=n(),o.alpha.g1_s=n(),o.alpha.g1_sx=n(),o.beta.g1_s=n(),o.beta.g1_sx=n(),o.tau.g2_spx=l(),o.alpha.g2_spx=l(),o.beta.g2_spx=l(),o;function n(){let o;return o=i?a.G1.fromRprLEM(t,e):a.G1.fromRprUncompressed(t,e),e+=2*a.G1.F.n8,o}function l(){let o;return o=i?a.G2.fromRprLEM(t,e):a.G2.fromRprUncompressed(t,e),e+=2*a.G2.F.n8,o}}function en(t,e,a,i,o){async function n(i){o?a.G1.toRprLEM(t,e,i):a.G1.toRprUncompressed(t,e,i),e+=2*a.F1.n8}async function l(i){o?a.G2.toRprLEM(t,e,i):a.G2.toRprUncompressed(t,e,i),e+=2*a.F2.n8}return n(i.tau.g1_s),n(i.tau.g1_sx),n(i.alpha.g1_s),n(i.alpha.g1_sx),n(i.beta.g1_s),n(i.beta.g1_sx),l(i.tau.g2_spx),l(i.alpha.g2_spx),l(i.beta.g2_spx),t}async function an(t,e){const a={};a.tauG1=await s(),a.tauG2=await c(),a.alphaG1=await s(),a.betaG1=await s(),a.betaG2=await c(),a.key=await async function(t,e,a){return tn(await t.read(2*e.F1.n8*6+2*e.F2.n8*3),0,e,a)}(t,e,!0),a.partialHash=await t.read(216),a.nextChallenge=await t.read(64),a.type=await t.readULE32();const i=new Uint8Array(2*e.G1.F.n8*6+2*e.G2.F.n8*3);en(i,0,e,a.key,!1);const o=ii.exports(64);o.setPartialHash(a.partialHash),o.update(i),a.responseHash=o.digest();const n=await t.readULE32(),l=t.pos;let r=0;for(;t.pos-l1)throw new Error(t.fileName+": File has more than one contributions section");t.pos=a[7][0].p;const i=await t.readULE32(),o=[];for(let a=0;a0){const e=new Uint8Array(n);await t.writeULE32(e.byteLength),await t.write(e)}else await t.writeULE32(0);async function l(a){e.G1.toRprLEM(i,0,a),await t.write(i)}async function r(a){e.G2.toRprLEM(o,0,a),await t.write(o)}}async function ln(t,e,a){await t.writeULE32(7);const i=t.pos;await t.writeULE64(0),await t.writeULE32(a.length);for(let i=0;i0?u[u.length-1].nextChallenge:rn(c,d,n);const L=await Ra(a,"ptau",1,o?7:2);await Jo(L,c,d);const b=await m.read(64);if(ki(l,w)&&(w=b,u[u.length-1].nextChallenge=w),!ki(b,w))throw new Error("Wrong contribution. this contribution is not based on the previus hash");const A=new ii.exports(64);A.update(b);const y=[];let C;C=await x(m,L,"G1",2,2**d*2-1,[1],"tauG1"),g.tauG1=C[0],C=await x(m,L,"G2",3,2**d,[1],"tauG2"),g.tauG2=C[0],C=await x(m,L,"G1",4,2**d,[0],"alphaG1"),g.alphaG1=C[0],C=await x(m,L,"G1",5,2**d,[0],"betaG1"),g.betaG1=C[0],C=await x(m,L,"G2",6,1,[0],"betaG2"),g.betaG2=C[0],g.partialHash=A.getPartialHash();const I=await m.read(2*c.F1.n8*6+2*c.F2.n8*3);g.key=tn(I,0,c,!1),A.update(new Uint8Array(I));const F=A.digest();if(n&&n.info(Mi(F,"Contribution Response Hash imported: ")),o){const t=new ii.exports(64);t.update(F),await E(t,L,"G1",2,2**d*2-1,"tauG1",n),await E(t,L,"G2",3,2**d,"tauG2",n),await E(t,L,"G1",4,2**d,"alphaTauG1",n),await E(t,L,"G1",5,2**d,"betaTauG1",n),await E(t,L,"G2",6,1,"betaG2",n),g.nextChallenge=t.digest(),n&&n.info(Mi(g.nextChallenge,"Next Challenge Hash: "))}else g.nextChallenge=l;return u.push(g),await ln(L,c,u),await m.close(),await L.close(),await r.close(),g.nextChallenge;async function x(t,e,a,i,l,r,s){return o?await async function(t,e,a,i,o,l,r){const s=c[a],d=s.F.n8,u=2*s.F.n8,g=[];await Da(e,i);const f=Math.floor((1<<24)/u);y[i]=e.pos;for(let a=0;a=a&&e=e&&o1?s[s.length-2]:c;const u=s[s.length-1];if(e&&e.debug("Validating contribution #"+s[s.length-1].id),!await dn(n,u,d,e))return!1;const g=ii.exports(64);g.update(u.responseHash),e&&e.debug("Verifying powers in tau*G1 section");const f=await b(2,"G1","tauG1",2**l*2-1,[0,1],e);if(a=await cn(n,f.R1,f.R2,n.G2.g,u.tauG2),!0!==a)return e&&e.error("tauG1 section. Powers do not match"),!1;if(!n.G1.eq(n.G1.g,f.singularPoints[0]))return e&&e.error("First element of tau*G1 section must be the generator"),!1;if(!n.G1.eq(u.tauG1,f.singularPoints[1]))return e&&e.error("Second element of tau*G1 section does not match the one in the contribution section"),!1;e&&e.debug("Verifying powers in tau*G2 section");const h=await b(3,"G2","tauG2",2**l,[0,1],e);if(a=await cn(n,n.G1.g,u.tauG1,h.R1,h.R2),!0!==a)return e&&e.error("tauG2 section. Powers do not match"),!1;if(!n.G2.eq(n.G2.g,h.singularPoints[0]))return e&&e.error("First element of tau*G2 section must be the generator"),!1;if(!n.G2.eq(u.tauG2,h.singularPoints[1]))return e&&e.error("Second element of tau*G2 section does not match the one in the contribution section"),!1;e&&e.debug("Verifying powers in alpha*tau*G1 section");const _=await b(4,"G1","alphatauG1",2**l,[0],e);if(a=await cn(n,_.R1,_.R2,n.G2.g,u.tauG2),!0!==a)return e&&e.error("alphaTauG1 section. Powers do not match"),!1;if(!n.G1.eq(u.alphaG1,_.singularPoints[0]))return e&&e.error("First element of alpha*tau*G1 section (alpha*G1) does not match the one in the contribution section"),!1;e&&e.debug("Verifying powers in beta*tau*G1 section");const p=await b(5,"G1","betatauG1",2**l,[0],e);if(a=await cn(n,p.R1,p.R2,n.G2.g,u.tauG2),!0!==a)return e&&e.error("betaTauG1 section. Powers do not match"),!1;if(!n.G1.eq(u.betaG1,p.singularPoints[0]))return e&&e.error("First element of beta*tau*G1 section (beta*G1) does not match the one in the contribution section"),!1;const m=await async function(t){const e=n.G2,a=2*e.F.n8,l=new Uint8Array(a);if(!o[6])throw t.error("File has no BetaG2 section"),new Error("File has no BetaG2 section");if(o[6].length>1)throw t.error("File has no BetaG2 section"),new Error("File has more than one GetaG2 section");i.pos=o[6][0].p;const r=await i.read(a),s=e.fromRprLEM(r);return e.toRprUncompressed(l,0,s),g.update(l),s}(e);if(!n.G2.eq(u.betaG2,m))return e&&e.error("betaG2 element in betaG2 section does not match the one in the contribution section"),!1;const w=g.digest();if(l==r&&!ki(w,u.nextChallenge))return e&&e.error("Hash of the values does not match the next challenge of the last contributor in the contributions section"),!1;e&&e.info(Mi(w,"Next challenge hash: ")),L(u,d);for(let t=s.length-2;t>=0;t--){const a=s[t],i=t>0?s[t-1]:c;if(!await dn(n,a,i,e))return!1;L(a,i)}if(e&&e.info("-----------------------------------------------------"),o[12]&&o[13]&&o[14]&&o[15]){let t;if(t=await A("G1",2,12,"tauG1",e),!t)return!1;if(t=await A("G2",3,13,"tauG2",e),!t)return!1;if(t=await A("G1",4,14,"alphaTauG1",e),!t)return!1;if(t=await A("G1",5,15,"betaTauG1",e),!t)return!1}else e&&e.warn('this file does not contain phase2 precalculated values. Please run: \n snarkjs "powersoftau preparephase2" to prepare this file to be used in the phase2 ceremony.');return await i.close(),e&&e.info("Powers of Tau Ok!"),!0;function L(t,a){if(!e)return;e.info("-----------------------------------------------------"),e.info(`Contribution #${t.id}: ${t.name||""}`),e.info(Mi(t.nextChallenge,"Next Challenge: "));const i=new Uint8Array(2*n.G1.F.n8*6+2*n.G2.F.n8*3);en(i,0,n,t.key,!1);const o=ii.exports(64);o.setPartialHash(t.partialHash),o.update(i);const l=o.digest();e.info(Mi(l,"Response Hash:")),e.info(Mi(a.nextChallenge,"Response Hash:")),1==t.type&&(e.info(`Beacon generator: ${Zi(t.beaconHash)}`),e.info(`Beacon iterations Exp: ${t.numIterationsExp}`))}async function b(t,e,a,l,r,s){const c=n[e],d=2*c.F.n8;await $a(i,o,t);const u=[];let f=c.zero,h=c.zero,_=c.zero;for(let t=0;t0){const t=c.fromRprLEM(o,0),e=Vi(Ni(4),0);f=c.add(f,c.timesScalar(_,e)),h=c.add(h,c.timesScalar(t,e))}const m=await c.multiExpAffine(o.slice(0,(e-1)*d),p),w=await c.multiExpAffine(o.slice(d),p);f=c.add(f,m),h=c.add(h,w),_=c.fromRprLEM(o,(e-1)*d);for(let a=0;a=t&&i1;)c/=2,d+=1;if(2**d!=s)throw new Error("Invalid file size");o&&o.debug("Power to tau size: "+d);const u=await Ki(i),g=await qa(a),f=ii.exports(64);for(let t=0;t{o.debug(e+".g1_s: "+t.G1.toString(p[e].g1_s,16)),o.debug(e+".g1_sx: "+t.G1.toString(p[e].g1_sx,16)),o.debug(e+".g2_sp: "+t.G2.toString(p[e].g2_sp,16)),o.debug(e+".g2_spx: "+t.G2.toString(p[e].g2_spx,16)),o.debug("")}));const m=ii.exports(64);await g.write(_),m.update(_),await gn(n,g,m,t,"G1",2**d*2-1,t.Fr.one,p.tau.prvKey,"COMPRESSED","tauG1",o),await gn(n,g,m,t,"G2",2**d,t.Fr.one,p.tau.prvKey,"COMPRESSED","tauG2",o),await gn(n,g,m,t,"G1",2**d,p.alpha.prvKey,p.tau.prvKey,"COMPRESSED","alphaTauG1",o),await gn(n,g,m,t,"G1",2**d,p.beta.prvKey,p.tau.prvKey,"COMPRESSED","betaTauG1",o),await gn(n,g,m,t,"G2",1,p.beta.prvKey,p.tau.prvKey,"COMPRESSED","betaTauG2",o);const w=new Uint8Array(2*t.F1.n8*6+2*t.F2.n8*3);en(w,0,t,p,!1),await g.write(w),m.update(w);const L=m.digest();o&&o.info(Mi(L,"Contribution Response Hash: ")),await g.close(),await n.close()},beacon:async function(t,e,a,i,o,n){const l=Hi(i);if(0==l.byteLength||2*l.byteLength!=i.length)return n&&n.error("Invalid Beacon Hash. (It must be a valid hexadecimal sequence)"),!1;if(l.length>=256)return n&&n.error("Maximum lenght of beacon hash is 255 bytes"),!1;if((o=parseInt(o))<10||o>63)return n&&n.error("Invalid numIterationsExp. (Must be between 10 and 63)"),!1;await ii.exports.ready();const{fd:r,sections:s}=await ka(t,"ptau",1),{curve:c,power:d,ceremonyPower:u}=await Xo(r,s);if(d!=u)return n&&n.error("This file has been reduced. You cannot contribute into a reduced file."),!1;s[12]&&n&&n.warn("Contributing into a file that has phase2 calculated. You will have to prepare phase2 again.");const g=await on(r,c,s),f={name:a,type:1,numIterationsExp:o,beaconHash:l};let h;h=g.length>0?g[g.length-1].nextChallenge:rn(c,d,n),f.key=await sn(c,h,l,o);const _=new ii.exports(64);_.update(h);const p=await Ra(e,"ptau",1,7);await Jo(p,c,d);const m=[];let w;w=await y(2,"G1",2**d*2-1,c.Fr.e(1),f.key.tau.prvKey,"tauG1",n),f.tauG1=w[1],w=await y(3,"G2",2**d,c.Fr.e(1),f.key.tau.prvKey,"tauG2",n),f.tauG2=w[1],w=await y(4,"G1",2**d,f.key.alpha.prvKey,f.key.tau.prvKey,"alphaTauG1",n),f.alphaG1=w[0],w=await y(5,"G1",2**d,f.key.beta.prvKey,f.key.tau.prvKey,"betaTauG1",n),f.betaG1=w[0],w=await y(6,"G2",1,f.key.beta.prvKey,f.key.tau.prvKey,"betaTauG2",n),f.betaG2=w[0],f.partialHash=_.getPartialHash();const L=new Uint8Array(2*c.F1.n8*6+2*c.F2.n8*3);en(L,0,c,f.key,!1),_.update(new Uint8Array(L));const b=_.digest();n&&n.info(Mi(b,"Contribution Response Hash imported: "));const A=new ii.exports(64);return A.update(b),await C(p,"G1",2,2**d*2-1,"tauG1",n),await C(p,"G2",3,2**d,"tauG2",n),await C(p,"G1",4,2**d,"alphaTauG1",n),await C(p,"G1",5,2**d,"betaTauG1",n),await C(p,"G2",6,1,"betaG2",n),f.nextChallenge=A.digest(),n&&n.info(Mi(f.nextChallenge,"Next Challenge Hash: ")),g.push(f),await ln(p,c,g),await r.close(),await p.close(),b;async function y(t,e,a,i,o,n,l){const d=[];r.pos=s[t][0].p,await Da(p,t),m[t]=p.pos;const u=c[e],g=2*u.F.n8,f=Math.floor((1<<20)/g);let h=i;for(let t=0;t0?d[d.length-1].nextChallenge:rn(r,s,o),u.key=Yo(r,g,f);const h=new ii.exports(64);h.update(g);const _=await Ra(e,"ptau",1,7);await Jo(_,r,s);const p=[];let m;m=await A(2,"G1",2**s*2-1,r.Fr.e(1),u.key.tau.prvKey,"tauG1"),u.tauG1=m[1],m=await A(3,"G2",2**s,r.Fr.e(1),u.key.tau.prvKey,"tauG2"),u.tauG2=m[1],m=await A(4,"G1",2**s,u.key.alpha.prvKey,u.key.tau.prvKey,"alphaTauG1"),u.alphaG1=m[0],m=await A(5,"G1",2**s,u.key.beta.prvKey,u.key.tau.prvKey,"betaTauG1"),u.betaG1=m[0],m=await A(6,"G2",1,u.key.beta.prvKey,u.key.tau.prvKey,"betaTauG2"),u.betaG2=m[0],u.partialHash=h.getPartialHash();const w=new Uint8Array(2*r.F1.n8*6+2*r.F2.n8*3);en(w,0,r,u.key,!1),h.update(new Uint8Array(w));const L=h.digest();o&&o.info(Mi(L,"Contribution Response Hash imported: "));const b=new ii.exports(64);return b.update(L),await y(_,"G1",2,2**s*2-1,"tauG1"),await y(_,"G2",3,2**s,"tauG2"),await y(_,"G1",4,2**s,"alphaTauG1"),await y(_,"G1",5,2**s,"betaTauG1"),await y(_,"G2",6,1,"betaG2"),u.nextChallenge=b.digest(),o&&o.info(Mi(u.nextChallenge,"Next Challenge Hash: ")),d.push(u),await ln(_,r,d),await n.close(),await _.close(),L;async function A(t,e,a,i,s,c){const d=[];n.pos=l[t][0].p,await Da(_,t),p[t]=_.pos;const u=r[e],g=2*u.F.n8,f=Math.floor((1<<20)/g);let m=i;for(let t=0;t=this.length&&(this.length=t+1),!0}getKeys(){const t=new mn;for(let e=0;e1<<20?new mn:[];for(let t=0;t1<<20?new mn:[];for(let t=0;t1<<20?new mn:[];for(let t=0;t{let i="";return Object.keys(a).forEach((o=>{let n=e.varIdx2Name[o];"one"==n&&(n="1");let l=t.curve.Fr.toString(a[o]);"1"==l&&(l=""),"-1"==l&&(l="-"),""!=i&&"-"!=l[0]&&(l="+"+l),""!=i&&(l=" "+l),i=i+l+n})),i},n=`[ ${o(i[0])} ] * [ ${o(i[1])} ] - [ ${o(i[2])} ] = 0`;a&&a.info(n)}},info:async function(t,e){const a=await yn(t);return pa.eq(a.prime,In)?e&&e.info("Curve: bn-128"):pa.eq(a.prime,Cn)?e&&e.info("Curve: bls12-381"):e&&e.info(`Unknown Curve. Prime: ${pa.toString(a.prime)}`),e&&e.info(`# of Wires: ${a.nVars}`),e&&e.info(`# of Constraints: ${a.nConstraints}`),e&&e.info(`# of Private Inputs: ${a.nPrvInputs}`),e&&e.info(`# of Public Inputs: ${a.nPubInputs}`),e&&e.info(`# of Labels: ${a.nLabels}`),e&&e.info(`# of Outputs: ${a.nOutputs}`),a},exportJson:async function(t,e){const a=await yn(t,!0,!0,!0,e),i=a.curve.Fr;return delete a.curve,delete a.F,Wi(i,a)}});async function xn(t){const e={labelIdx2Name:["one"],varIdx2Name:["one"],componentIdx2Name:[]},a=await Ma(t),i=await a.read(a.totalSize),o=new TextDecoder("utf-8").decode(i).split("\n");for(let t=0;t Reading r1cs file");const{fd:i,sections:o}=await ka(t,"r1cs",1),n=await An(i,o,{loadConstraints:!1,loadCustomGates:!1});a&&a.info("> Reading witness file");const{fd:l,sections:r}=await ka(e,"wtns",2),s=await Go(l,r);if(!pa.eq(n.prime,s.q))throw new Error("Curve of the witness does not match the curve of the proving key");const c=await Za(l,r,2);await l.close();const d=(await async function(t){let e;if(pa.eq(t,Ja))e=await ga();else{if(!pa.eq(t,Ya))throw new Error(`Curve not supported: ${pa.toString(t)}`);e=await fa()}return e}(n.prime)).Fr,u=d.n8,g=await Za(i,o,2);a&&(a.info("----------------------------"),a.info(" WITNESS CHECK"),a.info(` Curve: ${n.curve.name}`),a.info(` Vars (wires): ${n.nVars}`),a.info(` Ouputs: ${n.nOutputs}`),a.info(` Public Inputs: ${n.nPubInputs}`),a.info(` Private Inputs: ${n.nPrvInputs}`),a.info(` Labels: ${n.nLabels}`),a.info(` Constraints: ${n.nConstraints}`),a.info(` Custom Gates: ${n.useCustomGates}`),a.info("----------------------------")),a&&a.info("> Checking witness correctness");let f=0,h=!0;for(let t=0;t{const i=function(t){return d.fromRprLE(c.slice(t*u,t*u+u))}(a),o=t[a];e=d.add(e,d.mul(i,o))})),e}function p(){const t={},e=g.slice(f,f+4);f+=4;const a=new DataView(e.buffer).getUint32(0,!0),i=g.slice(f,f+(4+n.n8)*a);f+=(4+n.n8)*a;const o=new DataView(i.buffer);for(let e=0;e=this.length&&(this.length=t+1),!0}getKeys(){const t=new Gn;for(let e=0;eg)return i&&i.error(`circuit too big for this power of tau ceremony. ${_.nConstraints}*2 > 2**${g}`),-1;if(!d[12])return i&&i.error("Powers of tau is not prepared."),-1;const b=_.nOutputs+_.nPubInputs,A=2**L;await Da(p,1),await p.writeULE32(1),await Na(p),await Da(p,2);const y=u.q,C=8*(Math.floor((pa.bitLength(y)-1)/64)+1),I=u.r,F=8*(Math.floor((pa.bitLength(I)-1)/64)+1),x=pa.mod(pa.shl(1,8*F),I),E=u.Fr.e(pa.mod(pa.mul(x,x),I));let v,B,S;await p.writeULE32(C),await Ka(p,y,C),await p.writeULE32(F),await Ka(p,I,F),await p.writeULE32(_.nVars),await p.writeULE32(b),await p.writeULE32(A),v=await c.read(m,d[4][0].p),await p.write(v),v=await u.G1.batchLEMtoU(v),s.update(v),B=await c.read(m,d[5][0].p),await p.write(B),B=await u.G1.batchLEMtoU(B),s.update(B),S=await c.read(w,d[6][0].p),await p.write(S),S=await u.G2.batchLEMtoU(S),s.update(S);const P=new Uint8Array(m);u.G1.toRprLEM(P,0,u.G1.g);const G=new Uint8Array(w);u.G2.toRprLEM(G,0,u.G2.g);const O=new Uint8Array(m);u.G1.toRprUncompressed(O,0,u.G1.g);const T=new Uint8Array(w);u.G2.toRprUncompressed(T,0,u.G2.g),await p.write(G),await p.write(P),await p.write(G),s.update(T),s.update(O),s.update(T),await Na(p),i&&i.info("Reading r1cs");let z=await Za(f,h,2);const U=new Gn(_.nVars),Q=new Gn(_.nVars),q=new Gn(_.nVars),M=new Gn(_.nVars-b-1),k=new Array(b+1);i&&i.info("Reading tauG1");let R=await Za(c,d,12,(A-1)*m,A*m);i&&i.info("Reading tauG2");let D=await Za(c,d,13,(A-1)*w,A*w);i&&i.info("Reading alphatauG1");let N=await Za(c,d,14,(A-1)*m,A*m);i&&i.info("Reading betatauG1");let $=await Za(c,d,15,(A-1)*m,A*m);await async function(){const t=new Uint8Array(12+u.Fr.n8),e=new DataView(t.buffer),a=new Uint8Array(u.Fr.n8);u.Fr.toRprLE(a,0,u.Fr.e(1));let s=0;function c(){const t=z.slice(s,s+4);s+=4;return new DataView(t.buffer).getUint32(0,!0)}const d=new Gn;for(let t=0;t<_.nConstraints;t++){i&&t%1e4==0&&i.debug(`processing constraints: ${t}/${_.nConstraints}`);const e=c();for(let a=0;a=0?u.Fr.fromRprLE(z.slice(i[3],i[3]+u.Fr.n8),0):u.Fr.fromRprLE(a,0);const n=u.Fr.mul(o,E);u.Fr.toRprLE(t,12,n),g.set(t,h),h+=t.length}await p.write(g),await Na(p)}(),await K(3,"G1",k,"IC"),await async function(){await Da(p,9);const t=new Ge(A*m);if(L(i&&i.debug(`Writing points end ${o}: ${d}/${a.length}`),t)))),r+=n,t++}const c=await Promise.all(l);for(let t=0;t32768?(f=new Ge(_*n),h=new Ge(_*u.Fr.n8)):(f=new Uint8Array(_*n),h=new Uint8Array(_*u.Fr.n8));let p=0,m=0;const w=[R,D,N,$],L=new Uint8Array(u.Fr.n8);u.Fr.toRprLE(L,0,u.Fr.e(1));let b=0;for(let t=0;t=0?h.set(z.slice(e[t][o][2],e[t][o][2]+u.Fr.n8),b*u.Fr.n8):h.set(L,b*u.Fr.n8),b++;if(e.length>1){const t=[];t.push({cmd:"ALLOCSET",var:0,buff:f}),t.push({cmd:"ALLOCSET",var:1,buff:h}),t.push({cmd:"ALLOC",var:2,len:e.length*l}),p=0,m=0;let a=0;for(let i=0;i=0;t--){const e=d.contributions[t];i&&i.info("-------------------------"),i&&i.info(Mi(e.contributionHash,`contribution #${t+1} ${e.name?e.name:""}:`)),1==e.type&&(i&&i.info(`Beacon generator: ${Zi(e.beaconHash)}`),i&&i.info(`Beacon iterations Exp: ${e.numIterationsExp}`))}return i&&i.info("-------------------------"),i&&i.info("ZKey Ok!"),!0;async function w(t,e){const a=2*s.G1.F.n8,i=t.byteLength/a,o=s.tm.concurrency,n=Math.floor(i/o),l=[];for(let a=0;a Detected protocol: "+o.protocol),"groth16"===o.protocol)n=await async function(t,e,a){const i=await ei(t.q),o=2*i.G1.F.n8,n=await i.pairing(t.vk_alpha_1,t.vk_beta_2);let l={protocol:t.protocol,curve:i.name,nPublic:t.nPublic,vk_alpha_1:i.G1.toObject(t.vk_alpha_1),vk_beta_2:i.G2.toObject(t.vk_beta_2),vk_gamma_2:i.G2.toObject(t.vk_gamma_2),vk_delta_2:i.G2.toObject(t.vk_delta_2),vk_alphabeta_12:i.Gt.toObject(n)};await $a(e,a,3),l.IC=[];for(let a=0;a<=t.nPublic;a++){const t=await e.read(o),a=i.G1.toObject(t);l.IC.push(a)}return await Va(e),l=Un(l),l}(o,a,i);else if("plonk"===o.protocol)n=await async function(t){const e=await ei(t.q);let a={protocol:t.protocol,curve:e.name,nPublic:t.nPublic,power:t.power,k1:e.Fr.toObject(t.k1),k2:e.Fr.toObject(t.k2),Qm:e.G1.toObject(t.Qm),Ql:e.G1.toObject(t.Ql),Qr:e.G1.toObject(t.Qr),Qo:e.G1.toObject(t.Qo),Qc:e.G1.toObject(t.Qc),S1:e.G1.toObject(t.S1),S2:e.G1.toObject(t.S2),S3:e.G1.toObject(t.S3),X_2:e.G2.toObject(t.X_2),w:e.Fr.toObject(e.Fr.w[t.power])};return a=Un(a),a}(o);else{if(!o.protocolId||o.protocolId!==to)throw new Error("zkey file protocol unrecognized");n=await async function(t,e){const a=await ei(t.q);let i={protocol:t.protocol,curve:a.name,nPublic:t.nPublic,power:t.power,k1:a.Fr.toObject(t.k1),k2:a.Fr.toObject(t.k2),w:a.Fr.toObject(a.Fr.w[t.power]),w3:a.Fr.toObject(t.w3),w4:a.Fr.toObject(t.w4),w8:a.Fr.toObject(t.w8),wr:a.Fr.toObject(t.wr),X_2:a.G2.toObject(t.X_2),C0:a.G1.toObject(t.C0)};return Un(i)}(o)}return await a.close(),e&&e.info("EXPORT VERIFICATION KEY FINISHED"),n}var qn={};const{unstringifyBigInts:Mn,stringifyBigInts:kn}=ma;async function Rn(t,e,a){a&&a.info("FFLONK EXPORT SOLIDITY VERIFIER STARTED");const i=await ai(t.curve);let o=c(t.w3);t.w3_2=d(i.Fr.square(o));let n=c(t.w4);t.w4_2=d(i.Fr.square(n)),t.w4_3=d(i.Fr.mul(i.Fr.square(n),n));let l=c(t.w8),r=i.Fr.one;for(let e=1;e<8;e++)r=i.Fr.mul(r,l),t["w8_"+e]=d(r);let s=e[t.protocol];return a&&a.info("FFLONK EXPORT SOLIDITY VERIFIER FINISHED"),qn.render(s,t);function c(t){const e=Mn(t);return i.Fr.fromObject(e)}function d(t){const e=i.Fr.toObject(t);return kn(e)}}var Dn=Object.freeze({__proto__:null,newZKey:On,exportBellman:async function(t,e,a){const{fd:i,sections:o}=await ka(t,"zkey",2),n=await Fo(i,o);if("groth16"!=n.protocol)throw new Error("zkey file is not groth16");const l=await ei(n.q),r=2*l.G1.F.n8,s=2*l.G2.F.n8,c=await Eo(i,l,o),d=await qa(e);let u;await w(n.vk_alpha_1),await w(n.vk_beta_1),await L(n.vk_beta_2),await L(n.vk_gamma_2),await w(n.vk_delta_1),await L(n.vk_delta_2),u=await Za(i,o,3),u=await l.G1.batchLEMtoU(u),await b("G1",u);const g=await Za(i,o,9);let f,h,_,p,m;f=await l.G1.fft(g,"affine","jacobian",a),f=await l.G1.batchApplyKey(f,l.Fr.neg(l.Fr.e(2)),l.Fr.w[n.power+1],"jacobian","affine",a),f=f.slice(0,f.byteLength-r),f=await l.G1.batchLEMtoU(f),await b("G1",f),h=await Za(i,o,8),h=await l.G1.batchLEMtoU(h),await b("G1",h),_=await Za(i,o,5),_=await l.G1.batchLEMtoU(_),await b("G1",_),p=await Za(i,o,6),p=await l.G1.batchLEMtoU(p),await b("G1",p),m=await Za(i,o,7),m=await l.G2.batchLEMtoU(m),await b("G2",m),await d.write(c.csHash),await async function(t){const e=new Uint8Array(4);new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(0,t,!1),await d.write(e)}(c.contributions.length);for(let t=0;tg.contributions.length)return o&&o.error("The impoerted file does not include new contributions"),!1;for(let t=0;t=256)return n&&n.error("Maximum lenght of beacon hash is 255 bytes"),!1;if((o=parseInt(o))<10||o>63)return n&&n.error("Invalid numIterationsExp. (Must be between 10 and 63)"),!1;const{fd:r,sections:s}=await ka(t,"zkey",2),c=await Fo(r,s);if("groth16"!=c.protocol)throw new Error("zkey file is not groth16");const d=await ei(c.q),u=await Eo(r,d,s),g=await Ra(e,"zkey",1,10),f=await ji(l,o),h=ii.exports(64);h.update(u.csHash);for(let t=0;t{const i=this.curve.G1.toObject(this.polynomials[a]);t?e.polynomials[a]=i:e[a]=i})),Object.keys(this.evaluations).forEach((a=>{const i=this.curve.Fr.toObject(this.evaluations[a]);t?e.evaluations[a]=i:e[a]=i})),e}fromObjectProof(t){this.resetProof(),Object.keys(t.polynomials).forEach((e=>{this.polynomials[e]=this.curve.G1.fromObject(t.polynomials[e])})),Object.keys(t.evaluations).forEach((e=>{this.evaluations[e]=this.curve.Fr.fromObject(t.evaluations[e])}))}}var $n,Vn={exports:{}}; +var snarkjs=function(t){"use strict";const a=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4];function e(t,a){return a&&10!=a?16==a?"0x"==t.slice(0,2)?BigInt(t):BigInt("0x"+t):void 0:BigInt(t)}const o=e;function i(t){const e=t.toString(16);return 4*(e.length-1)+a[parseInt(e[0],16)]}function n(t){return BigInt(t)>BigInt(a)}const r=c,d=s;function u(t){return(BigInt(t)&BigInt(1))==BigInt(1)}function _(t){let a=BigInt(t);const e=[];for(;a;)a&BigInt(1)?e.push(1):e.push(0),a>>=BigInt(1);return e}function g(t){if(t>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Number too big");return Number(t)}function f(t,a){return BigInt(t)+BigInt(a)}function p(t,a){return BigInt(t)-BigInt(a)}function h(t){return-BigInt(t)}function m(t,a){return BigInt(t)*BigInt(a)}function L(t,a){return BigInt(t)**BigInt(a)}function b(t,a){return BigInt(t)/BigInt(a)}function w(t,a){return BigInt(t)%BigInt(a)}function y(t,a){return BigInt(t)==BigInt(a)}function A(t,a){return BigInt(t)>BigInt(a)}function C(t,a){return BigInt(t)>=BigInt(a)}function x(t,a){return BigInt(t)&BigInt(a)}function F(t,a,e,o){const i="0000000"+e.toString(16),n=new Uint32Array(t.buffer,t.byteOffset+a,o/4),l=1+(4*(i.length-7)-1>>5);for(let t=0;t>5);for(let t=0;tn[n.length-a-1]=t.toString(16).padStart(8,"0"))),e(n.join(""),16)}function E(t,a,o){o=o||t.byteLength,a=a||0;const i=new DataView(t.buffer,t.byteOffset+a,o),n=new Array(o/4);for(let t=0;t>=BigInt(1)}return e},bits:_,toNumber:g,toArray:function(t,a){const e=[];let o=BigInt(t);for(a=BigInt(a);o;)e.unshift(Number(o%a)),o/=a;return e},add:f,sub:p,neg:h,mul:m,square:function(t){return BigInt(t)*BigInt(t)},pow:L,exp:function(t,a){return BigInt(t)**BigInt(a)},abs:function(t){return BigInt(t)>=0?BigInt(t):-BigInt(t)},div:b,mod:w,eq:y,neq:function(t,a){return BigInt(t)!=BigInt(a)},lt:function(t,a){return BigInt(t)=0;e--)i=t.square(i),o[e]&&(i=t.mul(i,a));return i}function z(t){if(t.m%2==1)if(y(w(t.p,4),1))if(y(w(t.p,8),1))if(y(w(t.p,16),1))!function(t){t.sqrt_q=L(t.p,t.m),t.sqrt_s=0,t.sqrt_t=p(t.sqrt_q,1);for(;!u(t.sqrt_t);)t.sqrt_s=t.sqrt_s+1,t.sqrt_t=b(t.sqrt_t,2);let a=t.one;for(;t.eq(a,t.one);){const e=t.random();t.sqrt_z=t.pow(e,t.sqrt_t),a=t.pow(t.sqrt_z,2**(t.sqrt_s-1))}t.sqrt_tm1d2=b(p(t.sqrt_t,1),2),t.sqrt=function(t){const a=this;if(a.isZero(t))return a.zero;let e=a.pow(t,a.sqrt_tm1d2);const o=a.pow(a.mul(a.square(e),t),2**(a.sqrt_s-1));if(a.eq(o,a.negone))return null;let i=a.sqrt_s,n=a.mul(t,e),l=a.mul(n,e),c=a.sqrt_z;for(;!a.eq(l,a.one);){let t=a.square(l),o=1;for(;!a.eq(t,a.one);)t=a.square(t),o++;e=c;for(let t=0;t>>0,t[i]=(t[i]^t[a])>>>0,t[i]=(t[i]<<16|t[i]>>>16&65535)>>>0,t[o]=t[o]+t[i]>>>0,t[e]=(t[e]^t[o])>>>0,t[e]=(t[e]<<12|t[e]>>>20&4095)>>>0,t[a]=t[a]+t[e]>>>0,t[i]=(t[i]^t[a])>>>0,t[i]=(t[i]<<8|t[i]>>>24&255)>>>0,t[o]=t[o]+t[i]>>>0,t[e]=(t[e]^t[o])>>>0,t[e]=(t[e]<<7|t[e]>>>25&127)>>>0}class M{constructor(t){t=t||[0,0,0,0,0,0,0,0],this.state=[1634760805,857760878,2036477234,1797285236,t[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 f(m(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 a=0;a<10;a++)T(t=this.buff,0,4,8,12),T(t,1,5,9,13),T(t,2,6,10,14),T(t,3,7,11,15),T(t,0,5,10,15),T(t,1,6,11,12),T(t,2,7,8,13),T(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)))}}var U={};function Q(t){let a=new Uint8Array(t);if(void 0!==globalThis.crypto)globalThis.crypto.getRandomValues(a);else for(let e=0;e>>0;return a}let k=null;function R(){return k||(k=new M(function(){const t=Q(32),a=new Uint32Array(t.buffer),e=[];for(let t=0;t<8;t++)e.push(a[t]);return e}()),k)}class N{constructor(t,a,e){this.F=a,this.G=t,this.opMulGF=e;let o=a.sqrt_t||a.t,i=a.sqrt_s||a.s,n=a.one;for(;a.eq(a.pow(n,a.half),a.one);)n=a.add(n,a.one);this.w=new Array(i+1),this.wi=new Array(i+1),this.w[i]=this.F.pow(n,o),this.wi[i]=this.F.inv(this.w[i]);let l=i-1;for(;l>=0;)this.w[l]=this.F.square(this.w[l+1]),this.wi[l]=this.F.square(this.wi[l+1]),l--;this.roots=[],this._setRoots(Math.min(i,15))}_setRoots(t){for(let a=t;a>=0&&!this.roots[a];a--){let t=this.F.one;const e=1<>1,c=$(t,a,e-1,o,2*i),s=$(t,a,e-1,o+i,2*i),r=new Array(n);for(let a=0;a>this.one,this.bitLength=i(this.p),this.mask=(this.one<>this.one;this.nqr=this.two;let e=this.pow(this.nqr,a);for(;!this.eq(e,this.negone);)this.nqr=this.nqr+this.one,e=this.pow(this.nqr,a);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),z(this),this.FFT=new N(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,a){let e;if(a?16==a&&(e=BigInt("0x"+t)):e=BigInt(t),e<0){let t=-e;return t>=this.p&&(t%=this.p),this.p-t}return e>=this.p?e%this.p:e}add(t,a){const e=t+a;return e>=this.p?e-this.p:e}sub(t,a){return t>=a?t-a:this.p-a+t}neg(t){return t?this.p-t:t}mul(t,a){return t*a%this.p}mulScalar(t,a){return t*this.e(a)%this.p}square(t){return t*t%this.p}eq(t,a){return t==a}neq(t,a){return t!=a}lt(t,a){return(t>this.half?t-this.p:t)<(a>this.half?a-this.p:a)}gt(t,a){return(t>this.half?t-this.p:t)>(a>this.half?a-this.p:a)}leq(t,a){return(t>this.half?t-this.p:t)<=(a>this.half?a-this.p:a)}geq(t,a){return(t>this.half?t-this.p:t)>=(a>this.half?a-this.p:a)}div(t,a){return this.mul(t,this.inv(a))}idiv(t,a){if(!a)throw new Error("Division by zero");return t/a}inv(t){if(!t)throw new Error("Division by zero");let a=this.zero,e=this.p,o=this.one,i=t%this.p;for(;i;){let t=e/i;[a,o]=[o,a-t*o],[e,i]=[i,e-t*i]}return a=this.p?e-this.p:e}bor(t,a){const e=(t|a)&this.mask;return e>=this.p?e-this.p:e}bxor(t,a){const e=(t^a)&this.mask;return e>=this.p?e-this.p:e}bnot(t){const a=t^this.mask;return a>=this.p?a-this.p:a}shl(t,a){if(Number(a)=this.p?e-this.p:e}{const e=this.p-a;return Number(e)>e:this.zero}}shr(t,a){if(Number(a)>a;{const e=this.p-a;if(Number(e)=this.p?a-this.p:a}return 0}}land(t,a){return t&&a?this.one:this.zero}lor(t,a){return t||a?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 a=this.s,e=this.nqr_to_t,o=this.pow(t,this.t),i=this.pow(t,this.add(this.t,this.one)>>this.one);for(;o!=this.one;){let t=this.square(o),n=1;for(;t!=this.one;)n++,t=this.square(t);let l=e;for(let t=0;tthis.p>>this.one&&(i=this.neg(i)),i}normalize(t,a){if((t=BigInt(t,a))<0){let a=-t;return a>=this.p&&(a%=this.p),this.p-a}return t>=this.p?t%this.p:t}random(){const t=2*this.bitLength/8;let a=this.zero;for(let e=0;ethis.half&&10==a){e="-"+(this.p-t).toString(a)}else e=t.toString(a);return e}isZero(t){return t==this.zero}fromRng(t){let a;do{a=this.zero;for(let e=0;e=this.p);return a=a*this.Ri%this.p,a}fft(t){return this.FFT.fft(t)}ifft(t){return this.FFT.ifft(t)}toRprLE(t,a,e){F(t,a,e,8*this.n64)}toRprBE(t,a,e){I(t,a,e,8*this.n64)}toRprBEM(t,a,e){return this.toRprBE(t,a,this.mul(this.R,e))}toRprLEM(t,a,e){return this.toRprLE(t,a,this.mul(this.R,e))}fromRprLE(t,a){return B(t,a,this.n8)}fromRprBE(t,a){return E(t,a,this.n8)}fromRprLEM(t,a){return this.mul(this.fromRprLE(t,a),this.Ri)}fromRprBEM(t,a){return this.mul(this.fromRprBE(t,a),this.Ri)}toObject(t){return t}}var V="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},K={bigInt2BytesLE:function(t,a){const e=Array(a);let o=BigInt(t);for(let t=0;t>=8n;return e},bigInt2U32LE:function(t,a){const e=Array(a);let o=BigInt(t);for(let t=0;t>=32n;return e},isOcamNum:function(t){return!!Array.isArray(t)&&(3==t.length&&("number"==typeof t[0]&&("number"==typeof t[1]&&!!Array.isArray(t[2]))))}},H=function(t,a,e,o,i,n,l){const c=t.addFunction(a);c.addParam("base","i32"),c.addParam("scalar","i32"),c.addParam("scalarLength","i32"),c.addParam("r","i32"),c.addLocal("i","i32"),c.addLocal("b","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(e));c.addCode(s.if(s.i32_eqz(s.getLocal("scalarLength")),[...s.call(l,s.getLocal("r")),...s.ret([])])),c.addCode(s.call(n,s.getLocal("base"),r)),c.addCode(s.call(l,s.getLocal("r"))),c.addCode(s.setLocal("i",s.getLocal("scalarLength"))),c.addCode(s.block(s.loop(s.setLocal("i",s.i32_sub(s.getLocal("i"),s.i32_const(1))),s.setLocal("b",s.i32_load8_u(s.i32_add(s.getLocal("scalar"),s.getLocal("i")))),...function(){const t=[];for(let a=0;a<8;a++)t.push(...s.call(i,s.getLocal("r"),s.getLocal("r")),...s.if(s.i32_ge_u(s.getLocal("b"),s.i32_const(128>>a)),[...s.setLocal("b",s.i32_sub(s.getLocal("b"),s.i32_const(128>>a))),...s.call(o,s.getLocal("r"),r,s.getLocal("r"))]));return t}(),s.br_if(1,s.i32_eqz(s.getLocal("i"))),s.br(0))))},Z=function(t,a){const e=8*t.modules[a].n64,o=t.addFunction(a+"_batchInverse");o.addParam("pIn","i32"),o.addParam("inStep","i32"),o.addParam("n","i32"),o.addParam("pOut","i32"),o.addParam("outStep","i32"),o.addLocal("itAux","i32"),o.addLocal("itIn","i32"),o.addLocal("itOut","i32"),o.addLocal("i","i32");const i=o.getCodeBuilder(),n=i.i32_const(t.alloc(e));o.addCode(i.setLocal("itAux",i.i32_load(i.i32_const(0))),i.i32_store(i.i32_const(0),i.i32_add(i.getLocal("itAux"),i.i32_mul(i.i32_add(i.getLocal("n"),i.i32_const(1)),i.i32_const(e))))),o.addCode(i.call(a+"_one",i.getLocal("itAux")),i.setLocal("itIn",i.getLocal("pIn")),i.setLocal("itAux",i.i32_add(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("i",i.i32_const(0)),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("i"),i.getLocal("n"))),i.if(i.call(a+"_isZero",i.getLocal("itIn")),i.call(a+"_copy",i.i32_sub(i.getLocal("itAux"),i.i32_const(e)),i.getLocal("itAux")),i.call(a+"_mul",i.getLocal("itIn"),i.i32_sub(i.getLocal("itAux"),i.i32_const(e)),i.getLocal("itAux"))),i.setLocal("itIn",i.i32_add(i.getLocal("itIn"),i.getLocal("inStep"))),i.setLocal("itAux",i.i32_add(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("i",i.i32_add(i.getLocal("i"),i.i32_const(1))),i.br(0))),i.setLocal("itIn",i.i32_sub(i.getLocal("itIn"),i.getLocal("inStep"))),i.setLocal("itAux",i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("itOut",i.i32_add(i.getLocal("pOut"),i.i32_mul(i.i32_sub(i.getLocal("n"),i.i32_const(1)),i.getLocal("outStep")))),i.call(a+"_inverse",i.getLocal("itAux"),i.getLocal("itAux")),i.block(i.loop(i.br_if(1,i.i32_eqz(i.getLocal("i"))),i.if(i.call(a+"_isZero",i.getLocal("itIn")),[...i.call(a+"_copy",i.getLocal("itAux"),i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),...i.call(a+"_zero",i.getLocal("itOut"))],[...i.call(a+"_copy",i.i32_sub(i.getLocal("itAux"),i.i32_const(e)),n),...i.call(a+"_mul",i.getLocal("itAux"),i.getLocal("itIn"),i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),...i.call(a+"_mul",i.getLocal("itAux"),n,i.getLocal("itOut"))]),i.setLocal("itIn",i.i32_sub(i.getLocal("itIn"),i.getLocal("inStep"))),i.setLocal("itOut",i.i32_sub(i.getLocal("itOut"),i.getLocal("outStep"))),i.setLocal("itAux",i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("i",i.i32_sub(i.getLocal("i"),i.i32_const(1))),i.br(0)))),o.addCode(i.i32_store(i.i32_const(0),i.getLocal("itAux")))};var W=function(t,a,e,o,i,n){void 0===n&&(n=oa?1:-1}function tt(t){return t*t}function at(t){return t%2n!==0n}function et(t){return t%2n===0n}function ot(t){return t<0n}function it(t){return t>0n}function nt(t){return ot(t)?t.toString(2).length-1:t.toString(2).length}function lt(t){return t<0n?-t:t}function ct(t){return 1n===lt(t)}function st(t,a){for(var e,o,i,n=0n,l=1n,c=a,s=lt(t);0n!==s;)e=c/s,o=n,i=c,n=l,c=s,l=o-e*l,s=i-e*s;if(!ct(c))throw new Error(t.toString()+" and "+a.toString()+" are not co-prime");return-1===X(n,0n)&&(n+=a),ot(t)?-n:n}function rt(t,a,e){if(0n===e)throw new Error("Cannot take modPow with modulus 0");var o=1n,i=t%e;for(ot(a)&&(a*=-1n,i=st(i,e));it(a);){if(0n===i)return 0n;at(a)&&(o=o*i%e),a/=2n,i=tt(i)%e}return o}function dt(t,a){return 0n!==a&&(!!ct(a)||(0===function(t,a){return(t=t>=0n?t:-t)===(a=a>=0n?a:-a)?0:t>a?1:-1}(a,2n)?et(t):t%a===0n))}function ut(t,a){for(var e,o,i,n=function(t){return t-1n}(t),l=n,c=0;et(l);)l/=2n,c++;t:for(o=0;o>1&&o>1,t>>1)))),a.addCode(e.setLocal(s,e.i64_add(e.getLocal(s),e.i64_shr_u(e.getLocal(c),e.i64_const(32)))))),t>0&&(a.addCode(e.setLocal(c,e.i64_add(e.i64_and(e.getLocal(c),e.i64_const(4294967295)),e.i64_and(e.getLocal(r),e.i64_const(4294967295))))),a.addCode(e.setLocal(s,e.i64_add(e.i64_add(e.getLocal(s),e.i64_shr_u(e.getLocal(c),e.i64_const(32))),e.getLocal(d))))),a.addCode(e.i64_store32(e.getLocal("r"),4*t,e.getLocal(c))),a.addCode(e.setLocal(r,e.getLocal(s)),e.setLocal(d,e.i64_shr_u(e.getLocal(r),e.i64_const(32))))}a.addCode(e.i64_store32(e.getLocal("r"),4*i*2-4,e.getLocal(r)))}(),function(){const a=t.addFunction(o+"_squareOld");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(o+"_mul",e.getLocal("x"),e.getLocal("x"),e.getLocal("r")))}(),function(){!function(){const a=t.addFunction(o+"__mul1");a.addParam("px","i32"),a.addParam("y","i64"),a.addParam("pr","i32"),a.addLocal("c","i64");const e=a.getCodeBuilder();a.addCode(e.setLocal("c",e.i64_mul(e.i64_load32_u(e.getLocal("px"),0,0),e.getLocal("y")))),a.addCode(e.i64_store32(e.getLocal("pr"),0,0,e.getLocal("c")));for(let t=1;t>1n,h=t.alloc(c,gt.bigInt2BytesLE(p,c)),m=p+1n,L=t.alloc(c,gt.bigInt2BytesLE(m,c));t.modules[s]={pq:d,pR2:u,n64:n,q:i,pOne:_,pZero:g,pePlusOne:L};let b=2n;if(yt(i))for(;wt(b,p,i)!==f;)b+=1n;let w=0,y=f;for(;!At(y)&&0n!==y;)w++,y>>=1n;const A=t.alloc(c,gt.bigInt2BytesLE(y,c)),C=wt(b,y,i),x=t.alloc(gt.bigInt2BytesLE((C<>1n,I=t.alloc(c,gt.bigInt2BytesLE(F,c));return t.exportFunction(r+"_copy",s+"_copy"),t.exportFunction(r+"_zero",s+"_zero"),t.exportFunction(r+"_isZero",s+"_isZero"),t.exportFunction(r+"_eq",s+"_eq"),function(){const a=t.addFunction(s+"_isOne");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder();a.addCode(e.ret(e.call(r+"_eq",e.getLocal("x"),e.i32_const(_))))}(),function(){const a=t.addFunction(s+"_add");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.if(e.call(r+"_add",e.getLocal("x"),e.getLocal("y"),e.getLocal("r")),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),e.if(e.call(r+"_gte",e.getLocal("r"),e.i32_const(d)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))))))}(),function(){const a=t.addFunction(s+"_sub");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.if(e.call(r+"_sub",e.getLocal("x"),e.getLocal("y"),e.getLocal("r")),e.drop(e.call(r+"_add",e.getLocal("r"),e.i32_const(d),e.getLocal("r")))))}(),function(){const a=t.addFunction(s+"_neg");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_sub",e.i32_const(g),e.getLocal("x"),e.getLocal("r")))}(),function(){const a=t.alloc(l*l*8),e=t.addFunction(s+"_mReduct");e.addParam("t","i32"),e.addParam("r","i32"),e.addLocal("np32","i64"),e.addLocal("c","i64"),e.addLocal("m","i64");const o=e.getCodeBuilder(),n=Number(0x100000000n-bt(i,0x100000000n));e.addCode(o.setLocal("np32",o.i64_const(n)));for(let t=0;t=l&&a.addCode(e.i64_store32(e.getLocal("r"),4*(t-l),e.getLocal(f))),[f,p]=[p,f],a.addCode(e.setLocal(p,e.i64_shr_u(e.getLocal(f),e.i64_const(32))))}a.addCode(e.i64_store32(e.getLocal("r"),4*l-4,e.getLocal(f))),a.addCode(e.if(e.i32_wrap_i64(e.getLocal(p)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),e.if(e.call(r+"_gte",e.getLocal("r"),e.i32_const(d)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))))))}(),function(){const a=t.addFunction(s+"_square");a.addParam("x","i32"),a.addParam("r","i32"),a.addLocal("c0","i64"),a.addLocal("c1","i64"),a.addLocal("c0_old","i64"),a.addLocal("c1_old","i64"),a.addLocal("np32","i64");for(let t=0;t>1&&o>1,t>>1)))),a.addCode(e.setLocal(f,e.i64_add(e.getLocal(f),e.i64_shr_u(e.getLocal(g),e.i64_const(32)))))),t>0&&(a.addCode(e.setLocal(g,e.i64_add(e.i64_and(e.getLocal(g),e.i64_const(4294967295)),e.i64_and(e.getLocal(p),e.i64_const(4294967295))))),a.addCode(e.setLocal(f,e.i64_add(e.i64_add(e.getLocal(f),e.i64_shr_u(e.getLocal(g),e.i64_const(32))),e.getLocal(h)))));for(let o=Math.max(1,t-l+1);o<=t&&o=l&&a.addCode(e.i64_store32(e.getLocal("r"),4*(t-l),e.getLocal(g))),a.addCode(e.setLocal(p,e.getLocal(f)),e.setLocal(h,e.i64_shr_u(e.getLocal(p),e.i64_const(32))))}a.addCode(e.i64_store32(e.getLocal("r"),4*l-4,e.getLocal(p))),a.addCode(e.if(e.i32_wrap_i64(e.getLocal(h)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),e.if(e.call(r+"_gte",e.getLocal("r"),e.i32_const(d)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))))))}(),function(){const a=t.addFunction(s+"_squareOld");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_mul",e.getLocal("x"),e.getLocal("x"),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_toMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_mul",e.getLocal("x"),e.i32_const(u),e.getLocal("r")))}(),function(){const a=t.alloc(2*c),e=t.addFunction(s+"_fromMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const o=e.getCodeBuilder();e.addCode(o.call(r+"_copy",o.getLocal("x"),o.i32_const(a))),e.addCode(o.call(r+"_zero",o.i32_const(a+c))),e.addCode(o.call(s+"_mReduct",o.i32_const(a),o.getLocal("r")))}(),function(){const a=t.addFunction(s+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c));a.addCode(e.call(s+"_fromMontgomery",e.getLocal("x"),o),e.call(r+"_gte",o,e.i32_const(L)))}(),function(){const a=t.addFunction(s+"_sign");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c));a.addCode(e.if(e.call(r+"_isZero",e.getLocal("x")),e.ret(e.i32_const(0))),e.call(s+"_fromMontgomery",e.getLocal("x"),o),e.if(e.call(r+"_gte",o,e.i32_const(L)),e.ret(e.i32_const(-1))),e.ret(e.i32_const(1)))}(),function(){const a=t.addFunction(s+"_inverse");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_fromMontgomery",e.getLocal("x"),e.getLocal("r"))),a.addCode(e.call(r+"_inverseMod",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),a.addCode(e.call(s+"_toMontgomery",e.getLocal("r"),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_one");a.addParam("pr","i32");const e=a.getCodeBuilder();a.addCode(e.call(r+"_copy",e.i32_const(_),e.getLocal("pr")))}(),function(){const a=t.addFunction(s+"_load");a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32"),a.addLocal("p","i32"),a.addLocal("l","i32"),a.addLocal("i","i32"),a.addLocal("j","i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c)),i=t.alloc(c),n=e.i32_const(i);a.addCode(e.call(r+"_zero",e.getLocal("r")),e.setLocal("i",e.i32_const(c)),e.setLocal("p",e.getLocal("scalar")),e.block(e.loop(e.br_if(1,e.i32_gt_u(e.getLocal("i"),e.getLocal("scalarLen"))),e.if(e.i32_eq(e.getLocal("i"),e.i32_const(c)),e.call(s+"_one",o),e.call(s+"_mul",o,e.i32_const(u),o)),e.call(s+"_mul",e.getLocal("p"),o,n),e.call(s+"_add",e.getLocal("r"),n,e.getLocal("r")),e.setLocal("p",e.i32_add(e.getLocal("p"),e.i32_const(c))),e.setLocal("i",e.i32_add(e.getLocal("i"),e.i32_const(c))),e.br(0))),e.setLocal("l",e.i32_rem_u(e.getLocal("scalarLen"),e.i32_const(c))),e.if(e.i32_eqz(e.getLocal("l")),e.ret([])),e.call(r+"_zero",n),e.setLocal("j",e.i32_const(0)),e.block(e.loop(e.br_if(1,e.i32_eq(e.getLocal("j"),e.getLocal("l"))),e.i32_store8(e.getLocal("j"),i,e.i32_load8_u(e.getLocal("p"))),e.setLocal("p",e.i32_add(e.getLocal("p"),e.i32_const(1))),e.setLocal("j",e.i32_add(e.getLocal("j"),e.i32_const(1))),e.br(0))),e.if(e.i32_eq(e.getLocal("i"),e.i32_const(c)),e.call(s+"_one",o),e.call(s+"_mul",o,e.i32_const(u),o)),e.call(s+"_mul",n,o,n),e.call(s+"_add",e.getLocal("r"),n,e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_timesScalar");a.addParam("x","i32"),a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c));a.addCode(e.call(s+"_load",e.getLocal("scalar"),e.getLocal("scalarLen"),o),e.call(s+"_toMontgomery",o,o),e.call(s+"_mul",e.getLocal("x"),o,e.getLocal("r")))}(),pt(t,s),ht(t,s+"_batchToMontgomery",s+"_toMontgomery",c,c),ht(t,s+"_batchFromMontgomery",s+"_fromMontgomery",c,c),ht(t,s+"_batchNeg",s+"_neg",c,c),mt(t,s+"_batchAdd",s+"_add",c,c),mt(t,s+"_batchSub",s+"_sub",c,c),mt(t,s+"_batchMul",s+"_mul",c,c),t.exportFunction(s+"_add"),t.exportFunction(s+"_sub"),t.exportFunction(s+"_neg"),t.exportFunction(s+"_isNegative"),t.exportFunction(s+"_isOne"),t.exportFunction(s+"_sign"),t.exportFunction(s+"_mReduct"),t.exportFunction(s+"_mul"),t.exportFunction(s+"_square"),t.exportFunction(s+"_squareOld"),t.exportFunction(s+"_fromMontgomery"),t.exportFunction(s+"_toMontgomery"),t.exportFunction(s+"_inverse"),t.exportFunction(s+"_one"),t.exportFunction(s+"_load"),t.exportFunction(s+"_timesScalar"),ft(t,s+"_exp",c,s+"_mul",s+"_square",r+"_copy",s+"_one"),t.exportFunction(s+"_exp"),t.exportFunction(s+"_batchInverse"),yt(i)&&(!function(){const a=t.addFunction(s+"_sqrt");a.addParam("n","i32"),a.addParam("r","i32"),a.addLocal("m","i32"),a.addLocal("i","i32"),a.addLocal("j","i32");const e=a.getCodeBuilder(),o=e.i32_const(_),i=e.i32_const(t.alloc(c)),n=e.i32_const(t.alloc(c)),l=e.i32_const(t.alloc(c)),r=e.i32_const(t.alloc(c)),d=e.i32_const(t.alloc(c));a.addCode(e.if(e.call(s+"_isZero",e.getLocal("n")),e.ret(e.call(s+"_zero",e.getLocal("r")))),e.setLocal("m",e.i32_const(w)),e.call(s+"_copy",e.i32_const(x),i),e.call(s+"_exp",e.getLocal("n"),e.i32_const(A),e.i32_const(c),n),e.call(s+"_exp",e.getLocal("n"),e.i32_const(I),e.i32_const(c),l),e.block(e.loop(e.br_if(1,e.call(s+"_eq",n,o)),e.call(s+"_square",n,r),e.setLocal("i",e.i32_const(1)),e.block(e.loop(e.br_if(1,e.call(s+"_eq",r,o)),e.call(s+"_square",r,r),e.setLocal("i",e.i32_add(e.getLocal("i"),e.i32_const(1))),e.br(0))),e.call(s+"_copy",i,d),e.setLocal("j",e.i32_sub(e.i32_sub(e.getLocal("m"),e.getLocal("i")),e.i32_const(1))),e.block(e.loop(e.br_if(1,e.i32_eqz(e.getLocal("j"))),e.call(s+"_square",d,d),e.setLocal("j",e.i32_sub(e.getLocal("j"),e.i32_const(1))),e.br(0))),e.setLocal("m",e.getLocal("i")),e.call(s+"_square",d,i),e.call(s+"_mul",n,i,n),e.call(s+"_mul",l,d,l),e.br(0))),e.if(e.call(s+"_isNegative",l),e.call(s+"_neg",l,e.getLocal("r")),e.call(s+"_copy",l,e.getLocal("r"))))}(),function(){const a=t.addFunction(s+"_isSquare");a.addParam("n","i32"),a.setReturnType("i32");const e=a.getCodeBuilder(),o=e.i32_const(_),i=e.i32_const(t.alloc(c));a.addCode(e.if(e.call(s+"_isZero",e.getLocal("n")),e.ret(e.i32_const(1))),e.call(s+"_exp",e.getLocal("n"),e.i32_const(h),e.i32_const(c),i),e.call(s+"_eq",i,o))}(),t.exportFunction(s+"_sqrt"),t.exportFunction(s+"_isSquare")),t.exportFunction(s+"_batchToMontgomery"),t.exportFunction(s+"_batchFromMontgomery"),s};const Ft=xt,{bitLength:It}=J;var Bt=function(t,a,e,o,i){const n=BigInt(a),l=Math.floor((It(n-1n)-1)/64)+1,c=8*l,s=e||"f1";if(t.modules[s])return s;t.modules[s]={n64:l};const r=i||"int",d=Ft(t,n,o,r),u=t.modules[d].pR2,_=t.modules[d].pq,g=t.modules[d].pePlusOne;return function(){const a=t.alloc(c),e=t.addFunction(s+"_mul");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const o=e.getCodeBuilder();e.addCode(o.call(d+"_mul",o.getLocal("x"),o.getLocal("y"),o.i32_const(a))),e.addCode(o.call(d+"_mul",o.i32_const(a),o.i32_const(u),o.getLocal("r")))}(),function(){const a=t.addFunction(s+"_square");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_mul",e.getLocal("x"),e.getLocal("x"),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_inverse");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(r+"_inverseMod",e.getLocal("x"),e.i32_const(_),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder();a.addCode(e.call(r+"_gte",e.getLocal("x"),e.i32_const(g)))}(),t.exportFunction(d+"_add",s+"_add"),t.exportFunction(d+"_sub",s+"_sub"),t.exportFunction(d+"_neg",s+"_neg"),t.exportFunction(s+"_mul"),t.exportFunction(s+"_square"),t.exportFunction(s+"_inverse"),t.exportFunction(s+"_isNegative"),t.exportFunction(d+"_copy",s+"_copy"),t.exportFunction(d+"_zero",s+"_zero"),t.exportFunction(d+"_one",s+"_one"),t.exportFunction(d+"_isZero",s+"_isZero"),t.exportFunction(d+"_eq",s+"_eq"),s};const Et=H,vt=Z,St=K;var Pt=function(t,a,e,o){if(t.modules[e])return e;const i=8*t.modules[o].n64,n=t.modules[o].q;return t.modules[e]={n64:2*t.modules[o].n64},function(){const a=t.addFunction(e+"_isZero");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.i32_and(n.call(o+"_isZero",l),n.call(o+"_isZero",c)))}(),function(){const a=t.addFunction(e+"_isOne");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.ret(n.i32_and(n.call(o+"_isOne",l),n.call(o+"_isZero",c))))}(),function(){const a=t.addFunction(e+"_zero");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.call(o+"_zero",l),n.call(o+"_zero",c))}(),function(){const a=t.addFunction(e+"_one");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.call(o+"_one",l),n.call(o+"_zero",c))}(),function(){const a=t.addFunction(e+"_copy");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_copy",l,s),n.call(o+"_copy",c,r))}(),function(){const n=t.addFunction(e+"_mul");n.addParam("x","i32"),n.addParam("y","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.getLocal("y"),d=l.i32_add(l.getLocal("y"),l.i32_const(i)),u=l.getLocal("r"),_=l.i32_add(l.getLocal("r"),l.i32_const(i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i)),p=l.i32_const(t.alloc(i)),h=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_mul",c,r,g),l.call(o+"_mul",s,d,f),l.call(o+"_add",c,s,p),l.call(o+"_add",r,d,h),l.call(o+"_mul",p,h,p),l.call(a,f,u),l.call(o+"_add",g,u,u),l.call(o+"_add",g,f,_),l.call(o+"_sub",p,_,_))}(),function(){const a=t.addFunction(e+"_mul1");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_mul",l,s,r),n.call(o+"_mul",c,s,d))}(),function(){const n=t.addFunction(e+"_square");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.getLocal("r"),d=l.i32_add(l.getLocal("r"),l.i32_const(i)),u=l.i32_const(t.alloc(i)),_=l.i32_const(t.alloc(i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_mul",c,s,u),l.call(o+"_add",c,s,_),l.call(a,s,g),l.call(o+"_add",c,g,g),l.call(a,u,f),l.call(o+"_add",f,u,f),l.call(o+"_mul",_,g,r),l.call(o+"_sub",r,f,r),l.call(o+"_add",u,u,d))}(),function(){const a=t.addFunction(e+"_add");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.i32_add(n.getLocal("y"),n.i32_const(i)),d=n.getLocal("r"),u=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_add",l,s,d),n.call(o+"_add",c,r,u))}(),function(){const a=t.addFunction(e+"_sub");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.i32_add(n.getLocal("y"),n.i32_const(i)),d=n.getLocal("r"),u=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_sub",l,s,d),n.call(o+"_sub",c,r,u))}(),function(){const a=t.addFunction(e+"_neg");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_neg",l,s),n.call(o+"_neg",c,r))}(),function(){const a=t.addFunction(e+"_conjugate");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_copy",l,s),n.call(o+"_neg",c,r))}(),function(){const a=t.addFunction(e+"_toMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_toMontgomery",l,s),n.call(o+"_toMontgomery",c,r))}(),function(){const a=t.addFunction(e+"_fromMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_fromMontgomery",l,s),n.call(o+"_fromMontgomery",c,r))}(),function(){const a=t.addFunction(e+"_eq");a.addParam("x","i32"),a.addParam("y","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.i32_add(n.getLocal("y"),n.i32_const(i));a.addCode(n.i32_and(n.call(o+"_eq",l,s),n.call(o+"_eq",c,r)))}(),function(){const n=t.addFunction(e+"_inverse");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.getLocal("r"),d=l.i32_add(l.getLocal("r"),l.i32_const(i)),u=l.i32_const(t.alloc(i)),_=l.i32_const(t.alloc(i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_square",c,u),l.call(o+"_square",s,_),l.call(a,_,g),l.call(o+"_sub",u,g,g),l.call(o+"_inverse",g,f),l.call(o+"_mul",c,f,r),l.call(o+"_mul",s,f,d),l.call(o+"_neg",d,d))}(),function(){const a=t.addFunction(e+"_timesScalar");a.addParam("x","i32"),a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_timesScalar",l,n.getLocal("scalar"),n.getLocal("scalarLen"),s),n.call(o+"_timesScalar",c,n.getLocal("scalar"),n.getLocal("scalarLen"),r))}(),function(){const a=t.addFunction(e+"_sign");a.addParam("x","i32"),a.addLocal("s","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.setLocal("s",n.call(o+"_sign",c)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.ret(n.call(o+"_sign",l)))}(),function(){const a=t.addFunction(e+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.if(n.call(o+"_isZero",c),n.ret(n.call(o+"_isNegative",l))),n.ret(n.call(o+"_isNegative",c)))}(),t.exportFunction(e+"_isZero"),t.exportFunction(e+"_isOne"),t.exportFunction(e+"_zero"),t.exportFunction(e+"_one"),t.exportFunction(e+"_copy"),t.exportFunction(e+"_mul"),t.exportFunction(e+"_mul1"),t.exportFunction(e+"_square"),t.exportFunction(e+"_add"),t.exportFunction(e+"_sub"),t.exportFunction(e+"_neg"),t.exportFunction(e+"_sign"),t.exportFunction(e+"_conjugate"),t.exportFunction(e+"_fromMontgomery"),t.exportFunction(e+"_toMontgomery"),t.exportFunction(e+"_eq"),t.exportFunction(e+"_inverse"),vt(t,e),Et(t,e+"_exp",2*i,e+"_mul",e+"_square",e+"_copy",e+"_one"),function(){const a=t.addFunction(e+"_sqrt");a.addParam("a","i32"),a.addParam("pr","i32");const l=a.getCodeBuilder(),c=l.i32_const(t.alloc(St.bigInt2BytesLE((BigInt(n||0)-3n)/4n,i))),s=l.i32_const(t.alloc(St.bigInt2BytesLE((BigInt(n||0)-1n)/2n,i))),r=l.getLocal("a"),d=l.i32_const(t.alloc(2*i)),u=l.i32_const(t.alloc(2*i)),_=l.i32_const(t.alloc(2*i)),g=t.alloc(2*i),f=l.i32_const(g),p=l.i32_const(g),h=l.i32_const(g+i),m=l.i32_const(t.alloc(2*i)),L=l.i32_const(t.alloc(2*i));a.addCode(l.call(e+"_one",f),l.call(e+"_neg",f,f),l.call(e+"_exp",r,c,l.i32_const(i),d),l.call(e+"_square",d,u),l.call(e+"_mul",r,u,u),l.call(e+"_conjugate",u,_),l.call(e+"_mul",_,u,_),l.if(l.call(e+"_eq",_,f),l.unreachable()),l.call(e+"_mul",d,r,m),l.if(l.call(e+"_eq",u,f),[...l.call(o+"_zero",p),...l.call(o+"_one",h),...l.call(e+"_mul",f,m,l.getLocal("pr"))],[...l.call(e+"_one",L),...l.call(e+"_add",L,u,L),...l.call(e+"_exp",L,s,l.i32_const(i),L),...l.call(e+"_mul",L,m,l.getLocal("pr"))]))}(),function(){const a=t.addFunction(e+"_isSquare");a.addParam("a","i32"),a.setReturnType("i32");const o=a.getCodeBuilder(),l=o.i32_const(t.alloc(St.bigInt2BytesLE((BigInt(n||0)-3n)/4n,i))),c=o.getLocal("a"),s=o.i32_const(t.alloc(2*i)),r=o.i32_const(t.alloc(2*i)),d=o.i32_const(t.alloc(2*i)),u=t.alloc(2*i),_=o.i32_const(u);a.addCode(o.call(e+"_one",_),o.call(e+"_neg",_,_),o.call(e+"_exp",c,l,o.i32_const(i),s),o.call(e+"_square",s,r),o.call(e+"_mul",c,r,r),o.call(e+"_conjugate",r,d),o.call(e+"_mul",d,r,d),o.if(o.call(e+"_eq",d,_),o.ret(o.i32_const(0))),o.ret(o.i32_const(1)))}(),t.exportFunction(e+"_exp"),t.exportFunction(e+"_timesScalar"),t.exportFunction(e+"_batchInverse"),t.exportFunction(e+"_sqrt"),t.exportFunction(e+"_isSquare"),t.exportFunction(e+"_isNegative"),e};const Ot=H,qt=Z;var Gt=function(t,a,e,o){if(t.modules[e])return e;const i=8*t.modules[o].n64;return t.modules[e]={n64:3*t.modules[o].n64},function(){const a=t.addFunction(e+"_isZero");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.i32_and(n.i32_and(n.call(o+"_isZero",l),n.call(o+"_isZero",c)),n.call(o+"_isZero",s)))}(),function(){const a=t.addFunction(e+"_isOne");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.ret(n.i32_and(n.i32_and(n.call(o+"_isOne",l),n.call(o+"_isZero",c)),n.call(o+"_isZero",s))))}(),function(){const a=t.addFunction(e+"_zero");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.call(o+"_zero",l),n.call(o+"_zero",c),n.call(o+"_zero",s))}(),function(){const a=t.addFunction(e+"_one");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.call(o+"_one",l),n.call(o+"_zero",c),n.call(o+"_zero",s))}(),function(){const a=t.addFunction(e+"_copy");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_copy",l,r),n.call(o+"_copy",c,d),n.call(o+"_copy",s,u))}(),function(){const n=t.addFunction(e+"_mul");n.addParam("x","i32"),n.addParam("y","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.i32_add(l.getLocal("x"),l.i32_const(2*i)),d=l.getLocal("y"),u=l.i32_add(l.getLocal("y"),l.i32_const(i)),_=l.i32_add(l.getLocal("y"),l.i32_const(2*i)),g=l.getLocal("r"),f=l.i32_add(l.getLocal("r"),l.i32_const(i)),p=l.i32_add(l.getLocal("r"),l.i32_const(2*i)),h=l.i32_const(t.alloc(i)),m=l.i32_const(t.alloc(i)),L=l.i32_const(t.alloc(i)),b=l.i32_const(t.alloc(i)),w=l.i32_const(t.alloc(i)),y=l.i32_const(t.alloc(i)),A=l.i32_const(t.alloc(i)),C=l.i32_const(t.alloc(i)),x=l.i32_const(t.alloc(i)),F=l.i32_const(t.alloc(i)),I=l.i32_const(t.alloc(i)),B=l.i32_const(t.alloc(i)),E=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_mul",c,d,h),l.call(o+"_mul",s,u,m),l.call(o+"_mul",r,_,L),l.call(o+"_add",c,s,b),l.call(o+"_add",d,u,w),l.call(o+"_add",c,r,y),l.call(o+"_add",d,_,A),l.call(o+"_add",s,r,C),l.call(o+"_add",u,_,x),l.call(o+"_add",h,m,F),l.call(o+"_add",h,L,I),l.call(o+"_add",m,L,B),l.call(o+"_mul",C,x,g),l.call(o+"_sub",g,B,g),l.call(a,g,g),l.call(o+"_add",h,g,g),l.call(o+"_mul",b,w,f),l.call(o+"_sub",f,F,f),l.call(a,L,E),l.call(o+"_add",f,E,f),l.call(o+"_mul",y,A,p),l.call(o+"_sub",p,I,p),l.call(o+"_add",p,m,p))}(),function(){const n=t.addFunction(e+"_square");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.i32_add(l.getLocal("x"),l.i32_const(2*i)),d=l.getLocal("r"),u=l.i32_add(l.getLocal("r"),l.i32_const(i)),_=l.i32_add(l.getLocal("r"),l.i32_const(2*i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i)),p=l.i32_const(t.alloc(i)),h=l.i32_const(t.alloc(i)),m=l.i32_const(t.alloc(i)),L=l.i32_const(t.alloc(i)),b=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_square",c,g),l.call(o+"_mul",c,s,f),l.call(o+"_add",f,f,p),l.call(o+"_sub",c,s,h),l.call(o+"_add",h,r,h),l.call(o+"_square",h,h),l.call(o+"_mul",s,r,m),l.call(o+"_add",m,m,L),l.call(o+"_square",r,b),l.call(a,L,d),l.call(o+"_add",g,d,d),l.call(a,b,u),l.call(o+"_add",p,u,u),l.call(o+"_add",g,b,_),l.call(o+"_sub",L,_,_),l.call(o+"_add",h,_,_),l.call(o+"_add",p,_,_))}(),function(){const a=t.addFunction(e+"_add");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(i)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*i)),_=n.getLocal("r"),g=n.i32_add(n.getLocal("r"),n.i32_const(i)),f=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_add",l,r,_),n.call(o+"_add",c,d,g),n.call(o+"_add",s,u,f))}(),function(){const a=t.addFunction(e+"_sub");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(i)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*i)),_=n.getLocal("r"),g=n.i32_add(n.getLocal("r"),n.i32_const(i)),f=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_sub",l,r,_),n.call(o+"_sub",c,d,g),n.call(o+"_sub",s,u,f))}(),function(){const a=t.addFunction(e+"_neg");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_neg",l,r),n.call(o+"_neg",c,d),n.call(o+"_neg",s,u))}(),function(){const a=t.addFunction(e+"_sign");a.addParam("x","i32"),a.addLocal("s","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.setLocal("s",n.call(o+"_sign",s)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.setLocal("s",n.call(o+"_sign",c)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.ret(n.call(o+"_sign",l)))}(),function(){const a=t.addFunction(e+"_toMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_toMontgomery",l,r),n.call(o+"_toMontgomery",c,d),n.call(o+"_toMontgomery",s,u))}(),function(){const a=t.addFunction(e+"_fromMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_fromMontgomery",l,r),n.call(o+"_fromMontgomery",c,d),n.call(o+"_fromMontgomery",s,u))}(),function(){const a=t.addFunction(e+"_eq");a.addParam("x","i32"),a.addParam("y","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(i)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*i));a.addCode(n.i32_and(n.i32_and(n.call(o+"_eq",l,r),n.call(o+"_eq",c,d)),n.call(o+"_eq",s,u)))}(),function(){const n=t.addFunction(e+"_inverse");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.i32_add(l.getLocal("x"),l.i32_const(2*i)),d=l.getLocal("r"),u=l.i32_add(l.getLocal("r"),l.i32_const(i)),_=l.i32_add(l.getLocal("r"),l.i32_const(2*i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i)),p=l.i32_const(t.alloc(i)),h=l.i32_const(t.alloc(i)),m=l.i32_const(t.alloc(i)),L=l.i32_const(t.alloc(i)),b=l.i32_const(t.alloc(i)),w=l.i32_const(t.alloc(i)),y=l.i32_const(t.alloc(i)),A=l.i32_const(t.alloc(i)),C=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_square",c,g),l.call(o+"_square",s,f),l.call(o+"_square",r,p),l.call(o+"_mul",c,s,h),l.call(o+"_mul",c,r,m),l.call(o+"_mul",s,r,L),l.call(a,L,b),l.call(o+"_sub",g,b,b),l.call(a,p,w),l.call(o+"_sub",w,h,w),l.call(o+"_sub",f,m,y),l.call(o+"_mul",r,w,A),l.call(o+"_mul",s,y,C),l.call(o+"_add",A,C,A),l.call(a,A,A),l.call(o+"_mul",c,b,C),l.call(o+"_add",C,A,A),l.call(o+"_inverse",A,A),l.call(o+"_mul",A,b,d),l.call(o+"_mul",A,w,u),l.call(o+"_mul",A,y,_))}(),function(){const a=t.addFunction(e+"_timesScalar");a.addParam("x","i32"),a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_timesScalar",l,n.getLocal("scalar"),n.getLocal("scalarLen"),r),n.call(o+"_timesScalar",c,n.getLocal("scalar"),n.getLocal("scalarLen"),d),n.call(o+"_timesScalar",s,n.getLocal("scalar"),n.getLocal("scalarLen"),u))}(),function(){const a=t.addFunction(e+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.if(n.call(o+"_isZero",s),n.if(n.call(o+"_isZero",c),n.ret(n.call(o+"_isNegative",l)),n.ret(n.call(o+"_isNegative",c)))),n.ret(n.call(o+"_isNegative",s)))}(),t.exportFunction(e+"_isZero"),t.exportFunction(e+"_isOne"),t.exportFunction(e+"_zero"),t.exportFunction(e+"_one"),t.exportFunction(e+"_copy"),t.exportFunction(e+"_mul"),t.exportFunction(e+"_square"),t.exportFunction(e+"_add"),t.exportFunction(e+"_sub"),t.exportFunction(e+"_neg"),t.exportFunction(e+"_sign"),t.exportFunction(e+"_fromMontgomery"),t.exportFunction(e+"_toMontgomery"),t.exportFunction(e+"_eq"),t.exportFunction(e+"_inverse"),qt(t,e),Ot(t,e+"_exp",3*i,e+"_mul",e+"_square",e+"_copy",e+"_one"),t.exportFunction(e+"_exp"),t.exportFunction(e+"_timesScalar"),t.exportFunction(e+"_batchInverse"),t.exportFunction(e+"_isNegative"),e};const zt=function(t,a,e,o,i,n,l,c){const s=t.addFunction(a);s.addParam("base","i32"),s.addParam("scalar","i32"),s.addParam("scalarLength","i32"),s.addParam("r","i32"),s.addLocal("old0","i32"),s.addLocal("nbits","i32"),s.addLocal("i","i32"),s.addLocal("last","i32"),s.addLocal("cur","i32"),s.addLocal("carry","i32"),s.addLocal("p","i32");const r=s.getCodeBuilder(),d=r.i32_const(t.alloc(e));function u(t){return r.i32_and(r.i32_shr_u(r.i32_load(r.i32_add(r.getLocal("scalar"),r.i32_and(r.i32_shr_u(t,r.i32_const(3)),r.i32_const(4294967292)))),r.i32_and(t,r.i32_const(31))),r.i32_const(1))}function _(t){return[...r.i32_store8(r.getLocal("p"),r.i32_const(t)),...r.setLocal("p",r.i32_add(r.getLocal("p"),r.i32_const(1)))]}s.addCode(r.if(r.i32_eqz(r.getLocal("scalarLength")),[...r.call(c,r.getLocal("r")),...r.ret([])]),r.setLocal("nbits",r.i32_shl(r.getLocal("scalarLength"),r.i32_const(3))),r.setLocal("old0",r.i32_load(r.i32_const(0))),r.setLocal("p",r.getLocal("old0")),r.i32_store(r.i32_const(0),r.i32_and(r.i32_add(r.i32_add(r.getLocal("old0"),r.i32_const(32)),r.getLocal("nbits")),r.i32_const(4294967288))),r.setLocal("i",r.i32_const(1)),r.setLocal("last",u(r.i32_const(0))),r.setLocal("carry",r.i32_const(0)),r.block(r.loop(r.br_if(1,r.i32_eq(r.getLocal("i"),r.getLocal("nbits"))),r.setLocal("cur",u(r.getLocal("i"))),r.if(r.getLocal("last"),r.if(r.getLocal("cur"),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(1)],[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(255)]),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(255)],[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(0)),..._(1)])),r.if(r.getLocal("cur"),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(0)],[...r.setLocal("last",r.i32_const(1)),...r.setLocal("carry",r.i32_const(0)),..._(0)]),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(1)),...r.setLocal("carry",r.i32_const(0)),..._(0)],[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(0)),..._(0)]))),r.setLocal("i",r.i32_add(r.getLocal("i"),r.i32_const(1))),r.br(0))),r.if(r.getLocal("last"),r.if(r.getLocal("carry"),[..._(255),..._(0),..._(1)],[..._(1)]),r.if(r.getLocal("carry"),[..._(0),..._(1)])),r.setLocal("p",r.i32_sub(r.getLocal("p"),r.i32_const(1))),r.call(l,r.getLocal("base"),d),r.call(c,r.getLocal("r")),r.block(r.loop(r.call(i,r.getLocal("r"),r.getLocal("r")),r.setLocal("cur",r.i32_load8_u(r.getLocal("p"))),r.if(r.getLocal("cur"),r.if(r.i32_eq(r.getLocal("cur"),r.i32_const(1)),r.call(o,r.getLocal("r"),d,r.getLocal("r")),r.call(n,r.getLocal("r"),d,r.getLocal("r")))),r.br_if(1,r.i32_eq(r.getLocal("old0"),r.getLocal("p"))),r.setLocal("p",r.i32_sub(r.getLocal("p"),r.i32_const(1))),r.br(0))),r.i32_store(r.i32_const(0),r.getLocal("old0")))},Tt=W,Mt=function(t,a,e,o,i){const n=8*t.modules[a].n64;function l(){const o=t.addFunction(e);o.addParam("pBases","i32"),o.addParam("pScalars","i32"),o.addParam("scalarSize","i32"),o.addParam("n","i32"),o.addParam("pr","i32"),o.addLocal("chunkSize","i32"),o.addLocal("nChunks","i32"),o.addLocal("itScalar","i32"),o.addLocal("endScalar","i32"),o.addLocal("itBase","i32"),o.addLocal("itBit","i32"),o.addLocal("i","i32"),o.addLocal("j","i32"),o.addLocal("nTable","i32"),o.addLocal("pTable","i32"),o.addLocal("idx","i32"),o.addLocal("pIdxTable","i32");const i=o.getCodeBuilder(),l=i.i32_const(t.alloc(n)),c=t.alloc([17,17,17,17,17,17,17,17,17,17,16,16,15,14,13,13,12,11,10,9,8,7,7,6,5,4,3,2,1,1,1,1]);o.addCode(i.call(a+"_zero",i.getLocal("pr")),i.if(i.i32_eqz(i.getLocal("n")),i.ret([])),i.setLocal("chunkSize",i.i32_load8_u(i.i32_clz(i.getLocal("n")),c)),i.setLocal("nChunks",i.i32_add(i.i32_div_u(i.i32_sub(i.i32_shl(i.getLocal("scalarSize"),i.i32_const(3)),i.i32_const(1)),i.getLocal("chunkSize")),i.i32_const(1))),i.setLocal("itBit",i.i32_mul(i.i32_sub(i.getLocal("nChunks"),i.i32_const(1)),i.getLocal("chunkSize"))),i.block(i.loop(i.br_if(1,i.i32_lt_s(i.getLocal("itBit"),i.i32_const(0))),i.if(i.i32_eqz(i.call(a+"_isZero",i.getLocal("pr"))),[...i.setLocal("j",i.i32_const(0)),...i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("j"),i.getLocal("chunkSize"))),i.call(a+"_double",i.getLocal("pr"),i.getLocal("pr")),i.setLocal("j",i.i32_add(i.getLocal("j"),i.i32_const(1))),i.br(0)))]),i.call(e+"_chunk",i.getLocal("pBases"),i.getLocal("pScalars"),i.getLocal("scalarSize"),i.getLocal("n"),i.getLocal("itBit"),i.getLocal("chunkSize"),l),i.call(a+"_add",i.getLocal("pr"),l,i.getLocal("pr")),i.setLocal("itBit",i.i32_sub(i.getLocal("itBit"),i.getLocal("chunkSize"))),i.br(0))))}!function(){const a=t.addFunction(e+"_getChunk");a.addParam("pScalar","i32"),a.addParam("scalarSize","i32"),a.addParam("startBit","i32"),a.addParam("chunkSize","i32"),a.addLocal("bitsToEnd","i32"),a.addLocal("mask","i32"),a.setReturnType("i32");const o=a.getCodeBuilder();a.addCode(o.setLocal("bitsToEnd",o.i32_sub(o.i32_mul(o.getLocal("scalarSize"),o.i32_const(8)),o.getLocal("startBit"))),o.if(o.i32_gt_s(o.getLocal("chunkSize"),o.getLocal("bitsToEnd")),o.setLocal("mask",o.i32_sub(o.i32_shl(o.i32_const(1),o.getLocal("bitsToEnd")),o.i32_const(1))),o.setLocal("mask",o.i32_sub(o.i32_shl(o.i32_const(1),o.getLocal("chunkSize")),o.i32_const(1)))),o.i32_and(o.i32_shr_u(o.i32_load(o.i32_add(o.getLocal("pScalar"),o.i32_shr_u(o.getLocal("startBit"),o.i32_const(3))),0,0),o.i32_and(o.getLocal("startBit"),o.i32_const(7))),o.getLocal("mask")))}(),function(){const o=t.addFunction(e+"_reduceTable");o.addParam("pTable","i32"),o.addParam("p","i32"),o.addLocal("half","i32"),o.addLocal("it1","i32"),o.addLocal("it2","i32"),o.addLocal("pAcc","i32");const i=o.getCodeBuilder();o.addCode(i.if(i.i32_eq(i.getLocal("p"),i.i32_const(1)),i.ret([])),i.setLocal("half",i.i32_shl(i.i32_const(1),i.i32_sub(i.getLocal("p"),i.i32_const(1)))),i.setLocal("it1",i.getLocal("pTable")),i.setLocal("it2",i.i32_add(i.getLocal("pTable"),i.i32_mul(i.getLocal("half"),i.i32_const(n)))),i.setLocal("pAcc",i.i32_sub(i.getLocal("it2"),i.i32_const(n))),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("it1"),i.getLocal("pAcc"))),i.call(a+"_add",i.getLocal("it1"),i.getLocal("it2"),i.getLocal("it1")),i.call(a+"_add",i.getLocal("pAcc"),i.getLocal("it2"),i.getLocal("pAcc")),i.setLocal("it1",i.i32_add(i.getLocal("it1"),i.i32_const(n))),i.setLocal("it2",i.i32_add(i.getLocal("it2"),i.i32_const(n))),i.br(0))),i.call(e+"_reduceTable",i.getLocal("pTable"),i.i32_sub(i.getLocal("p"),i.i32_const(1))),i.setLocal("p",i.i32_sub(i.getLocal("p"),i.i32_const(1))),i.block(i.loop(i.br_if(1,i.i32_eqz(i.getLocal("p"))),i.call(a+"_double",i.getLocal("pAcc"),i.getLocal("pAcc")),i.setLocal("p",i.i32_sub(i.getLocal("p"),i.i32_const(1))),i.br(0))),i.call(a+"_add",i.getLocal("pTable"),i.getLocal("pAcc"),i.getLocal("pTable")))}(),function(){const l=t.addFunction(e+"_chunk");l.addParam("pBases","i32"),l.addParam("pScalars","i32"),l.addParam("scalarSize","i32"),l.addParam("n","i32"),l.addParam("startBit","i32"),l.addParam("chunkSize","i32"),l.addParam("pr","i32"),l.addLocal("nChunks","i32"),l.addLocal("itScalar","i32"),l.addLocal("endScalar","i32"),l.addLocal("itBase","i32"),l.addLocal("i","i32"),l.addLocal("j","i32"),l.addLocal("nTable","i32"),l.addLocal("pTable","i32"),l.addLocal("idx","i32"),l.addLocal("pIdxTable","i32");const c=l.getCodeBuilder();l.addCode(c.if(c.i32_eqz(c.getLocal("n")),[...c.call(a+"_zero",c.getLocal("pr")),...c.ret([])]),c.setLocal("nTable",c.i32_shl(c.i32_const(1),c.getLocal("chunkSize"))),c.setLocal("pTable",c.i32_load(c.i32_const(0))),c.i32_store(c.i32_const(0),c.i32_add(c.getLocal("pTable"),c.i32_mul(c.getLocal("nTable"),c.i32_const(n)))),c.setLocal("j",c.i32_const(0)),c.block(c.loop(c.br_if(1,c.i32_eq(c.getLocal("j"),c.getLocal("nTable"))),c.call(a+"_zero",c.i32_add(c.getLocal("pTable"),c.i32_mul(c.getLocal("j"),c.i32_const(n)))),c.setLocal("j",c.i32_add(c.getLocal("j"),c.i32_const(1))),c.br(0))),c.setLocal("itBase",c.getLocal("pBases")),c.setLocal("itScalar",c.getLocal("pScalars")),c.setLocal("endScalar",c.i32_add(c.getLocal("pScalars"),c.i32_mul(c.getLocal("n"),c.getLocal("scalarSize")))),c.block(c.loop(c.br_if(1,c.i32_eq(c.getLocal("itScalar"),c.getLocal("endScalar"))),c.setLocal("idx",c.call(e+"_getChunk",c.getLocal("itScalar"),c.getLocal("scalarSize"),c.getLocal("startBit"),c.getLocal("chunkSize"))),c.if(c.getLocal("idx"),[...c.setLocal("pIdxTable",c.i32_add(c.getLocal("pTable"),c.i32_mul(c.i32_sub(c.getLocal("idx"),c.i32_const(1)),c.i32_const(n)))),...c.call(o,c.getLocal("pIdxTable"),c.getLocal("itBase"),c.getLocal("pIdxTable"))]),c.setLocal("itScalar",c.i32_add(c.getLocal("itScalar"),c.getLocal("scalarSize"))),c.setLocal("itBase",c.i32_add(c.getLocal("itBase"),c.i32_const(i))),c.br(0))),c.call(e+"_reduceTable",c.getLocal("pTable"),c.getLocal("chunkSize")),c.call(a+"_copy",c.getLocal("pTable"),c.getLocal("pr")),c.i32_store(c.i32_const(0),c.getLocal("pTable")))}(),l(),t.exportFunction(e),t.exportFunction(e+"_chunk")};var Ut=function(t,a,e,o){const i=t.modules[e].n64,n=8*i;if(t.modules[a])return a;return t.modules[a]={n64:3*i},function(){const o=t.addFunction(a+"_isZeroAffine");o.addParam("p1","i32"),o.setReturnType("i32");const i=o.getCodeBuilder();o.addCode(i.i32_and(i.call(e+"_isZero",i.getLocal("p1")),i.call(e+"_isZero",i.i32_add(i.getLocal("p1"),i.i32_const(n)))))}(),function(){const o=t.addFunction(a+"_isZero");o.addParam("p1","i32"),o.setReturnType("i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_isZero",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))))}(),function(){const o=t.addFunction(a+"_zeroAffine");o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_zero",i.getLocal("pr"))),o.addCode(i.call(e+"_zero",i.i32_add(i.getLocal("pr"),i.i32_const(n))))}(),function(){const o=t.addFunction(a+"_zero");o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_zero",i.getLocal("pr"))),o.addCode(i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(n)))),o.addCode(i.call(e+"_zero",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))))}(),function(){const e=t.addFunction(a+"_copyAffine");e.addParam("ps","i32"),e.addParam("pd","i32");const o=e.getCodeBuilder();for(let t=0;t<2*i;t++)e.addCode(o.i64_store(o.getLocal("pd"),8*t,o.i64_load(o.getLocal("ps"),8*t)))}(),function(){const e=t.addFunction(a+"_copy");e.addParam("ps","i32"),e.addParam("pd","i32");const o=e.getCodeBuilder();for(let t=0;t<3*i;t++)e.addCode(o.i64_store(o.getLocal("pd"),8*t,o.i64_load(o.getLocal("ps"),8*t)))}(),function(){const o=t.addFunction(a+"_toJacobian");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.getLocal("pr"),r=i.i32_add(i.getLocal("pr"),i.i32_const(n)),d=i.i32_add(i.getLocal("pr"),i.i32_const(2*n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("p1")),i.call(a+"_zero",i.getLocal("pr")),[...i.call(e+"_one",d),...i.call(e+"_copy",c,r),...i.call(e+"_copy",l,s)]))}(),function(){const o=t.addFunction(a+"_eqAffine");o.addParam("p1","i32"),o.addParam("p2","i32"),o.setReturnType("i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder();o.addCode(i.ret(i.i32_and(i.call(e+"_eq",i.getLocal("p1"),i.getLocal("p2")),i.call(e+"_eq",i.i32_add(i.getLocal("p1"),i.i32_const(n)),i.i32_add(i.getLocal("p2"),i.i32_const(n))))))}(),function(){const o=t.addFunction(a+"_eqMixed");o.addParam("p1","i32"),o.addParam("p2","i32"),o.setReturnType("i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n)),u=i.i32_const(t.alloc(n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),i.ret(i.call(a+"_isZeroAffine",i.getLocal("p2")))),i.if(i.call(a+"_isZeroAffine",i.getLocal("p2")),i.ret(i.i32_const(0))),i.if(i.call(e+"_isOne",s),i.ret(i.call(a+"_eqAffine",i.getLocal("p1"),i.getLocal("p2")))),i.call(e+"_square",s,u),i.call(e+"_mul",r,u,_),i.call(e+"_mul",s,u,g),i.call(e+"_mul",d,g,f),i.if(i.call(e+"_eq",l,_),i.if(i.call(e+"_eq",c,f),i.ret(i.i32_const(1)))),i.ret(i.i32_const(0)))}(),function(){const o=t.addFunction(a+"_eq");o.addParam("p1","i32"),o.addParam("p2","i32"),o.setReturnType("i32"),o.addLocal("z1","i32"),o.addLocal("z2","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n));o.addCode(i.setLocal("z2",i.i32_add(i.getLocal("p2"),i.i32_const(2*n))));const u=i.getLocal("z2"),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),i.ret(i.call(a+"_isZero",i.getLocal("p2")))),i.if(i.call(a+"_isZero",i.getLocal("p2")),i.ret(i.i32_const(0))),i.if(i.call(e+"_isOne",s),i.ret(i.call(a+"_eqMixed",i.getLocal("p2"),i.getLocal("p1")))),i.if(i.call(e+"_isOne",u),i.ret(i.call(a+"_eqMixed",i.getLocal("p1"),i.getLocal("p2")))),i.call(e+"_square",s,_),i.call(e+"_square",u,g),i.call(e+"_mul",l,g,f),i.call(e+"_mul",r,_,p),i.call(e+"_mul",s,_,h),i.call(e+"_mul",u,g,m),i.call(e+"_mul",c,m,L),i.call(e+"_mul",d,h,b),i.if(i.call(e+"_eq",f,p),i.if(i.call(e+"_eq",L,b),i.ret(i.i32_const(1)))),i.ret(i.i32_const(0)))}(),function(){const o=t.addFunction(a+"_doubleAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.getLocal("pr"),r=i.i32_add(i.getLocal("pr"),i.i32_const(n)),d=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),u=i.i32_const(t.alloc(n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("p1")),[...i.call(a+"_toJacobian",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.call(e+"_square",l,u),i.call(e+"_square",c,_),i.call(e+"_square",_,g),i.call(e+"_add",l,_,f),i.call(e+"_square",f,f),i.call(e+"_sub",f,u,f),i.call(e+"_sub",f,g,f),i.call(e+"_add",f,f,f),i.call(e+"_add",u,u,p),i.call(e+"_add",p,u,p),i.call(e+"_add",c,c,d),i.call(e+"_square",p,s),i.call(e+"_sub",s,f,s),i.call(e+"_sub",s,f,s),i.call(e+"_add",g,g,h),i.call(e+"_add",h,h,h),i.call(e+"_add",h,h,h),i.call(e+"_sub",f,s,r),i.call(e+"_mul",r,p,r),i.call(e+"_sub",r,h,r))}(),function(){const o=t.addFunction(a+"_double");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(a+"_copy",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(e+"_isOne",s),[...i.ret(i.call(a+"_doubleAffine",i.getLocal("p1"),i.getLocal("pr"))),...i.ret([])]),i.call(e+"_square",l,_),i.call(e+"_square",c,g),i.call(e+"_square",g,f),i.call(e+"_add",l,g,p),i.call(e+"_square",p,p),i.call(e+"_sub",p,_,p),i.call(e+"_sub",p,f,p),i.call(e+"_add",p,p,p),i.call(e+"_add",_,_,h),i.call(e+"_add",h,_,h),i.call(e+"_square",h,m),i.call(e+"_mul",c,s,L),i.call(e+"_add",p,p,r),i.call(e+"_sub",m,r,r),i.call(e+"_add",f,f,b),i.call(e+"_add",b,b,b),i.call(e+"_add",b,b,b),i.call(e+"_sub",p,r,d),i.call(e+"_mul",d,h,d),i.call(e+"_sub",d,b,d),i.call(e+"_add",L,L,u))}(),function(){const o=t.addFunction(a+"_addAffine");o.addParam("p1","i32"),o.addParam("p2","i32"),o.addParam("pr","i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("p2"),r=i.i32_add(i.getLocal("p2"),i.i32_const(n)),d=i.getLocal("pr"),u=i.i32_add(i.getLocal("pr"),i.i32_const(n)),_=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n)),w=i.i32_const(t.alloc(n)),y=i.i32_const(t.alloc(n)),A=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("p1")),[...i.call(a+"_copyAffine",i.getLocal("p2"),i.getLocal("pr")),...i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))),...i.ret([])]),i.if(i.call(a+"_isZeroAffine",i.getLocal("p2")),[...i.call(a+"_copyAffine",i.getLocal("p1"),i.getLocal("pr")),...i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))),...i.ret([])]),i.if(i.call(e+"_eq",l,s),i.if(i.call(e+"_eq",c,r),[...i.call(a+"_doubleAffine",i.getLocal("p2"),i.getLocal("pr")),...i.ret([])])),i.call(e+"_sub",s,l,g),i.call(e+"_sub",r,c,p),i.call(e+"_square",g,f),i.call(e+"_add",f,f,h),i.call(e+"_add",h,h,h),i.call(e+"_mul",g,h,m),i.call(e+"_add",p,p,L),i.call(e+"_mul",l,h,w),i.call(e+"_square",L,b),i.call(e+"_add",w,w,y),i.call(e+"_sub",b,m,d),i.call(e+"_sub",d,y,d),i.call(e+"_mul",c,m,A),i.call(e+"_add",A,A,A),i.call(e+"_sub",w,d,u),i.call(e+"_mul",u,L,u),i.call(e+"_sub",u,A,u),i.call(e+"_add",g,g,_))}(),function(){const o=t.addFunction(a+"_addMixed");o.addParam("p1","i32"),o.addParam("p2","i32"),o.addParam("pr","i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n)),u=i.getLocal("pr"),_=i.i32_add(i.getLocal("pr"),i.i32_const(n)),g=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n)),w=i.i32_const(t.alloc(n)),y=i.i32_const(t.alloc(n)),A=i.i32_const(t.alloc(n)),C=i.i32_const(t.alloc(n)),x=i.i32_const(t.alloc(n)),F=i.i32_const(t.alloc(n)),I=i.i32_const(t.alloc(n)),B=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(a+"_copyAffine",i.getLocal("p2"),i.getLocal("pr")),...i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))),...i.ret([])]),i.if(i.call(a+"_isZeroAffine",i.getLocal("p2")),[...i.call(a+"_copy",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(e+"_isOne",s),[...i.call(a+"_addAffine",l,r,u),...i.ret([])]),i.call(e+"_square",s,f),i.call(e+"_mul",r,f,p),i.call(e+"_mul",s,f,h),i.call(e+"_mul",d,h,m),i.if(i.call(e+"_eq",l,p),i.if(i.call(e+"_eq",c,m),[...i.call(a+"_doubleAffine",i.getLocal("p2"),i.getLocal("pr")),...i.ret([])])),i.call(e+"_sub",p,l,L),i.call(e+"_sub",m,c,w),i.call(e+"_square",L,b),i.call(e+"_add",b,b,y),i.call(e+"_add",y,y,y),i.call(e+"_mul",L,y,A),i.call(e+"_add",w,w,C),i.call(e+"_mul",l,y,F),i.call(e+"_square",C,x),i.call(e+"_add",F,F,I),i.call(e+"_sub",x,A,u),i.call(e+"_sub",u,I,u),i.call(e+"_mul",c,A,B),i.call(e+"_add",B,B,B),i.call(e+"_sub",F,u,_),i.call(e+"_mul",_,C,_),i.call(e+"_sub",_,B,_),i.call(e+"_add",s,L,g),i.call(e+"_square",g,g),i.call(e+"_sub",g,f,g),i.call(e+"_sub",g,b,g))}(),function(){const o=t.addFunction(a+"_add");o.addParam("p1","i32"),o.addParam("p2","i32"),o.addParam("pr","i32"),o.addLocal("z1","i32"),o.addLocal("z2","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n));o.addCode(i.setLocal("z2",i.i32_add(i.getLocal("p2"),i.i32_const(2*n))));const u=i.getLocal("z2"),_=i.getLocal("pr"),g=i.i32_add(i.getLocal("pr"),i.i32_const(n)),f=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n)),w=i.i32_const(t.alloc(n)),y=i.i32_const(t.alloc(n)),A=i.i32_const(t.alloc(n)),C=i.i32_const(t.alloc(n)),x=i.i32_const(t.alloc(n)),F=i.i32_const(t.alloc(n)),I=i.i32_const(t.alloc(n)),B=i.i32_const(t.alloc(n)),E=i.i32_const(t.alloc(n)),v=i.i32_const(t.alloc(n)),S=i.i32_const(t.alloc(n)),P=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(a+"_copy",i.getLocal("p2"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(a+"_isZero",i.getLocal("p2")),[...i.call(a+"_copy",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(e+"_isOne",s),[...i.call(a+"_addMixed",r,l,_),...i.ret([])]),i.if(i.call(e+"_isOne",u),[...i.call(a+"_addMixed",l,r,_),...i.ret([])]),i.call(e+"_square",s,p),i.call(e+"_square",u,h),i.call(e+"_mul",l,h,m),i.call(e+"_mul",r,p,L),i.call(e+"_mul",s,p,b),i.call(e+"_mul",u,h,w),i.call(e+"_mul",c,w,y),i.call(e+"_mul",d,b,A),i.if(i.call(e+"_eq",m,L),i.if(i.call(e+"_eq",y,A),[...i.call(a+"_double",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])])),i.call(e+"_sub",L,m,C),i.call(e+"_sub",A,y,x),i.call(e+"_add",C,C,F),i.call(e+"_square",F,F),i.call(e+"_mul",C,F,I),i.call(e+"_add",x,x,B),i.call(e+"_mul",m,F,v),i.call(e+"_square",B,E),i.call(e+"_add",v,v,S),i.call(e+"_sub",E,I,_),i.call(e+"_sub",_,S,_),i.call(e+"_mul",y,I,P),i.call(e+"_add",P,P,P),i.call(e+"_sub",v,_,g),i.call(e+"_mul",g,B,g),i.call(e+"_sub",g,P,g),i.call(e+"_add",s,u,f),i.call(e+"_square",f,f),i.call(e+"_sub",f,p,f),i.call(e+"_sub",f,h,f),i.call(e+"_mul",f,C,f))}(),function(){const o=t.addFunction(a+"_negAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.getLocal("pr"),r=i.i32_add(i.getLocal("pr"),i.i32_const(n));o.addCode(i.call(e+"_copy",l,s),i.call(e+"_neg",c,r))}(),function(){const o=t.addFunction(a+"_neg");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_add(i.getLocal("pr"),i.i32_const(2*n));o.addCode(i.call(e+"_copy",l,r),i.call(e+"_neg",c,d),i.call(e+"_copy",s,u))}(),function(){const e=t.addFunction(a+"_subAffine");e.addParam("p1","i32"),e.addParam("p2","i32"),e.addParam("pr","i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(3*n));e.addCode(o.call(a+"_negAffine",o.getLocal("p2"),i),o.call(a+"_addAffine",o.getLocal("p1"),i,o.getLocal("pr")))}(),function(){const e=t.addFunction(a+"_subMixed");e.addParam("p1","i32"),e.addParam("p2","i32"),e.addParam("pr","i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(3*n));e.addCode(o.call(a+"_negAffine",o.getLocal("p2"),i),o.call(a+"_addMixed",o.getLocal("p1"),i,o.getLocal("pr")))}(),function(){const e=t.addFunction(a+"_sub");e.addParam("p1","i32"),e.addParam("p2","i32"),e.addParam("pr","i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(3*n));e.addCode(o.call(a+"_neg",o.getLocal("p2"),i),o.call(a+"_add",o.getLocal("p1"),i,o.getLocal("pr")))}(),function(){const o=t.addFunction(a+"_fromMontgomeryAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_fromMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<2;t++)o.addCode(i.call(e+"_fromMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_fromMontgomery");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_fromMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<3;t++)o.addCode(i.call(e+"_fromMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_toMontgomeryAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_toMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<2;t++)o.addCode(i.call(e+"_toMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_toMontgomery");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_toMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<3;t++)o.addCode(i.call(e+"_toMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_toAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_const(t.alloc(n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(e+"_zero",r),...i.call(e+"_zero",d)],[...i.call(e+"_inverse",s,u),...i.call(e+"_square",u,_),...i.call(e+"_mul",u,_,g),...i.call(e+"_mul",l,_,r),...i.call(e+"_mul",c,g,d)]))}(),function(){const i=t.addFunction(a+"_inCurveAffine");i.addParam("pIn","i32"),i.setReturnType("i32");const l=i.getCodeBuilder(),c=l.getLocal("pIn"),s=l.i32_add(l.getLocal("pIn"),l.i32_const(n)),r=l.i32_const(t.alloc(n)),d=l.i32_const(t.alloc(n));i.addCode(l.call(e+"_square",s,r),l.call(e+"_square",c,d),l.call(e+"_mul",c,d,d),l.call(e+"_add",d,l.i32_const(o),d),l.ret(l.call(e+"_eq",r,d)))}(),function(){const e=t.addFunction(a+"_inCurve");e.addParam("pIn","i32"),e.setReturnType("i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(2*n));e.addCode(o.call(a+"_toAffine",o.getLocal("pIn"),i),o.ret(o.call(a+"_inCurveAffine",i)))}(),function(){const o=t.addFunction(a+"_batchToAffine");o.addParam("pIn","i32"),o.addParam("n","i32"),o.addParam("pOut","i32"),o.addLocal("pAux","i32"),o.addLocal("itIn","i32"),o.addLocal("itAux","i32"),o.addLocal("itOut","i32"),o.addLocal("i","i32");const i=o.getCodeBuilder(),l=i.i32_const(t.alloc(n));o.addCode(i.setLocal("pAux",i.i32_load(i.i32_const(0))),i.i32_store(i.i32_const(0),i.i32_add(i.getLocal("pAux"),i.i32_mul(i.getLocal("n"),i.i32_const(n)))),i.call(e+"_batchInverse",i.i32_add(i.getLocal("pIn"),i.i32_const(2*n)),i.i32_const(3*n),i.getLocal("n"),i.getLocal("pAux"),i.i32_const(n)),i.setLocal("itIn",i.getLocal("pIn")),i.setLocal("itAux",i.getLocal("pAux")),i.setLocal("itOut",i.getLocal("pOut")),i.setLocal("i",i.i32_const(0)),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("i"),i.getLocal("n"))),i.if(i.call(e+"_isZero",i.getLocal("itAux")),[...i.call(e+"_zero",i.getLocal("itOut")),...i.call(e+"_zero",i.i32_add(i.getLocal("itOut"),i.i32_const(n)))],[...i.call(e+"_mul",i.getLocal("itAux"),i.i32_add(i.getLocal("itIn"),i.i32_const(n)),l),...i.call(e+"_square",i.getLocal("itAux"),i.getLocal("itAux")),...i.call(e+"_mul",i.getLocal("itAux"),i.getLocal("itIn"),i.getLocal("itOut")),...i.call(e+"_mul",i.getLocal("itAux"),l,i.i32_add(i.getLocal("itOut"),i.i32_const(n)))]),i.setLocal("itIn",i.i32_add(i.getLocal("itIn"),i.i32_const(3*n))),i.setLocal("itOut",i.i32_add(i.getLocal("itOut"),i.i32_const(2*n))),i.setLocal("itAux",i.i32_add(i.getLocal("itAux"),i.i32_const(n))),i.setLocal("i",i.i32_add(i.getLocal("i"),i.i32_const(1))),i.br(0))),i.i32_store(i.i32_const(0),i.getLocal("pAux")))}(),function(){const o=t.addFunction(a+"_normalize");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),i.call(a+"_zero",i.getLocal("pr")),[...i.call(e+"_inverse",s,_),...i.call(e+"_square",_,g),...i.call(e+"_mul",_,g,f),...i.call(e+"_mul",l,g,r),...i.call(e+"_mul",c,f,d),...i.call(e+"_one",u)]))}(),function(){const e=t.addFunction(a+"__reverseBytes");e.addParam("pIn","i32"),e.addParam("n","i32"),e.addParam("pOut","i32"),e.addLocal("itOut","i32"),e.addLocal("itIn","i32");const o=e.getCodeBuilder();e.addCode(o.setLocal("itOut",o.i32_sub(o.i32_add(o.getLocal("pOut"),o.getLocal("n")),o.i32_const(1))),o.setLocal("itIn",o.getLocal("pIn")),o.block(o.loop(o.br_if(1,o.i32_lt_s(o.getLocal("itOut"),o.getLocal("pOut"))),o.i32_store8(o.getLocal("itOut"),o.i32_load8_u(o.getLocal("itIn"))),o.setLocal("itOut",o.i32_sub(o.getLocal("itOut"),o.i32_const(1))),o.setLocal("itIn",o.i32_add(o.getLocal("itIn"),o.i32_const(1))),o.br(0))))}(),function(){const e=t.addFunction(a+"_LEMtoU");e.addParam("pIn","i32"),e.addParam("pOut","i32");const o=e.getCodeBuilder(),i=t.alloc(2*n),l=o.i32_const(i),c=o.i32_const(i),s=o.i32_const(i+n);e.addCode(o.if(o.call(a+"_isZeroAffine",o.getLocal("pIn")),[...o.call(a+"_zeroAffine",o.getLocal("pOut")),...o.ret([])]),o.call(a+"_fromMontgomeryAffine",o.getLocal("pIn"),l),o.call(a+"__reverseBytes",c,o.i32_const(n),o.getLocal("pOut")),o.call(a+"__reverseBytes",s,o.i32_const(n),o.i32_add(o.getLocal("pOut"),o.i32_const(n))))}(),function(){const o=t.addFunction(a+"_LEMtoC");o.addParam("pIn","i32"),o.addParam("pOut","i32");const i=o.getCodeBuilder(),l=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("pIn")),[...i.call(e+"_zero",i.getLocal("pOut")),...i.i32_store8(i.getLocal("pOut"),i.i32_const(64)),...i.ret([])]),i.call(e+"_fromMontgomery",i.getLocal("pIn"),l),i.call(a+"__reverseBytes",l,i.i32_const(n),i.getLocal("pOut")),i.if(i.i32_eq(i.call(e+"_sign",i.i32_add(i.getLocal("pIn"),i.i32_const(n))),i.i32_const(-1)),i.i32_store8(i.getLocal("pOut"),i.i32_or(i.i32_load8_u(i.getLocal("pOut")),i.i32_const(128)))))}(),function(){const e=t.addFunction(a+"_UtoLEM");e.addParam("pIn","i32"),e.addParam("pOut","i32");const o=e.getCodeBuilder(),i=t.alloc(2*n),l=o.i32_const(i),c=o.i32_const(i),s=o.i32_const(i+n);e.addCode(o.if(o.i32_and(o.i32_load8_u(o.getLocal("pIn")),o.i32_const(64)),[...o.call(a+"_zeroAffine",o.getLocal("pOut")),...o.ret([])]),o.call(a+"__reverseBytes",o.getLocal("pIn"),o.i32_const(n),c),o.call(a+"__reverseBytes",o.i32_add(o.getLocal("pIn"),o.i32_const(n)),o.i32_const(n),s),o.call(a+"_toMontgomeryAffine",l,o.getLocal("pOut")))}(),function(){const i=t.addFunction(a+"_CtoLEM");i.addParam("pIn","i32"),i.addParam("pOut","i32"),i.addLocal("firstByte","i32"),i.addLocal("greatest","i32");const l=i.getCodeBuilder(),c=t.alloc(2*n),s=l.i32_const(c),r=l.i32_const(c+n);i.addCode(l.setLocal("firstByte",l.i32_load8_u(l.getLocal("pIn"))),l.if(l.i32_and(l.getLocal("firstByte"),l.i32_const(64)),[...l.call(a+"_zeroAffine",l.getLocal("pOut")),...l.ret([])]),l.setLocal("greatest",l.i32_and(l.getLocal("firstByte"),l.i32_const(128))),l.call(e+"_copy",l.getLocal("pIn"),r),l.i32_store8(r,l.i32_and(l.getLocal("firstByte"),l.i32_const(63))),l.call(a+"__reverseBytes",r,l.i32_const(n),s),l.call(e+"_toMontgomery",s,l.getLocal("pOut")),l.call(e+"_square",l.getLocal("pOut"),r),l.call(e+"_mul",l.getLocal("pOut"),r,r),l.call(e+"_add",r,l.i32_const(o),r),l.call(e+"_sqrt",r,r),l.call(e+"_neg",r,s),l.if(l.i32_eq(l.call(e+"_sign",r),l.i32_const(-1)),l.if(l.getLocal("greatest"),l.call(e+"_copy",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n))),l.call(e+"_neg",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n)))),l.if(l.getLocal("greatest"),l.call(e+"_neg",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n))),l.call(e+"_copy",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n))))))}(),Tt(t,a+"_batchLEMtoU",a+"_LEMtoU",2*n,2*n),Tt(t,a+"_batchLEMtoC",a+"_LEMtoC",2*n,n),Tt(t,a+"_batchUtoLEM",a+"_UtoLEM",2*n,2*n),Tt(t,a+"_batchCtoLEM",a+"_CtoLEM",n,2*n,!0),Tt(t,a+"_batchToJacobian",a+"_toJacobian",2*n,3*n,!0),Mt(t,a,a+"_multiexp",a+"_add",3*n),Mt(t,a,a+"_multiexpAffine",a+"_addMixed",2*n),zt(t,a+"_timesScalar",3*n,a+"_add",a+"_double",a+"_sub",a+"_copy",a+"_zero"),zt(t,a+"_timesScalarAffine",2*n,a+"_addMixed",a+"_double",a+"_subMixed",a+"_copyAffine",a+"_zero"),t.exportFunction(a+"_isZero"),t.exportFunction(a+"_isZeroAffine"),t.exportFunction(a+"_eq"),t.exportFunction(a+"_eqMixed"),t.exportFunction(a+"_eqAffine"),t.exportFunction(a+"_copy"),t.exportFunction(a+"_copyAffine"),t.exportFunction(a+"_zero"),t.exportFunction(a+"_zeroAffine"),t.exportFunction(a+"_double"),t.exportFunction(a+"_doubleAffine"),t.exportFunction(a+"_add"),t.exportFunction(a+"_addMixed"),t.exportFunction(a+"_addAffine"),t.exportFunction(a+"_neg"),t.exportFunction(a+"_negAffine"),t.exportFunction(a+"_sub"),t.exportFunction(a+"_subMixed"),t.exportFunction(a+"_subAffine"),t.exportFunction(a+"_fromMontgomery"),t.exportFunction(a+"_fromMontgomeryAffine"),t.exportFunction(a+"_toMontgomery"),t.exportFunction(a+"_toMontgomeryAffine"),t.exportFunction(a+"_timesScalar"),t.exportFunction(a+"_timesScalarAffine"),t.exportFunction(a+"_normalize"),t.exportFunction(a+"_LEMtoU"),t.exportFunction(a+"_LEMtoC"),t.exportFunction(a+"_UtoLEM"),t.exportFunction(a+"_CtoLEM"),t.exportFunction(a+"_batchLEMtoU"),t.exportFunction(a+"_batchLEMtoC"),t.exportFunction(a+"_batchUtoLEM"),t.exportFunction(a+"_batchCtoLEM"),t.exportFunction(a+"_toAffine"),t.exportFunction(a+"_toJacobian"),t.exportFunction(a+"_batchToAffine"),t.exportFunction(a+"_batchToJacobian"),t.exportFunction(a+"_inCurve"),t.exportFunction(a+"_inCurveAffine"),a};const{isOdd:Qt,modInv:kt,modPow:Rt}=J,Nt=K;var Dt=function(t,a,e,o,i){const n=8*t.modules[o].n64,l=8*t.modules[e].n64,c=t.modules[o].q;let s=c-1n,r=0;for(;!Qt(s);)r++,s>>=1n;let d=2n;for(;1n===Rt(d,c>>1n,c);)d+=1n;const u=new Array(r+1);u[r]=Rt(d,s,c);let _=r-1;for(;_>=0;)u[_]=Rt(u[_+1],2n,c),_--;const g=[],f=(1n<>e);return a}const F=Array(256);for(let t=0;t<256;t++)F[t]=x(t);const I=t.alloc(F);function B(){const e=t.addFunction(a+"_fft");e.addParam("px","i32"),e.addParam("n","i32"),e.addLocal("bits","i32");const i=e.getCodeBuilder(),l=i.i32_const(t.alloc(n));e.addCode(i.setLocal("bits",i.call(a+"__log2",i.getLocal("n"))),i.call(o+"_one",l),i.call(a+"_rawfft",i.getLocal("px"),i.getLocal("bits"),i.i32_const(0),l))}!function(){const e=t.addFunction(a+"__rev");e.addParam("x","i32"),e.addParam("bits","i32"),e.setReturnType("i32");const o=e.getCodeBuilder();e.addCode(o.i32_rotl(o.i32_add(o.i32_add(o.i32_shl(o.i32_load8_u(o.i32_and(o.getLocal("x"),o.i32_const(255)),I,0),o.i32_const(24)),o.i32_shl(o.i32_load8_u(o.i32_and(o.i32_shr_u(o.getLocal("x"),o.i32_const(8)),o.i32_const(255)),I,0),o.i32_const(16))),o.i32_add(o.i32_shl(o.i32_load8_u(o.i32_and(o.i32_shr_u(o.getLocal("x"),o.i32_const(16)),o.i32_const(255)),I,0),o.i32_const(8)),o.i32_load8_u(o.i32_and(o.i32_shr_u(o.getLocal("x"),o.i32_const(24)),o.i32_const(255)),I,0))),o.getLocal("bits")))}(),function(){const o=t.addFunction(a+"__reversePermutation");o.addParam("px","i32"),o.addParam("bits","i32"),o.addLocal("n","i32"),o.addLocal("i","i32"),o.addLocal("ri","i32"),o.addLocal("idx1","i32"),o.addLocal("idx2","i32");const i=o.getCodeBuilder(),n=i.i32_const(t.alloc(l));o.addCode(i.setLocal("n",i.i32_shl(i.i32_const(1),i.getLocal("bits"))),i.setLocal("i",i.i32_const(0)),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("i"),i.getLocal("n"))),i.setLocal("idx1",i.i32_add(i.getLocal("px"),i.i32_mul(i.getLocal("i"),i.i32_const(l)))),i.setLocal("ri",i.call(a+"__rev",i.getLocal("i"),i.getLocal("bits"))),i.setLocal("idx2",i.i32_add(i.getLocal("px"),i.i32_mul(i.getLocal("ri"),i.i32_const(l)))),i.if(i.i32_lt_u(i.getLocal("i"),i.getLocal("ri")),[...i.call(e+"_copy",i.getLocal("idx1"),n),...i.call(e+"_copy",i.getLocal("idx2"),i.getLocal("idx1")),...i.call(e+"_copy",n,i.getLocal("idx2"))]),i.setLocal("i",i.i32_add(i.getLocal("i"),i.i32_const(1))),i.br(0))))}(),function(){const n=t.addFunction(a+"__fftFinal");n.addParam("px","i32"),n.addParam("bits","i32"),n.addParam("reverse","i32"),n.addParam("mulFactor","i32"),n.addLocal("n","i32"),n.addLocal("ndiv2","i32"),n.addLocal("pInv2","i32"),n.addLocal("i","i32"),n.addLocal("mask","i32"),n.addLocal("idx1","i32"),n.addLocal("idx2","i32");const c=n.getCodeBuilder(),s=c.i32_const(t.alloc(l));n.addCode(c.if(c.i32_and(c.i32_eqz(c.getLocal("reverse")),c.call(o+"_isOne",c.getLocal("mulFactor"))),c.ret([])),c.setLocal("n",c.i32_shl(c.i32_const(1),c.getLocal("bits"))),c.setLocal("mask",c.i32_sub(c.getLocal("n"),c.i32_const(1))),c.setLocal("i",c.i32_const(1)),c.setLocal("ndiv2",c.i32_shr_u(c.getLocal("n"),c.i32_const(1))),c.block(c.loop(c.br_if(1,c.i32_ge_u(c.getLocal("i"),c.getLocal("ndiv2"))),c.setLocal("idx1",c.i32_add(c.getLocal("px"),c.i32_mul(c.getLocal("i"),c.i32_const(l)))),c.setLocal("idx2",c.i32_add(c.getLocal("px"),c.i32_mul(c.i32_sub(c.getLocal("n"),c.getLocal("i")),c.i32_const(l)))),c.if(c.getLocal("reverse"),c.if(c.call(o+"_isOne",c.getLocal("mulFactor")),[...c.call(e+"_copy",c.getLocal("idx1"),s),...c.call(e+"_copy",c.getLocal("idx2"),c.getLocal("idx1")),...c.call(e+"_copy",s,c.getLocal("idx2"))],[...c.call(e+"_copy",c.getLocal("idx1"),s),...c.call(i,c.getLocal("idx2"),c.getLocal("mulFactor"),c.getLocal("idx1")),...c.call(i,s,c.getLocal("mulFactor"),c.getLocal("idx2"))]),c.if(c.call(o+"_isOne",c.getLocal("mulFactor")),[],[...c.call(i,c.getLocal("idx1"),c.getLocal("mulFactor"),c.getLocal("idx1")),...c.call(i,c.getLocal("idx2"),c.getLocal("mulFactor"),c.getLocal("idx2"))])),c.setLocal("i",c.i32_add(c.getLocal("i"),c.i32_const(1))),c.br(0))),c.if(c.call(o+"_isOne",c.getLocal("mulFactor")),[],[...c.call(i,c.getLocal("px"),c.getLocal("mulFactor"),c.getLocal("px")),...c.setLocal("idx2",c.i32_add(c.getLocal("px"),c.i32_mul(c.getLocal("ndiv2"),c.i32_const(l)))),...c.call(i,c.getLocal("idx2"),c.getLocal("mulFactor"),c.getLocal("idx2"))]))}(),function(){const c=t.addFunction(a+"_rawfft");c.addParam("px","i32"),c.addParam("bits","i32"),c.addParam("reverse","i32"),c.addParam("mulFactor","i32"),c.addLocal("s","i32"),c.addLocal("k","i32"),c.addLocal("j","i32"),c.addLocal("m","i32"),c.addLocal("mdiv2","i32"),c.addLocal("n","i32"),c.addLocal("pwm","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));c.addCode(s.call(a+"__reversePermutation",s.getLocal("px"),s.getLocal("bits")),s.setLocal("n",s.i32_shl(s.i32_const(1),s.getLocal("bits"))),s.setLocal("s",s.i32_const(1)),s.block(s.loop(s.br_if(1,s.i32_gt_u(s.getLocal("s"),s.getLocal("bits"))),s.setLocal("m",s.i32_shl(s.i32_const(1),s.getLocal("s"))),s.setLocal("pwm",s.i32_add(s.i32_const(p),s.i32_mul(s.getLocal("s"),s.i32_const(n)))),s.setLocal("k",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_ge_u(s.getLocal("k"),s.getLocal("n"))),s.call(o+"_one",r),s.setLocal("mdiv2",s.i32_shr_u(s.getLocal("m"),s.i32_const(1))),s.setLocal("j",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_ge_u(s.getLocal("j"),s.getLocal("mdiv2"))),s.setLocal("idx1",s.i32_add(s.getLocal("px"),s.i32_mul(s.i32_add(s.getLocal("k"),s.getLocal("j")),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("idx1"),s.i32_mul(s.getLocal("mdiv2"),s.i32_const(l)))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_copy",s.getLocal("idx1"),u),s.call(e+"_add",u,d,s.getLocal("idx1")),s.call(e+"_sub",u,d,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("pwm"),r),s.setLocal("j",s.i32_add(s.getLocal("j"),s.i32_const(1))),s.br(0))),s.setLocal("k",s.i32_add(s.getLocal("k"),s.getLocal("m"))),s.br(0))),s.setLocal("s",s.i32_add(s.getLocal("s"),s.i32_const(1))),s.br(0))),s.call(a+"__fftFinal",s.getLocal("px"),s.getLocal("bits"),s.getLocal("reverse"),s.getLocal("mulFactor")))}(),function(){const e=t.addFunction(a+"__log2");e.addParam("n","i32"),e.setReturnType("i32"),e.addLocal("bits","i32"),e.addLocal("aux","i32");const o=e.getCodeBuilder();e.addCode(o.setLocal("aux",o.i32_shr_u(o.getLocal("n"),o.i32_const(1)))),e.addCode(o.setLocal("bits",o.i32_const(0))),e.addCode(o.block(o.loop(o.br_if(1,o.i32_eqz(o.getLocal("aux"))),o.setLocal("aux",o.i32_shr_u(o.getLocal("aux"),o.i32_const(1))),o.setLocal("bits",o.i32_add(o.getLocal("bits"),o.i32_const(1))),o.br(0)))),e.addCode(o.if(o.i32_ne(o.getLocal("n"),o.i32_shl(o.i32_const(1),o.getLocal("bits"))),o.unreachable())),e.addCode(o.if(o.i32_gt_u(o.getLocal("bits"),o.i32_const(r)),o.unreachable())),e.addCode(o.getLocal("bits"))}(),B(),function(){const e=t.addFunction(a+"_ifft");e.addParam("px","i32"),e.addParam("n","i32"),e.addLocal("bits","i32"),e.addLocal("pInv2","i32");const o=e.getCodeBuilder();e.addCode(o.setLocal("bits",o.call(a+"__log2",o.getLocal("n"))),o.setLocal("pInv2",o.i32_add(o.i32_const(L),o.i32_mul(o.getLocal("bits"),o.i32_const(n)))),o.call(a+"_rawfft",o.getLocal("px"),o.getLocal("bits"),o.i32_const(1),o.getLocal("pInv2")))}(),function(){const c=t.addFunction(a+"_fftJoin");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));c.addCode(s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_copy",s.getLocal("idx1"),u),s.call(e+"_add",u,d,s.getLocal("idx1")),s.call(e+"_sub",u,d,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const c=t.addFunction(a+"_fftJoinExt");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addParam("totalBits","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("pShiftToM","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));c.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(A),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(e+"_add",s.getLocal("idx1"),s.getLocal("idx2"),d),s.call(i,s.getLocal("idx2"),s.getLocal("pShiftToM"),s.getLocal("idx2")),s.call(e+"_add",s.getLocal("idx1"),s.getLocal("idx2"),s.getLocal("idx2")),s.call(i,s.getLocal("idx2"),r,s.getLocal("idx2")),s.call(e+"_copy",d,s.getLocal("idx1")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const c=t.addFunction(a+"_fftJoinExtInv");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addParam("totalBits","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("pShiftToM","i32"),c.addLocal("pSConst","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));c.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(A),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.setLocal("pSConst",s.i32_add(s.i32_const(C),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_sub",s.getLocal("idx1"),d,s.getLocal("idx2")),s.call(i,s.getLocal("idx2"),s.getLocal("pSConst"),s.getLocal("idx2")),s.call(i,s.getLocal("idx1"),s.getLocal("pShiftToM"),s.getLocal("idx1")),s.call(e+"_sub",d,s.getLocal("idx1"),s.getLocal("idx1")),s.call(i,s.getLocal("idx1"),s.getLocal("pSConst"),s.getLocal("idx1")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const c=t.addFunction(a+"_fftMix");c.addParam("pBuff","i32"),c.addParam("n","i32"),c.addParam("exp","i32"),c.addLocal("nGroups","i32"),c.addLocal("nPerGroup","i32"),c.addLocal("nPerGroupDiv2","i32"),c.addLocal("pairOffset","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("j","i32"),c.addLocal("pwm","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));c.addCode(s.setLocal("nPerGroup",s.i32_shl(s.i32_const(1),s.getLocal("exp"))),s.setLocal("nPerGroupDiv2",s.i32_shr_u(s.getLocal("nPerGroup"),s.i32_const(1))),s.setLocal("nGroups",s.i32_shr_u(s.getLocal("n"),s.getLocal("exp"))),s.setLocal("pairOffset",s.i32_mul(s.getLocal("nPerGroupDiv2"),s.i32_const(l))),s.setLocal("pwm",s.i32_add(s.i32_const(p),s.i32_mul(s.getLocal("exp"),s.i32_const(n)))),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("nGroups"))),s.call(o+"_one",r),s.setLocal("j",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("j"),s.getLocal("nPerGroupDiv2"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff"),s.i32_mul(s.i32_add(s.i32_mul(s.getLocal("i"),s.getLocal("nPerGroup")),s.getLocal("j")),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("idx1"),s.getLocal("pairOffset"))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_copy",s.getLocal("idx1"),u),s.call(e+"_add",u,d,s.getLocal("idx1")),s.call(e+"_sub",u,d,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("pwm"),r),s.setLocal("j",s.i32_add(s.getLocal("j"),s.i32_const(1))),s.br(0))),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const o=t.addFunction(a+"_fftFinal");o.addParam("pBuff","i32"),o.addParam("n","i32"),o.addParam("factor","i32"),o.addLocal("idx1","i32"),o.addLocal("idx2","i32"),o.addLocal("i","i32"),o.addLocal("ndiv2","i32");const n=o.getCodeBuilder(),c=n.i32_const(t.alloc(l));o.addCode(n.setLocal("ndiv2",n.i32_shr_u(n.getLocal("n"),n.i32_const(1))),n.if(n.i32_and(n.getLocal("n"),n.i32_const(1)),n.call(i,n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("ndiv2"),n.i32_const(l))),n.getLocal("factor"),n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("ndiv2"),n.i32_const(l))))),n.setLocal("i",n.i32_const(0)),n.block(n.loop(n.br_if(1,n.i32_ge_u(n.getLocal("i"),n.getLocal("ndiv2"))),n.setLocal("idx1",n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("i"),n.i32_const(l)))),n.setLocal("idx2",n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.i32_sub(n.i32_sub(n.getLocal("n"),n.i32_const(1)),n.getLocal("i")),n.i32_const(l)))),n.call(i,n.getLocal("idx2"),n.getLocal("factor"),c),n.call(i,n.getLocal("idx1"),n.getLocal("factor"),n.getLocal("idx2")),n.call(e+"_copy",c,n.getLocal("idx1")),n.setLocal("i",n.i32_add(n.getLocal("i"),n.i32_const(1))),n.br(0))))}(),function(){const c=t.addFunction(a+"_prepareLagrangeEvaluation");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addParam("totalBits","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("pShiftToM","i32"),c.addLocal("pSConst","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));c.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(A),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.setLocal("pSConst",s.i32_add(s.i32_const(C),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(i,s.getLocal("idx1"),s.getLocal("pShiftToM"),d),s.call(e+"_sub",s.getLocal("idx2"),d,d),s.call(e+"_sub",s.getLocal("idx1"),s.getLocal("idx2"),s.getLocal("idx2")),s.call(i,d,s.getLocal("pSConst"),s.getLocal("idx1")),s.call(i,s.getLocal("idx2"),r,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),t.exportFunction(a+"_fft"),t.exportFunction(a+"_ifft"),t.exportFunction(a+"_rawfft"),t.exportFunction(a+"_fftJoin"),t.exportFunction(a+"_fftJoinExt"),t.exportFunction(a+"_fftJoinExtInv"),t.exportFunction(a+"_fftMix"),t.exportFunction(a+"_fftFinal"),t.exportFunction(a+"_prepareLagrangeEvaluation")},$t=function(t,a,e){const o=8*t.modules[e].n64;return function(){const i=t.addFunction(a+"_zero");i.addParam("px","i32"),i.addParam("n","i32"),i.addLocal("lastp","i32"),i.addLocal("p","i32");const n=i.getCodeBuilder();i.addCode(n.setLocal("p",n.getLocal("px")),n.setLocal("lastp",n.i32_add(n.getLocal("px"),n.i32_mul(n.getLocal("n"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("p"),n.getLocal("lastp"))),n.call(e+"_zero",n.getLocal("p")),n.setLocal("p",n.i32_add(n.getLocal("p"),n.i32_const(o))),n.br(0))))}(),function(){const i=t.addFunction(a+"_constructLC");i.addParam("ppolynomials","i32"),i.addParam("psignals","i32"),i.addParam("nSignals","i32"),i.addParam("pres","i32"),i.addLocal("i","i32"),i.addLocal("j","i32"),i.addLocal("pp","i32"),i.addLocal("ps","i32"),i.addLocal("pd","i32"),i.addLocal("ncoefs","i32");const n=i.getCodeBuilder(),l=n.i32_const(t.alloc(o));i.addCode(n.setLocal("i",n.i32_const(0)),n.setLocal("pp",n.getLocal("ppolynomials")),n.setLocal("ps",n.getLocal("psignals")),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("i"),n.getLocal("nSignals"))),n.setLocal("ncoefs",n.i32_load(n.getLocal("pp"))),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(4))),n.setLocal("j",n.i32_const(0)),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("j"),n.getLocal("ncoefs"))),n.setLocal("pd",n.i32_add(n.getLocal("pres"),n.i32_mul(n.i32_load(n.getLocal("pp")),n.i32_const(o)))),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(4))),n.call(e+"_mul",n.getLocal("ps"),n.getLocal("pp"),l),n.call(e+"_add",l,n.getLocal("pd"),n.getLocal("pd")),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(o))),n.setLocal("j",n.i32_add(n.getLocal("j"),n.i32_const(1))),n.br(0))),n.setLocal("ps",n.i32_add(n.getLocal("ps"),n.i32_const(o))),n.setLocal("i",n.i32_add(n.getLocal("i"),n.i32_const(1))),n.br(0))))}(),t.exportFunction(a+"_zero"),t.exportFunction(a+"_constructLC"),a},jt=function(t,a,e){const o=8*t.modules[e].n64;return function(){const i=t.addFunction(a+"_buildABC");i.addParam("pCoefs","i32"),i.addParam("nCoefs","i32"),i.addParam("pWitness","i32"),i.addParam("pA","i32"),i.addParam("pB","i32"),i.addParam("pC","i32"),i.addParam("offsetOut","i32"),i.addParam("nOut","i32"),i.addParam("offsetWitness","i32"),i.addParam("nWitness","i32"),i.addLocal("it","i32"),i.addLocal("ita","i32"),i.addLocal("itb","i32"),i.addLocal("last","i32"),i.addLocal("m","i32"),i.addLocal("c","i32"),i.addLocal("s","i32"),i.addLocal("pOut","i32");const n=i.getCodeBuilder(),l=n.i32_const(t.alloc(o));i.addCode(n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("nOut"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_zero",n.getLocal("ita")),n.call(e+"_zero",n.getLocal("itb")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.br(0))),n.setLocal("it",n.getLocal("pCoefs")),n.setLocal("last",n.i32_add(n.getLocal("pCoefs"),n.i32_mul(n.getLocal("nCoefs"),n.i32_const(o+12)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("it"),n.getLocal("last"))),n.setLocal("s",n.i32_load(n.getLocal("it"),8)),n.if(n.i32_or(n.i32_lt_u(n.getLocal("s"),n.getLocal("offsetWitness")),n.i32_ge_u(n.getLocal("s"),n.i32_add(n.getLocal("offsetWitness"),n.getLocal("nWitness")))),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),...n.br(1)]),n.setLocal("m",n.i32_load(n.getLocal("it"))),n.if(n.i32_eq(n.getLocal("m"),n.i32_const(0)),n.setLocal("pOut",n.getLocal("pA")),n.if(n.i32_eq(n.getLocal("m"),n.i32_const(1)),n.setLocal("pOut",n.getLocal("pB")),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),...n.br(1)])),n.setLocal("c",n.i32_load(n.getLocal("it"),4)),n.if(n.i32_or(n.i32_lt_u(n.getLocal("c"),n.getLocal("offsetOut")),n.i32_ge_u(n.getLocal("c"),n.i32_add(n.getLocal("offsetOut"),n.getLocal("nOut")))),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),...n.br(1)]),n.setLocal("pOut",n.i32_add(n.getLocal("pOut"),n.i32_mul(n.i32_sub(n.getLocal("c"),n.getLocal("offsetOut")),n.i32_const(o)))),n.call(e+"_mul",n.i32_add(n.getLocal("pWitness"),n.i32_mul(n.i32_sub(n.getLocal("s"),n.getLocal("offsetWitness")),n.i32_const(o))),n.i32_add(n.getLocal("it"),n.i32_const(12)),l),n.call(e+"_add",n.getLocal("pOut"),l,n.getLocal("pOut")),n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),n.br(0))),n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("it",n.getLocal("pC")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("nOut"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_mul",n.getLocal("ita"),n.getLocal("itb"),n.getLocal("it")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o))),n.br(0))))}(),function(){const i=t.addFunction(a+"_joinABC");i.addParam("pA","i32"),i.addParam("pB","i32"),i.addParam("pC","i32"),i.addParam("n","i32"),i.addParam("pP","i32"),i.addLocal("ita","i32"),i.addLocal("itb","i32"),i.addLocal("itc","i32"),i.addLocal("itp","i32"),i.addLocal("last","i32");const n=i.getCodeBuilder(),l=n.i32_const(t.alloc(o));i.addCode(n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("itc",n.getLocal("pC")),n.setLocal("itp",n.getLocal("pP")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("n"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_mul",n.getLocal("ita"),n.getLocal("itb"),l),n.call(e+"_sub",l,n.getLocal("itc"),n.getLocal("itp")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.setLocal("itc",n.i32_add(n.getLocal("itc"),n.i32_const(o))),n.setLocal("itp",n.i32_add(n.getLocal("itp"),n.i32_const(o))),n.br(0))))}(),function(){const i=t.addFunction(a+"_batchAdd");i.addParam("pa","i32"),i.addParam("pb","i32"),i.addParam("n","i32"),i.addParam("pr","i32"),i.addLocal("ita","i32"),i.addLocal("itb","i32"),i.addLocal("itr","i32"),i.addLocal("last","i32");const n=i.getCodeBuilder();i.addCode(n.setLocal("ita",n.getLocal("pa")),n.setLocal("itb",n.getLocal("pb")),n.setLocal("itr",n.getLocal("pr")),n.setLocal("last",n.i32_add(n.getLocal("pa"),n.i32_mul(n.getLocal("n"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_add",n.getLocal("ita"),n.getLocal("itb"),n.getLocal("itr")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.setLocal("itr",n.i32_add(n.getLocal("itr"),n.i32_const(o))),n.br(0))))}(),t.exportFunction(a+"_buildABC"),t.exportFunction(a+"_joinABC"),t.exportFunction(a+"_batchAdd"),a},Vt=function(t,a,e,o,i,n,l,c){const s=t.addFunction(a);s.addParam("pIn","i32"),s.addParam("n","i32"),s.addParam("pFirst","i32"),s.addParam("pInc","i32"),s.addParam("pOut","i32"),s.addLocal("pOldFree","i32"),s.addLocal("i","i32"),s.addLocal("pFrom","i32"),s.addLocal("pTo","i32");const r=s.getCodeBuilder(),d=r.i32_const(t.alloc(l));s.addCode(r.setLocal("pFrom",r.getLocal("pIn")),r.setLocal("pTo",r.getLocal("pOut"))),s.addCode(r.call(o+"_copy",r.getLocal("pFirst"),d)),s.addCode(r.setLocal("i",r.i32_const(0)),r.block(r.loop(r.br_if(1,r.i32_eq(r.getLocal("i"),r.getLocal("n"))),r.call(c,r.getLocal("pFrom"),d,r.getLocal("pTo")),r.setLocal("pFrom",r.i32_add(r.getLocal("pFrom"),r.i32_const(i))),r.setLocal("pTo",r.i32_add(r.getLocal("pTo"),r.i32_const(n))),r.call(o+"_mul",d,r.getLocal("pInc"),d),r.setLocal("i",r.i32_add(r.getLocal("i"),r.i32_const(1))),r.br(0)))),t.exportFunction(a)};const Kt=K,Ht=xt,Zt=Bt,Wt=Pt,Yt=Gt,Jt=Ut,Xt=Dt,ta=$t,aa=jt,ea=Vt,{bitLength:oa,modInv:ia,isOdd:na,isNegative:la}=J;const ca=K,sa=xt,ra=Bt,da=Pt,ua=Gt,_a=Ut,ga=Dt,fa=$t,pa=jt,ha=Vt,{bitLength:ma,isOdd:La,isNegative:ba}=J;var wa=function(t,a){const e=a||"bn128";if(t.modules[e])return e;const o=21888242871839275222246405745257275088696311157297823662689037894645226208583n,i=21888242871839275222246405745257275088548364400416034343698204186575808495617n,n=Math.floor((oa(o-1n)-1)/64)+1,l=8*n,c=l,s=l,r=2*s,d=12*s,u=t.alloc(Kt.bigInt2BytesLE(i,c)),_=Ht(t,o,"f1m");Zt(t,i,"fr","frm");const g=t.alloc(Kt.bigInt2BytesLE(b(3n),s)),f=Jt(t,"g1m","f1m",g);Xt(t,"frm","frm","frm","frm_mul"),ta(t,"pol","frm"),aa(t,"qap","frm");const p=Wt(t,"f1m_neg","f2m","f1m"),h=t.alloc([...Kt.bigInt2BytesLE(b(19485874751759354771024239261021720505790618469301721065564631296452457478373n),s),...Kt.bigInt2BytesLE(b(266929791119991161246907387137283842545076965332900288569378510910307636690n),s)]),m=Jt(t,"g2m","f2m",h);function L(a,e){const o=t.addFunction(a);o.addParam("pG","i32"),o.addParam("pFr","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),n=i.i32_const(t.alloc(l));o.addCode(i.call("frm_fromMontgomery",i.getLocal("pFr"),n),i.call(e,i.getLocal("pG"),n,i.i32_const(l),i.getLocal("pr"))),t.exportFunction(a)}function b(t){return BigInt(t)*(1n<0n;)na(a)?e.push(1):e.push(0),a>>=1n;return e}(29793968203157093288n),T=t.alloc(z),M=3*r,U=z.length-1,Q=z.reduce(((t,a)=>t+(0!=a?1:0)),0),k=6*l,R=3*l*2+(Q+U+1)*M;t.modules[e]={n64:n,pG1gen:y,pG1zero:C,pG1b:g,pG2gen:F,pG2zero:B,pG2b:h,pq:t.modules.f1m.pq,pr:u,pOneT:E,prePSize:k,preQSize:R,r:i.toString(),q:o.toString()};const N=4965661367192848881n;function D(a){const i=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[8376118865763821496583973867626364092589906065868298776909617916018768340080n,16469823323077808223889137241176536799009286646108169935659301613961712198316n],[21888242871839275220042445260109153167277707414472061641714758635765020556617n,0n],[11697423496358154304825782922584725312912383441159505038794027105778954184319n,303847389135065887422783454877609941456349188919719272345083954437860409601n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[3321304630594332808241809054958361220322477375291206261884409189760185844239n,5722266937896532885780051958958348231143373700109372999374820235121374419868n],[21888242871839275222246405745257275088696311157297823662689037894645226208582n,0n],[13512124006075453725662431877630910996106405091429524885779419978626457868503n,5418419548761466998357268504080738289687024511189653727029736280683514010267n],[2203960485148121921418603742825762020974279258880205651966n,0n],[10190819375481120917420622822672549775783927716138318623895010788866272024264n,21584395482704209334823622290379665147239961968378104390343953940207365798982n],[2203960485148121921418603742825762020974279258880205651967n,0n],[18566938241244942414004596690298913868373833782006617400804628704885040364344n,16165975933942742336466353786298926857552937457188450663314217659523851788715n]]],n=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[21575463638280843010398324269430826099269044274347216827212613867836435027261n,10307601595873709700152284273816112264069230130616436755625194854815875713954n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[3772000881919853776433695186713858239009073593817195771773381919316419345261n,2236595495967245188281701248203181795121068902605861227855261137820944008926n],[2203960485148121921418603742825762020974279258880205651966n,0n],[18429021223477853657660792034369865839114504446431234726392080002137598044644n,9344045779998320333812420223237981029506012124075525679208581902008406485703n]],[[1n,0n],[2581911344467009335267311115468803099551665605076196740867805258568234346338n,19937756971775647987995932169929341994314640652964949448313374472400716661030n],[2203960485148121921418603742825762020974279258880205651966n,0n],[5324479202449903542726783395506214481928257762400643279780343368557297135718n,16208900380737693084919495127334387981393726419856888799917914180988844123039n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[13981852324922362344252311234282257507216387789820983642040889267519694726527n,7629828391165209371577384193250820201684255241773809077146787135900891633097n]]],l=t.addFunction(e+"__frobeniusMap"+a);l.addParam("x","i32"),l.addParam("r","i32");const c=l.getCodeBuilder();for(let e=0;e<6;e++){const o=0==e?c.getLocal("x"):c.i32_add(c.getLocal("x"),c.i32_const(e*r)),u=o,g=c.i32_add(c.getLocal("x"),c.i32_const(e*r+s)),f=0==e?c.getLocal("r"):c.i32_add(c.getLocal("r"),c.i32_const(e*r)),h=f,m=c.i32_add(c.getLocal("r"),c.i32_const(e*r+s)),L=d(i[Math.floor(e/3)][a%12],n[e%3][a%6]),w=t.alloc([...Kt.bigInt2BytesLE(b(L[0]),32),...Kt.bigInt2BytesLE(b(L[1]),32)]);a%2==1?l.addCode(c.call(_+"_copy",u,h),c.call(_+"_neg",g,m),c.call(p+"_mul",f,c.i32_const(w),f)):l.addCode(c.call(p+"_mul",o,c.i32_const(w),f))}function d(t,a){const e=BigInt(t[0]),i=BigInt(t[1]),n=BigInt(a[0]),l=BigInt(a[1]),c=[(e*n-i*l)%o,(e*l+i*n)%o];return la(c[0])&&(c[0]=c[0]+o),c}}function $(a,o){const i=function(t){let a=t;const e=[];for(;a>0n;){if(na(a)){const t=2-Number(a%4n);e.push(t),a-=BigInt(t)}else e.push(0);a>>=1n}return e}(a).map((t=>-1==t?255:t)),n=t.alloc(i),l=t.addFunction(e+"__cyclotomicExp_"+o);l.addParam("x","i32"),l.addParam("r","i32"),l.addLocal("bit","i32"),l.addLocal("i","i32");const c=l.getCodeBuilder(),s=c.getLocal("x"),r=c.getLocal("r"),u=c.i32_const(t.alloc(d));l.addCode(c.call(G+"_conjugate",s,u),c.call(G+"_one",r),c.if(c.teeLocal("bit",c.i32_load8_s(c.i32_const(i.length-1),n)),c.if(c.i32_eq(c.getLocal("bit"),c.i32_const(1)),c.call(G+"_mul",r,s,r),c.call(G+"_mul",r,u,r))),c.setLocal("i",c.i32_const(i.length-2)),c.block(c.loop(c.call(e+"__cyclotomicSquare",r,r),c.if(c.teeLocal("bit",c.i32_load8_s(c.getLocal("i"),n)),c.if(c.i32_eq(c.getLocal("bit"),c.i32_const(1)),c.call(G+"_mul",r,s,r),c.call(G+"_mul",r,u,r))),c.br_if(1,c.i32_eqz(c.getLocal("i"))),c.setLocal("i",c.i32_sub(c.getLocal("i"),c.i32_const(1))),c.br(0))))}function j(){!function(){const a=t.addFunction(e+"__cyclotomicSquare");a.addParam("x","i32"),a.addParam("r","i32");const o=a.getCodeBuilder(),i=o.getLocal("x"),n=o.i32_add(o.getLocal("x"),o.i32_const(r)),l=o.i32_add(o.getLocal("x"),o.i32_const(2*r)),c=o.i32_add(o.getLocal("x"),o.i32_const(3*r)),s=o.i32_add(o.getLocal("x"),o.i32_const(4*r)),d=o.i32_add(o.getLocal("x"),o.i32_const(5*r)),u=o.getLocal("r"),_=o.i32_add(o.getLocal("r"),o.i32_const(r)),g=o.i32_add(o.getLocal("r"),o.i32_const(2*r)),f=o.i32_add(o.getLocal("r"),o.i32_const(3*r)),h=o.i32_add(o.getLocal("r"),o.i32_const(4*r)),m=o.i32_add(o.getLocal("r"),o.i32_const(5*r)),L=o.i32_const(t.alloc(r)),b=o.i32_const(t.alloc(r)),w=o.i32_const(t.alloc(r)),y=o.i32_const(t.alloc(r)),A=o.i32_const(t.alloc(r)),C=o.i32_const(t.alloc(r)),x=o.i32_const(t.alloc(r)),F=o.i32_const(t.alloc(r));a.addCode(o.call(p+"_mul",i,s,x),o.call(p+"_mul",s,o.i32_const(v),L),o.call(p+"_add",i,L,L),o.call(p+"_add",i,s,F),o.call(p+"_mul",F,L,L),o.call(p+"_mul",o.i32_const(v),x,F),o.call(p+"_add",x,F,F),o.call(p+"_sub",L,F,L),o.call(p+"_add",x,x,b),o.call(p+"_mul",c,l,x),o.call(p+"_mul",l,o.i32_const(v),w),o.call(p+"_add",c,w,w),o.call(p+"_add",c,l,F),o.call(p+"_mul",F,w,w),o.call(p+"_mul",o.i32_const(v),x,F),o.call(p+"_add",x,F,F),o.call(p+"_sub",w,F,w),o.call(p+"_add",x,x,y),o.call(p+"_mul",n,d,x),o.call(p+"_mul",d,o.i32_const(v),A),o.call(p+"_add",n,A,A),o.call(p+"_add",n,d,F),o.call(p+"_mul",F,A,A),o.call(p+"_mul",o.i32_const(v),x,F),o.call(p+"_add",x,F,F),o.call(p+"_sub",A,F,A),o.call(p+"_add",x,x,C),o.call(p+"_sub",L,i,u),o.call(p+"_add",u,u,u),o.call(p+"_add",L,u,u),o.call(p+"_add",b,s,h),o.call(p+"_add",h,h,h),o.call(p+"_add",b,h,h),o.call(p+"_mul",C,o.i32_const(P),F),o.call(p+"_add",F,c,f),o.call(p+"_add",f,f,f),o.call(p+"_add",F,f,f),o.call(p+"_sub",A,l,g),o.call(p+"_add",g,g,g),o.call(p+"_add",A,g,g),o.call(p+"_sub",w,n,_),o.call(p+"_add",_,_,_),o.call(p+"_add",w,_,_),o.call(p+"_add",y,d,m),o.call(p+"_add",m,m,m),o.call(p+"_add",y,m,m))}(),$(N,"w0");const a=t.addFunction(e+"__finalExponentiationLastChunk");a.addParam("x","i32"),a.addParam("r","i32");const o=a.getCodeBuilder(),i=o.getLocal("x"),n=o.getLocal("r"),l=o.i32_const(t.alloc(d)),c=o.i32_const(t.alloc(d)),s=o.i32_const(t.alloc(d)),u=o.i32_const(t.alloc(d)),_=o.i32_const(t.alloc(d)),g=o.i32_const(t.alloc(d)),f=o.i32_const(t.alloc(d)),h=o.i32_const(t.alloc(d)),m=o.i32_const(t.alloc(d)),L=o.i32_const(t.alloc(d)),b=o.i32_const(t.alloc(d)),w=o.i32_const(t.alloc(d)),y=o.i32_const(t.alloc(d)),A=o.i32_const(t.alloc(d)),C=o.i32_const(t.alloc(d)),x=o.i32_const(t.alloc(d)),F=o.i32_const(t.alloc(d)),I=o.i32_const(t.alloc(d)),B=o.i32_const(t.alloc(d)),E=o.i32_const(t.alloc(d)),S=o.i32_const(t.alloc(d));a.addCode(o.call(e+"__cyclotomicExp_w0",i,l),o.call(G+"_conjugate",l,l),o.call(e+"__cyclotomicSquare",l,c),o.call(e+"__cyclotomicSquare",c,s),o.call(G+"_mul",s,c,u),o.call(e+"__cyclotomicExp_w0",u,_),o.call(G+"_conjugate",_,_),o.call(e+"__cyclotomicSquare",_,g),o.call(e+"__cyclotomicExp_w0",g,f),o.call(G+"_conjugate",f,f),o.call(G+"_conjugate",u,h),o.call(G+"_conjugate",f,m),o.call(G+"_mul",m,_,L),o.call(G+"_mul",L,h,b),o.call(G+"_mul",b,c,w),o.call(G+"_mul",b,_,y),o.call(G+"_mul",y,i,A),o.call(e+"__frobeniusMap1",w,C),o.call(G+"_mul",C,A,x),o.call(e+"__frobeniusMap2",b,F),o.call(G+"_mul",F,x,I),o.call(G+"_conjugate",i,B),o.call(G+"_mul",B,w,E),o.call(e+"__frobeniusMap3",E,S),o.call(G+"_mul",S,I,n))}const V=t.alloc(k),K=t.alloc(R);function H(a){const o=t.addFunction(e+"_pairingEq"+a);for(let t=0;t0n;)La(a)?e.push(1):e.push(0),a>>=1n;return e}(0xd201000000010000n),z=t.alloc(G),T=3*s,M=G.length-1,U=G.reduce(((t,a)=>t+(0!=a?1:0)),0),Q=6*l,k=3*l*2+(U+M+1)*T,R=!0,N=15132376222941642752n;function D(a){const e=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n,151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n,0n],[2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n,1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n,877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n],[4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n,0n],[151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n,3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n,2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n,0n],[877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n,3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n]]],i=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[0n,4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[0n,1n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[0n,793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n]],[[1n,0n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n,0n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n,0n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n,0n]]],n=t.addFunction(q+"_frobeniusMap"+a);n.addParam("x","i32"),n.addParam("r","i32");const r=n.getCodeBuilder();for(let o=0;o<6;o++){const u=0==o?r.getLocal("x"):r.i32_add(r.getLocal("x"),r.i32_const(o*s)),_=u,g=r.i32_add(r.getLocal("x"),r.i32_const(o*s+c)),p=0==o?r.getLocal("r"):r.i32_add(r.getLocal("r"),r.i32_const(o*s)),h=p,L=r.i32_add(r.getLocal("r"),r.i32_const(o*s+c)),b=d(e[Math.floor(o/3)][a%12],i[o%3][a%6]),w=t.alloc([...ca.bigInt2BytesLE(y(b[0]),l),...ca.bigInt2BytesLE(y(b[1]),l)]);a%2==1?n.addCode(r.call(f+"_copy",_,h),r.call(f+"_neg",g,L),r.call(m+"_mul",p,r.i32_const(w),p)):n.addCode(r.call(m+"_mul",u,r.i32_const(w),p))}function d(t,a){const e=t[0],i=t[1],n=a[0],l=a[1],c=[(e*n-i*l)%o,(e*l+i*n)%o];return ba(c[0])&&(c[0]=c[0]+o),c}}function $(a,o,i){const n=function(t){let a=t;const e=[];for(;a>0n;){if(La(a)){const t=2-Number(a%4n);e.push(t),a-=BigInt(t)}else e.push(0);a>>=1n}return e}(a).map((t=>-1==t?255:t)),l=t.alloc(n),c=t.addFunction(e+"__cyclotomicExp_"+i);c.addParam("x","i32"),c.addParam("r","i32"),c.addLocal("bit","i32"),c.addLocal("i","i32");const s=c.getCodeBuilder(),d=s.getLocal("x"),u=s.getLocal("r"),_=s.i32_const(t.alloc(r));c.addCode(s.call(q+"_conjugate",d,_),s.call(q+"_one",u),s.if(s.teeLocal("bit",s.i32_load8_s(s.i32_const(n.length-1),l)),s.if(s.i32_eq(s.getLocal("bit"),s.i32_const(1)),s.call(q+"_mul",u,d,u),s.call(q+"_mul",u,_,u))),s.setLocal("i",s.i32_const(n.length-2)),s.block(s.loop(s.call(e+"__cyclotomicSquare",u,u),s.if(s.teeLocal("bit",s.i32_load8_s(s.getLocal("i"),l)),s.if(s.i32_eq(s.getLocal("bit"),s.i32_const(1)),s.call(q+"_mul",u,d,u),s.call(q+"_mul",u,_,u))),s.br_if(1,s.i32_eqz(s.getLocal("i"))),s.setLocal("i",s.i32_sub(s.getLocal("i"),s.i32_const(1))),s.br(0)))),o&&c.addCode(s.call(q+"_conjugate",u,u))}t.modules[e]={n64q:n,n64r:d,n8q:l,n8r:u,pG1gen:C,pG1zero:F,pG1b:p,pG2gen:B,pG2zero:v,pG2b:L,pq:t.modules.f1m.pq,pr:g,pOneT:S,r:i,q:o,prePSize:Q,preQSize:k},function(){const a=t.addFunction(O+"_mul1");a.addParam("pA","i32"),a.addParam("pC1","i32"),a.addParam("pR","i32");const e=a.getCodeBuilder(),o=e.getLocal("pA"),i=e.i32_add(e.getLocal("pA"),e.i32_const(2*c)),n=e.i32_add(e.getLocal("pA"),e.i32_const(4*c)),l=e.getLocal("pC1"),s=e.getLocal("pR"),r=e.i32_add(e.getLocal("pR"),e.i32_const(2*c)),d=e.i32_add(e.getLocal("pR"),e.i32_const(4*c)),u=e.i32_const(t.alloc(2*c)),_=e.i32_const(t.alloc(2*c));a.addCode(e.call(m+"_add",o,i,u),e.call(m+"_add",i,n,_),e.call(m+"_mul",i,l,d),e.call(m+"_mul",_,l,s),e.call(m+"_sub",s,d,s),e.call(m+"_mulNR",s,s),e.call(m+"_mul",u,l,r),e.call(m+"_sub",r,d,r))}(),function(){const a=t.addFunction(O+"_mul01");a.addParam("pA","i32"),a.addParam("pC0","i32"),a.addParam("pC1","i32"),a.addParam("pR","i32");const e=a.getCodeBuilder(),o=e.getLocal("pA"),i=e.i32_add(e.getLocal("pA"),e.i32_const(2*c)),n=e.i32_add(e.getLocal("pA"),e.i32_const(4*c)),l=e.getLocal("pC0"),s=e.getLocal("pC1"),r=e.getLocal("pR"),d=e.i32_add(e.getLocal("pR"),e.i32_const(2*c)),u=e.i32_add(e.getLocal("pR"),e.i32_const(4*c)),_=e.i32_const(t.alloc(2*c)),g=e.i32_const(t.alloc(2*c)),f=e.i32_const(t.alloc(2*c)),p=e.i32_const(t.alloc(2*c));a.addCode(e.call(m+"_mul",o,l,_),e.call(m+"_mul",i,s,g),e.call(m+"_add",o,i,f),e.call(m+"_add",o,n,p),e.call(m+"_add",i,n,r),e.call(m+"_mul",r,s,r),e.call(m+"_sub",r,g,r),e.call(m+"_mulNR",r,r),e.call(m+"_add",r,_,r),e.call(m+"_add",l,s,d),e.call(m+"_mul",d,f,d),e.call(m+"_sub",d,_,d),e.call(m+"_sub",d,g,d),e.call(m+"_mul",p,l,u),e.call(m+"_sub",u,_,u),e.call(m+"_add",u,g,u))}(),function(){const a=t.addFunction(q+"_mul014");a.addParam("pA","i32"),a.addParam("pC0","i32"),a.addParam("pC1","i32"),a.addParam("pC4","i32"),a.addParam("pR","i32");const e=a.getCodeBuilder(),o=e.getLocal("pA"),i=e.i32_add(e.getLocal("pA"),e.i32_const(6*c)),n=e.getLocal("pC0"),l=e.getLocal("pC1"),s=e.getLocal("pC4"),r=e.i32_const(t.alloc(6*c)),d=e.i32_const(t.alloc(6*c)),u=e.i32_const(t.alloc(2*c)),_=e.getLocal("pR"),g=e.i32_add(e.getLocal("pR"),e.i32_const(6*c));a.addCode(e.call(O+"_mul01",o,n,l,r),e.call(O+"_mul1",i,s,d),e.call(m+"_add",l,s,u),e.call(O+"_add",i,o,g),e.call(O+"_mul01",g,n,u,g),e.call(O+"_sub",g,r,g),e.call(O+"_sub",g,d,g),e.call(O+"_copy",d,_),e.call(O+"_mulNR",_,_),e.call(O+"_add",_,r,_))}(),function(){const a=t.addFunction(e+"_ell");a.addParam("pP","i32"),a.addParam("pCoefs","i32"),a.addParam("pF","i32");const o=a.getCodeBuilder(),i=o.getLocal("pP"),n=o.i32_add(o.getLocal("pP"),o.i32_const(l)),s=o.getLocal("pF"),r=o.getLocal("pCoefs"),d=o.i32_add(o.getLocal("pCoefs"),o.i32_const(c)),u=o.i32_add(o.getLocal("pCoefs"),o.i32_const(2*c)),_=o.i32_add(o.getLocal("pCoefs"),o.i32_const(3*c)),g=o.i32_add(o.getLocal("pCoefs"),o.i32_const(4*c)),p=t.alloc(2*c),h=o.i32_const(p),m=o.i32_const(p),L=o.i32_const(p+c),b=t.alloc(2*c),w=o.i32_const(b),y=o.i32_const(b),A=o.i32_const(b+c);a.addCode(o.call(f+"_mul",r,n,m),o.call(f+"_mul",d,n,L),o.call(f+"_mul",u,i,y),o.call(f+"_mul",_,i,A),o.call(q+"_mul014",s,g,w,h,s))}();const j=t.alloc(Q),V=t.alloc(k);function K(a){const o=t.addFunction(e+"_pairingEq"+a);for(let t=0;t>=1;return e}function xa(t,a){return(Aa[t>>>24]|Aa[t>>>16&255]<<8|Aa[t>>>8&255]<<16|Aa[255&t]<<24)>>>32-a}function Fa(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 Ia(t,a){const e=new Uint8Array(a*t.length);for(let o=0;o0;){const t=l+c>Ea?Ea-l:c,a=new Uint8Array(this.buffers[n].buffer,this.buffers[n].byteOffset+l,t);if(t==e)return a.slice();i||(i=e<=Ea?new Uint8Array(e):new va(e)),i.set(a,e-c),c-=t,n++,l=0}return i}set(t,a){void 0===a&&(a=0);const e=t.byteLength;if(0==e)return;const o=Math.floor(a/Ea);if(o==Math.floor((a+e-1)/Ea))return t instanceof va&&1==t.buffers.length?this.buffers[o].set(t.buffers[0],a%Ea):this.buffers[o].set(t,a%Ea);let i=o,n=a%Ea,l=e;for(;l>0;){const a=n+l>Ea?Ea-n:l,o=t.slice(e-l,e-l+a);new Uint8Array(this.buffers[i].buffer,this.buffers[i].byteOffset+n,a).set(o),l-=a,i++,n=0}}}function Sa(t,a,e,o){return async function(i){const n=Math.floor(i.byteLength/e);if(n*e!==i.byteLength)throw new Error("Invalid buffer size");const l=Math.floor(n/t.concurrency),c=[];for(let s=0;s=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=Sa(t,a+"_batchToMontgomery",this.n8,this.n8),this.batchFromMontgomery=Sa(t,a+"_batchFromMontgomery",this.n8,this.n8)}op2(t,a,e){return this.tm.setBuff(this.pOp1,a),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op2Bool(t,a,e){return this.tm.setBuff(this.pOp1,a),this.tm.setBuff(this.pOp2,e),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2)}op1(t,a){return this.tm.setBuff(this.pOp1,a),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op1Bool(t,a){return this.tm.setBuff(this.pOp1,a),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3)}add(t,a){return this.op2("_add",t,a)}eq(t,a){return this.op2Bool("_eq",t,a)}isZero(t){return this.op1Bool("_isZero",t)}sub(t,a){return this.op2("_sub",t,a)}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,a){return this.op2("_mul",t,a)}div(t,a){return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,a),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,a){return a instanceof Uint8Array||(a=S(o(a))),this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,a),this.tm.instance.exports[this.prefix+"_exp"](this.pOp1,this.pOp2,a.byteLength,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}isNegative(t){return this.op1Bool("_isNegative",t)}e(t,a){if(t instanceof Uint8Array)return t;let e=o(t,a);n(e)?(e=h(e),A(e,this.p)&&(e=w(e,this.p)),e=p(this.p,e)):A(e,this.p)&&(e=w(e,this.p));const l=function(t,a){let e=t;void 0===a&&0==(a=Math.floor((i(t)-1)/8)+1)&&(a=1);const o=new Uint8Array(a),n=new DataView(o.buffer);let l=0;for(;l>=BigInt(32)):l+2<=a?(n.setUint16(l,Number(e&BigInt(65535)),!0),l+=2,e>>=BigInt(16)):(n.setUint8(l,Number(e&BigInt(255)),!0),l+=1,e>>=BigInt(8));if(e)throw new Error("Number does not fit in this length");return o}(e,this.n8);return this.toMontgomery(l)}toString(t,a){return v(B(this.fromMontgomery(t),0),a)}fromRng(t){let a;const e=new Uint8Array(this.n8);do{a=P;for(let e=0;eo.buffer.byteLength){const i=o.buffer.byteLength/65536;let n=Math.floor((e[0]+t)/65536)+1;n>a&&(n=a),o.grow(n-i)}return i}function l(t){const a=n(t.byteLength);return s(a,t),a}function c(t,a){const e=new Uint8Array(o.buffer);return new Uint8Array(e.buffer,e.byteOffset+t,a)}function s(t,a){new Uint8Array(o.buffer).set(new Uint8Array(a),t)}function r(t){if("INIT"==t[0].cmd)return i(t[0]);const a={vars:[],out:[]},r=new Uint32Array(o.buffer,0,1)[0];for(let o=0;o{this.reject=a,this.resolve=t}))}}var ka;const Ra="data:application/javascript;base64,"+(ka="("+za.toString()+")(self)",globalThis.btoa(ka));class Na{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,a,e,o){if(this.working[t])throw new Error("Posting a job t a working worker");return this.working[t]=!0,this.pendingDeferreds[t]=o||new Qa,this.workers[t].postMessage(a,e),this.pendingDeferreds[t].promise}processWorks(){for(let t=0;t0;t++)if(0==this.working[t]){const a=this.actionQueue.shift();this.postAction(t,a.data,a.transfers,a.deferred)}}queueAction(t,a){const e=new Qa;if(this.singleThread){const a=this.taskManager(t);e.resolve(a)}else this.actionQueue.push({data:t,transfers:a,deferred:e}),this.processWorks();return e.promise}resetMemory(){this.u32[0]=this.initalPFree}allocBuff(t){const a=this.alloc(t.byteLength);return this.setBuff(a,t),a}getBuff(t,a){return this.u8.slice(t,t+a)}setBuff(t,a){this.u8.set(new Uint8Array(a),t)}alloc(t){for(;3&this.u32[0];)this.u32[0]++;const a=this.u32[0];return this.u32[0]+=t,a}async terminate(){for(let t=0;tsetTimeout(a,t))))}}function Da(t,a){const e=t[a],o=t.Fr,i=t.tm;t[a].batchApplyKey=async function(t,n,l,c,s){let r,d,u,_,g;if(c=c||"affine",s=s||"affine","G1"==a)"jacobian"==c?(u=3*e.F.n8,r="g1m_batchApplyKey"):(u=2*e.F.n8,r="g1m_batchApplyKeyMixed"),_=3*e.F.n8,"jacobian"==s?g=3*e.F.n8:(d="g1m_batchToAffine",g=2*e.F.n8);else if("G2"==a)"jacobian"==c?(u=3*e.F.n8,r="g2m_batchApplyKey"):(u=2*e.F.n8,r="g2m_batchApplyKeyMixed"),_=3*e.F.n8,"jacobian"==s?g=3*e.F.n8:(d="g2m_batchToAffine",g=2*e.F.n8);else{if("Fr"!=a)throw new Error("Invalid group: "+a);r="frm_batchApplyKey",u=e.n8,_=e.n8,g=e.n8}const f=Math.floor(t.byteLength/u),p=Math.floor(f/i.concurrency),h=[];l=o.e(l);let m=o.e(n);for(let a=0;a=0;t--){if(!e.isZero(p))for(let t=0;tr&&(p=r),p<1024&&(p=1024);const h=[];for(let a=0;a(c&&c.debug(`Multiexp end: ${s}: ${a}/${u}`),t))))}const m=await Promise.all(h);let L=e.zero;for(let t=m.length-1;t>=0;t--)L=e.add(L,m[t]);return L}e.multiExp=async function(t,a,e,o){return await n(t,a,"jacobian",e,o)},e.multiExpAffine=async function(t,a,e,o){return await n(t,a,"affine",e,o)}}function Va(t,a){const e=t[a],o=t.Fr,i=e.tm;async function n(t,c,s,r,d,u){s=s||"affine",r=r||"affine";let _,g,f,p,h,m,L,b;"G1"==a?("affine"==s?(_=2*e.F.n8,p="g1m_batchToJacobian"):_=3*e.F.n8,g=3*e.F.n8,c&&(b="g1m_fftFinal"),L="g1m_fftJoin",m="g1m_fftMix","affine"==r?(f=2*e.F.n8,h="g1m_batchToAffine"):f=3*e.F.n8):"G2"==a?("affine"==s?(_=2*e.F.n8,p="g2m_batchToJacobian"):_=3*e.F.n8,g=3*e.F.n8,c&&(b="g2m_fftFinal"),L="g2m_fftJoin",m="g2m_fftMix","affine"==r?(f=2*e.F.n8,h="g2m_batchToAffine"):f=3*e.F.n8):"Fr"==a&&(_=e.n8,g=e.n8,f=e.n8,c&&(b="frm_fftFinal"),m="frm_fftMix",L="frm_fftJoin");let w=!1;Array.isArray(t)?(t=Ia(t,_),w=!0):t=t.slice(0,t.byteLength);const y=t.byteLength/_,A=Fa(y);if(1<1<<28?new va(2*u[0].byteLength):new Uint8Array(2*u[0].byteLength);return _.set(u[0]),_.set(u[1],u[0].byteLength),_}(t,s,r,d,u):await async function(t,a,e,i,c){let s,r;s=t.slice(0,t.byteLength/2),r=t.slice(t.byteLength/2,t.byteLength);const d=[];[s,r]=await l(s,r,"fftJoinExt",o.one,o.shift,a,"jacobian",i,c),d.push(n(s,!1,"jacobian",e,i,c)),d.push(n(r,!1,"jacobian",e,i,c));const u=await Promise.all(d);let _;_=u[0].byteLength>1<<28?new va(2*u[0].byteLength):new Uint8Array(2*u[0].byteLength);return _.set(u[0]),_.set(u[1],u[0].byteLength),_}(t,s,r,d,u),w?Ba(a,f):a}let C,x,F;c&&(C=o.inv(o.e(y))),function(t,a){const e=t.byteLength/a,o=Fa(e);if(e!=1<e){const o=t.slice(i*a,(i+1)*a);t.set(t.slice(e*a,(e+1)*a),i*a),t.set(o,e*a)}}}(t,_);let I=Math.min(16384,y),B=y/I;for(;B=16;)B*=2,I/=2;const E=Fa(I),v=[];for(let a=0;a(d&&d.debug(`${u}: fft ${A} mix end: ${a}/${B}`),t))))}F=await Promise.all(v);for(let t=0;t(d&&d.debug(`${u}: fft ${A} join ${t}/${A} ${l+1}/${a} ${c}/${e/2}`),o))))}const l=await Promise.all(n);for(let t=0;t0;a--)x.set(F[a],t),t+=I*f,delete F[a];x.set(F[0].slice(0,(I-1)*f),t),delete F[0]}else for(let t=0;t65536&&(w=65536);const y=[];for(let a=0;a(u&&u.debug(`${_}: fftJoinExt End: ${a}/${b}`),t))))}const A=await Promise.all(y);let C,x;b*h>1<<28?(C=new va(b*h),x=new va(b*h)):(C=new Uint8Array(b*h),x=new Uint8Array(b*h));let F=0;for(let t=0;to.s+1)throw s&&s.error("lagrangeEvaluations input too big"),new Error("lagrangeEvaluations input too big");let g=t.slice(0,t.byteLength/2),f=t.slice(t.byteLength/2,t.byteLength);const p=o.exp(o.shift,u/2),h=o.inv(o.sub(o.one,p));[g,f]=await l(g,f,"prepareLagrangeEvaluation",h,o.shiftInv,i,"jacobian",s,r+" prep");const m=[];let L;return m.push(n(g,!0,"jacobian",c,s,r+" t0")),m.push(n(f,!0,"jacobian",c,s,r+" t1")),[g,f]=await Promise.all(m),L=g.byteLength>1<<28?new va(2*g.byteLength):new Uint8Array(2*g.byteLength),L.set(g),L.set(f,g.byteLength),L},e.fftMix=async function(t){const n=3*e.F.n8;let l,c;if("G1"==a)l="g1m_fftMix",c="g1m_fftJoin";else if("G2"==a)l="g2m_fftMix",c="g2m_fftJoin";else{if("Fr"!=a)throw new Error("Invalid group");l="frm_fftMix",c="frm_fftJoin"}const s=Math.floor(t.byteLength/n),r=Fa(s);let d=1<=0;t--)g.set(_[t][0],f),f+=_[t][0].byteLength;return g}}async function Ka(t){const a=await async function(t,a){const e=new Na;e.memory=new WebAssembly.Memory({initial:Ua}),e.u8=new Uint8Array(e.memory.buffer),e.u32=new Uint32Array(e.memory.buffer);const o=await WebAssembly.compile(t.code);if(e.instance=await WebAssembly.instantiate(o,{env:{memory:e.memory}}),e.singleThread=a,e.initalPFree=e.u32[0],e.pq=t.pq,e.pr=t.pr,e.pG1gen=t.pG1gen,e.pG1zero=t.pG1zero,e.pG2gen=t.pG2gen,e.pG2zero=t.pG2zero,e.pOneT=t.pOneT,a)e.code=t.code,e.taskManager=za(),await e.taskManager([{cmd:"INIT",init:Ua,code:e.code.slice()}]),e.concurrency=1;else{let a;e.workers=[],e.pendingDeferreds=[],e.working=[],a="object"==typeof navigator&&navigator.hardwareConcurrency?navigator.hardwareConcurrency:Ta.cpus().length,0==a&&(a=2),a>64&&(a=64),e.concurrency=a;for(let t=0;t>8n&0xFFn)),a.push(Number(e>>16n&0xFFn)),a.push(Number(e>>24n&0xFFn)),a}function Ja(t){const a=function(t){for(var a=[],e=0;e>6,128|63&o):o<55296||o>=57344?a.push(224|o>>12,128|o>>6&63,128|63&o):(e++,o=65536+((1023&o)<<10|1023&t.charCodeAt(e)),a.push(240|o>>18,128|o>>12&63,128|o>>6&63,128|63&o))}return a}(t);return[...oe(a.length),...a]}function Xa(t){const a=[];let e=Ha(t);if(Za(e))throw new Error("Number cannot be negative");for(;!Wa(e);)a.push(Number(0x7Fn&e)),e>>=7n;0==a.length&&a.push(0);for(let t=0;t0xFFFFFFFFn)throw new Error("Number too big");if(a>0x7FFFFFFFn&&(a-=0x100000000n),a<-2147483648n)throw new Error("Number too small");return te(a)}function ee(t){let a=Ha(t);if(a>0xFFFFFFFFFFFFFFFFn)throw new Error("Number too big");if(a>0x7FFFFFFFFFFFFFFFn&&(a-=0x10000000000000000n),a<-9223372036854775808n)throw new Error("Number too small");return te(a)}function oe(t){let a=Ha(t);if(a>0xFFFFFFFFn)throw new Error("Number too big");return Xa(a)}function ie(t){return Array.from(t,(function(t){return("0"+(255&t).toString(16)).slice(-2)})).join("")}class ne{constructor(t){this.func=t,this.functionName=t.functionName,this.module=t.module}setLocal(t,a){const e=this.func.localIdxByName[t];if(void 0===e)throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${t} `);return[...a,33,...oe(e)]}teeLocal(t,a){const e=this.func.localIdxByName[t];if(void 0===e)throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${t} `);return[...a,34,...oe(e)]}getLocal(t){const a=this.func.localIdxByName[t];if(void 0===a)throw new Error(`Local Variable not defined: Function: ${this.functionName} local: ${t} `);return[32,...oe(a)]}i64_load8_s(t,a,e){return[...t,48,void 0===e?0:e,...oe(a||0)]}i64_load8_u(t,a,e){return[...t,49,void 0===e?0:e,...oe(a||0)]}i64_load16_s(t,a,e){return[...t,50,void 0===e?1:e,...oe(a||0)]}i64_load16_u(t,a,e){return[...t,51,void 0===e?1:e,...oe(a||0)]}i64_load32_s(t,a,e){return[...t,52,void 0===e?2:e,...oe(a||0)]}i64_load32_u(t,a,e){return[...t,53,void 0===e?2:e,...oe(a||0)]}i64_load(t,a,e){return[...t,41,void 0===e?3:e,...oe(a||0)]}i64_store(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=3,l=a):Array.isArray(e)?(i=a,n=3,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,55,n,...oe(i)]}i64_store32(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=2,l=a):Array.isArray(e)?(i=a,n=2,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,62,n,...oe(i)]}i64_store16(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=1,l=a):Array.isArray(e)?(i=a,n=1,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,61,n,...oe(i)]}i64_store8(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=0,l=a):Array.isArray(e)?(i=a,n=0,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,60,n,...oe(i)]}i32_load8_s(t,a,e){return[...t,44,void 0===e?0:e,...oe(a||0)]}i32_load8_u(t,a,e){return[...t,45,void 0===e?0:e,...oe(a||0)]}i32_load16_s(t,a,e){return[...t,46,void 0===e?1:e,...oe(a||0)]}i32_load16_u(t,a,e){return[...t,47,void 0===e?1:e,...oe(a||0)]}i32_load(t,a,e){return[...t,40,void 0===e?2:e,...oe(a||0)]}i32_store(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=2,l=a):Array.isArray(e)?(i=a,n=2,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,54,n,...oe(i)]}i32_store16(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=1,l=a):Array.isArray(e)?(i=a,n=1,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,59,n,...oe(i)]}i32_store8(t,a,e,o){let i,n,l;return Array.isArray(a)?(i=0,n=0,l=a):Array.isArray(e)?(i=a,n=0,l=e):Array.isArray(o)&&(i=a,n=e,l=o),[...t,...l,58,n,...oe(i)]}call(t,...a){const e=this.module.functionIdxByName[t];if(void 0===e)throw new Error(`Function not defined: Function: ${t}`);return[...[].concat(...a),16,...oe(e)]}call_indirect(t,...a){return[...[].concat(...a),...t,17,0,0]}if(t,a,e){return e?[...t,4,64,...a,5,...e,11]:[...t,4,64,...a,11]}block(t){return[2,64,...t,11]}loop(...t){return[3,64,...[].concat(...t),11]}br_if(t,a){return[...a,13,...oe(t)]}br(t){return[12,...oe(t)]}ret(t){return[...t,15]}drop(t){return[...t,26]}i64_const(t){return[66,...ee(t)]}i32_const(t){return[65,...ae(t)]}i64_eqz(t){return[...t,80]}i64_eq(t,a){return[...t,...a,81]}i64_ne(t,a){return[...t,...a,82]}i64_lt_s(t,a){return[...t,...a,83]}i64_lt_u(t,a){return[...t,...a,84]}i64_gt_s(t,a){return[...t,...a,85]}i64_gt_u(t,a){return[...t,...a,86]}i64_le_s(t,a){return[...t,...a,87]}i64_le_u(t,a){return[...t,...a,88]}i64_ge_s(t,a){return[...t,...a,89]}i64_ge_u(t,a){return[...t,...a,90]}i64_add(t,a){return[...t,...a,124]}i64_sub(t,a){return[...t,...a,125]}i64_mul(t,a){return[...t,...a,126]}i64_div_s(t,a){return[...t,...a,127]}i64_div_u(t,a){return[...t,...a,128]}i64_rem_s(t,a){return[...t,...a,129]}i64_rem_u(t,a){return[...t,...a,130]}i64_and(t,a){return[...t,...a,131]}i64_or(t,a){return[...t,...a,132]}i64_xor(t,a){return[...t,...a,133]}i64_shl(t,a){return[...t,...a,134]}i64_shr_s(t,a){return[...t,...a,135]}i64_shr_u(t,a){return[...t,...a,136]}i64_extend_i32_s(t){return[...t,172]}i64_extend_i32_u(t){return[...t,173]}i64_clz(t){return[...t,121]}i64_ctz(t){return[...t,122]}i32_eqz(t){return[...t,69]}i32_eq(t,a){return[...t,...a,70]}i32_ne(t,a){return[...t,...a,71]}i32_lt_s(t,a){return[...t,...a,72]}i32_lt_u(t,a){return[...t,...a,73]}i32_gt_s(t,a){return[...t,...a,74]}i32_gt_u(t,a){return[...t,...a,75]}i32_le_s(t,a){return[...t,...a,76]}i32_le_u(t,a){return[...t,...a,77]}i32_ge_s(t,a){return[...t,...a,78]}i32_ge_u(t,a){return[...t,...a,79]}i32_add(t,a){return[...t,...a,106]}i32_sub(t,a){return[...t,...a,107]}i32_mul(t,a){return[...t,...a,108]}i32_div_s(t,a){return[...t,...a,109]}i32_div_u(t,a){return[...t,...a,110]}i32_rem_s(t,a){return[...t,...a,111]}i32_rem_u(t,a){return[...t,...a,112]}i32_and(t,a){return[...t,...a,113]}i32_or(t,a){return[...t,...a,114]}i32_xor(t,a){return[...t,...a,115]}i32_shl(t,a){return[...t,...a,116]}i32_shr_s(t,a){return[...t,...a,117]}i32_shr_u(t,a){return[...t,...a,118]}i32_rotl(t,a){return[...t,...a,119]}i32_rotr(t,a){return[...t,...a,120]}i32_wrap_i64(t){return[...t,167]}i32_clz(t){return[...t,103]}i32_ctz(t){return[...t,104]}unreachable(){return[0]}current_memory(){return[63,0]}comment(){return[]}}const le={i32:127,i64:126,f32:125,f64:124,anyfunc:112,func:96,emptyblock:64};class ce{constructor(t,a,e,o,i){if("import"==e)this.fnType="import",this.moduleName=o,this.fieldName=i;else{if("internal"!=e)throw new Error("Invalid function fnType: "+e);this.fnType="internal"}this.module=t,this.fnName=a,this.params=[],this.locals=[],this.localIdxByName={},this.code=[],this.returnType=null,this.nextLocal=0}addParam(t,a){if(this.localIdxByName[t])throw new Error(`param already exists. Function: ${this.fnName}, Param: ${t} `);const e=this.nextLocal++;this.localIdxByName[t]=e,this.params.push({type:a})}addLocal(t,a,e){const o=e||1;if(this.localIdxByName[t])throw new Error(`local already exists. Function: ${this.fnName}, Param: ${t} `);const i=this.nextLocal++;this.localIdxByName[t]=i,this.locals.push({type:a,length:o})}setReturnType(t){if(this.returnType)throw new Error(`returnType already defined. Function: ${this.fnName}`);this.returnType=t}getSignature(){return[96,...[...oe(this.params.length),...this.params.map((t=>le[t.type]))],...this.returnType?[1,le[this.returnType]]:[0]]}getBody(){const t=this.locals.map((t=>[...oe(t.length),le[t.type]])),a=[...oe(this.locals.length),...[].concat(...t),...this.code,11];return[...oe(a.length),...a]}addCode(...t){this.code.push(...[].concat(...t))}getCodeBuilder(){return new ne(this)}}class se{constructor(){this.functions=[],this.functionIdxByName={},this.nImportFunctions=0,this.nInternalFunctions=0,this.memory={pagesSize:1,moduleName:"env",fieldName:"memory"},this.free=8,this.datas=[],this.modules={},this.exports=[],this.functionsTable=[]}build(){return this._setSignatures(),new Uint8Array([...Ya(1836278016),...Ya(1),...this._buildType(),...this._buildImport(),...this._buildFunctionDeclarations(),...this._buildFunctionsTable(),...this._buildExports(),...this._buildElements(),...this._buildCode(),...this._buildData()])}addFunction(t){if(void 0!==this.functionIdxByName[t])throw new Error(`Function already defined: ${t}`);const a=this.functions.length;return this.functionIdxByName[t]=a,this.functions.push(new ce(this,t,"internal")),this.nInternalFunctions++,this.functions[a]}addIimportFunction(t,a,e){if(void 0!==this.functionIdxByName[t])throw new Error(`Function already defined: ${t}`);if(this.functions.length>0&&"internal"==this.functions[this.functions.length-1].type)throw new Error(`Import functions must be declared before internal: ${t}`);let o=e||t;const i=this.functions.length;return this.functionIdxByName[t]=i,this.functions.push(new ce(this,t,"import",a,o)),this.nImportFunctions++,this.functions[i]}setMemory(t,a,e){this.memory={pagesSize:t,moduleName:a||"env",fieldName:e||"memory"}}exportFunction(t,a){const e=a||t;if(void 0===this.functionIdxByName[t])throw new Error(`Function not defined: ${t}`);const o=this.functionIdxByName[t];e!=t&&(this.functionIdxByName[e]=o),this.exports.push({exportName:e,idx:o})}addFunctionToTable(t){const a=this.functionIdxByName[t];this.functionsTable.push(a)}addData(t,a){this.datas.push({offset:t,bytes:a})}alloc(t,a){let e,o;(Array.isArray(t)||ArrayBuffer.isView(t))&&void 0===a?(e=t.length,o=t):(e=t,o=a),e=1+(e-1>>3)<<3;const i=this.free;return this.free+=e,o&&this.addData(i,o),i}allocString(t){const a=(new globalThis.TextEncoder).encode(t);return this.alloc([...a,0])}_setSignatures(){this.signatures=[];const t={};if(this.functionsTable.length>0){const a=this.functions[this.functionsTable[0]].getSignature();t["s_"+ie(a)]=0,this.signatures.push(a)}for(let a=0;a{a.pendingLoads.push({page:t,resolve:e,reject:o})}));return a.__statusPage("After Load request: ",t),e}__statusPage(t,a){const e=[],o=this;if(!o.logHistory)return;e.push("=="+t+" "+a);let i="";for(let t=0;t "+a.history[t][e][o])}_triggerLoad(){const t=this;if(t.reading)return;if(0==t.pendingLoads.length)return;const a=Object.keys(t.pages),e=[];for(let o=0;o0&&(void 0!==t.pages[t.pendingLoads[0].page]||o>0||e.length>0);){const a=t.pendingLoads.shift();if(void 0!==t.pages[a.page]){t.pages[a.page].pendingOps++;const o=e.indexOf(a.page);o>=0&&e.splice(o,1),t.pages[a.page].loading?t.pages[a.page].loading.push(a):a.resolve(),t.__statusPage("After Load (cached): ",a.page)}else{if(o)o--;else{const a=e.shift();t.__statusPage("Before Unload: ",a),t.avBuffs.unshift(t.pages[a]),delete t.pages[a],t.__statusPage("After Unload: ",a)}a.page>=t.totalPages?(t.pages[a.page]=n(),a.resolve(),t.__statusPage("After Load (new): ",a.page)):(t.reading=!0,t.pages[a.page]=n(),t.pages[a.page].loading=[a],i.push(t.fd.read(t.pages[a.page].buff,0,t.pageSize,a.page*t.pageSize).then((e=>{t.pages[a.page].size=e.bytesRead;const o=t.pages[a.page].loading;delete t.pages[a.page].loading;for(let t=0;t{a.reject(t)}))),t.__statusPage("After Load (loading): ",a.page))}}function n(){if(t.avBuffs.length>0){const a=t.avBuffs.shift();return a.dirty=!1,a.pendingOps=1,a.size=0,a}return{dirty:!1,buff:new Uint8Array(t.pageSize),pendingOps:1,size:0}}Promise.all(i).then((()=>{t.reading=!1,t.pendingLoads.length>0&&setImmediate(t._triggerLoad.bind(t)),t._tryClose()}))}_triggerWrite(){const t=this;if(t.writing)return;const a=Object.keys(t.pages),e=[];for(let o=0;o{i.writing=!1}),(a=>{console.log("ERROR Writing: "+a),t.error=a,t._tryClose()}))))}t.writing&&Promise.all(e).then((()=>{t.writing=!1,setImmediate(t._triggerWrite.bind(t)),t._tryClose(),t.pendingLoads.length>0&&setImmediate(t._triggerLoad.bind(t))}))}_getDirtyPage(){for(let t in this.pages)if(this.pages[t].dirty)return t;return-1}async write(t,a){if(0==t.byteLength)return;const e=this;if(void 0===a&&(a=e.pos),e.pos=a+t.byteLength,e.totalSize0;){await n[l-o];const a=c+s>e.pageSize?e.pageSize-c:s,i=t.slice(t.byteLength-s,t.byteLength-s+a);new Uint8Array(e.pages[l].buff.buffer,c,a).set(i),e.pages[l].dirty=!0,e.pages[l].pendingOps--,e.pages[l].size=Math.max(c+a,e.pages[l].size),l>=e.totalPages&&(e.totalPages=l+1),s-=a,l++,c=0,e.writing||setImmediate(e._triggerWrite.bind(e))}}async read(t,a){let e=new Uint8Array(t);return await this.readToBuffer(e,0,t,a),e}async readToBuffer(t,a,e,o){if(0==e)return;const i=this;if(e>i.pageSize*i.maxPagesLoaded*.8){const t=Math.floor(1.1*e);this.maxPagesLoaded=Math.floor(t/i.pageSize)+1}if(void 0===o&&(o=i.pos),i.pos=o+e,i.pendingClose)throw new Error("Reading a closing file");const n=Math.floor(o/i.pageSize),l=Math.floor((o+e-1)/i.pageSize),c=[];for(let t=n;t<=l;t++)c.push(i._loadPage(t));i._triggerLoad();let s=n,r=o%i.pageSize,d=o+e>i.totalSize?e-(o+e-i.totalSize):e;for(;d>0;){await c[s-n],i.__statusPage("After Await (read): ",s);const o=r+d>i.pageSize?i.pageSize-r:d,l=new Uint8Array(i.pages[s].buff.buffer,i.pages[s].buff.byteOffset+r,o);t.set(l,a+e-d),i.pages[s].pendingOps--,i.__statusPage("After Op done: ",s),d-=o,s++,r=0,i.pendingLoads.length>0&&setImmediate(i._triggerLoad.bind(i))}this.pos=o+e}_tryClose(){const t=this;if(!t.pendingClose)return;t.error&&t.pendingCloseReject(t.error);t._getDirtyPage()>=0||t.writing||t.reading||t.pendingLoads.length>0||t.pendingClose()}close(){const t=this;if(t.pendingClose)throw new Error("Closing the file twice");return new Promise(((a,e)=>{t.pendingClose=a,t.pendingCloseReject=e,t._tryClose()})).then((()=>{t.fd.close()}),(a=>{throw t.fd.close(),a}))}async discard(){await this.close(),await ge.promises.unlink(this.fileName)}async writeULE32(t,a){const e=new Uint8Array(4);new DataView(e.buffer).setUint32(0,t,!0),await this.write(e,a)}async writeUBE32(t,a){const e=new Uint8Array(4);new DataView(e.buffer).setUint32(0,t,!1),await this.write(e,a)}async writeULE64(t,a){const e=new Uint8Array(8),o=new DataView(e.buffer);o.setUint32(0,4294967295&t,!0),o.setUint32(4,Math.floor(t/4294967296),!0),await this.write(e,a)}async readULE32(t){const a=await this.read(4,t);return new Uint32Array(a.buffer)[0]}async readUBE32(t){const a=await this.read(4,t);return new DataView(a.buffer).getUint32(0,!1)}async readULE64(t){const a=await this.read(8,t),e=new Uint32Array(a.buffer);return 4294967296*e[1]+e[0]}async readString(t){const a=this;if(a.pendingClose)throw new Error("Reading a closing file");let e=void 0===t?a.pos:t,o=Math.floor(e/a.pageSize),i=!1,n="";for(;!i;){let t=a._loadPage(o);a._triggerLoad(),await t,a.__statusPage("After Await (read): ",o);let l=e%a.pageSize;const c=new Uint8Array(a.pages[o].buff.buffer,a.pages[o].buff.byteOffset+l,a.pageSize-l);let s=c.findIndex((t=>0===t));i=-1!==s,i?(n+=(new TextDecoder).decode(c.slice(0,s)),a.pos=o*this.pageSize+l+s+1):(n+=(new TextDecoder).decode(c),a.pos=o*this.pageSize+l+c.length),a.pages[o].pendingOps--,a.__statusPage("After Op done: ",o),e=a.pos,o++,a.pendingLoads.length>0&&setImmediate(a._triggerLoad.bind(a))}return n}}const he=new Uint8Array(4),me=new DataView(he.buffer),Le=new Uint8Array(8),be=new DataView(Le.buffer);class we{constructor(){this.pageSize=16384}_resizeIfNeeded(t){if(t>this.allocSize){const a=Math.max(this.allocSize+(1<<20),Math.floor(1.1*this.allocSize),t),e=new Uint8Array(a);e.set(this.o.data),this.o.data=e,this.allocSize=a}}async write(t,a){if(void 0===a&&(a=this.pos),this.readOnly)throw new Error("Writing a read only file");this._resizeIfNeeded(a+t.byteLength),this.o.data.set(t.slice(),a),a+t.byteLength>this.totalSize&&(this.totalSize=a+t.byteLength),this.pos=a+t.byteLength}async readToBuffer(t,a,e,o){if(void 0===o&&(o=this.pos),this.readOnly&&o+e>this.totalSize)throw new Error("Reading out of bounds");this._resizeIfNeeded(o+e);const i=new Uint8Array(this.o.data.buffer,this.o.data.byteOffset+o,e);t.set(i,a),this.pos=o+e}async read(t,a){const e=new Uint8Array(t);return await this.readToBuffer(e,0,t,a),e}close(){this.o.data.byteLength!=this.totalSize&&(this.o.data=this.o.data.slice(0,this.totalSize))}async discard(){}async writeULE32(t,a){me.setUint32(0,t,!0),await this.write(he,a)}async writeUBE32(t,a){me.setUint32(0,t,!1),await this.write(he,a)}async writeULE64(t,a){be.setUint32(0,4294967295&t,!0),be.setUint32(4,Math.floor(t/4294967296),!0),await this.write(Le,a)}async readULE32(t){const a=await this.read(4,t);return new Uint32Array(a.buffer)[0]}async readUBE32(t){const a=await this.read(4,t);return new DataView(a.buffer).getUint32(0,!1)}async readULE64(t){const a=await this.read(8,t),e=new Uint32Array(a.buffer);return 4294967296*e[1]+e[0]}async readString(t){const a=this;let e=void 0===t?a.pos:t;if(e>this.totalSize){if(this.readOnly)throw new Error("Reading out of bounds");this._resizeIfNeeded(t)}const o=new Uint8Array(a.o.data.buffer,e,this.totalSize-e);let i=o.findIndex((t=>0===t)),n="";return-1!==i?(n=(new TextDecoder).decode(o.slice(0,i)),a.pos=e+i+1):a.pos=e,n}}const ye=1<<22;const Ae=new Uint8Array(4),Ce=new DataView(Ae.buffer),xe=new Uint8Array(8),Fe=new DataView(xe.buffer);class Ie{constructor(){this.pageSize=16384}_resizeIfNeeded(t){if(t<=this.totalSize)return;if(this.readOnly)throw new Error("Reading out of file bounds");const a=Math.floor((t-1)/ye)+1;for(let e=Math.max(this.o.data.length-1,0);e0;){const a=i+n>ye?ye-i:n,l=t.slice(t.byteLength-n,t.byteLength-n+a);new Uint8Array(e.o.data[o].buffer,i,a).set(l),n-=a,o++,i=0}this.pos=a+t.byteLength}async readToBuffer(t,a,e,o){const i=this;if(void 0===o&&(o=i.pos),this.readOnly&&o+e>this.totalSize)throw new Error("Reading out of bounds");this._resizeIfNeeded(o+e);let n=Math.floor(o/ye),l=o%ye,c=e;for(;c>0;){const o=l+c>ye?ye-l:c,s=new Uint8Array(i.o.data[n].buffer,l,o);t.set(s,a+e-c),c-=o,n++,l=0}this.pos=o+e}async read(t,a){const e=new Uint8Array(t);return await this.readToBuffer(e,0,t,a),e}close(){}async discard(){}async writeULE32(t,a){Ce.setUint32(0,t,!0),await this.write(Ae,a)}async writeUBE32(t,a){Ce.setUint32(0,t,!1),await this.write(Ae,a)}async writeULE64(t,a){Fe.setUint32(0,4294967295&t,!0),Fe.setUint32(4,Math.floor(t/4294967296),!0),await this.write(xe,a)}async readULE32(t){const a=await this.read(4,t);return new Uint32Array(a.buffer)[0]}async readUBE32(t){const a=await this.read(4,t);return new DataView(a.buffer).getUint32(0,!1)}async readULE64(t){const a=await this.read(8,t),e=new Uint32Array(a.buffer);return 4294967296*e[1]+e[0]}async readString(t){const a=this;let e=void 0===t?a.pos:t;if(e>this.totalSize){if(this.readOnly)throw new Error("Reading out of bounds");this._resizeIfNeeded(t)}let o=!1,i="";for(;!o;){let t=Math.floor(e/ye),n=e%ye;if(void 0===a.o.data[t])throw new Error("ERROR");let l=Math.min(2048,a.o.data[t].length-n);const c=new Uint8Array(a.o.data[t].buffer,n,l);let s=c.findIndex((t=>0===t));o=-1!==s,o?(i+=(new TextDecoder).decode(c.slice(0,s)),a.pos=t*ye+n+s+1):(i+=(new TextDecoder).decode(c),a.pos=t*ye+n+c.length),e=a.pos}return i}}const Be=512,Ee=64,ve=2,Se=0,Pe=65536,Oe=8192;async function qe(t,a,e){if("string"==typeof t&&(t={type:"file",fileName:t,cacheSize:a||Pe,pageSize:e||Oe}),"file"==t.type)return await fe(t.fileName,Be|Ee|ve,t.cacheSize,t.pageSize);if("mem"==t.type)return function(t){const a=t.initialSize||1<<20,e=new we;return e.o=t,e.o.data=new Uint8Array(a),e.allocSize=a,e.totalSize=0,e.readOnly=!1,e.pos=0,e}(t);if("bigMem"==t.type)return function(t){const a=t.initialSize||0,e=new Ie;e.o=t;const o=a?Math.floor((a-1)/ye)+1:0;e.o.data=[];for(let t=0;te)throw new Error("Version not supported");const s=await n.readULE32();let r=[];for(let t=0;t1)throw new Error(t.fileName+": Section Duplicated "+e);t.pos=a[e][0].p,t.readingSection=a[e][0]}async function ke(t,a){if(void 0===t.readingSection)throw new Error("Not reading a section");if(!a&&t.pos-t.readingSection.p!=t.readingSection.size)throw new Error("Invalid section size reading");delete t.readingSection}async function Re(t,a,e,o){const i=new Uint8Array(e);_e.toRprLE(i,0,a,e),await t.write(i,o)}async function Ne(t,a,e){const o=await t.read(a,e);return _e.fromRprLE(o,0,a)}async function De(t,a,e,o,i){void 0===i&&(i=a[o][0].size);const n=t.pageSize;await Qe(t,a,o),await Me(e,o);for(let a=0;aa[e][0].size)throw new Error("Reading out of the range of the section");let n;return n=i<1<<30?new Uint8Array(i):new va(i),await t.readToBuffer(n,0,i,a[e][0].p+o),n}async function je(t,a,e,o,i){const n=16*t.pageSize;if(await Qe(t,a,i),await Qe(e,o,i),a[i][0].size!=o[i][0].size)return!1;const l=a[i][0].size;for(let a=0;a>BigInt(a)}const to=Je,ao=Xe;function eo(t){return(BigInt(t)&BigInt(1))==BigInt(1)}function oo(t){let a=BigInt(t);const e=[];for(;a;)a&BigInt(1)?e.push(1):e.push(0),a>>=BigInt(1);return e}function io(t){if(t>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Number too big");return Number(t)}function no(t,a){return BigInt(t)+BigInt(a)}function lo(t,a){return BigInt(t)-BigInt(a)}function co(t){return-BigInt(t)}function so(t,a){return BigInt(t)*BigInt(a)}function ro(t,a){return BigInt(t)**BigInt(a)}function uo(t,a){return BigInt(t)/BigInt(a)}function _o(t,a){return BigInt(t)%BigInt(a)}function go(t,a){return BigInt(t)==BigInt(a)}function fo(t,a){return BigInt(t)>BigInt(a)}function po(t,a){return BigInt(t)>=BigInt(a)}function ho(t,a){return BigInt(t)&BigInt(a)}function mo(t,a,e,o){const i="0000000"+e.toString(16),n=new Uint32Array(t.buffer,t.byteOffset+a,o/4),l=1+(4*(i.length-7)-1>>5);for(let t=0;t>5);for(let t=0;ti[i.length-a-1]=t.toString(16).padStart(8,"0"))),Ke(i.join(""),16)}function wo(t,a,e){e=e||t.byteLength,a=a||0;const o=new DataView(t.buffer,t.byteOffset+a,e),i=new Array(e/4);for(let t=0;t>=BigInt(1)}return e},bits:oo,toNumber:io,toArray:function(t,a){const e=[];let o=BigInt(t);for(a=BigInt(a);o;)e.unshift(Number(o%a)),o/=a;return e},add:no,sub:lo,neg:co,mul:so,square:function(t){return BigInt(t)*BigInt(t)},pow:ro,exp:function(t,a){return BigInt(t)**BigInt(a)},abs:function(t){return BigInt(t)>=0?BigInt(t):-BigInt(t)},div:uo,mod:_o,eq:go,neq:function(t,a){return BigInt(t)!=BigInt(a)},lt:function(t,a){return BigInt(t)=0;e--)i=t.square(i),o[e]&&(i=t.mul(i,a));return i}function Bo(t){if(t.m%2==1)if(go(_o(t.p,4),1))if(go(_o(t.p,8),1))if(go(_o(t.p,16),1))!function(t){t.sqrt_q=ro(t.p,t.m),t.sqrt_s=0,t.sqrt_t=lo(t.sqrt_q,1);for(;!eo(t.sqrt_t);)t.sqrt_s=t.sqrt_s+1,t.sqrt_t=uo(t.sqrt_t,2);let a=t.one;for(;t.eq(a,t.one);){const e=t.random();t.sqrt_z=t.pow(e,t.sqrt_t),a=t.pow(t.sqrt_z,2**(t.sqrt_s-1))}t.sqrt_tm1d2=uo(lo(t.sqrt_t,1),2),t.sqrt=function(t){const a=this;if(a.isZero(t))return a.zero;let e=a.pow(t,a.sqrt_tm1d2);const o=a.pow(a.mul(a.square(e),t),2**(a.sqrt_s-1));if(a.eq(o,a.negone))return null;let i=a.sqrt_s,n=a.mul(t,e),l=a.mul(n,e),c=a.sqrt_z;for(;!a.eq(l,a.one);){let t=a.square(l),o=1;for(;!a.eq(t,a.one);)t=a.square(t),o++;e=c;for(let t=0;t>>0,t[i]=(t[i]^t[a])>>>0,t[i]=(t[i]<<16|t[i]>>>16&65535)>>>0,t[o]=t[o]+t[i]>>>0,t[e]=(t[e]^t[o])>>>0,t[e]=(t[e]<<12|t[e]>>>20&4095)>>>0,t[a]=t[a]+t[e]>>>0,t[i]=(t[i]^t[a])>>>0,t[i]=(t[i]<<8|t[i]>>>24&255)>>>0,t[o]=t[o]+t[i]>>>0,t[e]=(t[e]^t[o])>>>0,t[e]=(t[e]<<7|t[e]>>>25&127)>>>0}class vo{constructor(t){t=t||[0,0,0,0,0,0,0,0],this.state=[1634760805,857760878,2036477234,1797285236,t[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 no(so(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 a=0;a<10;a++)Eo(t=this.buff,0,4,8,12),Eo(t,1,5,9,13),Eo(t,2,6,10,14),Eo(t,3,7,11,15),Eo(t,0,5,10,15),Eo(t,1,6,11,12),Eo(t,2,7,8,13),Eo(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)))}}function So(t){let a=new Uint8Array(t);if(void 0!==globalThis.crypto)globalThis.crypto.getRandomValues(a);else for(let e=0;e>>0;return a}let Po=null;function Oo(){return Po||(Po=new vo(function(){const t=So(32),a=new Uint32Array(t.buffer),e=[];for(let t=0;t<8;t++)e.push(a[t]);return e}()),Po)}class qo{constructor(t,a,e){this.F=a,this.G=t,this.opMulGF=e;let o=a.sqrt_t||a.t,i=a.sqrt_s||a.s,n=a.one;for(;a.eq(a.pow(n,a.half),a.one);)n=a.add(n,a.one);this.w=new Array(i+1),this.wi=new Array(i+1),this.w[i]=this.F.pow(n,o),this.wi[i]=this.F.inv(this.w[i]);let l=i-1;for(;l>=0;)this.w[l]=this.F.square(this.w[l+1]),this.wi[l]=this.F.square(this.wi[l+1]),l--;this.roots=[],this._setRoots(Math.min(i,15))}_setRoots(t){for(let a=t;a>=0&&!this.roots[a];a--){let t=this.F.one;const e=1<>1,c=zo(t,a,e-1,o,2*i),s=zo(t,a,e-1,o+i,2*i),r=new Array(n);for(let a=0;a>this.one,this.bitLength=Ze(this.p),this.mask=(this.one<>this.one;this.nqr=this.two;let e=this.pow(this.nqr,a);for(;!this.eq(e,this.negone);)this.nqr=this.nqr+this.one,e=this.pow(this.nqr,a);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),Bo(this),this.FFT=new qo(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,a){let e;if(a?16==a&&(e=BigInt("0x"+t)):e=BigInt(t),e<0){let t=-e;return t>=this.p&&(t%=this.p),this.p-t}return e>=this.p?e%this.p:e}add(t,a){const e=t+a;return e>=this.p?e-this.p:e}sub(t,a){return t>=a?t-a:this.p-a+t}neg(t){return t?this.p-t:t}mul(t,a){return t*a%this.p}mulScalar(t,a){return t*this.e(a)%this.p}square(t){return t*t%this.p}eq(t,a){return t==a}neq(t,a){return t!=a}lt(t,a){return(t>this.half?t-this.p:t)<(a>this.half?a-this.p:a)}gt(t,a){return(t>this.half?t-this.p:t)>(a>this.half?a-this.p:a)}leq(t,a){return(t>this.half?t-this.p:t)<=(a>this.half?a-this.p:a)}geq(t,a){return(t>this.half?t-this.p:t)>=(a>this.half?a-this.p:a)}div(t,a){return this.mul(t,this.inv(a))}idiv(t,a){if(!a)throw new Error("Division by zero");return t/a}inv(t){if(!t)throw new Error("Division by zero");let a=this.zero,e=this.p,o=this.one,i=t%this.p;for(;i;){let t=e/i;[a,o]=[o,a-t*o],[e,i]=[i,e-t*i]}return a=this.p?e-this.p:e}bor(t,a){const e=(t|a)&this.mask;return e>=this.p?e-this.p:e}bxor(t,a){const e=(t^a)&this.mask;return e>=this.p?e-this.p:e}bnot(t){const a=t^this.mask;return a>=this.p?a-this.p:a}shl(t,a){if(Number(a)=this.p?e-this.p:e}{const e=this.p-a;return Number(e)>e:this.zero}}shr(t,a){if(Number(a)>a;{const e=this.p-a;if(Number(e)=this.p?a-this.p:a}return 0}}land(t,a){return t&&a?this.one:this.zero}lor(t,a){return t||a?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 a=this.s,e=this.nqr_to_t,o=this.pow(t,this.t),i=this.pow(t,this.add(this.t,this.one)>>this.one);for(;o!=this.one;){let t=this.square(o),n=1;for(;t!=this.one;)n++,t=this.square(t);let l=e;for(let t=0;tthis.p>>this.one&&(i=this.neg(i)),i}normalize(t,a){if((t=BigInt(t,a))<0){let a=-t;return a>=this.p&&(a%=this.p),this.p-a}return t>=this.p?t%this.p:t}random(){const t=2*this.bitLength/8;let a=this.zero;for(let e=0;ethis.half&&10==a){e="-"+(this.p-t).toString(a)}else e=t.toString(a);return e}isZero(t){return t==this.zero}fromRng(t){let a;do{a=this.zero;for(let e=0;e=this.p);return a=a*this.Ri%this.p,a}fft(t){return this.FFT.fft(t)}ifft(t){return this.FFT.ifft(t)}toRprLE(t,a,e){mo(t,a,e,8*this.n64)}toRprBE(t,a,e){Lo(t,a,e,8*this.n64)}toRprBEM(t,a,e){return this.toRprBE(t,a,this.mul(this.R,e))}toRprLEM(t,a,e){return this.toRprLE(t,a,this.mul(this.R,e))}fromRprLE(t,a){return bo(t,a,this.n8)}fromRprBE(t,a){return wo(t,a,this.n8)}fromRprLEM(t,a){return this.mul(this.fromRprLE(t,a),this.Ri)}fromRprBEM(t,a){return this.mul(this.fromRprBE(t,a),this.Ri)}toObject(t){return t}}var Mo={bigInt2BytesLE:function(t,a){const e=Array(a);let o=BigInt(t);for(let t=0;t>=8n;return e},bigInt2U32LE:function(t,a){const e=Array(a);let o=BigInt(t);for(let t=0;t>=32n;return e},isOcamNum:function(t){return!!Array.isArray(t)&&(3==t.length&&("number"==typeof t[0]&&("number"==typeof t[1]&&!!Array.isArray(t[2]))))}},Uo=function(t,a,e,o,i,n,l){const c=t.addFunction(a);c.addParam("base","i32"),c.addParam("scalar","i32"),c.addParam("scalarLength","i32"),c.addParam("r","i32"),c.addLocal("i","i32"),c.addLocal("b","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(e));c.addCode(s.if(s.i32_eqz(s.getLocal("scalarLength")),[...s.call(l,s.getLocal("r")),...s.ret([])])),c.addCode(s.call(n,s.getLocal("base"),r)),c.addCode(s.call(l,s.getLocal("r"))),c.addCode(s.setLocal("i",s.getLocal("scalarLength"))),c.addCode(s.block(s.loop(s.setLocal("i",s.i32_sub(s.getLocal("i"),s.i32_const(1))),s.setLocal("b",s.i32_load8_u(s.i32_add(s.getLocal("scalar"),s.getLocal("i")))),...function(){const t=[];for(let a=0;a<8;a++)t.push(...s.call(i,s.getLocal("r"),s.getLocal("r")),...s.if(s.i32_ge_u(s.getLocal("b"),s.i32_const(128>>a)),[...s.setLocal("b",s.i32_sub(s.getLocal("b"),s.i32_const(128>>a))),...s.call(o,s.getLocal("r"),r,s.getLocal("r"))]));return t}(),s.br_if(1,s.i32_eqz(s.getLocal("i"))),s.br(0))))},Qo=function(t,a){const e=8*t.modules[a].n64,o=t.addFunction(a+"_batchInverse");o.addParam("pIn","i32"),o.addParam("inStep","i32"),o.addParam("n","i32"),o.addParam("pOut","i32"),o.addParam("outStep","i32"),o.addLocal("itAux","i32"),o.addLocal("itIn","i32"),o.addLocal("itOut","i32"),o.addLocal("i","i32");const i=o.getCodeBuilder(),n=i.i32_const(t.alloc(e));o.addCode(i.setLocal("itAux",i.i32_load(i.i32_const(0))),i.i32_store(i.i32_const(0),i.i32_add(i.getLocal("itAux"),i.i32_mul(i.i32_add(i.getLocal("n"),i.i32_const(1)),i.i32_const(e))))),o.addCode(i.call(a+"_one",i.getLocal("itAux")),i.setLocal("itIn",i.getLocal("pIn")),i.setLocal("itAux",i.i32_add(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("i",i.i32_const(0)),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("i"),i.getLocal("n"))),i.if(i.call(a+"_isZero",i.getLocal("itIn")),i.call(a+"_copy",i.i32_sub(i.getLocal("itAux"),i.i32_const(e)),i.getLocal("itAux")),i.call(a+"_mul",i.getLocal("itIn"),i.i32_sub(i.getLocal("itAux"),i.i32_const(e)),i.getLocal("itAux"))),i.setLocal("itIn",i.i32_add(i.getLocal("itIn"),i.getLocal("inStep"))),i.setLocal("itAux",i.i32_add(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("i",i.i32_add(i.getLocal("i"),i.i32_const(1))),i.br(0))),i.setLocal("itIn",i.i32_sub(i.getLocal("itIn"),i.getLocal("inStep"))),i.setLocal("itAux",i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("itOut",i.i32_add(i.getLocal("pOut"),i.i32_mul(i.i32_sub(i.getLocal("n"),i.i32_const(1)),i.getLocal("outStep")))),i.call(a+"_inverse",i.getLocal("itAux"),i.getLocal("itAux")),i.block(i.loop(i.br_if(1,i.i32_eqz(i.getLocal("i"))),i.if(i.call(a+"_isZero",i.getLocal("itIn")),[...i.call(a+"_copy",i.getLocal("itAux"),i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),...i.call(a+"_zero",i.getLocal("itOut"))],[...i.call(a+"_copy",i.i32_sub(i.getLocal("itAux"),i.i32_const(e)),n),...i.call(a+"_mul",i.getLocal("itAux"),i.getLocal("itIn"),i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),...i.call(a+"_mul",i.getLocal("itAux"),n,i.getLocal("itOut"))]),i.setLocal("itIn",i.i32_sub(i.getLocal("itIn"),i.getLocal("inStep"))),i.setLocal("itOut",i.i32_sub(i.getLocal("itOut"),i.getLocal("outStep"))),i.setLocal("itAux",i.i32_sub(i.getLocal("itAux"),i.i32_const(e))),i.setLocal("i",i.i32_sub(i.getLocal("i"),i.i32_const(1))),i.br(0)))),o.addCode(i.i32_store(i.i32_const(0),i.getLocal("itAux")))};var ko=function(t,a,e,o,i,n){void 0===n&&(n=oa?1:-1}function $o(t){return t*t}function jo(t){return t%2n!==0n}function Vo(t){return t%2n===0n}function Ko(t){return t<0n}function Ho(t){return t>0n}function Zo(t){return Ko(t)?t.toString(2).length-1:t.toString(2).length}function Wo(t){return t<0n?-t:t}function Yo(t){return 1n===Wo(t)}function Jo(t,a){for(var e,o,i,n=0n,l=1n,c=a,s=Wo(t);0n!==s;)e=c/s,o=n,i=c,n=l,c=s,l=o-e*l,s=i-e*s;if(!Yo(c))throw new Error(t.toString()+" and "+a.toString()+" are not co-prime");return-1===Do(n,0n)&&(n+=a),Ko(t)?-n:n}function Xo(t,a,e){if(0n===e)throw new Error("Cannot take modPow with modulus 0");var o=1n,i=t%e;for(Ko(a)&&(a*=-1n,i=Jo(i,e));Ho(a);){if(0n===i)return 0n;jo(a)&&(o=o*i%e),a/=2n,i=$o(i)%e}return o}function ti(t,a){return 0n!==a&&(!!Yo(a)||(0===function(t,a){return(t=t>=0n?t:-t)===(a=a>=0n?a:-a)?0:t>a?1:-1}(a,2n)?Vo(t):t%a===0n))}function ai(t,a){for(var e,o,i,n=function(t){return t-1n}(t),l=n,c=0;Vo(l);)l/=2n,c++;t:for(o=0;o>1&&o>1,t>>1)))),a.addCode(e.setLocal(s,e.i64_add(e.getLocal(s),e.i64_shr_u(e.getLocal(c),e.i64_const(32)))))),t>0&&(a.addCode(e.setLocal(c,e.i64_add(e.i64_and(e.getLocal(c),e.i64_const(4294967295)),e.i64_and(e.getLocal(r),e.i64_const(4294967295))))),a.addCode(e.setLocal(s,e.i64_add(e.i64_add(e.getLocal(s),e.i64_shr_u(e.getLocal(c),e.i64_const(32))),e.getLocal(d))))),a.addCode(e.i64_store32(e.getLocal("r"),4*t,e.getLocal(c))),a.addCode(e.setLocal(r,e.getLocal(s)),e.setLocal(d,e.i64_shr_u(e.getLocal(r),e.i64_const(32))))}a.addCode(e.i64_store32(e.getLocal("r"),4*i*2-4,e.getLocal(r)))}(),function(){const a=t.addFunction(o+"_squareOld");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(o+"_mul",e.getLocal("x"),e.getLocal("x"),e.getLocal("r")))}(),function(){!function(){const a=t.addFunction(o+"__mul1");a.addParam("px","i32"),a.addParam("y","i64"),a.addParam("pr","i32"),a.addLocal("c","i64");const e=a.getCodeBuilder();a.addCode(e.setLocal("c",e.i64_mul(e.i64_load32_u(e.getLocal("px"),0,0),e.getLocal("y")))),a.addCode(e.i64_store32(e.getLocal("pr"),0,0,e.getLocal("c")));for(let t=1;t>1n,h=t.alloc(c,oi.bigInt2BytesLE(p,c)),m=p+1n,L=t.alloc(c,oi.bigInt2BytesLE(m,c));t.modules[s]={pq:d,pR2:u,n64:n,q:i,pOne:_,pZero:g,pePlusOne:L};let b=2n;if(ui(i))for(;di(b,p,i)!==f;)b+=1n;let w=0,y=f;for(;!_i(y)&&0n!==y;)w++,y>>=1n;const A=t.alloc(c,oi.bigInt2BytesLE(y,c)),C=di(b,y,i),x=t.alloc(oi.bigInt2BytesLE((C<>1n,I=t.alloc(c,oi.bigInt2BytesLE(F,c));return t.exportFunction(r+"_copy",s+"_copy"),t.exportFunction(r+"_zero",s+"_zero"),t.exportFunction(r+"_isZero",s+"_isZero"),t.exportFunction(r+"_eq",s+"_eq"),function(){const a=t.addFunction(s+"_isOne");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder();a.addCode(e.ret(e.call(r+"_eq",e.getLocal("x"),e.i32_const(_))))}(),function(){const a=t.addFunction(s+"_add");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.if(e.call(r+"_add",e.getLocal("x"),e.getLocal("y"),e.getLocal("r")),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),e.if(e.call(r+"_gte",e.getLocal("r"),e.i32_const(d)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))))))}(),function(){const a=t.addFunction(s+"_sub");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.if(e.call(r+"_sub",e.getLocal("x"),e.getLocal("y"),e.getLocal("r")),e.drop(e.call(r+"_add",e.getLocal("r"),e.i32_const(d),e.getLocal("r")))))}(),function(){const a=t.addFunction(s+"_neg");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_sub",e.i32_const(g),e.getLocal("x"),e.getLocal("r")))}(),function(){const a=t.alloc(l*l*8),e=t.addFunction(s+"_mReduct");e.addParam("t","i32"),e.addParam("r","i32"),e.addLocal("np32","i64"),e.addLocal("c","i64"),e.addLocal("m","i64");const o=e.getCodeBuilder(),n=Number(0x100000000n-ri(i,0x100000000n));e.addCode(o.setLocal("np32",o.i64_const(n)));for(let t=0;t=l&&a.addCode(e.i64_store32(e.getLocal("r"),4*(t-l),e.getLocal(f))),[f,p]=[p,f],a.addCode(e.setLocal(p,e.i64_shr_u(e.getLocal(f),e.i64_const(32))))}a.addCode(e.i64_store32(e.getLocal("r"),4*l-4,e.getLocal(f))),a.addCode(e.if(e.i32_wrap_i64(e.getLocal(p)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),e.if(e.call(r+"_gte",e.getLocal("r"),e.i32_const(d)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))))))}(),function(){const a=t.addFunction(s+"_square");a.addParam("x","i32"),a.addParam("r","i32"),a.addLocal("c0","i64"),a.addLocal("c1","i64"),a.addLocal("c0_old","i64"),a.addLocal("c1_old","i64"),a.addLocal("np32","i64");for(let t=0;t>1&&o>1,t>>1)))),a.addCode(e.setLocal(f,e.i64_add(e.getLocal(f),e.i64_shr_u(e.getLocal(g),e.i64_const(32)))))),t>0&&(a.addCode(e.setLocal(g,e.i64_add(e.i64_and(e.getLocal(g),e.i64_const(4294967295)),e.i64_and(e.getLocal(p),e.i64_const(4294967295))))),a.addCode(e.setLocal(f,e.i64_add(e.i64_add(e.getLocal(f),e.i64_shr_u(e.getLocal(g),e.i64_const(32))),e.getLocal(h)))));for(let o=Math.max(1,t-l+1);o<=t&&o=l&&a.addCode(e.i64_store32(e.getLocal("r"),4*(t-l),e.getLocal(g))),a.addCode(e.setLocal(p,e.getLocal(f)),e.setLocal(h,e.i64_shr_u(e.getLocal(p),e.i64_const(32))))}a.addCode(e.i64_store32(e.getLocal("r"),4*l-4,e.getLocal(p))),a.addCode(e.if(e.i32_wrap_i64(e.getLocal(h)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),e.if(e.call(r+"_gte",e.getLocal("r"),e.i32_const(d)),e.drop(e.call(r+"_sub",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))))))}(),function(){const a=t.addFunction(s+"_squareOld");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_mul",e.getLocal("x"),e.getLocal("x"),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_toMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_mul",e.getLocal("x"),e.i32_const(u),e.getLocal("r")))}(),function(){const a=t.alloc(2*c),e=t.addFunction(s+"_fromMontgomery");e.addParam("x","i32"),e.addParam("r","i32");const o=e.getCodeBuilder();e.addCode(o.call(r+"_copy",o.getLocal("x"),o.i32_const(a))),e.addCode(o.call(r+"_zero",o.i32_const(a+c))),e.addCode(o.call(s+"_mReduct",o.i32_const(a),o.getLocal("r")))}(),function(){const a=t.addFunction(s+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c));a.addCode(e.call(s+"_fromMontgomery",e.getLocal("x"),o),e.call(r+"_gte",o,e.i32_const(L)))}(),function(){const a=t.addFunction(s+"_sign");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c));a.addCode(e.if(e.call(r+"_isZero",e.getLocal("x")),e.ret(e.i32_const(0))),e.call(s+"_fromMontgomery",e.getLocal("x"),o),e.if(e.call(r+"_gte",o,e.i32_const(L)),e.ret(e.i32_const(-1))),e.ret(e.i32_const(1)))}(),function(){const a=t.addFunction(s+"_inverse");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_fromMontgomery",e.getLocal("x"),e.getLocal("r"))),a.addCode(e.call(r+"_inverseMod",e.getLocal("r"),e.i32_const(d),e.getLocal("r"))),a.addCode(e.call(s+"_toMontgomery",e.getLocal("r"),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_one");a.addParam("pr","i32");const e=a.getCodeBuilder();a.addCode(e.call(r+"_copy",e.i32_const(_),e.getLocal("pr")))}(),function(){const a=t.addFunction(s+"_load");a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32"),a.addLocal("p","i32"),a.addLocal("l","i32"),a.addLocal("i","i32"),a.addLocal("j","i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c)),i=t.alloc(c),n=e.i32_const(i);a.addCode(e.call(r+"_zero",e.getLocal("r")),e.setLocal("i",e.i32_const(c)),e.setLocal("p",e.getLocal("scalar")),e.block(e.loop(e.br_if(1,e.i32_gt_u(e.getLocal("i"),e.getLocal("scalarLen"))),e.if(e.i32_eq(e.getLocal("i"),e.i32_const(c)),e.call(s+"_one",o),e.call(s+"_mul",o,e.i32_const(u),o)),e.call(s+"_mul",e.getLocal("p"),o,n),e.call(s+"_add",e.getLocal("r"),n,e.getLocal("r")),e.setLocal("p",e.i32_add(e.getLocal("p"),e.i32_const(c))),e.setLocal("i",e.i32_add(e.getLocal("i"),e.i32_const(c))),e.br(0))),e.setLocal("l",e.i32_rem_u(e.getLocal("scalarLen"),e.i32_const(c))),e.if(e.i32_eqz(e.getLocal("l")),e.ret([])),e.call(r+"_zero",n),e.setLocal("j",e.i32_const(0)),e.block(e.loop(e.br_if(1,e.i32_eq(e.getLocal("j"),e.getLocal("l"))),e.i32_store8(e.getLocal("j"),i,e.i32_load8_u(e.getLocal("p"))),e.setLocal("p",e.i32_add(e.getLocal("p"),e.i32_const(1))),e.setLocal("j",e.i32_add(e.getLocal("j"),e.i32_const(1))),e.br(0))),e.if(e.i32_eq(e.getLocal("i"),e.i32_const(c)),e.call(s+"_one",o),e.call(s+"_mul",o,e.i32_const(u),o)),e.call(s+"_mul",n,o,n),e.call(s+"_add",e.getLocal("r"),n,e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_timesScalar");a.addParam("x","i32"),a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32");const e=a.getCodeBuilder(),o=e.i32_const(t.alloc(c));a.addCode(e.call(s+"_load",e.getLocal("scalar"),e.getLocal("scalarLen"),o),e.call(s+"_toMontgomery",o,o),e.call(s+"_mul",e.getLocal("x"),o,e.getLocal("r")))}(),ni(t,s),li(t,s+"_batchToMontgomery",s+"_toMontgomery",c,c),li(t,s+"_batchFromMontgomery",s+"_fromMontgomery",c,c),li(t,s+"_batchNeg",s+"_neg",c,c),ci(t,s+"_batchAdd",s+"_add",c,c),ci(t,s+"_batchSub",s+"_sub",c,c),ci(t,s+"_batchMul",s+"_mul",c,c),t.exportFunction(s+"_add"),t.exportFunction(s+"_sub"),t.exportFunction(s+"_neg"),t.exportFunction(s+"_isNegative"),t.exportFunction(s+"_isOne"),t.exportFunction(s+"_sign"),t.exportFunction(s+"_mReduct"),t.exportFunction(s+"_mul"),t.exportFunction(s+"_square"),t.exportFunction(s+"_squareOld"),t.exportFunction(s+"_fromMontgomery"),t.exportFunction(s+"_toMontgomery"),t.exportFunction(s+"_inverse"),t.exportFunction(s+"_one"),t.exportFunction(s+"_load"),t.exportFunction(s+"_timesScalar"),ii(t,s+"_exp",c,s+"_mul",s+"_square",r+"_copy",s+"_one"),t.exportFunction(s+"_exp"),t.exportFunction(s+"_batchInverse"),ui(i)&&(!function(){const a=t.addFunction(s+"_sqrt");a.addParam("n","i32"),a.addParam("r","i32"),a.addLocal("m","i32"),a.addLocal("i","i32"),a.addLocal("j","i32");const e=a.getCodeBuilder(),o=e.i32_const(_),i=e.i32_const(t.alloc(c)),n=e.i32_const(t.alloc(c)),l=e.i32_const(t.alloc(c)),r=e.i32_const(t.alloc(c)),d=e.i32_const(t.alloc(c));a.addCode(e.if(e.call(s+"_isZero",e.getLocal("n")),e.ret(e.call(s+"_zero",e.getLocal("r")))),e.setLocal("m",e.i32_const(w)),e.call(s+"_copy",e.i32_const(x),i),e.call(s+"_exp",e.getLocal("n"),e.i32_const(A),e.i32_const(c),n),e.call(s+"_exp",e.getLocal("n"),e.i32_const(I),e.i32_const(c),l),e.block(e.loop(e.br_if(1,e.call(s+"_eq",n,o)),e.call(s+"_square",n,r),e.setLocal("i",e.i32_const(1)),e.block(e.loop(e.br_if(1,e.call(s+"_eq",r,o)),e.call(s+"_square",r,r),e.setLocal("i",e.i32_add(e.getLocal("i"),e.i32_const(1))),e.br(0))),e.call(s+"_copy",i,d),e.setLocal("j",e.i32_sub(e.i32_sub(e.getLocal("m"),e.getLocal("i")),e.i32_const(1))),e.block(e.loop(e.br_if(1,e.i32_eqz(e.getLocal("j"))),e.call(s+"_square",d,d),e.setLocal("j",e.i32_sub(e.getLocal("j"),e.i32_const(1))),e.br(0))),e.setLocal("m",e.getLocal("i")),e.call(s+"_square",d,i),e.call(s+"_mul",n,i,n),e.call(s+"_mul",l,d,l),e.br(0))),e.if(e.call(s+"_isNegative",l),e.call(s+"_neg",l,e.getLocal("r")),e.call(s+"_copy",l,e.getLocal("r"))))}(),function(){const a=t.addFunction(s+"_isSquare");a.addParam("n","i32"),a.setReturnType("i32");const e=a.getCodeBuilder(),o=e.i32_const(_),i=e.i32_const(t.alloc(c));a.addCode(e.if(e.call(s+"_isZero",e.getLocal("n")),e.ret(e.i32_const(1))),e.call(s+"_exp",e.getLocal("n"),e.i32_const(h),e.i32_const(c),i),e.call(s+"_eq",i,o))}(),t.exportFunction(s+"_sqrt"),t.exportFunction(s+"_isSquare")),t.exportFunction(s+"_batchToMontgomery"),t.exportFunction(s+"_batchFromMontgomery"),s};const pi=fi,{bitLength:hi}=No;var mi=function(t,a,e,o,i){const n=BigInt(a),l=Math.floor((hi(n-1n)-1)/64)+1,c=8*l,s=e||"f1";if(t.modules[s])return s;t.modules[s]={n64:l};const r=i||"int",d=pi(t,n,o,r),u=t.modules[d].pR2,_=t.modules[d].pq,g=t.modules[d].pePlusOne;return function(){const a=t.alloc(c),e=t.addFunction(s+"_mul");e.addParam("x","i32"),e.addParam("y","i32"),e.addParam("r","i32");const o=e.getCodeBuilder();e.addCode(o.call(d+"_mul",o.getLocal("x"),o.getLocal("y"),o.i32_const(a))),e.addCode(o.call(d+"_mul",o.i32_const(a),o.i32_const(u),o.getLocal("r")))}(),function(){const a=t.addFunction(s+"_square");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(s+"_mul",e.getLocal("x"),e.getLocal("x"),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_inverse");a.addParam("x","i32"),a.addParam("r","i32");const e=a.getCodeBuilder();a.addCode(e.call(r+"_inverseMod",e.getLocal("x"),e.i32_const(_),e.getLocal("r")))}(),function(){const a=t.addFunction(s+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const e=a.getCodeBuilder();a.addCode(e.call(r+"_gte",e.getLocal("x"),e.i32_const(g)))}(),t.exportFunction(d+"_add",s+"_add"),t.exportFunction(d+"_sub",s+"_sub"),t.exportFunction(d+"_neg",s+"_neg"),t.exportFunction(s+"_mul"),t.exportFunction(s+"_square"),t.exportFunction(s+"_inverse"),t.exportFunction(s+"_isNegative"),t.exportFunction(d+"_copy",s+"_copy"),t.exportFunction(d+"_zero",s+"_zero"),t.exportFunction(d+"_one",s+"_one"),t.exportFunction(d+"_isZero",s+"_isZero"),t.exportFunction(d+"_eq",s+"_eq"),s};const Li=Uo,bi=Qo,wi=Mo;var yi=function(t,a,e,o){if(t.modules[e])return e;const i=8*t.modules[o].n64,n=t.modules[o].q;return t.modules[e]={n64:2*t.modules[o].n64},function(){const a=t.addFunction(e+"_isZero");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.i32_and(n.call(o+"_isZero",l),n.call(o+"_isZero",c)))}(),function(){const a=t.addFunction(e+"_isOne");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.ret(n.i32_and(n.call(o+"_isOne",l),n.call(o+"_isZero",c))))}(),function(){const a=t.addFunction(e+"_zero");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.call(o+"_zero",l),n.call(o+"_zero",c))}(),function(){const a=t.addFunction(e+"_one");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.call(o+"_one",l),n.call(o+"_zero",c))}(),function(){const a=t.addFunction(e+"_copy");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_copy",l,s),n.call(o+"_copy",c,r))}(),function(){const n=t.addFunction(e+"_mul");n.addParam("x","i32"),n.addParam("y","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.getLocal("y"),d=l.i32_add(l.getLocal("y"),l.i32_const(i)),u=l.getLocal("r"),_=l.i32_add(l.getLocal("r"),l.i32_const(i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i)),p=l.i32_const(t.alloc(i)),h=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_mul",c,r,g),l.call(o+"_mul",s,d,f),l.call(o+"_add",c,s,p),l.call(o+"_add",r,d,h),l.call(o+"_mul",p,h,p),l.call(a,f,u),l.call(o+"_add",g,u,u),l.call(o+"_add",g,f,_),l.call(o+"_sub",p,_,_))}(),function(){const a=t.addFunction(e+"_mul1");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_mul",l,s,r),n.call(o+"_mul",c,s,d))}(),function(){const n=t.addFunction(e+"_square");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.getLocal("r"),d=l.i32_add(l.getLocal("r"),l.i32_const(i)),u=l.i32_const(t.alloc(i)),_=l.i32_const(t.alloc(i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_mul",c,s,u),l.call(o+"_add",c,s,_),l.call(a,s,g),l.call(o+"_add",c,g,g),l.call(a,u,f),l.call(o+"_add",f,u,f),l.call(o+"_mul",_,g,r),l.call(o+"_sub",r,f,r),l.call(o+"_add",u,u,d))}(),function(){const a=t.addFunction(e+"_add");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.i32_add(n.getLocal("y"),n.i32_const(i)),d=n.getLocal("r"),u=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_add",l,s,d),n.call(o+"_add",c,r,u))}(),function(){const a=t.addFunction(e+"_sub");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.i32_add(n.getLocal("y"),n.i32_const(i)),d=n.getLocal("r"),u=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_sub",l,s,d),n.call(o+"_sub",c,r,u))}(),function(){const a=t.addFunction(e+"_neg");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_neg",l,s),n.call(o+"_neg",c,r))}(),function(){const a=t.addFunction(e+"_conjugate");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_copy",l,s),n.call(o+"_neg",c,r))}(),function(){const a=t.addFunction(e+"_toMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_toMontgomery",l,s),n.call(o+"_toMontgomery",c,r))}(),function(){const a=t.addFunction(e+"_fromMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_fromMontgomery",l,s),n.call(o+"_fromMontgomery",c,r))}(),function(){const a=t.addFunction(e+"_eq");a.addParam("x","i32"),a.addParam("y","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("y"),r=n.i32_add(n.getLocal("y"),n.i32_const(i));a.addCode(n.i32_and(n.call(o+"_eq",l,s),n.call(o+"_eq",c,r)))}(),function(){const n=t.addFunction(e+"_inverse");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.getLocal("r"),d=l.i32_add(l.getLocal("r"),l.i32_const(i)),u=l.i32_const(t.alloc(i)),_=l.i32_const(t.alloc(i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_square",c,u),l.call(o+"_square",s,_),l.call(a,_,g),l.call(o+"_sub",u,g,g),l.call(o+"_inverse",g,f),l.call(o+"_mul",c,f,r),l.call(o+"_mul",s,f,d),l.call(o+"_neg",d,d))}(),function(){const a=t.addFunction(e+"_timesScalar");a.addParam("x","i32"),a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.getLocal("r"),r=n.i32_add(n.getLocal("r"),n.i32_const(i));a.addCode(n.call(o+"_timesScalar",l,n.getLocal("scalar"),n.getLocal("scalarLen"),s),n.call(o+"_timesScalar",c,n.getLocal("scalar"),n.getLocal("scalarLen"),r))}(),function(){const a=t.addFunction(e+"_sign");a.addParam("x","i32"),a.addLocal("s","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.setLocal("s",n.call(o+"_sign",c)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.ret(n.call(o+"_sign",l)))}(),function(){const a=t.addFunction(e+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i));a.addCode(n.if(n.call(o+"_isZero",c),n.ret(n.call(o+"_isNegative",l))),n.ret(n.call(o+"_isNegative",c)))}(),t.exportFunction(e+"_isZero"),t.exportFunction(e+"_isOne"),t.exportFunction(e+"_zero"),t.exportFunction(e+"_one"),t.exportFunction(e+"_copy"),t.exportFunction(e+"_mul"),t.exportFunction(e+"_mul1"),t.exportFunction(e+"_square"),t.exportFunction(e+"_add"),t.exportFunction(e+"_sub"),t.exportFunction(e+"_neg"),t.exportFunction(e+"_sign"),t.exportFunction(e+"_conjugate"),t.exportFunction(e+"_fromMontgomery"),t.exportFunction(e+"_toMontgomery"),t.exportFunction(e+"_eq"),t.exportFunction(e+"_inverse"),bi(t,e),Li(t,e+"_exp",2*i,e+"_mul",e+"_square",e+"_copy",e+"_one"),function(){const a=t.addFunction(e+"_sqrt");a.addParam("a","i32"),a.addParam("pr","i32");const l=a.getCodeBuilder(),c=l.i32_const(t.alloc(wi.bigInt2BytesLE((BigInt(n||0)-3n)/4n,i))),s=l.i32_const(t.alloc(wi.bigInt2BytesLE((BigInt(n||0)-1n)/2n,i))),r=l.getLocal("a"),d=l.i32_const(t.alloc(2*i)),u=l.i32_const(t.alloc(2*i)),_=l.i32_const(t.alloc(2*i)),g=t.alloc(2*i),f=l.i32_const(g),p=l.i32_const(g),h=l.i32_const(g+i),m=l.i32_const(t.alloc(2*i)),L=l.i32_const(t.alloc(2*i));a.addCode(l.call(e+"_one",f),l.call(e+"_neg",f,f),l.call(e+"_exp",r,c,l.i32_const(i),d),l.call(e+"_square",d,u),l.call(e+"_mul",r,u,u),l.call(e+"_conjugate",u,_),l.call(e+"_mul",_,u,_),l.if(l.call(e+"_eq",_,f),l.unreachable()),l.call(e+"_mul",d,r,m),l.if(l.call(e+"_eq",u,f),[...l.call(o+"_zero",p),...l.call(o+"_one",h),...l.call(e+"_mul",f,m,l.getLocal("pr"))],[...l.call(e+"_one",L),...l.call(e+"_add",L,u,L),...l.call(e+"_exp",L,s,l.i32_const(i),L),...l.call(e+"_mul",L,m,l.getLocal("pr"))]))}(),function(){const a=t.addFunction(e+"_isSquare");a.addParam("a","i32"),a.setReturnType("i32");const o=a.getCodeBuilder(),l=o.i32_const(t.alloc(wi.bigInt2BytesLE((BigInt(n||0)-3n)/4n,i))),c=o.getLocal("a"),s=o.i32_const(t.alloc(2*i)),r=o.i32_const(t.alloc(2*i)),d=o.i32_const(t.alloc(2*i)),u=t.alloc(2*i),_=o.i32_const(u);a.addCode(o.call(e+"_one",_),o.call(e+"_neg",_,_),o.call(e+"_exp",c,l,o.i32_const(i),s),o.call(e+"_square",s,r),o.call(e+"_mul",c,r,r),o.call(e+"_conjugate",r,d),o.call(e+"_mul",d,r,d),o.if(o.call(e+"_eq",d,_),o.ret(o.i32_const(0))),o.ret(o.i32_const(1)))}(),t.exportFunction(e+"_exp"),t.exportFunction(e+"_timesScalar"),t.exportFunction(e+"_batchInverse"),t.exportFunction(e+"_sqrt"),t.exportFunction(e+"_isSquare"),t.exportFunction(e+"_isNegative"),e};const Ai=Uo,Ci=Qo;var xi=function(t,a,e,o){if(t.modules[e])return e;const i=8*t.modules[o].n64;return t.modules[e]={n64:3*t.modules[o].n64},function(){const a=t.addFunction(e+"_isZero");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.i32_and(n.i32_and(n.call(o+"_isZero",l),n.call(o+"_isZero",c)),n.call(o+"_isZero",s)))}(),function(){const a=t.addFunction(e+"_isOne");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.ret(n.i32_and(n.i32_and(n.call(o+"_isOne",l),n.call(o+"_isZero",c)),n.call(o+"_isZero",s))))}(),function(){const a=t.addFunction(e+"_zero");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.call(o+"_zero",l),n.call(o+"_zero",c),n.call(o+"_zero",s))}(),function(){const a=t.addFunction(e+"_one");a.addParam("x","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.call(o+"_one",l),n.call(o+"_zero",c),n.call(o+"_zero",s))}(),function(){const a=t.addFunction(e+"_copy");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_copy",l,r),n.call(o+"_copy",c,d),n.call(o+"_copy",s,u))}(),function(){const n=t.addFunction(e+"_mul");n.addParam("x","i32"),n.addParam("y","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.i32_add(l.getLocal("x"),l.i32_const(2*i)),d=l.getLocal("y"),u=l.i32_add(l.getLocal("y"),l.i32_const(i)),_=l.i32_add(l.getLocal("y"),l.i32_const(2*i)),g=l.getLocal("r"),f=l.i32_add(l.getLocal("r"),l.i32_const(i)),p=l.i32_add(l.getLocal("r"),l.i32_const(2*i)),h=l.i32_const(t.alloc(i)),m=l.i32_const(t.alloc(i)),L=l.i32_const(t.alloc(i)),b=l.i32_const(t.alloc(i)),w=l.i32_const(t.alloc(i)),y=l.i32_const(t.alloc(i)),A=l.i32_const(t.alloc(i)),C=l.i32_const(t.alloc(i)),x=l.i32_const(t.alloc(i)),F=l.i32_const(t.alloc(i)),I=l.i32_const(t.alloc(i)),B=l.i32_const(t.alloc(i)),E=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_mul",c,d,h),l.call(o+"_mul",s,u,m),l.call(o+"_mul",r,_,L),l.call(o+"_add",c,s,b),l.call(o+"_add",d,u,w),l.call(o+"_add",c,r,y),l.call(o+"_add",d,_,A),l.call(o+"_add",s,r,C),l.call(o+"_add",u,_,x),l.call(o+"_add",h,m,F),l.call(o+"_add",h,L,I),l.call(o+"_add",m,L,B),l.call(o+"_mul",C,x,g),l.call(o+"_sub",g,B,g),l.call(a,g,g),l.call(o+"_add",h,g,g),l.call(o+"_mul",b,w,f),l.call(o+"_sub",f,F,f),l.call(a,L,E),l.call(o+"_add",f,E,f),l.call(o+"_mul",y,A,p),l.call(o+"_sub",p,I,p),l.call(o+"_add",p,m,p))}(),function(){const n=t.addFunction(e+"_square");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.i32_add(l.getLocal("x"),l.i32_const(2*i)),d=l.getLocal("r"),u=l.i32_add(l.getLocal("r"),l.i32_const(i)),_=l.i32_add(l.getLocal("r"),l.i32_const(2*i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i)),p=l.i32_const(t.alloc(i)),h=l.i32_const(t.alloc(i)),m=l.i32_const(t.alloc(i)),L=l.i32_const(t.alloc(i)),b=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_square",c,g),l.call(o+"_mul",c,s,f),l.call(o+"_add",f,f,p),l.call(o+"_sub",c,s,h),l.call(o+"_add",h,r,h),l.call(o+"_square",h,h),l.call(o+"_mul",s,r,m),l.call(o+"_add",m,m,L),l.call(o+"_square",r,b),l.call(a,L,d),l.call(o+"_add",g,d,d),l.call(a,b,u),l.call(o+"_add",p,u,u),l.call(o+"_add",g,b,_),l.call(o+"_sub",L,_,_),l.call(o+"_add",h,_,_),l.call(o+"_add",p,_,_))}(),function(){const a=t.addFunction(e+"_add");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(i)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*i)),_=n.getLocal("r"),g=n.i32_add(n.getLocal("r"),n.i32_const(i)),f=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_add",l,r,_),n.call(o+"_add",c,d,g),n.call(o+"_add",s,u,f))}(),function(){const a=t.addFunction(e+"_sub");a.addParam("x","i32"),a.addParam("y","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(i)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*i)),_=n.getLocal("r"),g=n.i32_add(n.getLocal("r"),n.i32_const(i)),f=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_sub",l,r,_),n.call(o+"_sub",c,d,g),n.call(o+"_sub",s,u,f))}(),function(){const a=t.addFunction(e+"_neg");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_neg",l,r),n.call(o+"_neg",c,d),n.call(o+"_neg",s,u))}(),function(){const a=t.addFunction(e+"_sign");a.addParam("x","i32"),a.addLocal("s","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.setLocal("s",n.call(o+"_sign",s)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.setLocal("s",n.call(o+"_sign",c)),n.if(n.getLocal("s"),n.ret(n.getLocal("s"))),n.ret(n.call(o+"_sign",l)))}(),function(){const a=t.addFunction(e+"_toMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_toMontgomery",l,r),n.call(o+"_toMontgomery",c,d),n.call(o+"_toMontgomery",s,u))}(),function(){const a=t.addFunction(e+"_fromMontgomery");a.addParam("x","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_fromMontgomery",l,r),n.call(o+"_fromMontgomery",c,d),n.call(o+"_fromMontgomery",s,u))}(),function(){const a=t.addFunction(e+"_eq");a.addParam("x","i32"),a.addParam("y","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("y"),d=n.i32_add(n.getLocal("y"),n.i32_const(i)),u=n.i32_add(n.getLocal("y"),n.i32_const(2*i));a.addCode(n.i32_and(n.i32_and(n.call(o+"_eq",l,r),n.call(o+"_eq",c,d)),n.call(o+"_eq",s,u)))}(),function(){const n=t.addFunction(e+"_inverse");n.addParam("x","i32"),n.addParam("r","i32");const l=n.getCodeBuilder(),c=l.getLocal("x"),s=l.i32_add(l.getLocal("x"),l.i32_const(i)),r=l.i32_add(l.getLocal("x"),l.i32_const(2*i)),d=l.getLocal("r"),u=l.i32_add(l.getLocal("r"),l.i32_const(i)),_=l.i32_add(l.getLocal("r"),l.i32_const(2*i)),g=l.i32_const(t.alloc(i)),f=l.i32_const(t.alloc(i)),p=l.i32_const(t.alloc(i)),h=l.i32_const(t.alloc(i)),m=l.i32_const(t.alloc(i)),L=l.i32_const(t.alloc(i)),b=l.i32_const(t.alloc(i)),w=l.i32_const(t.alloc(i)),y=l.i32_const(t.alloc(i)),A=l.i32_const(t.alloc(i)),C=l.i32_const(t.alloc(i));n.addCode(l.call(o+"_square",c,g),l.call(o+"_square",s,f),l.call(o+"_square",r,p),l.call(o+"_mul",c,s,h),l.call(o+"_mul",c,r,m),l.call(o+"_mul",s,r,L),l.call(a,L,b),l.call(o+"_sub",g,b,b),l.call(a,p,w),l.call(o+"_sub",w,h,w),l.call(o+"_sub",f,m,y),l.call(o+"_mul",r,w,A),l.call(o+"_mul",s,y,C),l.call(o+"_add",A,C,A),l.call(a,A,A),l.call(o+"_mul",c,b,C),l.call(o+"_add",C,A,A),l.call(o+"_inverse",A,A),l.call(o+"_mul",A,b,d),l.call(o+"_mul",A,w,u),l.call(o+"_mul",A,y,_))}(),function(){const a=t.addFunction(e+"_timesScalar");a.addParam("x","i32"),a.addParam("scalar","i32"),a.addParam("scalarLen","i32"),a.addParam("r","i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i)),r=n.getLocal("r"),d=n.i32_add(n.getLocal("r"),n.i32_const(i)),u=n.i32_add(n.getLocal("r"),n.i32_const(2*i));a.addCode(n.call(o+"_timesScalar",l,n.getLocal("scalar"),n.getLocal("scalarLen"),r),n.call(o+"_timesScalar",c,n.getLocal("scalar"),n.getLocal("scalarLen"),d),n.call(o+"_timesScalar",s,n.getLocal("scalar"),n.getLocal("scalarLen"),u))}(),function(){const a=t.addFunction(e+"_isNegative");a.addParam("x","i32"),a.setReturnType("i32");const n=a.getCodeBuilder(),l=n.getLocal("x"),c=n.i32_add(n.getLocal("x"),n.i32_const(i)),s=n.i32_add(n.getLocal("x"),n.i32_const(2*i));a.addCode(n.if(n.call(o+"_isZero",s),n.if(n.call(o+"_isZero",c),n.ret(n.call(o+"_isNegative",l)),n.ret(n.call(o+"_isNegative",c)))),n.ret(n.call(o+"_isNegative",s)))}(),t.exportFunction(e+"_isZero"),t.exportFunction(e+"_isOne"),t.exportFunction(e+"_zero"),t.exportFunction(e+"_one"),t.exportFunction(e+"_copy"),t.exportFunction(e+"_mul"),t.exportFunction(e+"_square"),t.exportFunction(e+"_add"),t.exportFunction(e+"_sub"),t.exportFunction(e+"_neg"),t.exportFunction(e+"_sign"),t.exportFunction(e+"_fromMontgomery"),t.exportFunction(e+"_toMontgomery"),t.exportFunction(e+"_eq"),t.exportFunction(e+"_inverse"),Ci(t,e),Ai(t,e+"_exp",3*i,e+"_mul",e+"_square",e+"_copy",e+"_one"),t.exportFunction(e+"_exp"),t.exportFunction(e+"_timesScalar"),t.exportFunction(e+"_batchInverse"),t.exportFunction(e+"_isNegative"),e};const Fi=function(t,a,e,o,i,n,l,c){const s=t.addFunction(a);s.addParam("base","i32"),s.addParam("scalar","i32"),s.addParam("scalarLength","i32"),s.addParam("r","i32"),s.addLocal("old0","i32"),s.addLocal("nbits","i32"),s.addLocal("i","i32"),s.addLocal("last","i32"),s.addLocal("cur","i32"),s.addLocal("carry","i32"),s.addLocal("p","i32");const r=s.getCodeBuilder(),d=r.i32_const(t.alloc(e));function u(t){return r.i32_and(r.i32_shr_u(r.i32_load(r.i32_add(r.getLocal("scalar"),r.i32_and(r.i32_shr_u(t,r.i32_const(3)),r.i32_const(4294967292)))),r.i32_and(t,r.i32_const(31))),r.i32_const(1))}function _(t){return[...r.i32_store8(r.getLocal("p"),r.i32_const(t)),...r.setLocal("p",r.i32_add(r.getLocal("p"),r.i32_const(1)))]}s.addCode(r.if(r.i32_eqz(r.getLocal("scalarLength")),[...r.call(c,r.getLocal("r")),...r.ret([])]),r.setLocal("nbits",r.i32_shl(r.getLocal("scalarLength"),r.i32_const(3))),r.setLocal("old0",r.i32_load(r.i32_const(0))),r.setLocal("p",r.getLocal("old0")),r.i32_store(r.i32_const(0),r.i32_and(r.i32_add(r.i32_add(r.getLocal("old0"),r.i32_const(32)),r.getLocal("nbits")),r.i32_const(4294967288))),r.setLocal("i",r.i32_const(1)),r.setLocal("last",u(r.i32_const(0))),r.setLocal("carry",r.i32_const(0)),r.block(r.loop(r.br_if(1,r.i32_eq(r.getLocal("i"),r.getLocal("nbits"))),r.setLocal("cur",u(r.getLocal("i"))),r.if(r.getLocal("last"),r.if(r.getLocal("cur"),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(1)],[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(255)]),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(255)],[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(0)),..._(1)])),r.if(r.getLocal("cur"),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(1)),..._(0)],[...r.setLocal("last",r.i32_const(1)),...r.setLocal("carry",r.i32_const(0)),..._(0)]),r.if(r.getLocal("carry"),[...r.setLocal("last",r.i32_const(1)),...r.setLocal("carry",r.i32_const(0)),..._(0)],[...r.setLocal("last",r.i32_const(0)),...r.setLocal("carry",r.i32_const(0)),..._(0)]))),r.setLocal("i",r.i32_add(r.getLocal("i"),r.i32_const(1))),r.br(0))),r.if(r.getLocal("last"),r.if(r.getLocal("carry"),[..._(255),..._(0),..._(1)],[..._(1)]),r.if(r.getLocal("carry"),[..._(0),..._(1)])),r.setLocal("p",r.i32_sub(r.getLocal("p"),r.i32_const(1))),r.call(l,r.getLocal("base"),d),r.call(c,r.getLocal("r")),r.block(r.loop(r.call(i,r.getLocal("r"),r.getLocal("r")),r.setLocal("cur",r.i32_load8_u(r.getLocal("p"))),r.if(r.getLocal("cur"),r.if(r.i32_eq(r.getLocal("cur"),r.i32_const(1)),r.call(o,r.getLocal("r"),d,r.getLocal("r")),r.call(n,r.getLocal("r"),d,r.getLocal("r")))),r.br_if(1,r.i32_eq(r.getLocal("old0"),r.getLocal("p"))),r.setLocal("p",r.i32_sub(r.getLocal("p"),r.i32_const(1))),r.br(0))),r.i32_store(r.i32_const(0),r.getLocal("old0")))},Ii=ko,Bi=function(t,a,e,o,i){const n=8*t.modules[a].n64;function l(){const o=t.addFunction(e);o.addParam("pBases","i32"),o.addParam("pScalars","i32"),o.addParam("scalarSize","i32"),o.addParam("n","i32"),o.addParam("pr","i32"),o.addLocal("chunkSize","i32"),o.addLocal("nChunks","i32"),o.addLocal("itScalar","i32"),o.addLocal("endScalar","i32"),o.addLocal("itBase","i32"),o.addLocal("itBit","i32"),o.addLocal("i","i32"),o.addLocal("j","i32"),o.addLocal("nTable","i32"),o.addLocal("pTable","i32"),o.addLocal("idx","i32"),o.addLocal("pIdxTable","i32");const i=o.getCodeBuilder(),l=i.i32_const(t.alloc(n)),c=t.alloc([17,17,17,17,17,17,17,17,17,17,16,16,15,14,13,13,12,11,10,9,8,7,7,6,5,4,3,2,1,1,1,1]);o.addCode(i.call(a+"_zero",i.getLocal("pr")),i.if(i.i32_eqz(i.getLocal("n")),i.ret([])),i.setLocal("chunkSize",i.i32_load8_u(i.i32_clz(i.getLocal("n")),c)),i.setLocal("nChunks",i.i32_add(i.i32_div_u(i.i32_sub(i.i32_shl(i.getLocal("scalarSize"),i.i32_const(3)),i.i32_const(1)),i.getLocal("chunkSize")),i.i32_const(1))),i.setLocal("itBit",i.i32_mul(i.i32_sub(i.getLocal("nChunks"),i.i32_const(1)),i.getLocal("chunkSize"))),i.block(i.loop(i.br_if(1,i.i32_lt_s(i.getLocal("itBit"),i.i32_const(0))),i.if(i.i32_eqz(i.call(a+"_isZero",i.getLocal("pr"))),[...i.setLocal("j",i.i32_const(0)),...i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("j"),i.getLocal("chunkSize"))),i.call(a+"_double",i.getLocal("pr"),i.getLocal("pr")),i.setLocal("j",i.i32_add(i.getLocal("j"),i.i32_const(1))),i.br(0)))]),i.call(e+"_chunk",i.getLocal("pBases"),i.getLocal("pScalars"),i.getLocal("scalarSize"),i.getLocal("n"),i.getLocal("itBit"),i.getLocal("chunkSize"),l),i.call(a+"_add",i.getLocal("pr"),l,i.getLocal("pr")),i.setLocal("itBit",i.i32_sub(i.getLocal("itBit"),i.getLocal("chunkSize"))),i.br(0))))}!function(){const a=t.addFunction(e+"_getChunk");a.addParam("pScalar","i32"),a.addParam("scalarSize","i32"),a.addParam("startBit","i32"),a.addParam("chunkSize","i32"),a.addLocal("bitsToEnd","i32"),a.addLocal("mask","i32"),a.setReturnType("i32");const o=a.getCodeBuilder();a.addCode(o.setLocal("bitsToEnd",o.i32_sub(o.i32_mul(o.getLocal("scalarSize"),o.i32_const(8)),o.getLocal("startBit"))),o.if(o.i32_gt_s(o.getLocal("chunkSize"),o.getLocal("bitsToEnd")),o.setLocal("mask",o.i32_sub(o.i32_shl(o.i32_const(1),o.getLocal("bitsToEnd")),o.i32_const(1))),o.setLocal("mask",o.i32_sub(o.i32_shl(o.i32_const(1),o.getLocal("chunkSize")),o.i32_const(1)))),o.i32_and(o.i32_shr_u(o.i32_load(o.i32_add(o.getLocal("pScalar"),o.i32_shr_u(o.getLocal("startBit"),o.i32_const(3))),0,0),o.i32_and(o.getLocal("startBit"),o.i32_const(7))),o.getLocal("mask")))}(),function(){const o=t.addFunction(e+"_reduceTable");o.addParam("pTable","i32"),o.addParam("p","i32"),o.addLocal("half","i32"),o.addLocal("it1","i32"),o.addLocal("it2","i32"),o.addLocal("pAcc","i32");const i=o.getCodeBuilder();o.addCode(i.if(i.i32_eq(i.getLocal("p"),i.i32_const(1)),i.ret([])),i.setLocal("half",i.i32_shl(i.i32_const(1),i.i32_sub(i.getLocal("p"),i.i32_const(1)))),i.setLocal("it1",i.getLocal("pTable")),i.setLocal("it2",i.i32_add(i.getLocal("pTable"),i.i32_mul(i.getLocal("half"),i.i32_const(n)))),i.setLocal("pAcc",i.i32_sub(i.getLocal("it2"),i.i32_const(n))),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("it1"),i.getLocal("pAcc"))),i.call(a+"_add",i.getLocal("it1"),i.getLocal("it2"),i.getLocal("it1")),i.call(a+"_add",i.getLocal("pAcc"),i.getLocal("it2"),i.getLocal("pAcc")),i.setLocal("it1",i.i32_add(i.getLocal("it1"),i.i32_const(n))),i.setLocal("it2",i.i32_add(i.getLocal("it2"),i.i32_const(n))),i.br(0))),i.call(e+"_reduceTable",i.getLocal("pTable"),i.i32_sub(i.getLocal("p"),i.i32_const(1))),i.setLocal("p",i.i32_sub(i.getLocal("p"),i.i32_const(1))),i.block(i.loop(i.br_if(1,i.i32_eqz(i.getLocal("p"))),i.call(a+"_double",i.getLocal("pAcc"),i.getLocal("pAcc")),i.setLocal("p",i.i32_sub(i.getLocal("p"),i.i32_const(1))),i.br(0))),i.call(a+"_add",i.getLocal("pTable"),i.getLocal("pAcc"),i.getLocal("pTable")))}(),function(){const l=t.addFunction(e+"_chunk");l.addParam("pBases","i32"),l.addParam("pScalars","i32"),l.addParam("scalarSize","i32"),l.addParam("n","i32"),l.addParam("startBit","i32"),l.addParam("chunkSize","i32"),l.addParam("pr","i32"),l.addLocal("nChunks","i32"),l.addLocal("itScalar","i32"),l.addLocal("endScalar","i32"),l.addLocal("itBase","i32"),l.addLocal("i","i32"),l.addLocal("j","i32"),l.addLocal("nTable","i32"),l.addLocal("pTable","i32"),l.addLocal("idx","i32"),l.addLocal("pIdxTable","i32");const c=l.getCodeBuilder();l.addCode(c.if(c.i32_eqz(c.getLocal("n")),[...c.call(a+"_zero",c.getLocal("pr")),...c.ret([])]),c.setLocal("nTable",c.i32_shl(c.i32_const(1),c.getLocal("chunkSize"))),c.setLocal("pTable",c.i32_load(c.i32_const(0))),c.i32_store(c.i32_const(0),c.i32_add(c.getLocal("pTable"),c.i32_mul(c.getLocal("nTable"),c.i32_const(n)))),c.setLocal("j",c.i32_const(0)),c.block(c.loop(c.br_if(1,c.i32_eq(c.getLocal("j"),c.getLocal("nTable"))),c.call(a+"_zero",c.i32_add(c.getLocal("pTable"),c.i32_mul(c.getLocal("j"),c.i32_const(n)))),c.setLocal("j",c.i32_add(c.getLocal("j"),c.i32_const(1))),c.br(0))),c.setLocal("itBase",c.getLocal("pBases")),c.setLocal("itScalar",c.getLocal("pScalars")),c.setLocal("endScalar",c.i32_add(c.getLocal("pScalars"),c.i32_mul(c.getLocal("n"),c.getLocal("scalarSize")))),c.block(c.loop(c.br_if(1,c.i32_eq(c.getLocal("itScalar"),c.getLocal("endScalar"))),c.setLocal("idx",c.call(e+"_getChunk",c.getLocal("itScalar"),c.getLocal("scalarSize"),c.getLocal("startBit"),c.getLocal("chunkSize"))),c.if(c.getLocal("idx"),[...c.setLocal("pIdxTable",c.i32_add(c.getLocal("pTable"),c.i32_mul(c.i32_sub(c.getLocal("idx"),c.i32_const(1)),c.i32_const(n)))),...c.call(o,c.getLocal("pIdxTable"),c.getLocal("itBase"),c.getLocal("pIdxTable"))]),c.setLocal("itScalar",c.i32_add(c.getLocal("itScalar"),c.getLocal("scalarSize"))),c.setLocal("itBase",c.i32_add(c.getLocal("itBase"),c.i32_const(i))),c.br(0))),c.call(e+"_reduceTable",c.getLocal("pTable"),c.getLocal("chunkSize")),c.call(a+"_copy",c.getLocal("pTable"),c.getLocal("pr")),c.i32_store(c.i32_const(0),c.getLocal("pTable")))}(),l(),t.exportFunction(e),t.exportFunction(e+"_chunk")};var Ei=function(t,a,e,o){const i=t.modules[e].n64,n=8*i;if(t.modules[a])return a;return t.modules[a]={n64:3*i},function(){const o=t.addFunction(a+"_isZeroAffine");o.addParam("p1","i32"),o.setReturnType("i32");const i=o.getCodeBuilder();o.addCode(i.i32_and(i.call(e+"_isZero",i.getLocal("p1")),i.call(e+"_isZero",i.i32_add(i.getLocal("p1"),i.i32_const(n)))))}(),function(){const o=t.addFunction(a+"_isZero");o.addParam("p1","i32"),o.setReturnType("i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_isZero",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))))}(),function(){const o=t.addFunction(a+"_zeroAffine");o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_zero",i.getLocal("pr"))),o.addCode(i.call(e+"_zero",i.i32_add(i.getLocal("pr"),i.i32_const(n))))}(),function(){const o=t.addFunction(a+"_zero");o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_zero",i.getLocal("pr"))),o.addCode(i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(n)))),o.addCode(i.call(e+"_zero",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))))}(),function(){const e=t.addFunction(a+"_copyAffine");e.addParam("ps","i32"),e.addParam("pd","i32");const o=e.getCodeBuilder();for(let t=0;t<2*i;t++)e.addCode(o.i64_store(o.getLocal("pd"),8*t,o.i64_load(o.getLocal("ps"),8*t)))}(),function(){const e=t.addFunction(a+"_copy");e.addParam("ps","i32"),e.addParam("pd","i32");const o=e.getCodeBuilder();for(let t=0;t<3*i;t++)e.addCode(o.i64_store(o.getLocal("pd"),8*t,o.i64_load(o.getLocal("ps"),8*t)))}(),function(){const o=t.addFunction(a+"_toJacobian");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.getLocal("pr"),r=i.i32_add(i.getLocal("pr"),i.i32_const(n)),d=i.i32_add(i.getLocal("pr"),i.i32_const(2*n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("p1")),i.call(a+"_zero",i.getLocal("pr")),[...i.call(e+"_one",d),...i.call(e+"_copy",c,r),...i.call(e+"_copy",l,s)]))}(),function(){const o=t.addFunction(a+"_eqAffine");o.addParam("p1","i32"),o.addParam("p2","i32"),o.setReturnType("i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder();o.addCode(i.ret(i.i32_and(i.call(e+"_eq",i.getLocal("p1"),i.getLocal("p2")),i.call(e+"_eq",i.i32_add(i.getLocal("p1"),i.i32_const(n)),i.i32_add(i.getLocal("p2"),i.i32_const(n))))))}(),function(){const o=t.addFunction(a+"_eqMixed");o.addParam("p1","i32"),o.addParam("p2","i32"),o.setReturnType("i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n)),u=i.i32_const(t.alloc(n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),i.ret(i.call(a+"_isZeroAffine",i.getLocal("p2")))),i.if(i.call(a+"_isZeroAffine",i.getLocal("p2")),i.ret(i.i32_const(0))),i.if(i.call(e+"_isOne",s),i.ret(i.call(a+"_eqAffine",i.getLocal("p1"),i.getLocal("p2")))),i.call(e+"_square",s,u),i.call(e+"_mul",r,u,_),i.call(e+"_mul",s,u,g),i.call(e+"_mul",d,g,f),i.if(i.call(e+"_eq",l,_),i.if(i.call(e+"_eq",c,f),i.ret(i.i32_const(1)))),i.ret(i.i32_const(0)))}(),function(){const o=t.addFunction(a+"_eq");o.addParam("p1","i32"),o.addParam("p2","i32"),o.setReturnType("i32"),o.addLocal("z1","i32"),o.addLocal("z2","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n));o.addCode(i.setLocal("z2",i.i32_add(i.getLocal("p2"),i.i32_const(2*n))));const u=i.getLocal("z2"),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),i.ret(i.call(a+"_isZero",i.getLocal("p2")))),i.if(i.call(a+"_isZero",i.getLocal("p2")),i.ret(i.i32_const(0))),i.if(i.call(e+"_isOne",s),i.ret(i.call(a+"_eqMixed",i.getLocal("p2"),i.getLocal("p1")))),i.if(i.call(e+"_isOne",u),i.ret(i.call(a+"_eqMixed",i.getLocal("p1"),i.getLocal("p2")))),i.call(e+"_square",s,_),i.call(e+"_square",u,g),i.call(e+"_mul",l,g,f),i.call(e+"_mul",r,_,p),i.call(e+"_mul",s,_,h),i.call(e+"_mul",u,g,m),i.call(e+"_mul",c,m,L),i.call(e+"_mul",d,h,b),i.if(i.call(e+"_eq",f,p),i.if(i.call(e+"_eq",L,b),i.ret(i.i32_const(1)))),i.ret(i.i32_const(0)))}(),function(){const o=t.addFunction(a+"_doubleAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.getLocal("pr"),r=i.i32_add(i.getLocal("pr"),i.i32_const(n)),d=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),u=i.i32_const(t.alloc(n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("p1")),[...i.call(a+"_toJacobian",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.call(e+"_square",l,u),i.call(e+"_square",c,_),i.call(e+"_square",_,g),i.call(e+"_add",l,_,f),i.call(e+"_square",f,f),i.call(e+"_sub",f,u,f),i.call(e+"_sub",f,g,f),i.call(e+"_add",f,f,f),i.call(e+"_add",u,u,p),i.call(e+"_add",p,u,p),i.call(e+"_add",c,c,d),i.call(e+"_square",p,s),i.call(e+"_sub",s,f,s),i.call(e+"_sub",s,f,s),i.call(e+"_add",g,g,h),i.call(e+"_add",h,h,h),i.call(e+"_add",h,h,h),i.call(e+"_sub",f,s,r),i.call(e+"_mul",r,p,r),i.call(e+"_sub",r,h,r))}(),function(){const o=t.addFunction(a+"_double");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(a+"_copy",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(e+"_isOne",s),[...i.ret(i.call(a+"_doubleAffine",i.getLocal("p1"),i.getLocal("pr"))),...i.ret([])]),i.call(e+"_square",l,_),i.call(e+"_square",c,g),i.call(e+"_square",g,f),i.call(e+"_add",l,g,p),i.call(e+"_square",p,p),i.call(e+"_sub",p,_,p),i.call(e+"_sub",p,f,p),i.call(e+"_add",p,p,p),i.call(e+"_add",_,_,h),i.call(e+"_add",h,_,h),i.call(e+"_square",h,m),i.call(e+"_mul",c,s,L),i.call(e+"_add",p,p,r),i.call(e+"_sub",m,r,r),i.call(e+"_add",f,f,b),i.call(e+"_add",b,b,b),i.call(e+"_add",b,b,b),i.call(e+"_sub",p,r,d),i.call(e+"_mul",d,h,d),i.call(e+"_sub",d,b,d),i.call(e+"_add",L,L,u))}(),function(){const o=t.addFunction(a+"_addAffine");o.addParam("p1","i32"),o.addParam("p2","i32"),o.addParam("pr","i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("p2"),r=i.i32_add(i.getLocal("p2"),i.i32_const(n)),d=i.getLocal("pr"),u=i.i32_add(i.getLocal("pr"),i.i32_const(n)),_=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n)),w=i.i32_const(t.alloc(n)),y=i.i32_const(t.alloc(n)),A=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZeroAffine",i.getLocal("p1")),[...i.call(a+"_copyAffine",i.getLocal("p2"),i.getLocal("pr")),...i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))),...i.ret([])]),i.if(i.call(a+"_isZeroAffine",i.getLocal("p2")),[...i.call(a+"_copyAffine",i.getLocal("p1"),i.getLocal("pr")),...i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))),...i.ret([])]),i.if(i.call(e+"_eq",l,s),i.if(i.call(e+"_eq",c,r),[...i.call(a+"_doubleAffine",i.getLocal("p2"),i.getLocal("pr")),...i.ret([])])),i.call(e+"_sub",s,l,g),i.call(e+"_sub",r,c,p),i.call(e+"_square",g,f),i.call(e+"_add",f,f,h),i.call(e+"_add",h,h,h),i.call(e+"_mul",g,h,m),i.call(e+"_add",p,p,L),i.call(e+"_mul",l,h,w),i.call(e+"_square",L,b),i.call(e+"_add",w,w,y),i.call(e+"_sub",b,m,d),i.call(e+"_sub",d,y,d),i.call(e+"_mul",c,m,A),i.call(e+"_add",A,A,A),i.call(e+"_sub",w,d,u),i.call(e+"_mul",u,L,u),i.call(e+"_sub",u,A,u),i.call(e+"_add",g,g,_))}(),function(){const o=t.addFunction(a+"_addMixed");o.addParam("p1","i32"),o.addParam("p2","i32"),o.addParam("pr","i32"),o.addLocal("z1","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n)),u=i.getLocal("pr"),_=i.i32_add(i.getLocal("pr"),i.i32_const(n)),g=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),f=i.i32_const(t.alloc(n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n)),w=i.i32_const(t.alloc(n)),y=i.i32_const(t.alloc(n)),A=i.i32_const(t.alloc(n)),C=i.i32_const(t.alloc(n)),x=i.i32_const(t.alloc(n)),F=i.i32_const(t.alloc(n)),I=i.i32_const(t.alloc(n)),B=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(a+"_copyAffine",i.getLocal("p2"),i.getLocal("pr")),...i.call(e+"_one",i.i32_add(i.getLocal("pr"),i.i32_const(2*n))),...i.ret([])]),i.if(i.call(a+"_isZeroAffine",i.getLocal("p2")),[...i.call(a+"_copy",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(e+"_isOne",s),[...i.call(a+"_addAffine",l,r,u),...i.ret([])]),i.call(e+"_square",s,f),i.call(e+"_mul",r,f,p),i.call(e+"_mul",s,f,h),i.call(e+"_mul",d,h,m),i.if(i.call(e+"_eq",l,p),i.if(i.call(e+"_eq",c,m),[...i.call(a+"_doubleAffine",i.getLocal("p2"),i.getLocal("pr")),...i.ret([])])),i.call(e+"_sub",p,l,L),i.call(e+"_sub",m,c,w),i.call(e+"_square",L,b),i.call(e+"_add",b,b,y),i.call(e+"_add",y,y,y),i.call(e+"_mul",L,y,A),i.call(e+"_add",w,w,C),i.call(e+"_mul",l,y,F),i.call(e+"_square",C,x),i.call(e+"_add",F,F,I),i.call(e+"_sub",x,A,u),i.call(e+"_sub",u,I,u),i.call(e+"_mul",c,A,B),i.call(e+"_add",B,B,B),i.call(e+"_sub",F,u,_),i.call(e+"_mul",_,C,_),i.call(e+"_sub",_,B,_),i.call(e+"_add",s,L,g),i.call(e+"_square",g,g),i.call(e+"_sub",g,f,g),i.call(e+"_sub",g,b,g))}(),function(){const o=t.addFunction(a+"_add");o.addParam("p1","i32"),o.addParam("p2","i32"),o.addParam("pr","i32"),o.addLocal("z1","i32"),o.addLocal("z2","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n));o.addCode(i.setLocal("z1",i.i32_add(i.getLocal("p1"),i.i32_const(2*n))));const s=i.getLocal("z1"),r=i.getLocal("p2"),d=i.i32_add(i.getLocal("p2"),i.i32_const(n));o.addCode(i.setLocal("z2",i.i32_add(i.getLocal("p2"),i.i32_const(2*n))));const u=i.getLocal("z2"),_=i.getLocal("pr"),g=i.i32_add(i.getLocal("pr"),i.i32_const(n)),f=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),p=i.i32_const(t.alloc(n)),h=i.i32_const(t.alloc(n)),m=i.i32_const(t.alloc(n)),L=i.i32_const(t.alloc(n)),b=i.i32_const(t.alloc(n)),w=i.i32_const(t.alloc(n)),y=i.i32_const(t.alloc(n)),A=i.i32_const(t.alloc(n)),C=i.i32_const(t.alloc(n)),x=i.i32_const(t.alloc(n)),F=i.i32_const(t.alloc(n)),I=i.i32_const(t.alloc(n)),B=i.i32_const(t.alloc(n)),E=i.i32_const(t.alloc(n)),v=i.i32_const(t.alloc(n)),S=i.i32_const(t.alloc(n)),P=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(a+"_copy",i.getLocal("p2"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(a+"_isZero",i.getLocal("p2")),[...i.call(a+"_copy",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])]),i.if(i.call(e+"_isOne",s),[...i.call(a+"_addMixed",r,l,_),...i.ret([])]),i.if(i.call(e+"_isOne",u),[...i.call(a+"_addMixed",l,r,_),...i.ret([])]),i.call(e+"_square",s,p),i.call(e+"_square",u,h),i.call(e+"_mul",l,h,m),i.call(e+"_mul",r,p,L),i.call(e+"_mul",s,p,b),i.call(e+"_mul",u,h,w),i.call(e+"_mul",c,w,y),i.call(e+"_mul",d,b,A),i.if(i.call(e+"_eq",m,L),i.if(i.call(e+"_eq",y,A),[...i.call(a+"_double",i.getLocal("p1"),i.getLocal("pr")),...i.ret([])])),i.call(e+"_sub",L,m,C),i.call(e+"_sub",A,y,x),i.call(e+"_add",C,C,F),i.call(e+"_square",F,F),i.call(e+"_mul",C,F,I),i.call(e+"_add",x,x,B),i.call(e+"_mul",m,F,v),i.call(e+"_square",B,E),i.call(e+"_add",v,v,S),i.call(e+"_sub",E,I,_),i.call(e+"_sub",_,S,_),i.call(e+"_mul",y,I,P),i.call(e+"_add",P,P,P),i.call(e+"_sub",v,_,g),i.call(e+"_mul",g,B,g),i.call(e+"_sub",g,P,g),i.call(e+"_add",s,u,f),i.call(e+"_square",f,f),i.call(e+"_sub",f,p,f),i.call(e+"_sub",f,h,f),i.call(e+"_mul",f,C,f))}(),function(){const o=t.addFunction(a+"_negAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.getLocal("pr"),r=i.i32_add(i.getLocal("pr"),i.i32_const(n));o.addCode(i.call(e+"_copy",l,s),i.call(e+"_neg",c,r))}(),function(){const o=t.addFunction(a+"_neg");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_add(i.getLocal("pr"),i.i32_const(2*n));o.addCode(i.call(e+"_copy",l,r),i.call(e+"_neg",c,d),i.call(e+"_copy",s,u))}(),function(){const e=t.addFunction(a+"_subAffine");e.addParam("p1","i32"),e.addParam("p2","i32"),e.addParam("pr","i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(3*n));e.addCode(o.call(a+"_negAffine",o.getLocal("p2"),i),o.call(a+"_addAffine",o.getLocal("p1"),i,o.getLocal("pr")))}(),function(){const e=t.addFunction(a+"_subMixed");e.addParam("p1","i32"),e.addParam("p2","i32"),e.addParam("pr","i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(3*n));e.addCode(o.call(a+"_negAffine",o.getLocal("p2"),i),o.call(a+"_addMixed",o.getLocal("p1"),i,o.getLocal("pr")))}(),function(){const e=t.addFunction(a+"_sub");e.addParam("p1","i32"),e.addParam("p2","i32"),e.addParam("pr","i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(3*n));e.addCode(o.call(a+"_neg",o.getLocal("p2"),i),o.call(a+"_add",o.getLocal("p1"),i,o.getLocal("pr")))}(),function(){const o=t.addFunction(a+"_fromMontgomeryAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_fromMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<2;t++)o.addCode(i.call(e+"_fromMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_fromMontgomery");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_fromMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<3;t++)o.addCode(i.call(e+"_fromMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_toMontgomeryAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_toMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<2;t++)o.addCode(i.call(e+"_toMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_toMontgomery");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder();o.addCode(i.call(e+"_toMontgomery",i.getLocal("p1"),i.getLocal("pr")));for(let t=1;t<3;t++)o.addCode(i.call(e+"_toMontgomery",i.i32_add(i.getLocal("p1"),i.i32_const(t*n)),i.i32_add(i.getLocal("pr"),i.i32_const(t*n))))}(),function(){const o=t.addFunction(a+"_toAffine");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_const(t.alloc(n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),[...i.call(e+"_zero",r),...i.call(e+"_zero",d)],[...i.call(e+"_inverse",s,u),...i.call(e+"_square",u,_),...i.call(e+"_mul",u,_,g),...i.call(e+"_mul",l,_,r),...i.call(e+"_mul",c,g,d)]))}(),function(){const i=t.addFunction(a+"_inCurveAffine");i.addParam("pIn","i32"),i.setReturnType("i32");const l=i.getCodeBuilder(),c=l.getLocal("pIn"),s=l.i32_add(l.getLocal("pIn"),l.i32_const(n)),r=l.i32_const(t.alloc(n)),d=l.i32_const(t.alloc(n));i.addCode(l.call(e+"_square",s,r),l.call(e+"_square",c,d),l.call(e+"_mul",c,d,d),l.call(e+"_add",d,l.i32_const(o),d),l.ret(l.call(e+"_eq",r,d)))}(),function(){const e=t.addFunction(a+"_inCurve");e.addParam("pIn","i32"),e.setReturnType("i32");const o=e.getCodeBuilder(),i=o.i32_const(t.alloc(2*n));e.addCode(o.call(a+"_toAffine",o.getLocal("pIn"),i),o.ret(o.call(a+"_inCurveAffine",i)))}(),function(){const o=t.addFunction(a+"_batchToAffine");o.addParam("pIn","i32"),o.addParam("n","i32"),o.addParam("pOut","i32"),o.addLocal("pAux","i32"),o.addLocal("itIn","i32"),o.addLocal("itAux","i32"),o.addLocal("itOut","i32"),o.addLocal("i","i32");const i=o.getCodeBuilder(),l=i.i32_const(t.alloc(n));o.addCode(i.setLocal("pAux",i.i32_load(i.i32_const(0))),i.i32_store(i.i32_const(0),i.i32_add(i.getLocal("pAux"),i.i32_mul(i.getLocal("n"),i.i32_const(n)))),i.call(e+"_batchInverse",i.i32_add(i.getLocal("pIn"),i.i32_const(2*n)),i.i32_const(3*n),i.getLocal("n"),i.getLocal("pAux"),i.i32_const(n)),i.setLocal("itIn",i.getLocal("pIn")),i.setLocal("itAux",i.getLocal("pAux")),i.setLocal("itOut",i.getLocal("pOut")),i.setLocal("i",i.i32_const(0)),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("i"),i.getLocal("n"))),i.if(i.call(e+"_isZero",i.getLocal("itAux")),[...i.call(e+"_zero",i.getLocal("itOut")),...i.call(e+"_zero",i.i32_add(i.getLocal("itOut"),i.i32_const(n)))],[...i.call(e+"_mul",i.getLocal("itAux"),i.i32_add(i.getLocal("itIn"),i.i32_const(n)),l),...i.call(e+"_square",i.getLocal("itAux"),i.getLocal("itAux")),...i.call(e+"_mul",i.getLocal("itAux"),i.getLocal("itIn"),i.getLocal("itOut")),...i.call(e+"_mul",i.getLocal("itAux"),l,i.i32_add(i.getLocal("itOut"),i.i32_const(n)))]),i.setLocal("itIn",i.i32_add(i.getLocal("itIn"),i.i32_const(3*n))),i.setLocal("itOut",i.i32_add(i.getLocal("itOut"),i.i32_const(2*n))),i.setLocal("itAux",i.i32_add(i.getLocal("itAux"),i.i32_const(n))),i.setLocal("i",i.i32_add(i.getLocal("i"),i.i32_const(1))),i.br(0))),i.i32_store(i.i32_const(0),i.getLocal("pAux")))}(),function(){const o=t.addFunction(a+"_normalize");o.addParam("p1","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),l=i.getLocal("p1"),c=i.i32_add(i.getLocal("p1"),i.i32_const(n)),s=i.i32_add(i.getLocal("p1"),i.i32_const(2*n)),r=i.getLocal("pr"),d=i.i32_add(i.getLocal("pr"),i.i32_const(n)),u=i.i32_add(i.getLocal("pr"),i.i32_const(2*n)),_=i.i32_const(t.alloc(n)),g=i.i32_const(t.alloc(n)),f=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("p1")),i.call(a+"_zero",i.getLocal("pr")),[...i.call(e+"_inverse",s,_),...i.call(e+"_square",_,g),...i.call(e+"_mul",_,g,f),...i.call(e+"_mul",l,g,r),...i.call(e+"_mul",c,f,d),...i.call(e+"_one",u)]))}(),function(){const e=t.addFunction(a+"__reverseBytes");e.addParam("pIn","i32"),e.addParam("n","i32"),e.addParam("pOut","i32"),e.addLocal("itOut","i32"),e.addLocal("itIn","i32");const o=e.getCodeBuilder();e.addCode(o.setLocal("itOut",o.i32_sub(o.i32_add(o.getLocal("pOut"),o.getLocal("n")),o.i32_const(1))),o.setLocal("itIn",o.getLocal("pIn")),o.block(o.loop(o.br_if(1,o.i32_lt_s(o.getLocal("itOut"),o.getLocal("pOut"))),o.i32_store8(o.getLocal("itOut"),o.i32_load8_u(o.getLocal("itIn"))),o.setLocal("itOut",o.i32_sub(o.getLocal("itOut"),o.i32_const(1))),o.setLocal("itIn",o.i32_add(o.getLocal("itIn"),o.i32_const(1))),o.br(0))))}(),function(){const e=t.addFunction(a+"_LEMtoU");e.addParam("pIn","i32"),e.addParam("pOut","i32");const o=e.getCodeBuilder(),i=t.alloc(2*n),l=o.i32_const(i),c=o.i32_const(i),s=o.i32_const(i+n);e.addCode(o.if(o.call(a+"_isZeroAffine",o.getLocal("pIn")),[...o.call(a+"_zeroAffine",o.getLocal("pOut")),...o.ret([])]),o.call(a+"_fromMontgomeryAffine",o.getLocal("pIn"),l),o.call(a+"__reverseBytes",c,o.i32_const(n),o.getLocal("pOut")),o.call(a+"__reverseBytes",s,o.i32_const(n),o.i32_add(o.getLocal("pOut"),o.i32_const(n))))}(),function(){const o=t.addFunction(a+"_LEMtoC");o.addParam("pIn","i32"),o.addParam("pOut","i32");const i=o.getCodeBuilder(),l=i.i32_const(t.alloc(n));o.addCode(i.if(i.call(a+"_isZero",i.getLocal("pIn")),[...i.call(e+"_zero",i.getLocal("pOut")),...i.i32_store8(i.getLocal("pOut"),i.i32_const(64)),...i.ret([])]),i.call(e+"_fromMontgomery",i.getLocal("pIn"),l),i.call(a+"__reverseBytes",l,i.i32_const(n),i.getLocal("pOut")),i.if(i.i32_eq(i.call(e+"_sign",i.i32_add(i.getLocal("pIn"),i.i32_const(n))),i.i32_const(-1)),i.i32_store8(i.getLocal("pOut"),i.i32_or(i.i32_load8_u(i.getLocal("pOut")),i.i32_const(128)))))}(),function(){const e=t.addFunction(a+"_UtoLEM");e.addParam("pIn","i32"),e.addParam("pOut","i32");const o=e.getCodeBuilder(),i=t.alloc(2*n),l=o.i32_const(i),c=o.i32_const(i),s=o.i32_const(i+n);e.addCode(o.if(o.i32_and(o.i32_load8_u(o.getLocal("pIn")),o.i32_const(64)),[...o.call(a+"_zeroAffine",o.getLocal("pOut")),...o.ret([])]),o.call(a+"__reverseBytes",o.getLocal("pIn"),o.i32_const(n),c),o.call(a+"__reverseBytes",o.i32_add(o.getLocal("pIn"),o.i32_const(n)),o.i32_const(n),s),o.call(a+"_toMontgomeryAffine",l,o.getLocal("pOut")))}(),function(){const i=t.addFunction(a+"_CtoLEM");i.addParam("pIn","i32"),i.addParam("pOut","i32"),i.addLocal("firstByte","i32"),i.addLocal("greatest","i32");const l=i.getCodeBuilder(),c=t.alloc(2*n),s=l.i32_const(c),r=l.i32_const(c+n);i.addCode(l.setLocal("firstByte",l.i32_load8_u(l.getLocal("pIn"))),l.if(l.i32_and(l.getLocal("firstByte"),l.i32_const(64)),[...l.call(a+"_zeroAffine",l.getLocal("pOut")),...l.ret([])]),l.setLocal("greatest",l.i32_and(l.getLocal("firstByte"),l.i32_const(128))),l.call(e+"_copy",l.getLocal("pIn"),r),l.i32_store8(r,l.i32_and(l.getLocal("firstByte"),l.i32_const(63))),l.call(a+"__reverseBytes",r,l.i32_const(n),s),l.call(e+"_toMontgomery",s,l.getLocal("pOut")),l.call(e+"_square",l.getLocal("pOut"),r),l.call(e+"_mul",l.getLocal("pOut"),r,r),l.call(e+"_add",r,l.i32_const(o),r),l.call(e+"_sqrt",r,r),l.call(e+"_neg",r,s),l.if(l.i32_eq(l.call(e+"_sign",r),l.i32_const(-1)),l.if(l.getLocal("greatest"),l.call(e+"_copy",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n))),l.call(e+"_neg",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n)))),l.if(l.getLocal("greatest"),l.call(e+"_neg",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n))),l.call(e+"_copy",r,l.i32_add(l.getLocal("pOut"),l.i32_const(n))))))}(),Ii(t,a+"_batchLEMtoU",a+"_LEMtoU",2*n,2*n),Ii(t,a+"_batchLEMtoC",a+"_LEMtoC",2*n,n),Ii(t,a+"_batchUtoLEM",a+"_UtoLEM",2*n,2*n),Ii(t,a+"_batchCtoLEM",a+"_CtoLEM",n,2*n,!0),Ii(t,a+"_batchToJacobian",a+"_toJacobian",2*n,3*n,!0),Bi(t,a,a+"_multiexp",a+"_add",3*n),Bi(t,a,a+"_multiexpAffine",a+"_addMixed",2*n),Fi(t,a+"_timesScalar",3*n,a+"_add",a+"_double",a+"_sub",a+"_copy",a+"_zero"),Fi(t,a+"_timesScalarAffine",2*n,a+"_addMixed",a+"_double",a+"_subMixed",a+"_copyAffine",a+"_zero"),t.exportFunction(a+"_isZero"),t.exportFunction(a+"_isZeroAffine"),t.exportFunction(a+"_eq"),t.exportFunction(a+"_eqMixed"),t.exportFunction(a+"_eqAffine"),t.exportFunction(a+"_copy"),t.exportFunction(a+"_copyAffine"),t.exportFunction(a+"_zero"),t.exportFunction(a+"_zeroAffine"),t.exportFunction(a+"_double"),t.exportFunction(a+"_doubleAffine"),t.exportFunction(a+"_add"),t.exportFunction(a+"_addMixed"),t.exportFunction(a+"_addAffine"),t.exportFunction(a+"_neg"),t.exportFunction(a+"_negAffine"),t.exportFunction(a+"_sub"),t.exportFunction(a+"_subMixed"),t.exportFunction(a+"_subAffine"),t.exportFunction(a+"_fromMontgomery"),t.exportFunction(a+"_fromMontgomeryAffine"),t.exportFunction(a+"_toMontgomery"),t.exportFunction(a+"_toMontgomeryAffine"),t.exportFunction(a+"_timesScalar"),t.exportFunction(a+"_timesScalarAffine"),t.exportFunction(a+"_normalize"),t.exportFunction(a+"_LEMtoU"),t.exportFunction(a+"_LEMtoC"),t.exportFunction(a+"_UtoLEM"),t.exportFunction(a+"_CtoLEM"),t.exportFunction(a+"_batchLEMtoU"),t.exportFunction(a+"_batchLEMtoC"),t.exportFunction(a+"_batchUtoLEM"),t.exportFunction(a+"_batchCtoLEM"),t.exportFunction(a+"_toAffine"),t.exportFunction(a+"_toJacobian"),t.exportFunction(a+"_batchToAffine"),t.exportFunction(a+"_batchToJacobian"),t.exportFunction(a+"_inCurve"),t.exportFunction(a+"_inCurveAffine"),a};const{isOdd:vi,modInv:Si,modPow:Pi}=No,Oi=Mo;var qi=function(t,a,e,o,i){const n=8*t.modules[o].n64,l=8*t.modules[e].n64,c=t.modules[o].q;let s=c-1n,r=0;for(;!vi(s);)r++,s>>=1n;let d=2n;for(;1n===Pi(d,c>>1n,c);)d+=1n;const u=new Array(r+1);u[r]=Pi(d,s,c);let _=r-1;for(;_>=0;)u[_]=Pi(u[_+1],2n,c),_--;const g=[],f=(1n<>e);return a}const F=Array(256);for(let t=0;t<256;t++)F[t]=x(t);const I=t.alloc(F);function B(){const e=t.addFunction(a+"_fft");e.addParam("px","i32"),e.addParam("n","i32"),e.addLocal("bits","i32");const i=e.getCodeBuilder(),l=i.i32_const(t.alloc(n));e.addCode(i.setLocal("bits",i.call(a+"__log2",i.getLocal("n"))),i.call(o+"_one",l),i.call(a+"_rawfft",i.getLocal("px"),i.getLocal("bits"),i.i32_const(0),l))}!function(){const e=t.addFunction(a+"__rev");e.addParam("x","i32"),e.addParam("bits","i32"),e.setReturnType("i32");const o=e.getCodeBuilder();e.addCode(o.i32_rotl(o.i32_add(o.i32_add(o.i32_shl(o.i32_load8_u(o.i32_and(o.getLocal("x"),o.i32_const(255)),I,0),o.i32_const(24)),o.i32_shl(o.i32_load8_u(o.i32_and(o.i32_shr_u(o.getLocal("x"),o.i32_const(8)),o.i32_const(255)),I,0),o.i32_const(16))),o.i32_add(o.i32_shl(o.i32_load8_u(o.i32_and(o.i32_shr_u(o.getLocal("x"),o.i32_const(16)),o.i32_const(255)),I,0),o.i32_const(8)),o.i32_load8_u(o.i32_and(o.i32_shr_u(o.getLocal("x"),o.i32_const(24)),o.i32_const(255)),I,0))),o.getLocal("bits")))}(),function(){const o=t.addFunction(a+"__reversePermutation");o.addParam("px","i32"),o.addParam("bits","i32"),o.addLocal("n","i32"),o.addLocal("i","i32"),o.addLocal("ri","i32"),o.addLocal("idx1","i32"),o.addLocal("idx2","i32");const i=o.getCodeBuilder(),n=i.i32_const(t.alloc(l));o.addCode(i.setLocal("n",i.i32_shl(i.i32_const(1),i.getLocal("bits"))),i.setLocal("i",i.i32_const(0)),i.block(i.loop(i.br_if(1,i.i32_eq(i.getLocal("i"),i.getLocal("n"))),i.setLocal("idx1",i.i32_add(i.getLocal("px"),i.i32_mul(i.getLocal("i"),i.i32_const(l)))),i.setLocal("ri",i.call(a+"__rev",i.getLocal("i"),i.getLocal("bits"))),i.setLocal("idx2",i.i32_add(i.getLocal("px"),i.i32_mul(i.getLocal("ri"),i.i32_const(l)))),i.if(i.i32_lt_u(i.getLocal("i"),i.getLocal("ri")),[...i.call(e+"_copy",i.getLocal("idx1"),n),...i.call(e+"_copy",i.getLocal("idx2"),i.getLocal("idx1")),...i.call(e+"_copy",n,i.getLocal("idx2"))]),i.setLocal("i",i.i32_add(i.getLocal("i"),i.i32_const(1))),i.br(0))))}(),function(){const n=t.addFunction(a+"__fftFinal");n.addParam("px","i32"),n.addParam("bits","i32"),n.addParam("reverse","i32"),n.addParam("mulFactor","i32"),n.addLocal("n","i32"),n.addLocal("ndiv2","i32"),n.addLocal("pInv2","i32"),n.addLocal("i","i32"),n.addLocal("mask","i32"),n.addLocal("idx1","i32"),n.addLocal("idx2","i32");const c=n.getCodeBuilder(),s=c.i32_const(t.alloc(l));n.addCode(c.if(c.i32_and(c.i32_eqz(c.getLocal("reverse")),c.call(o+"_isOne",c.getLocal("mulFactor"))),c.ret([])),c.setLocal("n",c.i32_shl(c.i32_const(1),c.getLocal("bits"))),c.setLocal("mask",c.i32_sub(c.getLocal("n"),c.i32_const(1))),c.setLocal("i",c.i32_const(1)),c.setLocal("ndiv2",c.i32_shr_u(c.getLocal("n"),c.i32_const(1))),c.block(c.loop(c.br_if(1,c.i32_ge_u(c.getLocal("i"),c.getLocal("ndiv2"))),c.setLocal("idx1",c.i32_add(c.getLocal("px"),c.i32_mul(c.getLocal("i"),c.i32_const(l)))),c.setLocal("idx2",c.i32_add(c.getLocal("px"),c.i32_mul(c.i32_sub(c.getLocal("n"),c.getLocal("i")),c.i32_const(l)))),c.if(c.getLocal("reverse"),c.if(c.call(o+"_isOne",c.getLocal("mulFactor")),[...c.call(e+"_copy",c.getLocal("idx1"),s),...c.call(e+"_copy",c.getLocal("idx2"),c.getLocal("idx1")),...c.call(e+"_copy",s,c.getLocal("idx2"))],[...c.call(e+"_copy",c.getLocal("idx1"),s),...c.call(i,c.getLocal("idx2"),c.getLocal("mulFactor"),c.getLocal("idx1")),...c.call(i,s,c.getLocal("mulFactor"),c.getLocal("idx2"))]),c.if(c.call(o+"_isOne",c.getLocal("mulFactor")),[],[...c.call(i,c.getLocal("idx1"),c.getLocal("mulFactor"),c.getLocal("idx1")),...c.call(i,c.getLocal("idx2"),c.getLocal("mulFactor"),c.getLocal("idx2"))])),c.setLocal("i",c.i32_add(c.getLocal("i"),c.i32_const(1))),c.br(0))),c.if(c.call(o+"_isOne",c.getLocal("mulFactor")),[],[...c.call(i,c.getLocal("px"),c.getLocal("mulFactor"),c.getLocal("px")),...c.setLocal("idx2",c.i32_add(c.getLocal("px"),c.i32_mul(c.getLocal("ndiv2"),c.i32_const(l)))),...c.call(i,c.getLocal("idx2"),c.getLocal("mulFactor"),c.getLocal("idx2"))]))}(),function(){const c=t.addFunction(a+"_rawfft");c.addParam("px","i32"),c.addParam("bits","i32"),c.addParam("reverse","i32"),c.addParam("mulFactor","i32"),c.addLocal("s","i32"),c.addLocal("k","i32"),c.addLocal("j","i32"),c.addLocal("m","i32"),c.addLocal("mdiv2","i32"),c.addLocal("n","i32"),c.addLocal("pwm","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));c.addCode(s.call(a+"__reversePermutation",s.getLocal("px"),s.getLocal("bits")),s.setLocal("n",s.i32_shl(s.i32_const(1),s.getLocal("bits"))),s.setLocal("s",s.i32_const(1)),s.block(s.loop(s.br_if(1,s.i32_gt_u(s.getLocal("s"),s.getLocal("bits"))),s.setLocal("m",s.i32_shl(s.i32_const(1),s.getLocal("s"))),s.setLocal("pwm",s.i32_add(s.i32_const(p),s.i32_mul(s.getLocal("s"),s.i32_const(n)))),s.setLocal("k",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_ge_u(s.getLocal("k"),s.getLocal("n"))),s.call(o+"_one",r),s.setLocal("mdiv2",s.i32_shr_u(s.getLocal("m"),s.i32_const(1))),s.setLocal("j",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_ge_u(s.getLocal("j"),s.getLocal("mdiv2"))),s.setLocal("idx1",s.i32_add(s.getLocal("px"),s.i32_mul(s.i32_add(s.getLocal("k"),s.getLocal("j")),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("idx1"),s.i32_mul(s.getLocal("mdiv2"),s.i32_const(l)))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_copy",s.getLocal("idx1"),u),s.call(e+"_add",u,d,s.getLocal("idx1")),s.call(e+"_sub",u,d,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("pwm"),r),s.setLocal("j",s.i32_add(s.getLocal("j"),s.i32_const(1))),s.br(0))),s.setLocal("k",s.i32_add(s.getLocal("k"),s.getLocal("m"))),s.br(0))),s.setLocal("s",s.i32_add(s.getLocal("s"),s.i32_const(1))),s.br(0))),s.call(a+"__fftFinal",s.getLocal("px"),s.getLocal("bits"),s.getLocal("reverse"),s.getLocal("mulFactor")))}(),function(){const e=t.addFunction(a+"__log2");e.addParam("n","i32"),e.setReturnType("i32"),e.addLocal("bits","i32"),e.addLocal("aux","i32");const o=e.getCodeBuilder();e.addCode(o.setLocal("aux",o.i32_shr_u(o.getLocal("n"),o.i32_const(1)))),e.addCode(o.setLocal("bits",o.i32_const(0))),e.addCode(o.block(o.loop(o.br_if(1,o.i32_eqz(o.getLocal("aux"))),o.setLocal("aux",o.i32_shr_u(o.getLocal("aux"),o.i32_const(1))),o.setLocal("bits",o.i32_add(o.getLocal("bits"),o.i32_const(1))),o.br(0)))),e.addCode(o.if(o.i32_ne(o.getLocal("n"),o.i32_shl(o.i32_const(1),o.getLocal("bits"))),o.unreachable())),e.addCode(o.if(o.i32_gt_u(o.getLocal("bits"),o.i32_const(r)),o.unreachable())),e.addCode(o.getLocal("bits"))}(),B(),function(){const e=t.addFunction(a+"_ifft");e.addParam("px","i32"),e.addParam("n","i32"),e.addLocal("bits","i32"),e.addLocal("pInv2","i32");const o=e.getCodeBuilder();e.addCode(o.setLocal("bits",o.call(a+"__log2",o.getLocal("n"))),o.setLocal("pInv2",o.i32_add(o.i32_const(L),o.i32_mul(o.getLocal("bits"),o.i32_const(n)))),o.call(a+"_rawfft",o.getLocal("px"),o.getLocal("bits"),o.i32_const(1),o.getLocal("pInv2")))}(),function(){const c=t.addFunction(a+"_fftJoin");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));c.addCode(s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_copy",s.getLocal("idx1"),u),s.call(e+"_add",u,d,s.getLocal("idx1")),s.call(e+"_sub",u,d,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const c=t.addFunction(a+"_fftJoinExt");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addParam("totalBits","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("pShiftToM","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));c.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(A),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(e+"_add",s.getLocal("idx1"),s.getLocal("idx2"),d),s.call(i,s.getLocal("idx2"),s.getLocal("pShiftToM"),s.getLocal("idx2")),s.call(e+"_add",s.getLocal("idx1"),s.getLocal("idx2"),s.getLocal("idx2")),s.call(i,s.getLocal("idx2"),r,s.getLocal("idx2")),s.call(e+"_copy",d,s.getLocal("idx1")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const c=t.addFunction(a+"_fftJoinExtInv");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addParam("totalBits","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("pShiftToM","i32"),c.addLocal("pSConst","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));c.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(A),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.setLocal("pSConst",s.i32_add(s.i32_const(C),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_sub",s.getLocal("idx1"),d,s.getLocal("idx2")),s.call(i,s.getLocal("idx2"),s.getLocal("pSConst"),s.getLocal("idx2")),s.call(i,s.getLocal("idx1"),s.getLocal("pShiftToM"),s.getLocal("idx1")),s.call(e+"_sub",d,s.getLocal("idx1"),s.getLocal("idx1")),s.call(i,s.getLocal("idx1"),s.getLocal("pSConst"),s.getLocal("idx1")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const c=t.addFunction(a+"_fftMix");c.addParam("pBuff","i32"),c.addParam("n","i32"),c.addParam("exp","i32"),c.addLocal("nGroups","i32"),c.addLocal("nPerGroup","i32"),c.addLocal("nPerGroupDiv2","i32"),c.addLocal("pairOffset","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("j","i32"),c.addLocal("pwm","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l)),u=s.i32_const(t.alloc(l));c.addCode(s.setLocal("nPerGroup",s.i32_shl(s.i32_const(1),s.getLocal("exp"))),s.setLocal("nPerGroupDiv2",s.i32_shr_u(s.getLocal("nPerGroup"),s.i32_const(1))),s.setLocal("nGroups",s.i32_shr_u(s.getLocal("n"),s.getLocal("exp"))),s.setLocal("pairOffset",s.i32_mul(s.getLocal("nPerGroupDiv2"),s.i32_const(l))),s.setLocal("pwm",s.i32_add(s.i32_const(p),s.i32_mul(s.getLocal("exp"),s.i32_const(n)))),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("nGroups"))),s.call(o+"_one",r),s.setLocal("j",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("j"),s.getLocal("nPerGroupDiv2"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff"),s.i32_mul(s.i32_add(s.i32_mul(s.getLocal("i"),s.getLocal("nPerGroup")),s.getLocal("j")),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("idx1"),s.getLocal("pairOffset"))),s.call(i,s.getLocal("idx2"),r,d),s.call(e+"_copy",s.getLocal("idx1"),u),s.call(e+"_add",u,d,s.getLocal("idx1")),s.call(e+"_sub",u,d,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("pwm"),r),s.setLocal("j",s.i32_add(s.getLocal("j"),s.i32_const(1))),s.br(0))),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),function(){const o=t.addFunction(a+"_fftFinal");o.addParam("pBuff","i32"),o.addParam("n","i32"),o.addParam("factor","i32"),o.addLocal("idx1","i32"),o.addLocal("idx2","i32"),o.addLocal("i","i32"),o.addLocal("ndiv2","i32");const n=o.getCodeBuilder(),c=n.i32_const(t.alloc(l));o.addCode(n.setLocal("ndiv2",n.i32_shr_u(n.getLocal("n"),n.i32_const(1))),n.if(n.i32_and(n.getLocal("n"),n.i32_const(1)),n.call(i,n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("ndiv2"),n.i32_const(l))),n.getLocal("factor"),n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("ndiv2"),n.i32_const(l))))),n.setLocal("i",n.i32_const(0)),n.block(n.loop(n.br_if(1,n.i32_ge_u(n.getLocal("i"),n.getLocal("ndiv2"))),n.setLocal("idx1",n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.getLocal("i"),n.i32_const(l)))),n.setLocal("idx2",n.i32_add(n.getLocal("pBuff"),n.i32_mul(n.i32_sub(n.i32_sub(n.getLocal("n"),n.i32_const(1)),n.getLocal("i")),n.i32_const(l)))),n.call(i,n.getLocal("idx2"),n.getLocal("factor"),c),n.call(i,n.getLocal("idx1"),n.getLocal("factor"),n.getLocal("idx2")),n.call(e+"_copy",c,n.getLocal("idx1")),n.setLocal("i",n.i32_add(n.getLocal("i"),n.i32_const(1))),n.br(0))))}(),function(){const c=t.addFunction(a+"_prepareLagrangeEvaluation");c.addParam("pBuff1","i32"),c.addParam("pBuff2","i32"),c.addParam("n","i32"),c.addParam("first","i32"),c.addParam("inc","i32"),c.addParam("totalBits","i32"),c.addLocal("idx1","i32"),c.addLocal("idx2","i32"),c.addLocal("i","i32"),c.addLocal("pShiftToM","i32"),c.addLocal("pSConst","i32");const s=c.getCodeBuilder(),r=s.i32_const(t.alloc(n)),d=s.i32_const(t.alloc(l));c.addCode(s.setLocal("pShiftToM",s.i32_add(s.i32_const(A),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.setLocal("pSConst",s.i32_add(s.i32_const(C),s.i32_mul(s.getLocal("totalBits"),s.i32_const(n)))),s.call(o+"_copy",s.getLocal("first"),r),s.setLocal("i",s.i32_const(0)),s.block(s.loop(s.br_if(1,s.i32_eq(s.getLocal("i"),s.getLocal("n"))),s.setLocal("idx1",s.i32_add(s.getLocal("pBuff1"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.setLocal("idx2",s.i32_add(s.getLocal("pBuff2"),s.i32_mul(s.getLocal("i"),s.i32_const(l)))),s.call(i,s.getLocal("idx1"),s.getLocal("pShiftToM"),d),s.call(e+"_sub",s.getLocal("idx2"),d,d),s.call(e+"_sub",s.getLocal("idx1"),s.getLocal("idx2"),s.getLocal("idx2")),s.call(i,d,s.getLocal("pSConst"),s.getLocal("idx1")),s.call(i,s.getLocal("idx2"),r,s.getLocal("idx2")),s.call(o+"_mul",r,s.getLocal("inc"),r),s.setLocal("i",s.i32_add(s.getLocal("i"),s.i32_const(1))),s.br(0))))}(),t.exportFunction(a+"_fft"),t.exportFunction(a+"_ifft"),t.exportFunction(a+"_rawfft"),t.exportFunction(a+"_fftJoin"),t.exportFunction(a+"_fftJoinExt"),t.exportFunction(a+"_fftJoinExtInv"),t.exportFunction(a+"_fftMix"),t.exportFunction(a+"_fftFinal"),t.exportFunction(a+"_prepareLagrangeEvaluation")},Gi=function(t,a,e){const o=8*t.modules[e].n64;return function(){const i=t.addFunction(a+"_zero");i.addParam("px","i32"),i.addParam("n","i32"),i.addLocal("lastp","i32"),i.addLocal("p","i32");const n=i.getCodeBuilder();i.addCode(n.setLocal("p",n.getLocal("px")),n.setLocal("lastp",n.i32_add(n.getLocal("px"),n.i32_mul(n.getLocal("n"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("p"),n.getLocal("lastp"))),n.call(e+"_zero",n.getLocal("p")),n.setLocal("p",n.i32_add(n.getLocal("p"),n.i32_const(o))),n.br(0))))}(),function(){const i=t.addFunction(a+"_constructLC");i.addParam("ppolynomials","i32"),i.addParam("psignals","i32"),i.addParam("nSignals","i32"),i.addParam("pres","i32"),i.addLocal("i","i32"),i.addLocal("j","i32"),i.addLocal("pp","i32"),i.addLocal("ps","i32"),i.addLocal("pd","i32"),i.addLocal("ncoefs","i32");const n=i.getCodeBuilder(),l=n.i32_const(t.alloc(o));i.addCode(n.setLocal("i",n.i32_const(0)),n.setLocal("pp",n.getLocal("ppolynomials")),n.setLocal("ps",n.getLocal("psignals")),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("i"),n.getLocal("nSignals"))),n.setLocal("ncoefs",n.i32_load(n.getLocal("pp"))),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(4))),n.setLocal("j",n.i32_const(0)),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("j"),n.getLocal("ncoefs"))),n.setLocal("pd",n.i32_add(n.getLocal("pres"),n.i32_mul(n.i32_load(n.getLocal("pp")),n.i32_const(o)))),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(4))),n.call(e+"_mul",n.getLocal("ps"),n.getLocal("pp"),l),n.call(e+"_add",l,n.getLocal("pd"),n.getLocal("pd")),n.setLocal("pp",n.i32_add(n.getLocal("pp"),n.i32_const(o))),n.setLocal("j",n.i32_add(n.getLocal("j"),n.i32_const(1))),n.br(0))),n.setLocal("ps",n.i32_add(n.getLocal("ps"),n.i32_const(o))),n.setLocal("i",n.i32_add(n.getLocal("i"),n.i32_const(1))),n.br(0))))}(),t.exportFunction(a+"_zero"),t.exportFunction(a+"_constructLC"),a},zi=function(t,a,e){const o=8*t.modules[e].n64;return function(){const i=t.addFunction(a+"_buildABC");i.addParam("pCoefs","i32"),i.addParam("nCoefs","i32"),i.addParam("pWitness","i32"),i.addParam("pA","i32"),i.addParam("pB","i32"),i.addParam("pC","i32"),i.addParam("offsetOut","i32"),i.addParam("nOut","i32"),i.addParam("offsetWitness","i32"),i.addParam("nWitness","i32"),i.addLocal("it","i32"),i.addLocal("ita","i32"),i.addLocal("itb","i32"),i.addLocal("last","i32"),i.addLocal("m","i32"),i.addLocal("c","i32"),i.addLocal("s","i32"),i.addLocal("pOut","i32");const n=i.getCodeBuilder(),l=n.i32_const(t.alloc(o));i.addCode(n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("nOut"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_zero",n.getLocal("ita")),n.call(e+"_zero",n.getLocal("itb")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.br(0))),n.setLocal("it",n.getLocal("pCoefs")),n.setLocal("last",n.i32_add(n.getLocal("pCoefs"),n.i32_mul(n.getLocal("nCoefs"),n.i32_const(o+12)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("it"),n.getLocal("last"))),n.setLocal("s",n.i32_load(n.getLocal("it"),8)),n.if(n.i32_or(n.i32_lt_u(n.getLocal("s"),n.getLocal("offsetWitness")),n.i32_ge_u(n.getLocal("s"),n.i32_add(n.getLocal("offsetWitness"),n.getLocal("nWitness")))),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),...n.br(1)]),n.setLocal("m",n.i32_load(n.getLocal("it"))),n.if(n.i32_eq(n.getLocal("m"),n.i32_const(0)),n.setLocal("pOut",n.getLocal("pA")),n.if(n.i32_eq(n.getLocal("m"),n.i32_const(1)),n.setLocal("pOut",n.getLocal("pB")),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),...n.br(1)])),n.setLocal("c",n.i32_load(n.getLocal("it"),4)),n.if(n.i32_or(n.i32_lt_u(n.getLocal("c"),n.getLocal("offsetOut")),n.i32_ge_u(n.getLocal("c"),n.i32_add(n.getLocal("offsetOut"),n.getLocal("nOut")))),[...n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),...n.br(1)]),n.setLocal("pOut",n.i32_add(n.getLocal("pOut"),n.i32_mul(n.i32_sub(n.getLocal("c"),n.getLocal("offsetOut")),n.i32_const(o)))),n.call(e+"_mul",n.i32_add(n.getLocal("pWitness"),n.i32_mul(n.i32_sub(n.getLocal("s"),n.getLocal("offsetWitness")),n.i32_const(o))),n.i32_add(n.getLocal("it"),n.i32_const(12)),l),n.call(e+"_add",n.getLocal("pOut"),l,n.getLocal("pOut")),n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o+12))),n.br(0))),n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("it",n.getLocal("pC")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("nOut"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_mul",n.getLocal("ita"),n.getLocal("itb"),n.getLocal("it")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.setLocal("it",n.i32_add(n.getLocal("it"),n.i32_const(o))),n.br(0))))}(),function(){const i=t.addFunction(a+"_joinABC");i.addParam("pA","i32"),i.addParam("pB","i32"),i.addParam("pC","i32"),i.addParam("n","i32"),i.addParam("pP","i32"),i.addLocal("ita","i32"),i.addLocal("itb","i32"),i.addLocal("itc","i32"),i.addLocal("itp","i32"),i.addLocal("last","i32");const n=i.getCodeBuilder(),l=n.i32_const(t.alloc(o));i.addCode(n.setLocal("ita",n.getLocal("pA")),n.setLocal("itb",n.getLocal("pB")),n.setLocal("itc",n.getLocal("pC")),n.setLocal("itp",n.getLocal("pP")),n.setLocal("last",n.i32_add(n.getLocal("pA"),n.i32_mul(n.getLocal("n"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_mul",n.getLocal("ita"),n.getLocal("itb"),l),n.call(e+"_sub",l,n.getLocal("itc"),n.getLocal("itp")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.setLocal("itc",n.i32_add(n.getLocal("itc"),n.i32_const(o))),n.setLocal("itp",n.i32_add(n.getLocal("itp"),n.i32_const(o))),n.br(0))))}(),function(){const i=t.addFunction(a+"_batchAdd");i.addParam("pa","i32"),i.addParam("pb","i32"),i.addParam("n","i32"),i.addParam("pr","i32"),i.addLocal("ita","i32"),i.addLocal("itb","i32"),i.addLocal("itr","i32"),i.addLocal("last","i32");const n=i.getCodeBuilder();i.addCode(n.setLocal("ita",n.getLocal("pa")),n.setLocal("itb",n.getLocal("pb")),n.setLocal("itr",n.getLocal("pr")),n.setLocal("last",n.i32_add(n.getLocal("pa"),n.i32_mul(n.getLocal("n"),n.i32_const(o)))),n.block(n.loop(n.br_if(1,n.i32_eq(n.getLocal("ita"),n.getLocal("last"))),n.call(e+"_add",n.getLocal("ita"),n.getLocal("itb"),n.getLocal("itr")),n.setLocal("ita",n.i32_add(n.getLocal("ita"),n.i32_const(o))),n.setLocal("itb",n.i32_add(n.getLocal("itb"),n.i32_const(o))),n.setLocal("itr",n.i32_add(n.getLocal("itr"),n.i32_const(o))),n.br(0))))}(),t.exportFunction(a+"_buildABC"),t.exportFunction(a+"_joinABC"),t.exportFunction(a+"_batchAdd"),a},Ti=function(t,a,e,o,i,n,l,c){const s=t.addFunction(a);s.addParam("pIn","i32"),s.addParam("n","i32"),s.addParam("pFirst","i32"),s.addParam("pInc","i32"),s.addParam("pOut","i32"),s.addLocal("pOldFree","i32"),s.addLocal("i","i32"),s.addLocal("pFrom","i32"),s.addLocal("pTo","i32");const r=s.getCodeBuilder(),d=r.i32_const(t.alloc(l));s.addCode(r.setLocal("pFrom",r.getLocal("pIn")),r.setLocal("pTo",r.getLocal("pOut"))),s.addCode(r.call(o+"_copy",r.getLocal("pFirst"),d)),s.addCode(r.setLocal("i",r.i32_const(0)),r.block(r.loop(r.br_if(1,r.i32_eq(r.getLocal("i"),r.getLocal("n"))),r.call(c,r.getLocal("pFrom"),d,r.getLocal("pTo")),r.setLocal("pFrom",r.i32_add(r.getLocal("pFrom"),r.i32_const(i))),r.setLocal("pTo",r.i32_add(r.getLocal("pTo"),r.i32_const(n))),r.call(o+"_mul",d,r.getLocal("pInc"),d),r.setLocal("i",r.i32_add(r.getLocal("i"),r.i32_const(1))),r.br(0)))),t.exportFunction(a)};const Mi=Mo,Ui=fi,Qi=mi,ki=yi,Ri=xi,Ni=Ei,Di=qi,$i=Gi,ji=zi,Vi=Ti,{bitLength:Ki,modInv:Hi,isOdd:Zi,isNegative:Wi}=No;const Yi=Mo,Ji=fi,Xi=mi,tn=yi,an=xi,en=Ei,on=qi,nn=Gi,ln=zi,cn=Ti,{bitLength:sn,isOdd:rn,isNegative:dn}=No;var un=function(t,a){const e=a||"bn128";if(t.modules[e])return e;const o=21888242871839275222246405745257275088696311157297823662689037894645226208583n,i=21888242871839275222246405745257275088548364400416034343698204186575808495617n,n=Math.floor((Ki(o-1n)-1)/64)+1,l=8*n,c=l,s=l,r=2*s,d=12*s,u=t.alloc(Mi.bigInt2BytesLE(i,c)),_=Ui(t,o,"f1m");Qi(t,i,"fr","frm");const g=t.alloc(Mi.bigInt2BytesLE(b(3n),s)),f=Ni(t,"g1m","f1m",g);Di(t,"frm","frm","frm","frm_mul"),$i(t,"pol","frm"),ji(t,"qap","frm");const p=ki(t,"f1m_neg","f2m","f1m"),h=t.alloc([...Mi.bigInt2BytesLE(b(19485874751759354771024239261021720505790618469301721065564631296452457478373n),s),...Mi.bigInt2BytesLE(b(266929791119991161246907387137283842545076965332900288569378510910307636690n),s)]),m=Ni(t,"g2m","f2m",h);function L(a,e){const o=t.addFunction(a);o.addParam("pG","i32"),o.addParam("pFr","i32"),o.addParam("pr","i32");const i=o.getCodeBuilder(),n=i.i32_const(t.alloc(l));o.addCode(i.call("frm_fromMontgomery",i.getLocal("pFr"),n),i.call(e,i.getLocal("pG"),n,i.i32_const(l),i.getLocal("pr"))),t.exportFunction(a)}function b(t){return BigInt(t)*(1n<0n;)Zi(a)?e.push(1):e.push(0),a>>=1n;return e}(29793968203157093288n),T=t.alloc(z),M=3*r,U=z.length-1,Q=z.reduce(((t,a)=>t+(0!=a?1:0)),0),k=6*l,R=3*l*2+(Q+U+1)*M;t.modules[e]={n64:n,pG1gen:y,pG1zero:C,pG1b:g,pG2gen:F,pG2zero:B,pG2b:h,pq:t.modules.f1m.pq,pr:u,pOneT:E,prePSize:k,preQSize:R,r:i.toString(),q:o.toString()};const N=4965661367192848881n;function D(a){const i=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[8376118865763821496583973867626364092589906065868298776909617916018768340080n,16469823323077808223889137241176536799009286646108169935659301613961712198316n],[21888242871839275220042445260109153167277707414472061641714758635765020556617n,0n],[11697423496358154304825782922584725312912383441159505038794027105778954184319n,303847389135065887422783454877609941456349188919719272345083954437860409601n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[3321304630594332808241809054958361220322477375291206261884409189760185844239n,5722266937896532885780051958958348231143373700109372999374820235121374419868n],[21888242871839275222246405745257275088696311157297823662689037894645226208582n,0n],[13512124006075453725662431877630910996106405091429524885779419978626457868503n,5418419548761466998357268504080738289687024511189653727029736280683514010267n],[2203960485148121921418603742825762020974279258880205651966n,0n],[10190819375481120917420622822672549775783927716138318623895010788866272024264n,21584395482704209334823622290379665147239961968378104390343953940207365798982n],[2203960485148121921418603742825762020974279258880205651967n,0n],[18566938241244942414004596690298913868373833782006617400804628704885040364344n,16165975933942742336466353786298926857552937457188450663314217659523851788715n]]],n=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[21575463638280843010398324269430826099269044274347216827212613867836435027261n,10307601595873709700152284273816112264069230130616436755625194854815875713954n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[3772000881919853776433695186713858239009073593817195771773381919316419345261n,2236595495967245188281701248203181795121068902605861227855261137820944008926n],[2203960485148121921418603742825762020974279258880205651966n,0n],[18429021223477853657660792034369865839114504446431234726392080002137598044644n,9344045779998320333812420223237981029506012124075525679208581902008406485703n]],[[1n,0n],[2581911344467009335267311115468803099551665605076196740867805258568234346338n,19937756971775647987995932169929341994314640652964949448313374472400716661030n],[2203960485148121921418603742825762020974279258880205651966n,0n],[5324479202449903542726783395506214481928257762400643279780343368557297135718n,16208900380737693084919495127334387981393726419856888799917914180988844123039n],[21888242871839275220042445260109153167277707414472061641714758635765020556616n,0n],[13981852324922362344252311234282257507216387789820983642040889267519694726527n,7629828391165209371577384193250820201684255241773809077146787135900891633097n]]],l=t.addFunction(e+"__frobeniusMap"+a);l.addParam("x","i32"),l.addParam("r","i32");const c=l.getCodeBuilder();for(let e=0;e<6;e++){const o=0==e?c.getLocal("x"):c.i32_add(c.getLocal("x"),c.i32_const(e*r)),u=o,g=c.i32_add(c.getLocal("x"),c.i32_const(e*r+s)),f=0==e?c.getLocal("r"):c.i32_add(c.getLocal("r"),c.i32_const(e*r)),h=f,m=c.i32_add(c.getLocal("r"),c.i32_const(e*r+s)),L=d(i[Math.floor(e/3)][a%12],n[e%3][a%6]),w=t.alloc([...Mi.bigInt2BytesLE(b(L[0]),32),...Mi.bigInt2BytesLE(b(L[1]),32)]);a%2==1?l.addCode(c.call(_+"_copy",u,h),c.call(_+"_neg",g,m),c.call(p+"_mul",f,c.i32_const(w),f)):l.addCode(c.call(p+"_mul",o,c.i32_const(w),f))}function d(t,a){const e=BigInt(t[0]),i=BigInt(t[1]),n=BigInt(a[0]),l=BigInt(a[1]),c=[(e*n-i*l)%o,(e*l+i*n)%o];return Wi(c[0])&&(c[0]=c[0]+o),c}}function $(a,o){const i=function(t){let a=t;const e=[];for(;a>0n;){if(Zi(a)){const t=2-Number(a%4n);e.push(t),a-=BigInt(t)}else e.push(0);a>>=1n}return e}(a).map((t=>-1==t?255:t)),n=t.alloc(i),l=t.addFunction(e+"__cyclotomicExp_"+o);l.addParam("x","i32"),l.addParam("r","i32"),l.addLocal("bit","i32"),l.addLocal("i","i32");const c=l.getCodeBuilder(),s=c.getLocal("x"),r=c.getLocal("r"),u=c.i32_const(t.alloc(d));l.addCode(c.call(G+"_conjugate",s,u),c.call(G+"_one",r),c.if(c.teeLocal("bit",c.i32_load8_s(c.i32_const(i.length-1),n)),c.if(c.i32_eq(c.getLocal("bit"),c.i32_const(1)),c.call(G+"_mul",r,s,r),c.call(G+"_mul",r,u,r))),c.setLocal("i",c.i32_const(i.length-2)),c.block(c.loop(c.call(e+"__cyclotomicSquare",r,r),c.if(c.teeLocal("bit",c.i32_load8_s(c.getLocal("i"),n)),c.if(c.i32_eq(c.getLocal("bit"),c.i32_const(1)),c.call(G+"_mul",r,s,r),c.call(G+"_mul",r,u,r))),c.br_if(1,c.i32_eqz(c.getLocal("i"))),c.setLocal("i",c.i32_sub(c.getLocal("i"),c.i32_const(1))),c.br(0))))}function j(){!function(){const a=t.addFunction(e+"__cyclotomicSquare");a.addParam("x","i32"),a.addParam("r","i32");const o=a.getCodeBuilder(),i=o.getLocal("x"),n=o.i32_add(o.getLocal("x"),o.i32_const(r)),l=o.i32_add(o.getLocal("x"),o.i32_const(2*r)),c=o.i32_add(o.getLocal("x"),o.i32_const(3*r)),s=o.i32_add(o.getLocal("x"),o.i32_const(4*r)),d=o.i32_add(o.getLocal("x"),o.i32_const(5*r)),u=o.getLocal("r"),_=o.i32_add(o.getLocal("r"),o.i32_const(r)),g=o.i32_add(o.getLocal("r"),o.i32_const(2*r)),f=o.i32_add(o.getLocal("r"),o.i32_const(3*r)),h=o.i32_add(o.getLocal("r"),o.i32_const(4*r)),m=o.i32_add(o.getLocal("r"),o.i32_const(5*r)),L=o.i32_const(t.alloc(r)),b=o.i32_const(t.alloc(r)),w=o.i32_const(t.alloc(r)),y=o.i32_const(t.alloc(r)),A=o.i32_const(t.alloc(r)),C=o.i32_const(t.alloc(r)),x=o.i32_const(t.alloc(r)),F=o.i32_const(t.alloc(r));a.addCode(o.call(p+"_mul",i,s,x),o.call(p+"_mul",s,o.i32_const(v),L),o.call(p+"_add",i,L,L),o.call(p+"_add",i,s,F),o.call(p+"_mul",F,L,L),o.call(p+"_mul",o.i32_const(v),x,F),o.call(p+"_add",x,F,F),o.call(p+"_sub",L,F,L),o.call(p+"_add",x,x,b),o.call(p+"_mul",c,l,x),o.call(p+"_mul",l,o.i32_const(v),w),o.call(p+"_add",c,w,w),o.call(p+"_add",c,l,F),o.call(p+"_mul",F,w,w),o.call(p+"_mul",o.i32_const(v),x,F),o.call(p+"_add",x,F,F),o.call(p+"_sub",w,F,w),o.call(p+"_add",x,x,y),o.call(p+"_mul",n,d,x),o.call(p+"_mul",d,o.i32_const(v),A),o.call(p+"_add",n,A,A),o.call(p+"_add",n,d,F),o.call(p+"_mul",F,A,A),o.call(p+"_mul",o.i32_const(v),x,F),o.call(p+"_add",x,F,F),o.call(p+"_sub",A,F,A),o.call(p+"_add",x,x,C),o.call(p+"_sub",L,i,u),o.call(p+"_add",u,u,u),o.call(p+"_add",L,u,u),o.call(p+"_add",b,s,h),o.call(p+"_add",h,h,h),o.call(p+"_add",b,h,h),o.call(p+"_mul",C,o.i32_const(P),F),o.call(p+"_add",F,c,f),o.call(p+"_add",f,f,f),o.call(p+"_add",F,f,f),o.call(p+"_sub",A,l,g),o.call(p+"_add",g,g,g),o.call(p+"_add",A,g,g),o.call(p+"_sub",w,n,_),o.call(p+"_add",_,_,_),o.call(p+"_add",w,_,_),o.call(p+"_add",y,d,m),o.call(p+"_add",m,m,m),o.call(p+"_add",y,m,m))}(),$(N,"w0");const a=t.addFunction(e+"__finalExponentiationLastChunk");a.addParam("x","i32"),a.addParam("r","i32");const o=a.getCodeBuilder(),i=o.getLocal("x"),n=o.getLocal("r"),l=o.i32_const(t.alloc(d)),c=o.i32_const(t.alloc(d)),s=o.i32_const(t.alloc(d)),u=o.i32_const(t.alloc(d)),_=o.i32_const(t.alloc(d)),g=o.i32_const(t.alloc(d)),f=o.i32_const(t.alloc(d)),h=o.i32_const(t.alloc(d)),m=o.i32_const(t.alloc(d)),L=o.i32_const(t.alloc(d)),b=o.i32_const(t.alloc(d)),w=o.i32_const(t.alloc(d)),y=o.i32_const(t.alloc(d)),A=o.i32_const(t.alloc(d)),C=o.i32_const(t.alloc(d)),x=o.i32_const(t.alloc(d)),F=o.i32_const(t.alloc(d)),I=o.i32_const(t.alloc(d)),B=o.i32_const(t.alloc(d)),E=o.i32_const(t.alloc(d)),S=o.i32_const(t.alloc(d));a.addCode(o.call(e+"__cyclotomicExp_w0",i,l),o.call(G+"_conjugate",l,l),o.call(e+"__cyclotomicSquare",l,c),o.call(e+"__cyclotomicSquare",c,s),o.call(G+"_mul",s,c,u),o.call(e+"__cyclotomicExp_w0",u,_),o.call(G+"_conjugate",_,_),o.call(e+"__cyclotomicSquare",_,g),o.call(e+"__cyclotomicExp_w0",g,f),o.call(G+"_conjugate",f,f),o.call(G+"_conjugate",u,h),o.call(G+"_conjugate",f,m),o.call(G+"_mul",m,_,L),o.call(G+"_mul",L,h,b),o.call(G+"_mul",b,c,w),o.call(G+"_mul",b,_,y),o.call(G+"_mul",y,i,A),o.call(e+"__frobeniusMap1",w,C),o.call(G+"_mul",C,A,x),o.call(e+"__frobeniusMap2",b,F),o.call(G+"_mul",F,x,I),o.call(G+"_conjugate",i,B),o.call(G+"_mul",B,w,E),o.call(e+"__frobeniusMap3",E,S),o.call(G+"_mul",S,I,n))}const V=t.alloc(k),K=t.alloc(R);function H(a){const o=t.addFunction(e+"_pairingEq"+a);for(let t=0;t0n;)rn(a)?e.push(1):e.push(0),a>>=1n;return e}(0xd201000000010000n),z=t.alloc(G),T=3*s,M=G.length-1,U=G.reduce(((t,a)=>t+(0!=a?1:0)),0),Q=6*l,k=3*l*2+(U+M+1)*T,R=!0,N=15132376222941642752n;function D(a){const e=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n,151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n,0n],[2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n,1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n,877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n],[4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n,0n],[151655185184498381465642749684540099398075398968325446656007613510403227271200139370504932015952886146304766135027n,3850754370037169011952147076051364057158807420970682438676050522613628423219637725072182697113062777891589506424760n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257n,2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n,0n],[877076961050607968509681729531255177986764537961432449499635504522207616027455086505066378536590128544573588734230n,3125332594171059424908108096204648978570118281977575435832422631601824034463382777937621250592425535493320683825557n]]],i=[[[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n],[1n,0n]],[[1n,0n],[0n,4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[0n,1n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[0n,793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n]],[[1n,0n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437n,0n],[4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436n,0n],[4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786n,0n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350n,0n],[793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620351n,0n]]],n=t.addFunction(q+"_frobeniusMap"+a);n.addParam("x","i32"),n.addParam("r","i32");const r=n.getCodeBuilder();for(let o=0;o<6;o++){const u=0==o?r.getLocal("x"):r.i32_add(r.getLocal("x"),r.i32_const(o*s)),_=u,g=r.i32_add(r.getLocal("x"),r.i32_const(o*s+c)),p=0==o?r.getLocal("r"):r.i32_add(r.getLocal("r"),r.i32_const(o*s)),h=p,L=r.i32_add(r.getLocal("r"),r.i32_const(o*s+c)),b=d(e[Math.floor(o/3)][a%12],i[o%3][a%6]),w=t.alloc([...Yi.bigInt2BytesLE(y(b[0]),l),...Yi.bigInt2BytesLE(y(b[1]),l)]);a%2==1?n.addCode(r.call(f+"_copy",_,h),r.call(f+"_neg",g,L),r.call(m+"_mul",p,r.i32_const(w),p)):n.addCode(r.call(m+"_mul",u,r.i32_const(w),p))}function d(t,a){const e=t[0],i=t[1],n=a[0],l=a[1],c=[(e*n-i*l)%o,(e*l+i*n)%o];return dn(c[0])&&(c[0]=c[0]+o),c}}function $(a,o,i){const n=function(t){let a=t;const e=[];for(;a>0n;){if(rn(a)){const t=2-Number(a%4n);e.push(t),a-=BigInt(t)}else e.push(0);a>>=1n}return e}(a).map((t=>-1==t?255:t)),l=t.alloc(n),c=t.addFunction(e+"__cyclotomicExp_"+i);c.addParam("x","i32"),c.addParam("r","i32"),c.addLocal("bit","i32"),c.addLocal("i","i32");const s=c.getCodeBuilder(),d=s.getLocal("x"),u=s.getLocal("r"),_=s.i32_const(t.alloc(r));c.addCode(s.call(q+"_conjugate",d,_),s.call(q+"_one",u),s.if(s.teeLocal("bit",s.i32_load8_s(s.i32_const(n.length-1),l)),s.if(s.i32_eq(s.getLocal("bit"),s.i32_const(1)),s.call(q+"_mul",u,d,u),s.call(q+"_mul",u,_,u))),s.setLocal("i",s.i32_const(n.length-2)),s.block(s.loop(s.call(e+"__cyclotomicSquare",u,u),s.if(s.teeLocal("bit",s.i32_load8_s(s.getLocal("i"),l)),s.if(s.i32_eq(s.getLocal("bit"),s.i32_const(1)),s.call(q+"_mul",u,d,u),s.call(q+"_mul",u,_,u))),s.br_if(1,s.i32_eqz(s.getLocal("i"))),s.setLocal("i",s.i32_sub(s.getLocal("i"),s.i32_const(1))),s.br(0)))),o&&c.addCode(s.call(q+"_conjugate",u,u))}t.modules[e]={n64q:n,n64r:d,n8q:l,n8r:u,pG1gen:C,pG1zero:F,pG1b:p,pG2gen:B,pG2zero:v,pG2b:L,pq:t.modules.f1m.pq,pr:g,pOneT:S,r:i,q:o,prePSize:Q,preQSize:k},function(){const a=t.addFunction(O+"_mul1");a.addParam("pA","i32"),a.addParam("pC1","i32"),a.addParam("pR","i32");const e=a.getCodeBuilder(),o=e.getLocal("pA"),i=e.i32_add(e.getLocal("pA"),e.i32_const(2*c)),n=e.i32_add(e.getLocal("pA"),e.i32_const(4*c)),l=e.getLocal("pC1"),s=e.getLocal("pR"),r=e.i32_add(e.getLocal("pR"),e.i32_const(2*c)),d=e.i32_add(e.getLocal("pR"),e.i32_const(4*c)),u=e.i32_const(t.alloc(2*c)),_=e.i32_const(t.alloc(2*c));a.addCode(e.call(m+"_add",o,i,u),e.call(m+"_add",i,n,_),e.call(m+"_mul",i,l,d),e.call(m+"_mul",_,l,s),e.call(m+"_sub",s,d,s),e.call(m+"_mulNR",s,s),e.call(m+"_mul",u,l,r),e.call(m+"_sub",r,d,r))}(),function(){const a=t.addFunction(O+"_mul01");a.addParam("pA","i32"),a.addParam("pC0","i32"),a.addParam("pC1","i32"),a.addParam("pR","i32");const e=a.getCodeBuilder(),o=e.getLocal("pA"),i=e.i32_add(e.getLocal("pA"),e.i32_const(2*c)),n=e.i32_add(e.getLocal("pA"),e.i32_const(4*c)),l=e.getLocal("pC0"),s=e.getLocal("pC1"),r=e.getLocal("pR"),d=e.i32_add(e.getLocal("pR"),e.i32_const(2*c)),u=e.i32_add(e.getLocal("pR"),e.i32_const(4*c)),_=e.i32_const(t.alloc(2*c)),g=e.i32_const(t.alloc(2*c)),f=e.i32_const(t.alloc(2*c)),p=e.i32_const(t.alloc(2*c));a.addCode(e.call(m+"_mul",o,l,_),e.call(m+"_mul",i,s,g),e.call(m+"_add",o,i,f),e.call(m+"_add",o,n,p),e.call(m+"_add",i,n,r),e.call(m+"_mul",r,s,r),e.call(m+"_sub",r,g,r),e.call(m+"_mulNR",r,r),e.call(m+"_add",r,_,r),e.call(m+"_add",l,s,d),e.call(m+"_mul",d,f,d),e.call(m+"_sub",d,_,d),e.call(m+"_sub",d,g,d),e.call(m+"_mul",p,l,u),e.call(m+"_sub",u,_,u),e.call(m+"_add",u,g,u))}(),function(){const a=t.addFunction(q+"_mul014");a.addParam("pA","i32"),a.addParam("pC0","i32"),a.addParam("pC1","i32"),a.addParam("pC4","i32"),a.addParam("pR","i32");const e=a.getCodeBuilder(),o=e.getLocal("pA"),i=e.i32_add(e.getLocal("pA"),e.i32_const(6*c)),n=e.getLocal("pC0"),l=e.getLocal("pC1"),s=e.getLocal("pC4"),r=e.i32_const(t.alloc(6*c)),d=e.i32_const(t.alloc(6*c)),u=e.i32_const(t.alloc(2*c)),_=e.getLocal("pR"),g=e.i32_add(e.getLocal("pR"),e.i32_const(6*c));a.addCode(e.call(O+"_mul01",o,n,l,r),e.call(O+"_mul1",i,s,d),e.call(m+"_add",l,s,u),e.call(O+"_add",i,o,g),e.call(O+"_mul01",g,n,u,g),e.call(O+"_sub",g,r,g),e.call(O+"_sub",g,d,g),e.call(O+"_copy",d,_),e.call(O+"_mulNR",_,_),e.call(O+"_add",_,r,_))}(),function(){const a=t.addFunction(e+"_ell");a.addParam("pP","i32"),a.addParam("pCoefs","i32"),a.addParam("pF","i32");const o=a.getCodeBuilder(),i=o.getLocal("pP"),n=o.i32_add(o.getLocal("pP"),o.i32_const(l)),s=o.getLocal("pF"),r=o.getLocal("pCoefs"),d=o.i32_add(o.getLocal("pCoefs"),o.i32_const(c)),u=o.i32_add(o.getLocal("pCoefs"),o.i32_const(2*c)),_=o.i32_add(o.getLocal("pCoefs"),o.i32_const(3*c)),g=o.i32_add(o.getLocal("pCoefs"),o.i32_const(4*c)),p=t.alloc(2*c),h=o.i32_const(p),m=o.i32_const(p),L=o.i32_const(p+c),b=t.alloc(2*c),w=o.i32_const(b),y=o.i32_const(b),A=o.i32_const(b+c);a.addCode(o.call(f+"_mul",r,n,m),o.call(f+"_mul",d,n,L),o.call(f+"_mul",u,i,y),o.call(f+"_mul",_,i,A),o.call(q+"_mul014",s,g,w,h,s))}();const j=t.alloc(Q),V=t.alloc(k);function K(a){const o=t.addFunction(e+"_pairingEq"+a);for(let t=0;t>=BigInt(32)):n+2<=a?(i.setUint16(n,Number(e&BigInt(65535)),!0),n+=2,e>>=BigInt(16)):(i.setUint8(n,Number(e&BigInt(255)),!0),n+=1,e>>=BigInt(8));if(e)throw new Error("Number does not fit in this length");return o}const fn=[];for(let t=0;t<256;t++)fn[t]=pn(t,8);function pn(t,a){let e=0,o=t;for(let t=0;t>=1;return e}function hn(t,a){return(fn[t>>>24]|fn[t>>>16&255]<<8|fn[t>>>8&255]<<16|fn[255&t]<<24)>>>32-a}function mn(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 Ln(t,a){const e=t.byteLength/a,o=mn(e);if(e!=1<e){const o=t.slice(i*a,(i+1)*a);t.set(t.slice(e*a,(e+1)*a),i*a),t.set(o,e*a)}}}function bn(t,a){const e=new Uint8Array(a*t.length);for(let o=0;o{e[o]=t(a[o])})),e}return a},unstringifyBigInts:function t(a){if("string"==typeof a&&/^[0-9]+$/.test(a))return BigInt(a);if("string"==typeof a&&/^0x[0-9a-fA-F]+$/.test(a))return BigInt(a);if(Array.isArray(a))return a.map(t);if("object"==typeof a){if(null===a)return null;const e={};return Object.keys(a).forEach((o=>{e[o]=t(a[o])})),e}return a},beBuff2int:function(t){let a=BigInt(0),e=t.length,o=0;const i=new DataView(t.buffer,t.byteOffset,t.byteLength);for(;e>0;)e>=4?(e-=4,a+=BigInt(i.getUint32(e))<=2?(e-=2,a+=BigInt(i.getUint16(e))<0;)n-4>=0?(n-=4,i.setUint32(n,Number(e&BigInt(4294967295))),e>>=BigInt(32)):n-2>=0?(n-=2,i.setUint16(n,Number(e&BigInt(65535))),e>>=BigInt(16)):(n-=1,i.setUint8(n,Number(e&BigInt(255))),e>>=BigInt(8));if(e)throw new Error("Number does not fit in this length");return o},leBuff2int:function(t){let a=BigInt(0),e=0;const o=new DataView(t.buffer,t.byteOffset,t.byteLength);for(;e{o[i]=t(a,e[i])})),o}return e},unstringifyFElements:function t(a,e){if("string"==typeof e&&/^[0-9]+$/.test(e))return a.e(e);if("string"==typeof e&&/^0x[0-9a-fA-F]+$/.test(e))return a.e(e);if(Array.isArray(e))return e.map(t.bind(this,a));if("object"==typeof e){if(null===e)return null;const o={};return Object.keys(e).forEach((i=>{o[i]=t(a,e[i])})),o}return e},bitReverse:hn,log2:mn,buffReverseBits:Ln,array2buffer:bn,buffer2array:wn});const An=1<<30;class Cn{constructor(t){this.buffers=[],this.byteLength=t;for(let a=0;a0;){const t=l+c>An?An-l:c,a=new Uint8Array(this.buffers[n].buffer,this.buffers[n].byteOffset+l,t);if(t==e)return a.slice();i||(i=e<=An?new Uint8Array(e):new Cn(e)),i.set(a,e-c),c-=t,n++,l=0}return i}set(t,a){void 0===a&&(a=0);const e=t.byteLength;if(0==e)return;const o=Math.floor(a/An);if(o==Math.floor((a+e-1)/An))return t instanceof Cn&&1==t.buffers.length?this.buffers[o].set(t.buffers[0],a%An):this.buffers[o].set(t,a%An);let i=o,n=a%An,l=e;for(;l>0;){const a=n+l>An?An-n:l,o=t.slice(e-l,e-l+a);new Uint8Array(this.buffers[i].buffer,this.buffers[i].byteOffset+n,a).set(o),l-=a,i++,n=0}}}function xn(t,a,e,o){return async function(i){const n=Math.floor(i.byteLength/e);if(n*e!==i.byteLength)throw new Error("Invalid buffer size");const l=Math.floor(n/t.concurrency),c=[];for(let s=0;s=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=xn(t,a+"_batchToMontgomery",this.n8,this.n8),this.batchFromMontgomery=xn(t,a+"_batchFromMontgomery",this.n8,this.n8)}op2(t,a,e){return this.tm.setBuff(this.pOp1,a),this.tm.setBuff(this.pOp2,e),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op2Bool(t,a,e){return this.tm.setBuff(this.pOp1,a),this.tm.setBuff(this.pOp2,e),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp2)}op1(t,a){return this.tm.setBuff(this.pOp1,a),this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}op1Bool(t,a){return this.tm.setBuff(this.pOp1,a),!!this.tm.instance.exports[this.prefix+t](this.pOp1,this.pOp3)}add(t,a){return this.op2("_add",t,a)}eq(t,a){return this.op2Bool("_eq",t,a)}isZero(t){return this.op1Bool("_isZero",t)}sub(t,a){return this.op2("_sub",t,a)}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,a){return this.op2("_mul",t,a)}div(t,a){return this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,a),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,a){return a instanceof Uint8Array||(a=Ao(He(a))),this.tm.setBuff(this.pOp1,t),this.tm.setBuff(this.pOp2,a),this.tm.instance.exports[this.prefix+"_exp"](this.pOp1,this.pOp2,a.byteLength,this.pOp3),this.tm.getBuff(this.pOp3,this.n8)}isNegative(t){return this.op1Bool("_isNegative",t)}e(t,a){if(t instanceof Uint8Array)return t;let e=He(t,a);We(e)?(e=co(e),fo(e,this.p)&&(e=_o(e,this.p)),e=lo(this.p,e)):fo(e,this.p)&&(e=_o(e,this.p));const o=gn(e,this.n8);return this.toMontgomery(o)}toString(t,a){return yo(bo(this.fromMontgomery(t),0),a)}fromRng(t){let a;const e=new Uint8Array(this.n8);do{a=Co;for(let e=0;eo.buffer.byteLength){const i=o.buffer.byteLength/65536;let n=Math.floor((e[0]+t)/65536)+1;n>a&&(n=a),o.grow(n-i)}return i}function l(t){const a=n(t.byteLength);return s(a,t),a}function c(t,a){const e=new Uint8Array(o.buffer);return new Uint8Array(e.buffer,e.byteOffset+t,a)}function s(t,a){new Uint8Array(o.buffer).set(new Uint8Array(a),t)}function r(t){if("INIT"==t[0].cmd)return i(t[0]);const a={vars:[],out:[]},r=new Uint32Array(o.buffer,0,1)[0];for(let o=0;o0?i-4:i;for(e=0;e>16&255,l[c++]=a>>8&255,l[c++]=255&a;2===n&&(a=On[t.charCodeAt(e)]<<2|On[t.charCodeAt(e+1)]>>4,l[c++]=255&a);1===n&&(a=On[t.charCodeAt(e)]<<10|On[t.charCodeAt(e+1)]<<4|On[t.charCodeAt(e+2)]>>2,l[c++]=a>>8&255,l[c++]=255&a);return l},fromByteArray:function(t){for(var a,e=t.length,o=e%3,i=[],n=16383,l=0,c=e-o;lc?c:l+n));1===o?(a=t[e-1],i.push(Pn[a>>2]+Pn[a<<4&63]+"==")):2===o&&(a=(t[e-2]<<8)+t[e-1],i.push(Pn[a>>10]+Pn[a>>4&63]+Pn[a<<2&63]+"="));return i.join("")}},Pn=[],On=[],qn="undefined"!=typeof Uint8Array?Uint8Array:Array,Gn="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",zn=0,Tn=Gn.length;zn0)throw new Error("Invalid string. Length must be a multiple of 4");var e=t.indexOf("=");return-1===e&&(e=a),[e,e===a?0:4-e%4]}function Un(t,a,e){for(var o,i,n=[],l=a;l>18&63]+Pn[i>>12&63]+Pn[i>>6&63]+Pn[63&i]);return n.join("")}On["-".charCodeAt(0)]=62,On["_".charCodeAt(0)]=63;const Qn=25;class kn{constructor(){this.promise=new Promise(((t,a)=>{this.reject=a,this.resolve=t}))}}const Rn=function(t){return globalThis.btoa(t)}("("+vn.toString()+")(self)"),Nn="data:application/javascript;base64,"+Rn;class Dn{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,a,e,o){if(this.working[t])throw new Error("Posting a job t a working worker");return this.working[t]=!0,this.pendingDeferreds[t]=o||new kn,this.workers[t].postMessage(a,e),this.pendingDeferreds[t].promise}processWorks(){for(let t=0;t0;t++)if(0==this.working[t]){const a=this.actionQueue.shift();this.postAction(t,a.data,a.transfers,a.deferred)}}queueAction(t,a){const e=new kn;if(this.singleThread){const a=this.taskManager(t);e.resolve(a)}else this.actionQueue.push({data:t,transfers:a,deferred:e}),this.processWorks();return e.promise}resetMemory(){this.u32[0]=this.initalPFree}allocBuff(t){const a=this.alloc(t.byteLength);return this.setBuff(a,t),a}getBuff(t,a){return this.u8.slice(t,t+a)}setBuff(t,a){this.u8.set(new Uint8Array(a),t)}alloc(t){for(;3&this.u32[0];)this.u32[0]++;const a=this.u32[0];return this.u32[0]+=t,a}async terminate(){for(let t=0;tsetTimeout(a,t))))}}function $n(t,a){const e=t[a],o=t.Fr,i=t.tm;t[a].batchApplyKey=async function(t,n,l,c,s){let r,d,u,_,g;if(c=c||"affine",s=s||"affine","G1"==a)"jacobian"==c?(u=3*e.F.n8,r="g1m_batchApplyKey"):(u=2*e.F.n8,r="g1m_batchApplyKeyMixed"),_=3*e.F.n8,"jacobian"==s?g=3*e.F.n8:(d="g1m_batchToAffine",g=2*e.F.n8);else if("G2"==a)"jacobian"==c?(u=3*e.F.n8,r="g2m_batchApplyKey"):(u=2*e.F.n8,r="g2m_batchApplyKeyMixed"),_=3*e.F.n8,"jacobian"==s?g=3*e.F.n8:(d="g2m_batchToAffine",g=2*e.F.n8);else{if("Fr"!=a)throw new Error("Invalid group: "+a);r="frm_batchApplyKey",u=e.n8,_=e.n8,g=e.n8}const f=Math.floor(t.byteLength/u),p=Math.floor(f/i.concurrency),h=[];l=o.e(l);let m=o.e(n);for(let a=0;a=0;t--){if(!e.isZero(p))for(let t=0;tr&&(p=r),p<1024&&(p=1024);const h=[];for(let a=0;a(c&&c.debug(`Multiexp end: ${s}: ${a}/${u}`),t))))}const m=await Promise.all(h);let L=e.zero;for(let t=m.length-1;t>=0;t--)L=e.add(L,m[t]);return L}e.multiExp=async function(t,a,e,o){return await n(t,a,"jacobian",e,o)},e.multiExpAffine=async function(t,a,e,o){return await n(t,a,"affine",e,o)}}function Kn(t,a){const e=t[a],o=t.Fr,i=e.tm;async function n(t,c,s,r,d,u){s=s||"affine",r=r||"affine";let _,g,f,p,h,m,L,b;"G1"==a?("affine"==s?(_=2*e.F.n8,p="g1m_batchToJacobian"):_=3*e.F.n8,g=3*e.F.n8,c&&(b="g1m_fftFinal"),L="g1m_fftJoin",m="g1m_fftMix","affine"==r?(f=2*e.F.n8,h="g1m_batchToAffine"):f=3*e.F.n8):"G2"==a?("affine"==s?(_=2*e.F.n8,p="g2m_batchToJacobian"):_=3*e.F.n8,g=3*e.F.n8,c&&(b="g2m_fftFinal"),L="g2m_fftJoin",m="g2m_fftMix","affine"==r?(f=2*e.F.n8,h="g2m_batchToAffine"):f=3*e.F.n8):"Fr"==a&&(_=e.n8,g=e.n8,f=e.n8,c&&(b="frm_fftFinal"),m="frm_fftMix",L="frm_fftJoin");let w=!1;Array.isArray(t)?(t=bn(t,_),w=!0):t=t.slice(0,t.byteLength);const y=t.byteLength/_,A=mn(y);if(1<1<<28?new Cn(2*u[0].byteLength):new Uint8Array(2*u[0].byteLength);return _.set(u[0]),_.set(u[1],u[0].byteLength),_}(t,s,r,d,u):await async function(t,a,e,i,c){let s,r;s=t.slice(0,t.byteLength/2),r=t.slice(t.byteLength/2,t.byteLength);const d=[];[s,r]=await l(s,r,"fftJoinExt",o.one,o.shift,a,"jacobian",i,c),d.push(n(s,!1,"jacobian",e,i,c)),d.push(n(r,!1,"jacobian",e,i,c));const u=await Promise.all(d);let _;_=u[0].byteLength>1<<28?new Cn(2*u[0].byteLength):new Uint8Array(2*u[0].byteLength);return _.set(u[0]),_.set(u[1],u[0].byteLength),_}(t,s,r,d,u),w?wn(a,f):a}let C,x,F;c&&(C=o.inv(o.e(y))),Ln(t,_);let I=Math.min(16384,y),B=y/I;for(;B=16;)B*=2,I/=2;const E=mn(I),v=[];for(let a=0;a(d&&d.debug(`${u}: fft ${A} mix end: ${a}/${B}`),t))))}F=await Promise.all(v);for(let t=0;t(d&&d.debug(`${u}: fft ${A} join ${t}/${A} ${l+1}/${a} ${c}/${e/2}`),o))))}const l=await Promise.all(n);for(let t=0;t0;a--)x.set(F[a],t),t+=I*f,delete F[a];x.set(F[0].slice(0,(I-1)*f),t),delete F[0]}else for(let t=0;t65536&&(w=65536);const y=[];for(let a=0;a(u&&u.debug(`${_}: fftJoinExt End: ${a}/${b}`),t))))}const A=await Promise.all(y);let C,x;b*h>1<<28?(C=new Cn(b*h),x=new Cn(b*h)):(C=new Uint8Array(b*h),x=new Uint8Array(b*h));let F=0;for(let t=0;to.s+1)throw s&&s.error("lagrangeEvaluations input too big"),new Error("lagrangeEvaluations input too big");let g=t.slice(0,t.byteLength/2),f=t.slice(t.byteLength/2,t.byteLength);const p=o.exp(o.shift,u/2),h=o.inv(o.sub(o.one,p));[g,f]=await l(g,f,"prepareLagrangeEvaluation",h,o.shiftInv,i,"jacobian",s,r+" prep");const m=[];let L;return m.push(n(g,!0,"jacobian",c,s,r+" t0")),m.push(n(f,!0,"jacobian",c,s,r+" t1")),[g,f]=await Promise.all(m),L=g.byteLength>1<<28?new Cn(2*g.byteLength):new Uint8Array(2*g.byteLength),L.set(g),L.set(f,g.byteLength),L},e.fftMix=async function(t){const n=3*e.F.n8;let l,c;if("G1"==a)l="g1m_fftMix",c="g1m_fftJoin";else if("G2"==a)l="g2m_fftMix",c="g2m_fftJoin";else{if("Fr"!=a)throw new Error("Invalid group");l="frm_fftMix",c="frm_fftJoin"}const s=Math.floor(t.byteLength/n),r=mn(s);let d=1<=0;t--)g.set(_[t][0],f),f+=_[t][0].byteLength;return g}}async function Hn(t){const a=await async function(t,a){const e=new Dn;e.memory=new WebAssembly.Memory({initial:Qn}),e.u8=new Uint8Array(e.memory.buffer),e.u32=new Uint32Array(e.memory.buffer);const o=await WebAssembly.compile(t.code);if(e.instance=await WebAssembly.instantiate(o,{env:{memory:e.memory}}),e.singleThread=a,e.initalPFree=e.u32[0],e.pq=t.pq,e.pr=t.pr,e.pG1gen=t.pG1gen,e.pG1zero=t.pG1zero,e.pG2gen=t.pG2gen,e.pG2zero=t.pG2zero,e.pOneT=t.pOneT,a)e.code=t.code,e.taskManager=vn(),await e.taskManager([{cmd:"INIT",init:Qn,code:e.code.slice()}]),e.concurrency=1;else{let a;e.workers=[],e.pendingDeferreds=[],e.working=[],a="object"==typeof navigator&&navigator.hardwareConcurrency?navigator.hardwareConcurrency:Ta.cpus().length,0==a&&(a=2),a>64&&(a=64),e.concurrency=a;for(let t=0;t=0)a=await Zn();else{if(!(["BLS12381"].indexOf(e)>=0))throw new Error(`Curve not supported: ${t}`);a=await Wn()}return a}var nl={exports:{}},ll=function t(a,e){if(!a){var o=new cl(e);throw Error.captureStackTrace&&Error.captureStackTrace(o,t),o}};class cl extends Error{}cl.prototype.name="AssertionError";var sl={exports:{}};function rl(t){return t.length}var dl={byteLength:rl,toString:function(t){const a=t.byteLength;let e="";for(let o=0;o1&&61===t.charCodeAt(a-1)&&a--,3*a>>>2}_l[45]=62,_l[95]=63;var fl={byteLength:gl,toString:function(t){const a=t.byteLength;let e="";for(let o=0;o>2]+ul[(3&t[o])<<4|t[o+1]>>4]+ul[(15&t[o+1])<<2|t[o+2]>>6]+ul[63&t[o+2]];return a%3==2?e=e.substring(0,e.length-1)+"=":a%3==1&&(e=e.substring(0,e.length-2)+"=="),e},write:function(t,a,e=0,o=gl(a)){const i=Math.min(o,t.byteLength-e);for(let e=0,o=0;o>4,t[o++]=(15&n)<<4|l>>2,t[o++]=(3&l)<<6|63&c}return i}};function pl(t){return t.length>>>1}var hl={byteLength:pl,toString:function(t){const a=t.byteLength;t=new DataView(t.buffer,t.byteOffset,a);let e="",o=0;for(let i=a-a%4;o=48&&t<=57?t-48:t>=65&&t<=70?t-65+10:t>=97&&t<=102?t-97+10:void 0}function Ll(t){let a=0;for(let e=0,o=t.length;e=55296&&i<=56319&&e+1=56320&&o<=57343){a+=4,e++;continue}}a+=i<=127?1:i<=2047?2:3}return a}let bl,wl;if("undefined"!=typeof TextDecoder){const t=new TextDecoder;bl=function(a){return t.decode(a)}}else bl=function(t){const a=t.byteLength;let e="",o=0;for(;o0){let a=0;for(;a>o,o-=6;o>=0;)t[l++]=128|e>>o&63,o-=6;n+=e>=65536?2:1}return i};var yl={byteLength:Ll,toString:bl,write:wl};function Al(t){return 2*t.length}var Cl,xl,Fl={byteLength:Al,toString:function(t){const a=t.byteLength;let e="";for(let o=0;o>8,l=i%256;t[e+2*o]=l,t[e+2*o+1]=n}return i}};!function(t,a){const e=dl,o=fl,i=hl,n=yl,l=Fl,c=255===new Uint8Array(Uint16Array.of(255).buffer)[0];function s(t){switch(t){case"ascii":return e;case"base64":return o;case"hex":return i;case"utf8":case"utf-8":case void 0:return n;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return l;default:throw new Error(`Unknown encoding: ${t}`)}}function r(t){return t instanceof Uint8Array}function d(t,a,e){return"string"==typeof t?function(t,a){const e=s(a),o=new Uint8Array(e.byteLength(t));return e.write(o,t,0,o.byteLength),o}(t,a):Array.isArray(t)?function(t){const a=new Uint8Array(t.length);return a.set(t),a}(t):ArrayBuffer.isView(t)?function(t){const a=new Uint8Array(t.byteLength);return a.set(t),a}(t):function(t,a,e){return new Uint8Array(t,a,e)}(t,a,e)}function u(t,a,e,o,i){if(0===t.byteLength)return-1;if("string"==typeof e?(o=e,e=0):void 0===e?e=i?0:t.length-1:e<0&&(e+=t.byteLength),e>=t.byteLength){if(i)return-1;e=t.byteLength-1}else if(e<0){if(!i)return-1;e=0}if("string"==typeof a)a=d(a,o);else if("number"==typeof a)return a&=255,i?t.indexOf(a,e):t.lastIndexOf(a,e);if(0===a.byteLength)return-1;if(i){let o=-1;for(let i=e;it.byteLength&&(e=t.byteLength-a.byteLength);for(let o=e;o>=0;o--){let e=!0;for(let i=0;ii)return 1}return t.byteLength>a.byteLength?1:t.byteLengtht+a.byteLength),0));const e=new Uint8Array(a);return t.reduce(((t,a)=>(e.set(a,t),t+a.byteLength)),0),e},copy:function(t,a,e=0,o=0,i=t.byteLength){if(i>0&&i=t.byteLength)throw new RangeError("sourceStart is out of range");if(i<0)throw new RangeError("sourceEnd is out of range");e>=a.byteLength&&(e=a.byteLength),i>t.byteLength&&(i=t.byteLength),a.byteLength-e=i||o<=e?"":(e<0&&(e=0),o>i&&(o=i),(0!==e||o{for(var t=new Uint8Array(128),a=0;a<64;a++)t[a<26?a+65:a<52?a+71:a<62?a-4:4*a-205]=a;return a=>{for(var e=a.length,o=new Uint8Array(3*(e-("="==a[e-1])-("="==a[e-2]))/4|0),i=0,n=0;i>4,o[n++]=c<<4|s>>2,o[n++]=s<<6|r}return o}})(),a=((t,a)=>function(){return a||(0,t[Object.keys(t)[0]])((a={exports:{}}).exports,a),a.exports})({"wasm-binary:./blake2b.wat"(a,e){e.exports=t("AGFzbQEAAAABEANgAn9/AGADf39/AGABfwADBQQAAQICBQUBAQroBwdNBQZtZW1vcnkCAAxibGFrZTJiX2luaXQAAA5ibGFrZTJiX3VwZGF0ZQABDWJsYWtlMmJfZmluYWwAAhBibGFrZTJiX2NvbXByZXNzAAMKvz8EwAIAIABCADcDACAAQgA3AwggAEIANwMQIABCADcDGCAAQgA3AyAgAEIANwMoIABCADcDMCAAQgA3AzggAEIANwNAIABCADcDSCAAQgA3A1AgAEIANwNYIABCADcDYCAAQgA3A2ggAEIANwNwIABCADcDeCAAQoiS853/zPmE6gBBACkDAIU3A4ABIABCu86qptjQ67O7f0EIKQMAhTcDiAEgAEKr8NP0r+68tzxBECkDAIU3A5ABIABC8e30+KWn/aelf0EYKQMAhTcDmAEgAELRhZrv+s+Uh9EAQSApAwCFNwOgASAAQp/Y+dnCkdqCm39BKCkDAIU3A6gBIABC6/qG2r+19sEfQTApAwCFNwOwASAAQvnC+JuRo7Pw2wBBOCkDAIU3A7gBIABCADcDwAEgAEIANwPIASAAQgA3A9ABC20BA38gAEHAAWohAyAAQcgBaiEEIAQpAwCnIQUCQANAIAEgAkYNASAFQYABRgRAIAMgAykDACAFrXw3AwBBACEFIAAQAwsgACAFaiABLQAAOgAAIAVBAWohBSABQQFqIQEMAAsLIAQgBa03AwALYQEDfyAAQcABaiEBIABByAFqIQIgASABKQMAIAIpAwB8NwMAIABCfzcD0AEgAikDAKchAwJAA0AgA0GAAUYNASAAIANqQQA6AAAgA0EBaiEDDAALCyACIAOtNwMAIAAQAwuqOwIgfgl/IABBgAFqISEgAEGIAWohIiAAQZABaiEjIABBmAFqISQgAEGgAWohJSAAQagBaiEmIABBsAFqIScgAEG4AWohKCAhKQMAIQEgIikDACECICMpAwAhAyAkKQMAIQQgJSkDACEFICYpAwAhBiAnKQMAIQcgKCkDACEIQoiS853/zPmE6gAhCUK7zqqm2NDrs7t/IQpCq/DT9K/uvLc8IQtC8e30+KWn/aelfyEMQtGFmu/6z5SH0QAhDUKf2PnZwpHagpt/IQ5C6/qG2r+19sEfIQ9C+cL4m5Gjs/DbACEQIAApAwAhESAAKQMIIRIgACkDECETIAApAxghFCAAKQMgIRUgACkDKCEWIAApAzAhFyAAKQM4IRggACkDQCEZIAApA0ghGiAAKQNQIRsgACkDWCEcIAApA2AhHSAAKQNoIR4gACkDcCEfIAApA3ghICANIAApA8ABhSENIA8gACkD0AGFIQ8gASAFIBF8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSASfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgE3x8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBR8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAVfHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgFnx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBd8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAYfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgGXx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBp8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAbfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgHHx8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIB18fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCAefHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgH3x8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFICB8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAffHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgG3x8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBV8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAZfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgGnx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHICB8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAefHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggF3x8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBJ8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiAdfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgEXx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBN8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAcfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggGHx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBZ8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAUfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgHHx8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIBl8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAdfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgEXx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBZ8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByATfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggIHx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIB58fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAbfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgH3x8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBR8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAXfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggGHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBJ8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAafHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFXx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIBh8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSAafHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgFHx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBJ8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAefHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgHXx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBx8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAffHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgE3x8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBd8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAWfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgG3x8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIBV8fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCARfHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgIHx8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFIBl8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAafHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgEXx8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBZ8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAYfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgE3x8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHIBV8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAbfHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggIHx8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIB98fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiASfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgHHx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIB18fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAXfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggGXx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBR8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAefHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgE3x8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIB18fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAXfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgG3x8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBF8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAcfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggGXx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBR8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAVfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgHnx8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBh8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAWfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggIHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIB98fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSASfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgGnx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIB18fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSAWfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgEnx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGICB8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAffHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgHnx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBV8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAbfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgEXx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBh8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAXfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgFHx8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIBp8fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCATfHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgGXx8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFIBx8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAefHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgHHx8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBh8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAffHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgHXx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHIBJ8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAUfHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggGnx8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBZ8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiARfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgIHx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBV8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAZfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggF3x8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBN8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAbfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgF3x8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFICB8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAffHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgGnx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBx8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAUfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggEXx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBl8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAdfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgE3x8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIB58fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAYfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggEnx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBV8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAbfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFnx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIBt8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSATfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgGXx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBV8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAYfHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgF3x8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBJ8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAWfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgIHx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBx8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAafHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgH3x8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIBR8fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCAdfHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgHnx8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFIBF8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSARfHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgEnx8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBN8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAUfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgFXx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHIBZ8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAXfHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggGHx8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBl8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiAafHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgG3x8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBx8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAdfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggHnx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIB98fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAgfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgH3x8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIBt8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAVfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgGXx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBp8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAgfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggHnx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBd8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiASfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgHXx8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBF8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByATfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggHHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBh8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAWfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFHx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgISAhKQMAIAEgCYWFNwMAICIgIikDACACIAqFhTcDACAjICMpAwAgAyALhYU3AwAgJCAkKQMAIAQgDIWFNwMAICUgJSkDACAFIA2FhTcDACAmICYpAwAgBiAOhYU3AwAgJyAnKQMAIAcgD4WFNwMAICggKCkDACAIIBCFhTcDAAs=")}}),e=a(),o=WebAssembly.compile(e);return Cl=async t=>(await WebAssembly.instantiate(await o,t)).exports}()().then((t=>{El=t})),Sl=64,Pl=[];nl.exports=Ul;var Ol=nl.exports.BYTES_MIN=16,ql=nl.exports.BYTES_MAX=64;nl.exports.BYTES=32;var Gl=nl.exports.KEYBYTES_MIN=16,zl=nl.exports.KEYBYTES_MAX=64;nl.exports.KEYBYTES=32;var Tl=nl.exports.SALTBYTES=16,Ml=nl.exports.PERSONALBYTES=16;function Ul(t,a,e,o,i){if(!(this instanceof Ul))return new Ul(t,a,e,o,i);if(!El)throw new Error("WASM not loaded. Wait for Blake2b.ready(cb)");t||(t=32),!0!==i&&(Il(t>=Ol,"digestLength must be at least "+Ol+", was given "+t),Il(t<=ql,"digestLength must be at most "+ql+", was given "+t),null!=a&&(Il(a instanceof Uint8Array,"key must be Uint8Array or Buffer"),Il(a.length>=Gl,"key must be at least "+Gl+", was given "+a.length),Il(a.length<=zl,"key must be at least "+zl+", was given "+a.length)),null!=e&&(Il(e instanceof Uint8Array,"salt must be Uint8Array or Buffer"),Il(e.length===Tl,"salt must be exactly "+Tl+", was given "+e.length)),null!=o&&(Il(o instanceof Uint8Array,"personal must be Uint8Array or Buffer"),Il(o.length===Ml,"personal must be exactly "+Ml+", was given "+o.length))),Pl.length||(Pl.push(Sl),Sl+=216),this.digestLength=t,this.finalized=!1,this.pointer=Pl.pop(),this._memory=new Uint8Array(El.memory.buffer),this._memory.fill(0,0,64),this._memory[0]=this.digestLength,this._memory[1]=a?a.length:0,this._memory[2]=1,this._memory[3]=1,e&&this._memory.set(e,32),o&&this._memory.set(o,48),this.pointer+216>this._memory.length&&this._realloc(this.pointer+216),El.blake2b_init(this.pointer,this.digestLength),a&&(this.update(a),this._memory.fill(0,Sl,Sl+a.length),this._memory[this.pointer+200]=128)}function Ql(){}function kl(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 Rl(t,a){const e=new DataView(t.buffer,t.byteOffset,t.byteLength);let o="";for(let t=0;t<4;t++){t>0&&(o+="\n"),o+="\t\t";for(let a=0;a<4;a++)a>0&&(o+=" "),o+=e.getUint32(16*t+4*a).toString(16).padStart(8,"0")}return a&&(o=a+"\n"+o),o}function Nl(t,a){if(t.byteLength!=a.byteLength)return!1;for(var e=new Int8Array(t),o=new Int8Array(a),i=0;i!=t.byteLength;i++)if(e[i]!=o[i])return!1;return!0}function Dl(t){const a=t.getPartialHash(),e=nl.exports(64);return e.setPartialHash(a),e}async function $l(t,a,e,o,i){if(t.G1.isZero(a))return!1;if(t.G1.isZero(e))return!1;if(t.G2.isZero(o))return!1;if(t.G2.isZero(i))return!1;return await t.pairingEq(a,i,t.G1.neg(e),o)}function jl(t){let a=new Uint8Array(t);return void 0!==globalThis.crypto?globalThis.crypto.getRandomValues(a):U.randomFillSync(a),a}async function Vl(t){if(void 0!==globalThis.crypto&&void 0!==globalThis.crypto.subtle){const a=await globalThis.crypto.subtle.digest("SHA-256",t.buffer);return new Uint8Array(a)}return U.createHash("sha256").update(t).digest()}function Kl(t,a){return new DataView(t.buffer).getUint32(a,!1)}async function Hl(t){for(;!t;)t=await window.prompt("Enter a random text. (Entropy): ","");const a=nl.exports(64);a.update(jl(64));const e=new TextEncoder;a.update(e.encode(t));const o=a.digest(),i=[];for(let t=0;t<8;t++)i[t]=Kl(o,4*t);return new vo(i)}async function Zl(t,a){let e,o;a<32?(e=1<>>0,o=1):(e=4294967296,o=1<>>0);let i=t;for(let t=0;t{e[o]=Jl(t,a[o])})),e}return"bigint"==typeof a||void 0!==a.eq?a.toString(10):a}Ul.prototype._realloc=function(t){El.memory.grow(Math.max(0,Math.ceil(Math.abs(t-this._memory.length)/65536))),this._memory=new Uint8Array(El.memory.buffer)},Ul.prototype.update=function(t){return Il(!1===this.finalized,"Hash instance finalized"),Il(t instanceof Uint8Array,"input must be Uint8Array or Buffer"),Sl+t.length>this._memory.length&&this._realloc(Sl+t.length),this._memory.set(t,Sl),El.blake2b_update(this.pointer,Sl,Sl+t.length),this},Ul.prototype.digest=function(t){if(Il(!1===this.finalized,"Hash instance finalized"),this.finalized=!0,Pl.push(this.pointer),El.blake2b_final(this.pointer),!t||"binary"===t)return this._memory.slice(this.pointer+128,this.pointer+128+this.digestLength);if("string"==typeof t)return Bl.toString(this._memory,t,this.pointer+128,this.pointer+128+this.digestLength);Il(t instanceof Uint8Array&&t.length>=this.digestLength,"input must be Uint8Array or Buffer");for(var a=0;at()),t):t(new Error("WebAssembly not supported"))},Ul.prototype.ready=Ul.ready,Ul.prototype.getPartialHash=function(){return this._memory.slice(this.pointer,this.pointer+216)},Ul.prototype.setPartialHash=function(t){this._memory.set(t,this.pointer)};const Xl=1,tc=1,ac=2,ec=10,oc=3,ic=17,nc=2,lc=3,cc=4,sc=5,rc=6,dc=7,uc=8,_c=9,gc=10,fc=11,pc=12,hc=13,mc=14,Lc=15,bc=16,wc=17;async function yc(t,a){await Me(t,1),await t.writeULE32(1),await Ue(t);const e=await ol(a.q);await Me(t,2);const o=e.q,i=8*(Math.floor((Yn.bitLength(o)-1)/64)+1),n=e.r,l=8*(Math.floor((Yn.bitLength(n)-1)/64)+1);await t.writeULE32(i),await Re(t,o,i),await t.writeULE32(l),await Re(t,n,l),await t.writeULE32(a.nVars),await t.writeULE32(a.nPublic),await t.writeULE32(a.domainSize),await Ac(t,e,a.vk_alpha_1),await Ac(t,e,a.vk_beta_1),await Cc(t,e,a.vk_beta_2),await Cc(t,e,a.vk_gamma_2),await Ac(t,e,a.vk_delta_1),await Cc(t,e,a.vk_delta_2),await Ue(t)}async function Ac(t,a,e){const o=new Uint8Array(2*a.G1.F.n8);a.G1.toRprLEM(o,0,e),await t.write(o)}async function Cc(t,a,e){const o=new Uint8Array(2*a.G2.F.n8);a.G2.toRprLEM(o,0,e),await t.write(o)}async function xc(t,a,e){const o=await t.read(2*a.G1.F.n8),i=a.G1.fromRprLEM(o,0);return e?a.G1.toObject(i):i}async function Fc(t,a,e){const o=await t.read(2*a.G2.F.n8),i=a.G2.fromRprLEM(o,0);return e?a.G2.toObject(i):i}async function Ic(t,a,e){await Qe(t,a,1);const o=await t.readULE32();if(await ke(t),o===tc)return await async function(t,a,e){const o={protocol:"groth16"};await Qe(t,a,2);const i=await t.readULE32();o.n8q=i,o.q=await Ne(t,i);const n=await t.readULE32();return o.n8r=n,o.r=await Ne(t,n),o.curve=await ol(o.q),o.nVars=await t.readULE32(),o.nPublic=await t.readULE32(),o.domainSize=await t.readULE32(),o.power=kl(o.domainSize),o.vk_alpha_1=await xc(t,o.curve,e),o.vk_beta_1=await xc(t,o.curve,e),o.vk_beta_2=await Fc(t,o.curve,e),o.vk_gamma_2=await Fc(t,o.curve,e),o.vk_delta_1=await xc(t,o.curve,e),o.vk_delta_2=await Fc(t,o.curve,e),await ke(t),o}(t,a,e);if(o===ac)return await async function(t,a,e){const o={protocol:"plonk"};await Qe(t,a,2);const i=await t.readULE32();o.n8q=i,o.q=await Ne(t,i);const n=await t.readULE32();return o.n8r=n,o.r=await Ne(t,n),o.curve=await ol(o.q),o.nVars=await t.readULE32(),o.nPublic=await t.readULE32(),o.domainSize=await t.readULE32(),o.power=kl(o.domainSize),o.nAdditions=await t.readULE32(),o.nConstraints=await t.readULE32(),o.k1=await t.read(n),o.k2=await t.read(n),o.Qm=await xc(t,o.curve,e),o.Ql=await xc(t,o.curve,e),o.Qr=await xc(t,o.curve,e),o.Qo=await xc(t,o.curve,e),o.Qc=await xc(t,o.curve,e),o.S1=await xc(t,o.curve,e),o.S2=await xc(t,o.curve,e),o.S3=await xc(t,o.curve,e),o.X_2=await Fc(t,o.curve,e),await ke(t),o}(t,a,e);if(o===ec)return await async function(t,a,e){const o={protocol:"fflonk"};o.protocolId=ec,await Qe(t,a,nc);const i=await t.readULE32();o.n8q=i,o.q=await Ne(t,i),o.curve=await ol(o.q);const n=await t.readULE32();return o.n8r=n,o.r=await Ne(t,n),o.nVars=await t.readULE32(),o.nPublic=await t.readULE32(),o.domainSize=await t.readULE32(),o.power=kl(o.domainSize),o.nAdditions=await t.readULE32(),o.nConstraints=await t.readULE32(),o.k1=await t.read(n),o.k2=await t.read(n),o.w3=await t.read(n),o.w4=await t.read(n),o.w8=await t.read(n),o.wr=await t.read(n),o.X_2=await Fc(t,o.curve,e),o.C0=await xc(t,o.curve,e),await ke(t),o}(t,a,e);throw new Error("Protocol not supported: ")}async function Bc(t,a,e){const o={delta:{}};o.deltaAfter=await xc(t,a,e),o.delta.g1_s=await xc(t,a,e),o.delta.g1_sx=await xc(t,a,e),o.delta.g2_spx=await Fc(t,a,e),o.transcript=await t.read(64),o.type=await t.readULE32();const i=await t.readULE32(),n=t.pos;let l=0;for(;t.pos-n0){const a=new Uint8Array(o);await t.writeULE32(a.byteLength),await t.write(a)}else await t.writeULE32(0)}async function Sc(t,a,e){await Me(t,10),await t.write(e.csHash),await t.writeULE32(e.contributions.length);for(let o=0;o0;)e.unshift(0),n--;return e}async function Qc(t,a){a=a||{};let e,o=32767,i=!1;for(;!i;)try{e=new WebAssembly.Memory({initial:o}),i=!0}catch(t){if(1===o)throw t;console.warn("Could not allocate "+1024*o*64+" bytes. This may cause severe instability. Trying with "+1024*o*64/2+" bytes"),o=Math.floor(o/2)}const n=await WebAssembly.compile(t);let l,c="",s="",r=1,d=0,u=0;const _=await WebAssembly.instantiate(n,{env:{memory:e},runtime:{exceptionHandler:function(t){let a;throw a=1==t?"Signal not found. ":2==t?"Too many signals set. ":3==t?"Signal already set. ":4==t?"Assert Failed. ":5==t?"Not enough memory. ":6==t?"Input signal array access exceeds the size. ":"Unknown error. ",console.error("ERROR: ",t,c),new Error(a+c)},printErrorMessage:function(){c+=f()+"\n"},writeBufferMessage:function(){const t=f();"\n"===t?(console.log(s),s=""):(""!==s&&(s+=" "),s+=t)},showSharedRWMemory:function(){const t=_.exports.getFieldNumLen32(),a=new Uint32Array(t);for(let e=0;e=2&&(d>=1||u>=7)){""!==s&&(s+=" ");const t=_e.fromArray(a,4294967296).toString();s+=t}else console.log(_e.fromArray(a,4294967296))},error:function(t,e,o,i,n,c){let s;throw s=7==t?p(e)+" "+l.getFr(i).toString()+" != "+l.getFr(n).toString()+" "+p(c):9==t?p(e)+" "+l.getFr(i).toString()+" "+p(n):5==t&&a.sym?p(e)+" "+a.sym.labelIdx2Name[n]:p(e)+" "+o+" "+i+" "+n+" "+c,console.log("ERROR: ",t,s),new Error(s)},log:function(t){console.log(l.getFr(t).toString())},logGetSignal:function(t,e){a.logGetSignal&&a.logGetSignal(t,l.getFr(e))},logSetSignal:function(t,e){a.logSetSignal&&a.logSetSignal(t,l.getFr(e))},logStartComponent:function(t){a.logStartComponent&&a.logStartComponent(t)},logFinishComponent:function(t){a.logFinishComponent&&a.logFinishComponent(t)}}});"function"==typeof _.exports.getVersion&&(r=_.exports.getVersion()),"function"==typeof _.exports.getMinorVersion&&(d=_.exports.getMinorVersion()),"function"==typeof _.exports.getPatchVersion&&(u=_.exports.getPatchVersion());const g=a&&(a.sanityCheck||a.logGetSignal||a.logSetSignal||a.logStartComponent||a.logFinishComponent);return l=2===r?new Rc(_,g):new kc(e,_,g),l;function f(){for(var t="",a=_.exports.getMessageChar();0!=a;)t+=String.fromCharCode(a),a=_.exports.getMessageChar();return t}function p(t){const a=new Uint8Array(e.buffer),o=[];for(let e=0;a[t+e]>0;e++)o.push(a[t+e]);return String.fromCharCode.apply(null,o)}}class kc{constructor(t,a,e){this.memory=t,this.i32=new Uint32Array(t.buffer),this.instance=a,this.n32=(this.instance.exports.getFrLen()>>2)-2;const o=this.instance.exports.getPRawPrime(),i=new Array(this.n32);for(let t=0;t>2)+t];this.prime=_e.fromArray(i,4294967296),this.Fr=new j(this.prime),this.mask32=_e.fromString("FFFFFFFF",16),this.NVars=this.instance.exports.getNVars(),this.n64=Math.floor((this.Fr.bitLength-1)/64)+1,this.R=this.Fr.e(_e.shiftLeft(1,64*this.n64)),this.RInv=this.Fr.inv(this.R),this.sanityCheck=e}circom_version(){return 1}async _doCalculateWitness(t,a){this.instance.exports.init(this.sanityCheck||a?1:0);const e=this.allocInt(),o=this.allocFr();Object.keys(t).forEach((a=>{const i=Mc(a),n=parseInt(i.slice(0,8),16),l=parseInt(i.slice(8,16),16);try{this.instance.exports.getSignalOffset32(e,0,n,l)}catch(t){throw new Error(`Signal ${a} is not an input of the circuit.`)}const c=this.getInt(e),s=Tc(t[a]);for(let t=0;t>2]}setInt(t,a){this.i32[t>>2]=a}getFr(t){const a=this,e=t>>2;if(2147483648&a.i32[e+1]){const t=new Array(a.n32);for(let o=0;o>2]=i,void(e.i32[1+(t>>2)]=0)}e.i32[t>>2]=0,e.i32[1+(t>>2)]=2147483648;const n=_e.toArray(a,4294967296);for(let a=0;a>2)+a]=o>=0?n[o]:0}}}class Rc{constructor(t,a){this.instance=t,this.version=this.instance.exports.getVersion(),this.n32=this.instance.exports.getFieldNumLen32(),this.instance.exports.getRawPrime();const e=new Array(this.n32);for(let t=0;t{const e=Mc(a),i=parseInt(e.slice(0,8),16),n=parseInt(e.slice(8,16),16),l=Tc(t[a]);for(let t=0;t1)throw new Error(t.fileName+": File has more than one header");t.pos=a[1][0].p;const e=await t.readULE32(),o=await t.read(e),i=Yn.fromRprLE(o),n=await ol(i);if(8*n.F1.n64!=e)throw new Error(t.fileName+": Invalid size");const l=await t.readULE32(),c=await t.readULE32();if(t.pos-a[1][0].p!=a[1][0].size)throw new Error("Invalid PTau header size");return{curve:n,power:l,ceremonyPower:c}}function as(t,a,e,o){const i={tau:{},alpha:{},beta:{}};return i.tau.g1_s=n(),i.tau.g1_sx=n(),i.alpha.g1_s=n(),i.alpha.g1_sx=n(),i.beta.g1_s=n(),i.beta.g1_sx=n(),i.tau.g2_spx=l(),i.alpha.g2_spx=l(),i.beta.g2_spx=l(),i;function n(){let i;return i=o?e.G1.fromRprLEM(t,a):e.G1.fromRprUncompressed(t,a),a+=2*e.G1.F.n8,i}function l(){let i;return i=o?e.G2.fromRprLEM(t,a):e.G2.fromRprUncompressed(t,a),a+=2*e.G2.F.n8,i}}function es(t,a,e,o,i){async function n(o){i?e.G1.toRprLEM(t,a,o):e.G1.toRprUncompressed(t,a,o),a+=2*e.F1.n8}async function l(o){i?e.G2.toRprLEM(t,a,o):e.G2.toRprUncompressed(t,a,o),a+=2*e.F2.n8}return n(o.tau.g1_s),n(o.tau.g1_sx),n(o.alpha.g1_s),n(o.alpha.g1_sx),n(o.beta.g1_s),n(o.beta.g1_sx),l(o.tau.g2_spx),l(o.alpha.g2_spx),l(o.beta.g2_spx),t}async function os(t,a){const e={};e.tauG1=await s(),e.tauG2=await r(),e.alphaG1=await s(),e.betaG1=await s(),e.betaG2=await r(),e.key=await async function(t,a,e){return as(await t.read(2*a.F1.n8*6+2*a.F2.n8*3),0,a,e)}(t,a,!0),e.partialHash=await t.read(216),e.nextChallenge=await t.read(64),e.type=await t.readULE32();const o=new Uint8Array(2*a.G1.F.n8*6+2*a.G2.F.n8*3);es(o,0,a,e.key,!1);const i=nl.exports(64);i.setPartialHash(e.partialHash),i.update(o),e.responseHash=i.digest();const n=await t.readULE32(),l=t.pos;let c=0;for(;t.pos-l1)throw new Error(t.fileName+": File has more than one contributions section");t.pos=e[7][0].p;const o=await t.readULE32(),i=[];for(let e=0;e0){const a=new Uint8Array(n);await t.writeULE32(a.byteLength),await t.write(a)}else await t.writeULE32(0);async function l(e){a.G1.toRprLEM(o,0,e),await t.write(o)}async function c(e){a.G2.toRprLEM(i,0,e),await t.write(i)}}async function ls(t,a,e){await t.writeULE32(7);const o=t.pos;await t.writeULE64(0),await t.writeULE32(e.length);for(let o=0;o0?u[u.length-1].nextChallenge:cs(r,d,n);const b=await Te(e,"ptau",1,i?7:2);await Xc(b,r,d);const w=await m.read(64);if(Nl(l,L)&&(L=w,u[u.length-1].nextChallenge=L),!Nl(w,L))throw new Error("Wrong contribution. this contribution is not based on the previus hash");const y=new nl.exports(64);y.update(w);const A=[];let C;C=await I(m,b,"G1",2,2**d*2-1,[1],"tauG1"),_.tauG1=C[0],C=await I(m,b,"G2",3,2**d,[1],"tauG2"),_.tauG2=C[0],C=await I(m,b,"G1",4,2**d,[0],"alphaG1"),_.alphaG1=C[0],C=await I(m,b,"G1",5,2**d,[0],"betaG1"),_.betaG1=C[0],C=await I(m,b,"G2",6,1,[0],"betaG2"),_.betaG2=C[0],_.partialHash=y.getPartialHash();const x=await m.read(2*r.F1.n8*6+2*r.F2.n8*3);_.key=as(x,0,r,!1),y.update(new Uint8Array(x));const F=y.digest();if(n&&n.info(Rl(F,"Contribution Response Hash imported: ")),i){const t=new nl.exports(64);t.update(F),await B(t,b,"G1",2,2**d*2-1,"tauG1",n),await B(t,b,"G2",3,2**d,"tauG2",n),await B(t,b,"G1",4,2**d,"alphaTauG1",n),await B(t,b,"G1",5,2**d,"betaTauG1",n),await B(t,b,"G2",6,1,"betaG2",n),_.nextChallenge=t.digest(),n&&n.info(Rl(_.nextChallenge,"Next Challenge Hash: "))}else _.nextChallenge=l;return u.push(_),await ls(b,r,u),await m.close(),await b.close(),await c.close(),_.nextChallenge;async function I(t,a,e,o,l,c,s){return i?await async function(t,a,e,o,i,l,c){const s=r[e],d=s.F.n8,u=2*s.F.n8,_=[];await Me(a,o);const g=Math.floor((1<<24)/u);A[o]=a.pos;for(let e=0;e=e&&a=a&&i1?s[s.length-2]:r;const u=s[s.length-1];if(a&&a.debug("Validating contribution #"+s[s.length-1].id),!await ds(n,u,d,a))return!1;const _=nl.exports(64);_.update(u.responseHash),a&&a.debug("Verifying powers in tau*G1 section");const g=await w(2,"G1","tauG1",2**l*2-1,[0,1],a);if(e=await rs(n,g.R1,g.R2,n.G2.g,u.tauG2),!0!==e)return a&&a.error("tauG1 section. Powers do not match"),!1;if(!n.G1.eq(n.G1.g,g.singularPoints[0]))return a&&a.error("First element of tau*G1 section must be the generator"),!1;if(!n.G1.eq(u.tauG1,g.singularPoints[1]))return a&&a.error("Second element of tau*G1 section does not match the one in the contribution section"),!1;a&&a.debug("Verifying powers in tau*G2 section");const f=await w(3,"G2","tauG2",2**l,[0,1],a);if(e=await rs(n,n.G1.g,u.tauG1,f.R1,f.R2),!0!==e)return a&&a.error("tauG2 section. Powers do not match"),!1;if(!n.G2.eq(n.G2.g,f.singularPoints[0]))return a&&a.error("First element of tau*G2 section must be the generator"),!1;if(!n.G2.eq(u.tauG2,f.singularPoints[1]))return a&&a.error("Second element of tau*G2 section does not match the one in the contribution section"),!1;a&&a.debug("Verifying powers in alpha*tau*G1 section");const p=await w(4,"G1","alphatauG1",2**l,[0],a);if(e=await rs(n,p.R1,p.R2,n.G2.g,u.tauG2),!0!==e)return a&&a.error("alphaTauG1 section. Powers do not match"),!1;if(!n.G1.eq(u.alphaG1,p.singularPoints[0]))return a&&a.error("First element of alpha*tau*G1 section (alpha*G1) does not match the one in the contribution section"),!1;a&&a.debug("Verifying powers in beta*tau*G1 section");const h=await w(5,"G1","betatauG1",2**l,[0],a);if(e=await rs(n,h.R1,h.R2,n.G2.g,u.tauG2),!0!==e)return a&&a.error("betaTauG1 section. Powers do not match"),!1;if(!n.G1.eq(u.betaG1,h.singularPoints[0]))return a&&a.error("First element of beta*tau*G1 section (beta*G1) does not match the one in the contribution section"),!1;const m=await async function(t){const a=n.G2,e=2*a.F.n8,l=new Uint8Array(e);if(!i[6])throw t.error("File has no BetaG2 section"),new Error("File has no BetaG2 section");if(i[6].length>1)throw t.error("File has no BetaG2 section"),new Error("File has more than one GetaG2 section");o.pos=i[6][0].p;const c=await o.read(e),s=a.fromRprLEM(c);return a.toRprUncompressed(l,0,s),_.update(l),s}(a);if(!n.G2.eq(u.betaG2,m))return a&&a.error("betaG2 element in betaG2 section does not match the one in the contribution section"),!1;const L=_.digest();if(l==c&&!Nl(L,u.nextChallenge))return a&&a.error("Hash of the values does not match the next challenge of the last contributor in the contributions section"),!1;a&&a.info(Rl(L,"Next challenge hash: ")),b(u,d);for(let t=s.length-2;t>=0;t--){const e=s[t],o=t>0?s[t-1]:r;if(!await ds(n,e,o,a))return!1;b(e,o)}if(a&&a.info("-----------------------------------------------------"),i[12]&&i[13]&&i[14]&&i[15]){let t;if(t=await y("G1",2,12,"tauG1",a),!t)return!1;if(t=await y("G2",3,13,"tauG2",a),!t)return!1;if(t=await y("G1",4,14,"alphaTauG1",a),!t)return!1;if(t=await y("G1",5,15,"betaTauG1",a),!t)return!1}else a&&a.warn('this file does not contain phase2 precalculated values. Please run: \n snarkjs "powersoftau preparephase2" to prepare this file to be used in the phase2 ceremony.');return await o.close(),a&&a.info("Powers of Tau Ok!"),!0;function b(t,e){if(!a)return;a.info("-----------------------------------------------------"),a.info(`Contribution #${t.id}: ${t.name||""}`),a.info(Rl(t.nextChallenge,"Next Challenge: "));const o=new Uint8Array(2*n.G1.F.n8*6+2*n.G2.F.n8*3);es(o,0,n,t.key,!1);const i=nl.exports(64);i.setPartialHash(t.partialHash),i.update(o);const l=i.digest();a.info(Rl(l,"Response Hash:")),a.info(Rl(e.nextChallenge,"Response Hash:")),1==t.type&&(a.info(`Beacon generator: ${Yl(t.beaconHash)}`),a.info(`Beacon iterations Exp: ${t.numIterationsExp}`))}async function w(t,a,e,l,c,s){const r=n[a],d=2*r.F.n8;await Qe(o,i,t);const u=[];let g=r.zero,f=r.zero,p=r.zero;for(let t=0;t0){const t=r.fromRprLEM(i,0),a=Kl(jl(4),0);g=r.add(g,r.timesScalar(p,a)),f=r.add(f,r.timesScalar(t,a))}const m=await r.multiExpAffine(i.slice(0,(a-1)*d),h),L=await r.multiExpAffine(i.slice(d),h);g=r.add(g,m),f=r.add(f,L),p=r.fromRprLEM(i,(a-1)*d);for(let e=0;e=t&&o1;)r/=2,d+=1;if(2**d!=s)throw new Error("Invalid file size");i&&i.debug("Power to tau size: "+d);const u=await Hl(o),_=await qe(e),g=nl.exports(64);for(let t=0;t{i.debug(a+".g1_s: "+t.G1.toString(h[a].g1_s,16)),i.debug(a+".g1_sx: "+t.G1.toString(h[a].g1_sx,16)),i.debug(a+".g2_sp: "+t.G2.toString(h[a].g2_sp,16)),i.debug(a+".g2_spx: "+t.G2.toString(h[a].g2_spx,16)),i.debug("")}));const m=nl.exports(64);await _.write(p),m.update(p),await _s(n,_,m,t,"G1",2**d*2-1,t.Fr.one,h.tau.prvKey,"COMPRESSED","tauG1",i),await _s(n,_,m,t,"G2",2**d,t.Fr.one,h.tau.prvKey,"COMPRESSED","tauG2",i),await _s(n,_,m,t,"G1",2**d,h.alpha.prvKey,h.tau.prvKey,"COMPRESSED","alphaTauG1",i),await _s(n,_,m,t,"G1",2**d,h.beta.prvKey,h.tau.prvKey,"COMPRESSED","betaTauG1",i),await _s(n,_,m,t,"G2",1,h.beta.prvKey,h.tau.prvKey,"COMPRESSED","betaTauG2",i);const L=new Uint8Array(2*t.F1.n8*6+2*t.F2.n8*3);es(L,0,t,h,!1),await _.write(L),m.update(L);const b=m.digest();i&&i.info(Rl(b,"Contribution Response Hash: ")),await _.close(),await n.close()},beacon:async function(t,a,e,o,i,n){const l=Wl(o);if(0==l.byteLength||2*l.byteLength!=o.length)return n&&n.error("Invalid Beacon Hash. (It must be a valid hexadecimal sequence)"),!1;if(l.length>=256)return n&&n.error("Maximum lenght of beacon hash is 255 bytes"),!1;if((i=parseInt(i))<10||i>63)return n&&n.error("Invalid numIterationsExp. (Must be between 10 and 63)"),!1;await nl.exports.ready();const{fd:c,sections:s}=await ze(t,"ptau",1),{curve:r,power:d,ceremonyPower:u}=await ts(c,s);if(d!=u)return n&&n.error("This file has been reduced. You cannot contribute into a reduced file."),!1;s[12]&&n&&n.warn("Contributing into a file that has phase2 calculated. You will have to prepare phase2 again.");const _=await is(c,r,s),g={name:e,type:1,numIterationsExp:i,beaconHash:l};let f;f=_.length>0?_[_.length-1].nextChallenge:cs(r,d,n),g.key=await ss(r,f,l,i);const p=new nl.exports(64);p.update(f);const h=await Te(a,"ptau",1,7);await Xc(h,r,d);const m=[];let L;L=await A(2,"G1",2**d*2-1,r.Fr.e(1),g.key.tau.prvKey,"tauG1",n),g.tauG1=L[1],L=await A(3,"G2",2**d,r.Fr.e(1),g.key.tau.prvKey,"tauG2",n),g.tauG2=L[1],L=await A(4,"G1",2**d,g.key.alpha.prvKey,g.key.tau.prvKey,"alphaTauG1",n),g.alphaG1=L[0],L=await A(5,"G1",2**d,g.key.beta.prvKey,g.key.tau.prvKey,"betaTauG1",n),g.betaG1=L[0],L=await A(6,"G2",1,g.key.beta.prvKey,g.key.tau.prvKey,"betaTauG2",n),g.betaG2=L[0],g.partialHash=p.getPartialHash();const b=new Uint8Array(2*r.F1.n8*6+2*r.F2.n8*3);es(b,0,r,g.key,!1),p.update(new Uint8Array(b));const w=p.digest();n&&n.info(Rl(w,"Contribution Response Hash imported: "));const y=new nl.exports(64);return y.update(w),await C(h,"G1",2,2**d*2-1,"tauG1",n),await C(h,"G2",3,2**d,"tauG2",n),await C(h,"G1",4,2**d,"alphaTauG1",n),await C(h,"G1",5,2**d,"betaTauG1",n),await C(h,"G2",6,1,"betaG2",n),g.nextChallenge=y.digest(),n&&n.info(Rl(g.nextChallenge,"Next Challenge Hash: ")),_.push(g),await ls(h,r,_),await c.close(),await h.close(),w;async function A(t,a,e,o,i,n,l){const d=[];c.pos=s[t][0].p,await Me(h,t),m[t]=h.pos;const u=r[a],_=2*u.F.n8,g=Math.floor((1<<20)/_);let f=o;for(let t=0;t0?d[d.length-1].nextChallenge:cs(c,s,i),u.key=Jc(c,_,g);const f=new nl.exports(64);f.update(_);const p=await Te(a,"ptau",1,7);await Xc(p,c,s);const h=[];let m;m=await y(2,"G1",2**s*2-1,c.Fr.e(1),u.key.tau.prvKey,"tauG1"),u.tauG1=m[1],m=await y(3,"G2",2**s,c.Fr.e(1),u.key.tau.prvKey,"tauG2"),u.tauG2=m[1],m=await y(4,"G1",2**s,u.key.alpha.prvKey,u.key.tau.prvKey,"alphaTauG1"),u.alphaG1=m[0],m=await y(5,"G1",2**s,u.key.beta.prvKey,u.key.tau.prvKey,"betaTauG1"),u.betaG1=m[0],m=await y(6,"G2",1,u.key.beta.prvKey,u.key.tau.prvKey,"betaTauG2"),u.betaG2=m[0],u.partialHash=f.getPartialHash();const L=new Uint8Array(2*c.F1.n8*6+2*c.F2.n8*3);es(L,0,c,u.key,!1),f.update(new Uint8Array(L));const b=f.digest();i&&i.info(Rl(b,"Contribution Response Hash imported: "));const w=new nl.exports(64);return w.update(b),await A(p,"G1",2,2**s*2-1,"tauG1"),await A(p,"G2",3,2**s,"tauG2"),await A(p,"G1",4,2**s,"alphaTauG1"),await A(p,"G1",5,2**s,"betaTauG1"),await A(p,"G2",6,1,"betaG2"),u.nextChallenge=w.digest(),i&&i.info(Rl(u.nextChallenge,"Next Challenge Hash: ")),d.push(u),await ls(p,c,d),await n.close(),await p.close(),b;async function y(t,a,e,o,s,r){const d=[];n.pos=l[t][0].p,await Me(p,t),h[t]=p.pos;const u=c[a],_=2*u.F.n8,g=Math.floor((1<<20)/_);let m=o;for(let t=0;t=this.length&&(this.length=t+1),!0}getKeys(){const t=new ms;for(let a=0;a1<<20?new ms:[];for(let t=0;t1<<20?new ms:[];for(let t=0;t1<<20?new ms:[];for(let t=0;t{let o="";return Object.keys(e).forEach((i=>{let n=a.varIdx2Name[i];"one"==n&&(n="1");let l=t.curve.Fr.toString(e[i]);"1"==l&&(l=""),"-1"==l&&(l="-"),""!=o&&"-"!=l[0]&&(l="+"+l),""!=o&&(l=" "+l),o=o+l+n})),o},n=`[ ${i(o[0])} ] * [ ${i(o[1])} ] - [ ${i(o[2])} ] = 0`;e&&e.info(n)}},info:async function(t,a){const e=await As(t);return Yn.eq(e.prime,xs)?a&&a.info("Curve: bn-128"):Yn.eq(e.prime,Cs)?a&&a.info("Curve: bls12-381"):a&&a.info(`Unknown Curve. Prime: ${Yn.toString(e.prime)}`),a&&a.info(`# of Wires: ${e.nVars}`),a&&a.info(`# of Constraints: ${e.nConstraints}`),a&&a.info(`# of Private Inputs: ${e.nPrvInputs}`),a&&a.info(`# of Public Inputs: ${e.nPubInputs}`),a&&a.info(`# of Labels: ${e.nLabels}`),a&&a.info(`# of Outputs: ${e.nOutputs}`),e},exportJson:async function(t,a){const e=await As(t,!0,!0,!0,a),o=e.curve.Fr;return delete e.curve,delete e.F,Jl(o,e)}});async function Is(t){const a={labelIdx2Name:["one"],varIdx2Name:["one"],componentIdx2Name:[]},e=await Ge(t),o=await e.read(e.totalSize),i=new TextDecoder("utf-8").decode(o).split("\n");for(let t=0;t Reading r1cs file");const{fd:o,sections:i}=await ze(t,"r1cs",1),n=await ys(o,i,{loadConstraints:!1,loadCustomGates:!1});e&&e.info("> Reading witness file");const{fd:l,sections:c}=await ze(a,"wtns",2),s=await qc(l,c);if(!Yn.eq(n.prime,s.q))throw new Error("Curve of the witness does not match the curve of the proving key");const r=await $e(l,c,2);await l.close();const d=(await async function(t){let a;if(Yn.eq(t,tl))a=await Zn();else{if(!Yn.eq(t,Xn))throw new Error(`Curve not supported: ${Yn.toString(t)}`);a=await Wn()}return a}(n.prime)).Fr,u=d.n8,_=await $e(o,i,2);e&&(e.info("----------------------------"),e.info(" WITNESS CHECK"),e.info(` Curve: ${n.curve.name}`),e.info(` Vars (wires): ${n.nVars}`),e.info(` Ouputs: ${n.nOutputs}`),e.info(` Public Inputs: ${n.nPubInputs}`),e.info(` Private Inputs: ${n.nPrvInputs}`),e.info(` Labels: ${n.nLabels}`),e.info(` Constraints: ${n.nConstraints}`),e.info(` Custom Gates: ${n.useCustomGates}`),e.info("----------------------------")),e&&e.info("> Checking witness correctness");let g=0,f=!0;for(let t=0;t{const o=function(t){return d.fromRprLE(r.slice(t*u,t*u+u))}(e),i=t[e];a=d.add(a,d.mul(o,i))})),a}function h(){const t={},a=_.slice(g,g+4);g+=4;const e=new DataView(a.buffer).getUint32(0,!0),o=_.slice(g,g+(4+n.n8)*e);g+=(4+n.n8)*e;const i=new DataView(o.buffer);for(let a=0;a=this.length&&(this.length=t+1),!0}getKeys(){const t=new Os;for(let a=0;a_)return o&&o.error(`circuit too big for this power of tau ceremony. ${p.nConstraints}*2 > 2**${_}`),-1;if(!d[12])return o&&o.error("Powers of tau is not prepared."),-1;const w=p.nOutputs+p.nPubInputs,y=2**b;await Me(h,1),await h.writeULE32(1),await Ue(h),await Me(h,2);const A=u.q,C=8*(Math.floor((Yn.bitLength(A)-1)/64)+1),x=u.r,F=8*(Math.floor((Yn.bitLength(x)-1)/64)+1),I=Yn.mod(Yn.shl(1,8*F),x),B=u.Fr.e(Yn.mod(Yn.mul(I,I),x));let E,v,S;await h.writeULE32(C),await Re(h,A,C),await h.writeULE32(F),await Re(h,x,F),await h.writeULE32(p.nVars),await h.writeULE32(w),await h.writeULE32(y),E=await r.read(m,d[4][0].p),await h.write(E),E=await u.G1.batchLEMtoU(E),s.update(E),v=await r.read(m,d[5][0].p),await h.write(v),v=await u.G1.batchLEMtoU(v),s.update(v),S=await r.read(L,d[6][0].p),await h.write(S),S=await u.G2.batchLEMtoU(S),s.update(S);const P=new Uint8Array(m);u.G1.toRprLEM(P,0,u.G1.g);const O=new Uint8Array(L);u.G2.toRprLEM(O,0,u.G2.g);const q=new Uint8Array(m);u.G1.toRprUncompressed(q,0,u.G1.g);const G=new Uint8Array(L);u.G2.toRprUncompressed(G,0,u.G2.g),await h.write(O),await h.write(P),await h.write(O),s.update(G),s.update(q),s.update(G),await Ue(h),o&&o.info("Reading r1cs");let z=await $e(g,f,2);const T=new Os(p.nVars),M=new Os(p.nVars),U=new Os(p.nVars),Q=new Os(p.nVars-w-1),k=new Array(w+1);o&&o.info("Reading tauG1");let R=await $e(r,d,12,(y-1)*m,y*m);o&&o.info("Reading tauG2");let N=await $e(r,d,13,(y-1)*L,y*L);o&&o.info("Reading alphatauG1");let D=await $e(r,d,14,(y-1)*m,y*m);o&&o.info("Reading betatauG1");let $=await $e(r,d,15,(y-1)*m,y*m);await async function(){const t=new Uint8Array(12+u.Fr.n8),a=new DataView(t.buffer),e=new Uint8Array(u.Fr.n8);u.Fr.toRprLE(e,0,u.Fr.e(1));let s=0;function r(){const t=z.slice(s,s+4);s+=4;return new DataView(t.buffer).getUint32(0,!0)}const d=new Os;for(let t=0;t=0?u.Fr.fromRprLE(z.slice(o[3],o[3]+u.Fr.n8),0):u.Fr.fromRprLE(e,0);const n=u.Fr.mul(i,B);u.Fr.toRprLE(t,12,n),_.set(t,f),f+=t.length}await h.write(_),await Ue(h)}(),await V(3,"G1",k,"IC"),await async function(){await Me(h,9);const t=new Cn(y*m);if(b(o&&o.debug(`Writing points end ${i}: ${d}/${e.length}`),t)))),c+=n,t++}const r=await Promise.all(l);for(let t=0;t32768?(g=new Cn(p*n),f=new Cn(p*u.Fr.n8)):(g=new Uint8Array(p*n),f=new Uint8Array(p*u.Fr.n8));let h=0,m=0;const L=[R,N,D,$],b=new Uint8Array(u.Fr.n8);u.Fr.toRprLE(b,0,u.Fr.e(1));let w=0;for(let t=0;t=0?f.set(z.slice(a[t][i][2],a[t][i][2]+u.Fr.n8),w*u.Fr.n8):f.set(b,w*u.Fr.n8),w++;if(a.length>1){const t=[];t.push({cmd:"ALLOCSET",var:0,buff:g}),t.push({cmd:"ALLOCSET",var:1,buff:f}),t.push({cmd:"ALLOC",var:2,len:a.length*l}),h=0,m=0;let e=0;for(let o=0;o=0;t--){const a=d.contributions[t];o&&o.info("-------------------------"),o&&o.info(Rl(a.contributionHash,`contribution #${t+1} ${a.name?a.name:""}:`)),1==a.type&&(o&&o.info(`Beacon generator: ${Yl(a.beaconHash)}`),o&&o.info(`Beacon iterations Exp: ${a.numIterationsExp}`))}return o&&o.info("-------------------------"),o&&o.info("ZKey Ok!"),!0;async function L(t,a){const e=2*s.G1.F.n8,o=t.byteLength/e,i=s.tm.concurrency,n=Math.floor(o/i),l=[];for(let e=0;e Detected protocol: "+i.protocol),"groth16"===i.protocol)n=await async function(t,a,e){const o=await ol(t.q),i=2*o.G1.F.n8,n=await o.pairing(t.vk_alpha_1,t.vk_beta_2);let l={protocol:t.protocol,curve:o.name,nPublic:t.nPublic,vk_alpha_1:o.G1.toObject(t.vk_alpha_1),vk_beta_2:o.G2.toObject(t.vk_beta_2),vk_gamma_2:o.G2.toObject(t.vk_gamma_2),vk_delta_2:o.G2.toObject(t.vk_delta_2),vk_alphabeta_12:o.Gt.toObject(n)};await Qe(a,e,3),l.IC=[];for(let e=0;e<=t.nPublic;e++){const t=await a.read(i),e=o.G1.toObject(t);l.IC.push(e)}return await ke(a),l=Ts(l),l}(i,e,o);else if("plonk"===i.protocol)n=await async function(t){const a=await ol(t.q);let e={protocol:t.protocol,curve:a.name,nPublic:t.nPublic,power:t.power,k1:a.Fr.toObject(t.k1),k2:a.Fr.toObject(t.k2),Qm:a.G1.toObject(t.Qm),Ql:a.G1.toObject(t.Ql),Qr:a.G1.toObject(t.Qr),Qo:a.G1.toObject(t.Qo),Qc:a.G1.toObject(t.Qc),S1:a.G1.toObject(t.S1),S2:a.G1.toObject(t.S2),S3:a.G1.toObject(t.S3),X_2:a.G2.toObject(t.X_2),w:a.Fr.toObject(a.Fr.w[t.power])};return e=Ts(e),e}(i);else{if(!i.protocolId||i.protocolId!==ec)throw new Error("zkey file protocol unrecognized");n=await async function(t,a){const e=await ol(t.q);let o={protocol:t.protocol,curve:e.name,nPublic:t.nPublic,power:t.power,k1:e.Fr.toObject(t.k1),k2:e.Fr.toObject(t.k2),w:e.Fr.toObject(e.Fr.w[t.power]),w3:e.Fr.toObject(t.w3),w4:e.Fr.toObject(t.w4),w8:e.Fr.toObject(t.w8),wr:e.Fr.toObject(t.wr),X_2:e.G2.toObject(t.X_2),C0:e.G1.toObject(t.C0)};return Ts(o)}(i)}return await e.close(),a&&a.info("EXPORT VERIFICATION KEY FINISHED"),n}var Us={};const{unstringifyBigInts:Qs,stringifyBigInts:ks}=Jn;async function Rs(t,a,e){e&&e.info("FFLONK EXPORT SOLIDITY VERIFIER STARTED");const o=await il(t.curve);let i=r(t.w3);t.w3_2=d(o.Fr.square(i));let n=r(t.w4);t.w4_2=d(o.Fr.square(n)),t.w4_3=d(o.Fr.mul(o.Fr.square(n),n));let l=r(t.w8),c=o.Fr.one;for(let a=1;a<8;a++)c=o.Fr.mul(c,l),t["w8_"+a]=d(c);let s=a[t.protocol];return e&&e.info("FFLONK EXPORT SOLIDITY VERIFIER FINISHED"),Us.render(s,t);function r(t){const a=Qs(t);return o.Fr.fromObject(a)}function d(t){const a=o.Fr.toObject(t);return ks(a)}}var Ns=Object.freeze({__proto__:null,newZKey:qs,exportBellman:async function(t,a,e){const{fd:o,sections:i}=await ze(t,"zkey",2),n=await Ic(o,i);if("groth16"!=n.protocol)throw new Error("zkey file is not groth16");const l=await ol(n.q),c=2*l.G1.F.n8,s=2*l.G2.F.n8,r=await Ec(o,l,i),d=await qe(a);let u;await L(n.vk_alpha_1),await L(n.vk_beta_1),await b(n.vk_beta_2),await b(n.vk_gamma_2),await L(n.vk_delta_1),await b(n.vk_delta_2),u=await $e(o,i,3),u=await l.G1.batchLEMtoU(u),await w("G1",u);const _=await $e(o,i,9);let g,f,p,h,m;g=await l.G1.fft(_,"affine","jacobian",e),g=await l.G1.batchApplyKey(g,l.Fr.neg(l.Fr.e(2)),l.Fr.w[n.power+1],"jacobian","affine",e),g=g.slice(0,g.byteLength-c),g=await l.G1.batchLEMtoU(g),await w("G1",g),f=await $e(o,i,8),f=await l.G1.batchLEMtoU(f),await w("G1",f),p=await $e(o,i,5),p=await l.G1.batchLEMtoU(p),await w("G1",p),h=await $e(o,i,6),h=await l.G1.batchLEMtoU(h),await w("G1",h),m=await $e(o,i,7),m=await l.G2.batchLEMtoU(m),await w("G2",m),await d.write(r.csHash),await async function(t){const a=new Uint8Array(4);new DataView(a.buffer,a.byteOffset,a.byteLength).setUint32(0,t,!1),await d.write(a)}(r.contributions.length);for(let t=0;t_.contributions.length)return i&&i.error("The impoerted file does not include new contributions"),!1;for(let t=0;t=256)return n&&n.error("Maximum lenght of beacon hash is 255 bytes"),!1;if((i=parseInt(i))<10||i>63)return n&&n.error("Invalid numIterationsExp. (Must be between 10 and 63)"),!1;const{fd:c,sections:s}=await ze(t,"zkey",2),r=await Ic(c,s);if("groth16"!=r.protocol)throw new Error("zkey file is not groth16");const d=await ol(r.q),u=await Ec(c,d,s),_=await Te(a,"zkey",1,10),g=await Zl(l,i),f=nl.exports(64);f.update(u.csHash);for(let t=0;t{const o=this.curve.G1.toObject(this.polynomials[e]);t?a.polynomials[e]=o:a[e]=o})),Object.keys(this.evaluations).forEach((e=>{const o=this.curve.Fr.toObject(this.evaluations[e]);t?a.evaluations[e]=o:a[e]=o})),a}fromObjectProof(t){this.resetProof(),Object.keys(t.polynomials).forEach((a=>{this.polynomials[a]=this.curve.G1.fromObject(t.polynomials[a])})),Object.keys(t.evaluations).forEach((a=>{this.evaluations[a]=this.curve.Fr.fromObject(t.evaluations[a])}))}}var $s,js={exports:{}}; /** * [js-sha3]{@link https://github.com/emn178/js-sha3} * @@ -6,4 +6,4 @@ var snarkjs=function(t){"use strict";const e=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4];f * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2015-2018 * @license MIT - */$n=Vn,function(){var t="input is invalid type",e="object"==typeof window,a=e?window:{};a.JS_SHA3_NO_WINDOW&&(e=!1);var i=!e&&"object"==typeof self;!a.JS_SHA3_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?a=K:i&&(a=self);var o=!a.JS_SHA3_NO_COMMON_JS&&$n.exports,n=!a.JS_SHA3_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,l="0123456789abcdef".split(""),r=[4,1024,262144,67108864],s=[0,8,16,24],c=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],d=[224,256,384,512],u=[128,256],g=["hex","buffer","arrayBuffer","array","digest"],f={128:168,256:136};!a.JS_SHA3_NO_NODE_JS&&Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),!n||!a.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView||(ArrayBuffer.isView=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});for(var h=function(t,e,a){return function(i){return new B(t,e,t).update(i)[a]()}},_=function(t,e,a){return function(i,o){return new B(t,e,o).update(i)[a]()}},p=function(t,e,a){return function(e,i,o,n){return A["cshake"+t].update(e,i,o,n)[a]()}},m=function(t,e,a){return function(e,i,o,n){return A["kmac"+t].update(e,i,o,n)[a]()}},w=function(t,e,a,i){for(var o=0;o>5,this.byteCount=this.blockCount<<2,this.outputBlocks=a>>5,this.extraBytes=(31&a)>>3;for(var i=0;i<50;++i)this.s[i]=0}function S(t,e,a){B.call(this,t,e,a)}B.prototype.update=function(e){if(this.finalized)throw new Error("finalize already called");var a,i=typeof e;if("string"!==i){if("object"!==i)throw new Error(t);if(null===e)throw new Error(t);if(n&&e.constructor===ArrayBuffer)e=new Uint8Array(e);else if(!(Array.isArray(e)||n&&ArrayBuffer.isView(e)))throw new Error(t);a=!0}for(var o,l,r=this.blocks,c=this.byteCount,d=e.length,u=this.blockCount,g=0,f=this.s;g>2]|=e[g]<>2]|=l<>2]|=(192|l>>6)<>2]|=(128|63&l)<=57344?(r[o>>2]|=(224|l>>12)<>2]|=(128|l>>6&63)<>2]|=(128|63&l)<>2]|=(240|l>>18)<>2]|=(128|l>>12&63)<>2]|=(128|l>>6&63)<>2]|=(128|63&l)<=c){for(this.start=o-c,this.block=r[u],o=0;o>=8);a>0;)o.unshift(a),a=255&(t>>=8),++i;return e?o.push(i):o.unshift(i),this.update(o),o.length},B.prototype.encodeString=function(e){var a,i=typeof e;if("string"!==i){if("object"!==i)throw new Error(t);if(null===e)throw new Error(t);if(n&&e.constructor===ArrayBuffer)e=new Uint8Array(e);else if(!(Array.isArray(e)||n&&ArrayBuffer.isView(e)))throw new Error(t);a=!0}var o=0,l=e.length;if(a)o=l;else for(var r=0;r=57344?o+=3:(s=65536+((1023&s)<<10|1023&e.charCodeAt(++r)),o+=4)}return o+=this.encode(8*o),this.update(e),o},B.prototype.bytepad=function(t,e){for(var a=this.encode(e),i=0;i>2]|=this.padding[3&e],this.lastByteIndex===this.byteCount)for(t[0]=t[a],e=1;e>4&15]+l[15&t]+l[t>>12&15]+l[t>>8&15]+l[t>>20&15]+l[t>>16&15]+l[t>>28&15]+l[t>>24&15];r%e==0&&(P(a),n=0)}return o&&(t=a[n],s+=l[t>>4&15]+l[15&t],o>1&&(s+=l[t>>12&15]+l[t>>8&15]),o>2&&(s+=l[t>>20&15]+l[t>>16&15])),s},B.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,a=this.s,i=this.outputBlocks,o=this.extraBytes,n=0,l=0,r=this.outputBits>>3;t=o?new ArrayBuffer(i+1<<2):new ArrayBuffer(r);for(var s=new Uint32Array(t);l>8&255,s[t+2]=e>>16&255,s[t+3]=e>>24&255;r%a==0&&P(i)}return n&&(t=r<<2,e=i[l],s[t]=255&e,n>1&&(s[t+1]=e>>8&255),n>2&&(s[t+2]=e>>16&255)),s},S.prototype=new B,S.prototype.finalize=function(){return this.encode(this.outputBits,!0),B.prototype.finalize.call(this)};var P=function(t){var e,a,i,o,n,l,r,s,d,u,g,f,h,_,p,m,w,L,b,A,y,C,I,F,x,E,v,B,S,P,G,O,T,z,U,Q,q,M,k,R,D,N,$,V,K,j,H,Z,W,Y,J,X,tt,et,at,it,ot,nt,lt,rt,st,ct,dt;for(i=0;i<48;i+=2)o=t[0]^t[10]^t[20]^t[30]^t[40],n=t[1]^t[11]^t[21]^t[31]^t[41],l=t[2]^t[12]^t[22]^t[32]^t[42],r=t[3]^t[13]^t[23]^t[33]^t[43],s=t[4]^t[14]^t[24]^t[34]^t[44],d=t[5]^t[15]^t[25]^t[35]^t[45],u=t[6]^t[16]^t[26]^t[36]^t[46],g=t[7]^t[17]^t[27]^t[37]^t[47],e=(f=t[8]^t[18]^t[28]^t[38]^t[48])^(l<<1|r>>>31),a=(h=t[9]^t[19]^t[29]^t[39]^t[49])^(r<<1|l>>>31),t[0]^=e,t[1]^=a,t[10]^=e,t[11]^=a,t[20]^=e,t[21]^=a,t[30]^=e,t[31]^=a,t[40]^=e,t[41]^=a,e=o^(s<<1|d>>>31),a=n^(d<<1|s>>>31),t[2]^=e,t[3]^=a,t[12]^=e,t[13]^=a,t[22]^=e,t[23]^=a,t[32]^=e,t[33]^=a,t[42]^=e,t[43]^=a,e=l^(u<<1|g>>>31),a=r^(g<<1|u>>>31),t[4]^=e,t[5]^=a,t[14]^=e,t[15]^=a,t[24]^=e,t[25]^=a,t[34]^=e,t[35]^=a,t[44]^=e,t[45]^=a,e=s^(f<<1|h>>>31),a=d^(h<<1|f>>>31),t[6]^=e,t[7]^=a,t[16]^=e,t[17]^=a,t[26]^=e,t[27]^=a,t[36]^=e,t[37]^=a,t[46]^=e,t[47]^=a,e=u^(o<<1|n>>>31),a=g^(n<<1|o>>>31),t[8]^=e,t[9]^=a,t[18]^=e,t[19]^=a,t[28]^=e,t[29]^=a,t[38]^=e,t[39]^=a,t[48]^=e,t[49]^=a,_=t[0],p=t[1],j=t[11]<<4|t[10]>>>28,H=t[10]<<4|t[11]>>>28,B=t[20]<<3|t[21]>>>29,S=t[21]<<3|t[20]>>>29,rt=t[31]<<9|t[30]>>>23,st=t[30]<<9|t[31]>>>23,N=t[40]<<18|t[41]>>>14,$=t[41]<<18|t[40]>>>14,z=t[2]<<1|t[3]>>>31,U=t[3]<<1|t[2]>>>31,m=t[13]<<12|t[12]>>>20,w=t[12]<<12|t[13]>>>20,Z=t[22]<<10|t[23]>>>22,W=t[23]<<10|t[22]>>>22,P=t[33]<<13|t[32]>>>19,G=t[32]<<13|t[33]>>>19,ct=t[42]<<2|t[43]>>>30,dt=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,at=t[4]<<30|t[5]>>>2,Q=t[14]<<6|t[15]>>>26,q=t[15]<<6|t[14]>>>26,L=t[25]<<11|t[24]>>>21,b=t[24]<<11|t[25]>>>21,Y=t[34]<<15|t[35]>>>17,J=t[35]<<15|t[34]>>>17,O=t[45]<<29|t[44]>>>3,T=t[44]<<29|t[45]>>>3,F=t[6]<<28|t[7]>>>4,x=t[7]<<28|t[6]>>>4,it=t[17]<<23|t[16]>>>9,ot=t[16]<<23|t[17]>>>9,M=t[26]<<25|t[27]>>>7,k=t[27]<<25|t[26]>>>7,A=t[36]<<21|t[37]>>>11,y=t[37]<<21|t[36]>>>11,X=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,V=t[8]<<27|t[9]>>>5,K=t[9]<<27|t[8]>>>5,E=t[18]<<20|t[19]>>>12,v=t[19]<<20|t[18]>>>12,nt=t[29]<<7|t[28]>>>25,lt=t[28]<<7|t[29]>>>25,R=t[38]<<8|t[39]>>>24,D=t[39]<<8|t[38]>>>24,C=t[48]<<14|t[49]>>>18,I=t[49]<<14|t[48]>>>18,t[0]=_^~m&L,t[1]=p^~w&b,t[10]=F^~E&B,t[11]=x^~v&S,t[20]=z^~Q&M,t[21]=U^~q&k,t[30]=V^~j&Z,t[31]=K^~H&W,t[40]=et^~it&nt,t[41]=at^~ot<,t[2]=m^~L&A,t[3]=w^~b&y,t[12]=E^~B&P,t[13]=v^~S&G,t[22]=Q^~M&R,t[23]=q^~k&D,t[32]=j^~Z&Y,t[33]=H^~W&J,t[42]=it^~nt&rt,t[43]=ot^~lt&st,t[4]=L^~A&C,t[5]=b^~y&I,t[14]=B^~P&O,t[15]=S^~G&T,t[24]=M^~R&N,t[25]=k^~D&$,t[34]=Z^~Y&X,t[35]=W^~J&tt,t[44]=nt^~rt&ct,t[45]=lt^~st&dt,t[6]=A^~C&_,t[7]=y^~I&p,t[16]=P^~O&F,t[17]=G^~T&x,t[26]=R^~N&z,t[27]=D^~$&U,t[36]=Y^~X&V,t[37]=J^~tt&K,t[46]=rt^~ct&et,t[47]=st^~dt&at,t[8]=C^~_&m,t[9]=I^~p&w,t[18]=O^~F&E,t[19]=T^~x&v,t[28]=N^~z&Q,t[29]=$^~U&q,t[38]=X^~V&j,t[39]=tt^~K&H,t[48]=ct^~et&it,t[49]=dt^~at&ot,t[0]^=c[i],t[1]^=c[i+1]};if(o)$n.exports=A;else for(C=0;C0===a.type?t++:e++));let a=new Uint8Array(e*this.Fr.n8+t*this.G1.F.n8*2),i=0;for(let t=0;t32768?new Ge(t.length*i.n8):new Uint8Array(t.length*i.n8);for(let e=0;e32768?new Ge(i*o.n8):new Uint8Array(i*o.n8);return n.set(t.coef.slice(),0),new sl(n,e,a)}isEqual(t){const e=this.degree();if(e!==t.degree())return!1;for(let a=0;a32768?new Ge((this.length()+t.length)*this.Fr.n8):new Uint8Array((this.length()+t.length)*this.Fr.n8);e.set(this.coef,0);for(let a=0;athis.coef.byteLength?this.Fr.zero:this.coef.slice(e,e+this.Fr.n8)}setCoef(t,e){if(t>this.length()-1)throw new Error("Coef index is not available");this.coef.set(e,t*this.Fr.n8)}static async to4T(t,e,a,i){a=a||[];let o=await i.ifft(t);const n=4*e>32768?new Ge(4*e*i.n8):new Uint8Array(4*e*i.n8);n.set(o,0);const l=await i.fft(n);if(0===a.length)return[o,l];const r=e+a.length>32768?new Ge((e+a.length)*i.n8):new Uint8Array((e+a.length)*i.n8);r.set(o,0);for(let t=0;t0;t--){const e=t*this.Fr.n8;if(!this.Fr.eq(this.Fr.zero,this.coef.slice(e,e+this.Fr.n8)))return t}return 0}evaluate(t){let e=this.Fr.zero;for(let a=this.degree()+1;a>0;a--){let i=a*this.Fr.n8;const o=this.coef.slice(i-this.Fr.n8,i);e=this.Fr.add(o,this.Fr.mul(e,t))}return e}fastEvaluate(t){const e=this.Fr;let a=this.degree()+1,i=parseInt(a/3),o=a-3*i,n=[],l=[];l[0]=e.one;for(let a=0;a<3;a++){n[a]=e.zero;for(let r=2===a?i+o:i;r>0;r--)n[a]=e.add(this.getCoef(a*i+r-1),e.mul(n[a],t)),0===a&&(l[0]=e.mul(l[0],t))}for(let t=1;t<3;t++)n[0]=e.add(n[0],e.mul(l[t-1],n[t])),l[t]=e.mul(l[t-1],l[0]);return n[0]}add(t,e){let a=!1;t.length()>this.length()&&(a=!0);const i=this.length(),o=t.length();for(let n=0;nthis.length()&&(a=!0);const i=this.length(),o=t.length();for(let n=0;n32768?new Ge(a*e.n8):new Uint8Array(a*e.n8);let o=new sl(i,this.curve,this.logger);o.coef.set(this.coef.slice(0,(a-1)*e.n8),32),this.mulScalar(e.neg(t)),o.add(this),this.coef=o.coef}byXNSubValue(t,e){const a=this.Fr,i=!(this.length()-t-1>=this.degree())?this.length()+t:this.length(),o=i>32768?new Ge(i*a.n8):new Uint8Array(i*a.n8);let n=new sl(o,this.curve,this.logger);n.coef.set(this.coef.slice(0,32*(this.degree()+1)),32*t),this.mulScalar(e),n.add(this),this.coef=n.coef}divBy(t){const e=this.Fr,a=this.degree(),i=t.degree();let o=new sl(this.coef,this.curve,this.logger);this.coef=this.length()>32768?new Ge(this.length()*e.n8):new Uint8Array(this.length()*e.n8);for(let n=a-i;n>=0;n--){this.setCoef(n,e.div(o.getCoef(n+i),t.getCoef(i)));for(let a=0;a<=i;a++)o.setCoef(n+a,e.sub(o.getCoef(n+a),e.mul(this.getCoef(n),t.getCoef(a))))}return o}divByMonic(t,e){const a=this.Fr;let i=this.degree(),o=this.length()>32768?new Ge(this.length()*a.n8):new Uint8Array(this.length()*a.n8),n=new sl(o,this.curve,this.logger),l=[];for(let e=0;e=0&&!(s<0);s-=r){let i=o;l[i]=a.add(this.getCoef(s+t),a.mul(l[i],e)),n.setCoef(s,l[i])}this.coef=n.coef}divByVanishing(t,e){if(this.degree()32768?new Ge(this.length()*a.n8):new Uint8Array(this.length()*a.n8);for(let o=this.length()-1;o>=t;o--){let n=i.getCoef(o);a.eq(a.zero,n)||(i.setCoef(o,a.zero),i.setCoef(o-t,a.add(i.getCoef(o-t),a.mul(e,n))),this.setCoef(o-t,a.add(this.getCoef(o-t),n)))}return i}divByVanishing2(t,e){if(this.degree()32768?new Ge(this.length()*a.n8):new Uint8Array(this.length()*a.n8);let o=this.length()-t,n=Math.floor(o/3),l=o-2*n;console.log(o),console.log(n+" "+l);for(let o=0;o<3;o++){console.log("> Thread "+o);for(let r=0===o?l:n;r>0;r--){let s=r-1;0!==o&&(s+=(o-1)*n+l);let c=s+t,d=i.getCoef(c);a.eq(a.zero,d)||(i.setCoef(c,a.zero),i.setCoef(s,a.add(i.getCoef(s),a.mul(e,d))),this.setCoef(s,a.add(this.getCoef(s),d)),console.log(s+" <-- "+c))}}return this.print(),i}fastDivByVanishing(t){const e=this.Fr;for(let a=0;a32768?new Ge(this.length()*e.n8):new Uint8Array(this.length()*e.n8),this.curve,this.logger),u=this.coef;this.coef=d.coef,d.coef=u;for(let t=0;t0;t--){let a=t-1,o=a*s+c;h[a]=[];for(let l=0;l32768?new Ge(this.length()*this.Fr.n8):new Uint8Array(this.length()*this.Fr.n8);e.set(this.Fr.zero,(this.length()-1)*this.Fr.n8),e.set(this.coef.slice((this.length()-1)*this.Fr.n8,this.length()*this.Fr.n8),(this.length()-2)*this.Fr.n8);for(let a=this.length()-3;a>=0;a--){let i=a*this.Fr.n8;e.set(this.Fr.add(this.coef.slice(i+this.Fr.n8,i+2*this.Fr.n8),this.Fr.mul(t,e.slice(i+this.Fr.n8,i+2*this.Fr.n8))),a*this.Fr.n8)}if(!this.Fr.eq(this.coef.slice(0,this.Fr.n8),this.Fr.mul(this.Fr.neg(t),e.slice(0,this.Fr.n8))))throw new Error("Polynomial does not divide");this.coef=e}divZh(t,e=4){for(let e=0;et*(e-1)-e&&!this.Fr.isZero(o))throw new Error("Polynomial is not divisible")}return this}divByZerofier(t,e){let a=this.Fr;const i=a.inv(e),o=a.neg(i);let n=a.eq(a.one,o),l=a.eq(a.negone,o);if(!n)for(let e=0;ethis.length()-t-1&&!this.Fr.isZero(s))throw new Error("Polynomial is not divisible")}return this}byX(){const t=this.length()+1>32768?new Ge(this.coef.byteLength+this.Fr.n8):new Uint8Array(this.coef.byteLength+this.Fr.n8);t.set(this.Fr.zero,0),t.set(this.coef,this.Fr.n8),this.coef=t}static async expX(t,e,a=!1){const i=t.Fr;if(e<1)throw new Error("Compute a new polynomial to a zero or negative number is not allowed");if(1===e)return await sl.fromEvaluations(t.coef,curve,t.logger);const o=a?t.degree():t.length()-1,n=o*e+1>32768?new Ge((o*e+1)*i.n8):new Uint8Array((o*e+1)*i.n8);n.set(t.getCoef(0),0);for(let a=1;a<=o;a++){const o=a*i.n8,l=t.getCoef(a);n.set(l,o*e)}return new sl(n,t.curve,t.logger)}split(t,e,a){if(t<1)throw new Error(`Polynomials can't be split in ${t} parts`);if(1===t)return[this];if(0!==a.length&&a.length32768?new Ge(l):new Uint8Array(l);o[e]=new sl(r,this.curve,this.logger);const s=e*i,c=n?this.coef.byteLength:(e+1)*i;if(o[e].coef.set(this.coef.slice(s,c),0),n||o[e].coef.set(a[e],i),0!==e){const t=this.Fr.sub(o[e].coef.slice(0,this.Fr.n8),a[e-1]);o[e].coef.set(t,0)}n&&o[e].truncate()}return o}truncate(){const t=this.degree();if(t+132768?new Ge((t+1)*this.Fr.n8):new Uint8Array((t+1)*this.Fr.n8);e.set(this.coef.slice(0,(t+1)*this.Fr.n8),0),this.coef=e}}static lagrangePolynomialInterpolation(t,e,a){const i=a.Fr;let o=n(0);for(let e=1;e32768?new Ge(t.length*i.n8):new Uint8Array(t.length*i.n8);n=new sl(o,a),n.setCoef(0,i.neg(t[e])),n.setCoef(1,i.one)}else n.byXSubValue(t[e]);let l=n.evaluate(t[o]);l=i.inv(l);const r=i.mul(e[o],l);return n.mulScalar(r),n}}static zerofierPolynomial(t,e){const a=e.Fr;let i=t.length+1>32768?new Ge((t.length+1)*a.n8):new Uint8Array((t.length+1)*a.n8),o=new sl(i,e);o.setCoef(0,a.neg(t[0])),o.setCoef(1,a.one);for(let e=1;e=0;a--){const i=this.getCoef(a);t.eq(t.zero,i)||(t.isNegative(i)?e+=" - ":a!==this.degree()&&(e+=" + "),e+=t.toString(i),a>0&&(e+=a>1?"x^"+a:"x"))}console.log(e)}async multiExponentiation(t,e){const a=this.coef.byteLength/this.Fr.n8,i=t.slice(0,a*this.G1.F.n8*2),o=await this.Fr.batchFromMontgomery(this.coef);let n=await this.G1.multiExpAffine(i,o,this.logger,e);return n=this.G1.toAffine(n),n}}class cl{constructor(t,e,a){this.eval=t,this.curve=e,this.Fr=e.Fr,this.logger=a}static async fromPolynomial(t,e,a,i){const o=new Ge(t.length()*e*a.Fr.n8);o.set(t.coef,0);const n=await a.Fr.fft(o);return new cl(n,a,i)}getEvaluation(t){const e=t*this.Fr.n8;if(e+this.Fr.n8>this.eval.byteLength)throw new Error("Evaluations.getEvaluation() out of bounds");return this.eval.slice(e,e+this.Fr.n8)}length(){let t=this.eval.byteLength/this.Fr.n8;if(t!==Math.floor(this.eval.byteLength/this.Fr.n8))throw new Error("Polynomial evaluations buffer has incorrect size");return 0===t&&this.logger.warn("Polynomial has length zero"),t}}const{stringifyBigInts:dl}=ma;async function ul(t,e,a){const{fd:i,sections:o}=await ka(e,"wtns",2);a&&a.debug("> Reading witness file");const n=await Go(i,o);a&&a.debug("> Reading zkey file");const{fd:l,sections:r}=await ka(t,"zkey",2),s=await Fo(l,r);if("plonk"!=s.protocol)throw new Error("zkey file is not plonk");if(!pa.eq(s.r,n.q))throw new Error("Curve of the witness does not match the curve of the proving key");if(n.nWitness!=s.nVars-s.nAdditions)throw new Error(`Invalid witness length. Circuit: ${s.nVars}, witness: ${n.nWitness}, ${s.nAdditions}`);const c=s.curve,d=c.Fr,u=c.Fr.n8,g=s.domainSize*u;a&&(a.debug("----------------------------"),a.debug(" PLONK PROVE SETTINGS"),a.debug(` Curve: ${c.name}`),a.debug(` Circuit power: ${s.power}`),a.debug(` Domain size: ${s.domainSize}`),a.debug(` Vars: ${s.nVars}`),a.debug(` Public vars: ${s.nPublic}`),a.debug(` Constraints: ${s.nConstraints}`),a.debug(` Additions: ${s.nAdditions}`),a.debug("----------------------------")),a&&a.debug("> Reading witness file data");const f=await Za(i,o,2);f.set(d.zero,0);const h=new Ge(u*s.nAdditions);let _={},p={},m={},w={},L=new Nn(c,a);const b=new Hn(c);a&&a.debug(`> Reading Section ${Wn}. Additions`),await async function(){a&&a.debug("··· Computing additions");const t=await Za(l,r,Wn),e=8+2*u;for(let i=0;i Reading Section ${nl}. Sigma1, Sigma2 & Sigma 3`),a&&a.debug("··· Reading Sigma polynomials "),p.Sigma1=new sl(new Ge(g),c,a),p.Sigma2=new sl(new Ge(g),c,a),p.Sigma3=new sl(new Ge(g),c,a),await l.readToBuffer(p.Sigma1.coef,0,g,r[nl][0].p),await l.readToBuffer(p.Sigma2.coef,0,g,r[nl][0].p+5*g),await l.readToBuffer(p.Sigma3.coef,0,g,r[nl][0].p+10*g),a&&a.debug("··· Reading Sigma evaluations"),m.Sigma1=new cl(new Ge(4*g),c,a),m.Sigma2=new cl(new Ge(4*g),c,a),m.Sigma3=new cl(new Ge(4*g),c,a),await l.readToBuffer(m.Sigma1.eval,0,4*g,r[nl][0].p+g),await l.readToBuffer(m.Sigma2.eval,0,4*g,r[nl][0].p+6*g),await l.readToBuffer(m.Sigma3.eval,0,4*g,r[nl][0].p+11*g),a&&a.debug(`> Reading Section ${rl}. Powers of Tau`);const A=await Za(l,r,rl);let y=[];for(let t=1;t<=s.nPublic;t++){const e=f.slice(t*d.n8,t*d.n8+d.n8);y.push(pa.fromRprLE(e))}a&&a.debug(""),a&&a.debug("> ROUND 1"),await async function(){w.b=[];for(let t=1;t<=11;t++)w.b[t]=c.Fr.random();a&&a.debug("> Computing A, B, C wire polynomials");await async function(){a&&a.debug("··· Reading data from zkey file");_.A=new Ge(g),_.B=new Ge(g),_.C=new Ge(g);const t=await Za(l,r,Yn),e=await Za(l,r,Jn),i=await Za(l,r,Xn);for(let a=0;a=s.domainSize+2)throw new Error("A Polynomial is not well calculated");if(p.B.degree()>=s.domainSize+2)throw new Error("B Polynomial is not well calculated");if(p.C.degree()>=s.domainSize+2)throw new Error("C Polynomial is not well calculated")}(),a&&a.debug("> Computing A, B, C MSM");let t=await p.A.multiExponentiation(A,"A"),e=await p.B.multiExponentiation(A,"B"),i=await p.C.multiExponentiation(A,"C");return L.addPolynomial("A",t),L.addPolynomial("B",e),L.addPolynomial("C",i),0}(),a&&a.debug("> ROUND 2"),await async function(){a&&a.debug("> Computing challenges beta and gamma");b.reset(),b.addPolCommitment(s.Qm),b.addPolCommitment(s.Ql),b.addPolCommitment(s.Qr),b.addPolCommitment(s.Qo),b.addPolCommitment(s.Qc),b.addPolCommitment(s.S1),b.addPolCommitment(s.S2),b.addPolCommitment(s.S3);for(let t=0;t Computing Z polynomial");await async function(){a&&a.debug("··· Computing Z evaluations");let t=new Ge(g),e=new Ge(g);t.set(d.one,0),e.set(d.one,0);let i=d.one;for(let a=0;a=s.domainSize+3)throw new Error("Z Polynomial is not well calculated");delete _.Z}(),a&&a.debug("> Computing Z MSM");let t=await p.Z.multiExponentiation(A,"Z");L.addPolynomial("Z",t)}(),a&&a.debug("> ROUND 3"),await async function(){a&&a.debug("> Computing challenge alpha");b.reset(),b.addScalar(w.beta),b.addScalar(w.gamma),b.addPolCommitment(L.getPolynomial("Z")),w.alpha=b.getChallenge(),w.alpha2=d.square(w.alpha),a&&a.debug("··· challenges.alpha: "+d.toString(w.alpha,16));a&&a.debug("> Computing T polynomial");await async function(){a&&a.debug(`··· Reading sections ${el}, ${al}, ${tl}, ${il}, ${ol}. Q selectors`);m.QL=new cl(new Ge(4*g),c,a),m.QR=new cl(new Ge(4*g),c,a),m.QM=new cl(new Ge(4*g),c,a),m.QO=new cl(new Ge(4*g),c,a),m.QC=new cl(new Ge(4*g),c,a),await l.readToBuffer(m.QL.eval,0,4*g,r[el][0].p+g),await l.readToBuffer(m.QR.eval,0,4*g,r[al][0].p+g),await l.readToBuffer(m.QM.eval,0,4*g,r[tl][0].p+g),await l.readToBuffer(m.QO.eval,0,4*g,r[il][0].p+g),await l.readToBuffer(m.QC.eval,0,4*g,r[ol][0].p+g),m.Lagrange=new cl(new Ge(4*g*s.nPublic),c,a);for(let t=0;t=3*s.domainSize+6)throw new Error("T Polynomial is not well calculated");a&&a.debug("··· Computing T1, T2, T3 polynomials");p.T1=new sl(new Ge((s.domainSize+1)*u),c,a),p.T2=new sl(new Ge((s.domainSize+1)*u),c,a),p.T3=new sl(new Ge((s.domainSize+6)*u),c,a),p.T1.coef.set(p.T.coef.slice(0,g),0),p.T2.coef.set(p.T.coef.slice(g,2*g),0),p.T3.coef.set(p.T.coef.slice(2*g,3*g+6*u),0),p.T1.setCoef(s.domainSize,w.b[10]);const e=d.sub(p.T2.getCoef(0),w.b[10]);p.T2.setCoef(0,e),p.T2.setCoef(s.domainSize,w.b[11]);const i=d.sub(p.T3.getCoef(0),w.b[11]);p.T3.setCoef(0,i)}(),a&&a.debug("> Computing T MSM");let t=await p.T1.multiExponentiation(A,"T1"),e=await p.T2.multiExponentiation(A,"T2"),i=await p.T3.multiExponentiation(A,"T3");L.addPolynomial("T1",t),L.addPolynomial("T2",e),L.addPolynomial("T3",i)}(),a&&a.debug("> ROUND 4"),await async function(){a&&a.debug("> Computing challenge xi");b.reset(),b.addScalar(w.alpha),b.addPolCommitment(L.getPolynomial("T1")),b.addPolCommitment(L.getPolynomial("T2")),b.addPolCommitment(L.getPolynomial("T3")),w.xi=b.getChallenge(),w.xiw=d.mul(w.xi,d.w[s.power]),a&&a.debug("··· challenges.xi: "+d.toString(w.xi,16));L.addEvaluation("eval_a",p.A.evaluate(w.xi)),L.addEvaluation("eval_b",p.B.evaluate(w.xi)),L.addEvaluation("eval_c",p.C.evaluate(w.xi)),L.addEvaluation("eval_s1",p.Sigma1.evaluate(w.xi)),L.addEvaluation("eval_s2",p.Sigma2.evaluate(w.xi)),L.addEvaluation("eval_zw",p.Z.evaluate(w.xiw))}(),a&&a.debug("> ROUND 5"),await async function(){a&&a.debug("> Computing challenge v");b.reset(),b.addScalar(w.xi),b.addScalar(L.getEvaluation("eval_a")),b.addScalar(L.getEvaluation("eval_b")),b.addScalar(L.getEvaluation("eval_c")),b.addScalar(L.getEvaluation("eval_s1")),b.addScalar(L.getEvaluation("eval_s2")),b.addScalar(L.getEvaluation("eval_zw")),w.v=[],w.v[1]=b.getChallenge(),a&&a.debug("··· challenges.v: "+d.toString(w.v[1],16));for(let t=2;t<6;t++)w.v[t]=d.mul(w.v[t-1],w.v[1]);a&&a.debug("> Computing linearisation polynomial R(X)");await async function(){const t=c.Fr;p.QL=new sl(new Ge(g),c,a),p.QR=new sl(new Ge(g),c,a),p.QM=new sl(new Ge(g),c,a),p.QO=new sl(new Ge(g),c,a),p.QC=new sl(new Ge(g),c,a),await l.readToBuffer(p.QL.coef,0,g,r[el][0].p),await l.readToBuffer(p.QR.coef,0,g,r[al][0].p),await l.readToBuffer(p.QM.coef,0,g,r[tl][0].p),await l.readToBuffer(p.QO.coef,0,g,r[il][0].p),await l.readToBuffer(p.QC.coef,0,g,r[ol][0].p),w.xin=w.xi;for(let e=0;e Computing opening proof polynomial Wxi(X) polynomial");p.Wxi=new sl(new Ge(g+6*u),c,a),p.Wxi.add(p.R),p.Wxi.add(p.A,w.v[1]),p.Wxi.add(p.B,w.v[2]),p.Wxi.add(p.C,w.v[3]),p.Wxi.add(p.Sigma1,w.v[4]),p.Wxi.add(p.Sigma2,w.v[5]),p.Wxi.subScalar(d.mul(w.v[1],L.evaluations.eval_a)),p.Wxi.subScalar(d.mul(w.v[2],L.evaluations.eval_b)),p.Wxi.subScalar(d.mul(w.v[3],L.evaluations.eval_c)),p.Wxi.subScalar(d.mul(w.v[4],L.evaluations.eval_s1)),p.Wxi.subScalar(d.mul(w.v[5],L.evaluations.eval_s2)),void p.Wxi.divByZerofier(1,w.xi),a&&a.debug("> Computing opening proof polynomial Wxiw(X) polynomial");(async function(){p.Wxiw=sl.fromPolynomial(p.Z,c,a),p.Wxiw.subScalar(L.evaluations.eval_zw),p.Wxiw.divByZerofier(1,w.xiw)})(),a&&a.debug("> Computing Wxi, Wxiw MSM");let t=await p.Wxi.multiExponentiation(A,"Wxi"),e=await p.Wxiw.multiExponentiation(A,"Wxiw");L.addPolynomial("Wxi",t),L.addPolynomial("Wxiw",e)}(),await l.close(),await i.close();let C=L.toObjectProof(!1);return C.protocol="plonk",C.curve=c.name,a&&a.debug("PLONK PROVER FINISHED"),{proof:dl(C),publicSignals:dl(y)};function I(t,e){const a=t.slice(e,e+4);return new DataView(a.buffer,a.byteOffset,a.byteLength).getUint32(0,!0)}function F(t){return ta;){const e=o.shift(),a=o.shift(),i=e[0],n=a[0],l=w++,r=t.zero,s=t.neg(e[1]),c=t.neg(a[1]),d=t.one,u=t.zero;p.push([i,n,l,r,s,c,d,u]),m.push([i,n,e[1],a[1]]),o.push([l,t.one])}for(let t=0;t0?i.toString():a!=t.zero?"k":"0"}function s(e,a,s){const c=r(e),d=r(a);if("0"===c||"0"===d)i(s),l(s);else if("k"===c){l(o(a,e[0],s))}else if("k"===d){l(o(e,a[0],s))}else!function(e,a,i){const o=n(e,1),l=n(a,1),r=n(i,1),s=o.s[0],c=l.s[0],d=r.s[0],u=t.mul(o.coefs[0],l.coefs[0]),g=t.mul(o.coefs[0],l.k),f=t.mul(o.k,l.coefs[0]),h=t.neg(r.coefs[0]),_=t.sub(t.mul(o.k,l.k),r.k);p.push([s,c,d,u,g,f,h,_])}(e,a,s)}for(let e=1;e<=L;e++){const a=e,i=0,o=0,n=t.zero,l=t.one,r=t.zero,s=t.zero,c=t.zero;p.push([a,i,o,n,l,r,s,c])}for(let t=0;tr)return i&&i.error(`circuit too big for this power of tau ceremony. ${p.length} > 2**${r}`),-1;if(!n[12])return i&&i.error("Powers of tau is not prepared."),-1;const C=new Ge(y*u),I=n[12][0].p+(2**A-1)*u;await o.readToBuffer(C,0,y*u,I);const[F,x]=function(){let t=h.two;for(;a(t,[],A);)h.add(t,h.one);let e=h.add(t,h.one);for(;a(e,[t],A);)h.add(e,h.one);return[t,e];function a(t,e,a){const i=2**a;let o=h.one;for(let n=0;n0?2:this.Fr.isZero(e)?0:1}normalizeLinearCombination(t){const e=Object.keys(t);for(let a=0;ao;){const i=l.shift(),o=l.shift(),n=t.nVars++,r=this.fnGetAdditionConstraint(i[0],o[0],n,this.Fr.neg(i[1]),this.Fr.neg(o[1]),this.Fr.zero,this.Fr.one,this.Fr.zero);e.push(r),a.push([i[0],o[0],i[1],o[1]]),l.push([n,this.Fr.one])}for(let t=0;tthis.n-1)throw new Error("CPolynomial:addPolynomial, cannot add a polynomial to a position greater than n-1");this.polynomials[t]=e}degree(){let t=this.polynomials.map(((t,e)=>void 0===t?0:t.degree()*this.n+e));return Math.max(...t)}getPolynomial(){let t=this.polynomials.map((t=>void 0===t?0:t.degree()));const e=this.degree(),a=2**(qi(e-1)+1),i=this.Fr.n8;let o=new sl(new Ge(a*i),this.curve,this.logger);for(let a=0;a Reading witness file");const{fd:i,sections:o}=await ka(e,"wtns",2),n=await Go(i,o);a&&a.info("> Reading zkey file");const{fd:l,sections:r}=await ka(t,"zkey",2),s=await Fo(l,r);if(s.protocolId!==to)throw new Error("zkey file is not fflonk");if(!pa.eq(s.r,n.q))throw new Error("Curve of the witness does not match the curve of the proving key");if(n.nWitness!==s.nVars-s.nAdditions)throw new Error(`Invalid witness length. Circuit: ${s.nVars}, witness: ${n.nWitness}, ${s.nAdditions}`);const c=s.curve,d=c.Fr,u=c.Fr.n8,g=2*c.G1.F.n8,f=s.domainSize*u;a&&(a.info("----------------------------"),a.info(" FFLONK PROVE SETTINGS"),a.info(` Curve: ${c.name}`),a.info(` Circuit power: ${s.power}`),a.info(` Domain size: ${s.domainSize}`),a.info(` Vars: ${s.nVars}`),a.info(` Public vars: ${s.nPublic}`),a.info(` Constraints: ${s.nConstraints}`),a.info(` Additions: ${s.nAdditions}`),a.info("----------------------------")),a&&a.info("> Reading witness file data");const h=await Za(i,o,2);await i.close(),h.set(d.zero,0);const _=new Ge(s.nAdditions*u);let p={},m={},w={},L={},b={},A={},y=new Nn(c,a);a&&a.info(`> Reading Section ${oo}. Additions`),await async function(){a&&a.info("··· Computing additions");const t=await Za(l,r,oo),e=8+2*u;for(let i=0;i Reading Sections ${ho},${_o},${po}. Sigma1, Sigma2 & Sigma 3`),a&&a.info("··· Reading Sigma polynomials "),m.Sigma1=new sl(new Ge(f),c,a),m.Sigma2=new sl(new Ge(f),c,a),m.Sigma3=new sl(new Ge(f),c,a),await l.readToBuffer(m.Sigma1.coef,0,f,r[ho][0].p),await l.readToBuffer(m.Sigma2.coef,0,f,r[_o][0].p),await l.readToBuffer(m.Sigma3.coef,0,f,r[po][0].p),a&&a.info("··· Reading Sigma evaluations"),w.Sigma1=new cl(new Ge(4*f),c,a),w.Sigma2=new cl(new Ge(4*f),c,a),w.Sigma3=new cl(new Ge(4*f),c,a),await l.readToBuffer(w.Sigma1.eval,0,4*f,r[ho][0].p+f),await l.readToBuffer(w.Sigma2.eval,0,4*f,r[_o][0].p+f),await l.readToBuffer(w.Sigma3.eval,0,4*f,r[po][0].p+f),a&&a.info(`> Reading Section ${wo}. Powers of Tau`);const C=new Ge(16*s.domainSize*g);await l.readToBuffer(C,0,(9*s.domainSize+18)*g,r[wo][0].p),globalThis.gc&&globalThis.gc(),a&&a.info(""),a&&a.info("> ROUND 1"),await async function(){b.b=[];for(let t=1;t<=9;t++)b.b[t]=d.random();a&&a.info("> Computing A, B, C wire polynomials");await async function(){a&&a.info("··· Reading data from zkey file");p.A=new Ge(f),p.B=new Ge(f),p.C=new Ge(f);const t=await Za(l,r,no),e=await Za(l,r,lo),i=await Za(l,r,ro);for(let a=0;a=s.domainSize)throw new Error("A Polynomial is not well calculated");if(m.B.degree()>=s.domainSize)throw new Error("B Polynomial is not well calculated");if(m.C.degree()>=s.domainSize)throw new Error("C Polynomial is not well calculated")}(),a&&a.info("> Computing T0 polynomial");await async function(){a&&a.info(`··· Reading sections ${so}, ${co}, ${uo}, ${go}, ${fo}. Q selectors`);w.QL=new cl(new Ge(4*f),c,a),w.QR=new cl(new Ge(4*f),c,a),w.QM=new cl(new Ge(4*f),c,a),w.QO=new cl(new Ge(4*f),c,a),w.QC=new cl(new Ge(4*f),c,a),await l.readToBuffer(w.QL.eval,0,4*f,r[so][0].p+f),await l.readToBuffer(w.QR.eval,0,4*f,r[co][0].p+f),await l.readToBuffer(w.QM.eval,0,4*f,r[uo][0].p+f),await l.readToBuffer(w.QO.eval,0,4*f,r[go][0].p+f),await l.readToBuffer(w.QC.eval,0,4*f,r[fo][0].p+f);const t=await Za(l,r,mo);w.lagrange1=new cl(t,c,a),p.T0=new Ge(4*f),a&&a.info("··· Computing T0 evaluations");for(let t=0;t<4*s.domainSize;t++){a&&0!==t&&t%1e5==0&&a.info(` T0 evaluation ${t}/${4*s.domainSize}`);const e=w.A.getEvaluation(t),i=w.B.getEvaluation(t),o=w.C.getEvaluation(t),n=w.QL.getEvaluation(t),l=w.QR.getEvaluation(t),r=w.QM.getEvaluation(t),c=w.QO.getEvaluation(t),g=w.QC.getEvaluation(t);let f=d.zero;for(let e=0;e=2*s.domainSize-2)throw new Error(`T0 Polynomial is not well calculated (degree is ${m.T0.degree()} and must be less than ${2*s.domainSize+2}`);delete p.T0}(),a&&a.info("> Computing C1 polynomial");await async function(){let t=new Al(4,c,a);if(t.addPolynomial(0,m.A),t.addPolynomial(1,m.B),t.addPolynomial(2,m.C),t.addPolynomial(3,m.T0),m.C1=t.getPolynomial(),m.C1.degree()>=8*s.domainSize-8)throw new Error("C1 Polynomial is not well calculated")}(),a&&a.info("> Computing C1 multi exponentiation");let t=await m.C1.multiExponentiation(C,"C1");return y.addPolynomial("C1",t),0}(),delete m.T0,delete w.QL,delete w.QR,delete w.QM,delete w.QO,delete w.QC,globalThis.gc&&globalThis.gc(),a&&a.info("> ROUND 2"),await async function(){a&&a.info("> Computing challenges beta and gamma");const t=new Hn(c);t.addPolCommitment(s.C0);for(let e=0;e Computing Z polynomial");await async function(){a&&a.info("··· Computing Z evaluations");let t=new Ge(f),e=new Ge(f);t.set(d.one,0),e.set(d.one,0);let i=d.one;for(let o=0;o=s.domainSize+3)throw new Error("Z Polynomial is not well calculated");delete p.Z}(),a&&a.info("> Computing T1 polynomial");await async function(){a&&a.info("··· Computing T1 evaluations");p.T1=new Ge(2*f),p.T1z=new Ge(2*f);let t=d.one;for(let e=0;e<2*s.domainSize;e++){a&&0!==e&&e%1e5==0&&a.info(` T1 evaluation ${e}/${4*s.domainSize}`);const i=d.square(t),o=w.Z.getEvaluation(2*e),n=d.add(d.add(d.mul(b.b[7],i),d.mul(b.b[8],t)),b.b[9]),l=w.lagrange1.getEvaluation(s.domainSize+2*e);let r=d.mul(d.sub(o,d.one),l),c=d.mul(n,l);p.T1.set(r,e*u),p.T1z.set(c,e*u),t=d.mul(t,d.w[s.power+1])}a&&a.info("··· Computing T1 ifft");m.T1=await sl.fromEvaluations(p.T1,c,a),m.T1.divByZerofier(s.domainSize,d.one),a&&a.info("··· Computing T1z ifft");if(m.T1z=await sl.fromEvaluations(p.T1z,c,a),m.T1.add(m.T1z),m.T1.degree()>=s.domainSize+2)throw new Error("T1 Polynomial is not well calculated");delete p.T1,delete p.T1z,delete m.T1z}(),a&&a.info("> Computing T2 polynomial");await async function(){a&&a.info("··· Computing T2 evaluations");p.T2=new Ge(4*f),p.T2z=new Ge(4*f);let t=d.one;for(let e=0;e<4*s.domainSize;e++){a&&0!==e&&e%1e5==0&&a.info(` T2 evaluation ${e}/${4*s.domainSize}`);const i=d.square(t),o=d.mul(t,d.w[s.power]),n=d.square(o),l=w.A.getEvaluation(e),r=w.B.getEvaluation(e),c=w.C.getEvaluation(e),g=w.Z.getEvaluation(e),f=w.Z.getEvaluation((4*s.domainSize+4+e)%(4*s.domainSize)),h=d.add(d.add(d.mul(b.b[7],i),d.mul(b.b[8],t)),b.b[9]),_=d.add(d.add(d.mul(b.b[7],n),d.mul(b.b[8],o)),b.b[9]),m=w.Sigma1.getEvaluation(e),L=w.Sigma2.getEvaluation(e),A=w.Sigma3.getEvaluation(e),y=d.mul(b.beta,t);let C=d.add(l,y);C=d.add(C,b.gamma);let I=d.add(r,d.mul(y,s.k1));I=d.add(I,b.gamma);let F=d.add(c,d.mul(y,s.k2));F=d.add(F,b.gamma);let x=d.mul(d.mul(d.mul(C,I),F),g),E=d.mul(d.mul(d.mul(C,I),F),h),v=d.add(l,d.mul(b.beta,m));v=d.add(v,b.gamma);let B=d.add(r,d.mul(b.beta,L));B=d.add(B,b.gamma);let S=d.add(c,d.mul(b.beta,A));S=d.add(S,b.gamma);let P=d.mul(d.mul(d.mul(v,B),S),f),G=d.mul(d.mul(d.mul(v,B),S),_),O=d.sub(x,P),T=d.sub(E,G);p.T2.set(O,e*u),p.T2z.set(T,e*u),t=d.mul(t,d.w[s.power+2])}a&&a.info("··· Computing T2 ifft");m.T2=await sl.fromEvaluations(p.T2,c,a),a&&a.info("··· Computing T2 / ZH");m.T2.divByZerofier(s.domainSize,d.one),a&&a.info("··· Computing T2z ifft");if(m.T2z=await sl.fromEvaluations(p.T2z,c,a),m.T2.add(m.T2z),m.T2.degree()>=3*s.domainSize)throw new Error("T2 Polynomial is not well calculated");delete p.T2,delete p.T2z,delete m.T2z}(),a&&a.info("> Computing C2 polynomial");await async function(){let t=new Al(3,c,a);if(t.addPolynomial(0,m.Z),t.addPolynomial(1,m.T1),t.addPolynomial(2,m.T2),m.C2=t.getPolynomial(),m.C2.degree()>=9*s.domainSize)throw new Error("C2 Polynomial is not well calculated")}(),a&&a.info("> Computing C2 multi exponentiation");let e=await m.C2.multiExponentiation(C,"C2");return y.addPolynomial("C2",e),0}(),delete p.A,delete p.B,delete p.C,delete w.A,delete w.B,delete w.C,delete w.Sigma1,delete w.Sigma2,delete w.Sigma3,delete w.lagrange1,delete w.Z,globalThis.gc&&globalThis.gc(),a&&a.info("> ROUND 3"),await async function(){a&&a.info("> Computing challenge xi");const t=new Hn(c);t.addScalar(b.gamma),t.addPolCommitment(y.getPolynomial("C2")),b.xiSeed=t.getChallenge();const e=d.square(b.xiSeed);A.w8=[],A.w8[0]=d.one;for(let t=1;t<8;t++)A.w8[t]=d.mul(A.w8[t-1],s.w8);A.w4=[],A.w4[0]=d.one;for(let t=1;t<4;t++)A.w4[t]=d.mul(A.w4[t-1],s.w4);A.w3=[],A.w3[0]=d.one,A.w3[1]=s.w3,A.w3[2]=d.square(s.w3),A.S0={},A.S0.h0w8=[],A.S0.h0w8[0]=d.mul(e,b.xiSeed);for(let t=1;t<8;t++)A.S0.h0w8[t]=d.mul(A.S0.h0w8[0],A.w8[t]);A.S1={},A.S1.h1w4=[],A.S1.h1w4[0]=d.square(A.S0.h0w8[0]);for(let t=1;t<4;t++)A.S1.h1w4[t]=d.mul(A.S1.h1w4[0],A.w4[t]);A.S2={},A.S2.h2w3=[],A.S2.h2w3[0]=d.mul(A.S1.h1w4[0],e),A.S2.h2w3[1]=d.mul(A.S2.h2w3[0],A.w3[1]),A.S2.h2w3[2]=d.mul(A.S2.h2w3[0],A.w3[2]),A.S2.h3w3=[],A.S2.h3w3[0]=d.mul(A.S2.h2w3[0],s.wr),A.S2.h3w3[1]=d.mul(A.S2.h3w3[0],A.w3[1]),A.S2.h3w3[2]=d.mul(A.S2.h3w3[0],A.w3[2]),b.xi=d.mul(d.square(A.S2.h2w3[0]),A.S2.h2w3[0]),a&&a.info("··· challenges.xi: "+d.toString(b.xi));m.QL=new sl(new Ge(f),c,a),m.QR=new sl(new Ge(f),c,a),m.QM=new sl(new Ge(f),c,a),m.QO=new sl(new Ge(f),c,a),m.QC=new sl(new Ge(f),c,a),await l.readToBuffer(m.QL.coef,0,f,r[so][0].p),await l.readToBuffer(m.QR.coef,0,f,r[co][0].p),await l.readToBuffer(m.QM.coef,0,f,r[uo][0].p),await l.readToBuffer(m.QO.coef,0,f,r[go][0].p),await l.readToBuffer(m.QC.coef,0,f,r[fo][0].p),a&&a.info("··· Computing evaluations");y.addEvaluation("ql",m.QL.evaluate(b.xi)),y.addEvaluation("qr",m.QR.evaluate(b.xi)),y.addEvaluation("qm",m.QM.evaluate(b.xi)),y.addEvaluation("qo",m.QO.evaluate(b.xi)),y.addEvaluation("qc",m.QC.evaluate(b.xi)),y.addEvaluation("s1",m.Sigma1.evaluate(b.xi)),y.addEvaluation("s2",m.Sigma2.evaluate(b.xi)),y.addEvaluation("s3",m.Sigma3.evaluate(b.xi)),y.addEvaluation("a",m.A.evaluate(b.xi)),y.addEvaluation("b",m.B.evaluate(b.xi)),y.addEvaluation("c",m.C.evaluate(b.xi)),y.addEvaluation("z",m.Z.evaluate(b.xi)),b.xiw=d.mul(b.xi,d.w[s.power]),y.addEvaluation("zw",m.Z.evaluate(b.xiw)),y.addEvaluation("t1w",m.T1.evaluate(b.xiw)),y.addEvaluation("t2w",m.T2.evaluate(b.xiw))}(),delete m.A,delete m.B,delete m.C,delete m.Z,delete m.T1,delete m.T2,delete m.Sigma1,delete m.Sigma2,delete m.Sigma3,delete m.QL,delete m.QR,delete m.QM,delete m.QC,delete m.QO,globalThis.gc&&globalThis.gc(),a&&a.info("> ROUND 4"),await async function(){a&&a.info("> Computing challenge alpha");const t=new Hn(c);t.addScalar(b.xiSeed),t.addScalar(y.getEvaluation("ql")),t.addScalar(y.getEvaluation("qr")),t.addScalar(y.getEvaluation("qm")),t.addScalar(y.getEvaluation("qo")),t.addScalar(y.getEvaluation("qc")),t.addScalar(y.getEvaluation("s1")),t.addScalar(y.getEvaluation("s2")),t.addScalar(y.getEvaluation("s3")),t.addScalar(y.getEvaluation("a")),t.addScalar(y.getEvaluation("b")),t.addScalar(y.getEvaluation("c")),t.addScalar(y.getEvaluation("z")),t.addScalar(y.getEvaluation("zw")),t.addScalar(y.getEvaluation("t1w")),t.addScalar(y.getEvaluation("t2w")),b.alpha=t.getChallenge(),a&&a.info("··· challenges.alpha: "+d.toString(b.alpha));a&&a.info("> Reading C0 polynomial");m.C0=new sl(new Ge(8*f),c,a),await l.readToBuffer(m.C0.coef,0,8*f,r[Lo][0].p),a&&a.info("> Computing R0 polynomial");(function(){if(m.R0=sl.lagrangePolynomialInterpolation([A.S0.h0w8[0],A.S0.h0w8[1],A.S0.h0w8[2],A.S0.h0w8[3],A.S0.h0w8[4],A.S0.h0w8[5],A.S0.h0w8[6],A.S0.h0w8[7]],[m.C0.evaluate(A.S0.h0w8[0]),m.C0.evaluate(A.S0.h0w8[1]),m.C0.evaluate(A.S0.h0w8[2]),m.C0.evaluate(A.S0.h0w8[3]),m.C0.evaluate(A.S0.h0w8[4]),m.C0.evaluate(A.S0.h0w8[5]),m.C0.evaluate(A.S0.h0w8[6]),m.C0.evaluate(A.S0.h0w8[7])],c),m.R0.degree()>7)throw new Error("R0 Polynomial is not well calculated")})(),a&&a.info("> Computing R1 polynomial");(function(){if(m.R1=sl.lagrangePolynomialInterpolation([A.S1.h1w4[0],A.S1.h1w4[1],A.S1.h1w4[2],A.S1.h1w4[3]],[m.C1.evaluate(A.S1.h1w4[0]),m.C1.evaluate(A.S1.h1w4[1]),m.C1.evaluate(A.S1.h1w4[2]),m.C1.evaluate(A.S1.h1w4[3])],c),m.R1.degree()>3)throw new Error("R1 Polynomial is not well calculated")})(),a&&a.info("> Computing R2 polynomial");(function(){if(m.R2=sl.lagrangePolynomialInterpolation([A.S2.h2w3[0],A.S2.h2w3[1],A.S2.h2w3[2],A.S2.h3w3[0],A.S2.h3w3[1],A.S2.h3w3[2]],[m.C2.evaluate(A.S2.h2w3[0]),m.C2.evaluate(A.S2.h2w3[1]),m.C2.evaluate(A.S2.h2w3[2]),m.C2.evaluate(A.S2.h3w3[0]),m.C2.evaluate(A.S2.h3w3[1]),m.C2.evaluate(A.S2.h3w3[2])],c),m.R2.degree()>5)throw new Error("R2 Polynomial is not well calculated")})(),a&&a.info("> Computing F polynomial");await async function(){a&&a.info("··· Computing F polynomial");m.F=sl.fromPolynomial(m.C0,c,a),m.F.sub(m.R0),m.F.divByZerofier(8,b.xi);let t=sl.fromPolynomial(m.C1,c,a);t.sub(m.R1),t.mulScalar(b.alpha),t.divByZerofier(4,b.xi);let e=sl.fromPolynomial(m.C2,c,a);if(e.sub(m.R2),e.mulScalar(d.square(b.alpha)),e.divByZerofier(3,b.xi),e.divByZerofier(3,b.xiw),m.F.add(t),m.F.add(e),m.F.degree()>=9*s.domainSize-6)throw new Error("F Polynomial is not well calculated")}(),a&&a.info("> Computing W1 multi exponentiation");let e=await m.F.multiExponentiation(C,"W1");return y.addPolynomial("W1",e),0}(),globalThis.gc&&globalThis.gc(),a&&a.info("> ROUND 5"),await async function(){a&&a.info("> Computing challenge y");const t=new Hn(c);t.addScalar(b.alpha),t.addPolCommitment(y.getPolynomial("W1")),b.y=t.getChallenge(),a&&a.info("··· challenges.y: "+d.toString(b.y));a&&a.info("> Computing L polynomial");await async function(){a&&a.info("··· Computing L polynomial");const t=m.R0.evaluate(b.y),e=m.R1.evaluate(b.y),i=m.R2.evaluate(b.y);let o=d.sub(b.y,A.S0.h0w8[0]);for(let t=1;t<8;t++)o=d.mul(o,d.sub(b.y,A.S0.h0w8[t]));let n=d.sub(b.y,A.S1.h1w4[0]);for(let t=1;t<4;t++)n=d.mul(n,d.sub(b.y,A.S1.h1w4[t]));let l=d.sub(b.y,A.S2.h2w3[0]);for(let t=1;t<3;t++)l=d.mul(l,d.sub(b.y,A.S2.h2w3[t]));for(let t=0;t<3;t++)l=d.mul(l,d.sub(b.y,A.S2.h3w3[t]));let r=d.mul(n,l),u=d.mul(b.alpha,d.mul(o,l)),g=d.mul(d.square(b.alpha),d.mul(o,n));L.denH1=n,L.denH2=l,m.L=sl.fromPolynomial(m.C0,c,a),m.L.subScalar(t),m.L.mulScalar(r);let f=sl.fromPolynomial(m.C1,c,a);f.subScalar(e),f.mulScalar(u);let h=sl.fromPolynomial(m.C2,c,a);h.subScalar(i),h.mulScalar(g),m.L.add(f),m.L.add(h),a&&a.info("> Computing ZT polynomial");await async function(){m.ZT=sl.zerofierPolynomial([A.S0.h0w8[0],A.S0.h0w8[1],A.S0.h0w8[2],A.S0.h0w8[3],A.S0.h0w8[4],A.S0.h0w8[5],A.S0.h0w8[6],A.S0.h0w8[7],A.S1.h1w4[0],A.S1.h1w4[1],A.S1.h1w4[2],A.S1.h1w4[3],A.S2.h2w3[0],A.S2.h2w3[1],A.S2.h2w3[2],A.S2.h3w3[0],A.S2.h3w3[1],A.S2.h3w3[2]],c)}();const _=m.ZT.evaluate(b.y);if(m.F.mulScalar(_),m.L.sub(m.F),m.L.degree()>=9*s.domainSize)throw new Error("L Polynomial is not well calculated");delete p.L}(),a&&a.info("> Computing ZTS2 polynomial");await async function(){m.ZTS2=sl.zerofierPolynomial([A.S1.h1w4[0],A.S1.h1w4[1],A.S1.h1w4[2],A.S1.h1w4[3],A.S2.h2w3[0],A.S2.h2w3[1],A.S2.h2w3[2],A.S2.h3w3[0],A.S2.h3w3[1],A.S2.h3w3[2]],c)}();let e=m.ZTS2.evaluate(b.y);e=d.inv(e),m.L.mulScalar(e);const i=sl.fromCoefficientsArray([d.neg(b.y),d.one],c);a&&a.info("> Computing W' = L / ZTS2 polynomial");const o=m.L.divBy(i);if(o.degree()>0)throw new Error(`Degree of L(X)/(ZTS2(y)(X-y)) remainder is ${o.degree()} and should be 0`);if(m.L.degree()>=9*s.domainSize-1)throw new Error("Degree of L(X)/(ZTS2(y)(X-y)) is not correct");a&&a.info("> Computing W' multi exponentiation");let n=await m.L.multiExponentiation(C,"W2");return y.addPolynomial("W2",n),0}(),delete m.C0,delete m.C1,delete m.C2,delete m.R1,delete m.R2,delete m.F,delete m.L,delete m.ZT,delete m.ZTS2,await l.close(),globalThis.gc&&globalThis.gc(),y.addEvaluation("inv",function(){let t=b.xi;for(let e=0;e Reading PTau file");const{fd:o,sections:n}=await ka(e,"ptau",1);if(!n[12])throw new Error("Powers of Tau is not well prepared. Section 12 missing.");i&&i.info("> Getting curve from PTau settings");const{curve:l}=await Xo(o,n);i&&i.info("> Reading r1cs file");const{fd:r,sections:s}=await ka(t,"r1cs",1),c=await An(r,s,{loadConstraints:!1,loadCustomGates:!0});if(c.prime!==l.r)throw new Error("r1cs curve does not match powers of tau ceremony curve");const d=l.Fr,u=l.Fr.n8,g=2*l.G1.F.n8,f=2*l.G2.F.n8;let h,_={},p={},m={nVars:c.nVars,nPublic:c.nOutputs+c.nPubInputs};const w=new Gn;let L=new Gn;if(i&&i.info("> Processing FFlonk constraints"),await async function(t,e,a){for(let e=0;e computing k1 and k2");const[b,A]=function(){let t=d.two;for(;a(t,[],m.cirPower);)d.add(t,d.one);let e=d.add(t,d.one);for(;a(e,[t],m.cirPower);)d.add(e,d.one);return[t,e];function a(t,e,a){const i=2**a;let o=d.one;for(let n=0;n computing w3");const y=function(){let t=d.e(31624),e=pa.div(3648040478639879203707734290876212514758060733402672390616367364429301415936n,pa.e(3));return d.exp(t,e)}();i&&i.info("> computing w4");const C=d.w[2];i&&i.info("> computing w8");const I=d.w[3];i&&i.info("> computing wr");const F=function(t,e){const a=e.e(467799165886069610036046866799264026481344299079011762026774533774345988080n);return e.exp(a,2**(28-t))}(m.cirPower,l.Fr);return await async function(){i&&i.info("> Writing the zkey file");const t=await Ra(a,"zkey",1,ao,1<<22,1<<24);i&&i.info(`··· Writing Section ${Yi}. Zkey Header`);await async function(t){await Da(t,Yi),await t.writeULE32(to),await Na(t)}(t),i&&i.info(`··· Writing Section ${oo}. Additions`);await async function(t){await Da(t,oo);const e=new Uint8Array(8+2*u),a=new DataView(e.buffer);for(let o=0;o=8*m.domainSize)throw new Error("C0 Polynomial is not well calculated");await Da(t,Lo),await t.write(_.C0.coef),await Na(t)}(t),globalThis.gc&&globalThis.gc();i&&i.info(`··· Writing Section ${io}. FFlonk Header`);await async function(t){await Da(t,io);const e=l.q,a=8*(Math.floor((pa.bitLength(e)-1)/64)+1);await t.writeULE32(a),await Ka(t,e,a);const i=l.r,r=8*(Math.floor((pa.bitLength(i)-1)/64)+1);let s;await t.writeULE32(r),await Ka(t,i,r),await t.writeULE32(m.nVars),await t.writeULE32(m.nPublic),await t.writeULE32(m.domainSize),await t.writeULE32(L.length),await t.writeULE32(w.length),await t.write(b),await t.write(A),await t.write(y),await t.write(C),await t.write(I),await t.write(F),s=await o.read(f,n[3][0].p+f),await t.write(s);let c=await _.C0.multiExponentiation(h,"C0");await t.write(c),await Na(t)}(t),globalThis.gc&&globalThis.gc();i&&i.info("> Writing the zkey file finished");await t.close()}(),await r.close(),await o.close(),i&&i.info("FFLONK SETUP FINISHED"),0;async function x(t,e,a,o){await Da(t,e);for(let e=0;e Checking commitments belong to G1"),!function(t,e,a){const i=t.G1;return i.isValid(e.polynomials.C1)&&i.isValid(e.polynomials.C2)&&i.isValid(e.polynomials.W1)&&i.isValid(e.polynomials.W2)&&i.isValid(a.C0)}(o,l,n))return i&&i.error("Proof commitments are not valid"),!1;if(i&&i.info("> Checking evaluations belong to F"),!function(t,e){return El(t,e.evaluations.ql)&&El(t,e.evaluations.qr)&&El(t,e.evaluations.qm)&&El(t,e.evaluations.qo)&&El(t,e.evaluations.qc)&&El(t,e.evaluations.s1)&&El(t,e.evaluations.s2)&&El(t,e.evaluations.s3)&&El(t,e.evaluations.a)&&El(t,e.evaluations.b)&&El(t,e.evaluations.c)&&El(t,e.evaluations.z)&&El(t,e.evaluations.zw)&&El(t,e.evaluations.t1w)&&El(t,e.evaluations.t2w)}(o,l))return i&&i.error("Proof evaluations are not valid."),!1;if(i&&i.info("> Checking public inputs belong to F"),!function(t,e){for(let a=0;a Computing challenges");const{challenges:c,roots:d}=function(t,e,a,i,o){const n=t.Fr,l={},r={},s=new Hn(t);s.addPolCommitment(a.C0);for(let t=0;t Computing Zero polynomial evaluation Z_H(xi)"),c.zh=s.sub(c.xiN,s.one),c.invzh=s.inv(c.zh),i&&i.info("> Computing Lagrange evaluations");const u=await async function(t,e,a){const i=t.Fr,o=Math.max(1,a.nPublic),n=new Ge(o*i.n8);let l=new Ge(o*i.n8),r=i.one;for(let t=0;t Computing polynomial identities PI(X)");const g=function(t,e,a){const i=t.Fr;let o=i.zero;for(let t=0;t Computing r0(y)");const f=function(t,e,a,i,o){const n=i.Fr,l=vl(a.S0.h0w8,e.y,e.xi,i);o&&o.info("··· Computing r0(y)");let r=n.zero;for(let e=0;e<8;e++){let i=[];i[1]=a.S0.h0w8[e];for(let t=2;t<8;t++)i[t]=n.mul(i[t-1],a.S0.h0w8[e]);let o=n.add(t.evaluations.ql,n.mul(t.evaluations.qr,i[1]));o=n.add(o,n.mul(t.evaluations.qo,i[2])),o=n.add(o,n.mul(t.evaluations.qm,i[3])),o=n.add(o,n.mul(t.evaluations.qc,i[4])),o=n.add(o,n.mul(t.evaluations.s1,i[5])),o=n.add(o,n.mul(t.evaluations.s2,i[6])),o=n.add(o,n.mul(t.evaluations.s3,i[7])),r=n.add(r,n.mul(o,l[e]))}return r}(l,c,d,o,i);i&&i.info("> Computing r1(y)");const h=function(t,e,a,i,o,n){const l=o.Fr,r=vl(a.S1.h1w4,e.y,e.xi,o);n&&n.info("··· Computing T0(xi)");let s=l.mul(t.evaluations.ql,t.evaluations.a);s=l.add(s,l.mul(t.evaluations.qr,t.evaluations.b)),s=l.add(s,l.mul(t.evaluations.qm,l.mul(t.evaluations.a,t.evaluations.b))),s=l.add(s,l.mul(t.evaluations.qo,t.evaluations.c)),s=l.add(s,t.evaluations.qc),s=l.add(s,i),s=l.mul(s,e.invzh),n&&n.info("··· Computing C1(h_1ω_4^i) values");let c=l.zero;for(let e=0;e<4;e++){let i=t.evaluations.a;i=l.add(i,l.mul(a.S1.h1w4[e],t.evaluations.b));const o=l.square(a.S1.h1w4[e]);i=l.add(i,l.mul(o,t.evaluations.c)),i=l.add(i,l.mul(l.mul(o,a.S1.h1w4[e]),s)),c=l.add(c,l.mul(i,r[e]))}return c}(l,c,d,g,o,i);i&&i.info("> Computing r2(y)");const _=function(t,e,a,i,o,n,l){const r=n.Fr,s=function(t,e,a,i,o){const n=o.Fr,l=[],r=t[0].length,s=r*t.length,c=n.exp(e,s),d=n.mul(n.add(a,i),n.exp(e,r)),u=n.mul(a,i),g=n.add(n.sub(c,d),u);let f=n.mul(n.mul(n.e(r),t[0][0]),n.sub(a,i));for(let a=0;a Computing F");const p=function(t,e,a,i,o){const n=t.G1,l=t.Fr;let r=l.sub(i.y,o.S0.h0w8[0]);for(let t=1;t<8;t++)r=l.mul(r,l.sub(i.y,o.S0.h0w8[t]));i.temp=r;let s=l.sub(i.y,o.S1.h1w4[0]);for(let t=1;t<4;t++)s=l.mul(s,l.sub(i.y,o.S1.h1w4[t]));let c=l.sub(i.y,o.S2.h2w3[0]);for(let t=1;t<3;t++)c=l.mul(c,l.sub(i.y,o.S2.h2w3[t]));for(let t=0;t<3;t++)c=l.mul(c,l.sub(i.y,o.S2.h3w3[t]));i.quotient1=l.mul(i.alpha,l.div(r,s)),i.quotient2=l.mul(l.square(i.alpha),l.div(r,c));let d=n.timesFr(e.polynomials.C1,i.quotient1),u=n.timesFr(e.polynomials.C2,i.quotient2);return n.add(a.C0,n.add(d,u))}(o,l,n,c,d);i&&i.info("> Computing E");const m=function(t,e,a,i,o,n,l){const r=t.G1,s=t.Fr;let c=s.mul(n,a.quotient1),d=s.mul(l,a.quotient2);return r.timesFr(r.one,s.add(o,s.add(c,d)))}(o,0,c,0,f,h,_);i&&i.info("> Computing J");const w=function(t,e,a){const i=t.G1;return i.timesFr(e.polynomials.W1,a.temp)}(o,l,c);i&&i.info("> Validate all evaluations with a pairing");const L=await async function(t,e,a,i,o,n,l){const r=t.G1;let s=r.timesFr(e.polynomials.W2,a.y);s=r.add(r.sub(r.sub(o,n),l),s);const c=t.G2.one,d=e.polynomials.W2,u=i.X_2;return await t.pairingEq(r.neg(s),c,d,u)}(o,l,c,n,p,m,w);return i&&(L?i.info("PROOF VERIFIED SUCCESSFULLY"):i.warn("Invalid Proof")),i&&i.info("FFLONK VERIFIER FINISHED"),L},exportSolidityVerifier:Rn,exportSolidityCallData:async function(t,e){const a=Bl(e),i=Bl(t),o=await ai(a.curve);o.G1,o.Fr;let n="";for(let t=0;t>5,this.byteCount=this.blockCount<<2,this.outputBlocks=e>>5,this.extraBytes=(31&e)>>3;for(var o=0;o<50;++o)this.s[o]=0}function S(t,a,e){v.call(this,t,a,e)}v.prototype.update=function(a){if(this.finalized)throw new Error("finalize already called");var e,o=typeof a;if("string"!==o){if("object"!==o)throw new Error(t);if(null===a)throw new Error(t);if(n&&a.constructor===ArrayBuffer)a=new Uint8Array(a);else if(!(Array.isArray(a)||n&&ArrayBuffer.isView(a)))throw new Error(t);e=!0}for(var i,l,c=this.blocks,r=this.byteCount,d=a.length,u=this.blockCount,_=0,g=this.s;_>2]|=a[_]<>2]|=l<>2]|=(192|l>>6)<>2]|=(128|63&l)<=57344?(c[i>>2]|=(224|l>>12)<>2]|=(128|l>>6&63)<>2]|=(128|63&l)<>2]|=(240|l>>18)<>2]|=(128|l>>12&63)<>2]|=(128|l>>6&63)<>2]|=(128|63&l)<=r){for(this.start=i-r,this.block=c[u],i=0;i>=8);e>0;)i.unshift(e),e=255&(t>>=8),++o;return a?i.push(o):i.unshift(o),this.update(i),i.length},v.prototype.encodeString=function(a){var e,o=typeof a;if("string"!==o){if("object"!==o)throw new Error(t);if(null===a)throw new Error(t);if(n&&a.constructor===ArrayBuffer)a=new Uint8Array(a);else if(!(Array.isArray(a)||n&&ArrayBuffer.isView(a)))throw new Error(t);e=!0}var i=0,l=a.length;if(e)i=l;else for(var c=0;c=57344?i+=3:(s=65536+((1023&s)<<10|1023&a.charCodeAt(++c)),i+=4)}return i+=this.encode(8*i),this.update(a),i},v.prototype.bytepad=function(t,a){for(var e=this.encode(a),o=0;o>2]|=this.padding[3&a],this.lastByteIndex===this.byteCount)for(t[0]=t[e],a=1;a>4&15]+l[15&t]+l[t>>12&15]+l[t>>8&15]+l[t>>20&15]+l[t>>16&15]+l[t>>28&15]+l[t>>24&15];c%a==0&&(P(e),n=0)}return i&&(t=e[n],s+=l[t>>4&15]+l[15&t],i>1&&(s+=l[t>>12&15]+l[t>>8&15]),i>2&&(s+=l[t>>20&15]+l[t>>16&15])),s},v.prototype.arrayBuffer=function(){this.finalize();var t,a=this.blockCount,e=this.s,o=this.outputBlocks,i=this.extraBytes,n=0,l=0,c=this.outputBits>>3;t=i?new ArrayBuffer(o+1<<2):new ArrayBuffer(c);for(var s=new Uint32Array(t);l>8&255,s[t+2]=a>>16&255,s[t+3]=a>>24&255;c%e==0&&P(o)}return n&&(t=c<<2,a=o[l],s[t]=255&a,n>1&&(s[t+1]=a>>8&255),n>2&&(s[t+2]=a>>16&255)),s},S.prototype=new v,S.prototype.finalize=function(){return this.encode(this.outputBits,!0),v.prototype.finalize.call(this)};var P=function(t){var a,e,o,i,n,l,c,s,d,u,_,g,f,p,h,m,L,b,w,y,A,C,x,F,I,B,E,v,S,P,O,q,G,z,T,M,U,Q,k,R,N,D,$,j,V,K,H,Z,W,Y,J,X,tt,at,et,ot,it,nt,lt,ct,st,rt,dt;for(o=0;o<48;o+=2)i=t[0]^t[10]^t[20]^t[30]^t[40],n=t[1]^t[11]^t[21]^t[31]^t[41],l=t[2]^t[12]^t[22]^t[32]^t[42],c=t[3]^t[13]^t[23]^t[33]^t[43],s=t[4]^t[14]^t[24]^t[34]^t[44],d=t[5]^t[15]^t[25]^t[35]^t[45],u=t[6]^t[16]^t[26]^t[36]^t[46],_=t[7]^t[17]^t[27]^t[37]^t[47],a=(g=t[8]^t[18]^t[28]^t[38]^t[48])^(l<<1|c>>>31),e=(f=t[9]^t[19]^t[29]^t[39]^t[49])^(c<<1|l>>>31),t[0]^=a,t[1]^=e,t[10]^=a,t[11]^=e,t[20]^=a,t[21]^=e,t[30]^=a,t[31]^=e,t[40]^=a,t[41]^=e,a=i^(s<<1|d>>>31),e=n^(d<<1|s>>>31),t[2]^=a,t[3]^=e,t[12]^=a,t[13]^=e,t[22]^=a,t[23]^=e,t[32]^=a,t[33]^=e,t[42]^=a,t[43]^=e,a=l^(u<<1|_>>>31),e=c^(_<<1|u>>>31),t[4]^=a,t[5]^=e,t[14]^=a,t[15]^=e,t[24]^=a,t[25]^=e,t[34]^=a,t[35]^=e,t[44]^=a,t[45]^=e,a=s^(g<<1|f>>>31),e=d^(f<<1|g>>>31),t[6]^=a,t[7]^=e,t[16]^=a,t[17]^=e,t[26]^=a,t[27]^=e,t[36]^=a,t[37]^=e,t[46]^=a,t[47]^=e,a=u^(i<<1|n>>>31),e=_^(n<<1|i>>>31),t[8]^=a,t[9]^=e,t[18]^=a,t[19]^=e,t[28]^=a,t[29]^=e,t[38]^=a,t[39]^=e,t[48]^=a,t[49]^=e,p=t[0],h=t[1],K=t[11]<<4|t[10]>>>28,H=t[10]<<4|t[11]>>>28,v=t[20]<<3|t[21]>>>29,S=t[21]<<3|t[20]>>>29,ct=t[31]<<9|t[30]>>>23,st=t[30]<<9|t[31]>>>23,D=t[40]<<18|t[41]>>>14,$=t[41]<<18|t[40]>>>14,z=t[2]<<1|t[3]>>>31,T=t[3]<<1|t[2]>>>31,m=t[13]<<12|t[12]>>>20,L=t[12]<<12|t[13]>>>20,Z=t[22]<<10|t[23]>>>22,W=t[23]<<10|t[22]>>>22,P=t[33]<<13|t[32]>>>19,O=t[32]<<13|t[33]>>>19,rt=t[42]<<2|t[43]>>>30,dt=t[43]<<2|t[42]>>>30,at=t[5]<<30|t[4]>>>2,et=t[4]<<30|t[5]>>>2,M=t[14]<<6|t[15]>>>26,U=t[15]<<6|t[14]>>>26,b=t[25]<<11|t[24]>>>21,w=t[24]<<11|t[25]>>>21,Y=t[34]<<15|t[35]>>>17,J=t[35]<<15|t[34]>>>17,q=t[45]<<29|t[44]>>>3,G=t[44]<<29|t[45]>>>3,F=t[6]<<28|t[7]>>>4,I=t[7]<<28|t[6]>>>4,ot=t[17]<<23|t[16]>>>9,it=t[16]<<23|t[17]>>>9,Q=t[26]<<25|t[27]>>>7,k=t[27]<<25|t[26]>>>7,y=t[36]<<21|t[37]>>>11,A=t[37]<<21|t[36]>>>11,X=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,j=t[8]<<27|t[9]>>>5,V=t[9]<<27|t[8]>>>5,B=t[18]<<20|t[19]>>>12,E=t[19]<<20|t[18]>>>12,nt=t[29]<<7|t[28]>>>25,lt=t[28]<<7|t[29]>>>25,R=t[38]<<8|t[39]>>>24,N=t[39]<<8|t[38]>>>24,C=t[48]<<14|t[49]>>>18,x=t[49]<<14|t[48]>>>18,t[0]=p^~m&b,t[1]=h^~L&w,t[10]=F^~B&v,t[11]=I^~E&S,t[20]=z^~M&Q,t[21]=T^~U&k,t[30]=j^~K&Z,t[31]=V^~H&W,t[40]=at^~ot&nt,t[41]=et^~it<,t[2]=m^~b&y,t[3]=L^~w&A,t[12]=B^~v&P,t[13]=E^~S&O,t[22]=M^~Q&R,t[23]=U^~k&N,t[32]=K^~Z&Y,t[33]=H^~W&J,t[42]=ot^~nt&ct,t[43]=it^~lt&st,t[4]=b^~y&C,t[5]=w^~A&x,t[14]=v^~P&q,t[15]=S^~O&G,t[24]=Q^~R&D,t[25]=k^~N&$,t[34]=Z^~Y&X,t[35]=W^~J&tt,t[44]=nt^~ct&rt,t[45]=lt^~st&dt,t[6]=y^~C&p,t[7]=A^~x&h,t[16]=P^~q&F,t[17]=O^~G&I,t[26]=R^~D&z,t[27]=N^~$&T,t[36]=Y^~X&j,t[37]=J^~tt&V,t[46]=ct^~rt&at,t[47]=st^~dt&et,t[8]=C^~p&m,t[9]=x^~h&L,t[18]=q^~F&B,t[19]=G^~I&E,t[28]=D^~z&M,t[29]=$^~T&U,t[38]=X^~j&K,t[39]=tt^~V&H,t[48]=rt^~at&ot,t[49]=dt^~et&it,t[0]^=r[o],t[1]^=r[o+1]};if(i)$s.exports=y;else for(C=0;C0===e.type?t++:a++));let e=new Uint8Array(a*this.Fr.n8+t*this.G1.F.n8*2),o=0;for(let t=0;t32768?new Cn(t.length*o.n8):new Uint8Array(t.length*o.n8);for(let a=0;a32768?new Cn(o*i.n8):new Uint8Array(o*i.n8);return n.set(t.coef.slice(),0),new sr(n,a,e)}isEqual(t){const a=this.degree();if(a!==t.degree())return!1;for(let e=0;e32768?new Cn((this.length()+t.length)*this.Fr.n8):new Uint8Array((this.length()+t.length)*this.Fr.n8);a.set(this.coef,0);for(let e=0;ethis.coef.byteLength?this.Fr.zero:this.coef.slice(a,a+this.Fr.n8)}setCoef(t,a){if(t>this.length()-1)throw new Error("Coef index is not available");this.coef.set(a,t*this.Fr.n8)}static async to4T(t,a,e,o){e=e||[];let i=await o.ifft(t);const n=4*a>32768?new Cn(4*a*o.n8):new Uint8Array(4*a*o.n8);n.set(i,0);const l=await o.fft(n);if(0===e.length)return[i,l];const c=a+e.length>32768?new Cn((a+e.length)*o.n8):new Uint8Array((a+e.length)*o.n8);c.set(i,0);for(let t=0;t0;t--){const a=t*this.Fr.n8;if(!this.Fr.eq(this.Fr.zero,this.coef.slice(a,a+this.Fr.n8)))return t}return 0}evaluate(t){let a=this.Fr.zero;for(let e=this.degree()+1;e>0;e--){let o=e*this.Fr.n8;const i=this.coef.slice(o-this.Fr.n8,o);a=this.Fr.add(i,this.Fr.mul(a,t))}return a}fastEvaluate(t){const a=this.Fr;let e=this.degree()+1,o=parseInt(e/3),i=e-3*o,n=[],l=[];l[0]=a.one;for(let e=0;e<3;e++){n[e]=a.zero;for(let c=2===e?o+i:o;c>0;c--)n[e]=a.add(this.getCoef(e*o+c-1),a.mul(n[e],t)),0===e&&(l[0]=a.mul(l[0],t))}for(let t=1;t<3;t++)n[0]=a.add(n[0],a.mul(l[t-1],n[t])),l[t]=a.mul(l[t-1],l[0]);return n[0]}add(t,a){let e=!1;t.length()>this.length()&&(e=!0);const o=this.length(),i=t.length();for(let n=0;nthis.length()&&(e=!0);const o=this.length(),i=t.length();for(let n=0;n32768?new Cn(e*a.n8):new Uint8Array(e*a.n8);let i=new sr(o,this.curve,this.logger);i.coef.set(this.coef.slice(0,(e-1)*a.n8),32),this.mulScalar(a.neg(t)),i.add(this),this.coef=i.coef}byXNSubValue(t,a){const e=this.Fr,o=!(this.length()-t-1>=this.degree())?this.length()+t:this.length(),i=o>32768?new Cn(o*e.n8):new Uint8Array(o*e.n8);let n=new sr(i,this.curve,this.logger);n.coef.set(this.coef.slice(0,32*(this.degree()+1)),32*t),this.mulScalar(a),n.add(this),this.coef=n.coef}divBy(t){const a=this.Fr,e=this.degree(),o=t.degree();let i=new sr(this.coef,this.curve,this.logger);this.coef=this.length()>32768?new Cn(this.length()*a.n8):new Uint8Array(this.length()*a.n8);for(let n=e-o;n>=0;n--){this.setCoef(n,a.div(i.getCoef(n+o),t.getCoef(o)));for(let e=0;e<=o;e++)i.setCoef(n+e,a.sub(i.getCoef(n+e),a.mul(this.getCoef(n),t.getCoef(e))))}return i}divByMonic(t,a){const e=this.Fr;let o=this.degree(),i=this.length()>32768?new Cn(this.length()*e.n8):new Uint8Array(this.length()*e.n8),n=new sr(i,this.curve,this.logger),l=[];for(let a=0;a=0&&!(s<0);s-=c){let o=i;l[o]=e.add(this.getCoef(s+t),e.mul(l[o],a)),n.setCoef(s,l[o])}this.coef=n.coef}divByVanishing(t,a){if(this.degree()32768?new Cn(this.length()*e.n8):new Uint8Array(this.length()*e.n8);for(let i=this.length()-1;i>=t;i--){let n=o.getCoef(i);e.eq(e.zero,n)||(o.setCoef(i,e.zero),o.setCoef(i-t,e.add(o.getCoef(i-t),e.mul(a,n))),this.setCoef(i-t,e.add(this.getCoef(i-t),n)))}return o}divByVanishing2(t,a){if(this.degree()32768?new Cn(this.length()*e.n8):new Uint8Array(this.length()*e.n8);let i=this.length()-t,n=Math.floor(i/3),l=i-2*n;console.log(i),console.log(n+" "+l);for(let i=0;i<3;i++){console.log("> Thread "+i);for(let c=0===i?l:n;c>0;c--){let s=c-1;0!==i&&(s+=(i-1)*n+l);let r=s+t,d=o.getCoef(r);e.eq(e.zero,d)||(o.setCoef(r,e.zero),o.setCoef(s,e.add(o.getCoef(s),e.mul(a,d))),this.setCoef(s,e.add(this.getCoef(s),d)),console.log(s+" <-- "+r))}}return this.print(),o}fastDivByVanishing(t){const a=this.Fr;for(let e=0;e32768?new Cn(this.length()*a.n8):new Uint8Array(this.length()*a.n8),this.curve,this.logger),u=this.coef;this.coef=d.coef,d.coef=u;for(let t=0;t0;t--){let e=t-1,i=e*s+r;f[e]=[];for(let l=0;l32768?new Cn(this.length()*this.Fr.n8):new Uint8Array(this.length()*this.Fr.n8);a.set(this.Fr.zero,(this.length()-1)*this.Fr.n8),a.set(this.coef.slice((this.length()-1)*this.Fr.n8,this.length()*this.Fr.n8),(this.length()-2)*this.Fr.n8);for(let e=this.length()-3;e>=0;e--){let o=e*this.Fr.n8;a.set(this.Fr.add(this.coef.slice(o+this.Fr.n8,o+2*this.Fr.n8),this.Fr.mul(t,a.slice(o+this.Fr.n8,o+2*this.Fr.n8))),e*this.Fr.n8)}if(!this.Fr.eq(this.coef.slice(0,this.Fr.n8),this.Fr.mul(this.Fr.neg(t),a.slice(0,this.Fr.n8))))throw new Error("Polynomial does not divide");this.coef=a}divZh(t,a=4){for(let a=0;at*(a-1)-a&&!this.Fr.isZero(i))throw new Error("Polynomial is not divisible")}return this}divByZerofier(t,a){let e=this.Fr;const o=e.inv(a),i=e.neg(o);let n=e.eq(e.one,i),l=e.eq(e.negone,i);if(!n)for(let a=0;athis.length()-t-1&&!this.Fr.isZero(s))throw new Error("Polynomial is not divisible")}return this}byX(){const t=this.length()+1>32768?new Cn(this.coef.byteLength+this.Fr.n8):new Uint8Array(this.coef.byteLength+this.Fr.n8);t.set(this.Fr.zero,0),t.set(this.coef,this.Fr.n8),this.coef=t}static async expX(t,a,e=!1){const o=t.Fr;if(a<1)throw new Error("Compute a new polynomial to a zero or negative number is not allowed");if(1===a)return await sr.fromEvaluations(t.coef,curve,t.logger);const i=e?t.degree():t.length()-1,n=i*a+1>32768?new Cn((i*a+1)*o.n8):new Uint8Array((i*a+1)*o.n8);n.set(t.getCoef(0),0);for(let e=1;e<=i;e++){const i=e*o.n8,l=t.getCoef(e);n.set(l,i*a)}return new sr(n,t.curve,t.logger)}split(t,a,e){if(t<1)throw new Error(`Polynomials can't be split in ${t} parts`);if(1===t)return[this];if(0!==e.length&&e.length32768?new Cn(l):new Uint8Array(l);i[a]=new sr(c,this.curve,this.logger);const s=a*o,r=n?this.coef.byteLength:(a+1)*o;if(i[a].coef.set(this.coef.slice(s,r),0),n||i[a].coef.set(e[a],o),0!==a){const t=this.Fr.sub(i[a].coef.slice(0,this.Fr.n8),e[a-1]);i[a].coef.set(t,0)}n&&i[a].truncate()}return i}truncate(){const t=this.degree();if(t+132768?new Cn((t+1)*this.Fr.n8):new Uint8Array((t+1)*this.Fr.n8);a.set(this.coef.slice(0,(t+1)*this.Fr.n8),0),this.coef=a}}static lagrangePolynomialInterpolation(t,a,e){const o=e.Fr;let i=n(0);for(let a=1;a32768?new Cn(t.length*o.n8):new Uint8Array(t.length*o.n8);n=new sr(i,e),n.setCoef(0,o.neg(t[a])),n.setCoef(1,o.one)}else n.byXSubValue(t[a]);let l=n.evaluate(t[i]);l=o.inv(l);const c=o.mul(a[i],l);return n.mulScalar(c),n}}static zerofierPolynomial(t,a){const e=a.Fr;let o=t.length+1>32768?new Cn((t.length+1)*e.n8):new Uint8Array((t.length+1)*e.n8),i=new sr(o,a);i.setCoef(0,e.neg(t[0])),i.setCoef(1,e.one);for(let a=1;a=0;e--){const o=this.getCoef(e);t.eq(t.zero,o)||(t.isNegative(o)?a+=" - ":e!==this.degree()&&(a+=" + "),a+=t.toString(o),e>0&&(a+=e>1?"x^"+e:"x"))}console.log(a)}async multiExponentiation(t,a){const e=this.coef.byteLength/this.Fr.n8,o=t.slice(0,e*this.G1.F.n8*2),i=await this.Fr.batchFromMontgomery(this.coef);let n=await this.G1.multiExpAffine(o,i,this.logger,a);return n=this.G1.toAffine(n),n}}class rr{constructor(t,a,e){this.eval=t,this.curve=a,this.Fr=a.Fr,this.logger=e}static async fromPolynomial(t,a,e,o){const i=new Cn(t.length()*a*e.Fr.n8);i.set(t.coef,0);const n=await e.Fr.fft(i);return new rr(n,e,o)}getEvaluation(t){const a=t*this.Fr.n8;if(a+this.Fr.n8>this.eval.byteLength)throw new Error("Evaluations.getEvaluation() out of bounds");return this.eval.slice(a,a+this.Fr.n8)}length(){let t=this.eval.byteLength/this.Fr.n8;if(t!==Math.floor(this.eval.byteLength/this.Fr.n8))throw new Error("Polynomial evaluations buffer has incorrect size");return 0===t&&this.logger.warn("Polynomial has length zero"),t}}const{stringifyBigInts:dr}=Jn;async function ur(t,a,e){const{fd:o,sections:i}=await ze(a,"wtns",2);e&&e.debug("> Reading witness file");const n=await qc(o,i);e&&e.debug("> Reading zkey file");const{fd:l,sections:c}=await ze(t,"zkey",2),s=await Ic(l,c);if("plonk"!=s.protocol)throw new Error("zkey file is not plonk");if(!Yn.eq(s.r,n.q))throw new Error("Curve of the witness does not match the curve of the proving key");if(n.nWitness!=s.nVars-s.nAdditions)throw new Error(`Invalid witness length. Circuit: ${s.nVars}, witness: ${n.nWitness}, ${s.nAdditions}`);const r=s.curve,d=r.Fr,u=r.Fr.n8,_=s.domainSize*u;e&&(e.debug("----------------------------"),e.debug(" PLONK PROVE SETTINGS"),e.debug(` Curve: ${r.name}`),e.debug(` Circuit power: ${s.power}`),e.debug(` Domain size: ${s.domainSize}`),e.debug(` Vars: ${s.nVars}`),e.debug(` Public vars: ${s.nPublic}`),e.debug(` Constraints: ${s.nConstraints}`),e.debug(` Additions: ${s.nAdditions}`),e.debug("----------------------------")),e&&e.debug("> Reading witness file data");const g=await $e(o,i,2);g.set(d.zero,0);const f=new Cn(u*s.nAdditions);let p={},h={},m={},L={},b=new Ds(r,e);const w=new Hs(r);e&&e.debug(`> Reading Section ${Ws}. Additions`),await async function(){e&&e.debug("··· Computing additions");const t=await $e(l,c,Ws),a=8+2*u;for(let o=0;o Reading Section ${nr}. Sigma1, Sigma2 & Sigma 3`),e&&e.debug("··· Reading Sigma polynomials "),h.Sigma1=new sr(new Cn(_),r,e),h.Sigma2=new sr(new Cn(_),r,e),h.Sigma3=new sr(new Cn(_),r,e),await l.readToBuffer(h.Sigma1.coef,0,_,c[nr][0].p),await l.readToBuffer(h.Sigma2.coef,0,_,c[nr][0].p+5*_),await l.readToBuffer(h.Sigma3.coef,0,_,c[nr][0].p+10*_),e&&e.debug("··· Reading Sigma evaluations"),m.Sigma1=new rr(new Cn(4*_),r,e),m.Sigma2=new rr(new Cn(4*_),r,e),m.Sigma3=new rr(new Cn(4*_),r,e),await l.readToBuffer(m.Sigma1.eval,0,4*_,c[nr][0].p+_),await l.readToBuffer(m.Sigma2.eval,0,4*_,c[nr][0].p+6*_),await l.readToBuffer(m.Sigma3.eval,0,4*_,c[nr][0].p+11*_),e&&e.debug(`> Reading Section ${cr}. Powers of Tau`);const y=await $e(l,c,cr);let A=[];for(let t=1;t<=s.nPublic;t++){const a=g.slice(t*d.n8,t*d.n8+d.n8);A.push(Yn.fromRprLE(a))}e&&e.debug(""),e&&e.debug("> ROUND 1"),await async function(){L.b=[];for(let t=1;t<=11;t++)L.b[t]=r.Fr.random();e&&e.debug("> Computing A, B, C wire polynomials");await async function(){e&&e.debug("··· Reading data from zkey file");p.A=new Cn(_),p.B=new Cn(_),p.C=new Cn(_);const t=await $e(l,c,Ys),a=await $e(l,c,Js),o=await $e(l,c,Xs);for(let e=0;e=s.domainSize+2)throw new Error("A Polynomial is not well calculated");if(h.B.degree()>=s.domainSize+2)throw new Error("B Polynomial is not well calculated");if(h.C.degree()>=s.domainSize+2)throw new Error("C Polynomial is not well calculated")}(),e&&e.debug("> Computing A, B, C MSM");let t=await h.A.multiExponentiation(y,"A"),a=await h.B.multiExponentiation(y,"B"),o=await h.C.multiExponentiation(y,"C");return b.addPolynomial("A",t),b.addPolynomial("B",a),b.addPolynomial("C",o),0}(),e&&e.debug("> ROUND 2"),await async function(){e&&e.debug("> Computing challenges beta and gamma");w.reset(),w.addPolCommitment(s.Qm),w.addPolCommitment(s.Ql),w.addPolCommitment(s.Qr),w.addPolCommitment(s.Qo),w.addPolCommitment(s.Qc),w.addPolCommitment(s.S1),w.addPolCommitment(s.S2),w.addPolCommitment(s.S3);for(let t=0;t Computing Z polynomial");await async function(){e&&e.debug("··· Computing Z evaluations");let t=new Cn(_),a=new Cn(_);t.set(d.one,0),a.set(d.one,0);let o=d.one;for(let e=0;e=s.domainSize+3)throw new Error("Z Polynomial is not well calculated");delete p.Z}(),e&&e.debug("> Computing Z MSM");let t=await h.Z.multiExponentiation(y,"Z");b.addPolynomial("Z",t)}(),e&&e.debug("> ROUND 3"),await async function(){e&&e.debug("> Computing challenge alpha");w.reset(),w.addScalar(L.beta),w.addScalar(L.gamma),w.addPolCommitment(b.getPolynomial("Z")),L.alpha=w.getChallenge(),L.alpha2=d.square(L.alpha),e&&e.debug("··· challenges.alpha: "+d.toString(L.alpha,16));e&&e.debug("> Computing T polynomial");await async function(){e&&e.debug(`··· Reading sections ${ar}, ${er}, ${tr}, ${or}, ${ir}. Q selectors`);m.QL=new rr(new Cn(4*_),r,e),m.QR=new rr(new Cn(4*_),r,e),m.QM=new rr(new Cn(4*_),r,e),m.QO=new rr(new Cn(4*_),r,e),m.QC=new rr(new Cn(4*_),r,e),await l.readToBuffer(m.QL.eval,0,4*_,c[ar][0].p+_),await l.readToBuffer(m.QR.eval,0,4*_,c[er][0].p+_),await l.readToBuffer(m.QM.eval,0,4*_,c[tr][0].p+_),await l.readToBuffer(m.QO.eval,0,4*_,c[or][0].p+_),await l.readToBuffer(m.QC.eval,0,4*_,c[ir][0].p+_),m.Lagrange=new rr(new Cn(4*_*s.nPublic),r,e);for(let t=0;t=3*s.domainSize+6)throw new Error("T Polynomial is not well calculated");e&&e.debug("··· Computing T1, T2, T3 polynomials");h.T1=new sr(new Cn((s.domainSize+1)*u),r,e),h.T2=new sr(new Cn((s.domainSize+1)*u),r,e),h.T3=new sr(new Cn((s.domainSize+6)*u),r,e),h.T1.coef.set(h.T.coef.slice(0,_),0),h.T2.coef.set(h.T.coef.slice(_,2*_),0),h.T3.coef.set(h.T.coef.slice(2*_,3*_+6*u),0),h.T1.setCoef(s.domainSize,L.b[10]);const a=d.sub(h.T2.getCoef(0),L.b[10]);h.T2.setCoef(0,a),h.T2.setCoef(s.domainSize,L.b[11]);const o=d.sub(h.T3.getCoef(0),L.b[11]);h.T3.setCoef(0,o)}(),e&&e.debug("> Computing T MSM");let t=await h.T1.multiExponentiation(y,"T1"),a=await h.T2.multiExponentiation(y,"T2"),o=await h.T3.multiExponentiation(y,"T3");b.addPolynomial("T1",t),b.addPolynomial("T2",a),b.addPolynomial("T3",o)}(),e&&e.debug("> ROUND 4"),await async function(){e&&e.debug("> Computing challenge xi");w.reset(),w.addScalar(L.alpha),w.addPolCommitment(b.getPolynomial("T1")),w.addPolCommitment(b.getPolynomial("T2")),w.addPolCommitment(b.getPolynomial("T3")),L.xi=w.getChallenge(),L.xiw=d.mul(L.xi,d.w[s.power]),e&&e.debug("··· challenges.xi: "+d.toString(L.xi,16));b.addEvaluation("eval_a",h.A.evaluate(L.xi)),b.addEvaluation("eval_b",h.B.evaluate(L.xi)),b.addEvaluation("eval_c",h.C.evaluate(L.xi)),b.addEvaluation("eval_s1",h.Sigma1.evaluate(L.xi)),b.addEvaluation("eval_s2",h.Sigma2.evaluate(L.xi)),b.addEvaluation("eval_zw",h.Z.evaluate(L.xiw))}(),e&&e.debug("> ROUND 5"),await async function(){e&&e.debug("> Computing challenge v");w.reset(),w.addScalar(L.xi),w.addScalar(b.getEvaluation("eval_a")),w.addScalar(b.getEvaluation("eval_b")),w.addScalar(b.getEvaluation("eval_c")),w.addScalar(b.getEvaluation("eval_s1")),w.addScalar(b.getEvaluation("eval_s2")),w.addScalar(b.getEvaluation("eval_zw")),L.v=[],L.v[1]=w.getChallenge(),e&&e.debug("··· challenges.v: "+d.toString(L.v[1],16));for(let t=2;t<6;t++)L.v[t]=d.mul(L.v[t-1],L.v[1]);e&&e.debug("> Computing linearisation polynomial R(X)");await async function(){const t=r.Fr;h.QL=new sr(new Cn(_),r,e),h.QR=new sr(new Cn(_),r,e),h.QM=new sr(new Cn(_),r,e),h.QO=new sr(new Cn(_),r,e),h.QC=new sr(new Cn(_),r,e),await l.readToBuffer(h.QL.coef,0,_,c[ar][0].p),await l.readToBuffer(h.QR.coef,0,_,c[er][0].p),await l.readToBuffer(h.QM.coef,0,_,c[tr][0].p),await l.readToBuffer(h.QO.coef,0,_,c[or][0].p),await l.readToBuffer(h.QC.coef,0,_,c[ir][0].p),L.xin=L.xi;for(let a=0;a Computing opening proof polynomial Wxi(X) polynomial");h.Wxi=new sr(new Cn(_+6*u),r,e),h.Wxi.add(h.R),h.Wxi.add(h.A,L.v[1]),h.Wxi.add(h.B,L.v[2]),h.Wxi.add(h.C,L.v[3]),h.Wxi.add(h.Sigma1,L.v[4]),h.Wxi.add(h.Sigma2,L.v[5]),h.Wxi.subScalar(d.mul(L.v[1],b.evaluations.eval_a)),h.Wxi.subScalar(d.mul(L.v[2],b.evaluations.eval_b)),h.Wxi.subScalar(d.mul(L.v[3],b.evaluations.eval_c)),h.Wxi.subScalar(d.mul(L.v[4],b.evaluations.eval_s1)),h.Wxi.subScalar(d.mul(L.v[5],b.evaluations.eval_s2)),void h.Wxi.divByZerofier(1,L.xi),e&&e.debug("> Computing opening proof polynomial Wxiw(X) polynomial");(async function(){h.Wxiw=sr.fromPolynomial(h.Z,r,e),h.Wxiw.subScalar(b.evaluations.eval_zw),h.Wxiw.divByZerofier(1,L.xiw)})(),e&&e.debug("> Computing Wxi, Wxiw MSM");let t=await h.Wxi.multiExponentiation(y,"Wxi"),a=await h.Wxiw.multiExponentiation(y,"Wxiw");b.addPolynomial("Wxi",t),b.addPolynomial("Wxiw",a)}(),await l.close(),await o.close();let C=b.toObjectProof(!1);return C.protocol="plonk",C.curve=r.name,e&&e.debug("PLONK PROVER FINISHED"),{proof:dr(C),publicSignals:dr(A)};function x(t,a){const e=t.slice(a,a+4);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(0,!0)}function F(t){return te;){const a=i.shift(),e=i.shift(),o=a[0],n=e[0],l=L++,c=t.zero,s=t.neg(a[1]),r=t.neg(e[1]),d=t.one,u=t.zero;h.push([o,n,l,c,s,r,d,u]),m.push([o,n,a[1],e[1]]),i.push([l,t.one])}for(let t=0;t0?o.toString():e!=t.zero?"k":"0"}function s(a,e,s){const r=c(a),d=c(e);if("0"===r||"0"===d)o(s),l(s);else if("k"===r){l(i(e,a[0],s))}else if("k"===d){l(i(a,e[0],s))}else!function(a,e,o){const i=n(a,1),l=n(e,1),c=n(o,1),s=i.s[0],r=l.s[0],d=c.s[0],u=t.mul(i.coefs[0],l.coefs[0]),_=t.mul(i.coefs[0],l.k),g=t.mul(i.k,l.coefs[0]),f=t.neg(c.coefs[0]),p=t.sub(t.mul(i.k,l.k),c.k);h.push([s,r,d,u,_,g,f,p])}(a,e,s)}for(let a=1;a<=b;a++){const e=a,o=0,i=0,n=t.zero,l=t.one,c=t.zero,s=t.zero,r=t.zero;h.push([e,o,i,n,l,c,s,r])}for(let t=0;tc)return o&&o.error(`circuit too big for this power of tau ceremony. ${h.length} > 2**${c}`),-1;if(!n[12])return o&&o.error("Powers of tau is not prepared."),-1;const C=new Cn(A*u),x=n[12][0].p+(2**y-1)*u;await i.readToBuffer(C,0,A*u,x);const[F,I]=function(){let t=f.two;for(;e(t,[],y);)f.add(t,f.one);let a=f.add(t,f.one);for(;e(a,[t],y);)f.add(a,f.one);return[t,a];function e(t,a,e){const o=2**e;let i=f.one;for(let n=0;n0?2:this.Fr.isZero(a)?0:1}normalizeLinearCombination(t){const a=Object.keys(t);for(let e=0;ei;){const o=l.shift(),i=l.shift(),n=t.nVars++,c=this.fnGetAdditionConstraint(o[0],i[0],n,this.Fr.neg(o[1]),this.Fr.neg(i[1]),this.Fr.zero,this.Fr.one,this.Fr.zero);a.push(c),e.push([o[0],i[0],o[1],i[1]]),l.push([n,this.Fr.one])}for(let t=0;tthis.n-1)throw new Error("CPolynomial:addPolynomial, cannot add a polynomial to a position greater than n-1");this.polynomials[t]=a}degree(){let t=this.polynomials.map(((t,a)=>void 0===t?0:t.degree()*this.n+a));return Math.max(...t)}getPolynomial(){let t=this.polynomials.map((t=>void 0===t?0:t.degree()));const a=this.degree(),e=2**(kl(a-1)+1),o=this.Fr.n8;let i=new sr(new Cn(e*o),this.curve,this.logger);for(let e=0;e Reading witness file");const{fd:o,sections:i}=await ze(a,"wtns",2),n=await qc(o,i);e&&e.info("> Reading zkey file");const{fd:l,sections:c}=await ze(t,"zkey",2),s=await Ic(l,c);if(s.protocolId!==ec)throw new Error("zkey file is not fflonk");if(!Yn.eq(s.r,n.q))throw new Error("Curve of the witness does not match the curve of the proving key");if(n.nWitness!==s.nVars-s.nAdditions)throw new Error(`Invalid witness length. Circuit: ${s.nVars}, witness: ${n.nWitness}, ${s.nAdditions}`);const r=s.curve,d=r.Fr,u=r.Fr.n8,_=2*r.G1.F.n8,g=s.domainSize*u;e&&(e.info("----------------------------"),e.info(" FFLONK PROVE SETTINGS"),e.info(` Curve: ${r.name}`),e.info(` Circuit power: ${s.power}`),e.info(` Domain size: ${s.domainSize}`),e.info(` Vars: ${s.nVars}`),e.info(` Public vars: ${s.nPublic}`),e.info(` Constraints: ${s.nConstraints}`),e.info(` Additions: ${s.nAdditions}`),e.info("----------------------------")),e&&e.info("> Reading witness file data");const f=await $e(o,i,2);await o.close(),f.set(d.zero,0);const p=new Cn(s.nAdditions*u);let h={},m={},L={},b={},w={},y={},A=new Ds(r,e);e&&e.info(`> Reading Section ${lc}. Additions`),await async function(){e&&e.info("··· Computing additions");const t=await $e(l,c,lc),a=8+2*u;for(let o=0;o Reading Sections ${pc},${hc},${mc}. Sigma1, Sigma2 & Sigma 3`),e&&e.info("··· Reading Sigma polynomials "),m.Sigma1=new sr(new Cn(g),r,e),m.Sigma2=new sr(new Cn(g),r,e),m.Sigma3=new sr(new Cn(g),r,e),await l.readToBuffer(m.Sigma1.coef,0,g,c[pc][0].p),await l.readToBuffer(m.Sigma2.coef,0,g,c[hc][0].p),await l.readToBuffer(m.Sigma3.coef,0,g,c[mc][0].p),e&&e.info("··· Reading Sigma evaluations"),L.Sigma1=new rr(new Cn(4*g),r,e),L.Sigma2=new rr(new Cn(4*g),r,e),L.Sigma3=new rr(new Cn(4*g),r,e),await l.readToBuffer(L.Sigma1.eval,0,4*g,c[pc][0].p+g),await l.readToBuffer(L.Sigma2.eval,0,4*g,c[hc][0].p+g),await l.readToBuffer(L.Sigma3.eval,0,4*g,c[mc][0].p+g),e&&e.info(`> Reading Section ${bc}. Powers of Tau`);const C=new Cn(16*s.domainSize*_);await l.readToBuffer(C,0,(9*s.domainSize+18)*_,c[bc][0].p),globalThis.gc&&globalThis.gc(),e&&e.info(""),e&&e.info("> ROUND 1"),await async function(){w.b=[];for(let t=1;t<=9;t++)w.b[t]=d.random();e&&e.info("> Computing A, B, C wire polynomials");await async function(){e&&e.info("··· Reading data from zkey file");h.A=new Cn(g),h.B=new Cn(g),h.C=new Cn(g);const t=await $e(l,c,cc),a=await $e(l,c,sc),o=await $e(l,c,rc);for(let e=0;e=s.domainSize)throw new Error("A Polynomial is not well calculated");if(m.B.degree()>=s.domainSize)throw new Error("B Polynomial is not well calculated");if(m.C.degree()>=s.domainSize)throw new Error("C Polynomial is not well calculated")}(),e&&e.info("> Computing T0 polynomial");await async function(){e&&e.info(`··· Reading sections ${dc}, ${uc}, ${_c}, ${gc}, ${fc}. Q selectors`);L.QL=new rr(new Cn(4*g),r,e),L.QR=new rr(new Cn(4*g),r,e),L.QM=new rr(new Cn(4*g),r,e),L.QO=new rr(new Cn(4*g),r,e),L.QC=new rr(new Cn(4*g),r,e),await l.readToBuffer(L.QL.eval,0,4*g,c[dc][0].p+g),await l.readToBuffer(L.QR.eval,0,4*g,c[uc][0].p+g),await l.readToBuffer(L.QM.eval,0,4*g,c[_c][0].p+g),await l.readToBuffer(L.QO.eval,0,4*g,c[gc][0].p+g),await l.readToBuffer(L.QC.eval,0,4*g,c[fc][0].p+g);const t=await $e(l,c,Lc);L.lagrange1=new rr(t,r,e),h.T0=new Cn(4*g),e&&e.info("··· Computing T0 evaluations");for(let t=0;t<4*s.domainSize;t++){e&&0!==t&&t%1e5==0&&e.info(` T0 evaluation ${t}/${4*s.domainSize}`);const a=L.A.getEvaluation(t),o=L.B.getEvaluation(t),i=L.C.getEvaluation(t),n=L.QL.getEvaluation(t),l=L.QR.getEvaluation(t),c=L.QM.getEvaluation(t),r=L.QO.getEvaluation(t),_=L.QC.getEvaluation(t);let g=d.zero;for(let a=0;a=2*s.domainSize-2)throw new Error(`T0 Polynomial is not well calculated (degree is ${m.T0.degree()} and must be less than ${2*s.domainSize+2}`);delete h.T0}(),e&&e.info("> Computing C1 polynomial");await async function(){let t=new yr(4,r,e);if(t.addPolynomial(0,m.A),t.addPolynomial(1,m.B),t.addPolynomial(2,m.C),t.addPolynomial(3,m.T0),m.C1=t.getPolynomial(),m.C1.degree()>=8*s.domainSize-8)throw new Error("C1 Polynomial is not well calculated")}(),e&&e.info("> Computing C1 multi exponentiation");let t=await m.C1.multiExponentiation(C,"C1");return A.addPolynomial("C1",t),0}(),delete m.T0,delete L.QL,delete L.QR,delete L.QM,delete L.QO,delete L.QC,globalThis.gc&&globalThis.gc(),e&&e.info("> ROUND 2"),await async function(){e&&e.info("> Computing challenges beta and gamma");const t=new Hs(r);t.addPolCommitment(s.C0);for(let a=0;a Computing Z polynomial");await async function(){e&&e.info("··· Computing Z evaluations");let t=new Cn(g),a=new Cn(g);t.set(d.one,0),a.set(d.one,0);let o=d.one;for(let i=0;i=s.domainSize+3)throw new Error("Z Polynomial is not well calculated");delete h.Z}(),e&&e.info("> Computing T1 polynomial");await async function(){e&&e.info("··· Computing T1 evaluations");h.T1=new Cn(2*g),h.T1z=new Cn(2*g);let t=d.one;for(let a=0;a<2*s.domainSize;a++){e&&0!==a&&a%1e5==0&&e.info(` T1 evaluation ${a}/${4*s.domainSize}`);const o=d.square(t),i=L.Z.getEvaluation(2*a),n=d.add(d.add(d.mul(w.b[7],o),d.mul(w.b[8],t)),w.b[9]),l=L.lagrange1.getEvaluation(s.domainSize+2*a);let c=d.mul(d.sub(i,d.one),l),r=d.mul(n,l);h.T1.set(c,a*u),h.T1z.set(r,a*u),t=d.mul(t,d.w[s.power+1])}e&&e.info("··· Computing T1 ifft");m.T1=await sr.fromEvaluations(h.T1,r,e),m.T1.divByZerofier(s.domainSize,d.one),e&&e.info("··· Computing T1z ifft");if(m.T1z=await sr.fromEvaluations(h.T1z,r,e),m.T1.add(m.T1z),m.T1.degree()>=s.domainSize+2)throw new Error("T1 Polynomial is not well calculated");delete h.T1,delete h.T1z,delete m.T1z}(),e&&e.info("> Computing T2 polynomial");await async function(){e&&e.info("··· Computing T2 evaluations");h.T2=new Cn(4*g),h.T2z=new Cn(4*g);let t=d.one;for(let a=0;a<4*s.domainSize;a++){e&&0!==a&&a%1e5==0&&e.info(` T2 evaluation ${a}/${4*s.domainSize}`);const o=d.square(t),i=d.mul(t,d.w[s.power]),n=d.square(i),l=L.A.getEvaluation(a),c=L.B.getEvaluation(a),r=L.C.getEvaluation(a),_=L.Z.getEvaluation(a),g=L.Z.getEvaluation((4*s.domainSize+4+a)%(4*s.domainSize)),f=d.add(d.add(d.mul(w.b[7],o),d.mul(w.b[8],t)),w.b[9]),p=d.add(d.add(d.mul(w.b[7],n),d.mul(w.b[8],i)),w.b[9]),m=L.Sigma1.getEvaluation(a),b=L.Sigma2.getEvaluation(a),y=L.Sigma3.getEvaluation(a),A=d.mul(w.beta,t);let C=d.add(l,A);C=d.add(C,w.gamma);let x=d.add(c,d.mul(A,s.k1));x=d.add(x,w.gamma);let F=d.add(r,d.mul(A,s.k2));F=d.add(F,w.gamma);let I=d.mul(d.mul(d.mul(C,x),F),_),B=d.mul(d.mul(d.mul(C,x),F),f),E=d.add(l,d.mul(w.beta,m));E=d.add(E,w.gamma);let v=d.add(c,d.mul(w.beta,b));v=d.add(v,w.gamma);let S=d.add(r,d.mul(w.beta,y));S=d.add(S,w.gamma);let P=d.mul(d.mul(d.mul(E,v),S),g),O=d.mul(d.mul(d.mul(E,v),S),p),q=d.sub(I,P),G=d.sub(B,O);h.T2.set(q,a*u),h.T2z.set(G,a*u),t=d.mul(t,d.w[s.power+2])}e&&e.info("··· Computing T2 ifft");m.T2=await sr.fromEvaluations(h.T2,r,e),e&&e.info("··· Computing T2 / ZH");m.T2.divByZerofier(s.domainSize,d.one),e&&e.info("··· Computing T2z ifft");if(m.T2z=await sr.fromEvaluations(h.T2z,r,e),m.T2.add(m.T2z),m.T2.degree()>=3*s.domainSize)throw new Error("T2 Polynomial is not well calculated");delete h.T2,delete h.T2z,delete m.T2z}(),e&&e.info("> Computing C2 polynomial");await async function(){let t=new yr(3,r,e);if(t.addPolynomial(0,m.Z),t.addPolynomial(1,m.T1),t.addPolynomial(2,m.T2),m.C2=t.getPolynomial(),m.C2.degree()>=9*s.domainSize)throw new Error("C2 Polynomial is not well calculated")}(),e&&e.info("> Computing C2 multi exponentiation");let a=await m.C2.multiExponentiation(C,"C2");return A.addPolynomial("C2",a),0}(),delete h.A,delete h.B,delete h.C,delete L.A,delete L.B,delete L.C,delete L.Sigma1,delete L.Sigma2,delete L.Sigma3,delete L.lagrange1,delete L.Z,globalThis.gc&&globalThis.gc(),e&&e.info("> ROUND 3"),await async function(){e&&e.info("> Computing challenge xi");const t=new Hs(r);t.addScalar(w.gamma),t.addPolCommitment(A.getPolynomial("C2")),w.xiSeed=t.getChallenge();const a=d.square(w.xiSeed);y.w8=[],y.w8[0]=d.one;for(let t=1;t<8;t++)y.w8[t]=d.mul(y.w8[t-1],s.w8);y.w4=[],y.w4[0]=d.one;for(let t=1;t<4;t++)y.w4[t]=d.mul(y.w4[t-1],s.w4);y.w3=[],y.w3[0]=d.one,y.w3[1]=s.w3,y.w3[2]=d.square(s.w3),y.S0={},y.S0.h0w8=[],y.S0.h0w8[0]=d.mul(a,w.xiSeed);for(let t=1;t<8;t++)y.S0.h0w8[t]=d.mul(y.S0.h0w8[0],y.w8[t]);y.S1={},y.S1.h1w4=[],y.S1.h1w4[0]=d.square(y.S0.h0w8[0]);for(let t=1;t<4;t++)y.S1.h1w4[t]=d.mul(y.S1.h1w4[0],y.w4[t]);y.S2={},y.S2.h2w3=[],y.S2.h2w3[0]=d.mul(y.S1.h1w4[0],a),y.S2.h2w3[1]=d.mul(y.S2.h2w3[0],y.w3[1]),y.S2.h2w3[2]=d.mul(y.S2.h2w3[0],y.w3[2]),y.S2.h3w3=[],y.S2.h3w3[0]=d.mul(y.S2.h2w3[0],s.wr),y.S2.h3w3[1]=d.mul(y.S2.h3w3[0],y.w3[1]),y.S2.h3w3[2]=d.mul(y.S2.h3w3[0],y.w3[2]),w.xi=d.mul(d.square(y.S2.h2w3[0]),y.S2.h2w3[0]),e&&e.info("··· challenges.xi: "+d.toString(w.xi));m.QL=new sr(new Cn(g),r,e),m.QR=new sr(new Cn(g),r,e),m.QM=new sr(new Cn(g),r,e),m.QO=new sr(new Cn(g),r,e),m.QC=new sr(new Cn(g),r,e),await l.readToBuffer(m.QL.coef,0,g,c[dc][0].p),await l.readToBuffer(m.QR.coef,0,g,c[uc][0].p),await l.readToBuffer(m.QM.coef,0,g,c[_c][0].p),await l.readToBuffer(m.QO.coef,0,g,c[gc][0].p),await l.readToBuffer(m.QC.coef,0,g,c[fc][0].p),e&&e.info("··· Computing evaluations");A.addEvaluation("ql",m.QL.evaluate(w.xi)),A.addEvaluation("qr",m.QR.evaluate(w.xi)),A.addEvaluation("qm",m.QM.evaluate(w.xi)),A.addEvaluation("qo",m.QO.evaluate(w.xi)),A.addEvaluation("qc",m.QC.evaluate(w.xi)),A.addEvaluation("s1",m.Sigma1.evaluate(w.xi)),A.addEvaluation("s2",m.Sigma2.evaluate(w.xi)),A.addEvaluation("s3",m.Sigma3.evaluate(w.xi)),A.addEvaluation("a",m.A.evaluate(w.xi)),A.addEvaluation("b",m.B.evaluate(w.xi)),A.addEvaluation("c",m.C.evaluate(w.xi)),A.addEvaluation("z",m.Z.evaluate(w.xi)),w.xiw=d.mul(w.xi,d.w[s.power]),A.addEvaluation("zw",m.Z.evaluate(w.xiw)),A.addEvaluation("t1w",m.T1.evaluate(w.xiw)),A.addEvaluation("t2w",m.T2.evaluate(w.xiw))}(),delete m.A,delete m.B,delete m.C,delete m.Z,delete m.T1,delete m.T2,delete m.Sigma1,delete m.Sigma2,delete m.Sigma3,delete m.QL,delete m.QR,delete m.QM,delete m.QC,delete m.QO,globalThis.gc&&globalThis.gc(),e&&e.info("> ROUND 4"),await async function(){e&&e.info("> Computing challenge alpha");const t=new Hs(r);t.addScalar(w.xiSeed),t.addScalar(A.getEvaluation("ql")),t.addScalar(A.getEvaluation("qr")),t.addScalar(A.getEvaluation("qm")),t.addScalar(A.getEvaluation("qo")),t.addScalar(A.getEvaluation("qc")),t.addScalar(A.getEvaluation("s1")),t.addScalar(A.getEvaluation("s2")),t.addScalar(A.getEvaluation("s3")),t.addScalar(A.getEvaluation("a")),t.addScalar(A.getEvaluation("b")),t.addScalar(A.getEvaluation("c")),t.addScalar(A.getEvaluation("z")),t.addScalar(A.getEvaluation("zw")),t.addScalar(A.getEvaluation("t1w")),t.addScalar(A.getEvaluation("t2w")),w.alpha=t.getChallenge(),e&&e.info("··· challenges.alpha: "+d.toString(w.alpha));e&&e.info("> Reading C0 polynomial");m.C0=new sr(new Cn(8*g),r,e),await l.readToBuffer(m.C0.coef,0,8*g,c[wc][0].p),e&&e.info("> Computing R0 polynomial");(function(){if(m.R0=sr.lagrangePolynomialInterpolation([y.S0.h0w8[0],y.S0.h0w8[1],y.S0.h0w8[2],y.S0.h0w8[3],y.S0.h0w8[4],y.S0.h0w8[5],y.S0.h0w8[6],y.S0.h0w8[7]],[m.C0.evaluate(y.S0.h0w8[0]),m.C0.evaluate(y.S0.h0w8[1]),m.C0.evaluate(y.S0.h0w8[2]),m.C0.evaluate(y.S0.h0w8[3]),m.C0.evaluate(y.S0.h0w8[4]),m.C0.evaluate(y.S0.h0w8[5]),m.C0.evaluate(y.S0.h0w8[6]),m.C0.evaluate(y.S0.h0w8[7])],r),m.R0.degree()>7)throw new Error("R0 Polynomial is not well calculated")})(),e&&e.info("> Computing R1 polynomial");(function(){if(m.R1=sr.lagrangePolynomialInterpolation([y.S1.h1w4[0],y.S1.h1w4[1],y.S1.h1w4[2],y.S1.h1w4[3]],[m.C1.evaluate(y.S1.h1w4[0]),m.C1.evaluate(y.S1.h1w4[1]),m.C1.evaluate(y.S1.h1w4[2]),m.C1.evaluate(y.S1.h1w4[3])],r),m.R1.degree()>3)throw new Error("R1 Polynomial is not well calculated")})(),e&&e.info("> Computing R2 polynomial");(function(){if(m.R2=sr.lagrangePolynomialInterpolation([y.S2.h2w3[0],y.S2.h2w3[1],y.S2.h2w3[2],y.S2.h3w3[0],y.S2.h3w3[1],y.S2.h3w3[2]],[m.C2.evaluate(y.S2.h2w3[0]),m.C2.evaluate(y.S2.h2w3[1]),m.C2.evaluate(y.S2.h2w3[2]),m.C2.evaluate(y.S2.h3w3[0]),m.C2.evaluate(y.S2.h3w3[1]),m.C2.evaluate(y.S2.h3w3[2])],r),m.R2.degree()>5)throw new Error("R2 Polynomial is not well calculated")})(),e&&e.info("> Computing F polynomial");await async function(){e&&e.info("··· Computing F polynomial");m.F=sr.fromPolynomial(m.C0,r,e),m.F.sub(m.R0),m.F.divByZerofier(8,w.xi);let t=sr.fromPolynomial(m.C1,r,e);t.sub(m.R1),t.mulScalar(w.alpha),t.divByZerofier(4,w.xi);let a=sr.fromPolynomial(m.C2,r,e);if(a.sub(m.R2),a.mulScalar(d.square(w.alpha)),a.divByZerofier(3,w.xi),a.divByZerofier(3,w.xiw),m.F.add(t),m.F.add(a),m.F.degree()>=9*s.domainSize-6)throw new Error("F Polynomial is not well calculated")}(),e&&e.info("> Computing W1 multi exponentiation");let a=await m.F.multiExponentiation(C,"W1");return A.addPolynomial("W1",a),0}(),globalThis.gc&&globalThis.gc(),e&&e.info("> ROUND 5"),await async function(){e&&e.info("> Computing challenge y");const t=new Hs(r);t.addScalar(w.alpha),t.addPolCommitment(A.getPolynomial("W1")),w.y=t.getChallenge(),e&&e.info("··· challenges.y: "+d.toString(w.y));e&&e.info("> Computing L polynomial");await async function(){e&&e.info("··· Computing L polynomial");const t=m.R0.evaluate(w.y),a=m.R1.evaluate(w.y),o=m.R2.evaluate(w.y);let i=d.sub(w.y,y.S0.h0w8[0]);for(let t=1;t<8;t++)i=d.mul(i,d.sub(w.y,y.S0.h0w8[t]));let n=d.sub(w.y,y.S1.h1w4[0]);for(let t=1;t<4;t++)n=d.mul(n,d.sub(w.y,y.S1.h1w4[t]));let l=d.sub(w.y,y.S2.h2w3[0]);for(let t=1;t<3;t++)l=d.mul(l,d.sub(w.y,y.S2.h2w3[t]));for(let t=0;t<3;t++)l=d.mul(l,d.sub(w.y,y.S2.h3w3[t]));let c=d.mul(n,l),u=d.mul(w.alpha,d.mul(i,l)),_=d.mul(d.square(w.alpha),d.mul(i,n));b.denH1=n,b.denH2=l,m.L=sr.fromPolynomial(m.C0,r,e),m.L.subScalar(t),m.L.mulScalar(c);let g=sr.fromPolynomial(m.C1,r,e);g.subScalar(a),g.mulScalar(u);let f=sr.fromPolynomial(m.C2,r,e);f.subScalar(o),f.mulScalar(_),m.L.add(g),m.L.add(f),e&&e.info("> Computing ZT polynomial");await async function(){m.ZT=sr.zerofierPolynomial([y.S0.h0w8[0],y.S0.h0w8[1],y.S0.h0w8[2],y.S0.h0w8[3],y.S0.h0w8[4],y.S0.h0w8[5],y.S0.h0w8[6],y.S0.h0w8[7],y.S1.h1w4[0],y.S1.h1w4[1],y.S1.h1w4[2],y.S1.h1w4[3],y.S2.h2w3[0],y.S2.h2w3[1],y.S2.h2w3[2],y.S2.h3w3[0],y.S2.h3w3[1],y.S2.h3w3[2]],r)}();const p=m.ZT.evaluate(w.y);if(m.F.mulScalar(p),m.L.sub(m.F),m.L.degree()>=9*s.domainSize)throw new Error("L Polynomial is not well calculated");delete h.L}(),e&&e.info("> Computing ZTS2 polynomial");await async function(){m.ZTS2=sr.zerofierPolynomial([y.S1.h1w4[0],y.S1.h1w4[1],y.S1.h1w4[2],y.S1.h1w4[3],y.S2.h2w3[0],y.S2.h2w3[1],y.S2.h2w3[2],y.S2.h3w3[0],y.S2.h3w3[1],y.S2.h3w3[2]],r)}();let a=m.ZTS2.evaluate(w.y);a=d.inv(a),m.L.mulScalar(a);const o=sr.fromCoefficientsArray([d.neg(w.y),d.one],r);e&&e.info("> Computing W' = L / ZTS2 polynomial");const i=m.L.divBy(o);if(i.degree()>0)throw new Error(`Degree of L(X)/(ZTS2(y)(X-y)) remainder is ${i.degree()} and should be 0`);if(m.L.degree()>=9*s.domainSize-1)throw new Error("Degree of L(X)/(ZTS2(y)(X-y)) is not correct");e&&e.info("> Computing W' multi exponentiation");let n=await m.L.multiExponentiation(C,"W2");return A.addPolynomial("W2",n),0}(),delete m.C0,delete m.C1,delete m.C2,delete m.R1,delete m.R2,delete m.F,delete m.L,delete m.ZT,delete m.ZTS2,await l.close(),globalThis.gc&&globalThis.gc(),A.addEvaluation("inv",function(){let t=w.xi;for(let a=0;a Reading PTau file");const{fd:i,sections:n}=await ze(a,"ptau",1);if(!n[12])throw new Error("Powers of Tau is not well prepared. Section 12 missing.");o&&o.info("> Getting curve from PTau settings");const{curve:l}=await ts(i,n);o&&o.info("> Reading r1cs file");const{fd:c,sections:s}=await ze(t,"r1cs",1),r=await ys(c,s,{loadConstraints:!1,loadCustomGates:!0});if(r.prime!==l.r)throw new Error("r1cs curve does not match powers of tau ceremony curve");const d=l.Fr,u=l.Fr.n8,_=2*l.G1.F.n8,g=2*l.G2.F.n8;let f,p={},h={},m={nVars:r.nVars,nPublic:r.nOutputs+r.nPubInputs};const L=new Os;let b=new Os;if(o&&o.info("> Processing FFlonk constraints"),await async function(t,a,e){for(let a=0;a computing k1 and k2");const[w,y]=function(){let t=d.two;for(;e(t,[],m.cirPower);)d.add(t,d.one);let a=d.add(t,d.one);for(;e(a,[t],m.cirPower);)d.add(a,d.one);return[t,a];function e(t,a,e){const o=2**e;let i=d.one;for(let n=0;n computing w3");const A=function(){let t=d.e(31624),a=Yn.div(3648040478639879203707734290876212514758060733402672390616367364429301415936n,Yn.e(3));return d.exp(t,a)}();o&&o.info("> computing w4");const C=d.w[2];o&&o.info("> computing w8");const x=d.w[3];o&&o.info("> computing wr");const F=function(t,a){const e=a.e(467799165886069610036046866799264026481344299079011762026774533774345988080n);return a.exp(e,2**(28-t))}(m.cirPower,l.Fr);return await async function(){o&&o.info("> Writing the zkey file");const t=await Te(e,"zkey",1,ic,1<<22,1<<24);o&&o.info(`··· Writing Section ${Xl}. Zkey Header`);await async function(t){await Me(t,Xl),await t.writeULE32(ec),await Ue(t)}(t),o&&o.info(`··· Writing Section ${lc}. Additions`);await async function(t){await Me(t,lc);const a=new Uint8Array(8+2*u),e=new DataView(a.buffer);for(let i=0;i=8*m.domainSize)throw new Error("C0 Polynomial is not well calculated");await Me(t,wc),await t.write(p.C0.coef),await Ue(t)}(t),globalThis.gc&&globalThis.gc();o&&o.info(`··· Writing Section ${nc}. FFlonk Header`);await async function(t){await Me(t,nc);const a=l.q,e=8*(Math.floor((Yn.bitLength(a)-1)/64)+1);await t.writeULE32(e),await Re(t,a,e);const o=l.r,c=8*(Math.floor((Yn.bitLength(o)-1)/64)+1);let s;await t.writeULE32(c),await Re(t,o,c),await t.writeULE32(m.nVars),await t.writeULE32(m.nPublic),await t.writeULE32(m.domainSize),await t.writeULE32(b.length),await t.writeULE32(L.length),await t.write(w),await t.write(y),await t.write(A),await t.write(C),await t.write(x),await t.write(F),s=await i.read(g,n[3][0].p+g),await t.write(s);let r=await p.C0.multiExponentiation(f,"C0");await t.write(r),await Ue(t)}(t),globalThis.gc&&globalThis.gc();o&&o.info("> Writing the zkey file finished");await t.close()}(),await c.close(),await i.close(),o&&o.info("FFLONK SETUP FINISHED"),0;async function I(t,a,e,i){await Me(t,a);for(let a=0;a Checking commitments belong to G1"),!function(t,a,e){const o=t.G1;return o.isValid(a.polynomials.C1)&&o.isValid(a.polynomials.C2)&&o.isValid(a.polynomials.W1)&&o.isValid(a.polynomials.W2)&&o.isValid(e.C0)}(i,l,n))return o&&o.error("Proof commitments are not valid"),!1;if(o&&o.info("> Checking evaluations belong to F"),!function(t,a){return Br(t,a.evaluations.ql)&&Br(t,a.evaluations.qr)&&Br(t,a.evaluations.qm)&&Br(t,a.evaluations.qo)&&Br(t,a.evaluations.qc)&&Br(t,a.evaluations.s1)&&Br(t,a.evaluations.s2)&&Br(t,a.evaluations.s3)&&Br(t,a.evaluations.a)&&Br(t,a.evaluations.b)&&Br(t,a.evaluations.c)&&Br(t,a.evaluations.z)&&Br(t,a.evaluations.zw)&&Br(t,a.evaluations.t1w)&&Br(t,a.evaluations.t2w)}(i,l))return o&&o.error("Proof evaluations are not valid."),!1;if(o&&o.info("> Checking public inputs belong to F"),!function(t,a){for(let e=0;e Computing challenges");const{challenges:r,roots:d}=function(t,a,e,o,i){const n=t.Fr,l={},c={},s=new Hs(t);s.addPolCommitment(e.C0);for(let t=0;t Computing Zero polynomial evaluation Z_H(xi)"),r.zh=s.sub(r.xiN,s.one),r.invzh=s.inv(r.zh),o&&o.info("> Computing Lagrange evaluations");const u=await async function(t,a,e){const o=t.Fr,i=Math.max(1,e.nPublic),n=new Cn(i*o.n8);let l=new Cn(i*o.n8),c=o.one;for(let t=0;t Computing polynomial identities PI(X)");const _=function(t,a,e){const o=t.Fr;let i=o.zero;for(let t=0;t Computing r0(y)");const g=function(t,a,e,o,i){const n=o.Fr,l=Er(e.S0.h0w8,a.y,a.xi,o);i&&i.info("··· Computing r0(y)");let c=n.zero;for(let a=0;a<8;a++){let o=[];o[1]=e.S0.h0w8[a];for(let t=2;t<8;t++)o[t]=n.mul(o[t-1],e.S0.h0w8[a]);let i=n.add(t.evaluations.ql,n.mul(t.evaluations.qr,o[1]));i=n.add(i,n.mul(t.evaluations.qo,o[2])),i=n.add(i,n.mul(t.evaluations.qm,o[3])),i=n.add(i,n.mul(t.evaluations.qc,o[4])),i=n.add(i,n.mul(t.evaluations.s1,o[5])),i=n.add(i,n.mul(t.evaluations.s2,o[6])),i=n.add(i,n.mul(t.evaluations.s3,o[7])),c=n.add(c,n.mul(i,l[a]))}return c}(l,r,d,i,o);o&&o.info("> Computing r1(y)");const f=function(t,a,e,o,i,n){const l=i.Fr,c=Er(e.S1.h1w4,a.y,a.xi,i);n&&n.info("··· Computing T0(xi)");let s=l.mul(t.evaluations.ql,t.evaluations.a);s=l.add(s,l.mul(t.evaluations.qr,t.evaluations.b)),s=l.add(s,l.mul(t.evaluations.qm,l.mul(t.evaluations.a,t.evaluations.b))),s=l.add(s,l.mul(t.evaluations.qo,t.evaluations.c)),s=l.add(s,t.evaluations.qc),s=l.add(s,o),s=l.mul(s,a.invzh),n&&n.info("··· Computing C1(h_1ω_4^i) values");let r=l.zero;for(let a=0;a<4;a++){let o=t.evaluations.a;o=l.add(o,l.mul(e.S1.h1w4[a],t.evaluations.b));const i=l.square(e.S1.h1w4[a]);o=l.add(o,l.mul(i,t.evaluations.c)),o=l.add(o,l.mul(l.mul(i,e.S1.h1w4[a]),s)),r=l.add(r,l.mul(o,c[a]))}return r}(l,r,d,_,i,o);o&&o.info("> Computing r2(y)");const p=function(t,a,e,o,i,n,l){const c=n.Fr,s=function(t,a,e,o,i){const n=i.Fr,l=[],c=t[0].length,s=c*t.length,r=n.exp(a,s),d=n.mul(n.add(e,o),n.exp(a,c)),u=n.mul(e,o),_=n.add(n.sub(r,d),u);let g=n.mul(n.mul(n.e(c),t[0][0]),n.sub(e,o));for(let e=0;e Computing F");const h=function(t,a,e,o,i){const n=t.G1,l=t.Fr;let c=l.sub(o.y,i.S0.h0w8[0]);for(let t=1;t<8;t++)c=l.mul(c,l.sub(o.y,i.S0.h0w8[t]));o.temp=c;let s=l.sub(o.y,i.S1.h1w4[0]);for(let t=1;t<4;t++)s=l.mul(s,l.sub(o.y,i.S1.h1w4[t]));let r=l.sub(o.y,i.S2.h2w3[0]);for(let t=1;t<3;t++)r=l.mul(r,l.sub(o.y,i.S2.h2w3[t]));for(let t=0;t<3;t++)r=l.mul(r,l.sub(o.y,i.S2.h3w3[t]));o.quotient1=l.mul(o.alpha,l.div(c,s)),o.quotient2=l.mul(l.square(o.alpha),l.div(c,r));let d=n.timesFr(a.polynomials.C1,o.quotient1),u=n.timesFr(a.polynomials.C2,o.quotient2);return n.add(e.C0,n.add(d,u))}(i,l,n,r,d);o&&o.info("> Computing E");const m=function(t,a,e,o,i,n,l){const c=t.G1,s=t.Fr;let r=s.mul(n,e.quotient1),d=s.mul(l,e.quotient2);return c.timesFr(c.one,s.add(i,s.add(r,d)))}(i,0,r,0,g,f,p);o&&o.info("> Computing J");const L=function(t,a,e){const o=t.G1;return o.timesFr(a.polynomials.W1,e.temp)}(i,l,r);o&&o.info("> Validate all evaluations with a pairing");const b=await async function(t,a,e,o,i,n,l){const c=t.G1;let s=c.timesFr(a.polynomials.W2,e.y);s=c.add(c.sub(c.sub(i,n),l),s);const r=t.G2.one,d=a.polynomials.W2,u=o.X_2;return await t.pairingEq(c.neg(s),r,d,u)}(i,l,r,n,h,m,L);return o&&(b?o.info("PROOF VERIFIED SUCCESSFULLY"):o.warn("Invalid Proof")),o&&o.info("FFLONK VERIFIER FINISHED"),b},exportSolidityVerifier:Rs,exportSolidityCallData:async function(t,a){const e=vr(a),o=vr(t),i=await il(e.curve);i.G1,i.Fr;let n="";for(let t=0;t