protect-mcp 0.6.3 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2446 @@
1
+ import {
2
+ Hash,
3
+ abytes,
4
+ aexists,
5
+ anumber,
6
+ aoutput,
7
+ bytesToHex,
8
+ clean,
9
+ concatBytes,
10
+ createHasher,
11
+ createView,
12
+ hexToBytes,
13
+ isBytes,
14
+ randomBytes,
15
+ rotr,
16
+ toBytes,
17
+ utf8ToBytes
18
+ } from "./chunk-D733KAPG.mjs";
19
+
20
+ // node_modules/@noble/hashes/esm/_md.js
21
+ function setBigUint64(view, byteOffset, value, isLE) {
22
+ if (typeof view.setBigUint64 === "function")
23
+ return view.setBigUint64(byteOffset, value, isLE);
24
+ const _32n2 = BigInt(32);
25
+ const _u32_max = BigInt(4294967295);
26
+ const wh = Number(value >> _32n2 & _u32_max);
27
+ const wl = Number(value & _u32_max);
28
+ const h = isLE ? 4 : 0;
29
+ const l = isLE ? 0 : 4;
30
+ view.setUint32(byteOffset + h, wh, isLE);
31
+ view.setUint32(byteOffset + l, wl, isLE);
32
+ }
33
+ function Chi(a, b, c) {
34
+ return a & b ^ ~a & c;
35
+ }
36
+ function Maj(a, b, c) {
37
+ return a & b ^ a & c ^ b & c;
38
+ }
39
+ var HashMD = class extends Hash {
40
+ constructor(blockLen, outputLen, padOffset, isLE) {
41
+ super();
42
+ this.finished = false;
43
+ this.length = 0;
44
+ this.pos = 0;
45
+ this.destroyed = false;
46
+ this.blockLen = blockLen;
47
+ this.outputLen = outputLen;
48
+ this.padOffset = padOffset;
49
+ this.isLE = isLE;
50
+ this.buffer = new Uint8Array(blockLen);
51
+ this.view = createView(this.buffer);
52
+ }
53
+ update(data) {
54
+ aexists(this);
55
+ data = toBytes(data);
56
+ abytes(data);
57
+ const { view, buffer, blockLen } = this;
58
+ const len = data.length;
59
+ for (let pos = 0; pos < len; ) {
60
+ const take = Math.min(blockLen - this.pos, len - pos);
61
+ if (take === blockLen) {
62
+ const dataView = createView(data);
63
+ for (; blockLen <= len - pos; pos += blockLen)
64
+ this.process(dataView, pos);
65
+ continue;
66
+ }
67
+ buffer.set(data.subarray(pos, pos + take), this.pos);
68
+ this.pos += take;
69
+ pos += take;
70
+ if (this.pos === blockLen) {
71
+ this.process(view, 0);
72
+ this.pos = 0;
73
+ }
74
+ }
75
+ this.length += data.length;
76
+ this.roundClean();
77
+ return this;
78
+ }
79
+ digestInto(out) {
80
+ aexists(this);
81
+ aoutput(out, this);
82
+ this.finished = true;
83
+ const { buffer, view, blockLen, isLE } = this;
84
+ let { pos } = this;
85
+ buffer[pos++] = 128;
86
+ clean(this.buffer.subarray(pos));
87
+ if (this.padOffset > blockLen - pos) {
88
+ this.process(view, 0);
89
+ pos = 0;
90
+ }
91
+ for (let i = pos; i < blockLen; i++)
92
+ buffer[i] = 0;
93
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
94
+ this.process(view, 0);
95
+ const oview = createView(out);
96
+ const len = this.outputLen;
97
+ if (len % 4)
98
+ throw new Error("_sha2: outputLen should be aligned to 32bit");
99
+ const outLen = len / 4;
100
+ const state = this.get();
101
+ if (outLen > state.length)
102
+ throw new Error("_sha2: outputLen bigger than state");
103
+ for (let i = 0; i < outLen; i++)
104
+ oview.setUint32(4 * i, state[i], isLE);
105
+ }
106
+ digest() {
107
+ const { buffer, outputLen } = this;
108
+ this.digestInto(buffer);
109
+ const res = buffer.slice(0, outputLen);
110
+ this.destroy();
111
+ return res;
112
+ }
113
+ _cloneInto(to) {
114
+ to || (to = new this.constructor());
115
+ to.set(...this.get());
116
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
117
+ to.destroyed = destroyed;
118
+ to.finished = finished;
119
+ to.length = length;
120
+ to.pos = pos;
121
+ if (length % blockLen)
122
+ to.buffer.set(buffer);
123
+ return to;
124
+ }
125
+ clone() {
126
+ return this._cloneInto();
127
+ }
128
+ };
129
+ var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
130
+ 1779033703,
131
+ 3144134277,
132
+ 1013904242,
133
+ 2773480762,
134
+ 1359893119,
135
+ 2600822924,
136
+ 528734635,
137
+ 1541459225
138
+ ]);
139
+ var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
140
+ 1779033703,
141
+ 4089235720,
142
+ 3144134277,
143
+ 2227873595,
144
+ 1013904242,
145
+ 4271175723,
146
+ 2773480762,
147
+ 1595750129,
148
+ 1359893119,
149
+ 2917565137,
150
+ 2600822924,
151
+ 725511199,
152
+ 528734635,
153
+ 4215389547,
154
+ 1541459225,
155
+ 327033209
156
+ ]);
157
+
158
+ // node_modules/@noble/hashes/esm/_u64.js
159
+ var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
160
+ var _32n = /* @__PURE__ */ BigInt(32);
161
+ function fromBig(n, le = false) {
162
+ if (le)
163
+ return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
164
+ return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
165
+ }
166
+ function split(lst, le = false) {
167
+ const len = lst.length;
168
+ let Ah = new Uint32Array(len);
169
+ let Al = new Uint32Array(len);
170
+ for (let i = 0; i < len; i++) {
171
+ const { h, l } = fromBig(lst[i], le);
172
+ [Ah[i], Al[i]] = [h, l];
173
+ }
174
+ return [Ah, Al];
175
+ }
176
+ var shrSH = (h, _l, s) => h >>> s;
177
+ var shrSL = (h, l, s) => h << 32 - s | l >>> s;
178
+ var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
179
+ var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
180
+ var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
181
+ var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
182
+ function add(Ah, Al, Bh, Bl) {
183
+ const l = (Al >>> 0) + (Bl >>> 0);
184
+ return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
185
+ }
186
+ var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
187
+ var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
188
+ var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
189
+ var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
190
+ var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
191
+ var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
192
+
193
+ // node_modules/@noble/hashes/esm/sha2.js
194
+ var SHA256_K = /* @__PURE__ */ Uint32Array.from([
195
+ 1116352408,
196
+ 1899447441,
197
+ 3049323471,
198
+ 3921009573,
199
+ 961987163,
200
+ 1508970993,
201
+ 2453635748,
202
+ 2870763221,
203
+ 3624381080,
204
+ 310598401,
205
+ 607225278,
206
+ 1426881987,
207
+ 1925078388,
208
+ 2162078206,
209
+ 2614888103,
210
+ 3248222580,
211
+ 3835390401,
212
+ 4022224774,
213
+ 264347078,
214
+ 604807628,
215
+ 770255983,
216
+ 1249150122,
217
+ 1555081692,
218
+ 1996064986,
219
+ 2554220882,
220
+ 2821834349,
221
+ 2952996808,
222
+ 3210313671,
223
+ 3336571891,
224
+ 3584528711,
225
+ 113926993,
226
+ 338241895,
227
+ 666307205,
228
+ 773529912,
229
+ 1294757372,
230
+ 1396182291,
231
+ 1695183700,
232
+ 1986661051,
233
+ 2177026350,
234
+ 2456956037,
235
+ 2730485921,
236
+ 2820302411,
237
+ 3259730800,
238
+ 3345764771,
239
+ 3516065817,
240
+ 3600352804,
241
+ 4094571909,
242
+ 275423344,
243
+ 430227734,
244
+ 506948616,
245
+ 659060556,
246
+ 883997877,
247
+ 958139571,
248
+ 1322822218,
249
+ 1537002063,
250
+ 1747873779,
251
+ 1955562222,
252
+ 2024104815,
253
+ 2227730452,
254
+ 2361852424,
255
+ 2428436474,
256
+ 2756734187,
257
+ 3204031479,
258
+ 3329325298
259
+ ]);
260
+ var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
261
+ var SHA256 = class extends HashMD {
262
+ constructor(outputLen = 32) {
263
+ super(64, outputLen, 8, false);
264
+ this.A = SHA256_IV[0] | 0;
265
+ this.B = SHA256_IV[1] | 0;
266
+ this.C = SHA256_IV[2] | 0;
267
+ this.D = SHA256_IV[3] | 0;
268
+ this.E = SHA256_IV[4] | 0;
269
+ this.F = SHA256_IV[5] | 0;
270
+ this.G = SHA256_IV[6] | 0;
271
+ this.H = SHA256_IV[7] | 0;
272
+ }
273
+ get() {
274
+ const { A, B, C, D, E, F, G, H } = this;
275
+ return [A, B, C, D, E, F, G, H];
276
+ }
277
+ // prettier-ignore
278
+ set(A, B, C, D, E, F, G, H) {
279
+ this.A = A | 0;
280
+ this.B = B | 0;
281
+ this.C = C | 0;
282
+ this.D = D | 0;
283
+ this.E = E | 0;
284
+ this.F = F | 0;
285
+ this.G = G | 0;
286
+ this.H = H | 0;
287
+ }
288
+ process(view, offset) {
289
+ for (let i = 0; i < 16; i++, offset += 4)
290
+ SHA256_W[i] = view.getUint32(offset, false);
291
+ for (let i = 16; i < 64; i++) {
292
+ const W15 = SHA256_W[i - 15];
293
+ const W2 = SHA256_W[i - 2];
294
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
295
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
296
+ SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
297
+ }
298
+ let { A, B, C, D, E, F, G, H } = this;
299
+ for (let i = 0; i < 64; i++) {
300
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
301
+ const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
302
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
303
+ const T2 = sigma0 + Maj(A, B, C) | 0;
304
+ H = G;
305
+ G = F;
306
+ F = E;
307
+ E = D + T1 | 0;
308
+ D = C;
309
+ C = B;
310
+ B = A;
311
+ A = T1 + T2 | 0;
312
+ }
313
+ A = A + this.A | 0;
314
+ B = B + this.B | 0;
315
+ C = C + this.C | 0;
316
+ D = D + this.D | 0;
317
+ E = E + this.E | 0;
318
+ F = F + this.F | 0;
319
+ G = G + this.G | 0;
320
+ H = H + this.H | 0;
321
+ this.set(A, B, C, D, E, F, G, H);
322
+ }
323
+ roundClean() {
324
+ clean(SHA256_W);
325
+ }
326
+ destroy() {
327
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
328
+ clean(this.buffer);
329
+ }
330
+ };
331
+ var K512 = /* @__PURE__ */ (() => split([
332
+ "0x428a2f98d728ae22",
333
+ "0x7137449123ef65cd",
334
+ "0xb5c0fbcfec4d3b2f",
335
+ "0xe9b5dba58189dbbc",
336
+ "0x3956c25bf348b538",
337
+ "0x59f111f1b605d019",
338
+ "0x923f82a4af194f9b",
339
+ "0xab1c5ed5da6d8118",
340
+ "0xd807aa98a3030242",
341
+ "0x12835b0145706fbe",
342
+ "0x243185be4ee4b28c",
343
+ "0x550c7dc3d5ffb4e2",
344
+ "0x72be5d74f27b896f",
345
+ "0x80deb1fe3b1696b1",
346
+ "0x9bdc06a725c71235",
347
+ "0xc19bf174cf692694",
348
+ "0xe49b69c19ef14ad2",
349
+ "0xefbe4786384f25e3",
350
+ "0x0fc19dc68b8cd5b5",
351
+ "0x240ca1cc77ac9c65",
352
+ "0x2de92c6f592b0275",
353
+ "0x4a7484aa6ea6e483",
354
+ "0x5cb0a9dcbd41fbd4",
355
+ "0x76f988da831153b5",
356
+ "0x983e5152ee66dfab",
357
+ "0xa831c66d2db43210",
358
+ "0xb00327c898fb213f",
359
+ "0xbf597fc7beef0ee4",
360
+ "0xc6e00bf33da88fc2",
361
+ "0xd5a79147930aa725",
362
+ "0x06ca6351e003826f",
363
+ "0x142929670a0e6e70",
364
+ "0x27b70a8546d22ffc",
365
+ "0x2e1b21385c26c926",
366
+ "0x4d2c6dfc5ac42aed",
367
+ "0x53380d139d95b3df",
368
+ "0x650a73548baf63de",
369
+ "0x766a0abb3c77b2a8",
370
+ "0x81c2c92e47edaee6",
371
+ "0x92722c851482353b",
372
+ "0xa2bfe8a14cf10364",
373
+ "0xa81a664bbc423001",
374
+ "0xc24b8b70d0f89791",
375
+ "0xc76c51a30654be30",
376
+ "0xd192e819d6ef5218",
377
+ "0xd69906245565a910",
378
+ "0xf40e35855771202a",
379
+ "0x106aa07032bbd1b8",
380
+ "0x19a4c116b8d2d0c8",
381
+ "0x1e376c085141ab53",
382
+ "0x2748774cdf8eeb99",
383
+ "0x34b0bcb5e19b48a8",
384
+ "0x391c0cb3c5c95a63",
385
+ "0x4ed8aa4ae3418acb",
386
+ "0x5b9cca4f7763e373",
387
+ "0x682e6ff3d6b2b8a3",
388
+ "0x748f82ee5defb2fc",
389
+ "0x78a5636f43172f60",
390
+ "0x84c87814a1f0ab72",
391
+ "0x8cc702081a6439ec",
392
+ "0x90befffa23631e28",
393
+ "0xa4506cebde82bde9",
394
+ "0xbef9a3f7b2c67915",
395
+ "0xc67178f2e372532b",
396
+ "0xca273eceea26619c",
397
+ "0xd186b8c721c0c207",
398
+ "0xeada7dd6cde0eb1e",
399
+ "0xf57d4f7fee6ed178",
400
+ "0x06f067aa72176fba",
401
+ "0x0a637dc5a2c898a6",
402
+ "0x113f9804bef90dae",
403
+ "0x1b710b35131c471b",
404
+ "0x28db77f523047d84",
405
+ "0x32caab7b40c72493",
406
+ "0x3c9ebe0a15c9bebc",
407
+ "0x431d67c49c100d4c",
408
+ "0x4cc5d4becb3e42b6",
409
+ "0x597f299cfc657e2a",
410
+ "0x5fcb6fab3ad6faec",
411
+ "0x6c44198c4a475817"
412
+ ].map((n) => BigInt(n))))();
413
+ var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
414
+ var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
415
+ var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
416
+ var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
417
+ var SHA512 = class extends HashMD {
418
+ constructor(outputLen = 64) {
419
+ super(128, outputLen, 16, false);
420
+ this.Ah = SHA512_IV[0] | 0;
421
+ this.Al = SHA512_IV[1] | 0;
422
+ this.Bh = SHA512_IV[2] | 0;
423
+ this.Bl = SHA512_IV[3] | 0;
424
+ this.Ch = SHA512_IV[4] | 0;
425
+ this.Cl = SHA512_IV[5] | 0;
426
+ this.Dh = SHA512_IV[6] | 0;
427
+ this.Dl = SHA512_IV[7] | 0;
428
+ this.Eh = SHA512_IV[8] | 0;
429
+ this.El = SHA512_IV[9] | 0;
430
+ this.Fh = SHA512_IV[10] | 0;
431
+ this.Fl = SHA512_IV[11] | 0;
432
+ this.Gh = SHA512_IV[12] | 0;
433
+ this.Gl = SHA512_IV[13] | 0;
434
+ this.Hh = SHA512_IV[14] | 0;
435
+ this.Hl = SHA512_IV[15] | 0;
436
+ }
437
+ // prettier-ignore
438
+ get() {
439
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
440
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
441
+ }
442
+ // prettier-ignore
443
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
444
+ this.Ah = Ah | 0;
445
+ this.Al = Al | 0;
446
+ this.Bh = Bh | 0;
447
+ this.Bl = Bl | 0;
448
+ this.Ch = Ch | 0;
449
+ this.Cl = Cl | 0;
450
+ this.Dh = Dh | 0;
451
+ this.Dl = Dl | 0;
452
+ this.Eh = Eh | 0;
453
+ this.El = El | 0;
454
+ this.Fh = Fh | 0;
455
+ this.Fl = Fl | 0;
456
+ this.Gh = Gh | 0;
457
+ this.Gl = Gl | 0;
458
+ this.Hh = Hh | 0;
459
+ this.Hl = Hl | 0;
460
+ }
461
+ process(view, offset) {
462
+ for (let i = 0; i < 16; i++, offset += 4) {
463
+ SHA512_W_H[i] = view.getUint32(offset);
464
+ SHA512_W_L[i] = view.getUint32(offset += 4);
465
+ }
466
+ for (let i = 16; i < 80; i++) {
467
+ const W15h = SHA512_W_H[i - 15] | 0;
468
+ const W15l = SHA512_W_L[i - 15] | 0;
469
+ const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
470
+ const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
471
+ const W2h = SHA512_W_H[i - 2] | 0;
472
+ const W2l = SHA512_W_L[i - 2] | 0;
473
+ const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
474
+ const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
475
+ const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
476
+ const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
477
+ SHA512_W_H[i] = SUMh | 0;
478
+ SHA512_W_L[i] = SUMl | 0;
479
+ }
480
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
481
+ for (let i = 0; i < 80; i++) {
482
+ const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
483
+ const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
484
+ const CHIh = Eh & Fh ^ ~Eh & Gh;
485
+ const CHIl = El & Fl ^ ~El & Gl;
486
+ const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
487
+ const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
488
+ const T1l = T1ll | 0;
489
+ const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
490
+ const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
491
+ const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
492
+ const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
493
+ Hh = Gh | 0;
494
+ Hl = Gl | 0;
495
+ Gh = Fh | 0;
496
+ Gl = Fl | 0;
497
+ Fh = Eh | 0;
498
+ Fl = El | 0;
499
+ ({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
500
+ Dh = Ch | 0;
501
+ Dl = Cl | 0;
502
+ Ch = Bh | 0;
503
+ Cl = Bl | 0;
504
+ Bh = Ah | 0;
505
+ Bl = Al | 0;
506
+ const All = add3L(T1l, sigma0l, MAJl);
507
+ Ah = add3H(All, T1h, sigma0h, MAJh);
508
+ Al = All | 0;
509
+ }
510
+ ({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
511
+ ({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
512
+ ({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
513
+ ({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
514
+ ({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
515
+ ({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
516
+ ({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
517
+ ({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
518
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
519
+ }
520
+ roundClean() {
521
+ clean(SHA512_W_H, SHA512_W_L);
522
+ }
523
+ destroy() {
524
+ clean(this.buffer);
525
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
526
+ }
527
+ };
528
+ var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
529
+ var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
530
+
531
+ // node_modules/@noble/curves/esm/utils.js
532
+ var _0n = /* @__PURE__ */ BigInt(0);
533
+ var _1n = /* @__PURE__ */ BigInt(1);
534
+ function _abool2(value, title = "") {
535
+ if (typeof value !== "boolean") {
536
+ const prefix = title && `"${title}"`;
537
+ throw new Error(prefix + "expected boolean, got type=" + typeof value);
538
+ }
539
+ return value;
540
+ }
541
+ function _abytes2(value, length, title = "") {
542
+ const bytes = isBytes(value);
543
+ const len = value?.length;
544
+ const needsLen = length !== void 0;
545
+ if (!bytes || needsLen && len !== length) {
546
+ const prefix = title && `"${title}" `;
547
+ const ofLen = needsLen ? ` of length ${length}` : "";
548
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
549
+ throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
550
+ }
551
+ return value;
552
+ }
553
+ function hexToNumber(hex) {
554
+ if (typeof hex !== "string")
555
+ throw new Error("hex string expected, got " + typeof hex);
556
+ return hex === "" ? _0n : BigInt("0x" + hex);
557
+ }
558
+ function bytesToNumberBE(bytes) {
559
+ return hexToNumber(bytesToHex(bytes));
560
+ }
561
+ function bytesToNumberLE(bytes) {
562
+ abytes(bytes);
563
+ return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
564
+ }
565
+ function numberToBytesBE(n, len) {
566
+ return hexToBytes(n.toString(16).padStart(len * 2, "0"));
567
+ }
568
+ function numberToBytesLE(n, len) {
569
+ return numberToBytesBE(n, len).reverse();
570
+ }
571
+ function ensureBytes(title, hex, expectedLength) {
572
+ let res;
573
+ if (typeof hex === "string") {
574
+ try {
575
+ res = hexToBytes(hex);
576
+ } catch (e) {
577
+ throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
578
+ }
579
+ } else if (isBytes(hex)) {
580
+ res = Uint8Array.from(hex);
581
+ } else {
582
+ throw new Error(title + " must be hex string or Uint8Array");
583
+ }
584
+ const len = res.length;
585
+ if (typeof expectedLength === "number" && len !== expectedLength)
586
+ throw new Error(title + " of length " + expectedLength + " expected, got " + len);
587
+ return res;
588
+ }
589
+ function equalBytes(a, b) {
590
+ if (a.length !== b.length)
591
+ return false;
592
+ let diff = 0;
593
+ for (let i = 0; i < a.length; i++)
594
+ diff |= a[i] ^ b[i];
595
+ return diff === 0;
596
+ }
597
+ function copyBytes(bytes) {
598
+ return Uint8Array.from(bytes);
599
+ }
600
+ var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
601
+ function inRange(n, min, max) {
602
+ return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
603
+ }
604
+ function aInRange(title, n, min, max) {
605
+ if (!inRange(n, min, max))
606
+ throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
607
+ }
608
+ function bitLen(n) {
609
+ let len;
610
+ for (len = 0; n > _0n; n >>= _1n, len += 1)
611
+ ;
612
+ return len;
613
+ }
614
+ var bitMask = (n) => (_1n << BigInt(n)) - _1n;
615
+ function isHash(val) {
616
+ return typeof val === "function" && Number.isSafeInteger(val.outputLen);
617
+ }
618
+ function _validateObject(object, fields, optFields = {}) {
619
+ if (!object || typeof object !== "object")
620
+ throw new Error("expected valid options object");
621
+ function checkField(fieldName, expectedType, isOpt) {
622
+ const val = object[fieldName];
623
+ if (isOpt && val === void 0)
624
+ return;
625
+ const current = typeof val;
626
+ if (current !== expectedType || val === null)
627
+ throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
628
+ }
629
+ Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
630
+ Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
631
+ }
632
+ var notImplemented = () => {
633
+ throw new Error("not implemented");
634
+ };
635
+ function memoized(fn) {
636
+ const map = /* @__PURE__ */ new WeakMap();
637
+ return (arg, ...args) => {
638
+ const val = map.get(arg);
639
+ if (val !== void 0)
640
+ return val;
641
+ const computed = fn(arg, ...args);
642
+ map.set(arg, computed);
643
+ return computed;
644
+ };
645
+ }
646
+
647
+ // node_modules/@noble/curves/esm/abstract/modular.js
648
+ var _0n2 = BigInt(0);
649
+ var _1n2 = BigInt(1);
650
+ var _2n = /* @__PURE__ */ BigInt(2);
651
+ var _3n = /* @__PURE__ */ BigInt(3);
652
+ var _4n = /* @__PURE__ */ BigInt(4);
653
+ var _5n = /* @__PURE__ */ BigInt(5);
654
+ var _7n = /* @__PURE__ */ BigInt(7);
655
+ var _8n = /* @__PURE__ */ BigInt(8);
656
+ var _9n = /* @__PURE__ */ BigInt(9);
657
+ var _16n = /* @__PURE__ */ BigInt(16);
658
+ function mod(a, b) {
659
+ const result = a % b;
660
+ return result >= _0n2 ? result : b + result;
661
+ }
662
+ function pow2(x, power, modulo) {
663
+ let res = x;
664
+ while (power-- > _0n2) {
665
+ res *= res;
666
+ res %= modulo;
667
+ }
668
+ return res;
669
+ }
670
+ function invert(number, modulo) {
671
+ if (number === _0n2)
672
+ throw new Error("invert: expected non-zero number");
673
+ if (modulo <= _0n2)
674
+ throw new Error("invert: expected positive modulus, got " + modulo);
675
+ let a = mod(number, modulo);
676
+ let b = modulo;
677
+ let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
678
+ while (a !== _0n2) {
679
+ const q = b / a;
680
+ const r = b % a;
681
+ const m = x - u * q;
682
+ const n = y - v * q;
683
+ b = a, a = r, x = u, y = v, u = m, v = n;
684
+ }
685
+ const gcd = b;
686
+ if (gcd !== _1n2)
687
+ throw new Error("invert: does not exist");
688
+ return mod(x, modulo);
689
+ }
690
+ function assertIsSquare(Fp2, root, n) {
691
+ if (!Fp2.eql(Fp2.sqr(root), n))
692
+ throw new Error("Cannot find square root");
693
+ }
694
+ function sqrt3mod4(Fp2, n) {
695
+ const p1div4 = (Fp2.ORDER + _1n2) / _4n;
696
+ const root = Fp2.pow(n, p1div4);
697
+ assertIsSquare(Fp2, root, n);
698
+ return root;
699
+ }
700
+ function sqrt5mod8(Fp2, n) {
701
+ const p5div8 = (Fp2.ORDER - _5n) / _8n;
702
+ const n2 = Fp2.mul(n, _2n);
703
+ const v = Fp2.pow(n2, p5div8);
704
+ const nv = Fp2.mul(n, v);
705
+ const i = Fp2.mul(Fp2.mul(nv, _2n), v);
706
+ const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
707
+ assertIsSquare(Fp2, root, n);
708
+ return root;
709
+ }
710
+ function sqrt9mod16(P) {
711
+ const Fp_ = Field(P);
712
+ const tn = tonelliShanks(P);
713
+ const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
714
+ const c2 = tn(Fp_, c1);
715
+ const c3 = tn(Fp_, Fp_.neg(c1));
716
+ const c4 = (P + _7n) / _16n;
717
+ return (Fp2, n) => {
718
+ let tv1 = Fp2.pow(n, c4);
719
+ let tv2 = Fp2.mul(tv1, c1);
720
+ const tv3 = Fp2.mul(tv1, c2);
721
+ const tv4 = Fp2.mul(tv1, c3);
722
+ const e1 = Fp2.eql(Fp2.sqr(tv2), n);
723
+ const e2 = Fp2.eql(Fp2.sqr(tv3), n);
724
+ tv1 = Fp2.cmov(tv1, tv2, e1);
725
+ tv2 = Fp2.cmov(tv4, tv3, e2);
726
+ const e3 = Fp2.eql(Fp2.sqr(tv2), n);
727
+ const root = Fp2.cmov(tv1, tv2, e3);
728
+ assertIsSquare(Fp2, root, n);
729
+ return root;
730
+ };
731
+ }
732
+ function tonelliShanks(P) {
733
+ if (P < _3n)
734
+ throw new Error("sqrt is not defined for small field");
735
+ let Q = P - _1n2;
736
+ let S = 0;
737
+ while (Q % _2n === _0n2) {
738
+ Q /= _2n;
739
+ S++;
740
+ }
741
+ let Z = _2n;
742
+ const _Fp = Field(P);
743
+ while (FpLegendre(_Fp, Z) === 1) {
744
+ if (Z++ > 1e3)
745
+ throw new Error("Cannot find square root: probably non-prime P");
746
+ }
747
+ if (S === 1)
748
+ return sqrt3mod4;
749
+ let cc = _Fp.pow(Z, Q);
750
+ const Q1div2 = (Q + _1n2) / _2n;
751
+ return function tonelliSlow(Fp2, n) {
752
+ if (Fp2.is0(n))
753
+ return n;
754
+ if (FpLegendre(Fp2, n) !== 1)
755
+ throw new Error("Cannot find square root");
756
+ let M = S;
757
+ let c = Fp2.mul(Fp2.ONE, cc);
758
+ let t = Fp2.pow(n, Q);
759
+ let R = Fp2.pow(n, Q1div2);
760
+ while (!Fp2.eql(t, Fp2.ONE)) {
761
+ if (Fp2.is0(t))
762
+ return Fp2.ZERO;
763
+ let i = 1;
764
+ let t_tmp = Fp2.sqr(t);
765
+ while (!Fp2.eql(t_tmp, Fp2.ONE)) {
766
+ i++;
767
+ t_tmp = Fp2.sqr(t_tmp);
768
+ if (i === M)
769
+ throw new Error("Cannot find square root");
770
+ }
771
+ const exponent = _1n2 << BigInt(M - i - 1);
772
+ const b = Fp2.pow(c, exponent);
773
+ M = i;
774
+ c = Fp2.sqr(b);
775
+ t = Fp2.mul(t, c);
776
+ R = Fp2.mul(R, b);
777
+ }
778
+ return R;
779
+ };
780
+ }
781
+ function FpSqrt(P) {
782
+ if (P % _4n === _3n)
783
+ return sqrt3mod4;
784
+ if (P % _8n === _5n)
785
+ return sqrt5mod8;
786
+ if (P % _16n === _9n)
787
+ return sqrt9mod16(P);
788
+ return tonelliShanks(P);
789
+ }
790
+ var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
791
+ var FIELD_FIELDS = [
792
+ "create",
793
+ "isValid",
794
+ "is0",
795
+ "neg",
796
+ "inv",
797
+ "sqrt",
798
+ "sqr",
799
+ "eql",
800
+ "add",
801
+ "sub",
802
+ "mul",
803
+ "pow",
804
+ "div",
805
+ "addN",
806
+ "subN",
807
+ "mulN",
808
+ "sqrN"
809
+ ];
810
+ function validateField(field) {
811
+ const initial = {
812
+ ORDER: "bigint",
813
+ MASK: "bigint",
814
+ BYTES: "number",
815
+ BITS: "number"
816
+ };
817
+ const opts = FIELD_FIELDS.reduce((map, val) => {
818
+ map[val] = "function";
819
+ return map;
820
+ }, initial);
821
+ _validateObject(field, opts);
822
+ return field;
823
+ }
824
+ function FpPow(Fp2, num, power) {
825
+ if (power < _0n2)
826
+ throw new Error("invalid exponent, negatives unsupported");
827
+ if (power === _0n2)
828
+ return Fp2.ONE;
829
+ if (power === _1n2)
830
+ return num;
831
+ let p = Fp2.ONE;
832
+ let d = num;
833
+ while (power > _0n2) {
834
+ if (power & _1n2)
835
+ p = Fp2.mul(p, d);
836
+ d = Fp2.sqr(d);
837
+ power >>= _1n2;
838
+ }
839
+ return p;
840
+ }
841
+ function FpInvertBatch(Fp2, nums, passZero = false) {
842
+ const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
843
+ const multipliedAcc = nums.reduce((acc, num, i) => {
844
+ if (Fp2.is0(num))
845
+ return acc;
846
+ inverted[i] = acc;
847
+ return Fp2.mul(acc, num);
848
+ }, Fp2.ONE);
849
+ const invertedAcc = Fp2.inv(multipliedAcc);
850
+ nums.reduceRight((acc, num, i) => {
851
+ if (Fp2.is0(num))
852
+ return acc;
853
+ inverted[i] = Fp2.mul(acc, inverted[i]);
854
+ return Fp2.mul(acc, num);
855
+ }, invertedAcc);
856
+ return inverted;
857
+ }
858
+ function FpLegendre(Fp2, n) {
859
+ const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
860
+ const powered = Fp2.pow(n, p1mod2);
861
+ const yes = Fp2.eql(powered, Fp2.ONE);
862
+ const zero = Fp2.eql(powered, Fp2.ZERO);
863
+ const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
864
+ if (!yes && !zero && !no)
865
+ throw new Error("invalid Legendre symbol result");
866
+ return yes ? 1 : zero ? 0 : -1;
867
+ }
868
+ function nLength(n, nBitLength) {
869
+ if (nBitLength !== void 0)
870
+ anumber(nBitLength);
871
+ const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
872
+ const nByteLength = Math.ceil(_nBitLength / 8);
873
+ return { nBitLength: _nBitLength, nByteLength };
874
+ }
875
+ function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
876
+ if (ORDER <= _0n2)
877
+ throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
878
+ let _nbitLength = void 0;
879
+ let _sqrt = void 0;
880
+ let modFromBytes = false;
881
+ let allowedLengths = void 0;
882
+ if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
883
+ if (opts.sqrt || isLE)
884
+ throw new Error("cannot specify opts in two arguments");
885
+ const _opts = bitLenOrOpts;
886
+ if (_opts.BITS)
887
+ _nbitLength = _opts.BITS;
888
+ if (_opts.sqrt)
889
+ _sqrt = _opts.sqrt;
890
+ if (typeof _opts.isLE === "boolean")
891
+ isLE = _opts.isLE;
892
+ if (typeof _opts.modFromBytes === "boolean")
893
+ modFromBytes = _opts.modFromBytes;
894
+ allowedLengths = _opts.allowedLengths;
895
+ } else {
896
+ if (typeof bitLenOrOpts === "number")
897
+ _nbitLength = bitLenOrOpts;
898
+ if (opts.sqrt)
899
+ _sqrt = opts.sqrt;
900
+ }
901
+ const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
902
+ if (BYTES > 2048)
903
+ throw new Error("invalid field: expected ORDER of <= 2048 bytes");
904
+ let sqrtP;
905
+ const f = Object.freeze({
906
+ ORDER,
907
+ isLE,
908
+ BITS,
909
+ BYTES,
910
+ MASK: bitMask(BITS),
911
+ ZERO: _0n2,
912
+ ONE: _1n2,
913
+ allowedLengths,
914
+ create: (num) => mod(num, ORDER),
915
+ isValid: (num) => {
916
+ if (typeof num !== "bigint")
917
+ throw new Error("invalid field element: expected bigint, got " + typeof num);
918
+ return _0n2 <= num && num < ORDER;
919
+ },
920
+ is0: (num) => num === _0n2,
921
+ // is valid and invertible
922
+ isValidNot0: (num) => !f.is0(num) && f.isValid(num),
923
+ isOdd: (num) => (num & _1n2) === _1n2,
924
+ neg: (num) => mod(-num, ORDER),
925
+ eql: (lhs, rhs) => lhs === rhs,
926
+ sqr: (num) => mod(num * num, ORDER),
927
+ add: (lhs, rhs) => mod(lhs + rhs, ORDER),
928
+ sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
929
+ mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
930
+ pow: (num, power) => FpPow(f, num, power),
931
+ div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
932
+ // Same as above, but doesn't normalize
933
+ sqrN: (num) => num * num,
934
+ addN: (lhs, rhs) => lhs + rhs,
935
+ subN: (lhs, rhs) => lhs - rhs,
936
+ mulN: (lhs, rhs) => lhs * rhs,
937
+ inv: (num) => invert(num, ORDER),
938
+ sqrt: _sqrt || ((n) => {
939
+ if (!sqrtP)
940
+ sqrtP = FpSqrt(ORDER);
941
+ return sqrtP(f, n);
942
+ }),
943
+ toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
944
+ fromBytes: (bytes, skipValidation = true) => {
945
+ if (allowedLengths) {
946
+ if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
947
+ throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
948
+ }
949
+ const padded = new Uint8Array(BYTES);
950
+ padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
951
+ bytes = padded;
952
+ }
953
+ if (bytes.length !== BYTES)
954
+ throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
955
+ let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
956
+ if (modFromBytes)
957
+ scalar = mod(scalar, ORDER);
958
+ if (!skipValidation) {
959
+ if (!f.isValid(scalar))
960
+ throw new Error("invalid field element: outside of range 0..ORDER");
961
+ }
962
+ return scalar;
963
+ },
964
+ // TODO: we don't need it here, move out to separate fn
965
+ invertBatch: (lst) => FpInvertBatch(f, lst),
966
+ // We can't move this out because Fp6, Fp12 implement it
967
+ // and it's unclear what to return in there.
968
+ cmov: (a, b, c) => c ? b : a
969
+ });
970
+ return Object.freeze(f);
971
+ }
972
+ function FpSqrtEven(Fp2, elm) {
973
+ if (!Fp2.isOdd)
974
+ throw new Error("Field doesn't have isOdd");
975
+ const root = Fp2.sqrt(elm);
976
+ return Fp2.isOdd(root) ? Fp2.neg(root) : root;
977
+ }
978
+
979
+ // node_modules/@noble/curves/esm/abstract/curve.js
980
+ var _0n3 = BigInt(0);
981
+ var _1n3 = BigInt(1);
982
+ function negateCt(condition, item) {
983
+ const neg = item.negate();
984
+ return condition ? neg : item;
985
+ }
986
+ function normalizeZ(c, points) {
987
+ const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
988
+ return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
989
+ }
990
+ function validateW(W, bits) {
991
+ if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
992
+ throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
993
+ }
994
+ function calcWOpts(W, scalarBits) {
995
+ validateW(W, scalarBits);
996
+ const windows = Math.ceil(scalarBits / W) + 1;
997
+ const windowSize = 2 ** (W - 1);
998
+ const maxNumber = 2 ** W;
999
+ const mask = bitMask(W);
1000
+ const shiftBy = BigInt(W);
1001
+ return { windows, windowSize, mask, maxNumber, shiftBy };
1002
+ }
1003
+ function calcOffsets(n, window, wOpts) {
1004
+ const { windowSize, mask, maxNumber, shiftBy } = wOpts;
1005
+ let wbits = Number(n & mask);
1006
+ let nextN = n >> shiftBy;
1007
+ if (wbits > windowSize) {
1008
+ wbits -= maxNumber;
1009
+ nextN += _1n3;
1010
+ }
1011
+ const offsetStart = window * windowSize;
1012
+ const offset = offsetStart + Math.abs(wbits) - 1;
1013
+ const isZero = wbits === 0;
1014
+ const isNeg = wbits < 0;
1015
+ const isNegF = window % 2 !== 0;
1016
+ const offsetF = offsetStart;
1017
+ return { nextN, offset, isZero, isNeg, isNegF, offsetF };
1018
+ }
1019
+ function validateMSMPoints(points, c) {
1020
+ if (!Array.isArray(points))
1021
+ throw new Error("array expected");
1022
+ points.forEach((p, i) => {
1023
+ if (!(p instanceof c))
1024
+ throw new Error("invalid point at index " + i);
1025
+ });
1026
+ }
1027
+ function validateMSMScalars(scalars, field) {
1028
+ if (!Array.isArray(scalars))
1029
+ throw new Error("array of scalars expected");
1030
+ scalars.forEach((s, i) => {
1031
+ if (!field.isValid(s))
1032
+ throw new Error("invalid scalar at index " + i);
1033
+ });
1034
+ }
1035
+ var pointPrecomputes = /* @__PURE__ */ new WeakMap();
1036
+ var pointWindowSizes = /* @__PURE__ */ new WeakMap();
1037
+ function getW(P) {
1038
+ return pointWindowSizes.get(P) || 1;
1039
+ }
1040
+ function assert0(n) {
1041
+ if (n !== _0n3)
1042
+ throw new Error("invalid wNAF");
1043
+ }
1044
+ var wNAF = class {
1045
+ // Parametrized with a given Point class (not individual point)
1046
+ constructor(Point, bits) {
1047
+ this.BASE = Point.BASE;
1048
+ this.ZERO = Point.ZERO;
1049
+ this.Fn = Point.Fn;
1050
+ this.bits = bits;
1051
+ }
1052
+ // non-const time multiplication ladder
1053
+ _unsafeLadder(elm, n, p = this.ZERO) {
1054
+ let d = elm;
1055
+ while (n > _0n3) {
1056
+ if (n & _1n3)
1057
+ p = p.add(d);
1058
+ d = d.double();
1059
+ n >>= _1n3;
1060
+ }
1061
+ return p;
1062
+ }
1063
+ /**
1064
+ * Creates a wNAF precomputation window. Used for caching.
1065
+ * Default window size is set by `utils.precompute()` and is equal to 8.
1066
+ * Number of precomputed points depends on the curve size:
1067
+ * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
1068
+ * - 𝑊 is the window size
1069
+ * - 𝑛 is the bitlength of the curve order.
1070
+ * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
1071
+ * @param point Point instance
1072
+ * @param W window size
1073
+ * @returns precomputed point tables flattened to a single array
1074
+ */
1075
+ precomputeWindow(point, W) {
1076
+ const { windows, windowSize } = calcWOpts(W, this.bits);
1077
+ const points = [];
1078
+ let p = point;
1079
+ let base = p;
1080
+ for (let window = 0; window < windows; window++) {
1081
+ base = p;
1082
+ points.push(base);
1083
+ for (let i = 1; i < windowSize; i++) {
1084
+ base = base.add(p);
1085
+ points.push(base);
1086
+ }
1087
+ p = base.double();
1088
+ }
1089
+ return points;
1090
+ }
1091
+ /**
1092
+ * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
1093
+ * More compact implementation:
1094
+ * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
1095
+ * @returns real and fake (for const-time) points
1096
+ */
1097
+ wNAF(W, precomputes, n) {
1098
+ if (!this.Fn.isValid(n))
1099
+ throw new Error("invalid scalar");
1100
+ let p = this.ZERO;
1101
+ let f = this.BASE;
1102
+ const wo = calcWOpts(W, this.bits);
1103
+ for (let window = 0; window < wo.windows; window++) {
1104
+ const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
1105
+ n = nextN;
1106
+ if (isZero) {
1107
+ f = f.add(negateCt(isNegF, precomputes[offsetF]));
1108
+ } else {
1109
+ p = p.add(negateCt(isNeg, precomputes[offset]));
1110
+ }
1111
+ }
1112
+ assert0(n);
1113
+ return { p, f };
1114
+ }
1115
+ /**
1116
+ * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
1117
+ * @param acc accumulator point to add result of multiplication
1118
+ * @returns point
1119
+ */
1120
+ wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
1121
+ const wo = calcWOpts(W, this.bits);
1122
+ for (let window = 0; window < wo.windows; window++) {
1123
+ if (n === _0n3)
1124
+ break;
1125
+ const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
1126
+ n = nextN;
1127
+ if (isZero) {
1128
+ continue;
1129
+ } else {
1130
+ const item = precomputes[offset];
1131
+ acc = acc.add(isNeg ? item.negate() : item);
1132
+ }
1133
+ }
1134
+ assert0(n);
1135
+ return acc;
1136
+ }
1137
+ getPrecomputes(W, point, transform) {
1138
+ let comp = pointPrecomputes.get(point);
1139
+ if (!comp) {
1140
+ comp = this.precomputeWindow(point, W);
1141
+ if (W !== 1) {
1142
+ if (typeof transform === "function")
1143
+ comp = transform(comp);
1144
+ pointPrecomputes.set(point, comp);
1145
+ }
1146
+ }
1147
+ return comp;
1148
+ }
1149
+ cached(point, scalar, transform) {
1150
+ const W = getW(point);
1151
+ return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
1152
+ }
1153
+ unsafe(point, scalar, transform, prev) {
1154
+ const W = getW(point);
1155
+ if (W === 1)
1156
+ return this._unsafeLadder(point, scalar, prev);
1157
+ return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
1158
+ }
1159
+ // We calculate precomputes for elliptic curve point multiplication
1160
+ // using windowed method. This specifies window size and
1161
+ // stores precomputed values. Usually only base point would be precomputed.
1162
+ createCache(P, W) {
1163
+ validateW(W, this.bits);
1164
+ pointWindowSizes.set(P, W);
1165
+ pointPrecomputes.delete(P);
1166
+ }
1167
+ hasCache(elm) {
1168
+ return getW(elm) !== 1;
1169
+ }
1170
+ };
1171
+ function pippenger(c, fieldN, points, scalars) {
1172
+ validateMSMPoints(points, c);
1173
+ validateMSMScalars(scalars, fieldN);
1174
+ const plength = points.length;
1175
+ const slength = scalars.length;
1176
+ if (plength !== slength)
1177
+ throw new Error("arrays of points and scalars must have equal length");
1178
+ const zero = c.ZERO;
1179
+ const wbits = bitLen(BigInt(plength));
1180
+ let windowSize = 1;
1181
+ if (wbits > 12)
1182
+ windowSize = wbits - 3;
1183
+ else if (wbits > 4)
1184
+ windowSize = wbits - 2;
1185
+ else if (wbits > 0)
1186
+ windowSize = 2;
1187
+ const MASK = bitMask(windowSize);
1188
+ const buckets = new Array(Number(MASK) + 1).fill(zero);
1189
+ const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
1190
+ let sum = zero;
1191
+ for (let i = lastBits; i >= 0; i -= windowSize) {
1192
+ buckets.fill(zero);
1193
+ for (let j = 0; j < slength; j++) {
1194
+ const scalar = scalars[j];
1195
+ const wbits2 = Number(scalar >> BigInt(i) & MASK);
1196
+ buckets[wbits2] = buckets[wbits2].add(points[j]);
1197
+ }
1198
+ let resI = zero;
1199
+ for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
1200
+ sumI = sumI.add(buckets[j]);
1201
+ resI = resI.add(sumI);
1202
+ }
1203
+ sum = sum.add(resI);
1204
+ if (i !== 0)
1205
+ for (let j = 0; j < windowSize; j++)
1206
+ sum = sum.double();
1207
+ }
1208
+ return sum;
1209
+ }
1210
+ function createField(order, field, isLE) {
1211
+ if (field) {
1212
+ if (field.ORDER !== order)
1213
+ throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
1214
+ validateField(field);
1215
+ return field;
1216
+ } else {
1217
+ return Field(order, { isLE });
1218
+ }
1219
+ }
1220
+ function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
1221
+ if (FpFnLE === void 0)
1222
+ FpFnLE = type === "edwards";
1223
+ if (!CURVE || typeof CURVE !== "object")
1224
+ throw new Error(`expected valid ${type} CURVE object`);
1225
+ for (const p of ["p", "n", "h"]) {
1226
+ const val = CURVE[p];
1227
+ if (!(typeof val === "bigint" && val > _0n3))
1228
+ throw new Error(`CURVE.${p} must be positive bigint`);
1229
+ }
1230
+ const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
1231
+ const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
1232
+ const _b = type === "weierstrass" ? "b" : "d";
1233
+ const params = ["Gx", "Gy", "a", _b];
1234
+ for (const p of params) {
1235
+ if (!Fp2.isValid(CURVE[p]))
1236
+ throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
1237
+ }
1238
+ CURVE = Object.freeze(Object.assign({}, CURVE));
1239
+ return { CURVE, Fp: Fp2, Fn: Fn2 };
1240
+ }
1241
+
1242
+ // node_modules/@noble/curves/esm/abstract/edwards.js
1243
+ var _0n4 = BigInt(0);
1244
+ var _1n4 = BigInt(1);
1245
+ var _2n2 = BigInt(2);
1246
+ var _8n2 = BigInt(8);
1247
+ function isEdValidXY(Fp2, CURVE, x, y) {
1248
+ const x2 = Fp2.sqr(x);
1249
+ const y2 = Fp2.sqr(y);
1250
+ const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
1251
+ const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
1252
+ return Fp2.eql(left, right);
1253
+ }
1254
+ function edwards(params, extraOpts = {}) {
1255
+ const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
1256
+ const { Fp: Fp2, Fn: Fn2 } = validated;
1257
+ let CURVE = validated.CURVE;
1258
+ const { h: cofactor } = CURVE;
1259
+ _validateObject(extraOpts, {}, { uvRatio: "function" });
1260
+ const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
1261
+ const modP = (n) => Fp2.create(n);
1262
+ const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
1263
+ try {
1264
+ return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
1265
+ } catch (e) {
1266
+ return { isValid: false, value: _0n4 };
1267
+ }
1268
+ });
1269
+ if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
1270
+ throw new Error("bad curve params: generator point");
1271
+ function acoord(title, n, banZero = false) {
1272
+ const min = banZero ? _1n4 : _0n4;
1273
+ aInRange("coordinate " + title, n, min, MASK);
1274
+ return n;
1275
+ }
1276
+ function aextpoint(other) {
1277
+ if (!(other instanceof Point))
1278
+ throw new Error("ExtendedPoint expected");
1279
+ }
1280
+ const toAffineMemo = memoized((p, iz) => {
1281
+ const { X, Y, Z } = p;
1282
+ const is0 = p.is0();
1283
+ if (iz == null)
1284
+ iz = is0 ? _8n2 : Fp2.inv(Z);
1285
+ const x = modP(X * iz);
1286
+ const y = modP(Y * iz);
1287
+ const zz = Fp2.mul(Z, iz);
1288
+ if (is0)
1289
+ return { x: _0n4, y: _1n4 };
1290
+ if (zz !== _1n4)
1291
+ throw new Error("invZ was invalid");
1292
+ return { x, y };
1293
+ });
1294
+ const assertValidMemo = memoized((p) => {
1295
+ const { a, d } = CURVE;
1296
+ if (p.is0())
1297
+ throw new Error("bad point: ZERO");
1298
+ const { X, Y, Z, T } = p;
1299
+ const X2 = modP(X * X);
1300
+ const Y2 = modP(Y * Y);
1301
+ const Z2 = modP(Z * Z);
1302
+ const Z4 = modP(Z2 * Z2);
1303
+ const aX2 = modP(X2 * a);
1304
+ const left = modP(Z2 * modP(aX2 + Y2));
1305
+ const right = modP(Z4 + modP(d * modP(X2 * Y2)));
1306
+ if (left !== right)
1307
+ throw new Error("bad point: equation left != right (1)");
1308
+ const XY = modP(X * Y);
1309
+ const ZT = modP(Z * T);
1310
+ if (XY !== ZT)
1311
+ throw new Error("bad point: equation left != right (2)");
1312
+ return true;
1313
+ });
1314
+ class Point {
1315
+ constructor(X, Y, Z, T) {
1316
+ this.X = acoord("x", X);
1317
+ this.Y = acoord("y", Y);
1318
+ this.Z = acoord("z", Z, true);
1319
+ this.T = acoord("t", T);
1320
+ Object.freeze(this);
1321
+ }
1322
+ static CURVE() {
1323
+ return CURVE;
1324
+ }
1325
+ static fromAffine(p) {
1326
+ if (p instanceof Point)
1327
+ throw new Error("extended point not allowed");
1328
+ const { x, y } = p || {};
1329
+ acoord("x", x);
1330
+ acoord("y", y);
1331
+ return new Point(x, y, _1n4, modP(x * y));
1332
+ }
1333
+ // Uses algo from RFC8032 5.1.3.
1334
+ static fromBytes(bytes, zip215 = false) {
1335
+ const len = Fp2.BYTES;
1336
+ const { a, d } = CURVE;
1337
+ bytes = copyBytes(_abytes2(bytes, len, "point"));
1338
+ _abool2(zip215, "zip215");
1339
+ const normed = copyBytes(bytes);
1340
+ const lastByte = bytes[len - 1];
1341
+ normed[len - 1] = lastByte & ~128;
1342
+ const y = bytesToNumberLE(normed);
1343
+ const max = zip215 ? MASK : Fp2.ORDER;
1344
+ aInRange("point.y", y, _0n4, max);
1345
+ const y2 = modP(y * y);
1346
+ const u = modP(y2 - _1n4);
1347
+ const v = modP(d * y2 - a);
1348
+ let { isValid, value: x } = uvRatio2(u, v);
1349
+ if (!isValid)
1350
+ throw new Error("bad point: invalid y coordinate");
1351
+ const isXOdd = (x & _1n4) === _1n4;
1352
+ const isLastByteOdd = (lastByte & 128) !== 0;
1353
+ if (!zip215 && x === _0n4 && isLastByteOdd)
1354
+ throw new Error("bad point: x=0 and x_0=1");
1355
+ if (isLastByteOdd !== isXOdd)
1356
+ x = modP(-x);
1357
+ return Point.fromAffine({ x, y });
1358
+ }
1359
+ static fromHex(bytes, zip215 = false) {
1360
+ return Point.fromBytes(ensureBytes("point", bytes), zip215);
1361
+ }
1362
+ get x() {
1363
+ return this.toAffine().x;
1364
+ }
1365
+ get y() {
1366
+ return this.toAffine().y;
1367
+ }
1368
+ precompute(windowSize = 8, isLazy = true) {
1369
+ wnaf.createCache(this, windowSize);
1370
+ if (!isLazy)
1371
+ this.multiply(_2n2);
1372
+ return this;
1373
+ }
1374
+ // Useful in fromAffine() - not for fromBytes(), which always created valid points.
1375
+ assertValidity() {
1376
+ assertValidMemo(this);
1377
+ }
1378
+ // Compare one point to another.
1379
+ equals(other) {
1380
+ aextpoint(other);
1381
+ const { X: X1, Y: Y1, Z: Z1 } = this;
1382
+ const { X: X2, Y: Y2, Z: Z2 } = other;
1383
+ const X1Z2 = modP(X1 * Z2);
1384
+ const X2Z1 = modP(X2 * Z1);
1385
+ const Y1Z2 = modP(Y1 * Z2);
1386
+ const Y2Z1 = modP(Y2 * Z1);
1387
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
1388
+ }
1389
+ is0() {
1390
+ return this.equals(Point.ZERO);
1391
+ }
1392
+ negate() {
1393
+ return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
1394
+ }
1395
+ // Fast algo for doubling Extended Point.
1396
+ // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
1397
+ // Cost: 4M + 4S + 1*a + 6add + 1*2.
1398
+ double() {
1399
+ const { a } = CURVE;
1400
+ const { X: X1, Y: Y1, Z: Z1 } = this;
1401
+ const A = modP(X1 * X1);
1402
+ const B = modP(Y1 * Y1);
1403
+ const C = modP(_2n2 * modP(Z1 * Z1));
1404
+ const D = modP(a * A);
1405
+ const x1y1 = X1 + Y1;
1406
+ const E = modP(modP(x1y1 * x1y1) - A - B);
1407
+ const G = D + B;
1408
+ const F = G - C;
1409
+ const H = D - B;
1410
+ const X3 = modP(E * F);
1411
+ const Y3 = modP(G * H);
1412
+ const T3 = modP(E * H);
1413
+ const Z3 = modP(F * G);
1414
+ return new Point(X3, Y3, Z3, T3);
1415
+ }
1416
+ // Fast algo for adding 2 Extended Points.
1417
+ // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
1418
+ // Cost: 9M + 1*a + 1*d + 7add.
1419
+ add(other) {
1420
+ aextpoint(other);
1421
+ const { a, d } = CURVE;
1422
+ const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
1423
+ const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
1424
+ const A = modP(X1 * X2);
1425
+ const B = modP(Y1 * Y2);
1426
+ const C = modP(T1 * d * T2);
1427
+ const D = modP(Z1 * Z2);
1428
+ const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
1429
+ const F = D - C;
1430
+ const G = D + C;
1431
+ const H = modP(B - a * A);
1432
+ const X3 = modP(E * F);
1433
+ const Y3 = modP(G * H);
1434
+ const T3 = modP(E * H);
1435
+ const Z3 = modP(F * G);
1436
+ return new Point(X3, Y3, Z3, T3);
1437
+ }
1438
+ subtract(other) {
1439
+ return this.add(other.negate());
1440
+ }
1441
+ // Constant-time multiplication.
1442
+ multiply(scalar) {
1443
+ if (!Fn2.isValidNot0(scalar))
1444
+ throw new Error("invalid scalar: expected 1 <= sc < curve.n");
1445
+ const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
1446
+ return normalizeZ(Point, [p, f])[0];
1447
+ }
1448
+ // Non-constant-time multiplication. Uses double-and-add algorithm.
1449
+ // It's faster, but should only be used when you don't care about
1450
+ // an exposed private key e.g. sig verification.
1451
+ // Does NOT allow scalars higher than CURVE.n.
1452
+ // Accepts optional accumulator to merge with multiply (important for sparse scalars)
1453
+ multiplyUnsafe(scalar, acc = Point.ZERO) {
1454
+ if (!Fn2.isValid(scalar))
1455
+ throw new Error("invalid scalar: expected 0 <= sc < curve.n");
1456
+ if (scalar === _0n4)
1457
+ return Point.ZERO;
1458
+ if (this.is0() || scalar === _1n4)
1459
+ return this;
1460
+ return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
1461
+ }
1462
+ // Checks if point is of small order.
1463
+ // If you add something to small order point, you will have "dirty"
1464
+ // point with torsion component.
1465
+ // Multiplies point by cofactor and checks if the result is 0.
1466
+ isSmallOrder() {
1467
+ return this.multiplyUnsafe(cofactor).is0();
1468
+ }
1469
+ // Multiplies point by curve order and checks if the result is 0.
1470
+ // Returns `false` is the point is dirty.
1471
+ isTorsionFree() {
1472
+ return wnaf.unsafe(this, CURVE.n).is0();
1473
+ }
1474
+ // Converts Extended point to default (x, y) coordinates.
1475
+ // Can accept precomputed Z^-1 - for example, from invertBatch.
1476
+ toAffine(invertedZ) {
1477
+ return toAffineMemo(this, invertedZ);
1478
+ }
1479
+ clearCofactor() {
1480
+ if (cofactor === _1n4)
1481
+ return this;
1482
+ return this.multiplyUnsafe(cofactor);
1483
+ }
1484
+ toBytes() {
1485
+ const { x, y } = this.toAffine();
1486
+ const bytes = Fp2.toBytes(y);
1487
+ bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
1488
+ return bytes;
1489
+ }
1490
+ toHex() {
1491
+ return bytesToHex(this.toBytes());
1492
+ }
1493
+ toString() {
1494
+ return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
1495
+ }
1496
+ // TODO: remove
1497
+ get ex() {
1498
+ return this.X;
1499
+ }
1500
+ get ey() {
1501
+ return this.Y;
1502
+ }
1503
+ get ez() {
1504
+ return this.Z;
1505
+ }
1506
+ get et() {
1507
+ return this.T;
1508
+ }
1509
+ static normalizeZ(points) {
1510
+ return normalizeZ(Point, points);
1511
+ }
1512
+ static msm(points, scalars) {
1513
+ return pippenger(Point, Fn2, points, scalars);
1514
+ }
1515
+ _setWindowSize(windowSize) {
1516
+ this.precompute(windowSize);
1517
+ }
1518
+ toRawBytes() {
1519
+ return this.toBytes();
1520
+ }
1521
+ }
1522
+ Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
1523
+ Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
1524
+ Point.Fp = Fp2;
1525
+ Point.Fn = Fn2;
1526
+ const wnaf = new wNAF(Point, Fn2.BITS);
1527
+ Point.BASE.precompute(8);
1528
+ return Point;
1529
+ }
1530
+ var PrimeEdwardsPoint = class {
1531
+ constructor(ep) {
1532
+ this.ep = ep;
1533
+ }
1534
+ // Static methods that must be implemented by subclasses
1535
+ static fromBytes(_bytes) {
1536
+ notImplemented();
1537
+ }
1538
+ static fromHex(_hex) {
1539
+ notImplemented();
1540
+ }
1541
+ get x() {
1542
+ return this.toAffine().x;
1543
+ }
1544
+ get y() {
1545
+ return this.toAffine().y;
1546
+ }
1547
+ // Common implementations
1548
+ clearCofactor() {
1549
+ return this;
1550
+ }
1551
+ assertValidity() {
1552
+ this.ep.assertValidity();
1553
+ }
1554
+ toAffine(invertedZ) {
1555
+ return this.ep.toAffine(invertedZ);
1556
+ }
1557
+ toHex() {
1558
+ return bytesToHex(this.toBytes());
1559
+ }
1560
+ toString() {
1561
+ return this.toHex();
1562
+ }
1563
+ isTorsionFree() {
1564
+ return true;
1565
+ }
1566
+ isSmallOrder() {
1567
+ return false;
1568
+ }
1569
+ add(other) {
1570
+ this.assertSame(other);
1571
+ return this.init(this.ep.add(other.ep));
1572
+ }
1573
+ subtract(other) {
1574
+ this.assertSame(other);
1575
+ return this.init(this.ep.subtract(other.ep));
1576
+ }
1577
+ multiply(scalar) {
1578
+ return this.init(this.ep.multiply(scalar));
1579
+ }
1580
+ multiplyUnsafe(scalar) {
1581
+ return this.init(this.ep.multiplyUnsafe(scalar));
1582
+ }
1583
+ double() {
1584
+ return this.init(this.ep.double());
1585
+ }
1586
+ negate() {
1587
+ return this.init(this.ep.negate());
1588
+ }
1589
+ precompute(windowSize, isLazy) {
1590
+ return this.init(this.ep.precompute(windowSize, isLazy));
1591
+ }
1592
+ /** @deprecated use `toBytes` */
1593
+ toRawBytes() {
1594
+ return this.toBytes();
1595
+ }
1596
+ };
1597
+ function eddsa(Point, cHash, eddsaOpts = {}) {
1598
+ if (typeof cHash !== "function")
1599
+ throw new Error('"hash" function param is required');
1600
+ _validateObject(eddsaOpts, {}, {
1601
+ adjustScalarBytes: "function",
1602
+ randomBytes: "function",
1603
+ domain: "function",
1604
+ prehash: "function",
1605
+ mapToCurve: "function"
1606
+ });
1607
+ const { prehash } = eddsaOpts;
1608
+ const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
1609
+ const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
1610
+ const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
1611
+ const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
1612
+ _abool2(phflag, "phflag");
1613
+ if (ctx.length || phflag)
1614
+ throw new Error("Contexts/pre-hash are not supported");
1615
+ return data;
1616
+ });
1617
+ function modN_LE(hash) {
1618
+ return Fn2.create(bytesToNumberLE(hash));
1619
+ }
1620
+ function getPrivateScalar(key) {
1621
+ const len = lengths.secretKey;
1622
+ key = ensureBytes("private key", key, len);
1623
+ const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
1624
+ const head = adjustScalarBytes2(hashed.slice(0, len));
1625
+ const prefix = hashed.slice(len, 2 * len);
1626
+ const scalar = modN_LE(head);
1627
+ return { head, prefix, scalar };
1628
+ }
1629
+ function getExtendedPublicKey(secretKey) {
1630
+ const { head, prefix, scalar } = getPrivateScalar(secretKey);
1631
+ const point = BASE.multiply(scalar);
1632
+ const pointBytes = point.toBytes();
1633
+ return { head, prefix, scalar, point, pointBytes };
1634
+ }
1635
+ function getPublicKey(secretKey) {
1636
+ return getExtendedPublicKey(secretKey).pointBytes;
1637
+ }
1638
+ function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
1639
+ const msg = concatBytes(...msgs);
1640
+ return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
1641
+ }
1642
+ function sign(msg, secretKey, options = {}) {
1643
+ msg = ensureBytes("message", msg);
1644
+ if (prehash)
1645
+ msg = prehash(msg);
1646
+ const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
1647
+ const r = hashDomainToScalar(options.context, prefix, msg);
1648
+ const R = BASE.multiply(r).toBytes();
1649
+ const k = hashDomainToScalar(options.context, R, pointBytes, msg);
1650
+ const s = Fn2.create(r + k * scalar);
1651
+ if (!Fn2.isValid(s))
1652
+ throw new Error("sign failed: invalid s");
1653
+ const rs = concatBytes(R, Fn2.toBytes(s));
1654
+ return _abytes2(rs, lengths.signature, "result");
1655
+ }
1656
+ const verifyOpts = { zip215: true };
1657
+ function verify(sig, msg, publicKey, options = verifyOpts) {
1658
+ const { context, zip215 } = options;
1659
+ const len = lengths.signature;
1660
+ sig = ensureBytes("signature", sig, len);
1661
+ msg = ensureBytes("message", msg);
1662
+ publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
1663
+ if (zip215 !== void 0)
1664
+ _abool2(zip215, "zip215");
1665
+ if (prehash)
1666
+ msg = prehash(msg);
1667
+ const mid = len / 2;
1668
+ const r = sig.subarray(0, mid);
1669
+ const s = bytesToNumberLE(sig.subarray(mid, len));
1670
+ let A, R, SB;
1671
+ try {
1672
+ A = Point.fromBytes(publicKey, zip215);
1673
+ R = Point.fromBytes(r, zip215);
1674
+ SB = BASE.multiplyUnsafe(s);
1675
+ } catch (error) {
1676
+ return false;
1677
+ }
1678
+ if (!zip215 && A.isSmallOrder())
1679
+ return false;
1680
+ const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
1681
+ const RkA = R.add(A.multiplyUnsafe(k));
1682
+ return RkA.subtract(SB).clearCofactor().is0();
1683
+ }
1684
+ const _size = Fp2.BYTES;
1685
+ const lengths = {
1686
+ secretKey: _size,
1687
+ publicKey: _size,
1688
+ signature: 2 * _size,
1689
+ seed: _size
1690
+ };
1691
+ function randomSecretKey(seed = randomBytes2(lengths.seed)) {
1692
+ return _abytes2(seed, lengths.seed, "seed");
1693
+ }
1694
+ function keygen(seed) {
1695
+ const secretKey = utils.randomSecretKey(seed);
1696
+ return { secretKey, publicKey: getPublicKey(secretKey) };
1697
+ }
1698
+ function isValidSecretKey(key) {
1699
+ return isBytes(key) && key.length === Fn2.BYTES;
1700
+ }
1701
+ function isValidPublicKey(key, zip215) {
1702
+ try {
1703
+ return !!Point.fromBytes(key, zip215);
1704
+ } catch (error) {
1705
+ return false;
1706
+ }
1707
+ }
1708
+ const utils = {
1709
+ getExtendedPublicKey,
1710
+ randomSecretKey,
1711
+ isValidSecretKey,
1712
+ isValidPublicKey,
1713
+ /**
1714
+ * Converts ed public key to x public key. Uses formula:
1715
+ * - ed25519:
1716
+ * - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
1717
+ * - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
1718
+ * - ed448:
1719
+ * - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
1720
+ * - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
1721
+ */
1722
+ toMontgomery(publicKey) {
1723
+ const { y } = Point.fromBytes(publicKey);
1724
+ const size = lengths.publicKey;
1725
+ const is25519 = size === 32;
1726
+ if (!is25519 && size !== 57)
1727
+ throw new Error("only defined for 25519 and 448");
1728
+ const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
1729
+ return Fp2.toBytes(u);
1730
+ },
1731
+ toMontgomerySecret(secretKey) {
1732
+ const size = lengths.secretKey;
1733
+ _abytes2(secretKey, size);
1734
+ const hashed = cHash(secretKey.subarray(0, size));
1735
+ return adjustScalarBytes2(hashed).subarray(0, size);
1736
+ },
1737
+ /** @deprecated */
1738
+ randomPrivateKey: randomSecretKey,
1739
+ /** @deprecated */
1740
+ precompute(windowSize = 8, point = Point.BASE) {
1741
+ return point.precompute(windowSize, false);
1742
+ }
1743
+ };
1744
+ return Object.freeze({
1745
+ keygen,
1746
+ getPublicKey,
1747
+ sign,
1748
+ verify,
1749
+ utils,
1750
+ Point,
1751
+ lengths
1752
+ });
1753
+ }
1754
+ function _eddsa_legacy_opts_to_new(c) {
1755
+ const CURVE = {
1756
+ a: c.a,
1757
+ d: c.d,
1758
+ p: c.Fp.ORDER,
1759
+ n: c.n,
1760
+ h: c.h,
1761
+ Gx: c.Gx,
1762
+ Gy: c.Gy
1763
+ };
1764
+ const Fp2 = c.Fp;
1765
+ const Fn2 = Field(CURVE.n, c.nBitLength, true);
1766
+ const curveOpts = { Fp: Fp2, Fn: Fn2, uvRatio: c.uvRatio };
1767
+ const eddsaOpts = {
1768
+ randomBytes: c.randomBytes,
1769
+ adjustScalarBytes: c.adjustScalarBytes,
1770
+ domain: c.domain,
1771
+ prehash: c.prehash,
1772
+ mapToCurve: c.mapToCurve
1773
+ };
1774
+ return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
1775
+ }
1776
+ function _eddsa_new_output_to_legacy(c, eddsa2) {
1777
+ const Point = eddsa2.Point;
1778
+ const legacy = Object.assign({}, eddsa2, {
1779
+ ExtendedPoint: Point,
1780
+ CURVE: c,
1781
+ nBitLength: Point.Fn.BITS,
1782
+ nByteLength: Point.Fn.BYTES
1783
+ });
1784
+ return legacy;
1785
+ }
1786
+ function twistedEdwards(c) {
1787
+ const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
1788
+ const Point = edwards(CURVE, curveOpts);
1789
+ const EDDSA = eddsa(Point, hash, eddsaOpts);
1790
+ return _eddsa_new_output_to_legacy(c, EDDSA);
1791
+ }
1792
+
1793
+ // node_modules/@noble/curves/esm/abstract/hash-to-curve.js
1794
+ var os2ip = bytesToNumberBE;
1795
+ function i2osp(value, length) {
1796
+ anum(value);
1797
+ anum(length);
1798
+ if (value < 0 || value >= 1 << 8 * length)
1799
+ throw new Error("invalid I2OSP input: " + value);
1800
+ const res = Array.from({ length }).fill(0);
1801
+ for (let i = length - 1; i >= 0; i--) {
1802
+ res[i] = value & 255;
1803
+ value >>>= 8;
1804
+ }
1805
+ return new Uint8Array(res);
1806
+ }
1807
+ function strxor(a, b) {
1808
+ const arr = new Uint8Array(a.length);
1809
+ for (let i = 0; i < a.length; i++) {
1810
+ arr[i] = a[i] ^ b[i];
1811
+ }
1812
+ return arr;
1813
+ }
1814
+ function anum(item) {
1815
+ if (!Number.isSafeInteger(item))
1816
+ throw new Error("number expected");
1817
+ }
1818
+ function normDST(DST) {
1819
+ if (!isBytes(DST) && typeof DST !== "string")
1820
+ throw new Error("DST must be Uint8Array or string");
1821
+ return typeof DST === "string" ? utf8ToBytes(DST) : DST;
1822
+ }
1823
+ function expand_message_xmd(msg, DST, lenInBytes, H) {
1824
+ abytes(msg);
1825
+ anum(lenInBytes);
1826
+ DST = normDST(DST);
1827
+ if (DST.length > 255)
1828
+ DST = H(concatBytes(utf8ToBytes("H2C-OVERSIZE-DST-"), DST));
1829
+ const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
1830
+ const ell = Math.ceil(lenInBytes / b_in_bytes);
1831
+ if (lenInBytes > 65535 || ell > 255)
1832
+ throw new Error("expand_message_xmd: invalid lenInBytes");
1833
+ const DST_prime = concatBytes(DST, i2osp(DST.length, 1));
1834
+ const Z_pad = i2osp(0, r_in_bytes);
1835
+ const l_i_b_str = i2osp(lenInBytes, 2);
1836
+ const b = new Array(ell);
1837
+ const b_0 = H(concatBytes(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
1838
+ b[0] = H(concatBytes(b_0, i2osp(1, 1), DST_prime));
1839
+ for (let i = 1; i <= ell; i++) {
1840
+ const args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
1841
+ b[i] = H(concatBytes(...args));
1842
+ }
1843
+ const pseudo_random_bytes = concatBytes(...b);
1844
+ return pseudo_random_bytes.slice(0, lenInBytes);
1845
+ }
1846
+ function expand_message_xof(msg, DST, lenInBytes, k, H) {
1847
+ abytes(msg);
1848
+ anum(lenInBytes);
1849
+ DST = normDST(DST);
1850
+ if (DST.length > 255) {
1851
+ const dkLen = Math.ceil(2 * k / 8);
1852
+ DST = H.create({ dkLen }).update(utf8ToBytes("H2C-OVERSIZE-DST-")).update(DST).digest();
1853
+ }
1854
+ if (lenInBytes > 65535 || DST.length > 255)
1855
+ throw new Error("expand_message_xof: invalid lenInBytes");
1856
+ return H.create({ dkLen: lenInBytes }).update(msg).update(i2osp(lenInBytes, 2)).update(DST).update(i2osp(DST.length, 1)).digest();
1857
+ }
1858
+ function hash_to_field(msg, count, options) {
1859
+ _validateObject(options, {
1860
+ p: "bigint",
1861
+ m: "number",
1862
+ k: "number",
1863
+ hash: "function"
1864
+ });
1865
+ const { p, k, m, hash, expand, DST } = options;
1866
+ if (!isHash(options.hash))
1867
+ throw new Error("expected valid hash");
1868
+ abytes(msg);
1869
+ anum(count);
1870
+ const log2p = p.toString(2).length;
1871
+ const L = Math.ceil((log2p + k) / 8);
1872
+ const len_in_bytes = count * m * L;
1873
+ let prb;
1874
+ if (expand === "xmd") {
1875
+ prb = expand_message_xmd(msg, DST, len_in_bytes, hash);
1876
+ } else if (expand === "xof") {
1877
+ prb = expand_message_xof(msg, DST, len_in_bytes, k, hash);
1878
+ } else if (expand === "_internal_pass") {
1879
+ prb = msg;
1880
+ } else {
1881
+ throw new Error('expand must be "xmd" or "xof"');
1882
+ }
1883
+ const u = new Array(count);
1884
+ for (let i = 0; i < count; i++) {
1885
+ const e = new Array(m);
1886
+ for (let j = 0; j < m; j++) {
1887
+ const elm_offset = L * (j + i * m);
1888
+ const tv = prb.subarray(elm_offset, elm_offset + L);
1889
+ e[j] = mod(os2ip(tv), p);
1890
+ }
1891
+ u[i] = e;
1892
+ }
1893
+ return u;
1894
+ }
1895
+ var _DST_scalar = utf8ToBytes("HashToScalar-");
1896
+ function createHasher2(Point, mapToCurve, defaults) {
1897
+ if (typeof mapToCurve !== "function")
1898
+ throw new Error("mapToCurve() must be defined");
1899
+ function map(num) {
1900
+ return Point.fromAffine(mapToCurve(num));
1901
+ }
1902
+ function clear(initial) {
1903
+ const P = initial.clearCofactor();
1904
+ if (P.equals(Point.ZERO))
1905
+ return Point.ZERO;
1906
+ P.assertValidity();
1907
+ return P;
1908
+ }
1909
+ return {
1910
+ defaults,
1911
+ hashToCurve(msg, options) {
1912
+ const opts = Object.assign({}, defaults, options);
1913
+ const u = hash_to_field(msg, 2, opts);
1914
+ const u0 = map(u[0]);
1915
+ const u1 = map(u[1]);
1916
+ return clear(u0.add(u1));
1917
+ },
1918
+ encodeToCurve(msg, options) {
1919
+ const optsDst = defaults.encodeDST ? { DST: defaults.encodeDST } : {};
1920
+ const opts = Object.assign({}, defaults, optsDst, options);
1921
+ const u = hash_to_field(msg, 1, opts);
1922
+ const u0 = map(u[0]);
1923
+ return clear(u0);
1924
+ },
1925
+ /** See {@link H2CHasher} */
1926
+ mapToCurve(scalars) {
1927
+ if (!Array.isArray(scalars))
1928
+ throw new Error("expected array of bigints");
1929
+ for (const i of scalars)
1930
+ if (typeof i !== "bigint")
1931
+ throw new Error("expected array of bigints");
1932
+ return clear(map(scalars));
1933
+ },
1934
+ // hash_to_scalar can produce 0: https://www.rfc-editor.org/errata/eid8393
1935
+ // RFC 9380, draft-irtf-cfrg-bbs-signatures-08
1936
+ hashToScalar(msg, options) {
1937
+ const N = Point.Fn.ORDER;
1938
+ const opts = Object.assign({}, defaults, { p: N, m: 1, DST: _DST_scalar }, options);
1939
+ return hash_to_field(msg, 1, opts)[0][0];
1940
+ }
1941
+ };
1942
+ }
1943
+
1944
+ // node_modules/@noble/curves/esm/abstract/montgomery.js
1945
+ var _0n5 = BigInt(0);
1946
+ var _1n5 = BigInt(1);
1947
+ var _2n3 = BigInt(2);
1948
+ function validateOpts(curve) {
1949
+ _validateObject(curve, {
1950
+ adjustScalarBytes: "function",
1951
+ powPminus2: "function"
1952
+ });
1953
+ return Object.freeze({ ...curve });
1954
+ }
1955
+ function montgomery(curveDef) {
1956
+ const CURVE = validateOpts(curveDef);
1957
+ const { P, type, adjustScalarBytes: adjustScalarBytes2, powPminus2, randomBytes: rand } = CURVE;
1958
+ const is25519 = type === "x25519";
1959
+ if (!is25519 && type !== "x448")
1960
+ throw new Error("invalid type");
1961
+ const randomBytes_ = rand || randomBytes;
1962
+ const montgomeryBits = is25519 ? 255 : 448;
1963
+ const fieldLen = is25519 ? 32 : 56;
1964
+ const Gu = is25519 ? BigInt(9) : BigInt(5);
1965
+ const a24 = is25519 ? BigInt(121665) : BigInt(39081);
1966
+ const minScalar = is25519 ? _2n3 ** BigInt(254) : _2n3 ** BigInt(447);
1967
+ const maxAdded = is25519 ? BigInt(8) * _2n3 ** BigInt(251) - _1n5 : BigInt(4) * _2n3 ** BigInt(445) - _1n5;
1968
+ const maxScalar = minScalar + maxAdded + _1n5;
1969
+ const modP = (n) => mod(n, P);
1970
+ const GuBytes = encodeU(Gu);
1971
+ function encodeU(u) {
1972
+ return numberToBytesLE(modP(u), fieldLen);
1973
+ }
1974
+ function decodeU(u) {
1975
+ const _u = ensureBytes("u coordinate", u, fieldLen);
1976
+ if (is25519)
1977
+ _u[31] &= 127;
1978
+ return modP(bytesToNumberLE(_u));
1979
+ }
1980
+ function decodeScalar(scalar) {
1981
+ return bytesToNumberLE(adjustScalarBytes2(ensureBytes("scalar", scalar, fieldLen)));
1982
+ }
1983
+ function scalarMult(scalar, u) {
1984
+ const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));
1985
+ if (pu === _0n5)
1986
+ throw new Error("invalid private or public key received");
1987
+ return encodeU(pu);
1988
+ }
1989
+ function scalarMultBase(scalar) {
1990
+ return scalarMult(scalar, GuBytes);
1991
+ }
1992
+ function cswap(swap, x_2, x_3) {
1993
+ const dummy = modP(swap * (x_2 - x_3));
1994
+ x_2 = modP(x_2 - dummy);
1995
+ x_3 = modP(x_3 + dummy);
1996
+ return { x_2, x_3 };
1997
+ }
1998
+ function montgomeryLadder(u, scalar) {
1999
+ aInRange("u", u, _0n5, P);
2000
+ aInRange("scalar", scalar, minScalar, maxScalar);
2001
+ const k = scalar;
2002
+ const x_1 = u;
2003
+ let x_2 = _1n5;
2004
+ let z_2 = _0n5;
2005
+ let x_3 = u;
2006
+ let z_3 = _1n5;
2007
+ let swap = _0n5;
2008
+ for (let t = BigInt(montgomeryBits - 1); t >= _0n5; t--) {
2009
+ const k_t = k >> t & _1n5;
2010
+ swap ^= k_t;
2011
+ ({ x_2, x_3 } = cswap(swap, x_2, x_3));
2012
+ ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
2013
+ swap = k_t;
2014
+ const A = x_2 + z_2;
2015
+ const AA = modP(A * A);
2016
+ const B = x_2 - z_2;
2017
+ const BB = modP(B * B);
2018
+ const E = AA - BB;
2019
+ const C = x_3 + z_3;
2020
+ const D = x_3 - z_3;
2021
+ const DA = modP(D * A);
2022
+ const CB = modP(C * B);
2023
+ const dacb = DA + CB;
2024
+ const da_cb = DA - CB;
2025
+ x_3 = modP(dacb * dacb);
2026
+ z_3 = modP(x_1 * modP(da_cb * da_cb));
2027
+ x_2 = modP(AA * BB);
2028
+ z_2 = modP(E * (AA + modP(a24 * E)));
2029
+ }
2030
+ ({ x_2, x_3 } = cswap(swap, x_2, x_3));
2031
+ ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
2032
+ const z2 = powPminus2(z_2);
2033
+ return modP(x_2 * z2);
2034
+ }
2035
+ const lengths = {
2036
+ secretKey: fieldLen,
2037
+ publicKey: fieldLen,
2038
+ seed: fieldLen
2039
+ };
2040
+ const randomSecretKey = (seed = randomBytes_(fieldLen)) => {
2041
+ abytes(seed, lengths.seed);
2042
+ return seed;
2043
+ };
2044
+ function keygen(seed) {
2045
+ const secretKey = randomSecretKey(seed);
2046
+ return { secretKey, publicKey: scalarMultBase(secretKey) };
2047
+ }
2048
+ const utils = {
2049
+ randomSecretKey,
2050
+ randomPrivateKey: randomSecretKey
2051
+ };
2052
+ return {
2053
+ keygen,
2054
+ getSharedSecret: (secretKey, publicKey) => scalarMult(secretKey, publicKey),
2055
+ getPublicKey: (secretKey) => scalarMultBase(secretKey),
2056
+ scalarMult,
2057
+ scalarMultBase,
2058
+ utils,
2059
+ GuBytes: GuBytes.slice(),
2060
+ lengths
2061
+ };
2062
+ }
2063
+
2064
+ // node_modules/@noble/curves/esm/ed25519.js
2065
+ var _0n6 = /* @__PURE__ */ BigInt(0);
2066
+ var _1n6 = BigInt(1);
2067
+ var _2n4 = BigInt(2);
2068
+ var _3n2 = BigInt(3);
2069
+ var _5n2 = BigInt(5);
2070
+ var _8n3 = BigInt(8);
2071
+ var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
2072
+ var ed25519_CURVE = /* @__PURE__ */ (() => ({
2073
+ p: ed25519_CURVE_p,
2074
+ n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
2075
+ h: _8n3,
2076
+ a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
2077
+ d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
2078
+ Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
2079
+ Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
2080
+ }))();
2081
+ function ed25519_pow_2_252_3(x) {
2082
+ const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
2083
+ const P = ed25519_CURVE_p;
2084
+ const x2 = x * x % P;
2085
+ const b2 = x2 * x % P;
2086
+ const b4 = pow2(b2, _2n4, P) * b2 % P;
2087
+ const b5 = pow2(b4, _1n6, P) * x % P;
2088
+ const b10 = pow2(b5, _5n2, P) * b5 % P;
2089
+ const b20 = pow2(b10, _10n, P) * b10 % P;
2090
+ const b40 = pow2(b20, _20n, P) * b20 % P;
2091
+ const b80 = pow2(b40, _40n, P) * b40 % P;
2092
+ const b160 = pow2(b80, _80n, P) * b80 % P;
2093
+ const b240 = pow2(b160, _80n, P) * b80 % P;
2094
+ const b250 = pow2(b240, _10n, P) * b10 % P;
2095
+ const pow_p_5_8 = pow2(b250, _2n4, P) * x % P;
2096
+ return { pow_p_5_8, b2 };
2097
+ }
2098
+ function adjustScalarBytes(bytes) {
2099
+ bytes[0] &= 248;
2100
+ bytes[31] &= 127;
2101
+ bytes[31] |= 64;
2102
+ return bytes;
2103
+ }
2104
+ var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
2105
+ function uvRatio(u, v) {
2106
+ const P = ed25519_CURVE_p;
2107
+ const v3 = mod(v * v * v, P);
2108
+ const v7 = mod(v3 * v3 * v, P);
2109
+ const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
2110
+ let x = mod(u * v3 * pow, P);
2111
+ const vx2 = mod(v * x * x, P);
2112
+ const root1 = x;
2113
+ const root2 = mod(x * ED25519_SQRT_M1, P);
2114
+ const useRoot1 = vx2 === u;
2115
+ const useRoot2 = vx2 === mod(-u, P);
2116
+ const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
2117
+ if (useRoot1)
2118
+ x = root1;
2119
+ if (useRoot2 || noRoot)
2120
+ x = root2;
2121
+ if (isNegativeLE(x, P))
2122
+ x = mod(-x, P);
2123
+ return { isValid: useRoot1 || useRoot2, value: x };
2124
+ }
2125
+ var Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
2126
+ var Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
2127
+ var ed25519Defaults = /* @__PURE__ */ (() => ({
2128
+ ...ed25519_CURVE,
2129
+ Fp,
2130
+ hash: sha512,
2131
+ adjustScalarBytes,
2132
+ // dom2
2133
+ // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
2134
+ // Constant-time, u/√v
2135
+ uvRatio
2136
+ }))();
2137
+ var ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
2138
+ function ed25519_domain(data, ctx, phflag) {
2139
+ if (ctx.length > 255)
2140
+ throw new Error("Context is too big");
2141
+ return concatBytes(utf8ToBytes("SigEd25519 no Ed25519 collisions"), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
2142
+ }
2143
+ var ed25519ctx = /* @__PURE__ */ (() => twistedEdwards({
2144
+ ...ed25519Defaults,
2145
+ domain: ed25519_domain
2146
+ }))();
2147
+ var ed25519ph = /* @__PURE__ */ (() => twistedEdwards(Object.assign({}, ed25519Defaults, {
2148
+ domain: ed25519_domain,
2149
+ prehash: sha512
2150
+ })))();
2151
+ var x25519 = /* @__PURE__ */ (() => {
2152
+ const P = Fp.ORDER;
2153
+ return montgomery({
2154
+ P,
2155
+ type: "x25519",
2156
+ powPminus2: (x) => {
2157
+ const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
2158
+ return mod(pow2(pow_p_5_8, _3n2, P) * b2, P);
2159
+ },
2160
+ adjustScalarBytes
2161
+ });
2162
+ })();
2163
+ var ELL2_C1 = /* @__PURE__ */ (() => (ed25519_CURVE_p + _3n2) / _8n3)();
2164
+ var ELL2_C2 = /* @__PURE__ */ (() => Fp.pow(_2n4, ELL2_C1))();
2165
+ var ELL2_C3 = /* @__PURE__ */ (() => Fp.sqrt(Fp.neg(Fp.ONE)))();
2166
+ function map_to_curve_elligator2_curve25519(u) {
2167
+ const ELL2_C4 = (ed25519_CURVE_p - _5n2) / _8n3;
2168
+ const ELL2_J = BigInt(486662);
2169
+ let tv1 = Fp.sqr(u);
2170
+ tv1 = Fp.mul(tv1, _2n4);
2171
+ let xd = Fp.add(tv1, Fp.ONE);
2172
+ let x1n = Fp.neg(ELL2_J);
2173
+ let tv2 = Fp.sqr(xd);
2174
+ let gxd = Fp.mul(tv2, xd);
2175
+ let gx1 = Fp.mul(tv1, ELL2_J);
2176
+ gx1 = Fp.mul(gx1, x1n);
2177
+ gx1 = Fp.add(gx1, tv2);
2178
+ gx1 = Fp.mul(gx1, x1n);
2179
+ let tv3 = Fp.sqr(gxd);
2180
+ tv2 = Fp.sqr(tv3);
2181
+ tv3 = Fp.mul(tv3, gxd);
2182
+ tv3 = Fp.mul(tv3, gx1);
2183
+ tv2 = Fp.mul(tv2, tv3);
2184
+ let y11 = Fp.pow(tv2, ELL2_C4);
2185
+ y11 = Fp.mul(y11, tv3);
2186
+ let y12 = Fp.mul(y11, ELL2_C3);
2187
+ tv2 = Fp.sqr(y11);
2188
+ tv2 = Fp.mul(tv2, gxd);
2189
+ let e1 = Fp.eql(tv2, gx1);
2190
+ let y1 = Fp.cmov(y12, y11, e1);
2191
+ let x2n = Fp.mul(x1n, tv1);
2192
+ let y21 = Fp.mul(y11, u);
2193
+ y21 = Fp.mul(y21, ELL2_C2);
2194
+ let y22 = Fp.mul(y21, ELL2_C3);
2195
+ let gx2 = Fp.mul(gx1, tv1);
2196
+ tv2 = Fp.sqr(y21);
2197
+ tv2 = Fp.mul(tv2, gxd);
2198
+ let e2 = Fp.eql(tv2, gx2);
2199
+ let y2 = Fp.cmov(y22, y21, e2);
2200
+ tv2 = Fp.sqr(y1);
2201
+ tv2 = Fp.mul(tv2, gxd);
2202
+ let e3 = Fp.eql(tv2, gx1);
2203
+ let xn = Fp.cmov(x2n, x1n, e3);
2204
+ let y = Fp.cmov(y2, y1, e3);
2205
+ let e4 = Fp.isOdd(y);
2206
+ y = Fp.cmov(y, Fp.neg(y), e3 !== e4);
2207
+ return { xMn: xn, xMd: xd, yMn: y, yMd: _1n6 };
2208
+ }
2209
+ var ELL2_C1_EDWARDS = /* @__PURE__ */ (() => FpSqrtEven(Fp, Fp.neg(BigInt(486664))))();
2210
+ function map_to_curve_elligator2_edwards25519(u) {
2211
+ const { xMn, xMd, yMn, yMd } = map_to_curve_elligator2_curve25519(u);
2212
+ let xn = Fp.mul(xMn, yMd);
2213
+ xn = Fp.mul(xn, ELL2_C1_EDWARDS);
2214
+ let xd = Fp.mul(xMd, yMn);
2215
+ let yn = Fp.sub(xMn, xMd);
2216
+ let yd = Fp.add(xMn, xMd);
2217
+ let tv1 = Fp.mul(xd, yd);
2218
+ let e = Fp.eql(tv1, Fp.ZERO);
2219
+ xn = Fp.cmov(xn, Fp.ZERO, e);
2220
+ xd = Fp.cmov(xd, Fp.ONE, e);
2221
+ yn = Fp.cmov(yn, Fp.ONE, e);
2222
+ yd = Fp.cmov(yd, Fp.ONE, e);
2223
+ const [xd_inv, yd_inv] = FpInvertBatch(Fp, [xd, yd], true);
2224
+ return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) };
2225
+ }
2226
+ var ed25519_hasher = /* @__PURE__ */ (() => createHasher2(ed25519.Point, (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]), {
2227
+ DST: "edwards25519_XMD:SHA-512_ELL2_RO_",
2228
+ encodeDST: "edwards25519_XMD:SHA-512_ELL2_NU_",
2229
+ p: ed25519_CURVE_p,
2230
+ m: 1,
2231
+ k: 128,
2232
+ expand: "xmd",
2233
+ hash: sha512
2234
+ }))();
2235
+ var SQRT_M1 = ED25519_SQRT_M1;
2236
+ var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
2237
+ var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
2238
+ var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
2239
+ var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
2240
+ var invertSqrt = (number) => uvRatio(_1n6, number);
2241
+ var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
2242
+ var bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
2243
+ function calcElligatorRistrettoMap(r0) {
2244
+ const { d } = ed25519_CURVE;
2245
+ const P = ed25519_CURVE_p;
2246
+ const mod2 = (n) => Fp.create(n);
2247
+ const r = mod2(SQRT_M1 * r0 * r0);
2248
+ const Ns = mod2((r + _1n6) * ONE_MINUS_D_SQ);
2249
+ let c = BigInt(-1);
2250
+ const D = mod2((c - d * r) * mod2(r + d));
2251
+ let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
2252
+ let s_ = mod2(s * r0);
2253
+ if (!isNegativeLE(s_, P))
2254
+ s_ = mod2(-s_);
2255
+ if (!Ns_D_is_sq)
2256
+ s = s_;
2257
+ if (!Ns_D_is_sq)
2258
+ c = r;
2259
+ const Nt = mod2(c * (r - _1n6) * D_MINUS_ONE_SQ - D);
2260
+ const s2 = s * s;
2261
+ const W0 = mod2((s + s) * D);
2262
+ const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
2263
+ const W2 = mod2(_1n6 - s2);
2264
+ const W3 = mod2(_1n6 + s2);
2265
+ return new ed25519.Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
2266
+ }
2267
+ function ristretto255_map(bytes) {
2268
+ abytes(bytes, 64);
2269
+ const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
2270
+ const R1 = calcElligatorRistrettoMap(r1);
2271
+ const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
2272
+ const R2 = calcElligatorRistrettoMap(r2);
2273
+ return new _RistrettoPoint(R1.add(R2));
2274
+ }
2275
+ var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
2276
+ constructor(ep) {
2277
+ super(ep);
2278
+ }
2279
+ static fromAffine(ap) {
2280
+ return new __RistrettoPoint(ed25519.Point.fromAffine(ap));
2281
+ }
2282
+ assertSame(other) {
2283
+ if (!(other instanceof __RistrettoPoint))
2284
+ throw new Error("RistrettoPoint expected");
2285
+ }
2286
+ init(ep) {
2287
+ return new __RistrettoPoint(ep);
2288
+ }
2289
+ /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
2290
+ static hashToCurve(hex) {
2291
+ return ristretto255_map(ensureBytes("ristrettoHash", hex, 64));
2292
+ }
2293
+ static fromBytes(bytes) {
2294
+ abytes(bytes, 32);
2295
+ const { a, d } = ed25519_CURVE;
2296
+ const P = ed25519_CURVE_p;
2297
+ const mod2 = (n) => Fp.create(n);
2298
+ const s = bytes255ToNumberLE(bytes);
2299
+ if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
2300
+ throw new Error("invalid ristretto255 encoding 1");
2301
+ const s2 = mod2(s * s);
2302
+ const u1 = mod2(_1n6 + a * s2);
2303
+ const u2 = mod2(_1n6 - a * s2);
2304
+ const u1_2 = mod2(u1 * u1);
2305
+ const u2_2 = mod2(u2 * u2);
2306
+ const v = mod2(a * d * u1_2 - u2_2);
2307
+ const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
2308
+ const Dx = mod2(I * u2);
2309
+ const Dy = mod2(I * Dx * v);
2310
+ let x = mod2((s + s) * Dx);
2311
+ if (isNegativeLE(x, P))
2312
+ x = mod2(-x);
2313
+ const y = mod2(u1 * Dy);
2314
+ const t = mod2(x * y);
2315
+ if (!isValid || isNegativeLE(t, P) || y === _0n6)
2316
+ throw new Error("invalid ristretto255 encoding 2");
2317
+ return new __RistrettoPoint(new ed25519.Point(x, y, _1n6, t));
2318
+ }
2319
+ /**
2320
+ * Converts ristretto-encoded string to ristretto point.
2321
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
2322
+ * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
2323
+ */
2324
+ static fromHex(hex) {
2325
+ return __RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex, 32));
2326
+ }
2327
+ static msm(points, scalars) {
2328
+ return pippenger(__RistrettoPoint, ed25519.Point.Fn, points, scalars);
2329
+ }
2330
+ /**
2331
+ * Encodes ristretto point to Uint8Array.
2332
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
2333
+ */
2334
+ toBytes() {
2335
+ let { X, Y, Z, T } = this.ep;
2336
+ const P = ed25519_CURVE_p;
2337
+ const mod2 = (n) => Fp.create(n);
2338
+ const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
2339
+ const u2 = mod2(X * Y);
2340
+ const u2sq = mod2(u2 * u2);
2341
+ const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
2342
+ const D1 = mod2(invsqrt * u1);
2343
+ const D2 = mod2(invsqrt * u2);
2344
+ const zInv = mod2(D1 * D2 * T);
2345
+ let D;
2346
+ if (isNegativeLE(T * zInv, P)) {
2347
+ let _x = mod2(Y * SQRT_M1);
2348
+ let _y = mod2(X * SQRT_M1);
2349
+ X = _x;
2350
+ Y = _y;
2351
+ D = mod2(D1 * INVSQRT_A_MINUS_D);
2352
+ } else {
2353
+ D = D2;
2354
+ }
2355
+ if (isNegativeLE(X * zInv, P))
2356
+ Y = mod2(-Y);
2357
+ let s = mod2((Z - Y) * D);
2358
+ if (isNegativeLE(s, P))
2359
+ s = mod2(-s);
2360
+ return Fp.toBytes(s);
2361
+ }
2362
+ /**
2363
+ * Compares two Ristretto points.
2364
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
2365
+ */
2366
+ equals(other) {
2367
+ this.assertSame(other);
2368
+ const { X: X1, Y: Y1 } = this.ep;
2369
+ const { X: X2, Y: Y2 } = other.ep;
2370
+ const mod2 = (n) => Fp.create(n);
2371
+ const one = mod2(X1 * Y2) === mod2(Y1 * X2);
2372
+ const two = mod2(Y1 * Y2) === mod2(X1 * X2);
2373
+ return one || two;
2374
+ }
2375
+ is0() {
2376
+ return this.equals(__RistrettoPoint.ZERO);
2377
+ }
2378
+ };
2379
+ _RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
2380
+ _RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
2381
+ _RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
2382
+ _RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
2383
+ var ristretto255 = { Point: _RistrettoPoint };
2384
+ var ristretto255_hasher = {
2385
+ hashToCurve(msg, options) {
2386
+ const DST = options?.DST || "ristretto255_XMD:SHA-512_R255MAP_RO_";
2387
+ const xmd = expand_message_xmd(msg, DST, 64, sha512);
2388
+ return ristretto255_map(xmd);
2389
+ },
2390
+ hashToScalar(msg, options = { DST: _DST_scalar }) {
2391
+ const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
2392
+ return Fn.create(bytesToNumberLE(xmd));
2393
+ }
2394
+ };
2395
+ var ED25519_TORSION_SUBGROUP = [
2396
+ "0100000000000000000000000000000000000000000000000000000000000000",
2397
+ "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a",
2398
+ "0000000000000000000000000000000000000000000000000000000000000080",
2399
+ "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05",
2400
+ "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
2401
+ "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85",
2402
+ "0000000000000000000000000000000000000000000000000000000000000000",
2403
+ "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa"
2404
+ ];
2405
+ function edwardsToMontgomeryPub(edwardsPub) {
2406
+ return ed25519.utils.toMontgomery(ensureBytes("pub", edwardsPub));
2407
+ }
2408
+ var edwardsToMontgomery = edwardsToMontgomeryPub;
2409
+ function edwardsToMontgomeryPriv(edwardsPriv) {
2410
+ return ed25519.utils.toMontgomerySecret(ensureBytes("pub", edwardsPriv));
2411
+ }
2412
+ var RistrettoPoint = _RistrettoPoint;
2413
+ var hashToCurve = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
2414
+ var encodeToCurve = /* @__PURE__ */ (() => ed25519_hasher.encodeToCurve)();
2415
+ var hashToRistretto255 = /* @__PURE__ */ (() => ristretto255_hasher.hashToCurve)();
2416
+ var hash_to_ristretto255 = /* @__PURE__ */ (() => ristretto255_hasher.hashToCurve)();
2417
+
2418
+ export {
2419
+ sha256,
2420
+ ed25519,
2421
+ ed25519ctx,
2422
+ ed25519ph,
2423
+ x25519,
2424
+ ed25519_hasher,
2425
+ ristretto255,
2426
+ ristretto255_hasher,
2427
+ ED25519_TORSION_SUBGROUP,
2428
+ edwardsToMontgomeryPub,
2429
+ edwardsToMontgomery,
2430
+ edwardsToMontgomeryPriv,
2431
+ RistrettoPoint,
2432
+ hashToCurve,
2433
+ encodeToCurve,
2434
+ hashToRistretto255,
2435
+ hash_to_ristretto255
2436
+ };
2437
+ /*! Bundled license information:
2438
+
2439
+ @noble/curves/esm/utils.js:
2440
+ @noble/curves/esm/abstract/modular.js:
2441
+ @noble/curves/esm/abstract/curve.js:
2442
+ @noble/curves/esm/abstract/edwards.js:
2443
+ @noble/curves/esm/abstract/montgomery.js:
2444
+ @noble/curves/esm/ed25519.js:
2445
+ (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2446
+ */