@zoralabs/protocol-deployments 0.3.5-COMMENTS.2 → 0.3.5-COMMENTS.4

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.
@@ -10,12 +10,12 @@ import {
10
10
  rotr,
11
11
  toBytes,
12
12
  wrapConstructor
13
- } from "./chunk-5JV63AHR.js";
13
+ } from "./chunk-FJ2PMLO3.js";
14
14
  import {
15
15
  __export
16
16
  } from "./chunk-3EJPJMEH.js";
17
17
 
18
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_md.js
18
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_md.js
19
19
  function setBigUint64(view, byteOffset, value, isLE) {
20
20
  if (typeof view.setBigUint64 === "function")
21
21
  return view.setBigUint64(byteOffset, value, isLE);
@@ -117,7 +117,7 @@ var HashMD = class extends Hash {
117
117
  }
118
118
  };
119
119
 
120
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/sha256.js
120
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/sha256.js
121
121
  var SHA256_K = /* @__PURE__ */ new Uint32Array([
122
122
  1116352408,
123
123
  1899447441,
@@ -267,9 +267,76 @@ var SHA256 = class extends HashMD {
267
267
  };
268
268
  var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
269
269
 
270
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/abstract/utils.js
270
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/hmac.js
271
+ var HMAC = class extends Hash {
272
+ constructor(hash2, _key) {
273
+ super();
274
+ this.finished = false;
275
+ this.destroyed = false;
276
+ hash(hash2);
277
+ const key = toBytes(_key);
278
+ this.iHash = hash2.create();
279
+ if (typeof this.iHash.update !== "function")
280
+ throw new Error("Expected instance of class which extends utils.Hash");
281
+ this.blockLen = this.iHash.blockLen;
282
+ this.outputLen = this.iHash.outputLen;
283
+ const blockLen = this.blockLen;
284
+ const pad = new Uint8Array(blockLen);
285
+ pad.set(key.length > blockLen ? hash2.create().update(key).digest() : key);
286
+ for (let i = 0; i < pad.length; i++)
287
+ pad[i] ^= 54;
288
+ this.iHash.update(pad);
289
+ this.oHash = hash2.create();
290
+ for (let i = 0; i < pad.length; i++)
291
+ pad[i] ^= 54 ^ 92;
292
+ this.oHash.update(pad);
293
+ pad.fill(0);
294
+ }
295
+ update(buf) {
296
+ exists(this);
297
+ this.iHash.update(buf);
298
+ return this;
299
+ }
300
+ digestInto(out) {
301
+ exists(this);
302
+ bytes(out, this.outputLen);
303
+ this.finished = true;
304
+ this.iHash.digestInto(out);
305
+ this.oHash.update(out);
306
+ this.oHash.digestInto(out);
307
+ this.destroy();
308
+ }
309
+ digest() {
310
+ const out = new Uint8Array(this.oHash.outputLen);
311
+ this.digestInto(out);
312
+ return out;
313
+ }
314
+ _cloneInto(to) {
315
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
316
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
317
+ to = to;
318
+ to.finished = finished;
319
+ to.destroyed = destroyed;
320
+ to.blockLen = blockLen;
321
+ to.outputLen = outputLen;
322
+ to.oHash = oHash._cloneInto(to.oHash);
323
+ to.iHash = iHash._cloneInto(to.iHash);
324
+ return to;
325
+ }
326
+ destroy() {
327
+ this.destroyed = true;
328
+ this.oHash.destroy();
329
+ this.iHash.destroy();
330
+ }
331
+ };
332
+ var hmac = (hash2, key, message) => new HMAC(hash2, key).update(message).digest();
333
+ hmac.create = (hash2, key) => new HMAC(hash2, key);
334
+
335
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/abstract/utils.js
271
336
  var utils_exports = {};
272
337
  __export(utils_exports, {
338
+ aInRange: () => aInRange,
339
+ abool: () => abool,
273
340
  abytes: () => abytes,
274
341
  bitGet: () => bitGet,
275
342
  bitLen: () => bitLen,
@@ -284,7 +351,10 @@ __export(utils_exports, {
284
351
  equalBytes: () => equalBytes,
285
352
  hexToBytes: () => hexToBytes,
286
353
  hexToNumber: () => hexToNumber,
354
+ inRange: () => inRange,
287
355
  isBytes: () => isBytes,
356
+ memoized: () => memoized,
357
+ notImplemented: () => notImplemented,
288
358
  numberToBytesBE: () => numberToBytesBE,
289
359
  numberToBytesLE: () => numberToBytesLE,
290
360
  numberToHexUnpadded: () => numberToHexUnpadded,
@@ -292,9 +362,9 @@ __export(utils_exports, {
292
362
  utf8ToBytes: () => utf8ToBytes,
293
363
  validateObject: () => validateObject
294
364
  });
295
- var _0n = BigInt(0);
296
- var _1n = BigInt(1);
297
- var _2n = BigInt(2);
365
+ var _0n = /* @__PURE__ */ BigInt(0);
366
+ var _1n = /* @__PURE__ */ BigInt(1);
367
+ var _2n = /* @__PURE__ */ BigInt(2);
298
368
  function isBytes(a) {
299
369
  return a instanceof Uint8Array || a != null && typeof a === "object" && a.constructor.name === "Uint8Array";
300
370
  }
@@ -302,6 +372,10 @@ function abytes(item) {
302
372
  if (!isBytes(item))
303
373
  throw new Error("Uint8Array expected");
304
374
  }
375
+ function abool(title, value) {
376
+ if (typeof value !== "boolean")
377
+ throw new Error(`${title} must be valid boolean, got "${value}".`);
378
+ }
305
379
  var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
306
380
  function bytesToHex(bytes2) {
307
381
  abytes(bytes2);
@@ -311,8 +385,8 @@ function bytesToHex(bytes2) {
311
385
  }
312
386
  return hex;
313
387
  }
314
- function numberToHexUnpadded(num) {
315
- const hex = num.toString(16);
388
+ function numberToHexUnpadded(num2) {
389
+ const hex = num2.toString(16);
316
390
  return hex.length & 1 ? `0${hex}` : hex;
317
391
  }
318
392
  function hexToNumber(hex) {
@@ -411,6 +485,14 @@ function utf8ToBytes(str) {
411
485
  throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
412
486
  return new Uint8Array(new TextEncoder().encode(str));
413
487
  }
488
+ var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
489
+ function inRange(n, min, max) {
490
+ return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
491
+ }
492
+ function aInRange(title, n, min, max) {
493
+ if (!inRange(n, min, max))
494
+ throw new Error(`expected valid ${title}: ${min} <= n < ${max}, got ${typeof n} ${n}`);
495
+ }
414
496
  function bitLen(n) {
415
497
  let len;
416
498
  for (len = 0; n > _0n; n >>= _1n, len += 1)
@@ -503,8 +585,22 @@ function validateObject(object, validators, optValidators = {}) {
503
585
  checkField(fieldName, type, true);
504
586
  return object;
505
587
  }
588
+ var notImplemented = () => {
589
+ throw new Error("not implemented");
590
+ };
591
+ function memoized(fn) {
592
+ const map = /* @__PURE__ */ new WeakMap();
593
+ return (arg, ...args) => {
594
+ const val = map.get(arg);
595
+ if (val !== void 0)
596
+ return val;
597
+ const computed = fn(arg, ...args);
598
+ map.set(arg, computed);
599
+ return computed;
600
+ };
601
+ }
506
602
 
507
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/abstract/modular.js
603
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/abstract/modular.js
508
604
  var _0n2 = BigInt(0);
509
605
  var _1n2 = BigInt(1);
510
606
  var _2n2 = BigInt(2);
@@ -518,7 +614,7 @@ function mod(a, b) {
518
614
  const result = a % b;
519
615
  return result >= _0n2 ? result : b + result;
520
616
  }
521
- function pow(num, power, modulo) {
617
+ function pow(num2, power, modulo) {
522
618
  if (modulo <= _0n2 || power < _0n2)
523
619
  throw new Error("Expected power/modulo > 0");
524
620
  if (modulo === _1n2)
@@ -526,8 +622,8 @@ function pow(num, power, modulo) {
526
622
  let res = _1n2;
527
623
  while (power > _0n2) {
528
624
  if (power & _1n2)
529
- res = res * num % modulo;
530
- num = num * num % modulo;
625
+ res = res * num2 % modulo;
626
+ num2 = num2 * num2 % modulo;
531
627
  power >>= _1n2;
532
628
  }
533
629
  return res;
@@ -592,9 +688,9 @@ function tonelliShanks(P) {
592
688
  break;
593
689
  t2 = Fp2.sqr(t2);
594
690
  }
595
- const ge2 = Fp2.pow(g, _1n2 << BigInt(r - m - 1));
596
- g = Fp2.sqr(ge2);
597
- x = Fp2.mul(x, ge2);
691
+ const ge = Fp2.pow(g, _1n2 << BigInt(r - m - 1));
692
+ g = Fp2.sqr(ge);
693
+ x = Fp2.mul(x, ge);
598
694
  b = Fp2.mul(b, g);
599
695
  r = m;
600
696
  }
@@ -660,15 +756,15 @@ function validateField(field) {
660
756
  }, initial);
661
757
  return validateObject(field, opts);
662
758
  }
663
- function FpPow(f, num, power) {
759
+ function FpPow(f, num2, power) {
664
760
  if (power < _0n2)
665
761
  throw new Error("Expected power > 0");
666
762
  if (power === _0n2)
667
763
  return f.ONE;
668
764
  if (power === _1n2)
669
- return num;
765
+ return num2;
670
766
  let p = f.ONE;
671
- let d = num;
767
+ let d = num2;
672
768
  while (power > _0n2) {
673
769
  if (power & _1n2)
674
770
  p = f.mul(p, d);
@@ -679,18 +775,18 @@ function FpPow(f, num, power) {
679
775
  }
680
776
  function FpInvertBatch(f, nums) {
681
777
  const tmp = new Array(nums.length);
682
- const lastMultiplied = nums.reduce((acc, num, i) => {
683
- if (f.is0(num))
778
+ const lastMultiplied = nums.reduce((acc, num2, i) => {
779
+ if (f.is0(num2))
684
780
  return acc;
685
781
  tmp[i] = acc;
686
- return f.mul(acc, num);
782
+ return f.mul(acc, num2);
687
783
  }, f.ONE);
688
784
  const inverted = f.inv(lastMultiplied);
689
- nums.reduceRight((acc, num, i) => {
690
- if (f.is0(num))
785
+ nums.reduceRight((acc, num2, i) => {
786
+ if (f.is0(num2))
691
787
  return acc;
692
788
  tmp[i] = f.mul(acc, tmp[i]);
693
- return f.mul(acc, num);
789
+ return f.mul(acc, num2);
694
790
  }, inverted);
695
791
  return tmp;
696
792
  }
@@ -713,34 +809,34 @@ function Field(ORDER, bitLen2, isLE = false, redef = {}) {
713
809
  MASK: bitMask(BITS),
714
810
  ZERO: _0n2,
715
811
  ONE: _1n2,
716
- create: (num) => mod(num, ORDER),
717
- isValid: (num) => {
718
- if (typeof num !== "bigint")
719
- throw new Error(`Invalid field element: expected bigint, got ${typeof num}`);
720
- return _0n2 <= num && num < ORDER;
812
+ create: (num2) => mod(num2, ORDER),
813
+ isValid: (num2) => {
814
+ if (typeof num2 !== "bigint")
815
+ throw new Error(`Invalid field element: expected bigint, got ${typeof num2}`);
816
+ return _0n2 <= num2 && num2 < ORDER;
721
817
  },
722
- is0: (num) => num === _0n2,
723
- isOdd: (num) => (num & _1n2) === _1n2,
724
- neg: (num) => mod(-num, ORDER),
818
+ is0: (num2) => num2 === _0n2,
819
+ isOdd: (num2) => (num2 & _1n2) === _1n2,
820
+ neg: (num2) => mod(-num2, ORDER),
725
821
  eql: (lhs, rhs) => lhs === rhs,
726
- sqr: (num) => mod(num * num, ORDER),
822
+ sqr: (num2) => mod(num2 * num2, ORDER),
727
823
  add: (lhs, rhs) => mod(lhs + rhs, ORDER),
728
824
  sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
729
825
  mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
730
- pow: (num, power) => FpPow(f, num, power),
826
+ pow: (num2, power) => FpPow(f, num2, power),
731
827
  div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
732
828
  // Same as above, but doesn't normalize
733
- sqrN: (num) => num * num,
829
+ sqrN: (num2) => num2 * num2,
734
830
  addN: (lhs, rhs) => lhs + rhs,
735
831
  subN: (lhs, rhs) => lhs - rhs,
736
832
  mulN: (lhs, rhs) => lhs * rhs,
737
- inv: (num) => invert(num, ORDER),
833
+ inv: (num2) => invert(num2, ORDER),
738
834
  sqrt: redef.sqrt || ((n) => sqrtP(f, n)),
739
835
  invertBatch: (lst) => FpInvertBatch(f, lst),
740
836
  // TODO: do we really need constant cmov?
741
837
  // We don't have const-time bigints anyway, so probably will be not very useful
742
838
  cmov: (a, b, c) => c ? b : a,
743
- toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
839
+ toBytes: (num2) => isLE ? numberToBytesLE(num2, BYTES) : numberToBytesBE(num2, BYTES),
744
840
  fromBytes: (bytes2) => {
745
841
  if (bytes2.length !== BYTES)
746
842
  throw new Error(`Fp.fromBytes: expected ${BYTES}, got ${bytes2.length}`);
@@ -765,20 +861,27 @@ function mapHashToField(key, fieldOrder, isLE = false) {
765
861
  const minLen = getMinHashLength(fieldOrder);
766
862
  if (len < 16 || len < minLen || len > 1024)
767
863
  throw new Error(`expected ${minLen}-1024 bytes of input, got ${len}`);
768
- const num = isLE ? bytesToNumberBE(key) : bytesToNumberLE(key);
769
- const reduced = mod(num, fieldOrder - _1n2) + _1n2;
864
+ const num2 = isLE ? bytesToNumberBE(key) : bytesToNumberLE(key);
865
+ const reduced = mod(num2, fieldOrder - _1n2) + _1n2;
770
866
  return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
771
867
  }
772
868
 
773
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/abstract/curve.js
869
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/abstract/curve.js
774
870
  var _0n3 = BigInt(0);
775
871
  var _1n3 = BigInt(1);
872
+ var pointPrecomputes = /* @__PURE__ */ new WeakMap();
873
+ var pointWindowSizes = /* @__PURE__ */ new WeakMap();
776
874
  function wNAF(c, bits) {
777
875
  const constTimeNegate = (condition, item) => {
778
876
  const neg = item.negate();
779
877
  return condition ? neg : item;
780
878
  };
879
+ const validateW = (W) => {
880
+ if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
881
+ throw new Error(`Wrong window size=${W}, should be [1..${bits}]`);
882
+ };
781
883
  const opts = (W) => {
884
+ validateW(W);
782
885
  const windows = Math.ceil(bits / W) + 1;
783
886
  const windowSize = 2 ** (W - 1);
784
887
  return { windows, windowSize };
@@ -857,19 +960,62 @@ function wNAF(c, bits) {
857
960
  }
858
961
  return { p, f };
859
962
  },
860
- wNAFCached(P, precomputesMap, n, transform) {
861
- const W = P._WINDOW_SIZE || 1;
862
- let comp = precomputesMap.get(P);
963
+ wNAFCached(P, n, transform) {
964
+ const W = pointWindowSizes.get(P) || 1;
965
+ let comp = pointPrecomputes.get(P);
863
966
  if (!comp) {
864
967
  comp = this.precomputeWindow(P, W);
865
- if (W !== 1) {
866
- precomputesMap.set(P, transform(comp));
867
- }
968
+ if (W !== 1)
969
+ pointPrecomputes.set(P, transform(comp));
868
970
  }
869
971
  return this.wNAF(W, comp, n);
972
+ },
973
+ // We calculate precomputes for elliptic curve point multiplication
974
+ // using windowed method. This specifies window size and
975
+ // stores precomputed values. Usually only base point would be precomputed.
976
+ setWindowSize(P, W) {
977
+ validateW(W);
978
+ pointWindowSizes.set(P, W);
979
+ pointPrecomputes.delete(P);
870
980
  }
871
981
  };
872
982
  }
983
+ function pippenger(c, field, points, scalars) {
984
+ if (!Array.isArray(points) || !Array.isArray(scalars) || scalars.length !== points.length)
985
+ throw new Error("arrays of points and scalars must have equal length");
986
+ scalars.forEach((s, i) => {
987
+ if (!field.isValid(s))
988
+ throw new Error(`wrong scalar at index ${i}`);
989
+ });
990
+ points.forEach((p, i) => {
991
+ if (!(p instanceof c))
992
+ throw new Error(`wrong point at index ${i}`);
993
+ });
994
+ const wbits = bitLen(BigInt(points.length));
995
+ const windowSize = wbits > 12 ? wbits - 3 : wbits > 4 ? wbits - 2 : wbits ? 2 : 1;
996
+ const MASK = (1 << windowSize) - 1;
997
+ const buckets = new Array(MASK + 1).fill(c.ZERO);
998
+ const lastBits = Math.floor((field.BITS - 1) / windowSize) * windowSize;
999
+ let sum = c.ZERO;
1000
+ for (let i = lastBits; i >= 0; i -= windowSize) {
1001
+ buckets.fill(c.ZERO);
1002
+ for (let j = 0; j < scalars.length; j++) {
1003
+ const scalar = scalars[j];
1004
+ const wbits2 = Number(scalar >> BigInt(i) & BigInt(MASK));
1005
+ buckets[wbits2] = buckets[wbits2].add(points[j]);
1006
+ }
1007
+ let resI = c.ZERO;
1008
+ for (let j = buckets.length - 1, sumI = c.ZERO; j > 0; j--) {
1009
+ sumI = sumI.add(buckets[j]);
1010
+ resI = resI.add(sumI);
1011
+ }
1012
+ sum = sum.add(resI);
1013
+ if (i !== 0)
1014
+ for (let j = 0; j < windowSize; j++)
1015
+ sum = sum.double();
1016
+ }
1017
+ return sum;
1018
+ }
873
1019
  function validateBasic(curve) {
874
1020
  validateField(curve.Fp);
875
1021
  validateObject(curve, {
@@ -888,7 +1034,13 @@ function validateBasic(curve) {
888
1034
  });
889
1035
  }
890
1036
 
891
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/abstract/weierstrass.js
1037
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/abstract/weierstrass.js
1038
+ function validateSigVerOpts(opts) {
1039
+ if (opts.lowS !== void 0)
1040
+ abool("lowS", opts.lowS);
1041
+ if (opts.prehash !== void 0)
1042
+ abool("prehash", opts.prehash);
1043
+ }
892
1044
  function validatePointOpts(curve) {
893
1045
  const opts = validateBasic(curve);
894
1046
  validateObject(opts, {
@@ -922,48 +1074,99 @@ var DER = {
922
1074
  super(m);
923
1075
  }
924
1076
  },
925
- _parseInt(data) {
926
- const { Err: E } = DER;
927
- if (data.length < 2 || data[0] !== 2)
928
- throw new E("Invalid signature integer tag");
929
- const len = data[1];
930
- const res = data.subarray(2, len + 2);
931
- if (!len || res.length !== len)
932
- throw new E("Invalid signature integer: wrong length");
933
- if (res[0] & 128)
934
- throw new E("Invalid signature integer: negative");
935
- if (res[0] === 0 && !(res[1] & 128))
936
- throw new E("Invalid signature integer: unnecessary leading zero");
937
- return { d: b2n(res), l: data.subarray(len + 2) };
1077
+ // Basic building block is TLV (Tag-Length-Value)
1078
+ _tlv: {
1079
+ encode: (tag, data) => {
1080
+ const { Err: E } = DER;
1081
+ if (tag < 0 || tag > 256)
1082
+ throw new E("tlv.encode: wrong tag");
1083
+ if (data.length & 1)
1084
+ throw new E("tlv.encode: unpadded data");
1085
+ const dataLen = data.length / 2;
1086
+ const len = numberToHexUnpadded(dataLen);
1087
+ if (len.length / 2 & 128)
1088
+ throw new E("tlv.encode: long form length too big");
1089
+ const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
1090
+ return `${numberToHexUnpadded(tag)}${lenLen}${len}${data}`;
1091
+ },
1092
+ // v - value, l - left bytes (unparsed)
1093
+ decode(tag, data) {
1094
+ const { Err: E } = DER;
1095
+ let pos = 0;
1096
+ if (tag < 0 || tag > 256)
1097
+ throw new E("tlv.encode: wrong tag");
1098
+ if (data.length < 2 || data[pos++] !== tag)
1099
+ throw new E("tlv.decode: wrong tlv");
1100
+ const first = data[pos++];
1101
+ const isLong = !!(first & 128);
1102
+ let length = 0;
1103
+ if (!isLong)
1104
+ length = first;
1105
+ else {
1106
+ const lenLen = first & 127;
1107
+ if (!lenLen)
1108
+ throw new E("tlv.decode(long): indefinite length not supported");
1109
+ if (lenLen > 4)
1110
+ throw new E("tlv.decode(long): byte length is too big");
1111
+ const lengthBytes = data.subarray(pos, pos + lenLen);
1112
+ if (lengthBytes.length !== lenLen)
1113
+ throw new E("tlv.decode: length bytes not complete");
1114
+ if (lengthBytes[0] === 0)
1115
+ throw new E("tlv.decode(long): zero leftmost byte");
1116
+ for (const b of lengthBytes)
1117
+ length = length << 8 | b;
1118
+ pos += lenLen;
1119
+ if (length < 128)
1120
+ throw new E("tlv.decode(long): not minimal encoding");
1121
+ }
1122
+ const v = data.subarray(pos, pos + length);
1123
+ if (v.length !== length)
1124
+ throw new E("tlv.decode: wrong value length");
1125
+ return { v, l: data.subarray(pos + length) };
1126
+ }
1127
+ },
1128
+ // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
1129
+ // since we always use positive integers here. It must always be empty:
1130
+ // - add zero byte if exists
1131
+ // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
1132
+ _int: {
1133
+ encode(num2) {
1134
+ const { Err: E } = DER;
1135
+ if (num2 < _0n4)
1136
+ throw new E("integer: negative integers are not allowed");
1137
+ let hex = numberToHexUnpadded(num2);
1138
+ if (Number.parseInt(hex[0], 16) & 8)
1139
+ hex = "00" + hex;
1140
+ if (hex.length & 1)
1141
+ throw new E("unexpected assertion");
1142
+ return hex;
1143
+ },
1144
+ decode(data) {
1145
+ const { Err: E } = DER;
1146
+ if (data[0] & 128)
1147
+ throw new E("Invalid signature integer: negative");
1148
+ if (data[0] === 0 && !(data[1] & 128))
1149
+ throw new E("Invalid signature integer: unnecessary leading zero");
1150
+ return b2n(data);
1151
+ }
938
1152
  },
939
1153
  toSig(hex) {
940
- const { Err: E } = DER;
1154
+ const { Err: E, _int: int, _tlv: tlv } = DER;
941
1155
  const data = typeof hex === "string" ? h2b(hex) : hex;
942
1156
  abytes(data);
943
- let l = data.length;
944
- if (l < 2 || data[0] != 48)
945
- throw new E("Invalid signature tag");
946
- if (data[1] !== l - 2)
947
- throw new E("Invalid signature: incorrect length");
948
- const { d: r, l: sBytes } = DER._parseInt(data.subarray(2));
949
- const { d: s, l: rBytesLeft } = DER._parseInt(sBytes);
950
- if (rBytesLeft.length)
1157
+ const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
1158
+ if (seqLeftBytes.length)
1159
+ throw new E("Invalid signature: left bytes after parsing");
1160
+ const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
1161
+ const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
1162
+ if (sLeftBytes.length)
951
1163
  throw new E("Invalid signature: left bytes after parsing");
952
- return { r, s };
1164
+ return { r: int.decode(rBytes), s: int.decode(sBytes) };
953
1165
  },
954
1166
  hexFromSig(sig) {
955
- const slice = (s2) => Number.parseInt(s2[0], 16) & 8 ? "00" + s2 : s2;
956
- const h = (num) => {
957
- const hex = num.toString(16);
958
- return hex.length & 1 ? `0${hex}` : hex;
959
- };
960
- const s = slice(h(sig.s));
961
- const r = slice(h(sig.r));
962
- const shl = s.length / 2;
963
- const rhl = r.length / 2;
964
- const sl = h(shl);
965
- const rl = h(rhl);
966
- return `30${h(rhl + shl + 4)}02${rl}${r}02${sl}${s}`;
1167
+ const { _tlv: tlv, _int: int } = DER;
1168
+ const seq = `${tlv.encode(2, int.encode(sig.r))}${tlv.encode(2, int.encode(sig.s))}`;
1169
+ return tlv.encode(48, seq);
967
1170
  }
968
1171
  };
969
1172
  var _0n4 = BigInt(0);
@@ -974,6 +1177,7 @@ var _4n2 = BigInt(4);
974
1177
  function weierstrassPoints(opts) {
975
1178
  const CURVE = validatePointOpts(opts);
976
1179
  const { Fp: Fp2 } = CURVE;
1180
+ const Fn = Field(CURVE.n, CURVE.nBitLength);
977
1181
  const toBytes2 = CURVE.toBytes || ((_c, point, _isCompressed) => {
978
1182
  const a = point.toAffine();
979
1183
  return concatBytes2(Uint8Array.from([4]), Fp2.toBytes(a.x), Fp2.toBytes(a.y));
@@ -992,15 +1196,11 @@ function weierstrassPoints(opts) {
992
1196
  }
993
1197
  if (!Fp2.eql(Fp2.sqr(CURVE.Gy), weierstrassEquation(CURVE.Gx)))
994
1198
  throw new Error("bad generator point: equation left != right");
995
- function isWithinCurveOrder(num) {
996
- return typeof num === "bigint" && _0n4 < num && num < CURVE.n;
997
- }
998
- function assertGE(num) {
999
- if (!isWithinCurveOrder(num))
1000
- throw new Error("Expected valid bigint: 0 < bigint < curve.n");
1199
+ function isWithinCurveOrder(num2) {
1200
+ return inRange(num2, _1n4, CURVE.n);
1001
1201
  }
1002
1202
  function normPrivateKeyToScalar(key) {
1003
- const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n } = CURVE;
1203
+ const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N } = CURVE;
1004
1204
  if (lengths && typeof key !== "bigint") {
1005
1205
  if (isBytes(key))
1006
1206
  key = bytesToHex(key);
@@ -1008,22 +1208,54 @@ function weierstrassPoints(opts) {
1008
1208
  throw new Error("Invalid key");
1009
1209
  key = key.padStart(nByteLength * 2, "0");
1010
1210
  }
1011
- let num;
1211
+ let num2;
1012
1212
  try {
1013
- num = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
1213
+ num2 = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
1014
1214
  } catch (error) {
1015
1215
  throw new Error(`private key must be ${nByteLength} bytes, hex or bigint, not ${typeof key}`);
1016
1216
  }
1017
1217
  if (wrapPrivateKey)
1018
- num = mod(num, n);
1019
- assertGE(num);
1020
- return num;
1218
+ num2 = mod(num2, N);
1219
+ aInRange("private key", num2, _1n4, N);
1220
+ return num2;
1021
1221
  }
1022
- const pointPrecomputes = /* @__PURE__ */ new Map();
1023
1222
  function assertPrjPoint(other) {
1024
1223
  if (!(other instanceof Point2))
1025
1224
  throw new Error("ProjectivePoint expected");
1026
1225
  }
1226
+ const toAffineMemo = memoized((p, iz) => {
1227
+ const { px: x, py: y, pz: z } = p;
1228
+ if (Fp2.eql(z, Fp2.ONE))
1229
+ return { x, y };
1230
+ const is0 = p.is0();
1231
+ if (iz == null)
1232
+ iz = is0 ? Fp2.ONE : Fp2.inv(z);
1233
+ const ax = Fp2.mul(x, iz);
1234
+ const ay = Fp2.mul(y, iz);
1235
+ const zz = Fp2.mul(z, iz);
1236
+ if (is0)
1237
+ return { x: Fp2.ZERO, y: Fp2.ZERO };
1238
+ if (!Fp2.eql(zz, Fp2.ONE))
1239
+ throw new Error("invZ was invalid");
1240
+ return { x: ax, y: ay };
1241
+ });
1242
+ const assertValidMemo = memoized((p) => {
1243
+ if (p.is0()) {
1244
+ if (CURVE.allowInfinityPoint && !Fp2.is0(p.py))
1245
+ return;
1246
+ throw new Error("bad point: ZERO");
1247
+ }
1248
+ const { x, y } = p.toAffine();
1249
+ if (!Fp2.isValid(x) || !Fp2.isValid(y))
1250
+ throw new Error("bad point: x or y not FE");
1251
+ const left = Fp2.sqr(y);
1252
+ const right = weierstrassEquation(x);
1253
+ if (!Fp2.eql(left, right))
1254
+ throw new Error("bad point: equation left != right");
1255
+ if (!p.isTorsionFree())
1256
+ throw new Error("bad point: not in prime-order subgroup");
1257
+ return true;
1258
+ });
1027
1259
  class Point2 {
1028
1260
  constructor(px, py, pz) {
1029
1261
  this.px = px;
@@ -1035,6 +1267,7 @@ function weierstrassPoints(opts) {
1035
1267
  throw new Error("y required");
1036
1268
  if (pz == null || !Fp2.isValid(pz))
1037
1269
  throw new Error("z required");
1270
+ Object.freeze(this);
1038
1271
  }
1039
1272
  // Does not validate if the point is on-curve.
1040
1273
  // Use fromHex instead, or call assertValidity() later.
@@ -1078,27 +1311,17 @@ function weierstrassPoints(opts) {
1078
1311
  static fromPrivateKey(privateKey) {
1079
1312
  return Point2.BASE.multiply(normPrivateKeyToScalar(privateKey));
1080
1313
  }
1314
+ // Multiscalar Multiplication
1315
+ static msm(points, scalars) {
1316
+ return pippenger(Point2, Fn, points, scalars);
1317
+ }
1081
1318
  // "Private method", don't use it directly
1082
1319
  _setWindowSize(windowSize) {
1083
- this._WINDOW_SIZE = windowSize;
1084
- pointPrecomputes.delete(this);
1320
+ wnaf.setWindowSize(this, windowSize);
1085
1321
  }
1086
1322
  // A point on curve is valid if it conforms to equation.
1087
1323
  assertValidity() {
1088
- if (this.is0()) {
1089
- if (CURVE.allowInfinityPoint && !Fp2.is0(this.py))
1090
- return;
1091
- throw new Error("bad point: ZERO");
1092
- }
1093
- const { x, y } = this.toAffine();
1094
- if (!Fp2.isValid(x) || !Fp2.isValid(y))
1095
- throw new Error("bad point: x or y not FE");
1096
- const left = Fp2.sqr(y);
1097
- const right = weierstrassEquation(x);
1098
- if (!Fp2.eql(left, right))
1099
- throw new Error("bad point: equation left != right");
1100
- if (!this.isTorsionFree())
1101
- throw new Error("bad point: not in prime-order subgroup");
1324
+ assertValidMemo(this);
1102
1325
  }
1103
1326
  hasEvenY() {
1104
1327
  const { y } = this.toAffine();
@@ -1225,27 +1448,24 @@ function weierstrassPoints(opts) {
1225
1448
  return this.equals(Point2.ZERO);
1226
1449
  }
1227
1450
  wNAF(n) {
1228
- return wnaf.wNAFCached(this, pointPrecomputes, n, (comp) => {
1229
- const toInv = Fp2.invertBatch(comp.map((p) => p.pz));
1230
- return comp.map((p, i) => p.toAffine(toInv[i])).map(Point2.fromAffine);
1231
- });
1451
+ return wnaf.wNAFCached(this, n, Point2.normalizeZ);
1232
1452
  }
1233
1453
  /**
1234
1454
  * Non-constant-time multiplication. Uses double-and-add algorithm.
1235
1455
  * It's faster, but should only be used when you don't care about
1236
1456
  * an exposed private key e.g. sig verification, which works over *public* keys.
1237
1457
  */
1238
- multiplyUnsafe(n) {
1458
+ multiplyUnsafe(sc) {
1459
+ aInRange("scalar", sc, _0n4, CURVE.n);
1239
1460
  const I = Point2.ZERO;
1240
- if (n === _0n4)
1461
+ if (sc === _0n4)
1241
1462
  return I;
1242
- assertGE(n);
1243
- if (n === _1n4)
1463
+ if (sc === _1n4)
1244
1464
  return this;
1245
1465
  const { endo } = CURVE;
1246
1466
  if (!endo)
1247
- return wnaf.unsafeLadder(this, n);
1248
- let { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
1467
+ return wnaf.unsafeLadder(this, sc);
1468
+ let { k1neg, k1, k2neg, k2 } = endo.splitScalar(sc);
1249
1469
  let k1p = I;
1250
1470
  let k2p = I;
1251
1471
  let d = this;
@@ -1275,12 +1495,11 @@ function weierstrassPoints(opts) {
1275
1495
  * @returns New point
1276
1496
  */
1277
1497
  multiply(scalar) {
1278
- assertGE(scalar);
1279
- let n = scalar;
1498
+ const { endo, n: N } = CURVE;
1499
+ aInRange("scalar", scalar, _1n4, N);
1280
1500
  let point, fake;
1281
- const { endo } = CURVE;
1282
1501
  if (endo) {
1283
- const { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
1502
+ const { k1neg, k1, k2neg, k2 } = endo.splitScalar(scalar);
1284
1503
  let { p: k1p, f: f1p } = this.wNAF(k1);
1285
1504
  let { p: k2p, f: f2p } = this.wNAF(k2);
1286
1505
  k1p = wnaf.constTimeNegate(k1neg, k1p);
@@ -1289,7 +1508,7 @@ function weierstrassPoints(opts) {
1289
1508
  point = k1p.add(k2p);
1290
1509
  fake = f1p.add(f2p);
1291
1510
  } else {
1292
- const { p, f } = this.wNAF(n);
1511
+ const { p, f } = this.wNAF(scalar);
1293
1512
  point = p;
1294
1513
  fake = f;
1295
1514
  }
@@ -1311,18 +1530,7 @@ function weierstrassPoints(opts) {
1311
1530
  // Can accept precomputed Z^-1 - for example, from invertBatch.
1312
1531
  // (x, y, z) ∋ (x=x/z, y=y/z)
1313
1532
  toAffine(iz) {
1314
- const { px: x, py: y, pz: z } = this;
1315
- const is0 = this.is0();
1316
- if (iz == null)
1317
- iz = is0 ? Fp2.ONE : Fp2.inv(z);
1318
- const ax = Fp2.mul(x, iz);
1319
- const ay = Fp2.mul(y, iz);
1320
- const zz = Fp2.mul(z, iz);
1321
- if (is0)
1322
- return { x: Fp2.ZERO, y: Fp2.ZERO };
1323
- if (!Fp2.eql(zz, Fp2.ONE))
1324
- throw new Error("invZ was invalid");
1325
- return { x: ax, y: ay };
1533
+ return toAffineMemo(this, iz);
1326
1534
  }
1327
1535
  isTorsionFree() {
1328
1536
  const { h: cofactor, isTorsionFree } = CURVE;
@@ -1341,10 +1549,12 @@ function weierstrassPoints(opts) {
1341
1549
  return this.multiplyUnsafe(CURVE.h);
1342
1550
  }
1343
1551
  toRawBytes(isCompressed = true) {
1552
+ abool("isCompressed", isCompressed);
1344
1553
  this.assertValidity();
1345
1554
  return toBytes2(Point2, this, isCompressed);
1346
1555
  }
1347
1556
  toHex(isCompressed = true) {
1557
+ abool("isCompressed", isCompressed);
1348
1558
  return bytesToHex(this.toRawBytes(isCompressed));
1349
1559
  }
1350
1560
  }
@@ -1378,9 +1588,6 @@ function weierstrass(curveDef) {
1378
1588
  const { Fp: Fp2, n: CURVE_ORDER } = CURVE;
1379
1589
  const compressedLen = Fp2.BYTES + 1;
1380
1590
  const uncompressedLen = 2 * Fp2.BYTES + 1;
1381
- function isValidFieldElement(num) {
1382
- return _0n4 < num && num < Fp2.ORDER;
1383
- }
1384
1591
  function modN2(a) {
1385
1592
  return mod(a, CURVE_ORDER);
1386
1593
  }
@@ -1393,6 +1600,7 @@ function weierstrass(curveDef) {
1393
1600
  const a = point.toAffine();
1394
1601
  const x = Fp2.toBytes(a.x);
1395
1602
  const cat = concatBytes2;
1603
+ abool("isCompressed", isCompressed);
1396
1604
  if (isCompressed) {
1397
1605
  return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
1398
1606
  } else {
@@ -1405,7 +1613,7 @@ function weierstrass(curveDef) {
1405
1613
  const tail = bytes2.subarray(1);
1406
1614
  if (len === compressedLen && (head === 2 || head === 3)) {
1407
1615
  const x = bytesToNumberBE(tail);
1408
- if (!isValidFieldElement(x))
1616
+ if (!inRange(x, _1n4, Fp2.ORDER))
1409
1617
  throw new Error("Point is not on curve");
1410
1618
  const y2 = weierstrassEquation(x);
1411
1619
  let y;
@@ -1429,7 +1637,7 @@ function weierstrass(curveDef) {
1429
1637
  }
1430
1638
  }
1431
1639
  });
1432
- const numToNByteStr = (num) => bytesToHex(numberToBytesBE(num, CURVE.nByteLength));
1640
+ const numToNByteStr = (num2) => bytesToHex(numberToBytesBE(num2, CURVE.nByteLength));
1433
1641
  function isBiggerThanHalfOrder(number) {
1434
1642
  const HALF = CURVE_ORDER >> _1n4;
1435
1643
  return number > HALF;
@@ -1458,10 +1666,8 @@ function weierstrass(curveDef) {
1458
1666
  return new Signature(r, s);
1459
1667
  }
1460
1668
  assertValidity() {
1461
- if (!isWithinCurveOrder(this.r))
1462
- throw new Error("r must be 0 < r < CURVE.n");
1463
- if (!isWithinCurveOrder(this.s))
1464
- throw new Error("s must be 0 < s < CURVE.n");
1669
+ aInRange("r", this.r, _1n4, CURVE_ORDER);
1670
+ aInRange("s", this.s, _1n4, CURVE_ORDER);
1465
1671
  }
1466
1672
  addRecoveryBit(recovery) {
1467
1673
  return new Signature(this.r, this.s, recovery);
@@ -1563,20 +1769,17 @@ function weierstrass(curveDef) {
1563
1769
  return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
1564
1770
  }
1565
1771
  const bits2int = CURVE.bits2int || function(bytes2) {
1566
- const num = bytesToNumberBE(bytes2);
1772
+ const num2 = bytesToNumberBE(bytes2);
1567
1773
  const delta = bytes2.length * 8 - CURVE.nBitLength;
1568
- return delta > 0 ? num >> BigInt(delta) : num;
1774
+ return delta > 0 ? num2 >> BigInt(delta) : num2;
1569
1775
  };
1570
1776
  const bits2int_modN = CURVE.bits2int_modN || function(bytes2) {
1571
1777
  return modN2(bits2int(bytes2));
1572
1778
  };
1573
1779
  const ORDER_MASK = bitMask(CURVE.nBitLength);
1574
- function int2octets(num) {
1575
- if (typeof num !== "bigint")
1576
- throw new Error("bigint expected");
1577
- if (!(_0n4 <= num && num < ORDER_MASK))
1578
- throw new Error(`bigint expected < 2^${CURVE.nBitLength}`);
1579
- return numberToBytesBE(num, CURVE.nByteLength);
1780
+ function int2octets(num2) {
1781
+ aInRange(`num < 2^${CURVE.nBitLength}`, num2, _0n4, ORDER_MASK);
1782
+ return numberToBytesBE(num2, CURVE.nByteLength);
1580
1783
  }
1581
1784
  function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
1582
1785
  if (["recovered", "canonical"].some((k) => k in opts))
@@ -1586,6 +1789,7 @@ function weierstrass(curveDef) {
1586
1789
  if (lowS == null)
1587
1790
  lowS = true;
1588
1791
  msgHash = ensureBytes("msgHash", msgHash);
1792
+ validateSigVerOpts(opts);
1589
1793
  if (prehash)
1590
1794
  msgHash = ensureBytes("prehashed msgHash", hash2(msgHash));
1591
1795
  const h1int = bits2int_modN(msgHash);
@@ -1634,6 +1838,7 @@ function weierstrass(curveDef) {
1634
1838
  publicKey = ensureBytes("publicKey", publicKey);
1635
1839
  if ("strict" in opts)
1636
1840
  throw new Error("options.strict was renamed to lowS");
1841
+ validateSigVerOpts(opts);
1637
1842
  const { lowS, prehash } = opts;
1638
1843
  let _sig = void 0;
1639
1844
  let P;
@@ -1784,9 +1989,24 @@ function mapToCurveSimpleSWU(Fp2, opts) {
1784
1989
  };
1785
1990
  }
1786
1991
 
1787
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/abstract/hash-to-curve.js
1992
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/_shortw_utils.js
1993
+ function getHash(hash2) {
1994
+ return {
1995
+ hash: hash2,
1996
+ hmac: (key, ...msgs) => hmac(hash2, key, concatBytes(...msgs)),
1997
+ randomBytes
1998
+ };
1999
+ }
2000
+ function createCurve(curveDef, defHash) {
2001
+ const create = (hash2) => weierstrass({ ...curveDef, ...getHash(hash2) });
2002
+ return Object.freeze({ ...create(defHash), create });
2003
+ }
2004
+
2005
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/abstract/hash-to-curve.js
1788
2006
  var os2ip = bytesToNumberBE;
1789
2007
  function i2osp(value, length) {
2008
+ anum(value);
2009
+ anum(length);
1790
2010
  if (value < 0 || value >= 1 << 8 * length) {
1791
2011
  throw new Error(`bad I2OSP call: value=${value} length=${length}`);
1792
2012
  }
@@ -1816,8 +2036,8 @@ function expand_message_xmd(msg, DST, lenInBytes, H) {
1816
2036
  DST = H(concatBytes2(utf8ToBytes("H2C-OVERSIZE-DST-"), DST));
1817
2037
  const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
1818
2038
  const ell = Math.ceil(lenInBytes / b_in_bytes);
1819
- if (ell > 255)
1820
- throw new Error("Invalid xmd length");
2039
+ if (lenInBytes > 65535 || ell > 255)
2040
+ throw new Error("expand_message_xmd: invalid lenInBytes");
1821
2041
  const DST_prime = concatBytes2(DST, i2osp(DST.length, 1));
1822
2042
  const Z_pad = i2osp(0, r_in_bytes);
1823
2043
  const l_i_b_str = i2osp(lenInBytes, 2);
@@ -1910,89 +2130,22 @@ function createHasher(Point2, mapToCurve, def) {
1910
2130
  const P = Point2.fromAffine(mapToCurve(u[0])).clearCofactor();
1911
2131
  P.assertValidity();
1912
2132
  return P;
2133
+ },
2134
+ // Same as encodeToCurve, but without hash
2135
+ mapToCurve(scalars) {
2136
+ if (!Array.isArray(scalars))
2137
+ throw new Error("mapToCurve: expected array of bigints");
2138
+ for (const i of scalars)
2139
+ if (typeof i !== "bigint")
2140
+ throw new Error(`mapToCurve: expected array of bigints, got ${i} in array`);
2141
+ const P = Point2.fromAffine(mapToCurve(scalars)).clearCofactor();
2142
+ P.assertValidity();
2143
+ return P;
1913
2144
  }
1914
2145
  };
1915
2146
  }
1916
2147
 
1917
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/hmac.js
1918
- var HMAC = class extends Hash {
1919
- constructor(hash2, _key) {
1920
- super();
1921
- this.finished = false;
1922
- this.destroyed = false;
1923
- hash(hash2);
1924
- const key = toBytes(_key);
1925
- this.iHash = hash2.create();
1926
- if (typeof this.iHash.update !== "function")
1927
- throw new Error("Expected instance of class which extends utils.Hash");
1928
- this.blockLen = this.iHash.blockLen;
1929
- this.outputLen = this.iHash.outputLen;
1930
- const blockLen = this.blockLen;
1931
- const pad = new Uint8Array(blockLen);
1932
- pad.set(key.length > blockLen ? hash2.create().update(key).digest() : key);
1933
- for (let i = 0; i < pad.length; i++)
1934
- pad[i] ^= 54;
1935
- this.iHash.update(pad);
1936
- this.oHash = hash2.create();
1937
- for (let i = 0; i < pad.length; i++)
1938
- pad[i] ^= 54 ^ 92;
1939
- this.oHash.update(pad);
1940
- pad.fill(0);
1941
- }
1942
- update(buf) {
1943
- exists(this);
1944
- this.iHash.update(buf);
1945
- return this;
1946
- }
1947
- digestInto(out) {
1948
- exists(this);
1949
- bytes(out, this.outputLen);
1950
- this.finished = true;
1951
- this.iHash.digestInto(out);
1952
- this.oHash.update(out);
1953
- this.oHash.digestInto(out);
1954
- this.destroy();
1955
- }
1956
- digest() {
1957
- const out = new Uint8Array(this.oHash.outputLen);
1958
- this.digestInto(out);
1959
- return out;
1960
- }
1961
- _cloneInto(to) {
1962
- to || (to = Object.create(Object.getPrototypeOf(this), {}));
1963
- const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
1964
- to = to;
1965
- to.finished = finished;
1966
- to.destroyed = destroyed;
1967
- to.blockLen = blockLen;
1968
- to.outputLen = outputLen;
1969
- to.oHash = oHash._cloneInto(to.oHash);
1970
- to.iHash = iHash._cloneInto(to.iHash);
1971
- return to;
1972
- }
1973
- destroy() {
1974
- this.destroyed = true;
1975
- this.oHash.destroy();
1976
- this.iHash.destroy();
1977
- }
1978
- };
1979
- var hmac = (hash2, key, message) => new HMAC(hash2, key).update(message).digest();
1980
- hmac.create = (hash2, key) => new HMAC(hash2, key);
1981
-
1982
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/_shortw_utils.js
1983
- function getHash(hash2) {
1984
- return {
1985
- hash: hash2,
1986
- hmac: (key, ...msgs) => hmac(hash2, key, concatBytes(...msgs)),
1987
- randomBytes
1988
- };
1989
- }
1990
- function createCurve(curveDef, defHash) {
1991
- const create = (hash2) => weierstrass({ ...curveDef, ...getHash(hash2) });
1992
- return Object.freeze({ ...create(defHash), create });
1993
- }
1994
-
1995
- // ../../node_modules/.pnpm/@noble+curves@1.4.0/node_modules/@noble/curves/esm/secp256k1.js
2148
+ // ../../node_modules/.pnpm/@noble+curves@1.6.0/node_modules/@noble/curves/esm/secp256k1.js
1996
2149
  var secp256k1P = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
1997
2150
  var secp256k1N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
1998
2151
  var _1n5 = BigInt(1);
@@ -2070,8 +2223,6 @@ var secp256k1 = createCurve({
2070
2223
  }
2071
2224
  }, sha256);
2072
2225
  var _0n5 = BigInt(0);
2073
- var fe = (x) => typeof x === "bigint" && _0n5 < x && x < secp256k1P;
2074
- var ge = (x) => typeof x === "bigint" && _0n5 < x && x < secp256k1N;
2075
2226
  var TAGGED_HASH_PREFIXES = {};
2076
2227
  function taggedHash(tag, ...messages) {
2077
2228
  let tagP = TAGGED_HASH_PREFIXES[tag];
@@ -2095,8 +2246,7 @@ function schnorrGetExtPubKey(priv) {
2095
2246
  return { scalar, bytes: pointToBytes(p) };
2096
2247
  }
2097
2248
  function lift_x(x) {
2098
- if (!fe(x))
2099
- throw new Error("bad x: need 0 < x < p");
2249
+ aInRange("x", x, _1n5, secp256k1P);
2100
2250
  const xx = modP(x * x);
2101
2251
  const c = modP(xx * x + BigInt(7));
2102
2252
  let y = sqrtMod(c);
@@ -2106,8 +2256,9 @@ function lift_x(x) {
2106
2256
  p.assertValidity();
2107
2257
  return p;
2108
2258
  }
2259
+ var num = bytesToNumberBE;
2109
2260
  function challenge(...args) {
2110
- return modN(bytesToNumberBE(taggedHash("BIP0340/challenge", ...args)));
2261
+ return modN(num(taggedHash("BIP0340/challenge", ...args)));
2111
2262
  }
2112
2263
  function schnorrGetPublicKey(privateKey) {
2113
2264
  return schnorrGetExtPubKey(privateKey).bytes;
@@ -2116,9 +2267,9 @@ function schnorrSign(message, privateKey, auxRand = randomBytes(32)) {
2116
2267
  const m = ensureBytes("message", message);
2117
2268
  const { bytes: px, scalar: d } = schnorrGetExtPubKey(privateKey);
2118
2269
  const a = ensureBytes("auxRand", auxRand, 32);
2119
- const t = numTo32b(d ^ bytesToNumberBE(taggedHash("BIP0340/aux", a)));
2270
+ const t = numTo32b(d ^ num(taggedHash("BIP0340/aux", a)));
2120
2271
  const rand = taggedHash("BIP0340/nonce", t, px, m);
2121
- const k_ = modN(bytesToNumberBE(rand));
2272
+ const k_ = modN(num(rand));
2122
2273
  if (k_ === _0n5)
2123
2274
  throw new Error("sign failed: k is zero");
2124
2275
  const { bytes: rx, scalar: k } = schnorrGetExtPubKey(k_);
@@ -2135,12 +2286,12 @@ function schnorrVerify(signature, message, publicKey) {
2135
2286
  const m = ensureBytes("message", message);
2136
2287
  const pub = ensureBytes("publicKey", publicKey, 32);
2137
2288
  try {
2138
- const P = lift_x(bytesToNumberBE(pub));
2139
- const r = bytesToNumberBE(sig.subarray(0, 32));
2140
- if (!fe(r))
2289
+ const P = lift_x(num(pub));
2290
+ const r = num(sig.subarray(0, 32));
2291
+ if (!inRange(r, _1n5, secp256k1P))
2141
2292
  return false;
2142
- const s = bytesToNumberBE(sig.subarray(32, 64));
2143
- if (!ge(s))
2293
+ const s = num(sig.subarray(32, 64));
2294
+ if (!inRange(s, _1n5, secp256k1N))
2144
2295
  return false;
2145
2296
  const e = challenge(numTo32b(r), pointToBytes(P), m);
2146
2297
  const R = GmulAdd(P, s, modN(-e));
@@ -2241,4 +2392,4 @@ export {
2241
2392
  @noble/curves/esm/secp256k1.js:
2242
2393
  (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2243
2394
  */
2244
- //# sourceMappingURL=secp256k1-5CEPNAX3.js.map
2395
+ //# sourceMappingURL=secp256k1-NJATWSDZ.js.map