@t2000/mcp 5.17.0 → 5.19.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.
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { createRequire } from 'module';
2
2
  import url, { fileURLToPath } from 'url';
3
3
  import { dirname, join as join$1, resolve } from 'path';
4
- import crypto2 from 'crypto';
4
+ import * as nc from 'crypto';
5
+ import nc__default from 'crypto';
5
6
  import stream3, { Readable } from 'stream';
6
7
  import util3 from 'util';
7
8
  import http from 'http';
@@ -30,8 +31,8 @@ var __require = /* @__PURE__ */ ((x) => typeof require$1 !== "undefined" ? requi
30
31
  var __esm = (fn, res) => function __init() {
31
32
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
32
33
  };
33
- var __commonJS = (cb, mod3) => function __require3() {
34
- return mod3 || (0, cb[__getOwnPropNames(cb)[0]])((mod3 = { exports: {} }).exports, mod3), mod3.exports;
34
+ var __commonJS = (cb, mod4) => function __require3() {
35
+ return mod4 || (0, cb[__getOwnPropNames(cb)[0]])((mod4 = { exports: {} }).exports, mod4), mod4.exports;
35
36
  };
36
37
  var __export = (target, all3) => {
37
38
  for (var name in all3)
@@ -45,13 +46,13 @@ var __copyProps = (to, from, except, desc) => {
45
46
  }
46
47
  return to;
47
48
  };
48
- var __toESM = (mod3, isNodeMode, target) => (target = mod3 != null ? __create(__getProtoOf(mod3)) : {}, __copyProps(
49
+ var __toESM = (mod4, isNodeMode, target) => (target = mod4 != null ? __create(__getProtoOf(mod4)) : {}, __copyProps(
49
50
  // If the importer is in node compatibility mode or this is not an ESM
50
51
  // file that has been converted to a CommonJS file using a Babel-
51
52
  // compatible transform (i.e. "__esModule" has not been set), then set
52
53
  // "default" to the CommonJS "module.exports" for node compatibility.
53
- __defProp(target, "default", { value: mod3, enumerable: true }) ,
54
- mod3
54
+ __defProp(target, "default", { value: mod4, enumerable: true }) ,
55
+ mod4
55
56
  ));
56
57
 
57
58
  // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/util.js
@@ -11652,7 +11653,7 @@ function dynamicSizeBcsType({ serialize, ...options }) {
11652
11653
  });
11653
11654
  return type;
11654
11655
  }
11655
- function stringLikeBcsType({ toBytes, fromBytes, ...options }) {
11656
+ function stringLikeBcsType({ toBytes: toBytes2, fromBytes, ...options }) {
11656
11657
  return new BcsType({
11657
11658
  ...options,
11658
11659
  read: (reader) => {
@@ -11660,12 +11661,12 @@ function stringLikeBcsType({ toBytes, fromBytes, ...options }) {
11660
11661
  return fromBytes(reader.readBytes(length));
11661
11662
  },
11662
11663
  write: (hex, writer) => {
11663
- const bytes = toBytes(hex);
11664
+ const bytes = toBytes2(hex);
11664
11665
  writer.writeULEB(bytes.length);
11665
11666
  for (let i = 0; i < bytes.length; i++) writer.write8(bytes[i]);
11666
11667
  },
11667
11668
  serialize: (value) => {
11668
- const bytes = toBytes(value);
11669
+ const bytes = toBytes2(value);
11669
11670
  const size = ulebEncode(bytes.length);
11670
11671
  const result = new Uint8Array(size.length + bytes.length);
11671
11672
  result.set(size, 0);
@@ -14883,7 +14884,7 @@ function serializeV1TransactionData(transactionData) {
14883
14884
  };
14884
14885
  if (command.Publish) return {
14885
14886
  kind: "Publish",
14886
- modules: command.Publish.modules.map((mod3) => Array.from(fromBase64(mod3))),
14887
+ modules: command.Publish.modules.map((mod4) => Array.from(fromBase64(mod4))),
14887
14888
  dependencies: command.Publish.dependencies
14888
14889
  };
14889
14890
  if (command.SplitCoins) return {
@@ -14898,7 +14899,7 @@ function serializeV1TransactionData(transactionData) {
14898
14899
  };
14899
14900
  if (command.Upgrade) return {
14900
14901
  kind: "Upgrade",
14901
- modules: command.Upgrade.modules.map((mod3) => Array.from(fromBase64(mod3))),
14902
+ modules: command.Upgrade.modules.map((mod4) => Array.from(fromBase64(mod4))),
14902
14903
  dependencies: command.Upgrade.dependencies,
14903
14904
  packageId: command.Upgrade.package,
14904
14905
  ticket: convertTransactionArgument(command.Upgrade.ticket, inputs)
@@ -14978,10 +14979,10 @@ function transactionDataFromV1(data) {
14978
14979
  sources: transaction.sources.map((arg) => parseV1TransactionArgument(arg))
14979
14980
  } };
14980
14981
  case "MoveCall": {
14981
- const [pkg, mod3, fn] = transaction.target.split("::");
14982
+ const [pkg, mod4, fn] = transaction.target.split("::");
14982
14983
  return { MoveCall: {
14983
14984
  package: pkg,
14984
- module: mod3,
14985
+ module: mod4,
14985
14986
  function: fn,
14986
14987
  typeArguments: transaction.typeArguments,
14987
14988
  arguments: transaction.arguments.map((arg) => parseV1TransactionArgument(arg))
@@ -14989,7 +14990,7 @@ function transactionDataFromV1(data) {
14989
14990
  }
14990
14991
  case "Publish":
14991
14992
  return { Publish: {
14992
- modules: transaction.modules.map((mod3) => toBase64(Uint8Array.from(mod3))),
14993
+ modules: transaction.modules.map((mod4) => toBase64(Uint8Array.from(mod4))),
14993
14994
  dependencies: transaction.dependencies
14994
14995
  } };
14995
14996
  case "SplitCoins":
@@ -15004,7 +15005,7 @@ function transactionDataFromV1(data) {
15004
15005
  } };
15005
15006
  case "Upgrade":
15006
15007
  return { Upgrade: {
15007
- modules: transaction.modules.map((mod3) => toBase64(Uint8Array.from(mod3))),
15008
+ modules: transaction.modules.map((mod4) => toBase64(Uint8Array.from(mod4))),
15008
15009
  dependencies: transaction.dependencies,
15009
15010
  package: transaction.packageId,
15010
15011
  ticket: parseV1TransactionArgument(transaction.ticket)
@@ -15392,7 +15393,7 @@ var init_Commands = __esm({
15392
15393
  init_dist3();
15393
15394
  TransactionCommands = {
15394
15395
  MoveCall(input) {
15395
- const [pkg, mod3 = "", fn = ""] = "target" in input ? input.target.split("::") : [
15396
+ const [pkg, mod4 = "", fn = ""] = "target" in input ? input.target.split("::") : [
15396
15397
  input.package,
15397
15398
  input.module,
15398
15399
  input.function
@@ -15401,7 +15402,7 @@ var init_Commands = __esm({
15401
15402
  $kind: "MoveCall",
15402
15403
  MoveCall: {
15403
15404
  package: pkg,
15404
- module: mod3,
15405
+ module: mod4,
15405
15406
  function: fn,
15406
15407
  typeArguments: input.typeArguments ?? [],
15407
15408
  arguments: input.arguments ?? []
@@ -42405,10 +42406,10 @@ var require_abort = __commonJS({
42405
42406
  "../../node_modules/.pnpm/asynckit@0.4.0/node_modules/asynckit/lib/abort.js"(exports$1, module) {
42406
42407
  module.exports = abort;
42407
42408
  function abort(state) {
42408
- Object.keys(state.jobs).forEach(clean2.bind(state));
42409
+ Object.keys(state.jobs).forEach(clean3.bind(state));
42409
42410
  state.jobs = {};
42410
42411
  }
42411
- function clean2(key) {
42412
+ function clean3(key) {
42412
42413
  if (typeof this.jobs[key] == "function") {
42413
42414
  this.jobs[key]();
42414
42415
  }
@@ -43404,7 +43405,7 @@ var require_form_data = __commonJS({
43404
43405
  var parseUrl2 = __require("url").parse;
43405
43406
  var fs = __require("fs");
43406
43407
  var Stream = __require("stream").Stream;
43407
- var crypto3 = __require("crypto");
43408
+ var crypto4 = __require("crypto");
43408
43409
  var mime = require_mime_types();
43409
43410
  var asynckit = require_asynckit();
43410
43411
  var setToStringTag = require_es_set_tostringtag();
@@ -43610,7 +43611,7 @@ var require_form_data = __commonJS({
43610
43611
  return Buffer.concat([dataBuffer, Buffer.from(this._lastBoundary())]);
43611
43612
  };
43612
43613
  FormData3.prototype._generateBoundary = function() {
43613
- this._boundary = "--------------------------" + crypto3.randomBytes(12).toString("hex");
43614
+ this._boundary = "--------------------------" + crypto4.randomBytes(12).toString("hex");
43614
43615
  };
43615
43616
  FormData3.prototype.getLengthSync = function() {
43616
43617
  var knownLength = this._overheadLength + this._valueLength;
@@ -44031,7 +44032,7 @@ var init_node = __esm({
44031
44032
  let str = "";
44032
44033
  const { length } = alphabet2;
44033
44034
  const randomValues = new Uint32Array(size);
44034
- crypto2.randomFillSync(randomValues);
44035
+ nc__default.randomFillSync(randomValues);
44035
44036
  for (let i = 0; i < size; i++) {
44036
44037
  str += alphabet2[randomValues[i] % length];
44037
44038
  }
@@ -50515,7 +50516,7 @@ function maxOrMin(Ctor, args, n) {
50515
50516
  return x;
50516
50517
  }
50517
50518
  function naturalExponential(x, sd) {
50518
- var denominator, guard, j, pow22, sum2, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
50519
+ var denominator, guard, j, pow23, sum2, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
50519
50520
  if (!x.d || !x.d[0] || x.e > 17) {
50520
50521
  return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : 0 / 0);
50521
50522
  }
@@ -50532,19 +50533,19 @@ function naturalExponential(x, sd) {
50532
50533
  }
50533
50534
  guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
50534
50535
  wpr += guard;
50535
- denominator = pow22 = sum2 = new Ctor(1);
50536
+ denominator = pow23 = sum2 = new Ctor(1);
50536
50537
  Ctor.precision = wpr;
50537
50538
  for (; ; ) {
50538
- pow22 = finalise(pow22.times(x), wpr, 1);
50539
+ pow23 = finalise(pow23.times(x), wpr, 1);
50539
50540
  denominator = denominator.times(++i);
50540
- t = sum2.plus(divide(pow22, denominator, wpr, 1));
50541
+ t = sum2.plus(divide(pow23, denominator, wpr, 1));
50541
50542
  if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
50542
50543
  j = k;
50543
50544
  while (j--) sum2 = finalise(sum2.times(sum2), wpr, 1);
50544
50545
  if (sd == null) {
50545
50546
  if (rep < 3 && checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
50546
50547
  Ctor.precision = wpr += 10;
50547
- denominator = pow22 = t = new Ctor(1);
50548
+ denominator = pow23 = t = new Ctor(1);
50548
50549
  i = 0;
50549
50550
  rep++;
50550
50551
  } else {
@@ -51791,12 +51792,12 @@ var init_dist7 = __esm({
51791
51792
  }
51792
51793
  }
51793
51794
  if (mod22 !== 0) {
51794
- var pow22 = 1;
51795
+ var pow23 = 1;
51795
51796
  word = parseBase(number4, i, number4.length, base);
51796
51797
  for (i = 0; i < mod22; i++) {
51797
- pow22 *= base;
51798
+ pow23 *= base;
51798
51799
  }
51799
- this.imuln(pow22);
51800
+ this.imuln(pow23);
51800
51801
  if (this.words[0] + word < 67108864) {
51801
51802
  this.words[0] += word;
51802
51803
  } else {
@@ -53131,7 +53132,7 @@ var init_dist7 = __esm({
53131
53132
  BN7.prototype.isqr = function isqr() {
53132
53133
  return this.imul(this.clone());
53133
53134
  };
53134
- BN7.prototype.pow = function pow22(num) {
53135
+ BN7.prototype.pow = function pow23(num) {
53135
53136
  var w2 = toBitArray(num);
53136
53137
  if (w2.length === 0) return new BN7(1);
53137
53138
  var res = this;
@@ -54125,11 +54126,11 @@ var init_dist7 = __esm({
54125
54126
  };
54126
54127
  Red.prototype.sqrt = function sqrt2(a) {
54127
54128
  if (a.isZero()) return a.clone();
54128
- var mod3 = this.m.andln(3);
54129
- assert3(mod3 % 2 === 1);
54130
- if (mod3 === 3) {
54131
- var pow22 = this.m.add(new BN7(1)).iushrn(2);
54132
- return this.pow(a, pow22);
54129
+ var mod32 = this.m.andln(3);
54130
+ assert3(mod32 % 2 === 1);
54131
+ if (mod32 === 3) {
54132
+ var pow23 = this.m.add(new BN7(1)).iushrn(2);
54133
+ return this.pow(a, pow23);
54133
54134
  }
54134
54135
  var q = this.m.subn(1);
54135
54136
  var s = 0;
@@ -54173,7 +54174,7 @@ var init_dist7 = __esm({
54173
54174
  return this.imod(inv);
54174
54175
  }
54175
54176
  };
54176
- Red.prototype.pow = function pow22(a, num) {
54177
+ Red.prototype.pow = function pow23(a, num) {
54177
54178
  if (num.isZero()) return new BN7(1).toRed(this);
54178
54179
  if (num.cmpn(1) === 0) return a.clone();
54179
54180
  var windowSize = 4;
@@ -58968,8 +58969,8 @@ var require_bn2 = __commonJS({
58968
58969
  limbLen--;
58969
58970
  limbPow = limbPow / base | 0;
58970
58971
  var total = number4.length - start;
58971
- var mod3 = total % limbLen;
58972
- var end = Math.min(total, total - mod3) + start;
58972
+ var mod4 = total % limbLen;
58973
+ var end = Math.min(total, total - mod4) + start;
58973
58974
  var word = 0;
58974
58975
  for (var i = start; i < end; i += limbLen) {
58975
58976
  word = parseBase(number4, i, i + limbLen, base);
@@ -58980,10 +58981,10 @@ var require_bn2 = __commonJS({
58980
58981
  this._iaddn(word);
58981
58982
  }
58982
58983
  }
58983
- if (mod3 !== 0) {
58984
+ if (mod4 !== 0) {
58984
58985
  var pow3 = 1;
58985
58986
  word = parseBase(number4, i, number4.length, base);
58986
- for (i = 0; i < mod3; i++) {
58987
+ for (i = 0; i < mod4; i++) {
58987
58988
  pow3 *= base;
58988
58989
  }
58989
58990
  this.imuln(pow3);
@@ -60623,21 +60624,21 @@ var require_bn2 = __commonJS({
60623
60624
  mod: new BN2(0)
60624
60625
  };
60625
60626
  }
60626
- var div2, mod3, res;
60627
+ var div2, mod4, res;
60627
60628
  if (this.negative !== 0 && num.negative === 0) {
60628
60629
  res = this.neg().divmod(num, mode);
60629
60630
  if (mode !== "mod") {
60630
60631
  div2 = res.div.neg();
60631
60632
  }
60632
60633
  if (mode !== "div") {
60633
- mod3 = res.mod.neg();
60634
- if (positive && mod3.negative !== 0) {
60635
- mod3.iadd(num);
60634
+ mod4 = res.mod.neg();
60635
+ if (positive && mod4.negative !== 0) {
60636
+ mod4.iadd(num);
60636
60637
  }
60637
60638
  }
60638
60639
  return {
60639
60640
  div: div2,
60640
- mod: mod3
60641
+ mod: mod4
60641
60642
  };
60642
60643
  }
60643
60644
  if (this.negative === 0 && num.negative !== 0) {
@@ -60653,14 +60654,14 @@ var require_bn2 = __commonJS({
60653
60654
  if ((this.negative & num.negative) !== 0) {
60654
60655
  res = this.neg().divmod(num.neg(), mode);
60655
60656
  if (mode !== "div") {
60656
- mod3 = res.mod.neg();
60657
- if (positive && mod3.negative !== 0) {
60658
- mod3.isub(num);
60657
+ mod4 = res.mod.neg();
60658
+ if (positive && mod4.negative !== 0) {
60659
+ mod4.isub(num);
60659
60660
  }
60660
60661
  }
60661
60662
  return {
60662
60663
  div: res.div,
60663
- mod: mod3
60664
+ mod: mod4
60664
60665
  };
60665
60666
  }
60666
60667
  if (num.length > this.length || this.cmp(num) < 0) {
@@ -60692,7 +60693,7 @@ var require_bn2 = __commonJS({
60692
60693
  BN2.prototype.div = function div2(num) {
60693
60694
  return this.divmod(num, "div", false).div;
60694
60695
  };
60695
- BN2.prototype.mod = function mod3(num) {
60696
+ BN2.prototype.mod = function mod4(num) {
60696
60697
  return this.divmod(num, "mod", false).mod;
60697
60698
  };
60698
60699
  BN2.prototype.umod = function umod(num) {
@@ -60701,10 +60702,10 @@ var require_bn2 = __commonJS({
60701
60702
  BN2.prototype.divRound = function divRound(num) {
60702
60703
  var dm = this.divmod(num);
60703
60704
  if (dm.mod.isZero()) return dm.div;
60704
- var mod3 = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
60705
+ var mod4 = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
60705
60706
  var half = num.ushrn(1);
60706
60707
  var r2 = num.andln(1);
60707
- var cmp = mod3.cmp(half);
60708
+ var cmp = mod4.cmp(half);
60708
60709
  if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div;
60709
60710
  return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
60710
60711
  };
@@ -61318,9 +61319,9 @@ var require_bn2 = __commonJS({
61318
61319
  };
61319
61320
  Red.prototype.sqrt = function sqrt2(a) {
61320
61321
  if (a.isZero()) return a.clone();
61321
- var mod3 = this.m.andln(3);
61322
- assert3(mod3 % 2 === 1);
61323
- if (mod3 === 3) {
61322
+ var mod32 = this.m.andln(3);
61323
+ assert3(mod32 % 2 === 1);
61324
+ if (mod32 === 3) {
61324
61325
  var pow3 = this.m.add(new BN2(1)).iushrn(2);
61325
61326
  return this.pow(a, pow3);
61326
61327
  }
@@ -76998,7 +76999,7 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
76998
76999
  });
76999
77000
  const { prehash } = eddsaOpts;
77000
77001
  const { BASE: BASE2, Fp, Fn } = Point;
77001
- const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
77002
+ const randomBytes3 = eddsaOpts.randomBytes || randomBytes;
77002
77003
  const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
77003
77004
  const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
77004
77005
  abool(phflag, "phflag");
@@ -77080,7 +77081,7 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
77080
77081
  signature: 2 * _size2,
77081
77082
  seed: _size2
77082
77083
  };
77083
- function randomSecretKey(seed = randomBytes2(lengths.seed)) {
77084
+ function randomSecretKey(seed = randomBytes3(lengths.seed)) {
77084
77085
  return abytes(seed, lengths.seed, "seed");
77085
77086
  }
77086
77087
  function isValidSecretKey(key) {
@@ -77648,6 +77649,2310 @@ var Ed25519Keypair = class Ed25519Keypair2 extends Keypair {
77648
77649
  return Ed25519Keypair2.fromSecretKey(key);
77649
77650
  }
77650
77651
  };
77652
+ var crypto3 = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : nc && typeof nc === "object" && "randomBytes" in nc ? nc : void 0;
77653
+
77654
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/utils.js
77655
+ function isBytes3(a) {
77656
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
77657
+ }
77658
+ function anumber3(n) {
77659
+ if (!Number.isSafeInteger(n) || n < 0)
77660
+ throw new Error("positive integer expected, got " + n);
77661
+ }
77662
+ function abytes2(b, ...lengths) {
77663
+ if (!isBytes3(b))
77664
+ throw new Error("Uint8Array expected");
77665
+ if (lengths.length > 0 && !lengths.includes(b.length))
77666
+ throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
77667
+ }
77668
+ function ahash2(h) {
77669
+ if (typeof h !== "function" || typeof h.create !== "function")
77670
+ throw new Error("Hash should be wrapped by utils.createHasher");
77671
+ anumber3(h.outputLen);
77672
+ anumber3(h.blockLen);
77673
+ }
77674
+ function aexists2(instance, checkFinished = true) {
77675
+ if (instance.destroyed)
77676
+ throw new Error("Hash instance has been destroyed");
77677
+ if (checkFinished && instance.finished)
77678
+ throw new Error("Hash#digest() has already been called");
77679
+ }
77680
+ function aoutput2(out, instance) {
77681
+ abytes2(out);
77682
+ const min2 = instance.outputLen;
77683
+ if (out.length < min2) {
77684
+ throw new Error("digestInto() expects output buffer of length at least " + min2);
77685
+ }
77686
+ }
77687
+ function clean2(...arrays) {
77688
+ for (let i = 0; i < arrays.length; i++) {
77689
+ arrays[i].fill(0);
77690
+ }
77691
+ }
77692
+ function createView2(arr) {
77693
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
77694
+ }
77695
+ function rotr2(word, shift) {
77696
+ return word << 32 - shift | word >>> shift;
77697
+ }
77698
+ var hasHexBuiltin2 = /* @__PURE__ */ (() => (
77699
+ // @ts-ignore
77700
+ typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
77701
+ ))();
77702
+ var hexes2 = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
77703
+ function bytesToHex2(bytes) {
77704
+ abytes2(bytes);
77705
+ if (hasHexBuiltin2)
77706
+ return bytes.toHex();
77707
+ let hex = "";
77708
+ for (let i = 0; i < bytes.length; i++) {
77709
+ hex += hexes2[bytes[i]];
77710
+ }
77711
+ return hex;
77712
+ }
77713
+ var asciis2 = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
77714
+ function asciiToBase162(ch) {
77715
+ if (ch >= asciis2._0 && ch <= asciis2._9)
77716
+ return ch - asciis2._0;
77717
+ if (ch >= asciis2.A && ch <= asciis2.F)
77718
+ return ch - (asciis2.A - 10);
77719
+ if (ch >= asciis2.a && ch <= asciis2.f)
77720
+ return ch - (asciis2.a - 10);
77721
+ return;
77722
+ }
77723
+ function hexToBytes2(hex) {
77724
+ if (typeof hex !== "string")
77725
+ throw new Error("hex string expected, got " + typeof hex);
77726
+ if (hasHexBuiltin2)
77727
+ return Uint8Array.fromHex(hex);
77728
+ const hl = hex.length;
77729
+ const al = hl / 2;
77730
+ if (hl % 2)
77731
+ throw new Error("hex string expected, got unpadded hex of length " + hl);
77732
+ const array3 = new Uint8Array(al);
77733
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
77734
+ const n1 = asciiToBase162(hex.charCodeAt(hi));
77735
+ const n2 = asciiToBase162(hex.charCodeAt(hi + 1));
77736
+ if (n1 === void 0 || n2 === void 0) {
77737
+ const char = hex[hi] + hex[hi + 1];
77738
+ throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
77739
+ }
77740
+ array3[ai] = n1 * 16 + n2;
77741
+ }
77742
+ return array3;
77743
+ }
77744
+ function utf8ToBytes2(str) {
77745
+ if (typeof str !== "string")
77746
+ throw new Error("string expected");
77747
+ return new Uint8Array(new TextEncoder().encode(str));
77748
+ }
77749
+ function toBytes(data) {
77750
+ if (typeof data === "string")
77751
+ data = utf8ToBytes2(data);
77752
+ abytes2(data);
77753
+ return data;
77754
+ }
77755
+ function concatBytes3(...arrays) {
77756
+ let sum2 = 0;
77757
+ for (let i = 0; i < arrays.length; i++) {
77758
+ const a = arrays[i];
77759
+ abytes2(a);
77760
+ sum2 += a.length;
77761
+ }
77762
+ const res = new Uint8Array(sum2);
77763
+ for (let i = 0, pad = 0; i < arrays.length; i++) {
77764
+ const a = arrays[i];
77765
+ res.set(a, pad);
77766
+ pad += a.length;
77767
+ }
77768
+ return res;
77769
+ }
77770
+ var Hash = class {
77771
+ };
77772
+ function createHasher2(hashCons) {
77773
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
77774
+ const tmp = hashCons();
77775
+ hashC.outputLen = tmp.outputLen;
77776
+ hashC.blockLen = tmp.blockLen;
77777
+ hashC.create = () => hashCons();
77778
+ return hashC;
77779
+ }
77780
+ function randomBytes2(bytesLength = 32) {
77781
+ if (crypto3 && typeof crypto3.getRandomValues === "function") {
77782
+ return crypto3.getRandomValues(new Uint8Array(bytesLength));
77783
+ }
77784
+ if (crypto3 && typeof crypto3.randomBytes === "function") {
77785
+ return Uint8Array.from(crypto3.randomBytes(bytesLength));
77786
+ }
77787
+ throw new Error("crypto.getRandomValues must be defined");
77788
+ }
77789
+
77790
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_md.js
77791
+ function setBigUint64(view, byteOffset, value, isLE2) {
77792
+ if (typeof view.setBigUint64 === "function")
77793
+ return view.setBigUint64(byteOffset, value, isLE2);
77794
+ const _32n2 = BigInt(32);
77795
+ const _u32_max = BigInt(4294967295);
77796
+ const wh = Number(value >> _32n2 & _u32_max);
77797
+ const wl = Number(value & _u32_max);
77798
+ const h = isLE2 ? 4 : 0;
77799
+ const l2 = isLE2 ? 0 : 4;
77800
+ view.setUint32(byteOffset + h, wh, isLE2);
77801
+ view.setUint32(byteOffset + l2, wl, isLE2);
77802
+ }
77803
+ function Chi2(a, b, c) {
77804
+ return a & b ^ ~a & c;
77805
+ }
77806
+ function Maj2(a, b, c) {
77807
+ return a & b ^ a & c ^ b & c;
77808
+ }
77809
+ var HashMD2 = class extends Hash {
77810
+ constructor(blockLen, outputLen, padOffset, isLE2) {
77811
+ super();
77812
+ this.finished = false;
77813
+ this.length = 0;
77814
+ this.pos = 0;
77815
+ this.destroyed = false;
77816
+ this.blockLen = blockLen;
77817
+ this.outputLen = outputLen;
77818
+ this.padOffset = padOffset;
77819
+ this.isLE = isLE2;
77820
+ this.buffer = new Uint8Array(blockLen);
77821
+ this.view = createView2(this.buffer);
77822
+ }
77823
+ update(data) {
77824
+ aexists2(this);
77825
+ data = toBytes(data);
77826
+ abytes2(data);
77827
+ const { view, buffer, blockLen } = this;
77828
+ const len = data.length;
77829
+ for (let pos = 0; pos < len; ) {
77830
+ const take = Math.min(blockLen - this.pos, len - pos);
77831
+ if (take === blockLen) {
77832
+ const dataView = createView2(data);
77833
+ for (; blockLen <= len - pos; pos += blockLen)
77834
+ this.process(dataView, pos);
77835
+ continue;
77836
+ }
77837
+ buffer.set(data.subarray(pos, pos + take), this.pos);
77838
+ this.pos += take;
77839
+ pos += take;
77840
+ if (this.pos === blockLen) {
77841
+ this.process(view, 0);
77842
+ this.pos = 0;
77843
+ }
77844
+ }
77845
+ this.length += data.length;
77846
+ this.roundClean();
77847
+ return this;
77848
+ }
77849
+ digestInto(out) {
77850
+ aexists2(this);
77851
+ aoutput2(out, this);
77852
+ this.finished = true;
77853
+ const { buffer, view, blockLen, isLE: isLE2 } = this;
77854
+ let { pos } = this;
77855
+ buffer[pos++] = 128;
77856
+ clean2(this.buffer.subarray(pos));
77857
+ if (this.padOffset > blockLen - pos) {
77858
+ this.process(view, 0);
77859
+ pos = 0;
77860
+ }
77861
+ for (let i = pos; i < blockLen; i++)
77862
+ buffer[i] = 0;
77863
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
77864
+ this.process(view, 0);
77865
+ const oview = createView2(out);
77866
+ const len = this.outputLen;
77867
+ if (len % 4)
77868
+ throw new Error("_sha2: outputLen should be aligned to 32bit");
77869
+ const outLen = len / 4;
77870
+ const state = this.get();
77871
+ if (outLen > state.length)
77872
+ throw new Error("_sha2: outputLen bigger than state");
77873
+ for (let i = 0; i < outLen; i++)
77874
+ oview.setUint32(4 * i, state[i], isLE2);
77875
+ }
77876
+ digest() {
77877
+ const { buffer, outputLen } = this;
77878
+ this.digestInto(buffer);
77879
+ const res = buffer.slice(0, outputLen);
77880
+ this.destroy();
77881
+ return res;
77882
+ }
77883
+ _cloneInto(to) {
77884
+ to || (to = new this.constructor());
77885
+ to.set(...this.get());
77886
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
77887
+ to.destroyed = destroyed;
77888
+ to.finished = finished;
77889
+ to.length = length;
77890
+ to.pos = pos;
77891
+ if (length % blockLen)
77892
+ to.buffer.set(buffer);
77893
+ return to;
77894
+ }
77895
+ clone() {
77896
+ return this._cloneInto();
77897
+ }
77898
+ };
77899
+ var SHA256_IV2 = /* @__PURE__ */ Uint32Array.from([
77900
+ 1779033703,
77901
+ 3144134277,
77902
+ 1013904242,
77903
+ 2773480762,
77904
+ 1359893119,
77905
+ 2600822924,
77906
+ 528734635,
77907
+ 1541459225
77908
+ ]);
77909
+
77910
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/sha2.js
77911
+ var SHA256_K = /* @__PURE__ */ Uint32Array.from([
77912
+ 1116352408,
77913
+ 1899447441,
77914
+ 3049323471,
77915
+ 3921009573,
77916
+ 961987163,
77917
+ 1508970993,
77918
+ 2453635748,
77919
+ 2870763221,
77920
+ 3624381080,
77921
+ 310598401,
77922
+ 607225278,
77923
+ 1426881987,
77924
+ 1925078388,
77925
+ 2162078206,
77926
+ 2614888103,
77927
+ 3248222580,
77928
+ 3835390401,
77929
+ 4022224774,
77930
+ 264347078,
77931
+ 604807628,
77932
+ 770255983,
77933
+ 1249150122,
77934
+ 1555081692,
77935
+ 1996064986,
77936
+ 2554220882,
77937
+ 2821834349,
77938
+ 2952996808,
77939
+ 3210313671,
77940
+ 3336571891,
77941
+ 3584528711,
77942
+ 113926993,
77943
+ 338241895,
77944
+ 666307205,
77945
+ 773529912,
77946
+ 1294757372,
77947
+ 1396182291,
77948
+ 1695183700,
77949
+ 1986661051,
77950
+ 2177026350,
77951
+ 2456956037,
77952
+ 2730485921,
77953
+ 2820302411,
77954
+ 3259730800,
77955
+ 3345764771,
77956
+ 3516065817,
77957
+ 3600352804,
77958
+ 4094571909,
77959
+ 275423344,
77960
+ 430227734,
77961
+ 506948616,
77962
+ 659060556,
77963
+ 883997877,
77964
+ 958139571,
77965
+ 1322822218,
77966
+ 1537002063,
77967
+ 1747873779,
77968
+ 1955562222,
77969
+ 2024104815,
77970
+ 2227730452,
77971
+ 2361852424,
77972
+ 2428436474,
77973
+ 2756734187,
77974
+ 3204031479,
77975
+ 3329325298
77976
+ ]);
77977
+ var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
77978
+ var SHA256 = class extends HashMD2 {
77979
+ constructor(outputLen = 32) {
77980
+ super(64, outputLen, 8, false);
77981
+ this.A = SHA256_IV2[0] | 0;
77982
+ this.B = SHA256_IV2[1] | 0;
77983
+ this.C = SHA256_IV2[2] | 0;
77984
+ this.D = SHA256_IV2[3] | 0;
77985
+ this.E = SHA256_IV2[4] | 0;
77986
+ this.F = SHA256_IV2[5] | 0;
77987
+ this.G = SHA256_IV2[6] | 0;
77988
+ this.H = SHA256_IV2[7] | 0;
77989
+ }
77990
+ get() {
77991
+ const { A, B: B2, C: C2, D: D2, E, F: F2, G: G2, H: H2 } = this;
77992
+ return [A, B2, C2, D2, E, F2, G2, H2];
77993
+ }
77994
+ // prettier-ignore
77995
+ set(A, B2, C2, D2, E, F2, G2, H2) {
77996
+ this.A = A | 0;
77997
+ this.B = B2 | 0;
77998
+ this.C = C2 | 0;
77999
+ this.D = D2 | 0;
78000
+ this.E = E | 0;
78001
+ this.F = F2 | 0;
78002
+ this.G = G2 | 0;
78003
+ this.H = H2 | 0;
78004
+ }
78005
+ process(view, offset) {
78006
+ for (let i = 0; i < 16; i++, offset += 4)
78007
+ SHA256_W[i] = view.getUint32(offset, false);
78008
+ for (let i = 16; i < 64; i++) {
78009
+ const W15 = SHA256_W[i - 15];
78010
+ const W2 = SHA256_W[i - 2];
78011
+ const s0 = rotr2(W15, 7) ^ rotr2(W15, 18) ^ W15 >>> 3;
78012
+ const s1 = rotr2(W2, 17) ^ rotr2(W2, 19) ^ W2 >>> 10;
78013
+ SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
78014
+ }
78015
+ let { A, B: B2, C: C2, D: D2, E, F: F2, G: G2, H: H2 } = this;
78016
+ for (let i = 0; i < 64; i++) {
78017
+ const sigma1 = rotr2(E, 6) ^ rotr2(E, 11) ^ rotr2(E, 25);
78018
+ const T1 = H2 + sigma1 + Chi2(E, F2, G2) + SHA256_K[i] + SHA256_W[i] | 0;
78019
+ const sigma0 = rotr2(A, 2) ^ rotr2(A, 13) ^ rotr2(A, 22);
78020
+ const T2 = sigma0 + Maj2(A, B2, C2) | 0;
78021
+ H2 = G2;
78022
+ G2 = F2;
78023
+ F2 = E;
78024
+ E = D2 + T1 | 0;
78025
+ D2 = C2;
78026
+ C2 = B2;
78027
+ B2 = A;
78028
+ A = T1 + T2 | 0;
78029
+ }
78030
+ A = A + this.A | 0;
78031
+ B2 = B2 + this.B | 0;
78032
+ C2 = C2 + this.C | 0;
78033
+ D2 = D2 + this.D | 0;
78034
+ E = E + this.E | 0;
78035
+ F2 = F2 + this.F | 0;
78036
+ G2 = G2 + this.G | 0;
78037
+ H2 = H2 + this.H | 0;
78038
+ this.set(A, B2, C2, D2, E, F2, G2, H2);
78039
+ }
78040
+ roundClean() {
78041
+ clean2(SHA256_W);
78042
+ }
78043
+ destroy() {
78044
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
78045
+ clean2(this.buffer);
78046
+ }
78047
+ };
78048
+ var sha2562 = /* @__PURE__ */ createHasher2(() => new SHA256());
78049
+
78050
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/hmac.js
78051
+ var HMAC = class extends Hash {
78052
+ constructor(hash, _key) {
78053
+ super();
78054
+ this.finished = false;
78055
+ this.destroyed = false;
78056
+ ahash2(hash);
78057
+ const key = toBytes(_key);
78058
+ this.iHash = hash.create();
78059
+ if (typeof this.iHash.update !== "function")
78060
+ throw new Error("Expected instance of class which extends utils.Hash");
78061
+ this.blockLen = this.iHash.blockLen;
78062
+ this.outputLen = this.iHash.outputLen;
78063
+ const blockLen = this.blockLen;
78064
+ const pad = new Uint8Array(blockLen);
78065
+ pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
78066
+ for (let i = 0; i < pad.length; i++)
78067
+ pad[i] ^= 54;
78068
+ this.iHash.update(pad);
78069
+ this.oHash = hash.create();
78070
+ for (let i = 0; i < pad.length; i++)
78071
+ pad[i] ^= 54 ^ 92;
78072
+ this.oHash.update(pad);
78073
+ clean2(pad);
78074
+ }
78075
+ update(buf) {
78076
+ aexists2(this);
78077
+ this.iHash.update(buf);
78078
+ return this;
78079
+ }
78080
+ digestInto(out) {
78081
+ aexists2(this);
78082
+ abytes2(out, this.outputLen);
78083
+ this.finished = true;
78084
+ this.iHash.digestInto(out);
78085
+ this.oHash.update(out);
78086
+ this.oHash.digestInto(out);
78087
+ this.destroy();
78088
+ }
78089
+ digest() {
78090
+ const out = new Uint8Array(this.oHash.outputLen);
78091
+ this.digestInto(out);
78092
+ return out;
78093
+ }
78094
+ _cloneInto(to) {
78095
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
78096
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
78097
+ to = to;
78098
+ to.finished = finished;
78099
+ to.destroyed = destroyed;
78100
+ to.blockLen = blockLen;
78101
+ to.outputLen = outputLen;
78102
+ to.oHash = oHash._cloneInto(to.oHash);
78103
+ to.iHash = iHash._cloneInto(to.iHash);
78104
+ return to;
78105
+ }
78106
+ clone() {
78107
+ return this._cloneInto();
78108
+ }
78109
+ destroy() {
78110
+ this.destroyed = true;
78111
+ this.oHash.destroy();
78112
+ this.iHash.destroy();
78113
+ }
78114
+ };
78115
+ var hmac2 = (hash, key, message) => new HMAC(hash, key).update(message).digest();
78116
+ hmac2.create = (hash, key) => new HMAC(hash, key);
78117
+
78118
+ // ../../node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/utils.js
78119
+ var _0n5 = /* @__PURE__ */ BigInt(0);
78120
+ var _1n6 = /* @__PURE__ */ BigInt(1);
78121
+ function _abool2(value, title = "") {
78122
+ if (typeof value !== "boolean") {
78123
+ const prefix = title && `"${title}"`;
78124
+ throw new Error(prefix + "expected boolean, got type=" + typeof value);
78125
+ }
78126
+ return value;
78127
+ }
78128
+ function _abytes2(value, length, title = "") {
78129
+ const bytes = isBytes3(value);
78130
+ const len = value?.length;
78131
+ const needsLen = length !== void 0;
78132
+ if (!bytes || needsLen && len !== length) {
78133
+ const prefix = title && `"${title}" `;
78134
+ const ofLen = needsLen ? ` of length ${length}` : "";
78135
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
78136
+ throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
78137
+ }
78138
+ return value;
78139
+ }
78140
+ function numberToHexUnpadded(num) {
78141
+ const hex = num.toString(16);
78142
+ return hex.length & 1 ? "0" + hex : hex;
78143
+ }
78144
+ function hexToNumber2(hex) {
78145
+ if (typeof hex !== "string")
78146
+ throw new Error("hex string expected, got " + typeof hex);
78147
+ return hex === "" ? _0n5 : BigInt("0x" + hex);
78148
+ }
78149
+ function bytesToNumberBE2(bytes) {
78150
+ return hexToNumber2(bytesToHex2(bytes));
78151
+ }
78152
+ function bytesToNumberLE2(bytes) {
78153
+ abytes2(bytes);
78154
+ return hexToNumber2(bytesToHex2(Uint8Array.from(bytes).reverse()));
78155
+ }
78156
+ function numberToBytesBE2(n, len) {
78157
+ return hexToBytes2(n.toString(16).padStart(len * 2, "0"));
78158
+ }
78159
+ function numberToBytesLE2(n, len) {
78160
+ return numberToBytesBE2(n, len).reverse();
78161
+ }
78162
+ function ensureBytes(title, hex, expectedLength) {
78163
+ let res;
78164
+ if (typeof hex === "string") {
78165
+ try {
78166
+ res = hexToBytes2(hex);
78167
+ } catch (e) {
78168
+ throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
78169
+ }
78170
+ } else if (isBytes3(hex)) {
78171
+ res = Uint8Array.from(hex);
78172
+ } else {
78173
+ throw new Error(title + " must be hex string or Uint8Array");
78174
+ }
78175
+ res.length;
78176
+ return res;
78177
+ }
78178
+ var isPosBig2 = (n) => typeof n === "bigint" && _0n5 <= n;
78179
+ function inRange2(n, min2, max2) {
78180
+ return isPosBig2(n) && isPosBig2(min2) && isPosBig2(max2) && min2 <= n && n < max2;
78181
+ }
78182
+ function aInRange2(title, n, min2, max2) {
78183
+ if (!inRange2(n, min2, max2))
78184
+ throw new Error("expected valid " + title + ": " + min2 + " <= n < " + max2 + ", got " + n);
78185
+ }
78186
+ function bitLen2(n) {
78187
+ let len;
78188
+ for (len = 0; n > _0n5; n >>= _1n6, len += 1)
78189
+ ;
78190
+ return len;
78191
+ }
78192
+ var bitMask2 = (n) => (_1n6 << BigInt(n)) - _1n6;
78193
+ function createHmacDrbg(hashLen, qByteLen, hmacFn) {
78194
+ if (typeof hashLen !== "number" || hashLen < 2)
78195
+ throw new Error("hashLen must be a number");
78196
+ if (typeof qByteLen !== "number" || qByteLen < 2)
78197
+ throw new Error("qByteLen must be a number");
78198
+ if (typeof hmacFn !== "function")
78199
+ throw new Error("hmacFn must be a function");
78200
+ const u8n = (len) => new Uint8Array(len);
78201
+ const u8of = (byte) => Uint8Array.of(byte);
78202
+ let v = u8n(hashLen);
78203
+ let k = u8n(hashLen);
78204
+ let i = 0;
78205
+ const reset = () => {
78206
+ v.fill(1);
78207
+ k.fill(0);
78208
+ i = 0;
78209
+ };
78210
+ const h = (...b) => hmacFn(k, v, ...b);
78211
+ const reseed = (seed = u8n(0)) => {
78212
+ k = h(u8of(0), seed);
78213
+ v = h();
78214
+ if (seed.length === 0)
78215
+ return;
78216
+ k = h(u8of(1), seed);
78217
+ v = h();
78218
+ };
78219
+ const gen = () => {
78220
+ if (i++ >= 1e3)
78221
+ throw new Error("drbg: tried 1000 values");
78222
+ let len = 0;
78223
+ const out = [];
78224
+ while (len < qByteLen) {
78225
+ v = h();
78226
+ const sl = v.slice();
78227
+ out.push(sl);
78228
+ len += v.length;
78229
+ }
78230
+ return concatBytes3(...out);
78231
+ };
78232
+ const genUntil = (seed, pred) => {
78233
+ reset();
78234
+ reseed(seed);
78235
+ let res = void 0;
78236
+ while (!(res = pred(gen())))
78237
+ reseed();
78238
+ reset();
78239
+ return res;
78240
+ };
78241
+ return genUntil;
78242
+ }
78243
+ function _validateObject(object4, fields, optFields = {}) {
78244
+ if (!object4 || typeof object4 !== "object")
78245
+ throw new Error("expected valid options object");
78246
+ function checkField(fieldName, expectedType, isOpt) {
78247
+ const val = object4[fieldName];
78248
+ if (isOpt && val === void 0)
78249
+ return;
78250
+ const current = typeof val;
78251
+ if (current !== expectedType || val === null)
78252
+ throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
78253
+ }
78254
+ Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
78255
+ Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
78256
+ }
78257
+ function memoized2(fn) {
78258
+ const map2 = /* @__PURE__ */ new WeakMap();
78259
+ return (arg, ...args) => {
78260
+ const val = map2.get(arg);
78261
+ if (val !== void 0)
78262
+ return val;
78263
+ const computed = fn(arg, ...args);
78264
+ map2.set(arg, computed);
78265
+ return computed;
78266
+ };
78267
+ }
78268
+
78269
+ // ../../node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/abstract/modular.js
78270
+ var _0n6 = BigInt(0);
78271
+ var _1n7 = BigInt(1);
78272
+ var _2n4 = /* @__PURE__ */ BigInt(2);
78273
+ var _3n2 = /* @__PURE__ */ BigInt(3);
78274
+ var _4n2 = /* @__PURE__ */ BigInt(4);
78275
+ var _5n3 = /* @__PURE__ */ BigInt(5);
78276
+ var _7n2 = /* @__PURE__ */ BigInt(7);
78277
+ var _8n4 = /* @__PURE__ */ BigInt(8);
78278
+ var _9n2 = /* @__PURE__ */ BigInt(9);
78279
+ var _16n2 = /* @__PURE__ */ BigInt(16);
78280
+ function mod3(a, b) {
78281
+ const result = a % b;
78282
+ return result >= _0n6 ? result : b + result;
78283
+ }
78284
+ function pow22(x, power, modulo) {
78285
+ let res = x;
78286
+ while (power-- > _0n6) {
78287
+ res *= res;
78288
+ res %= modulo;
78289
+ }
78290
+ return res;
78291
+ }
78292
+ function invert2(number4, modulo) {
78293
+ if (number4 === _0n6)
78294
+ throw new Error("invert: expected non-zero number");
78295
+ if (modulo <= _0n6)
78296
+ throw new Error("invert: expected positive modulus, got " + modulo);
78297
+ let a = mod3(number4, modulo);
78298
+ let b = modulo;
78299
+ let x = _0n6, u2 = _1n7;
78300
+ while (a !== _0n6) {
78301
+ const q = b / a;
78302
+ const r = b % a;
78303
+ const m = x - u2 * q;
78304
+ b = a, a = r, x = u2, u2 = m;
78305
+ }
78306
+ const gcd2 = b;
78307
+ if (gcd2 !== _1n7)
78308
+ throw new Error("invert: does not exist");
78309
+ return mod3(x, modulo);
78310
+ }
78311
+ function assertIsSquare2(Fp, root, n) {
78312
+ if (!Fp.eql(Fp.sqr(root), n))
78313
+ throw new Error("Cannot find square root");
78314
+ }
78315
+ function sqrt3mod42(Fp, n) {
78316
+ const p1div4 = (Fp.ORDER + _1n7) / _4n2;
78317
+ const root = Fp.pow(n, p1div4);
78318
+ assertIsSquare2(Fp, root, n);
78319
+ return root;
78320
+ }
78321
+ function sqrt5mod82(Fp, n) {
78322
+ const p5div8 = (Fp.ORDER - _5n3) / _8n4;
78323
+ const n2 = Fp.mul(n, _2n4);
78324
+ const v = Fp.pow(n2, p5div8);
78325
+ const nv = Fp.mul(n, v);
78326
+ const i = Fp.mul(Fp.mul(nv, _2n4), v);
78327
+ const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
78328
+ assertIsSquare2(Fp, root, n);
78329
+ return root;
78330
+ }
78331
+ function sqrt9mod162(P3) {
78332
+ const Fp_ = Field2(P3);
78333
+ const tn = tonelliShanks2(P3);
78334
+ const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
78335
+ const c2 = tn(Fp_, c1);
78336
+ const c3 = tn(Fp_, Fp_.neg(c1));
78337
+ const c4 = (P3 + _7n2) / _16n2;
78338
+ return (Fp, n) => {
78339
+ let tv1 = Fp.pow(n, c4);
78340
+ let tv2 = Fp.mul(tv1, c1);
78341
+ const tv3 = Fp.mul(tv1, c2);
78342
+ const tv4 = Fp.mul(tv1, c3);
78343
+ const e1 = Fp.eql(Fp.sqr(tv2), n);
78344
+ const e2 = Fp.eql(Fp.sqr(tv3), n);
78345
+ tv1 = Fp.cmov(tv1, tv2, e1);
78346
+ tv2 = Fp.cmov(tv4, tv3, e2);
78347
+ const e3 = Fp.eql(Fp.sqr(tv2), n);
78348
+ const root = Fp.cmov(tv1, tv2, e3);
78349
+ assertIsSquare2(Fp, root, n);
78350
+ return root;
78351
+ };
78352
+ }
78353
+ function tonelliShanks2(P3) {
78354
+ if (P3 < _3n2)
78355
+ throw new Error("sqrt is not defined for small field");
78356
+ let Q = P3 - _1n7;
78357
+ let S2 = 0;
78358
+ while (Q % _2n4 === _0n6) {
78359
+ Q /= _2n4;
78360
+ S2++;
78361
+ }
78362
+ let Z2 = _2n4;
78363
+ const _Fp = Field2(P3);
78364
+ while (FpLegendre2(_Fp, Z2) === 1) {
78365
+ if (Z2++ > 1e3)
78366
+ throw new Error("Cannot find square root: probably non-prime P");
78367
+ }
78368
+ if (S2 === 1)
78369
+ return sqrt3mod42;
78370
+ let cc = _Fp.pow(Z2, Q);
78371
+ const Q1div2 = (Q + _1n7) / _2n4;
78372
+ return function tonelliSlow(Fp, n) {
78373
+ if (Fp.is0(n))
78374
+ return n;
78375
+ if (FpLegendre2(Fp, n) !== 1)
78376
+ throw new Error("Cannot find square root");
78377
+ let M2 = S2;
78378
+ let c = Fp.mul(Fp.ONE, cc);
78379
+ let t = Fp.pow(n, Q);
78380
+ let R2 = Fp.pow(n, Q1div2);
78381
+ while (!Fp.eql(t, Fp.ONE)) {
78382
+ if (Fp.is0(t))
78383
+ return Fp.ZERO;
78384
+ let i = 1;
78385
+ let t_tmp = Fp.sqr(t);
78386
+ while (!Fp.eql(t_tmp, Fp.ONE)) {
78387
+ i++;
78388
+ t_tmp = Fp.sqr(t_tmp);
78389
+ if (i === M2)
78390
+ throw new Error("Cannot find square root");
78391
+ }
78392
+ const exponent = _1n7 << BigInt(M2 - i - 1);
78393
+ const b = Fp.pow(c, exponent);
78394
+ M2 = i;
78395
+ c = Fp.sqr(b);
78396
+ t = Fp.mul(t, c);
78397
+ R2 = Fp.mul(R2, b);
78398
+ }
78399
+ return R2;
78400
+ };
78401
+ }
78402
+ function FpSqrt2(P3) {
78403
+ if (P3 % _4n2 === _3n2)
78404
+ return sqrt3mod42;
78405
+ if (P3 % _8n4 === _5n3)
78406
+ return sqrt5mod82;
78407
+ if (P3 % _16n2 === _9n2)
78408
+ return sqrt9mod162(P3);
78409
+ return tonelliShanks2(P3);
78410
+ }
78411
+ var FIELD_FIELDS2 = [
78412
+ "create",
78413
+ "isValid",
78414
+ "is0",
78415
+ "neg",
78416
+ "inv",
78417
+ "sqrt",
78418
+ "sqr",
78419
+ "eql",
78420
+ "add",
78421
+ "sub",
78422
+ "mul",
78423
+ "pow",
78424
+ "div",
78425
+ "addN",
78426
+ "subN",
78427
+ "mulN",
78428
+ "sqrN"
78429
+ ];
78430
+ function validateField2(field) {
78431
+ const initial = {
78432
+ ORDER: "bigint",
78433
+ MASK: "bigint",
78434
+ BYTES: "number",
78435
+ BITS: "number"
78436
+ };
78437
+ const opts = FIELD_FIELDS2.reduce((map2, val) => {
78438
+ map2[val] = "function";
78439
+ return map2;
78440
+ }, initial);
78441
+ _validateObject(field, opts);
78442
+ return field;
78443
+ }
78444
+ function FpPow2(Fp, num, power) {
78445
+ if (power < _0n6)
78446
+ throw new Error("invalid exponent, negatives unsupported");
78447
+ if (power === _0n6)
78448
+ return Fp.ONE;
78449
+ if (power === _1n7)
78450
+ return num;
78451
+ let p = Fp.ONE;
78452
+ let d = num;
78453
+ while (power > _0n6) {
78454
+ if (power & _1n7)
78455
+ p = Fp.mul(p, d);
78456
+ d = Fp.sqr(d);
78457
+ power >>= _1n7;
78458
+ }
78459
+ return p;
78460
+ }
78461
+ function FpInvertBatch2(Fp, nums, passZero = false) {
78462
+ const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
78463
+ const multipliedAcc = nums.reduce((acc, num, i) => {
78464
+ if (Fp.is0(num))
78465
+ return acc;
78466
+ inverted[i] = acc;
78467
+ return Fp.mul(acc, num);
78468
+ }, Fp.ONE);
78469
+ const invertedAcc = Fp.inv(multipliedAcc);
78470
+ nums.reduceRight((acc, num, i) => {
78471
+ if (Fp.is0(num))
78472
+ return acc;
78473
+ inverted[i] = Fp.mul(acc, inverted[i]);
78474
+ return Fp.mul(acc, num);
78475
+ }, invertedAcc);
78476
+ return inverted;
78477
+ }
78478
+ function FpLegendre2(Fp, n) {
78479
+ const p1mod2 = (Fp.ORDER - _1n7) / _2n4;
78480
+ const powered = Fp.pow(n, p1mod2);
78481
+ const yes = Fp.eql(powered, Fp.ONE);
78482
+ const zero = Fp.eql(powered, Fp.ZERO);
78483
+ const no = Fp.eql(powered, Fp.neg(Fp.ONE));
78484
+ if (!yes && !zero && !no)
78485
+ throw new Error("invalid Legendre symbol result");
78486
+ return yes ? 1 : zero ? 0 : -1;
78487
+ }
78488
+ function nLength2(n, nBitLength) {
78489
+ if (nBitLength !== void 0)
78490
+ anumber3(nBitLength);
78491
+ const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
78492
+ const nByteLength = Math.ceil(_nBitLength / 8);
78493
+ return { nBitLength: _nBitLength, nByteLength };
78494
+ }
78495
+ function Field2(ORDER, bitLenOrOpts, isLE2 = false, opts = {}) {
78496
+ if (ORDER <= _0n6)
78497
+ throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
78498
+ let _nbitLength = void 0;
78499
+ let _sqrt = void 0;
78500
+ let modFromBytes = false;
78501
+ let allowedLengths = void 0;
78502
+ if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
78503
+ if (opts.sqrt || isLE2)
78504
+ throw new Error("cannot specify opts in two arguments");
78505
+ const _opts = bitLenOrOpts;
78506
+ if (_opts.BITS)
78507
+ _nbitLength = _opts.BITS;
78508
+ if (_opts.sqrt)
78509
+ _sqrt = _opts.sqrt;
78510
+ if (typeof _opts.isLE === "boolean")
78511
+ isLE2 = _opts.isLE;
78512
+ if (typeof _opts.modFromBytes === "boolean")
78513
+ modFromBytes = _opts.modFromBytes;
78514
+ allowedLengths = _opts.allowedLengths;
78515
+ } else {
78516
+ if (typeof bitLenOrOpts === "number")
78517
+ _nbitLength = bitLenOrOpts;
78518
+ if (opts.sqrt)
78519
+ _sqrt = opts.sqrt;
78520
+ }
78521
+ const { nBitLength: BITS, nByteLength: BYTES } = nLength2(ORDER, _nbitLength);
78522
+ if (BYTES > 2048)
78523
+ throw new Error("invalid field: expected ORDER of <= 2048 bytes");
78524
+ let sqrtP;
78525
+ const f = Object.freeze({
78526
+ ORDER,
78527
+ isLE: isLE2,
78528
+ BITS,
78529
+ BYTES,
78530
+ MASK: bitMask2(BITS),
78531
+ ZERO: _0n6,
78532
+ ONE: _1n7,
78533
+ allowedLengths,
78534
+ create: (num) => mod3(num, ORDER),
78535
+ isValid: (num) => {
78536
+ if (typeof num !== "bigint")
78537
+ throw new Error("invalid field element: expected bigint, got " + typeof num);
78538
+ return _0n6 <= num && num < ORDER;
78539
+ },
78540
+ is0: (num) => num === _0n6,
78541
+ // is valid and invertible
78542
+ isValidNot0: (num) => !f.is0(num) && f.isValid(num),
78543
+ isOdd: (num) => (num & _1n7) === _1n7,
78544
+ neg: (num) => mod3(-num, ORDER),
78545
+ eql: (lhs, rhs) => lhs === rhs,
78546
+ sqr: (num) => mod3(num * num, ORDER),
78547
+ add: (lhs, rhs) => mod3(lhs + rhs, ORDER),
78548
+ sub: (lhs, rhs) => mod3(lhs - rhs, ORDER),
78549
+ mul: (lhs, rhs) => mod3(lhs * rhs, ORDER),
78550
+ pow: (num, power) => FpPow2(f, num, power),
78551
+ div: (lhs, rhs) => mod3(lhs * invert2(rhs, ORDER), ORDER),
78552
+ // Same as above, but doesn't normalize
78553
+ sqrN: (num) => num * num,
78554
+ addN: (lhs, rhs) => lhs + rhs,
78555
+ subN: (lhs, rhs) => lhs - rhs,
78556
+ mulN: (lhs, rhs) => lhs * rhs,
78557
+ inv: (num) => invert2(num, ORDER),
78558
+ sqrt: _sqrt || ((n) => {
78559
+ if (!sqrtP)
78560
+ sqrtP = FpSqrt2(ORDER);
78561
+ return sqrtP(f, n);
78562
+ }),
78563
+ toBytes: (num) => isLE2 ? numberToBytesLE2(num, BYTES) : numberToBytesBE2(num, BYTES),
78564
+ fromBytes: (bytes, skipValidation = true) => {
78565
+ if (allowedLengths) {
78566
+ if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
78567
+ throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
78568
+ }
78569
+ const padded = new Uint8Array(BYTES);
78570
+ padded.set(bytes, isLE2 ? 0 : padded.length - bytes.length);
78571
+ bytes = padded;
78572
+ }
78573
+ if (bytes.length !== BYTES)
78574
+ throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
78575
+ let scalar = isLE2 ? bytesToNumberLE2(bytes) : bytesToNumberBE2(bytes);
78576
+ if (modFromBytes)
78577
+ scalar = mod3(scalar, ORDER);
78578
+ if (!skipValidation) {
78579
+ if (!f.isValid(scalar))
78580
+ throw new Error("invalid field element: outside of range 0..ORDER");
78581
+ }
78582
+ return scalar;
78583
+ },
78584
+ // TODO: we don't need it here, move out to separate fn
78585
+ invertBatch: (lst) => FpInvertBatch2(f, lst),
78586
+ // We can't move this out because Fp6, Fp12 implement it
78587
+ // and it's unclear what to return in there.
78588
+ cmov: (a, b, c) => c ? b : a
78589
+ });
78590
+ return Object.freeze(f);
78591
+ }
78592
+ function getFieldBytesLength(fieldOrder) {
78593
+ if (typeof fieldOrder !== "bigint")
78594
+ throw new Error("field order must be bigint");
78595
+ const bitLength = fieldOrder.toString(2).length;
78596
+ return Math.ceil(bitLength / 8);
78597
+ }
78598
+ function getMinHashLength(fieldOrder) {
78599
+ const length = getFieldBytesLength(fieldOrder);
78600
+ return length + Math.ceil(length / 2);
78601
+ }
78602
+ function mapHashToField(key, fieldOrder, isLE2 = false) {
78603
+ const len = key.length;
78604
+ const fieldLen = getFieldBytesLength(fieldOrder);
78605
+ const minLen = getMinHashLength(fieldOrder);
78606
+ if (len < 16 || len < minLen || len > 1024)
78607
+ throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
78608
+ const num = isLE2 ? bytesToNumberLE2(key) : bytesToNumberBE2(key);
78609
+ const reduced = mod3(num, fieldOrder - _1n7) + _1n7;
78610
+ return isLE2 ? numberToBytesLE2(reduced, fieldLen) : numberToBytesBE2(reduced, fieldLen);
78611
+ }
78612
+
78613
+ // ../../node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/abstract/curve.js
78614
+ var _0n7 = BigInt(0);
78615
+ var _1n8 = BigInt(1);
78616
+ function negateCt2(condition, item) {
78617
+ const neg = item.negate();
78618
+ return condition ? neg : item;
78619
+ }
78620
+ function normalizeZ2(c, points) {
78621
+ const invertedZs = FpInvertBatch2(c.Fp, points.map((p) => p.Z));
78622
+ return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
78623
+ }
78624
+ function validateW2(W, bits) {
78625
+ if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
78626
+ throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
78627
+ }
78628
+ function calcWOpts2(W, scalarBits) {
78629
+ validateW2(W, scalarBits);
78630
+ const windows = Math.ceil(scalarBits / W) + 1;
78631
+ const windowSize = 2 ** (W - 1);
78632
+ const maxNumber = 2 ** W;
78633
+ const mask = bitMask2(W);
78634
+ const shiftBy = BigInt(W);
78635
+ return { windows, windowSize, mask, maxNumber, shiftBy };
78636
+ }
78637
+ function calcOffsets2(n, window2, wOpts) {
78638
+ const { windowSize, mask, maxNumber, shiftBy } = wOpts;
78639
+ let wbits = Number(n & mask);
78640
+ let nextN = n >> shiftBy;
78641
+ if (wbits > windowSize) {
78642
+ wbits -= maxNumber;
78643
+ nextN += _1n8;
78644
+ }
78645
+ const offsetStart = window2 * windowSize;
78646
+ const offset = offsetStart + Math.abs(wbits) - 1;
78647
+ const isZero = wbits === 0;
78648
+ const isNeg = wbits < 0;
78649
+ const isNegF = window2 % 2 !== 0;
78650
+ const offsetF = offsetStart;
78651
+ return { nextN, offset, isZero, isNeg, isNegF, offsetF };
78652
+ }
78653
+ function validateMSMPoints(points, c) {
78654
+ if (!Array.isArray(points))
78655
+ throw new Error("array expected");
78656
+ points.forEach((p, i) => {
78657
+ if (!(p instanceof c))
78658
+ throw new Error("invalid point at index " + i);
78659
+ });
78660
+ }
78661
+ function validateMSMScalars(scalars, field) {
78662
+ if (!Array.isArray(scalars))
78663
+ throw new Error("array of scalars expected");
78664
+ scalars.forEach((s, i) => {
78665
+ if (!field.isValid(s))
78666
+ throw new Error("invalid scalar at index " + i);
78667
+ });
78668
+ }
78669
+ var pointPrecomputes2 = /* @__PURE__ */ new WeakMap();
78670
+ var pointWindowSizes2 = /* @__PURE__ */ new WeakMap();
78671
+ function getW2(P3) {
78672
+ return pointWindowSizes2.get(P3) || 1;
78673
+ }
78674
+ function assert02(n) {
78675
+ if (n !== _0n7)
78676
+ throw new Error("invalid wNAF");
78677
+ }
78678
+ var wNAF2 = class {
78679
+ // Parametrized with a given Point class (not individual point)
78680
+ constructor(Point, bits) {
78681
+ this.BASE = Point.BASE;
78682
+ this.ZERO = Point.ZERO;
78683
+ this.Fn = Point.Fn;
78684
+ this.bits = bits;
78685
+ }
78686
+ // non-const time multiplication ladder
78687
+ _unsafeLadder(elm, n, p = this.ZERO) {
78688
+ let d = elm;
78689
+ while (n > _0n7) {
78690
+ if (n & _1n8)
78691
+ p = p.add(d);
78692
+ d = d.double();
78693
+ n >>= _1n8;
78694
+ }
78695
+ return p;
78696
+ }
78697
+ /**
78698
+ * Creates a wNAF precomputation window. Used for caching.
78699
+ * Default window size is set by `utils.precompute()` and is equal to 8.
78700
+ * Number of precomputed points depends on the curve size:
78701
+ * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
78702
+ * - 𝑊 is the window size
78703
+ * - 𝑛 is the bitlength of the curve order.
78704
+ * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
78705
+ * @param point Point instance
78706
+ * @param W window size
78707
+ * @returns precomputed point tables flattened to a single array
78708
+ */
78709
+ precomputeWindow(point, W) {
78710
+ const { windows, windowSize } = calcWOpts2(W, this.bits);
78711
+ const points = [];
78712
+ let p = point;
78713
+ let base = p;
78714
+ for (let window2 = 0; window2 < windows; window2++) {
78715
+ base = p;
78716
+ points.push(base);
78717
+ for (let i = 1; i < windowSize; i++) {
78718
+ base = base.add(p);
78719
+ points.push(base);
78720
+ }
78721
+ p = base.double();
78722
+ }
78723
+ return points;
78724
+ }
78725
+ /**
78726
+ * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
78727
+ * More compact implementation:
78728
+ * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
78729
+ * @returns real and fake (for const-time) points
78730
+ */
78731
+ wNAF(W, precomputes, n) {
78732
+ if (!this.Fn.isValid(n))
78733
+ throw new Error("invalid scalar");
78734
+ let p = this.ZERO;
78735
+ let f = this.BASE;
78736
+ const wo = calcWOpts2(W, this.bits);
78737
+ for (let window2 = 0; window2 < wo.windows; window2++) {
78738
+ const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets2(n, window2, wo);
78739
+ n = nextN;
78740
+ if (isZero) {
78741
+ f = f.add(negateCt2(isNegF, precomputes[offsetF]));
78742
+ } else {
78743
+ p = p.add(negateCt2(isNeg, precomputes[offset]));
78744
+ }
78745
+ }
78746
+ assert02(n);
78747
+ return { p, f };
78748
+ }
78749
+ /**
78750
+ * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
78751
+ * @param acc accumulator point to add result of multiplication
78752
+ * @returns point
78753
+ */
78754
+ wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
78755
+ const wo = calcWOpts2(W, this.bits);
78756
+ for (let window2 = 0; window2 < wo.windows; window2++) {
78757
+ if (n === _0n7)
78758
+ break;
78759
+ const { nextN, offset, isZero, isNeg } = calcOffsets2(n, window2, wo);
78760
+ n = nextN;
78761
+ if (isZero) {
78762
+ continue;
78763
+ } else {
78764
+ const item = precomputes[offset];
78765
+ acc = acc.add(isNeg ? item.negate() : item);
78766
+ }
78767
+ }
78768
+ assert02(n);
78769
+ return acc;
78770
+ }
78771
+ getPrecomputes(W, point, transform3) {
78772
+ let comp = pointPrecomputes2.get(point);
78773
+ if (!comp) {
78774
+ comp = this.precomputeWindow(point, W);
78775
+ if (W !== 1) {
78776
+ if (typeof transform3 === "function")
78777
+ comp = transform3(comp);
78778
+ pointPrecomputes2.set(point, comp);
78779
+ }
78780
+ }
78781
+ return comp;
78782
+ }
78783
+ cached(point, scalar, transform3) {
78784
+ const W = getW2(point);
78785
+ return this.wNAF(W, this.getPrecomputes(W, point, transform3), scalar);
78786
+ }
78787
+ unsafe(point, scalar, transform3, prev) {
78788
+ const W = getW2(point);
78789
+ if (W === 1)
78790
+ return this._unsafeLadder(point, scalar, prev);
78791
+ return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform3), scalar, prev);
78792
+ }
78793
+ // We calculate precomputes for elliptic curve point multiplication
78794
+ // using windowed method. This specifies window size and
78795
+ // stores precomputed values. Usually only base point would be precomputed.
78796
+ createCache(P3, W) {
78797
+ validateW2(W, this.bits);
78798
+ pointWindowSizes2.set(P3, W);
78799
+ pointPrecomputes2.delete(P3);
78800
+ }
78801
+ hasCache(elm) {
78802
+ return getW2(elm) !== 1;
78803
+ }
78804
+ };
78805
+ function mulEndoUnsafe(Point, point, k1, k2) {
78806
+ let acc = point;
78807
+ let p1 = Point.ZERO;
78808
+ let p2 = Point.ZERO;
78809
+ while (k1 > _0n7 || k2 > _0n7) {
78810
+ if (k1 & _1n8)
78811
+ p1 = p1.add(acc);
78812
+ if (k2 & _1n8)
78813
+ p2 = p2.add(acc);
78814
+ acc = acc.double();
78815
+ k1 >>= _1n8;
78816
+ k2 >>= _1n8;
78817
+ }
78818
+ return { p1, p2 };
78819
+ }
78820
+ function pippenger(c, fieldN, points, scalars) {
78821
+ validateMSMPoints(points, c);
78822
+ validateMSMScalars(scalars, fieldN);
78823
+ const plength = points.length;
78824
+ const slength = scalars.length;
78825
+ if (plength !== slength)
78826
+ throw new Error("arrays of points and scalars must have equal length");
78827
+ const zero = c.ZERO;
78828
+ const wbits = bitLen2(BigInt(plength));
78829
+ let windowSize = 1;
78830
+ if (wbits > 12)
78831
+ windowSize = wbits - 3;
78832
+ else if (wbits > 4)
78833
+ windowSize = wbits - 2;
78834
+ else if (wbits > 0)
78835
+ windowSize = 2;
78836
+ const MASK = bitMask2(windowSize);
78837
+ const buckets = new Array(Number(MASK) + 1).fill(zero);
78838
+ const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
78839
+ let sum2 = zero;
78840
+ for (let i = lastBits; i >= 0; i -= windowSize) {
78841
+ buckets.fill(zero);
78842
+ for (let j = 0; j < slength; j++) {
78843
+ const scalar = scalars[j];
78844
+ const wbits2 = Number(scalar >> BigInt(i) & MASK);
78845
+ buckets[wbits2] = buckets[wbits2].add(points[j]);
78846
+ }
78847
+ let resI = zero;
78848
+ for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
78849
+ sumI = sumI.add(buckets[j]);
78850
+ resI = resI.add(sumI);
78851
+ }
78852
+ sum2 = sum2.add(resI);
78853
+ if (i !== 0)
78854
+ for (let j = 0; j < windowSize; j++)
78855
+ sum2 = sum2.double();
78856
+ }
78857
+ return sum2;
78858
+ }
78859
+ function createField2(order, field, isLE2) {
78860
+ if (field) {
78861
+ if (field.ORDER !== order)
78862
+ throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
78863
+ validateField2(field);
78864
+ return field;
78865
+ } else {
78866
+ return Field2(order, { isLE: isLE2 });
78867
+ }
78868
+ }
78869
+ function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
78870
+ if (FpFnLE === void 0)
78871
+ FpFnLE = type === "edwards";
78872
+ if (!CURVE || typeof CURVE !== "object")
78873
+ throw new Error(`expected valid ${type} CURVE object`);
78874
+ for (const p of ["p", "n", "h"]) {
78875
+ const val = CURVE[p];
78876
+ if (!(typeof val === "bigint" && val > _0n7))
78877
+ throw new Error(`CURVE.${p} must be positive bigint`);
78878
+ }
78879
+ const Fp = createField2(CURVE.p, curveOpts.Fp, FpFnLE);
78880
+ const Fn = createField2(CURVE.n, curveOpts.Fn, FpFnLE);
78881
+ const _b = "b" ;
78882
+ const params = ["Gx", "Gy", "a", _b];
78883
+ for (const p of params) {
78884
+ if (!Fp.isValid(CURVE[p]))
78885
+ throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
78886
+ }
78887
+ CURVE = Object.freeze(Object.assign({}, CURVE));
78888
+ return { CURVE, Fp, Fn };
78889
+ }
78890
+
78891
+ // ../../node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/abstract/weierstrass.js
78892
+ var divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n5) / den;
78893
+ function _splitEndoScalar(k, basis, n) {
78894
+ const [[a1, b1], [a2, b2]] = basis;
78895
+ const c1 = divNearest(b2 * k, n);
78896
+ const c2 = divNearest(-b1 * k, n);
78897
+ let k1 = k - c1 * a1 - c2 * a2;
78898
+ let k2 = -c1 * b1 - c2 * b2;
78899
+ const k1neg = k1 < _0n8;
78900
+ const k2neg = k2 < _0n8;
78901
+ if (k1neg)
78902
+ k1 = -k1;
78903
+ if (k2neg)
78904
+ k2 = -k2;
78905
+ const MAX_NUM = bitMask2(Math.ceil(bitLen2(n) / 2)) + _1n9;
78906
+ if (k1 < _0n8 || k1 >= MAX_NUM || k2 < _0n8 || k2 >= MAX_NUM) {
78907
+ throw new Error("splitScalar (endomorphism): failed, k=" + k);
78908
+ }
78909
+ return { k1neg, k1, k2neg, k2 };
78910
+ }
78911
+ function validateSigFormat(format) {
78912
+ if (!["compact", "recovered", "der"].includes(format))
78913
+ throw new Error('Signature format must be "compact", "recovered", or "der"');
78914
+ return format;
78915
+ }
78916
+ function validateSigOpts(opts, def) {
78917
+ const optsn = {};
78918
+ for (let optName of Object.keys(def)) {
78919
+ optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
78920
+ }
78921
+ _abool2(optsn.lowS, "lowS");
78922
+ _abool2(optsn.prehash, "prehash");
78923
+ if (optsn.format !== void 0)
78924
+ validateSigFormat(optsn.format);
78925
+ return optsn;
78926
+ }
78927
+ var DERErr = class extends Error {
78928
+ constructor(m = "") {
78929
+ super(m);
78930
+ }
78931
+ };
78932
+ var DER = {
78933
+ // asn.1 DER encoding utils
78934
+ Err: DERErr,
78935
+ // Basic building block is TLV (Tag-Length-Value)
78936
+ _tlv: {
78937
+ encode: (tag2, data) => {
78938
+ const { Err: E } = DER;
78939
+ if (tag2 < 0 || tag2 > 256)
78940
+ throw new E("tlv.encode: wrong tag");
78941
+ if (data.length & 1)
78942
+ throw new E("tlv.encode: unpadded data");
78943
+ const dataLen = data.length / 2;
78944
+ const len = numberToHexUnpadded(dataLen);
78945
+ if (len.length / 2 & 128)
78946
+ throw new E("tlv.encode: long form length too big");
78947
+ const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
78948
+ const t = numberToHexUnpadded(tag2);
78949
+ return t + lenLen + len + data;
78950
+ },
78951
+ // v - value, l - left bytes (unparsed)
78952
+ decode(tag2, data) {
78953
+ const { Err: E } = DER;
78954
+ let pos = 0;
78955
+ if (tag2 < 0 || tag2 > 256)
78956
+ throw new E("tlv.encode: wrong tag");
78957
+ if (data.length < 2 || data[pos++] !== tag2)
78958
+ throw new E("tlv.decode: wrong tlv");
78959
+ const first = data[pos++];
78960
+ const isLong = !!(first & 128);
78961
+ let length = 0;
78962
+ if (!isLong)
78963
+ length = first;
78964
+ else {
78965
+ const lenLen = first & 127;
78966
+ if (!lenLen)
78967
+ throw new E("tlv.decode(long): indefinite length not supported");
78968
+ if (lenLen > 4)
78969
+ throw new E("tlv.decode(long): byte length is too big");
78970
+ const lengthBytes = data.subarray(pos, pos + lenLen);
78971
+ if (lengthBytes.length !== lenLen)
78972
+ throw new E("tlv.decode: length bytes not complete");
78973
+ if (lengthBytes[0] === 0)
78974
+ throw new E("tlv.decode(long): zero leftmost byte");
78975
+ for (const b of lengthBytes)
78976
+ length = length << 8 | b;
78977
+ pos += lenLen;
78978
+ if (length < 128)
78979
+ throw new E("tlv.decode(long): not minimal encoding");
78980
+ }
78981
+ const v = data.subarray(pos, pos + length);
78982
+ if (v.length !== length)
78983
+ throw new E("tlv.decode: wrong value length");
78984
+ return { v, l: data.subarray(pos + length) };
78985
+ }
78986
+ },
78987
+ // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
78988
+ // since we always use positive integers here. It must always be empty:
78989
+ // - add zero byte if exists
78990
+ // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
78991
+ _int: {
78992
+ encode(num) {
78993
+ const { Err: E } = DER;
78994
+ if (num < _0n8)
78995
+ throw new E("integer: negative integers are not allowed");
78996
+ let hex = numberToHexUnpadded(num);
78997
+ if (Number.parseInt(hex[0], 16) & 8)
78998
+ hex = "00" + hex;
78999
+ if (hex.length & 1)
79000
+ throw new E("unexpected DER parsing assertion: unpadded hex");
79001
+ return hex;
79002
+ },
79003
+ decode(data) {
79004
+ const { Err: E } = DER;
79005
+ if (data[0] & 128)
79006
+ throw new E("invalid signature integer: negative");
79007
+ if (data[0] === 0 && !(data[1] & 128))
79008
+ throw new E("invalid signature integer: unnecessary leading zero");
79009
+ return bytesToNumberBE2(data);
79010
+ }
79011
+ },
79012
+ toSig(hex) {
79013
+ const { Err: E, _int: int2, _tlv: tlv } = DER;
79014
+ const data = ensureBytes("signature", hex);
79015
+ const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
79016
+ if (seqLeftBytes.length)
79017
+ throw new E("invalid signature: left bytes after parsing");
79018
+ const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
79019
+ const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
79020
+ if (sLeftBytes.length)
79021
+ throw new E("invalid signature: left bytes after parsing");
79022
+ return { r: int2.decode(rBytes), s: int2.decode(sBytes) };
79023
+ },
79024
+ hexFromSig(sig) {
79025
+ const { _tlv: tlv, _int: int2 } = DER;
79026
+ const rs = tlv.encode(2, int2.encode(sig.r));
79027
+ const ss = tlv.encode(2, int2.encode(sig.s));
79028
+ const seq = rs + ss;
79029
+ return tlv.encode(48, seq);
79030
+ }
79031
+ };
79032
+ var _0n8 = BigInt(0);
79033
+ var _1n9 = BigInt(1);
79034
+ var _2n5 = BigInt(2);
79035
+ var _3n3 = BigInt(3);
79036
+ var _4n3 = BigInt(4);
79037
+ function _normFnElement(Fn, key) {
79038
+ const { BYTES: expected } = Fn;
79039
+ let num;
79040
+ if (typeof key === "bigint") {
79041
+ num = key;
79042
+ } else {
79043
+ let bytes = ensureBytes("private key", key);
79044
+ try {
79045
+ num = Fn.fromBytes(bytes);
79046
+ } catch (error2) {
79047
+ throw new Error(`invalid private key: expected ui8a of size ${expected}, got ${typeof key}`);
79048
+ }
79049
+ }
79050
+ if (!Fn.isValidNot0(num))
79051
+ throw new Error("invalid private key: out of range [1..N-1]");
79052
+ return num;
79053
+ }
79054
+ function weierstrassN(params, extraOpts = {}) {
79055
+ const validated = _createCurveFields("weierstrass", params, extraOpts);
79056
+ const { Fp, Fn } = validated;
79057
+ let CURVE = validated.CURVE;
79058
+ const { h: cofactor, n: CURVE_ORDER } = CURVE;
79059
+ _validateObject(extraOpts, {}, {
79060
+ allowInfinityPoint: "boolean",
79061
+ clearCofactor: "function",
79062
+ isTorsionFree: "function",
79063
+ fromBytes: "function",
79064
+ toBytes: "function",
79065
+ endo: "object",
79066
+ wrapPrivateKey: "boolean"
79067
+ });
79068
+ const { endo } = extraOpts;
79069
+ if (endo) {
79070
+ if (!Fp.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) {
79071
+ throw new Error('invalid endo: expected "beta": bigint and "basises": array');
79072
+ }
79073
+ }
79074
+ const lengths = getWLengths(Fp, Fn);
79075
+ function assertCompressionIsSupported() {
79076
+ if (!Fp.isOdd)
79077
+ throw new Error("compression is not supported: Field does not have .isOdd()");
79078
+ }
79079
+ function pointToBytes(_c, point, isCompressed) {
79080
+ const { x, y: y2 } = point.toAffine();
79081
+ const bx = Fp.toBytes(x);
79082
+ _abool2(isCompressed, "isCompressed");
79083
+ if (isCompressed) {
79084
+ assertCompressionIsSupported();
79085
+ const hasEvenY = !Fp.isOdd(y2);
79086
+ return concatBytes3(pprefix(hasEvenY), bx);
79087
+ } else {
79088
+ return concatBytes3(Uint8Array.of(4), bx, Fp.toBytes(y2));
79089
+ }
79090
+ }
79091
+ function pointFromBytes(bytes) {
79092
+ _abytes2(bytes, void 0, "Point");
79093
+ const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
79094
+ const length = bytes.length;
79095
+ const head = bytes[0];
79096
+ const tail = bytes.subarray(1);
79097
+ if (length === comp && (head === 2 || head === 3)) {
79098
+ const x = Fp.fromBytes(tail);
79099
+ if (!Fp.isValid(x))
79100
+ throw new Error("bad point: is not on curve, wrong x");
79101
+ const y2 = weierstrassEquation(x);
79102
+ let y3;
79103
+ try {
79104
+ y3 = Fp.sqrt(y2);
79105
+ } catch (sqrtError) {
79106
+ const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
79107
+ throw new Error("bad point: is not on curve, sqrt error" + err);
79108
+ }
79109
+ assertCompressionIsSupported();
79110
+ const isYOdd = Fp.isOdd(y3);
79111
+ const isHeadOdd = (head & 1) === 1;
79112
+ if (isHeadOdd !== isYOdd)
79113
+ y3 = Fp.neg(y3);
79114
+ return { x, y: y3 };
79115
+ } else if (length === uncomp && head === 4) {
79116
+ const L = Fp.BYTES;
79117
+ const x = Fp.fromBytes(tail.subarray(0, L));
79118
+ const y2 = Fp.fromBytes(tail.subarray(L, L * 2));
79119
+ if (!isValidXY(x, y2))
79120
+ throw new Error("bad point: is not on curve");
79121
+ return { x, y: y2 };
79122
+ } else {
79123
+ throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
79124
+ }
79125
+ }
79126
+ const encodePoint = extraOpts.toBytes || pointToBytes;
79127
+ const decodePoint = extraOpts.fromBytes || pointFromBytes;
79128
+ function weierstrassEquation(x) {
79129
+ const x2 = Fp.sqr(x);
79130
+ const x3 = Fp.mul(x2, x);
79131
+ return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
79132
+ }
79133
+ function isValidXY(x, y2) {
79134
+ const left = Fp.sqr(y2);
79135
+ const right = weierstrassEquation(x);
79136
+ return Fp.eql(left, right);
79137
+ }
79138
+ if (!isValidXY(CURVE.Gx, CURVE.Gy))
79139
+ throw new Error("bad curve params: generator point");
79140
+ const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n3), _4n3);
79141
+ const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
79142
+ if (Fp.is0(Fp.add(_4a3, _27b2)))
79143
+ throw new Error("bad curve params: a or b");
79144
+ function acoord(title, n, banZero = false) {
79145
+ if (!Fp.isValid(n) || banZero && Fp.is0(n))
79146
+ throw new Error(`bad point coordinate ${title}`);
79147
+ return n;
79148
+ }
79149
+ function aprjpoint(other) {
79150
+ if (!(other instanceof Point))
79151
+ throw new Error("ProjectivePoint expected");
79152
+ }
79153
+ function splitEndoScalarN(k) {
79154
+ if (!endo || !endo.basises)
79155
+ throw new Error("no endo");
79156
+ return _splitEndoScalar(k, endo.basises, Fn.ORDER);
79157
+ }
79158
+ const toAffineMemo = memoized2((p, iz) => {
79159
+ const { X, Y, Z: Z2 } = p;
79160
+ if (Fp.eql(Z2, Fp.ONE))
79161
+ return { x: X, y: Y };
79162
+ const is0 = p.is0();
79163
+ if (iz == null)
79164
+ iz = is0 ? Fp.ONE : Fp.inv(Z2);
79165
+ const x = Fp.mul(X, iz);
79166
+ const y2 = Fp.mul(Y, iz);
79167
+ const zz = Fp.mul(Z2, iz);
79168
+ if (is0)
79169
+ return { x: Fp.ZERO, y: Fp.ZERO };
79170
+ if (!Fp.eql(zz, Fp.ONE))
79171
+ throw new Error("invZ was invalid");
79172
+ return { x, y: y2 };
79173
+ });
79174
+ const assertValidMemo = memoized2((p) => {
79175
+ if (p.is0()) {
79176
+ if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y))
79177
+ return;
79178
+ throw new Error("bad point: ZERO");
79179
+ }
79180
+ const { x, y: y2 } = p.toAffine();
79181
+ if (!Fp.isValid(x) || !Fp.isValid(y2))
79182
+ throw new Error("bad point: x or y not field elements");
79183
+ if (!isValidXY(x, y2))
79184
+ throw new Error("bad point: equation left != right");
79185
+ if (!p.isTorsionFree())
79186
+ throw new Error("bad point: not in prime-order subgroup");
79187
+ return true;
79188
+ });
79189
+ function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
79190
+ k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
79191
+ k1p = negateCt2(k1neg, k1p);
79192
+ k2p = negateCt2(k2neg, k2p);
79193
+ return k1p.add(k2p);
79194
+ }
79195
+ class Point {
79196
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
79197
+ constructor(X, Y, Z2) {
79198
+ this.X = acoord("x", X);
79199
+ this.Y = acoord("y", Y, true);
79200
+ this.Z = acoord("z", Z2);
79201
+ Object.freeze(this);
79202
+ }
79203
+ static CURVE() {
79204
+ return CURVE;
79205
+ }
79206
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
79207
+ static fromAffine(p) {
79208
+ const { x, y: y2 } = p || {};
79209
+ if (!p || !Fp.isValid(x) || !Fp.isValid(y2))
79210
+ throw new Error("invalid affine point");
79211
+ if (p instanceof Point)
79212
+ throw new Error("projective point not allowed");
79213
+ if (Fp.is0(x) && Fp.is0(y2))
79214
+ return Point.ZERO;
79215
+ return new Point(x, y2, Fp.ONE);
79216
+ }
79217
+ static fromBytes(bytes) {
79218
+ const P3 = Point.fromAffine(decodePoint(_abytes2(bytes, void 0, "point")));
79219
+ P3.assertValidity();
79220
+ return P3;
79221
+ }
79222
+ static fromHex(hex) {
79223
+ return Point.fromBytes(ensureBytes("pointHex", hex));
79224
+ }
79225
+ get x() {
79226
+ return this.toAffine().x;
79227
+ }
79228
+ get y() {
79229
+ return this.toAffine().y;
79230
+ }
79231
+ /**
79232
+ *
79233
+ * @param windowSize
79234
+ * @param isLazy true will defer table computation until the first multiplication
79235
+ * @returns
79236
+ */
79237
+ precompute(windowSize = 8, isLazy = true) {
79238
+ wnaf.createCache(this, windowSize);
79239
+ if (!isLazy)
79240
+ this.multiply(_3n3);
79241
+ return this;
79242
+ }
79243
+ // TODO: return `this`
79244
+ /** A point on curve is valid if it conforms to equation. */
79245
+ assertValidity() {
79246
+ assertValidMemo(this);
79247
+ }
79248
+ hasEvenY() {
79249
+ const { y: y2 } = this.toAffine();
79250
+ if (!Fp.isOdd)
79251
+ throw new Error("Field doesn't support isOdd");
79252
+ return !Fp.isOdd(y2);
79253
+ }
79254
+ /** Compare one point to another. */
79255
+ equals(other) {
79256
+ aprjpoint(other);
79257
+ const { X: X1, Y: Y1, Z: Z1 } = this;
79258
+ const { X: X2, Y: Y2, Z: Z2 } = other;
79259
+ const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
79260
+ const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
79261
+ return U1 && U2;
79262
+ }
79263
+ /** Flips point to one corresponding to (x, -y) in Affine coordinates. */
79264
+ negate() {
79265
+ return new Point(this.X, Fp.neg(this.Y), this.Z);
79266
+ }
79267
+ // Renes-Costello-Batina exception-free doubling formula.
79268
+ // There is 30% faster Jacobian formula, but it is not complete.
79269
+ // https://eprint.iacr.org/2015/1060, algorithm 3
79270
+ // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
79271
+ double() {
79272
+ const { a, b } = CURVE;
79273
+ const b3 = Fp.mul(b, _3n3);
79274
+ const { X: X1, Y: Y1, Z: Z1 } = this;
79275
+ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
79276
+ let t0 = Fp.mul(X1, X1);
79277
+ let t1 = Fp.mul(Y1, Y1);
79278
+ let t2 = Fp.mul(Z1, Z1);
79279
+ let t3 = Fp.mul(X1, Y1);
79280
+ t3 = Fp.add(t3, t3);
79281
+ Z3 = Fp.mul(X1, Z1);
79282
+ Z3 = Fp.add(Z3, Z3);
79283
+ X3 = Fp.mul(a, Z3);
79284
+ Y3 = Fp.mul(b3, t2);
79285
+ Y3 = Fp.add(X3, Y3);
79286
+ X3 = Fp.sub(t1, Y3);
79287
+ Y3 = Fp.add(t1, Y3);
79288
+ Y3 = Fp.mul(X3, Y3);
79289
+ X3 = Fp.mul(t3, X3);
79290
+ Z3 = Fp.mul(b3, Z3);
79291
+ t2 = Fp.mul(a, t2);
79292
+ t3 = Fp.sub(t0, t2);
79293
+ t3 = Fp.mul(a, t3);
79294
+ t3 = Fp.add(t3, Z3);
79295
+ Z3 = Fp.add(t0, t0);
79296
+ t0 = Fp.add(Z3, t0);
79297
+ t0 = Fp.add(t0, t2);
79298
+ t0 = Fp.mul(t0, t3);
79299
+ Y3 = Fp.add(Y3, t0);
79300
+ t2 = Fp.mul(Y1, Z1);
79301
+ t2 = Fp.add(t2, t2);
79302
+ t0 = Fp.mul(t2, t3);
79303
+ X3 = Fp.sub(X3, t0);
79304
+ Z3 = Fp.mul(t2, t1);
79305
+ Z3 = Fp.add(Z3, Z3);
79306
+ Z3 = Fp.add(Z3, Z3);
79307
+ return new Point(X3, Y3, Z3);
79308
+ }
79309
+ // Renes-Costello-Batina exception-free addition formula.
79310
+ // There is 30% faster Jacobian formula, but it is not complete.
79311
+ // https://eprint.iacr.org/2015/1060, algorithm 1
79312
+ // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
79313
+ add(other) {
79314
+ aprjpoint(other);
79315
+ const { X: X1, Y: Y1, Z: Z1 } = this;
79316
+ const { X: X2, Y: Y2, Z: Z2 } = other;
79317
+ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
79318
+ const a = CURVE.a;
79319
+ const b3 = Fp.mul(CURVE.b, _3n3);
79320
+ let t0 = Fp.mul(X1, X2);
79321
+ let t1 = Fp.mul(Y1, Y2);
79322
+ let t2 = Fp.mul(Z1, Z2);
79323
+ let t3 = Fp.add(X1, Y1);
79324
+ let t4 = Fp.add(X2, Y2);
79325
+ t3 = Fp.mul(t3, t4);
79326
+ t4 = Fp.add(t0, t1);
79327
+ t3 = Fp.sub(t3, t4);
79328
+ t4 = Fp.add(X1, Z1);
79329
+ let t5 = Fp.add(X2, Z2);
79330
+ t4 = Fp.mul(t4, t5);
79331
+ t5 = Fp.add(t0, t2);
79332
+ t4 = Fp.sub(t4, t5);
79333
+ t5 = Fp.add(Y1, Z1);
79334
+ X3 = Fp.add(Y2, Z2);
79335
+ t5 = Fp.mul(t5, X3);
79336
+ X3 = Fp.add(t1, t2);
79337
+ t5 = Fp.sub(t5, X3);
79338
+ Z3 = Fp.mul(a, t4);
79339
+ X3 = Fp.mul(b3, t2);
79340
+ Z3 = Fp.add(X3, Z3);
79341
+ X3 = Fp.sub(t1, Z3);
79342
+ Z3 = Fp.add(t1, Z3);
79343
+ Y3 = Fp.mul(X3, Z3);
79344
+ t1 = Fp.add(t0, t0);
79345
+ t1 = Fp.add(t1, t0);
79346
+ t2 = Fp.mul(a, t2);
79347
+ t4 = Fp.mul(b3, t4);
79348
+ t1 = Fp.add(t1, t2);
79349
+ t2 = Fp.sub(t0, t2);
79350
+ t2 = Fp.mul(a, t2);
79351
+ t4 = Fp.add(t4, t2);
79352
+ t0 = Fp.mul(t1, t4);
79353
+ Y3 = Fp.add(Y3, t0);
79354
+ t0 = Fp.mul(t5, t4);
79355
+ X3 = Fp.mul(t3, X3);
79356
+ X3 = Fp.sub(X3, t0);
79357
+ t0 = Fp.mul(t3, t1);
79358
+ Z3 = Fp.mul(t5, Z3);
79359
+ Z3 = Fp.add(Z3, t0);
79360
+ return new Point(X3, Y3, Z3);
79361
+ }
79362
+ subtract(other) {
79363
+ return this.add(other.negate());
79364
+ }
79365
+ is0() {
79366
+ return this.equals(Point.ZERO);
79367
+ }
79368
+ /**
79369
+ * Constant time multiplication.
79370
+ * Uses wNAF method. Windowed method may be 10% faster,
79371
+ * but takes 2x longer to generate and consumes 2x memory.
79372
+ * Uses precomputes when available.
79373
+ * Uses endomorphism for Koblitz curves.
79374
+ * @param scalar by which the point would be multiplied
79375
+ * @returns New point
79376
+ */
79377
+ multiply(scalar) {
79378
+ const { endo: endo2 } = extraOpts;
79379
+ if (!Fn.isValidNot0(scalar))
79380
+ throw new Error("invalid scalar: out of range");
79381
+ let point, fake;
79382
+ const mul2 = (n) => wnaf.cached(this, n, (p) => normalizeZ2(Point, p));
79383
+ if (endo2) {
79384
+ const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
79385
+ const { p: k1p, f: k1f } = mul2(k1);
79386
+ const { p: k2p, f: k2f } = mul2(k2);
79387
+ fake = k1f.add(k2f);
79388
+ point = finishEndo(endo2.beta, k1p, k2p, k1neg, k2neg);
79389
+ } else {
79390
+ const { p, f } = mul2(scalar);
79391
+ point = p;
79392
+ fake = f;
79393
+ }
79394
+ return normalizeZ2(Point, [point, fake])[0];
79395
+ }
79396
+ /**
79397
+ * Non-constant-time multiplication. Uses double-and-add algorithm.
79398
+ * It's faster, but should only be used when you don't care about
79399
+ * an exposed secret key e.g. sig verification, which works over *public* keys.
79400
+ */
79401
+ multiplyUnsafe(sc) {
79402
+ const { endo: endo2 } = extraOpts;
79403
+ const p = this;
79404
+ if (!Fn.isValid(sc))
79405
+ throw new Error("invalid scalar: out of range");
79406
+ if (sc === _0n8 || p.is0())
79407
+ return Point.ZERO;
79408
+ if (sc === _1n9)
79409
+ return p;
79410
+ if (wnaf.hasCache(this))
79411
+ return this.multiply(sc);
79412
+ if (endo2) {
79413
+ const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
79414
+ const { p1, p2 } = mulEndoUnsafe(Point, p, k1, k2);
79415
+ return finishEndo(endo2.beta, p1, p2, k1neg, k2neg);
79416
+ } else {
79417
+ return wnaf.unsafe(p, sc);
79418
+ }
79419
+ }
79420
+ multiplyAndAddUnsafe(Q, a, b) {
79421
+ const sum2 = this.multiplyUnsafe(a).add(Q.multiplyUnsafe(b));
79422
+ return sum2.is0() ? void 0 : sum2;
79423
+ }
79424
+ /**
79425
+ * Converts Projective point to affine (x, y) coordinates.
79426
+ * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
79427
+ */
79428
+ toAffine(invertedZ) {
79429
+ return toAffineMemo(this, invertedZ);
79430
+ }
79431
+ /**
79432
+ * Checks whether Point is free of torsion elements (is in prime subgroup).
79433
+ * Always torsion-free for cofactor=1 curves.
79434
+ */
79435
+ isTorsionFree() {
79436
+ const { isTorsionFree } = extraOpts;
79437
+ if (cofactor === _1n9)
79438
+ return true;
79439
+ if (isTorsionFree)
79440
+ return isTorsionFree(Point, this);
79441
+ return wnaf.unsafe(this, CURVE_ORDER).is0();
79442
+ }
79443
+ clearCofactor() {
79444
+ const { clearCofactor } = extraOpts;
79445
+ if (cofactor === _1n9)
79446
+ return this;
79447
+ if (clearCofactor)
79448
+ return clearCofactor(Point, this);
79449
+ return this.multiplyUnsafe(cofactor);
79450
+ }
79451
+ isSmallOrder() {
79452
+ return this.multiplyUnsafe(cofactor).is0();
79453
+ }
79454
+ toBytes(isCompressed = true) {
79455
+ _abool2(isCompressed, "isCompressed");
79456
+ this.assertValidity();
79457
+ return encodePoint(Point, this, isCompressed);
79458
+ }
79459
+ toHex(isCompressed = true) {
79460
+ return bytesToHex2(this.toBytes(isCompressed));
79461
+ }
79462
+ toString() {
79463
+ return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
79464
+ }
79465
+ // TODO: remove
79466
+ get px() {
79467
+ return this.X;
79468
+ }
79469
+ get py() {
79470
+ return this.X;
79471
+ }
79472
+ get pz() {
79473
+ return this.Z;
79474
+ }
79475
+ toRawBytes(isCompressed = true) {
79476
+ return this.toBytes(isCompressed);
79477
+ }
79478
+ _setWindowSize(windowSize) {
79479
+ this.precompute(windowSize);
79480
+ }
79481
+ static normalizeZ(points) {
79482
+ return normalizeZ2(Point, points);
79483
+ }
79484
+ static msm(points, scalars) {
79485
+ return pippenger(Point, Fn, points, scalars);
79486
+ }
79487
+ static fromPrivateKey(privateKey) {
79488
+ return Point.BASE.multiply(_normFnElement(Fn, privateKey));
79489
+ }
79490
+ }
79491
+ Point.BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
79492
+ Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
79493
+ Point.Fp = Fp;
79494
+ Point.Fn = Fn;
79495
+ const bits = Fn.BITS;
79496
+ const wnaf = new wNAF2(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
79497
+ Point.BASE.precompute(8);
79498
+ return Point;
79499
+ }
79500
+ function pprefix(hasEvenY) {
79501
+ return Uint8Array.of(hasEvenY ? 2 : 3);
79502
+ }
79503
+ function getWLengths(Fp, Fn) {
79504
+ return {
79505
+ secretKey: Fn.BYTES,
79506
+ publicKey: 1 + Fp.BYTES,
79507
+ publicKeyUncompressed: 1 + 2 * Fp.BYTES,
79508
+ publicKeyHasPrefix: true,
79509
+ signature: 2 * Fn.BYTES
79510
+ };
79511
+ }
79512
+ function ecdh(Point, ecdhOpts = {}) {
79513
+ const { Fn } = Point;
79514
+ const randomBytes_ = ecdhOpts.randomBytes || randomBytes2;
79515
+ const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: getMinHashLength(Fn.ORDER) });
79516
+ function isValidSecretKey(secretKey) {
79517
+ try {
79518
+ return !!_normFnElement(Fn, secretKey);
79519
+ } catch (error2) {
79520
+ return false;
79521
+ }
79522
+ }
79523
+ function isValidPublicKey(publicKey, isCompressed) {
79524
+ const { publicKey: comp, publicKeyUncompressed } = lengths;
79525
+ try {
79526
+ const l2 = publicKey.length;
79527
+ if (isCompressed === true && l2 !== comp)
79528
+ return false;
79529
+ if (isCompressed === false && l2 !== publicKeyUncompressed)
79530
+ return false;
79531
+ return !!Point.fromBytes(publicKey);
79532
+ } catch (error2) {
79533
+ return false;
79534
+ }
79535
+ }
79536
+ function randomSecretKey(seed = randomBytes_(lengths.seed)) {
79537
+ return mapHashToField(_abytes2(seed, lengths.seed, "seed"), Fn.ORDER);
79538
+ }
79539
+ function getPublicKey(secretKey, isCompressed = true) {
79540
+ return Point.BASE.multiply(_normFnElement(Fn, secretKey)).toBytes(isCompressed);
79541
+ }
79542
+ function keygen(seed) {
79543
+ const secretKey = randomSecretKey(seed);
79544
+ return { secretKey, publicKey: getPublicKey(secretKey) };
79545
+ }
79546
+ function isProbPub(item) {
79547
+ if (typeof item === "bigint")
79548
+ return false;
79549
+ if (item instanceof Point)
79550
+ return true;
79551
+ const { secretKey, publicKey, publicKeyUncompressed } = lengths;
79552
+ if (Fn.allowedLengths || secretKey === publicKey)
79553
+ return void 0;
79554
+ const l2 = ensureBytes("key", item).length;
79555
+ return l2 === publicKey || l2 === publicKeyUncompressed;
79556
+ }
79557
+ function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
79558
+ if (isProbPub(secretKeyA) === true)
79559
+ throw new Error("first arg must be private key");
79560
+ if (isProbPub(publicKeyB) === false)
79561
+ throw new Error("second arg must be public key");
79562
+ const s = _normFnElement(Fn, secretKeyA);
79563
+ const b = Point.fromHex(publicKeyB);
79564
+ return b.multiply(s).toBytes(isCompressed);
79565
+ }
79566
+ const utils = {
79567
+ isValidSecretKey,
79568
+ isValidPublicKey,
79569
+ randomSecretKey,
79570
+ // TODO: remove
79571
+ isValidPrivateKey: isValidSecretKey,
79572
+ randomPrivateKey: randomSecretKey,
79573
+ normPrivateKeyToScalar: (key) => _normFnElement(Fn, key),
79574
+ precompute(windowSize = 8, point = Point.BASE) {
79575
+ return point.precompute(windowSize, false);
79576
+ }
79577
+ };
79578
+ return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
79579
+ }
79580
+ function ecdsa(Point, hash, ecdsaOpts = {}) {
79581
+ ahash2(hash);
79582
+ _validateObject(ecdsaOpts, {}, {
79583
+ hmac: "function",
79584
+ lowS: "boolean",
79585
+ randomBytes: "function",
79586
+ bits2int: "function",
79587
+ bits2int_modN: "function"
79588
+ });
79589
+ const randomBytes3 = ecdsaOpts.randomBytes || randomBytes2;
79590
+ const hmac3 = ecdsaOpts.hmac || ((key, ...msgs) => hmac2(hash, key, concatBytes3(...msgs)));
79591
+ const { Fp, Fn } = Point;
79592
+ const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
79593
+ const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
79594
+ const defaultSigOpts = {
79595
+ prehash: false,
79596
+ lowS: typeof ecdsaOpts.lowS === "boolean" ? ecdsaOpts.lowS : false,
79597
+ format: void 0,
79598
+ //'compact' as ECDSASigFormat,
79599
+ extraEntropy: false
79600
+ };
79601
+ const defaultSigOpts_format = "compact";
79602
+ function isBiggerThanHalfOrder(number4) {
79603
+ const HALF = CURVE_ORDER >> _1n9;
79604
+ return number4 > HALF;
79605
+ }
79606
+ function validateRS(title, num) {
79607
+ if (!Fn.isValidNot0(num))
79608
+ throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
79609
+ return num;
79610
+ }
79611
+ function validateSigLength(bytes, format) {
79612
+ validateSigFormat(format);
79613
+ const size = lengths.signature;
79614
+ const sizer = format === "compact" ? size : format === "recovered" ? size + 1 : void 0;
79615
+ return _abytes2(bytes, sizer, `${format} signature`);
79616
+ }
79617
+ class Signature {
79618
+ constructor(r, s, recovery) {
79619
+ this.r = validateRS("r", r);
79620
+ this.s = validateRS("s", s);
79621
+ if (recovery != null)
79622
+ this.recovery = recovery;
79623
+ Object.freeze(this);
79624
+ }
79625
+ static fromBytes(bytes, format = defaultSigOpts_format) {
79626
+ validateSigLength(bytes, format);
79627
+ let recid;
79628
+ if (format === "der") {
79629
+ const { r: r2, s: s2 } = DER.toSig(_abytes2(bytes));
79630
+ return new Signature(r2, s2);
79631
+ }
79632
+ if (format === "recovered") {
79633
+ recid = bytes[0];
79634
+ format = "compact";
79635
+ bytes = bytes.subarray(1);
79636
+ }
79637
+ const L = Fn.BYTES;
79638
+ const r = bytes.subarray(0, L);
79639
+ const s = bytes.subarray(L, L * 2);
79640
+ return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
79641
+ }
79642
+ static fromHex(hex, format) {
79643
+ return this.fromBytes(hexToBytes2(hex), format);
79644
+ }
79645
+ addRecoveryBit(recovery) {
79646
+ return new Signature(this.r, this.s, recovery);
79647
+ }
79648
+ recoverPublicKey(messageHash) {
79649
+ const FIELD_ORDER = Fp.ORDER;
79650
+ const { r, s, recovery: rec } = this;
79651
+ if (rec == null || ![0, 1, 2, 3].includes(rec))
79652
+ throw new Error("recovery id invalid");
79653
+ const hasCofactor = CURVE_ORDER * _2n5 < FIELD_ORDER;
79654
+ if (hasCofactor && rec > 1)
79655
+ throw new Error("recovery id is ambiguous for h>1 curve");
79656
+ const radj = rec === 2 || rec === 3 ? r + CURVE_ORDER : r;
79657
+ if (!Fp.isValid(radj))
79658
+ throw new Error("recovery id 2 or 3 invalid");
79659
+ const x = Fp.toBytes(radj);
79660
+ const R2 = Point.fromBytes(concatBytes3(pprefix((rec & 1) === 0), x));
79661
+ const ir = Fn.inv(radj);
79662
+ const h = bits2int_modN(ensureBytes("msgHash", messageHash));
79663
+ const u1 = Fn.create(-h * ir);
79664
+ const u2 = Fn.create(s * ir);
79665
+ const Q = Point.BASE.multiplyUnsafe(u1).add(R2.multiplyUnsafe(u2));
79666
+ if (Q.is0())
79667
+ throw new Error("point at infinify");
79668
+ Q.assertValidity();
79669
+ return Q;
79670
+ }
79671
+ // Signatures should be low-s, to prevent malleability.
79672
+ hasHighS() {
79673
+ return isBiggerThanHalfOrder(this.s);
79674
+ }
79675
+ toBytes(format = defaultSigOpts_format) {
79676
+ validateSigFormat(format);
79677
+ if (format === "der")
79678
+ return hexToBytes2(DER.hexFromSig(this));
79679
+ const r = Fn.toBytes(this.r);
79680
+ const s = Fn.toBytes(this.s);
79681
+ if (format === "recovered") {
79682
+ if (this.recovery == null)
79683
+ throw new Error("recovery bit must be present");
79684
+ return concatBytes3(Uint8Array.of(this.recovery), r, s);
79685
+ }
79686
+ return concatBytes3(r, s);
79687
+ }
79688
+ toHex(format) {
79689
+ return bytesToHex2(this.toBytes(format));
79690
+ }
79691
+ // TODO: remove
79692
+ assertValidity() {
79693
+ }
79694
+ static fromCompact(hex) {
79695
+ return Signature.fromBytes(ensureBytes("sig", hex), "compact");
79696
+ }
79697
+ static fromDER(hex) {
79698
+ return Signature.fromBytes(ensureBytes("sig", hex), "der");
79699
+ }
79700
+ normalizeS() {
79701
+ return this.hasHighS() ? new Signature(this.r, Fn.neg(this.s), this.recovery) : this;
79702
+ }
79703
+ toDERRawBytes() {
79704
+ return this.toBytes("der");
79705
+ }
79706
+ toDERHex() {
79707
+ return bytesToHex2(this.toBytes("der"));
79708
+ }
79709
+ toCompactRawBytes() {
79710
+ return this.toBytes("compact");
79711
+ }
79712
+ toCompactHex() {
79713
+ return bytesToHex2(this.toBytes("compact"));
79714
+ }
79715
+ }
79716
+ const bits2int = ecdsaOpts.bits2int || function bits2int_def(bytes) {
79717
+ if (bytes.length > 8192)
79718
+ throw new Error("input is too large");
79719
+ const num = bytesToNumberBE2(bytes);
79720
+ const delta = bytes.length * 8 - fnBits;
79721
+ return delta > 0 ? num >> BigInt(delta) : num;
79722
+ };
79723
+ const bits2int_modN = ecdsaOpts.bits2int_modN || function bits2int_modN_def(bytes) {
79724
+ return Fn.create(bits2int(bytes));
79725
+ };
79726
+ const ORDER_MASK = bitMask2(fnBits);
79727
+ function int2octets(num) {
79728
+ aInRange2("num < 2^" + fnBits, num, _0n8, ORDER_MASK);
79729
+ return Fn.toBytes(num);
79730
+ }
79731
+ function validateMsgAndHash(message, prehash) {
79732
+ _abytes2(message, void 0, "message");
79733
+ return prehash ? _abytes2(hash(message), void 0, "prehashed message") : message;
79734
+ }
79735
+ function prepSig(message, privateKey, opts) {
79736
+ if (["recovered", "canonical"].some((k) => k in opts))
79737
+ throw new Error("sign() legacy options not supported");
79738
+ const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
79739
+ message = validateMsgAndHash(message, prehash);
79740
+ const h1int = bits2int_modN(message);
79741
+ const d = _normFnElement(Fn, privateKey);
79742
+ const seedArgs = [int2octets(d), int2octets(h1int)];
79743
+ if (extraEntropy != null && extraEntropy !== false) {
79744
+ const e = extraEntropy === true ? randomBytes3(lengths.secretKey) : extraEntropy;
79745
+ seedArgs.push(ensureBytes("extraEntropy", e));
79746
+ }
79747
+ const seed = concatBytes3(...seedArgs);
79748
+ const m = h1int;
79749
+ function k2sig(kBytes) {
79750
+ const k = bits2int(kBytes);
79751
+ if (!Fn.isValidNot0(k))
79752
+ return;
79753
+ const ik = Fn.inv(k);
79754
+ const q = Point.BASE.multiply(k).toAffine();
79755
+ const r = Fn.create(q.x);
79756
+ if (r === _0n8)
79757
+ return;
79758
+ const s = Fn.create(ik * Fn.create(m + r * d));
79759
+ if (s === _0n8)
79760
+ return;
79761
+ let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n9);
79762
+ let normS = s;
79763
+ if (lowS && isBiggerThanHalfOrder(s)) {
79764
+ normS = Fn.neg(s);
79765
+ recovery ^= 1;
79766
+ }
79767
+ return new Signature(r, normS, recovery);
79768
+ }
79769
+ return { seed, k2sig };
79770
+ }
79771
+ function sign2(message, secretKey, opts = {}) {
79772
+ message = ensureBytes("message", message);
79773
+ const { seed, k2sig } = prepSig(message, secretKey, opts);
79774
+ const drbg = createHmacDrbg(hash.outputLen, Fn.BYTES, hmac3);
79775
+ const sig = drbg(seed, k2sig);
79776
+ return sig;
79777
+ }
79778
+ function tryParsingSig(sg) {
79779
+ let sig = void 0;
79780
+ const isHex3 = typeof sg === "string" || isBytes3(sg);
79781
+ const isObj = !isHex3 && sg !== null && typeof sg === "object" && typeof sg.r === "bigint" && typeof sg.s === "bigint";
79782
+ if (!isHex3 && !isObj)
79783
+ throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
79784
+ if (isObj) {
79785
+ sig = new Signature(sg.r, sg.s);
79786
+ } else if (isHex3) {
79787
+ try {
79788
+ sig = Signature.fromBytes(ensureBytes("sig", sg), "der");
79789
+ } catch (derError) {
79790
+ if (!(derError instanceof DER.Err))
79791
+ throw derError;
79792
+ }
79793
+ if (!sig) {
79794
+ try {
79795
+ sig = Signature.fromBytes(ensureBytes("sig", sg), "compact");
79796
+ } catch (error2) {
79797
+ return false;
79798
+ }
79799
+ }
79800
+ }
79801
+ if (!sig)
79802
+ return false;
79803
+ return sig;
79804
+ }
79805
+ function verify(signature, message, publicKey, opts = {}) {
79806
+ const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
79807
+ publicKey = ensureBytes("publicKey", publicKey);
79808
+ message = validateMsgAndHash(ensureBytes("message", message), prehash);
79809
+ if ("strict" in opts)
79810
+ throw new Error("options.strict was renamed to lowS");
79811
+ const sig = format === void 0 ? tryParsingSig(signature) : Signature.fromBytes(ensureBytes("sig", signature), format);
79812
+ if (sig === false)
79813
+ return false;
79814
+ try {
79815
+ const P3 = Point.fromBytes(publicKey);
79816
+ if (lowS && sig.hasHighS())
79817
+ return false;
79818
+ const { r, s } = sig;
79819
+ const h = bits2int_modN(message);
79820
+ const is2 = Fn.inv(s);
79821
+ const u1 = Fn.create(h * is2);
79822
+ const u2 = Fn.create(r * is2);
79823
+ const R2 = Point.BASE.multiplyUnsafe(u1).add(P3.multiplyUnsafe(u2));
79824
+ if (R2.is0())
79825
+ return false;
79826
+ const v = Fn.create(R2.x);
79827
+ return v === r;
79828
+ } catch (e) {
79829
+ return false;
79830
+ }
79831
+ }
79832
+ function recoverPublicKey(signature, message, opts = {}) {
79833
+ const { prehash } = validateSigOpts(opts, defaultSigOpts);
79834
+ message = validateMsgAndHash(message, prehash);
79835
+ return Signature.fromBytes(signature, "recovered").recoverPublicKey(message).toBytes();
79836
+ }
79837
+ return Object.freeze({
79838
+ keygen,
79839
+ getPublicKey,
79840
+ getSharedSecret,
79841
+ utils,
79842
+ lengths,
79843
+ Point,
79844
+ sign: sign2,
79845
+ verify,
79846
+ recoverPublicKey,
79847
+ Signature,
79848
+ hash
79849
+ });
79850
+ }
79851
+ function _weierstrass_legacy_opts_to_new(c) {
79852
+ const CURVE = {
79853
+ a: c.a,
79854
+ b: c.b,
79855
+ p: c.Fp.ORDER,
79856
+ n: c.n,
79857
+ h: c.h,
79858
+ Gx: c.Gx,
79859
+ Gy: c.Gy
79860
+ };
79861
+ const Fp = c.Fp;
79862
+ let allowedLengths = c.allowedPrivateKeyLengths ? Array.from(new Set(c.allowedPrivateKeyLengths.map((l2) => Math.ceil(l2 / 2)))) : void 0;
79863
+ const Fn = Field2(CURVE.n, {
79864
+ BITS: c.nBitLength,
79865
+ allowedLengths,
79866
+ modFromBytes: c.wrapPrivateKey
79867
+ });
79868
+ const curveOpts = {
79869
+ Fp,
79870
+ Fn,
79871
+ allowInfinityPoint: c.allowInfinityPoint,
79872
+ endo: c.endo,
79873
+ isTorsionFree: c.isTorsionFree,
79874
+ clearCofactor: c.clearCofactor,
79875
+ fromBytes: c.fromBytes,
79876
+ toBytes: c.toBytes
79877
+ };
79878
+ return { CURVE, curveOpts };
79879
+ }
79880
+ function _ecdsa_legacy_opts_to_new(c) {
79881
+ const { CURVE, curveOpts } = _weierstrass_legacy_opts_to_new(c);
79882
+ const ecdsaOpts = {
79883
+ hmac: c.hmac,
79884
+ randomBytes: c.randomBytes,
79885
+ lowS: c.lowS,
79886
+ bits2int: c.bits2int,
79887
+ bits2int_modN: c.bits2int_modN
79888
+ };
79889
+ return { CURVE, curveOpts, hash: c.hash, ecdsaOpts };
79890
+ }
79891
+ function _ecdsa_new_output_to_legacy(c, _ecdsa) {
79892
+ const Point = _ecdsa.Point;
79893
+ return Object.assign({}, _ecdsa, {
79894
+ ProjectivePoint: Point,
79895
+ CURVE: Object.assign({}, c, nLength2(Point.Fn.ORDER, Point.Fn.BITS))
79896
+ });
79897
+ }
79898
+ function weierstrass(c) {
79899
+ const { CURVE, curveOpts, hash, ecdsaOpts } = _ecdsa_legacy_opts_to_new(c);
79900
+ const Point = weierstrassN(CURVE, curveOpts);
79901
+ const signs = ecdsa(Point, hash, ecdsaOpts);
79902
+ return _ecdsa_new_output_to_legacy(c, signs);
79903
+ }
79904
+
79905
+ // ../../node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/_shortw_utils.js
79906
+ function createCurve(curveDef, defHash) {
79907
+ const create = (hash) => weierstrass({ ...curveDef, hash });
79908
+ return { ...create(defHash), create };
79909
+ }
79910
+
79911
+ // ../../node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/secp256k1.js
79912
+ var secp256k1_CURVE = {
79913
+ p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
79914
+ n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
79915
+ h: BigInt(1),
79916
+ a: BigInt(0),
79917
+ b: BigInt(7),
79918
+ Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
79919
+ Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
79920
+ };
79921
+ var secp256k1_ENDO = {
79922
+ beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
79923
+ basises: [
79924
+ [BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
79925
+ [BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
79926
+ ]
79927
+ };
79928
+ var _2n6 = /* @__PURE__ */ BigInt(2);
79929
+ function sqrtMod(y2) {
79930
+ const P3 = secp256k1_CURVE.p;
79931
+ const _3n4 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
79932
+ const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
79933
+ const b2 = y2 * y2 * y2 % P3;
79934
+ const b3 = b2 * b2 * y2 % P3;
79935
+ const b6 = pow22(b3, _3n4, P3) * b3 % P3;
79936
+ const b9 = pow22(b6, _3n4, P3) * b3 % P3;
79937
+ const b11 = pow22(b9, _2n6, P3) * b2 % P3;
79938
+ const b22 = pow22(b11, _11n, P3) * b11 % P3;
79939
+ const b44 = pow22(b22, _22n, P3) * b22 % P3;
79940
+ const b88 = pow22(b44, _44n, P3) * b44 % P3;
79941
+ const b176 = pow22(b88, _88n, P3) * b88 % P3;
79942
+ const b220 = pow22(b176, _44n, P3) * b44 % P3;
79943
+ const b223 = pow22(b220, _3n4, P3) * b3 % P3;
79944
+ const t1 = pow22(b223, _23n, P3) * b22 % P3;
79945
+ const t2 = pow22(t1, _6n, P3) * b2 % P3;
79946
+ const root = pow22(t2, _2n6, P3);
79947
+ if (!Fpk1.eql(Fpk1.sqr(root), y2))
79948
+ throw new Error("Cannot find square root");
79949
+ return root;
79950
+ }
79951
+ var Fpk1 = Field2(secp256k1_CURVE.p, { sqrt: sqrtMod });
79952
+ var secp256k1 = createCurve({ ...secp256k1_CURVE, Fp: Fpk1, lowS: true, endo: secp256k1_ENDO }, sha2562);
79953
+
79954
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/sha256.js
79955
+ var sha2563 = sha2562;
77651
79956
  var __defProp4 = Object.defineProperty;
77652
79957
  var __getOwnPropNames3 = Object.getOwnPropertyNames;
77653
79958
  var __esm2 = (fn, res) => function __init() {
@@ -79238,10 +81543,10 @@ function recordFromGqlNode(node, address) {
79238
81543
  commandTypes.push("MoveCall");
79239
81544
  const fn = cmd.function;
79240
81545
  const pkg = fn?.module?.package?.address;
79241
- const mod3 = fn?.module?.name;
81546
+ const mod4 = fn?.module?.name;
79242
81547
  const name = fn?.name;
79243
- if (pkg && mod3 && name) {
79244
- moveCallTargets.push(`${pkg}::${mod3}::${name}`);
81548
+ if (pkg && mod4 && name) {
81549
+ moveCallTargets.push(`${pkg}::${mod4}::${name}`);
79245
81550
  }
79246
81551
  } else if (cmd.__typename === "TransferObjectsCommand") {
79247
81552
  commandTypes.push("TransferObjects");
@@ -79466,9 +81771,85 @@ async function listModels(opts) {
79466
81771
  }));
79467
81772
  }
79468
81773
  var RECEIPT_ANCHORED_SUFFIX = "::anchor::ReceiptAnchored";
81774
+ function normalizeClaims(claims) {
81775
+ if (!claims) {
81776
+ return [];
81777
+ }
81778
+ if (Array.isArray(claims)) {
81779
+ return claims.filter((c) => c.name).map((c) => ({
81780
+ name: c.name,
81781
+ status: c.status ?? "unknown",
81782
+ source: c.source
81783
+ }));
81784
+ }
81785
+ return Object.entries(claims).map(([name, v]) => ({
81786
+ name,
81787
+ status: v?.status ?? "unknown",
81788
+ source: v?.source
81789
+ }));
81790
+ }
79469
81791
  function fullnodeUrl(network) {
79470
81792
  return network === "testnet" ? "https://fullnode.testnet.sui.io" : "https://fullnode.mainnet.sui.io";
79471
81793
  }
81794
+ function jcs(value) {
81795
+ if (value === null) {
81796
+ return "null";
81797
+ }
81798
+ if (typeof value === "boolean") {
81799
+ return value ? "true" : "false";
81800
+ }
81801
+ if (typeof value === "number") {
81802
+ if (!Number.isInteger(value)) {
81803
+ throw new Error("JCS: non-integer number");
81804
+ }
81805
+ return String(value);
81806
+ }
81807
+ if (typeof value === "string") {
81808
+ return JSON.stringify(value);
81809
+ }
81810
+ if (Array.isArray(value)) {
81811
+ return `[${value.map(jcs).join(",")}]`;
81812
+ }
81813
+ const keys = Object.keys(value).sort();
81814
+ return `{${keys.map((k) => `${JSON.stringify(k)}:${jcs(value[k])}`).join(",")}}`;
81815
+ }
81816
+ function verifyReceiptSignature(receipt, signingKeyHex) {
81817
+ try {
81818
+ const sig = receipt.signature;
81819
+ if (sig?.algo !== "ecdsa-secp256k1" || !sig.value) {
81820
+ return false;
81821
+ }
81822
+ const canonical = {
81823
+ api_version: receipt.api_version ?? "",
81824
+ receipt_id: receipt.receipt_id ?? "",
81825
+ chat_id: receipt.chat_id ?? null,
81826
+ workload_id: receipt.workload_id ?? "",
81827
+ workload_keyset_digest: receipt.workload_keyset_digest ?? "",
81828
+ endpoint: receipt.endpoint ?? "",
81829
+ method: receipt.method ?? "",
81830
+ served_at: receipt.served_at ?? 0,
81831
+ event_log: receipt.event_log ?? [],
81832
+ signature: { algo: sig.algo, key_id: sig.key_id ?? "" }
81833
+ };
81834
+ const prehash = sha2563(new TextEncoder().encode(jcs(canonical)));
81835
+ const sigBytes = hexToBytes2(sig.value);
81836
+ if (sigBytes.length !== 65) {
81837
+ return false;
81838
+ }
81839
+ let v = sigBytes[64];
81840
+ if (v >= 27 && v <= 30) {
81841
+ v -= 27;
81842
+ }
81843
+ if (v > 3) {
81844
+ return false;
81845
+ }
81846
+ const recovered = secp256k1.Signature.fromCompact(sigBytes.slice(0, 64)).addRecoveryBit(v).recoverPublicKey(prehash).toHex(false);
81847
+ const endorsed = bytesToHex2(hexToBytes2(signingKeyHex.replace(/^0x/, "")));
81848
+ return recovered.toLowerCase() === endorsed.toLowerCase();
81849
+ } catch {
81850
+ return false;
81851
+ }
81852
+ }
79472
81853
  async function verifyReceipt(receiptId, opts = {}) {
79473
81854
  const base = opts.apiBase ?? DEFAULT_API_BASE;
79474
81855
  const network = opts.network ?? "mainnet";
@@ -79500,6 +81881,7 @@ async function verifyReceipt(receiptId, opts = {}) {
79500
81881
  });
79501
81882
  const upstreamEv = receipt.event_log.find((e) => e.type === "upstream.verified");
79502
81883
  const upstreamOk = upstreamEv?.result === "verified";
81884
+ const claims = normalizeClaims(upstreamEv?.claims);
79503
81885
  checks.push({
79504
81886
  name: "Confidential upstream",
79505
81887
  status: upstreamEv ? upstreamOk ? "pass" : "fail" : "skip",
@@ -79562,23 +81944,50 @@ async function verifyReceipt(receiptId, opts = {}) {
79562
81944
  });
79563
81945
  }
79564
81946
  }
81947
+ let signatureForged = false;
81948
+ let sigStatus = "skip";
81949
+ let sigDetail = "no signature on receipt";
81950
+ if (receipt.signature?.value) {
81951
+ try {
81952
+ const model = opts.model ?? "phala/glm-5.2";
81953
+ const res = await fetch(
81954
+ `${base}/aci/attestation?model=${encodeURIComponent(model)}`
81955
+ );
81956
+ const att = res.ok ? await res.json() : null;
81957
+ if (!att?.signingKey) {
81958
+ sigDetail = "could not fetch the attested keyset to check the signature";
81959
+ } else if (att.workloadId && att.workloadId !== workloadId) {
81960
+ sigDetail = `attested keyset is for a different workload \u2014 pass --model for ${workloadId}`;
81961
+ } else {
81962
+ const ok = verifyReceiptSignature(receipt, att.signingKey);
81963
+ sigStatus = ok ? "pass" : "fail";
81964
+ signatureForged = !ok;
81965
+ sigDetail = ok ? `signed by the attested receipt key (${receipt.signature.key_id ?? "key"})` : "signature does NOT recover the attested receipt key \u2014 forged/altered";
81966
+ }
81967
+ } catch {
81968
+ sigDetail = "signature check errored";
81969
+ }
81970
+ }
79565
81971
  checks.push({
79566
81972
  name: "Receipt signature",
79567
- status: "skip",
79568
- detail: receipt.signature ? "present (local recompute via RedPill keyset canonicalization = roadmap)" : "no signature on receipt",
79569
- trust: "roadmap"
81973
+ status: sigStatus,
81974
+ detail: sigDetail,
81975
+ trust: sigStatus === "skip" ? "roadmap" : "trustless"
79570
81976
  });
79571
81977
  return {
79572
81978
  receiptId,
79573
- verified: Boolean(wireHash && workloadId) && anchorVerified,
81979
+ verified: Boolean(wireHash && workloadId) && anchorVerified && !signatureForged,
79574
81980
  anchorVerified,
79575
81981
  checks,
79576
81982
  wireHash,
79577
81983
  workloadId,
79578
81984
  upstream: upstreamEv ? {
79579
81985
  provider: upstreamEv.provider ?? upstreamEv.upstream_name,
81986
+ modelId: upstreamEv.model_id,
79580
81987
  result: upstreamEv.result,
79581
- tcbStatus: upstreamEv.tcb_status
81988
+ tcbStatus: upstreamEv.tcb_status,
81989
+ sessionId: upstreamEv.session_id,
81990
+ claims: claims.length > 0 ? claims : void 0
79582
81991
  } : void 0,
79583
81992
  anchor
79584
81993
  };
@@ -80704,7 +83113,7 @@ CRITICAL: When the user asks to use any external or paid API, names a provider (
80704
83113
  Spending is the user's own USDC and every t2000_pay call is bounded by maxPrice. For larger or multi-step spends, state the estimated cost first and proceed once the user is happy. Use t2000_balance to check funds. The v4 wallet is payments-only; savings / lending live on audric.ai.`;
80705
83114
 
80706
83115
  // src/index.ts
80707
- var PKG_VERSION = "5.17.0" ;
83116
+ var PKG_VERSION = "5.19.0" ;
80708
83117
  console.log = (...args) => console.error("[log]", ...args);
80709
83118
  console.warn = (...args) => console.error("[warn]", ...args);
80710
83119
  async function startMcpServer(opts) {
@@ -80727,6 +83136,7 @@ async function startMcpServer(opts) {
80727
83136
  (*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
80728
83137
 
80729
83138
  @noble/hashes/utils.js:
83139
+ @noble/hashes/esm/utils.js:
80730
83140
  (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
80731
83141
 
80732
83142
  mime-db/index.js:
@@ -80763,6 +83173,12 @@ mime-types/index.js:
80763
83173
  @noble/curves/abstract/curve.js:
80764
83174
  @noble/curves/abstract/edwards.js:
80765
83175
  @noble/curves/ed25519.js:
83176
+ @noble/curves/esm/utils.js:
83177
+ @noble/curves/esm/abstract/modular.js:
83178
+ @noble/curves/esm/abstract/curve.js:
83179
+ @noble/curves/esm/abstract/weierstrass.js:
83180
+ @noble/curves/esm/_shortw_utils.js:
83181
+ @noble/curves/esm/secp256k1.js:
80766
83182
  (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
80767
83183
 
80768
83184
  @scure/bip39/index.js: