@ocap/mpc-hsm 1.33.6 → 1.33.8

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 (56) 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 +66 -2
  4. package/esm/constants.mjs +65 -1
  5. package/esm/dkg.mjs +2 -2
  6. package/esm/index.d.mts +11 -3
  7. package/esm/index.mjs +11 -3
  8. package/esm/ocap-clear-signing.d.mts +125 -0
  9. package/esm/ocap-clear-signing.mjs +1170 -0
  10. package/esm/offline-ceremony.d.mts +127 -0
  11. package/esm/offline-ceremony.mjs +481 -0
  12. package/esm/ot.d.mts +7 -1
  13. package/esm/ot.mjs +149 -16
  14. package/esm/personal-vault.d.mts +1292 -0
  15. package/esm/personal-vault.mjs +3615 -0
  16. package/esm/personal.d.mts +10 -0
  17. package/esm/personal.mjs +11 -0
  18. package/esm/presign.mjs +1 -1
  19. package/esm/rlp-eip1559.d.mts +75 -0
  20. package/esm/rlp-eip1559.mjs +293 -0
  21. package/esm/secp256k1-scalar.d.mts +1 -1
  22. package/esm/secp256k1-scalar.mjs +14 -5
  23. package/esm/steel-mnemonic.d.mts +55 -0
  24. package/esm/steel-mnemonic.mjs +247 -0
  25. package/esm/ves-backup.d.mts +296 -0
  26. package/esm/ves-backup.mjs +1025 -0
  27. package/esm/x402-agent-guard.d.mts +111 -0
  28. package/esm/x402-agent-guard.mjs +780 -0
  29. package/lib/bip39-english-wordlist.cjs +33 -0
  30. package/lib/bip39-english-wordlist.d.cts +11 -0
  31. package/lib/constants.cjs +128 -0
  32. package/lib/constants.d.cts +66 -2
  33. package/lib/dkg.cjs +2 -2
  34. package/lib/index.cjs +249 -0
  35. package/lib/index.d.cts +11 -3
  36. package/lib/ocap-clear-signing.cjs +1176 -0
  37. package/lib/ocap-clear-signing.d.cts +125 -0
  38. package/lib/offline-ceremony.cjs +487 -0
  39. package/lib/offline-ceremony.d.cts +127 -0
  40. package/lib/ot.cjs +150 -15
  41. package/lib/ot.d.cts +7 -1
  42. package/lib/personal-vault.cjs +3725 -0
  43. package/lib/personal-vault.d.cts +1292 -0
  44. package/lib/personal.cjs +338 -0
  45. package/lib/personal.d.cts +10 -0
  46. package/lib/presign.cjs +1 -1
  47. package/lib/rlp-eip1559.cjs +303 -0
  48. package/lib/rlp-eip1559.d.cts +75 -0
  49. package/lib/secp256k1-scalar.cjs +14 -5
  50. package/lib/steel-mnemonic.cjs +253 -0
  51. package/lib/steel-mnemonic.d.cts +55 -0
  52. package/lib/ves-backup.cjs +1053 -0
  53. package/lib/ves-backup.d.cts +296 -0
  54. package/lib/x402-agent-guard.cjs +788 -0
  55. package/lib/x402-agent-guard.d.cts +111 -0
  56. package/package.json +6 -1
package/esm/ot.mjs CHANGED
@@ -1,6 +1,5 @@
1
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
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
3
  import { sha256 } from "@noble/hashes/sha2.js";
5
4
  import { shake256 } from "@noble/hashes/sha3.js";
6
5
 
@@ -175,24 +174,158 @@ function baseOtSenderStep3(params) {
175
174
  zeroizeBytes(pending.yScalarBytes32);
176
175
  }
177
176
  }
178
- const ZERO_COLUMN_128 = new Uint8Array(OT_COLUMN_BYTES);
177
+ function gf256Xtime(x) {
178
+ return (x << 1 ^ (x >>> 7 & 1) * 27) & 255;
179
+ }
180
+ function gf256Mul(a, b) {
181
+ let p = 0;
182
+ let curA = a & 255;
183
+ let curB = b & 255;
184
+ for (let i = 0; i < 8; i++) {
185
+ const mask = -(curB & 1) & 255;
186
+ p ^= curA & mask;
187
+ curA = gf256Xtime(curA);
188
+ curB >>>= 1;
189
+ }
190
+ return p & 255;
191
+ }
192
+ function buildAesSbox256() {
193
+ const sbox = new Uint8Array(256);
194
+ for (let x = 0; x < 256; x++) {
195
+ let inv = x === 0 ? 0 : 1;
196
+ if (x !== 0) {
197
+ let base = x;
198
+ let exp = 254;
199
+ while (exp > 0) {
200
+ if ((exp & 1) === 1) inv = gf256Mul(inv, base);
201
+ base = gf256Mul(base, base);
202
+ exp >>>= 1;
203
+ }
204
+ }
205
+ const rot1 = (inv << 1 | inv >>> 7) & 255;
206
+ const rot2 = (inv << 2 | inv >>> 6) & 255;
207
+ const rot3 = (inv << 3 | inv >>> 5) & 255;
208
+ const rot4 = (inv << 4 | inv >>> 4) & 255;
209
+ sbox[x] = (inv ^ rot1 ^ rot2 ^ rot3 ^ rot4 ^ 99) & 255;
210
+ }
211
+ return sbox;
212
+ }
213
+ const AES_SBOX = buildAesSbox256();
214
+ const AES_RCON = [
215
+ 16777216,
216
+ 33554432,
217
+ 67108864,
218
+ 134217728,
219
+ 268435456,
220
+ 536870912,
221
+ 1073741824
222
+ ];
223
+ function aesSubWord(word) {
224
+ return (AES_SBOX[word >>> 24 & 255] << 24 | AES_SBOX[word >>> 16 & 255] << 16 | AES_SBOX[word >>> 8 & 255] << 8 | AES_SBOX[word & 255]) >>> 0;
225
+ }
226
+ function aesRotWord(word) {
227
+ return (word << 8 | word >>> 24) >>> 0;
228
+ }
229
+ function expandAes256RoundKeys(key32, outWords60) {
230
+ if (key32.length !== 32) throw new MpcProtocolError("INVALID_PARAMETER", `AES-256 key must be 32 bytes, got ${key32.length}`);
231
+ for (let i = 0; i < 8; i++) {
232
+ const o = i * 4;
233
+ outWords60[i] = (key32[o] << 24 | key32[o + 1] << 16 | key32[o + 2] << 8 | key32[o + 3]) >>> 0;
234
+ }
235
+ for (let i = 8; i < 60; i++) {
236
+ let temp = outWords60[i - 1];
237
+ if ((i & 7) === 0) temp = (aesSubWord(aesRotWord(temp)) ^ AES_RCON[(i >>> 3) - 1]) >>> 0;
238
+ else if ((i & 7) === 4) temp = aesSubWord(temp);
239
+ outWords60[i] = (outWords60[i - 8] ^ temp) >>> 0;
240
+ }
241
+ }
242
+ function addRoundKey16(state16, roundWords, wordOffset) {
243
+ for (let c = 0; c < 4; c++) {
244
+ const w = roundWords[wordOffset + c];
245
+ const o = c * 4;
246
+ state16[o] = (state16[o] ^ w >>> 24 & 255) & 255;
247
+ state16[o + 1] = (state16[o + 1] ^ w >>> 16 & 255) & 255;
248
+ state16[o + 2] = (state16[o + 2] ^ w >>> 8 & 255) & 255;
249
+ state16[o + 3] = (state16[o + 3] ^ w & 255) & 255;
250
+ }
251
+ }
252
+ function encryptAes256WithRoundKeysInto(roundWords60, inputBlock16, outBlock16) {
253
+ outBlock16.set(inputBlock16);
254
+ addRoundKey16(outBlock16, roundWords60, 0);
255
+ for (let round = 1; round <= 14; round++) {
256
+ for (let i = 0; i < 16; i++) outBlock16[i] = AES_SBOX[outBlock16[i]];
257
+ const t1 = outBlock16[1];
258
+ outBlock16[1] = outBlock16[5];
259
+ outBlock16[5] = outBlock16[9];
260
+ outBlock16[9] = outBlock16[13];
261
+ outBlock16[13] = t1;
262
+ const t2a = outBlock16[2];
263
+ const t2b = outBlock16[6];
264
+ outBlock16[2] = outBlock16[10];
265
+ outBlock16[6] = outBlock16[14];
266
+ outBlock16[10] = t2a;
267
+ outBlock16[14] = t2b;
268
+ const t3 = outBlock16[15];
269
+ outBlock16[15] = outBlock16[11];
270
+ outBlock16[11] = outBlock16[7];
271
+ outBlock16[7] = outBlock16[3];
272
+ outBlock16[3] = t3;
273
+ if (round < 14) for (let c = 0; c < 4; c++) {
274
+ const o = c * 4;
275
+ const a0 = outBlock16[o];
276
+ const a1 = outBlock16[o + 1];
277
+ const a2 = outBlock16[o + 2];
278
+ const a3 = outBlock16[o + 3];
279
+ const xorAll = a0 ^ a1 ^ a2 ^ a3;
280
+ outBlock16[o] = (a0 ^ xorAll ^ gf256Xtime(a0 ^ a1)) & 255;
281
+ outBlock16[o + 1] = (a1 ^ xorAll ^ gf256Xtime(a1 ^ a2)) & 255;
282
+ outBlock16[o + 2] = (a2 ^ xorAll ^ gf256Xtime(a2 ^ a3)) & 255;
283
+ outBlock16[o + 3] = (a3 ^ xorAll ^ gf256Xtime(a3 ^ a0)) & 255;
284
+ }
285
+ addRoundKey16(outBlock16, roundWords60, round * 4);
286
+ }
287
+ }
288
+ /**
289
+ * Pure synchronous FIPS-197 AES-256 single-block encryption (`16B` plaintext -> `16B` ciphertext).
290
+ * Zeroizes expanded round keys immediately in `finally`.
291
+ */
292
+ function aes256EncryptBlock(key32, block16) {
293
+ if (!(key32 instanceof Uint8Array) || key32.length !== 32) throw new MpcProtocolError("INVALID_PARAMETER", "AES-256 key32 must be 32 bytes");
294
+ if (!(block16 instanceof Uint8Array) || block16.length !== 16) throw new MpcProtocolError("INVALID_PARAMETER", "AES-256 block16 must be 16 bytes");
295
+ const roundWords = new Uint32Array(60);
296
+ try {
297
+ expandAes256RoundKeys(key32, roundWords);
298
+ const out = new Uint8Array(16);
299
+ encryptAes256WithRoundKeysInto(roundWords, block16, out);
300
+ return out;
301
+ } finally {
302
+ roundWords.fill(0);
303
+ }
304
+ }
179
305
  function expandColumnPrgAes256Ctr(seed32, ctxOtExt, mtaNonce, columnIndex) {
180
- const iv16 = sha256(lengthPrefixedEncode(DST_OT_CTR_IV_V1, [
306
+ const ivDigest = sha256(lengthPrefixedEncode(DST_OT_CTR_IV_V1, [
181
307
  ctxOtExt,
182
308
  mtaNonce,
183
309
  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;
310
+ ]));
311
+ const ctrBlock16 = new Uint8Array(16);
312
+ ctrBlock16.set(ivDigest.subarray(0, 12), 0);
313
+ const roundWords = new Uint32Array(60);
314
+ try {
315
+ expandAes256RoundKeys(seed32, roundWords);
316
+ const out = new Uint8Array(OT_COLUMN_BYTES);
317
+ for (let blk = 0; blk < 8; blk++) {
318
+ ctrBlock16[12] = 0;
319
+ ctrBlock16[13] = 0;
320
+ ctrBlock16[14] = 0;
321
+ ctrBlock16[15] = blk;
322
+ encryptAes256WithRoundKeysInto(roundWords, ctrBlock16, out.subarray(blk * 16, (blk + 1) * 16));
323
+ }
324
+ return out;
325
+ } finally {
326
+ roundWords.fill(0);
327
+ zeroizeBytes(ivDigest, ctrBlock16);
328
+ }
196
329
  }
197
330
  const EKLUNDH_MASKS_128 = [
198
331
  113427455640312821154458202477256070485n,
@@ -590,4 +723,4 @@ function otExtendSenderStep4VerifyBeforePads(params) {
590
723
  }
591
724
 
592
725
  //#endregion
593
- export { baseOtReceiverStep2, baseOtSenderStep1, baseOtSenderStep3, bytes16ToU128LE, eklundhTranspose128x1024, expandKos15ChiWeights, gf128Mul, otExtendReceiverStep1, otExtendReceiverStep3, otExtendSenderStep2, otExtendSenderStep4VerifyBeforePads, u128ToBytes16LE, wipeBaseOtReceiverState, wipeBaseOtSenderState, wipeVerifiedCotReceiverRows, wipeVerifiedCotSenderRows };
726
+ export { aes256EncryptBlock, baseOtReceiverStep2, baseOtSenderStep1, baseOtSenderStep3, bytes16ToU128LE, eklundhTranspose128x1024, expandColumnPrgAes256Ctr, expandKos15ChiWeights, gf128Mul, otExtendReceiverStep1, otExtendReceiverStep3, otExtendSenderStep2, otExtendSenderStep4VerifyBeforePads, u128ToBytes16LE, wipeBaseOtReceiverState, wipeBaseOtSenderState, wipeVerifiedCotReceiverRows, wipeVerifiedCotSenderRows };