@waku/enr 0.0.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.
Files changed (52) hide show
  1. package/bundle/crypto-5b69130e.js +2914 -0
  2. package/bundle/crypto.js +1 -0
  3. package/bundle/index.js +30072 -0
  4. package/dist/constants.d.ts +4 -0
  5. package/dist/constants.js +8 -0
  6. package/dist/constants.js.map +1 -0
  7. package/dist/crypto.d.ts +22 -0
  8. package/dist/crypto.js +47 -0
  9. package/dist/crypto.js.map +1 -0
  10. package/dist/enr.d.ts +90 -0
  11. package/dist/enr.js +432 -0
  12. package/dist/enr.js.map +1 -0
  13. package/dist/index.d.ts +6 -0
  14. package/dist/index.js +7 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/keypair/index.d.ts +8 -0
  17. package/dist/keypair/index.js +53 -0
  18. package/dist/keypair/index.js.map +1 -0
  19. package/dist/keypair/secp256k1.d.ts +13 -0
  20. package/dist/keypair/secp256k1.js +57 -0
  21. package/dist/keypair/secp256k1.js.map +1 -0
  22. package/dist/keypair/types.d.ts +13 -0
  23. package/dist/keypair/types.js +7 -0
  24. package/dist/keypair/types.js.map +1 -0
  25. package/dist/multiaddr_from_fields.d.ts +2 -0
  26. package/dist/multiaddr_from_fields.js +8 -0
  27. package/dist/multiaddr_from_fields.js.map +1 -0
  28. package/dist/multiaddrs_codec.d.ts +3 -0
  29. package/dist/multiaddrs_codec.js +32 -0
  30. package/dist/multiaddrs_codec.js.map +1 -0
  31. package/dist/types.d.ts +8 -0
  32. package/dist/types.js +3 -0
  33. package/dist/types.js.map +1 -0
  34. package/dist/v4.d.ts +3 -0
  35. package/dist/v4.js +14 -0
  36. package/dist/v4.js.map +1 -0
  37. package/dist/waku2_codec.d.ts +8 -0
  38. package/dist/waku2_codec.js +36 -0
  39. package/dist/waku2_codec.js.map +1 -0
  40. package/package.json +191 -0
  41. package/src/constants.ts +10 -0
  42. package/src/crypto.ts +61 -0
  43. package/src/enr.ts +524 -0
  44. package/src/index.ts +6 -0
  45. package/src/keypair/index.ts +76 -0
  46. package/src/keypair/secp256k1.ts +69 -0
  47. package/src/keypair/types.ts +14 -0
  48. package/src/multiaddr_from_fields.ts +18 -0
  49. package/src/multiaddrs_codec.ts +50 -0
  50. package/src/types.ts +11 -0
  51. package/src/v4.ts +21 -0
  52. package/src/waku2_codec.ts +39 -0
@@ -0,0 +1,2914 @@
1
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
2
+
3
+ function getAugmentedNamespace(n) {
4
+ var f = n.default;
5
+ if (typeof f == "function") {
6
+ var a = function () {
7
+ return f.apply(this, arguments);
8
+ };
9
+ a.prototype = f.prototype;
10
+ } else a = {};
11
+ Object.defineProperty(a, '__esModule', {value: true});
12
+ Object.keys(n).forEach(function (k) {
13
+ var d = Object.getOwnPropertyDescriptor(n, k);
14
+ Object.defineProperty(a, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () {
17
+ return n[k];
18
+ }
19
+ });
20
+ });
21
+ return a;
22
+ }
23
+
24
+ // base-x encoding / decoding
25
+ // Copyright (c) 2018 base-x contributors
26
+ // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
27
+ // Distributed under the MIT software license, see the accompanying
28
+ // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
29
+ function base (ALPHABET, name) {
30
+ if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
31
+ var BASE_MAP = new Uint8Array(256);
32
+ for (var j = 0; j < BASE_MAP.length; j++) {
33
+ BASE_MAP[j] = 255;
34
+ }
35
+ for (var i = 0; i < ALPHABET.length; i++) {
36
+ var x = ALPHABET.charAt(i);
37
+ var xc = x.charCodeAt(0);
38
+ if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
39
+ BASE_MAP[xc] = i;
40
+ }
41
+ var BASE = ALPHABET.length;
42
+ var LEADER = ALPHABET.charAt(0);
43
+ var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
44
+ var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
45
+ function encode (source) {
46
+ if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
47
+ source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
48
+ } else if (Array.isArray(source)) {
49
+ source = Uint8Array.from(source);
50
+ }
51
+ if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
52
+ if (source.length === 0) { return '' }
53
+ // Skip & count leading zeroes.
54
+ var zeroes = 0;
55
+ var length = 0;
56
+ var pbegin = 0;
57
+ var pend = source.length;
58
+ while (pbegin !== pend && source[pbegin] === 0) {
59
+ pbegin++;
60
+ zeroes++;
61
+ }
62
+ // Allocate enough space in big-endian base58 representation.
63
+ var size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
64
+ var b58 = new Uint8Array(size);
65
+ // Process the bytes.
66
+ while (pbegin !== pend) {
67
+ var carry = source[pbegin];
68
+ // Apply "b58 = b58 * 256 + ch".
69
+ var i = 0;
70
+ for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
71
+ carry += (256 * b58[it1]) >>> 0;
72
+ b58[it1] = (carry % BASE) >>> 0;
73
+ carry = (carry / BASE) >>> 0;
74
+ }
75
+ if (carry !== 0) { throw new Error('Non-zero carry') }
76
+ length = i;
77
+ pbegin++;
78
+ }
79
+ // Skip leading zeroes in base58 result.
80
+ var it2 = size - length;
81
+ while (it2 !== size && b58[it2] === 0) {
82
+ it2++;
83
+ }
84
+ // Translate the result into a string.
85
+ var str = LEADER.repeat(zeroes);
86
+ for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]); }
87
+ return str
88
+ }
89
+ function decodeUnsafe (source) {
90
+ if (typeof source !== 'string') { throw new TypeError('Expected String') }
91
+ if (source.length === 0) { return new Uint8Array() }
92
+ var psz = 0;
93
+ // Skip leading spaces.
94
+ if (source[psz] === ' ') { return }
95
+ // Skip and count leading '1's.
96
+ var zeroes = 0;
97
+ var length = 0;
98
+ while (source[psz] === LEADER) {
99
+ zeroes++;
100
+ psz++;
101
+ }
102
+ // Allocate enough space in big-endian base256 representation.
103
+ var size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
104
+ var b256 = new Uint8Array(size);
105
+ // Process the characters.
106
+ while (source[psz]) {
107
+ // Decode character
108
+ var carry = BASE_MAP[source.charCodeAt(psz)];
109
+ // Invalid character
110
+ if (carry === 255) { return }
111
+ var i = 0;
112
+ for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
113
+ carry += (BASE * b256[it3]) >>> 0;
114
+ b256[it3] = (carry % 256) >>> 0;
115
+ carry = (carry / 256) >>> 0;
116
+ }
117
+ if (carry !== 0) { throw new Error('Non-zero carry') }
118
+ length = i;
119
+ psz++;
120
+ }
121
+ // Skip trailing spaces.
122
+ if (source[psz] === ' ') { return }
123
+ // Skip leading zeroes in b256.
124
+ var it4 = size - length;
125
+ while (it4 !== size && b256[it4] === 0) {
126
+ it4++;
127
+ }
128
+ var vch = new Uint8Array(zeroes + (size - it4));
129
+ var j = zeroes;
130
+ while (it4 !== size) {
131
+ vch[j++] = b256[it4++];
132
+ }
133
+ return vch
134
+ }
135
+ function decode (string) {
136
+ var buffer = decodeUnsafe(string);
137
+ if (buffer) { return buffer }
138
+ throw new Error(`Non-${name} character`)
139
+ }
140
+ return {
141
+ encode: encode,
142
+ decodeUnsafe: decodeUnsafe,
143
+ decode: decode
144
+ }
145
+ }
146
+ var src = base;
147
+
148
+ var _brrp__multiformats_scope_baseX = src;
149
+
150
+ /**
151
+ * @param {ArrayBufferView|ArrayBuffer|Uint8Array} o
152
+ * @returns {Uint8Array}
153
+ */
154
+ const coerce = o => {
155
+ if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') return o
156
+ if (o instanceof ArrayBuffer) return new Uint8Array(o)
157
+ if (ArrayBuffer.isView(o)) {
158
+ return new Uint8Array(o.buffer, o.byteOffset, o.byteLength)
159
+ }
160
+ throw new Error('Unknown type, must be binary type')
161
+ };
162
+
163
+ /**
164
+ * @param {string} str
165
+ * @returns {Uint8Array}
166
+ */
167
+ const fromString$1 = str => (new TextEncoder()).encode(str);
168
+
169
+ /**
170
+ * @param {Uint8Array} b
171
+ * @returns {string}
172
+ */
173
+ const toString$1 = b => (new TextDecoder()).decode(b);
174
+
175
+ /**
176
+ * Class represents both BaseEncoder and MultibaseEncoder meaning it
177
+ * can be used to encode to multibase or base encode without multibase
178
+ * prefix.
179
+ *
180
+ * @class
181
+ * @template {string} Base
182
+ * @template {string} Prefix
183
+ * @implements {API.MultibaseEncoder<Prefix>}
184
+ * @implements {API.BaseEncoder}
185
+ */
186
+ class Encoder {
187
+ /**
188
+ * @param {Base} name
189
+ * @param {Prefix} prefix
190
+ * @param {(bytes:Uint8Array) => string} baseEncode
191
+ */
192
+ constructor (name, prefix, baseEncode) {
193
+ this.name = name;
194
+ this.prefix = prefix;
195
+ this.baseEncode = baseEncode;
196
+ }
197
+
198
+ /**
199
+ * @param {Uint8Array} bytes
200
+ * @returns {API.Multibase<Prefix>}
201
+ */
202
+ encode (bytes) {
203
+ if (bytes instanceof Uint8Array) {
204
+ return `${this.prefix}${this.baseEncode(bytes)}`
205
+ } else {
206
+ throw Error('Unknown type, must be binary type')
207
+ }
208
+ }
209
+ }
210
+
211
+ /**
212
+ * @template {string} Prefix
213
+ */
214
+ /**
215
+ * Class represents both BaseDecoder and MultibaseDecoder so it could be used
216
+ * to decode multibases (with matching prefix) or just base decode strings
217
+ * with corresponding base encoding.
218
+ *
219
+ * @class
220
+ * @template {string} Base
221
+ * @template {string} Prefix
222
+ * @implements {API.MultibaseDecoder<Prefix>}
223
+ * @implements {API.UnibaseDecoder<Prefix>}
224
+ * @implements {API.BaseDecoder}
225
+ */
226
+ class Decoder {
227
+ /**
228
+ * @param {Base} name
229
+ * @param {Prefix} prefix
230
+ * @param {(text:string) => Uint8Array} baseDecode
231
+ */
232
+ constructor (name, prefix, baseDecode) {
233
+ this.name = name;
234
+ this.prefix = prefix;
235
+ /* c8 ignore next 3 */
236
+ if (prefix.codePointAt(0) === undefined) {
237
+ throw new Error('Invalid prefix character')
238
+ }
239
+ /** @private */
240
+ this.prefixCodePoint = /** @type {number} */ (prefix.codePointAt(0));
241
+ this.baseDecode = baseDecode;
242
+ }
243
+
244
+ /**
245
+ * @param {string} text
246
+ */
247
+ decode (text) {
248
+ if (typeof text === 'string') {
249
+ if (text.codePointAt(0) !== this.prefixCodePoint) {
250
+ throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`)
251
+ }
252
+ return this.baseDecode(text.slice(this.prefix.length))
253
+ } else {
254
+ throw Error('Can only multibase decode strings')
255
+ }
256
+ }
257
+
258
+ /**
259
+ * @template {string} OtherPrefix
260
+ * @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
261
+ * @returns {ComposedDecoder<Prefix|OtherPrefix>}
262
+ */
263
+ or (decoder) {
264
+ return or(this, decoder)
265
+ }
266
+ }
267
+
268
+ /**
269
+ * @template {string} Prefix
270
+ * @typedef {Record<Prefix, API.UnibaseDecoder<Prefix>>} Decoders
271
+ */
272
+
273
+ /**
274
+ * @template {string} Prefix
275
+ * @implements {API.MultibaseDecoder<Prefix>}
276
+ * @implements {API.CombobaseDecoder<Prefix>}
277
+ */
278
+ class ComposedDecoder {
279
+ /**
280
+ * @param {Decoders<Prefix>} decoders
281
+ */
282
+ constructor (decoders) {
283
+ this.decoders = decoders;
284
+ }
285
+
286
+ /**
287
+ * @template {string} OtherPrefix
288
+ * @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
289
+ * @returns {ComposedDecoder<Prefix|OtherPrefix>}
290
+ */
291
+ or (decoder) {
292
+ return or(this, decoder)
293
+ }
294
+
295
+ /**
296
+ * @param {string} input
297
+ * @returns {Uint8Array}
298
+ */
299
+ decode (input) {
300
+ const prefix = /** @type {Prefix} */ (input[0]);
301
+ const decoder = this.decoders[prefix];
302
+ if (decoder) {
303
+ return decoder.decode(input)
304
+ } else {
305
+ throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)
306
+ }
307
+ }
308
+ }
309
+
310
+ /**
311
+ * @template {string} L
312
+ * @template {string} R
313
+ * @param {API.UnibaseDecoder<L>|API.CombobaseDecoder<L>} left
314
+ * @param {API.UnibaseDecoder<R>|API.CombobaseDecoder<R>} right
315
+ * @returns {ComposedDecoder<L|R>}
316
+ */
317
+ const or = (left, right) => new ComposedDecoder(/** @type {Decoders<L|R>} */({
318
+ ...(left.decoders || { [/** @type API.UnibaseDecoder<L> */(left).prefix]: left }),
319
+ ...(right.decoders || { [/** @type API.UnibaseDecoder<R> */(right).prefix]: right })
320
+ }));
321
+
322
+ /**
323
+ * @class
324
+ * @template {string} Base
325
+ * @template {string} Prefix
326
+ * @implements {API.MultibaseCodec<Prefix>}
327
+ * @implements {API.MultibaseEncoder<Prefix>}
328
+ * @implements {API.MultibaseDecoder<Prefix>}
329
+ * @implements {API.BaseCodec}
330
+ * @implements {API.BaseEncoder}
331
+ * @implements {API.BaseDecoder}
332
+ */
333
+ class Codec {
334
+ /**
335
+ * @param {Base} name
336
+ * @param {Prefix} prefix
337
+ * @param {(bytes:Uint8Array) => string} baseEncode
338
+ * @param {(text:string) => Uint8Array} baseDecode
339
+ */
340
+ constructor (name, prefix, baseEncode, baseDecode) {
341
+ this.name = name;
342
+ this.prefix = prefix;
343
+ this.baseEncode = baseEncode;
344
+ this.baseDecode = baseDecode;
345
+ this.encoder = new Encoder(name, prefix, baseEncode);
346
+ this.decoder = new Decoder(name, prefix, baseDecode);
347
+ }
348
+
349
+ /**
350
+ * @param {Uint8Array} input
351
+ */
352
+ encode (input) {
353
+ return this.encoder.encode(input)
354
+ }
355
+
356
+ /**
357
+ * @param {string} input
358
+ */
359
+ decode (input) {
360
+ return this.decoder.decode(input)
361
+ }
362
+ }
363
+
364
+ /**
365
+ * @template {string} Base
366
+ * @template {string} Prefix
367
+ * @param {object} options
368
+ * @param {Base} options.name
369
+ * @param {Prefix} options.prefix
370
+ * @param {(bytes:Uint8Array) => string} options.encode
371
+ * @param {(input:string) => Uint8Array} options.decode
372
+ * @returns {Codec<Base, Prefix>}
373
+ */
374
+ const from$1 = ({ name, prefix, encode, decode }) =>
375
+ new Codec(name, prefix, encode, decode);
376
+
377
+ /**
378
+ * @template {string} Base
379
+ * @template {string} Prefix
380
+ * @param {object} options
381
+ * @param {Base} options.name
382
+ * @param {Prefix} options.prefix
383
+ * @param {string} options.alphabet
384
+ * @returns {Codec<Base, Prefix>}
385
+ */
386
+ const baseX = ({ prefix, name, alphabet }) => {
387
+ const { encode, decode } = _brrp__multiformats_scope_baseX(alphabet, name);
388
+ return from$1({
389
+ prefix,
390
+ name,
391
+ encode,
392
+ /**
393
+ * @param {string} text
394
+ */
395
+ decode: text => coerce(decode(text))
396
+ })
397
+ };
398
+
399
+ /**
400
+ * @param {string} string
401
+ * @param {string} alphabet
402
+ * @param {number} bitsPerChar
403
+ * @param {string} name
404
+ * @returns {Uint8Array}
405
+ */
406
+ const decode$2 = (string, alphabet, bitsPerChar, name) => {
407
+ // Build the character lookup table:
408
+ /** @type {Record<string, number>} */
409
+ const codes = {};
410
+ for (let i = 0; i < alphabet.length; ++i) {
411
+ codes[alphabet[i]] = i;
412
+ }
413
+
414
+ // Count the padding bytes:
415
+ let end = string.length;
416
+ while (string[end - 1] === '=') {
417
+ --end;
418
+ }
419
+
420
+ // Allocate the output:
421
+ const out = new Uint8Array((end * bitsPerChar / 8) | 0);
422
+
423
+ // Parse the data:
424
+ let bits = 0; // Number of bits currently in the buffer
425
+ let buffer = 0; // Bits waiting to be written out, MSB first
426
+ let written = 0; // Next byte to write
427
+ for (let i = 0; i < end; ++i) {
428
+ // Read one character from the string:
429
+ const value = codes[string[i]];
430
+ if (value === undefined) {
431
+ throw new SyntaxError(`Non-${name} character`)
432
+ }
433
+
434
+ // Append the bits to the buffer:
435
+ buffer = (buffer << bitsPerChar) | value;
436
+ bits += bitsPerChar;
437
+
438
+ // Write out some bits if the buffer has a byte's worth:
439
+ if (bits >= 8) {
440
+ bits -= 8;
441
+ out[written++] = 0xff & (buffer >> bits);
442
+ }
443
+ }
444
+
445
+ // Verify that we have received just enough bits:
446
+ if (bits >= bitsPerChar || 0xff & (buffer << (8 - bits))) {
447
+ throw new SyntaxError('Unexpected end of data')
448
+ }
449
+
450
+ return out
451
+ };
452
+
453
+ /**
454
+ * @param {Uint8Array} data
455
+ * @param {string} alphabet
456
+ * @param {number} bitsPerChar
457
+ * @returns {string}
458
+ */
459
+ const encode$3 = (data, alphabet, bitsPerChar) => {
460
+ const pad = alphabet[alphabet.length - 1] === '=';
461
+ const mask = (1 << bitsPerChar) - 1;
462
+ let out = '';
463
+
464
+ let bits = 0; // Number of bits currently in the buffer
465
+ let buffer = 0; // Bits waiting to be written out, MSB first
466
+ for (let i = 0; i < data.length; ++i) {
467
+ // Slurp data into the buffer:
468
+ buffer = (buffer << 8) | data[i];
469
+ bits += 8;
470
+
471
+ // Write out as much as we can:
472
+ while (bits > bitsPerChar) {
473
+ bits -= bitsPerChar;
474
+ out += alphabet[mask & (buffer >> bits)];
475
+ }
476
+ }
477
+
478
+ // Partial character:
479
+ if (bits) {
480
+ out += alphabet[mask & (buffer << (bitsPerChar - bits))];
481
+ }
482
+
483
+ // Add padding characters until we hit a byte boundary:
484
+ if (pad) {
485
+ while ((out.length * bitsPerChar) & 7) {
486
+ out += '=';
487
+ }
488
+ }
489
+
490
+ return out
491
+ };
492
+
493
+ /**
494
+ * RFC4648 Factory
495
+ *
496
+ * @template {string} Base
497
+ * @template {string} Prefix
498
+ * @param {object} options
499
+ * @param {Base} options.name
500
+ * @param {Prefix} options.prefix
501
+ * @param {string} options.alphabet
502
+ * @param {number} options.bitsPerChar
503
+ */
504
+ const rfc4648 = ({ name, prefix, bitsPerChar, alphabet }) => {
505
+ return from$1({
506
+ prefix,
507
+ name,
508
+ encode (input) {
509
+ return encode$3(input, alphabet, bitsPerChar)
510
+ },
511
+ decode (input) {
512
+ return decode$2(input, alphabet, bitsPerChar, name)
513
+ }
514
+ })
515
+ };
516
+
517
+ // @ts-check
518
+
519
+ const identity$2 = from$1({
520
+ prefix: '\x00',
521
+ name: 'identity',
522
+ encode: (buf) => toString$1(buf),
523
+ decode: (str) => fromString$1(str)
524
+ });
525
+
526
+ var identityBase = /*#__PURE__*/Object.freeze({
527
+ __proto__: null,
528
+ identity: identity$2
529
+ });
530
+
531
+ // @ts-check
532
+
533
+ const base2 = rfc4648({
534
+ prefix: '0',
535
+ name: 'base2',
536
+ alphabet: '01',
537
+ bitsPerChar: 1
538
+ });
539
+
540
+ var base2$1 = /*#__PURE__*/Object.freeze({
541
+ __proto__: null,
542
+ base2: base2
543
+ });
544
+
545
+ // @ts-check
546
+
547
+ const base8 = rfc4648({
548
+ prefix: '7',
549
+ name: 'base8',
550
+ alphabet: '01234567',
551
+ bitsPerChar: 3
552
+ });
553
+
554
+ var base8$1 = /*#__PURE__*/Object.freeze({
555
+ __proto__: null,
556
+ base8: base8
557
+ });
558
+
559
+ const base10 = baseX({
560
+ prefix: '9',
561
+ name: 'base10',
562
+ alphabet: '0123456789'
563
+ });
564
+
565
+ var base10$1 = /*#__PURE__*/Object.freeze({
566
+ __proto__: null,
567
+ base10: base10
568
+ });
569
+
570
+ // @ts-check
571
+
572
+ const base16 = rfc4648({
573
+ prefix: 'f',
574
+ name: 'base16',
575
+ alphabet: '0123456789abcdef',
576
+ bitsPerChar: 4
577
+ });
578
+
579
+ const base16upper = rfc4648({
580
+ prefix: 'F',
581
+ name: 'base16upper',
582
+ alphabet: '0123456789ABCDEF',
583
+ bitsPerChar: 4
584
+ });
585
+
586
+ var base16$1 = /*#__PURE__*/Object.freeze({
587
+ __proto__: null,
588
+ base16: base16,
589
+ base16upper: base16upper
590
+ });
591
+
592
+ const base32 = rfc4648({
593
+ prefix: 'b',
594
+ name: 'base32',
595
+ alphabet: 'abcdefghijklmnopqrstuvwxyz234567',
596
+ bitsPerChar: 5
597
+ });
598
+
599
+ const base32upper = rfc4648({
600
+ prefix: 'B',
601
+ name: 'base32upper',
602
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
603
+ bitsPerChar: 5
604
+ });
605
+
606
+ const base32pad = rfc4648({
607
+ prefix: 'c',
608
+ name: 'base32pad',
609
+ alphabet: 'abcdefghijklmnopqrstuvwxyz234567=',
610
+ bitsPerChar: 5
611
+ });
612
+
613
+ const base32padupper = rfc4648({
614
+ prefix: 'C',
615
+ name: 'base32padupper',
616
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=',
617
+ bitsPerChar: 5
618
+ });
619
+
620
+ const base32hex = rfc4648({
621
+ prefix: 'v',
622
+ name: 'base32hex',
623
+ alphabet: '0123456789abcdefghijklmnopqrstuv',
624
+ bitsPerChar: 5
625
+ });
626
+
627
+ const base32hexupper = rfc4648({
628
+ prefix: 'V',
629
+ name: 'base32hexupper',
630
+ alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
631
+ bitsPerChar: 5
632
+ });
633
+
634
+ const base32hexpad = rfc4648({
635
+ prefix: 't',
636
+ name: 'base32hexpad',
637
+ alphabet: '0123456789abcdefghijklmnopqrstuv=',
638
+ bitsPerChar: 5
639
+ });
640
+
641
+ const base32hexpadupper = rfc4648({
642
+ prefix: 'T',
643
+ name: 'base32hexpadupper',
644
+ alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=',
645
+ bitsPerChar: 5
646
+ });
647
+
648
+ const base32z = rfc4648({
649
+ prefix: 'h',
650
+ name: 'base32z',
651
+ alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
652
+ bitsPerChar: 5
653
+ });
654
+
655
+ var base32$1 = /*#__PURE__*/Object.freeze({
656
+ __proto__: null,
657
+ base32: base32,
658
+ base32upper: base32upper,
659
+ base32pad: base32pad,
660
+ base32padupper: base32padupper,
661
+ base32hex: base32hex,
662
+ base32hexupper: base32hexupper,
663
+ base32hexpad: base32hexpad,
664
+ base32hexpadupper: base32hexpadupper,
665
+ base32z: base32z
666
+ });
667
+
668
+ const base36 = baseX({
669
+ prefix: 'k',
670
+ name: 'base36',
671
+ alphabet: '0123456789abcdefghijklmnopqrstuvwxyz'
672
+ });
673
+
674
+ const base36upper = baseX({
675
+ prefix: 'K',
676
+ name: 'base36upper',
677
+ alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
678
+ });
679
+
680
+ var base36$1 = /*#__PURE__*/Object.freeze({
681
+ __proto__: null,
682
+ base36: base36,
683
+ base36upper: base36upper
684
+ });
685
+
686
+ const base58btc = baseX({
687
+ name: 'base58btc',
688
+ prefix: 'z',
689
+ alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
690
+ });
691
+
692
+ const base58flickr = baseX({
693
+ name: 'base58flickr',
694
+ prefix: 'Z',
695
+ alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
696
+ });
697
+
698
+ var base58 = /*#__PURE__*/Object.freeze({
699
+ __proto__: null,
700
+ base58btc: base58btc,
701
+ base58flickr: base58flickr
702
+ });
703
+
704
+ // @ts-check
705
+
706
+ const base64 = rfc4648({
707
+ prefix: 'm',
708
+ name: 'base64',
709
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
710
+ bitsPerChar: 6
711
+ });
712
+
713
+ const base64pad = rfc4648({
714
+ prefix: 'M',
715
+ name: 'base64pad',
716
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
717
+ bitsPerChar: 6
718
+ });
719
+
720
+ const base64url = rfc4648({
721
+ prefix: 'u',
722
+ name: 'base64url',
723
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
724
+ bitsPerChar: 6
725
+ });
726
+
727
+ const base64urlpad = rfc4648({
728
+ prefix: 'U',
729
+ name: 'base64urlpad',
730
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=',
731
+ bitsPerChar: 6
732
+ });
733
+
734
+ var base64$1 = /*#__PURE__*/Object.freeze({
735
+ __proto__: null,
736
+ base64: base64,
737
+ base64pad: base64pad,
738
+ base64url: base64url,
739
+ base64urlpad: base64urlpad
740
+ });
741
+
742
+ const alphabet = Array.from('🚀🪐☄🛰🌌🌑🌒🌓🌔🌕🌖🌗🌘🌍🌏🌎🐉☀💻🖥💾💿😂❤😍🤣😊🙏💕😭😘👍😅👏😁🔥🥰💔💖💙😢🤔😆🙄💪😉☺👌🤗💜😔😎😇🌹🤦🎉💞✌✨🤷😱😌🌸🙌😋💗💚😏💛🙂💓🤩😄😀🖤😃💯🙈👇🎶😒🤭❣😜💋👀😪😑💥🙋😞😩😡🤪👊🥳😥🤤👉💃😳✋😚😝😴🌟😬🙃🍀🌷😻😓⭐✅🥺🌈😈🤘💦✔😣🏃💐☹🎊💘😠☝😕🌺🎂🌻😐🖕💝🙊😹🗣💫💀👑🎵🤞😛🔴😤🌼😫⚽🤙☕🏆🤫👈😮🙆🍻🍃🐶💁😲🌿🧡🎁⚡🌞🎈❌✊👋😰🤨😶🤝🚶💰🍓💢🤟🙁🚨💨🤬✈🎀🍺🤓😙💟🌱😖👶🥴▶➡❓💎💸⬇😨🌚🦋😷🕺⚠🙅😟😵👎🤲🤠🤧📌🔵💅🧐🐾🍒😗🤑🌊🤯🐷☎💧😯💆👆🎤🙇🍑❄🌴💣🐸💌📍🥀🤢👅💡💩👐📸👻🤐🤮🎼🥵🚩🍎🍊👼💍📣🥂');
743
+ const alphabetBytesToChars = /** @type {string[]} */ (alphabet.reduce((p, c, i) => { p[i] = c; return p }, /** @type {string[]} */([])));
744
+ const alphabetCharsToBytes = /** @type {number[]} */ (alphabet.reduce((p, c, i) => { p[/** @type {number} */ (c.codePointAt(0))] = i; return p }, /** @type {number[]} */([])));
745
+
746
+ /**
747
+ * @param {Uint8Array} data
748
+ * @returns {string}
749
+ */
750
+ function encode$2 (data) {
751
+ return data.reduce((p, c) => {
752
+ p += alphabetBytesToChars[c];
753
+ return p
754
+ }, '')
755
+ }
756
+
757
+ /**
758
+ * @param {string} str
759
+ * @returns {Uint8Array}
760
+ */
761
+ function decode$1 (str) {
762
+ const byts = [];
763
+ for (const char of str) {
764
+ const byt = alphabetCharsToBytes[/** @type {number} */ (char.codePointAt(0))];
765
+ if (byt === undefined) {
766
+ throw new Error(`Non-base256emoji character: ${char}`)
767
+ }
768
+ byts.push(byt);
769
+ }
770
+ return new Uint8Array(byts)
771
+ }
772
+
773
+ const base256emoji = from$1({
774
+ prefix: '🚀',
775
+ name: 'base256emoji',
776
+ encode: encode$2,
777
+ decode: decode$1
778
+ });
779
+
780
+ var base256emoji$1 = /*#__PURE__*/Object.freeze({
781
+ __proto__: null,
782
+ base256emoji: base256emoji
783
+ });
784
+
785
+ var encode_1 = encode$1;
786
+
787
+ var MSB = 0x80
788
+ , REST = 0x7F
789
+ , MSBALL = ~REST
790
+ , INT = Math.pow(2, 31);
791
+
792
+ function encode$1(num, out, offset) {
793
+ out = out || [];
794
+ offset = offset || 0;
795
+ var oldOffset = offset;
796
+
797
+ while(num >= INT) {
798
+ out[offset++] = (num & 0xFF) | MSB;
799
+ num /= 128;
800
+ }
801
+ while(num & MSBALL) {
802
+ out[offset++] = (num & 0xFF) | MSB;
803
+ num >>>= 7;
804
+ }
805
+ out[offset] = num | 0;
806
+
807
+ encode$1.bytes = offset - oldOffset + 1;
808
+
809
+ return out
810
+ }
811
+
812
+ var decode = read;
813
+
814
+ var MSB$1 = 0x80
815
+ , REST$1 = 0x7F;
816
+
817
+ function read(buf, offset) {
818
+ var res = 0
819
+ , offset = offset || 0
820
+ , shift = 0
821
+ , counter = offset
822
+ , b
823
+ , l = buf.length;
824
+
825
+ do {
826
+ if (counter >= l) {
827
+ read.bytes = 0;
828
+ throw new RangeError('Could not decode varint')
829
+ }
830
+ b = buf[counter++];
831
+ res += shift < 28
832
+ ? (b & REST$1) << shift
833
+ : (b & REST$1) * Math.pow(2, shift);
834
+ shift += 7;
835
+ } while (b >= MSB$1)
836
+
837
+ read.bytes = counter - offset;
838
+
839
+ return res
840
+ }
841
+
842
+ var N1 = Math.pow(2, 7);
843
+ var N2 = Math.pow(2, 14);
844
+ var N3 = Math.pow(2, 21);
845
+ var N4 = Math.pow(2, 28);
846
+ var N5 = Math.pow(2, 35);
847
+ var N6 = Math.pow(2, 42);
848
+ var N7 = Math.pow(2, 49);
849
+ var N8 = Math.pow(2, 56);
850
+ var N9 = Math.pow(2, 63);
851
+
852
+ var length = function (value) {
853
+ return (
854
+ value < N1 ? 1
855
+ : value < N2 ? 2
856
+ : value < N3 ? 3
857
+ : value < N4 ? 4
858
+ : value < N5 ? 5
859
+ : value < N6 ? 6
860
+ : value < N7 ? 7
861
+ : value < N8 ? 8
862
+ : value < N9 ? 9
863
+ : 10
864
+ )
865
+ };
866
+
867
+ var varint = {
868
+ encode: encode_1
869
+ , decode: decode
870
+ , encodingLength: length
871
+ };
872
+
873
+ var _brrp_varint = varint;
874
+
875
+ /**
876
+ * @param {number} int
877
+ * @param {Uint8Array} target
878
+ * @param {number} [offset=0]
879
+ */
880
+ const encodeTo = (int, target, offset = 0) => {
881
+ _brrp_varint.encode(int, target, offset);
882
+ return target
883
+ };
884
+
885
+ /**
886
+ * @param {number} int
887
+ * @returns {number}
888
+ */
889
+ const encodingLength = (int) => {
890
+ return _brrp_varint.encodingLength(int)
891
+ };
892
+
893
+ /**
894
+ * Creates a multihash digest.
895
+ *
896
+ * @template {number} Code
897
+ * @param {Code} code
898
+ * @param {Uint8Array} digest
899
+ */
900
+ const create = (code, digest) => {
901
+ const size = digest.byteLength;
902
+ const sizeOffset = encodingLength(code);
903
+ const digestOffset = sizeOffset + encodingLength(size);
904
+
905
+ const bytes = new Uint8Array(digestOffset + size);
906
+ encodeTo(code, bytes, 0);
907
+ encodeTo(size, bytes, sizeOffset);
908
+ bytes.set(digest, digestOffset);
909
+
910
+ return new Digest(code, size, digest, bytes)
911
+ };
912
+
913
+ /**
914
+ * @typedef {import('./interface.js').MultihashDigest} MultihashDigest
915
+ */
916
+
917
+ /**
918
+ * Represents a multihash digest which carries information about the
919
+ * hashing alogrithm and an actual hash digest.
920
+ *
921
+ * @template {number} Code
922
+ * @template {number} Size
923
+ * @class
924
+ * @implements {MultihashDigest}
925
+ */
926
+ class Digest {
927
+ /**
928
+ * Creates a multihash digest.
929
+ *
930
+ * @param {Code} code
931
+ * @param {Size} size
932
+ * @param {Uint8Array} digest
933
+ * @param {Uint8Array} bytes
934
+ */
935
+ constructor (code, size, digest, bytes) {
936
+ this.code = code;
937
+ this.size = size;
938
+ this.digest = digest;
939
+ this.bytes = bytes;
940
+ }
941
+ }
942
+
943
+ /**
944
+ * @template {string} Name
945
+ * @template {number} Code
946
+ * @param {object} options
947
+ * @param {Name} options.name
948
+ * @param {Code} options.code
949
+ * @param {(input: Uint8Array) => Await<Uint8Array>} options.encode
950
+ */
951
+ const from = ({ name, code, encode }) => new Hasher(name, code, encode);
952
+
953
+ /**
954
+ * Hasher represents a hashing algorithm implementation that produces as
955
+ * `MultihashDigest`.
956
+ *
957
+ * @template {string} Name
958
+ * @template {number} Code
959
+ * @class
960
+ * @implements {MultihashHasher<Code>}
961
+ */
962
+ class Hasher {
963
+ /**
964
+ *
965
+ * @param {Name} name
966
+ * @param {Code} code
967
+ * @param {(input: Uint8Array) => Await<Uint8Array>} encode
968
+ */
969
+ constructor (name, code, encode) {
970
+ this.name = name;
971
+ this.code = code;
972
+ this.encode = encode;
973
+ }
974
+
975
+ /**
976
+ * @param {Uint8Array} input
977
+ * @returns {Await<Digest.Digest<Code, number>>}
978
+ */
979
+ digest (input) {
980
+ if (input instanceof Uint8Array) {
981
+ const result = this.encode(input);
982
+ return result instanceof Uint8Array
983
+ ? create(this.code, result)
984
+ /* c8 ignore next 1 */
985
+ : result.then(digest => create(this.code, digest))
986
+ } else {
987
+ throw Error('Unknown type, must be binary type')
988
+ /* c8 ignore next 1 */
989
+ }
990
+ }
991
+ }
992
+
993
+ /**
994
+ * @template {number} Alg
995
+ * @typedef {import('./interface.js').MultihashHasher} MultihashHasher
996
+ */
997
+
998
+ /**
999
+ * @template T
1000
+ * @typedef {Promise<T>|T} Await
1001
+ */
1002
+
1003
+ /* global crypto */
1004
+
1005
+ /**
1006
+ * @param {AlgorithmIdentifier} name
1007
+ */
1008
+ const sha = name =>
1009
+ /**
1010
+ * @param {Uint8Array} data
1011
+ */
1012
+ async data => new Uint8Array(await crypto.subtle.digest(name, data));
1013
+
1014
+ const sha256 = from({
1015
+ name: 'sha2-256',
1016
+ code: 0x12,
1017
+ encode: sha('SHA-256')
1018
+ });
1019
+
1020
+ const sha512 = from({
1021
+ name: 'sha2-512',
1022
+ code: 0x13,
1023
+ encode: sha('SHA-512')
1024
+ });
1025
+
1026
+ var sha2 = /*#__PURE__*/Object.freeze({
1027
+ __proto__: null,
1028
+ sha256: sha256,
1029
+ sha512: sha512
1030
+ });
1031
+
1032
+ const code = 0x0;
1033
+ const name = 'identity';
1034
+
1035
+ /** @type {(input:Uint8Array) => Uint8Array} */
1036
+ const encode = coerce;
1037
+
1038
+ /**
1039
+ * @param {Uint8Array} input
1040
+ * @returns {Digest.Digest<typeof code, number>}
1041
+ */
1042
+ const digest = (input) => create(code, encode(input));
1043
+
1044
+ const identity = { code, name, encode, digest };
1045
+
1046
+ var identity$1 = /*#__PURE__*/Object.freeze({
1047
+ __proto__: null,
1048
+ identity: identity
1049
+ });
1050
+
1051
+ // @ts-check
1052
+
1053
+ /**
1054
+ * @template T
1055
+ * @typedef {import('./interface.js').ByteView<T>} ByteView
1056
+ */
1057
+
1058
+ new TextEncoder();
1059
+ new TextDecoder();
1060
+
1061
+ // @ts-check
1062
+
1063
+ const bases = { ...identityBase, ...base2$1, ...base8$1, ...base10$1, ...base16$1, ...base32$1, ...base36$1, ...base58, ...base64$1, ...base256emoji$1 };
1064
+ ({ ...sha2, ...identity$1 });
1065
+
1066
+ /**
1067
+ * To guarantee Uint8Array semantics, convert nodejs Buffers
1068
+ * into vanilla Uint8Arrays
1069
+ */
1070
+ function asUint8Array(buf) {
1071
+ if (globalThis.Buffer != null) {
1072
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
1073
+ }
1074
+ return buf;
1075
+ }
1076
+
1077
+ /**
1078
+ * Where possible returns a Uint8Array of the requested size that references
1079
+ * uninitialized memory. Only use if you are certain you will immediately
1080
+ * overwrite every value in the returned `Uint8Array`.
1081
+ */
1082
+ function allocUnsafe(size = 0) {
1083
+ if (globalThis.Buffer?.allocUnsafe != null) {
1084
+ return asUint8Array(globalThis.Buffer.allocUnsafe(size));
1085
+ }
1086
+ return new Uint8Array(size);
1087
+ }
1088
+
1089
+ function createCodec(name, prefix, encode, decode) {
1090
+ return {
1091
+ name,
1092
+ prefix,
1093
+ encoder: {
1094
+ name,
1095
+ prefix,
1096
+ encode
1097
+ },
1098
+ decoder: {
1099
+ decode
1100
+ }
1101
+ };
1102
+ }
1103
+ const string = createCodec('utf8', 'u', (buf) => {
1104
+ const decoder = new TextDecoder('utf8');
1105
+ return 'u' + decoder.decode(buf);
1106
+ }, (str) => {
1107
+ const encoder = new TextEncoder();
1108
+ return encoder.encode(str.substring(1));
1109
+ });
1110
+ const ascii = createCodec('ascii', 'a', (buf) => {
1111
+ let string = 'a';
1112
+ for (let i = 0; i < buf.length; i++) {
1113
+ string += String.fromCharCode(buf[i]);
1114
+ }
1115
+ return string;
1116
+ }, (str) => {
1117
+ str = str.substring(1);
1118
+ const buf = allocUnsafe(str.length);
1119
+ for (let i = 0; i < str.length; i++) {
1120
+ buf[i] = str.charCodeAt(i);
1121
+ }
1122
+ return buf;
1123
+ });
1124
+ const BASES = {
1125
+ utf8: string,
1126
+ 'utf-8': string,
1127
+ hex: bases.base16,
1128
+ latin1: ascii,
1129
+ ascii: ascii,
1130
+ binary: ascii,
1131
+ ...bases
1132
+ };
1133
+
1134
+ /**
1135
+ * Create a `Uint8Array` from the passed string
1136
+ *
1137
+ * Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module.
1138
+ *
1139
+ * Also `ascii` which is similar to node's 'binary' encoding.
1140
+ */
1141
+ function fromString(string, encoding = 'utf8') {
1142
+ const base = BASES[encoding];
1143
+ if (base == null) {
1144
+ throw new Error(`Unsupported encoding "${encoding}"`);
1145
+ }
1146
+ if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
1147
+ return asUint8Array(globalThis.Buffer.from(string, 'utf-8'));
1148
+ }
1149
+ // add multibase prefix
1150
+ return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
1151
+ }
1152
+
1153
+ /**
1154
+ * Turns a `Uint8Array` into a string.
1155
+ *
1156
+ * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
1157
+ *
1158
+ * Also `ascii` which is similar to node's 'binary' encoding.
1159
+ */
1160
+ function toString(array, encoding = 'utf8') {
1161
+ const base = BASES[encoding];
1162
+ if (base == null) {
1163
+ throw new Error(`Unsupported encoding "${encoding}"`);
1164
+ }
1165
+ if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
1166
+ return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8');
1167
+ }
1168
+ // strip multibase prefix
1169
+ return base.encoder.encode(array).substring(1);
1170
+ }
1171
+
1172
+ /**
1173
+ * Convert input to a byte array.
1174
+ *
1175
+ * Handles both `0x` prefixed and non-prefixed strings.
1176
+ */
1177
+ function hexToBytes$1(hex) {
1178
+ if (typeof hex === "string") {
1179
+ const _hex = hex.replace(/^0x/i, "");
1180
+ return fromString(_hex.toLowerCase(), "base16");
1181
+ }
1182
+ return hex;
1183
+ }
1184
+ /**
1185
+ * Convert byte array to hex string (no `0x` prefix).
1186
+ */
1187
+ const bytesToHex$1 = (bytes) => toString(bytes, "base16");
1188
+ /**
1189
+ * Decode byte array to utf-8 string.
1190
+ */
1191
+ const bytesToUtf8 = (b) => toString(b, "utf8");
1192
+ /**
1193
+ * Encode utf-8 string to byte array.
1194
+ */
1195
+ const utf8ToBytes = (s) => fromString(s, "utf8");
1196
+ /**
1197
+ * Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
1198
+ */
1199
+ function concat(byteArrays, totalLength) {
1200
+ const len = totalLength ?? byteArrays.reduce((acc, curr) => acc + curr.length, 0);
1201
+ const res = new Uint8Array(len);
1202
+ let offset = 0;
1203
+ for (const bytes of byteArrays) {
1204
+ res.set(bytes, offset);
1205
+ offset += bytes.length;
1206
+ }
1207
+ return res;
1208
+ }
1209
+
1210
+ var _nodeResolve_empty = {};
1211
+
1212
+ var nodeCrypto = /*#__PURE__*/Object.freeze({
1213
+ __proto__: null,
1214
+ 'default': _nodeResolve_empty
1215
+ });
1216
+
1217
+ /*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) */
1218
+ const _0n = BigInt(0);
1219
+ const _1n = BigInt(1);
1220
+ const _2n = BigInt(2);
1221
+ const _3n = BigInt(3);
1222
+ const _8n = BigInt(8);
1223
+ const CURVE = Object.freeze({
1224
+ a: _0n,
1225
+ b: BigInt(7),
1226
+ P: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
1227
+ n: BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'),
1228
+ h: _1n,
1229
+ Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
1230
+ Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
1231
+ beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
1232
+ });
1233
+ function weistrass(x) {
1234
+ const { a, b } = CURVE;
1235
+ const x2 = mod(x * x);
1236
+ const x3 = mod(x2 * x);
1237
+ return mod(x3 + a * x + b);
1238
+ }
1239
+ const USE_ENDOMORPHISM = CURVE.a === _0n;
1240
+ class ShaError extends Error {
1241
+ constructor(message) {
1242
+ super(message);
1243
+ }
1244
+ }
1245
+ class JacobianPoint {
1246
+ constructor(x, y, z) {
1247
+ this.x = x;
1248
+ this.y = y;
1249
+ this.z = z;
1250
+ }
1251
+ static fromAffine(p) {
1252
+ if (!(p instanceof Point)) {
1253
+ throw new TypeError('JacobianPoint#fromAffine: expected Point');
1254
+ }
1255
+ return new JacobianPoint(p.x, p.y, _1n);
1256
+ }
1257
+ static toAffineBatch(points) {
1258
+ const toInv = invertBatch(points.map((p) => p.z));
1259
+ return points.map((p, i) => p.toAffine(toInv[i]));
1260
+ }
1261
+ static normalizeZ(points) {
1262
+ return JacobianPoint.toAffineBatch(points).map(JacobianPoint.fromAffine);
1263
+ }
1264
+ equals(other) {
1265
+ if (!(other instanceof JacobianPoint))
1266
+ throw new TypeError('JacobianPoint expected');
1267
+ const { x: X1, y: Y1, z: Z1 } = this;
1268
+ const { x: X2, y: Y2, z: Z2 } = other;
1269
+ const Z1Z1 = mod(Z1 * Z1);
1270
+ const Z2Z2 = mod(Z2 * Z2);
1271
+ const U1 = mod(X1 * Z2Z2);
1272
+ const U2 = mod(X2 * Z1Z1);
1273
+ const S1 = mod(mod(Y1 * Z2) * Z2Z2);
1274
+ const S2 = mod(mod(Y2 * Z1) * Z1Z1);
1275
+ return U1 === U2 && S1 === S2;
1276
+ }
1277
+ negate() {
1278
+ return new JacobianPoint(this.x, mod(-this.y), this.z);
1279
+ }
1280
+ double() {
1281
+ const { x: X1, y: Y1, z: Z1 } = this;
1282
+ const A = mod(X1 * X1);
1283
+ const B = mod(Y1 * Y1);
1284
+ const C = mod(B * B);
1285
+ const x1b = X1 + B;
1286
+ const D = mod(_2n * (mod(x1b * x1b) - A - C));
1287
+ const E = mod(_3n * A);
1288
+ const F = mod(E * E);
1289
+ const X3 = mod(F - _2n * D);
1290
+ const Y3 = mod(E * (D - X3) - _8n * C);
1291
+ const Z3 = mod(_2n * Y1 * Z1);
1292
+ return new JacobianPoint(X3, Y3, Z3);
1293
+ }
1294
+ add(other) {
1295
+ if (!(other instanceof JacobianPoint))
1296
+ throw new TypeError('JacobianPoint expected');
1297
+ const { x: X1, y: Y1, z: Z1 } = this;
1298
+ const { x: X2, y: Y2, z: Z2 } = other;
1299
+ if (X2 === _0n || Y2 === _0n)
1300
+ return this;
1301
+ if (X1 === _0n || Y1 === _0n)
1302
+ return other;
1303
+ const Z1Z1 = mod(Z1 * Z1);
1304
+ const Z2Z2 = mod(Z2 * Z2);
1305
+ const U1 = mod(X1 * Z2Z2);
1306
+ const U2 = mod(X2 * Z1Z1);
1307
+ const S1 = mod(mod(Y1 * Z2) * Z2Z2);
1308
+ const S2 = mod(mod(Y2 * Z1) * Z1Z1);
1309
+ const H = mod(U2 - U1);
1310
+ const r = mod(S2 - S1);
1311
+ if (H === _0n) {
1312
+ if (r === _0n) {
1313
+ return this.double();
1314
+ }
1315
+ else {
1316
+ return JacobianPoint.ZERO;
1317
+ }
1318
+ }
1319
+ const HH = mod(H * H);
1320
+ const HHH = mod(H * HH);
1321
+ const V = mod(U1 * HH);
1322
+ const X3 = mod(r * r - HHH - _2n * V);
1323
+ const Y3 = mod(r * (V - X3) - S1 * HHH);
1324
+ const Z3 = mod(Z1 * Z2 * H);
1325
+ return new JacobianPoint(X3, Y3, Z3);
1326
+ }
1327
+ subtract(other) {
1328
+ return this.add(other.negate());
1329
+ }
1330
+ multiplyUnsafe(scalar) {
1331
+ const P0 = JacobianPoint.ZERO;
1332
+ if (typeof scalar === 'bigint' && scalar === _0n)
1333
+ return P0;
1334
+ let n = normalizeScalar(scalar);
1335
+ if (n === _1n)
1336
+ return this;
1337
+ if (!USE_ENDOMORPHISM) {
1338
+ let p = P0;
1339
+ let d = this;
1340
+ while (n > _0n) {
1341
+ if (n & _1n)
1342
+ p = p.add(d);
1343
+ d = d.double();
1344
+ n >>= _1n;
1345
+ }
1346
+ return p;
1347
+ }
1348
+ let { k1neg, k1, k2neg, k2 } = splitScalarEndo(n);
1349
+ let k1p = P0;
1350
+ let k2p = P0;
1351
+ let d = this;
1352
+ while (k1 > _0n || k2 > _0n) {
1353
+ if (k1 & _1n)
1354
+ k1p = k1p.add(d);
1355
+ if (k2 & _1n)
1356
+ k2p = k2p.add(d);
1357
+ d = d.double();
1358
+ k1 >>= _1n;
1359
+ k2 >>= _1n;
1360
+ }
1361
+ if (k1neg)
1362
+ k1p = k1p.negate();
1363
+ if (k2neg)
1364
+ k2p = k2p.negate();
1365
+ k2p = new JacobianPoint(mod(k2p.x * CURVE.beta), k2p.y, k2p.z);
1366
+ return k1p.add(k2p);
1367
+ }
1368
+ precomputeWindow(W) {
1369
+ const windows = USE_ENDOMORPHISM ? 128 / W + 1 : 256 / W + 1;
1370
+ const points = [];
1371
+ let p = this;
1372
+ let base = p;
1373
+ for (let window = 0; window < windows; window++) {
1374
+ base = p;
1375
+ points.push(base);
1376
+ for (let i = 1; i < 2 ** (W - 1); i++) {
1377
+ base = base.add(p);
1378
+ points.push(base);
1379
+ }
1380
+ p = base.double();
1381
+ }
1382
+ return points;
1383
+ }
1384
+ wNAF(n, affinePoint) {
1385
+ if (!affinePoint && this.equals(JacobianPoint.BASE))
1386
+ affinePoint = Point.BASE;
1387
+ const W = (affinePoint && affinePoint._WINDOW_SIZE) || 1;
1388
+ if (256 % W) {
1389
+ throw new Error('Point#wNAF: Invalid precomputation window, must be power of 2');
1390
+ }
1391
+ let precomputes = affinePoint && pointPrecomputes.get(affinePoint);
1392
+ if (!precomputes) {
1393
+ precomputes = this.precomputeWindow(W);
1394
+ if (affinePoint && W !== 1) {
1395
+ precomputes = JacobianPoint.normalizeZ(precomputes);
1396
+ pointPrecomputes.set(affinePoint, precomputes);
1397
+ }
1398
+ }
1399
+ let p = JacobianPoint.ZERO;
1400
+ let f = JacobianPoint.ZERO;
1401
+ const windows = 1 + (USE_ENDOMORPHISM ? 128 / W : 256 / W);
1402
+ const windowSize = 2 ** (W - 1);
1403
+ const mask = BigInt(2 ** W - 1);
1404
+ const maxNumber = 2 ** W;
1405
+ const shiftBy = BigInt(W);
1406
+ for (let window = 0; window < windows; window++) {
1407
+ const offset = window * windowSize;
1408
+ let wbits = Number(n & mask);
1409
+ n >>= shiftBy;
1410
+ if (wbits > windowSize) {
1411
+ wbits -= maxNumber;
1412
+ n += _1n;
1413
+ }
1414
+ if (wbits === 0) {
1415
+ let pr = precomputes[offset];
1416
+ if (window % 2)
1417
+ pr = pr.negate();
1418
+ f = f.add(pr);
1419
+ }
1420
+ else {
1421
+ let cached = precomputes[offset + Math.abs(wbits) - 1];
1422
+ if (wbits < 0)
1423
+ cached = cached.negate();
1424
+ p = p.add(cached);
1425
+ }
1426
+ }
1427
+ return { p, f };
1428
+ }
1429
+ multiply(scalar, affinePoint) {
1430
+ let n = normalizeScalar(scalar);
1431
+ let point;
1432
+ let fake;
1433
+ if (USE_ENDOMORPHISM) {
1434
+ const { k1neg, k1, k2neg, k2 } = splitScalarEndo(n);
1435
+ let { p: k1p, f: f1p } = this.wNAF(k1, affinePoint);
1436
+ let { p: k2p, f: f2p } = this.wNAF(k2, affinePoint);
1437
+ if (k1neg)
1438
+ k1p = k1p.negate();
1439
+ if (k2neg)
1440
+ k2p = k2p.negate();
1441
+ k2p = new JacobianPoint(mod(k2p.x * CURVE.beta), k2p.y, k2p.z);
1442
+ point = k1p.add(k2p);
1443
+ fake = f1p.add(f2p);
1444
+ }
1445
+ else {
1446
+ const { p, f } = this.wNAF(n, affinePoint);
1447
+ point = p;
1448
+ fake = f;
1449
+ }
1450
+ return JacobianPoint.normalizeZ([point, fake])[0];
1451
+ }
1452
+ toAffine(invZ = invert(this.z)) {
1453
+ const { x, y, z } = this;
1454
+ const iz1 = invZ;
1455
+ const iz2 = mod(iz1 * iz1);
1456
+ const iz3 = mod(iz2 * iz1);
1457
+ const ax = mod(x * iz2);
1458
+ const ay = mod(y * iz3);
1459
+ const zz = mod(z * iz1);
1460
+ if (zz !== _1n)
1461
+ throw new Error('invZ was invalid');
1462
+ return new Point(ax, ay);
1463
+ }
1464
+ }
1465
+ JacobianPoint.BASE = new JacobianPoint(CURVE.Gx, CURVE.Gy, _1n);
1466
+ JacobianPoint.ZERO = new JacobianPoint(_0n, _1n, _0n);
1467
+ const pointPrecomputes = new WeakMap();
1468
+ class Point {
1469
+ constructor(x, y) {
1470
+ this.x = x;
1471
+ this.y = y;
1472
+ }
1473
+ _setWindowSize(windowSize) {
1474
+ this._WINDOW_SIZE = windowSize;
1475
+ pointPrecomputes.delete(this);
1476
+ }
1477
+ hasEvenY() {
1478
+ return this.y % _2n === _0n;
1479
+ }
1480
+ static fromCompressedHex(bytes) {
1481
+ const isShort = bytes.length === 32;
1482
+ const x = bytesToNumber(isShort ? bytes : bytes.subarray(1));
1483
+ if (!isValidFieldElement(x))
1484
+ throw new Error('Point is not on curve');
1485
+ const y2 = weistrass(x);
1486
+ let y = sqrtMod(y2);
1487
+ const isYOdd = (y & _1n) === _1n;
1488
+ if (isShort) {
1489
+ if (isYOdd)
1490
+ y = mod(-y);
1491
+ }
1492
+ else {
1493
+ const isFirstByteOdd = (bytes[0] & 1) === 1;
1494
+ if (isFirstByteOdd !== isYOdd)
1495
+ y = mod(-y);
1496
+ }
1497
+ const point = new Point(x, y);
1498
+ point.assertValidity();
1499
+ return point;
1500
+ }
1501
+ static fromUncompressedHex(bytes) {
1502
+ const x = bytesToNumber(bytes.subarray(1, 33));
1503
+ const y = bytesToNumber(bytes.subarray(33, 65));
1504
+ const point = new Point(x, y);
1505
+ point.assertValidity();
1506
+ return point;
1507
+ }
1508
+ static fromHex(hex) {
1509
+ const bytes = ensureBytes(hex);
1510
+ const len = bytes.length;
1511
+ const header = bytes[0];
1512
+ if (len === 32 || (len === 33 && (header === 0x02 || header === 0x03))) {
1513
+ return this.fromCompressedHex(bytes);
1514
+ }
1515
+ if (len === 65 && header === 0x04)
1516
+ return this.fromUncompressedHex(bytes);
1517
+ throw new Error(`Point.fromHex: received invalid point. Expected 32-33 compressed bytes or 65 uncompressed bytes, not ${len}`);
1518
+ }
1519
+ static fromPrivateKey(privateKey) {
1520
+ return Point.BASE.multiply(normalizePrivateKey(privateKey));
1521
+ }
1522
+ static fromSignature(msgHash, signature, recovery) {
1523
+ msgHash = ensureBytes(msgHash);
1524
+ const h = truncateHash(msgHash);
1525
+ const { r, s } = normalizeSignature(signature);
1526
+ if (recovery !== 0 && recovery !== 1) {
1527
+ throw new Error('Cannot recover signature: invalid recovery bit');
1528
+ }
1529
+ const prefix = recovery & 1 ? '03' : '02';
1530
+ const R = Point.fromHex(prefix + numTo32bStr(r));
1531
+ const { n } = CURVE;
1532
+ const rinv = invert(r, n);
1533
+ const u1 = mod(-h * rinv, n);
1534
+ const u2 = mod(s * rinv, n);
1535
+ const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2);
1536
+ if (!Q)
1537
+ throw new Error('Cannot recover signature: point at infinify');
1538
+ Q.assertValidity();
1539
+ return Q;
1540
+ }
1541
+ toRawBytes(isCompressed = false) {
1542
+ return hexToBytes(this.toHex(isCompressed));
1543
+ }
1544
+ toHex(isCompressed = false) {
1545
+ const x = numTo32bStr(this.x);
1546
+ if (isCompressed) {
1547
+ const prefix = this.hasEvenY() ? '02' : '03';
1548
+ return `${prefix}${x}`;
1549
+ }
1550
+ else {
1551
+ return `04${x}${numTo32bStr(this.y)}`;
1552
+ }
1553
+ }
1554
+ toHexX() {
1555
+ return this.toHex(true).slice(2);
1556
+ }
1557
+ toRawX() {
1558
+ return this.toRawBytes(true).slice(1);
1559
+ }
1560
+ assertValidity() {
1561
+ const msg = 'Point is not on elliptic curve';
1562
+ const { x, y } = this;
1563
+ if (!isValidFieldElement(x) || !isValidFieldElement(y))
1564
+ throw new Error(msg);
1565
+ const left = mod(y * y);
1566
+ const right = weistrass(x);
1567
+ if (mod(left - right) !== _0n)
1568
+ throw new Error(msg);
1569
+ }
1570
+ equals(other) {
1571
+ return this.x === other.x && this.y === other.y;
1572
+ }
1573
+ negate() {
1574
+ return new Point(this.x, mod(-this.y));
1575
+ }
1576
+ double() {
1577
+ return JacobianPoint.fromAffine(this).double().toAffine();
1578
+ }
1579
+ add(other) {
1580
+ return JacobianPoint.fromAffine(this).add(JacobianPoint.fromAffine(other)).toAffine();
1581
+ }
1582
+ subtract(other) {
1583
+ return this.add(other.negate());
1584
+ }
1585
+ multiply(scalar) {
1586
+ return JacobianPoint.fromAffine(this).multiply(scalar, this).toAffine();
1587
+ }
1588
+ multiplyAndAddUnsafe(Q, a, b) {
1589
+ const P = JacobianPoint.fromAffine(this);
1590
+ const aP = a === _0n || a === _1n || this !== Point.BASE ? P.multiplyUnsafe(a) : P.multiply(a);
1591
+ const bQ = JacobianPoint.fromAffine(Q).multiplyUnsafe(b);
1592
+ const sum = aP.add(bQ);
1593
+ return sum.equals(JacobianPoint.ZERO) ? undefined : sum.toAffine();
1594
+ }
1595
+ }
1596
+ Point.BASE = new Point(CURVE.Gx, CURVE.Gy);
1597
+ Point.ZERO = new Point(_0n, _0n);
1598
+ function sliceDER(s) {
1599
+ return Number.parseInt(s[0], 16) >= 8 ? '00' + s : s;
1600
+ }
1601
+ function parseDERInt(data) {
1602
+ if (data.length < 2 || data[0] !== 0x02) {
1603
+ throw new Error(`Invalid signature integer tag: ${bytesToHex(data)}`);
1604
+ }
1605
+ const len = data[1];
1606
+ const res = data.subarray(2, len + 2);
1607
+ if (!len || res.length !== len) {
1608
+ throw new Error(`Invalid signature integer: wrong length`);
1609
+ }
1610
+ if (res[0] === 0x00 && res[1] <= 0x7f) {
1611
+ throw new Error('Invalid signature integer: trailing length');
1612
+ }
1613
+ return { data: bytesToNumber(res), left: data.subarray(len + 2) };
1614
+ }
1615
+ function parseDERSignature(data) {
1616
+ if (data.length < 2 || data[0] != 0x30) {
1617
+ throw new Error(`Invalid signature tag: ${bytesToHex(data)}`);
1618
+ }
1619
+ if (data[1] !== data.length - 2) {
1620
+ throw new Error('Invalid signature: incorrect length');
1621
+ }
1622
+ const { data: r, left: sBytes } = parseDERInt(data.subarray(2));
1623
+ const { data: s, left: rBytesLeft } = parseDERInt(sBytes);
1624
+ if (rBytesLeft.length) {
1625
+ throw new Error(`Invalid signature: left bytes after parsing: ${bytesToHex(rBytesLeft)}`);
1626
+ }
1627
+ return { r, s };
1628
+ }
1629
+ class Signature {
1630
+ constructor(r, s) {
1631
+ this.r = r;
1632
+ this.s = s;
1633
+ this.assertValidity();
1634
+ }
1635
+ static fromCompact(hex) {
1636
+ const arr = hex instanceof Uint8Array;
1637
+ const name = 'Signature.fromCompact';
1638
+ if (typeof hex !== 'string' && !arr)
1639
+ throw new TypeError(`${name}: Expected string or Uint8Array`);
1640
+ const str = arr ? bytesToHex(hex) : hex;
1641
+ if (str.length !== 128)
1642
+ throw new Error(`${name}: Expected 64-byte hex`);
1643
+ return new Signature(hexToNumber(str.slice(0, 64)), hexToNumber(str.slice(64, 128)));
1644
+ }
1645
+ static fromDER(hex) {
1646
+ const arr = hex instanceof Uint8Array;
1647
+ if (typeof hex !== 'string' && !arr)
1648
+ throw new TypeError(`Signature.fromDER: Expected string or Uint8Array`);
1649
+ const { r, s } = parseDERSignature(arr ? hex : hexToBytes(hex));
1650
+ return new Signature(r, s);
1651
+ }
1652
+ static fromHex(hex) {
1653
+ return this.fromDER(hex);
1654
+ }
1655
+ assertValidity() {
1656
+ const { r, s } = this;
1657
+ if (!isWithinCurveOrder(r))
1658
+ throw new Error('Invalid Signature: r must be 0 < r < n');
1659
+ if (!isWithinCurveOrder(s))
1660
+ throw new Error('Invalid Signature: s must be 0 < s < n');
1661
+ }
1662
+ hasHighS() {
1663
+ const HALF = CURVE.n >> _1n;
1664
+ return this.s > HALF;
1665
+ }
1666
+ normalizeS() {
1667
+ return this.hasHighS() ? new Signature(this.r, CURVE.n - this.s) : this;
1668
+ }
1669
+ toDERRawBytes(isCompressed = false) {
1670
+ return hexToBytes(this.toDERHex(isCompressed));
1671
+ }
1672
+ toDERHex(isCompressed = false) {
1673
+ const sHex = sliceDER(numberToHexUnpadded(this.s));
1674
+ if (isCompressed)
1675
+ return sHex;
1676
+ const rHex = sliceDER(numberToHexUnpadded(this.r));
1677
+ const rLen = numberToHexUnpadded(rHex.length / 2);
1678
+ const sLen = numberToHexUnpadded(sHex.length / 2);
1679
+ const length = numberToHexUnpadded(rHex.length / 2 + sHex.length / 2 + 4);
1680
+ return `30${length}02${rLen}${rHex}02${sLen}${sHex}`;
1681
+ }
1682
+ toRawBytes() {
1683
+ return this.toDERRawBytes();
1684
+ }
1685
+ toHex() {
1686
+ return this.toDERHex();
1687
+ }
1688
+ toCompactRawBytes() {
1689
+ return hexToBytes(this.toCompactHex());
1690
+ }
1691
+ toCompactHex() {
1692
+ return numTo32bStr(this.r) + numTo32bStr(this.s);
1693
+ }
1694
+ }
1695
+ function concatBytes(...arrays) {
1696
+ if (!arrays.every((b) => b instanceof Uint8Array))
1697
+ throw new Error('Uint8Array list expected');
1698
+ if (arrays.length === 1)
1699
+ return arrays[0];
1700
+ const length = arrays.reduce((a, arr) => a + arr.length, 0);
1701
+ const result = new Uint8Array(length);
1702
+ for (let i = 0, pad = 0; i < arrays.length; i++) {
1703
+ const arr = arrays[i];
1704
+ result.set(arr, pad);
1705
+ pad += arr.length;
1706
+ }
1707
+ return result;
1708
+ }
1709
+ const hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));
1710
+ function bytesToHex(uint8a) {
1711
+ if (!(uint8a instanceof Uint8Array))
1712
+ throw new Error('Expected Uint8Array');
1713
+ let hex = '';
1714
+ for (let i = 0; i < uint8a.length; i++) {
1715
+ hex += hexes[uint8a[i]];
1716
+ }
1717
+ return hex;
1718
+ }
1719
+ const POW_2_256 = BigInt('0x10000000000000000000000000000000000000000000000000000000000000000');
1720
+ function numTo32bStr(num) {
1721
+ if (typeof num !== 'bigint')
1722
+ throw new Error('Expected bigint');
1723
+ if (!(_0n <= num && num < POW_2_256))
1724
+ throw new Error('Expected number < 2^256');
1725
+ return num.toString(16).padStart(64, '0');
1726
+ }
1727
+ function numTo32b(num) {
1728
+ const b = hexToBytes(numTo32bStr(num));
1729
+ if (b.length !== 32)
1730
+ throw new Error('Error: expected 32 bytes');
1731
+ return b;
1732
+ }
1733
+ function numberToHexUnpadded(num) {
1734
+ const hex = num.toString(16);
1735
+ return hex.length & 1 ? `0${hex}` : hex;
1736
+ }
1737
+ function hexToNumber(hex) {
1738
+ if (typeof hex !== 'string') {
1739
+ throw new TypeError('hexToNumber: expected string, got ' + typeof hex);
1740
+ }
1741
+ return BigInt(`0x${hex}`);
1742
+ }
1743
+ function hexToBytes(hex) {
1744
+ if (typeof hex !== 'string') {
1745
+ throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
1746
+ }
1747
+ if (hex.length % 2)
1748
+ throw new Error('hexToBytes: received invalid unpadded hex' + hex.length);
1749
+ const array = new Uint8Array(hex.length / 2);
1750
+ for (let i = 0; i < array.length; i++) {
1751
+ const j = i * 2;
1752
+ const hexByte = hex.slice(j, j + 2);
1753
+ const byte = Number.parseInt(hexByte, 16);
1754
+ if (Number.isNaN(byte) || byte < 0)
1755
+ throw new Error('Invalid byte sequence');
1756
+ array[i] = byte;
1757
+ }
1758
+ return array;
1759
+ }
1760
+ function bytesToNumber(bytes) {
1761
+ return hexToNumber(bytesToHex(bytes));
1762
+ }
1763
+ function ensureBytes(hex) {
1764
+ return hex instanceof Uint8Array ? Uint8Array.from(hex) : hexToBytes(hex);
1765
+ }
1766
+ function normalizeScalar(num) {
1767
+ if (typeof num === 'number' && Number.isSafeInteger(num) && num > 0)
1768
+ return BigInt(num);
1769
+ if (typeof num === 'bigint' && isWithinCurveOrder(num))
1770
+ return num;
1771
+ throw new TypeError('Expected valid private scalar: 0 < scalar < curve.n');
1772
+ }
1773
+ function mod(a, b = CURVE.P) {
1774
+ const result = a % b;
1775
+ return result >= _0n ? result : b + result;
1776
+ }
1777
+ function pow2(x, power) {
1778
+ const { P } = CURVE;
1779
+ let res = x;
1780
+ while (power-- > _0n) {
1781
+ res *= res;
1782
+ res %= P;
1783
+ }
1784
+ return res;
1785
+ }
1786
+ function sqrtMod(x) {
1787
+ const { P } = CURVE;
1788
+ const _6n = BigInt(6);
1789
+ const _11n = BigInt(11);
1790
+ const _22n = BigInt(22);
1791
+ const _23n = BigInt(23);
1792
+ const _44n = BigInt(44);
1793
+ const _88n = BigInt(88);
1794
+ const b2 = (x * x * x) % P;
1795
+ const b3 = (b2 * b2 * x) % P;
1796
+ const b6 = (pow2(b3, _3n) * b3) % P;
1797
+ const b9 = (pow2(b6, _3n) * b3) % P;
1798
+ const b11 = (pow2(b9, _2n) * b2) % P;
1799
+ const b22 = (pow2(b11, _11n) * b11) % P;
1800
+ const b44 = (pow2(b22, _22n) * b22) % P;
1801
+ const b88 = (pow2(b44, _44n) * b44) % P;
1802
+ const b176 = (pow2(b88, _88n) * b88) % P;
1803
+ const b220 = (pow2(b176, _44n) * b44) % P;
1804
+ const b223 = (pow2(b220, _3n) * b3) % P;
1805
+ const t1 = (pow2(b223, _23n) * b22) % P;
1806
+ const t2 = (pow2(t1, _6n) * b2) % P;
1807
+ return pow2(t2, _2n);
1808
+ }
1809
+ function invert(number, modulo = CURVE.P) {
1810
+ if (number === _0n || modulo <= _0n) {
1811
+ throw new Error(`invert: expected positive integers, got n=${number} mod=${modulo}`);
1812
+ }
1813
+ let a = mod(number, modulo);
1814
+ let b = modulo;
1815
+ let x = _0n, u = _1n;
1816
+ while (a !== _0n) {
1817
+ const q = b / a;
1818
+ const r = b % a;
1819
+ const m = x - u * q;
1820
+ b = a, a = r, x = u, u = m;
1821
+ }
1822
+ const gcd = b;
1823
+ if (gcd !== _1n)
1824
+ throw new Error('invert: does not exist');
1825
+ return mod(x, modulo);
1826
+ }
1827
+ function invertBatch(nums, p = CURVE.P) {
1828
+ const scratch = new Array(nums.length);
1829
+ const lastMultiplied = nums.reduce((acc, num, i) => {
1830
+ if (num === _0n)
1831
+ return acc;
1832
+ scratch[i] = acc;
1833
+ return mod(acc * num, p);
1834
+ }, _1n);
1835
+ const inverted = invert(lastMultiplied, p);
1836
+ nums.reduceRight((acc, num, i) => {
1837
+ if (num === _0n)
1838
+ return acc;
1839
+ scratch[i] = mod(acc * scratch[i], p);
1840
+ return mod(acc * num, p);
1841
+ }, inverted);
1842
+ return scratch;
1843
+ }
1844
+ const divNearest = (a, b) => (a + b / _2n) / b;
1845
+ const ENDO = {
1846
+ a1: BigInt('0x3086d221a7d46bcde86c90e49284eb15'),
1847
+ b1: -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3'),
1848
+ a2: BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8'),
1849
+ b2: BigInt('0x3086d221a7d46bcde86c90e49284eb15'),
1850
+ POW_2_128: BigInt('0x100000000000000000000000000000000'),
1851
+ };
1852
+ function splitScalarEndo(k) {
1853
+ const { n } = CURVE;
1854
+ const { a1, b1, a2, b2, POW_2_128 } = ENDO;
1855
+ const c1 = divNearest(b2 * k, n);
1856
+ const c2 = divNearest(-b1 * k, n);
1857
+ let k1 = mod(k - c1 * a1 - c2 * a2, n);
1858
+ let k2 = mod(-c1 * b1 - c2 * b2, n);
1859
+ const k1neg = k1 > POW_2_128;
1860
+ const k2neg = k2 > POW_2_128;
1861
+ if (k1neg)
1862
+ k1 = n - k1;
1863
+ if (k2neg)
1864
+ k2 = n - k2;
1865
+ if (k1 > POW_2_128 || k2 > POW_2_128) {
1866
+ throw new Error('splitScalarEndo: Endomorphism failed, k=' + k);
1867
+ }
1868
+ return { k1neg, k1, k2neg, k2 };
1869
+ }
1870
+ function truncateHash(hash) {
1871
+ const { n } = CURVE;
1872
+ const byteLength = hash.length;
1873
+ const delta = byteLength * 8 - 256;
1874
+ let h = bytesToNumber(hash);
1875
+ if (delta > 0)
1876
+ h = h >> BigInt(delta);
1877
+ if (h >= n)
1878
+ h -= n;
1879
+ return h;
1880
+ }
1881
+ let _sha256Sync;
1882
+ let _hmacSha256Sync;
1883
+ class HmacDrbg {
1884
+ constructor() {
1885
+ this.v = new Uint8Array(32).fill(1);
1886
+ this.k = new Uint8Array(32).fill(0);
1887
+ this.counter = 0;
1888
+ }
1889
+ hmac(...values) {
1890
+ return utils.hmacSha256(this.k, ...values);
1891
+ }
1892
+ hmacSync(...values) {
1893
+ return _hmacSha256Sync(this.k, ...values);
1894
+ }
1895
+ checkSync() {
1896
+ if (typeof _hmacSha256Sync !== 'function')
1897
+ throw new ShaError('hmacSha256Sync needs to be set');
1898
+ }
1899
+ incr() {
1900
+ if (this.counter >= 1000)
1901
+ throw new Error('Tried 1,000 k values for sign(), all were invalid');
1902
+ this.counter += 1;
1903
+ }
1904
+ async reseed(seed = new Uint8Array()) {
1905
+ this.k = await this.hmac(this.v, Uint8Array.from([0x00]), seed);
1906
+ this.v = await this.hmac(this.v);
1907
+ if (seed.length === 0)
1908
+ return;
1909
+ this.k = await this.hmac(this.v, Uint8Array.from([0x01]), seed);
1910
+ this.v = await this.hmac(this.v);
1911
+ }
1912
+ reseedSync(seed = new Uint8Array()) {
1913
+ this.checkSync();
1914
+ this.k = this.hmacSync(this.v, Uint8Array.from([0x00]), seed);
1915
+ this.v = this.hmacSync(this.v);
1916
+ if (seed.length === 0)
1917
+ return;
1918
+ this.k = this.hmacSync(this.v, Uint8Array.from([0x01]), seed);
1919
+ this.v = this.hmacSync(this.v);
1920
+ }
1921
+ async generate() {
1922
+ this.incr();
1923
+ this.v = await this.hmac(this.v);
1924
+ return this.v;
1925
+ }
1926
+ generateSync() {
1927
+ this.checkSync();
1928
+ this.incr();
1929
+ this.v = this.hmacSync(this.v);
1930
+ return this.v;
1931
+ }
1932
+ }
1933
+ function isWithinCurveOrder(num) {
1934
+ return _0n < num && num < CURVE.n;
1935
+ }
1936
+ function isValidFieldElement(num) {
1937
+ return _0n < num && num < CURVE.P;
1938
+ }
1939
+ function kmdToSig(kBytes, m, d) {
1940
+ const k = bytesToNumber(kBytes);
1941
+ if (!isWithinCurveOrder(k))
1942
+ return;
1943
+ const { n } = CURVE;
1944
+ const q = Point.BASE.multiply(k);
1945
+ const r = mod(q.x, n);
1946
+ if (r === _0n)
1947
+ return;
1948
+ const s = mod(invert(k, n) * mod(m + d * r, n), n);
1949
+ if (s === _0n)
1950
+ return;
1951
+ const sig = new Signature(r, s);
1952
+ const recovery = (q.x === sig.r ? 0 : 2) | Number(q.y & _1n);
1953
+ return { sig, recovery };
1954
+ }
1955
+ function normalizePrivateKey(key) {
1956
+ let num;
1957
+ if (typeof key === 'bigint') {
1958
+ num = key;
1959
+ }
1960
+ else if (typeof key === 'number' && Number.isSafeInteger(key) && key > 0) {
1961
+ num = BigInt(key);
1962
+ }
1963
+ else if (typeof key === 'string') {
1964
+ if (key.length !== 64)
1965
+ throw new Error('Expected 32 bytes of private key');
1966
+ num = hexToNumber(key);
1967
+ }
1968
+ else if (key instanceof Uint8Array) {
1969
+ if (key.length !== 32)
1970
+ throw new Error('Expected 32 bytes of private key');
1971
+ num = bytesToNumber(key);
1972
+ }
1973
+ else {
1974
+ throw new TypeError('Expected valid private key');
1975
+ }
1976
+ if (!isWithinCurveOrder(num))
1977
+ throw new Error('Expected private key: 0 < key < n');
1978
+ return num;
1979
+ }
1980
+ function normalizePublicKey(publicKey) {
1981
+ if (publicKey instanceof Point) {
1982
+ publicKey.assertValidity();
1983
+ return publicKey;
1984
+ }
1985
+ else {
1986
+ return Point.fromHex(publicKey);
1987
+ }
1988
+ }
1989
+ function normalizeSignature(signature) {
1990
+ if (signature instanceof Signature) {
1991
+ signature.assertValidity();
1992
+ return signature;
1993
+ }
1994
+ try {
1995
+ return Signature.fromDER(signature);
1996
+ }
1997
+ catch (error) {
1998
+ return Signature.fromCompact(signature);
1999
+ }
2000
+ }
2001
+ function getPublicKey$1(privateKey, isCompressed = false) {
2002
+ return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
2003
+ }
2004
+ function bits2int(bytes) {
2005
+ const slice = bytes.length > 32 ? bytes.slice(0, 32) : bytes;
2006
+ return bytesToNumber(slice);
2007
+ }
2008
+ function bits2octets(bytes) {
2009
+ const z1 = bits2int(bytes);
2010
+ const z2 = mod(z1, CURVE.n);
2011
+ return int2octets(z2 < _0n ? z1 : z2);
2012
+ }
2013
+ function int2octets(num) {
2014
+ return numTo32b(num);
2015
+ }
2016
+ function initSigArgs(msgHash, privateKey, extraEntropy) {
2017
+ if (msgHash == null)
2018
+ throw new Error(`sign: expected valid message hash, not "${msgHash}"`);
2019
+ const h1 = ensureBytes(msgHash);
2020
+ const d = normalizePrivateKey(privateKey);
2021
+ const seedArgs = [int2octets(d), bits2octets(h1)];
2022
+ if (extraEntropy != null) {
2023
+ if (extraEntropy === true)
2024
+ extraEntropy = utils.randomBytes(32);
2025
+ const e = ensureBytes(extraEntropy);
2026
+ if (e.length !== 32)
2027
+ throw new Error('sign: Expected 32 bytes of extra data');
2028
+ seedArgs.push(e);
2029
+ }
2030
+ const seed = concatBytes(...seedArgs);
2031
+ const m = bits2int(h1);
2032
+ return { seed, m, d };
2033
+ }
2034
+ function finalizeSig(recSig, opts) {
2035
+ let { sig, recovery } = recSig;
2036
+ const { canonical, der, recovered } = Object.assign({ canonical: true, der: true }, opts);
2037
+ if (canonical && sig.hasHighS()) {
2038
+ sig = sig.normalizeS();
2039
+ recovery ^= 1;
2040
+ }
2041
+ const hashed = der ? sig.toDERRawBytes() : sig.toCompactRawBytes();
2042
+ return recovered ? [hashed, recovery] : hashed;
2043
+ }
2044
+ async function sign$1(msgHash, privKey, opts = {}) {
2045
+ const { seed, m, d } = initSigArgs(msgHash, privKey, opts.extraEntropy);
2046
+ let sig;
2047
+ const drbg = new HmacDrbg();
2048
+ await drbg.reseed(seed);
2049
+ while (!(sig = kmdToSig(await drbg.generate(), m, d)))
2050
+ await drbg.reseed();
2051
+ return finalizeSig(sig, opts);
2052
+ }
2053
+ const vopts = { strict: true };
2054
+ function verify(signature, msgHash, publicKey, opts = vopts) {
2055
+ let sig;
2056
+ try {
2057
+ sig = normalizeSignature(signature);
2058
+ msgHash = ensureBytes(msgHash);
2059
+ }
2060
+ catch (error) {
2061
+ return false;
2062
+ }
2063
+ const { r, s } = sig;
2064
+ if (opts.strict && sig.hasHighS())
2065
+ return false;
2066
+ const h = truncateHash(msgHash);
2067
+ let P;
2068
+ try {
2069
+ P = normalizePublicKey(publicKey);
2070
+ }
2071
+ catch (error) {
2072
+ return false;
2073
+ }
2074
+ const { n } = CURVE;
2075
+ const sinv = invert(s, n);
2076
+ const u1 = mod(h * sinv, n);
2077
+ const u2 = mod(r * sinv, n);
2078
+ const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2);
2079
+ if (!R)
2080
+ return false;
2081
+ const v = mod(R.x, n);
2082
+ return v === r;
2083
+ }
2084
+ Point.BASE._setWindowSize(8);
2085
+ const crypto$1 = {
2086
+ node: nodeCrypto,
2087
+ web: typeof self === 'object' && 'crypto' in self ? self.crypto : undefined,
2088
+ };
2089
+ const TAGGED_HASH_PREFIXES = {};
2090
+ const utils = {
2091
+ bytesToHex,
2092
+ hexToBytes,
2093
+ concatBytes,
2094
+ mod,
2095
+ invert,
2096
+ isValidPrivateKey(privateKey) {
2097
+ try {
2098
+ normalizePrivateKey(privateKey);
2099
+ return true;
2100
+ }
2101
+ catch (error) {
2102
+ return false;
2103
+ }
2104
+ },
2105
+ _bigintTo32Bytes: numTo32b,
2106
+ _normalizePrivateKey: normalizePrivateKey,
2107
+ hashToPrivateKey: (hash) => {
2108
+ hash = ensureBytes(hash);
2109
+ if (hash.length < 40 || hash.length > 1024)
2110
+ throw new Error('Expected 40-1024 bytes of private key as per FIPS 186');
2111
+ const num = mod(bytesToNumber(hash), CURVE.n - _1n) + _1n;
2112
+ return numTo32b(num);
2113
+ },
2114
+ randomBytes: (bytesLength = 32) => {
2115
+ if (crypto$1.web) {
2116
+ return crypto$1.web.getRandomValues(new Uint8Array(bytesLength));
2117
+ }
2118
+ else if (crypto$1.node) {
2119
+ const { randomBytes } = crypto$1.node;
2120
+ return Uint8Array.from(randomBytes(bytesLength));
2121
+ }
2122
+ else {
2123
+ throw new Error("The environment doesn't have randomBytes function");
2124
+ }
2125
+ },
2126
+ randomPrivateKey: () => {
2127
+ return utils.hashToPrivateKey(utils.randomBytes(40));
2128
+ },
2129
+ sha256: async (...messages) => {
2130
+ if (crypto$1.web) {
2131
+ const buffer = await crypto$1.web.subtle.digest('SHA-256', concatBytes(...messages));
2132
+ return new Uint8Array(buffer);
2133
+ }
2134
+ else if (crypto$1.node) {
2135
+ const { createHash } = crypto$1.node;
2136
+ const hash = createHash('sha256');
2137
+ messages.forEach((m) => hash.update(m));
2138
+ return Uint8Array.from(hash.digest());
2139
+ }
2140
+ else {
2141
+ throw new Error("The environment doesn't have sha256 function");
2142
+ }
2143
+ },
2144
+ hmacSha256: async (key, ...messages) => {
2145
+ if (crypto$1.web) {
2146
+ const ckey = await crypto$1.web.subtle.importKey('raw', key, { name: 'HMAC', hash: { name: 'SHA-256' } }, false, ['sign']);
2147
+ const message = concatBytes(...messages);
2148
+ const buffer = await crypto$1.web.subtle.sign('HMAC', ckey, message);
2149
+ return new Uint8Array(buffer);
2150
+ }
2151
+ else if (crypto$1.node) {
2152
+ const { createHmac } = crypto$1.node;
2153
+ const hash = createHmac('sha256', key);
2154
+ messages.forEach((m) => hash.update(m));
2155
+ return Uint8Array.from(hash.digest());
2156
+ }
2157
+ else {
2158
+ throw new Error("The environment doesn't have hmac-sha256 function");
2159
+ }
2160
+ },
2161
+ sha256Sync: undefined,
2162
+ hmacSha256Sync: undefined,
2163
+ taggedHash: async (tag, ...messages) => {
2164
+ let tagP = TAGGED_HASH_PREFIXES[tag];
2165
+ if (tagP === undefined) {
2166
+ const tagH = await utils.sha256(Uint8Array.from(tag, (c) => c.charCodeAt(0)));
2167
+ tagP = concatBytes(tagH, tagH);
2168
+ TAGGED_HASH_PREFIXES[tag] = tagP;
2169
+ }
2170
+ return utils.sha256(tagP, ...messages);
2171
+ },
2172
+ taggedHashSync: (tag, ...messages) => {
2173
+ if (typeof _sha256Sync !== 'function')
2174
+ throw new ShaError('sha256Sync is undefined, you need to set it');
2175
+ let tagP = TAGGED_HASH_PREFIXES[tag];
2176
+ if (tagP === undefined) {
2177
+ const tagH = _sha256Sync(Uint8Array.from(tag, (c) => c.charCodeAt(0)));
2178
+ tagP = concatBytes(tagH, tagH);
2179
+ TAGGED_HASH_PREFIXES[tag] = tagP;
2180
+ }
2181
+ return _sha256Sync(tagP, ...messages);
2182
+ },
2183
+ precompute(windowSize = 8, point = Point.BASE) {
2184
+ const cached = point === Point.BASE ? point : new Point(point.x, point.y);
2185
+ cached._setWindowSize(windowSize);
2186
+ cached.multiply(_3n);
2187
+ return cached;
2188
+ },
2189
+ };
2190
+ Object.defineProperties(utils, {
2191
+ sha256Sync: {
2192
+ configurable: false,
2193
+ get() {
2194
+ return _sha256Sync;
2195
+ },
2196
+ set(val) {
2197
+ if (!_sha256Sync)
2198
+ _sha256Sync = val;
2199
+ },
2200
+ },
2201
+ hmacSha256Sync: {
2202
+ configurable: false,
2203
+ get() {
2204
+ return _hmacSha256Sync;
2205
+ },
2206
+ set(val) {
2207
+ if (!_hmacSha256Sync)
2208
+ _hmacSha256Sync = val;
2209
+ },
2210
+ },
2211
+ });
2212
+
2213
+ var sha3$1 = {exports: {}};
2214
+
2215
+ /**
2216
+ * [js-sha3]{@link https://github.com/emn178/js-sha3}
2217
+ *
2218
+ * @version 0.8.0
2219
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
2220
+ * @copyright Chen, Yi-Cyuan 2015-2018
2221
+ * @license MIT
2222
+ */
2223
+
2224
+ (function (module) {
2225
+ /*jslint bitwise: true */
2226
+ (function () {
2227
+
2228
+ var INPUT_ERROR = 'input is invalid type';
2229
+ var FINALIZE_ERROR = 'finalize already called';
2230
+ var WINDOW = typeof window === 'object';
2231
+ var root = WINDOW ? window : {};
2232
+ if (root.JS_SHA3_NO_WINDOW) {
2233
+ WINDOW = false;
2234
+ }
2235
+ var WEB_WORKER = !WINDOW && typeof self === 'object';
2236
+ var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
2237
+ if (NODE_JS) {
2238
+ root = commonjsGlobal;
2239
+ } else if (WEB_WORKER) {
2240
+ root = self;
2241
+ }
2242
+ var COMMON_JS = !root.JS_SHA3_NO_COMMON_JS && 'object' === 'object' && module.exports;
2243
+ var ARRAY_BUFFER = !root.JS_SHA3_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
2244
+ var HEX_CHARS = '0123456789abcdef'.split('');
2245
+ var SHAKE_PADDING = [31, 7936, 2031616, 520093696];
2246
+ var CSHAKE_PADDING = [4, 1024, 262144, 67108864];
2247
+ var KECCAK_PADDING = [1, 256, 65536, 16777216];
2248
+ var PADDING = [6, 1536, 393216, 100663296];
2249
+ var SHIFT = [0, 8, 16, 24];
2250
+ var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649,
2251
+ 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0,
2252
+ 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771,
2253
+ 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648,
2254
+ 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
2255
+ var BITS = [224, 256, 384, 512];
2256
+ var SHAKE_BITS = [128, 256];
2257
+ var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array', 'digest'];
2258
+ var CSHAKE_BYTEPAD = {
2259
+ '128': 168,
2260
+ '256': 136
2261
+ };
2262
+
2263
+ if (root.JS_SHA3_NO_NODE_JS || !Array.isArray) {
2264
+ Array.isArray = function (obj) {
2265
+ return Object.prototype.toString.call(obj) === '[object Array]';
2266
+ };
2267
+ }
2268
+
2269
+ if (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
2270
+ ArrayBuffer.isView = function (obj) {
2271
+ return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
2272
+ };
2273
+ }
2274
+
2275
+ var createOutputMethod = function (bits, padding, outputType) {
2276
+ return function (message) {
2277
+ return new Keccak(bits, padding, bits).update(message)[outputType]();
2278
+ };
2279
+ };
2280
+
2281
+ var createShakeOutputMethod = function (bits, padding, outputType) {
2282
+ return function (message, outputBits) {
2283
+ return new Keccak(bits, padding, outputBits).update(message)[outputType]();
2284
+ };
2285
+ };
2286
+
2287
+ var createCshakeOutputMethod = function (bits, padding, outputType) {
2288
+ return function (message, outputBits, n, s) {
2289
+ return methods['cshake' + bits].update(message, outputBits, n, s)[outputType]();
2290
+ };
2291
+ };
2292
+
2293
+ var createKmacOutputMethod = function (bits, padding, outputType) {
2294
+ return function (key, message, outputBits, s) {
2295
+ return methods['kmac' + bits].update(key, message, outputBits, s)[outputType]();
2296
+ };
2297
+ };
2298
+
2299
+ var createOutputMethods = function (method, createMethod, bits, padding) {
2300
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
2301
+ var type = OUTPUT_TYPES[i];
2302
+ method[type] = createMethod(bits, padding, type);
2303
+ }
2304
+ return method;
2305
+ };
2306
+
2307
+ var createMethod = function (bits, padding) {
2308
+ var method = createOutputMethod(bits, padding, 'hex');
2309
+ method.create = function () {
2310
+ return new Keccak(bits, padding, bits);
2311
+ };
2312
+ method.update = function (message) {
2313
+ return method.create().update(message);
2314
+ };
2315
+ return createOutputMethods(method, createOutputMethod, bits, padding);
2316
+ };
2317
+
2318
+ var createShakeMethod = function (bits, padding) {
2319
+ var method = createShakeOutputMethod(bits, padding, 'hex');
2320
+ method.create = function (outputBits) {
2321
+ return new Keccak(bits, padding, outputBits);
2322
+ };
2323
+ method.update = function (message, outputBits) {
2324
+ return method.create(outputBits).update(message);
2325
+ };
2326
+ return createOutputMethods(method, createShakeOutputMethod, bits, padding);
2327
+ };
2328
+
2329
+ var createCshakeMethod = function (bits, padding) {
2330
+ var w = CSHAKE_BYTEPAD[bits];
2331
+ var method = createCshakeOutputMethod(bits, padding, 'hex');
2332
+ method.create = function (outputBits, n, s) {
2333
+ if (!n && !s) {
2334
+ return methods['shake' + bits].create(outputBits);
2335
+ } else {
2336
+ return new Keccak(bits, padding, outputBits).bytepad([n, s], w);
2337
+ }
2338
+ };
2339
+ method.update = function (message, outputBits, n, s) {
2340
+ return method.create(outputBits, n, s).update(message);
2341
+ };
2342
+ return createOutputMethods(method, createCshakeOutputMethod, bits, padding);
2343
+ };
2344
+
2345
+ var createKmacMethod = function (bits, padding) {
2346
+ var w = CSHAKE_BYTEPAD[bits];
2347
+ var method = createKmacOutputMethod(bits, padding, 'hex');
2348
+ method.create = function (key, outputBits, s) {
2349
+ return new Kmac(bits, padding, outputBits).bytepad(['KMAC', s], w).bytepad([key], w);
2350
+ };
2351
+ method.update = function (key, message, outputBits, s) {
2352
+ return method.create(key, outputBits, s).update(message);
2353
+ };
2354
+ return createOutputMethods(method, createKmacOutputMethod, bits, padding);
2355
+ };
2356
+
2357
+ var algorithms = [
2358
+ { name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod },
2359
+ { name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod },
2360
+ { name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod },
2361
+ { name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod },
2362
+ { name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod }
2363
+ ];
2364
+
2365
+ var methods = {}, methodNames = [];
2366
+
2367
+ for (var i = 0; i < algorithms.length; ++i) {
2368
+ var algorithm = algorithms[i];
2369
+ var bits = algorithm.bits;
2370
+ for (var j = 0; j < bits.length; ++j) {
2371
+ var methodName = algorithm.name + '_' + bits[j];
2372
+ methodNames.push(methodName);
2373
+ methods[methodName] = algorithm.createMethod(bits[j], algorithm.padding);
2374
+ if (algorithm.name !== 'sha3') {
2375
+ var newMethodName = algorithm.name + bits[j];
2376
+ methodNames.push(newMethodName);
2377
+ methods[newMethodName] = methods[methodName];
2378
+ }
2379
+ }
2380
+ }
2381
+
2382
+ function Keccak(bits, padding, outputBits) {
2383
+ this.blocks = [];
2384
+ this.s = [];
2385
+ this.padding = padding;
2386
+ this.outputBits = outputBits;
2387
+ this.reset = true;
2388
+ this.finalized = false;
2389
+ this.block = 0;
2390
+ this.start = 0;
2391
+ this.blockCount = (1600 - (bits << 1)) >> 5;
2392
+ this.byteCount = this.blockCount << 2;
2393
+ this.outputBlocks = outputBits >> 5;
2394
+ this.extraBytes = (outputBits & 31) >> 3;
2395
+
2396
+ for (var i = 0; i < 50; ++i) {
2397
+ this.s[i] = 0;
2398
+ }
2399
+ }
2400
+
2401
+ Keccak.prototype.update = function (message) {
2402
+ if (this.finalized) {
2403
+ throw new Error(FINALIZE_ERROR);
2404
+ }
2405
+ var notString, type = typeof message;
2406
+ if (type !== 'string') {
2407
+ if (type === 'object') {
2408
+ if (message === null) {
2409
+ throw new Error(INPUT_ERROR);
2410
+ } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
2411
+ message = new Uint8Array(message);
2412
+ } else if (!Array.isArray(message)) {
2413
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
2414
+ throw new Error(INPUT_ERROR);
2415
+ }
2416
+ }
2417
+ } else {
2418
+ throw new Error(INPUT_ERROR);
2419
+ }
2420
+ notString = true;
2421
+ }
2422
+ var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
2423
+ blockCount = this.blockCount, index = 0, s = this.s, i, code;
2424
+
2425
+ while (index < length) {
2426
+ if (this.reset) {
2427
+ this.reset = false;
2428
+ blocks[0] = this.block;
2429
+ for (i = 1; i < blockCount + 1; ++i) {
2430
+ blocks[i] = 0;
2431
+ }
2432
+ }
2433
+ if (notString) {
2434
+ for (i = this.start; index < length && i < byteCount; ++index) {
2435
+ blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
2436
+ }
2437
+ } else {
2438
+ for (i = this.start; index < length && i < byteCount; ++index) {
2439
+ code = message.charCodeAt(index);
2440
+ if (code < 0x80) {
2441
+ blocks[i >> 2] |= code << SHIFT[i++ & 3];
2442
+ } else if (code < 0x800) {
2443
+ blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
2444
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
2445
+ } else if (code < 0xd800 || code >= 0xe000) {
2446
+ blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
2447
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
2448
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
2449
+ } else {
2450
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
2451
+ blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
2452
+ blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
2453
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
2454
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
2455
+ }
2456
+ }
2457
+ }
2458
+ this.lastByteIndex = i;
2459
+ if (i >= byteCount) {
2460
+ this.start = i - byteCount;
2461
+ this.block = blocks[blockCount];
2462
+ for (i = 0; i < blockCount; ++i) {
2463
+ s[i] ^= blocks[i];
2464
+ }
2465
+ f(s);
2466
+ this.reset = true;
2467
+ } else {
2468
+ this.start = i;
2469
+ }
2470
+ }
2471
+ return this;
2472
+ };
2473
+
2474
+ Keccak.prototype.encode = function (x, right) {
2475
+ var o = x & 255, n = 1;
2476
+ var bytes = [o];
2477
+ x = x >> 8;
2478
+ o = x & 255;
2479
+ while (o > 0) {
2480
+ bytes.unshift(o);
2481
+ x = x >> 8;
2482
+ o = x & 255;
2483
+ ++n;
2484
+ }
2485
+ if (right) {
2486
+ bytes.push(n);
2487
+ } else {
2488
+ bytes.unshift(n);
2489
+ }
2490
+ this.update(bytes);
2491
+ return bytes.length;
2492
+ };
2493
+
2494
+ Keccak.prototype.encodeString = function (str) {
2495
+ var notString, type = typeof str;
2496
+ if (type !== 'string') {
2497
+ if (type === 'object') {
2498
+ if (str === null) {
2499
+ throw new Error(INPUT_ERROR);
2500
+ } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) {
2501
+ str = new Uint8Array(str);
2502
+ } else if (!Array.isArray(str)) {
2503
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) {
2504
+ throw new Error(INPUT_ERROR);
2505
+ }
2506
+ }
2507
+ } else {
2508
+ throw new Error(INPUT_ERROR);
2509
+ }
2510
+ notString = true;
2511
+ }
2512
+ var bytes = 0, length = str.length;
2513
+ if (notString) {
2514
+ bytes = length;
2515
+ } else {
2516
+ for (var i = 0; i < str.length; ++i) {
2517
+ var code = str.charCodeAt(i);
2518
+ if (code < 0x80) {
2519
+ bytes += 1;
2520
+ } else if (code < 0x800) {
2521
+ bytes += 2;
2522
+ } else if (code < 0xd800 || code >= 0xe000) {
2523
+ bytes += 3;
2524
+ } else {
2525
+ code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
2526
+ bytes += 4;
2527
+ }
2528
+ }
2529
+ }
2530
+ bytes += this.encode(bytes * 8);
2531
+ this.update(str);
2532
+ return bytes;
2533
+ };
2534
+
2535
+ Keccak.prototype.bytepad = function (strs, w) {
2536
+ var bytes = this.encode(w);
2537
+ for (var i = 0; i < strs.length; ++i) {
2538
+ bytes += this.encodeString(strs[i]);
2539
+ }
2540
+ var paddingBytes = w - bytes % w;
2541
+ var zeros = [];
2542
+ zeros.length = paddingBytes;
2543
+ this.update(zeros);
2544
+ return this;
2545
+ };
2546
+
2547
+ Keccak.prototype.finalize = function () {
2548
+ if (this.finalized) {
2549
+ return;
2550
+ }
2551
+ this.finalized = true;
2552
+ var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s;
2553
+ blocks[i >> 2] |= this.padding[i & 3];
2554
+ if (this.lastByteIndex === this.byteCount) {
2555
+ blocks[0] = blocks[blockCount];
2556
+ for (i = 1; i < blockCount + 1; ++i) {
2557
+ blocks[i] = 0;
2558
+ }
2559
+ }
2560
+ blocks[blockCount - 1] |= 0x80000000;
2561
+ for (i = 0; i < blockCount; ++i) {
2562
+ s[i] ^= blocks[i];
2563
+ }
2564
+ f(s);
2565
+ };
2566
+
2567
+ Keccak.prototype.toString = Keccak.prototype.hex = function () {
2568
+ this.finalize();
2569
+
2570
+ var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
2571
+ extraBytes = this.extraBytes, i = 0, j = 0;
2572
+ var hex = '', block;
2573
+ while (j < outputBlocks) {
2574
+ for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
2575
+ block = s[i];
2576
+ hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] +
2577
+ HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] +
2578
+ HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] +
2579
+ HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F];
2580
+ }
2581
+ if (j % blockCount === 0) {
2582
+ f(s);
2583
+ i = 0;
2584
+ }
2585
+ }
2586
+ if (extraBytes) {
2587
+ block = s[i];
2588
+ hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F];
2589
+ if (extraBytes > 1) {
2590
+ hex += HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F];
2591
+ }
2592
+ if (extraBytes > 2) {
2593
+ hex += HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F];
2594
+ }
2595
+ }
2596
+ return hex;
2597
+ };
2598
+
2599
+ Keccak.prototype.arrayBuffer = function () {
2600
+ this.finalize();
2601
+
2602
+ var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
2603
+ extraBytes = this.extraBytes, i = 0, j = 0;
2604
+ var bytes = this.outputBits >> 3;
2605
+ var buffer;
2606
+ if (extraBytes) {
2607
+ buffer = new ArrayBuffer((outputBlocks + 1) << 2);
2608
+ } else {
2609
+ buffer = new ArrayBuffer(bytes);
2610
+ }
2611
+ var array = new Uint32Array(buffer);
2612
+ while (j < outputBlocks) {
2613
+ for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
2614
+ array[j] = s[i];
2615
+ }
2616
+ if (j % blockCount === 0) {
2617
+ f(s);
2618
+ }
2619
+ }
2620
+ if (extraBytes) {
2621
+ array[i] = s[i];
2622
+ buffer = buffer.slice(0, bytes);
2623
+ }
2624
+ return buffer;
2625
+ };
2626
+
2627
+ Keccak.prototype.buffer = Keccak.prototype.arrayBuffer;
2628
+
2629
+ Keccak.prototype.digest = Keccak.prototype.array = function () {
2630
+ this.finalize();
2631
+
2632
+ var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
2633
+ extraBytes = this.extraBytes, i = 0, j = 0;
2634
+ var array = [], offset, block;
2635
+ while (j < outputBlocks) {
2636
+ for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
2637
+ offset = j << 2;
2638
+ block = s[i];
2639
+ array[offset] = block & 0xFF;
2640
+ array[offset + 1] = (block >> 8) & 0xFF;
2641
+ array[offset + 2] = (block >> 16) & 0xFF;
2642
+ array[offset + 3] = (block >> 24) & 0xFF;
2643
+ }
2644
+ if (j % blockCount === 0) {
2645
+ f(s);
2646
+ }
2647
+ }
2648
+ if (extraBytes) {
2649
+ offset = j << 2;
2650
+ block = s[i];
2651
+ array[offset] = block & 0xFF;
2652
+ if (extraBytes > 1) {
2653
+ array[offset + 1] = (block >> 8) & 0xFF;
2654
+ }
2655
+ if (extraBytes > 2) {
2656
+ array[offset + 2] = (block >> 16) & 0xFF;
2657
+ }
2658
+ }
2659
+ return array;
2660
+ };
2661
+
2662
+ function Kmac(bits, padding, outputBits) {
2663
+ Keccak.call(this, bits, padding, outputBits);
2664
+ }
2665
+
2666
+ Kmac.prototype = new Keccak();
2667
+
2668
+ Kmac.prototype.finalize = function () {
2669
+ this.encode(this.outputBits, true);
2670
+ return Keccak.prototype.finalize.call(this);
2671
+ };
2672
+
2673
+ var f = function (s) {
2674
+ var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9,
2675
+ b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17,
2676
+ b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33,
2677
+ b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49;
2678
+ for (n = 0; n < 48; n += 2) {
2679
+ c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40];
2680
+ c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41];
2681
+ c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42];
2682
+ c3 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43];
2683
+ c4 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44];
2684
+ c5 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45];
2685
+ c6 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46];
2686
+ c7 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47];
2687
+ c8 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48];
2688
+ c9 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49];
2689
+
2690
+ h = c8 ^ ((c2 << 1) | (c3 >>> 31));
2691
+ l = c9 ^ ((c3 << 1) | (c2 >>> 31));
2692
+ s[0] ^= h;
2693
+ s[1] ^= l;
2694
+ s[10] ^= h;
2695
+ s[11] ^= l;
2696
+ s[20] ^= h;
2697
+ s[21] ^= l;
2698
+ s[30] ^= h;
2699
+ s[31] ^= l;
2700
+ s[40] ^= h;
2701
+ s[41] ^= l;
2702
+ h = c0 ^ ((c4 << 1) | (c5 >>> 31));
2703
+ l = c1 ^ ((c5 << 1) | (c4 >>> 31));
2704
+ s[2] ^= h;
2705
+ s[3] ^= l;
2706
+ s[12] ^= h;
2707
+ s[13] ^= l;
2708
+ s[22] ^= h;
2709
+ s[23] ^= l;
2710
+ s[32] ^= h;
2711
+ s[33] ^= l;
2712
+ s[42] ^= h;
2713
+ s[43] ^= l;
2714
+ h = c2 ^ ((c6 << 1) | (c7 >>> 31));
2715
+ l = c3 ^ ((c7 << 1) | (c6 >>> 31));
2716
+ s[4] ^= h;
2717
+ s[5] ^= l;
2718
+ s[14] ^= h;
2719
+ s[15] ^= l;
2720
+ s[24] ^= h;
2721
+ s[25] ^= l;
2722
+ s[34] ^= h;
2723
+ s[35] ^= l;
2724
+ s[44] ^= h;
2725
+ s[45] ^= l;
2726
+ h = c4 ^ ((c8 << 1) | (c9 >>> 31));
2727
+ l = c5 ^ ((c9 << 1) | (c8 >>> 31));
2728
+ s[6] ^= h;
2729
+ s[7] ^= l;
2730
+ s[16] ^= h;
2731
+ s[17] ^= l;
2732
+ s[26] ^= h;
2733
+ s[27] ^= l;
2734
+ s[36] ^= h;
2735
+ s[37] ^= l;
2736
+ s[46] ^= h;
2737
+ s[47] ^= l;
2738
+ h = c6 ^ ((c0 << 1) | (c1 >>> 31));
2739
+ l = c7 ^ ((c1 << 1) | (c0 >>> 31));
2740
+ s[8] ^= h;
2741
+ s[9] ^= l;
2742
+ s[18] ^= h;
2743
+ s[19] ^= l;
2744
+ s[28] ^= h;
2745
+ s[29] ^= l;
2746
+ s[38] ^= h;
2747
+ s[39] ^= l;
2748
+ s[48] ^= h;
2749
+ s[49] ^= l;
2750
+
2751
+ b0 = s[0];
2752
+ b1 = s[1];
2753
+ b32 = (s[11] << 4) | (s[10] >>> 28);
2754
+ b33 = (s[10] << 4) | (s[11] >>> 28);
2755
+ b14 = (s[20] << 3) | (s[21] >>> 29);
2756
+ b15 = (s[21] << 3) | (s[20] >>> 29);
2757
+ b46 = (s[31] << 9) | (s[30] >>> 23);
2758
+ b47 = (s[30] << 9) | (s[31] >>> 23);
2759
+ b28 = (s[40] << 18) | (s[41] >>> 14);
2760
+ b29 = (s[41] << 18) | (s[40] >>> 14);
2761
+ b20 = (s[2] << 1) | (s[3] >>> 31);
2762
+ b21 = (s[3] << 1) | (s[2] >>> 31);
2763
+ b2 = (s[13] << 12) | (s[12] >>> 20);
2764
+ b3 = (s[12] << 12) | (s[13] >>> 20);
2765
+ b34 = (s[22] << 10) | (s[23] >>> 22);
2766
+ b35 = (s[23] << 10) | (s[22] >>> 22);
2767
+ b16 = (s[33] << 13) | (s[32] >>> 19);
2768
+ b17 = (s[32] << 13) | (s[33] >>> 19);
2769
+ b48 = (s[42] << 2) | (s[43] >>> 30);
2770
+ b49 = (s[43] << 2) | (s[42] >>> 30);
2771
+ b40 = (s[5] << 30) | (s[4] >>> 2);
2772
+ b41 = (s[4] << 30) | (s[5] >>> 2);
2773
+ b22 = (s[14] << 6) | (s[15] >>> 26);
2774
+ b23 = (s[15] << 6) | (s[14] >>> 26);
2775
+ b4 = (s[25] << 11) | (s[24] >>> 21);
2776
+ b5 = (s[24] << 11) | (s[25] >>> 21);
2777
+ b36 = (s[34] << 15) | (s[35] >>> 17);
2778
+ b37 = (s[35] << 15) | (s[34] >>> 17);
2779
+ b18 = (s[45] << 29) | (s[44] >>> 3);
2780
+ b19 = (s[44] << 29) | (s[45] >>> 3);
2781
+ b10 = (s[6] << 28) | (s[7] >>> 4);
2782
+ b11 = (s[7] << 28) | (s[6] >>> 4);
2783
+ b42 = (s[17] << 23) | (s[16] >>> 9);
2784
+ b43 = (s[16] << 23) | (s[17] >>> 9);
2785
+ b24 = (s[26] << 25) | (s[27] >>> 7);
2786
+ b25 = (s[27] << 25) | (s[26] >>> 7);
2787
+ b6 = (s[36] << 21) | (s[37] >>> 11);
2788
+ b7 = (s[37] << 21) | (s[36] >>> 11);
2789
+ b38 = (s[47] << 24) | (s[46] >>> 8);
2790
+ b39 = (s[46] << 24) | (s[47] >>> 8);
2791
+ b30 = (s[8] << 27) | (s[9] >>> 5);
2792
+ b31 = (s[9] << 27) | (s[8] >>> 5);
2793
+ b12 = (s[18] << 20) | (s[19] >>> 12);
2794
+ b13 = (s[19] << 20) | (s[18] >>> 12);
2795
+ b44 = (s[29] << 7) | (s[28] >>> 25);
2796
+ b45 = (s[28] << 7) | (s[29] >>> 25);
2797
+ b26 = (s[38] << 8) | (s[39] >>> 24);
2798
+ b27 = (s[39] << 8) | (s[38] >>> 24);
2799
+ b8 = (s[48] << 14) | (s[49] >>> 18);
2800
+ b9 = (s[49] << 14) | (s[48] >>> 18);
2801
+
2802
+ s[0] = b0 ^ (~b2 & b4);
2803
+ s[1] = b1 ^ (~b3 & b5);
2804
+ s[10] = b10 ^ (~b12 & b14);
2805
+ s[11] = b11 ^ (~b13 & b15);
2806
+ s[20] = b20 ^ (~b22 & b24);
2807
+ s[21] = b21 ^ (~b23 & b25);
2808
+ s[30] = b30 ^ (~b32 & b34);
2809
+ s[31] = b31 ^ (~b33 & b35);
2810
+ s[40] = b40 ^ (~b42 & b44);
2811
+ s[41] = b41 ^ (~b43 & b45);
2812
+ s[2] = b2 ^ (~b4 & b6);
2813
+ s[3] = b3 ^ (~b5 & b7);
2814
+ s[12] = b12 ^ (~b14 & b16);
2815
+ s[13] = b13 ^ (~b15 & b17);
2816
+ s[22] = b22 ^ (~b24 & b26);
2817
+ s[23] = b23 ^ (~b25 & b27);
2818
+ s[32] = b32 ^ (~b34 & b36);
2819
+ s[33] = b33 ^ (~b35 & b37);
2820
+ s[42] = b42 ^ (~b44 & b46);
2821
+ s[43] = b43 ^ (~b45 & b47);
2822
+ s[4] = b4 ^ (~b6 & b8);
2823
+ s[5] = b5 ^ (~b7 & b9);
2824
+ s[14] = b14 ^ (~b16 & b18);
2825
+ s[15] = b15 ^ (~b17 & b19);
2826
+ s[24] = b24 ^ (~b26 & b28);
2827
+ s[25] = b25 ^ (~b27 & b29);
2828
+ s[34] = b34 ^ (~b36 & b38);
2829
+ s[35] = b35 ^ (~b37 & b39);
2830
+ s[44] = b44 ^ (~b46 & b48);
2831
+ s[45] = b45 ^ (~b47 & b49);
2832
+ s[6] = b6 ^ (~b8 & b0);
2833
+ s[7] = b7 ^ (~b9 & b1);
2834
+ s[16] = b16 ^ (~b18 & b10);
2835
+ s[17] = b17 ^ (~b19 & b11);
2836
+ s[26] = b26 ^ (~b28 & b20);
2837
+ s[27] = b27 ^ (~b29 & b21);
2838
+ s[36] = b36 ^ (~b38 & b30);
2839
+ s[37] = b37 ^ (~b39 & b31);
2840
+ s[46] = b46 ^ (~b48 & b40);
2841
+ s[47] = b47 ^ (~b49 & b41);
2842
+ s[8] = b8 ^ (~b0 & b2);
2843
+ s[9] = b9 ^ (~b1 & b3);
2844
+ s[18] = b18 ^ (~b10 & b12);
2845
+ s[19] = b19 ^ (~b11 & b13);
2846
+ s[28] = b28 ^ (~b20 & b22);
2847
+ s[29] = b29 ^ (~b21 & b23);
2848
+ s[38] = b38 ^ (~b30 & b32);
2849
+ s[39] = b39 ^ (~b31 & b33);
2850
+ s[48] = b48 ^ (~b40 & b42);
2851
+ s[49] = b49 ^ (~b41 & b43);
2852
+
2853
+ s[0] ^= RC[n];
2854
+ s[1] ^= RC[n + 1];
2855
+ }
2856
+ };
2857
+
2858
+ if (COMMON_JS) {
2859
+ module.exports = methods;
2860
+ } else {
2861
+ for (i = 0; i < methodNames.length; ++i) {
2862
+ root[methodNames[i]] = methods[methodNames[i]];
2863
+ }
2864
+ }
2865
+ })();
2866
+ } (sha3$1));
2867
+
2868
+ var sha3 = sha3$1.exports;
2869
+
2870
+ const randomBytes = utils.randomBytes;
2871
+ /**
2872
+ * Return the public key for the given private key, to be used for asymmetric
2873
+ * encryption.
2874
+ */
2875
+ const getPublicKey = getPublicKey$1;
2876
+ /**
2877
+ * ECDSA Sign a message with the given private key.
2878
+ *
2879
+ * @param message The message to sign, usually a hash.
2880
+ * @param privateKey The ECDSA private key to use to sign the message.
2881
+ *
2882
+ * @returns The signature and the recovery id concatenated.
2883
+ */
2884
+ async function sign(message, privateKey) {
2885
+ const [signature, recoveryId] = await sign$1(message, privateKey, {
2886
+ recovered: true,
2887
+ der: false,
2888
+ });
2889
+ return concat([signature, new Uint8Array([recoveryId])], signature.length + 1);
2890
+ }
2891
+ function keccak256(input) {
2892
+ return new Uint8Array(sha3.keccak256.arrayBuffer(input));
2893
+ }
2894
+ function compressPublicKey(publicKey) {
2895
+ if (publicKey.length === 64) {
2896
+ publicKey = concat([new Uint8Array([4]), publicKey], 65);
2897
+ }
2898
+ const point = Point.fromHex(publicKey);
2899
+ return point.toRawBytes(true);
2900
+ }
2901
+ /**
2902
+ * Verify an ECDSA signature.
2903
+ */
2904
+ function verifySignature(signature, message, publicKey) {
2905
+ try {
2906
+ const _signature = Signature.fromCompact(signature.slice(0, 64));
2907
+ return verify(_signature, message, publicKey);
2908
+ }
2909
+ catch {
2910
+ return false;
2911
+ }
2912
+ }
2913
+
2914
+ export { Point as P, getPublicKey$1 as a, compressPublicKey as b, commonjsGlobal as c, bytesToHex$1 as d, utf8ToBytes as e, bytesToUtf8 as f, getAugmentedNamespace as g, hexToBytes$1 as h, verifySignature as i, getPublicKey as j, keccak256 as k, sign as l, nodeCrypto as n, randomBytes as r, sign$1 as s, utils as u, verify as v };