@dfinity/vetkeys
Version:
JavaScript and TypeScript library to use Internet Computer vetKeys
1,473 lines (1,472 loc) • 126 kB
JavaScript
const Gt = typeof globalThis == "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
function me(n) {
return n instanceof Uint8Array || ArrayBuffer.isView(n) && n.constructor.name === "Uint8Array";
}
function Dt(n) {
if (!Number.isSafeInteger(n) || n < 0)
throw new Error("positive integer expected, got " + n);
}
function J(n, ...t) {
if (!me(n))
throw new Error("Uint8Array expected");
if (t.length > 0 && !t.includes(n.length))
throw new Error("Uint8Array expected of length " + t + ", got length=" + n.length);
}
function Pe(n) {
if (typeof n != "function" || typeof n.create != "function")
throw new Error("Hash should be wrapped by utils.createHasher");
Dt(n.outputLen), Dt(n.blockLen);
}
function Zt(n, t = !0) {
if (n.destroyed)
throw new Error("Hash instance has been destroyed");
if (t && n.finished)
throw new Error("Hash#digest() has already been called");
}
function In(n, t) {
J(n);
const r = t.outputLen;
if (n.length < r)
throw new Error("digestInto() expects output buffer of length at least " + r);
}
function dr(n) {
return new Uint32Array(n.buffer, n.byteOffset, Math.floor(n.byteLength / 4));
}
function mt(...n) {
for (let t = 0; t < n.length; t++)
n[t].fill(0);
}
function Ee(n) {
return new DataView(n.buffer, n.byteOffset, n.byteLength);
}
function ft(n, t) {
return n << 32 - t | n >>> t;
}
const ur = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
function lr(n) {
return n << 24 & 4278190080 | n << 8 & 16711680 | n >>> 8 & 65280 | n >>> 24 & 255;
}
function hr(n) {
for (let t = 0; t < n.length; t++)
n[t] = lr(n[t]);
return n;
}
const ze = ur ? (n) => n : hr, Nn = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", br = /* @__PURE__ */ Array.from({ length: 256 }, (n, t) => t.toString(16).padStart(2, "0"));
function jt(n) {
if (J(n), Nn)
return n.toHex();
let t = "";
for (let r = 0; r < n.length; r++)
t += br[n[r]];
return t;
}
const ut = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
function $e(n) {
if (n >= ut._0 && n <= ut._9)
return n - ut._0;
if (n >= ut.A && n <= ut.F)
return n - (ut.A - 10);
if (n >= ut.a && n <= ut.f)
return n - (ut.a - 10);
}
function Tn(n) {
if (typeof n != "string")
throw new Error("hex string expected, got " + typeof n);
if (Nn)
return Uint8Array.fromHex(n);
const t = n.length, r = t / 2;
if (t % 2)
throw new Error("hex string expected, got unpadded hex of length " + t);
const e = new Uint8Array(r);
for (let s = 0, o = 0; s < r; s++, o += 2) {
const c = $e(n.charCodeAt(o)), i = $e(n.charCodeAt(o + 1));
if (c === void 0 || i === void 0) {
const a = n[o] + n[o + 1];
throw new Error('hex string expected, got non-hex character "' + a + '" at index ' + o);
}
e[s] = c * 16 + i;
}
return e;
}
function Wt(n) {
if (typeof n != "string")
throw new Error("string expected");
return new Uint8Array(new TextEncoder().encode(n));
}
function Nt(n) {
return typeof n == "string" && (n = Wt(n)), J(n), n;
}
function V(...n) {
let t = 0;
for (let e = 0; e < n.length; e++) {
const s = n[e];
J(s), t += s.length;
}
const r = new Uint8Array(t);
for (let e = 0, s = 0; e < n.length; e++) {
const o = n[e];
r.set(o, s), s += o.length;
}
return r;
}
class ke {
}
function De(n) {
const t = (e) => n().update(Nt(e)).digest(), r = n();
return t.outputLen = r.outputLen, t.blockLen = r.blockLen, t.create = () => n(), t;
}
function mr(n) {
const t = (e, s) => n(s).update(Nt(e)).digest(), r = n({});
return t.outputLen = r.outputLen, t.blockLen = r.blockLen, t.create = (e) => n(e), t;
}
function gr(n = 32) {
if (Gt && typeof Gt.getRandomValues == "function")
return Gt.getRandomValues(new Uint8Array(n));
if (Gt && typeof Gt.randomBytes == "function")
return Uint8Array.from(Gt.randomBytes(n));
throw new Error("crypto.getRandomValues must be defined");
}
function yr(n, t, r, e) {
if (typeof n.setBigUint64 == "function")
return n.setBigUint64(t, r, e);
const s = BigInt(32), o = BigInt(4294967295), c = Number(r >> s & o), i = Number(r & o), a = e ? 4 : 0, u = e ? 0 : 4;
n.setUint32(t + a, c, e), n.setUint32(t + u, i, e);
}
function pr(n, t, r) {
return n & t ^ ~n & r;
}
function wr(n, t, r) {
return n & t ^ n & r ^ t & r;
}
class qn extends ke {
constructor(t, r, e, s) {
super(), this.finished = !1, this.length = 0, this.pos = 0, this.destroyed = !1, this.blockLen = t, this.outputLen = r, this.padOffset = e, this.isLE = s, this.buffer = new Uint8Array(t), this.view = Ee(this.buffer);
}
update(t) {
Zt(this), t = Nt(t), J(t);
const { view: r, buffer: e, blockLen: s } = this, o = t.length;
for (let c = 0; c < o; ) {
const i = Math.min(s - this.pos, o - c);
if (i === s) {
const a = Ee(t);
for (; s <= o - c; c += s)
this.process(a, c);
continue;
}
e.set(t.subarray(c, c + i), this.pos), this.pos += i, c += i, this.pos === s && (this.process(r, 0), this.pos = 0);
}
return this.length += t.length, this.roundClean(), this;
}
digestInto(t) {
Zt(this), In(t, this), this.finished = !0;
const { buffer: r, view: e, blockLen: s, isLE: o } = this;
let { pos: c } = this;
r[c++] = 128, mt(this.buffer.subarray(c)), this.padOffset > s - c && (this.process(e, 0), c = 0);
for (let d = c; d < s; d++)
r[d] = 0;
yr(e, s - 8, BigInt(this.length * 8), o), this.process(e, 0);
const i = Ee(t), a = this.outputLen;
if (a % 4)
throw new Error("_sha2: outputLen should be aligned to 32bit");
const u = a / 4, l = this.get();
if (u > l.length)
throw new Error("_sha2: outputLen bigger than state");
for (let d = 0; d < u; d++)
i.setUint32(4 * d, l[d], o);
}
digest() {
const { buffer: t, outputLen: r } = this;
this.digestInto(t);
const e = t.slice(0, r);
return this.destroy(), e;
}
_cloneInto(t) {
t || (t = new this.constructor()), t.set(...this.get());
const { blockLen: r, buffer: e, length: s, finished: o, destroyed: c, pos: i } = this;
return t.destroyed = c, t.finished = o, t.length = s, t.pos = i, s % r && t.buffer.set(e), t;
}
clone() {
return this._cloneInto();
}
}
const gt = /* @__PURE__ */ Uint32Array.from([
1779033703,
3144134277,
1013904242,
2773480762,
1359893119,
2600822924,
528734635,
1541459225
]), yt = /* @__PURE__ */ Uint32Array.from([
3238371032,
914150663,
812702999,
4144912697,
4290775857,
1750603025,
1694076839,
3204075428
]), z = /* @__PURE__ */ Uint32Array.from([
1779033703,
4089235720,
3144134277,
2227873595,
1013904242,
4271175723,
2773480762,
1595750129,
1359893119,
2917565137,
2600822924,
725511199,
528734635,
4215389547,
1541459225,
327033209
]), te = /* @__PURE__ */ BigInt(2 ** 32 - 1), Xe = /* @__PURE__ */ BigInt(32);
function xr(n, t = !1) {
return t ? { h: Number(n & te), l: Number(n >> Xe & te) } : { h: Number(n >> Xe & te) | 0, l: Number(n & te) | 0 };
}
function Rn(n, t = !1) {
const r = n.length;
let e = new Uint32Array(r), s = new Uint32Array(r);
for (let o = 0; o < r; o++) {
const { h: c, l: i } = xr(n[o], t);
[e[o], s[o]] = [c, i];
}
return [e, s];
}
const We = (n, t, r) => n >>> r, Qe = (n, t, r) => n << 32 - r | t >>> r, Ut = (n, t, r) => n >>> r | t << 32 - r, Lt = (n, t, r) => n << 32 - r | t >>> r, ee = (n, t, r) => n << 64 - r | t >>> r - 32, ne = (n, t, r) => n >>> r - 32 | t << 64 - r, Er = (n, t, r) => n << r | t >>> 32 - r, Br = (n, t, r) => t << r | n >>> 32 - r, Sr = (n, t, r) => t << r - 32 | n >>> 64 - r, _r = (n, t, r) => n << r - 32 | t >>> 64 - r;
function lt(n, t, r, e) {
const s = (t >>> 0) + (e >>> 0);
return { h: n + r + (s / 2 ** 32 | 0) | 0, l: s | 0 };
}
const vr = (n, t, r) => (n >>> 0) + (t >>> 0) + (r >>> 0), Ar = (n, t, r, e) => t + r + e + (n / 2 ** 32 | 0) | 0, Or = (n, t, r, e) => (n >>> 0) + (t >>> 0) + (r >>> 0) + (e >>> 0), Ir = (n, t, r, e, s) => t + r + e + s + (n / 2 ** 32 | 0) | 0, Nr = (n, t, r, e, s) => (n >>> 0) + (t >>> 0) + (r >>> 0) + (e >>> 0) + (s >>> 0), Tr = (n, t, r, e, s, o) => t + r + e + s + o + (n / 2 ** 32 | 0) | 0, qr = /* @__PURE__ */ Uint32Array.from([
1116352408,
1899447441,
3049323471,
3921009573,
961987163,
1508970993,
2453635748,
2870763221,
3624381080,
310598401,
607225278,
1426881987,
1925078388,
2162078206,
2614888103,
3248222580,
3835390401,
4022224774,
264347078,
604807628,
770255983,
1249150122,
1555081692,
1996064986,
2554220882,
2821834349,
2952996808,
3210313671,
3336571891,
3584528711,
113926993,
338241895,
666307205,
773529912,
1294757372,
1396182291,
1695183700,
1986661051,
2177026350,
2456956037,
2730485921,
2820302411,
3259730800,
3345764771,
3516065817,
3600352804,
4094571909,
275423344,
430227734,
506948616,
659060556,
883997877,
958139571,
1322822218,
1537002063,
1747873779,
1955562222,
2024104815,
2227730452,
2361852424,
2428436474,
2756734187,
3204031479,
3329325298
]), pt = /* @__PURE__ */ new Uint32Array(64);
class Hn extends qn {
constructor(t = 32) {
super(64, t, 8, !1), this.A = gt[0] | 0, this.B = gt[1] | 0, this.C = gt[2] | 0, this.D = gt[3] | 0, this.E = gt[4] | 0, this.F = gt[5] | 0, this.G = gt[6] | 0, this.H = gt[7] | 0;
}
get() {
const { A: t, B: r, C: e, D: s, E: o, F: c, G: i, H: a } = this;
return [t, r, e, s, o, c, i, a];
}
// prettier-ignore
set(t, r, e, s, o, c, i, a) {
this.A = t | 0, this.B = r | 0, this.C = e | 0, this.D = s | 0, this.E = o | 0, this.F = c | 0, this.G = i | 0, this.H = a | 0;
}
process(t, r) {
for (let d = 0; d < 16; d++, r += 4)
pt[d] = t.getUint32(r, !1);
for (let d = 16; d < 64; d++) {
const f = pt[d - 15], h = pt[d - 2], m = ft(f, 7) ^ ft(f, 18) ^ f >>> 3, g = ft(h, 17) ^ ft(h, 19) ^ h >>> 10;
pt[d] = g + pt[d - 7] + m + pt[d - 16] | 0;
}
let { A: e, B: s, C: o, D: c, E: i, F: a, G: u, H: l } = this;
for (let d = 0; d < 64; d++) {
const f = ft(i, 6) ^ ft(i, 11) ^ ft(i, 25), h = l + f + pr(i, a, u) + qr[d] + pt[d] | 0, g = (ft(e, 2) ^ ft(e, 13) ^ ft(e, 22)) + wr(e, s, o) | 0;
l = u, u = a, a = i, i = c + h | 0, c = o, o = s, s = e, e = h + g | 0;
}
e = e + this.A | 0, s = s + this.B | 0, o = o + this.C | 0, c = c + this.D | 0, i = i + this.E | 0, a = a + this.F | 0, u = u + this.G | 0, l = l + this.H | 0, this.set(e, s, o, c, i, a, u, l);
}
roundClean() {
mt(pt);
}
destroy() {
this.set(0, 0, 0, 0, 0, 0, 0, 0), mt(this.buffer);
}
}
class Rr extends Hn {
constructor() {
super(28), this.A = yt[0] | 0, this.B = yt[1] | 0, this.C = yt[2] | 0, this.D = yt[3] | 0, this.E = yt[4] | 0, this.F = yt[5] | 0, this.G = yt[6] | 0, this.H = yt[7] | 0;
}
}
const Gn = Rn([
"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))), Hr = Gn[0], Gr = Gn[1], wt = /* @__PURE__ */ new Uint32Array(80), xt = /* @__PURE__ */ new Uint32Array(80);
class Ur extends qn {
constructor(t = 64) {
super(128, t, 16, !1), this.Ah = z[0] | 0, this.Al = z[1] | 0, this.Bh = z[2] | 0, this.Bl = z[3] | 0, this.Ch = z[4] | 0, this.Cl = z[5] | 0, this.Dh = z[6] | 0, this.Dl = z[7] | 0, this.Eh = z[8] | 0, this.El = z[9] | 0, this.Fh = z[10] | 0, this.Fl = z[11] | 0, this.Gh = z[12] | 0, this.Gl = z[13] | 0, this.Hh = z[14] | 0, this.Hl = z[15] | 0;
}
// prettier-ignore
get() {
const { Ah: t, Al: r, Bh: e, Bl: s, Ch: o, Cl: c, Dh: i, Dl: a, Eh: u, El: l, Fh: d, Fl: f, Gh: h, Gl: m, Hh: g, Hl: p } = this;
return [t, r, e, s, o, c, i, a, u, l, d, f, h, m, g, p];
}
// prettier-ignore
set(t, r, e, s, o, c, i, a, u, l, d, f, h, m, g, p) {
this.Ah = t | 0, this.Al = r | 0, this.Bh = e | 0, this.Bl = s | 0, this.Ch = o | 0, this.Cl = c | 0, this.Dh = i | 0, this.Dl = a | 0, this.Eh = u | 0, this.El = l | 0, this.Fh = d | 0, this.Fl = f | 0, this.Gh = h | 0, this.Gl = m | 0, this.Hh = g | 0, this.Hl = p | 0;
}
process(t, r) {
for (let y = 0; y < 16; y++, r += 4)
wt[y] = t.getUint32(r), xt[y] = t.getUint32(r += 4);
for (let y = 16; y < 80; y++) {
const E = wt[y - 15] | 0, S = xt[y - 15] | 0, I = Ut(E, S, 1) ^ Ut(E, S, 8) ^ We(E, S, 7), N = Lt(E, S, 1) ^ Lt(E, S, 8) ^ Qe(E, S, 7), H = wt[y - 2] | 0, B = xt[y - 2] | 0, M = Ut(H, B, 19) ^ ee(H, B, 61) ^ We(H, B, 6), F = Lt(H, B, 19) ^ ne(H, B, 61) ^ Qe(H, B, 6), q = Or(N, F, xt[y - 7], xt[y - 16]), b = Ir(q, I, M, wt[y - 7], wt[y - 16]);
wt[y] = b | 0, xt[y] = q | 0;
}
let { Ah: e, Al: s, Bh: o, Bl: c, Ch: i, Cl: a, Dh: u, Dl: l, Eh: d, El: f, Fh: h, Fl: m, Gh: g, Gl: p, Hh: T, Hl: R } = this;
for (let y = 0; y < 80; y++) {
const E = Ut(d, f, 14) ^ Ut(d, f, 18) ^ ee(d, f, 41), S = Lt(d, f, 14) ^ Lt(d, f, 18) ^ ne(d, f, 41), I = d & h ^ ~d & g, N = f & m ^ ~f & p, H = Nr(R, S, N, Gr[y], xt[y]), B = Tr(H, T, E, I, Hr[y], wt[y]), M = H | 0, F = Ut(e, s, 28) ^ ee(e, s, 34) ^ ee(e, s, 39), q = Lt(e, s, 28) ^ ne(e, s, 34) ^ ne(e, s, 39), b = e & o ^ e & i ^ o & i, w = s & c ^ s & a ^ c & a;
T = g | 0, R = p | 0, g = h | 0, p = m | 0, h = d | 0, m = f | 0, { h: d, l: f } = lt(u | 0, l | 0, B | 0, M | 0), u = i | 0, l = a | 0, i = o | 0, a = c | 0, o = e | 0, c = s | 0;
const O = vr(M, q, w);
e = Ar(O, B, F, b), s = O | 0;
}
({ h: e, l: s } = lt(this.Ah | 0, this.Al | 0, e | 0, s | 0)), { h: o, l: c } = lt(this.Bh | 0, this.Bl | 0, o | 0, c | 0), { h: i, l: a } = lt(this.Ch | 0, this.Cl | 0, i | 0, a | 0), { h: u, l } = lt(this.Dh | 0, this.Dl | 0, u | 0, l | 0), { h: d, l: f } = lt(this.Eh | 0, this.El | 0, d | 0, f | 0), { h, l: m } = lt(this.Fh | 0, this.Fl | 0, h | 0, m | 0), { h: g, l: p } = lt(this.Gh | 0, this.Gl | 0, g | 0, p | 0), { h: T, l: R } = lt(this.Hh | 0, this.Hl | 0, T | 0, R | 0), this.set(e, s, o, c, i, a, u, l, d, f, h, m, g, p, T, R);
}
roundClean() {
mt(wt, xt);
}
destroy() {
mt(this.buffer), this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
}
const Un = /* @__PURE__ */ De(() => new Hn()), Ws = /* @__PURE__ */ De(() => new Rr()), Qs = /* @__PURE__ */ De(() => new Ur());
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const Ze = /* @__PURE__ */ BigInt(0), fe = /* @__PURE__ */ BigInt(1);
function Je(n, t = "") {
if (typeof n != "boolean") {
const r = t && `"${t}"`;
throw new Error(r + "expected boolean, got type=" + typeof n);
}
return n;
}
function tn(n, t, r = "") {
const e = me(n), s = n?.length, o = t !== void 0;
if (!e || o && s !== t) {
const c = r && `"${r}" `, i = o ? ` of length ${t}` : "", a = e ? `length=${s}` : `type=${typeof n}`;
throw new Error(c + "expected Uint8Array" + i + ", got " + a);
}
return n;
}
function Ln(n) {
if (typeof n != "string")
throw new Error("hex string expected, got " + typeof n);
return n === "" ? Ze : BigInt("0x" + n);
}
function dt(n) {
return Ln(jt(n));
}
function Pn(n) {
return J(n), Ln(jt(Uint8Array.from(n).reverse()));
}
function j(n, t) {
return Tn(n.toString(16).padStart(t * 2, "0"));
}
function kn(n, t) {
return j(n, t).reverse();
}
function Mt(n, t, r) {
let e;
if (typeof t == "string")
try {
e = Tn(t);
} catch (o) {
throw new Error(n + " must be hex string or Uint8Array, cause: " + o);
}
else if (me(t))
e = Uint8Array.from(t);
else
throw new Error(n + " must be hex string or Uint8Array");
const s = e.length;
if (typeof r == "number" && s !== r)
throw new Error(n + " of length " + r + " expected, got " + s);
return e;
}
function Js(n) {
return Uint8Array.from(n);
}
const Be = (n) => typeof n == "bigint" && Ze <= n;
function Dn(n, t, r) {
return Be(n) && Be(t) && Be(r) && t <= n && n < r;
}
function to(n, t, r, e) {
if (!Dn(t, r, e))
throw new Error("expected valid " + n + ": " + r + " <= n < " + e + ", got " + t);
}
function Yt(n) {
let t;
for (t = 0; n > Ze; n >>= fe, t += 1)
;
return t;
}
function Lr(n, t) {
return n >> BigInt(t) & fe;
}
const qt = (n) => (fe << BigInt(n)) - fe;
function Pr(n) {
return typeof n == "function" && Number.isSafeInteger(n.outputLen);
}
function Me(n, t, r = {}) {
if (!n || typeof n != "object")
throw new Error("expected valid options object");
function e(s, o, c) {
const i = n[s];
if (c && i === void 0)
return;
const a = typeof i;
if (a !== o || i === null)
throw new Error(`param "${s}" is invalid: expected ${o}, got ${a}`);
}
Object.entries(t).forEach(([s, o]) => e(s, o, !1)), Object.entries(r).forEach(([s, o]) => e(s, o, !0));
}
const Zn = () => {
throw new Error("not implemented");
};
function Re(n) {
const t = /* @__PURE__ */ new WeakMap();
return (r, ...e) => {
const s = t.get(r);
if (s !== void 0)
return s;
const o = n(r, ...e);
return t.set(r, o), o;
};
}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const et = BigInt(0), Y = BigInt(1), vt = /* @__PURE__ */ BigInt(2), Mn = /* @__PURE__ */ BigInt(3), Kn = /* @__PURE__ */ BigInt(4), Fn = /* @__PURE__ */ BigInt(5), kr = /* @__PURE__ */ BigInt(7), Vn = /* @__PURE__ */ BigInt(8), Dr = /* @__PURE__ */ BigInt(9), Cn = /* @__PURE__ */ BigInt(16);
function nt(n, t) {
const r = n % t;
return r >= et ? r : t + r;
}
function eo(n, t, r) {
let e = n;
for (; t-- > et; )
e *= e, e %= r;
return e;
}
function en(n, t) {
if (n === et)
throw new Error("invert: expected non-zero number");
if (t <= et)
throw new Error("invert: expected positive modulus, got " + t);
let r = nt(n, t), e = t, s = et, o = Y;
for (; r !== et; ) {
const i = e / r, a = e % r, u = s - o * i;
e = r, r = a, s = o, o = u;
}
if (e !== Y)
throw new Error("invert: does not exist");
return nt(s, t);
}
function Ke(n, t, r) {
if (!n.eql(n.sqr(t), r))
throw new Error("Cannot find square root");
}
function jn(n, t) {
const r = (n.ORDER + Y) / Kn, e = n.pow(t, r);
return Ke(n, e, t), e;
}
function Zr(n, t) {
const r = (n.ORDER - Fn) / Vn, e = n.mul(t, vt), s = n.pow(e, r), o = n.mul(t, s), c = n.mul(n.mul(o, vt), s), i = n.mul(o, n.sub(c, n.ONE));
return Ke(n, i, t), i;
}
function Mr(n) {
const t = Ft(n), r = Yn(n), e = r(t, t.neg(t.ONE)), s = r(t, e), o = r(t, t.neg(e)), c = (n + kr) / Cn;
return (i, a) => {
let u = i.pow(a, c), l = i.mul(u, e);
const d = i.mul(u, s), f = i.mul(u, o), h = i.eql(i.sqr(l), a), m = i.eql(i.sqr(d), a);
u = i.cmov(u, l, h), l = i.cmov(f, d, m);
const g = i.eql(i.sqr(l), a), p = i.cmov(u, l, g);
return Ke(i, p, a), p;
};
}
function Yn(n) {
if (n < Mn)
throw new Error("sqrt is not defined for small field");
let t = n - Y, r = 0;
for (; t % vt === et; )
t /= vt, r++;
let e = vt;
const s = Ft(n);
for (; de(s, e) === 1; )
if (e++ > 1e3)
throw new Error("Cannot find square root: probably non-prime P");
if (r === 1)
return jn;
let o = s.pow(e, t);
const c = (t + Y) / vt;
return function(a, u) {
if (a.is0(u))
return u;
if (de(a, u) !== 1)
throw new Error("Cannot find square root");
let l = r, d = a.mul(a.ONE, o), f = a.pow(u, t), h = a.pow(u, c);
for (; !a.eql(f, a.ONE); ) {
if (a.is0(f))
return a.ZERO;
let m = 1, g = a.sqr(f);
for (; !a.eql(g, a.ONE); )
if (m++, g = a.sqr(g), m === l)
throw new Error("Cannot find square root");
const p = Y << BigInt(l - m - 1), T = a.pow(d, p);
l = m, d = a.sqr(T), f = a.mul(f, d), h = a.mul(h, T);
}
return h;
};
}
function Kr(n) {
return n % Kn === Mn ? jn : n % Vn === Fn ? Zr : n % Cn === Dr ? Mr(n) : Yn(n);
}
const no = (n, t) => (nt(n, t) & Y) === Y, Fr = [
"create",
"isValid",
"is0",
"neg",
"inv",
"sqrt",
"sqr",
"eql",
"add",
"sub",
"mul",
"pow",
"div",
"addN",
"subN",
"mulN",
"sqrN"
];
function zn(n) {
const t = {
ORDER: "bigint",
MASK: "bigint",
BYTES: "number",
BITS: "number"
}, r = Fr.reduce((e, s) => (e[s] = "function", e), t);
return Me(n, r), n;
}
function ge(n, t, r) {
if (r < et)
throw new Error("invalid exponent, negatives unsupported");
if (r === et)
return n.ONE;
if (r === Y)
return t;
let e = n.ONE, s = t;
for (; r > et; )
r & Y && (e = n.mul(e, s)), s = n.sqr(s), r >>= Y;
return e;
}
function Rt(n, t, r = !1) {
const e = new Array(t.length).fill(r ? n.ZERO : void 0), s = t.reduce((c, i, a) => n.is0(i) ? c : (e[a] = c, n.mul(c, i)), n.ONE), o = n.inv(s);
return t.reduceRight((c, i, a) => n.is0(i) ? c : (e[a] = n.mul(c, e[a]), n.mul(c, i)), o), e;
}
function de(n, t) {
const r = (n.ORDER - Y) / vt, e = n.pow(t, r), s = n.eql(e, n.ONE), o = n.eql(e, n.ZERO), c = n.eql(e, n.neg(n.ONE));
if (!s && !o && !c)
throw new Error("invalid Legendre symbol result");
return s ? 1 : o ? 0 : -1;
}
function Vr(n, t) {
t !== void 0 && Dt(t);
const r = t !== void 0 ? t : n.toString(2).length, e = Math.ceil(r / 8);
return { nBitLength: r, nByteLength: e };
}
function Ft(n, t, r = !1, e = {}) {
if (n <= et)
throw new Error("invalid field: expected ORDER > 0, got " + n);
let s, o, c = !1, i;
if (typeof t == "object" && t != null) {
if (e.sqrt || r)
throw new Error("cannot specify opts in two arguments");
const f = t;
f.BITS && (s = f.BITS), f.sqrt && (o = f.sqrt), typeof f.isLE == "boolean" && (r = f.isLE), typeof f.modFromBytes == "boolean" && (c = f.modFromBytes), i = f.allowedLengths;
} else
typeof t == "number" && (s = t), e.sqrt && (o = e.sqrt);
const { nBitLength: a, nByteLength: u } = Vr(n, s);
if (u > 2048)
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
let l;
const d = Object.freeze({
ORDER: n,
isLE: r,
BITS: a,
BYTES: u,
MASK: qt(a),
ZERO: et,
ONE: Y,
allowedLengths: i,
create: (f) => nt(f, n),
isValid: (f) => {
if (typeof f != "bigint")
throw new Error("invalid field element: expected bigint, got " + typeof f);
return et <= f && f < n;
},
is0: (f) => f === et,
// is valid and invertible
isValidNot0: (f) => !d.is0(f) && d.isValid(f),
isOdd: (f) => (f & Y) === Y,
neg: (f) => nt(-f, n),
eql: (f, h) => f === h,
sqr: (f) => nt(f * f, n),
add: (f, h) => nt(f + h, n),
sub: (f, h) => nt(f - h, n),
mul: (f, h) => nt(f * h, n),
pow: (f, h) => ge(d, f, h),
div: (f, h) => nt(f * en(h, n), n),
// Same as above, but doesn't normalize
sqrN: (f) => f * f,
addN: (f, h) => f + h,
subN: (f, h) => f - h,
mulN: (f, h) => f * h,
inv: (f) => en(f, n),
sqrt: o || ((f) => (l || (l = Kr(n)), l(d, f))),
toBytes: (f) => r ? kn(f, u) : j(f, u),
fromBytes: (f, h = !0) => {
if (i) {
if (!i.includes(f.length) || f.length > u)
throw new Error("Field.fromBytes: expected " + i + " bytes, got " + f.length);
const g = new Uint8Array(u);
g.set(f, r ? 0 : g.length - f.length), f = g;
}
if (f.length !== u)
throw new Error("Field.fromBytes: expected " + u + " bytes, got " + f.length);
let m = r ? Pn(f) : dt(f);
if (c && (m = nt(m, n)), !h && !d.isValid(m))
throw new Error("invalid field element: outside of range 0..ORDER");
return m;
},
// TODO: we don't need it here, move out to separate fn
invertBatch: (f) => Rt(d, f),
// We can't move this out because Fp6, Fp12 implement it
// and it's unclear what to return in there.
cmov: (f, h, m) => m ? h : f
});
return Object.freeze(d);
}
function $n(n) {
if (typeof n != "bigint")
throw new Error("field order must be bigint");
const t = n.toString(2).length;
return Math.ceil(t / 8);
}
function Xn(n) {
const t = $n(n);
return t + Math.ceil(t / 2);
}
function Cr(n, t, r = !1) {
const e = n.length, s = $n(t), o = Xn(t);
if (e < 16 || e < o || e > 1024)
throw new Error("expected " + o + "-1024 bytes of input, got " + e);
const c = r ? Pn(n) : dt(n), i = nt(c, t - Y) + Y;
return r ? kn(i, s) : j(i, s);
}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const Kt = BigInt(0), At = BigInt(1);
function ue(n, t) {
const r = t.negate();
return n ? r : t;
}
function Ct(n, t) {
const r = Rt(n.Fp, t.map((e) => e.Z));
return t.map((e, s) => n.fromAffine(e.toAffine(r[s])));
}
function Wn(n, t) {
if (!Number.isSafeInteger(n) || n <= 0 || n > t)
throw new Error("invalid window size, expected [1.." + t + "], got W=" + n);
}
function Se(n, t) {
Wn(n, t);
const r = Math.ceil(t / n) + 1, e = 2 ** (n - 1), s = 2 ** n, o = qt(n), c = BigInt(n);
return { windows: r, windowSize: e, mask: o, maxNumber: s, shiftBy: c };
}
function nn(n, t, r) {
const { windowSize: e, mask: s, maxNumber: o, shiftBy: c } = r;
let i = Number(n & s), a = n >> c;
i > e && (i -= o, a += At);
const u = t * e, l = u + Math.abs(i) - 1, d = i === 0, f = i < 0, h = t % 2 !== 0;
return { nextN: a, offset: l, isZero: d, isNeg: f, isNegF: h, offsetF: u };
}
function jr(n, t) {
if (!Array.isArray(n))
throw new Error("array expected");
n.forEach((r, e) => {
if (!(r instanceof t))
throw new Error("invalid point at index " + e);
});
}
function Yr(n, t) {
if (!Array.isArray(n))
throw new Error("array of scalars expected");
n.forEach((r, e) => {
if (!t.isValid(r))
throw new Error("invalid scalar at index " + e);
});
}
const _e = /* @__PURE__ */ new WeakMap(), Qn = /* @__PURE__ */ new WeakMap();
function ve(n) {
return Qn.get(n) || 1;
}
function rn(n) {
if (n !== Kt)
throw new Error("invalid wNAF");
}
class zr {
// Parametrized with a given Point class (not individual point)
constructor(t, r) {
this.BASE = t.BASE, this.ZERO = t.ZERO, this.Fn = t.Fn, this.bits = r;
}
// non-const time multiplication ladder
_unsafeLadder(t, r, e = this.ZERO) {
let s = t;
for (; r > Kt; )
r & At && (e = e.add(s)), s = s.double(), r >>= At;
return e;
}
/**
* Creates a wNAF precomputation window. Used for caching.
* Default window size is set by `utils.precompute()` and is equal to 8.
* Number of precomputed points depends on the curve size:
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
* - 𝑊 is the window size
* - 𝑛 is the bitlength of the curve order.
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
* @param point Point instance
* @param W window size
* @returns precomputed point tables flattened to a single array
*/
precomputeWindow(t, r) {
const { windows: e, windowSize: s } = Se(r, this.bits), o = [];
let c = t, i = c;
for (let a = 0; a < e; a++) {
i = c, o.push(i);
for (let u = 1; u < s; u++)
i = i.add(c), o.push(i);
c = i.double();
}
return o;
}
/**
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
* More compact implementation:
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
* @returns real and fake (for const-time) points
*/
wNAF(t, r, e) {
if (!this.Fn.isValid(e))
throw new Error("invalid scalar");
let s = this.ZERO, o = this.BASE;
const c = Se(t, this.bits);
for (let i = 0; i < c.windows; i++) {
const { nextN: a, offset: u, isZero: l, isNeg: d, isNegF: f, offsetF: h } = nn(e, i, c);
e = a, l ? o = o.add(ue(f, r[h])) : s = s.add(ue(d, r[u]));
}
return rn(e), { p: s, f: o };
}
/**
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
* @param acc accumulator point to add result of multiplication
* @returns point
*/
wNAFUnsafe(t, r, e, s = this.ZERO) {
const o = Se(t, this.bits);
for (let c = 0; c < o.windows && e !== Kt; c++) {
const { nextN: i, offset: a, isZero: u, isNeg: l } = nn(e, c, o);
if (e = i, !u) {
const d = r[a];
s = s.add(l ? d.negate() : d);
}
}
return rn(e), s;
}
getPrecomputes(t, r, e) {
let s = _e.get(r);
return s || (s = this.precomputeWindow(r, t), t !== 1 && (typeof e == "function" && (s = e(s)), _e.set(r, s))), s;
}
cached(t, r, e) {
const s = ve(t);
return this.wNAF(s, this.getPrecomputes(s, t, e), r);
}
unsafe(t, r, e, s) {
const o = ve(t);
return o === 1 ? this._unsafeLadder(t, r, s) : this.wNAFUnsafe(o, this.getPrecomputes(o, t, e), r, s);
}
// We calculate precomputes for elliptic curve point multiplication
// using windowed method. This specifies window size and
// stores precomputed values. Usually only base point would be precomputed.
createCache(t, r) {
Wn(r, this.bits), Qn.set(t, r), _e.delete(t);
}
hasCache(t) {
return ve(t) !== 1;
}
}
function $r(n, t, r, e) {
let s = t, o = n.ZERO, c = n.ZERO;
for (; r > Kt || e > Kt; )
r & At && (o = o.add(s)), e & At && (c = c.add(s)), s = s.double(), r >>= At, e >>= At;
return { p1: o, p2: c };
}
function Xr(n, t, r, e) {
jr(r, n), Yr(e, t);
const s = r.length, o = e.length;
if (s !== o)
throw new Error("arrays of points and scalars must have equal length");
const c = n.ZERO, i = Yt(BigInt(s));
let a = 1;
i > 12 ? a = i - 3 : i > 4 ? a = i - 2 : i > 0 && (a = 2);
const u = qt(a), l = new Array(Number(u) + 1).fill(c), d = Math.floor((t.BITS - 1) / a) * a;
let f = c;
for (let h = d; h >= 0; h -= a) {
l.fill(c);
for (let g = 0; g < o; g++) {
const p = e[g], T = Number(p >> BigInt(h) & u);
l[T] = l[T].add(r[g]);
}
let m = c;
for (let g = l.length - 1, p = c; g > 0; g--)
p = p.add(l[g]), m = m.add(p);
if (f = f.add(m), h !== 0)
for (let g = 0; g < a; g++)
f = f.double();
}
return f;
}
function sn(n, t, r) {
if (t) {
if (t.ORDER !== n)
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
return zn(t), t;
} else
return Ft(n, { isLE: r });
}
function Wr(n, t, r = {}, e) {
if (e === void 0 && (e = n === "edwards"), !t || typeof t != "object")
throw new Error(`expected valid ${n} CURVE object`);
for (const a of ["p", "n", "h"]) {
const u = t[a];
if (!(typeof u == "bigint" && u > Kt))
throw new Error(`CURVE.${a} must be positive bigint`);
}
const s = sn(t.p, r.Fp, e), o = sn(t.n, r.Fn, e), i = ["Gx", "Gy", "a", n === "weierstrass" ? "b" : "d"];
for (const a of i)
if (!s.isValid(t[a]))
throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`);
return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: s, Fn: o };
}
const Qr = dt;
function Bt(n, t) {
if (zt(n), zt(t), n < 0 || n >= 1 << 8 * t)
throw new Error("invalid I2OSP input: " + n);
const r = Array.from({ length: t }).fill(0);
for (let e = t - 1; e >= 0; e--)
r[e] = n & 255, n >>>= 8;
return new Uint8Array(r);
}
function Jr(n, t) {
const r = new Uint8Array(n.length);
for (let e = 0; e < n.length; e++)
r[e] = n[e] ^ t[e];
return r;
}
function zt(n) {
if (!Number.isSafeInteger(n))
throw new Error("number expected");
}
function Jn(n) {
if (!me(n) && typeof n != "string")
throw new Error("DST must be Uint8Array or string");
return typeof n == "string" ? Wt(n) : n;
}
function ts(n, t, r, e) {
J(n), zt(r), t = Jn(t), t.length > 255 && (t = e(V(Wt("H2C-OVERSIZE-DST-"), t)));
const { outputLen: s, blockLen: o } = e, c = Math.ceil(r / s);
if (r > 65535 || c > 255)
throw new Error("expand_message_xmd: invalid lenInBytes");
const i = V(t, Bt(t.length, 1)), a = Bt(0, o), u = Bt(r, 2), l = new Array(c), d = e(V(a, n, u, Bt(0, 1), i));
l[0] = e(V(d, Bt(1, 1), i));
for (let h = 1; h <= c; h++) {
const m = [Jr(d, l[h - 1]), Bt(h + 1, 1), i];
l[h] = e(V(...m));
}
return V(...l).slice(0, r);
}
function es(n, t, r, e, s) {
if (J(n), zt(r), t = Jn(t), t.length > 255) {
const o = Math.ceil(2 * e / 8);
t = s.create({ dkLen: o }).update(Wt("H2C-OVERSIZE-DST-")).update(t).digest();
}
if (r > 65535 || t.length > 255)
throw new Error("expand_message_xof: invalid lenInBytes");
return s.create({ dkLen: r }).update(n).update(Bt(r, 2)).update(t).update(Bt(t.length, 1)).digest();
}
function oe(n, t, r) {
Me(r, {
p: "bigint",
m: "number",
k: "number",
hash: "function"
});
const { p: e, k: s, m: o, hash: c, expand: i, DST: a } = r;
if (!Pr(r.hash))
throw new Error("expected valid hash");
J(n), zt(t);
const u = e.toString(2).length, l = Math.ceil((u + s) / 8), d = t * o * l;
let f;
if (i === "xmd")
f = ts(n, a, d, c);
else if (i === "xof")
f = es(n, a, d, s, c);
else if (i === "_internal_pass")
f = n;
else
throw new Error('expand must be "xmd" or "xof"');
const h = new Array(t);
for (let m = 0; m < t; m++) {
const g = new Array(o);
for (let p = 0; p < o; p++) {
const T = l * (p + m * o), R = f.subarray(T, T + l);
g[p] = nt(Qr(R), e);
}
h[m] = g;
}
return h;
}
function tr(n, t) {
const r = t.map((e) => Array.from(e).reverse());
return (e, s) => {
const [o, c, i, a] = r.map((d) => d.reduce((f, h) => n.add(n.mul(f, e), h))), [u, l] = Rt(n, [c, a], !0);
return e = n.mul(o, u), s = n.mul(s, n.mul(i, l)), { x: e, y: s };
};
}
const ns = Wt("HashToScalar-");
function on(n, t, r) {
if (typeof t != "function")
throw new Error("mapToCurve() must be defined");
function e(o) {
return n.fromAffine(t(o));
}
function s(o) {
const c = o.clearCofactor();
return c.equals(n.ZERO) ? n.ZERO : (c.assertValidity(), c);
}
return {
defaults: r,
hashToCurve(o, c) {
const i = Object.assign({}, r, c), a = oe(o, 2, i), u = e(a[0]), l = e(a[1]);
return s(u.add(l));
},
encodeToCurve(o, c) {
const i = r.encodeDST ? { DST: r.encodeDST } : {}, a = Object.assign({}, r, i, c), u = oe(o, 1, a), l = e(u[0]);
return s(l);
},
/** See {@link H2CHasher} */
mapToCurve(o) {
if (!Array.isArray(o))
throw new Error("expected array of bigints");
for (const c of o)
if (typeof c != "bigint")
throw new Error("expected array of bigints");
return s(e(o));
},
// hash_to_scalar can produce 0: https://www.rfc-editor.org/errata/eid8393
// RFC 9380, draft-irtf-cfrg-bbs-signatures-08
hashToScalar(o, c) {
const i = n.Fn.ORDER, a = Object.assign({}, r, { p: i, m: 1, DST: ns }, c);
return oe(o, 1, a)[0][0];
}
};
}
class er extends ke {
constructor(t, r) {
super(), this.finished = !1, this.destroyed = !1, Pe(t);
const e = Nt(r);
if (this.iHash = t.create(), typeof this.iHash.update != "function")
throw new Error("Expected instance of class which extends utils.Hash");
this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
const s = this.blockLen, o = new Uint8Array(s);
o.set(e.length > s ? t.create().update(e).digest() : e);
for (let c = 0; c < o.length; c++)
o[c] ^= 54;
this.iHash.update(o), this.oHash = t.create();
for (let c = 0; c < o.length; c++)
o[c] ^= 106;
this.oHash.update(o), mt(o);
}
update(t) {
return Zt(this), this.iHash.update(t), this;
}
digestInto(t) {
Zt(this), J(t, this.outputLen), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy();
}
digest() {
const t = new Uint8Array(this.oHash.outputLen);
return this.digestInto(t), t;
}
_cloneInto(t) {
t || (t = Object.create(Object.getPrototypeOf(this), {}));
const { oHash: r, iHash: e, finished: s, destroyed: o, blockLen: c, outputLen: i } = this;
return t = t, t.finished = s, t.destroyed = o, t.blockLen = c, t.outputLen = i, t.oHash = r._cloneInto(t.oHash), t.iHash = e._cloneInto(t.iHash), t;
}
clone() {
return this._cloneInto();
}
destroy() {
this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
}
}
const Fe = (n, t, r) => new er(n, t).update(r).digest();
Fe.create = (n, t) => new er(n, t);
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const cn = (n, t) => (n + (n >= 0 ? t : -t) / ht) / t;
function rs(n, t, r) {
const [[e, s], [o, c]] = t, i = cn(c * n, r), a = cn(-s * n, r);
let u = n - i * e - a * o, l = -i * s - a * c;
const d = u < Ot, f = l < Ot;
d && (u = -u), f && (l = -l);
const h = qt(Math.ceil(Yt(r) / 2)) + W;
if (u < Ot || u >= h || l < Ot || l >= h)
throw new Error("splitScalar (endomorphism): failed, k=" + n);
return { k1neg: d, k1: u, k2neg: f, k2: l };
}
const Ot = BigInt(0), W = BigInt(1), ht = BigInt(2), Pt = BigInt(3), He = BigInt(4);
function le(n, t) {
const { BYTES: r } = n;
let e;
if (typeof t == "bigint")
e = t;
else {
let s = Mt("private key", t);
try {
e = n.fromBytes(s);
} catch {
throw new Error(`invalid private key: expected ui8a of size ${r}, got ${typeof t}`);
}
}
if (!n.isValidNot0(e))
throw new Error("invalid private key: out of range [1..N-1]");
return e;
}
function ss(n, t = {}) {
const r = Wr("weierstrass", n, t), { Fp: e, Fn: s } = r;
let o = r.CURVE;
const { h: c, n: i } = o;
Me(t, {}, {
allowInfinityPoint: "boolean",
clearCofactor: "function",
isTorsionFree: "function",
fromBytes: "function",
toBytes: "function",
endo: "object",
wrapPrivateKey: "boolean"
});
const { endo: a } = t;
if (a && (!e.is0(o.a) || typeof a.beta != "bigint" || !Array.isArray(a.basises)))
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
const u = is(e, s);
function l() {
if (!e.isOdd)
throw new Error("compression is not supported: Field does not have .isOdd()");
}
function d(q, b, w) {
const { x: O, y: L } = b.toAffine(), Z = e.toBytes(O);
if (Je(w, "isCompressed"), w) {
l();
const x = !e.isOdd(L);
return V(os(x), Z);
} else
return V(Uint8Array.of(4), Z, e.toBytes(L));
}
function f(q) {
tn(q, void 0, "Point");
const { publicKey: b, publicKeyUncompressed: w } = u, O = q.length, L = q[0], Z = q.subarray(1);
if (O === b && (L === 2 || L === 3)) {
const x = e.fromBytes(Z);
if (!e.isValid(x))
throw new Error("bad point: is not on curve, wrong x");
const _ = g(x);
let A;
try {
A = e.sqrt(_);
} catch (tt) {
const C = tt instanceof Error ? ": " + tt.message : "";
throw new Error("bad point: is not on curve, sqrt error" + C);
}
l();
const G = e.isOdd(A);
return (L & 1) === 1 !== G && (A = e.neg(A)), { x, y: A };
} else if (O === w && L === 4) {
const x = e.BYTES, _ = e.fromBytes(Z.subarray(0, x)), A = e.fromBytes(Z.subarray(x, x * 2));
if (!p(_, A))
throw new Error("bad point: is not on curve");
return { x: _, y: A };
} else
throw new Error(`bad point: got length ${O}, expected compressed=${b} or uncompressed=${w}`);
}
const h = t.toBytes || d, m = t.fromBytes || f;
function g(q) {
const b = e.sqr(q), w = e.mul(b, q);
return e.add(e.add(w, e.mul(q, o.a)), o.b);
}
function p(q, b) {
const w = e.sqr(b), O = g(q);
return e.eql(w, O);
}
if (!p(o.Gx, o.Gy))
throw new Error("bad curve params: generator point");
const T = e.mul(e.pow(o.a, Pt), He), R = e.mul(e.sqr(o.b), BigInt(27));
if (e.is0(e.add(T, R)))
throw new Error("bad curve params: a or b");
function y(q, b, w = !1) {
if (!e.isValid(b) || w && e.is0(b))
throw new Error(`bad point coordinate ${q}`);
return b;
}
function E(q) {
if (!(q instanceof B))
throw new Error("ProjectivePoint expected");
}
function S(q) {
if (!a || !a.basises)
throw new Error("no endo");
return rs(q, a.basises, s.ORDER);
}
const I = Re((q, b) => {
const { X: w, Y: O, Z: L } = q;
if (e.eql(L, e.ONE))
return { x: w, y: O };
const Z = q.is0();
b == null && (b = Z ? e.ONE : e.inv(L));
const x = e.mul(w, b), _ = e.mul(O, b), A = e.mul(L, b);
if (Z)
return { x: e.ZERO, y: e.ZERO };
if (!e.eql(A, e.ONE))
throw new Error("invZ was invalid");
return { x, y: _ };
}), N = Re((q) => {
if (q.is0()) {
if (t.allowInfinityPoint && !e.is0(q.Y))
return;
throw new Error("bad point: ZERO");
}
const { x: b, y: w } = q.toAffine();
if (!e.isValid(b) || !e.isValid(w))
throw new Error("bad point: x or y not field elements");
if (!p(b, w))
throw new Error("bad point: equation left != right");
if (!q.isTorsionFree())
throw new Error("bad point: not in prime-order subgroup");
return !0;
});
function H(q, b, w, O, L) {
return w = new B(e.mul(w.X, q), w.Y, w.Z), b = ue(O, b), w = ue(L, w), b.add(w);
}
class B {
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
constructor(b, w, O) {
this.X = y("x", b), this.Y = y("y", w, !0), this.Z = y("z", O), Object.freeze(this);
}
static CURVE() {
return o;
}
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
static fromAffine(b) {
const { x: w, y: O } = b || {};
if (!b || !e.isValid(w) || !e.isValid(O))
throw new Error("invalid affine point");
if (b instanceof B)
throw new Error("projective point not allowed");
return e.is0(w) && e.is0(O) ? B.ZERO : new B(w, O, e.ONE);
}
static fromBytes(b) {
const w = B.fromAffine(m(tn(b, void 0, "point")));
return w.assertValidity(), w;
}
static fromHex(b) {
return B.fromBytes(Mt("pointHex", b));
}
get x() {
return this.toAffine().x;
}
get y() {
return this.toAffine().y;
}
/**
*
* @param windowSize
* @param isLazy true will defer table computation until the first multiplication
* @returns
*/
precompute(b = 8, w = !0) {
return F.createCache(this, b), w || this.multiply(Pt), this;
}
// TODO: return `this`
/** A point on curve is valid if it conforms to equation. */
assertValidity() {
N(this);
}
hasEvenY() {
const { y: b } = this.toAffine();
if (!e.isOdd)
throw new Error("Field doesn't support isOdd");
return !e.isOdd(b);
}
/** Compare one point to another. */
equals(b) {
E(b);
const { X: w, Y: O, Z: L } = this, { X: Z, Y: x, Z: _ } = b, A = e.eql(e.mul(w, _), e.mul(Z, L)), G = e.eql(e.mul(O, _), e.mul(x, L));
return A && G;
}
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
negate() {
return new B(this.X, e.neg(this.Y), this.Z);
}
// Renes-Costello-Batina exception-free doubling formula.
// There is 30% faster Jacobian formula, but it is not complete.
// https://eprint.iacr.org/2015/1060, algorithm 3
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
double() {
const { a: b, b: w } = o, O = e.mul(w, Pt), { X: L, Y: Z, Z: x } = this;
let _ = e.ZERO, A = e.ZERO, G = e.ZERO, U = e.mul(L, L), tt = e.mul(Z, Z), C = e.mul(x, x), K = e.mul(L, Z);
return K = e.add(K, K), G = e.mul(L, x), G = e.add(G, G), _ = e.mul(b, G), A = e.mul(O, C), A = e.add(_, A), _ = e.sub(tt, A), A = e.add(tt, A), A = e.mul(_, A), _ = e.mul(K, _), G = e.mul(O, G), C = e.mul(b, C), K = e.sub(U, C), K = e.mul(b, K), K = e.add(K, G), G = e.add(U, U), U = e.add(G, U), U = e.add(U, C), U = e.mul(U, K), A = e.add(A, U), C = e.mul(Z, x), C = e.add(C, C), U = e.mul(C, K), _ = e.sub(_, U), G = e.mul(C, tt), G = e.add(G, G), G = e.add(G, G), new B(_, A, G);
}
// Renes-Costello-Batina exception-free addition formula.
// There is 30% faster Jacobian formula, but it is not complete.
// https://eprint.iacr.org/2015/1060, algorithm 1
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
add(b) {
E(b);
const { X: w, Y: O, Z: L } = this, { X: Z, Y: x, Z: _ } = b;
let A = e.ZERO, G = e.ZERO, U = e.ZERO;
const tt = o.a, C = e.mul(o.b, Pt);
let K = e.mul(w, Z), ot = e.mul(O, x), ct = e.mul(L, _), Ht = e.add(w, O), X = e.add(Z, x);
Ht = e.mul(Ht, X), X = e.add(K, ot), Ht = e.sub(Ht, X), X = e.add(w, L);
let at = e.add(Z, _);
return X = e.mul(X, at), at = e.add(K, ct), X = e.sub(X, at), at = e.add(O, L), A = e.add(x, _), at = e.mul(at, A), A = e.add(ot, ct), at = e.sub(at, A), U = e.mul(tt, X), A = e.mul(C, ct), U = e.add(A, U), A = e.sub(ot, U), U = e.add(ot, U), G = e.mul(A, U), ot = e.add(K, K), ot = e.add(ot, K), ct = e.mul(tt, ct), X = e.mul(C, X), ot = e.add(ot, ct), ct = e.sub(K, ct), ct = e.mul(tt, ct), X = e.add(X, ct), K = e.mul(ot, X), G = e.add(G, K), K = e.mul(at, X), A = e.mul(Ht, A), A = e.sub(A, K), K = e.mul(Ht, ot), U = e.mul(at, U), U = e.add(U, K), new B(A, G, U);
}
subtract(b) {
return this.add(b.negate());
}
is0() {
return this.equals(B.ZERO);
}
/**
* Constant time multiplication.
* Uses wNAF method. Windowed method may be 10% faster,
* but takes 2x longer to generate and consumes 2x memory.
* Uses precomputes when available.
* Uses endomorphism for Koblitz curves.
* @param scalar by which the point would be multiplied
* @returns New point
*/
multiply(b) {
const { endo: w } = t;
if (!s.isValidNot0(b))
throw new Error("invalid scalar: out of range");
let O, L;
const Z = (x) => F.cached(this, x, (_) => Ct(B, _));
if (w) {
const { k1neg: x, k1: _, k2neg: A, k2: G } = S(b), { p: U, f: tt } = Z(_), { p: C, f: K } = Z(G);
L = tt.add(K), O = H(w.beta, U, C, x, A);
} else {
const { p: x, f: _ } = Z(b);
O = x, L = _;
}
return Ct(B, [O, L])[0];
}
/**
* Non-constant-time multiplication. Uses double-and-add algorithm.
* It's faster, but should only be used when you don't care about
* an exposed secret key e.g. sig verification, which works over *public* keys.
*/
multiplyUnsafe(b) {
const { endo: w } = t, O = this;
if (!s.isValid(b))
throw new Error("invalid scalar: out of range");
if (b === Ot || O.is0())
return B.ZERO;
if (b === W)
return O;
if (F.hasCache(this))
return this.multiply(b);
if (w) {
const { k1neg: L, k1: Z, k2neg: x, k2: _ } = S(b), { p1: A, p2: G } = $r(B, O, Z, _);
return H(w.beta, A, G, L, x);
} else
return F.unsafe(O, b);
}
multiplyAndAddUnsafe(b, w, O) {
const L = this.multiplyUnsafe(w).add(b.multiplyUnsafe(O));
return L.is0() ? void 0 : L;
}
/**
* Converts Projective point to affine (x, y) coordinates.
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
*/
toAffine(b) {
return I(this, b);
}
/**
* Checks whether Point is free of torsion elements (is in prime subgroup).
* Always torsion-free for cofactor=1 curves.
*/
isTorsionFree() {
const { isTorsionFree: b } = t;
return c === W ? !0 : b ? b(B, this) : F.unsafe(this, i).is0();
}
clearCofactor() {
const { clearCofactor: b } = t;
return c === W ? this : b ? b(B, this) : this.multiplyUnsafe(c);
}
isSmallOrder() {
return this.multiplyUnsafe(c).is0();
}
toBytes(b = !0) {
return Je(b, "isCompressed"), this.assertValidity(), h(B, this, b);
}
toHex(b = !0) {
return jt(this.toBytes(b));
}
toString() {
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
}
// TODO: remove
get px() {
return this.X;
}
get py() {