@zoralabs/protocol-deployments 0.6.3 → 0.6.4-PRE.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,99 +1,41 @@
1
1
  import {
2
- __export
3
- } from "./chunk-PR4QN5HX.js";
2
+ Hash,
3
+ aInRange,
4
+ abool,
5
+ abytes,
6
+ abytes2,
7
+ aexists,
8
+ ahash,
9
+ anumber,
10
+ aoutput,
11
+ bitLen,
12
+ bitMask,
13
+ bytesToHex,
14
+ bytesToNumberBE,
15
+ bytesToNumberLE,
16
+ clean,
17
+ concatBytes,
18
+ concatBytes2,
19
+ createHasher,
20
+ createHmacDrbg,
21
+ createView,
22
+ ensureBytes,
23
+ hexToBytes,
24
+ inRange,
25
+ isBytes,
26
+ memoized,
27
+ numberToBytesBE,
28
+ numberToBytesLE,
29
+ numberToHexUnpadded,
30
+ randomBytes,
31
+ rotr,
32
+ toBytes,
33
+ utf8ToBytes,
34
+ validateObject
35
+ } from "./chunk-BYTNVMX7.js";
36
+ import "./chunk-PR4QN5HX.js";
4
37
 
5
- // ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_assert.js
6
- function anumber(n) {
7
- if (!Number.isSafeInteger(n) || n < 0)
8
- throw new Error("positive integer expected, got " + n);
9
- }
10
- function isBytes(a) {
11
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
12
- }
13
- function abytes(b, ...lengths) {
14
- if (!isBytes(b))
15
- throw new Error("Uint8Array expected");
16
- if (lengths.length > 0 && !lengths.includes(b.length))
17
- throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
18
- }
19
- function ahash(h) {
20
- if (typeof h !== "function" || typeof h.create !== "function")
21
- throw new Error("Hash should be wrapped by utils.wrapConstructor");
22
- anumber(h.outputLen);
23
- anumber(h.blockLen);
24
- }
25
- function aexists(instance, checkFinished = true) {
26
- if (instance.destroyed)
27
- throw new Error("Hash instance has been destroyed");
28
- if (checkFinished && instance.finished)
29
- throw new Error("Hash#digest() has already been called");
30
- }
31
- function aoutput(out, instance) {
32
- abytes(out);
33
- const min = instance.outputLen;
34
- if (out.length < min) {
35
- throw new Error("digestInto() expects output buffer of length at least " + min);
36
- }
37
- }
38
-
39
- // ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/cryptoNode.js
40
- import * as nc from "node:crypto";
41
- var crypto = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : nc && typeof nc === "object" && "randomBytes" in nc ? nc : void 0;
42
-
43
- // ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/utils.js
44
- var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
45
- var rotr = (word, shift) => word << 32 - shift | word >>> shift;
46
- function utf8ToBytes(str) {
47
- if (typeof str !== "string")
48
- throw new Error("utf8ToBytes expected string, got " + typeof str);
49
- return new Uint8Array(new TextEncoder().encode(str));
50
- }
51
- function toBytes(data) {
52
- if (typeof data === "string")
53
- data = utf8ToBytes(data);
54
- abytes(data);
55
- return data;
56
- }
57
- function concatBytes(...arrays) {
58
- let sum = 0;
59
- for (let i = 0; i < arrays.length; i++) {
60
- const a = arrays[i];
61
- abytes(a);
62
- sum += a.length;
63
- }
64
- const res = new Uint8Array(sum);
65
- for (let i = 0, pad = 0; i < arrays.length; i++) {
66
- const a = arrays[i];
67
- res.set(a, pad);
68
- pad += a.length;
69
- }
70
- return res;
71
- }
72
- var Hash = class {
73
- // Safe version that clones internal state
74
- clone() {
75
- return this._cloneInto();
76
- }
77
- };
78
- function wrapConstructor(hashCons) {
79
- const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
80
- const tmp = hashCons();
81
- hashC.outputLen = tmp.outputLen;
82
- hashC.blockLen = tmp.blockLen;
83
- hashC.create = () => hashCons();
84
- return hashC;
85
- }
86
- function randomBytes(bytesLength = 32) {
87
- if (crypto && typeof crypto.getRandomValues === "function") {
88
- return crypto.getRandomValues(new Uint8Array(bytesLength));
89
- }
90
- if (crypto && typeof crypto.randomBytes === "function") {
91
- return crypto.randomBytes(bytesLength);
92
- }
93
- throw new Error("crypto.getRandomValues must be defined");
94
- }
95
-
96
- // ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_md.js
38
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_md.js
97
39
  function setBigUint64(view, byteOffset, value, isLE) {
98
40
  if (typeof view.setBigUint64 === "function")
99
41
  return view.setBigUint64(byteOffset, value, isLE);
@@ -106,26 +48,31 @@ function setBigUint64(view, byteOffset, value, isLE) {
106
48
  view.setUint32(byteOffset + h, wh, isLE);
107
49
  view.setUint32(byteOffset + l, wl, isLE);
108
50
  }
109
- var Chi = (a, b, c) => a & b ^ ~a & c;
110
- var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
51
+ function Chi(a, b, c) {
52
+ return a & b ^ ~a & c;
53
+ }
54
+ function Maj(a, b, c) {
55
+ return a & b ^ a & c ^ b & c;
56
+ }
111
57
  var HashMD = class extends Hash {
112
58
  constructor(blockLen, outputLen, padOffset, isLE) {
113
59
  super();
114
- this.blockLen = blockLen;
115
- this.outputLen = outputLen;
116
- this.padOffset = padOffset;
117
- this.isLE = isLE;
118
60
  this.finished = false;
119
61
  this.length = 0;
120
62
  this.pos = 0;
121
63
  this.destroyed = false;
64
+ this.blockLen = blockLen;
65
+ this.outputLen = outputLen;
66
+ this.padOffset = padOffset;
67
+ this.isLE = isLE;
122
68
  this.buffer = new Uint8Array(blockLen);
123
69
  this.view = createView(this.buffer);
124
70
  }
125
71
  update(data) {
126
72
  aexists(this);
127
- const { view, buffer, blockLen } = this;
128
73
  data = toBytes(data);
74
+ abytes(data);
75
+ const { view, buffer, blockLen } = this;
129
76
  const len = data.length;
130
77
  for (let pos = 0; pos < len; ) {
131
78
  const take = Math.min(blockLen - this.pos, len - pos);
@@ -154,7 +101,7 @@ var HashMD = class extends Hash {
154
101
  const { buffer, view, blockLen, isLE } = this;
155
102
  let { pos } = this;
156
103
  buffer[pos++] = 128;
157
- this.buffer.subarray(pos).fill(0);
104
+ clean(this.buffer.subarray(pos));
158
105
  if (this.padOffset > blockLen - pos) {
159
106
  this.process(view, 0);
160
107
  pos = 0;
@@ -185,18 +132,31 @@ var HashMD = class extends Hash {
185
132
  to || (to = new this.constructor());
186
133
  to.set(...this.get());
187
134
  const { blockLen, buffer, length, finished, destroyed, pos } = this;
135
+ to.destroyed = destroyed;
136
+ to.finished = finished;
188
137
  to.length = length;
189
138
  to.pos = pos;
190
- to.finished = finished;
191
- to.destroyed = destroyed;
192
139
  if (length % blockLen)
193
140
  to.buffer.set(buffer);
194
141
  return to;
195
142
  }
143
+ clone() {
144
+ return this._cloneInto();
145
+ }
196
146
  };
147
+ var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
148
+ 1779033703,
149
+ 3144134277,
150
+ 1013904242,
151
+ 2773480762,
152
+ 1359893119,
153
+ 2600822924,
154
+ 528734635,
155
+ 1541459225
156
+ ]);
197
157
 
198
- // ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/sha256.js
199
- var SHA256_K = /* @__PURE__ */ new Uint32Array([
158
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/sha2.js
159
+ var SHA256_K = /* @__PURE__ */ Uint32Array.from([
200
160
  1116352408,
201
161
  1899447441,
202
162
  3049323471,
@@ -262,20 +222,10 @@ var SHA256_K = /* @__PURE__ */ new Uint32Array([
262
222
  3204031479,
263
223
  3329325298
264
224
  ]);
265
- var SHA256_IV = /* @__PURE__ */ new Uint32Array([
266
- 1779033703,
267
- 3144134277,
268
- 1013904242,
269
- 2773480762,
270
- 1359893119,
271
- 2600822924,
272
- 528734635,
273
- 1541459225
274
- ]);
275
225
  var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
276
226
  var SHA256 = class extends HashMD {
277
- constructor() {
278
- super(64, 32, 8, false);
227
+ constructor(outputLen = 32) {
228
+ super(64, outputLen, 8, false);
279
229
  this.A = SHA256_IV[0] | 0;
280
230
  this.B = SHA256_IV[1] | 0;
281
231
  this.C = SHA256_IV[2] | 0;
@@ -336,16 +286,16 @@ var SHA256 = class extends HashMD {
336
286
  this.set(A, B, C, D, E, F, G, H);
337
287
  }
338
288
  roundClean() {
339
- SHA256_W.fill(0);
289
+ clean(SHA256_W);
340
290
  }
341
291
  destroy() {
342
292
  this.set(0, 0, 0, 0, 0, 0, 0, 0);
343
- this.buffer.fill(0);
293
+ clean(this.buffer);
344
294
  }
345
295
  };
346
- var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
296
+ var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
347
297
 
348
- // ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/hmac.js
298
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/hmac.js
349
299
  var HMAC = class extends Hash {
350
300
  constructor(hash, _key) {
351
301
  super();
@@ -368,7 +318,7 @@ var HMAC = class extends Hash {
368
318
  for (let i = 0; i < pad.length; i++)
369
319
  pad[i] ^= 54 ^ 92;
370
320
  this.oHash.update(pad);
371
- pad.fill(0);
321
+ clean(pad);
372
322
  }
373
323
  update(buf) {
374
324
  aexists(this);
@@ -401,6 +351,9 @@ var HMAC = class extends Hash {
401
351
  to.iHash = iHash._cloneInto(to.iHash);
402
352
  return to;
403
353
  }
354
+ clone() {
355
+ return this._cloneInto();
356
+ }
404
357
  destroy() {
405
358
  this.destroyed = true;
406
359
  this.oHash.destroy();
@@ -410,321 +363,35 @@ var HMAC = class extends Hash {
410
363
  var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
411
364
  hmac.create = (hash, key) => new HMAC(hash, key);
412
365
 
413
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/utils.js
414
- var utils_exports = {};
415
- __export(utils_exports, {
416
- aInRange: () => aInRange,
417
- abool: () => abool,
418
- abytes: () => abytes2,
419
- bitGet: () => bitGet,
420
- bitLen: () => bitLen,
421
- bitMask: () => bitMask,
422
- bitSet: () => bitSet,
423
- bytesToHex: () => bytesToHex,
424
- bytesToNumberBE: () => bytesToNumberBE,
425
- bytesToNumberLE: () => bytesToNumberLE,
426
- concatBytes: () => concatBytes2,
427
- createHmacDrbg: () => createHmacDrbg,
428
- ensureBytes: () => ensureBytes,
429
- equalBytes: () => equalBytes,
430
- hexToBytes: () => hexToBytes,
431
- hexToNumber: () => hexToNumber,
432
- inRange: () => inRange,
433
- isBytes: () => isBytes2,
434
- memoized: () => memoized,
435
- notImplemented: () => notImplemented,
436
- numberToBytesBE: () => numberToBytesBE,
437
- numberToBytesLE: () => numberToBytesLE,
438
- numberToHexUnpadded: () => numberToHexUnpadded,
439
- numberToVarBytesBE: () => numberToVarBytesBE,
440
- utf8ToBytes: () => utf8ToBytes2,
441
- validateObject: () => validateObject
442
- });
443
- var _0n = /* @__PURE__ */ BigInt(0);
444
- var _1n = /* @__PURE__ */ BigInt(1);
366
+ // ../../node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/modular.js
367
+ var _0n = BigInt(0);
368
+ var _1n = BigInt(1);
445
369
  var _2n = /* @__PURE__ */ BigInt(2);
446
- function isBytes2(a) {
447
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
448
- }
449
- function abytes2(item) {
450
- if (!isBytes2(item))
451
- throw new Error("Uint8Array expected");
452
- }
453
- function abool(title, value) {
454
- if (typeof value !== "boolean")
455
- throw new Error(title + " boolean expected, got " + value);
456
- }
457
- var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
458
- function bytesToHex(bytes) {
459
- abytes2(bytes);
460
- let hex = "";
461
- for (let i = 0; i < bytes.length; i++) {
462
- hex += hexes[bytes[i]];
463
- }
464
- return hex;
465
- }
466
- function numberToHexUnpadded(num2) {
467
- const hex = num2.toString(16);
468
- return hex.length & 1 ? "0" + hex : hex;
469
- }
470
- function hexToNumber(hex) {
471
- if (typeof hex !== "string")
472
- throw new Error("hex string expected, got " + typeof hex);
473
- return hex === "" ? _0n : BigInt("0x" + hex);
474
- }
475
- var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
476
- function asciiToBase16(ch) {
477
- if (ch >= asciis._0 && ch <= asciis._9)
478
- return ch - asciis._0;
479
- if (ch >= asciis.A && ch <= asciis.F)
480
- return ch - (asciis.A - 10);
481
- if (ch >= asciis.a && ch <= asciis.f)
482
- return ch - (asciis.a - 10);
483
- return;
484
- }
485
- function hexToBytes(hex) {
486
- if (typeof hex !== "string")
487
- throw new Error("hex string expected, got " + typeof hex);
488
- const hl = hex.length;
489
- const al = hl / 2;
490
- if (hl % 2)
491
- throw new Error("hex string expected, got unpadded hex of length " + hl);
492
- const array = new Uint8Array(al);
493
- for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
494
- const n1 = asciiToBase16(hex.charCodeAt(hi));
495
- const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
496
- if (n1 === void 0 || n2 === void 0) {
497
- const char = hex[hi] + hex[hi + 1];
498
- throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
499
- }
500
- array[ai] = n1 * 16 + n2;
501
- }
502
- return array;
503
- }
504
- function bytesToNumberBE(bytes) {
505
- return hexToNumber(bytesToHex(bytes));
506
- }
507
- function bytesToNumberLE(bytes) {
508
- abytes2(bytes);
509
- return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
510
- }
511
- function numberToBytesBE(n, len) {
512
- return hexToBytes(n.toString(16).padStart(len * 2, "0"));
513
- }
514
- function numberToBytesLE(n, len) {
515
- return numberToBytesBE(n, len).reverse();
516
- }
517
- function numberToVarBytesBE(n) {
518
- return hexToBytes(numberToHexUnpadded(n));
519
- }
520
- function ensureBytes(title, hex, expectedLength) {
521
- let res;
522
- if (typeof hex === "string") {
523
- try {
524
- res = hexToBytes(hex);
525
- } catch (e) {
526
- throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
527
- }
528
- } else if (isBytes2(hex)) {
529
- res = Uint8Array.from(hex);
530
- } else {
531
- throw new Error(title + " must be hex string or Uint8Array");
532
- }
533
- const len = res.length;
534
- if (typeof expectedLength === "number" && len !== expectedLength)
535
- throw new Error(title + " of length " + expectedLength + " expected, got " + len);
536
- return res;
537
- }
538
- function concatBytes2(...arrays) {
539
- let sum = 0;
540
- for (let i = 0; i < arrays.length; i++) {
541
- const a = arrays[i];
542
- abytes2(a);
543
- sum += a.length;
544
- }
545
- const res = new Uint8Array(sum);
546
- for (let i = 0, pad = 0; i < arrays.length; i++) {
547
- const a = arrays[i];
548
- res.set(a, pad);
549
- pad += a.length;
550
- }
551
- return res;
552
- }
553
- function equalBytes(a, b) {
554
- if (a.length !== b.length)
555
- return false;
556
- let diff = 0;
557
- for (let i = 0; i < a.length; i++)
558
- diff |= a[i] ^ b[i];
559
- return diff === 0;
560
- }
561
- function utf8ToBytes2(str) {
562
- if (typeof str !== "string")
563
- throw new Error("string expected");
564
- return new Uint8Array(new TextEncoder().encode(str));
565
- }
566
- var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
567
- function inRange(n, min, max) {
568
- return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
569
- }
570
- function aInRange(title, n, min, max) {
571
- if (!inRange(n, min, max))
572
- throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
573
- }
574
- function bitLen(n) {
575
- let len;
576
- for (len = 0; n > _0n; n >>= _1n, len += 1)
577
- ;
578
- return len;
579
- }
580
- function bitGet(n, pos) {
581
- return n >> BigInt(pos) & _1n;
582
- }
583
- function bitSet(n, pos, value) {
584
- return n | (value ? _1n : _0n) << BigInt(pos);
585
- }
586
- var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
587
- var u8n = (data) => new Uint8Array(data);
588
- var u8fr = (arr) => Uint8Array.from(arr);
589
- function createHmacDrbg(hashLen, qByteLen, hmacFn) {
590
- if (typeof hashLen !== "number" || hashLen < 2)
591
- throw new Error("hashLen must be a number");
592
- if (typeof qByteLen !== "number" || qByteLen < 2)
593
- throw new Error("qByteLen must be a number");
594
- if (typeof hmacFn !== "function")
595
- throw new Error("hmacFn must be a function");
596
- let v = u8n(hashLen);
597
- let k = u8n(hashLen);
598
- let i = 0;
599
- const reset = () => {
600
- v.fill(1);
601
- k.fill(0);
602
- i = 0;
603
- };
604
- const h = (...b) => hmacFn(k, v, ...b);
605
- const reseed = (seed = u8n()) => {
606
- k = h(u8fr([0]), seed);
607
- v = h();
608
- if (seed.length === 0)
609
- return;
610
- k = h(u8fr([1]), seed);
611
- v = h();
612
- };
613
- const gen = () => {
614
- if (i++ >= 1e3)
615
- throw new Error("drbg: tried 1000 values");
616
- let len = 0;
617
- const out = [];
618
- while (len < qByteLen) {
619
- v = h();
620
- const sl = v.slice();
621
- out.push(sl);
622
- len += v.length;
623
- }
624
- return concatBytes2(...out);
625
- };
626
- const genUntil = (seed, pred) => {
627
- reset();
628
- reseed(seed);
629
- let res = void 0;
630
- while (!(res = pred(gen())))
631
- reseed();
632
- reset();
633
- return res;
634
- };
635
- return genUntil;
636
- }
637
- var validatorFns = {
638
- bigint: (val) => typeof val === "bigint",
639
- function: (val) => typeof val === "function",
640
- boolean: (val) => typeof val === "boolean",
641
- string: (val) => typeof val === "string",
642
- stringOrUint8Array: (val) => typeof val === "string" || isBytes2(val),
643
- isSafeInteger: (val) => Number.isSafeInteger(val),
644
- array: (val) => Array.isArray(val),
645
- field: (val, object) => object.Fp.isValid(val),
646
- hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
647
- };
648
- function validateObject(object, validators, optValidators = {}) {
649
- const checkField = (fieldName, type, isOptional) => {
650
- const checkVal = validatorFns[type];
651
- if (typeof checkVal !== "function")
652
- throw new Error("invalid validator function");
653
- const val = object[fieldName];
654
- if (isOptional && val === void 0)
655
- return;
656
- if (!checkVal(val, object)) {
657
- throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val);
658
- }
659
- };
660
- for (const [fieldName, type] of Object.entries(validators))
661
- checkField(fieldName, type, false);
662
- for (const [fieldName, type] of Object.entries(optValidators))
663
- checkField(fieldName, type, true);
664
- return object;
665
- }
666
- var notImplemented = () => {
667
- throw new Error("not implemented");
668
- };
669
- function memoized(fn) {
670
- const map = /* @__PURE__ */ new WeakMap();
671
- return (arg, ...args) => {
672
- const val = map.get(arg);
673
- if (val !== void 0)
674
- return val;
675
- const computed = fn(arg, ...args);
676
- map.set(arg, computed);
677
- return computed;
678
- };
679
- }
680
-
681
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/modular.js
682
- var _0n2 = BigInt(0);
683
- var _1n2 = BigInt(1);
684
- var _2n2 = /* @__PURE__ */ BigInt(2);
685
370
  var _3n = /* @__PURE__ */ BigInt(3);
686
371
  var _4n = /* @__PURE__ */ BigInt(4);
687
372
  var _5n = /* @__PURE__ */ BigInt(5);
688
373
  var _8n = /* @__PURE__ */ BigInt(8);
689
- var _9n = /* @__PURE__ */ BigInt(9);
690
- var _16n = /* @__PURE__ */ BigInt(16);
691
374
  function mod(a, b) {
692
375
  const result = a % b;
693
- return result >= _0n2 ? result : b + result;
694
- }
695
- function pow(num2, power, modulo) {
696
- if (power < _0n2)
697
- throw new Error("invalid exponent, negatives unsupported");
698
- if (modulo <= _0n2)
699
- throw new Error("invalid modulus");
700
- if (modulo === _1n2)
701
- return _0n2;
702
- let res = _1n2;
703
- while (power > _0n2) {
704
- if (power & _1n2)
705
- res = res * num2 % modulo;
706
- num2 = num2 * num2 % modulo;
707
- power >>= _1n2;
708
- }
709
- return res;
376
+ return result >= _0n ? result : b + result;
710
377
  }
711
378
  function pow2(x, power, modulo) {
712
379
  let res = x;
713
- while (power-- > _0n2) {
380
+ while (power-- > _0n) {
714
381
  res *= res;
715
382
  res %= modulo;
716
383
  }
717
384
  return res;
718
385
  }
719
386
  function invert(number, modulo) {
720
- if (number === _0n2)
387
+ if (number === _0n)
721
388
  throw new Error("invert: expected non-zero number");
722
- if (modulo <= _0n2)
389
+ if (modulo <= _0n)
723
390
  throw new Error("invert: expected positive modulus, got " + modulo);
724
391
  let a = mod(number, modulo);
725
392
  let b = modulo;
726
- let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
727
- while (a !== _0n2) {
393
+ let x = _0n, y = _1n, u = _1n, v = _0n;
394
+ while (a !== _0n) {
728
395
  const q = b / a;
729
396
  const r = b % a;
730
397
  const m = x - u * q;
@@ -732,79 +399,82 @@ function invert(number, modulo) {
732
399
  b = a, a = r, x = u, y = v, u = m, v = n;
733
400
  }
734
401
  const gcd = b;
735
- if (gcd !== _1n2)
402
+ if (gcd !== _1n)
736
403
  throw new Error("invert: does not exist");
737
404
  return mod(x, modulo);
738
405
  }
406
+ function sqrt3mod4(Fp, n) {
407
+ const p1div4 = (Fp.ORDER + _1n) / _4n;
408
+ const root = Fp.pow(n, p1div4);
409
+ if (!Fp.eql(Fp.sqr(root), n))
410
+ throw new Error("Cannot find square root");
411
+ return root;
412
+ }
413
+ function sqrt5mod8(Fp, n) {
414
+ const p5div8 = (Fp.ORDER - _5n) / _8n;
415
+ const n2 = Fp.mul(n, _2n);
416
+ const v = Fp.pow(n2, p5div8);
417
+ const nv = Fp.mul(n, v);
418
+ const i = Fp.mul(Fp.mul(nv, _2n), v);
419
+ const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
420
+ if (!Fp.eql(Fp.sqr(root), n))
421
+ throw new Error("Cannot find square root");
422
+ return root;
423
+ }
739
424
  function tonelliShanks(P) {
740
- const legendreC = (P - _1n2) / _2n2;
741
- let Q, S, Z;
742
- for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
743
- ;
744
- for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++) {
745
- if (Z > 1e3)
746
- throw new Error("Cannot find square root: likely non-prime P");
747
- }
748
- if (S === 1) {
749
- const p1div4 = (P + _1n2) / _4n;
750
- return function tonelliFast(Fp, n) {
751
- const root = Fp.pow(n, p1div4);
752
- if (!Fp.eql(Fp.sqr(root), n))
753
- throw new Error("Cannot find square root");
754
- return root;
755
- };
756
- }
757
- const Q1div2 = (Q + _1n2) / _2n2;
425
+ if (P < BigInt(3))
426
+ throw new Error("sqrt is not defined for small field");
427
+ let Q = P - _1n;
428
+ let S = 0;
429
+ while (Q % _2n === _0n) {
430
+ Q /= _2n;
431
+ S++;
432
+ }
433
+ let Z = _2n;
434
+ const _Fp = Field(P);
435
+ while (FpLegendre(_Fp, Z) === 1) {
436
+ if (Z++ > 1e3)
437
+ throw new Error("Cannot find square root: probably non-prime P");
438
+ }
439
+ if (S === 1)
440
+ return sqrt3mod4;
441
+ let cc = _Fp.pow(Z, Q);
442
+ const Q1div2 = (Q + _1n) / _2n;
758
443
  return function tonelliSlow(Fp, n) {
759
- if (Fp.pow(n, legendreC) === Fp.neg(Fp.ONE))
444
+ if (Fp.is0(n))
445
+ return n;
446
+ if (FpLegendre(Fp, n) !== 1)
760
447
  throw new Error("Cannot find square root");
761
- let r = S;
762
- let g = Fp.pow(Fp.mul(Fp.ONE, Z), Q);
763
- let x = Fp.pow(n, Q1div2);
764
- let b = Fp.pow(n, Q);
765
- while (!Fp.eql(b, Fp.ONE)) {
766
- if (Fp.eql(b, Fp.ZERO))
448
+ let M = S;
449
+ let c = Fp.mul(Fp.ONE, cc);
450
+ let t = Fp.pow(n, Q);
451
+ let R = Fp.pow(n, Q1div2);
452
+ while (!Fp.eql(t, Fp.ONE)) {
453
+ if (Fp.is0(t))
767
454
  return Fp.ZERO;
768
- let m = 1;
769
- for (let t2 = Fp.sqr(b); m < r; m++) {
770
- if (Fp.eql(t2, Fp.ONE))
771
- break;
772
- t2 = Fp.sqr(t2);
455
+ let i = 1;
456
+ let t_tmp = Fp.sqr(t);
457
+ while (!Fp.eql(t_tmp, Fp.ONE)) {
458
+ i++;
459
+ t_tmp = Fp.sqr(t_tmp);
460
+ if (i === M)
461
+ throw new Error("Cannot find square root");
773
462
  }
774
- const ge = Fp.pow(g, _1n2 << BigInt(r - m - 1));
775
- g = Fp.sqr(ge);
776
- x = Fp.mul(x, ge);
777
- b = Fp.mul(b, g);
778
- r = m;
779
- }
780
- return x;
463
+ const exponent = _1n << BigInt(M - i - 1);
464
+ const b = Fp.pow(c, exponent);
465
+ M = i;
466
+ c = Fp.sqr(b);
467
+ t = Fp.mul(t, c);
468
+ R = Fp.mul(R, b);
469
+ }
470
+ return R;
781
471
  };
782
472
  }
783
473
  function FpSqrt(P) {
784
- if (P % _4n === _3n) {
785
- const p1div4 = (P + _1n2) / _4n;
786
- return function sqrt3mod4(Fp, n) {
787
- const root = Fp.pow(n, p1div4);
788
- if (!Fp.eql(Fp.sqr(root), n))
789
- throw new Error("Cannot find square root");
790
- return root;
791
- };
792
- }
793
- if (P % _8n === _5n) {
794
- const c1 = (P - _5n) / _8n;
795
- return function sqrt5mod8(Fp, n) {
796
- const n2 = Fp.mul(n, _2n2);
797
- const v = Fp.pow(n2, c1);
798
- const nv = Fp.mul(n, v);
799
- const i = Fp.mul(Fp.mul(nv, _2n2), v);
800
- const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
801
- if (!Fp.eql(Fp.sqr(root), n))
802
- throw new Error("Cannot find square root");
803
- return root;
804
- };
805
- }
806
- if (P % _16n === _9n) {
807
- }
474
+ if (P % _4n === _3n)
475
+ return sqrt3mod4;
476
+ if (P % _8n === _5n)
477
+ return sqrt5mod8;
808
478
  return tonelliShanks(P);
809
479
  }
810
480
  var FIELD_FIELDS = [
@@ -839,47 +509,59 @@ function validateField(field) {
839
509
  }, initial);
840
510
  return validateObject(field, opts);
841
511
  }
842
- function FpPow(f, num2, power) {
843
- if (power < _0n2)
512
+ function FpPow(Fp, num2, power) {
513
+ if (power < _0n)
844
514
  throw new Error("invalid exponent, negatives unsupported");
845
- if (power === _0n2)
846
- return f.ONE;
847
- if (power === _1n2)
515
+ if (power === _0n)
516
+ return Fp.ONE;
517
+ if (power === _1n)
848
518
  return num2;
849
- let p = f.ONE;
519
+ let p = Fp.ONE;
850
520
  let d = num2;
851
- while (power > _0n2) {
852
- if (power & _1n2)
853
- p = f.mul(p, d);
854
- d = f.sqr(d);
855
- power >>= _1n2;
521
+ while (power > _0n) {
522
+ if (power & _1n)
523
+ p = Fp.mul(p, d);
524
+ d = Fp.sqr(d);
525
+ power >>= _1n;
856
526
  }
857
527
  return p;
858
528
  }
859
- function FpInvertBatch(f, nums) {
860
- const tmp = new Array(nums.length);
861
- const lastMultiplied = nums.reduce((acc, num2, i) => {
862
- if (f.is0(num2))
529
+ function FpInvertBatch(Fp, nums, passZero = false) {
530
+ const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
531
+ const multipliedAcc = nums.reduce((acc, num2, i) => {
532
+ if (Fp.is0(num2))
863
533
  return acc;
864
- tmp[i] = acc;
865
- return f.mul(acc, num2);
866
- }, f.ONE);
867
- const inverted = f.inv(lastMultiplied);
534
+ inverted[i] = acc;
535
+ return Fp.mul(acc, num2);
536
+ }, Fp.ONE);
537
+ const invertedAcc = Fp.inv(multipliedAcc);
868
538
  nums.reduceRight((acc, num2, i) => {
869
- if (f.is0(num2))
539
+ if (Fp.is0(num2))
870
540
  return acc;
871
- tmp[i] = f.mul(acc, tmp[i]);
872
- return f.mul(acc, num2);
873
- }, inverted);
874
- return tmp;
541
+ inverted[i] = Fp.mul(acc, inverted[i]);
542
+ return Fp.mul(acc, num2);
543
+ }, invertedAcc);
544
+ return inverted;
545
+ }
546
+ function FpLegendre(Fp, n) {
547
+ const p1mod2 = (Fp.ORDER - _1n) / _2n;
548
+ const powered = Fp.pow(n, p1mod2);
549
+ const yes = Fp.eql(powered, Fp.ONE);
550
+ const zero = Fp.eql(powered, Fp.ZERO);
551
+ const no = Fp.eql(powered, Fp.neg(Fp.ONE));
552
+ if (!yes && !zero && !no)
553
+ throw new Error("invalid Legendre symbol result");
554
+ return yes ? 1 : zero ? 0 : -1;
875
555
  }
876
556
  function nLength(n, nBitLength) {
557
+ if (nBitLength !== void 0)
558
+ anumber(nBitLength);
877
559
  const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
878
560
  const nByteLength = Math.ceil(_nBitLength / 8);
879
561
  return { nBitLength: _nBitLength, nByteLength };
880
562
  }
881
563
  function Field(ORDER, bitLen2, isLE = false, redef = {}) {
882
- if (ORDER <= _0n2)
564
+ if (ORDER <= _0n)
883
565
  throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
884
566
  const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
885
567
  if (BYTES > 2048)
@@ -887,19 +569,20 @@ function Field(ORDER, bitLen2, isLE = false, redef = {}) {
887
569
  let sqrtP;
888
570
  const f = Object.freeze({
889
571
  ORDER,
572
+ isLE,
890
573
  BITS,
891
574
  BYTES,
892
575
  MASK: bitMask(BITS),
893
- ZERO: _0n2,
894
- ONE: _1n2,
576
+ ZERO: _0n,
577
+ ONE: _1n,
895
578
  create: (num2) => mod(num2, ORDER),
896
579
  isValid: (num2) => {
897
580
  if (typeof num2 !== "bigint")
898
581
  throw new Error("invalid field element: expected bigint, got " + typeof num2);
899
- return _0n2 <= num2 && num2 < ORDER;
582
+ return _0n <= num2 && num2 < ORDER;
900
583
  },
901
- is0: (num2) => num2 === _0n2,
902
- isOdd: (num2) => (num2 & _1n2) === _1n2,
584
+ is0: (num2) => num2 === _0n,
585
+ isOdd: (num2) => (num2 & _1n) === _1n,
903
586
  neg: (num2) => mod(-num2, ORDER),
904
587
  eql: (lhs, rhs) => lhs === rhs,
905
588
  sqr: (num2) => mod(num2 * num2, ORDER),
@@ -919,16 +602,17 @@ function Field(ORDER, bitLen2, isLE = false, redef = {}) {
919
602
  sqrtP = FpSqrt(ORDER);
920
603
  return sqrtP(f, n);
921
604
  }),
922
- invertBatch: (lst) => FpInvertBatch(f, lst),
923
- // TODO: do we really need constant cmov?
924
- // We don't have const-time bigints anyway, so probably will be not very useful
925
- cmov: (a, b, c) => c ? b : a,
926
605
  toBytes: (num2) => isLE ? numberToBytesLE(num2, BYTES) : numberToBytesBE(num2, BYTES),
927
606
  fromBytes: (bytes) => {
928
607
  if (bytes.length !== BYTES)
929
608
  throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
930
609
  return isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
931
- }
610
+ },
611
+ // TODO: we don't need it here, move out to separate fn
612
+ invertBatch: (lst) => FpInvertBatch(f, lst),
613
+ // We can't move this out because Fp6, Fp12 implement it
614
+ // and it's unclear what to return in there.
615
+ cmov: (a, b, c) => c ? b : a
932
616
  });
933
617
  return Object.freeze(f);
934
618
  }
@@ -948,14 +632,14 @@ function mapHashToField(key, fieldOrder, isLE = false) {
948
632
  const minLen = getMinHashLength(fieldOrder);
949
633
  if (len < 16 || len < minLen || len > 1024)
950
634
  throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
951
- const num2 = isLE ? bytesToNumberBE(key) : bytesToNumberLE(key);
952
- const reduced = mod(num2, fieldOrder - _1n2) + _1n2;
635
+ const num2 = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
636
+ const reduced = mod(num2, fieldOrder - _1n) + _1n;
953
637
  return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
954
638
  }
955
639
 
956
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/curve.js
957
- var _0n3 = BigInt(0);
958
- var _1n3 = BigInt(1);
640
+ // ../../node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/curve.js
641
+ var _0n2 = BigInt(0);
642
+ var _1n2 = BigInt(1);
959
643
  function constTimeNegate(condition, item) {
960
644
  const neg = item.negate();
961
645
  return condition ? neg : item;
@@ -964,11 +648,30 @@ function validateW(W, bits) {
964
648
  if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
965
649
  throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
966
650
  }
967
- function calcWOpts(W, bits) {
968
- validateW(W, bits);
969
- const windows = Math.ceil(bits / W) + 1;
651
+ function calcWOpts(W, scalarBits) {
652
+ validateW(W, scalarBits);
653
+ const windows = Math.ceil(scalarBits / W) + 1;
970
654
  const windowSize = 2 ** (W - 1);
971
- return { windows, windowSize };
655
+ const maxNumber = 2 ** W;
656
+ const mask = bitMask(W);
657
+ const shiftBy = BigInt(W);
658
+ return { windows, windowSize, mask, maxNumber, shiftBy };
659
+ }
660
+ function calcOffsets(n, window, wOpts) {
661
+ const { windowSize, mask, maxNumber, shiftBy } = wOpts;
662
+ let wbits = Number(n & mask);
663
+ let nextN = n >> shiftBy;
664
+ if (wbits > windowSize) {
665
+ wbits -= maxNumber;
666
+ nextN += _1n2;
667
+ }
668
+ const offsetStart = window * windowSize;
669
+ const offset = offsetStart + Math.abs(wbits) - 1;
670
+ const isZero = wbits === 0;
671
+ const isNeg = wbits < 0;
672
+ const isNegF = window % 2 !== 0;
673
+ const offsetF = offsetStart;
674
+ return { nextN, offset, isZero, isNeg, isNegF, offsetF };
972
675
  }
973
676
  function validateMSMPoints(points, c) {
974
677
  if (!Array.isArray(points))
@@ -1000,11 +703,11 @@ function wNAF(c, bits) {
1000
703
  // non-const time multiplication ladder
1001
704
  unsafeLadder(elm, n, p = c.ZERO) {
1002
705
  let d = elm;
1003
- while (n > _0n3) {
1004
- if (n & _1n3)
706
+ while (n > _0n2) {
707
+ if (n & _1n2)
1005
708
  p = p.add(d);
1006
709
  d = d.double();
1007
- n >>= _1n3;
710
+ n >>= _1n2;
1008
711
  }
1009
712
  return p;
1010
713
  },
@@ -1044,28 +747,16 @@ function wNAF(c, bits) {
1044
747
  * @returns real and fake (for const-time) points
1045
748
  */
1046
749
  wNAF(W, precomputes, n) {
1047
- const { windows, windowSize } = calcWOpts(W, bits);
1048
750
  let p = c.ZERO;
1049
751
  let f = c.BASE;
1050
- const mask = BigInt(2 ** W - 1);
1051
- const maxNumber = 2 ** W;
1052
- const shiftBy = BigInt(W);
1053
- for (let window = 0; window < windows; window++) {
1054
- const offset = window * windowSize;
1055
- let wbits = Number(n & mask);
1056
- n >>= shiftBy;
1057
- if (wbits > windowSize) {
1058
- wbits -= maxNumber;
1059
- n += _1n3;
1060
- }
1061
- const offset1 = offset;
1062
- const offset2 = offset + Math.abs(wbits) - 1;
1063
- const cond1 = window % 2 !== 0;
1064
- const cond2 = wbits < 0;
1065
- if (wbits === 0) {
1066
- f = f.add(constTimeNegate(cond1, precomputes[offset1]));
752
+ const wo = calcWOpts(W, bits);
753
+ for (let window = 0; window < wo.windows; window++) {
754
+ const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
755
+ n = nextN;
756
+ if (isZero) {
757
+ f = f.add(constTimeNegate(isNegF, precomputes[offsetF]));
1067
758
  } else {
1068
- p = p.add(constTimeNegate(cond2, precomputes[offset2]));
759
+ p = p.add(constTimeNegate(isNeg, precomputes[offset]));
1069
760
  }
1070
761
  }
1071
762
  return { p, f };
@@ -1079,26 +770,18 @@ function wNAF(c, bits) {
1079
770
  * @returns point
1080
771
  */
1081
772
  wNAFUnsafe(W, precomputes, n, acc = c.ZERO) {
1082
- const { windows, windowSize } = calcWOpts(W, bits);
1083
- const mask = BigInt(2 ** W - 1);
1084
- const maxNumber = 2 ** W;
1085
- const shiftBy = BigInt(W);
1086
- for (let window = 0; window < windows; window++) {
1087
- const offset = window * windowSize;
1088
- if (n === _0n3)
773
+ const wo = calcWOpts(W, bits);
774
+ for (let window = 0; window < wo.windows; window++) {
775
+ if (n === _0n2)
1089
776
  break;
1090
- let wbits = Number(n & mask);
1091
- n >>= shiftBy;
1092
- if (wbits > windowSize) {
1093
- wbits -= maxNumber;
1094
- n += _1n3;
1095
- }
1096
- if (wbits === 0)
777
+ const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
778
+ n = nextN;
779
+ if (isZero) {
1097
780
  continue;
1098
- let curr = precomputes[offset + Math.abs(wbits) - 1];
1099
- if (wbits < 0)
1100
- curr = curr.negate();
1101
- acc = acc.add(curr);
781
+ } else {
782
+ const item = precomputes[offset];
783
+ acc = acc.add(isNeg ? item.negate() : item);
784
+ }
1102
785
  }
1103
786
  return acc;
1104
787
  },
@@ -1134,20 +817,28 @@ function wNAF(c, bits) {
1134
817
  function pippenger(c, fieldN, points, scalars) {
1135
818
  validateMSMPoints(points, c);
1136
819
  validateMSMScalars(scalars, fieldN);
1137
- if (points.length !== scalars.length)
820
+ const plength = points.length;
821
+ const slength = scalars.length;
822
+ if (plength !== slength)
1138
823
  throw new Error("arrays of points and scalars must have equal length");
1139
824
  const zero = c.ZERO;
1140
- const wbits = bitLen(BigInt(points.length));
1141
- const windowSize = wbits > 12 ? wbits - 3 : wbits > 4 ? wbits - 2 : wbits ? 2 : 1;
1142
- const MASK = (1 << windowSize) - 1;
1143
- const buckets = new Array(MASK + 1).fill(zero);
825
+ const wbits = bitLen(BigInt(plength));
826
+ let windowSize = 1;
827
+ if (wbits > 12)
828
+ windowSize = wbits - 3;
829
+ else if (wbits > 4)
830
+ windowSize = wbits - 2;
831
+ else if (wbits > 0)
832
+ windowSize = 2;
833
+ const MASK = bitMask(windowSize);
834
+ const buckets = new Array(Number(MASK) + 1).fill(zero);
1144
835
  const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
1145
836
  let sum = zero;
1146
837
  for (let i = lastBits; i >= 0; i -= windowSize) {
1147
838
  buckets.fill(zero);
1148
- for (let j = 0; j < scalars.length; j++) {
839
+ for (let j = 0; j < slength; j++) {
1149
840
  const scalar = scalars[j];
1150
- const wbits2 = Number(scalar >> BigInt(i) & BigInt(MASK));
841
+ const wbits2 = Number(scalar >> BigInt(i) & MASK);
1151
842
  buckets[wbits2] = buckets[wbits2].add(points[j]);
1152
843
  }
1153
844
  let resI = zero;
@@ -1180,7 +871,7 @@ function validateBasic(curve) {
1180
871
  });
1181
872
  }
1182
873
 
1183
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/weierstrass.js
874
+ // ../../node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/weierstrass.js
1184
875
  function validateSigVerOpts(opts) {
1185
876
  if (opts.lowS !== void 0)
1186
877
  abool("lowS", opts.lowS);
@@ -1193,33 +884,33 @@ function validatePointOpts(curve) {
1193
884
  a: "field",
1194
885
  b: "field"
1195
886
  }, {
887
+ allowInfinityPoint: "boolean",
1196
888
  allowedPrivateKeyLengths: "array",
1197
- wrapPrivateKey: "boolean",
1198
- isTorsionFree: "function",
1199
889
  clearCofactor: "function",
1200
- allowInfinityPoint: "boolean",
1201
890
  fromBytes: "function",
1202
- toBytes: "function"
891
+ isTorsionFree: "function",
892
+ toBytes: "function",
893
+ wrapPrivateKey: "boolean"
1203
894
  });
1204
895
  const { endo, Fp, a } = opts;
1205
896
  if (endo) {
1206
897
  if (!Fp.eql(a, Fp.ZERO)) {
1207
- throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");
898
+ throw new Error("invalid endo: CURVE.a must be 0");
1208
899
  }
1209
900
  if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") {
1210
- throw new Error("invalid endomorphism, expected beta: bigint and splitScalar: function");
901
+ throw new Error('invalid endo: expected "beta": bigint and "splitScalar": function');
1211
902
  }
1212
903
  }
1213
904
  return Object.freeze({ ...opts });
1214
905
  }
1215
- var { bytesToNumberBE: b2n, hexToBytes: h2b } = utils_exports;
906
+ var DERErr = class extends Error {
907
+ constructor(m = "") {
908
+ super(m);
909
+ }
910
+ };
1216
911
  var DER = {
1217
912
  // asn.1 DER encoding utils
1218
- Err: class DERErr extends Error {
1219
- constructor(m = "") {
1220
- super(m);
1221
- }
1222
- },
913
+ Err: DERErr,
1223
914
  // Basic building block is TLV (Tag-Length-Value)
1224
915
  _tlv: {
1225
916
  encode: (tag, data) => {
@@ -1279,7 +970,7 @@ var DER = {
1279
970
  _int: {
1280
971
  encode(num2) {
1281
972
  const { Err: E } = DER;
1282
- if (num2 < _0n4)
973
+ if (num2 < _0n3)
1283
974
  throw new E("integer: negative integers are not allowed");
1284
975
  let hex = numberToHexUnpadded(num2);
1285
976
  if (Number.parseInt(hex[0], 16) & 8)
@@ -1294,13 +985,12 @@ var DER = {
1294
985
  throw new E("invalid signature integer: negative");
1295
986
  if (data[0] === 0 && !(data[1] & 128))
1296
987
  throw new E("invalid signature integer: unnecessary leading zero");
1297
- return b2n(data);
988
+ return bytesToNumberBE(data);
1298
989
  }
1299
990
  },
1300
991
  toSig(hex) {
1301
992
  const { Err: E, _int: int, _tlv: tlv } = DER;
1302
- const data = typeof hex === "string" ? h2b(hex) : hex;
1303
- abytes2(data);
993
+ const data = ensureBytes("signature", hex);
1304
994
  const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
1305
995
  if (seqLeftBytes.length)
1306
996
  throw new E("invalid signature: left bytes after parsing");
@@ -1318,9 +1008,12 @@ var DER = {
1318
1008
  return tlv.encode(48, seq);
1319
1009
  }
1320
1010
  };
1321
- var _0n4 = BigInt(0);
1322
- var _1n4 = BigInt(1);
1323
- var _2n3 = BigInt(2);
1011
+ function numToSizedHex(num2, size) {
1012
+ return bytesToHex(numberToBytesBE(num2, size));
1013
+ }
1014
+ var _0n3 = BigInt(0);
1015
+ var _1n3 = BigInt(1);
1016
+ var _2n2 = BigInt(2);
1324
1017
  var _3n2 = BigInt(3);
1325
1018
  var _4n2 = BigInt(4);
1326
1019
  function weierstrassPoints(opts) {
@@ -1343,15 +1036,24 @@ function weierstrassPoints(opts) {
1343
1036
  const x3 = Fp.mul(x2, x);
1344
1037
  return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
1345
1038
  }
1346
- if (!Fp.eql(Fp.sqr(CURVE.Gy), weierstrassEquation(CURVE.Gx)))
1347
- throw new Error("bad generator point: equation left != right");
1039
+ function isValidXY(x, y) {
1040
+ const left = Fp.sqr(y);
1041
+ const right = weierstrassEquation(x);
1042
+ return Fp.eql(left, right);
1043
+ }
1044
+ if (!isValidXY(CURVE.Gx, CURVE.Gy))
1045
+ throw new Error("bad curve params: generator point");
1046
+ const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n2), _4n2);
1047
+ const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
1048
+ if (Fp.is0(Fp.add(_4a3, _27b2)))
1049
+ throw new Error("bad curve params: a or b");
1348
1050
  function isWithinCurveOrder(num2) {
1349
- return inRange(num2, _1n4, CURVE.n);
1051
+ return inRange(num2, _1n3, CURVE.n);
1350
1052
  }
1351
1053
  function normPrivateKeyToScalar(key) {
1352
1054
  const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N } = CURVE;
1353
1055
  if (lengths && typeof key !== "bigint") {
1354
- if (isBytes2(key))
1056
+ if (isBytes(key))
1355
1057
  key = bytesToHex(key);
1356
1058
  if (typeof key !== "string" || !lengths.includes(key.length))
1357
1059
  throw new Error("invalid private key");
@@ -1365,10 +1067,10 @@ function weierstrassPoints(opts) {
1365
1067
  }
1366
1068
  if (wrapPrivateKey)
1367
1069
  num2 = mod(num2, N);
1368
- aInRange("private key", num2, _1n4, N);
1070
+ aInRange("private key", num2, _1n3, N);
1369
1071
  return num2;
1370
1072
  }
1371
- function assertPrjPoint(other) {
1073
+ function aprjpoint(other) {
1372
1074
  if (!(other instanceof Point2))
1373
1075
  throw new Error("ProjectivePoint expected");
1374
1076
  }
@@ -1397,9 +1099,7 @@ function weierstrassPoints(opts) {
1397
1099
  const { x, y } = p.toAffine();
1398
1100
  if (!Fp.isValid(x) || !Fp.isValid(y))
1399
1101
  throw new Error("bad point: x or y not FE");
1400
- const left = Fp.sqr(y);
1401
- const right = weierstrassEquation(x);
1402
- if (!Fp.eql(left, right))
1102
+ if (!isValidXY(x, y))
1403
1103
  throw new Error("bad point: equation left != right");
1404
1104
  if (!p.isTorsionFree())
1405
1105
  throw new Error("bad point: not in prime-order subgroup");
@@ -1407,15 +1107,15 @@ function weierstrassPoints(opts) {
1407
1107
  });
1408
1108
  class Point2 {
1409
1109
  constructor(px, py, pz) {
1410
- this.px = px;
1411
- this.py = py;
1412
- this.pz = pz;
1413
1110
  if (px == null || !Fp.isValid(px))
1414
1111
  throw new Error("x required");
1415
- if (py == null || !Fp.isValid(py))
1112
+ if (py == null || !Fp.isValid(py) || Fp.is0(py))
1416
1113
  throw new Error("y required");
1417
1114
  if (pz == null || !Fp.isValid(pz))
1418
1115
  throw new Error("z required");
1116
+ this.px = px;
1117
+ this.py = py;
1118
+ this.pz = pz;
1419
1119
  Object.freeze(this);
1420
1120
  }
1421
1121
  // Does not validate if the point is on-curve.
@@ -1444,7 +1144,7 @@ function weierstrassPoints(opts) {
1444
1144
  * Optimization: converts a list of projective points to a list of identical points with Z=1.
1445
1145
  */
1446
1146
  static normalizeZ(points) {
1447
- const toInv = Fp.invertBatch(points.map((p) => p.pz));
1147
+ const toInv = FpInvertBatch(Fp, points.map((p) => p.pz));
1448
1148
  return points.map((p, i) => p.toAffine(toInv[i])).map(Point2.fromAffine);
1449
1149
  }
1450
1150
  /**
@@ -1482,7 +1182,7 @@ function weierstrassPoints(opts) {
1482
1182
  * Compare one point to another.
1483
1183
  */
1484
1184
  equals(other) {
1485
- assertPrjPoint(other);
1185
+ aprjpoint(other);
1486
1186
  const { px: X1, py: Y1, pz: Z1 } = this;
1487
1187
  const { px: X2, py: Y2, pz: Z2 } = other;
1488
1188
  const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
@@ -1542,7 +1242,7 @@ function weierstrassPoints(opts) {
1542
1242
  // https://eprint.iacr.org/2015/1060, algorithm 1
1543
1243
  // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
1544
1244
  add(other) {
1545
- assertPrjPoint(other);
1245
+ aprjpoint(other);
1546
1246
  const { px: X1, py: Y1, pz: Z1 } = this;
1547
1247
  const { px: X2, py: Y2, pz: Z2 } = other;
1548
1248
  let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
@@ -1605,33 +1305,33 @@ function weierstrassPoints(opts) {
1605
1305
  * an exposed private key e.g. sig verification, which works over *public* keys.
1606
1306
  */
1607
1307
  multiplyUnsafe(sc) {
1608
- const { endo, n: N } = CURVE;
1609
- aInRange("scalar", sc, _0n4, N);
1308
+ const { endo: endo2, n: N } = CURVE;
1309
+ aInRange("scalar", sc, _0n3, N);
1610
1310
  const I = Point2.ZERO;
1611
- if (sc === _0n4)
1311
+ if (sc === _0n3)
1612
1312
  return I;
1613
- if (this.is0() || sc === _1n4)
1313
+ if (this.is0() || sc === _1n3)
1614
1314
  return this;
1615
- if (!endo || wnaf.hasPrecomputes(this))
1315
+ if (!endo2 || wnaf.hasPrecomputes(this))
1616
1316
  return wnaf.wNAFCachedUnsafe(this, sc, Point2.normalizeZ);
1617
- let { k1neg, k1, k2neg, k2 } = endo.splitScalar(sc);
1317
+ let { k1neg, k1, k2neg, k2 } = endo2.splitScalar(sc);
1618
1318
  let k1p = I;
1619
1319
  let k2p = I;
1620
1320
  let d = this;
1621
- while (k1 > _0n4 || k2 > _0n4) {
1622
- if (k1 & _1n4)
1321
+ while (k1 > _0n3 || k2 > _0n3) {
1322
+ if (k1 & _1n3)
1623
1323
  k1p = k1p.add(d);
1624
- if (k2 & _1n4)
1324
+ if (k2 & _1n3)
1625
1325
  k2p = k2p.add(d);
1626
1326
  d = d.double();
1627
- k1 >>= _1n4;
1628
- k2 >>= _1n4;
1327
+ k1 >>= _1n3;
1328
+ k2 >>= _1n3;
1629
1329
  }
1630
1330
  if (k1neg)
1631
1331
  k1p = k1p.negate();
1632
1332
  if (k2neg)
1633
1333
  k2p = k2p.negate();
1634
- k2p = new Point2(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
1334
+ k2p = new Point2(Fp.mul(k2p.px, endo2.beta), k2p.py, k2p.pz);
1635
1335
  return k1p.add(k2p);
1636
1336
  }
1637
1337
  /**
@@ -1644,16 +1344,16 @@ function weierstrassPoints(opts) {
1644
1344
  * @returns New point
1645
1345
  */
1646
1346
  multiply(scalar) {
1647
- const { endo, n: N } = CURVE;
1648
- aInRange("scalar", scalar, _1n4, N);
1347
+ const { endo: endo2, n: N } = CURVE;
1348
+ aInRange("scalar", scalar, _1n3, N);
1649
1349
  let point, fake;
1650
- if (endo) {
1651
- const { k1neg, k1, k2neg, k2 } = endo.splitScalar(scalar);
1350
+ if (endo2) {
1351
+ const { k1neg, k1, k2neg, k2 } = endo2.splitScalar(scalar);
1652
1352
  let { p: k1p, f: f1p } = this.wNAF(k1);
1653
1353
  let { p: k2p, f: f2p } = this.wNAF(k2);
1654
1354
  k1p = wnaf.constTimeNegate(k1neg, k1p);
1655
1355
  k2p = wnaf.constTimeNegate(k2neg, k2p);
1656
- k2p = new Point2(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
1356
+ k2p = new Point2(Fp.mul(k2p.px, endo2.beta), k2p.py, k2p.pz);
1657
1357
  point = k1p.add(k2p);
1658
1358
  fake = f1p.add(f2p);
1659
1359
  } else {
@@ -1671,7 +1371,7 @@ function weierstrassPoints(opts) {
1671
1371
  */
1672
1372
  multiplyAndAddUnsafe(Q, a, b) {
1673
1373
  const G = Point2.BASE;
1674
- const mul = (P, a2) => a2 === _0n4 || a2 === _1n4 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
1374
+ const mul = (P, a2) => a2 === _0n3 || a2 === _1n3 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
1675
1375
  const sum = mul(this, a).add(mul(Q, b));
1676
1376
  return sum.is0() ? void 0 : sum;
1677
1377
  }
@@ -1683,7 +1383,7 @@ function weierstrassPoints(opts) {
1683
1383
  }
1684
1384
  isTorsionFree() {
1685
1385
  const { h: cofactor, isTorsionFree } = CURVE;
1686
- if (cofactor === _1n4)
1386
+ if (cofactor === _1n3)
1687
1387
  return true;
1688
1388
  if (isTorsionFree)
1689
1389
  return isTorsionFree(Point2, this);
@@ -1691,7 +1391,7 @@ function weierstrassPoints(opts) {
1691
1391
  }
1692
1392
  clearCofactor() {
1693
1393
  const { h: cofactor, clearCofactor } = CURVE;
1694
- if (cofactor === _1n4)
1394
+ if (cofactor === _1n3)
1695
1395
  return this;
1696
1396
  if (clearCofactor)
1697
1397
  return clearCofactor(Point2, this);
@@ -1709,8 +1409,8 @@ function weierstrassPoints(opts) {
1709
1409
  }
1710
1410
  Point2.BASE = new Point2(CURVE.Gx, CURVE.Gy, Fp.ONE);
1711
1411
  Point2.ZERO = new Point2(Fp.ZERO, Fp.ONE, Fp.ZERO);
1712
- const _bits = CURVE.nBitLength;
1713
- const wnaf = wNAF(Point2, CURVE.endo ? Math.ceil(_bits / 2) : _bits);
1412
+ const { endo, nBitLength } = CURVE;
1413
+ const wnaf = wNAF(Point2, endo ? Math.ceil(nBitLength / 2) : nBitLength);
1714
1414
  return {
1715
1415
  CURVE,
1716
1416
  ProjectivePoint: Point2,
@@ -1734,7 +1434,7 @@ function validateOpts(curve) {
1734
1434
  }
1735
1435
  function weierstrass(curveDef) {
1736
1436
  const CURVE = validateOpts(curveDef);
1737
- const { Fp, n: CURVE_ORDER } = CURVE;
1437
+ const { Fp, n: CURVE_ORDER, nByteLength, nBitLength } = CURVE;
1738
1438
  const compressedLen = Fp.BYTES + 1;
1739
1439
  const uncompressedLen = 2 * Fp.BYTES + 1;
1740
1440
  function modN2(a) {
@@ -1762,7 +1462,7 @@ function weierstrass(curveDef) {
1762
1462
  const tail = bytes.subarray(1);
1763
1463
  if (len === compressedLen && (head === 2 || head === 3)) {
1764
1464
  const x = bytesToNumberBE(tail);
1765
- if (!inRange(x, _1n4, Fp.ORDER))
1465
+ if (!inRange(x, _1n3, Fp.ORDER))
1766
1466
  throw new Error("Point is not on curve");
1767
1467
  const y2 = weierstrassEquation(x);
1768
1468
  let y;
@@ -1772,7 +1472,7 @@ function weierstrass(curveDef) {
1772
1472
  const suffix = sqrtError instanceof Error ? ": " + sqrtError.message : "";
1773
1473
  throw new Error("Point is not on curve" + suffix);
1774
1474
  }
1775
- const isYOdd = (y & _1n4) === _1n4;
1475
+ const isYOdd = (y & _1n3) === _1n3;
1776
1476
  const isHeadOdd = (head & 1) === 1;
1777
1477
  if (isHeadOdd !== isYOdd)
1778
1478
  y = Fp.neg(y);
@@ -1788,9 +1488,8 @@ function weierstrass(curveDef) {
1788
1488
  }
1789
1489
  }
1790
1490
  });
1791
- const numToNByteStr = (num2) => bytesToHex(numberToBytesBE(num2, CURVE.nByteLength));
1792
1491
  function isBiggerThanHalfOrder(number) {
1793
- const HALF = CURVE_ORDER >> _1n4;
1492
+ const HALF = CURVE_ORDER >> _1n3;
1794
1493
  return number > HALF;
1795
1494
  }
1796
1495
  function normalizeS(s) {
@@ -1799,14 +1498,17 @@ function weierstrass(curveDef) {
1799
1498
  const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
1800
1499
  class Signature {
1801
1500
  constructor(r, s, recovery) {
1501
+ aInRange("r", r, _1n3, CURVE_ORDER);
1502
+ aInRange("s", s, _1n3, CURVE_ORDER);
1802
1503
  this.r = r;
1803
1504
  this.s = s;
1804
- this.recovery = recovery;
1805
- this.assertValidity();
1505
+ if (recovery != null)
1506
+ this.recovery = recovery;
1507
+ Object.freeze(this);
1806
1508
  }
1807
1509
  // pair (bytes of r, bytes of s)
1808
1510
  static fromCompact(hex) {
1809
- const l = CURVE.nByteLength;
1511
+ const l = nByteLength;
1810
1512
  hex = ensureBytes("compactSignature", hex, l * 2);
1811
1513
  return new Signature(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
1812
1514
  }
@@ -1816,9 +1518,11 @@ function weierstrass(curveDef) {
1816
1518
  const { r, s } = DER.toSig(ensureBytes("DER", hex));
1817
1519
  return new Signature(r, s);
1818
1520
  }
1521
+ /**
1522
+ * @todo remove
1523
+ * @deprecated
1524
+ */
1819
1525
  assertValidity() {
1820
- aInRange("r", this.r, _1n4, CURVE_ORDER);
1821
- aInRange("s", this.s, _1n4, CURVE_ORDER);
1822
1526
  }
1823
1527
  addRecoveryBit(recovery) {
1824
1528
  return new Signature(this.r, this.s, recovery);
@@ -1832,7 +1536,7 @@ function weierstrass(curveDef) {
1832
1536
  if (radj >= Fp.ORDER)
1833
1537
  throw new Error("recovery id 2 or 3 invalid");
1834
1538
  const prefix = (rec & 1) === 0 ? "02" : "03";
1835
- const R = Point2.fromHex(prefix + numToNByteStr(radj));
1539
+ const R = Point2.fromHex(prefix + numToSizedHex(radj, Fp.BYTES));
1836
1540
  const ir = invN(radj);
1837
1541
  const u1 = modN2(-h * ir);
1838
1542
  const u2 = modN2(s * ir);
@@ -1854,14 +1558,15 @@ function weierstrass(curveDef) {
1854
1558
  return hexToBytes(this.toDERHex());
1855
1559
  }
1856
1560
  toDERHex() {
1857
- return DER.hexFromSig({ r: this.r, s: this.s });
1561
+ return DER.hexFromSig(this);
1858
1562
  }
1859
1563
  // padded bytes of r, then padded bytes of s
1860
1564
  toCompactRawBytes() {
1861
1565
  return hexToBytes(this.toCompactHex());
1862
1566
  }
1863
1567
  toCompactHex() {
1864
- return numToNByteStr(this.r) + numToNByteStr(this.s);
1568
+ const l = nByteLength;
1569
+ return numToSizedHex(this.r, l) + numToSizedHex(this.s, l);
1865
1570
  }
1866
1571
  }
1867
1572
  const utils = {
@@ -1900,21 +1605,25 @@ function weierstrass(curveDef) {
1900
1605
  return Point2.fromPrivateKey(privateKey).toRawBytes(isCompressed);
1901
1606
  }
1902
1607
  function isProbPub(item) {
1903
- const arr = isBytes2(item);
1904
- const str = typeof item === "string";
1905
- const len = (arr || str) && item.length;
1906
- if (arr)
1907
- return len === compressedLen || len === uncompressedLen;
1908
- if (str)
1909
- return len === 2 * compressedLen || len === 2 * uncompressedLen;
1608
+ if (typeof item === "bigint")
1609
+ return false;
1910
1610
  if (item instanceof Point2)
1911
1611
  return true;
1912
- return false;
1612
+ const arr = ensureBytes("key", item);
1613
+ const len = arr.length;
1614
+ const fpl = Fp.BYTES;
1615
+ const compLen = fpl + 1;
1616
+ const uncompLen = 2 * fpl + 1;
1617
+ if (CURVE.allowedPrivateKeyLengths || nByteLength === compLen) {
1618
+ return void 0;
1619
+ } else {
1620
+ return len === compLen || len === uncompLen;
1621
+ }
1913
1622
  }
1914
1623
  function getSharedSecret(privateA, publicB, isCompressed = true) {
1915
- if (isProbPub(privateA))
1624
+ if (isProbPub(privateA) === true)
1916
1625
  throw new Error("first arg must be private key");
1917
- if (!isProbPub(publicB))
1626
+ if (isProbPub(publicB) === false)
1918
1627
  throw new Error("second arg must be public key");
1919
1628
  const b = Point2.fromHex(publicB);
1920
1629
  return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
@@ -1923,16 +1632,16 @@ function weierstrass(curveDef) {
1923
1632
  if (bytes.length > 8192)
1924
1633
  throw new Error("input is too large");
1925
1634
  const num2 = bytesToNumberBE(bytes);
1926
- const delta = bytes.length * 8 - CURVE.nBitLength;
1635
+ const delta = bytes.length * 8 - nBitLength;
1927
1636
  return delta > 0 ? num2 >> BigInt(delta) : num2;
1928
1637
  };
1929
1638
  const bits2int_modN = CURVE.bits2int_modN || function(bytes) {
1930
1639
  return modN2(bits2int(bytes));
1931
1640
  };
1932
- const ORDER_MASK = bitMask(CURVE.nBitLength);
1641
+ const ORDER_MASK = bitMask(nBitLength);
1933
1642
  function int2octets(num2) {
1934
- aInRange("num < 2^" + CURVE.nBitLength, num2, _0n4, ORDER_MASK);
1935
- return numberToBytesBE(num2, CURVE.nByteLength);
1643
+ aInRange("num < 2^" + nBitLength, num2, _0n3, ORDER_MASK);
1644
+ return numberToBytesBE(num2, nByteLength);
1936
1645
  }
1937
1646
  function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
1938
1647
  if (["recovered", "canonical"].some((k) => k in opts))
@@ -1961,12 +1670,12 @@ function weierstrass(curveDef) {
1961
1670
  const ik = invN(k);
1962
1671
  const q = Point2.BASE.multiply(k).toAffine();
1963
1672
  const r = modN2(q.x);
1964
- if (r === _0n4)
1673
+ if (r === _0n3)
1965
1674
  return;
1966
1675
  const s = modN2(ik * modN2(m + r * d));
1967
- if (s === _0n4)
1676
+ if (s === _0n3)
1968
1677
  return;
1969
- let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
1678
+ let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n3);
1970
1679
  let normS = s;
1971
1680
  if (lowS && isBiggerThanHalfOrder(s)) {
1972
1681
  normS = normalizeS(s);
@@ -1995,7 +1704,7 @@ function weierstrass(curveDef) {
1995
1704
  throw new Error("options.strict was renamed to lowS");
1996
1705
  if (format !== void 0 && format !== "compact" && format !== "der")
1997
1706
  throw new Error("format must be compact or der");
1998
- const isHex = typeof sg === "string" || isBytes2(sg);
1707
+ const isHex = typeof sg === "string" || isBytes(sg);
1999
1708
  const isObj = !isHex && !format && typeof sg === "object" && sg !== null && typeof sg.r === "bigint" && typeof sg.s === "bigint";
2000
1709
  if (!isHex && !isObj)
2001
1710
  throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
@@ -2049,18 +1758,18 @@ function weierstrass(curveDef) {
2049
1758
  }
2050
1759
  function SWUFpSqrtRatio(Fp, Z) {
2051
1760
  const q = Fp.ORDER;
2052
- let l = _0n4;
2053
- for (let o = q - _1n4; o % _2n3 === _0n4; o /= _2n3)
2054
- l += _1n4;
1761
+ let l = _0n3;
1762
+ for (let o = q - _1n3; o % _2n2 === _0n3; o /= _2n2)
1763
+ l += _1n3;
2055
1764
  const c1 = l;
2056
- const _2n_pow_c1_1 = _2n3 << c1 - _1n4 - _1n4;
2057
- const _2n_pow_c1 = _2n_pow_c1_1 * _2n3;
2058
- const c2 = (q - _1n4) / _2n_pow_c1;
2059
- const c3 = (c2 - _1n4) / _2n3;
2060
- const c4 = _2n_pow_c1 - _1n4;
1765
+ const _2n_pow_c1_1 = _2n2 << c1 - _1n3 - _1n3;
1766
+ const _2n_pow_c1 = _2n_pow_c1_1 * _2n2;
1767
+ const c2 = (q - _1n3) / _2n_pow_c1;
1768
+ const c3 = (c2 - _1n3) / _2n2;
1769
+ const c4 = _2n_pow_c1 - _1n3;
2061
1770
  const c5 = _2n_pow_c1_1;
2062
1771
  const c6 = Fp.pow(Z, c2);
2063
- const c7 = Fp.pow(Z, (c2 + _1n4) / _2n3);
1772
+ const c7 = Fp.pow(Z, (c2 + _1n3) / _2n2);
2064
1773
  let sqrtRatio = (u, v) => {
2065
1774
  let tv1 = c6;
2066
1775
  let tv2 = Fp.pow(v, c4);
@@ -2078,9 +1787,9 @@ function SWUFpSqrtRatio(Fp, Z) {
2078
1787
  tv5 = Fp.mul(tv4, tv1);
2079
1788
  tv3 = Fp.cmov(tv2, tv3, isQR);
2080
1789
  tv4 = Fp.cmov(tv5, tv4, isQR);
2081
- for (let i = c1; i > _1n4; i--) {
2082
- let tv52 = i - _2n3;
2083
- tv52 = _2n3 << tv52 - _1n4;
1790
+ for (let i = c1; i > _1n3; i--) {
1791
+ let tv52 = i - _2n2;
1792
+ tv52 = _2n2 << tv52 - _1n3;
2084
1793
  let tvv5 = Fp.pow(tv4, tv52);
2085
1794
  const e1 = Fp.eql(tvv5, Fp.ONE);
2086
1795
  tv2 = Fp.mul(tv3, tv1);
@@ -2142,12 +1851,13 @@ function mapToCurveSimpleSWU(Fp, opts) {
2142
1851
  y = Fp.cmov(y, value, isValid);
2143
1852
  const e1 = Fp.isOdd(u) === Fp.isOdd(y);
2144
1853
  y = Fp.cmov(Fp.neg(y), y, e1);
2145
- x = Fp.div(x, tv4);
1854
+ const tv4_inv = FpInvertBatch(Fp, [tv4], true)[0];
1855
+ x = Fp.mul(x, tv4_inv);
2146
1856
  return { x, y };
2147
1857
  };
2148
1858
  }
2149
1859
 
2150
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/_shortw_utils.js
1860
+ // ../../node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/_shortw_utils.js
2151
1861
  function getHash(hash) {
2152
1862
  return {
2153
1863
  hash,
@@ -2157,10 +1867,10 @@ function getHash(hash) {
2157
1867
  }
2158
1868
  function createCurve(curveDef, defHash) {
2159
1869
  const create = (hash) => weierstrass({ ...curveDef, ...getHash(hash) });
2160
- return Object.freeze({ ...create(defHash), create });
1870
+ return { ...create(defHash), create };
2161
1871
  }
2162
1872
 
2163
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/hash-to-curve.js
1873
+ // ../../node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/hash-to-curve.js
2164
1874
  var os2ip = bytesToNumberBE;
2165
1875
  function i2osp(value, length) {
2166
1876
  anum(value);
@@ -2190,7 +1900,7 @@ function expand_message_xmd(msg, DST, lenInBytes, H) {
2190
1900
  abytes2(DST);
2191
1901
  anum(lenInBytes);
2192
1902
  if (DST.length > 255)
2193
- DST = H(concatBytes2(utf8ToBytes2("H2C-OVERSIZE-DST-"), DST));
1903
+ DST = H(concatBytes2(utf8ToBytes("H2C-OVERSIZE-DST-"), DST));
2194
1904
  const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
2195
1905
  const ell = Math.ceil(lenInBytes / b_in_bytes);
2196
1906
  if (lenInBytes > 65535 || ell > 255)
@@ -2214,7 +1924,7 @@ function expand_message_xof(msg, DST, lenInBytes, k, H) {
2214
1924
  anum(lenInBytes);
2215
1925
  if (DST.length > 255) {
2216
1926
  const dkLen = Math.ceil(2 * k / 8);
2217
- DST = H.create({ dkLen }).update(utf8ToBytes2("H2C-OVERSIZE-DST-")).update(DST).digest();
1927
+ DST = H.create({ dkLen }).update(utf8ToBytes("H2C-OVERSIZE-DST-")).update(DST).digest();
2218
1928
  }
2219
1929
  if (lenInBytes > 65535 || DST.length > 255)
2220
1930
  throw new Error("expand_message_xof: invalid lenInBytes");
@@ -2231,7 +1941,7 @@ function hash_to_field(msg, count, options) {
2231
1941
  const { p, k, m, hash, expand, DST: _DST } = options;
2232
1942
  abytes2(msg);
2233
1943
  anum(count);
2234
- const DST = typeof _DST === "string" ? utf8ToBytes2(_DST) : _DST;
1944
+ const DST = typeof _DST === "string" ? utf8ToBytes(_DST) : _DST;
2235
1945
  const log2p = p.toString(2).length;
2236
1946
  const L = Math.ceil((log2p + k) / 8);
2237
1947
  const len_in_bytes = count * m * L;
@@ -2258,56 +1968,63 @@ function hash_to_field(msg, count, options) {
2258
1968
  return u;
2259
1969
  }
2260
1970
  function isogenyMap(field, map) {
2261
- const COEFF = map.map((i) => Array.from(i).reverse());
1971
+ const coeff = map.map((i) => Array.from(i).reverse());
2262
1972
  return (x, y) => {
2263
- const [xNum, xDen, yNum, yDen] = COEFF.map((val) => val.reduce((acc, i) => field.add(field.mul(acc, x), i)));
2264
- x = field.div(xNum, xDen);
2265
- y = field.mul(y, field.div(yNum, yDen));
1973
+ const [xn, xd, yn, yd] = coeff.map((val) => val.reduce((acc, i) => field.add(field.mul(acc, x), i)));
1974
+ const [xd_inv, yd_inv] = FpInvertBatch(field, [xd, yd], true);
1975
+ x = field.mul(xn, xd_inv);
1976
+ y = field.mul(y, field.mul(yn, yd_inv));
2266
1977
  return { x, y };
2267
1978
  };
2268
1979
  }
2269
- function createHasher(Point2, mapToCurve, def) {
1980
+ function createHasher2(Point2, mapToCurve, defaults) {
2270
1981
  if (typeof mapToCurve !== "function")
2271
1982
  throw new Error("mapToCurve() must be defined");
1983
+ function map(num2) {
1984
+ return Point2.fromAffine(mapToCurve(num2));
1985
+ }
1986
+ function clear(initial) {
1987
+ const P = initial.clearCofactor();
1988
+ if (P.equals(Point2.ZERO))
1989
+ return Point2.ZERO;
1990
+ P.assertValidity();
1991
+ return P;
1992
+ }
2272
1993
  return {
1994
+ defaults,
2273
1995
  // Encodes byte string to elliptic curve.
2274
1996
  // hash_to_curve from https://www.rfc-editor.org/rfc/rfc9380#section-3
2275
1997
  hashToCurve(msg, options) {
2276
- const u = hash_to_field(msg, 2, { ...def, DST: def.DST, ...options });
2277
- const u0 = Point2.fromAffine(mapToCurve(u[0]));
2278
- const u1 = Point2.fromAffine(mapToCurve(u[1]));
2279
- const P = u0.add(u1).clearCofactor();
2280
- P.assertValidity();
2281
- return P;
1998
+ const u = hash_to_field(msg, 2, { ...defaults, DST: defaults.DST, ...options });
1999
+ const u0 = map(u[0]);
2000
+ const u1 = map(u[1]);
2001
+ return clear(u0.add(u1));
2282
2002
  },
2283
2003
  // Encodes byte string to elliptic curve.
2284
2004
  // encode_to_curve from https://www.rfc-editor.org/rfc/rfc9380#section-3
2285
2005
  encodeToCurve(msg, options) {
2286
- const u = hash_to_field(msg, 1, { ...def, DST: def.encodeDST, ...options });
2287
- const P = Point2.fromAffine(mapToCurve(u[0])).clearCofactor();
2288
- P.assertValidity();
2289
- return P;
2006
+ const u = hash_to_field(msg, 1, { ...defaults, DST: defaults.encodeDST, ...options });
2007
+ return clear(map(u[0]));
2290
2008
  },
2291
2009
  // Same as encodeToCurve, but without hash
2292
2010
  mapToCurve(scalars) {
2293
2011
  if (!Array.isArray(scalars))
2294
- throw new Error("mapToCurve: expected array of bigints");
2012
+ throw new Error("expected array of bigints");
2295
2013
  for (const i of scalars)
2296
2014
  if (typeof i !== "bigint")
2297
- throw new Error("mapToCurve: expected array of bigints");
2298
- const P = Point2.fromAffine(mapToCurve(scalars)).clearCofactor();
2299
- P.assertValidity();
2300
- return P;
2015
+ throw new Error("expected array of bigints");
2016
+ return clear(map(scalars));
2301
2017
  }
2302
2018
  };
2303
2019
  }
2304
2020
 
2305
- // ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/secp256k1.js
2021
+ // ../../node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/secp256k1.js
2306
2022
  var secp256k1P = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
2307
2023
  var secp256k1N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
2308
- var _1n5 = BigInt(1);
2309
- var _2n4 = BigInt(2);
2310
- var divNearest = (a, b) => (a + b / _2n4) / b;
2024
+ var _0n4 = BigInt(0);
2025
+ var _1n4 = BigInt(1);
2026
+ var _2n3 = BigInt(2);
2027
+ var divNearest = (a, b) => (a + b / _2n3) / b;
2311
2028
  function sqrtMod(y) {
2312
2029
  const P = secp256k1P;
2313
2030
  const _3n3 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
@@ -2316,7 +2033,7 @@ function sqrtMod(y) {
2316
2033
  const b3 = b2 * b2 * y % P;
2317
2034
  const b6 = pow2(b3, _3n3, P) * b3 % P;
2318
2035
  const b9 = pow2(b6, _3n3, P) * b3 % P;
2319
- const b11 = pow2(b9, _2n4, P) * b2 % P;
2036
+ const b11 = pow2(b9, _2n3, P) * b2 % P;
2320
2037
  const b22 = pow2(b11, _11n, P) * b11 % P;
2321
2038
  const b44 = pow2(b22, _22n, P) * b22 % P;
2322
2039
  const b88 = pow2(b44, _44n, P) * b44 % P;
@@ -2325,40 +2042,29 @@ function sqrtMod(y) {
2325
2042
  const b223 = pow2(b220, _3n3, P) * b3 % P;
2326
2043
  const t1 = pow2(b223, _23n, P) * b22 % P;
2327
2044
  const t2 = pow2(t1, _6n, P) * b2 % P;
2328
- const root = pow2(t2, _2n4, P);
2045
+ const root = pow2(t2, _2n3, P);
2329
2046
  if (!Fpk1.eql(Fpk1.sqr(root), y))
2330
2047
  throw new Error("Cannot find square root");
2331
2048
  return root;
2332
2049
  }
2333
2050
  var Fpk1 = Field(secp256k1P, void 0, void 0, { sqrt: sqrtMod });
2334
2051
  var secp256k1 = createCurve({
2335
- a: BigInt(0),
2336
- // equation params: a, b
2052
+ a: _0n4,
2337
2053
  b: BigInt(7),
2338
- // Seem to be rigid: bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975
2339
2054
  Fp: Fpk1,
2340
- // Field's prime: 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
2341
2055
  n: secp256k1N,
2342
- // Curve order, total count of valid points in the field
2343
- // Base point (x, y) aka generator point
2344
2056
  Gx: BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),
2345
2057
  Gy: BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),
2346
2058
  h: BigInt(1),
2347
- // Cofactor
2348
2059
  lowS: true,
2349
2060
  // Allow only low-S signatures by default in sign() and verify()
2350
- /**
2351
- * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
2352
- * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
2353
- * For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
2354
- * Explanation: https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
2355
- */
2356
2061
  endo: {
2062
+ // Endomorphism, see above
2357
2063
  beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
2358
2064
  splitScalar: (k) => {
2359
2065
  const n = secp256k1N;
2360
2066
  const a1 = BigInt("0x3086d221a7d46bcde86c90e49284eb15");
2361
- const b1 = -_1n5 * BigInt("0xe4437ed6010e88286f547fa90abfe4c3");
2067
+ const b1 = -_1n4 * BigInt("0xe4437ed6010e88286f547fa90abfe4c3");
2362
2068
  const a2 = BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8");
2363
2069
  const b2 = a1;
2364
2070
  const POW_2_128 = BigInt("0x100000000000000000000000000000000");
@@ -2379,7 +2085,6 @@ var secp256k1 = createCurve({
2379
2085
  }
2380
2086
  }
2381
2087
  }, sha256);
2382
- var _0n5 = BigInt(0);
2383
2088
  var TAGGED_HASH_PREFIXES = {};
2384
2089
  function taggedHash(tag, ...messages) {
2385
2090
  let tagP = TAGGED_HASH_PREFIXES[tag];
@@ -2394,7 +2099,7 @@ var pointToBytes = (point) => point.toRawBytes(true).slice(1);
2394
2099
  var numTo32b = (n) => numberToBytesBE(n, 32);
2395
2100
  var modP = (x) => mod(x, secp256k1P);
2396
2101
  var modN = (x) => mod(x, secp256k1N);
2397
- var Point = secp256k1.ProjectivePoint;
2102
+ var Point = /* @__PURE__ */ (() => secp256k1.ProjectivePoint)();
2398
2103
  var GmulAdd = (Q, a, b) => Point.BASE.multiplyAndAddUnsafe(Q, a, b);
2399
2104
  function schnorrGetExtPubKey(priv) {
2400
2105
  let d_ = secp256k1.utils.normPrivateKeyToScalar(priv);
@@ -2403,13 +2108,13 @@ function schnorrGetExtPubKey(priv) {
2403
2108
  return { scalar, bytes: pointToBytes(p) };
2404
2109
  }
2405
2110
  function lift_x(x) {
2406
- aInRange("x", x, _1n5, secp256k1P);
2111
+ aInRange("x", x, _1n4, secp256k1P);
2407
2112
  const xx = modP(x * x);
2408
2113
  const c = modP(xx * x + BigInt(7));
2409
2114
  let y = sqrtMod(c);
2410
- if (y % _2n4 !== _0n5)
2115
+ if (y % _2n3 !== _0n4)
2411
2116
  y = modP(-y);
2412
- const p = new Point(x, y, _1n5);
2117
+ const p = new Point(x, y, _1n4);
2413
2118
  p.assertValidity();
2414
2119
  return p;
2415
2120
  }
@@ -2427,7 +2132,7 @@ function schnorrSign(message, privateKey, auxRand = randomBytes(32)) {
2427
2132
  const t = numTo32b(d ^ num(taggedHash("BIP0340/aux", a)));
2428
2133
  const rand = taggedHash("BIP0340/nonce", t, px, m);
2429
2134
  const k_ = modN(num(rand));
2430
- if (k_ === _0n5)
2135
+ if (k_ === _0n4)
2431
2136
  throw new Error("sign failed: k is zero");
2432
2137
  const { bytes: rx, scalar: k } = schnorrGetExtPubKey(k_);
2433
2138
  const e = challenge(rx, px, m);
@@ -2445,10 +2150,10 @@ function schnorrVerify(signature, message, publicKey) {
2445
2150
  try {
2446
2151
  const P = lift_x(num(pub));
2447
2152
  const r = num(sig.subarray(0, 32));
2448
- if (!inRange(r, _1n5, secp256k1P))
2153
+ if (!inRange(r, _1n4, secp256k1P))
2449
2154
  return false;
2450
2155
  const s = num(sig.subarray(32, 64));
2451
- if (!inRange(s, _1n5, secp256k1N))
2156
+ if (!inRange(s, _1n4, secp256k1N))
2452
2157
  return false;
2453
2158
  const e = challenge(numTo32b(r), pointToBytes(P), m);
2454
2159
  const R = GmulAdd(P, s, modN(-e));
@@ -2509,7 +2214,7 @@ var mapSWU = /* @__PURE__ */ (() => mapToCurveSimpleSWU(Fpk1, {
2509
2214
  B: BigInt("1771"),
2510
2215
  Z: Fpk1.create(BigInt("-11"))
2511
2216
  }))();
2512
- var htf = /* @__PURE__ */ (() => createHasher(secp256k1.ProjectivePoint, (scalars) => {
2217
+ var secp256k1_hasher = /* @__PURE__ */ (() => createHasher2(secp256k1.ProjectivePoint, (scalars) => {
2513
2218
  const { x, y } = mapSWU(Fpk1.create(scalars[0]));
2514
2219
  return isoMap(x, y);
2515
2220
  }, {
@@ -2521,35 +2226,22 @@ var htf = /* @__PURE__ */ (() => createHasher(secp256k1.ProjectivePoint, (scalar
2521
2226
  expand: "xmd",
2522
2227
  hash: sha256
2523
2228
  }))();
2524
- var hashToCurve = /* @__PURE__ */ (() => htf.hashToCurve)();
2525
- var encodeToCurve = /* @__PURE__ */ (() => htf.encodeToCurve)();
2229
+ var hashToCurve = /* @__PURE__ */ (() => secp256k1_hasher.hashToCurve)();
2230
+ var encodeToCurve = /* @__PURE__ */ (() => secp256k1_hasher.encodeToCurve)();
2526
2231
  export {
2527
2232
  encodeToCurve,
2528
2233
  hashToCurve,
2529
2234
  schnorr,
2530
- secp256k1
2235
+ secp256k1,
2236
+ secp256k1_hasher
2531
2237
  };
2532
2238
  /*! Bundled license information:
2533
2239
 
2534
- @noble/hashes/esm/utils.js:
2535
- (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2536
-
2537
- @noble/curves/esm/abstract/utils.js:
2538
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2539
-
2540
2240
  @noble/curves/esm/abstract/modular.js:
2541
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2542
-
2543
2241
  @noble/curves/esm/abstract/curve.js:
2544
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2545
-
2546
2242
  @noble/curves/esm/abstract/weierstrass.js:
2547
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2548
-
2549
2243
  @noble/curves/esm/_shortw_utils.js:
2550
- (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2551
-
2552
2244
  @noble/curves/esm/secp256k1.js:
2553
2245
  (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2554
2246
  */
2555
- //# sourceMappingURL=secp256k1-QZA5SALG.js.map
2247
+ //# sourceMappingURL=secp256k1-XP7IUONI.js.map