@ocap/mpc-hsm 0.0.0-stage → 1.33.6
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/esm/constants.d.mts +76 -0
- package/esm/constants.mjs +78 -0
- package/esm/dkg.d.mts +279 -0
- package/esm/dkg.mjs +903 -0
- package/esm/index.d.mts +9 -0
- package/esm/index.mjs +10 -0
- package/esm/mta.d.mts +63 -0
- package/esm/mta.mjs +275 -0
- package/esm/ot.d.mts +187 -0
- package/esm/ot.mjs +593 -0
- package/esm/presign.d.mts +242 -0
- package/esm/presign.mjs +951 -0
- package/esm/secp256k1-scalar.d.mts +141 -0
- package/esm/secp256k1-scalar.mjs +465 -0
- package/esm/sign.d.mts +113 -0
- package/esm/sign.mjs +225 -0
- package/esm/tweak.d.mts +100 -0
- package/esm/tweak.mjs +168 -0
- package/lib/constants.cjs +136 -0
- package/lib/constants.d.cts +76 -0
- package/lib/dkg.cjs +910 -0
- package/lib/dkg.d.cts +279 -0
- package/lib/index.cjs +171 -0
- package/lib/index.d.cts +9 -0
- package/lib/mta.cjs +280 -0
- package/lib/mta.d.cts +63 -0
- package/lib/ot.cjs +609 -0
- package/lib/ot.d.cts +187 -0
- package/lib/presign.cjs +954 -0
- package/lib/presign.d.cts +242 -0
- package/lib/secp256k1-scalar.cjs +518 -0
- package/lib/secp256k1-scalar.d.cts +141 -0
- package/lib/sign.cjs +233 -0
- package/lib/sign.d.cts +113 -0
- package/lib/tweak.cjs +178 -0
- package/lib/tweak.d.cts +100 -0
- package/package.json +75 -4
- package/README.md +0 -3
package/esm/ot.mjs
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { DST_BASEOT_KEY_V1, DST_BASEOT_S_POK_V1, DST_KOS15_CHI_V1, DST_OT_CTR_IV_V1, MpcProtocolError, OT_COLUMN_BYTES, OT_EXTENSION_ROWS, OT_GADGET_BITS, OT_KAPPA_BITS, OT_KAPPA_BYTES, OT_SACRIFICE_BITS } from "./constants.mjs";
|
|
2
|
+
import { SECP256K1_G, bytesToHex, constantTimeEqual, createSchnorrPokInBase, decodePointCompressed, defaultRandomBytes, encodePointCompressed, hashTranscriptSha256, lengthPrefixedEncode, mulBaseG, mulPoint, sampleScalarNonZero, scalarToBytes32, u16be, verifySchnorrPokInBase, zeroizeBytes } from "./secp256k1-scalar.mjs";
|
|
3
|
+
import { createCipheriv } from "node:crypto";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { shake256 } from "@noble/hashes/sha3.js";
|
|
6
|
+
|
|
7
|
+
//#region src/ot.ts
|
|
8
|
+
function wipeBaseOtSenderState(state) {
|
|
9
|
+
state.invalidated = true;
|
|
10
|
+
for (const [k0, k1] of state.seedPairs) zeroizeBytes(k0, k1);
|
|
11
|
+
}
|
|
12
|
+
function wipeBaseOtReceiverState(state) {
|
|
13
|
+
state.invalidated = true;
|
|
14
|
+
zeroizeBytes(state.deltaBytes16, state.deltaBits128);
|
|
15
|
+
for (const seed of state.chosenSeeds) zeroizeBytes(seed);
|
|
16
|
+
}
|
|
17
|
+
function unpackBitsLSB(packed, bitCount) {
|
|
18
|
+
const out = new Uint8Array(bitCount);
|
|
19
|
+
for (let i = 0; i < bitCount; i++) out[i] = packed[i >>> 3] >>> (i & 7) & 1;
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function packBitsLSB(bits) {
|
|
23
|
+
if (bits.length % 8 !== 0) throw new MpcProtocolError("INVALID_PARAMETER", `Bit length ${bits.length} must be multiple of 8`);
|
|
24
|
+
const out = new Uint8Array(bits.length >>> 3);
|
|
25
|
+
for (let i = 0; i < bits.length; i++) {
|
|
26
|
+
const bit = bits[i];
|
|
27
|
+
if (bit !== 0 && bit !== 1) throw new MpcProtocolError("INVALID_PARAMETER", `Invalid bit value at ${i}: ${bit}`);
|
|
28
|
+
if (bit === 1) out[i >>> 3] |= 1 << (i & 7);
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
function baseOtSenderStep1(params) {
|
|
33
|
+
const { ctxBaseOt, senderSlotB, receiverSlotA, rng = defaultRandomBytes } = params;
|
|
34
|
+
if (senderSlotB === receiverSlotA || senderSlotB < 1 || receiverSlotA < 1) throw new MpcProtocolError("INVALID_PARAMETER", "Base-OT requires distinct positive slots");
|
|
35
|
+
const yScalar = sampleScalarNonZero(rng);
|
|
36
|
+
const sPoint = mulBaseG(yScalar);
|
|
37
|
+
const sPointComp33 = encodePointCompressed(sPoint);
|
|
38
|
+
const pokS = createSchnorrPokInBase({
|
|
39
|
+
dst: DST_BASEOT_S_POK_V1,
|
|
40
|
+
basePoint: SECP256K1_G,
|
|
41
|
+
statementPoint: sPoint,
|
|
42
|
+
secretScalar: yScalar,
|
|
43
|
+
contextParts: [
|
|
44
|
+
ctxBaseOt,
|
|
45
|
+
u16be(senderSlotB),
|
|
46
|
+
u16be(receiverSlotA)
|
|
47
|
+
],
|
|
48
|
+
rng
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
pending: {
|
|
52
|
+
senderSlotB,
|
|
53
|
+
receiverSlotA,
|
|
54
|
+
ctxBaseOt: new Uint8Array(ctxBaseOt),
|
|
55
|
+
yScalarBytes32: scalarToBytes32(yScalar),
|
|
56
|
+
sPointComp33,
|
|
57
|
+
completed: false
|
|
58
|
+
},
|
|
59
|
+
message: {
|
|
60
|
+
senderSlotB,
|
|
61
|
+
receiverSlotA,
|
|
62
|
+
sPointComp33,
|
|
63
|
+
pokS
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function sampleNonZeroDelta16(rng) {
|
|
68
|
+
for (;;) {
|
|
69
|
+
const candidate = rng(OT_KAPPA_BYTES);
|
|
70
|
+
if (candidate.length !== OT_KAPPA_BYTES) {
|
|
71
|
+
zeroizeBytes(candidate);
|
|
72
|
+
throw new MpcProtocolError("INVALID_PARAMETER", "RNG must produce 16 bytes for Delta");
|
|
73
|
+
}
|
|
74
|
+
let nonZero = 0;
|
|
75
|
+
for (let i = 0; i < OT_KAPPA_BYTES; i++) nonZero |= candidate[i];
|
|
76
|
+
if (nonZero !== 0) return candidate;
|
|
77
|
+
zeroizeBytes(candidate);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function baseOtReceiverStep2(params) {
|
|
81
|
+
const { ctxBaseOt, senderSlotB, receiverSlotA, step1Message, rng = defaultRandomBytes } = params;
|
|
82
|
+
if (!step1Message || step1Message.senderSlotB !== senderSlotB || step1Message.receiverSlotA !== receiverSlotA) throw new MpcProtocolError("BASE_OT_INVALID_PROOF", "Base-OT slot header mismatch", senderSlotB);
|
|
83
|
+
let sPoint;
|
|
84
|
+
try {
|
|
85
|
+
sPoint = decodePointCompressed(step1Message.sPointComp33, false);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
throw new MpcProtocolError("BASE_OT_INVALID_POINT", `Invalid Base-OT sender point S from slot ${senderSlotB}: ${err.message}`, senderSlotB);
|
|
88
|
+
}
|
|
89
|
+
if (!verifySchnorrPokInBase({
|
|
90
|
+
dst: DST_BASEOT_S_POK_V1,
|
|
91
|
+
basePoint: SECP256K1_G,
|
|
92
|
+
statementPoint: sPoint,
|
|
93
|
+
proof: step1Message.pokS,
|
|
94
|
+
contextParts: [
|
|
95
|
+
ctxBaseOt,
|
|
96
|
+
u16be(senderSlotB),
|
|
97
|
+
u16be(receiverSlotA)
|
|
98
|
+
]
|
|
99
|
+
})) throw new MpcProtocolError("BASE_OT_INVALID_PROOF", `Invalid Base-OT sender PoK from slot ${senderSlotB}`, senderSlotB);
|
|
100
|
+
const deltaBytes16 = sampleNonZeroDelta16(rng);
|
|
101
|
+
const deltaBits128 = unpackBitsLSB(deltaBytes16, OT_KAPPA_BITS);
|
|
102
|
+
const rPointsComp33 = new Array(OT_KAPPA_BITS);
|
|
103
|
+
const chosenSeeds = new Array(OT_KAPPA_BITS);
|
|
104
|
+
for (let m = 0; m < OT_KAPPA_BITS; m++) {
|
|
105
|
+
const bit = deltaBits128[m];
|
|
106
|
+
for (;;) {
|
|
107
|
+
const xM = sampleScalarNonZero(rng);
|
|
108
|
+
const xG = mulBaseG(xM);
|
|
109
|
+
const rPoint = bit === 1 ? xG.add(sPoint) : xG;
|
|
110
|
+
if (rPoint.is0() || rPoint.equals(sPoint)) continue;
|
|
111
|
+
const rComp33 = encodePointCompressed(rPoint);
|
|
112
|
+
const sharedComp33 = encodePointCompressed(mulPoint(sPoint, xM));
|
|
113
|
+
chosenSeeds[m] = hashTranscriptSha256(DST_BASEOT_KEY_V1, ctxBaseOt, u16be(senderSlotB), u16be(receiverSlotA), u16be(m), step1Message.sPointComp33, rComp33, sharedComp33);
|
|
114
|
+
rPointsComp33[m] = rComp33;
|
|
115
|
+
zeroizeBytes(sharedComp33);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
receiverState: {
|
|
121
|
+
ownerSlotA: receiverSlotA,
|
|
122
|
+
peerSlotB: senderSlotB,
|
|
123
|
+
ctxBaseOt: new Uint8Array(ctxBaseOt),
|
|
124
|
+
deltaBytes16,
|
|
125
|
+
deltaBits128,
|
|
126
|
+
chosenSeeds,
|
|
127
|
+
usedSessionKeys: /* @__PURE__ */ new Set(),
|
|
128
|
+
invalidated: false
|
|
129
|
+
},
|
|
130
|
+
message: {
|
|
131
|
+
senderSlotB,
|
|
132
|
+
receiverSlotA,
|
|
133
|
+
rPointsComp33
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function baseOtSenderStep3(params) {
|
|
138
|
+
const { pending, step2Message } = params;
|
|
139
|
+
if (pending.completed) throw new MpcProtocolError("OT_STATE_INVALIDATED", "Base-OT pending session already finalized");
|
|
140
|
+
pending.completed = true;
|
|
141
|
+
try {
|
|
142
|
+
if (step2Message.senderSlotB !== pending.senderSlotB || step2Message.receiverSlotA !== pending.receiverSlotA || step2Message.rPointsComp33.length !== OT_KAPPA_BITS) throw new MpcProtocolError("BASE_OT_INVALID_POINT", "Invalid Base-OT Step 2 message structure", pending.receiverSlotA);
|
|
143
|
+
const sPoint = decodePointCompressed(pending.sPointComp33, false);
|
|
144
|
+
let yScalar = 0n;
|
|
145
|
+
for (let i = 0; i < 32; i++) yScalar = yScalar << 8n | BigInt(pending.yScalarBytes32[i]);
|
|
146
|
+
const tPoint = mulPoint(sPoint, yScalar);
|
|
147
|
+
const seedPairs = new Array(OT_KAPPA_BITS);
|
|
148
|
+
for (let m = 0; m < OT_KAPPA_BITS; m++) {
|
|
149
|
+
const rComp33 = step2Message.rPointsComp33[m];
|
|
150
|
+
let rPoint;
|
|
151
|
+
try {
|
|
152
|
+
rPoint = decodePointCompressed(rComp33, false);
|
|
153
|
+
} catch {
|
|
154
|
+
throw new MpcProtocolError("BASE_OT_INVALID_POINT", `Invalid R_${m} point from slot ${pending.receiverSlotA}`, pending.receiverSlotA);
|
|
155
|
+
}
|
|
156
|
+
if (rPoint.equals(sPoint)) throw new MpcProtocolError("BASE_OT_INVALID_POINT", `Degenerate R_${m} == S from slot ${pending.receiverSlotA}`, pending.receiverSlotA);
|
|
157
|
+
const p0 = mulPoint(rPoint, yScalar);
|
|
158
|
+
const p1 = p0.subtract(tPoint);
|
|
159
|
+
const p0Comp33 = encodePointCompressed(p0);
|
|
160
|
+
const p1Comp33 = encodePointCompressed(p1);
|
|
161
|
+
const k0 = hashTranscriptSha256(DST_BASEOT_KEY_V1, pending.ctxBaseOt, u16be(pending.senderSlotB), u16be(pending.receiverSlotA), u16be(m), pending.sPointComp33, rComp33, p0Comp33);
|
|
162
|
+
const k1 = hashTranscriptSha256(DST_BASEOT_KEY_V1, pending.ctxBaseOt, u16be(pending.senderSlotB), u16be(pending.receiverSlotA), u16be(m), pending.sPointComp33, rComp33, p1Comp33);
|
|
163
|
+
zeroizeBytes(p0Comp33, p1Comp33);
|
|
164
|
+
seedPairs[m] = [k0, k1];
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
ownerSlotB: pending.senderSlotB,
|
|
168
|
+
peerSlotA: pending.receiverSlotA,
|
|
169
|
+
ctxBaseOt: new Uint8Array(pending.ctxBaseOt),
|
|
170
|
+
seedPairs,
|
|
171
|
+
usedSessionKeys: /* @__PURE__ */ new Set(),
|
|
172
|
+
invalidated: false
|
|
173
|
+
};
|
|
174
|
+
} finally {
|
|
175
|
+
zeroizeBytes(pending.yScalarBytes32);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const ZERO_COLUMN_128 = new Uint8Array(OT_COLUMN_BYTES);
|
|
179
|
+
function expandColumnPrgAes256Ctr(seed32, ctxOtExt, mtaNonce, columnIndex) {
|
|
180
|
+
const iv16 = sha256(lengthPrefixedEncode(DST_OT_CTR_IV_V1, [
|
|
181
|
+
ctxOtExt,
|
|
182
|
+
mtaNonce,
|
|
183
|
+
u16be(columnIndex)
|
|
184
|
+
])).subarray(0, 16);
|
|
185
|
+
iv16[12] = 0;
|
|
186
|
+
iv16[13] = 0;
|
|
187
|
+
iv16[14] = 0;
|
|
188
|
+
iv16[15] = 0;
|
|
189
|
+
const cipher = createCipheriv("aes-256-ctr", seed32, iv16);
|
|
190
|
+
const out = new Uint8Array(OT_COLUMN_BYTES);
|
|
191
|
+
const part1 = cipher.update(ZERO_COLUMN_128);
|
|
192
|
+
const part2 = cipher.final();
|
|
193
|
+
out.set(part1, 0);
|
|
194
|
+
if (part2.length > 0) out.set(part2, part1.length);
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
const EKLUNDH_MASKS_128 = [
|
|
198
|
+
113427455640312821154458202477256070485n,
|
|
199
|
+
68056473384187692692674921486353642291n,
|
|
200
|
+
20016609818878733144904388672456953615n,
|
|
201
|
+
1324055902416102970674609367438786815n,
|
|
202
|
+
5192217631581220737344928932233215n,
|
|
203
|
+
79228162495817593524129366015n,
|
|
204
|
+
18446744073709551615n
|
|
205
|
+
];
|
|
206
|
+
function bytes16ToU128LE(bytes16, offset = 0) {
|
|
207
|
+
let acc = 0n;
|
|
208
|
+
for (let i = 15; i >= 0; i--) acc = acc << 8n | BigInt(bytes16[offset + i]);
|
|
209
|
+
return acc;
|
|
210
|
+
}
|
|
211
|
+
function u128ToBytes16LE(value) {
|
|
212
|
+
const out = new Uint8Array(16);
|
|
213
|
+
let rem = value;
|
|
214
|
+
for (let i = 0; i < 16; i++) {
|
|
215
|
+
out[i] = Number(rem & 255n);
|
|
216
|
+
rem >>= 8n;
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Eklundh recursive butterfly `128 x 1024` bit-matrix transpose (`7` log-stages per `128 x 128`
|
|
222
|
+
* sub-block, zero data-dependent branches).
|
|
223
|
+
*
|
|
224
|
+
* Input: `128` columns (`m = 0..127`), each `128` bytes (`1024` bits LSB-first).
|
|
225
|
+
* Output: `1024` rows (`l = 0..1023`), each `16` bytes (`128` bits LSB-first), where
|
|
226
|
+
* bit `m` of `rows[l]` equals bit `l` of `columns[m]`.
|
|
227
|
+
*/
|
|
228
|
+
function eklundhTranspose128x1024(columns) {
|
|
229
|
+
if (columns.length !== OT_KAPPA_BITS) throw new MpcProtocolError("INVALID_PARAMETER", `Expected ${OT_KAPPA_BITS} columns, got ${columns.length}`);
|
|
230
|
+
for (let m = 0; m < OT_KAPPA_BITS; m++) if (columns[m].length !== OT_COLUMN_BYTES) throw new MpcProtocolError("INVALID_PARAMETER", `Column ${m} must be ${OT_COLUMN_BYTES} bytes`);
|
|
231
|
+
const rows = new Array(OT_EXTENSION_ROWS);
|
|
232
|
+
const blockWords = new Array(128);
|
|
233
|
+
for (let blockIdx = 0; blockIdx < 8; blockIdx++) {
|
|
234
|
+
const byteOffset = blockIdx * 16;
|
|
235
|
+
for (let m = 0; m < 128; m++) blockWords[m] = bytes16ToU128LE(columns[m], byteOffset);
|
|
236
|
+
for (let stage = 0; stage < 7; stage++) {
|
|
237
|
+
const w = 1 << stage;
|
|
238
|
+
const shift = BigInt(w);
|
|
239
|
+
const mask = EKLUNDH_MASKS_128[stage];
|
|
240
|
+
for (let base = 0; base < 128; base += w << 1) for (let j = 0; j < w; j++) {
|
|
241
|
+
const topIdx = base + j;
|
|
242
|
+
const botIdx = topIdx + w;
|
|
243
|
+
const a = blockWords[topIdx];
|
|
244
|
+
const b = blockWords[botIdx];
|
|
245
|
+
const t = (a >> shift ^ b) & mask;
|
|
246
|
+
blockWords[botIdx] = b ^ t;
|
|
247
|
+
blockWords[topIdx] = a ^ t << shift;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const rowBase = blockIdx * 128;
|
|
251
|
+
for (let r = 0; r < 128; r++) {
|
|
252
|
+
rows[rowBase + r] = u128ToBytes16LE(blockWords[r]);
|
|
253
|
+
blockWords[r] = 0n;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return rows;
|
|
257
|
+
}
|
|
258
|
+
function readU32LE(bytes, offset) {
|
|
259
|
+
return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] << 24) >>> 0;
|
|
260
|
+
}
|
|
261
|
+
function writeU32LE(out, offset, val) {
|
|
262
|
+
out[offset] = val & 255;
|
|
263
|
+
out[offset + 1] = val >>> 8 & 255;
|
|
264
|
+
out[offset + 2] = val >>> 16 & 255;
|
|
265
|
+
out[offset + 3] = val >>> 24 & 255;
|
|
266
|
+
}
|
|
267
|
+
function gf128MulAccumulateWords(acc, a0, a1, a2, a3, b0, b1, b2, b3) {
|
|
268
|
+
let c0 = a0 >>> 0;
|
|
269
|
+
let c1 = a1 >>> 0;
|
|
270
|
+
let c2 = a2 >>> 0;
|
|
271
|
+
let c3 = a3 >>> 0;
|
|
272
|
+
let r0 = acc[0];
|
|
273
|
+
let r1 = acc[1];
|
|
274
|
+
let r2 = acc[2];
|
|
275
|
+
let r3 = acc[3];
|
|
276
|
+
const bWords = [
|
|
277
|
+
b0 >>> 0,
|
|
278
|
+
b1 >>> 0,
|
|
279
|
+
b2 >>> 0,
|
|
280
|
+
b3 >>> 0
|
|
281
|
+
];
|
|
282
|
+
for (let w = 0; w < 4; w++) {
|
|
283
|
+
const bw = bWords[w];
|
|
284
|
+
for (let bit = 0; bit < 32; bit++) {
|
|
285
|
+
const mask = -(bw >>> bit & 1);
|
|
286
|
+
r0 = (r0 ^ c0 & mask) >>> 0;
|
|
287
|
+
r1 = (r1 ^ c1 & mask) >>> 0;
|
|
288
|
+
r2 = (r2 ^ c2 & mask) >>> 0;
|
|
289
|
+
r3 = (r3 ^ c3 & mask) >>> 0;
|
|
290
|
+
const msbMask = -(c3 >>> 31);
|
|
291
|
+
c3 = (c3 << 1 | c2 >>> 31) >>> 0;
|
|
292
|
+
c2 = (c2 << 1 | c1 >>> 31) >>> 0;
|
|
293
|
+
c1 = (c1 << 1 | c0 >>> 31) >>> 0;
|
|
294
|
+
c0 = (c0 << 1 ^ msbMask & 135) >>> 0;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
acc[0] = r0;
|
|
298
|
+
acc[1] = r1;
|
|
299
|
+
acc[2] = r2;
|
|
300
|
+
acc[3] = r3;
|
|
301
|
+
}
|
|
302
|
+
function gf128Mul(a, b) {
|
|
303
|
+
const aBytes = u128ToBytes16LE(a);
|
|
304
|
+
const bBytes = u128ToBytes16LE(b);
|
|
305
|
+
const acc = new Uint32Array(4);
|
|
306
|
+
gf128MulAccumulateWords(acc, readU32LE(aBytes, 0), readU32LE(aBytes, 4), readU32LE(aBytes, 8), readU32LE(aBytes, 12), readU32LE(bBytes, 0), readU32LE(bBytes, 4), readU32LE(bBytes, 8), readU32LE(bBytes, 12));
|
|
307
|
+
const outBytes = new Uint8Array(16);
|
|
308
|
+
writeU32LE(outBytes, 0, acc[0]);
|
|
309
|
+
writeU32LE(outBytes, 4, acc[1]);
|
|
310
|
+
writeU32LE(outBytes, 8, acc[2]);
|
|
311
|
+
writeU32LE(outBytes, 12, acc[3]);
|
|
312
|
+
return bytes16ToU128LE(outBytes, 0);
|
|
313
|
+
}
|
|
314
|
+
function squeezeKos15ChiRawBytes(ctxOtExt, mtaNonce, xiSeed32, culpritSlot) {
|
|
315
|
+
if (xiSeed32.length !== 32) throw new MpcProtocolError("INVALID_PARAMETER", "KOS15 coin-toss seed xiA must be 32 bytes", culpritSlot);
|
|
316
|
+
const xof = shake256.create({});
|
|
317
|
+
xof.update(lengthPrefixedEncode(DST_KOS15_CHI_V1, [
|
|
318
|
+
ctxOtExt,
|
|
319
|
+
mtaNonce,
|
|
320
|
+
xiSeed32
|
|
321
|
+
]));
|
|
322
|
+
const raw = new Uint8Array(OT_EXTENSION_ROWS * 16);
|
|
323
|
+
try {
|
|
324
|
+
xof.xofInto(raw);
|
|
325
|
+
return raw;
|
|
326
|
+
} finally {
|
|
327
|
+
xof.destroy();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function expandKos15ChiWeights(ctxOtExt, mtaNonce, xiSeed32, culpritSlot) {
|
|
331
|
+
const raw = squeezeKos15ChiRawBytes(ctxOtExt, mtaNonce, xiSeed32, culpritSlot);
|
|
332
|
+
try {
|
|
333
|
+
const weights = new Array(OT_EXTENSION_ROWS);
|
|
334
|
+
for (let l = 0; l < OT_EXTENSION_ROWS; l++) weights[l] = bytes16ToU128LE(raw, l * 16);
|
|
335
|
+
return weights;
|
|
336
|
+
} finally {
|
|
337
|
+
zeroizeBytes(raw);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function wipeVerifiedCotReceiverRows(rows) {
|
|
341
|
+
zeroizeBytes(rows.gadgetChoiceBits768);
|
|
342
|
+
for (const row of rows.tRows768) zeroizeBytes(row);
|
|
343
|
+
}
|
|
344
|
+
function wipeVerifiedCotSenderRows(rows) {
|
|
345
|
+
zeroizeBytes(rows.deltaBytes16);
|
|
346
|
+
for (const row of rows.qRows768) zeroizeBytes(row);
|
|
347
|
+
}
|
|
348
|
+
function registerSessionNonceOnce(usedSet, ctxOtExt, mtaNonce, culpritSlot) {
|
|
349
|
+
if (mtaNonce.length < 12 || mtaNonce.length > 64) throw new MpcProtocolError("INVALID_PARAMETER", "mtaNonce must be between 12 and 64 bytes", culpritSlot);
|
|
350
|
+
const key = `${bytesToHex(ctxOtExt)}:${bytesToHex(mtaNonce)}`;
|
|
351
|
+
if (usedSet.has(key)) throw new MpcProtocolError("OT_NONCE_REUSE", "OT-extension session nonce reuse is forbidden", culpritSlot);
|
|
352
|
+
usedSet.add(key);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Step 1 (Extended Receiver `B`, holding `BaseOtSenderState`):
|
|
356
|
+
* Appends `256` fresh CSPRNG sacrifice bits to the `768` gadget choice bits, expands
|
|
357
|
+
* `(t^{(0)}_m, t^{(1)}_m)`, emits `u^{col}_m = t^{(0)}_m ^ t^{(1)}_m ^ b`, and transposes `T_0..T_{1023}`.
|
|
358
|
+
*/
|
|
359
|
+
function otExtendReceiverStep1(params) {
|
|
360
|
+
const { baseOtSender, ctxOtExt, mtaNonce, gadgetChoiceBits768, rng = defaultRandomBytes } = params;
|
|
361
|
+
if (baseOtSender.invalidated) throw new MpcProtocolError("OT_STATE_INVALIDATED", "BaseOtSenderState has been invalidated");
|
|
362
|
+
if (gadgetChoiceBits768.length !== OT_GADGET_BITS) throw new MpcProtocolError("INVALID_PARAMETER", `Expected ${OT_GADGET_BITS} gadget choice bits, got ${gadgetChoiceBits768.length}`);
|
|
363
|
+
registerSessionNonceOnce(baseOtSender.usedSessionKeys, ctxOtExt, mtaNonce);
|
|
364
|
+
const sacrificePacked32 = rng(OT_SACRIFICE_BITS >>> 3);
|
|
365
|
+
const sacrificeBits256 = unpackBitsLSB(sacrificePacked32, OT_SACRIFICE_BITS);
|
|
366
|
+
zeroizeBytes(sacrificePacked32);
|
|
367
|
+
const allChoiceBits1024 = new Uint8Array(OT_EXTENSION_ROWS);
|
|
368
|
+
allChoiceBits1024.set(gadgetChoiceBits768, 0);
|
|
369
|
+
allChoiceBits1024.set(sacrificeBits256, OT_GADGET_BITS);
|
|
370
|
+
zeroizeBytes(sacrificeBits256);
|
|
371
|
+
const packedChoice128 = packBitsLSB(allChoiceBits1024);
|
|
372
|
+
const t0Columns = new Array(OT_KAPPA_BITS);
|
|
373
|
+
const uColumns = new Array(OT_KAPPA_BITS);
|
|
374
|
+
try {
|
|
375
|
+
for (let m = 0; m < OT_KAPPA_BITS; m++) {
|
|
376
|
+
const [seed0, seed1] = baseOtSender.seedPairs[m];
|
|
377
|
+
const t0 = expandColumnPrgAes256Ctr(seed0, ctxOtExt, mtaNonce, m);
|
|
378
|
+
const t1 = expandColumnPrgAes256Ctr(seed1, ctxOtExt, mtaNonce, m);
|
|
379
|
+
const uCol = new Uint8Array(OT_COLUMN_BYTES);
|
|
380
|
+
for (let byteIdx = 0; byteIdx < OT_COLUMN_BYTES; byteIdx++) uCol[byteIdx] = t0[byteIdx] ^ t1[byteIdx] ^ packedChoice128[byteIdx];
|
|
381
|
+
zeroizeBytes(t1);
|
|
382
|
+
t0Columns[m] = t0;
|
|
383
|
+
uColumns[m] = uCol;
|
|
384
|
+
}
|
|
385
|
+
const tRows1024 = eklundhTranspose128x1024(t0Columns);
|
|
386
|
+
return {
|
|
387
|
+
pendingReceiver: {
|
|
388
|
+
ctxOtExt: new Uint8Array(ctxOtExt),
|
|
389
|
+
mtaNonce: new Uint8Array(mtaNonce),
|
|
390
|
+
allChoiceBits1024,
|
|
391
|
+
tRows1024,
|
|
392
|
+
completed: false
|
|
393
|
+
},
|
|
394
|
+
step1Message: {
|
|
395
|
+
mtaNonce: new Uint8Array(mtaNonce),
|
|
396
|
+
uColumns
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
} finally {
|
|
400
|
+
zeroizeBytes(packedChoice128);
|
|
401
|
+
for (const col of t0Columns) zeroizeBytes(col);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Step 2 (Extended Sender `A`, holding `BaseOtReceiverState`):
|
|
406
|
+
* Receives `B`'s committed columns `uColumns`, expands `z^{col}_m` from `k_{m, Delta_m}`,
|
|
407
|
+
* computes `q^{col}_m = z^{col}_m ^ (Delta_m * u^{col}_m)`, transposes `Q_0..Q_{1023}`, and
|
|
408
|
+
* samples coin-toss challenge `xiSeed32` WITHOUT touching `k_i` or deriving any row pads.
|
|
409
|
+
*/
|
|
410
|
+
function otExtendSenderStep2(params) {
|
|
411
|
+
const { baseOtReceiver, ctxOtExt, step1Message, rng = defaultRandomBytes } = params;
|
|
412
|
+
if (baseOtReceiver.invalidated) throw new MpcProtocolError("OT_STATE_INVALIDATED", "BaseOtReceiverState has been invalidated");
|
|
413
|
+
const { mtaNonce, uColumns } = step1Message;
|
|
414
|
+
if (uColumns.length !== OT_KAPPA_BITS) throw new MpcProtocolError("INVALID_PARAMETER", `Expected ${OT_KAPPA_BITS} OT columns`, baseOtReceiver.peerSlotB);
|
|
415
|
+
for (let m = 0; m < OT_KAPPA_BITS; m++) if (uColumns[m].length !== OT_COLUMN_BYTES) throw new MpcProtocolError("INVALID_PARAMETER", `OT column ${m} length mismatch`, baseOtReceiver.peerSlotB);
|
|
416
|
+
registerSessionNonceOnce(baseOtReceiver.usedSessionKeys, ctxOtExt, mtaNonce, baseOtReceiver.peerSlotB);
|
|
417
|
+
const qColumns = new Array(OT_KAPPA_BITS);
|
|
418
|
+
try {
|
|
419
|
+
for (let m = 0; m < OT_KAPPA_BITS; m++) {
|
|
420
|
+
const zCol = expandColumnPrgAes256Ctr(baseOtReceiver.chosenSeeds[m], ctxOtExt, mtaNonce, m);
|
|
421
|
+
const maskByte = baseOtReceiver.deltaBits128[m] === 1 ? 255 : 0;
|
|
422
|
+
const uCol = uColumns[m];
|
|
423
|
+
const qCol = new Uint8Array(OT_COLUMN_BYTES);
|
|
424
|
+
for (let byteIdx = 0; byteIdx < OT_COLUMN_BYTES; byteIdx++) qCol[byteIdx] = zCol[byteIdx] ^ uCol[byteIdx] & maskByte;
|
|
425
|
+
zeroizeBytes(zCol);
|
|
426
|
+
qColumns[m] = qCol;
|
|
427
|
+
}
|
|
428
|
+
const qRows1024 = eklundhTranspose128x1024(qColumns);
|
|
429
|
+
const xiSeed32 = rng(32);
|
|
430
|
+
if (xiSeed32.length !== 32) {
|
|
431
|
+
zeroizeBytes(xiSeed32);
|
|
432
|
+
throw new MpcProtocolError("INVALID_PARAMETER", "RNG must produce 32-byte xiSeed32");
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
pendingSender: {
|
|
436
|
+
baseOtReceiver,
|
|
437
|
+
ctxOtExt: new Uint8Array(ctxOtExt),
|
|
438
|
+
mtaNonce: new Uint8Array(mtaNonce),
|
|
439
|
+
xiSeed32,
|
|
440
|
+
qRows1024,
|
|
441
|
+
completed: false
|
|
442
|
+
},
|
|
443
|
+
step2Message: {
|
|
444
|
+
mtaNonce: new Uint8Array(mtaNonce),
|
|
445
|
+
xiSeed32: new Uint8Array(xiSeed32)
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
} finally {
|
|
449
|
+
for (const col of qColumns) zeroizeBytes(col);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Step 3 (Extended Receiver `B`):
|
|
454
|
+
* Computes `KOS15` correlation response `(x, T_chk)` over all `1024` rows (masked by the
|
|
455
|
+
* `256` sacrifice bits), wipes sacrifice rows `768..1023`, and returns the `768` gadget rows.
|
|
456
|
+
*/
|
|
457
|
+
function otExtendReceiverStep3(params) {
|
|
458
|
+
const { pendingReceiver, step2Message, peerSenderSlotA } = params;
|
|
459
|
+
if (pendingReceiver.completed) throw new MpcProtocolError("OT_STATE_INVALIDATED", "OT receiver session already finalized");
|
|
460
|
+
pendingReceiver.completed = true;
|
|
461
|
+
if (!constantTimeEqual(pendingReceiver.mtaNonce, step2Message.mtaNonce)) {
|
|
462
|
+
for (const row of pendingReceiver.tRows1024) zeroizeBytes(row);
|
|
463
|
+
zeroizeBytes(pendingReceiver.allChoiceBits1024);
|
|
464
|
+
throw new MpcProtocolError("INVALID_PARAMETER", "OT Step 2 mtaNonce mismatch", peerSenderSlotA);
|
|
465
|
+
}
|
|
466
|
+
let chiRaw;
|
|
467
|
+
try {
|
|
468
|
+
chiRaw = squeezeKos15ChiRawBytes(pendingReceiver.ctxOtExt, pendingReceiver.mtaNonce, step2Message.xiSeed32, peerSenderSlotA);
|
|
469
|
+
} catch (err) {
|
|
470
|
+
for (const row of pendingReceiver.tRows1024) zeroizeBytes(row);
|
|
471
|
+
zeroizeBytes(pendingReceiver.allChoiceBits1024);
|
|
472
|
+
throw err;
|
|
473
|
+
}
|
|
474
|
+
let x0 = 0;
|
|
475
|
+
let x1 = 0;
|
|
476
|
+
let x2 = 0;
|
|
477
|
+
let x3 = 0;
|
|
478
|
+
const tChkWords = new Uint32Array(4);
|
|
479
|
+
try {
|
|
480
|
+
for (let l = 0; l < OT_EXTENSION_ROWS; l++) {
|
|
481
|
+
const chiOff = l * 16;
|
|
482
|
+
const c0 = readU32LE(chiRaw, chiOff);
|
|
483
|
+
const c1 = readU32LE(chiRaw, chiOff + 4);
|
|
484
|
+
const c2 = readU32LE(chiRaw, chiOff + 8);
|
|
485
|
+
const c3 = readU32LE(chiRaw, chiOff + 12);
|
|
486
|
+
const bitMask = -pendingReceiver.allChoiceBits1024[l];
|
|
487
|
+
x0 = (x0 ^ c0 & bitMask) >>> 0;
|
|
488
|
+
x1 = (x1 ^ c1 & bitMask) >>> 0;
|
|
489
|
+
x2 = (x2 ^ c2 & bitMask) >>> 0;
|
|
490
|
+
x3 = (x3 ^ c3 & bitMask) >>> 0;
|
|
491
|
+
const tRow = pendingReceiver.tRows1024[l];
|
|
492
|
+
gf128MulAccumulateWords(tChkWords, c0, c1, c2, c3, readU32LE(tRow, 0), readU32LE(tRow, 4), readU32LE(tRow, 8), readU32LE(tRow, 12));
|
|
493
|
+
}
|
|
494
|
+
} finally {
|
|
495
|
+
zeroizeBytes(chiRaw);
|
|
496
|
+
}
|
|
497
|
+
const xBytes16 = new Uint8Array(16);
|
|
498
|
+
writeU32LE(xBytes16, 0, x0);
|
|
499
|
+
writeU32LE(xBytes16, 4, x1);
|
|
500
|
+
writeU32LE(xBytes16, 8, x2);
|
|
501
|
+
writeU32LE(xBytes16, 12, x3);
|
|
502
|
+
const tChkBytes16 = new Uint8Array(16);
|
|
503
|
+
writeU32LE(tChkBytes16, 0, tChkWords[0]);
|
|
504
|
+
writeU32LE(tChkBytes16, 4, tChkWords[1]);
|
|
505
|
+
writeU32LE(tChkBytes16, 8, tChkWords[2]);
|
|
506
|
+
writeU32LE(tChkBytes16, 12, tChkWords[3]);
|
|
507
|
+
const gadgetChoiceBits768 = pendingReceiver.allChoiceBits1024.slice(0, OT_GADGET_BITS);
|
|
508
|
+
const tRows768 = pendingReceiver.tRows1024.slice(0, OT_GADGET_BITS);
|
|
509
|
+
for (let l = OT_GADGET_BITS; l < OT_EXTENSION_ROWS; l++) zeroizeBytes(pendingReceiver.tRows1024[l]);
|
|
510
|
+
zeroizeBytes(pendingReceiver.allChoiceBits1024);
|
|
511
|
+
return {
|
|
512
|
+
receiverRows: {
|
|
513
|
+
ctxOtExt: pendingReceiver.ctxOtExt,
|
|
514
|
+
mtaNonce: pendingReceiver.mtaNonce,
|
|
515
|
+
gadgetChoiceBits768,
|
|
516
|
+
tRows768
|
|
517
|
+
},
|
|
518
|
+
step3Message: {
|
|
519
|
+
mtaNonce: new Uint8Array(pendingReceiver.mtaNonce),
|
|
520
|
+
xBytes16,
|
|
521
|
+
tChkBytes16
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Step 4 (Extended Sender `A`):
|
|
527
|
+
* Strictly verifies `KOS15` `GF(2^128)` correlation check `Q_chk == T_chk ^ (x * Delta)`
|
|
528
|
+
* **before** any pad `(r_{l,b}, r_hat_{l,b})` or MtA ciphertext `(u_l, u_hat_l, r_star_l)` can
|
|
529
|
+
* be computed. On mismatch, immediately zeroizes all `Q_l` AND permanently wipes/drops `Delta`
|
|
530
|
+
* (`BaseOtReceiverState`)!
|
|
531
|
+
*/
|
|
532
|
+
function otExtendSenderStep4VerifyBeforePads(params) {
|
|
533
|
+
const { pendingSender, step3Message } = params;
|
|
534
|
+
if (!pendingSender || pendingSender.completed) throw new MpcProtocolError("OT_STATE_INVALIDATED", "OT sender session already finalized");
|
|
535
|
+
pendingSender.completed = true;
|
|
536
|
+
const wipeAllSenderSecrets = () => {
|
|
537
|
+
for (const row of pendingSender.qRows1024) zeroizeBytes(row);
|
|
538
|
+
zeroizeBytes(pendingSender.xiSeed32);
|
|
539
|
+
wipeBaseOtReceiverState(pendingSender.baseOtReceiver);
|
|
540
|
+
};
|
|
541
|
+
try {
|
|
542
|
+
if (pendingSender.baseOtReceiver.invalidated) throw new MpcProtocolError("KOS15_CORRELATION_CHECK_FAILED", "BaseOtReceiverState invalidated", pendingSender.baseOtReceiver.peerSlotB);
|
|
543
|
+
if (!step3Message || !(step3Message.mtaNonce instanceof Uint8Array) || !(step3Message.xBytes16 instanceof Uint8Array) || !(step3Message.tChkBytes16 instanceof Uint8Array) || !constantTimeEqual(pendingSender.mtaNonce, step3Message.mtaNonce) || step3Message.xBytes16.length !== 16 || step3Message.tChkBytes16.length !== 16) throw new MpcProtocolError("INVALID_PARAMETER", "Invalid KOS15 Step 3 message format", pendingSender.baseOtReceiver.peerSlotB);
|
|
544
|
+
const chiRaw = squeezeKos15ChiRawBytes(pendingSender.ctxOtExt, pendingSender.mtaNonce, pendingSender.xiSeed32, pendingSender.baseOtReceiver.peerSlotB);
|
|
545
|
+
zeroizeBytes(pendingSender.xiSeed32);
|
|
546
|
+
const qChkWords = new Uint32Array(4);
|
|
547
|
+
try {
|
|
548
|
+
for (let l = 0; l < OT_EXTENSION_ROWS; l++) {
|
|
549
|
+
const chiOff = l * 16;
|
|
550
|
+
const qRow = pendingSender.qRows1024[l];
|
|
551
|
+
gf128MulAccumulateWords(qChkWords, readU32LE(chiRaw, chiOff), readU32LE(chiRaw, chiOff + 4), readU32LE(chiRaw, chiOff + 8), readU32LE(chiRaw, chiOff + 12), readU32LE(qRow, 0), readU32LE(qRow, 4), readU32LE(qRow, 8), readU32LE(qRow, 12));
|
|
552
|
+
}
|
|
553
|
+
} finally {
|
|
554
|
+
zeroizeBytes(chiRaw);
|
|
555
|
+
}
|
|
556
|
+
const expectedWords = new Uint32Array([
|
|
557
|
+
readU32LE(step3Message.tChkBytes16, 0),
|
|
558
|
+
readU32LE(step3Message.tChkBytes16, 4),
|
|
559
|
+
readU32LE(step3Message.tChkBytes16, 8),
|
|
560
|
+
readU32LE(step3Message.tChkBytes16, 12)
|
|
561
|
+
]);
|
|
562
|
+
const deltaBytes = pendingSender.baseOtReceiver.deltaBytes16;
|
|
563
|
+
gf128MulAccumulateWords(expectedWords, readU32LE(step3Message.xBytes16, 0), readU32LE(step3Message.xBytes16, 4), readU32LE(step3Message.xBytes16, 8), readU32LE(step3Message.xBytes16, 12), readU32LE(deltaBytes, 0), readU32LE(deltaBytes, 4), readU32LE(deltaBytes, 8), readU32LE(deltaBytes, 12));
|
|
564
|
+
const qChkBytes = new Uint8Array(16);
|
|
565
|
+
writeU32LE(qChkBytes, 0, qChkWords[0]);
|
|
566
|
+
writeU32LE(qChkBytes, 4, qChkWords[1]);
|
|
567
|
+
writeU32LE(qChkBytes, 8, qChkWords[2]);
|
|
568
|
+
writeU32LE(qChkBytes, 12, qChkWords[3]);
|
|
569
|
+
const expectedBytes = new Uint8Array(16);
|
|
570
|
+
writeU32LE(expectedBytes, 0, expectedWords[0]);
|
|
571
|
+
writeU32LE(expectedBytes, 4, expectedWords[1]);
|
|
572
|
+
writeU32LE(expectedBytes, 8, expectedWords[2]);
|
|
573
|
+
writeU32LE(expectedBytes, 12, expectedWords[3]);
|
|
574
|
+
const checkMatched = constantTimeEqual(qChkBytes, expectedBytes);
|
|
575
|
+
zeroizeBytes(qChkBytes, expectedBytes);
|
|
576
|
+
if (!checkMatched) throw new MpcProtocolError("KOS15_CORRELATION_CHECK_FAILED", `KOS15 GF(2^128) correlation check failed against peer slot ${pendingSender.baseOtReceiver.peerSlotB}; Delta wiped before pad computation`, pendingSender.baseOtReceiver.peerSlotB);
|
|
577
|
+
const qRows768 = pendingSender.qRows1024.slice(0, OT_GADGET_BITS);
|
|
578
|
+
for (let l = OT_GADGET_BITS; l < OT_EXTENSION_ROWS; l++) zeroizeBytes(pendingSender.qRows1024[l]);
|
|
579
|
+
return {
|
|
580
|
+
ctxOtExt: pendingSender.ctxOtExt,
|
|
581
|
+
mtaNonce: pendingSender.mtaNonce,
|
|
582
|
+
deltaBytes16: new Uint8Array(pendingSender.baseOtReceiver.deltaBytes16),
|
|
583
|
+
qRows768
|
|
584
|
+
};
|
|
585
|
+
} catch (err) {
|
|
586
|
+
wipeAllSenderSecrets();
|
|
587
|
+
if (err instanceof MpcProtocolError) throw err;
|
|
588
|
+
throw new MpcProtocolError("KOS15_CORRELATION_CHECK_FAILED", `Malformed KOS15 Step 3 response from peer slot ${pendingSender.baseOtReceiver.peerSlotB}: ${err.message}`, pendingSender.baseOtReceiver.peerSlotB);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
//#endregion
|
|
593
|
+
export { baseOtReceiverStep2, baseOtSenderStep1, baseOtSenderStep3, bytes16ToU128LE, eklundhTranspose128x1024, expandKos15ChiWeights, gf128Mul, otExtendReceiverStep1, otExtendReceiverStep3, otExtendSenderStep2, otExtendSenderStep4VerifyBeforePads, u128ToBytes16LE, wipeBaseOtReceiverState, wipeBaseOtSenderState, wipeVerifiedCotReceiverRows, wipeVerifiedCotSenderRows };
|