@ocap/mpc-hsm 0.0.0-stage → 1.33.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/esm/bip39-english-wordlist.d.mts +11 -0
  2. package/esm/bip39-english-wordlist.mjs +31 -0
  3. package/esm/constants.d.mts +140 -0
  4. package/esm/constants.mjs +142 -0
  5. package/esm/dkg.d.mts +279 -0
  6. package/esm/dkg.mjs +903 -0
  7. package/esm/index.d.mts +17 -0
  8. package/esm/index.mjs +18 -0
  9. package/esm/mta.d.mts +63 -0
  10. package/esm/mta.mjs +275 -0
  11. package/esm/ocap-clear-signing.d.mts +125 -0
  12. package/esm/ocap-clear-signing.mjs +1170 -0
  13. package/esm/offline-ceremony.d.mts +127 -0
  14. package/esm/offline-ceremony.mjs +481 -0
  15. package/esm/ot.d.mts +193 -0
  16. package/esm/ot.mjs +726 -0
  17. package/esm/personal-vault.d.mts +1292 -0
  18. package/esm/personal-vault.mjs +3615 -0
  19. package/esm/personal.d.mts +10 -0
  20. package/esm/personal.mjs +11 -0
  21. package/esm/presign.d.mts +242 -0
  22. package/esm/presign.mjs +951 -0
  23. package/esm/rlp-eip1559.d.mts +75 -0
  24. package/esm/rlp-eip1559.mjs +293 -0
  25. package/esm/secp256k1-scalar.d.mts +141 -0
  26. package/esm/secp256k1-scalar.mjs +474 -0
  27. package/esm/sign.d.mts +113 -0
  28. package/esm/sign.mjs +225 -0
  29. package/esm/steel-mnemonic.d.mts +55 -0
  30. package/esm/steel-mnemonic.mjs +247 -0
  31. package/esm/tweak.d.mts +100 -0
  32. package/esm/tweak.mjs +168 -0
  33. package/esm/ves-backup.d.mts +296 -0
  34. package/esm/ves-backup.mjs +1025 -0
  35. package/esm/x402-agent-guard.d.mts +111 -0
  36. package/esm/x402-agent-guard.mjs +780 -0
  37. package/lib/bip39-english-wordlist.cjs +33 -0
  38. package/lib/bip39-english-wordlist.d.cts +11 -0
  39. package/lib/constants.cjs +264 -0
  40. package/lib/constants.d.cts +140 -0
  41. package/lib/dkg.cjs +910 -0
  42. package/lib/dkg.d.cts +279 -0
  43. package/lib/index.cjs +420 -0
  44. package/lib/index.d.cts +17 -0
  45. package/lib/mta.cjs +280 -0
  46. package/lib/mta.d.cts +63 -0
  47. package/lib/ocap-clear-signing.cjs +1176 -0
  48. package/lib/ocap-clear-signing.d.cts +125 -0
  49. package/lib/offline-ceremony.cjs +487 -0
  50. package/lib/offline-ceremony.d.cts +127 -0
  51. package/lib/ot.cjs +744 -0
  52. package/lib/ot.d.cts +193 -0
  53. package/lib/personal-vault.cjs +3725 -0
  54. package/lib/personal-vault.d.cts +1292 -0
  55. package/lib/personal.cjs +338 -0
  56. package/lib/personal.d.cts +10 -0
  57. package/lib/presign.cjs +954 -0
  58. package/lib/presign.d.cts +242 -0
  59. package/lib/rlp-eip1559.cjs +303 -0
  60. package/lib/rlp-eip1559.d.cts +75 -0
  61. package/lib/secp256k1-scalar.cjs +527 -0
  62. package/lib/secp256k1-scalar.d.cts +141 -0
  63. package/lib/sign.cjs +233 -0
  64. package/lib/sign.d.cts +113 -0
  65. package/lib/steel-mnemonic.cjs +253 -0
  66. package/lib/steel-mnemonic.d.cts +55 -0
  67. package/lib/tweak.cjs +178 -0
  68. package/lib/tweak.d.cts +100 -0
  69. package/lib/ves-backup.cjs +1053 -0
  70. package/lib/ves-backup.d.cts +296 -0
  71. package/lib/x402-agent-guard.cjs +788 -0
  72. package/lib/x402-agent-guard.d.cts +111 -0
  73. package/package.json +80 -4
  74. package/README.md +0 -3
@@ -0,0 +1,954 @@
1
+ const require_constants = require('./constants.cjs');
2
+ const require_secp256k1_scalar = require('./secp256k1-scalar.cjs');
3
+ const require_ot = require('./ot.cjs');
4
+ const require_mta = require('./mta.cjs');
5
+ let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
6
+ let _noble_curves_ed25519_js = require("@noble/curves/ed25519.js");
7
+ let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
8
+
9
+ //#region src/presign.ts
10
+ function encodeRound1RevealForCommit(reveal) {
11
+ return require_secp256k1_scalar.lengthPrefixedEncode("ARC-MPC-PRESIG-REVEAL-BODY-V1", [
12
+ require_secp256k1_scalar.u16be(reveal.slot),
13
+ reveal.gammaPointComp33,
14
+ reveal.bPointComp33,
15
+ reveal.ckPointComp33,
16
+ reveal.zetaEpk32,
17
+ reveal.pokGamma.noncePointComp33,
18
+ reveal.pokGamma.responseBytes32,
19
+ reveal.pokB.noncePointComp33,
20
+ reveal.pokB.responseBytes32,
21
+ reveal.pokCk.noncePointComp33,
22
+ reveal.pokCk.responseValueBytes32,
23
+ reveal.pokCk.responseBlindBytes32
24
+ ]);
25
+ }
26
+ function computeRound1CommitmentHash(params) {
27
+ return (0, _noble_hashes_sha2_js.sha256)(require_secp256k1_scalar.lengthPrefixedEncode(require_constants.DST_PRESIG_COM_V1, [
28
+ require_secp256k1_scalar.utf8Bytes(params.rootId),
29
+ require_secp256k1_scalar.u64be(params.keyEpoch),
30
+ require_secp256k1_scalar.sortedSubsetBytes(params.subset),
31
+ params.hDkg32,
32
+ params.sessionNonce32,
33
+ require_secp256k1_scalar.u16be(params.slot),
34
+ params.comSalt32,
35
+ params.revealBodyBytes
36
+ ]));
37
+ }
38
+ /**
39
+ * Computes the canonical subset-bound `presigId` (`design.md` §4.5(1)):
40
+ * `SHA256("ARC-MPC-PRESIG-ID-V1" || LP(rootId) || u64be(keyEpoch) || sortedSubsetBytes(S) || H_dkg || Com^{pre}_{i_1..i_t})`.
41
+ */
42
+ function computeSubsetBoundPresigId(params) {
43
+ const sorted = require_secp256k1_scalar.normalizeSortedSubset(params.subset);
44
+ if (params.orderedCommitments32.length !== sorted.length) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", "Ordered commitment count must match subset cardinality");
45
+ return (0, _noble_hashes_sha2_js.sha256)(require_secp256k1_scalar.lengthPrefixedEncode(require_constants.DST_PRESIG_ID_V1, [
46
+ require_secp256k1_scalar.utf8Bytes(params.rootId),
47
+ require_secp256k1_scalar.u64be(params.keyEpoch),
48
+ require_secp256k1_scalar.sortedSubsetBytes(sorted),
49
+ params.hDkg32,
50
+ ...params.orderedCommitments32
51
+ ]));
52
+ }
53
+ function deriveMtaContextAndNonce(presigId32, kind, senderSlotA, receiverSlotB) {
54
+ const kindByte = kind === "k_gamma" ? 1 : 2;
55
+ return {
56
+ ctxMta: require_secp256k1_scalar.lengthPrefixedEncode("ARC-MPC-PRESIG-MTA-CTX-V1", [
57
+ presigId32,
58
+ require_secp256k1_scalar.u8(kindByte),
59
+ require_secp256k1_scalar.u16be(senderSlotA),
60
+ require_secp256k1_scalar.u16be(receiverSlotB)
61
+ ]),
62
+ mtaNonce: require_secp256k1_scalar.concatBytes(presigId32, require_secp256k1_scalar.u8(kindByte), require_secp256k1_scalar.u16be(senderSlotA), require_secp256k1_scalar.u16be(receiverSlotB))
63
+ };
64
+ }
65
+ /**
66
+ * Stateful single-party 4-round Beaver-masked Presignature participant (`design.md` §4.5(1)).
67
+ * Enforces strict phase ordering, non-reuse of `b_i`, and immediate zeroization of all single-use
68
+ * ephemeral scalars upon completion or any abort!
69
+ */
70
+ var PresignPartySession = class {
71
+ constructor(params) {
72
+ this.phase = "INIT";
73
+ this.cShareBytes32 = new Uint8Array(32);
74
+ this.deltaShareBytes32 = new Uint8Array(32);
75
+ this.chiShareBytes32 = new Uint8Array(32);
76
+ this.ownRevealMsg = null;
77
+ this.round1CommitmentsBySlot = /* @__PURE__ */ new Map();
78
+ this.round1RevealsBySlot = /* @__PURE__ */ new Map();
79
+ this.linearizedPublicSharesBySlot = /* @__PURE__ */ new Map();
80
+ this.presigId32 = null;
81
+ this.aggGammaPoint = null;
82
+ this.rPoint = null;
83
+ this.pendingOtReceiverByPeer = /* @__PURE__ */ new Map();
84
+ this.pendingOtSenderByPeer = /* @__PURE__ */ new Map();
85
+ this.verifiedOtReceiverByPeer = /* @__PURE__ */ new Map();
86
+ this.senderMtaGammaAcc = 0n;
87
+ this.senderMtaBAcc = 0n;
88
+ const { slot, nodeSecretShareBytes32, cluster, subset, sessionNonce32, baseOtChannels, rng = require_secp256k1_scalar.defaultRandomBytes } = params;
89
+ if (sessionNonce32.length !== 32) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Presign sessionNonce32 must be 32 bytes");
90
+ if (cluster.hDkg32.length !== 32) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "hDkg32 must be 32 bytes");
91
+ if (!Number.isInteger(cluster.t) || !Number.isInteger(cluster.nParties) || cluster.t < 2 || cluster.t > cluster.nParties || cluster.nParties > require_constants.MAX_MPC_CLUSTER_NODES) throw new require_constants.MpcProtocolError("PRESIG_INVALID_SUBSET", `Invalid cluster threshold (${cluster.t}-of-${cluster.nParties}); online MPC requires 2 <= t <= n <= ${require_constants.MAX_MPC_CLUSTER_NODES}`);
92
+ const sortedSubset = require_secp256k1_scalar.normalizeSortedSubset(subset, cluster.t, cluster.nParties);
93
+ if (!sortedSubset.includes(slot)) throw new require_constants.MpcProtocolError("PRESIG_INVALID_SUBSET", `Party slot ${slot} not in subset`);
94
+ if (baseOtChannels.ownerSlot !== slot) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Base-OT channel ownerSlot mismatch");
95
+ this.slot = slot;
96
+ this.cluster = cluster;
97
+ this.subset = sortedSubset;
98
+ this.sessionNonce32 = new Uint8Array(sessionNonce32);
99
+ this.baseOtChannels = baseOtChannels;
100
+ this.rng = rng;
101
+ const pRoot = require_secp256k1_scalar.decodePointCompressed(cluster.rootPublicKeyComp33, false);
102
+ let sumX = require_secp256k1_scalar.SECP256K1_ZERO;
103
+ for (const j of sortedSubset) {
104
+ const xjBytes = cluster.publicSharesComp33BySlot.get(j);
105
+ if (!xjBytes) throw new require_constants.MpcProtocolError("PRESIG_INVALID_SUBSET", `Missing public share X_${j}`);
106
+ const xTildeJ = require_secp256k1_scalar.mulPoint(require_secp256k1_scalar.decodePointCompressed(xjBytes, false), require_secp256k1_scalar.lagrangeCoefficientAtZero(j, sortedSubset));
107
+ if (xTildeJ.is0()) throw new require_constants.MpcProtocolError("PRESIG_INVALID_SUBSET", `Degenerate linearized X_${j}`);
108
+ this.linearizedPublicSharesBySlot.set(j, xTildeJ);
109
+ sumX = sumX.add(xTildeJ);
110
+ }
111
+ if (!sumX.equals(pRoot)) throw new require_constants.MpcProtocolError("PRESIG_INVALID_SUBSET", "Public shares do not interpolate to P_root");
112
+ const xOwn = require_secp256k1_scalar.bytes32ToScalarNonZero(nodeSecretShareBytes32);
113
+ const xTildeOwn = require_secp256k1_scalar.mulModN(require_secp256k1_scalar.lagrangeCoefficientAtZero(slot, sortedSubset), xOwn);
114
+ const expectedXTildePoint = this.linearizedPublicSharesBySlot.get(slot);
115
+ if (!require_secp256k1_scalar.mulBaseG(xTildeOwn).equals(expectedXTildePoint)) throw new require_constants.MpcProtocolError("INVALID_SCALAR", `Node secret share mismatch for slot ${slot}`);
116
+ this.xTildeBytes32 = require_secp256k1_scalar.scalarToBytes32(xTildeOwn);
117
+ this.kBytes32 = require_secp256k1_scalar.scalarToBytes32(require_secp256k1_scalar.sampleScalarNonZero(rng));
118
+ this.gammaBytes32 = require_secp256k1_scalar.scalarToBytes32(require_secp256k1_scalar.sampleScalarNonZero(rng));
119
+ this.bBytes32 = require_secp256k1_scalar.scalarToBytes32(require_secp256k1_scalar.sampleScalarNonZero(rng));
120
+ this.rhoKBytes32 = require_secp256k1_scalar.scalarToBytes32(require_secp256k1_scalar.sampleScalarNonZero(rng));
121
+ this.zetaEsk32 = rng(32);
122
+ }
123
+ get isAborted() {
124
+ return this.phase === "ABORTED";
125
+ }
126
+ abort() {
127
+ this.phase = "ABORTED";
128
+ this.wipeAllEphemeralSecrets();
129
+ }
130
+ wipeAllEphemeralSecrets() {
131
+ require_secp256k1_scalar.zeroizeBytes(this.xTildeBytes32, this.kBytes32, this.gammaBytes32, this.bBytes32, this.rhoKBytes32, this.zetaEsk32, this.cShareBytes32, this.deltaShareBytes32, this.chiShareBytes32);
132
+ for (const [, pending] of this.pendingOtReceiverByPeer) {
133
+ require_secp256k1_scalar.zeroizeBytes(pending.kGamma.allChoiceBits1024, pending.kB.allChoiceBits1024);
134
+ for (const r of pending.kGamma.tRows1024) require_secp256k1_scalar.zeroizeBytes(r);
135
+ for (const r of pending.kB.tRows1024) require_secp256k1_scalar.zeroizeBytes(r);
136
+ }
137
+ for (const [, pending] of this.pendingOtSenderByPeer) {
138
+ require_secp256k1_scalar.zeroizeBytes(pending.kGamma.xiSeed32, pending.kB.xiSeed32);
139
+ for (const q of pending.kGamma.qRows1024) require_secp256k1_scalar.zeroizeBytes(q);
140
+ for (const q of pending.kB.qRows1024) require_secp256k1_scalar.zeroizeBytes(q);
141
+ }
142
+ for (const [, verified] of this.verifiedOtReceiverByPeer) {
143
+ require_ot.wipeVerifiedCotReceiverRows(verified.kGamma);
144
+ require_ot.wipeVerifiedCotReceiverRows(verified.kB);
145
+ }
146
+ this.pendingOtReceiverByPeer.clear();
147
+ this.pendingOtSenderByPeer.clear();
148
+ this.verifiedOtReceiverByPeer.clear();
149
+ this.senderMtaGammaAcc = 0n;
150
+ this.senderMtaBAcc = 0n;
151
+ }
152
+ assertPhase(expected) {
153
+ if (this.phase !== expected) {
154
+ this.abort();
155
+ throw new require_constants.MpcProtocolError("PRESIG_STATE_VIOLATION", `Expected presign phase ${expected}, got ${this.phase}`);
156
+ }
157
+ }
158
+ pokContextParts(targetSlot) {
159
+ return [
160
+ require_secp256k1_scalar.utf8Bytes(this.cluster.rootId),
161
+ require_secp256k1_scalar.u64be(this.cluster.keyEpoch),
162
+ require_secp256k1_scalar.sortedSubsetBytes(this.subset),
163
+ this.cluster.hDkg32,
164
+ this.sessionNonce32,
165
+ require_secp256k1_scalar.u16be(targetSlot)
166
+ ];
167
+ }
168
+ /**
169
+ * Round 1a: Generates hash commitment `Com^{pre}_i` over `(\Gamma_i, B_i, C_{k,i}, \pi_\Gamma, \pi_B, \pi_{C_k})`.
170
+ */
171
+ round1CreateCommitment() {
172
+ this.assertPhase("INIT");
173
+ try {
174
+ const kScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.kBytes32);
175
+ const gammaScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.gammaBytes32);
176
+ const bScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.bBytes32);
177
+ const rhoKScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.rhoKBytes32);
178
+ const gammaPoint = require_secp256k1_scalar.mulBaseG(gammaScalar);
179
+ const bPoint = require_secp256k1_scalar.mulBaseG(bScalar);
180
+ const ckPoint = require_secp256k1_scalar.pedersenCommit(kScalar, rhoKScalar);
181
+ if (ckPoint.is0()) throw new require_constants.MpcProtocolError("PRESIG_DEGENERATE_NONCE", "Pedersen C_{k,i} hit infinity");
182
+ const zetaEpk32 = _noble_curves_ed25519_js.x25519.getPublicKey(this.zetaEsk32);
183
+ const ctxParts = this.pokContextParts(this.slot);
184
+ const pokGamma = require_secp256k1_scalar.createSchnorrPokInBase({
185
+ dst: require_constants.DST_PRESIG_POK_GAMMA_V1,
186
+ basePoint: require_secp256k1_scalar.SECP256K1_G,
187
+ statementPoint: gammaPoint,
188
+ secretScalar: gammaScalar,
189
+ contextParts: ctxParts,
190
+ rng: this.rng
191
+ });
192
+ const pokB = require_secp256k1_scalar.createSchnorrPokInBase({
193
+ dst: require_constants.DST_PRESIG_POK_B_V1,
194
+ basePoint: require_secp256k1_scalar.SECP256K1_G,
195
+ statementPoint: bPoint,
196
+ secretScalar: bScalar,
197
+ contextParts: ctxParts,
198
+ rng: this.rng
199
+ });
200
+ const pokCk = require_secp256k1_scalar.createPedersenPok({
201
+ dst: require_constants.DST_PRESIG_POK_CK_V1,
202
+ commitmentPoint: ckPoint,
203
+ valueScalar: kScalar,
204
+ blindingScalar: rhoKScalar,
205
+ contextParts: ctxParts,
206
+ rng: this.rng
207
+ });
208
+ const comSalt32 = this.rng(32);
209
+ const reveal = {
210
+ slot: this.slot,
211
+ comSalt32,
212
+ gammaPointComp33: require_secp256k1_scalar.encodePointCompressed(gammaPoint),
213
+ bPointComp33: require_secp256k1_scalar.encodePointCompressed(bPoint),
214
+ ckPointComp33: require_secp256k1_scalar.encodePointCompressed(ckPoint),
215
+ zetaEpk32,
216
+ pokGamma,
217
+ pokB,
218
+ pokCk
219
+ };
220
+ const revealBody = encodeRound1RevealForCommit(reveal);
221
+ const commitment32 = computeRound1CommitmentHash({
222
+ rootId: this.cluster.rootId,
223
+ keyEpoch: this.cluster.keyEpoch,
224
+ subset: this.subset,
225
+ hDkg32: this.cluster.hDkg32,
226
+ sessionNonce32: this.sessionNonce32,
227
+ slot: this.slot,
228
+ comSalt32,
229
+ revealBodyBytes: revealBody
230
+ });
231
+ this.ownRevealMsg = reveal;
232
+ this.phase = "R1_COMMITTED";
233
+ return {
234
+ slot: this.slot,
235
+ commitment32
236
+ };
237
+ } catch (err) {
238
+ this.abort();
239
+ throw err;
240
+ }
241
+ }
242
+ /**
243
+ * Round 1b: Locks all `t` commitments, derives subset-bound `presigId`, and reveals `Reveal^{pre}_i`.
244
+ */
245
+ round1ReceiveCommitmentsAndReveal(commitments) {
246
+ this.assertPhase("R1_COMMITTED");
247
+ try {
248
+ if (commitments.length !== this.subset.length) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", "Missing Round-1 commitments for subset");
249
+ const map = /* @__PURE__ */ new Map();
250
+ for (const msg of commitments) {
251
+ if (!this.subset.includes(msg.slot) || msg.commitment32.length !== 32 || map.has(msg.slot)) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", `Invalid Round-1 commitment from slot ${msg.slot}`, msg.slot);
252
+ map.set(msg.slot, new Uint8Array(msg.commitment32));
253
+ }
254
+ const orderedCommitments = this.subset.map((s) => map.get(s));
255
+ const presigId32 = computeSubsetBoundPresigId({
256
+ rootId: this.cluster.rootId,
257
+ keyEpoch: this.cluster.keyEpoch,
258
+ subset: this.subset,
259
+ hDkg32: this.cluster.hDkg32,
260
+ orderedCommitments32: orderedCommitments
261
+ });
262
+ this.round1CommitmentsBySlot = map;
263
+ this.presigId32 = presigId32;
264
+ this.phase = "R1_REVEALED";
265
+ return {
266
+ presigId32: new Uint8Array(presigId32),
267
+ reveal: this.ownRevealMsg
268
+ };
269
+ } catch (err) {
270
+ this.abort();
271
+ throw err;
272
+ }
273
+ }
274
+ /**
275
+ * Verifies all `t` Round-1 reveals & PoKs, attributing `culpritSlot` on any invalid commitment or PoK.
276
+ */
277
+ round1VerifyAllReveals(reveals) {
278
+ this.assertPhase("R1_REVEALED");
279
+ try {
280
+ if (!this.presigId32 || reveals.length !== this.subset.length) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", "Incomplete Round-1 reveals");
281
+ const bySlot = /* @__PURE__ */ new Map();
282
+ for (const rev of reveals) {
283
+ if (!this.subset.includes(rev.slot) || bySlot.has(rev.slot)) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", `Unexpected Round-1 reveal slot ${rev.slot}`, rev.slot);
284
+ bySlot.set(rev.slot, rev);
285
+ }
286
+ let gammaSum = require_secp256k1_scalar.SECP256K1_ZERO;
287
+ for (const j of this.subset) {
288
+ const rev = bySlot.get(j);
289
+ if (rev.comSalt32.length !== 32 || rev.zetaEpk32.length !== 32) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", `Malformed Round-1 reveal fields from slot ${j}`, j);
290
+ require_secp256k1_scalar.assertValidX25519PublicKey(rev.zetaEpk32, "PRESIG_POK_INVALID", j);
291
+ if (!require_secp256k1_scalar.constantTimeEqual(this.round1CommitmentsBySlot.get(j), computeRound1CommitmentHash({
292
+ rootId: this.cluster.rootId,
293
+ keyEpoch: this.cluster.keyEpoch,
294
+ subset: this.subset,
295
+ hDkg32: this.cluster.hDkg32,
296
+ sessionNonce32: this.sessionNonce32,
297
+ slot: j,
298
+ comSalt32: rev.comSalt32,
299
+ revealBodyBytes: encodeRound1RevealForCommit(rev)
300
+ }))) throw new require_constants.MpcProtocolError("PRESIG_COMMITMENT_MISMATCH", `Round-1 commitment mismatch from slot ${j}`, j);
301
+ let gammaJ;
302
+ let bJ;
303
+ let ckJ;
304
+ try {
305
+ gammaJ = require_secp256k1_scalar.decodePointCompressed(rev.gammaPointComp33, false);
306
+ bJ = require_secp256k1_scalar.decodePointCompressed(rev.bPointComp33, false);
307
+ ckJ = require_secp256k1_scalar.decodePointCompressed(rev.ckPointComp33, false);
308
+ } catch {
309
+ throw new require_constants.MpcProtocolError("PRESIG_POK_INVALID", `Invalid curve point in Round-1 reveal from slot ${j}`, j);
310
+ }
311
+ const ctxParts = this.pokContextParts(j);
312
+ if (!require_secp256k1_scalar.verifySchnorrPokInBase({
313
+ dst: require_constants.DST_PRESIG_POK_GAMMA_V1,
314
+ basePoint: require_secp256k1_scalar.SECP256K1_G,
315
+ statementPoint: gammaJ,
316
+ proof: rev.pokGamma,
317
+ contextParts: ctxParts
318
+ }) || !require_secp256k1_scalar.verifySchnorrPokInBase({
319
+ dst: require_constants.DST_PRESIG_POK_B_V1,
320
+ basePoint: require_secp256k1_scalar.SECP256K1_G,
321
+ statementPoint: bJ,
322
+ proof: rev.pokB,
323
+ contextParts: ctxParts
324
+ }) || !require_secp256k1_scalar.verifyPedersenPok({
325
+ dst: require_constants.DST_PRESIG_POK_CK_V1,
326
+ commitmentPoint: ckJ,
327
+ proof: rev.pokCk,
328
+ contextParts: ctxParts
329
+ })) throw new require_constants.MpcProtocolError("PRESIG_POK_INVALID", `Invalid Round-1 zero-knowledge proof from slot ${j}`, j);
330
+ gammaSum = gammaSum.add(gammaJ);
331
+ }
332
+ if (gammaSum.is0()) throw new require_constants.MpcProtocolError("PRESIG_DEGENERATE_NONCE", "Aggregate Gamma == O");
333
+ this.round1RevealsBySlot = bySlot;
334
+ this.aggGammaPoint = gammaSum;
335
+ this.phase = "R1_VERIFIED";
336
+ } catch (err) {
337
+ this.abort();
338
+ throw err;
339
+ }
340
+ }
341
+ /**
342
+ * Round 2 Step 1 (acting as Extended Receiver `B` for `(\gamma_i, b_i)` against each peer `j != i`):
343
+ * Encodes `\gamma_i` and `b_i` via LHL NUMS gadget (`768` bits + `256` sacrifice bits) and emits
344
+ * `OT-Ext Step 1` columns (`\tilde{x}_i` NEVER enters OT!).
345
+ */
346
+ round2CreateOtStep1Messages() {
347
+ this.assertPhase("R1_VERIFIED");
348
+ try {
349
+ const gammaScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.gammaBytes32);
350
+ const bScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.bBytes32);
351
+ const out = [];
352
+ for (const peerSenderA of this.subset) {
353
+ if (peerSenderA === this.slot) continue;
354
+ const baseOtSender = this.baseOtChannels.asExtendedReceiverB.get(peerSenderA);
355
+ if (!baseOtSender) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Missing BaseOtSenderState for peer ${peerSenderA}`);
356
+ const gammaChoice768 = require_mta.decomposeScalarWithLhlGadget(gammaScalar, this.rng);
357
+ const bChoice768 = require_mta.decomposeScalarWithLhlGadget(bScalar, this.rng);
358
+ const { ctxMta: ctxGamma, mtaNonce: nonceGamma } = deriveMtaContextAndNonce(this.presigId32, "k_gamma", peerSenderA, this.slot);
359
+ const { ctxMta: ctxB, mtaNonce: nonceB } = deriveMtaContextAndNonce(this.presigId32, "k_b", peerSenderA, this.slot);
360
+ const ot1Gamma = require_ot.otExtendReceiverStep1({
361
+ baseOtSender,
362
+ ctxOtExt: ctxGamma,
363
+ mtaNonce: nonceGamma,
364
+ gadgetChoiceBits768: gammaChoice768,
365
+ rng: this.rng
366
+ });
367
+ let ot1B;
368
+ try {
369
+ ot1B = require_ot.otExtendReceiverStep1({
370
+ baseOtSender,
371
+ ctxOtExt: ctxB,
372
+ mtaNonce: nonceB,
373
+ gadgetChoiceBits768: bChoice768,
374
+ rng: this.rng
375
+ });
376
+ } catch (err) {
377
+ require_secp256k1_scalar.zeroizeBytes(ot1Gamma.pendingReceiver.allChoiceBits1024);
378
+ for (const r of ot1Gamma.pendingReceiver.tRows1024) require_secp256k1_scalar.zeroizeBytes(r);
379
+ throw err;
380
+ } finally {
381
+ require_secp256k1_scalar.zeroizeBytes(gammaChoice768, bChoice768);
382
+ }
383
+ this.pendingOtReceiverByPeer.set(peerSenderA, {
384
+ kGamma: ot1Gamma.pendingReceiver,
385
+ kB: ot1B.pendingReceiver
386
+ });
387
+ out.push({
388
+ fromReceiverSlotB: this.slot,
389
+ toSenderSlotA: peerSenderA,
390
+ kGammaOt1: ot1Gamma.step1Message,
391
+ kBOt1: ot1B.step1Message
392
+ });
393
+ }
394
+ this.phase = "R2_OT1_SENT";
395
+ return out;
396
+ } catch (err) {
397
+ this.abort();
398
+ throw err;
399
+ }
400
+ }
401
+ /**
402
+ * Round 2 Step 2 (acting as Extended Sender `A` against each peer `j != i`):
403
+ * Transposes `Q` and issues coin-toss `xiSeed32` challenges BEFORE touching `k_i`.
404
+ */
405
+ round2ProcessOtStep1AndCreateStep2(incomingStep1) {
406
+ this.assertPhase("R2_OT1_SENT");
407
+ try {
408
+ const expectedPeers = this.subset.filter((s) => s !== this.slot);
409
+ if (incomingStep1.length !== expectedPeers.length) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Missing Round-2 OT Step 1 messages");
410
+ const byPeer = /* @__PURE__ */ new Map();
411
+ for (const msg of incomingStep1) {
412
+ if (msg.toSenderSlotA !== this.slot || !expectedPeers.includes(msg.fromReceiverSlotB) || byPeer.has(msg.fromReceiverSlotB)) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Invalid OT Step 1 routing from slot ${msg.fromReceiverSlotB}`, msg.fromReceiverSlotB);
413
+ byPeer.set(msg.fromReceiverSlotB, msg);
414
+ }
415
+ const out = [];
416
+ for (const peerReceiverB of expectedPeers) {
417
+ const msg = byPeer.get(peerReceiverB);
418
+ const baseOtReceiver = this.baseOtChannels.asExtendedSenderA.get(peerReceiverB);
419
+ if (!baseOtReceiver) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Missing BaseOtReceiverState for peer ${peerReceiverB}`);
420
+ const { ctxMta: ctxGamma, mtaNonce: expectedNonceGamma } = deriveMtaContextAndNonce(this.presigId32, "k_gamma", this.slot, peerReceiverB);
421
+ const { ctxMta: ctxB, mtaNonce: expectedNonceB } = deriveMtaContextAndNonce(this.presigId32, "k_b", this.slot, peerReceiverB);
422
+ if (!require_secp256k1_scalar.constantTimeEqual(msg.kGammaOt1.mtaNonce, expectedNonceGamma) || !require_secp256k1_scalar.constantTimeEqual(msg.kBOt1.mtaNonce, expectedNonceB)) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `OT Step 1 mtaNonce mismatch from peer slot ${peerReceiverB}`, peerReceiverB);
423
+ const ot2Gamma = require_ot.otExtendSenderStep2({
424
+ baseOtReceiver,
425
+ ctxOtExt: ctxGamma,
426
+ step1Message: msg.kGammaOt1,
427
+ rng: this.rng
428
+ });
429
+ let ot2B;
430
+ try {
431
+ ot2B = require_ot.otExtendSenderStep2({
432
+ baseOtReceiver,
433
+ ctxOtExt: ctxB,
434
+ step1Message: msg.kBOt1,
435
+ rng: this.rng
436
+ });
437
+ } catch (err) {
438
+ require_secp256k1_scalar.zeroizeBytes(ot2Gamma.pendingSender.xiSeed32);
439
+ for (const q of ot2Gamma.pendingSender.qRows1024) require_secp256k1_scalar.zeroizeBytes(q);
440
+ throw err;
441
+ }
442
+ this.pendingOtSenderByPeer.set(peerReceiverB, {
443
+ kGamma: ot2Gamma.pendingSender,
444
+ kB: ot2B.pendingSender
445
+ });
446
+ out.push({
447
+ fromSenderSlotA: this.slot,
448
+ toReceiverSlotB: peerReceiverB,
449
+ kGammaOt2: ot2Gamma.step2Message,
450
+ kBOt2: ot2B.step2Message
451
+ });
452
+ }
453
+ this.phase = "R2_OT2_SENT";
454
+ return out;
455
+ } catch (err) {
456
+ this.abort();
457
+ throw err;
458
+ }
459
+ }
460
+ /**
461
+ * Round 2 Step 3 (acting as Extended Receiver `B`):
462
+ * Emits `KOS15` correlation proofs `(x, T_chk)`.
463
+ */
464
+ round2ProcessOtStep2AndCreateStep3(incomingStep2) {
465
+ this.assertPhase("R2_OT2_SENT");
466
+ try {
467
+ const expectedPeers = this.subset.filter((s) => s !== this.slot);
468
+ if (incomingStep2.length !== expectedPeers.length) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Missing Round-2 OT Step 2 messages");
469
+ const byPeer = /* @__PURE__ */ new Map();
470
+ for (const msg of incomingStep2) {
471
+ if (msg.toReceiverSlotB !== this.slot || !expectedPeers.includes(msg.fromSenderSlotA) || byPeer.has(msg.fromSenderSlotA)) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Invalid OT Step 2 routing from slot ${msg.fromSenderSlotA}`, msg.fromSenderSlotA);
472
+ byPeer.set(msg.fromSenderSlotA, msg);
473
+ }
474
+ const out = [];
475
+ for (const peerSenderA of expectedPeers) {
476
+ const msg = byPeer.get(peerSenderA);
477
+ const pending = this.pendingOtReceiverByPeer.get(peerSenderA);
478
+ const resGamma = require_ot.otExtendReceiverStep3({
479
+ pendingReceiver: pending.kGamma,
480
+ step2Message: msg.kGammaOt2,
481
+ peerSenderSlotA: peerSenderA
482
+ });
483
+ let resB;
484
+ try {
485
+ resB = require_ot.otExtendReceiverStep3({
486
+ pendingReceiver: pending.kB,
487
+ step2Message: msg.kBOt2,
488
+ peerSenderSlotA: peerSenderA
489
+ });
490
+ } catch (err) {
491
+ require_ot.wipeVerifiedCotReceiverRows(resGamma.receiverRows);
492
+ throw err;
493
+ }
494
+ this.verifiedOtReceiverByPeer.set(peerSenderA, {
495
+ kGamma: resGamma.receiverRows,
496
+ kB: resB.receiverRows
497
+ });
498
+ out.push({
499
+ fromReceiverSlotB: this.slot,
500
+ toSenderSlotA: peerSenderA,
501
+ kGammaOt3: resGamma.step3Message,
502
+ kBOt3: resB.step3Message
503
+ });
504
+ }
505
+ this.pendingOtReceiverByPeer.clear();
506
+ this.phase = "R2_OT3_SENT";
507
+ return out;
508
+ } catch (err) {
509
+ this.abort();
510
+ throw err;
511
+ }
512
+ }
513
+ /**
514
+ * Round 2 Step 4a (acting as Extended Sender `A`):
515
+ * Runs `KOS15` check strictly BEFORE computing single-row dual-pad `GadgetMtA` messages
516
+ * anchored to Round-1 `C_{k,i}`.
517
+ */
518
+ round2VerifyKos15AndCreateMtaPayloads(incomingStep3) {
519
+ this.assertPhase("R2_OT3_SENT");
520
+ try {
521
+ const expectedPeers = this.subset.filter((s) => s !== this.slot);
522
+ if (incomingStep3.length !== expectedPeers.length) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Missing Round-2 OT Step 3 messages");
523
+ const byPeer = /* @__PURE__ */ new Map();
524
+ for (const msg of incomingStep3) {
525
+ if (msg.toSenderSlotA !== this.slot || !expectedPeers.includes(msg.fromReceiverSlotB) || byPeer.has(msg.fromReceiverSlotB)) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Invalid OT Step 3 routing from slot ${msg.fromReceiverSlotB}`, msg.fromReceiverSlotB);
526
+ byPeer.set(msg.fromReceiverSlotB, msg);
527
+ }
528
+ const kScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.kBytes32);
529
+ const rhoKScalar = require_secp256k1_scalar.bytes32ToScalarNonZero(this.rhoKBytes32);
530
+ const ownCkComp33 = this.ownRevealMsg.ckPointComp33;
531
+ let accGamma = 0n;
532
+ let accB = 0n;
533
+ const out = [];
534
+ for (const peerReceiverB of expectedPeers) {
535
+ const msg = byPeer.get(peerReceiverB);
536
+ const pending = this.pendingOtSenderByPeer.get(peerReceiverB);
537
+ const verifiedGammaRows = require_ot.otExtendSenderStep4VerifyBeforePads({
538
+ pendingSender: pending.kGamma,
539
+ step3Message: msg.kGammaOt3
540
+ });
541
+ let verifiedBRows;
542
+ try {
543
+ verifiedBRows = require_ot.otExtendSenderStep4VerifyBeforePads({
544
+ pendingSender: pending.kB,
545
+ step3Message: msg.kBOt3
546
+ });
547
+ } catch (err) {
548
+ require_ot.wipeVerifiedCotSenderRows(verifiedGammaRows);
549
+ throw err;
550
+ }
551
+ try {
552
+ const { ctxMta: ctxGamma } = deriveMtaContextAndNonce(this.presigId32, "k_gamma", this.slot, peerReceiverB);
553
+ const { ctxMta: ctxB } = deriveMtaContextAndNonce(this.presigId32, "k_b", this.slot, peerReceiverB);
554
+ const mtaGamma = require_mta.gadgetMtaSenderCreateMessage({
555
+ ctxMta: ctxGamma,
556
+ senderSlotA: this.slot,
557
+ receiverSlotB: peerReceiverB,
558
+ verifiedSenderRows: verifiedGammaRows,
559
+ alphaScalar: kScalar,
560
+ rhoAlphaScalar: rhoKScalar,
561
+ expectedRound1CommitmentCk: ownCkComp33,
562
+ rng: this.rng
563
+ });
564
+ const mtaB = require_mta.gadgetMtaSenderCreateMessage({
565
+ ctxMta: ctxB,
566
+ senderSlotA: this.slot,
567
+ receiverSlotB: peerReceiverB,
568
+ verifiedSenderRows: verifiedBRows,
569
+ alphaScalar: kScalar,
570
+ rhoAlphaScalar: rhoKScalar,
571
+ expectedRound1CommitmentCk: ownCkComp33,
572
+ rng: this.rng
573
+ });
574
+ accGamma = require_secp256k1_scalar.addModN(accGamma, mtaGamma.senderShareModN);
575
+ accB = require_secp256k1_scalar.addModN(accB, mtaB.senderShareModN);
576
+ out.push({
577
+ fromSenderSlotA: this.slot,
578
+ toReceiverSlotB: peerReceiverB,
579
+ kGammaMta: mtaGamma.payload,
580
+ kBMta: mtaB.payload
581
+ });
582
+ } finally {
583
+ require_ot.wipeVerifiedCotSenderRows(verifiedGammaRows);
584
+ require_ot.wipeVerifiedCotSenderRows(verifiedBRows);
585
+ }
586
+ }
587
+ this.pendingOtSenderByPeer.clear();
588
+ this.senderMtaGammaAcc = accGamma;
589
+ this.senderMtaBAcc = accB;
590
+ this.phase = "R2_MTA_SENT";
591
+ return out;
592
+ } catch (err) {
593
+ this.abort();
594
+ throw err;
595
+ }
596
+ }
597
+ /**
598
+ * Round 2 Step 4b (acting as Extended Receiver `B`):
599
+ * Verifies `C_alpha === C_{k,j}`, Pedersen openings, and all 768 single-row dual-pad checks,
600
+ * then accumulates `\delta_i` and `c_i`.
601
+ */
602
+ round2VerifyMtaPayloads(incomingMta) {
603
+ this.assertPhase("R2_MTA_SENT");
604
+ try {
605
+ const expectedPeers = this.subset.filter((s) => s !== this.slot);
606
+ if (incomingMta.length !== expectedPeers.length) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Missing Round-2 GadgetMtA payloads");
607
+ const byPeer = /* @__PURE__ */ new Map();
608
+ for (const msg of incomingMta) {
609
+ if (msg.toReceiverSlotB !== this.slot || !expectedPeers.includes(msg.fromSenderSlotA) || byPeer.has(msg.fromSenderSlotA)) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Invalid MtA routing from slot ${msg.fromSenderSlotA}`, msg.fromSenderSlotA);
610
+ byPeer.set(msg.fromSenderSlotA, msg);
611
+ }
612
+ let recvGammaAcc = 0n;
613
+ let recvBAcc = 0n;
614
+ for (const peerSenderA of expectedPeers) {
615
+ const msg = byPeer.get(peerSenderA);
616
+ const verifiedRows = this.verifiedOtReceiverByPeer.get(peerSenderA);
617
+ const peerReveal = this.round1RevealsBySlot.get(peerSenderA);
618
+ try {
619
+ const { ctxMta: ctxGamma } = deriveMtaContextAndNonce(this.presigId32, "k_gamma", peerSenderA, this.slot);
620
+ const { ctxMta: ctxB } = deriveMtaContextAndNonce(this.presigId32, "k_b", peerSenderA, this.slot);
621
+ const vGamma = require_mta.gadgetMtaReceiverVerifyAndFinish({
622
+ ctxMta: ctxGamma,
623
+ senderSlotA: peerSenderA,
624
+ receiverSlotB: this.slot,
625
+ verifiedReceiverRows: verifiedRows.kGamma,
626
+ expectedRound1CommitmentCk: peerReveal.ckPointComp33,
627
+ payload: msg.kGammaMta
628
+ });
629
+ const vB = require_mta.gadgetMtaReceiverVerifyAndFinish({
630
+ ctxMta: ctxB,
631
+ senderSlotA: peerSenderA,
632
+ receiverSlotB: this.slot,
633
+ verifiedReceiverRows: verifiedRows.kB,
634
+ expectedRound1CommitmentCk: peerReveal.ckPointComp33,
635
+ payload: msg.kBMta
636
+ });
637
+ recvGammaAcc = require_secp256k1_scalar.addModN(recvGammaAcc, vGamma);
638
+ recvBAcc = require_secp256k1_scalar.addModN(recvBAcc, vB);
639
+ } finally {
640
+ require_ot.wipeVerifiedCotReceiverRows(verifiedRows.kGamma);
641
+ require_ot.wipeVerifiedCotReceiverRows(verifiedRows.kB);
642
+ }
643
+ }
644
+ this.verifiedOtReceiverByPeer.clear();
645
+ const kOwn = require_secp256k1_scalar.bytes32ToScalarNonZero(this.kBytes32);
646
+ const gammaOwn = require_secp256k1_scalar.bytes32ToScalarNonZero(this.gammaBytes32);
647
+ const bOwn = require_secp256k1_scalar.bytes32ToScalarNonZero(this.bBytes32);
648
+ const deltaI = require_secp256k1_scalar.addModN(require_secp256k1_scalar.mulModN(kOwn, gammaOwn), require_secp256k1_scalar.addModN(this.senderMtaGammaAcc, recvGammaAcc));
649
+ const cI = require_secp256k1_scalar.addModN(require_secp256k1_scalar.mulModN(kOwn, bOwn), require_secp256k1_scalar.addModN(this.senderMtaBAcc, recvBAcc));
650
+ this.deltaShareBytes32.set(require_secp256k1_scalar.scalarToBytes32(deltaI));
651
+ this.cShareBytes32.set(require_secp256k1_scalar.scalarToBytes32(cI));
652
+ require_secp256k1_scalar.zeroizeBytes(this.gammaBytes32);
653
+ this.phase = "R2_DONE";
654
+ } catch (err) {
655
+ this.abort();
656
+ throw err;
657
+ }
658
+ }
659
+ /**
660
+ * Round 3a: Reveals `(\delta_i, e_i = \tilde{x}_i - b_i mod n)` and IMMEDIATELY zeroizes
661
+ * `b_i` and `\tilde{x}_i` so `b_i` can never be reused across any abort or retry!
662
+ */
663
+ round3CreateBroadcast() {
664
+ this.assertPhase("R2_DONE");
665
+ try {
666
+ const eOwn = require_secp256k1_scalar.subModN(require_secp256k1_scalar.bytes32ToScalarNonZero(this.xTildeBytes32), require_secp256k1_scalar.bytes32ToScalarNonZero(this.bBytes32));
667
+ require_secp256k1_scalar.zeroizeBytes(this.bBytes32, this.xTildeBytes32);
668
+ this.phase = "R3_EMITTED";
669
+ return {
670
+ slot: this.slot,
671
+ deltaShareBytes32: new Uint8Array(this.deltaShareBytes32),
672
+ eShareBytes32: require_secp256k1_scalar.scalarToBytes32(eOwn)
673
+ };
674
+ } catch (err) {
675
+ this.abort();
676
+ throw err;
677
+ }
678
+ }
679
+ /**
680
+ * Round 3b: Verifies `e_j * G == \tilde{X}_j - B_j` (attributing exact `culpritSlot = j` on
681
+ * corruption), computes `R = \delta^{-1} \Gamma` (`R != O && R_x < n`), and derives `\chi_i`
682
+ * with pairwise cancelling `\zeta_{i,j} = -\zeta_{j,i} mod n`.
683
+ */
684
+ round3VerifyAndCreateRound4Broadcast(round3Messages) {
685
+ this.assertPhase("R3_EMITTED");
686
+ try {
687
+ if (round3Messages.length !== this.subset.length) throw new require_constants.MpcProtocolError("PRESIG_ROUND3_E_MISMATCH", "Missing Round-3 messages");
688
+ const bySlot = /* @__PURE__ */ new Map();
689
+ for (const msg of round3Messages) {
690
+ if (!this.subset.includes(msg.slot) || bySlot.has(msg.slot)) throw new require_constants.MpcProtocolError("PRESIG_ROUND3_E_MISMATCH", `Invalid Round-3 slot ${msg.slot}`, msg.slot);
691
+ bySlot.set(msg.slot, msg);
692
+ }
693
+ let deltaSum = 0n;
694
+ let eSum = 0n;
695
+ for (const j of this.subset) {
696
+ const msg = bySlot.get(j);
697
+ let deltaJ;
698
+ let eJ;
699
+ try {
700
+ deltaJ = require_secp256k1_scalar.bytes32ToScalarStrict(msg.deltaShareBytes32);
701
+ eJ = require_secp256k1_scalar.bytes32ToScalarStrict(msg.eShareBytes32);
702
+ } catch {
703
+ throw new require_constants.MpcProtocolError("PRESIG_ROUND3_E_MISMATCH", `Invalid scalar encoding in Round-3 message from slot ${j}`, j);
704
+ }
705
+ const xTildeJ = this.linearizedPublicSharesBySlot.get(j);
706
+ const bJ = require_secp256k1_scalar.decodePointCompressed(this.round1RevealsBySlot.get(j).bPointComp33, false);
707
+ const expectedEG = xTildeJ.subtract(bJ);
708
+ if (!require_secp256k1_scalar.mulBaseG(eJ).equals(expectedEG)) throw new require_constants.MpcProtocolError("PRESIG_ROUND3_E_MISMATCH", `Identifiable abort: corrupted e_${j} in Round 3`, j);
709
+ deltaSum = require_secp256k1_scalar.addModN(deltaSum, deltaJ);
710
+ eSum = require_secp256k1_scalar.addModN(eSum, eJ);
711
+ }
712
+ if (deltaSum === 0n) throw new require_constants.MpcProtocolError("PRESIG_DEGENERATE_NONCE", "Aggregate delta == 0 mod n");
713
+ const rPoint = require_secp256k1_scalar.mulPoint(this.aggGammaPoint, require_secp256k1_scalar.invModN(deltaSum));
714
+ if (rPoint.is0()) throw new require_constants.MpcProtocolError("PRESIG_DEGENERATE_NONCE", "Presignature R == O");
715
+ const rAffine = rPoint.toAffine();
716
+ if (rAffine.x >= require_constants.SECP256K1_N || rAffine.x % require_constants.SECP256K1_N === 0n) throw new require_constants.MpcProtocolError("PRESIG_DEGENERATE_NONCE", "Presignature R_x >= n or R_x mod n == 0");
717
+ this.rPoint = rPoint;
718
+ let zetaSum = 0n;
719
+ for (const j of this.subset) {
720
+ if (j === this.slot) continue;
721
+ const peerEpk = this.round1RevealsBySlot.get(j).zetaEpk32;
722
+ const shared = require_secp256k1_scalar.computeX25519SharedSecret(this.zetaEsk32, peerEpk, "PRESIG_POK_INVALID", j);
723
+ const minSlot = Math.min(this.slot, j);
724
+ const maxSlot = Math.max(this.slot, j);
725
+ const baseZeta = require_secp256k1_scalar.H_scalar(require_constants.DST_PRESIG_ZETA_V1, this.presigId32, require_secp256k1_scalar.u16be(minSlot), require_secp256k1_scalar.u16be(maxSlot), shared);
726
+ require_secp256k1_scalar.zeroizeBytes(shared);
727
+ const zetaIJ = this.slot < j ? baseZeta : require_secp256k1_scalar.negModN(baseZeta);
728
+ zetaSum = require_secp256k1_scalar.addModN(zetaSum, zetaIJ);
729
+ }
730
+ require_secp256k1_scalar.zeroizeBytes(this.zetaEsk32, this.deltaShareBytes32);
731
+ const kOwn = require_secp256k1_scalar.bytes32ToScalarNonZero(this.kBytes32);
732
+ const rhoKOwn = require_secp256k1_scalar.bytes32ToScalarNonZero(this.rhoKBytes32);
733
+ const cOwn = require_secp256k1_scalar.bytes32ToScalarStrict(this.cShareBytes32);
734
+ require_secp256k1_scalar.zeroizeBytes(this.cShareBytes32);
735
+ const chiOwn = require_secp256k1_scalar.addModN(require_secp256k1_scalar.addModN(cOwn, require_secp256k1_scalar.mulModN(kOwn, eSum)), zetaSum);
736
+ if (chiOwn === 0n) throw new require_constants.MpcProtocolError("PRESIG_DEGENERATE_NONCE", "Degenerate zero chi_i share");
737
+ this.chiShareBytes32.set(require_secp256k1_scalar.scalarToBytes32(chiOwn));
738
+ const kSharePoint = require_secp256k1_scalar.mulPoint(rPoint, kOwn);
739
+ const zSharePoint = require_secp256k1_scalar.mulPoint(rPoint, chiOwn);
740
+ const piLinK = require_secp256k1_scalar.createLinearPedersenDleqProof({
741
+ dst: require_constants.DST_PRESIG_PI_LIN_K_V1,
742
+ baseR: rPoint,
743
+ pointK: kSharePoint,
744
+ commitmentCk: require_secp256k1_scalar.decodePointCompressed(this.ownRevealMsg.ckPointComp33, false),
745
+ kScalar: kOwn,
746
+ rhoScalar: rhoKOwn,
747
+ contextParts: [this.presigId32, require_secp256k1_scalar.u16be(this.slot)],
748
+ rng: this.rng
749
+ });
750
+ require_secp256k1_scalar.zeroizeBytes(this.rhoKBytes32);
751
+ const piPokZ = require_secp256k1_scalar.createSchnorrPokInBase({
752
+ dst: require_constants.DST_PRESIG_PI_POK_Z_V1,
753
+ basePoint: rPoint,
754
+ statementPoint: zSharePoint,
755
+ secretScalar: chiOwn,
756
+ contextParts: [this.presigId32, require_secp256k1_scalar.u16be(this.slot)],
757
+ rng: this.rng
758
+ });
759
+ this.phase = "R4_EMITTED";
760
+ return {
761
+ slot: this.slot,
762
+ kSharePointComp33: require_secp256k1_scalar.encodePointCompressed(kSharePoint),
763
+ zSharePointComp33: require_secp256k1_scalar.encodePointCompressed(zSharePoint),
764
+ piLinK,
765
+ piPokZ
766
+ };
767
+ } catch (err) {
768
+ this.abort();
769
+ throw err;
770
+ }
771
+ }
772
+ /**
773
+ * Round 4b: Verifies `\pi^{lin}_{K,j}` and `\pi^{pok}_{Z,j}` in base `R` for every `j \in S`
774
+ * (attributing exact `culpritSlot = j` on invalid proof), checks `\sum K_j == G` and
775
+ * `\sum Z_j == P_root`, and outputs the single-use `PresignatureShareRecord`.
776
+ */
777
+ round4VerifyAndFinalize(round4Messages) {
778
+ this.assertPhase("R4_EMITTED");
779
+ try {
780
+ if (round4Messages.length !== this.subset.length) throw new require_constants.MpcProtocolError("PRESIG_GLOBAL_SUM_MISMATCH", "Missing Round-4 messages");
781
+ const bySlot = /* @__PURE__ */ new Map();
782
+ for (const msg of round4Messages) {
783
+ if (!this.subset.includes(msg.slot) || bySlot.has(msg.slot)) throw new require_constants.MpcProtocolError("PRESIG_GLOBAL_SUM_MISMATCH", `Invalid Round-4 slot ${msg.slot}`, msg.slot);
784
+ bySlot.set(msg.slot, msg);
785
+ }
786
+ const rPoint = this.rPoint;
787
+ let sumK = require_secp256k1_scalar.SECP256K1_ZERO;
788
+ let sumZ = require_secp256k1_scalar.SECP256K1_ZERO;
789
+ const kPointsComp33BySlot = /* @__PURE__ */ new Map();
790
+ const zPointsComp33BySlot = /* @__PURE__ */ new Map();
791
+ for (const j of this.subset) {
792
+ const msg = bySlot.get(j);
793
+ let kJ;
794
+ let zJ;
795
+ try {
796
+ kJ = require_secp256k1_scalar.decodePointCompressed(msg.kSharePointComp33, false);
797
+ } catch {
798
+ throw new require_constants.MpcProtocolError("PRESIG_PI_LIN_K_INVALID", `Invalid K_${j} point from slot ${j}`, j);
799
+ }
800
+ try {
801
+ zJ = require_secp256k1_scalar.decodePointCompressed(msg.zSharePointComp33, false);
802
+ } catch {
803
+ throw new require_constants.MpcProtocolError("PRESIG_PI_POK_Z_INVALID", `Invalid Z_${j} point from slot ${j}`, j);
804
+ }
805
+ const ckJ = require_secp256k1_scalar.decodePointCompressed(this.round1RevealsBySlot.get(j).ckPointComp33, false);
806
+ if (!require_secp256k1_scalar.verifyLinearPedersenDleqProof({
807
+ dst: require_constants.DST_PRESIG_PI_LIN_K_V1,
808
+ baseR: rPoint,
809
+ pointK: kJ,
810
+ commitmentCk: ckJ,
811
+ proof: msg.piLinK,
812
+ contextParts: [this.presigId32, require_secp256k1_scalar.u16be(j)]
813
+ })) throw new require_constants.MpcProtocolError("PRESIG_PI_LIN_K_INVALID", `Identifiable abort: invalid pi^{lin}_{K,${j}} proof from slot ${j}`, j);
814
+ if (!require_secp256k1_scalar.verifySchnorrPokInBase({
815
+ dst: require_constants.DST_PRESIG_PI_POK_Z_V1,
816
+ basePoint: rPoint,
817
+ statementPoint: zJ,
818
+ proof: msg.piPokZ,
819
+ contextParts: [this.presigId32, require_secp256k1_scalar.u16be(j)]
820
+ })) throw new require_constants.MpcProtocolError("PRESIG_PI_POK_Z_INVALID", `Identifiable abort: invalid pi^{pok}_{Z,${j}} proof from slot ${j}`, j);
821
+ sumK = sumK.add(kJ);
822
+ sumZ = sumZ.add(zJ);
823
+ kPointsComp33BySlot.set(j, new Uint8Array(msg.kSharePointComp33));
824
+ zPointsComp33BySlot.set(j, new Uint8Array(msg.zSharePointComp33));
825
+ }
826
+ const pRoot = require_secp256k1_scalar.decodePointCompressed(this.cluster.rootPublicKeyComp33, false);
827
+ if (!sumK.equals(require_secp256k1_scalar.SECP256K1_G)) throw new require_constants.MpcProtocolError("PRESIG_GLOBAL_SUM_MISMATCH", "Round-4 sum_{j in S} K_j != G");
828
+ if (!sumZ.equals(pRoot)) throw new require_constants.MpcProtocolError("PRESIG_GLOBAL_SUM_MISMATCH", "Round-4 sum_{j in S} Z_j != P_root");
829
+ const kCopy = new Uint8Array(this.kBytes32);
830
+ const chiCopy = new Uint8Array(this.chiShareBytes32);
831
+ this.wipeAllEphemeralSecrets();
832
+ this.phase = "COMPLETED";
833
+ const presigIdCopy = new Uint8Array(this.presigId32);
834
+ return {
835
+ slot: this.slot,
836
+ publicVerification: {
837
+ presigId32: presigIdCopy,
838
+ presigIdHex: (0, _noble_hashes_utils_js.bytesToHex)(presigIdCopy),
839
+ rootId: this.cluster.rootId,
840
+ keyEpoch: this.cluster.keyEpoch,
841
+ subset: [...this.subset],
842
+ hDkg32: new Uint8Array(this.cluster.hDkg32),
843
+ rootPublicKeyComp33: new Uint8Array(this.cluster.rootPublicKeyComp33),
844
+ rPointComp33: require_secp256k1_scalar.encodePointCompressed(rPoint),
845
+ kPointsComp33BySlot,
846
+ zPointsComp33BySlot
847
+ },
848
+ kShareBytes32: kCopy,
849
+ chiShareBytes32: chiCopy
850
+ };
851
+ } catch (err) {
852
+ this.abort();
853
+ throw err;
854
+ }
855
+ }
856
+ };
857
+ /**
858
+ * Volatile in-RAM single-use presignature pool (`design.md` §4.5(1), §7):
859
+ * - Synchronous `consume-before-use` atomic lock: under concurrent `Promise.all` contention,
860
+ * exact 1 caller acquires the `PresignatureShareRecord` and all others synchronously fail.
861
+ * - Strict `requestedSubset === presig.S` enforcement.
862
+ * - `wipeRoot(rootId)` / `wipeAll()` zeroize all RAM presignature scalars on Panic or shutdown.
863
+ */
864
+ var VolatilePresignaturePool = class {
865
+ constructor() {
866
+ this.activeByPresigIdHex = /* @__PURE__ */ new Map();
867
+ this.tombstonedPresigIdHexes = /* @__PURE__ */ new Set();
868
+ }
869
+ get size() {
870
+ return this.activeByPresigIdHex.size;
871
+ }
872
+ isTombstoned(presigId) {
873
+ const idHex = (0, _noble_hashes_utils_js.bytesToHex)(require_secp256k1_scalar.normalizeHexBytes(presigId, 32));
874
+ return this.tombstonedPresigIdHexes.has(idHex);
875
+ }
876
+ store(record) {
877
+ if (!record || !record.publicVerification) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Missing presignature record");
878
+ const wipeCallerBuffers = () => {
879
+ require_secp256k1_scalar.zeroizeBytes(record.kShareBytes32, record.chiShareBytes32);
880
+ };
881
+ let canonicalIdBytes32;
882
+ try {
883
+ canonicalIdBytes32 = require_secp256k1_scalar.normalizeHexBytes(record.publicVerification.presigId32, 32);
884
+ } catch (err) {
885
+ wipeCallerBuffers();
886
+ throw err;
887
+ }
888
+ const canonicalIdHex = (0, _noble_hashes_utils_js.bytesToHex)(canonicalIdBytes32);
889
+ if (record.publicVerification.presigIdHex !== canonicalIdHex) {
890
+ wipeCallerBuffers();
891
+ throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Uncanonicalized presigIdHex: expected ${canonicalIdHex}, got ${String(record.publicVerification.presigIdHex)}`);
892
+ }
893
+ if (this.tombstonedPresigIdHexes.has(canonicalIdHex) || this.activeByPresigIdHex.has(canonicalIdHex)) {
894
+ if (this.activeByPresigIdHex.get(canonicalIdHex) !== record) wipeCallerBuffers();
895
+ throw new require_constants.MpcProtocolError("PRESIG_ALREADY_CONSUMED", `Duplicate or tombstoned presigId ${canonicalIdHex}`);
896
+ }
897
+ try {
898
+ require_secp256k1_scalar.bytes32ToScalarNonZero(record.kShareBytes32);
899
+ require_secp256k1_scalar.bytes32ToScalarNonZero(record.chiShareBytes32);
900
+ } catch (err) {
901
+ wipeCallerBuffers();
902
+ throw err;
903
+ }
904
+ const poolOwnedRecord = {
905
+ slot: record.slot,
906
+ publicVerification: record.publicVerification,
907
+ kShareBytes32: new Uint8Array(record.kShareBytes32),
908
+ chiShareBytes32: new Uint8Array(record.chiShareBytes32)
909
+ };
910
+ wipeCallerBuffers();
911
+ this.activeByPresigIdHex.set(canonicalIdHex, poolOwnedRecord);
912
+ }
913
+ /**
914
+ * Synchronously validates `(rootId, requestedSubset)` and atomically removes + tombstones
915
+ * `presigId` in a single JS turn with zero `await` points.
916
+ */
917
+ consume(params) {
918
+ const idHex = (0, _noble_hashes_utils_js.bytesToHex)(require_secp256k1_scalar.normalizeHexBytes(params.presigId, 32));
919
+ if (this.tombstonedPresigIdHexes.has(idHex)) throw new require_constants.MpcProtocolError("PRESIG_ALREADY_CONSUMED", `Presignature ${idHex} has already been consumed or wiped`);
920
+ const record = this.activeByPresigIdHex.get(idHex);
921
+ if (!record) throw new require_constants.MpcProtocolError("PRESIG_ALREADY_CONSUMED", `Unknown presignature ${idHex}`);
922
+ if (record.publicVerification.rootId !== params.rootId) throw new require_constants.MpcProtocolError("PRESIG_SUBSET_MISMATCH", `Presignature rootId mismatch: expected ${record.publicVerification.rootId}, got ${params.rootId}`);
923
+ if (!require_secp256k1_scalar.subsetsEqual(params.requestedSubset, record.publicVerification.subset)) throw new require_constants.MpcProtocolError("PRESIG_SUBSET_MISMATCH", `Signing request subset [${params.requestedSubset.join(",")}] != presignature subset [${record.publicVerification.subset.join(",")}]`);
924
+ if (params.preValidateRecord) params.preValidateRecord(record);
925
+ this.activeByPresigIdHex.delete(idHex);
926
+ this.tombstonedPresigIdHexes.add(idHex);
927
+ return record;
928
+ }
929
+ wipeRoot(rootId) {
930
+ let wiped = 0;
931
+ for (const [idHex, rec] of this.activeByPresigIdHex) if (rec.publicVerification.rootId === rootId) {
932
+ require_secp256k1_scalar.zeroizeBytes(rec.kShareBytes32, rec.chiShareBytes32);
933
+ this.activeByPresigIdHex.delete(idHex);
934
+ this.tombstonedPresigIdHexes.add(idHex);
935
+ wiped += 1;
936
+ }
937
+ return wiped;
938
+ }
939
+ wipeAll() {
940
+ let wiped = 0;
941
+ for (const [idHex, rec] of this.activeByPresigIdHex) {
942
+ require_secp256k1_scalar.zeroizeBytes(rec.kShareBytes32, rec.chiShareBytes32);
943
+ this.tombstonedPresigIdHexes.add(idHex);
944
+ wiped += 1;
945
+ }
946
+ this.activeByPresigIdHex.clear();
947
+ return wiped;
948
+ }
949
+ };
950
+
951
+ //#endregion
952
+ exports.PresignPartySession = PresignPartySession;
953
+ exports.VolatilePresignaturePool = VolatilePresignaturePool;
954
+ exports.computeSubsetBoundPresigId = computeSubsetBoundPresigId;