@continuonai/rcan-ts 1.2.3 → 1.3.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/browser.mjs CHANGED
@@ -357,6 +357,1709 @@ function validateDelegationChain(chain) {
357
357
  }
358
358
 
359
359
  // src/crypto.ts
360
+ import { ml_dsa65 } from "@noble/post-quantum/ml-dsa.js";
361
+
362
+ // node_modules/@noble/hashes/utils.js
363
+ function isBytes(a) {
364
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
365
+ }
366
+ function anumber(n, title = "") {
367
+ if (!Number.isSafeInteger(n) || n < 0) {
368
+ const prefix = title && `"${title}" `;
369
+ throw new Error(`${prefix}expected integer >= 0, got ${n}`);
370
+ }
371
+ }
372
+ function abytes(value, length, title = "") {
373
+ const bytes = isBytes(value);
374
+ const len = value?.length;
375
+ const needsLen = length !== void 0;
376
+ if (!bytes || needsLen && len !== length) {
377
+ const prefix = title && `"${title}" `;
378
+ const ofLen = needsLen ? ` of length ${length}` : "";
379
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
380
+ throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
381
+ }
382
+ return value;
383
+ }
384
+ function aexists(instance, checkFinished = true) {
385
+ if (instance.destroyed)
386
+ throw new Error("Hash instance has been destroyed");
387
+ if (checkFinished && instance.finished)
388
+ throw new Error("Hash#digest() has already been called");
389
+ }
390
+ function aoutput(out, instance) {
391
+ abytes(out, void 0, "digestInto() output");
392
+ const min = instance.outputLen;
393
+ if (out.length < min) {
394
+ throw new Error('"digestInto() output" expected to be of length >=' + min);
395
+ }
396
+ }
397
+ function clean(...arrays) {
398
+ for (let i = 0; i < arrays.length; i++) {
399
+ arrays[i].fill(0);
400
+ }
401
+ }
402
+ function createView(arr) {
403
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
404
+ }
405
+ var hasHexBuiltin = /* @__PURE__ */ (() => (
406
+ // @ts-ignore
407
+ typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
408
+ ))();
409
+ var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
410
+ function bytesToHex(bytes) {
411
+ abytes(bytes);
412
+ if (hasHexBuiltin)
413
+ return bytes.toHex();
414
+ let hex = "";
415
+ for (let i = 0; i < bytes.length; i++) {
416
+ hex += hexes[bytes[i]];
417
+ }
418
+ return hex;
419
+ }
420
+ var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
421
+ function asciiToBase16(ch) {
422
+ if (ch >= asciis._0 && ch <= asciis._9)
423
+ return ch - asciis._0;
424
+ if (ch >= asciis.A && ch <= asciis.F)
425
+ return ch - (asciis.A - 10);
426
+ if (ch >= asciis.a && ch <= asciis.f)
427
+ return ch - (asciis.a - 10);
428
+ return;
429
+ }
430
+ function hexToBytes(hex) {
431
+ if (typeof hex !== "string")
432
+ throw new Error("hex string expected, got " + typeof hex);
433
+ if (hasHexBuiltin)
434
+ return Uint8Array.fromHex(hex);
435
+ const hl = hex.length;
436
+ const al = hl / 2;
437
+ if (hl % 2)
438
+ throw new Error("hex string expected, got unpadded hex of length " + hl);
439
+ const array = new Uint8Array(al);
440
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
441
+ const n1 = asciiToBase16(hex.charCodeAt(hi));
442
+ const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
443
+ if (n1 === void 0 || n2 === void 0) {
444
+ const char = hex[hi] + hex[hi + 1];
445
+ throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
446
+ }
447
+ array[ai] = n1 * 16 + n2;
448
+ }
449
+ return array;
450
+ }
451
+ function concatBytes(...arrays) {
452
+ let sum = 0;
453
+ for (let i = 0; i < arrays.length; i++) {
454
+ const a = arrays[i];
455
+ abytes(a);
456
+ sum += a.length;
457
+ }
458
+ const res = new Uint8Array(sum);
459
+ for (let i = 0, pad = 0; i < arrays.length; i++) {
460
+ const a = arrays[i];
461
+ res.set(a, pad);
462
+ pad += a.length;
463
+ }
464
+ return res;
465
+ }
466
+ function createHasher(hashCons, info = {}) {
467
+ const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
468
+ const tmp = hashCons(void 0);
469
+ hashC.outputLen = tmp.outputLen;
470
+ hashC.blockLen = tmp.blockLen;
471
+ hashC.create = (opts) => hashCons(opts);
472
+ Object.assign(hashC, info);
473
+ return Object.freeze(hashC);
474
+ }
475
+ function randomBytes(bytesLength = 32) {
476
+ const cr = typeof globalThis === "object" ? globalThis.crypto : null;
477
+ if (typeof cr?.getRandomValues !== "function")
478
+ throw new Error("crypto.getRandomValues must be defined");
479
+ return cr.getRandomValues(new Uint8Array(bytesLength));
480
+ }
481
+ var oidNist = (suffix) => ({
482
+ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
483
+ });
484
+
485
+ // node_modules/@noble/hashes/_md.js
486
+ var HashMD = class {
487
+ constructor(blockLen, outputLen, padOffset, isLE) {
488
+ __publicField(this, "blockLen");
489
+ __publicField(this, "outputLen");
490
+ __publicField(this, "padOffset");
491
+ __publicField(this, "isLE");
492
+ // For partial updates less than block size
493
+ __publicField(this, "buffer");
494
+ __publicField(this, "view");
495
+ __publicField(this, "finished", false);
496
+ __publicField(this, "length", 0);
497
+ __publicField(this, "pos", 0);
498
+ __publicField(this, "destroyed", false);
499
+ this.blockLen = blockLen;
500
+ this.outputLen = outputLen;
501
+ this.padOffset = padOffset;
502
+ this.isLE = isLE;
503
+ this.buffer = new Uint8Array(blockLen);
504
+ this.view = createView(this.buffer);
505
+ }
506
+ update(data) {
507
+ aexists(this);
508
+ abytes(data);
509
+ const { view, buffer, blockLen } = this;
510
+ const len = data.length;
511
+ for (let pos = 0; pos < len; ) {
512
+ const take = Math.min(blockLen - this.pos, len - pos);
513
+ if (take === blockLen) {
514
+ const dataView = createView(data);
515
+ for (; blockLen <= len - pos; pos += blockLen)
516
+ this.process(dataView, pos);
517
+ continue;
518
+ }
519
+ buffer.set(data.subarray(pos, pos + take), this.pos);
520
+ this.pos += take;
521
+ pos += take;
522
+ if (this.pos === blockLen) {
523
+ this.process(view, 0);
524
+ this.pos = 0;
525
+ }
526
+ }
527
+ this.length += data.length;
528
+ this.roundClean();
529
+ return this;
530
+ }
531
+ digestInto(out) {
532
+ aexists(this);
533
+ aoutput(out, this);
534
+ this.finished = true;
535
+ const { buffer, view, blockLen, isLE } = this;
536
+ let { pos } = this;
537
+ buffer[pos++] = 128;
538
+ clean(this.buffer.subarray(pos));
539
+ if (this.padOffset > blockLen - pos) {
540
+ this.process(view, 0);
541
+ pos = 0;
542
+ }
543
+ for (let i = pos; i < blockLen; i++)
544
+ buffer[i] = 0;
545
+ view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
546
+ this.process(view, 0);
547
+ const oview = createView(out);
548
+ const len = this.outputLen;
549
+ if (len % 4)
550
+ throw new Error("_sha2: outputLen must be aligned to 32bit");
551
+ const outLen = len / 4;
552
+ const state = this.get();
553
+ if (outLen > state.length)
554
+ throw new Error("_sha2: outputLen bigger than state");
555
+ for (let i = 0; i < outLen; i++)
556
+ oview.setUint32(4 * i, state[i], isLE);
557
+ }
558
+ digest() {
559
+ const { buffer, outputLen } = this;
560
+ this.digestInto(buffer);
561
+ const res = buffer.slice(0, outputLen);
562
+ this.destroy();
563
+ return res;
564
+ }
565
+ _cloneInto(to) {
566
+ to || (to = new this.constructor());
567
+ to.set(...this.get());
568
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
569
+ to.destroyed = destroyed;
570
+ to.finished = finished;
571
+ to.length = length;
572
+ to.pos = pos;
573
+ if (length % blockLen)
574
+ to.buffer.set(buffer);
575
+ return to;
576
+ }
577
+ clone() {
578
+ return this._cloneInto();
579
+ }
580
+ };
581
+ var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
582
+ 1779033703,
583
+ 4089235720,
584
+ 3144134277,
585
+ 2227873595,
586
+ 1013904242,
587
+ 4271175723,
588
+ 2773480762,
589
+ 1595750129,
590
+ 1359893119,
591
+ 2917565137,
592
+ 2600822924,
593
+ 725511199,
594
+ 528734635,
595
+ 4215389547,
596
+ 1541459225,
597
+ 327033209
598
+ ]);
599
+
600
+ // node_modules/@noble/hashes/_u64.js
601
+ var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
602
+ var _32n = /* @__PURE__ */ BigInt(32);
603
+ function fromBig(n, le = false) {
604
+ if (le)
605
+ return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
606
+ return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
607
+ }
608
+ function split(lst, le = false) {
609
+ const len = lst.length;
610
+ let Ah = new Uint32Array(len);
611
+ let Al = new Uint32Array(len);
612
+ for (let i = 0; i < len; i++) {
613
+ const { h, l } = fromBig(lst[i], le);
614
+ [Ah[i], Al[i]] = [h, l];
615
+ }
616
+ return [Ah, Al];
617
+ }
618
+ var shrSH = (h, _l, s) => h >>> s;
619
+ var shrSL = (h, l, s) => h << 32 - s | l >>> s;
620
+ var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
621
+ var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
622
+ var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
623
+ var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
624
+ function add(Ah, Al, Bh, Bl) {
625
+ const l = (Al >>> 0) + (Bl >>> 0);
626
+ return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
627
+ }
628
+ var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
629
+ var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
630
+ var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
631
+ var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
632
+ var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
633
+ var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
634
+
635
+ // node_modules/@noble/hashes/sha2.js
636
+ var K512 = /* @__PURE__ */ (() => split([
637
+ "0x428a2f98d728ae22",
638
+ "0x7137449123ef65cd",
639
+ "0xb5c0fbcfec4d3b2f",
640
+ "0xe9b5dba58189dbbc",
641
+ "0x3956c25bf348b538",
642
+ "0x59f111f1b605d019",
643
+ "0x923f82a4af194f9b",
644
+ "0xab1c5ed5da6d8118",
645
+ "0xd807aa98a3030242",
646
+ "0x12835b0145706fbe",
647
+ "0x243185be4ee4b28c",
648
+ "0x550c7dc3d5ffb4e2",
649
+ "0x72be5d74f27b896f",
650
+ "0x80deb1fe3b1696b1",
651
+ "0x9bdc06a725c71235",
652
+ "0xc19bf174cf692694",
653
+ "0xe49b69c19ef14ad2",
654
+ "0xefbe4786384f25e3",
655
+ "0x0fc19dc68b8cd5b5",
656
+ "0x240ca1cc77ac9c65",
657
+ "0x2de92c6f592b0275",
658
+ "0x4a7484aa6ea6e483",
659
+ "0x5cb0a9dcbd41fbd4",
660
+ "0x76f988da831153b5",
661
+ "0x983e5152ee66dfab",
662
+ "0xa831c66d2db43210",
663
+ "0xb00327c898fb213f",
664
+ "0xbf597fc7beef0ee4",
665
+ "0xc6e00bf33da88fc2",
666
+ "0xd5a79147930aa725",
667
+ "0x06ca6351e003826f",
668
+ "0x142929670a0e6e70",
669
+ "0x27b70a8546d22ffc",
670
+ "0x2e1b21385c26c926",
671
+ "0x4d2c6dfc5ac42aed",
672
+ "0x53380d139d95b3df",
673
+ "0x650a73548baf63de",
674
+ "0x766a0abb3c77b2a8",
675
+ "0x81c2c92e47edaee6",
676
+ "0x92722c851482353b",
677
+ "0xa2bfe8a14cf10364",
678
+ "0xa81a664bbc423001",
679
+ "0xc24b8b70d0f89791",
680
+ "0xc76c51a30654be30",
681
+ "0xd192e819d6ef5218",
682
+ "0xd69906245565a910",
683
+ "0xf40e35855771202a",
684
+ "0x106aa07032bbd1b8",
685
+ "0x19a4c116b8d2d0c8",
686
+ "0x1e376c085141ab53",
687
+ "0x2748774cdf8eeb99",
688
+ "0x34b0bcb5e19b48a8",
689
+ "0x391c0cb3c5c95a63",
690
+ "0x4ed8aa4ae3418acb",
691
+ "0x5b9cca4f7763e373",
692
+ "0x682e6ff3d6b2b8a3",
693
+ "0x748f82ee5defb2fc",
694
+ "0x78a5636f43172f60",
695
+ "0x84c87814a1f0ab72",
696
+ "0x8cc702081a6439ec",
697
+ "0x90befffa23631e28",
698
+ "0xa4506cebde82bde9",
699
+ "0xbef9a3f7b2c67915",
700
+ "0xc67178f2e372532b",
701
+ "0xca273eceea26619c",
702
+ "0xd186b8c721c0c207",
703
+ "0xeada7dd6cde0eb1e",
704
+ "0xf57d4f7fee6ed178",
705
+ "0x06f067aa72176fba",
706
+ "0x0a637dc5a2c898a6",
707
+ "0x113f9804bef90dae",
708
+ "0x1b710b35131c471b",
709
+ "0x28db77f523047d84",
710
+ "0x32caab7b40c72493",
711
+ "0x3c9ebe0a15c9bebc",
712
+ "0x431d67c49c100d4c",
713
+ "0x4cc5d4becb3e42b6",
714
+ "0x597f299cfc657e2a",
715
+ "0x5fcb6fab3ad6faec",
716
+ "0x6c44198c4a475817"
717
+ ].map((n) => BigInt(n))))();
718
+ var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
719
+ var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
720
+ var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
721
+ var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
722
+ var SHA2_64B = class extends HashMD {
723
+ constructor(outputLen) {
724
+ super(128, outputLen, 16, false);
725
+ }
726
+ // prettier-ignore
727
+ get() {
728
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
729
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
730
+ }
731
+ // prettier-ignore
732
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
733
+ this.Ah = Ah | 0;
734
+ this.Al = Al | 0;
735
+ this.Bh = Bh | 0;
736
+ this.Bl = Bl | 0;
737
+ this.Ch = Ch | 0;
738
+ this.Cl = Cl | 0;
739
+ this.Dh = Dh | 0;
740
+ this.Dl = Dl | 0;
741
+ this.Eh = Eh | 0;
742
+ this.El = El | 0;
743
+ this.Fh = Fh | 0;
744
+ this.Fl = Fl | 0;
745
+ this.Gh = Gh | 0;
746
+ this.Gl = Gl | 0;
747
+ this.Hh = Hh | 0;
748
+ this.Hl = Hl | 0;
749
+ }
750
+ process(view, offset) {
751
+ for (let i = 0; i < 16; i++, offset += 4) {
752
+ SHA512_W_H[i] = view.getUint32(offset);
753
+ SHA512_W_L[i] = view.getUint32(offset += 4);
754
+ }
755
+ for (let i = 16; i < 80; i++) {
756
+ const W15h = SHA512_W_H[i - 15] | 0;
757
+ const W15l = SHA512_W_L[i - 15] | 0;
758
+ const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
759
+ const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
760
+ const W2h = SHA512_W_H[i - 2] | 0;
761
+ const W2l = SHA512_W_L[i - 2] | 0;
762
+ const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
763
+ const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
764
+ const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
765
+ const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
766
+ SHA512_W_H[i] = SUMh | 0;
767
+ SHA512_W_L[i] = SUMl | 0;
768
+ }
769
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
770
+ for (let i = 0; i < 80; i++) {
771
+ const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
772
+ const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
773
+ const CHIh = Eh & Fh ^ ~Eh & Gh;
774
+ const CHIl = El & Fl ^ ~El & Gl;
775
+ const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
776
+ const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
777
+ const T1l = T1ll | 0;
778
+ const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
779
+ const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
780
+ const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
781
+ const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
782
+ Hh = Gh | 0;
783
+ Hl = Gl | 0;
784
+ Gh = Fh | 0;
785
+ Gl = Fl | 0;
786
+ Fh = Eh | 0;
787
+ Fl = El | 0;
788
+ ({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
789
+ Dh = Ch | 0;
790
+ Dl = Cl | 0;
791
+ Ch = Bh | 0;
792
+ Cl = Bl | 0;
793
+ Bh = Ah | 0;
794
+ Bl = Al | 0;
795
+ const All = add3L(T1l, sigma0l, MAJl);
796
+ Ah = add3H(All, T1h, sigma0h, MAJh);
797
+ Al = All | 0;
798
+ }
799
+ ({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
800
+ ({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
801
+ ({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
802
+ ({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
803
+ ({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
804
+ ({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
805
+ ({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
806
+ ({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
807
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
808
+ }
809
+ roundClean() {
810
+ clean(SHA512_W_H, SHA512_W_L);
811
+ }
812
+ destroy() {
813
+ clean(this.buffer);
814
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
815
+ }
816
+ };
817
+ var _SHA512 = class extends SHA2_64B {
818
+ constructor() {
819
+ super(64);
820
+ __publicField(this, "Ah", SHA512_IV[0] | 0);
821
+ __publicField(this, "Al", SHA512_IV[1] | 0);
822
+ __publicField(this, "Bh", SHA512_IV[2] | 0);
823
+ __publicField(this, "Bl", SHA512_IV[3] | 0);
824
+ __publicField(this, "Ch", SHA512_IV[4] | 0);
825
+ __publicField(this, "Cl", SHA512_IV[5] | 0);
826
+ __publicField(this, "Dh", SHA512_IV[6] | 0);
827
+ __publicField(this, "Dl", SHA512_IV[7] | 0);
828
+ __publicField(this, "Eh", SHA512_IV[8] | 0);
829
+ __publicField(this, "El", SHA512_IV[9] | 0);
830
+ __publicField(this, "Fh", SHA512_IV[10] | 0);
831
+ __publicField(this, "Fl", SHA512_IV[11] | 0);
832
+ __publicField(this, "Gh", SHA512_IV[12] | 0);
833
+ __publicField(this, "Gl", SHA512_IV[13] | 0);
834
+ __publicField(this, "Hh", SHA512_IV[14] | 0);
835
+ __publicField(this, "Hl", SHA512_IV[15] | 0);
836
+ }
837
+ };
838
+ var sha512 = /* @__PURE__ */ createHasher(
839
+ () => new _SHA512(),
840
+ /* @__PURE__ */ oidNist(3)
841
+ );
842
+
843
+ // node_modules/@noble/curves/utils.js
844
+ var _0n = /* @__PURE__ */ BigInt(0);
845
+ var _1n = /* @__PURE__ */ BigInt(1);
846
+ function abool(value, title = "") {
847
+ if (typeof value !== "boolean") {
848
+ const prefix = title && `"${title}" `;
849
+ throw new Error(prefix + "expected boolean, got type=" + typeof value);
850
+ }
851
+ return value;
852
+ }
853
+ function abignumber(n) {
854
+ if (typeof n === "bigint") {
855
+ if (!isPosBig(n))
856
+ throw new Error("positive bigint expected, got " + n);
857
+ } else
858
+ anumber(n);
859
+ return n;
860
+ }
861
+ function hexToNumber(hex) {
862
+ if (typeof hex !== "string")
863
+ throw new Error("hex string expected, got " + typeof hex);
864
+ return hex === "" ? _0n : BigInt("0x" + hex);
865
+ }
866
+ function bytesToNumberBE(bytes) {
867
+ return hexToNumber(bytesToHex(bytes));
868
+ }
869
+ function bytesToNumberLE(bytes) {
870
+ return hexToNumber(bytesToHex(copyBytes(abytes(bytes)).reverse()));
871
+ }
872
+ function numberToBytesBE(n, len) {
873
+ anumber(len);
874
+ n = abignumber(n);
875
+ const res = hexToBytes(n.toString(16).padStart(len * 2, "0"));
876
+ if (res.length !== len)
877
+ throw new Error("number too large");
878
+ return res;
879
+ }
880
+ function numberToBytesLE(n, len) {
881
+ return numberToBytesBE(n, len).reverse();
882
+ }
883
+ function copyBytes(bytes) {
884
+ return Uint8Array.from(bytes);
885
+ }
886
+ var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
887
+ function inRange(n, min, max) {
888
+ return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
889
+ }
890
+ function aInRange(title, n, min, max) {
891
+ if (!inRange(n, min, max))
892
+ throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
893
+ }
894
+ var bitMask = (n) => (_1n << BigInt(n)) - _1n;
895
+ function validateObject(object, fields = {}, optFields = {}) {
896
+ if (!object || typeof object !== "object")
897
+ throw new Error("expected valid options object");
898
+ function checkField(fieldName, expectedType, isOpt) {
899
+ const val = object[fieldName];
900
+ if (isOpt && val === void 0)
901
+ return;
902
+ const current = typeof val;
903
+ if (current !== expectedType || val === null)
904
+ throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
905
+ }
906
+ const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
907
+ iter(fields, false);
908
+ iter(optFields, true);
909
+ }
910
+ function memoized(fn) {
911
+ const map = /* @__PURE__ */ new WeakMap();
912
+ return (arg, ...args) => {
913
+ const val = map.get(arg);
914
+ if (val !== void 0)
915
+ return val;
916
+ const computed = fn(arg, ...args);
917
+ map.set(arg, computed);
918
+ return computed;
919
+ };
920
+ }
921
+
922
+ // node_modules/@noble/curves/abstract/modular.js
923
+ var _0n2 = /* @__PURE__ */ BigInt(0);
924
+ var _1n2 = /* @__PURE__ */ BigInt(1);
925
+ var _2n = /* @__PURE__ */ BigInt(2);
926
+ var _3n = /* @__PURE__ */ BigInt(3);
927
+ var _4n = /* @__PURE__ */ BigInt(4);
928
+ var _5n = /* @__PURE__ */ BigInt(5);
929
+ var _7n = /* @__PURE__ */ BigInt(7);
930
+ var _8n = /* @__PURE__ */ BigInt(8);
931
+ var _9n = /* @__PURE__ */ BigInt(9);
932
+ var _16n = /* @__PURE__ */ BigInt(16);
933
+ function mod(a, b) {
934
+ const result = a % b;
935
+ return result >= _0n2 ? result : b + result;
936
+ }
937
+ function pow2(x, power, modulo) {
938
+ let res = x;
939
+ while (power-- > _0n2) {
940
+ res *= res;
941
+ res %= modulo;
942
+ }
943
+ return res;
944
+ }
945
+ function invert(number, modulo) {
946
+ if (number === _0n2)
947
+ throw new Error("invert: expected non-zero number");
948
+ if (modulo <= _0n2)
949
+ throw new Error("invert: expected positive modulus, got " + modulo);
950
+ let a = mod(number, modulo);
951
+ let b = modulo;
952
+ let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
953
+ while (a !== _0n2) {
954
+ const q = b / a;
955
+ const r = b % a;
956
+ const m = x - u * q;
957
+ const n = y - v * q;
958
+ b = a, a = r, x = u, y = v, u = m, v = n;
959
+ }
960
+ const gcd = b;
961
+ if (gcd !== _1n2)
962
+ throw new Error("invert: does not exist");
963
+ return mod(x, modulo);
964
+ }
965
+ function assertIsSquare(Fp, root, n) {
966
+ if (!Fp.eql(Fp.sqr(root), n))
967
+ throw new Error("Cannot find square root");
968
+ }
969
+ function sqrt3mod4(Fp, n) {
970
+ const p1div4 = (Fp.ORDER + _1n2) / _4n;
971
+ const root = Fp.pow(n, p1div4);
972
+ assertIsSquare(Fp, root, n);
973
+ return root;
974
+ }
975
+ function sqrt5mod8(Fp, n) {
976
+ const p5div8 = (Fp.ORDER - _5n) / _8n;
977
+ const n2 = Fp.mul(n, _2n);
978
+ const v = Fp.pow(n2, p5div8);
979
+ const nv = Fp.mul(n, v);
980
+ const i = Fp.mul(Fp.mul(nv, _2n), v);
981
+ const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
982
+ assertIsSquare(Fp, root, n);
983
+ return root;
984
+ }
985
+ function sqrt9mod16(P) {
986
+ const Fp_ = Field(P);
987
+ const tn = tonelliShanks(P);
988
+ const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
989
+ const c2 = tn(Fp_, c1);
990
+ const c3 = tn(Fp_, Fp_.neg(c1));
991
+ const c4 = (P + _7n) / _16n;
992
+ return (Fp, n) => {
993
+ let tv1 = Fp.pow(n, c4);
994
+ let tv2 = Fp.mul(tv1, c1);
995
+ const tv3 = Fp.mul(tv1, c2);
996
+ const tv4 = Fp.mul(tv1, c3);
997
+ const e1 = Fp.eql(Fp.sqr(tv2), n);
998
+ const e2 = Fp.eql(Fp.sqr(tv3), n);
999
+ tv1 = Fp.cmov(tv1, tv2, e1);
1000
+ tv2 = Fp.cmov(tv4, tv3, e2);
1001
+ const e3 = Fp.eql(Fp.sqr(tv2), n);
1002
+ const root = Fp.cmov(tv1, tv2, e3);
1003
+ assertIsSquare(Fp, root, n);
1004
+ return root;
1005
+ };
1006
+ }
1007
+ function tonelliShanks(P) {
1008
+ if (P < _3n)
1009
+ throw new Error("sqrt is not defined for small field");
1010
+ let Q = P - _1n2;
1011
+ let S = 0;
1012
+ while (Q % _2n === _0n2) {
1013
+ Q /= _2n;
1014
+ S++;
1015
+ }
1016
+ let Z = _2n;
1017
+ const _Fp = Field(P);
1018
+ while (FpLegendre(_Fp, Z) === 1) {
1019
+ if (Z++ > 1e3)
1020
+ throw new Error("Cannot find square root: probably non-prime P");
1021
+ }
1022
+ if (S === 1)
1023
+ return sqrt3mod4;
1024
+ let cc = _Fp.pow(Z, Q);
1025
+ const Q1div2 = (Q + _1n2) / _2n;
1026
+ return function tonelliSlow(Fp, n) {
1027
+ if (Fp.is0(n))
1028
+ return n;
1029
+ if (FpLegendre(Fp, n) !== 1)
1030
+ throw new Error("Cannot find square root");
1031
+ let M = S;
1032
+ let c = Fp.mul(Fp.ONE, cc);
1033
+ let t = Fp.pow(n, Q);
1034
+ let R = Fp.pow(n, Q1div2);
1035
+ while (!Fp.eql(t, Fp.ONE)) {
1036
+ if (Fp.is0(t))
1037
+ return Fp.ZERO;
1038
+ let i = 1;
1039
+ let t_tmp = Fp.sqr(t);
1040
+ while (!Fp.eql(t_tmp, Fp.ONE)) {
1041
+ i++;
1042
+ t_tmp = Fp.sqr(t_tmp);
1043
+ if (i === M)
1044
+ throw new Error("Cannot find square root");
1045
+ }
1046
+ const exponent = _1n2 << BigInt(M - i - 1);
1047
+ const b = Fp.pow(c, exponent);
1048
+ M = i;
1049
+ c = Fp.sqr(b);
1050
+ t = Fp.mul(t, c);
1051
+ R = Fp.mul(R, b);
1052
+ }
1053
+ return R;
1054
+ };
1055
+ }
1056
+ function FpSqrt(P) {
1057
+ if (P % _4n === _3n)
1058
+ return sqrt3mod4;
1059
+ if (P % _8n === _5n)
1060
+ return sqrt5mod8;
1061
+ if (P % _16n === _9n)
1062
+ return sqrt9mod16(P);
1063
+ return tonelliShanks(P);
1064
+ }
1065
+ var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
1066
+ var FIELD_FIELDS = [
1067
+ "create",
1068
+ "isValid",
1069
+ "is0",
1070
+ "neg",
1071
+ "inv",
1072
+ "sqrt",
1073
+ "sqr",
1074
+ "eql",
1075
+ "add",
1076
+ "sub",
1077
+ "mul",
1078
+ "pow",
1079
+ "div",
1080
+ "addN",
1081
+ "subN",
1082
+ "mulN",
1083
+ "sqrN"
1084
+ ];
1085
+ function validateField(field) {
1086
+ const initial = {
1087
+ ORDER: "bigint",
1088
+ BYTES: "number",
1089
+ BITS: "number"
1090
+ };
1091
+ const opts = FIELD_FIELDS.reduce((map, val) => {
1092
+ map[val] = "function";
1093
+ return map;
1094
+ }, initial);
1095
+ validateObject(field, opts);
1096
+ return field;
1097
+ }
1098
+ function FpPow(Fp, num, power) {
1099
+ if (power < _0n2)
1100
+ throw new Error("invalid exponent, negatives unsupported");
1101
+ if (power === _0n2)
1102
+ return Fp.ONE;
1103
+ if (power === _1n2)
1104
+ return num;
1105
+ let p = Fp.ONE;
1106
+ let d = num;
1107
+ while (power > _0n2) {
1108
+ if (power & _1n2)
1109
+ p = Fp.mul(p, d);
1110
+ d = Fp.sqr(d);
1111
+ power >>= _1n2;
1112
+ }
1113
+ return p;
1114
+ }
1115
+ function FpInvertBatch(Fp, nums, passZero = false) {
1116
+ const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
1117
+ const multipliedAcc = nums.reduce((acc, num, i) => {
1118
+ if (Fp.is0(num))
1119
+ return acc;
1120
+ inverted[i] = acc;
1121
+ return Fp.mul(acc, num);
1122
+ }, Fp.ONE);
1123
+ const invertedAcc = Fp.inv(multipliedAcc);
1124
+ nums.reduceRight((acc, num, i) => {
1125
+ if (Fp.is0(num))
1126
+ return acc;
1127
+ inverted[i] = Fp.mul(acc, inverted[i]);
1128
+ return Fp.mul(acc, num);
1129
+ }, invertedAcc);
1130
+ return inverted;
1131
+ }
1132
+ function FpLegendre(Fp, n) {
1133
+ const p1mod2 = (Fp.ORDER - _1n2) / _2n;
1134
+ const powered = Fp.pow(n, p1mod2);
1135
+ const yes = Fp.eql(powered, Fp.ONE);
1136
+ const zero = Fp.eql(powered, Fp.ZERO);
1137
+ const no = Fp.eql(powered, Fp.neg(Fp.ONE));
1138
+ if (!yes && !zero && !no)
1139
+ throw new Error("invalid Legendre symbol result");
1140
+ return yes ? 1 : zero ? 0 : -1;
1141
+ }
1142
+ function nLength(n, nBitLength) {
1143
+ if (nBitLength !== void 0)
1144
+ anumber(nBitLength);
1145
+ const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
1146
+ const nByteLength = Math.ceil(_nBitLength / 8);
1147
+ return { nBitLength: _nBitLength, nByteLength };
1148
+ }
1149
+ var _Field = class {
1150
+ constructor(ORDER, opts = {}) {
1151
+ __publicField(this, "ORDER");
1152
+ __publicField(this, "BITS");
1153
+ __publicField(this, "BYTES");
1154
+ __publicField(this, "isLE");
1155
+ __publicField(this, "ZERO", _0n2);
1156
+ __publicField(this, "ONE", _1n2);
1157
+ __publicField(this, "_lengths");
1158
+ __publicField(this, "_sqrt");
1159
+ // cached sqrt
1160
+ __publicField(this, "_mod");
1161
+ if (ORDER <= _0n2)
1162
+ throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
1163
+ let _nbitLength = void 0;
1164
+ this.isLE = false;
1165
+ if (opts != null && typeof opts === "object") {
1166
+ if (typeof opts.BITS === "number")
1167
+ _nbitLength = opts.BITS;
1168
+ if (typeof opts.sqrt === "function")
1169
+ this.sqrt = opts.sqrt;
1170
+ if (typeof opts.isLE === "boolean")
1171
+ this.isLE = opts.isLE;
1172
+ if (opts.allowedLengths)
1173
+ this._lengths = opts.allowedLengths?.slice();
1174
+ if (typeof opts.modFromBytes === "boolean")
1175
+ this._mod = opts.modFromBytes;
1176
+ }
1177
+ const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
1178
+ if (nByteLength > 2048)
1179
+ throw new Error("invalid field: expected ORDER of <= 2048 bytes");
1180
+ this.ORDER = ORDER;
1181
+ this.BITS = nBitLength;
1182
+ this.BYTES = nByteLength;
1183
+ this._sqrt = void 0;
1184
+ Object.preventExtensions(this);
1185
+ }
1186
+ create(num) {
1187
+ return mod(num, this.ORDER);
1188
+ }
1189
+ isValid(num) {
1190
+ if (typeof num !== "bigint")
1191
+ throw new Error("invalid field element: expected bigint, got " + typeof num);
1192
+ return _0n2 <= num && num < this.ORDER;
1193
+ }
1194
+ is0(num) {
1195
+ return num === _0n2;
1196
+ }
1197
+ // is valid and invertible
1198
+ isValidNot0(num) {
1199
+ return !this.is0(num) && this.isValid(num);
1200
+ }
1201
+ isOdd(num) {
1202
+ return (num & _1n2) === _1n2;
1203
+ }
1204
+ neg(num) {
1205
+ return mod(-num, this.ORDER);
1206
+ }
1207
+ eql(lhs, rhs) {
1208
+ return lhs === rhs;
1209
+ }
1210
+ sqr(num) {
1211
+ return mod(num * num, this.ORDER);
1212
+ }
1213
+ add(lhs, rhs) {
1214
+ return mod(lhs + rhs, this.ORDER);
1215
+ }
1216
+ sub(lhs, rhs) {
1217
+ return mod(lhs - rhs, this.ORDER);
1218
+ }
1219
+ mul(lhs, rhs) {
1220
+ return mod(lhs * rhs, this.ORDER);
1221
+ }
1222
+ pow(num, power) {
1223
+ return FpPow(this, num, power);
1224
+ }
1225
+ div(lhs, rhs) {
1226
+ return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
1227
+ }
1228
+ // Same as above, but doesn't normalize
1229
+ sqrN(num) {
1230
+ return num * num;
1231
+ }
1232
+ addN(lhs, rhs) {
1233
+ return lhs + rhs;
1234
+ }
1235
+ subN(lhs, rhs) {
1236
+ return lhs - rhs;
1237
+ }
1238
+ mulN(lhs, rhs) {
1239
+ return lhs * rhs;
1240
+ }
1241
+ inv(num) {
1242
+ return invert(num, this.ORDER);
1243
+ }
1244
+ sqrt(num) {
1245
+ if (!this._sqrt)
1246
+ this._sqrt = FpSqrt(this.ORDER);
1247
+ return this._sqrt(this, num);
1248
+ }
1249
+ toBytes(num) {
1250
+ return this.isLE ? numberToBytesLE(num, this.BYTES) : numberToBytesBE(num, this.BYTES);
1251
+ }
1252
+ fromBytes(bytes, skipValidation = false) {
1253
+ abytes(bytes);
1254
+ const { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
1255
+ if (allowedLengths) {
1256
+ if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
1257
+ throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
1258
+ }
1259
+ const padded = new Uint8Array(BYTES);
1260
+ padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
1261
+ bytes = padded;
1262
+ }
1263
+ if (bytes.length !== BYTES)
1264
+ throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
1265
+ let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
1266
+ if (modFromBytes)
1267
+ scalar = mod(scalar, ORDER);
1268
+ if (!skipValidation) {
1269
+ if (!this.isValid(scalar))
1270
+ throw new Error("invalid field element: outside of range 0..ORDER");
1271
+ }
1272
+ return scalar;
1273
+ }
1274
+ // TODO: we don't need it here, move out to separate fn
1275
+ invertBatch(lst) {
1276
+ return FpInvertBatch(this, lst);
1277
+ }
1278
+ // We can't move this out because Fp6, Fp12 implement it
1279
+ // and it's unclear what to return in there.
1280
+ cmov(a, b, condition) {
1281
+ return condition ? b : a;
1282
+ }
1283
+ };
1284
+ function Field(ORDER, opts = {}) {
1285
+ return new _Field(ORDER, opts);
1286
+ }
1287
+
1288
+ // node_modules/@noble/curves/abstract/curve.js
1289
+ var _0n3 = /* @__PURE__ */ BigInt(0);
1290
+ var _1n3 = /* @__PURE__ */ BigInt(1);
1291
+ function negateCt(condition, item) {
1292
+ const neg = item.negate();
1293
+ return condition ? neg : item;
1294
+ }
1295
+ function normalizeZ(c, points) {
1296
+ const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
1297
+ return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
1298
+ }
1299
+ function validateW(W, bits) {
1300
+ if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
1301
+ throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
1302
+ }
1303
+ function calcWOpts(W, scalarBits) {
1304
+ validateW(W, scalarBits);
1305
+ const windows = Math.ceil(scalarBits / W) + 1;
1306
+ const windowSize = 2 ** (W - 1);
1307
+ const maxNumber = 2 ** W;
1308
+ const mask = bitMask(W);
1309
+ const shiftBy = BigInt(W);
1310
+ return { windows, windowSize, mask, maxNumber, shiftBy };
1311
+ }
1312
+ function calcOffsets(n, window, wOpts) {
1313
+ const { windowSize, mask, maxNumber, shiftBy } = wOpts;
1314
+ let wbits = Number(n & mask);
1315
+ let nextN = n >> shiftBy;
1316
+ if (wbits > windowSize) {
1317
+ wbits -= maxNumber;
1318
+ nextN += _1n3;
1319
+ }
1320
+ const offsetStart = window * windowSize;
1321
+ const offset = offsetStart + Math.abs(wbits) - 1;
1322
+ const isZero = wbits === 0;
1323
+ const isNeg = wbits < 0;
1324
+ const isNegF = window % 2 !== 0;
1325
+ const offsetF = offsetStart;
1326
+ return { nextN, offset, isZero, isNeg, isNegF, offsetF };
1327
+ }
1328
+ var pointPrecomputes = /* @__PURE__ */ new WeakMap();
1329
+ var pointWindowSizes = /* @__PURE__ */ new WeakMap();
1330
+ function getW(P) {
1331
+ return pointWindowSizes.get(P) || 1;
1332
+ }
1333
+ function assert0(n) {
1334
+ if (n !== _0n3)
1335
+ throw new Error("invalid wNAF");
1336
+ }
1337
+ var wNAF = class {
1338
+ // Parametrized with a given Point class (not individual point)
1339
+ constructor(Point, bits) {
1340
+ __publicField(this, "BASE");
1341
+ __publicField(this, "ZERO");
1342
+ __publicField(this, "Fn");
1343
+ __publicField(this, "bits");
1344
+ this.BASE = Point.BASE;
1345
+ this.ZERO = Point.ZERO;
1346
+ this.Fn = Point.Fn;
1347
+ this.bits = bits;
1348
+ }
1349
+ // non-const time multiplication ladder
1350
+ _unsafeLadder(elm, n, p = this.ZERO) {
1351
+ let d = elm;
1352
+ while (n > _0n3) {
1353
+ if (n & _1n3)
1354
+ p = p.add(d);
1355
+ d = d.double();
1356
+ n >>= _1n3;
1357
+ }
1358
+ return p;
1359
+ }
1360
+ /**
1361
+ * Creates a wNAF precomputation window. Used for caching.
1362
+ * Default window size is set by `utils.precompute()` and is equal to 8.
1363
+ * Number of precomputed points depends on the curve size:
1364
+ * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
1365
+ * - 𝑊 is the window size
1366
+ * - 𝑛 is the bitlength of the curve order.
1367
+ * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
1368
+ * @param point Point instance
1369
+ * @param W window size
1370
+ * @returns precomputed point tables flattened to a single array
1371
+ */
1372
+ precomputeWindow(point, W) {
1373
+ const { windows, windowSize } = calcWOpts(W, this.bits);
1374
+ const points = [];
1375
+ let p = point;
1376
+ let base = p;
1377
+ for (let window = 0; window < windows; window++) {
1378
+ base = p;
1379
+ points.push(base);
1380
+ for (let i = 1; i < windowSize; i++) {
1381
+ base = base.add(p);
1382
+ points.push(base);
1383
+ }
1384
+ p = base.double();
1385
+ }
1386
+ return points;
1387
+ }
1388
+ /**
1389
+ * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
1390
+ * More compact implementation:
1391
+ * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
1392
+ * @returns real and fake (for const-time) points
1393
+ */
1394
+ wNAF(W, precomputes, n) {
1395
+ if (!this.Fn.isValid(n))
1396
+ throw new Error("invalid scalar");
1397
+ let p = this.ZERO;
1398
+ let f = this.BASE;
1399
+ const wo = calcWOpts(W, this.bits);
1400
+ for (let window = 0; window < wo.windows; window++) {
1401
+ const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
1402
+ n = nextN;
1403
+ if (isZero) {
1404
+ f = f.add(negateCt(isNegF, precomputes[offsetF]));
1405
+ } else {
1406
+ p = p.add(negateCt(isNeg, precomputes[offset]));
1407
+ }
1408
+ }
1409
+ assert0(n);
1410
+ return { p, f };
1411
+ }
1412
+ /**
1413
+ * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
1414
+ * @param acc accumulator point to add result of multiplication
1415
+ * @returns point
1416
+ */
1417
+ wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
1418
+ const wo = calcWOpts(W, this.bits);
1419
+ for (let window = 0; window < wo.windows; window++) {
1420
+ if (n === _0n3)
1421
+ break;
1422
+ const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
1423
+ n = nextN;
1424
+ if (isZero) {
1425
+ continue;
1426
+ } else {
1427
+ const item = precomputes[offset];
1428
+ acc = acc.add(isNeg ? item.negate() : item);
1429
+ }
1430
+ }
1431
+ assert0(n);
1432
+ return acc;
1433
+ }
1434
+ getPrecomputes(W, point, transform) {
1435
+ let comp = pointPrecomputes.get(point);
1436
+ if (!comp) {
1437
+ comp = this.precomputeWindow(point, W);
1438
+ if (W !== 1) {
1439
+ if (typeof transform === "function")
1440
+ comp = transform(comp);
1441
+ pointPrecomputes.set(point, comp);
1442
+ }
1443
+ }
1444
+ return comp;
1445
+ }
1446
+ cached(point, scalar, transform) {
1447
+ const W = getW(point);
1448
+ return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
1449
+ }
1450
+ unsafe(point, scalar, transform, prev) {
1451
+ const W = getW(point);
1452
+ if (W === 1)
1453
+ return this._unsafeLadder(point, scalar, prev);
1454
+ return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
1455
+ }
1456
+ // We calculate precomputes for elliptic curve point multiplication
1457
+ // using windowed method. This specifies window size and
1458
+ // stores precomputed values. Usually only base point would be precomputed.
1459
+ createCache(P, W) {
1460
+ validateW(W, this.bits);
1461
+ pointWindowSizes.set(P, W);
1462
+ pointPrecomputes.delete(P);
1463
+ }
1464
+ hasCache(elm) {
1465
+ return getW(elm) !== 1;
1466
+ }
1467
+ };
1468
+ function createField(order, field, isLE) {
1469
+ if (field) {
1470
+ if (field.ORDER !== order)
1471
+ throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
1472
+ validateField(field);
1473
+ return field;
1474
+ } else {
1475
+ return Field(order, { isLE });
1476
+ }
1477
+ }
1478
+ function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
1479
+ if (FpFnLE === void 0)
1480
+ FpFnLE = type === "edwards";
1481
+ if (!CURVE || typeof CURVE !== "object")
1482
+ throw new Error(`expected valid ${type} CURVE object`);
1483
+ for (const p of ["p", "n", "h"]) {
1484
+ const val = CURVE[p];
1485
+ if (!(typeof val === "bigint" && val > _0n3))
1486
+ throw new Error(`CURVE.${p} must be positive bigint`);
1487
+ }
1488
+ const Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE);
1489
+ const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
1490
+ const _b = type === "weierstrass" ? "b" : "d";
1491
+ const params = ["Gx", "Gy", "a", _b];
1492
+ for (const p of params) {
1493
+ if (!Fp.isValid(CURVE[p]))
1494
+ throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
1495
+ }
1496
+ CURVE = Object.freeze(Object.assign({}, CURVE));
1497
+ return { CURVE, Fp, Fn };
1498
+ }
1499
+ function createKeygen(randomSecretKey, getPublicKey) {
1500
+ return function keygen(seed) {
1501
+ const secretKey = randomSecretKey(seed);
1502
+ return { secretKey, publicKey: getPublicKey(secretKey) };
1503
+ };
1504
+ }
1505
+
1506
+ // node_modules/@noble/curves/abstract/edwards.js
1507
+ var _0n4 = BigInt(0);
1508
+ var _1n4 = BigInt(1);
1509
+ var _2n2 = BigInt(2);
1510
+ var _8n2 = BigInt(8);
1511
+ function isEdValidXY(Fp, CURVE, x, y) {
1512
+ const x2 = Fp.sqr(x);
1513
+ const y2 = Fp.sqr(y);
1514
+ const left = Fp.add(Fp.mul(CURVE.a, x2), y2);
1515
+ const right = Fp.add(Fp.ONE, Fp.mul(CURVE.d, Fp.mul(x2, y2)));
1516
+ return Fp.eql(left, right);
1517
+ }
1518
+ function edwards(params, extraOpts = {}) {
1519
+ const validated = createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
1520
+ const { Fp, Fn } = validated;
1521
+ let CURVE = validated.CURVE;
1522
+ const { h: cofactor } = CURVE;
1523
+ validateObject(extraOpts, {}, { uvRatio: "function" });
1524
+ const MASK = _2n2 << BigInt(Fn.BYTES * 8) - _1n4;
1525
+ const modP = (n) => Fp.create(n);
1526
+ const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
1527
+ try {
1528
+ return { isValid: true, value: Fp.sqrt(Fp.div(u, v)) };
1529
+ } catch (e) {
1530
+ return { isValid: false, value: _0n4 };
1531
+ }
1532
+ });
1533
+ if (!isEdValidXY(Fp, CURVE, CURVE.Gx, CURVE.Gy))
1534
+ throw new Error("bad curve params: generator point");
1535
+ function acoord(title, n, banZero = false) {
1536
+ const min = banZero ? _1n4 : _0n4;
1537
+ aInRange("coordinate " + title, n, min, MASK);
1538
+ return n;
1539
+ }
1540
+ function aedpoint(other) {
1541
+ if (!(other instanceof Point))
1542
+ throw new Error("EdwardsPoint expected");
1543
+ }
1544
+ const toAffineMemo = memoized((p, iz) => {
1545
+ const { X, Y, Z } = p;
1546
+ const is0 = p.is0();
1547
+ if (iz == null)
1548
+ iz = is0 ? _8n2 : Fp.inv(Z);
1549
+ const x = modP(X * iz);
1550
+ const y = modP(Y * iz);
1551
+ const zz = Fp.mul(Z, iz);
1552
+ if (is0)
1553
+ return { x: _0n4, y: _1n4 };
1554
+ if (zz !== _1n4)
1555
+ throw new Error("invZ was invalid");
1556
+ return { x, y };
1557
+ });
1558
+ const assertValidMemo = memoized((p) => {
1559
+ const { a, d } = CURVE;
1560
+ if (p.is0())
1561
+ throw new Error("bad point: ZERO");
1562
+ const { X, Y, Z, T } = p;
1563
+ const X2 = modP(X * X);
1564
+ const Y2 = modP(Y * Y);
1565
+ const Z2 = modP(Z * Z);
1566
+ const Z4 = modP(Z2 * Z2);
1567
+ const aX2 = modP(X2 * a);
1568
+ const left = modP(Z2 * modP(aX2 + Y2));
1569
+ const right = modP(Z4 + modP(d * modP(X2 * Y2)));
1570
+ if (left !== right)
1571
+ throw new Error("bad point: equation left != right (1)");
1572
+ const XY = modP(X * Y);
1573
+ const ZT = modP(Z * T);
1574
+ if (XY !== ZT)
1575
+ throw new Error("bad point: equation left != right (2)");
1576
+ return true;
1577
+ });
1578
+ const _Point = class _Point {
1579
+ constructor(X, Y, Z, T) {
1580
+ __publicField(this, "X");
1581
+ __publicField(this, "Y");
1582
+ __publicField(this, "Z");
1583
+ __publicField(this, "T");
1584
+ this.X = acoord("x", X);
1585
+ this.Y = acoord("y", Y);
1586
+ this.Z = acoord("z", Z, true);
1587
+ this.T = acoord("t", T);
1588
+ Object.freeze(this);
1589
+ }
1590
+ static CURVE() {
1591
+ return CURVE;
1592
+ }
1593
+ static fromAffine(p) {
1594
+ if (p instanceof _Point)
1595
+ throw new Error("extended point not allowed");
1596
+ const { x, y } = p || {};
1597
+ acoord("x", x);
1598
+ acoord("y", y);
1599
+ return new _Point(x, y, _1n4, modP(x * y));
1600
+ }
1601
+ // Uses algo from RFC8032 5.1.3.
1602
+ static fromBytes(bytes, zip215 = false) {
1603
+ const len = Fp.BYTES;
1604
+ const { a, d } = CURVE;
1605
+ bytes = copyBytes(abytes(bytes, len, "point"));
1606
+ abool(zip215, "zip215");
1607
+ const normed = copyBytes(bytes);
1608
+ const lastByte = bytes[len - 1];
1609
+ normed[len - 1] = lastByte & ~128;
1610
+ const y = bytesToNumberLE(normed);
1611
+ const max = zip215 ? MASK : Fp.ORDER;
1612
+ aInRange("point.y", y, _0n4, max);
1613
+ const y2 = modP(y * y);
1614
+ const u = modP(y2 - _1n4);
1615
+ const v = modP(d * y2 - a);
1616
+ let { isValid, value: x } = uvRatio2(u, v);
1617
+ if (!isValid)
1618
+ throw new Error("bad point: invalid y coordinate");
1619
+ const isXOdd = (x & _1n4) === _1n4;
1620
+ const isLastByteOdd = (lastByte & 128) !== 0;
1621
+ if (!zip215 && x === _0n4 && isLastByteOdd)
1622
+ throw new Error("bad point: x=0 and x_0=1");
1623
+ if (isLastByteOdd !== isXOdd)
1624
+ x = modP(-x);
1625
+ return _Point.fromAffine({ x, y });
1626
+ }
1627
+ static fromHex(hex, zip215 = false) {
1628
+ return _Point.fromBytes(hexToBytes(hex), zip215);
1629
+ }
1630
+ get x() {
1631
+ return this.toAffine().x;
1632
+ }
1633
+ get y() {
1634
+ return this.toAffine().y;
1635
+ }
1636
+ precompute(windowSize = 8, isLazy = true) {
1637
+ wnaf.createCache(this, windowSize);
1638
+ if (!isLazy)
1639
+ this.multiply(_2n2);
1640
+ return this;
1641
+ }
1642
+ // Useful in fromAffine() - not for fromBytes(), which always created valid points.
1643
+ assertValidity() {
1644
+ assertValidMemo(this);
1645
+ }
1646
+ // Compare one point to another.
1647
+ equals(other) {
1648
+ aedpoint(other);
1649
+ const { X: X1, Y: Y1, Z: Z1 } = this;
1650
+ const { X: X2, Y: Y2, Z: Z2 } = other;
1651
+ const X1Z2 = modP(X1 * Z2);
1652
+ const X2Z1 = modP(X2 * Z1);
1653
+ const Y1Z2 = modP(Y1 * Z2);
1654
+ const Y2Z1 = modP(Y2 * Z1);
1655
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
1656
+ }
1657
+ is0() {
1658
+ return this.equals(_Point.ZERO);
1659
+ }
1660
+ negate() {
1661
+ return new _Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
1662
+ }
1663
+ // Fast algo for doubling Extended Point.
1664
+ // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
1665
+ // Cost: 4M + 4S + 1*a + 6add + 1*2.
1666
+ double() {
1667
+ const { a } = CURVE;
1668
+ const { X: X1, Y: Y1, Z: Z1 } = this;
1669
+ const A = modP(X1 * X1);
1670
+ const B = modP(Y1 * Y1);
1671
+ const C = modP(_2n2 * modP(Z1 * Z1));
1672
+ const D = modP(a * A);
1673
+ const x1y1 = X1 + Y1;
1674
+ const E = modP(modP(x1y1 * x1y1) - A - B);
1675
+ const G = D + B;
1676
+ const F = G - C;
1677
+ const H = D - B;
1678
+ const X3 = modP(E * F);
1679
+ const Y3 = modP(G * H);
1680
+ const T3 = modP(E * H);
1681
+ const Z3 = modP(F * G);
1682
+ return new _Point(X3, Y3, Z3, T3);
1683
+ }
1684
+ // Fast algo for adding 2 Extended Points.
1685
+ // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
1686
+ // Cost: 9M + 1*a + 1*d + 7add.
1687
+ add(other) {
1688
+ aedpoint(other);
1689
+ const { a, d } = CURVE;
1690
+ const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
1691
+ const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
1692
+ const A = modP(X1 * X2);
1693
+ const B = modP(Y1 * Y2);
1694
+ const C = modP(T1 * d * T2);
1695
+ const D = modP(Z1 * Z2);
1696
+ const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
1697
+ const F = D - C;
1698
+ const G = D + C;
1699
+ const H = modP(B - a * A);
1700
+ const X3 = modP(E * F);
1701
+ const Y3 = modP(G * H);
1702
+ const T3 = modP(E * H);
1703
+ const Z3 = modP(F * G);
1704
+ return new _Point(X3, Y3, Z3, T3);
1705
+ }
1706
+ subtract(other) {
1707
+ return this.add(other.negate());
1708
+ }
1709
+ // Constant-time multiplication.
1710
+ multiply(scalar) {
1711
+ if (!Fn.isValidNot0(scalar))
1712
+ throw new Error("invalid scalar: expected 1 <= sc < curve.n");
1713
+ const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(_Point, p2));
1714
+ return normalizeZ(_Point, [p, f])[0];
1715
+ }
1716
+ // Non-constant-time multiplication. Uses double-and-add algorithm.
1717
+ // It's faster, but should only be used when you don't care about
1718
+ // an exposed private key e.g. sig verification.
1719
+ // Does NOT allow scalars higher than CURVE.n.
1720
+ // Accepts optional accumulator to merge with multiply (important for sparse scalars)
1721
+ multiplyUnsafe(scalar, acc = _Point.ZERO) {
1722
+ if (!Fn.isValid(scalar))
1723
+ throw new Error("invalid scalar: expected 0 <= sc < curve.n");
1724
+ if (scalar === _0n4)
1725
+ return _Point.ZERO;
1726
+ if (this.is0() || scalar === _1n4)
1727
+ return this;
1728
+ return wnaf.unsafe(this, scalar, (p) => normalizeZ(_Point, p), acc);
1729
+ }
1730
+ // Checks if point is of small order.
1731
+ // If you add something to small order point, you will have "dirty"
1732
+ // point with torsion component.
1733
+ // Multiplies point by cofactor and checks if the result is 0.
1734
+ isSmallOrder() {
1735
+ return this.multiplyUnsafe(cofactor).is0();
1736
+ }
1737
+ // Multiplies point by curve order and checks if the result is 0.
1738
+ // Returns `false` is the point is dirty.
1739
+ isTorsionFree() {
1740
+ return wnaf.unsafe(this, CURVE.n).is0();
1741
+ }
1742
+ // Converts Extended point to default (x, y) coordinates.
1743
+ // Can accept precomputed Z^-1 - for example, from invertBatch.
1744
+ toAffine(invertedZ) {
1745
+ return toAffineMemo(this, invertedZ);
1746
+ }
1747
+ clearCofactor() {
1748
+ if (cofactor === _1n4)
1749
+ return this;
1750
+ return this.multiplyUnsafe(cofactor);
1751
+ }
1752
+ toBytes() {
1753
+ const { x, y } = this.toAffine();
1754
+ const bytes = Fp.toBytes(y);
1755
+ bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
1756
+ return bytes;
1757
+ }
1758
+ toHex() {
1759
+ return bytesToHex(this.toBytes());
1760
+ }
1761
+ toString() {
1762
+ return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
1763
+ }
1764
+ };
1765
+ // base / generator point
1766
+ __publicField(_Point, "BASE", new _Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy)));
1767
+ // zero / infinity / identity point
1768
+ __publicField(_Point, "ZERO", new _Point(_0n4, _1n4, _1n4, _0n4));
1769
+ // 0, 1, 1, 0
1770
+ // math field
1771
+ __publicField(_Point, "Fp", Fp);
1772
+ // scalar field
1773
+ __publicField(_Point, "Fn", Fn);
1774
+ let Point = _Point;
1775
+ const wnaf = new wNAF(Point, Fn.BITS);
1776
+ Point.BASE.precompute(8);
1777
+ return Point;
1778
+ }
1779
+ function eddsa(Point, cHash, eddsaOpts = {}) {
1780
+ if (typeof cHash !== "function")
1781
+ throw new Error('"hash" function param is required');
1782
+ validateObject(eddsaOpts, {}, {
1783
+ adjustScalarBytes: "function",
1784
+ randomBytes: "function",
1785
+ domain: "function",
1786
+ prehash: "function",
1787
+ mapToCurve: "function"
1788
+ });
1789
+ const { prehash } = eddsaOpts;
1790
+ const { BASE, Fp, Fn } = Point;
1791
+ const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
1792
+ const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
1793
+ const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
1794
+ abool(phflag, "phflag");
1795
+ if (ctx.length || phflag)
1796
+ throw new Error("Contexts/pre-hash are not supported");
1797
+ return data;
1798
+ });
1799
+ function modN_LE(hash) {
1800
+ return Fn.create(bytesToNumberLE(hash));
1801
+ }
1802
+ function getPrivateScalar(key) {
1803
+ const len = lengths.secretKey;
1804
+ abytes(key, lengths.secretKey, "secretKey");
1805
+ const hashed = abytes(cHash(key), 2 * len, "hashedSecretKey");
1806
+ const head = adjustScalarBytes2(hashed.slice(0, len));
1807
+ const prefix = hashed.slice(len, 2 * len);
1808
+ const scalar = modN_LE(head);
1809
+ return { head, prefix, scalar };
1810
+ }
1811
+ function getExtendedPublicKey(secretKey) {
1812
+ const { head, prefix, scalar } = getPrivateScalar(secretKey);
1813
+ const point = BASE.multiply(scalar);
1814
+ const pointBytes = point.toBytes();
1815
+ return { head, prefix, scalar, point, pointBytes };
1816
+ }
1817
+ function getPublicKey(secretKey) {
1818
+ return getExtendedPublicKey(secretKey).pointBytes;
1819
+ }
1820
+ function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
1821
+ const msg = concatBytes(...msgs);
1822
+ return modN_LE(cHash(domain(msg, abytes(context, void 0, "context"), !!prehash)));
1823
+ }
1824
+ function sign(msg, secretKey, options = {}) {
1825
+ msg = abytes(msg, void 0, "message");
1826
+ if (prehash)
1827
+ msg = prehash(msg);
1828
+ const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
1829
+ const r = hashDomainToScalar(options.context, prefix, msg);
1830
+ const R = BASE.multiply(r).toBytes();
1831
+ const k = hashDomainToScalar(options.context, R, pointBytes, msg);
1832
+ const s = Fn.create(r + k * scalar);
1833
+ if (!Fn.isValid(s))
1834
+ throw new Error("sign failed: invalid s");
1835
+ const rs = concatBytes(R, Fn.toBytes(s));
1836
+ return abytes(rs, lengths.signature, "result");
1837
+ }
1838
+ const verifyOpts = { zip215: true };
1839
+ function verify(sig, msg, publicKey, options = verifyOpts) {
1840
+ const { context, zip215 } = options;
1841
+ const len = lengths.signature;
1842
+ sig = abytes(sig, len, "signature");
1843
+ msg = abytes(msg, void 0, "message");
1844
+ publicKey = abytes(publicKey, lengths.publicKey, "publicKey");
1845
+ if (zip215 !== void 0)
1846
+ abool(zip215, "zip215");
1847
+ if (prehash)
1848
+ msg = prehash(msg);
1849
+ const mid = len / 2;
1850
+ const r = sig.subarray(0, mid);
1851
+ const s = bytesToNumberLE(sig.subarray(mid, len));
1852
+ let A, R, SB;
1853
+ try {
1854
+ A = Point.fromBytes(publicKey, zip215);
1855
+ R = Point.fromBytes(r, zip215);
1856
+ SB = BASE.multiplyUnsafe(s);
1857
+ } catch (error) {
1858
+ return false;
1859
+ }
1860
+ if (!zip215 && A.isSmallOrder())
1861
+ return false;
1862
+ const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
1863
+ const RkA = R.add(A.multiplyUnsafe(k));
1864
+ return RkA.subtract(SB).clearCofactor().is0();
1865
+ }
1866
+ const _size = Fp.BYTES;
1867
+ const lengths = {
1868
+ secretKey: _size,
1869
+ publicKey: _size,
1870
+ signature: 2 * _size,
1871
+ seed: _size
1872
+ };
1873
+ function randomSecretKey(seed = randomBytes2(lengths.seed)) {
1874
+ return abytes(seed, lengths.seed, "seed");
1875
+ }
1876
+ function isValidSecretKey(key) {
1877
+ return isBytes(key) && key.length === Fn.BYTES;
1878
+ }
1879
+ function isValidPublicKey(key, zip215) {
1880
+ try {
1881
+ return !!Point.fromBytes(key, zip215);
1882
+ } catch (error) {
1883
+ return false;
1884
+ }
1885
+ }
1886
+ const utils = {
1887
+ getExtendedPublicKey,
1888
+ randomSecretKey,
1889
+ isValidSecretKey,
1890
+ isValidPublicKey,
1891
+ /**
1892
+ * Converts ed public key to x public key. Uses formula:
1893
+ * - ed25519:
1894
+ * - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
1895
+ * - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
1896
+ * - ed448:
1897
+ * - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
1898
+ * - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
1899
+ */
1900
+ toMontgomery(publicKey) {
1901
+ const { y } = Point.fromBytes(publicKey);
1902
+ const size = lengths.publicKey;
1903
+ const is25519 = size === 32;
1904
+ if (!is25519 && size !== 57)
1905
+ throw new Error("only defined for 25519 and 448");
1906
+ const u = is25519 ? Fp.div(_1n4 + y, _1n4 - y) : Fp.div(y - _1n4, y + _1n4);
1907
+ return Fp.toBytes(u);
1908
+ },
1909
+ toMontgomerySecret(secretKey) {
1910
+ const size = lengths.secretKey;
1911
+ abytes(secretKey, size);
1912
+ const hashed = cHash(secretKey.subarray(0, size));
1913
+ return adjustScalarBytes2(hashed).subarray(0, size);
1914
+ }
1915
+ };
1916
+ return Object.freeze({
1917
+ keygen: createKeygen(randomSecretKey, getPublicKey),
1918
+ getPublicKey,
1919
+ sign,
1920
+ verify,
1921
+ utils,
1922
+ Point,
1923
+ lengths
1924
+ });
1925
+ }
1926
+
1927
+ // node_modules/@noble/curves/ed25519.js
1928
+ var _1n5 = BigInt(1);
1929
+ var _2n3 = BigInt(2);
1930
+ var _5n2 = BigInt(5);
1931
+ var _8n3 = BigInt(8);
1932
+ var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
1933
+ var ed25519_CURVE = /* @__PURE__ */ (() => ({
1934
+ p: ed25519_CURVE_p,
1935
+ n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
1936
+ h: _8n3,
1937
+ a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
1938
+ d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
1939
+ Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
1940
+ Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
1941
+ }))();
1942
+ function ed25519_pow_2_252_3(x) {
1943
+ const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
1944
+ const P = ed25519_CURVE_p;
1945
+ const x2 = x * x % P;
1946
+ const b2 = x2 * x % P;
1947
+ const b4 = pow2(b2, _2n3, P) * b2 % P;
1948
+ const b5 = pow2(b4, _1n5, P) * x % P;
1949
+ const b10 = pow2(b5, _5n2, P) * b5 % P;
1950
+ const b20 = pow2(b10, _10n, P) * b10 % P;
1951
+ const b40 = pow2(b20, _20n, P) * b20 % P;
1952
+ const b80 = pow2(b40, _40n, P) * b40 % P;
1953
+ const b160 = pow2(b80, _80n, P) * b80 % P;
1954
+ const b240 = pow2(b160, _80n, P) * b80 % P;
1955
+ const b250 = pow2(b240, _10n, P) * b10 % P;
1956
+ const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
1957
+ return { pow_p_5_8, b2 };
1958
+ }
1959
+ function adjustScalarBytes(bytes) {
1960
+ bytes[0] &= 248;
1961
+ bytes[31] &= 127;
1962
+ bytes[31] |= 64;
1963
+ return bytes;
1964
+ }
1965
+ var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
1966
+ function uvRatio(u, v) {
1967
+ const P = ed25519_CURVE_p;
1968
+ const v3 = mod(v * v * v, P);
1969
+ const v7 = mod(v3 * v3 * v, P);
1970
+ const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
1971
+ let x = mod(u * v3 * pow, P);
1972
+ const vx2 = mod(v * x * x, P);
1973
+ const root1 = x;
1974
+ const root2 = mod(x * ED25519_SQRT_M1, P);
1975
+ const useRoot1 = vx2 === u;
1976
+ const useRoot2 = vx2 === mod(-u, P);
1977
+ const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
1978
+ if (useRoot1)
1979
+ x = root1;
1980
+ if (useRoot2 || noRoot)
1981
+ x = root2;
1982
+ if (isNegativeLE(x, P))
1983
+ x = mod(-x, P);
1984
+ return { isValid: useRoot1 || useRoot2, value: x };
1985
+ }
1986
+ var ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
1987
+ function ed(opts) {
1988
+ return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes }, opts));
1989
+ }
1990
+ var ed25519 = /* @__PURE__ */ ed({});
1991
+
1992
+ // src/crypto.ts
1993
+ function toB64url(bytes) {
1994
+ let bin = "";
1995
+ for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
1996
+ return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
1997
+ }
1998
+ function fromB64url(s) {
1999
+ const padded = s.replace(/-/g, "+").replace(/_/g, "/");
2000
+ const bin = atob(padded);
2001
+ const out = new Uint8Array(bin.length);
2002
+ for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
2003
+ return out;
2004
+ }
2005
+ function generateMlDsaKeypair() {
2006
+ const kp = ml_dsa65.keygen();
2007
+ return { privateKey: kp.secretKey, publicKey: kp.publicKey };
2008
+ }
2009
+ function signMlDsa(privateKey, message) {
2010
+ return ml_dsa65.sign(message, privateKey);
2011
+ }
2012
+ function verifyMlDsa(publicKey, message, sig) {
2013
+ return ml_dsa65.verify(sig, message, publicKey);
2014
+ }
2015
+ function signHybrid(ed25519Priv, mlDsaPriv, msg) {
2016
+ return {
2017
+ profile: "pqc-hybrid-v1",
2018
+ ed25519Sig: ed25519.sign(msg, ed25519Priv),
2019
+ mlDsaSig: ml_dsa65.sign(msg, mlDsaPriv)
2020
+ };
2021
+ }
2022
+ function verifyHybrid(ed25519Pub, mlDsaPub, msg, sig) {
2023
+ if (sig.profile !== "pqc-hybrid-v1") return false;
2024
+ return ed25519.verify(sig.ed25519Sig, msg, ed25519Pub) && ml_dsa65.verify(sig.mlDsaSig, msg, mlDsaPub);
2025
+ }
2026
+ function encodeHybridSig(sig) {
2027
+ return `pqc-hybrid-v1.${toB64url(sig.ed25519Sig)}.${toB64url(sig.mlDsaSig)}`;
2028
+ }
2029
+ function decodeHybridSig(s) {
2030
+ const parts = s.split(".");
2031
+ if (parts.length !== 3 || parts[0] !== "pqc-hybrid-v1") {
2032
+ throw new Error(
2033
+ `Invalid hybrid signature: expected "pqc-hybrid-v1.<ed25519b64>.<mldsab64>", got "${s.slice(0, 40)}..."`
2034
+ );
2035
+ }
2036
+ return {
2037
+ profile: "pqc-hybrid-v1",
2038
+ ed25519Sig: fromB64url(parts[1]),
2039
+ mlDsaSig: fromB64url(parts[2])
2040
+ };
2041
+ }
2042
+ function encodeMlDsaPublicKeyJwk(pub) {
2043
+ return {
2044
+ kty: "OKP",
2045
+ alg: "ML-DSA-65",
2046
+ use: "sig",
2047
+ x: toB64url(pub)
2048
+ };
2049
+ }
2050
+ function decodeMlDsaPublicKeyJwk(jwk) {
2051
+ const j = jwk;
2052
+ if (j["kty"] !== "OKP" || j["alg"] !== "ML-DSA-65") {
2053
+ throw new Error(
2054
+ `Invalid ML-DSA-65 JWK: expected kty="OKP" alg="ML-DSA-65", got kty="${j["kty"]}" alg="${j["alg"]}"`
2055
+ );
2056
+ }
2057
+ const x = j["x"];
2058
+ if (typeof x !== "string" || x.length === 0) {
2059
+ throw new Error("Invalid ML-DSA-65 JWK: missing or empty x field");
2060
+ }
2061
+ return fromB64url(x);
2062
+ }
360
2063
  function generateUUID() {
361
2064
  if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.randomUUID === "function") {
362
2065
  return globalThis.crypto.randomUUID();
@@ -452,11 +2155,11 @@ function sha256(msg) {
452
2155
  let h0 = 1779033703, h1 = 3144134277, h2 = 1013904242, h3 = 2773480762;
453
2156
  let h4 = 1359893119, h5 = 2600822924, h6 = 528734635, h7 = 1541459225;
454
2157
  const msgLen = msg.length;
455
- const bitLen = msgLen * 8;
2158
+ const bitLen2 = msgLen * 8;
456
2159
  const padded = [...msg];
457
2160
  padded.push(128);
458
2161
  while (padded.length % 64 !== 56) padded.push(0);
459
- for (let i = 7; i >= 0; i--) padded.push(bitLen / Math.pow(2, i * 8) & 255);
2162
+ for (let i = 7; i >= 0; i--) padded.push(bitLen2 / Math.pow(2, i * 8) & 255);
460
2163
  for (let i = 0; i < padded.length; i += 64) {
461
2164
  const w = [];
462
2165
  for (let j = 0; j < 16; j++) {
@@ -3372,15 +5075,20 @@ export {
3372
5075
  clientAllowsTool,
3373
5076
  decodeBleFrames,
3374
5077
  decodeCompact,
5078
+ decodeHybridSig,
3375
5079
  decodeMinimal,
5080
+ decodeMlDsaPublicKeyJwk,
3376
5081
  encodeBleFrames,
3377
5082
  encodeCompact,
5083
+ encodeHybridSig,
3378
5084
  encodeMinimal,
5085
+ encodeMlDsaPublicKeyJwk,
3379
5086
  extractIdentityFromJwt,
3380
5087
  extractLoaFromJwt,
3381
5088
  extractRoleFromJwt,
3382
5089
  fetchCanonicalSchema,
3383
5090
  fetchRRFRevocations,
5091
+ generateMlDsaKeypair,
3384
5092
  isAuthorityRequestValid,
3385
5093
  isM2mTrustedRevoked,
3386
5094
  isPreemptedBy,
@@ -3417,7 +5125,9 @@ export {
3417
5125
  parseM2mTrustedToken,
3418
5126
  roleFromJwtLevel,
3419
5127
  selectTransport,
5128
+ signHybrid,
3420
5129
  signMessage,
5130
+ signMlDsa,
3421
5131
  validateAuthorityAccess,
3422
5132
  validateCompetitionScope,
3423
5133
  validateConfig,
@@ -3439,10 +5149,24 @@ export {
3439
5149
  validateURI,
3440
5150
  validateDelegationChain2 as validateV22DelegationChain,
3441
5151
  validateVersionCompat,
5152
+ verifyHybrid,
3442
5153
  verifyM2mTrustedToken,
3443
5154
  verifyM2mTrustedTokenClaims,
3444
5155
  verifyMessage,
5156
+ verifyMlDsa,
3445
5157
  verifyPQSignature,
3446
5158
  verifyMediaChunkHash as verifyV22MediaChunkHash
3447
5159
  };
5160
+ /*! Bundled license information:
5161
+
5162
+ @noble/hashes/utils.js:
5163
+ (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
5164
+
5165
+ @noble/curves/utils.js:
5166
+ @noble/curves/abstract/modular.js:
5167
+ @noble/curves/abstract/curve.js:
5168
+ @noble/curves/abstract/edwards.js:
5169
+ @noble/curves/ed25519.js:
5170
+ (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
5171
+ */
3448
5172
  //# sourceMappingURL=browser.mjs.map