@solana/webcrypto-ed25519-polyfill 2.0.0-experimental.e36f58d → 2.0.0-experimental.e4483d3

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.
@@ -1,1505 +1,420 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- // ../../node_modules/.pnpm/@noble+hashes@1.3.1/node_modules/@noble/hashes/esm/_assert.js
5
- function number(n) {
6
- if (!Number.isSafeInteger(n) || n < 0)
7
- throw new Error(`Wrong positive integer: ${n}`);
8
- }
9
- function bool(b) {
10
- if (typeof b !== "boolean")
11
- throw new Error(`Expected boolean, not ${b}`);
12
- }
13
- function bytes(b, ...lengths) {
14
- if (!(b instanceof Uint8Array))
15
- throw new Error("Expected Uint8Array");
16
- if (lengths.length > 0 && !lengths.includes(b.length))
17
- throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
18
- }
19
- function hash(hash2) {
20
- if (typeof hash2 !== "function" || typeof hash2.create !== "function")
21
- throw new Error("Hash should be wrapped by utils.wrapConstructor");
22
- number(hash2.outputLen);
23
- number(hash2.blockLen);
24
- }
25
- function exists(instance, checkFinished = true) {
26
- if (instance.destroyed)
27
- throw new Error("Hash instance has been destroyed");
28
- if (checkFinished && instance.finished)
29
- throw new Error("Hash#digest() has already been called");
30
- }
31
- function output(out, instance) {
32
- bytes(out);
33
- const min = instance.outputLen;
34
- if (out.length < min) {
35
- throw new Error(`digestInto() expects output buffer of length at least ${min}`);
36
- }
37
- }
38
- var assert = {
39
- number,
40
- bool,
41
- bytes,
42
- hash,
43
- exists,
44
- output
4
+ // ../../node_modules/.pnpm/@noble+ed25519@2.0.0/node_modules/@noble/ed25519/index.js
5
+ var P = 2n ** 255n - 19n;
6
+ var N = 2n ** 252n + 27742317777372353535851937790883648493n;
7
+ var Gx = 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an;
8
+ var Gy = 0x6666666666666666666666666666666666666666666666666666666666666658n;
9
+ var CURVE = {
10
+ a: -1n,
11
+ d: 37095705934669439343138083508754565189542113879843219016388785533085940283555n,
12
+ p: P,
13
+ n: N,
14
+ h: 8,
15
+ Gx,
16
+ Gy
17
+ // field prime, curve (group) order, cofactor
45
18
  };
46
- var assert_default = assert;
47
-
48
- // ../../node_modules/.pnpm/@noble+hashes@1.3.1/node_modules/@noble/hashes/esm/crypto.js
49
- var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
50
-
51
- // ../../node_modules/.pnpm/@noble+hashes@1.3.1/node_modules/@noble/hashes/esm/utils.js
52
- var u8a = (a) => a instanceof Uint8Array;
53
- var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
54
- var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
55
- if (!isLE)
56
- throw new Error("Non little-endian hardware is not supported");
57
- Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
58
- function utf8ToBytes(str) {
59
- if (typeof str !== "string")
60
- throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
61
- return new Uint8Array(new TextEncoder().encode(str));
62
- }
63
- function toBytes(data) {
64
- if (typeof data === "string")
65
- data = utf8ToBytes(data);
66
- if (!u8a(data))
67
- throw new Error(`expected Uint8Array, got ${typeof data}`);
68
- return data;
69
- }
70
- function concatBytes(...arrays) {
71
- const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
72
- let pad = 0;
73
- arrays.forEach((a) => {
74
- if (!u8a(a))
75
- throw new Error("Uint8Array expected");
76
- r.set(a, pad);
77
- pad += a.length;
78
- });
79
- return r;
80
- }
81
- var Hash = class {
82
- // Safe version that clones internal state
83
- clone() {
84
- return this._cloneInto();
85
- }
19
+ var err = (m = "") => {
20
+ throw new Error(m);
86
21
  };
87
- function wrapConstructor(hashCons) {
88
- const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
89
- const tmp = hashCons();
90
- hashC.outputLen = tmp.outputLen;
91
- hashC.blockLen = tmp.blockLen;
92
- hashC.create = () => hashCons();
93
- return hashC;
94
- }
95
- function randomBytes(bytesLength = 32) {
96
- if (crypto && typeof crypto.getRandomValues === "function") {
97
- return crypto.getRandomValues(new Uint8Array(bytesLength));
98
- }
99
- throw new Error("crypto.getRandomValues must be defined");
100
- }
101
-
102
- // ../../node_modules/.pnpm/@noble+hashes@1.3.1/node_modules/@noble/hashes/esm/_sha2.js
103
- function setBigUint64(view, byteOffset, value, isLE2) {
104
- if (typeof view.setBigUint64 === "function")
105
- return view.setBigUint64(byteOffset, value, isLE2);
106
- const _32n2 = BigInt(32);
107
- const _u32_max = BigInt(4294967295);
108
- const wh = Number(value >> _32n2 & _u32_max);
109
- const wl = Number(value & _u32_max);
110
- const h = isLE2 ? 4 : 0;
111
- const l = isLE2 ? 0 : 4;
112
- view.setUint32(byteOffset + h, wh, isLE2);
113
- view.setUint32(byteOffset + l, wl, isLE2);
114
- }
115
- var SHA2 = class extends Hash {
116
- constructor(blockLen, outputLen, padOffset, isLE2) {
117
- super();
118
- this.blockLen = blockLen;
119
- this.outputLen = outputLen;
120
- this.padOffset = padOffset;
121
- this.isLE = isLE2;
122
- this.finished = false;
123
- this.length = 0;
124
- this.pos = 0;
125
- this.destroyed = false;
126
- this.buffer = new Uint8Array(blockLen);
127
- this.view = createView(this.buffer);
128
- }
129
- update(data) {
130
- assert_default.exists(this);
131
- const { view, buffer, blockLen } = this;
132
- data = toBytes(data);
133
- const len = data.length;
134
- for (let pos = 0; pos < len; ) {
135
- const take = Math.min(blockLen - this.pos, len - pos);
136
- if (take === blockLen) {
137
- const dataView = createView(data);
138
- for (; blockLen <= len - pos; pos += blockLen)
139
- this.process(dataView, pos);
140
- continue;
141
- }
142
- buffer.set(data.subarray(pos, pos + take), this.pos);
143
- this.pos += take;
144
- pos += take;
145
- if (this.pos === blockLen) {
146
- this.process(view, 0);
147
- this.pos = 0;
148
- }
149
- }
150
- this.length += data.length;
151
- this.roundClean();
152
- return this;
153
- }
154
- digestInto(out) {
155
- assert_default.exists(this);
156
- assert_default.output(out, this);
157
- this.finished = true;
158
- const { buffer, view, blockLen, isLE: isLE2 } = this;
159
- let { pos } = this;
160
- buffer[pos++] = 128;
161
- this.buffer.subarray(pos).fill(0);
162
- if (this.padOffset > blockLen - pos) {
163
- this.process(view, 0);
164
- pos = 0;
165
- }
166
- for (let i = pos; i < blockLen; i++)
167
- buffer[i] = 0;
168
- setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
169
- this.process(view, 0);
170
- const oview = createView(out);
171
- const len = this.outputLen;
172
- if (len % 4)
173
- throw new Error("_sha2: outputLen should be aligned to 32bit");
174
- const outLen = len / 4;
175
- const state = this.get();
176
- if (outLen > state.length)
177
- throw new Error("_sha2: outputLen bigger than state");
178
- for (let i = 0; i < outLen; i++)
179
- oview.setUint32(4 * i, state[i], isLE2);
180
- }
181
- digest() {
182
- const { buffer, outputLen } = this;
183
- this.digestInto(buffer);
184
- const res = buffer.slice(0, outputLen);
185
- this.destroy();
186
- return res;
187
- }
188
- _cloneInto(to) {
189
- to || (to = new this.constructor());
190
- to.set(...this.get());
191
- const { blockLen, buffer, length, finished, destroyed, pos } = this;
192
- to.length = length;
193
- to.pos = pos;
194
- to.finished = finished;
195
- to.destroyed = destroyed;
196
- if (length % blockLen)
197
- to.buffer.set(buffer);
198
- return to;
199
- }
200
- };
201
-
202
- // ../../node_modules/.pnpm/@noble+hashes@1.3.1/node_modules/@noble/hashes/esm/_u64.js
203
- var U32_MASK64 = BigInt(2 ** 32 - 1);
204
- var _32n = BigInt(32);
205
- function fromBig(n, le = false) {
206
- if (le)
207
- return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
208
- return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
209
- }
210
- function split(lst, le = false) {
211
- let Ah = new Uint32Array(lst.length);
212
- let Al = new Uint32Array(lst.length);
213
- for (let i = 0; i < lst.length; i++) {
214
- const { h, l } = fromBig(lst[i], le);
215
- [Ah[i], Al[i]] = [h, l];
216
- }
217
- return [Ah, Al];
218
- }
219
- var toBig = (h, l) => BigInt(h >>> 0) << _32n | BigInt(l >>> 0);
220
- var shrSH = (h, l, s) => h >>> s;
221
- var shrSL = (h, l, s) => h << 32 - s | l >>> s;
222
- var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
223
- var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
224
- var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
225
- var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
226
- var rotr32H = (h, l) => l;
227
- var rotr32L = (h, l) => h;
228
- var rotlSH = (h, l, s) => h << s | l >>> 32 - s;
229
- var rotlSL = (h, l, s) => l << s | h >>> 32 - s;
230
- var rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
231
- var rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
232
- function add(Ah, Al, Bh, Bl) {
233
- const l = (Al >>> 0) + (Bl >>> 0);
234
- return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
235
- }
236
- var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
237
- var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
238
- var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
239
- var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
240
- var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
241
- var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
242
- var u64 = {
243
- fromBig,
244
- split,
245
- toBig,
246
- shrSH,
247
- shrSL,
248
- rotrSH,
249
- rotrSL,
250
- rotrBH,
251
- rotrBL,
252
- rotr32H,
253
- rotr32L,
254
- rotlSH,
255
- rotlSL,
256
- rotlBH,
257
- rotlBL,
258
- add,
259
- add3L,
260
- add3H,
261
- add4L,
262
- add4H,
263
- add5H,
264
- add5L
265
- };
266
- var u64_default = u64;
267
-
268
- // ../../node_modules/.pnpm/@noble+hashes@1.3.1/node_modules/@noble/hashes/esm/sha512.js
269
- var [SHA512_Kh, SHA512_Kl] = u64_default.split([
270
- "0x428a2f98d728ae22",
271
- "0x7137449123ef65cd",
272
- "0xb5c0fbcfec4d3b2f",
273
- "0xe9b5dba58189dbbc",
274
- "0x3956c25bf348b538",
275
- "0x59f111f1b605d019",
276
- "0x923f82a4af194f9b",
277
- "0xab1c5ed5da6d8118",
278
- "0xd807aa98a3030242",
279
- "0x12835b0145706fbe",
280
- "0x243185be4ee4b28c",
281
- "0x550c7dc3d5ffb4e2",
282
- "0x72be5d74f27b896f",
283
- "0x80deb1fe3b1696b1",
284
- "0x9bdc06a725c71235",
285
- "0xc19bf174cf692694",
286
- "0xe49b69c19ef14ad2",
287
- "0xefbe4786384f25e3",
288
- "0x0fc19dc68b8cd5b5",
289
- "0x240ca1cc77ac9c65",
290
- "0x2de92c6f592b0275",
291
- "0x4a7484aa6ea6e483",
292
- "0x5cb0a9dcbd41fbd4",
293
- "0x76f988da831153b5",
294
- "0x983e5152ee66dfab",
295
- "0xa831c66d2db43210",
296
- "0xb00327c898fb213f",
297
- "0xbf597fc7beef0ee4",
298
- "0xc6e00bf33da88fc2",
299
- "0xd5a79147930aa725",
300
- "0x06ca6351e003826f",
301
- "0x142929670a0e6e70",
302
- "0x27b70a8546d22ffc",
303
- "0x2e1b21385c26c926",
304
- "0x4d2c6dfc5ac42aed",
305
- "0x53380d139d95b3df",
306
- "0x650a73548baf63de",
307
- "0x766a0abb3c77b2a8",
308
- "0x81c2c92e47edaee6",
309
- "0x92722c851482353b",
310
- "0xa2bfe8a14cf10364",
311
- "0xa81a664bbc423001",
312
- "0xc24b8b70d0f89791",
313
- "0xc76c51a30654be30",
314
- "0xd192e819d6ef5218",
315
- "0xd69906245565a910",
316
- "0xf40e35855771202a",
317
- "0x106aa07032bbd1b8",
318
- "0x19a4c116b8d2d0c8",
319
- "0x1e376c085141ab53",
320
- "0x2748774cdf8eeb99",
321
- "0x34b0bcb5e19b48a8",
322
- "0x391c0cb3c5c95a63",
323
- "0x4ed8aa4ae3418acb",
324
- "0x5b9cca4f7763e373",
325
- "0x682e6ff3d6b2b8a3",
326
- "0x748f82ee5defb2fc",
327
- "0x78a5636f43172f60",
328
- "0x84c87814a1f0ab72",
329
- "0x8cc702081a6439ec",
330
- "0x90befffa23631e28",
331
- "0xa4506cebde82bde9",
332
- "0xbef9a3f7b2c67915",
333
- "0xc67178f2e372532b",
334
- "0xca273eceea26619c",
335
- "0xd186b8c721c0c207",
336
- "0xeada7dd6cde0eb1e",
337
- "0xf57d4f7fee6ed178",
338
- "0x06f067aa72176fba",
339
- "0x0a637dc5a2c898a6",
340
- "0x113f9804bef90dae",
341
- "0x1b710b35131c471b",
342
- "0x28db77f523047d84",
343
- "0x32caab7b40c72493",
344
- "0x3c9ebe0a15c9bebc",
345
- "0x431d67c49c100d4c",
346
- "0x4cc5d4becb3e42b6",
347
- "0x597f299cfc657e2a",
348
- "0x5fcb6fab3ad6faec",
349
- "0x6c44198c4a475817"
350
- ].map((n) => BigInt(n)));
351
- var SHA512_W_H = new Uint32Array(80);
352
- var SHA512_W_L = new Uint32Array(80);
353
- var SHA512 = class extends SHA2 {
354
- constructor() {
355
- super(128, 64, 16, false);
356
- this.Ah = 1779033703 | 0;
357
- this.Al = 4089235720 | 0;
358
- this.Bh = 3144134277 | 0;
359
- this.Bl = 2227873595 | 0;
360
- this.Ch = 1013904242 | 0;
361
- this.Cl = 4271175723 | 0;
362
- this.Dh = 2773480762 | 0;
363
- this.Dl = 1595750129 | 0;
364
- this.Eh = 1359893119 | 0;
365
- this.El = 2917565137 | 0;
366
- this.Fh = 2600822924 | 0;
367
- this.Fl = 725511199 | 0;
368
- this.Gh = 528734635 | 0;
369
- this.Gl = 4215389547 | 0;
370
- this.Hh = 1541459225 | 0;
371
- this.Hl = 327033209 | 0;
372
- }
373
- // prettier-ignore
374
- get() {
375
- const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
376
- return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
377
- }
378
- // prettier-ignore
379
- set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
380
- this.Ah = Ah | 0;
381
- this.Al = Al | 0;
382
- this.Bh = Bh | 0;
383
- this.Bl = Bl | 0;
384
- this.Ch = Ch | 0;
385
- this.Cl = Cl | 0;
386
- this.Dh = Dh | 0;
387
- this.Dl = Dl | 0;
388
- this.Eh = Eh | 0;
389
- this.El = El | 0;
390
- this.Fh = Fh | 0;
391
- this.Fl = Fl | 0;
392
- this.Gh = Gh | 0;
393
- this.Gl = Gl | 0;
394
- this.Hh = Hh | 0;
395
- this.Hl = Hl | 0;
396
- }
397
- process(view, offset) {
398
- for (let i = 0; i < 16; i++, offset += 4) {
399
- SHA512_W_H[i] = view.getUint32(offset);
400
- SHA512_W_L[i] = view.getUint32(offset += 4);
401
- }
402
- for (let i = 16; i < 80; i++) {
403
- const W15h = SHA512_W_H[i - 15] | 0;
404
- const W15l = SHA512_W_L[i - 15] | 0;
405
- const s0h = u64_default.rotrSH(W15h, W15l, 1) ^ u64_default.rotrSH(W15h, W15l, 8) ^ u64_default.shrSH(W15h, W15l, 7);
406
- const s0l = u64_default.rotrSL(W15h, W15l, 1) ^ u64_default.rotrSL(W15h, W15l, 8) ^ u64_default.shrSL(W15h, W15l, 7);
407
- const W2h = SHA512_W_H[i - 2] | 0;
408
- const W2l = SHA512_W_L[i - 2] | 0;
409
- const s1h = u64_default.rotrSH(W2h, W2l, 19) ^ u64_default.rotrBH(W2h, W2l, 61) ^ u64_default.shrSH(W2h, W2l, 6);
410
- const s1l = u64_default.rotrSL(W2h, W2l, 19) ^ u64_default.rotrBL(W2h, W2l, 61) ^ u64_default.shrSL(W2h, W2l, 6);
411
- const SUMl = u64_default.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
412
- const SUMh = u64_default.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
413
- SHA512_W_H[i] = SUMh | 0;
414
- SHA512_W_L[i] = SUMl | 0;
415
- }
416
- let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
417
- for (let i = 0; i < 80; i++) {
418
- const sigma1h = u64_default.rotrSH(Eh, El, 14) ^ u64_default.rotrSH(Eh, El, 18) ^ u64_default.rotrBH(Eh, El, 41);
419
- const sigma1l = u64_default.rotrSL(Eh, El, 14) ^ u64_default.rotrSL(Eh, El, 18) ^ u64_default.rotrBL(Eh, El, 41);
420
- const CHIh = Eh & Fh ^ ~Eh & Gh;
421
- const CHIl = El & Fl ^ ~El & Gl;
422
- const T1ll = u64_default.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
423
- const T1h = u64_default.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
424
- const T1l = T1ll | 0;
425
- const sigma0h = u64_default.rotrSH(Ah, Al, 28) ^ u64_default.rotrBH(Ah, Al, 34) ^ u64_default.rotrBH(Ah, Al, 39);
426
- const sigma0l = u64_default.rotrSL(Ah, Al, 28) ^ u64_default.rotrBL(Ah, Al, 34) ^ u64_default.rotrBL(Ah, Al, 39);
427
- const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
428
- const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
429
- Hh = Gh | 0;
430
- Hl = Gl | 0;
431
- Gh = Fh | 0;
432
- Gl = Fl | 0;
433
- Fh = Eh | 0;
434
- Fl = El | 0;
435
- ({ h: Eh, l: El } = u64_default.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
436
- Dh = Ch | 0;
437
- Dl = Cl | 0;
438
- Ch = Bh | 0;
439
- Cl = Bl | 0;
440
- Bh = Ah | 0;
441
- Bl = Al | 0;
442
- const All = u64_default.add3L(T1l, sigma0l, MAJl);
443
- Ah = u64_default.add3H(All, T1h, sigma0h, MAJh);
444
- Al = All | 0;
445
- }
446
- ({ h: Ah, l: Al } = u64_default.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
447
- ({ h: Bh, l: Bl } = u64_default.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
448
- ({ h: Ch, l: Cl } = u64_default.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
449
- ({ h: Dh, l: Dl } = u64_default.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
450
- ({ h: Eh, l: El } = u64_default.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
451
- ({ h: Fh, l: Fl } = u64_default.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
452
- ({ h: Gh, l: Gl } = u64_default.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
453
- ({ h: Hh, l: Hl } = u64_default.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
454
- this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
455
- }
456
- roundClean() {
457
- SHA512_W_H.fill(0);
458
- SHA512_W_L.fill(0);
459
- }
460
- destroy() {
461
- this.buffer.fill(0);
462
- this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
463
- }
22
+ var str = (s) => typeof s === "string";
23
+ var au8 = (a, l) => (
24
+ // is Uint8Array (of specific length)
25
+ !(a instanceof Uint8Array) || typeof l === "number" && l > 0 && a.length !== l ? err("Uint8Array expected") : a
26
+ );
27
+ var u8n = (data) => new Uint8Array(data);
28
+ var toU8 = (a, len) => au8(str(a) ? h2b(a) : u8n(a), len);
29
+ var mod = (a, b = P) => {
30
+ let r = a % b;
31
+ return r >= 0n ? r : b + r;
464
32
  };
465
- var SHA512_224 = class extends SHA512 {
466
- constructor() {
467
- super();
468
- this.Ah = 2352822216 | 0;
469
- this.Al = 424955298 | 0;
470
- this.Bh = 1944164710 | 0;
471
- this.Bl = 2312950998 | 0;
472
- this.Ch = 502970286 | 0;
473
- this.Cl = 855612546 | 0;
474
- this.Dh = 1738396948 | 0;
475
- this.Dl = 1479516111 | 0;
476
- this.Eh = 258812777 | 0;
477
- this.El = 2077511080 | 0;
478
- this.Fh = 2011393907 | 0;
479
- this.Fl = 79989058 | 0;
480
- this.Gh = 1067287976 | 0;
481
- this.Gl = 1780299464 | 0;
482
- this.Hh = 286451373 | 0;
483
- this.Hl = 2446758561 | 0;
484
- this.outputLen = 28;
485
- }
33
+ var isPoint = (p) => p instanceof Point ? p : err("Point expected");
34
+ var Gpows = void 0;
35
+ var Point = class _Point {
36
+ constructor(ex, ey, ez, et) {
37
+ this.ex = ex;
38
+ this.ey = ey;
39
+ this.ez = ez;
40
+ this.et = et;
41
+ }
42
+ static fromAffine(p) {
43
+ return new _Point(p.x, p.y, 1n, mod(p.x * p.y));
44
+ }
45
+ static fromHex(hex, strict = true) {
46
+ const { d } = CURVE;
47
+ hex = toU8(hex, 32);
48
+ const normed = hex.slice();
49
+ normed[31] = hex[31] & ~128;
50
+ const y = b2n_LE(normed);
51
+ if (y === 0n) ; else {
52
+ if (strict && !(0n < y && y < P))
53
+ err("bad y coord 1");
54
+ if (!strict && !(0n < y && y < 2n ** 256n))
55
+ err("bad y coord 2");
56
+ }
57
+ const y2 = mod(y * y);
58
+ const u = mod(y2 - 1n);
59
+ const v = mod(d * y2 + 1n);
60
+ let { isValid, value: x } = uvRatio(u, v);
61
+ if (!isValid)
62
+ err("bad y coordinate 3");
63
+ const isXOdd = (x & 1n) === 1n;
64
+ const isHeadOdd = (hex[31] & 128) !== 0;
65
+ if (isHeadOdd !== isXOdd)
66
+ x = mod(-x);
67
+ return new _Point(x, y, 1n, mod(x * y));
68
+ }
69
+ get x() {
70
+ return this.toAffine().x;
71
+ }
72
+ // .x, .y will call expensive toAffine.
73
+ get y() {
74
+ return this.toAffine().y;
75
+ }
76
+ // Should be used with care.
77
+ equals(other) {
78
+ const { ex: X1, ey: Y1, ez: Z1 } = this;
79
+ const { ex: X2, ey: Y2, ez: Z2 } = isPoint(other);
80
+ const X1Z2 = mod(X1 * Z2), X2Z1 = mod(X2 * Z1);
81
+ const Y1Z2 = mod(Y1 * Z2), Y2Z1 = mod(Y2 * Z1);
82
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
83
+ }
84
+ is0() {
85
+ return this.equals(I);
86
+ }
87
+ negate() {
88
+ return new _Point(mod(-this.ex), this.ey, this.ez, mod(-this.et));
89
+ }
90
+ double() {
91
+ const { ex: X1, ey: Y1, ez: Z1 } = this;
92
+ const { a } = CURVE;
93
+ const A = mod(X1 * X1);
94
+ const B = mod(Y1 * Y1);
95
+ const C = mod(2n * mod(Z1 * Z1));
96
+ const D = mod(a * A);
97
+ const x1y1 = X1 + Y1;
98
+ const E = mod(mod(x1y1 * x1y1) - A - B);
99
+ const G2 = D + B;
100
+ const F = G2 - C;
101
+ const H = D - B;
102
+ const X3 = mod(E * F);
103
+ const Y3 = mod(G2 * H);
104
+ const T3 = mod(E * H);
105
+ const Z3 = mod(F * G2);
106
+ return new _Point(X3, Y3, Z3, T3);
107
+ }
108
+ add(other) {
109
+ const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
110
+ const { ex: X2, ey: Y2, ez: Z2, et: T2 } = isPoint(other);
111
+ const { a, d } = CURVE;
112
+ const A = mod(X1 * X2);
113
+ const B = mod(Y1 * Y2);
114
+ const C = mod(T1 * d * T2);
115
+ const D = mod(Z1 * Z2);
116
+ const E = mod((X1 + Y1) * (X2 + Y2) - A - B);
117
+ const F = mod(D - C);
118
+ const G2 = mod(D + C);
119
+ const H = mod(B - a * A);
120
+ const X3 = mod(E * F);
121
+ const Y3 = mod(G2 * H);
122
+ const T3 = mod(E * H);
123
+ const Z3 = mod(F * G2);
124
+ return new _Point(X3, Y3, Z3, T3);
125
+ }
126
+ mul(n, safe = true) {
127
+ if (n === 0n)
128
+ return safe === true ? err("cannot multiply by 0") : I;
129
+ if (!(typeof n === "bigint" && 0n < n && n < N))
130
+ err("invalid scalar, must be < L");
131
+ if (!safe && this.is0() || n === 1n)
132
+ return this;
133
+ if (this.equals(G))
134
+ return wNAF(n).p;
135
+ let p = I, f = G;
136
+ for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
137
+ if (n & 1n)
138
+ p = p.add(d);
139
+ else if (safe)
140
+ f = f.add(d);
141
+ }
142
+ return p;
143
+ }
144
+ multiply(scalar) {
145
+ return this.mul(scalar);
146
+ }
147
+ // Aliases for compatibilty
148
+ clearCofactor() {
149
+ return this.mul(BigInt(CURVE.h), false);
150
+ }
151
+ // multiply by cofactor
152
+ isSmallOrder() {
153
+ return this.clearCofactor().is0();
154
+ }
155
+ // check if P is small order
156
+ isTorsionFree() {
157
+ let p = this.mul(N / 2n, false).double();
158
+ if (N % 2n)
159
+ p = p.add(this);
160
+ return p.is0();
161
+ }
162
+ toAffine() {
163
+ const { ex: x, ey: y, ez: z } = this;
164
+ if (this.is0())
165
+ return { x: 0n, y: 0n };
166
+ const iz = invert(z);
167
+ if (mod(z * iz) !== 1n)
168
+ err("invalid inverse");
169
+ return { x: mod(x * iz), y: mod(y * iz) };
170
+ }
171
+ toRawBytes() {
172
+ const { x, y } = this.toAffine();
173
+ const b = n2b_32LE(y);
174
+ b[31] |= x & 1n ? 128 : 0;
175
+ return b;
176
+ }
177
+ toHex() {
178
+ return b2h(this.toRawBytes());
179
+ }
180
+ // encode to hex string
486
181
  };
487
- var SHA512_256 = class extends SHA512 {
488
- constructor() {
489
- super();
490
- this.Ah = 573645204 | 0;
491
- this.Al = 4230739756 | 0;
492
- this.Bh = 2673172387 | 0;
493
- this.Bl = 3360449730 | 0;
494
- this.Ch = 596883563 | 0;
495
- this.Cl = 1867755857 | 0;
496
- this.Dh = 2520282905 | 0;
497
- this.Dl = 1497426621 | 0;
498
- this.Eh = 2519219938 | 0;
499
- this.El = 2827943907 | 0;
500
- this.Fh = 3193839141 | 0;
501
- this.Fl = 1401305490 | 0;
502
- this.Gh = 721525244 | 0;
503
- this.Gl = 746961066 | 0;
504
- this.Hh = 246885852 | 0;
505
- this.Hl = 2177182882 | 0;
506
- this.outputLen = 32;
507
- }
508
- };
509
- var SHA384 = class extends SHA512 {
510
- constructor() {
511
- super();
512
- this.Ah = 3418070365 | 0;
513
- this.Al = 3238371032 | 0;
514
- this.Bh = 1654270250 | 0;
515
- this.Bl = 914150663 | 0;
516
- this.Ch = 2438529370 | 0;
517
- this.Cl = 812702999 | 0;
518
- this.Dh = 355462360 | 0;
519
- this.Dl = 4144912697 | 0;
520
- this.Eh = 1731405415 | 0;
521
- this.El = 4290775857 | 0;
522
- this.Fh = 2394180231 | 0;
523
- this.Fl = 1750603025 | 0;
524
- this.Gh = 3675008525 | 0;
525
- this.Gl = 1694076839 | 0;
526
- this.Hh = 1203062813 | 0;
527
- this.Hl = 3204075428 | 0;
528
- this.outputLen = 48;
529
- }
530
- };
531
- var sha512 = wrapConstructor(() => new SHA512());
532
- wrapConstructor(() => new SHA512_224());
533
- wrapConstructor(() => new SHA512_256());
534
- wrapConstructor(() => new SHA384());
535
-
536
- // ../../node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/utils.js
537
- BigInt(0);
538
- var _1n = BigInt(1);
539
- var _2n = BigInt(2);
540
- var u8a2 = (a) => a instanceof Uint8Array;
541
- var hexes2 = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
542
- function bytesToHex(bytes2) {
543
- if (!u8a2(bytes2))
544
- throw new Error("Uint8Array expected");
545
- let hex = "";
546
- for (let i = 0; i < bytes2.length; i++) {
547
- hex += hexes2[bytes2[i]];
548
- }
549
- return hex;
550
- }
551
- function hexToNumber(hex) {
552
- if (typeof hex !== "string")
553
- throw new Error("hex string expected, got " + typeof hex);
554
- return BigInt(hex === "" ? "0" : `0x${hex}`);
555
- }
556
- function hexToBytes(hex) {
557
- if (typeof hex !== "string")
558
- throw new Error("hex string expected, got " + typeof hex);
559
- const len = hex.length;
560
- if (len % 2)
561
- throw new Error("padded hex string expected, got unpadded hex of length " + len);
562
- const array = new Uint8Array(len / 2);
563
- for (let i = 0; i < array.length; i++) {
182
+ Point.BASE = new Point(Gx, Gy, 1n, mod(Gx * Gy));
183
+ Point.ZERO = new Point(0n, 1n, 1n, 0n);
184
+ var { BASE: G, ZERO: I } = Point;
185
+ var padh = (num, pad) => num.toString(16).padStart(pad, "0");
186
+ var b2h = (b) => Array.from(b).map((e) => padh(e, 2)).join("");
187
+ var h2b = (hex) => {
188
+ const l = hex.length;
189
+ if (!str(hex) || l % 2)
190
+ err("hex invalid 1");
191
+ const arr = u8n(l / 2);
192
+ for (let i = 0; i < arr.length; i++) {
564
193
  const j = i * 2;
565
- const hexByte = hex.slice(j, j + 2);
566
- const byte = Number.parseInt(hexByte, 16);
567
- if (Number.isNaN(byte) || byte < 0)
568
- throw new Error("Invalid byte sequence");
569
- array[i] = byte;
194
+ const h = hex.slice(j, j + 2);
195
+ const b = Number.parseInt(h, 16);
196
+ if (Number.isNaN(b) || b < 0)
197
+ err("hex invalid 2");
198
+ arr[i] = b;
570
199
  }
571
- return array;
572
- }
573
- function bytesToNumberBE(bytes2) {
574
- return hexToNumber(bytesToHex(bytes2));
575
- }
576
- function bytesToNumberLE(bytes2) {
577
- if (!u8a2(bytes2))
578
- throw new Error("Uint8Array expected");
579
- return hexToNumber(bytesToHex(Uint8Array.from(bytes2).reverse()));
580
- }
581
- function numberToBytesBE(n, len) {
582
- return hexToBytes(n.toString(16).padStart(len * 2, "0"));
583
- }
584
- function numberToBytesLE(n, len) {
585
- return numberToBytesBE(n, len).reverse();
586
- }
587
- function ensureBytes(title, hex, expectedLength) {
588
- let res;
589
- if (typeof hex === "string") {
590
- try {
591
- res = hexToBytes(hex);
592
- } catch (e) {
593
- throw new Error(`${title} must be valid hex string, got "${hex}". Cause: ${e}`);
594
- }
595
- } else if (u8a2(hex)) {
596
- res = Uint8Array.from(hex);
597
- } else {
598
- throw new Error(`${title} must be hex string or Uint8Array`);
599
- }
600
- const len = res.length;
601
- if (typeof expectedLength === "number" && len !== expectedLength)
602
- throw new Error(`${title} expected ${expectedLength} bytes, got ${len}`);
603
- return res;
604
- }
605
- function concatBytes2(...arrays) {
606
- const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
200
+ return arr;
201
+ };
202
+ var n2b_32LE = (num) => h2b(padh(num, 32 * 2)).reverse();
203
+ var b2n_LE = (b) => BigInt("0x" + b2h(u8n(au8(b)).reverse()));
204
+ var concatB = (...arrs) => {
205
+ const r = u8n(arrs.reduce((sum, a) => sum + au8(a).length, 0));
607
206
  let pad = 0;
608
- arrays.forEach((a) => {
609
- if (!u8a2(a))
610
- throw new Error("Uint8Array expected");
207
+ arrs.forEach((a) => {
611
208
  r.set(a, pad);
612
209
  pad += a.length;
613
210
  });
614
211
  return r;
615
- }
616
- var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
617
- var validatorFns = {
618
- bigint: (val) => typeof val === "bigint",
619
- function: (val) => typeof val === "function",
620
- boolean: (val) => typeof val === "boolean",
621
- string: (val) => typeof val === "string",
622
- isSafeInteger: (val) => Number.isSafeInteger(val),
623
- array: (val) => Array.isArray(val),
624
- field: (val, object) => object.Fp.isValid(val),
625
- hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
626
212
  };
627
- function validateObject(object, validators, optValidators = {}) {
628
- const checkField = (fieldName, type, isOptional) => {
629
- const checkVal = validatorFns[type];
630
- if (typeof checkVal !== "function")
631
- throw new Error(`Invalid validator "${type}", expected function`);
632
- const val = object[fieldName];
633
- if (isOptional && val === void 0)
634
- return;
635
- if (!checkVal(val, object)) {
636
- throw new Error(`Invalid param ${String(fieldName)}=${val} (${typeof val}), expected ${type}`);
637
- }
638
- };
639
- for (const [fieldName, type] of Object.entries(validators))
640
- checkField(fieldName, type, false);
641
- for (const [fieldName, type] of Object.entries(optValidators))
642
- checkField(fieldName, type, true);
643
- return object;
644
- }
645
-
646
- // ../../node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/modular.js
647
- var _0n2 = BigInt(0);
648
- var _1n2 = BigInt(1);
649
- var _2n2 = BigInt(2);
650
- var _3n = BigInt(3);
651
- var _4n = BigInt(4);
652
- var _5n = BigInt(5);
653
- var _8n = BigInt(8);
654
- BigInt(9);
655
- BigInt(16);
656
- function mod(a, b) {
657
- const result = a % b;
658
- return result >= _0n2 ? result : b + result;
659
- }
660
- function pow(num, power, modulo) {
661
- if (modulo <= _0n2 || power < _0n2)
662
- throw new Error("Expected power/modulo > 0");
663
- if (modulo === _1n2)
664
- return _0n2;
665
- let res = _1n2;
666
- while (power > _0n2) {
667
- if (power & _1n2)
668
- res = res * num % modulo;
669
- num = num * num % modulo;
670
- power >>= _1n2;
671
- }
672
- return res;
673
- }
674
- function pow2(x, power, modulo) {
675
- let res = x;
676
- while (power-- > _0n2) {
677
- res *= res;
678
- res %= modulo;
679
- }
680
- return res;
681
- }
682
- function invert(number2, modulo) {
683
- if (number2 === _0n2 || modulo <= _0n2) {
684
- throw new Error(`invert: expected positive integers, got n=${number2} mod=${modulo}`);
685
- }
686
- let a = mod(number2, modulo);
687
- let b = modulo;
688
- let x = _0n2, u = _1n2;
689
- while (a !== _0n2) {
690
- const q = b / a;
691
- const r = b % a;
213
+ var invert = (num, md = P) => {
214
+ if (num === 0n || md <= 0n)
215
+ err("no inverse n=" + num + " mod=" + md);
216
+ let a = mod(num, md), b = md, x = 0n, u = 1n;
217
+ while (a !== 0n) {
218
+ const q = b / a, r = b % a;
692
219
  const m = x - u * q;
693
220
  b = a, a = r, x = u, u = m;
694
221
  }
695
- const gcd = b;
696
- if (gcd !== _1n2)
697
- throw new Error("invert: does not exist");
698
- return mod(x, modulo);
699
- }
700
- function tonelliShanks(P) {
701
- const legendreC = (P - _1n2) / _2n2;
702
- let Q, S, Z;
703
- for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
704
- ;
705
- for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++)
706
- ;
707
- if (S === 1) {
708
- const p1div4 = (P + _1n2) / _4n;
709
- return function tonelliFast(Fp2, n) {
710
- const root = Fp2.pow(n, p1div4);
711
- if (!Fp2.eql(Fp2.sqr(root), n))
712
- throw new Error("Cannot find square root");
713
- return root;
714
- };
715
- }
716
- const Q1div2 = (Q + _1n2) / _2n2;
717
- return function tonelliSlow(Fp2, n) {
718
- if (Fp2.pow(n, legendreC) === Fp2.neg(Fp2.ONE))
719
- throw new Error("Cannot find square root");
720
- let r = S;
721
- let g = Fp2.pow(Fp2.mul(Fp2.ONE, Z), Q);
722
- let x = Fp2.pow(n, Q1div2);
723
- let b = Fp2.pow(n, Q);
724
- while (!Fp2.eql(b, Fp2.ONE)) {
725
- if (Fp2.eql(b, Fp2.ZERO))
726
- return Fp2.ZERO;
727
- let m = 1;
728
- for (let t2 = Fp2.sqr(b); m < r; m++) {
729
- if (Fp2.eql(t2, Fp2.ONE))
730
- break;
731
- t2 = Fp2.sqr(t2);
732
- }
733
- const ge = Fp2.pow(g, _1n2 << BigInt(r - m - 1));
734
- g = Fp2.sqr(ge);
735
- x = Fp2.mul(x, ge);
736
- b = Fp2.mul(b, g);
737
- r = m;
738
- }
739
- return x;
740
- };
741
- }
742
- function FpSqrt(P) {
743
- if (P % _4n === _3n) {
744
- const p1div4 = (P + _1n2) / _4n;
745
- return function sqrt3mod4(Fp2, n) {
746
- const root = Fp2.pow(n, p1div4);
747
- if (!Fp2.eql(Fp2.sqr(root), n))
748
- throw new Error("Cannot find square root");
749
- return root;
750
- };
751
- }
752
- if (P % _8n === _5n) {
753
- const c1 = (P - _5n) / _8n;
754
- return function sqrt5mod8(Fp2, n) {
755
- const n2 = Fp2.mul(n, _2n2);
756
- const v = Fp2.pow(n2, c1);
757
- const nv = Fp2.mul(n, v);
758
- const i = Fp2.mul(Fp2.mul(nv, _2n2), v);
759
- const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
760
- if (!Fp2.eql(Fp2.sqr(root), n))
761
- throw new Error("Cannot find square root");
762
- return root;
763
- };
764
- }
765
- return tonelliShanks(P);
766
- }
767
- var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
768
- var FIELD_FIELDS = [
769
- "create",
770
- "isValid",
771
- "is0",
772
- "neg",
773
- "inv",
774
- "sqrt",
775
- "sqr",
776
- "eql",
777
- "add",
778
- "sub",
779
- "mul",
780
- "pow",
781
- "div",
782
- "addN",
783
- "subN",
784
- "mulN",
785
- "sqrN"
786
- ];
787
- function validateField(field) {
788
- const initial = {
789
- ORDER: "bigint",
790
- MASK: "bigint",
791
- BYTES: "isSafeInteger",
792
- BITS: "isSafeInteger"
793
- };
794
- const opts = FIELD_FIELDS.reduce((map, val) => {
795
- map[val] = "function";
796
- return map;
797
- }, initial);
798
- return validateObject(field, opts);
799
- }
800
- function FpPow(f, num, power) {
801
- if (power < _0n2)
802
- throw new Error("Expected power > 0");
803
- if (power === _0n2)
804
- return f.ONE;
805
- if (power === _1n2)
806
- return num;
807
- let p = f.ONE;
808
- let d = num;
809
- while (power > _0n2) {
810
- if (power & _1n2)
811
- p = f.mul(p, d);
812
- d = f.sqr(d);
813
- power >>= _1n2;
814
- }
815
- return p;
816
- }
817
- function FpInvertBatch(f, nums) {
818
- const tmp = new Array(nums.length);
819
- const lastMultiplied = nums.reduce((acc, num, i) => {
820
- if (f.is0(num))
821
- return acc;
822
- tmp[i] = acc;
823
- return f.mul(acc, num);
824
- }, f.ONE);
825
- const inverted = f.inv(lastMultiplied);
826
- nums.reduceRight((acc, num, i) => {
827
- if (f.is0(num))
828
- return acc;
829
- tmp[i] = f.mul(acc, tmp[i]);
830
- return f.mul(acc, num);
831
- }, inverted);
832
- return tmp;
833
- }
834
- function nLength(n, nBitLength) {
835
- const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
836
- const nByteLength = Math.ceil(_nBitLength / 8);
837
- return { nBitLength: _nBitLength, nByteLength };
838
- }
839
- function Field(ORDER, bitLen, isLE2 = false, redef = {}) {
840
- if (ORDER <= _0n2)
841
- throw new Error(`Expected Fp ORDER > 0, got ${ORDER}`);
842
- const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen);
843
- if (BYTES > 2048)
844
- throw new Error("Field lengths over 2048 bytes are not supported");
845
- const sqrtP = FpSqrt(ORDER);
846
- const f = Object.freeze({
847
- ORDER,
848
- BITS,
849
- BYTES,
850
- MASK: bitMask(BITS),
851
- ZERO: _0n2,
852
- ONE: _1n2,
853
- create: (num) => mod(num, ORDER),
854
- isValid: (num) => {
855
- if (typeof num !== "bigint")
856
- throw new Error(`Invalid field element: expected bigint, got ${typeof num}`);
857
- return _0n2 <= num && num < ORDER;
858
- },
859
- is0: (num) => num === _0n2,
860
- isOdd: (num) => (num & _1n2) === _1n2,
861
- neg: (num) => mod(-num, ORDER),
862
- eql: (lhs, rhs) => lhs === rhs,
863
- sqr: (num) => mod(num * num, ORDER),
864
- add: (lhs, rhs) => mod(lhs + rhs, ORDER),
865
- sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
866
- mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
867
- pow: (num, power) => FpPow(f, num, power),
868
- div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
869
- // Same as above, but doesn't normalize
870
- sqrN: (num) => num * num,
871
- addN: (lhs, rhs) => lhs + rhs,
872
- subN: (lhs, rhs) => lhs - rhs,
873
- mulN: (lhs, rhs) => lhs * rhs,
874
- inv: (num) => invert(num, ORDER),
875
- sqrt: redef.sqrt || ((n) => sqrtP(f, n)),
876
- invertBatch: (lst) => FpInvertBatch(f, lst),
877
- // TODO: do we really need constant cmov?
878
- // We don't have const-time bigints anyway, so probably will be not very useful
879
- cmov: (a, b, c) => c ? b : a,
880
- toBytes: (num) => isLE2 ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
881
- fromBytes: (bytes2) => {
882
- if (bytes2.length !== BYTES)
883
- throw new Error(`Fp.fromBytes: expected ${BYTES}, got ${bytes2.length}`);
884
- return isLE2 ? bytesToNumberLE(bytes2) : bytesToNumberBE(bytes2);
885
- }
886
- });
887
- return Object.freeze(f);
888
- }
889
- function FpSqrtEven(Fp2, elm) {
890
- if (!Fp2.isOdd)
891
- throw new Error(`Field doesn't have isOdd`);
892
- const root = Fp2.sqrt(elm);
893
- return Fp2.isOdd(root) ? Fp2.neg(root) : root;
894
- }
895
-
896
- // ../../node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/curve.js
897
- var _0n3 = BigInt(0);
898
- var _1n3 = BigInt(1);
899
- function wNAF(c, bits) {
900
- const constTimeNegate = (condition, item) => {
901
- const neg = item.negate();
902
- return condition ? neg : item;
903
- };
904
- const opts = (W) => {
905
- const windows = Math.ceil(bits / W) + 1;
906
- const windowSize = 2 ** (W - 1);
907
- return { windows, windowSize };
908
- };
909
- return {
910
- constTimeNegate,
911
- // non-const time multiplication ladder
912
- unsafeLadder(elm, n) {
913
- let p = c.ZERO;
914
- let d = elm;
915
- while (n > _0n3) {
916
- if (n & _1n3)
917
- p = p.add(d);
918
- d = d.double();
919
- n >>= _1n3;
920
- }
921
- return p;
922
- },
923
- /**
924
- * Creates a wNAF precomputation window. Used for caching.
925
- * Default window size is set by `utils.precompute()` and is equal to 8.
926
- * Number of precomputed points depends on the curve size:
927
- * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
928
- * - 𝑊 is the window size
929
- * - 𝑛 is the bitlength of the curve order.
930
- * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
931
- * @returns precomputed point tables flattened to a single array
932
- */
933
- precomputeWindow(elm, W) {
934
- const { windows, windowSize } = opts(W);
935
- const points = [];
936
- let p = elm;
937
- let base = p;
938
- for (let window = 0; window < windows; window++) {
939
- base = p;
940
- points.push(base);
941
- for (let i = 1; i < windowSize; i++) {
942
- base = base.add(p);
943
- points.push(base);
944
- }
945
- p = base.double();
946
- }
947
- return points;
948
- },
949
- /**
950
- * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
951
- * @param W window size
952
- * @param precomputes precomputed tables
953
- * @param n scalar (we don't check here, but should be less than curve order)
954
- * @returns real and fake (for const-time) points
955
- */
956
- wNAF(W, precomputes, n) {
957
- const { windows, windowSize } = opts(W);
958
- let p = c.ZERO;
959
- let f = c.BASE;
960
- const mask = BigInt(2 ** W - 1);
961
- const maxNumber = 2 ** W;
962
- const shiftBy = BigInt(W);
963
- for (let window = 0; window < windows; window++) {
964
- const offset = window * windowSize;
965
- let wbits = Number(n & mask);
966
- n >>= shiftBy;
967
- if (wbits > windowSize) {
968
- wbits -= maxNumber;
969
- n += _1n3;
970
- }
971
- const offset1 = offset;
972
- const offset2 = offset + Math.abs(wbits) - 1;
973
- const cond1 = window % 2 !== 0;
974
- const cond2 = wbits < 0;
975
- if (wbits === 0) {
976
- f = f.add(constTimeNegate(cond1, precomputes[offset1]));
977
- } else {
978
- p = p.add(constTimeNegate(cond2, precomputes[offset2]));
979
- }
980
- }
981
- return { p, f };
982
- },
983
- wNAFCached(P, precomputesMap, n, transform) {
984
- const W = P._WINDOW_SIZE || 1;
985
- let comp = precomputesMap.get(P);
986
- if (!comp) {
987
- comp = this.precomputeWindow(P, W);
988
- if (W !== 1) {
989
- precomputesMap.set(P, transform(comp));
990
- }
991
- }
992
- return this.wNAF(W, comp, n);
993
- }
994
- };
995
- }
996
- function validateBasic(curve) {
997
- validateField(curve.Fp);
998
- validateObject(curve, {
999
- n: "bigint",
1000
- h: "bigint",
1001
- Gx: "field",
1002
- Gy: "field"
1003
- }, {
1004
- nBitLength: "isSafeInteger",
1005
- nByteLength: "isSafeInteger"
1006
- });
1007
- return Object.freeze({
1008
- ...nLength(curve.n, curve.nBitLength),
1009
- ...curve,
1010
- ...{ p: curve.Fp.ORDER }
1011
- });
1012
- }
1013
-
1014
- // ../../node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/edwards.js
1015
- var _0n4 = BigInt(0);
1016
- var _1n4 = BigInt(1);
1017
- var _2n3 = BigInt(2);
1018
- var _8n2 = BigInt(8);
1019
- var VERIFY_DEFAULT = { zip215: true };
1020
- function validateOpts(curve) {
1021
- const opts = validateBasic(curve);
1022
- validateObject(curve, {
1023
- hash: "function",
1024
- a: "bigint",
1025
- d: "bigint",
1026
- randomBytes: "function"
1027
- }, {
1028
- adjustScalarBytes: "function",
1029
- domain: "function",
1030
- uvRatio: "function",
1031
- mapToCurve: "function"
1032
- });
1033
- return Object.freeze({ ...opts });
1034
- }
1035
- function twistedEdwards(curveDef) {
1036
- const CURVE = validateOpts(curveDef);
1037
- const { Fp: Fp2, n: CURVE_ORDER, prehash, hash: cHash, randomBytes: randomBytes2, nByteLength, h: cofactor } = CURVE;
1038
- const MASK = _2n3 << BigInt(nByteLength * 8) - _1n4;
1039
- const modP = Fp2.create;
1040
- const uvRatio2 = CURVE.uvRatio || ((u, v) => {
1041
- try {
1042
- return { isValid: true, value: Fp2.sqrt(u * Fp2.inv(v)) };
1043
- } catch (e) {
1044
- return { isValid: false, value: _0n4 };
1045
- }
1046
- });
1047
- const adjustScalarBytes2 = CURVE.adjustScalarBytes || ((bytes2) => bytes2);
1048
- const domain = CURVE.domain || ((data, ctx, phflag) => {
1049
- if (ctx.length || phflag)
1050
- throw new Error("Contexts/pre-hash are not supported");
1051
- return data;
1052
- });
1053
- const inBig = (n) => typeof n === "bigint" && _0n4 < n;
1054
- const inRange = (n, max) => inBig(n) && inBig(max) && n < max;
1055
- const in0MaskRange = (n) => n === _0n4 || inRange(n, MASK);
1056
- function assertInRange(n, max) {
1057
- if (inRange(n, max))
1058
- return n;
1059
- throw new Error(`Expected valid scalar < ${max}, got ${typeof n} ${n}`);
1060
- }
1061
- function assertGE0(n) {
1062
- return n === _0n4 ? n : assertInRange(n, CURVE_ORDER);
1063
- }
1064
- const pointPrecomputes = /* @__PURE__ */ new Map();
1065
- function isPoint(other) {
1066
- if (!(other instanceof Point))
1067
- throw new Error("ExtendedPoint expected");
1068
- }
1069
- class Point {
1070
- constructor(ex, ey, ez, et) {
1071
- this.ex = ex;
1072
- this.ey = ey;
1073
- this.ez = ez;
1074
- this.et = et;
1075
- if (!in0MaskRange(ex))
1076
- throw new Error("x required");
1077
- if (!in0MaskRange(ey))
1078
- throw new Error("y required");
1079
- if (!in0MaskRange(ez))
1080
- throw new Error("z required");
1081
- if (!in0MaskRange(et))
1082
- throw new Error("t required");
1083
- }
1084
- get x() {
1085
- return this.toAffine().x;
1086
- }
1087
- get y() {
1088
- return this.toAffine().y;
1089
- }
1090
- static fromAffine(p) {
1091
- if (p instanceof Point)
1092
- throw new Error("extended point not allowed");
1093
- const { x, y } = p || {};
1094
- if (!in0MaskRange(x) || !in0MaskRange(y))
1095
- throw new Error("invalid affine point");
1096
- return new Point(x, y, _1n4, modP(x * y));
1097
- }
1098
- static normalizeZ(points) {
1099
- const toInv = Fp2.invertBatch(points.map((p) => p.ez));
1100
- return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
1101
- }
1102
- // "Private method", don't use it directly
1103
- _setWindowSize(windowSize) {
1104
- this._WINDOW_SIZE = windowSize;
1105
- pointPrecomputes.delete(this);
1106
- }
1107
- // Not required for fromHex(), which always creates valid points.
1108
- // Could be useful for fromAffine().
1109
- assertValidity() {
1110
- const { a, d } = CURVE;
1111
- if (this.is0())
1112
- throw new Error("bad point: ZERO");
1113
- const { ex: X, ey: Y, ez: Z, et: T } = this;
1114
- const X2 = modP(X * X);
1115
- const Y2 = modP(Y * Y);
1116
- const Z2 = modP(Z * Z);
1117
- const Z4 = modP(Z2 * Z2);
1118
- const aX2 = modP(X2 * a);
1119
- const left = modP(Z2 * modP(aX2 + Y2));
1120
- const right = modP(Z4 + modP(d * modP(X2 * Y2)));
1121
- if (left !== right)
1122
- throw new Error("bad point: equation left != right (1)");
1123
- const XY = modP(X * Y);
1124
- const ZT = modP(Z * T);
1125
- if (XY !== ZT)
1126
- throw new Error("bad point: equation left != right (2)");
1127
- }
1128
- // Compare one point to another.
1129
- equals(other) {
1130
- isPoint(other);
1131
- const { ex: X1, ey: Y1, ez: Z1 } = this;
1132
- const { ex: X2, ey: Y2, ez: Z2 } = other;
1133
- const X1Z2 = modP(X1 * Z2);
1134
- const X2Z1 = modP(X2 * Z1);
1135
- const Y1Z2 = modP(Y1 * Z2);
1136
- const Y2Z1 = modP(Y2 * Z1);
1137
- return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
1138
- }
1139
- is0() {
1140
- return this.equals(Point.ZERO);
1141
- }
1142
- negate() {
1143
- return new Point(modP(-this.ex), this.ey, this.ez, modP(-this.et));
1144
- }
1145
- // Fast algo for doubling Extended Point.
1146
- // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
1147
- // Cost: 4M + 4S + 1*a + 6add + 1*2.
1148
- double() {
1149
- const { a } = CURVE;
1150
- const { ex: X1, ey: Y1, ez: Z1 } = this;
1151
- const A = modP(X1 * X1);
1152
- const B = modP(Y1 * Y1);
1153
- const C = modP(_2n3 * modP(Z1 * Z1));
1154
- const D = modP(a * A);
1155
- const x1y1 = X1 + Y1;
1156
- const E = modP(modP(x1y1 * x1y1) - A - B);
1157
- const G2 = D + B;
1158
- const F = G2 - C;
1159
- const H = D - B;
1160
- const X3 = modP(E * F);
1161
- const Y3 = modP(G2 * H);
1162
- const T3 = modP(E * H);
1163
- const Z3 = modP(F * G2);
1164
- return new Point(X3, Y3, Z3, T3);
1165
- }
1166
- // Fast algo for adding 2 Extended Points.
1167
- // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
1168
- // Cost: 9M + 1*a + 1*d + 7add.
1169
- add(other) {
1170
- isPoint(other);
1171
- const { a, d } = CURVE;
1172
- const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
1173
- const { ex: X2, ey: Y2, ez: Z2, et: T2 } = other;
1174
- if (a === BigInt(-1)) {
1175
- const A2 = modP((Y1 - X1) * (Y2 + X2));
1176
- const B2 = modP((Y1 + X1) * (Y2 - X2));
1177
- const F2 = modP(B2 - A2);
1178
- if (F2 === _0n4)
1179
- return this.double();
1180
- const C2 = modP(Z1 * _2n3 * T2);
1181
- const D2 = modP(T1 * _2n3 * Z2);
1182
- const E2 = D2 + C2;
1183
- const G3 = B2 + A2;
1184
- const H2 = D2 - C2;
1185
- const X32 = modP(E2 * F2);
1186
- const Y32 = modP(G3 * H2);
1187
- const T32 = modP(E2 * H2);
1188
- const Z32 = modP(F2 * G3);
1189
- return new Point(X32, Y32, Z32, T32);
1190
- }
1191
- const A = modP(X1 * X2);
1192
- const B = modP(Y1 * Y2);
1193
- const C = modP(T1 * d * T2);
1194
- const D = modP(Z1 * Z2);
1195
- const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
1196
- const F = D - C;
1197
- const G2 = D + C;
1198
- const H = modP(B - a * A);
1199
- const X3 = modP(E * F);
1200
- const Y3 = modP(G2 * H);
1201
- const T3 = modP(E * H);
1202
- const Z3 = modP(F * G2);
1203
- return new Point(X3, Y3, Z3, T3);
1204
- }
1205
- subtract(other) {
1206
- return this.add(other.negate());
1207
- }
1208
- wNAF(n) {
1209
- return wnaf.wNAFCached(this, pointPrecomputes, n, Point.normalizeZ);
1210
- }
1211
- // Constant-time multiplication.
1212
- multiply(scalar) {
1213
- const { p, f } = this.wNAF(assertInRange(scalar, CURVE_ORDER));
1214
- return Point.normalizeZ([p, f])[0];
1215
- }
1216
- // Non-constant-time multiplication. Uses double-and-add algorithm.
1217
- // It's faster, but should only be used when you don't care about
1218
- // an exposed private key e.g. sig verification.
1219
- // Does NOT allow scalars higher than CURVE.n.
1220
- multiplyUnsafe(scalar) {
1221
- let n = assertGE0(scalar);
1222
- if (n === _0n4)
1223
- return I;
1224
- if (this.equals(I) || n === _1n4)
1225
- return this;
1226
- if (this.equals(G))
1227
- return this.wNAF(n).p;
1228
- return wnaf.unsafeLadder(this, n);
1229
- }
1230
- // Checks if point is of small order.
1231
- // If you add something to small order point, you will have "dirty"
1232
- // point with torsion component.
1233
- // Multiplies point by cofactor and checks if the result is 0.
1234
- isSmallOrder() {
1235
- return this.multiplyUnsafe(cofactor).is0();
1236
- }
1237
- // Multiplies point by curve order and checks if the result is 0.
1238
- // Returns `false` is the point is dirty.
1239
- isTorsionFree() {
1240
- return wnaf.unsafeLadder(this, CURVE_ORDER).is0();
1241
- }
1242
- // Converts Extended point to default (x, y) coordinates.
1243
- // Can accept precomputed Z^-1 - for example, from invertBatch.
1244
- toAffine(iz) {
1245
- const { ex: x, ey: y, ez: z } = this;
1246
- const is0 = this.is0();
1247
- if (iz == null)
1248
- iz = is0 ? _8n2 : Fp2.inv(z);
1249
- const ax = modP(x * iz);
1250
- const ay = modP(y * iz);
1251
- const zz = modP(z * iz);
1252
- if (is0)
1253
- return { x: _0n4, y: _1n4 };
1254
- if (zz !== _1n4)
1255
- throw new Error("invZ was invalid");
1256
- return { x: ax, y: ay };
1257
- }
1258
- clearCofactor() {
1259
- const { h: cofactor2 } = CURVE;
1260
- if (cofactor2 === _1n4)
1261
- return this;
1262
- return this.multiplyUnsafe(cofactor2);
1263
- }
1264
- // Converts hash string or Uint8Array to Point.
1265
- // Uses algo from RFC8032 5.1.3.
1266
- static fromHex(hex, zip215 = false) {
1267
- const { d, a } = CURVE;
1268
- const len = Fp2.BYTES;
1269
- hex = ensureBytes("pointHex", hex, len);
1270
- const normed = hex.slice();
1271
- const lastByte = hex[len - 1];
1272
- normed[len - 1] = lastByte & ~128;
1273
- const y = bytesToNumberLE(normed);
1274
- if (y === _0n4) ; else {
1275
- if (zip215)
1276
- assertInRange(y, MASK);
1277
- else
1278
- assertInRange(y, Fp2.ORDER);
1279
- }
1280
- const y2 = modP(y * y);
1281
- const u = modP(y2 - _1n4);
1282
- const v = modP(d * y2 - a);
1283
- let { isValid, value: x } = uvRatio2(u, v);
1284
- if (!isValid)
1285
- throw new Error("Point.fromHex: invalid y coordinate");
1286
- const isXOdd = (x & _1n4) === _1n4;
1287
- const isLastByteOdd = (lastByte & 128) !== 0;
1288
- if (!zip215 && x === _0n4 && isLastByteOdd)
1289
- throw new Error("Point.fromHex: x=0 and x_0=1");
1290
- if (isLastByteOdd !== isXOdd)
1291
- x = modP(-x);
1292
- return Point.fromAffine({ x, y });
1293
- }
1294
- static fromPrivateKey(privKey) {
1295
- return getExtendedPublicKey(privKey).point;
1296
- }
1297
- toRawBytes() {
1298
- const { x, y } = this.toAffine();
1299
- const bytes2 = numberToBytesLE(y, Fp2.BYTES);
1300
- bytes2[bytes2.length - 1] |= x & _1n4 ? 128 : 0;
1301
- return bytes2;
1302
- }
1303
- toHex() {
1304
- return bytesToHex(this.toRawBytes());
1305
- }
1306
- }
1307
- Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
1308
- Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
1309
- const { BASE: G, ZERO: I } = Point;
1310
- const wnaf = wNAF(Point, nByteLength * 8);
1311
- function modN(a) {
1312
- return mod(a, CURVE_ORDER);
1313
- }
1314
- function modN_LE(hash2) {
1315
- return modN(bytesToNumberLE(hash2));
1316
- }
1317
- function getExtendedPublicKey(key) {
1318
- const len = nByteLength;
1319
- key = ensureBytes("private key", key, len);
1320
- const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
1321
- const head = adjustScalarBytes2(hashed.slice(0, len));
1322
- const prefix = hashed.slice(len, 2 * len);
1323
- const scalar = modN_LE(head);
1324
- const point = G.multiply(scalar);
1325
- const pointBytes = point.toRawBytes();
1326
- return { head, prefix, scalar, point, pointBytes };
1327
- }
1328
- function getPublicKey(privKey) {
1329
- return getExtendedPublicKey(privKey).pointBytes;
1330
- }
1331
- function hashDomainToScalar(context = new Uint8Array(), ...msgs) {
1332
- const msg = concatBytes2(...msgs);
1333
- return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
1334
- }
1335
- function sign(msg, privKey, options = {}) {
1336
- msg = ensureBytes("message", msg);
1337
- if (prehash)
1338
- msg = prehash(msg);
1339
- const { prefix, scalar, pointBytes } = getExtendedPublicKey(privKey);
1340
- const r = hashDomainToScalar(options.context, prefix, msg);
1341
- const R = G.multiply(r).toRawBytes();
1342
- const k = hashDomainToScalar(options.context, R, pointBytes, msg);
1343
- const s = modN(r + k * scalar);
1344
- assertGE0(s);
1345
- const res = concatBytes2(R, numberToBytesLE(s, Fp2.BYTES));
1346
- return ensureBytes("result", res, nByteLength * 2);
1347
- }
1348
- const verifyOpts = VERIFY_DEFAULT;
1349
- function verify(sig, msg, publicKey, options = verifyOpts) {
1350
- const { context, zip215 } = options;
1351
- const len = Fp2.BYTES;
1352
- sig = ensureBytes("signature", sig, 2 * len);
1353
- msg = ensureBytes("message", msg);
1354
- if (prehash)
1355
- msg = prehash(msg);
1356
- const s = bytesToNumberLE(sig.slice(len, 2 * len));
1357
- let A, R, SB;
1358
- try {
1359
- A = Point.fromHex(publicKey, zip215);
1360
- R = Point.fromHex(sig.slice(0, len), zip215);
1361
- SB = G.multiplyUnsafe(s);
1362
- } catch (error) {
1363
- return false;
1364
- }
1365
- if (!zip215 && A.isSmallOrder())
1366
- return false;
1367
- const k = hashDomainToScalar(context, R.toRawBytes(), A.toRawBytes(), msg);
1368
- const RkA = R.add(A.multiplyUnsafe(k));
1369
- return RkA.subtract(SB).clearCofactor().equals(Point.ZERO);
222
+ return b === 1n ? mod(x, md) : err("no inverse");
223
+ };
224
+ var pow2 = (x, power) => {
225
+ let r = x;
226
+ while (power-- > 0n) {
227
+ r *= r;
228
+ r %= P;
1370
229
  }
1371
- G._setWindowSize(8);
1372
- const utils = {
1373
- getExtendedPublicKey,
1374
- // ed25519 private keys are uniform 32b. No need to check for modulo bias, like in secp256k1.
1375
- randomPrivateKey: () => randomBytes2(Fp2.BYTES),
1376
- /**
1377
- * We're doing scalar multiplication (used in getPublicKey etc) with precomputed BASE_POINT
1378
- * values. This slows down first getPublicKey() by milliseconds (see Speed section),
1379
- * but allows to speed-up subsequent getPublicKey() calls up to 20x.
1380
- * @param windowSize 2, 4, 8, 16
1381
- */
1382
- precompute(windowSize = 8, point = Point.BASE) {
1383
- point._setWindowSize(windowSize);
1384
- point.multiply(BigInt(3));
1385
- return point;
1386
- }
1387
- };
1388
- return {
1389
- CURVE,
1390
- getPublicKey,
1391
- sign,
1392
- verify,
1393
- ExtendedPoint: Point,
1394
- utils
1395
- };
1396
- }
1397
-
1398
- // ../../node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/montgomery.js
1399
- BigInt(0);
1400
- BigInt(1);
1401
-
1402
- // ../../node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/ed25519.js
1403
- var ED25519_P = BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819949");
1404
- var ED25519_SQRT_M1 = BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
1405
- BigInt(0);
1406
- var _1n6 = BigInt(1);
1407
- var _2n4 = BigInt(2);
1408
- var _5n2 = BigInt(5);
1409
- var _10n = BigInt(10);
1410
- var _20n = BigInt(20);
1411
- var _40n = BigInt(40);
1412
- var _80n = BigInt(80);
1413
- function ed25519_pow_2_252_3(x) {
1414
- const P = ED25519_P;
230
+ return r;
231
+ };
232
+ var pow_2_252_3 = (x) => {
1415
233
  const x2 = x * x % P;
1416
234
  const b2 = x2 * x % P;
1417
- const b4 = pow2(b2, _2n4, P) * b2 % P;
1418
- const b5 = pow2(b4, _1n6, P) * x % P;
1419
- const b10 = pow2(b5, _5n2, P) * b5 % P;
1420
- const b20 = pow2(b10, _10n, P) * b10 % P;
1421
- const b40 = pow2(b20, _20n, P) * b20 % P;
1422
- const b80 = pow2(b40, _40n, P) * b40 % P;
1423
- const b160 = pow2(b80, _80n, P) * b80 % P;
1424
- const b240 = pow2(b160, _80n, P) * b80 % P;
1425
- const b250 = pow2(b240, _10n, P) * b10 % P;
1426
- const pow_p_5_8 = pow2(b250, _2n4, P) * x % P;
235
+ const b4 = pow2(b2, 2n) * b2 % P;
236
+ const b5 = pow2(b4, 1n) * x % P;
237
+ const b10 = pow2(b5, 5n) * b5 % P;
238
+ const b20 = pow2(b10, 10n) * b10 % P;
239
+ const b40 = pow2(b20, 20n) * b20 % P;
240
+ const b80 = pow2(b40, 40n) * b40 % P;
241
+ const b160 = pow2(b80, 80n) * b80 % P;
242
+ const b240 = pow2(b160, 80n) * b80 % P;
243
+ const b250 = pow2(b240, 10n) * b10 % P;
244
+ const pow_p_5_8 = pow2(b250, 2n) * x % P;
1427
245
  return { pow_p_5_8, b2 };
1428
- }
1429
- function adjustScalarBytes(bytes2) {
1430
- bytes2[0] &= 248;
1431
- bytes2[31] &= 127;
1432
- bytes2[31] |= 64;
1433
- return bytes2;
1434
- }
1435
- function uvRatio(u, v) {
1436
- const P = ED25519_P;
1437
- const v3 = mod(v * v * v, P);
1438
- const v7 = mod(v3 * v3 * v, P);
1439
- const pow3 = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
1440
- let x = mod(u * v3 * pow3, P);
1441
- const vx2 = mod(v * x * x, P);
246
+ };
247
+ var RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
248
+ var uvRatio = (u, v) => {
249
+ const v3 = mod(v * v * v);
250
+ const v7 = mod(v3 * v3 * v);
251
+ const pow = pow_2_252_3(u * v7).pow_p_5_8;
252
+ let x = mod(u * v3 * pow);
253
+ const vx2 = mod(v * x * x);
1442
254
  const root1 = x;
1443
- const root2 = mod(x * ED25519_SQRT_M1, P);
255
+ const root2 = mod(x * RM1);
1444
256
  const useRoot1 = vx2 === u;
1445
- const useRoot2 = vx2 === mod(-u, P);
1446
- const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
257
+ const useRoot2 = vx2 === mod(-u);
258
+ const noRoot = vx2 === mod(-u * RM1);
1447
259
  if (useRoot1)
1448
260
  x = root1;
1449
261
  if (useRoot2 || noRoot)
1450
262
  x = root2;
1451
- if (isNegativeLE(x, P))
1452
- x = mod(-x, P);
263
+ if ((mod(x) & 1n) === 1n)
264
+ x = mod(-x);
1453
265
  return { isValid: useRoot1 || useRoot2, value: x };
1454
- }
1455
- var Fp = Field(ED25519_P, void 0, true);
1456
- var ed25519Defaults = {
1457
- // Param: a
1458
- a: BigInt(-1),
1459
- // d is equal to -121665/121666 over finite field.
1460
- // Negative number is P - number, and division is invert(number, P)
1461
- d: BigInt("37095705934669439343138083508754565189542113879843219016388785533085940283555"),
1462
- // Finite field 𝔽p over which we'll do calculations; 2n**255n - 19n
1463
- Fp,
1464
- // Subgroup order: how many points curve has
1465
- // 2n**252n + 27742317777372353535851937790883648493n;
1466
- n: BigInt("7237005577332262213973186563042994240857116359379907606001950938285454250989"),
1467
- // Cofactor
1468
- h: BigInt(8),
1469
- // Base point (x, y) aka generator point
1470
- Gx: BigInt("15112221349535400772501151409588531511454012693041857206046113283949847762202"),
1471
- Gy: BigInt("46316835694926478169428394003475163141307993866256225615783033603165251855960"),
1472
- hash: sha512,
1473
- randomBytes,
1474
- adjustScalarBytes,
1475
- // dom2
1476
- // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
1477
- // Constant-time, u/√v
1478
- uvRatio
1479
266
  };
1480
- var ed25519 = twistedEdwards(ed25519Defaults);
1481
- function ed25519_domain(data, ctx, phflag) {
1482
- if (ctx.length > 255)
1483
- throw new Error("Context is too big");
1484
- return concatBytes(utf8ToBytes("SigEd25519 no Ed25519 collisions"), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
1485
- }
1486
- twistedEdwards({ ...ed25519Defaults, domain: ed25519_domain });
1487
- twistedEdwards({
1488
- ...ed25519Defaults,
1489
- domain: ed25519_domain,
1490
- prehash: sha512
1491
- });
1492
- var ELL2_C1 = (Fp.ORDER + BigInt(3)) / BigInt(8);
1493
- Fp.pow(_2n4, ELL2_C1);
1494
- Fp.sqrt(Fp.neg(Fp.ONE));
1495
- (Fp.ORDER - BigInt(5)) / BigInt(8);
1496
- BigInt(486662);
1497
- FpSqrtEven(Fp, Fp.neg(BigInt(486664)));
1498
- BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
1499
- BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
1500
- BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
1501
- BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
1502
- BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
267
+ var modL_LE = (hash) => mod(b2n_LE(hash), N);
268
+ var _shaS;
269
+ var sha512a = (...m) => etc.sha512Async(...m);
270
+ var sha512s = (...m) => (
271
+ // Sync SHA512, not set by default
272
+ typeof _shaS === "function" ? _shaS(...m) : err("etc.sha512Sync not set")
273
+ );
274
+ var hash2extK = (hashed) => {
275
+ const head = hashed.slice(0, 32);
276
+ head[0] &= 248;
277
+ head[31] &= 127;
278
+ head[31] |= 64;
279
+ const prefix = hashed.slice(32, 64);
280
+ const scalar = modL_LE(head);
281
+ const point = G.mul(scalar);
282
+ const pointBytes = point.toRawBytes();
283
+ return { head, prefix, scalar, point, pointBytes };
284
+ };
285
+ var getExtendedPublicKeyAsync = (priv) => sha512a(toU8(priv, 32)).then(hash2extK);
286
+ var getExtendedPublicKey = (priv) => hash2extK(sha512s(toU8(priv, 32)));
287
+ var getPublicKeyAsync = (priv) => getExtendedPublicKeyAsync(priv).then((p) => p.pointBytes);
288
+ function hashFinish(asynchronous, res) {
289
+ if (asynchronous)
290
+ return sha512a(res.hashable).then(res.finish);
291
+ return res.finish(sha512s(res.hashable));
292
+ }
293
+ var _sign = (e, rBytes, msg) => {
294
+ const { pointBytes: P2, scalar: s } = e;
295
+ const r = modL_LE(rBytes);
296
+ const R = G.mul(r).toRawBytes();
297
+ const hashable = concatB(R, P2, msg);
298
+ const finish = (hashed) => {
299
+ const S = mod(r + modL_LE(hashed) * s, N);
300
+ return au8(concatB(R, n2b_32LE(S)), 64);
301
+ };
302
+ return { hashable, finish };
303
+ };
304
+ var signAsync = async (msg, privKey) => {
305
+ const m = toU8(msg);
306
+ const e = await getExtendedPublicKeyAsync(privKey);
307
+ const rBytes = await sha512a(e.prefix, m);
308
+ return hashFinish(true, _sign(e, rBytes, m));
309
+ };
310
+ var _verify = (sig, msg, pub) => {
311
+ msg = toU8(msg);
312
+ sig = toU8(sig, 64);
313
+ const A = Point.fromHex(pub, false);
314
+ const R = Point.fromHex(sig.slice(0, 32), false);
315
+ const s = b2n_LE(sig.slice(32, 64));
316
+ const SB = G.mul(s, false);
317
+ const hashable = concatB(R.toRawBytes(), A.toRawBytes(), msg);
318
+ const finish = (hashed) => {
319
+ const k = modL_LE(hashed);
320
+ const RkA = R.add(A.mul(k, false));
321
+ return RkA.add(SB.negate()).clearCofactor().is0();
322
+ };
323
+ return { hashable, finish };
324
+ };
325
+ var verifyAsync = async (s, m, p) => hashFinish(true, _verify(s, m, p));
326
+ var cr = () => (
327
+ // We support: 1) browsers 2) node.js 19+
328
+ typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0
329
+ );
330
+ var etc = {
331
+ bytesToHex: b2h,
332
+ hexToBytes: h2b,
333
+ concatBytes: concatB,
334
+ mod,
335
+ invert,
336
+ randomBytes: (len) => {
337
+ const crypto = cr();
338
+ if (!crypto)
339
+ err("crypto.getRandomValues must be defined");
340
+ return crypto.getRandomValues(u8n(len));
341
+ },
342
+ sha512Async: async (...messages) => {
343
+ const crypto = cr();
344
+ if (!crypto)
345
+ err("crypto.subtle or etc.sha512Async must be defined");
346
+ const m = concatB(...messages);
347
+ return u8n(await crypto.subtle.digest("SHA-512", m.buffer));
348
+ },
349
+ sha512Sync: void 0
350
+ // Actual logic below
351
+ };
352
+ Object.defineProperties(etc, { sha512Sync: {
353
+ configurable: false,
354
+ get() {
355
+ return _shaS;
356
+ },
357
+ set(f) {
358
+ if (!_shaS)
359
+ _shaS = f;
360
+ }
361
+ } });
362
+ var utils = {
363
+ getExtendedPublicKeyAsync,
364
+ getExtendedPublicKey,
365
+ randomPrivateKey: () => etc.randomBytes(32),
366
+ precompute(w = 8, p = G) {
367
+ p.multiply(3n);
368
+ return p;
369
+ }
370
+ // no-op
371
+ };
372
+ var W = 8;
373
+ var precompute = () => {
374
+ const points = [];
375
+ const windows = 256 / W + 1;
376
+ let p = G, b = p;
377
+ for (let w = 0; w < windows; w++) {
378
+ b = p;
379
+ points.push(b);
380
+ for (let i = 1; i < 2 ** (W - 1); i++) {
381
+ b = b.add(p);
382
+ points.push(b);
383
+ }
384
+ p = b.double();
385
+ }
386
+ return points;
387
+ };
388
+ var wNAF = (n) => {
389
+ const comp = Gpows || (Gpows = precompute());
390
+ const neg = (cnd, p2) => {
391
+ let n2 = p2.negate();
392
+ return cnd ? n2 : p2;
393
+ };
394
+ let p = I, f = G;
395
+ const windows = 1 + 256 / W;
396
+ const wsize = 2 ** (W - 1);
397
+ const mask = BigInt(2 ** W - 1);
398
+ const maxNum = 2 ** W;
399
+ const shiftBy = BigInt(W);
400
+ for (let w = 0; w < windows; w++) {
401
+ const off = w * wsize;
402
+ let wbits = Number(n & mask);
403
+ n >>= shiftBy;
404
+ if (wbits > wsize) {
405
+ wbits -= maxNum;
406
+ n += 1n;
407
+ }
408
+ const off1 = off, off2 = off + Math.abs(wbits) - 1;
409
+ const cnd1 = w % 2 !== 0, cnd2 = wbits < 0;
410
+ if (wbits === 0) {
411
+ f = f.add(neg(cnd1, comp[off1]));
412
+ } else {
413
+ p = p.add(neg(cnd2, comp[off2]));
414
+ }
415
+ }
416
+ return { p, f };
417
+ };
1503
418
 
1504
419
  // src/secrets.ts
1505
420
  var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
@@ -1510,15 +425,62 @@
1510
425
  "unwrapKey",
1511
426
  "wrapKey"
1512
427
  ]);
428
+ var ED25519_PKCS8_HEADER = (
429
+ // prettier-ignore
430
+ [
431
+ /**
432
+ * PKCS#8 header
433
+ */
434
+ 48,
435
+ // ASN.1 sequence tag
436
+ 46,
437
+ // Length of sequence (46 more bytes)
438
+ 2,
439
+ // ASN.1 integer tag
440
+ 1,
441
+ // Length of integer
442
+ 0,
443
+ // Version number
444
+ 48,
445
+ // ASN.1 sequence tag
446
+ 5,
447
+ // Length of sequence
448
+ 6,
449
+ // ASN.1 object identifier tag
450
+ 3,
451
+ // Length of object identifier
452
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
453
+ 43,
454
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
455
+ 101,
456
+ // thawte(101)
457
+ // Ed25519 identifier
458
+ 112,
459
+ // id-Ed25519(112)
460
+ /**
461
+ * Private key payload
462
+ */
463
+ 4,
464
+ // ASN.1 octet string tag
465
+ 34,
466
+ // String length (34 more bytes)
467
+ // Private key bytes as octet string
468
+ 4,
469
+ // ASN.1 octet string tag
470
+ 32
471
+ // String length (32 bytes)
472
+ ]
473
+ );
1513
474
  function bufferSourceToUint8Array(data) {
1514
475
  return data instanceof Uint8Array ? data : new Uint8Array(ArrayBuffer.isView(data) ? data.buffer : data);
1515
476
  }
1516
477
  var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
1517
- function createKeyPairFromBytes(bytes2, extractable, keyUsages) {
478
+ var publicKeyBytesStore;
479
+ function createKeyPairFromBytes(bytes, extractable, keyUsages) {
1518
480
  const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
1519
481
  const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
1520
- cache.set(keyPair.privateKey, bytes2);
1521
- cache.set(keyPair.publicKey, bytes2);
482
+ cache.set(keyPair.privateKey, bytes);
483
+ cache.set(keyPair.publicKey, bytes);
1522
484
  return keyPair;
1523
485
  }
1524
486
  function createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages) {
@@ -1550,58 +512,117 @@
1550
512
  });
1551
513
  }
1552
514
  function getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key) {
1553
- const secretKeyBytes = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.get(key);
515
+ const secretKeyBytes = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT == null ? void 0 : storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT.get(key);
1554
516
  if (secretKeyBytes === void 0) {
1555
517
  throw new Error("Could not find secret key material associated with this `CryptoKey`");
1556
518
  }
1557
519
  return secretKeyBytes;
1558
520
  }
1559
- function exportKeyPolyfill(format, key) {
521
+ async function getPublicKeyBytes(key) {
522
+ const publicKeyStore = publicKeyBytesStore || (publicKeyBytesStore = /* @__PURE__ */ new WeakMap());
523
+ const fromPublicStore = publicKeyStore.get(key);
524
+ if (fromPublicStore)
525
+ return fromPublicStore;
526
+ const publicKeyBytes = await getPublicKeyAsync(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
527
+ publicKeyStore.set(key, publicKeyBytes);
528
+ return publicKeyBytes;
529
+ }
530
+ async function exportKeyPolyfill(format, key) {
531
+ if (key.extractable === false) {
532
+ throw new DOMException("key is not extractable", "InvalidAccessException");
533
+ }
1560
534
  switch (format) {
1561
535
  case "raw": {
1562
- if (key.extractable === false) {
1563
- throw new DOMException("key is not extractable", "InvalidAccessException");
1564
- }
1565
536
  if (key.type !== "public") {
1566
537
  throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
1567
538
  }
1568
- const publicKeyBytes = ed25519.getPublicKey(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
539
+ const publicKeyBytes = await getPublicKeyBytes(key);
1569
540
  return publicKeyBytes;
1570
541
  }
542
+ case "pkcs8": {
543
+ if (key.type !== "private") {
544
+ throw new DOMException(`Unable to export a pkcs8 Ed25519 ${key.type} key`, "InvalidAccessError");
545
+ }
546
+ const secretKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
547
+ return new Uint8Array([...ED25519_PKCS8_HEADER, ...secretKeyBytes]);
548
+ }
1571
549
  default:
1572
550
  throw new Error(`Exporting polyfilled Ed25519 keys in the "${format}" format is unimplemented`);
1573
551
  }
1574
552
  }
1575
553
  function generateKeyPolyfill(extractable, keyUsages) {
1576
- const privateKeyBytes = ed25519.utils.randomPrivateKey();
554
+ const privateKeyBytes = utils.randomPrivateKey();
1577
555
  const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
1578
556
  return keyPair;
1579
557
  }
1580
558
  function isPolyfilledKey(key) {
1581
- return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key);
559
+ return !!(storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT == null ? void 0 : storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT.has(key)) || !!(publicKeyBytesStore == null ? void 0 : publicKeyBytesStore.has(key));
1582
560
  }
1583
- function signPolyfill(key, data) {
561
+ async function signPolyfill(key, data) {
1584
562
  if (key.type !== "private" || !key.usages.includes("sign")) {
1585
563
  throw new DOMException("Unable to use this key to sign", "InvalidAccessError");
1586
564
  }
1587
565
  const privateKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
1588
566
  const payload = bufferSourceToUint8Array(data);
1589
- const signature = ed25519.sign(payload, privateKeyBytes);
567
+ const signature = await signAsync(payload, privateKeyBytes);
1590
568
  return signature;
1591
569
  }
1592
- function verifyPolyfill(key, signature, data) {
570
+ async function verifyPolyfill(key, signature, data) {
1593
571
  if (key.type !== "public" || !key.usages.includes("verify")) {
1594
572
  throw new DOMException("Unable to use this key to verify", "InvalidAccessError");
1595
573
  }
1596
- const publicKeyBytes = ed25519.getPublicKey(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
574
+ const publicKeyBytes = await getPublicKeyBytes(key);
1597
575
  try {
1598
- return ed25519.verify(bufferSourceToUint8Array(signature), bufferSourceToUint8Array(data), publicKeyBytes);
576
+ return await verifyAsync(bufferSourceToUint8Array(signature), bufferSourceToUint8Array(data), publicKeyBytes);
1599
577
  } catch {
1600
578
  return false;
1601
579
  }
1602
580
  }
1603
-
1604
- // src/index.ts
581
+ function importKeyPolyfill(format, keyData, extractable, keyUsages) {
582
+ const bytes = bufferSourceToUint8Array(keyData);
583
+ if (format === "raw") {
584
+ if (keyUsages.some((usage) => usage === "sign" || PROHIBITED_KEY_USAGES.has(usage))) {
585
+ throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
586
+ }
587
+ if (bytes.length !== 32) {
588
+ throw new DOMException("Ed25519 raw keys must be exactly 32-bytes", "DataError");
589
+ }
590
+ const publicKey = {
591
+ [Symbol.toStringTag]: "CryptoKey",
592
+ algorithm: Object.freeze({ name: "Ed25519" }),
593
+ extractable,
594
+ type: "public",
595
+ usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
596
+ };
597
+ const cache = publicKeyBytesStore || (publicKeyBytesStore = /* @__PURE__ */ new WeakMap());
598
+ cache.set(publicKey, bytes);
599
+ return publicKey;
600
+ }
601
+ if (format === "pkcs8") {
602
+ if (keyUsages.some((usage) => usage === "verify" || PROHIBITED_KEY_USAGES.has(usage))) {
603
+ throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
604
+ }
605
+ if (bytes.length !== 48) {
606
+ throw new DOMException("Invalid keyData", "DataError");
607
+ }
608
+ const header = bytes.slice(0, 16);
609
+ if (!header.every((val, i) => val === ED25519_PKCS8_HEADER[i])) {
610
+ throw new DOMException("Invalid keyData", "DataError");
611
+ }
612
+ const secretKeyBytes = bytes.slice(16);
613
+ const privateKey = {
614
+ [Symbol.toStringTag]: "CryptoKey",
615
+ algorithm: Object.freeze({ name: "Ed25519" }),
616
+ extractable,
617
+ type: "private",
618
+ usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
619
+ };
620
+ const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
621
+ cache.set(privateKey, secretKeyBytes);
622
+ return privateKey;
623
+ }
624
+ throw new Error(`Importing Ed25519 keys in the "${format}" format is unimplemented`);
625
+ }
1605
626
  if (globalThis.isSecureContext) {
1606
627
  const originalCryptoObject = globalThis.crypto || (globalThis.crypto = {});
1607
628
  const originalSubtleCrypto = originalCryptoObject.subtle || (originalCryptoObject.subtle = {});
@@ -1609,7 +630,7 @@
1609
630
  originalSubtleCrypto.exportKey = async (...args) => {
1610
631
  const [_, key] = args;
1611
632
  if (isPolyfilledKey(key)) {
1612
- return exportKeyPolyfill(...args);
633
+ return await exportKeyPolyfill(...args);
1613
634
  } else if (originalExportKey) {
1614
635
  return await originalExportKey.apply(originalSubtleCrypto, args);
1615
636
  } else {
@@ -1668,7 +689,7 @@
1668
689
  const [_, key] = args;
1669
690
  if (isPolyfilledKey(key)) {
1670
691
  const [_2, ...rest] = args;
1671
- return signPolyfill(...rest);
692
+ return await signPolyfill(...rest);
1672
693
  } else if (originalSign) {
1673
694
  return await originalSign.apply(originalSubtleCrypto, args);
1674
695
  } else {
@@ -1680,36 +701,65 @@
1680
701
  const [_, key] = args;
1681
702
  if (isPolyfilledKey(key)) {
1682
703
  const [_2, ...rest] = args;
1683
- return verifyPolyfill(...rest);
704
+ return await verifyPolyfill(...rest);
1684
705
  } else if (originalVerify) {
1685
706
  return await originalVerify.apply(originalSubtleCrypto, args);
1686
707
  } else {
1687
708
  throw new TypeError("No native `verify` function exists to handle this call");
1688
709
  }
1689
710
  };
711
+ const originalImportKey = originalSubtleCrypto.importKey;
712
+ let originalImportKeySupportsEd25519;
713
+ originalSubtleCrypto.importKey = async (...args) => {
714
+ const [format, keyData, algorithm] = args;
715
+ if (algorithm !== "Ed25519") {
716
+ if (originalImportKey) {
717
+ return await originalImportKey.apply(originalSubtleCrypto, args);
718
+ } else {
719
+ throw new TypeError("No native `importKey` function exists to handle this call");
720
+ }
721
+ }
722
+ let optimisticallyImportedKey;
723
+ if (originalImportKeySupportsEd25519 === void 0) {
724
+ originalImportKeySupportsEd25519 = new Promise((resolve) => {
725
+ if (!originalImportKey) {
726
+ resolve(originalImportKeySupportsEd25519 = false);
727
+ return;
728
+ }
729
+ originalImportKey.apply(originalSubtleCrypto, args).then((key) => {
730
+ {
731
+ console.warn(
732
+ "`@solana/webcrypto-ed25519-polyfill` was included in an environment that supports Ed25519 key manipulation natively. Falling back to the native implementation. Consider including this polyfill only in environments where Ed25519 is not supported."
733
+ );
734
+ }
735
+ if (originalSubtleCrypto.importKey !== originalImportKey) {
736
+ originalSubtleCrypto.importKey = originalImportKey;
737
+ }
738
+ optimisticallyImportedKey = key;
739
+ resolve(originalImportKeySupportsEd25519 = true);
740
+ }).catch(() => {
741
+ resolve(originalImportKeySupportsEd25519 = false);
742
+ });
743
+ });
744
+ }
745
+ if (typeof originalImportKey === "boolean" ? originalImportKeySupportsEd25519 : await originalImportKeySupportsEd25519) {
746
+ if (optimisticallyImportedKey) {
747
+ return optimisticallyImportedKey;
748
+ } else if (originalImportKey) {
749
+ return await originalImportKey.apply(originalSubtleCrypto, args);
750
+ } else {
751
+ throw new TypeError("No native `importKey` function exists to handle this call");
752
+ }
753
+ } else {
754
+ const [_format, _keyData, _algorithm, extractable, keyUsages] = args;
755
+ return importKeyPolyfill(format, keyData, extractable, keyUsages);
756
+ }
757
+ };
1690
758
  }
1691
759
  /*! Bundled license information:
1692
760
 
1693
- @noble/hashes/esm/utils.js:
1694
- (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1695
-
1696
- @noble/curves/esm/abstract/utils.js:
1697
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1698
-
1699
- @noble/curves/esm/abstract/modular.js:
1700
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1701
-
1702
- @noble/curves/esm/abstract/curve.js:
1703
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1704
-
1705
- @noble/curves/esm/abstract/edwards.js:
1706
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1707
-
1708
- @noble/curves/esm/abstract/montgomery.js:
1709
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1710
-
1711
- @noble/curves/esm/ed25519.js:
1712
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
761
+ @noble/ed25519/index.js:
762
+ (*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
1713
763
  */
1714
764
 
1715
765
  })();