@corewire/core 0.0.0 → 1.0.0-next.1

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,3093 @@
1
+ import { n as __exportAll } from "./chunk-wE0WeVzl.mjs";
2
+ import { c as DecryptionError } from "./errors-DVv1n4fL.mjs";
3
+ import { createCipheriv, createDecipheriv, hkdfSync } from "node:crypto";
4
+
5
+ //#region src/crypto/aes.ts
6
+ var aes_exports = /* @__PURE__ */ __exportAll({
7
+ decrypt: () => decrypt$1,
8
+ encrypt: () => encrypt$1
9
+ });
10
+ /**
11
+ * AES-128-CTR fallback for legacy HAP pairing. It has no authentication tag; the pairing flow relies on Ed25519 signatures in the TLV payload.
12
+ *
13
+ * @param key - 16-byte AES key.
14
+ * @param iv - 16-byte initialization vector.
15
+ */
16
+ function encrypt$1(key, iv, plaintext) {
17
+ const cipher = createCipheriv("aes-128-ctr", key, iv);
18
+ return Buffer.concat([cipher.update(plaintext), cipher.final()]);
19
+ }
20
+ /**
21
+ * Decrypts data using AES-128-CTR.
22
+ *
23
+ * @param key - 16-byte AES key.
24
+ * @param iv - 16-byte initialization vector.
25
+ */
26
+ function decrypt$1(key, iv, ciphertext) {
27
+ const decipher = createDecipheriv("aes-128-ctr", key, iv);
28
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
29
+ }
30
+
31
+ //#endregion
32
+ //#region ../../node_modules/.bun/@stablelib+int@2.0.1/node_modules/@stablelib/int/lib/int.js
33
+ /**
34
+ * Returns true if the argument is an integer number.
35
+ */
36
+ const isInteger = Number.isInteger;
37
+ /**
38
+ * Math.pow(2, 53) - 1
39
+ */
40
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
41
+ /**
42
+ * Returns true if the argument is a safe integer number
43
+ * (-MIN_SAFE_INTEGER < number <= MAX_SAFE_INTEGER)
44
+ */
45
+ const isSafeInteger = Number.isSafeInteger;
46
+
47
+ //#endregion
48
+ //#region ../../node_modules/.bun/@stablelib+binary@2.0.1/node_modules/@stablelib/binary/lib/binary.js
49
+ /**
50
+ * Reads 4 bytes from array starting at offset as big-endian
51
+ * unsigned 32-bit integer and returns it.
52
+ */
53
+ function readUint32BE(array, offset = 0) {
54
+ return (array[offset] << 24 | array[offset + 1] << 16 | array[offset + 2] << 8 | array[offset + 3]) >>> 0;
55
+ }
56
+ /**
57
+ * Writes 4-byte big-endian representation of 32-bit unsigned
58
+ * value to byte array starting at offset.
59
+ *
60
+ * If byte array is not given, creates a new 4-byte one.
61
+ *
62
+ * Returns the output byte array.
63
+ */
64
+ function writeUint32BE(value, out = new Uint8Array(4), offset = 0) {
65
+ out[offset + 0] = value >>> 24;
66
+ out[offset + 1] = value >>> 16;
67
+ out[offset + 2] = value >>> 8;
68
+ out[offset + 3] = value >>> 0;
69
+ return out;
70
+ }
71
+ /**
72
+ * Writes 4-byte little-endian representation of 32-bit unsigned
73
+ * value to array starting at offset.
74
+ *
75
+ * If byte array is not given, creates a new 4-byte one.
76
+ *
77
+ * Returns the output byte array.
78
+ */
79
+ function writeUint32LE(value, out = new Uint8Array(4), offset = 0) {
80
+ out[offset + 0] = value >>> 0;
81
+ out[offset + 1] = value >>> 8;
82
+ out[offset + 2] = value >>> 16;
83
+ out[offset + 3] = value >>> 24;
84
+ return out;
85
+ }
86
+ /**
87
+ * Writes 8-byte little-endian representation of 64-bit unsigned
88
+ * value to byte array starting at offset.
89
+ *
90
+ * Due to JavaScript limitation, supports values up to 2^53-1.
91
+ *
92
+ * If byte array is not given, creates a new 8-byte one.
93
+ *
94
+ * Returns the output byte array.
95
+ */
96
+ function writeUint64LE(value, out = new Uint8Array(8), offset = 0) {
97
+ writeUint32LE(value >>> 0, out, offset);
98
+ writeUint32LE(value / 4294967296 >>> 0, out, offset + 4);
99
+ return out;
100
+ }
101
+
102
+ //#endregion
103
+ //#region ../../node_modules/.bun/@stablelib+wipe@2.0.1/node_modules/@stablelib/wipe/lib/wipe.js
104
+ /**
105
+ * Sets all values in the given array to zero and returns it.
106
+ *
107
+ * The fact that it sets bytes to zero can be relied on.
108
+ *
109
+ * There is no guarantee that this function makes data disappear from memory,
110
+ * as runtime implementation can, for example, have copying garbage collector
111
+ * that will make copies of sensitive data before we wipe it. Or that an
112
+ * operating system will write our data to swap or sleep image. Another thing
113
+ * is that an optimizing compiler can remove calls to this function or make it
114
+ * no-op. There's nothing we can do with it, so we just do our best and hope
115
+ * that everything will be okay and good will triumph over evil.
116
+ */
117
+ function wipe(array) {
118
+ for (let i = 0; i < array.length; i++) array[i] = 0;
119
+ return array;
120
+ }
121
+
122
+ //#endregion
123
+ //#region ../../node_modules/.bun/@stablelib+chacha@2.0.1/node_modules/@stablelib/chacha/lib/chacha.js
124
+ /**
125
+ * Package chacha implements ChaCha stream cipher.
126
+ */
127
+ const ROUNDS = 20;
128
+ function core(out, input, key) {
129
+ let j0 = 1634760805;
130
+ let j1 = 857760878;
131
+ let j2 = 2036477234;
132
+ let j3 = 1797285236;
133
+ let j4 = key[3] << 24 | key[2] << 16 | key[1] << 8 | key[0];
134
+ let j5 = key[7] << 24 | key[6] << 16 | key[5] << 8 | key[4];
135
+ let j6 = key[11] << 24 | key[10] << 16 | key[9] << 8 | key[8];
136
+ let j7 = key[15] << 24 | key[14] << 16 | key[13] << 8 | key[12];
137
+ let j8 = key[19] << 24 | key[18] << 16 | key[17] << 8 | key[16];
138
+ let j9 = key[23] << 24 | key[22] << 16 | key[21] << 8 | key[20];
139
+ let j10 = key[27] << 24 | key[26] << 16 | key[25] << 8 | key[24];
140
+ let j11 = key[31] << 24 | key[30] << 16 | key[29] << 8 | key[28];
141
+ let j12 = input[3] << 24 | input[2] << 16 | input[1] << 8 | input[0];
142
+ let j13 = input[7] << 24 | input[6] << 16 | input[5] << 8 | input[4];
143
+ let j14 = input[11] << 24 | input[10] << 16 | input[9] << 8 | input[8];
144
+ let j15 = input[15] << 24 | input[14] << 16 | input[13] << 8 | input[12];
145
+ let x0 = j0;
146
+ let x1 = j1;
147
+ let x2 = j2;
148
+ let x3 = j3;
149
+ let x4 = j4;
150
+ let x5 = j5;
151
+ let x6 = j6;
152
+ let x7 = j7;
153
+ let x8 = j8;
154
+ let x9 = j9;
155
+ let x10 = j10;
156
+ let x11 = j11;
157
+ let x12 = j12;
158
+ let x13 = j13;
159
+ let x14 = j14;
160
+ let x15 = j15;
161
+ for (let i = 0; i < ROUNDS; i += 2) {
162
+ x0 = x0 + x4 | 0;
163
+ x12 ^= x0;
164
+ x12 = x12 >>> 16 | x12 << 16;
165
+ x8 = x8 + x12 | 0;
166
+ x4 ^= x8;
167
+ x4 = x4 >>> 20 | x4 << 12;
168
+ x1 = x1 + x5 | 0;
169
+ x13 ^= x1;
170
+ x13 = x13 >>> 16 | x13 << 16;
171
+ x9 = x9 + x13 | 0;
172
+ x5 ^= x9;
173
+ x5 = x5 >>> 20 | x5 << 12;
174
+ x2 = x2 + x6 | 0;
175
+ x14 ^= x2;
176
+ x14 = x14 >>> 16 | x14 << 16;
177
+ x10 = x10 + x14 | 0;
178
+ x6 ^= x10;
179
+ x6 = x6 >>> 20 | x6 << 12;
180
+ x3 = x3 + x7 | 0;
181
+ x15 ^= x3;
182
+ x15 = x15 >>> 16 | x15 << 16;
183
+ x11 = x11 + x15 | 0;
184
+ x7 ^= x11;
185
+ x7 = x7 >>> 20 | x7 << 12;
186
+ x2 = x2 + x6 | 0;
187
+ x14 ^= x2;
188
+ x14 = x14 >>> 24 | x14 << 8;
189
+ x10 = x10 + x14 | 0;
190
+ x6 ^= x10;
191
+ x6 = x6 >>> 25 | x6 << 7;
192
+ x3 = x3 + x7 | 0;
193
+ x15 ^= x3;
194
+ x15 = x15 >>> 24 | x15 << 8;
195
+ x11 = x11 + x15 | 0;
196
+ x7 ^= x11;
197
+ x7 = x7 >>> 25 | x7 << 7;
198
+ x1 = x1 + x5 | 0;
199
+ x13 ^= x1;
200
+ x13 = x13 >>> 24 | x13 << 8;
201
+ x9 = x9 + x13 | 0;
202
+ x5 ^= x9;
203
+ x5 = x5 >>> 25 | x5 << 7;
204
+ x0 = x0 + x4 | 0;
205
+ x12 ^= x0;
206
+ x12 = x12 >>> 24 | x12 << 8;
207
+ x8 = x8 + x12 | 0;
208
+ x4 ^= x8;
209
+ x4 = x4 >>> 25 | x4 << 7;
210
+ x0 = x0 + x5 | 0;
211
+ x15 ^= x0;
212
+ x15 = x15 >>> 16 | x15 << 16;
213
+ x10 = x10 + x15 | 0;
214
+ x5 ^= x10;
215
+ x5 = x5 >>> 20 | x5 << 12;
216
+ x1 = x1 + x6 | 0;
217
+ x12 ^= x1;
218
+ x12 = x12 >>> 16 | x12 << 16;
219
+ x11 = x11 + x12 | 0;
220
+ x6 ^= x11;
221
+ x6 = x6 >>> 20 | x6 << 12;
222
+ x2 = x2 + x7 | 0;
223
+ x13 ^= x2;
224
+ x13 = x13 >>> 16 | x13 << 16;
225
+ x8 = x8 + x13 | 0;
226
+ x7 ^= x8;
227
+ x7 = x7 >>> 20 | x7 << 12;
228
+ x3 = x3 + x4 | 0;
229
+ x14 ^= x3;
230
+ x14 = x14 >>> 16 | x14 << 16;
231
+ x9 = x9 + x14 | 0;
232
+ x4 ^= x9;
233
+ x4 = x4 >>> 20 | x4 << 12;
234
+ x2 = x2 + x7 | 0;
235
+ x13 ^= x2;
236
+ x13 = x13 >>> 24 | x13 << 8;
237
+ x8 = x8 + x13 | 0;
238
+ x7 ^= x8;
239
+ x7 = x7 >>> 25 | x7 << 7;
240
+ x3 = x3 + x4 | 0;
241
+ x14 ^= x3;
242
+ x14 = x14 >>> 24 | x14 << 8;
243
+ x9 = x9 + x14 | 0;
244
+ x4 ^= x9;
245
+ x4 = x4 >>> 25 | x4 << 7;
246
+ x1 = x1 + x6 | 0;
247
+ x12 ^= x1;
248
+ x12 = x12 >>> 24 | x12 << 8;
249
+ x11 = x11 + x12 | 0;
250
+ x6 ^= x11;
251
+ x6 = x6 >>> 25 | x6 << 7;
252
+ x0 = x0 + x5 | 0;
253
+ x15 ^= x0;
254
+ x15 = x15 >>> 24 | x15 << 8;
255
+ x10 = x10 + x15 | 0;
256
+ x5 ^= x10;
257
+ x5 = x5 >>> 25 | x5 << 7;
258
+ }
259
+ writeUint32LE(x0 + j0 | 0, out, 0);
260
+ writeUint32LE(x1 + j1 | 0, out, 4);
261
+ writeUint32LE(x2 + j2 | 0, out, 8);
262
+ writeUint32LE(x3 + j3 | 0, out, 12);
263
+ writeUint32LE(x4 + j4 | 0, out, 16);
264
+ writeUint32LE(x5 + j5 | 0, out, 20);
265
+ writeUint32LE(x6 + j6 | 0, out, 24);
266
+ writeUint32LE(x7 + j7 | 0, out, 28);
267
+ writeUint32LE(x8 + j8 | 0, out, 32);
268
+ writeUint32LE(x9 + j9 | 0, out, 36);
269
+ writeUint32LE(x10 + j10 | 0, out, 40);
270
+ writeUint32LE(x11 + j11 | 0, out, 44);
271
+ writeUint32LE(x12 + j12 | 0, out, 48);
272
+ writeUint32LE(x13 + j13 | 0, out, 52);
273
+ writeUint32LE(x14 + j14 | 0, out, 56);
274
+ writeUint32LE(x15 + j15 | 0, out, 60);
275
+ }
276
+ /**
277
+ * Encrypt src with ChaCha20 stream generated for the given 32-byte key and
278
+ * 8-byte (as in original implementation) or 12-byte (as in RFC7539) nonce and
279
+ * write the result into dst and return it.
280
+ *
281
+ * dst and src may be the same, but otherwise must not overlap.
282
+ *
283
+ * If nonce is 12 bytes, users should not encrypt more than 256 GiB with the
284
+ * same key and nonce, otherwise the stream will repeat. The function will
285
+ * throw error if counter overflows to prevent this.
286
+ *
287
+ * If nonce is 8 bytes, the output is practically unlimited (2^70 bytes, which
288
+ * is more than a million petabytes). However, it is not recommended to
289
+ * generate 8-byte nonces randomly, as the chance of collision is high.
290
+ *
291
+ * Never use the same key and nonce to encrypt more than one message.
292
+ *
293
+ * If nonceInplaceCounterLength is not 0, the nonce is assumed to be a 16-byte
294
+ * array with stream counter in first nonceInplaceCounterLength bytes and nonce
295
+ * in the last remaining bytes. The counter will be incremented inplace for
296
+ * each ChaCha block. This is useful if you need to encrypt one stream of data
297
+ * in chunks.
298
+ */
299
+ function streamXOR(key, nonce, src, dst, nonceInplaceCounterLength = 0) {
300
+ if (key.length !== 32) throw new Error("ChaCha: key size must be 32 bytes");
301
+ if (dst.length < src.length) throw new Error("ChaCha: destination is shorter than source");
302
+ let nc;
303
+ let counterLength;
304
+ if (nonceInplaceCounterLength === 0) {
305
+ if (nonce.length !== 8 && nonce.length !== 12) throw new Error("ChaCha nonce must be 8 or 12 bytes");
306
+ nc = new Uint8Array(16);
307
+ counterLength = nc.length - nonce.length;
308
+ nc.set(nonce, counterLength);
309
+ } else {
310
+ if (nonce.length !== 16) throw new Error("ChaCha nonce with counter must be 16 bytes");
311
+ nc = nonce;
312
+ counterLength = nonceInplaceCounterLength;
313
+ }
314
+ const block = new Uint8Array(64);
315
+ for (let i = 0; i < src.length; i += 64) {
316
+ core(block, nc, key);
317
+ for (let j = i; j < i + 64 && j < src.length; j++) dst[j] = src[j] ^ block[j - i];
318
+ incrementCounter(nc, 0, counterLength);
319
+ }
320
+ wipe(block);
321
+ if (nonceInplaceCounterLength === 0) wipe(nc);
322
+ return dst;
323
+ }
324
+ /**
325
+ * Generate ChaCha20 stream for the given 32-byte key and 8-byte or 12-byte
326
+ * nonce and write it into dst and return it.
327
+ *
328
+ * Never use the same key and nonce to generate more than one stream.
329
+ *
330
+ * If nonceInplaceCounterLength is not 0, it behaves the same with respect to
331
+ * the nonce as described in the streamXOR documentation.
332
+ *
333
+ * stream is like streamXOR with all-zero src.
334
+ */
335
+ function stream(key, nonce, dst, nonceInplaceCounterLength = 0) {
336
+ wipe(dst);
337
+ return streamXOR(key, nonce, dst, dst, nonceInplaceCounterLength);
338
+ }
339
+ function incrementCounter(counter, pos, len) {
340
+ let carry = 1;
341
+ while (len--) {
342
+ carry = carry + (counter[pos] & 255) | 0;
343
+ counter[pos] = carry & 255;
344
+ carry >>>= 8;
345
+ pos++;
346
+ }
347
+ if (carry > 0) throw new Error("ChaCha: counter overflow");
348
+ }
349
+
350
+ //#endregion
351
+ //#region ../../node_modules/.bun/@stablelib+constant-time@2.0.1/node_modules/@stablelib/constant-time/lib/constant-time.js
352
+ /**
353
+ * Returns 1 if a and b are of equal length and their contents
354
+ * are equal, or 0 otherwise.
355
+ *
356
+ * Note that unlike in equal(), zero-length inputs are considered
357
+ * the same, so this function will return 1.
358
+ */
359
+ function compare(a, b) {
360
+ if (a.length !== b.length) return 0;
361
+ let result = 0;
362
+ for (let i = 0; i < a.length; i++) result |= a[i] ^ b[i];
363
+ return 1 & result - 1 >>> 8;
364
+ }
365
+ /**
366
+ * Returns true if a and b are of equal non-zero length,
367
+ * and their contents are equal, or false otherwise.
368
+ *
369
+ * Note that unlike in compare() zero-length inputs are considered
370
+ * _not_ equal, so this function will return false.
371
+ */
372
+ function equal(a, b) {
373
+ if (a.length === 0 || b.length === 0) return false;
374
+ return compare(a, b) !== 0;
375
+ }
376
+
377
+ //#endregion
378
+ //#region ../../node_modules/.bun/@stablelib+poly1305@2.0.1/node_modules/@stablelib/poly1305/lib/poly1305.js
379
+ const DIGEST_LENGTH$1 = 16;
380
+ /**
381
+ * Poly1305 computes 16-byte authenticator of message using
382
+ * a one-time 32-byte key.
383
+ *
384
+ * Important: key should be used for only one message,
385
+ * it should never repeat.
386
+ */
387
+ var Poly1305 = class {
388
+ digestLength = 16;
389
+ _buffer = new Uint8Array(16);
390
+ _r = new Uint16Array(10);
391
+ _h = new Uint16Array(10);
392
+ _pad = new Uint16Array(8);
393
+ _leftover = 0;
394
+ _fin = 0;
395
+ _finished = false;
396
+ constructor(key) {
397
+ let t0 = key[0] | key[1] << 8;
398
+ this._r[0] = t0 & 8191;
399
+ let t1 = key[2] | key[3] << 8;
400
+ this._r[1] = (t0 >>> 13 | t1 << 3) & 8191;
401
+ let t2 = key[4] | key[5] << 8;
402
+ this._r[2] = (t1 >>> 10 | t2 << 6) & 7939;
403
+ let t3 = key[6] | key[7] << 8;
404
+ this._r[3] = (t2 >>> 7 | t3 << 9) & 8191;
405
+ let t4 = key[8] | key[9] << 8;
406
+ this._r[4] = (t3 >>> 4 | t4 << 12) & 255;
407
+ this._r[5] = t4 >>> 1 & 8190;
408
+ let t5 = key[10] | key[11] << 8;
409
+ this._r[6] = (t4 >>> 14 | t5 << 2) & 8191;
410
+ let t6 = key[12] | key[13] << 8;
411
+ this._r[7] = (t5 >>> 11 | t6 << 5) & 8065;
412
+ let t7 = key[14] | key[15] << 8;
413
+ this._r[8] = (t6 >>> 8 | t7 << 8) & 8191;
414
+ this._r[9] = t7 >>> 5 & 127;
415
+ this._pad[0] = key[16] | key[17] << 8;
416
+ this._pad[1] = key[18] | key[19] << 8;
417
+ this._pad[2] = key[20] | key[21] << 8;
418
+ this._pad[3] = key[22] | key[23] << 8;
419
+ this._pad[4] = key[24] | key[25] << 8;
420
+ this._pad[5] = key[26] | key[27] << 8;
421
+ this._pad[6] = key[28] | key[29] << 8;
422
+ this._pad[7] = key[30] | key[31] << 8;
423
+ }
424
+ _blocks(m, mpos, bytes) {
425
+ let hibit = this._fin ? 0 : 2048;
426
+ let h0 = this._h[0], h1 = this._h[1], h2 = this._h[2], h3 = this._h[3], h4 = this._h[4], h5 = this._h[5], h6 = this._h[6], h7 = this._h[7], h8 = this._h[8], h9 = this._h[9];
427
+ let r0 = this._r[0], r1 = this._r[1], r2 = this._r[2], r3 = this._r[3], r4 = this._r[4], r5 = this._r[5], r6 = this._r[6], r7 = this._r[7], r8 = this._r[8], r9 = this._r[9];
428
+ while (bytes >= 16) {
429
+ let t0 = m[mpos + 0] | m[mpos + 1] << 8;
430
+ h0 += t0 & 8191;
431
+ let t1 = m[mpos + 2] | m[mpos + 3] << 8;
432
+ h1 += (t0 >>> 13 | t1 << 3) & 8191;
433
+ let t2 = m[mpos + 4] | m[mpos + 5] << 8;
434
+ h2 += (t1 >>> 10 | t2 << 6) & 8191;
435
+ let t3 = m[mpos + 6] | m[mpos + 7] << 8;
436
+ h3 += (t2 >>> 7 | t3 << 9) & 8191;
437
+ let t4 = m[mpos + 8] | m[mpos + 9] << 8;
438
+ h4 += (t3 >>> 4 | t4 << 12) & 8191;
439
+ h5 += t4 >>> 1 & 8191;
440
+ let t5 = m[mpos + 10] | m[mpos + 11] << 8;
441
+ h6 += (t4 >>> 14 | t5 << 2) & 8191;
442
+ let t6 = m[mpos + 12] | m[mpos + 13] << 8;
443
+ h7 += (t5 >>> 11 | t6 << 5) & 8191;
444
+ let t7 = m[mpos + 14] | m[mpos + 15] << 8;
445
+ h8 += (t6 >>> 8 | t7 << 8) & 8191;
446
+ h9 += t7 >>> 5 | hibit;
447
+ let c = 0;
448
+ let d0 = c;
449
+ d0 += h0 * r0;
450
+ d0 += h1 * (5 * r9);
451
+ d0 += h2 * (5 * r8);
452
+ d0 += h3 * (5 * r7);
453
+ d0 += h4 * (5 * r6);
454
+ c = d0 >>> 13;
455
+ d0 &= 8191;
456
+ d0 += h5 * (5 * r5);
457
+ d0 += h6 * (5 * r4);
458
+ d0 += h7 * (5 * r3);
459
+ d0 += h8 * (5 * r2);
460
+ d0 += h9 * (5 * r1);
461
+ c += d0 >>> 13;
462
+ d0 &= 8191;
463
+ let d1 = c;
464
+ d1 += h0 * r1;
465
+ d1 += h1 * r0;
466
+ d1 += h2 * (5 * r9);
467
+ d1 += h3 * (5 * r8);
468
+ d1 += h4 * (5 * r7);
469
+ c = d1 >>> 13;
470
+ d1 &= 8191;
471
+ d1 += h5 * (5 * r6);
472
+ d1 += h6 * (5 * r5);
473
+ d1 += h7 * (5 * r4);
474
+ d1 += h8 * (5 * r3);
475
+ d1 += h9 * (5 * r2);
476
+ c += d1 >>> 13;
477
+ d1 &= 8191;
478
+ let d2 = c;
479
+ d2 += h0 * r2;
480
+ d2 += h1 * r1;
481
+ d2 += h2 * r0;
482
+ d2 += h3 * (5 * r9);
483
+ d2 += h4 * (5 * r8);
484
+ c = d2 >>> 13;
485
+ d2 &= 8191;
486
+ d2 += h5 * (5 * r7);
487
+ d2 += h6 * (5 * r6);
488
+ d2 += h7 * (5 * r5);
489
+ d2 += h8 * (5 * r4);
490
+ d2 += h9 * (5 * r3);
491
+ c += d2 >>> 13;
492
+ d2 &= 8191;
493
+ let d3 = c;
494
+ d3 += h0 * r3;
495
+ d3 += h1 * r2;
496
+ d3 += h2 * r1;
497
+ d3 += h3 * r0;
498
+ d3 += h4 * (5 * r9);
499
+ c = d3 >>> 13;
500
+ d3 &= 8191;
501
+ d3 += h5 * (5 * r8);
502
+ d3 += h6 * (5 * r7);
503
+ d3 += h7 * (5 * r6);
504
+ d3 += h8 * (5 * r5);
505
+ d3 += h9 * (5 * r4);
506
+ c += d3 >>> 13;
507
+ d3 &= 8191;
508
+ let d4 = c;
509
+ d4 += h0 * r4;
510
+ d4 += h1 * r3;
511
+ d4 += h2 * r2;
512
+ d4 += h3 * r1;
513
+ d4 += h4 * r0;
514
+ c = d4 >>> 13;
515
+ d4 &= 8191;
516
+ d4 += h5 * (5 * r9);
517
+ d4 += h6 * (5 * r8);
518
+ d4 += h7 * (5 * r7);
519
+ d4 += h8 * (5 * r6);
520
+ d4 += h9 * (5 * r5);
521
+ c += d4 >>> 13;
522
+ d4 &= 8191;
523
+ let d5 = c;
524
+ d5 += h0 * r5;
525
+ d5 += h1 * r4;
526
+ d5 += h2 * r3;
527
+ d5 += h3 * r2;
528
+ d5 += h4 * r1;
529
+ c = d5 >>> 13;
530
+ d5 &= 8191;
531
+ d5 += h5 * r0;
532
+ d5 += h6 * (5 * r9);
533
+ d5 += h7 * (5 * r8);
534
+ d5 += h8 * (5 * r7);
535
+ d5 += h9 * (5 * r6);
536
+ c += d5 >>> 13;
537
+ d5 &= 8191;
538
+ let d6 = c;
539
+ d6 += h0 * r6;
540
+ d6 += h1 * r5;
541
+ d6 += h2 * r4;
542
+ d6 += h3 * r3;
543
+ d6 += h4 * r2;
544
+ c = d6 >>> 13;
545
+ d6 &= 8191;
546
+ d6 += h5 * r1;
547
+ d6 += h6 * r0;
548
+ d6 += h7 * (5 * r9);
549
+ d6 += h8 * (5 * r8);
550
+ d6 += h9 * (5 * r7);
551
+ c += d6 >>> 13;
552
+ d6 &= 8191;
553
+ let d7 = c;
554
+ d7 += h0 * r7;
555
+ d7 += h1 * r6;
556
+ d7 += h2 * r5;
557
+ d7 += h3 * r4;
558
+ d7 += h4 * r3;
559
+ c = d7 >>> 13;
560
+ d7 &= 8191;
561
+ d7 += h5 * r2;
562
+ d7 += h6 * r1;
563
+ d7 += h7 * r0;
564
+ d7 += h8 * (5 * r9);
565
+ d7 += h9 * (5 * r8);
566
+ c += d7 >>> 13;
567
+ d7 &= 8191;
568
+ let d8 = c;
569
+ d8 += h0 * r8;
570
+ d8 += h1 * r7;
571
+ d8 += h2 * r6;
572
+ d8 += h3 * r5;
573
+ d8 += h4 * r4;
574
+ c = d8 >>> 13;
575
+ d8 &= 8191;
576
+ d8 += h5 * r3;
577
+ d8 += h6 * r2;
578
+ d8 += h7 * r1;
579
+ d8 += h8 * r0;
580
+ d8 += h9 * (5 * r9);
581
+ c += d8 >>> 13;
582
+ d8 &= 8191;
583
+ let d9 = c;
584
+ d9 += h0 * r9;
585
+ d9 += h1 * r8;
586
+ d9 += h2 * r7;
587
+ d9 += h3 * r6;
588
+ d9 += h4 * r5;
589
+ c = d9 >>> 13;
590
+ d9 &= 8191;
591
+ d9 += h5 * r4;
592
+ d9 += h6 * r3;
593
+ d9 += h7 * r2;
594
+ d9 += h8 * r1;
595
+ d9 += h9 * r0;
596
+ c += d9 >>> 13;
597
+ d9 &= 8191;
598
+ c = (c << 2) + c | 0;
599
+ c = c + d0 | 0;
600
+ d0 = c & 8191;
601
+ c = c >>> 13;
602
+ d1 += c;
603
+ h0 = d0;
604
+ h1 = d1;
605
+ h2 = d2;
606
+ h3 = d3;
607
+ h4 = d4;
608
+ h5 = d5;
609
+ h6 = d6;
610
+ h7 = d7;
611
+ h8 = d8;
612
+ h9 = d9;
613
+ mpos += 16;
614
+ bytes -= 16;
615
+ }
616
+ this._h[0] = h0;
617
+ this._h[1] = h1;
618
+ this._h[2] = h2;
619
+ this._h[3] = h3;
620
+ this._h[4] = h4;
621
+ this._h[5] = h5;
622
+ this._h[6] = h6;
623
+ this._h[7] = h7;
624
+ this._h[8] = h8;
625
+ this._h[9] = h9;
626
+ }
627
+ finish(mac, macpos = 0) {
628
+ const g = new Uint16Array(10);
629
+ let c;
630
+ let mask;
631
+ let f;
632
+ let i;
633
+ if (this._leftover) {
634
+ i = this._leftover;
635
+ this._buffer[i++] = 1;
636
+ for (; i < 16; i++) this._buffer[i] = 0;
637
+ this._fin = 1;
638
+ this._blocks(this._buffer, 0, 16);
639
+ }
640
+ c = this._h[1] >>> 13;
641
+ this._h[1] &= 8191;
642
+ for (i = 2; i < 10; i++) {
643
+ this._h[i] += c;
644
+ c = this._h[i] >>> 13;
645
+ this._h[i] &= 8191;
646
+ }
647
+ this._h[0] += c * 5;
648
+ c = this._h[0] >>> 13;
649
+ this._h[0] &= 8191;
650
+ this._h[1] += c;
651
+ c = this._h[1] >>> 13;
652
+ this._h[1] &= 8191;
653
+ this._h[2] += c;
654
+ g[0] = this._h[0] + 5;
655
+ c = g[0] >>> 13;
656
+ g[0] &= 8191;
657
+ for (i = 1; i < 10; i++) {
658
+ g[i] = this._h[i] + c;
659
+ c = g[i] >>> 13;
660
+ g[i] &= 8191;
661
+ }
662
+ g[9] -= 8192;
663
+ mask = (c ^ 1) - 1;
664
+ for (i = 0; i < 10; i++) g[i] &= mask;
665
+ mask = ~mask;
666
+ for (i = 0; i < 10; i++) this._h[i] = this._h[i] & mask | g[i];
667
+ this._h[0] = (this._h[0] | this._h[1] << 13) & 65535;
668
+ this._h[1] = (this._h[1] >>> 3 | this._h[2] << 10) & 65535;
669
+ this._h[2] = (this._h[2] >>> 6 | this._h[3] << 7) & 65535;
670
+ this._h[3] = (this._h[3] >>> 9 | this._h[4] << 4) & 65535;
671
+ this._h[4] = (this._h[4] >>> 12 | this._h[5] << 1 | this._h[6] << 14) & 65535;
672
+ this._h[5] = (this._h[6] >>> 2 | this._h[7] << 11) & 65535;
673
+ this._h[6] = (this._h[7] >>> 5 | this._h[8] << 8) & 65535;
674
+ this._h[7] = (this._h[8] >>> 8 | this._h[9] << 5) & 65535;
675
+ f = this._h[0] + this._pad[0];
676
+ this._h[0] = f & 65535;
677
+ for (i = 1; i < 8; i++) {
678
+ f = (this._h[i] + this._pad[i] | 0) + (f >>> 16) | 0;
679
+ this._h[i] = f & 65535;
680
+ }
681
+ mac[macpos + 0] = this._h[0] >>> 0;
682
+ mac[macpos + 1] = this._h[0] >>> 8;
683
+ mac[macpos + 2] = this._h[1] >>> 0;
684
+ mac[macpos + 3] = this._h[1] >>> 8;
685
+ mac[macpos + 4] = this._h[2] >>> 0;
686
+ mac[macpos + 5] = this._h[2] >>> 8;
687
+ mac[macpos + 6] = this._h[3] >>> 0;
688
+ mac[macpos + 7] = this._h[3] >>> 8;
689
+ mac[macpos + 8] = this._h[4] >>> 0;
690
+ mac[macpos + 9] = this._h[4] >>> 8;
691
+ mac[macpos + 10] = this._h[5] >>> 0;
692
+ mac[macpos + 11] = this._h[5] >>> 8;
693
+ mac[macpos + 12] = this._h[6] >>> 0;
694
+ mac[macpos + 13] = this._h[6] >>> 8;
695
+ mac[macpos + 14] = this._h[7] >>> 0;
696
+ mac[macpos + 15] = this._h[7] >>> 8;
697
+ this._finished = true;
698
+ return this;
699
+ }
700
+ update(m) {
701
+ let mpos = 0;
702
+ let bytes = m.length;
703
+ let want;
704
+ if (this._leftover) {
705
+ want = 16 - this._leftover;
706
+ if (want > bytes) want = bytes;
707
+ for (let i = 0; i < want; i++) this._buffer[this._leftover + i] = m[mpos + i];
708
+ bytes -= want;
709
+ mpos += want;
710
+ this._leftover += want;
711
+ if (this._leftover < 16) return this;
712
+ this._blocks(this._buffer, 0, 16);
713
+ this._leftover = 0;
714
+ }
715
+ if (bytes >= 16) {
716
+ want = bytes - bytes % 16;
717
+ this._blocks(m, mpos, want);
718
+ mpos += want;
719
+ bytes -= want;
720
+ }
721
+ if (bytes) {
722
+ for (let i = 0; i < bytes; i++) this._buffer[this._leftover + i] = m[mpos + i];
723
+ this._leftover += bytes;
724
+ }
725
+ return this;
726
+ }
727
+ digest() {
728
+ if (this._finished) throw new Error("Poly1305 was finished");
729
+ let mac = new Uint8Array(16);
730
+ this.finish(mac);
731
+ return mac;
732
+ }
733
+ clean() {
734
+ wipe(this._buffer);
735
+ wipe(this._r);
736
+ wipe(this._h);
737
+ wipe(this._pad);
738
+ this._leftover = 0;
739
+ this._fin = 0;
740
+ this._finished = true;
741
+ return this;
742
+ }
743
+ };
744
+
745
+ //#endregion
746
+ //#region ../../node_modules/.bun/@stablelib+chacha20poly1305@2.0.1/node_modules/@stablelib/chacha20poly1305/lib/chacha20poly1305.js
747
+ const KEY_LENGTH = 32;
748
+ const NONCE_LENGTH = 12;
749
+ const TAG_LENGTH = 16;
750
+ const ZEROS = new Uint8Array(16);
751
+ /**
752
+ * ChaCha20-Poly1305 Authenticated Encryption with Associated Data.
753
+ *
754
+ * Defined in RFC7539.
755
+ */
756
+ var ChaCha20Poly1305 = class {
757
+ nonceLength = 12;
758
+ tagLength = 16;
759
+ _key;
760
+ /**
761
+ * Creates a new instance with the given 32-byte key.
762
+ */
763
+ constructor(key) {
764
+ if (key.length !== 32) throw new Error("ChaCha20Poly1305 needs 32-byte key");
765
+ this._key = new Uint8Array(key);
766
+ }
767
+ /**
768
+ * Encrypts and authenticates plaintext, authenticates associated data,
769
+ * and returns sealed ciphertext, which includes authentication tag.
770
+ *
771
+ * RFC7539 specifies 12 bytes for nonce. It may be this 12-byte nonce
772
+ * ("IV"), or full 16-byte counter (called "32-bit fixed-common part")
773
+ * and nonce.
774
+ *
775
+ * If dst is given (it must be the size of plaintext + the size of tag
776
+ * length) the result will be put into it. Dst and plaintext must not
777
+ * overlap.
778
+ */
779
+ seal(nonce, plaintext, associatedData, dst) {
780
+ if (nonce.length > 16) throw new Error("ChaCha20Poly1305: incorrect nonce length");
781
+ const counter = new Uint8Array(16);
782
+ counter.set(nonce, counter.length - nonce.length);
783
+ const authKey = new Uint8Array(32);
784
+ stream(this._key, counter, authKey, 4);
785
+ const resultLength = plaintext.length + this.tagLength;
786
+ let result;
787
+ if (dst) {
788
+ if (dst.length !== resultLength) throw new Error("ChaCha20Poly1305: incorrect destination length");
789
+ result = dst;
790
+ } else result = new Uint8Array(resultLength);
791
+ streamXOR(this._key, counter, plaintext, result, 4);
792
+ this._authenticate(result.subarray(result.length - this.tagLength, result.length), authKey, result.subarray(0, result.length - this.tagLength), associatedData);
793
+ wipe(counter);
794
+ return result;
795
+ }
796
+ /**
797
+ * Authenticates sealed ciphertext (which includes authentication tag) and
798
+ * associated data, decrypts ciphertext and returns decrypted plaintext.
799
+ *
800
+ * RFC7539 specifies 12 bytes for nonce. It may be this 12-byte nonce
801
+ * ("IV"), or full 16-byte counter (called "32-bit fixed-common part")
802
+ * and nonce.
803
+ *
804
+ * If authentication fails, it returns null.
805
+ *
806
+ * If dst is given (it must be of ciphertext length minus tag length),
807
+ * the result will be put into it. Dst and plaintext must not overlap.
808
+ */
809
+ open(nonce, sealed, associatedData, dst) {
810
+ if (nonce.length > 16) throw new Error("ChaCha20Poly1305: incorrect nonce length");
811
+ if (sealed.length < this.tagLength) return null;
812
+ const counter = new Uint8Array(16);
813
+ counter.set(nonce, counter.length - nonce.length);
814
+ const authKey = new Uint8Array(32);
815
+ stream(this._key, counter, authKey, 4);
816
+ const calculatedTag = new Uint8Array(this.tagLength);
817
+ this._authenticate(calculatedTag, authKey, sealed.subarray(0, sealed.length - this.tagLength), associatedData);
818
+ if (!equal(calculatedTag, sealed.subarray(sealed.length - this.tagLength, sealed.length))) return null;
819
+ const resultLength = sealed.length - this.tagLength;
820
+ let result;
821
+ if (dst) {
822
+ if (dst.length !== resultLength) throw new Error("ChaCha20Poly1305: incorrect destination length");
823
+ result = dst;
824
+ } else result = new Uint8Array(resultLength);
825
+ streamXOR(this._key, counter, sealed.subarray(0, sealed.length - this.tagLength), result, 4);
826
+ wipe(counter);
827
+ return result;
828
+ }
829
+ clean() {
830
+ wipe(this._key);
831
+ return this;
832
+ }
833
+ _authenticate(tagOut, authKey, ciphertext, associatedData) {
834
+ const h = new Poly1305(authKey);
835
+ if (associatedData) {
836
+ h.update(associatedData);
837
+ if (associatedData.length % 16 > 0) h.update(ZEROS.subarray(associatedData.length % 16));
838
+ }
839
+ h.update(ciphertext);
840
+ if (ciphertext.length % 16 > 0) h.update(ZEROS.subarray(ciphertext.length % 16));
841
+ const length = new Uint8Array(8);
842
+ if (associatedData) writeUint64LE(associatedData.length, length);
843
+ h.update(length);
844
+ writeUint64LE(ciphertext.length, length);
845
+ h.update(length);
846
+ const tag = h.digest();
847
+ for (let i = 0; i < tag.length; i++) tagOut[i] = tag[i];
848
+ h.clean();
849
+ wipe(tag);
850
+ wipe(length);
851
+ }
852
+ };
853
+
854
+ //#endregion
855
+ //#region src/crypto/chacha20.ts
856
+ var chacha20_exports = /* @__PURE__ */ __exportAll({
857
+ CHACHA20_AUTH_TAG_LENGTH: () => 16,
858
+ CHACHA20_NONCE_LENGTH: () => 12,
859
+ decrypt: () => decrypt,
860
+ encrypt: () => encrypt,
861
+ padNonce: () => padNonce
862
+ });
863
+ /** Length in bytes of the Poly1305 authentication tag appended to ciphertext. */
864
+ const CHACHA20_AUTH_TAG_LENGTH = 16;
865
+ /** Required nonce length in bytes for ChaCha20-Poly1305. Shorter nonces are zero-padded. */
866
+ const CHACHA20_NONCE_LENGTH = 12;
867
+ /**
868
+ * Decrypts a ChaCha20-Poly1305 sealed message and verifies its authentication tag.
869
+ *
870
+ * @param key - 256-bit encryption key.
871
+ * @param nonce - Nonce (up to 12 bytes; shorter nonces are left-padded with zeros).
872
+ * @param aad - Additional authenticated data, or null if none.
873
+ * @param ciphertext - The encrypted payload (without the auth tag).
874
+ * @param authTag - The 16-byte Poly1305 authentication tag.
875
+ * @throws DecryptionError if the authentication tag does not match.
876
+ */
877
+ function decrypt(key, nonce, aad, ciphertext, authTag) {
878
+ nonce = padNonce(nonce);
879
+ const chacha = new ChaCha20Poly1305(key);
880
+ const sealed = Buffer.concat([ciphertext, authTag]);
881
+ const plaintext = chacha.open(nonce, sealed, aad ?? void 0);
882
+ if (!plaintext) throw new DecryptionError();
883
+ return Buffer.from(plaintext);
884
+ }
885
+ /**
886
+ * Encrypts a plaintext message using ChaCha20-Poly1305 authenticated encryption.
887
+ *
888
+ * @param key - 256-bit encryption key.
889
+ * @param nonce - Nonce (up to 12 bytes; shorter nonces are left-padded with zeros).
890
+ * @param aad - Additional authenticated data, or null if none.
891
+ * @returns The ciphertext and its Poly1305 authentication tag.
892
+ */
893
+ function encrypt(key, nonce, aad, plaintext) {
894
+ nonce = padNonce(nonce);
895
+ const sealed = new ChaCha20Poly1305(key).seal(nonce, plaintext, aad ?? void 0);
896
+ return {
897
+ ciphertext: Buffer.from(sealed.subarray(0, sealed.length - 16)),
898
+ authTag: Buffer.from(sealed.subarray(sealed.length - 16))
899
+ };
900
+ }
901
+ /** Left-pads a nonce with zero bytes to 12 bytes. A nonce of 12 bytes or more comes back unchanged. */
902
+ function padNonce(nonce) {
903
+ if (nonce.length >= 12) return nonce;
904
+ return Buffer.concat([Buffer.alloc(12 - nonce.length, 0), nonce]);
905
+ }
906
+
907
+ //#endregion
908
+ //#region ../../node_modules/.bun/@stablelib+random@2.0.1/node_modules/@stablelib/random/lib/source/system.js
909
+ const QUOTA = 65536;
910
+ var SystemRandomSource = class {
911
+ isAvailable = false;
912
+ isInstantiated = false;
913
+ constructor() {
914
+ if (typeof crypto !== "undefined" && "getRandomValues" in crypto) {
915
+ this.isAvailable = true;
916
+ this.isInstantiated = true;
917
+ }
918
+ }
919
+ randomBytes(length) {
920
+ if (!this.isAvailable) throw new Error("System random byte generator is not available.");
921
+ const out = new Uint8Array(length);
922
+ for (let i = 0; i < out.length; i += QUOTA) crypto.getRandomValues(out.subarray(i, i + Math.min(out.length - i, QUOTA)));
923
+ return out;
924
+ }
925
+ };
926
+
927
+ //#endregion
928
+ //#region ../../node_modules/.bun/@stablelib+random@2.0.1/node_modules/@stablelib/random/lib/random.js
929
+ const defaultRandomSource = new SystemRandomSource();
930
+ function randomBytes$1(length, prng = defaultRandomSource) {
931
+ return prng.randomBytes(length);
932
+ }
933
+
934
+ //#endregion
935
+ //#region ../../node_modules/.bun/@stablelib+x25519@2.0.1/node_modules/@stablelib/x25519/lib/x25519.js
936
+ const PUBLIC_KEY_LENGTH$1 = 32;
937
+ const SECRET_KEY_LENGTH$1 = 32;
938
+ function gf$1(init) {
939
+ const r = new Float64Array(16);
940
+ if (init) for (let i = 0; i < init.length; i++) r[i] = init[i];
941
+ return r;
942
+ }
943
+ const _9$1 = new Uint8Array(32);
944
+ _9$1[0] = 9;
945
+ const _121665 = gf$1([56129, 1]);
946
+ function car25519$1(o) {
947
+ let c = 1;
948
+ for (let i = 0; i < 16; i++) {
949
+ let v = o[i] + c + 65535;
950
+ c = Math.floor(v / 65536);
951
+ o[i] = v - c * 65536;
952
+ }
953
+ o[0] += c - 1 + 37 * (c - 1);
954
+ }
955
+ function sel25519$1(p, q, b) {
956
+ const c = ~(b - 1);
957
+ for (let i = 0; i < 16; i++) {
958
+ const t = c & (p[i] ^ q[i]);
959
+ p[i] ^= t;
960
+ q[i] ^= t;
961
+ }
962
+ }
963
+ function pack25519$1(o, n) {
964
+ const m = gf$1();
965
+ const t = gf$1();
966
+ for (let i = 0; i < 16; i++) t[i] = n[i];
967
+ car25519$1(t);
968
+ car25519$1(t);
969
+ car25519$1(t);
970
+ for (let j = 0; j < 2; j++) {
971
+ m[0] = t[0] - 65517;
972
+ for (let i = 1; i < 15; i++) {
973
+ m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
974
+ m[i - 1] &= 65535;
975
+ }
976
+ m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
977
+ const b = m[15] >> 16 & 1;
978
+ m[14] &= 65535;
979
+ sel25519$1(t, m, 1 - b);
980
+ }
981
+ for (let i = 0; i < 16; i++) {
982
+ o[2 * i] = t[i] & 255;
983
+ o[2 * i + 1] = t[i] >> 8;
984
+ }
985
+ }
986
+ function unpack25519$1(o, n) {
987
+ for (let i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
988
+ o[15] &= 32767;
989
+ }
990
+ function add$1(o, a, b) {
991
+ for (let i = 0; i < 16; i++) o[i] = a[i] + b[i];
992
+ }
993
+ function sub$1(o, a, b) {
994
+ for (let i = 0; i < 16; i++) o[i] = a[i] - b[i];
995
+ }
996
+ function mul$1(o, a, b) {
997
+ let v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0, t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0, b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15];
998
+ v = a[0];
999
+ t0 += v * b0;
1000
+ t1 += v * b1;
1001
+ t2 += v * b2;
1002
+ t3 += v * b3;
1003
+ t4 += v * b4;
1004
+ t5 += v * b5;
1005
+ t6 += v * b6;
1006
+ t7 += v * b7;
1007
+ t8 += v * b8;
1008
+ t9 += v * b9;
1009
+ t10 += v * b10;
1010
+ t11 += v * b11;
1011
+ t12 += v * b12;
1012
+ t13 += v * b13;
1013
+ t14 += v * b14;
1014
+ t15 += v * b15;
1015
+ v = a[1];
1016
+ t1 += v * b0;
1017
+ t2 += v * b1;
1018
+ t3 += v * b2;
1019
+ t4 += v * b3;
1020
+ t5 += v * b4;
1021
+ t6 += v * b5;
1022
+ t7 += v * b6;
1023
+ t8 += v * b7;
1024
+ t9 += v * b8;
1025
+ t10 += v * b9;
1026
+ t11 += v * b10;
1027
+ t12 += v * b11;
1028
+ t13 += v * b12;
1029
+ t14 += v * b13;
1030
+ t15 += v * b14;
1031
+ t16 += v * b15;
1032
+ v = a[2];
1033
+ t2 += v * b0;
1034
+ t3 += v * b1;
1035
+ t4 += v * b2;
1036
+ t5 += v * b3;
1037
+ t6 += v * b4;
1038
+ t7 += v * b5;
1039
+ t8 += v * b6;
1040
+ t9 += v * b7;
1041
+ t10 += v * b8;
1042
+ t11 += v * b9;
1043
+ t12 += v * b10;
1044
+ t13 += v * b11;
1045
+ t14 += v * b12;
1046
+ t15 += v * b13;
1047
+ t16 += v * b14;
1048
+ t17 += v * b15;
1049
+ v = a[3];
1050
+ t3 += v * b0;
1051
+ t4 += v * b1;
1052
+ t5 += v * b2;
1053
+ t6 += v * b3;
1054
+ t7 += v * b4;
1055
+ t8 += v * b5;
1056
+ t9 += v * b6;
1057
+ t10 += v * b7;
1058
+ t11 += v * b8;
1059
+ t12 += v * b9;
1060
+ t13 += v * b10;
1061
+ t14 += v * b11;
1062
+ t15 += v * b12;
1063
+ t16 += v * b13;
1064
+ t17 += v * b14;
1065
+ t18 += v * b15;
1066
+ v = a[4];
1067
+ t4 += v * b0;
1068
+ t5 += v * b1;
1069
+ t6 += v * b2;
1070
+ t7 += v * b3;
1071
+ t8 += v * b4;
1072
+ t9 += v * b5;
1073
+ t10 += v * b6;
1074
+ t11 += v * b7;
1075
+ t12 += v * b8;
1076
+ t13 += v * b9;
1077
+ t14 += v * b10;
1078
+ t15 += v * b11;
1079
+ t16 += v * b12;
1080
+ t17 += v * b13;
1081
+ t18 += v * b14;
1082
+ t19 += v * b15;
1083
+ v = a[5];
1084
+ t5 += v * b0;
1085
+ t6 += v * b1;
1086
+ t7 += v * b2;
1087
+ t8 += v * b3;
1088
+ t9 += v * b4;
1089
+ t10 += v * b5;
1090
+ t11 += v * b6;
1091
+ t12 += v * b7;
1092
+ t13 += v * b8;
1093
+ t14 += v * b9;
1094
+ t15 += v * b10;
1095
+ t16 += v * b11;
1096
+ t17 += v * b12;
1097
+ t18 += v * b13;
1098
+ t19 += v * b14;
1099
+ t20 += v * b15;
1100
+ v = a[6];
1101
+ t6 += v * b0;
1102
+ t7 += v * b1;
1103
+ t8 += v * b2;
1104
+ t9 += v * b3;
1105
+ t10 += v * b4;
1106
+ t11 += v * b5;
1107
+ t12 += v * b6;
1108
+ t13 += v * b7;
1109
+ t14 += v * b8;
1110
+ t15 += v * b9;
1111
+ t16 += v * b10;
1112
+ t17 += v * b11;
1113
+ t18 += v * b12;
1114
+ t19 += v * b13;
1115
+ t20 += v * b14;
1116
+ t21 += v * b15;
1117
+ v = a[7];
1118
+ t7 += v * b0;
1119
+ t8 += v * b1;
1120
+ t9 += v * b2;
1121
+ t10 += v * b3;
1122
+ t11 += v * b4;
1123
+ t12 += v * b5;
1124
+ t13 += v * b6;
1125
+ t14 += v * b7;
1126
+ t15 += v * b8;
1127
+ t16 += v * b9;
1128
+ t17 += v * b10;
1129
+ t18 += v * b11;
1130
+ t19 += v * b12;
1131
+ t20 += v * b13;
1132
+ t21 += v * b14;
1133
+ t22 += v * b15;
1134
+ v = a[8];
1135
+ t8 += v * b0;
1136
+ t9 += v * b1;
1137
+ t10 += v * b2;
1138
+ t11 += v * b3;
1139
+ t12 += v * b4;
1140
+ t13 += v * b5;
1141
+ t14 += v * b6;
1142
+ t15 += v * b7;
1143
+ t16 += v * b8;
1144
+ t17 += v * b9;
1145
+ t18 += v * b10;
1146
+ t19 += v * b11;
1147
+ t20 += v * b12;
1148
+ t21 += v * b13;
1149
+ t22 += v * b14;
1150
+ t23 += v * b15;
1151
+ v = a[9];
1152
+ t9 += v * b0;
1153
+ t10 += v * b1;
1154
+ t11 += v * b2;
1155
+ t12 += v * b3;
1156
+ t13 += v * b4;
1157
+ t14 += v * b5;
1158
+ t15 += v * b6;
1159
+ t16 += v * b7;
1160
+ t17 += v * b8;
1161
+ t18 += v * b9;
1162
+ t19 += v * b10;
1163
+ t20 += v * b11;
1164
+ t21 += v * b12;
1165
+ t22 += v * b13;
1166
+ t23 += v * b14;
1167
+ t24 += v * b15;
1168
+ v = a[10];
1169
+ t10 += v * b0;
1170
+ t11 += v * b1;
1171
+ t12 += v * b2;
1172
+ t13 += v * b3;
1173
+ t14 += v * b4;
1174
+ t15 += v * b5;
1175
+ t16 += v * b6;
1176
+ t17 += v * b7;
1177
+ t18 += v * b8;
1178
+ t19 += v * b9;
1179
+ t20 += v * b10;
1180
+ t21 += v * b11;
1181
+ t22 += v * b12;
1182
+ t23 += v * b13;
1183
+ t24 += v * b14;
1184
+ t25 += v * b15;
1185
+ v = a[11];
1186
+ t11 += v * b0;
1187
+ t12 += v * b1;
1188
+ t13 += v * b2;
1189
+ t14 += v * b3;
1190
+ t15 += v * b4;
1191
+ t16 += v * b5;
1192
+ t17 += v * b6;
1193
+ t18 += v * b7;
1194
+ t19 += v * b8;
1195
+ t20 += v * b9;
1196
+ t21 += v * b10;
1197
+ t22 += v * b11;
1198
+ t23 += v * b12;
1199
+ t24 += v * b13;
1200
+ t25 += v * b14;
1201
+ t26 += v * b15;
1202
+ v = a[12];
1203
+ t12 += v * b0;
1204
+ t13 += v * b1;
1205
+ t14 += v * b2;
1206
+ t15 += v * b3;
1207
+ t16 += v * b4;
1208
+ t17 += v * b5;
1209
+ t18 += v * b6;
1210
+ t19 += v * b7;
1211
+ t20 += v * b8;
1212
+ t21 += v * b9;
1213
+ t22 += v * b10;
1214
+ t23 += v * b11;
1215
+ t24 += v * b12;
1216
+ t25 += v * b13;
1217
+ t26 += v * b14;
1218
+ t27 += v * b15;
1219
+ v = a[13];
1220
+ t13 += v * b0;
1221
+ t14 += v * b1;
1222
+ t15 += v * b2;
1223
+ t16 += v * b3;
1224
+ t17 += v * b4;
1225
+ t18 += v * b5;
1226
+ t19 += v * b6;
1227
+ t20 += v * b7;
1228
+ t21 += v * b8;
1229
+ t22 += v * b9;
1230
+ t23 += v * b10;
1231
+ t24 += v * b11;
1232
+ t25 += v * b12;
1233
+ t26 += v * b13;
1234
+ t27 += v * b14;
1235
+ t28 += v * b15;
1236
+ v = a[14];
1237
+ t14 += v * b0;
1238
+ t15 += v * b1;
1239
+ t16 += v * b2;
1240
+ t17 += v * b3;
1241
+ t18 += v * b4;
1242
+ t19 += v * b5;
1243
+ t20 += v * b6;
1244
+ t21 += v * b7;
1245
+ t22 += v * b8;
1246
+ t23 += v * b9;
1247
+ t24 += v * b10;
1248
+ t25 += v * b11;
1249
+ t26 += v * b12;
1250
+ t27 += v * b13;
1251
+ t28 += v * b14;
1252
+ t29 += v * b15;
1253
+ v = a[15];
1254
+ t15 += v * b0;
1255
+ t16 += v * b1;
1256
+ t17 += v * b2;
1257
+ t18 += v * b3;
1258
+ t19 += v * b4;
1259
+ t20 += v * b5;
1260
+ t21 += v * b6;
1261
+ t22 += v * b7;
1262
+ t23 += v * b8;
1263
+ t24 += v * b9;
1264
+ t25 += v * b10;
1265
+ t26 += v * b11;
1266
+ t27 += v * b12;
1267
+ t28 += v * b13;
1268
+ t29 += v * b14;
1269
+ t30 += v * b15;
1270
+ t0 += 38 * t16;
1271
+ t1 += 38 * t17;
1272
+ t2 += 38 * t18;
1273
+ t3 += 38 * t19;
1274
+ t4 += 38 * t20;
1275
+ t5 += 38 * t21;
1276
+ t6 += 38 * t22;
1277
+ t7 += 38 * t23;
1278
+ t8 += 38 * t24;
1279
+ t9 += 38 * t25;
1280
+ t10 += 38 * t26;
1281
+ t11 += 38 * t27;
1282
+ t12 += 38 * t28;
1283
+ t13 += 38 * t29;
1284
+ t14 += 38 * t30;
1285
+ c = 1;
1286
+ v = t0 + c + 65535;
1287
+ c = Math.floor(v / 65536);
1288
+ t0 = v - c * 65536;
1289
+ v = t1 + c + 65535;
1290
+ c = Math.floor(v / 65536);
1291
+ t1 = v - c * 65536;
1292
+ v = t2 + c + 65535;
1293
+ c = Math.floor(v / 65536);
1294
+ t2 = v - c * 65536;
1295
+ v = t3 + c + 65535;
1296
+ c = Math.floor(v / 65536);
1297
+ t3 = v - c * 65536;
1298
+ v = t4 + c + 65535;
1299
+ c = Math.floor(v / 65536);
1300
+ t4 = v - c * 65536;
1301
+ v = t5 + c + 65535;
1302
+ c = Math.floor(v / 65536);
1303
+ t5 = v - c * 65536;
1304
+ v = t6 + c + 65535;
1305
+ c = Math.floor(v / 65536);
1306
+ t6 = v - c * 65536;
1307
+ v = t7 + c + 65535;
1308
+ c = Math.floor(v / 65536);
1309
+ t7 = v - c * 65536;
1310
+ v = t8 + c + 65535;
1311
+ c = Math.floor(v / 65536);
1312
+ t8 = v - c * 65536;
1313
+ v = t9 + c + 65535;
1314
+ c = Math.floor(v / 65536);
1315
+ t9 = v - c * 65536;
1316
+ v = t10 + c + 65535;
1317
+ c = Math.floor(v / 65536);
1318
+ t10 = v - c * 65536;
1319
+ v = t11 + c + 65535;
1320
+ c = Math.floor(v / 65536);
1321
+ t11 = v - c * 65536;
1322
+ v = t12 + c + 65535;
1323
+ c = Math.floor(v / 65536);
1324
+ t12 = v - c * 65536;
1325
+ v = t13 + c + 65535;
1326
+ c = Math.floor(v / 65536);
1327
+ t13 = v - c * 65536;
1328
+ v = t14 + c + 65535;
1329
+ c = Math.floor(v / 65536);
1330
+ t14 = v - c * 65536;
1331
+ v = t15 + c + 65535;
1332
+ c = Math.floor(v / 65536);
1333
+ t15 = v - c * 65536;
1334
+ t0 += c - 1 + 37 * (c - 1);
1335
+ c = 1;
1336
+ v = t0 + c + 65535;
1337
+ c = Math.floor(v / 65536);
1338
+ t0 = v - c * 65536;
1339
+ v = t1 + c + 65535;
1340
+ c = Math.floor(v / 65536);
1341
+ t1 = v - c * 65536;
1342
+ v = t2 + c + 65535;
1343
+ c = Math.floor(v / 65536);
1344
+ t2 = v - c * 65536;
1345
+ v = t3 + c + 65535;
1346
+ c = Math.floor(v / 65536);
1347
+ t3 = v - c * 65536;
1348
+ v = t4 + c + 65535;
1349
+ c = Math.floor(v / 65536);
1350
+ t4 = v - c * 65536;
1351
+ v = t5 + c + 65535;
1352
+ c = Math.floor(v / 65536);
1353
+ t5 = v - c * 65536;
1354
+ v = t6 + c + 65535;
1355
+ c = Math.floor(v / 65536);
1356
+ t6 = v - c * 65536;
1357
+ v = t7 + c + 65535;
1358
+ c = Math.floor(v / 65536);
1359
+ t7 = v - c * 65536;
1360
+ v = t8 + c + 65535;
1361
+ c = Math.floor(v / 65536);
1362
+ t8 = v - c * 65536;
1363
+ v = t9 + c + 65535;
1364
+ c = Math.floor(v / 65536);
1365
+ t9 = v - c * 65536;
1366
+ v = t10 + c + 65535;
1367
+ c = Math.floor(v / 65536);
1368
+ t10 = v - c * 65536;
1369
+ v = t11 + c + 65535;
1370
+ c = Math.floor(v / 65536);
1371
+ t11 = v - c * 65536;
1372
+ v = t12 + c + 65535;
1373
+ c = Math.floor(v / 65536);
1374
+ t12 = v - c * 65536;
1375
+ v = t13 + c + 65535;
1376
+ c = Math.floor(v / 65536);
1377
+ t13 = v - c * 65536;
1378
+ v = t14 + c + 65535;
1379
+ c = Math.floor(v / 65536);
1380
+ t14 = v - c * 65536;
1381
+ v = t15 + c + 65535;
1382
+ c = Math.floor(v / 65536);
1383
+ t15 = v - c * 65536;
1384
+ t0 += c - 1 + 37 * (c - 1);
1385
+ o[0] = t0;
1386
+ o[1] = t1;
1387
+ o[2] = t2;
1388
+ o[3] = t3;
1389
+ o[4] = t4;
1390
+ o[5] = t5;
1391
+ o[6] = t6;
1392
+ o[7] = t7;
1393
+ o[8] = t8;
1394
+ o[9] = t9;
1395
+ o[10] = t10;
1396
+ o[11] = t11;
1397
+ o[12] = t12;
1398
+ o[13] = t13;
1399
+ o[14] = t14;
1400
+ o[15] = t15;
1401
+ }
1402
+ function square$1(o, a) {
1403
+ mul$1(o, a, a);
1404
+ }
1405
+ function inv25519$1(o, inp) {
1406
+ const c = gf$1();
1407
+ for (let i = 0; i < 16; i++) c[i] = inp[i];
1408
+ for (let i = 253; i >= 0; i--) {
1409
+ square$1(c, c);
1410
+ if (i !== 2 && i !== 4) mul$1(c, c, inp);
1411
+ }
1412
+ for (let i = 0; i < 16; i++) o[i] = c[i];
1413
+ }
1414
+ function scalarMult(n, p) {
1415
+ const z = new Uint8Array(32);
1416
+ const x = new Float64Array(80);
1417
+ const a = gf$1(), b = gf$1(), c = gf$1(), d = gf$1(), e = gf$1(), f = gf$1();
1418
+ for (let i = 0; i < 31; i++) z[i] = n[i];
1419
+ z[31] = n[31] & 127 | 64;
1420
+ z[0] &= 248;
1421
+ unpack25519$1(x, p);
1422
+ for (let i = 0; i < 16; i++) b[i] = x[i];
1423
+ a[0] = d[0] = 1;
1424
+ for (let i = 254; i >= 0; --i) {
1425
+ const r = z[i >>> 3] >>> (i & 7) & 1;
1426
+ sel25519$1(a, b, r);
1427
+ sel25519$1(c, d, r);
1428
+ add$1(e, a, c);
1429
+ sub$1(a, a, c);
1430
+ add$1(c, b, d);
1431
+ sub$1(b, b, d);
1432
+ square$1(d, e);
1433
+ square$1(f, a);
1434
+ mul$1(a, c, a);
1435
+ mul$1(c, b, e);
1436
+ add$1(e, a, c);
1437
+ sub$1(a, a, c);
1438
+ square$1(b, a);
1439
+ sub$1(c, d, f);
1440
+ mul$1(a, c, _121665);
1441
+ add$1(a, a, d);
1442
+ mul$1(c, c, a);
1443
+ mul$1(a, d, f);
1444
+ mul$1(d, b, x);
1445
+ square$1(b, e);
1446
+ sel25519$1(a, b, r);
1447
+ sel25519$1(c, d, r);
1448
+ }
1449
+ for (let i = 0; i < 16; i++) {
1450
+ x[i + 16] = a[i];
1451
+ x[i + 32] = c[i];
1452
+ x[i + 48] = b[i];
1453
+ x[i + 64] = d[i];
1454
+ }
1455
+ const x32 = x.subarray(32);
1456
+ const x16 = x.subarray(16);
1457
+ inv25519$1(x32, x32);
1458
+ mul$1(x16, x16, x32);
1459
+ const q = new Uint8Array(32);
1460
+ pack25519$1(q, x16);
1461
+ return q;
1462
+ }
1463
+ function scalarMultBase(n) {
1464
+ return scalarMult(n, _9$1);
1465
+ }
1466
+ function generateKeyPairFromSeed$1(seed) {
1467
+ if (seed.length !== 32) throw new Error(`x25519: seed must be ${32} bytes`);
1468
+ const secretKey = new Uint8Array(seed);
1469
+ return {
1470
+ publicKey: scalarMultBase(secretKey),
1471
+ secretKey
1472
+ };
1473
+ }
1474
+ function generateKeyPair$3(prng) {
1475
+ const seed = randomBytes$1(32, prng);
1476
+ const result = generateKeyPairFromSeed$1(seed);
1477
+ wipe(seed);
1478
+ return result;
1479
+ }
1480
+ /**
1481
+ * Returns a shared key between our secret key and a peer's public key.
1482
+ *
1483
+ * Throws an error if the given keys are of wrong length.
1484
+ *
1485
+ * If rejectZero is true throws if the calculated shared key is all-zero.
1486
+ * From RFC 7748:
1487
+ *
1488
+ * > Protocol designers using Diffie-Hellman over the curves defined in
1489
+ * > this document must not assume "contributory behavior". Specially,
1490
+ * > contributory behavior means that both parties' private keys
1491
+ * > contribute to the resulting shared key. Since curve25519 and
1492
+ * > curve448 have cofactors of 8 and 4 (respectively), an input point of
1493
+ * > small order will eliminate any contribution from the other party's
1494
+ * > private key. This situation can be detected by checking for the all-
1495
+ * > zero output, which implementations MAY do, as specified in Section 6.
1496
+ * > However, a large number of existing implementations do not do this.
1497
+ *
1498
+ * IMPORTANT: the returned key is a raw result of scalar multiplication.
1499
+ * To use it as a key material, hash it with a cryptographic hash function.
1500
+ */
1501
+ function sharedKey(mySecretKey, theirPublicKey, rejectZero = false) {
1502
+ if (mySecretKey.length !== 32) throw new Error("X25519: incorrect secret key length");
1503
+ if (theirPublicKey.length !== 32) throw new Error("X25519: incorrect public key length");
1504
+ const result = scalarMult(mySecretKey, theirPublicKey);
1505
+ if (rejectZero) {
1506
+ let zeros = 0;
1507
+ for (let i = 0; i < result.length; i++) zeros |= result[i];
1508
+ if (zeros === 0) throw new Error("X25519: invalid shared key");
1509
+ }
1510
+ return result;
1511
+ }
1512
+
1513
+ //#endregion
1514
+ //#region src/crypto/curve25519.ts
1515
+ var curve25519_exports = /* @__PURE__ */ __exportAll({
1516
+ generateKeyPair: () => generateKeyPair$2,
1517
+ generateSharedSecKey: () => generateSharedSecKey
1518
+ });
1519
+ /**
1520
+ * Generates a new Curve25519 key pair for Diffie-Hellman key exchange.
1521
+ */
1522
+ function generateKeyPair$2() {
1523
+ return generateKeyPair$3();
1524
+ }
1525
+ /**
1526
+ * Derives a Curve25519 shared secret.
1527
+ *
1528
+ * @param priKey - Local private key.
1529
+ * @param pubKey - Remote public key.
1530
+ * @returns 32-byte shared secret.
1531
+ */
1532
+ function generateSharedSecKey(priKey, pubKey) {
1533
+ return sharedKey(priKey, pubKey);
1534
+ }
1535
+
1536
+ //#endregion
1537
+ //#region ../../node_modules/.bun/@stablelib+sha512@2.0.1/node_modules/@stablelib/sha512/lib/sha512.js
1538
+ const DIGEST_LENGTH = 64;
1539
+ const BLOCK_SIZE = 128;
1540
+ /**
1541
+ * SHA-2-512 cryptographic hash algorithm.
1542
+ */
1543
+ var SHA512 = class {
1544
+ /** Length of hash output */
1545
+ digestLength = 64;
1546
+ /** Block size */
1547
+ blockSize = 128;
1548
+ _stateHi = new Int32Array(8);
1549
+ _stateLo = new Int32Array(8);
1550
+ _tempHi = new Int32Array(16);
1551
+ _tempLo = new Int32Array(16);
1552
+ _buffer = new Uint8Array(256);
1553
+ _bufferLength = 0;
1554
+ _bytesHashed = 0;
1555
+ _finished = false;
1556
+ constructor() {
1557
+ this.reset();
1558
+ }
1559
+ _initState() {
1560
+ this._stateHi[0] = 1779033703;
1561
+ this._stateHi[1] = 3144134277;
1562
+ this._stateHi[2] = 1013904242;
1563
+ this._stateHi[3] = 2773480762;
1564
+ this._stateHi[4] = 1359893119;
1565
+ this._stateHi[5] = 2600822924;
1566
+ this._stateHi[6] = 528734635;
1567
+ this._stateHi[7] = 1541459225;
1568
+ this._stateLo[0] = 4089235720;
1569
+ this._stateLo[1] = 2227873595;
1570
+ this._stateLo[2] = 4271175723;
1571
+ this._stateLo[3] = 1595750129;
1572
+ this._stateLo[4] = 2917565137;
1573
+ this._stateLo[5] = 725511199;
1574
+ this._stateLo[6] = 4215389547;
1575
+ this._stateLo[7] = 327033209;
1576
+ }
1577
+ /**
1578
+ * Resets hash state making it possible
1579
+ * to re-use this instance to hash other data.
1580
+ */
1581
+ reset() {
1582
+ this._initState();
1583
+ this._bufferLength = 0;
1584
+ this._bytesHashed = 0;
1585
+ this._finished = false;
1586
+ return this;
1587
+ }
1588
+ /**
1589
+ * Cleans internal buffers and resets hash state.
1590
+ */
1591
+ clean() {
1592
+ wipe(this._buffer);
1593
+ wipe(this._tempHi);
1594
+ wipe(this._tempLo);
1595
+ this.reset();
1596
+ }
1597
+ /**
1598
+ * Updates hash state with the given data.
1599
+ *
1600
+ * Throws error when trying to update already finalized hash:
1601
+ * instance must be reset to update it again.
1602
+ */
1603
+ update(data, dataLength = data.length) {
1604
+ if (this._finished) throw new Error("SHA512: can't update because hash was finished.");
1605
+ let dataPos = 0;
1606
+ this._bytesHashed += dataLength;
1607
+ if (this._bufferLength > 0) {
1608
+ while (this._bufferLength < 128 && dataLength > 0) {
1609
+ this._buffer[this._bufferLength++] = data[dataPos++];
1610
+ dataLength--;
1611
+ }
1612
+ if (this._bufferLength === this.blockSize) {
1613
+ hashBlocks(this._tempHi, this._tempLo, this._stateHi, this._stateLo, this._buffer, 0, this.blockSize);
1614
+ this._bufferLength = 0;
1615
+ }
1616
+ }
1617
+ if (dataLength >= this.blockSize) {
1618
+ dataPos = hashBlocks(this._tempHi, this._tempLo, this._stateHi, this._stateLo, data, dataPos, dataLength);
1619
+ dataLength %= this.blockSize;
1620
+ }
1621
+ while (dataLength > 0) {
1622
+ this._buffer[this._bufferLength++] = data[dataPos++];
1623
+ dataLength--;
1624
+ }
1625
+ return this;
1626
+ }
1627
+ /**
1628
+ * Finalizes hash state and puts hash into out.
1629
+ * If hash was already finalized, puts the same value.
1630
+ */
1631
+ finish(out) {
1632
+ if (!this._finished) {
1633
+ const bytesHashed = this._bytesHashed;
1634
+ const left = this._bufferLength;
1635
+ const bitLenHi = bytesHashed / 536870912 | 0;
1636
+ const bitLenLo = bytesHashed << 3;
1637
+ const padLength = bytesHashed % 128 < 112 ? 128 : 256;
1638
+ this._buffer[left] = 128;
1639
+ for (let i = left + 1; i < padLength - 8; i++) this._buffer[i] = 0;
1640
+ writeUint32BE(bitLenHi, this._buffer, padLength - 8);
1641
+ writeUint32BE(bitLenLo, this._buffer, padLength - 4);
1642
+ hashBlocks(this._tempHi, this._tempLo, this._stateHi, this._stateLo, this._buffer, 0, padLength);
1643
+ this._finished = true;
1644
+ }
1645
+ for (let i = 0; i < this.digestLength / 8; i++) {
1646
+ writeUint32BE(this._stateHi[i], out, i * 8);
1647
+ writeUint32BE(this._stateLo[i], out, i * 8 + 4);
1648
+ }
1649
+ return this;
1650
+ }
1651
+ /**
1652
+ * Returns the final hash digest.
1653
+ */
1654
+ digest() {
1655
+ const out = new Uint8Array(this.digestLength);
1656
+ this.finish(out);
1657
+ return out;
1658
+ }
1659
+ /**
1660
+ * Function useful for HMAC/PBKDF2 optimization. Returns hash state to be
1661
+ * used with restoreState(). Only chain value is saved, not buffers or
1662
+ * other state variables.
1663
+ */
1664
+ saveState() {
1665
+ if (this._finished) throw new Error("SHA256: cannot save finished state");
1666
+ return {
1667
+ stateHi: new Int32Array(this._stateHi),
1668
+ stateLo: new Int32Array(this._stateLo),
1669
+ buffer: this._bufferLength > 0 ? new Uint8Array(this._buffer) : void 0,
1670
+ bufferLength: this._bufferLength,
1671
+ bytesHashed: this._bytesHashed
1672
+ };
1673
+ }
1674
+ /**
1675
+ * Function useful for HMAC/PBKDF2 optimization. Restores state saved by
1676
+ * saveState() and sets bytesHashed to the given value.
1677
+ */
1678
+ restoreState(savedState) {
1679
+ this._stateHi.set(savedState.stateHi);
1680
+ this._stateLo.set(savedState.stateLo);
1681
+ this._bufferLength = savedState.bufferLength;
1682
+ if (savedState.buffer) this._buffer.set(savedState.buffer);
1683
+ this._bytesHashed = savedState.bytesHashed;
1684
+ this._finished = false;
1685
+ return this;
1686
+ }
1687
+ /**
1688
+ * Cleans state returned by saveState().
1689
+ */
1690
+ cleanSavedState(savedState) {
1691
+ wipe(savedState.stateHi);
1692
+ wipe(savedState.stateLo);
1693
+ if (savedState.buffer) wipe(savedState.buffer);
1694
+ savedState.bufferLength = 0;
1695
+ savedState.bytesHashed = 0;
1696
+ }
1697
+ };
1698
+ const K = new Int32Array([
1699
+ 1116352408,
1700
+ 3609767458,
1701
+ 1899447441,
1702
+ 602891725,
1703
+ 3049323471,
1704
+ 3964484399,
1705
+ 3921009573,
1706
+ 2173295548,
1707
+ 961987163,
1708
+ 4081628472,
1709
+ 1508970993,
1710
+ 3053834265,
1711
+ 2453635748,
1712
+ 2937671579,
1713
+ 2870763221,
1714
+ 3664609560,
1715
+ 3624381080,
1716
+ 2734883394,
1717
+ 310598401,
1718
+ 1164996542,
1719
+ 607225278,
1720
+ 1323610764,
1721
+ 1426881987,
1722
+ 3590304994,
1723
+ 1925078388,
1724
+ 4068182383,
1725
+ 2162078206,
1726
+ 991336113,
1727
+ 2614888103,
1728
+ 633803317,
1729
+ 3248222580,
1730
+ 3479774868,
1731
+ 3835390401,
1732
+ 2666613458,
1733
+ 4022224774,
1734
+ 944711139,
1735
+ 264347078,
1736
+ 2341262773,
1737
+ 604807628,
1738
+ 2007800933,
1739
+ 770255983,
1740
+ 1495990901,
1741
+ 1249150122,
1742
+ 1856431235,
1743
+ 1555081692,
1744
+ 3175218132,
1745
+ 1996064986,
1746
+ 2198950837,
1747
+ 2554220882,
1748
+ 3999719339,
1749
+ 2821834349,
1750
+ 766784016,
1751
+ 2952996808,
1752
+ 2566594879,
1753
+ 3210313671,
1754
+ 3203337956,
1755
+ 3336571891,
1756
+ 1034457026,
1757
+ 3584528711,
1758
+ 2466948901,
1759
+ 113926993,
1760
+ 3758326383,
1761
+ 338241895,
1762
+ 168717936,
1763
+ 666307205,
1764
+ 1188179964,
1765
+ 773529912,
1766
+ 1546045734,
1767
+ 1294757372,
1768
+ 1522805485,
1769
+ 1396182291,
1770
+ 2643833823,
1771
+ 1695183700,
1772
+ 2343527390,
1773
+ 1986661051,
1774
+ 1014477480,
1775
+ 2177026350,
1776
+ 1206759142,
1777
+ 2456956037,
1778
+ 344077627,
1779
+ 2730485921,
1780
+ 1290863460,
1781
+ 2820302411,
1782
+ 3158454273,
1783
+ 3259730800,
1784
+ 3505952657,
1785
+ 3345764771,
1786
+ 106217008,
1787
+ 3516065817,
1788
+ 3606008344,
1789
+ 3600352804,
1790
+ 1432725776,
1791
+ 4094571909,
1792
+ 1467031594,
1793
+ 275423344,
1794
+ 851169720,
1795
+ 430227734,
1796
+ 3100823752,
1797
+ 506948616,
1798
+ 1363258195,
1799
+ 659060556,
1800
+ 3750685593,
1801
+ 883997877,
1802
+ 3785050280,
1803
+ 958139571,
1804
+ 3318307427,
1805
+ 1322822218,
1806
+ 3812723403,
1807
+ 1537002063,
1808
+ 2003034995,
1809
+ 1747873779,
1810
+ 3602036899,
1811
+ 1955562222,
1812
+ 1575990012,
1813
+ 2024104815,
1814
+ 1125592928,
1815
+ 2227730452,
1816
+ 2716904306,
1817
+ 2361852424,
1818
+ 442776044,
1819
+ 2428436474,
1820
+ 593698344,
1821
+ 2756734187,
1822
+ 3733110249,
1823
+ 3204031479,
1824
+ 2999351573,
1825
+ 3329325298,
1826
+ 3815920427,
1827
+ 3391569614,
1828
+ 3928383900,
1829
+ 3515267271,
1830
+ 566280711,
1831
+ 3940187606,
1832
+ 3454069534,
1833
+ 4118630271,
1834
+ 4000239992,
1835
+ 116418474,
1836
+ 1914138554,
1837
+ 174292421,
1838
+ 2731055270,
1839
+ 289380356,
1840
+ 3203993006,
1841
+ 460393269,
1842
+ 320620315,
1843
+ 685471733,
1844
+ 587496836,
1845
+ 852142971,
1846
+ 1086792851,
1847
+ 1017036298,
1848
+ 365543100,
1849
+ 1126000580,
1850
+ 2618297676,
1851
+ 1288033470,
1852
+ 3409855158,
1853
+ 1501505948,
1854
+ 4234509866,
1855
+ 1607167915,
1856
+ 987167468,
1857
+ 1816402316,
1858
+ 1246189591
1859
+ ]);
1860
+ function hashBlocks(wh, wl, hh, hl, m, pos, len) {
1861
+ let ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
1862
+ let h, l;
1863
+ let th, tl;
1864
+ let a, b, c, d;
1865
+ while (len >= 128) {
1866
+ for (let i = 0; i < 16; i++) {
1867
+ const j = 8 * i + pos;
1868
+ wh[i] = readUint32BE(m, j);
1869
+ wl[i] = readUint32BE(m, j + 4);
1870
+ }
1871
+ for (let i = 0; i < 80; i++) {
1872
+ let bh0 = ah0;
1873
+ let bh1 = ah1;
1874
+ let bh2 = ah2;
1875
+ let bh3 = ah3;
1876
+ let bh4 = ah4;
1877
+ let bh5 = ah5;
1878
+ let bh6 = ah6;
1879
+ let bh7 = ah7;
1880
+ let bl0 = al0;
1881
+ let bl1 = al1;
1882
+ let bl2 = al2;
1883
+ let bl3 = al3;
1884
+ let bl4 = al4;
1885
+ let bl5 = al5;
1886
+ let bl6 = al6;
1887
+ let bl7 = al7;
1888
+ h = ah7;
1889
+ l = al7;
1890
+ a = l & 65535;
1891
+ b = l >>> 16;
1892
+ c = h & 65535;
1893
+ d = h >>> 16;
1894
+ h = (ah4 >>> 14 | al4 << 18) ^ (ah4 >>> 18 | al4 << 14) ^ (al4 >>> 9 | ah4 << 23);
1895
+ l = (al4 >>> 14 | ah4 << 18) ^ (al4 >>> 18 | ah4 << 14) ^ (ah4 >>> 9 | al4 << 23);
1896
+ a += l & 65535;
1897
+ b += l >>> 16;
1898
+ c += h & 65535;
1899
+ d += h >>> 16;
1900
+ h = ah4 & ah5 ^ ~ah4 & ah6;
1901
+ l = al4 & al5 ^ ~al4 & al6;
1902
+ a += l & 65535;
1903
+ b += l >>> 16;
1904
+ c += h & 65535;
1905
+ d += h >>> 16;
1906
+ h = K[i * 2];
1907
+ l = K[i * 2 + 1];
1908
+ a += l & 65535;
1909
+ b += l >>> 16;
1910
+ c += h & 65535;
1911
+ d += h >>> 16;
1912
+ h = wh[i % 16];
1913
+ l = wl[i % 16];
1914
+ a += l & 65535;
1915
+ b += l >>> 16;
1916
+ c += h & 65535;
1917
+ d += h >>> 16;
1918
+ b += a >>> 16;
1919
+ c += b >>> 16;
1920
+ d += c >>> 16;
1921
+ th = c & 65535 | d << 16;
1922
+ tl = a & 65535 | b << 16;
1923
+ h = th;
1924
+ l = tl;
1925
+ a = l & 65535;
1926
+ b = l >>> 16;
1927
+ c = h & 65535;
1928
+ d = h >>> 16;
1929
+ h = (ah0 >>> 28 | al0 << 4) ^ (al0 >>> 2 | ah0 << 30) ^ (al0 >>> 7 | ah0 << 25);
1930
+ l = (al0 >>> 28 | ah0 << 4) ^ (ah0 >>> 2 | al0 << 30) ^ (ah0 >>> 7 | al0 << 25);
1931
+ a += l & 65535;
1932
+ b += l >>> 16;
1933
+ c += h & 65535;
1934
+ d += h >>> 16;
1935
+ h = ah0 & ah1 ^ ah0 & ah2 ^ ah1 & ah2;
1936
+ l = al0 & al1 ^ al0 & al2 ^ al1 & al2;
1937
+ a += l & 65535;
1938
+ b += l >>> 16;
1939
+ c += h & 65535;
1940
+ d += h >>> 16;
1941
+ b += a >>> 16;
1942
+ c += b >>> 16;
1943
+ d += c >>> 16;
1944
+ bh7 = c & 65535 | d << 16;
1945
+ bl7 = a & 65535 | b << 16;
1946
+ h = bh3;
1947
+ l = bl3;
1948
+ a = l & 65535;
1949
+ b = l >>> 16;
1950
+ c = h & 65535;
1951
+ d = h >>> 16;
1952
+ h = th;
1953
+ l = tl;
1954
+ a += l & 65535;
1955
+ b += l >>> 16;
1956
+ c += h & 65535;
1957
+ d += h >>> 16;
1958
+ b += a >>> 16;
1959
+ c += b >>> 16;
1960
+ d += c >>> 16;
1961
+ bh3 = c & 65535 | d << 16;
1962
+ bl3 = a & 65535 | b << 16;
1963
+ ah1 = bh0;
1964
+ ah2 = bh1;
1965
+ ah3 = bh2;
1966
+ ah4 = bh3;
1967
+ ah5 = bh4;
1968
+ ah6 = bh5;
1969
+ ah7 = bh6;
1970
+ ah0 = bh7;
1971
+ al1 = bl0;
1972
+ al2 = bl1;
1973
+ al3 = bl2;
1974
+ al4 = bl3;
1975
+ al5 = bl4;
1976
+ al6 = bl5;
1977
+ al7 = bl6;
1978
+ al0 = bl7;
1979
+ if (i % 16 === 15) for (let j = 0; j < 16; j++) {
1980
+ h = wh[j];
1981
+ l = wl[j];
1982
+ a = l & 65535;
1983
+ b = l >>> 16;
1984
+ c = h & 65535;
1985
+ d = h >>> 16;
1986
+ h = wh[(j + 9) % 16];
1987
+ l = wl[(j + 9) % 16];
1988
+ a += l & 65535;
1989
+ b += l >>> 16;
1990
+ c += h & 65535;
1991
+ d += h >>> 16;
1992
+ th = wh[(j + 1) % 16];
1993
+ tl = wl[(j + 1) % 16];
1994
+ h = (th >>> 1 | tl << 31) ^ (th >>> 8 | tl << 24) ^ th >>> 7;
1995
+ l = (tl >>> 1 | th << 31) ^ (tl >>> 8 | th << 24) ^ (tl >>> 7 | th << 25);
1996
+ a += l & 65535;
1997
+ b += l >>> 16;
1998
+ c += h & 65535;
1999
+ d += h >>> 16;
2000
+ th = wh[(j + 14) % 16];
2001
+ tl = wl[(j + 14) % 16];
2002
+ h = (th >>> 19 | tl << 13) ^ (tl >>> 29 | th << 3) ^ th >>> 6;
2003
+ l = (tl >>> 19 | th << 13) ^ (th >>> 29 | tl << 3) ^ (tl >>> 6 | th << 26);
2004
+ a += l & 65535;
2005
+ b += l >>> 16;
2006
+ c += h & 65535;
2007
+ d += h >>> 16;
2008
+ b += a >>> 16;
2009
+ c += b >>> 16;
2010
+ d += c >>> 16;
2011
+ wh[j] = c & 65535 | d << 16;
2012
+ wl[j] = a & 65535 | b << 16;
2013
+ }
2014
+ }
2015
+ h = ah0;
2016
+ l = al0;
2017
+ a = l & 65535;
2018
+ b = l >>> 16;
2019
+ c = h & 65535;
2020
+ d = h >>> 16;
2021
+ h = hh[0];
2022
+ l = hl[0];
2023
+ a += l & 65535;
2024
+ b += l >>> 16;
2025
+ c += h & 65535;
2026
+ d += h >>> 16;
2027
+ b += a >>> 16;
2028
+ c += b >>> 16;
2029
+ d += c >>> 16;
2030
+ hh[0] = ah0 = c & 65535 | d << 16;
2031
+ hl[0] = al0 = a & 65535 | b << 16;
2032
+ h = ah1;
2033
+ l = al1;
2034
+ a = l & 65535;
2035
+ b = l >>> 16;
2036
+ c = h & 65535;
2037
+ d = h >>> 16;
2038
+ h = hh[1];
2039
+ l = hl[1];
2040
+ a += l & 65535;
2041
+ b += l >>> 16;
2042
+ c += h & 65535;
2043
+ d += h >>> 16;
2044
+ b += a >>> 16;
2045
+ c += b >>> 16;
2046
+ d += c >>> 16;
2047
+ hh[1] = ah1 = c & 65535 | d << 16;
2048
+ hl[1] = al1 = a & 65535 | b << 16;
2049
+ h = ah2;
2050
+ l = al2;
2051
+ a = l & 65535;
2052
+ b = l >>> 16;
2053
+ c = h & 65535;
2054
+ d = h >>> 16;
2055
+ h = hh[2];
2056
+ l = hl[2];
2057
+ a += l & 65535;
2058
+ b += l >>> 16;
2059
+ c += h & 65535;
2060
+ d += h >>> 16;
2061
+ b += a >>> 16;
2062
+ c += b >>> 16;
2063
+ d += c >>> 16;
2064
+ hh[2] = ah2 = c & 65535 | d << 16;
2065
+ hl[2] = al2 = a & 65535 | b << 16;
2066
+ h = ah3;
2067
+ l = al3;
2068
+ a = l & 65535;
2069
+ b = l >>> 16;
2070
+ c = h & 65535;
2071
+ d = h >>> 16;
2072
+ h = hh[3];
2073
+ l = hl[3];
2074
+ a += l & 65535;
2075
+ b += l >>> 16;
2076
+ c += h & 65535;
2077
+ d += h >>> 16;
2078
+ b += a >>> 16;
2079
+ c += b >>> 16;
2080
+ d += c >>> 16;
2081
+ hh[3] = ah3 = c & 65535 | d << 16;
2082
+ hl[3] = al3 = a & 65535 | b << 16;
2083
+ h = ah4;
2084
+ l = al4;
2085
+ a = l & 65535;
2086
+ b = l >>> 16;
2087
+ c = h & 65535;
2088
+ d = h >>> 16;
2089
+ h = hh[4];
2090
+ l = hl[4];
2091
+ a += l & 65535;
2092
+ b += l >>> 16;
2093
+ c += h & 65535;
2094
+ d += h >>> 16;
2095
+ b += a >>> 16;
2096
+ c += b >>> 16;
2097
+ d += c >>> 16;
2098
+ hh[4] = ah4 = c & 65535 | d << 16;
2099
+ hl[4] = al4 = a & 65535 | b << 16;
2100
+ h = ah5;
2101
+ l = al5;
2102
+ a = l & 65535;
2103
+ b = l >>> 16;
2104
+ c = h & 65535;
2105
+ d = h >>> 16;
2106
+ h = hh[5];
2107
+ l = hl[5];
2108
+ a += l & 65535;
2109
+ b += l >>> 16;
2110
+ c += h & 65535;
2111
+ d += h >>> 16;
2112
+ b += a >>> 16;
2113
+ c += b >>> 16;
2114
+ d += c >>> 16;
2115
+ hh[5] = ah5 = c & 65535 | d << 16;
2116
+ hl[5] = al5 = a & 65535 | b << 16;
2117
+ h = ah6;
2118
+ l = al6;
2119
+ a = l & 65535;
2120
+ b = l >>> 16;
2121
+ c = h & 65535;
2122
+ d = h >>> 16;
2123
+ h = hh[6];
2124
+ l = hl[6];
2125
+ a += l & 65535;
2126
+ b += l >>> 16;
2127
+ c += h & 65535;
2128
+ d += h >>> 16;
2129
+ b += a >>> 16;
2130
+ c += b >>> 16;
2131
+ d += c >>> 16;
2132
+ hh[6] = ah6 = c & 65535 | d << 16;
2133
+ hl[6] = al6 = a & 65535 | b << 16;
2134
+ h = ah7;
2135
+ l = al7;
2136
+ a = l & 65535;
2137
+ b = l >>> 16;
2138
+ c = h & 65535;
2139
+ d = h >>> 16;
2140
+ h = hh[7];
2141
+ l = hl[7];
2142
+ a += l & 65535;
2143
+ b += l >>> 16;
2144
+ c += h & 65535;
2145
+ d += h >>> 16;
2146
+ b += a >>> 16;
2147
+ c += b >>> 16;
2148
+ d += c >>> 16;
2149
+ hh[7] = ah7 = c & 65535 | d << 16;
2150
+ hl[7] = al7 = a & 65535 | b << 16;
2151
+ pos += 128;
2152
+ len -= 128;
2153
+ }
2154
+ return pos;
2155
+ }
2156
+ function hash(data) {
2157
+ const h = new SHA512();
2158
+ h.update(data);
2159
+ const digest = h.digest();
2160
+ h.clean();
2161
+ return digest;
2162
+ }
2163
+
2164
+ //#endregion
2165
+ //#region ../../node_modules/.bun/@stablelib+ed25519@2.1.0/node_modules/@stablelib/ed25519/lib/ed25519.js
2166
+ const SIGNATURE_LENGTH = 64;
2167
+ const PUBLIC_KEY_LENGTH = 32;
2168
+ const SECRET_KEY_LENGTH = 64;
2169
+ const SEED_LENGTH = 32;
2170
+ function gf(init) {
2171
+ const r = new Float64Array(16);
2172
+ if (init) for (let i = 0; i < init.length; i++) r[i] = init[i];
2173
+ return r;
2174
+ }
2175
+ const _9 = new Uint8Array(32);
2176
+ _9[0] = 9;
2177
+ const gf0 = gf();
2178
+ const gf1 = gf([1]);
2179
+ const D = gf([
2180
+ 30883,
2181
+ 4953,
2182
+ 19914,
2183
+ 30187,
2184
+ 55467,
2185
+ 16705,
2186
+ 2637,
2187
+ 112,
2188
+ 59544,
2189
+ 30585,
2190
+ 16505,
2191
+ 36039,
2192
+ 65139,
2193
+ 11119,
2194
+ 27886,
2195
+ 20995
2196
+ ]);
2197
+ const D2 = gf([
2198
+ 61785,
2199
+ 9906,
2200
+ 39828,
2201
+ 60374,
2202
+ 45398,
2203
+ 33411,
2204
+ 5274,
2205
+ 224,
2206
+ 53552,
2207
+ 61171,
2208
+ 33010,
2209
+ 6542,
2210
+ 64743,
2211
+ 22239,
2212
+ 55772,
2213
+ 9222
2214
+ ]);
2215
+ const X = gf([
2216
+ 54554,
2217
+ 36645,
2218
+ 11616,
2219
+ 51542,
2220
+ 42930,
2221
+ 38181,
2222
+ 51040,
2223
+ 26924,
2224
+ 56412,
2225
+ 64982,
2226
+ 57905,
2227
+ 49316,
2228
+ 21502,
2229
+ 52590,
2230
+ 14035,
2231
+ 8553
2232
+ ]);
2233
+ const Y = gf([
2234
+ 26200,
2235
+ 26214,
2236
+ 26214,
2237
+ 26214,
2238
+ 26214,
2239
+ 26214,
2240
+ 26214,
2241
+ 26214,
2242
+ 26214,
2243
+ 26214,
2244
+ 26214,
2245
+ 26214,
2246
+ 26214,
2247
+ 26214,
2248
+ 26214,
2249
+ 26214
2250
+ ]);
2251
+ const I = gf([
2252
+ 41136,
2253
+ 18958,
2254
+ 6951,
2255
+ 50414,
2256
+ 58488,
2257
+ 44335,
2258
+ 6150,
2259
+ 12099,
2260
+ 55207,
2261
+ 15867,
2262
+ 153,
2263
+ 11085,
2264
+ 57099,
2265
+ 20417,
2266
+ 9344,
2267
+ 11139
2268
+ ]);
2269
+ function set25519(r, a) {
2270
+ for (let i = 0; i < 16; i++) r[i] = a[i] | 0;
2271
+ }
2272
+ function car25519(o) {
2273
+ let c = 1;
2274
+ for (let i = 0; i < 16; i++) {
2275
+ let v = o[i] + c + 65535;
2276
+ c = Math.floor(v / 65536);
2277
+ o[i] = v - c * 65536;
2278
+ }
2279
+ o[0] += c - 1 + 37 * (c - 1);
2280
+ }
2281
+ function sel25519(p, q, b) {
2282
+ const c = ~(b - 1);
2283
+ for (let i = 0; i < 16; i++) {
2284
+ const t = c & (p[i] ^ q[i]);
2285
+ p[i] ^= t;
2286
+ q[i] ^= t;
2287
+ }
2288
+ }
2289
+ function pack25519(o, n) {
2290
+ const m = gf();
2291
+ const t = gf();
2292
+ for (let i = 0; i < 16; i++) t[i] = n[i];
2293
+ car25519(t);
2294
+ car25519(t);
2295
+ car25519(t);
2296
+ for (let j = 0; j < 2; j++) {
2297
+ m[0] = t[0] - 65517;
2298
+ for (let i = 1; i < 15; i++) {
2299
+ m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
2300
+ m[i - 1] &= 65535;
2301
+ }
2302
+ m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
2303
+ const b = m[15] >> 16 & 1;
2304
+ m[14] &= 65535;
2305
+ sel25519(t, m, 1 - b);
2306
+ }
2307
+ for (let i = 0; i < 16; i++) {
2308
+ o[2 * i] = t[i] & 255;
2309
+ o[2 * i + 1] = t[i] >> 8;
2310
+ }
2311
+ }
2312
+ function verify32(x, y) {
2313
+ let d = 0;
2314
+ for (let i = 0; i < 32; i++) d |= x[i] ^ y[i];
2315
+ return (1 & d - 1 >>> 8) - 1;
2316
+ }
2317
+ function neq25519(a, b) {
2318
+ const c = new Uint8Array(32);
2319
+ const d = new Uint8Array(32);
2320
+ pack25519(c, a);
2321
+ pack25519(d, b);
2322
+ return verify32(c, d);
2323
+ }
2324
+ function par25519(a) {
2325
+ const d = new Uint8Array(32);
2326
+ pack25519(d, a);
2327
+ return d[0] & 1;
2328
+ }
2329
+ function unpack25519(o, n) {
2330
+ for (let i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
2331
+ o[15] &= 32767;
2332
+ }
2333
+ function add(o, a, b) {
2334
+ for (let i = 0; i < 16; i++) o[i] = a[i] + b[i];
2335
+ }
2336
+ function sub(o, a, b) {
2337
+ for (let i = 0; i < 16; i++) o[i] = a[i] - b[i];
2338
+ }
2339
+ function mul(o, a, b) {
2340
+ let v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0, t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0, b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15];
2341
+ v = a[0];
2342
+ t0 += v * b0;
2343
+ t1 += v * b1;
2344
+ t2 += v * b2;
2345
+ t3 += v * b3;
2346
+ t4 += v * b4;
2347
+ t5 += v * b5;
2348
+ t6 += v * b6;
2349
+ t7 += v * b7;
2350
+ t8 += v * b8;
2351
+ t9 += v * b9;
2352
+ t10 += v * b10;
2353
+ t11 += v * b11;
2354
+ t12 += v * b12;
2355
+ t13 += v * b13;
2356
+ t14 += v * b14;
2357
+ t15 += v * b15;
2358
+ v = a[1];
2359
+ t1 += v * b0;
2360
+ t2 += v * b1;
2361
+ t3 += v * b2;
2362
+ t4 += v * b3;
2363
+ t5 += v * b4;
2364
+ t6 += v * b5;
2365
+ t7 += v * b6;
2366
+ t8 += v * b7;
2367
+ t9 += v * b8;
2368
+ t10 += v * b9;
2369
+ t11 += v * b10;
2370
+ t12 += v * b11;
2371
+ t13 += v * b12;
2372
+ t14 += v * b13;
2373
+ t15 += v * b14;
2374
+ t16 += v * b15;
2375
+ v = a[2];
2376
+ t2 += v * b0;
2377
+ t3 += v * b1;
2378
+ t4 += v * b2;
2379
+ t5 += v * b3;
2380
+ t6 += v * b4;
2381
+ t7 += v * b5;
2382
+ t8 += v * b6;
2383
+ t9 += v * b7;
2384
+ t10 += v * b8;
2385
+ t11 += v * b9;
2386
+ t12 += v * b10;
2387
+ t13 += v * b11;
2388
+ t14 += v * b12;
2389
+ t15 += v * b13;
2390
+ t16 += v * b14;
2391
+ t17 += v * b15;
2392
+ v = a[3];
2393
+ t3 += v * b0;
2394
+ t4 += v * b1;
2395
+ t5 += v * b2;
2396
+ t6 += v * b3;
2397
+ t7 += v * b4;
2398
+ t8 += v * b5;
2399
+ t9 += v * b6;
2400
+ t10 += v * b7;
2401
+ t11 += v * b8;
2402
+ t12 += v * b9;
2403
+ t13 += v * b10;
2404
+ t14 += v * b11;
2405
+ t15 += v * b12;
2406
+ t16 += v * b13;
2407
+ t17 += v * b14;
2408
+ t18 += v * b15;
2409
+ v = a[4];
2410
+ t4 += v * b0;
2411
+ t5 += v * b1;
2412
+ t6 += v * b2;
2413
+ t7 += v * b3;
2414
+ t8 += v * b4;
2415
+ t9 += v * b5;
2416
+ t10 += v * b6;
2417
+ t11 += v * b7;
2418
+ t12 += v * b8;
2419
+ t13 += v * b9;
2420
+ t14 += v * b10;
2421
+ t15 += v * b11;
2422
+ t16 += v * b12;
2423
+ t17 += v * b13;
2424
+ t18 += v * b14;
2425
+ t19 += v * b15;
2426
+ v = a[5];
2427
+ t5 += v * b0;
2428
+ t6 += v * b1;
2429
+ t7 += v * b2;
2430
+ t8 += v * b3;
2431
+ t9 += v * b4;
2432
+ t10 += v * b5;
2433
+ t11 += v * b6;
2434
+ t12 += v * b7;
2435
+ t13 += v * b8;
2436
+ t14 += v * b9;
2437
+ t15 += v * b10;
2438
+ t16 += v * b11;
2439
+ t17 += v * b12;
2440
+ t18 += v * b13;
2441
+ t19 += v * b14;
2442
+ t20 += v * b15;
2443
+ v = a[6];
2444
+ t6 += v * b0;
2445
+ t7 += v * b1;
2446
+ t8 += v * b2;
2447
+ t9 += v * b3;
2448
+ t10 += v * b4;
2449
+ t11 += v * b5;
2450
+ t12 += v * b6;
2451
+ t13 += v * b7;
2452
+ t14 += v * b8;
2453
+ t15 += v * b9;
2454
+ t16 += v * b10;
2455
+ t17 += v * b11;
2456
+ t18 += v * b12;
2457
+ t19 += v * b13;
2458
+ t20 += v * b14;
2459
+ t21 += v * b15;
2460
+ v = a[7];
2461
+ t7 += v * b0;
2462
+ t8 += v * b1;
2463
+ t9 += v * b2;
2464
+ t10 += v * b3;
2465
+ t11 += v * b4;
2466
+ t12 += v * b5;
2467
+ t13 += v * b6;
2468
+ t14 += v * b7;
2469
+ t15 += v * b8;
2470
+ t16 += v * b9;
2471
+ t17 += v * b10;
2472
+ t18 += v * b11;
2473
+ t19 += v * b12;
2474
+ t20 += v * b13;
2475
+ t21 += v * b14;
2476
+ t22 += v * b15;
2477
+ v = a[8];
2478
+ t8 += v * b0;
2479
+ t9 += v * b1;
2480
+ t10 += v * b2;
2481
+ t11 += v * b3;
2482
+ t12 += v * b4;
2483
+ t13 += v * b5;
2484
+ t14 += v * b6;
2485
+ t15 += v * b7;
2486
+ t16 += v * b8;
2487
+ t17 += v * b9;
2488
+ t18 += v * b10;
2489
+ t19 += v * b11;
2490
+ t20 += v * b12;
2491
+ t21 += v * b13;
2492
+ t22 += v * b14;
2493
+ t23 += v * b15;
2494
+ v = a[9];
2495
+ t9 += v * b0;
2496
+ t10 += v * b1;
2497
+ t11 += v * b2;
2498
+ t12 += v * b3;
2499
+ t13 += v * b4;
2500
+ t14 += v * b5;
2501
+ t15 += v * b6;
2502
+ t16 += v * b7;
2503
+ t17 += v * b8;
2504
+ t18 += v * b9;
2505
+ t19 += v * b10;
2506
+ t20 += v * b11;
2507
+ t21 += v * b12;
2508
+ t22 += v * b13;
2509
+ t23 += v * b14;
2510
+ t24 += v * b15;
2511
+ v = a[10];
2512
+ t10 += v * b0;
2513
+ t11 += v * b1;
2514
+ t12 += v * b2;
2515
+ t13 += v * b3;
2516
+ t14 += v * b4;
2517
+ t15 += v * b5;
2518
+ t16 += v * b6;
2519
+ t17 += v * b7;
2520
+ t18 += v * b8;
2521
+ t19 += v * b9;
2522
+ t20 += v * b10;
2523
+ t21 += v * b11;
2524
+ t22 += v * b12;
2525
+ t23 += v * b13;
2526
+ t24 += v * b14;
2527
+ t25 += v * b15;
2528
+ v = a[11];
2529
+ t11 += v * b0;
2530
+ t12 += v * b1;
2531
+ t13 += v * b2;
2532
+ t14 += v * b3;
2533
+ t15 += v * b4;
2534
+ t16 += v * b5;
2535
+ t17 += v * b6;
2536
+ t18 += v * b7;
2537
+ t19 += v * b8;
2538
+ t20 += v * b9;
2539
+ t21 += v * b10;
2540
+ t22 += v * b11;
2541
+ t23 += v * b12;
2542
+ t24 += v * b13;
2543
+ t25 += v * b14;
2544
+ t26 += v * b15;
2545
+ v = a[12];
2546
+ t12 += v * b0;
2547
+ t13 += v * b1;
2548
+ t14 += v * b2;
2549
+ t15 += v * b3;
2550
+ t16 += v * b4;
2551
+ t17 += v * b5;
2552
+ t18 += v * b6;
2553
+ t19 += v * b7;
2554
+ t20 += v * b8;
2555
+ t21 += v * b9;
2556
+ t22 += v * b10;
2557
+ t23 += v * b11;
2558
+ t24 += v * b12;
2559
+ t25 += v * b13;
2560
+ t26 += v * b14;
2561
+ t27 += v * b15;
2562
+ v = a[13];
2563
+ t13 += v * b0;
2564
+ t14 += v * b1;
2565
+ t15 += v * b2;
2566
+ t16 += v * b3;
2567
+ t17 += v * b4;
2568
+ t18 += v * b5;
2569
+ t19 += v * b6;
2570
+ t20 += v * b7;
2571
+ t21 += v * b8;
2572
+ t22 += v * b9;
2573
+ t23 += v * b10;
2574
+ t24 += v * b11;
2575
+ t25 += v * b12;
2576
+ t26 += v * b13;
2577
+ t27 += v * b14;
2578
+ t28 += v * b15;
2579
+ v = a[14];
2580
+ t14 += v * b0;
2581
+ t15 += v * b1;
2582
+ t16 += v * b2;
2583
+ t17 += v * b3;
2584
+ t18 += v * b4;
2585
+ t19 += v * b5;
2586
+ t20 += v * b6;
2587
+ t21 += v * b7;
2588
+ t22 += v * b8;
2589
+ t23 += v * b9;
2590
+ t24 += v * b10;
2591
+ t25 += v * b11;
2592
+ t26 += v * b12;
2593
+ t27 += v * b13;
2594
+ t28 += v * b14;
2595
+ t29 += v * b15;
2596
+ v = a[15];
2597
+ t15 += v * b0;
2598
+ t16 += v * b1;
2599
+ t17 += v * b2;
2600
+ t18 += v * b3;
2601
+ t19 += v * b4;
2602
+ t20 += v * b5;
2603
+ t21 += v * b6;
2604
+ t22 += v * b7;
2605
+ t23 += v * b8;
2606
+ t24 += v * b9;
2607
+ t25 += v * b10;
2608
+ t26 += v * b11;
2609
+ t27 += v * b12;
2610
+ t28 += v * b13;
2611
+ t29 += v * b14;
2612
+ t30 += v * b15;
2613
+ t0 += 38 * t16;
2614
+ t1 += 38 * t17;
2615
+ t2 += 38 * t18;
2616
+ t3 += 38 * t19;
2617
+ t4 += 38 * t20;
2618
+ t5 += 38 * t21;
2619
+ t6 += 38 * t22;
2620
+ t7 += 38 * t23;
2621
+ t8 += 38 * t24;
2622
+ t9 += 38 * t25;
2623
+ t10 += 38 * t26;
2624
+ t11 += 38 * t27;
2625
+ t12 += 38 * t28;
2626
+ t13 += 38 * t29;
2627
+ t14 += 38 * t30;
2628
+ c = 1;
2629
+ v = t0 + c + 65535;
2630
+ c = Math.floor(v / 65536);
2631
+ t0 = v - c * 65536;
2632
+ v = t1 + c + 65535;
2633
+ c = Math.floor(v / 65536);
2634
+ t1 = v - c * 65536;
2635
+ v = t2 + c + 65535;
2636
+ c = Math.floor(v / 65536);
2637
+ t2 = v - c * 65536;
2638
+ v = t3 + c + 65535;
2639
+ c = Math.floor(v / 65536);
2640
+ t3 = v - c * 65536;
2641
+ v = t4 + c + 65535;
2642
+ c = Math.floor(v / 65536);
2643
+ t4 = v - c * 65536;
2644
+ v = t5 + c + 65535;
2645
+ c = Math.floor(v / 65536);
2646
+ t5 = v - c * 65536;
2647
+ v = t6 + c + 65535;
2648
+ c = Math.floor(v / 65536);
2649
+ t6 = v - c * 65536;
2650
+ v = t7 + c + 65535;
2651
+ c = Math.floor(v / 65536);
2652
+ t7 = v - c * 65536;
2653
+ v = t8 + c + 65535;
2654
+ c = Math.floor(v / 65536);
2655
+ t8 = v - c * 65536;
2656
+ v = t9 + c + 65535;
2657
+ c = Math.floor(v / 65536);
2658
+ t9 = v - c * 65536;
2659
+ v = t10 + c + 65535;
2660
+ c = Math.floor(v / 65536);
2661
+ t10 = v - c * 65536;
2662
+ v = t11 + c + 65535;
2663
+ c = Math.floor(v / 65536);
2664
+ t11 = v - c * 65536;
2665
+ v = t12 + c + 65535;
2666
+ c = Math.floor(v / 65536);
2667
+ t12 = v - c * 65536;
2668
+ v = t13 + c + 65535;
2669
+ c = Math.floor(v / 65536);
2670
+ t13 = v - c * 65536;
2671
+ v = t14 + c + 65535;
2672
+ c = Math.floor(v / 65536);
2673
+ t14 = v - c * 65536;
2674
+ v = t15 + c + 65535;
2675
+ c = Math.floor(v / 65536);
2676
+ t15 = v - c * 65536;
2677
+ t0 += c - 1 + 37 * (c - 1);
2678
+ c = 1;
2679
+ v = t0 + c + 65535;
2680
+ c = Math.floor(v / 65536);
2681
+ t0 = v - c * 65536;
2682
+ v = t1 + c + 65535;
2683
+ c = Math.floor(v / 65536);
2684
+ t1 = v - c * 65536;
2685
+ v = t2 + c + 65535;
2686
+ c = Math.floor(v / 65536);
2687
+ t2 = v - c * 65536;
2688
+ v = t3 + c + 65535;
2689
+ c = Math.floor(v / 65536);
2690
+ t3 = v - c * 65536;
2691
+ v = t4 + c + 65535;
2692
+ c = Math.floor(v / 65536);
2693
+ t4 = v - c * 65536;
2694
+ v = t5 + c + 65535;
2695
+ c = Math.floor(v / 65536);
2696
+ t5 = v - c * 65536;
2697
+ v = t6 + c + 65535;
2698
+ c = Math.floor(v / 65536);
2699
+ t6 = v - c * 65536;
2700
+ v = t7 + c + 65535;
2701
+ c = Math.floor(v / 65536);
2702
+ t7 = v - c * 65536;
2703
+ v = t8 + c + 65535;
2704
+ c = Math.floor(v / 65536);
2705
+ t8 = v - c * 65536;
2706
+ v = t9 + c + 65535;
2707
+ c = Math.floor(v / 65536);
2708
+ t9 = v - c * 65536;
2709
+ v = t10 + c + 65535;
2710
+ c = Math.floor(v / 65536);
2711
+ t10 = v - c * 65536;
2712
+ v = t11 + c + 65535;
2713
+ c = Math.floor(v / 65536);
2714
+ t11 = v - c * 65536;
2715
+ v = t12 + c + 65535;
2716
+ c = Math.floor(v / 65536);
2717
+ t12 = v - c * 65536;
2718
+ v = t13 + c + 65535;
2719
+ c = Math.floor(v / 65536);
2720
+ t13 = v - c * 65536;
2721
+ v = t14 + c + 65535;
2722
+ c = Math.floor(v / 65536);
2723
+ t14 = v - c * 65536;
2724
+ v = t15 + c + 65535;
2725
+ c = Math.floor(v / 65536);
2726
+ t15 = v - c * 65536;
2727
+ t0 += c - 1 + 37 * (c - 1);
2728
+ o[0] = t0;
2729
+ o[1] = t1;
2730
+ o[2] = t2;
2731
+ o[3] = t3;
2732
+ o[4] = t4;
2733
+ o[5] = t5;
2734
+ o[6] = t6;
2735
+ o[7] = t7;
2736
+ o[8] = t8;
2737
+ o[9] = t9;
2738
+ o[10] = t10;
2739
+ o[11] = t11;
2740
+ o[12] = t12;
2741
+ o[13] = t13;
2742
+ o[14] = t14;
2743
+ o[15] = t15;
2744
+ }
2745
+ function square(o, a) {
2746
+ mul(o, a, a);
2747
+ }
2748
+ function inv25519(o, i) {
2749
+ const c = gf();
2750
+ let a;
2751
+ for (a = 0; a < 16; a++) c[a] = i[a];
2752
+ for (a = 253; a >= 0; a--) {
2753
+ square(c, c);
2754
+ if (a !== 2 && a !== 4) mul(c, c, i);
2755
+ }
2756
+ for (a = 0; a < 16; a++) o[a] = c[a];
2757
+ }
2758
+ function pow2523(o, i) {
2759
+ const c = gf();
2760
+ let a;
2761
+ for (a = 0; a < 16; a++) c[a] = i[a];
2762
+ for (a = 250; a >= 0; a--) {
2763
+ square(c, c);
2764
+ if (a !== 1) mul(c, c, i);
2765
+ }
2766
+ for (a = 0; a < 16; a++) o[a] = c[a];
2767
+ }
2768
+ function edadd(p, q) {
2769
+ const a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
2770
+ sub(a, p[1], p[0]);
2771
+ sub(t, q[1], q[0]);
2772
+ mul(a, a, t);
2773
+ add(b, p[0], p[1]);
2774
+ add(t, q[0], q[1]);
2775
+ mul(b, b, t);
2776
+ mul(c, p[3], q[3]);
2777
+ mul(c, c, D2);
2778
+ mul(d, p[2], q[2]);
2779
+ add(d, d, d);
2780
+ sub(e, b, a);
2781
+ sub(f, d, c);
2782
+ add(g, d, c);
2783
+ add(h, b, a);
2784
+ mul(p[0], e, f);
2785
+ mul(p[1], h, g);
2786
+ mul(p[2], g, f);
2787
+ mul(p[3], e, h);
2788
+ }
2789
+ function cswap(p, q, b) {
2790
+ for (let i = 0; i < 4; i++) sel25519(p[i], q[i], b);
2791
+ }
2792
+ function pack(r, p) {
2793
+ const tx = gf(), ty = gf(), zi = gf();
2794
+ inv25519(zi, p[2]);
2795
+ mul(tx, p[0], zi);
2796
+ mul(ty, p[1], zi);
2797
+ pack25519(r, ty);
2798
+ r[31] ^= par25519(tx) << 7;
2799
+ }
2800
+ function scalarmult(p, q, s) {
2801
+ set25519(p[0], gf0);
2802
+ set25519(p[1], gf1);
2803
+ set25519(p[2], gf1);
2804
+ set25519(p[3], gf0);
2805
+ for (let i = 255; i >= 0; --i) {
2806
+ const b = s[i / 8 | 0] >> (i & 7) & 1;
2807
+ cswap(p, q, b);
2808
+ edadd(q, p);
2809
+ edadd(p, p);
2810
+ cswap(p, q, b);
2811
+ }
2812
+ }
2813
+ function scalarbase(p, s) {
2814
+ const q = [
2815
+ gf(),
2816
+ gf(),
2817
+ gf(),
2818
+ gf()
2819
+ ];
2820
+ set25519(q[0], X);
2821
+ set25519(q[1], Y);
2822
+ set25519(q[2], gf1);
2823
+ mul(q[3], X, Y);
2824
+ scalarmult(p, q, s);
2825
+ }
2826
+ function generateKeyPairFromSeed(seed) {
2827
+ if (seed.length !== 32) throw new Error(`ed25519: seed must be ${32} bytes`);
2828
+ const d = hash(seed);
2829
+ d[0] &= 248;
2830
+ d[31] &= 127;
2831
+ d[31] |= 64;
2832
+ const publicKey = new Uint8Array(32);
2833
+ const p = [
2834
+ gf(),
2835
+ gf(),
2836
+ gf(),
2837
+ gf()
2838
+ ];
2839
+ scalarbase(p, d);
2840
+ pack(publicKey, p);
2841
+ const secretKey = new Uint8Array(64);
2842
+ secretKey.set(seed);
2843
+ secretKey.set(publicKey, 32);
2844
+ return {
2845
+ publicKey,
2846
+ secretKey
2847
+ };
2848
+ }
2849
+ function generateKeyPair$1(prng) {
2850
+ const seed = randomBytes$1(32, prng);
2851
+ const result = generateKeyPairFromSeed(seed);
2852
+ wipe(seed);
2853
+ return result;
2854
+ }
2855
+ const L = new Uint8Array([
2856
+ 237,
2857
+ 211,
2858
+ 245,
2859
+ 92,
2860
+ 26,
2861
+ 99,
2862
+ 18,
2863
+ 88,
2864
+ 214,
2865
+ 156,
2866
+ 247,
2867
+ 162,
2868
+ 222,
2869
+ 249,
2870
+ 222,
2871
+ 20,
2872
+ 0,
2873
+ 0,
2874
+ 0,
2875
+ 0,
2876
+ 0,
2877
+ 0,
2878
+ 0,
2879
+ 0,
2880
+ 0,
2881
+ 0,
2882
+ 0,
2883
+ 0,
2884
+ 0,
2885
+ 0,
2886
+ 0,
2887
+ 16
2888
+ ]);
2889
+ function modL(r, x) {
2890
+ let carry;
2891
+ let i;
2892
+ let j;
2893
+ let k;
2894
+ for (i = 63; i >= 32; --i) {
2895
+ carry = 0;
2896
+ for (j = i - 32, k = i - 12; j < k; ++j) {
2897
+ x[j] += carry - 16 * x[i] * L[j - (i - 32)];
2898
+ carry = Math.floor((x[j] + 128) / 256);
2899
+ x[j] -= carry * 256;
2900
+ }
2901
+ x[j] += carry;
2902
+ x[i] = 0;
2903
+ }
2904
+ carry = 0;
2905
+ for (j = 0; j < 32; j++) {
2906
+ x[j] += carry - (x[31] >> 4) * L[j];
2907
+ carry = x[j] >> 8;
2908
+ x[j] &= 255;
2909
+ }
2910
+ for (j = 0; j < 32; j++) x[j] -= carry * L[j];
2911
+ for (i = 0; i < 32; i++) {
2912
+ x[i + 1] += x[i] >> 8;
2913
+ r[i] = x[i] & 255;
2914
+ }
2915
+ }
2916
+ function iscanonical(s) {
2917
+ let c = 0;
2918
+ let n = 1;
2919
+ for (let i = 31; i >= 0; i--) {
2920
+ c |= s[i] - L[i] >> 8 & n;
2921
+ n &= (s[i] ^ L[i]) - 1 >> 8;
2922
+ }
2923
+ return c !== 0;
2924
+ }
2925
+ function ispkcanonical(p) {
2926
+ let c = p[0] - 237 >> 8 & 1;
2927
+ for (let i = 1; i < 31; i++) c |= p[i] ^ 255;
2928
+ c |= p[31] & 127 ^ 127;
2929
+ return c !== 0;
2930
+ }
2931
+ function reduce(r) {
2932
+ const x = new Float64Array(64);
2933
+ for (let i = 0; i < 64; i++) x[i] = r[i];
2934
+ for (let i = 0; i < 64; i++) r[i] = 0;
2935
+ modL(r, x);
2936
+ }
2937
+ function sign$1(secretKey, message) {
2938
+ if (secretKey.length !== 64) throw new Error(`ed25519: secret key must be ${64} bytes`);
2939
+ const x = new Float64Array(64);
2940
+ const p = [
2941
+ gf(),
2942
+ gf(),
2943
+ gf(),
2944
+ gf()
2945
+ ];
2946
+ const d = hash(secretKey.subarray(0, 32));
2947
+ d[0] &= 248;
2948
+ d[31] &= 127;
2949
+ d[31] |= 64;
2950
+ const signature = new Uint8Array(64);
2951
+ signature.set(d.subarray(32), 32);
2952
+ const hs = new SHA512();
2953
+ hs.update(signature.subarray(32));
2954
+ hs.update(message);
2955
+ const r = hs.digest();
2956
+ hs.clean();
2957
+ reduce(r);
2958
+ scalarbase(p, r);
2959
+ pack(signature, p);
2960
+ hs.reset();
2961
+ hs.update(signature.subarray(0, 32));
2962
+ hs.update(secretKey.subarray(32));
2963
+ hs.update(message);
2964
+ const h = hs.digest();
2965
+ reduce(h);
2966
+ for (let i = 0; i < 32; i++) x[i] = r[i];
2967
+ for (let i = 0; i < 32; i++) for (let j = 0; j < 32; j++) x[i + j] += h[i] * d[j];
2968
+ modL(signature.subarray(32), x);
2969
+ return signature;
2970
+ }
2971
+ function unpackneg(r, p) {
2972
+ const t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
2973
+ set25519(r[2], gf1);
2974
+ unpack25519(r[1], p);
2975
+ square(num, r[1]);
2976
+ mul(den, num, D);
2977
+ sub(num, num, r[2]);
2978
+ add(den, r[2], den);
2979
+ square(den2, den);
2980
+ square(den4, den2);
2981
+ mul(den6, den4, den2);
2982
+ mul(t, den6, num);
2983
+ mul(t, t, den);
2984
+ pow2523(t, t);
2985
+ mul(t, t, num);
2986
+ mul(t, t, den);
2987
+ mul(t, t, den);
2988
+ mul(r[0], t, den);
2989
+ square(chk, r[0]);
2990
+ mul(chk, chk, den);
2991
+ if (neq25519(chk, num)) mul(r[0], r[0], I);
2992
+ square(chk, r[0]);
2993
+ mul(chk, chk, den);
2994
+ if (neq25519(chk, num)) return -1;
2995
+ if (par25519(r[0]) === p[31] >> 7) sub(r[0], gf0, r[0]);
2996
+ mul(r[3], r[0], r[1]);
2997
+ return 0;
2998
+ }
2999
+ function verify$1(publicKey, message, signature) {
3000
+ const t = new Uint8Array(32);
3001
+ const p = [
3002
+ gf(),
3003
+ gf(),
3004
+ gf(),
3005
+ gf()
3006
+ ];
3007
+ const q = [
3008
+ gf(),
3009
+ gf(),
3010
+ gf(),
3011
+ gf()
3012
+ ];
3013
+ if (signature.length !== 64) throw new Error(`ed25519: signature must be ${64} bytes`);
3014
+ if (publicKey.length !== 32) throw new Error(`ed25519: public key must be ${32} bytes`);
3015
+ if (!iscanonical(signature.subarray(32))) return false;
3016
+ if (!ispkcanonical(publicKey)) return false;
3017
+ if (unpackneg(q, publicKey)) return false;
3018
+ const hs = new SHA512();
3019
+ hs.update(signature.subarray(0, 32));
3020
+ hs.update(publicKey);
3021
+ hs.update(message);
3022
+ const h = hs.digest();
3023
+ reduce(h);
3024
+ scalarmult(p, q, h);
3025
+ scalarbase(q, signature.subarray(32));
3026
+ edadd(p, q);
3027
+ pack(t, p);
3028
+ if (verify32(signature, t)) return false;
3029
+ return true;
3030
+ }
3031
+
3032
+ //#endregion
3033
+ //#region src/crypto/ed25519.ts
3034
+ var ed25519_exports = /* @__PURE__ */ __exportAll({
3035
+ generateKeyPair: () => generateKeyPair,
3036
+ sign: () => sign,
3037
+ verify: () => verify
3038
+ });
3039
+ /**
3040
+ * Generates a new Ed25519 key pair for digital signature operations.
3041
+ */
3042
+ function generateKeyPair() {
3043
+ return generateKeyPair$1();
3044
+ }
3045
+ /** Signs a message with an Ed25519 secret key and returns the 64-byte signature. */
3046
+ function sign(message, secretKey) {
3047
+ return sign$1(secretKey, message);
3048
+ }
3049
+ /** Checks a 64-byte Ed25519 signature over a message against the signer's public key. */
3050
+ function verify(message, signature, publicKey) {
3051
+ return verify$1(publicKey, message, signature);
3052
+ }
3053
+
3054
+ //#endregion
3055
+ //#region src/crypto/hkdf.ts
3056
+ var hkdf_exports = /* @__PURE__ */ __exportAll({
3057
+ derive: () => derive,
3058
+ deriveEncryptionKeys: () => deriveEncryptionKeys
3059
+ });
3060
+ /** HKDF key derivation as defined in RFC 5869. */
3061
+ function derive(options) {
3062
+ return Buffer.from(hkdfSync(options.hash, options.key, options.salt, options.info, options.length));
3063
+ }
3064
+ /**
3065
+ * Derives 32-byte read and write keys with HKDF-SHA512. Salt and info strings depend on the protocol and stream.
3066
+ *
3067
+ * @param sharedSecret - Pair-verify or pair-setup shared secret.
3068
+ * @param salt - Protocol-specific salt, such as `Control-Salt`.
3069
+ * @param readInfo - Info string for the decrypt key.
3070
+ * @param writeInfo - Info string for the encrypt key.
3071
+ */
3072
+ function deriveEncryptionKeys(sharedSecret, salt, readInfo, writeInfo) {
3073
+ const saltBuffer = Buffer.from(salt);
3074
+ return {
3075
+ readKey: derive({
3076
+ hash: "sha512",
3077
+ key: sharedSecret,
3078
+ length: 32,
3079
+ salt: saltBuffer,
3080
+ info: Buffer.from(readInfo)
3081
+ }),
3082
+ writeKey: derive({
3083
+ hash: "sha512",
3084
+ key: sharedSecret,
3085
+ length: 32,
3086
+ salt: saltBuffer,
3087
+ info: Buffer.from(writeInfo)
3088
+ })
3089
+ };
3090
+ }
3091
+
3092
+ //#endregion
3093
+ export { generateKeyPair as a, curve25519_exports as c, chacha20_exports as d, decrypt as f, encrypt$1 as g, decrypt$1 as h, ed25519_exports as i, generateKeyPair$2 as l, aes_exports as m, deriveEncryptionKeys as n, sign as o, encrypt as p, hkdf_exports as r, verify as s, derive as t, generateSharedSecKey as u };