UNPKG

@fastnear/api

Version:

Interact with NEAR Protocol blockchain including transaction signing, utilities, and more.

1,532 lines (1,520 loc) 149 kB
/* ⋈ 🏃🏻💨 FastNEAR API - IIFE/UMD (@fastnear/api version 0.9.7) */ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.7 */ "use strict"; var near = (() => { var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2); // src/index.ts var src_exports3 = {}; __export(src_exports3, { MaxBlockDelayMs: () => MaxBlockDelayMs, accountId: () => accountId, actions: () => actions, afterTxSent: () => afterTxSent, authStatus: () => authStatus, config: () => config, event: () => event, exp: () => exp2, generateTxId: () => generateTxId, getPublicKeyForContract: () => getPublicKeyForContract, localTxHistory: () => localTxHistory, publicKey: () => publicKey, queryAccessKey: () => queryAccessKey, queryAccount: () => queryAccount, queryBlock: () => queryBlock, queryTx: () => queryTx, requestSignIn: () => requestSignIn, selected: () => selected, sendRpc: () => sendRpc, sendTx: () => sendTx, sendTxToRpc: () => sendTxToRpc, signOut: () => signOut, state: () => state, utils: () => utils, view: () => view, withBlockId: () => withBlockId }); // ../../node_modules/big.js/big.mjs var DP = 20; var RM = 1; var MAX_DP = 1e6; var MAX_POWER = 1e6; var NE = -7; var PE = 21; var STRICT = false; var NAME = "[big.js] "; var INVALID = NAME + "Invalid "; var INVALID_DP = INVALID + "decimal places"; var INVALID_RM = INVALID + "rounding mode"; var DIV_BY_ZERO = NAME + "Division by zero"; var P = {}; var UNDEFINED = void 0; var NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i; function _Big_() { function Big2(n) { var x = this; if (!(x instanceof Big2)) return n === UNDEFINED ? _Big_() : new Big2(n); if (n instanceof Big2) { x.s = n.s; x.e = n.e; x.c = n.c.slice(); } else { if (typeof n !== "string") { if (Big2.strict === true && typeof n !== "bigint") { throw TypeError(INVALID + "value"); } n = n === 0 && 1 / n < 0 ? "-0" : String(n); } parse(x, n); } x.constructor = Big2; } __name(Big2, "Big"); Big2.prototype = P; Big2.DP = DP; Big2.RM = RM; Big2.NE = NE; Big2.PE = PE; Big2.strict = STRICT; Big2.roundDown = 0; Big2.roundHalfUp = 1; Big2.roundHalfEven = 2; Big2.roundUp = 3; return Big2; } __name(_Big_, "_Big_"); function parse(x, n) { var e, i, nl; if (!NUMERIC.test(n)) { throw Error(INVALID + "number"); } x.s = n.charAt(0) == "-" ? (n = n.slice(1), -1) : 1; if ((e = n.indexOf(".")) > -1) n = n.replace(".", ""); if ((i = n.search(/e/i)) > 0) { if (e < 0) e = i; e += +n.slice(i + 1); n = n.substring(0, i); } else if (e < 0) { e = n.length; } nl = n.length; for (i = 0; i < nl && n.charAt(i) == "0"; ) ++i; if (i == nl) { x.c = [x.e = 0]; } else { for (; nl > 0 && n.charAt(--nl) == "0"; ) ; x.e = e - i - 1; x.c = []; for (e = 0; i <= nl; ) x.c[e++] = +n.charAt(i++); } return x; } __name(parse, "parse"); function round(x, sd, rm, more) { var xc = x.c; if (rm === UNDEFINED) rm = x.constructor.RM; if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) { throw Error(INVALID_RM); } if (sd < 1) { more = rm === 3 && (more || !!xc[0]) || sd === 0 && (rm === 1 && xc[0] >= 5 || rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED))); xc.length = 1; if (more) { x.e = x.e - sd + 1; xc[0] = 1; } else { xc[0] = x.e = 0; } } else if (sd < xc.length) { more = rm === 1 && xc[sd] >= 5 || rm === 2 && (xc[sd] > 5 || xc[sd] === 5 && (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) || rm === 3 && (more || !!xc[0]); xc.length = sd; if (more) { for (; ++xc[--sd] > 9; ) { xc[sd] = 0; if (sd === 0) { ++x.e; xc.unshift(1); break; } } } for (sd = xc.length; !xc[--sd]; ) xc.pop(); } return x; } __name(round, "round"); function stringify(x, doExponential, isNonzero) { var e = x.e, s = x.c.join(""), n = s.length; if (doExponential) { s = s.charAt(0) + (n > 1 ? "." + s.slice(1) : "") + (e < 0 ? "e" : "e+") + e; } else if (e < 0) { for (; ++e; ) s = "0" + s; s = "0." + s; } else if (e > 0) { if (++e > n) { for (e -= n; e--; ) s += "0"; } else if (e < n) { s = s.slice(0, e) + "." + s.slice(e); } } else if (n > 1) { s = s.charAt(0) + "." + s.slice(1); } return x.s < 0 && isNonzero ? "-" + s : s; } __name(stringify, "stringify"); P.abs = function() { var x = new this.constructor(this); x.s = 1; return x; }; P.cmp = function(y) { var isneg, x = this, xc = x.c, yc = (y = new x.constructor(y)).c, i = x.s, j = y.s, k = x.e, l = y.e; if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i; if (i != j) return i; isneg = i < 0; if (k != l) return k > l ^ isneg ? 1 : -1; j = (k = xc.length) < (l = yc.length) ? k : l; for (i = -1; ++i < j; ) { if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1; } return k == l ? 0 : k > l ^ isneg ? 1 : -1; }; P.div = function(y) { var x = this, Big2 = x.constructor, a = x.c, b = (y = new Big2(y)).c, k = x.s == y.s ? 1 : -1, dp = Big2.DP; if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { throw Error(INVALID_DP); } if (!b[0]) { throw Error(DIV_BY_ZERO); } if (!a[0]) { y.s = k; y.c = [y.e = 0]; return y; } var bl, bt, n, cmp, ri, bz = b.slice(), ai = bl = b.length, al = a.length, r = a.slice(0, bl), rl = r.length, q = y, qc = q.c = [], qi = 0, p = dp + (q.e = x.e - y.e) + 1; q.s = k; k = p < 0 ? 0 : p; bz.unshift(0); for (; rl++ < bl; ) r.push(0); do { for (n = 0; n < 10; n++) { if (bl != (rl = r.length)) { cmp = bl > rl ? 1 : -1; } else { for (ri = -1, cmp = 0; ++ri < bl; ) { if (b[ri] != r[ri]) { cmp = b[ri] > r[ri] ? 1 : -1; break; } } } if (cmp < 0) { for (bt = rl == bl ? b : bz; rl; ) { if (r[--rl] < bt[rl]) { ri = rl; for (; ri && !r[--ri]; ) r[ri] = 9; --r[ri]; r[rl] += 10; } r[rl] -= bt[rl]; } for (; !r[0]; ) r.shift(); } else { break; } } qc[qi++] = cmp ? n : ++n; if (r[0] && cmp) r[rl] = a[ai] || 0; else r = [a[ai]]; } while ((ai++ < al || r[0] !== UNDEFINED) && k--); if (!qc[0] && qi != 1) { qc.shift(); q.e--; p--; } if (qi > p) round(q, p, Big2.RM, r[0] !== UNDEFINED); return q; }; P.eq = function(y) { return this.cmp(y) === 0; }; P.gt = function(y) { return this.cmp(y) > 0; }; P.gte = function(y) { return this.cmp(y) > -1; }; P.lt = function(y) { return this.cmp(y) < 0; }; P.lte = function(y) { return this.cmp(y) < 1; }; P.minus = P.sub = function(y) { var i, j, t, xlty, x = this, Big2 = x.constructor, a = x.s, b = (y = new Big2(y)).s; if (a != b) { y.s = -b; return x.plus(y); } var xc = x.c.slice(), xe = x.e, yc = y.c, ye = y.e; if (!xc[0] || !yc[0]) { if (yc[0]) { y.s = -b; } else if (xc[0]) { y = new Big2(x); } else { y.s = 1; } return y; } if (a = xe - ye) { if (xlty = a < 0) { a = -a; t = xc; } else { ye = xe; t = yc; } t.reverse(); for (b = a; b--; ) t.push(0); t.reverse(); } else { j = ((xlty = xc.length < yc.length) ? xc : yc).length; for (a = b = 0; b < j; b++) { if (xc[b] != yc[b]) { xlty = xc[b] < yc[b]; break; } } } if (xlty) { t = xc; xc = yc; yc = t; y.s = -y.s; } if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--; ) xc[i++] = 0; for (b = i; j > a; ) { if (xc[--j] < yc[j]) { for (i = j; i && !xc[--i]; ) xc[i] = 9; --xc[i]; xc[j] += 10; } xc[j] -= yc[j]; } for (; xc[--b] === 0; ) xc.pop(); for (; xc[0] === 0; ) { xc.shift(); --ye; } if (!xc[0]) { y.s = 1; xc = [ye = 0]; } y.c = xc; y.e = ye; return y; }; P.mod = function(y) { var ygtx, x = this, Big2 = x.constructor, a = x.s, b = (y = new Big2(y)).s; if (!y.c[0]) { throw Error(DIV_BY_ZERO); } x.s = y.s = 1; ygtx = y.cmp(x) == 1; x.s = a; y.s = b; if (ygtx) return new Big2(x); a = Big2.DP; b = Big2.RM; Big2.DP = Big2.RM = 0; x = x.div(y); Big2.DP = a; Big2.RM = b; return this.minus(x.times(y)); }; P.neg = function() { var x = new this.constructor(this); x.s = -x.s; return x; }; P.plus = P.add = function(y) { var e, k, t, x = this, Big2 = x.constructor; y = new Big2(y); if (x.s != y.s) { y.s = -y.s; return x.minus(y); } var xe = x.e, xc = x.c, ye = y.e, yc = y.c; if (!xc[0] || !yc[0]) { if (!yc[0]) { if (xc[0]) { y = new Big2(x); } else { y.s = x.s; } } return y; } xc = xc.slice(); if (e = xe - ye) { if (e > 0) { ye = xe; t = yc; } else { e = -e; t = xc; } t.reverse(); for (; e--; ) t.push(0); t.reverse(); } if (xc.length - yc.length < 0) { t = yc; yc = xc; xc = t; } e = yc.length; for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0; if (k) { xc.unshift(k); ++ye; } for (e = xc.length; xc[--e] === 0; ) xc.pop(); y.c = xc; y.e = ye; return y; }; P.pow = function(n) { var x = this, one = new x.constructor("1"), y = one, isneg = n < 0; if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) { throw Error(INVALID + "exponent"); } if (isneg) n = -n; for (; ; ) { if (n & 1) y = y.times(x); n >>= 1; if (!n) break; x = x.times(x); } return isneg ? one.div(y) : y; }; P.prec = function(sd, rm) { if (sd !== ~~sd || sd < 1 || sd > MAX_DP) { throw Error(INVALID + "precision"); } return round(new this.constructor(this), sd, rm); }; P.round = function(dp, rm) { if (dp === UNDEFINED) dp = 0; else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) { throw Error(INVALID_DP); } return round(new this.constructor(this), dp + this.e + 1, rm); }; P.sqrt = function() { var r, c, t, x = this, Big2 = x.constructor, s = x.s, e = x.e, half = new Big2("0.5"); if (!x.c[0]) return new Big2(x); if (s < 0) { throw Error(NAME + "No square root"); } s = Math.sqrt(+stringify(x, true, true)); if (s === 0 || s === 1 / 0) { c = x.c.join(""); if (!(c.length + e & 1)) c += "0"; s = Math.sqrt(c); e = ((e + 1) / 2 | 0) - (e < 0 || e & 1); r = new Big2((s == 1 / 0 ? "5e" : (s = s.toExponential()).slice(0, s.indexOf("e") + 1)) + e); } else { r = new Big2(s + ""); } e = r.e + (Big2.DP += 4); do { t = r; r = half.times(t.plus(x.div(t))); } while (t.c.slice(0, e).join("") !== r.c.slice(0, e).join("")); return round(r, (Big2.DP -= 4) + r.e + 1, Big2.RM); }; P.times = P.mul = function(y) { var c, x = this, Big2 = x.constructor, xc = x.c, yc = (y = new Big2(y)).c, a = xc.length, b = yc.length, i = x.e, j = y.e; y.s = x.s == y.s ? 1 : -1; if (!xc[0] || !yc[0]) { y.c = [y.e = 0]; return y; } y.e = i + j; if (a < b) { c = xc; xc = yc; yc = c; j = a; a = b; b = j; } for (c = new Array(j = a + b); j--; ) c[j] = 0; for (i = b; i--; ) { b = 0; for (j = a + i; j > i; ) { b = c[j] + yc[i] * xc[j - i - 1] + b; c[j--] = b % 10; b = b / 10 | 0; } c[j] = b; } if (b) ++y.e; else c.shift(); for (i = c.length; !c[--i]; ) c.pop(); y.c = c; return y; }; P.toExponential = function(dp, rm) { var x = this, n = x.c[0]; if (dp !== UNDEFINED) { if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { throw Error(INVALID_DP); } x = round(new x.constructor(x), ++dp, rm); for (; x.c.length < dp; ) x.c.push(0); } return stringify(x, true, !!n); }; P.toFixed = function(dp, rm) { var x = this, n = x.c[0]; if (dp !== UNDEFINED) { if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { throw Error(INVALID_DP); } x = round(new x.constructor(x), dp + x.e + 1, rm); for (dp = dp + x.e + 1; x.c.length < dp; ) x.c.push(0); } return stringify(x, false, !!n); }; P[Symbol.for("nodejs.util.inspect.custom")] = P.toJSON = P.toString = function() { var x = this, Big2 = x.constructor; return stringify(x, x.e <= Big2.NE || x.e >= Big2.PE, !!x.c[0]); }; P.toNumber = function() { var n = +stringify(this, true, true); if (this.constructor.strict === true && !this.eq(n.toString())) { throw Error(NAME + "Imprecise conversion"); } return n; }; P.toPrecision = function(sd, rm) { var x = this, Big2 = x.constructor, n = x.c[0]; if (sd !== UNDEFINED) { if (sd !== ~~sd || sd < 1 || sd > MAX_DP) { throw Error(INVALID + "precision"); } x = round(new Big2(x), sd, rm); for (; x.c.length < sd; ) x.c.push(0); } return stringify(x, sd <= x.e || x.e <= Big2.NE || x.e >= Big2.PE, !!n); }; P.valueOf = function() { var x = this, Big2 = x.constructor; if (Big2.strict === true) { throw Error(NAME + "valueOf disallowed"); } return stringify(x, x.e <= Big2.NE || x.e >= Big2.PE, true); }; var Big = _Big_(); var big_default = Big; // ../utils/src/index.ts var src_exports2 = {}; __export(src_exports2, { LsPrefix: () => LsPrefix, SCHEMA: () => SCHEMA, base64ToBytes: () => base64ToBytes, bytesToBase64: () => bytesToBase64, canSignWithLAK: () => canSignWithLAK, convertUnit: () => convertUnit, createDefaultStorage: () => createDefaultStorage, deepCopy: () => deepCopy, exp: () => exp, fromBase58: () => base58_to_binary_default, fromBase64: () => fromBase64, fromHex: () => fromHex, keyFromString: () => keyFromString, keyToString: () => keyToString, lsGet: () => lsGet, lsSet: () => lsSet, mapAction: () => mapAction, mapTransaction: () => mapTransaction, memoryStore: () => memoryStore, parseJsonFromBytes: () => parseJsonFromBytes, privateKeyFromRandom: () => privateKeyFromRandom, publicKeyFromPrivate: () => publicKeyFromPrivate, serializeSignedTransaction: () => serializeSignedTransaction, serializeTransaction: () => serializeTransaction, sha256: () => sha256, signBytes: () => signBytes, signHash: () => signHash, storage: () => storage, toBase58: () => binary_to_base58_default, toBase64: () => toBase64, toHex: () => toHex, tryParseJson: () => tryParseJson, txToJson: () => txToJson, txToJsonStringified: () => txToJsonStringified }); // ../../node_modules/@noble/hashes/esm/_assert.js function isBytes(a) { return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array"; } __name(isBytes, "isBytes"); function abytes(b, ...lengths) { if (!isBytes(b)) throw new Error("Uint8Array expected"); if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length); } __name(abytes, "abytes"); function aexists(instance, checkFinished = true) { if (instance.destroyed) throw new Error("Hash instance has been destroyed"); if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called"); } __name(aexists, "aexists"); function aoutput(out, instance) { abytes(out); const min = instance.outputLen; if (out.length < min) { throw new Error("digestInto() expects output buffer of length at least " + min); } } __name(aoutput, "aoutput"); // ../../node_modules/@noble/hashes/esm/crypto.js var crypto2 = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0; // ../../node_modules/@noble/hashes/esm/utils.js function createView(arr) { return new DataView(arr.buffer, arr.byteOffset, arr.byteLength); } __name(createView, "createView"); function rotr(word, shift) { return word << 32 - shift | word >>> shift; } __name(rotr, "rotr"); function utf8ToBytes(str) { if (typeof str !== "string") throw new Error("utf8ToBytes expected string, got " + typeof str); return new Uint8Array(new TextEncoder().encode(str)); } __name(utf8ToBytes, "utf8ToBytes"); function toBytes(data) { if (typeof data === "string") data = utf8ToBytes(data); abytes(data); return data; } __name(toBytes, "toBytes"); var Hash = class { static { __name(this, "Hash"); } // Safe version that clones internal state clone() { return this._cloneInto(); } }; function wrapConstructor(hashCons) { const hashC = /* @__PURE__ */ __name((msg) => hashCons().update(toBytes(msg)).digest(), "hashC"); const tmp = hashCons(); hashC.outputLen = tmp.outputLen; hashC.blockLen = tmp.blockLen; hashC.create = () => hashCons(); return hashC; } __name(wrapConstructor, "wrapConstructor"); function randomBytes(bytesLength = 32) { if (crypto2 && typeof crypto2.getRandomValues === "function") { return crypto2.getRandomValues(new Uint8Array(bytesLength)); } if (crypto2 && typeof crypto2.randomBytes === "function") { return crypto2.randomBytes(bytesLength); } throw new Error("crypto.getRandomValues must be defined"); } __name(randomBytes, "randomBytes"); // ../../node_modules/@noble/hashes/esm/_md.js function setBigUint64(view2, byteOffset, value, isLE) { if (typeof view2.setBigUint64 === "function") return view2.setBigUint64(byteOffset, value, isLE); const _32n2 = BigInt(32); const _u32_max = BigInt(4294967295); const wh = Number(value >> _32n2 & _u32_max); const wl = Number(value & _u32_max); const h = isLE ? 4 : 0; const l = isLE ? 0 : 4; view2.setUint32(byteOffset + h, wh, isLE); view2.setUint32(byteOffset + l, wl, isLE); } __name(setBigUint64, "setBigUint64"); function Chi(a, b, c) { return a & b ^ ~a & c; } __name(Chi, "Chi"); function Maj(a, b, c) { return a & b ^ a & c ^ b & c; } __name(Maj, "Maj"); var HashMD = class extends Hash { static { __name(this, "HashMD"); } constructor(blockLen, outputLen, padOffset, isLE) { super(); this.blockLen = blockLen; this.outputLen = outputLen; this.padOffset = padOffset; this.isLE = isLE; this.finished = false; this.length = 0; this.pos = 0; this.destroyed = false; this.buffer = new Uint8Array(blockLen); this.view = createView(this.buffer); } update(data) { aexists(this); const { view: view2, buffer, blockLen } = this; data = toBytes(data); const len = data.length; for (let pos = 0; pos < len; ) { const take = Math.min(blockLen - this.pos, len - pos); if (take === blockLen) { const dataView = createView(data); for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos); continue; } buffer.set(data.subarray(pos, pos + take), this.pos); this.pos += take; pos += take; if (this.pos === blockLen) { this.process(view2, 0); this.pos = 0; } } this.length += data.length; this.roundClean(); return this; } digestInto(out) { aexists(this); aoutput(out, this); this.finished = true; const { buffer, view: view2, blockLen, isLE } = this; let { pos } = this; buffer[pos++] = 128; this.buffer.subarray(pos).fill(0); if (this.padOffset > blockLen - pos) { this.process(view2, 0); pos = 0; } for (let i = pos; i < blockLen; i++) buffer[i] = 0; setBigUint64(view2, blockLen - 8, BigInt(this.length * 8), isLE); this.process(view2, 0); const oview = createView(out); const len = this.outputLen; if (len % 4) throw new Error("_sha2: outputLen should be aligned to 32bit"); const outLen = len / 4; const state2 = this.get(); if (outLen > state2.length) throw new Error("_sha2: outputLen bigger than state"); for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state2[i], isLE); } digest() { const { buffer, outputLen } = this; this.digestInto(buffer); const res = buffer.slice(0, outputLen); this.destroy(); return res; } _cloneInto(to) { to || (to = new this.constructor()); to.set(...this.get()); const { blockLen, buffer, length, finished, destroyed, pos } = this; to.length = length; to.pos = pos; to.finished = finished; to.destroyed = destroyed; if (length % blockLen) to.buffer.set(buffer); return to; } }; // ../../node_modules/@noble/hashes/esm/_u64.js var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1); var _32n = /* @__PURE__ */ BigInt(32); function fromBig(n, le = false) { if (le) return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) }; return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 }; } __name(fromBig, "fromBig"); function split(lst, le = false) { let Ah = new Uint32Array(lst.length); let Al = new Uint32Array(lst.length); for (let i = 0; i < lst.length; i++) { const { h, l } = fromBig(lst[i], le); [Ah[i], Al[i]] = [h, l]; } return [Ah, Al]; } __name(split, "split"); var toBig = /* @__PURE__ */ __name((h, l) => BigInt(h >>> 0) << _32n | BigInt(l >>> 0), "toBig"); var shrSH = /* @__PURE__ */ __name((h, _l, s) => h >>> s, "shrSH"); var shrSL = /* @__PURE__ */ __name((h, l, s) => h << 32 - s | l >>> s, "shrSL"); var rotrSH = /* @__PURE__ */ __name((h, l, s) => h >>> s | l << 32 - s, "rotrSH"); var rotrSL = /* @__PURE__ */ __name((h, l, s) => h << 32 - s | l >>> s, "rotrSL"); var rotrBH = /* @__PURE__ */ __name((h, l, s) => h << 64 - s | l >>> s - 32, "rotrBH"); var rotrBL = /* @__PURE__ */ __name((h, l, s) => h >>> s - 32 | l << 64 - s, "rotrBL"); var rotr32H = /* @__PURE__ */ __name((_h, l) => l, "rotr32H"); var rotr32L = /* @__PURE__ */ __name((h, _l) => h, "rotr32L"); var rotlSH = /* @__PURE__ */ __name((h, l, s) => h << s | l >>> 32 - s, "rotlSH"); var rotlSL = /* @__PURE__ */ __name((h, l, s) => l << s | h >>> 32 - s, "rotlSL"); var rotlBH = /* @__PURE__ */ __name((h, l, s) => l << s - 32 | h >>> 64 - s, "rotlBH"); var rotlBL = /* @__PURE__ */ __name((h, l, s) => h << s - 32 | l >>> 64 - s, "rotlBL"); function add(Ah, Al, Bh, Bl) { const l = (Al >>> 0) + (Bl >>> 0); return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 }; } __name(add, "add"); var add3L = /* @__PURE__ */ __name((Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0), "add3L"); var add3H = /* @__PURE__ */ __name((low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0, "add3H"); var add4L = /* @__PURE__ */ __name((Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0), "add4L"); var add4H = /* @__PURE__ */ __name((low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0, "add4H"); var add5L = /* @__PURE__ */ __name((Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0), "add5L"); var add5H = /* @__PURE__ */ __name((low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0, "add5H"); var u64 = { fromBig, split, toBig, shrSH, shrSL, rotrSH, rotrSL, rotrBH, rotrBL, rotr32H, rotr32L, rotlSH, rotlSL, rotlBH, rotlBL, add, add3L, add3H, add4L, add4H, add5H, add5L }; var u64_default = u64; // ../../node_modules/@noble/hashes/esm/sha512.js var [SHA512_Kh, SHA512_Kl] = /* @__PURE__ */ (() => u64_default.split([ "0x428a2f98d728ae22", "0x7137449123ef65cd", "0xb5c0fbcfec4d3b2f", "0xe9b5dba58189dbbc", "0x3956c25bf348b538", "0x59f111f1b605d019", "0x923f82a4af194f9b", "0xab1c5ed5da6d8118", "0xd807aa98a3030242", "0x12835b0145706fbe", "0x243185be4ee4b28c", "0x550c7dc3d5ffb4e2", "0x72be5d74f27b896f", "0x80deb1fe3b1696b1", "0x9bdc06a725c71235", "0xc19bf174cf692694", "0xe49b69c19ef14ad2", "0xefbe4786384f25e3", "0x0fc19dc68b8cd5b5", "0x240ca1cc77ac9c65", "0x2de92c6f592b0275", "0x4a7484aa6ea6e483", "0x5cb0a9dcbd41fbd4", "0x76f988da831153b5", "0x983e5152ee66dfab", "0xa831c66d2db43210", "0xb00327c898fb213f", "0xbf597fc7beef0ee4", "0xc6e00bf33da88fc2", "0xd5a79147930aa725", "0x06ca6351e003826f", "0x142929670a0e6e70", "0x27b70a8546d22ffc", "0x2e1b21385c26c926", "0x4d2c6dfc5ac42aed", "0x53380d139d95b3df", "0x650a73548baf63de", "0x766a0abb3c77b2a8", "0x81c2c92e47edaee6", "0x92722c851482353b", "0xa2bfe8a14cf10364", "0xa81a664bbc423001", "0xc24b8b70d0f89791", "0xc76c51a30654be30", "0xd192e819d6ef5218", "0xd69906245565a910", "0xf40e35855771202a", "0x106aa07032bbd1b8", "0x19a4c116b8d2d0c8", "0x1e376c085141ab53", "0x2748774cdf8eeb99", "0x34b0bcb5e19b48a8", "0x391c0cb3c5c95a63", "0x4ed8aa4ae3418acb", "0x5b9cca4f7763e373", "0x682e6ff3d6b2b8a3", "0x748f82ee5defb2fc", "0x78a5636f43172f60", "0x84c87814a1f0ab72", "0x8cc702081a6439ec", "0x90befffa23631e28", "0xa4506cebde82bde9", "0xbef9a3f7b2c67915", "0xc67178f2e372532b", "0xca273eceea26619c", "0xd186b8c721c0c207", "0xeada7dd6cde0eb1e", "0xf57d4f7fee6ed178", "0x06f067aa72176fba", "0x0a637dc5a2c898a6", "0x113f9804bef90dae", "0x1b710b35131c471b", "0x28db77f523047d84", "0x32caab7b40c72493", "0x3c9ebe0a15c9bebc", "0x431d67c49c100d4c", "0x4cc5d4becb3e42b6", "0x597f299cfc657e2a", "0x5fcb6fab3ad6faec", "0x6c44198c4a475817" ].map((n) => BigInt(n))))(); var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80); var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80); var SHA512 = class extends HashMD { static { __name(this, "SHA512"); } constructor() { super(128, 64, 16, false); this.Ah = 1779033703 | 0; this.Al = 4089235720 | 0; this.Bh = 3144134277 | 0; this.Bl = 2227873595 | 0; this.Ch = 1013904242 | 0; this.Cl = 4271175723 | 0; this.Dh = 2773480762 | 0; this.Dl = 1595750129 | 0; this.Eh = 1359893119 | 0; this.El = 2917565137 | 0; this.Fh = 2600822924 | 0; this.Fl = 725511199 | 0; this.Gh = 528734635 | 0; this.Gl = 4215389547 | 0; this.Hh = 1541459225 | 0; this.Hl = 327033209 | 0; } // prettier-ignore get() { const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this; return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl]; } // prettier-ignore set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) { this.Ah = Ah | 0; this.Al = Al | 0; this.Bh = Bh | 0; this.Bl = Bl | 0; this.Ch = Ch | 0; this.Cl = Cl | 0; this.Dh = Dh | 0; this.Dl = Dl | 0; this.Eh = Eh | 0; this.El = El | 0; this.Fh = Fh | 0; this.Fl = Fl | 0; this.Gh = Gh | 0; this.Gl = Gl | 0; this.Hh = Hh | 0; this.Hl = Hl | 0; } process(view2, offset) { for (let i = 0; i < 16; i++, offset += 4) { SHA512_W_H[i] = view2.getUint32(offset); SHA512_W_L[i] = view2.getUint32(offset += 4); } for (let i = 16; i < 80; i++) { const W15h = SHA512_W_H[i - 15] | 0; const W15l = SHA512_W_L[i - 15] | 0; const s0h = u64_default.rotrSH(W15h, W15l, 1) ^ u64_default.rotrSH(W15h, W15l, 8) ^ u64_default.shrSH(W15h, W15l, 7); const s0l = u64_default.rotrSL(W15h, W15l, 1) ^ u64_default.rotrSL(W15h, W15l, 8) ^ u64_default.shrSL(W15h, W15l, 7); const W2h = SHA512_W_H[i - 2] | 0; const W2l = SHA512_W_L[i - 2] | 0; const s1h = u64_default.rotrSH(W2h, W2l, 19) ^ u64_default.rotrBH(W2h, W2l, 61) ^ u64_default.shrSH(W2h, W2l, 6); const s1l = u64_default.rotrSL(W2h, W2l, 19) ^ u64_default.rotrBL(W2h, W2l, 61) ^ u64_default.shrSL(W2h, W2l, 6); const SUMl = u64_default.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]); const SUMh = u64_default.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]); SHA512_W_H[i] = SUMh | 0; SHA512_W_L[i] = SUMl | 0; } let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this; for (let i = 0; i < 80; i++) { const sigma1h = u64_default.rotrSH(Eh, El, 14) ^ u64_default.rotrSH(Eh, El, 18) ^ u64_default.rotrBH(Eh, El, 41); const sigma1l = u64_default.rotrSL(Eh, El, 14) ^ u64_default.rotrSL(Eh, El, 18) ^ u64_default.rotrBL(Eh, El, 41); const CHIh = Eh & Fh ^ ~Eh & Gh; const CHIl = El & Fl ^ ~El & Gl; const T1ll = u64_default.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]); const T1h = u64_default.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]); const T1l = T1ll | 0; const sigma0h = u64_default.rotrSH(Ah, Al, 28) ^ u64_default.rotrBH(Ah, Al, 34) ^ u64_default.rotrBH(Ah, Al, 39); const sigma0l = u64_default.rotrSL(Ah, Al, 28) ^ u64_default.rotrBL(Ah, Al, 34) ^ u64_default.rotrBL(Ah, Al, 39); const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch; const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl; Hh = Gh | 0; Hl = Gl | 0; Gh = Fh | 0; Gl = Fl | 0; Fh = Eh | 0; Fl = El | 0; ({ h: Eh, l: El } = u64_default.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0)); Dh = Ch | 0; Dl = Cl | 0; Ch = Bh | 0; Cl = Bl | 0; Bh = Ah | 0; Bl = Al | 0; const All = u64_default.add3L(T1l, sigma0l, MAJl); Ah = u64_default.add3H(All, T1h, sigma0h, MAJh); Al = All | 0; } ({ h: Ah, l: Al } = u64_default.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0)); ({ h: Bh, l: Bl } = u64_default.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0)); ({ h: Ch, l: Cl } = u64_default.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0)); ({ h: Dh, l: Dl } = u64_default.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0)); ({ h: Eh, l: El } = u64_default.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0)); ({ h: Fh, l: Fl } = u64_default.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0)); ({ h: Gh, l: Gl } = u64_default.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0)); ({ h: Hh, l: Hl } = u64_default.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0)); this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl); } roundClean() { SHA512_W_H.fill(0); SHA512_W_L.fill(0); } destroy() { this.buffer.fill(0); this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } }; var sha512 = /* @__PURE__ */ wrapConstructor(() => new SHA512()); // ../../node_modules/@noble/curves/esm/abstract/utils.js var _0n = /* @__PURE__ */ BigInt(0); var _1n = /* @__PURE__ */ BigInt(1); var _2n = /* @__PURE__ */ BigInt(2); function isBytes2(a) { return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array"; } __name(isBytes2, "isBytes"); function abytes2(item) { if (!isBytes2(item)) throw new Error("Uint8Array expected"); } __name(abytes2, "abytes"); function abool(title, value) { if (typeof value !== "boolean") throw new Error(title + " boolean expected, got " + value); } __name(abool, "abool"); var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0")); function bytesToHex(bytes) { abytes2(bytes); let hex = ""; for (let i = 0; i < bytes.length; i++) { hex += hexes[bytes[i]]; } return hex; } __name(bytesToHex, "bytesToHex"); function hexToNumber(hex) { if (typeof hex !== "string") throw new Error("hex string expected, got " + typeof hex); return hex === "" ? _0n : BigInt("0x" + hex); } __name(hexToNumber, "hexToNumber"); var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; function asciiToBase16(ch) { if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0; if (ch >= asciis.A && ch <= asciis.F) return ch - (asciis.A - 10); if (ch >= asciis.a && ch <= asciis.f) return ch - (asciis.a - 10); return; } __name(asciiToBase16, "asciiToBase16"); function hexToBytes(hex) { if (typeof hex !== "string") throw new Error("hex string expected, got " + typeof hex); const hl = hex.length; const al = hl / 2; if (hl % 2) throw new Error("hex string expected, got unpadded hex of length " + hl); const array = new Uint8Array(al); for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) { const n1 = asciiToBase16(hex.charCodeAt(hi)); const n2 = asciiToBase16(hex.charCodeAt(hi + 1)); if (n1 === void 0 || n2 === void 0) { const char = hex[hi] + hex[hi + 1]; throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi); } array[ai] = n1 * 16 + n2; } return array; } __name(hexToBytes, "hexToBytes"); function bytesToNumberBE(bytes) { return hexToNumber(bytesToHex(bytes)); } __name(bytesToNumberBE, "bytesToNumberBE"); function bytesToNumberLE(bytes) { abytes2(bytes); return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse())); } __name(bytesToNumberLE, "bytesToNumberLE"); function numberToBytesBE(n, len) { return hexToBytes(n.toString(16).padStart(len * 2, "0")); } __name(numberToBytesBE, "numberToBytesBE"); function numberToBytesLE(n, len) { return numberToBytesBE(n, len).reverse(); } __name(numberToBytesLE, "numberToBytesLE"); function ensureBytes(title, hex, expectedLength) { let res; if (typeof hex === "string") { try { res = hexToBytes(hex); } catch (e) { throw new Error(title + " must be hex string or Uint8Array, cause: " + e); } } else if (isBytes2(hex)) { res = Uint8Array.from(hex); } else { throw new Error(title + " must be hex string or Uint8Array"); } const len = res.length; if (typeof expectedLength === "number" && len !== expectedLength) throw new Error(title + " of length " + expectedLength + " expected, got " + len); return res; } __name(ensureBytes, "ensureBytes"); function concatBytes(...arrays) { let sum = 0; for (let i = 0; i < arrays.length; i++) { const a = arrays[i]; abytes2(a); sum += a.length; } const res = new Uint8Array(sum); for (let i = 0, pad = 0; i < arrays.length; i++) { const a = arrays[i]; res.set(a, pad); pad += a.length; } return res; } __name(concatBytes, "concatBytes"); var isPosBig = /* @__PURE__ */ __name((n) => typeof n === "bigint" && _0n <= n, "isPosBig"); function inRange(n, min, max) { return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max; } __name(inRange, "inRange"); function aInRange(title, n, min, max) { if (!inRange(n, min, max)) throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n); } __name(aInRange, "aInRange"); function bitLen(n) { let len; for (len = 0; n > _0n; n >>= _1n, len += 1) ; return len; } __name(bitLen, "bitLen"); var bitMask = /* @__PURE__ */ __name((n) => (_2n << BigInt(n - 1)) - _1n, "bitMask"); var validatorFns = { bigint: /* @__PURE__ */ __name((val) => typeof val === "bigint", "bigint"), function: /* @__PURE__ */ __name((val) => typeof val === "function", "function"), boolean: /* @__PURE__ */ __name((val) => typeof val === "boolean", "boolean"), string: /* @__PURE__ */ __name((val) => typeof val === "string", "string"), stringOrUint8Array: /* @__PURE__ */ __name((val) => typeof val === "string" || isBytes2(val), "stringOrUint8Array"), isSafeInteger: /* @__PURE__ */ __name((val) => Number.isSafeInteger(val), "isSafeInteger"), array: /* @__PURE__ */ __name((val) => Array.isArray(val), "array"), field: /* @__PURE__ */ __name((val, object) => object.Fp.isValid(val), "field"), hash: /* @__PURE__ */ __name((val) => typeof val === "function" && Number.isSafeInteger(val.outputLen), "hash") }; function validateObject(object, validators, optValidators = {}) { const checkField = /* @__PURE__ */ __name((fieldName, type, isOptional) => { const checkVal = validatorFns[type]; if (typeof checkVal !== "function") throw new Error("invalid validator function"); const val = object[fieldName]; if (isOptional && val === void 0) return; if (!checkVal(val, object)) { throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val); } }, "checkField"); for (const [fieldName, type] of Object.entries(validators)) checkField(fieldName, type, false); for (const [fieldName, type] of Object.entries(optValidators)) checkField(fieldName, type, true); return object; } __name(validateObject, "validateObject"); function memoized(fn) { const map = /* @__PURE__ */ new WeakMap(); return (arg, ...args) => { const val = map.get(arg); if (val !== void 0) return val; const computed = fn(arg, ...args); map.set(arg, computed); return computed; }; } __name(memoized, "memoized"); // ../../node_modules/@noble/curves/esm/abstract/modular.js var _0n2 = BigInt(0); var _1n2 = BigInt(1); var _2n2 = /* @__PURE__ */ BigInt(2); var _3n = /* @__PURE__ */ BigInt(3); var _4n = /* @__PURE__ */ BigInt(4); var _5n = /* @__PURE__ */ BigInt(5); var _8n = /* @__PURE__ */ BigInt(8); var _9n = /* @__PURE__ */ BigInt(9); var _16n = /* @__PURE__ */ BigInt(16); function mod(a, b) { const result = a % b; return result >= _0n2 ? result : b + result; } __name(mod, "mod"); function pow(num, power, modulo) { if (power < _0n2) throw new Error("invalid exponent, negatives unsupported"); if (modulo <= _0n2) throw new Error("invalid modulus"); if (modulo === _1n2) return _0n2; let res = _1n2; while (power > _0n2) { if (power & _1n2) res = res * num % modulo; num = num * num % modulo; power >>= _1n2; } return res; } __name(pow, "pow"); function pow2(x, power, modulo) { let res = x; while (power-- > _0n2) { res *= res; res %= modulo; } return res; } __name(pow2, "pow2"); function invert(number, modulo) { if (number === _0n2) throw new Error("invert: expected non-zero number"); if (modulo <= _0n2) throw new Error("invert: expected positive modulus, got " + modulo); let a = mod(number, modulo); let b = modulo; let x = _0n2, y = _1n2, u = _1n2, v = _0n2; while (a !== _0n2) { const q = b / a; const r = b % a; const m = x - u * q; const n = y - v * q; b = a, a = r, x = u, y = v, u = m, v = n; } const gcd = b; if (gcd !== _1n2) throw new Error("invert: does not exist"); return mod(x, modulo); } __name(invert, "invert"); function tonelliShanks(P2) { const legendreC = (P2 - _1n2) / _2n2; let Q, S, Z; for (Q = P2 - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++) ; for (Z = _2n2; Z < P2 && pow(Z, legendreC, P2) !== P2 - _1n2; Z++) { if (Z > 1e3) throw new Error("Cannot find square root: likely non-prime P"); } if (S === 1) { const p1div4 = (P2 + _1n2) / _4n; return /* @__PURE__ */ __name(function tonelliFast(Fp2, n) { const root = Fp2.pow(n, p1div4); if (!Fp2.eql(Fp2.sqr(root), n)) throw new Error("Cannot find square root"); return root; }, "tonelliFast"); } const Q1div2 = (Q + _1n2) / _2n2; return /* @__PURE__ */ __name(function tonelliSlow(Fp2, n) { if (Fp2.pow(n, legendreC) === Fp2.neg(Fp2.ONE)) throw new Error("Cannot find square root"); let r = S; let g = Fp2.pow(Fp2.mul(Fp2.ONE, Z), Q); let x = Fp2.pow(n, Q1div2); let b = Fp2.pow(n, Q); while (!Fp2.eql(b, Fp2.ONE)) { if (Fp2.eql(b, Fp2.ZERO)) return Fp2.ZERO; let m = 1; for (let t2 = Fp2.sqr(b); m < r; m++) { if (Fp2.eql(t2, Fp2.ONE)) break; t2 = Fp2.sqr(t2); } const ge = Fp2.pow(g, _1n2 << BigInt(r - m - 1)); g = Fp2.sqr(ge); x = Fp2.mul(x, ge); b = Fp2.mul(b, g); r = m; } return x; }, "tonelliSlow"); } __name(tonelliShanks, "tonelliShanks"); function FpSqrt(P2) { if (P2 % _4n === _3n) { const p1div4 = (P2 + _1n2) / _4n; return /* @__PURE__ */ __name(function sqrt3mod4(Fp2, n) { const root = Fp2.pow(n, p1div4); if (!Fp2.eql(Fp2.sqr(root), n)) throw new Error("Cannot find square root"); return root; }, "sqrt3mod4"); } if (P2 % _8n === _5n) { const c1 = (P2 - _5n) / _8n; return /* @__PURE__ */ __name(function sqrt5mod8(Fp2, n) { const n2 = Fp2.mul(n, _2n2); const v = Fp2.pow(n2, c1); const nv = Fp2.mul(n, v); const i = Fp2.mul(Fp2.mul(nv, _2n2), v); const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE)); if (!Fp2.eql(Fp2.sqr(root), n)) throw new Error("Cannot find square root"); return root; }, "sqrt5mod8"); } if (P2 % _16n === _9n) { } return tonelliShanks(P2); } __name(FpSqrt, "FpSqrt"); var isNegativeLE = /* @__PURE__ */ __name((num, modulo) => (mod(num, modulo) & _1n2) === _1n2, "isNegativeLE"); var FIELD_FIELDS = [ "create", "isValid", "is0", "neg", "inv", "sqrt", "sqr", "eql", "add", "sub", "mul", "pow", "div", "addN", "subN", "mulN", "sqrN" ]; function validateField(field) { const initial = { ORDER: "bigint", MASK: "bigint", BYTES: "isSafeInteger", BITS: "isSafeInteger" }; const opts = FIELD_FIELDS.reduce((map, val) => { map[val] = "function"; return map; }, initial); return validateObject(field, opts); } __name(validateField, "validateField"); function FpPow(f, num, power) { if (power < _0n2) throw new Error("invalid exponent, negatives unsupported"); if (power === _0n2) return f.ONE; if (power === _1n2) return num; let p = f.ONE; let d = num; while (power > _0n2) { if (power & _1n2) p = f.mul(p, d); d = f.sqr(d); power >>= _1n2; } return p; } __name(FpPow, "FpPow"); function FpInvertBatch(f, nums) { const tmp = new Array(nums.length); const lastMultiplied = nums.reduce((acc, num, i) => { if (f.is0(num)) return acc; tmp[i] = acc; return f.mul(acc, num); }, f.ONE); const inverted = f.inv(lastMultiplied); nums.reduceRight((acc, num, i) => { if (f.is0(num)) return acc; tmp[i] = f.mul(acc, tmp[i]); return f.mul(acc, num); }, inverted); return tmp; } __name(FpInvertBatch, "FpInvertBatch"); function nLength(n, nBitLength) { const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length; const nByteLength = Math.ceil(_nBitLength / 8); return { nBitLength: _nBitLength, nByteLength }; } __name(nLength, "nLength"); function Field(ORDER, bitLen2, isLE = false, redef = {}) { if (ORDER <= _0n2) throw new Error("invalid field: expected ORDER > 0, got " + ORDER); const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2); if (BYTES > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes"); let sqrtP; const f = Object.freeze({ ORDER, isLE, BITS, BYTES, MASK: bitMask(BITS), ZERO: _0n2, ONE: _1n2, create: /* @__PURE__ */ __name((num) => mod(num, ORDER), "create"), isValid: /* @__PURE__ */ __name((num) => { if (typeof num !== "bigint") throw new Error("invalid field element: expected bigint, got " + typeof num); return _0n2 <= num && num < ORDER; }, "isValid"), is0: /* @__PURE__ */ __name((num) => num === _0n2, "is0"), isOdd: /* @__PURE__ */ __name((num) => (num & _1n2) === _1n2, "isOdd"), neg: /* @__PURE__ */ __name((num) => mod(-num, ORDER), "neg"), eql: /* @__PURE__ */ __name((lhs, rhs) => lhs === rhs, "eql"), sqr: /* @__PURE__ */ __name((num) => mod(num * num, ORDER), "sqr"), add: /* @__PURE__ */ __name((lhs, rhs) => mod(lhs + rhs, ORDER), "add"), sub: /* @__PURE__ */ __name((lhs, rhs) => mod(lhs - rhs, ORDER), "sub"), mul: /* @__PURE__ */ __name((lhs, rhs) => mod(lhs * rhs, ORDER), "mul"), pow: /* @__PURE__ */ __name((num, power) => FpPow(f, num, power), "pow"), div: /* @__PURE__ */ __name((lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER), "div"), // Same as above, but doesn't normalize sqrN: /* @__PURE__ */ __name((num) => num * num, "sqrN"), addN: /* @__PURE__ */ __name((lhs, rhs) => lhs + rhs, "addN"), subN: /* @__PURE__ */ __name((lhs, rhs) => lhs - rhs, "subN"), mulN: /* @__PURE__ */ __name((lhs, rhs) => lhs * rhs, "mulN"), inv: /* @__PURE__ */ __name((num) => invert(num, ORDER), "inv"), sqrt: redef.sqrt || ((n) => { if (!sqrtP) sqrtP = FpSqrt(ORDER); return sqrtP(f, n); }), invertBatch: /* @__PURE__ */ __name((lst) => FpInvertBatch(f, lst), "invertBatch"), // TODO: do we really need constant cmov? // We don't have const-time bigints anyway, so probably will be not very useful cmov: /* @__PURE__ */ __name((a, b, c) => c ? b : a, "cmov"), toBytes: /* @__PURE__ */ __name((num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES), "toBytes"), fromBytes: /* @__PURE__ */ __name((bytes) => { if (bytes.length !== BYTES) throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length); return isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes); }, "fromBytes") }); return Object.freeze(f); } __name(Field, "Field"); // ../../node_modules/@noble/curves/esm/abstract/curve.js var _0n3 = BigInt(0); var _1n3 = BigInt(1); function constTimeNegate(condition, item) { const neg = item.negate(); return condition ? neg : item; } __name(constTimeNegate, "constTimeNegate"); function validateW(W, bits) { if (!Number.isSafeInteger(W) || W <= 0 || W > bits) throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W); } __name(validateW, "validateW"); function calcWOpts(W, bits) { validateW(W, bits); const windows = Math.ceil(bits / W) + 1; const windowSize = 2 ** (W - 1); return { windows, windowSize }; } __name(calcWOpts, "calcWOpts"); function validateMSMPoints(points, c) { if (!Array.isArray(points)) throw new Error("array expected"); points.forEach((p, i) => { if (!(p instanceof c)) throw new Error("invalid point at index " + i); }); } __name(validateMSMPoints, "validateMSMPoints"); function validateMSMScalars(scalars, field) { if (!Array.isArray(scalars)) throw new Error("array of scalars expected"); scalars.forEach((s, i) => { if (!field.isValid(s)) throw new Error("invalid scalar at index " + i); }); } __name(validateMSMScalars, "validateMSMScalars"); var pointPrecomputes = /* @__PURE__ */ new WeakMap(); var pointWindowSizes = /* @__PURE__ */ new WeakMap();