@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.
- package/esm/bip39-english-wordlist.d.mts +11 -0
- package/esm/bip39-english-wordlist.mjs +31 -0
- package/esm/constants.d.mts +140 -0
- package/esm/constants.mjs +142 -0
- package/esm/dkg.d.mts +279 -0
- package/esm/dkg.mjs +903 -0
- package/esm/index.d.mts +17 -0
- package/esm/index.mjs +18 -0
- package/esm/mta.d.mts +63 -0
- package/esm/mta.mjs +275 -0
- package/esm/ocap-clear-signing.d.mts +125 -0
- package/esm/ocap-clear-signing.mjs +1170 -0
- package/esm/offline-ceremony.d.mts +127 -0
- package/esm/offline-ceremony.mjs +481 -0
- package/esm/ot.d.mts +193 -0
- package/esm/ot.mjs +726 -0
- package/esm/personal-vault.d.mts +1292 -0
- package/esm/personal-vault.mjs +3615 -0
- package/esm/personal.d.mts +10 -0
- package/esm/personal.mjs +11 -0
- package/esm/presign.d.mts +242 -0
- package/esm/presign.mjs +951 -0
- package/esm/rlp-eip1559.d.mts +75 -0
- package/esm/rlp-eip1559.mjs +293 -0
- package/esm/secp256k1-scalar.d.mts +141 -0
- package/esm/secp256k1-scalar.mjs +474 -0
- package/esm/sign.d.mts +113 -0
- package/esm/sign.mjs +225 -0
- package/esm/steel-mnemonic.d.mts +55 -0
- package/esm/steel-mnemonic.mjs +247 -0
- package/esm/tweak.d.mts +100 -0
- package/esm/tweak.mjs +168 -0
- package/esm/ves-backup.d.mts +296 -0
- package/esm/ves-backup.mjs +1025 -0
- package/esm/x402-agent-guard.d.mts +111 -0
- package/esm/x402-agent-guard.mjs +780 -0
- package/lib/bip39-english-wordlist.cjs +33 -0
- package/lib/bip39-english-wordlist.d.cts +11 -0
- package/lib/constants.cjs +264 -0
- package/lib/constants.d.cts +140 -0
- package/lib/dkg.cjs +910 -0
- package/lib/dkg.d.cts +279 -0
- package/lib/index.cjs +420 -0
- package/lib/index.d.cts +17 -0
- package/lib/mta.cjs +280 -0
- package/lib/mta.d.cts +63 -0
- package/lib/ocap-clear-signing.cjs +1176 -0
- package/lib/ocap-clear-signing.d.cts +125 -0
- package/lib/offline-ceremony.cjs +487 -0
- package/lib/offline-ceremony.d.cts +127 -0
- package/lib/ot.cjs +744 -0
- package/lib/ot.d.cts +193 -0
- package/lib/personal-vault.cjs +3725 -0
- package/lib/personal-vault.d.cts +1292 -0
- package/lib/personal.cjs +338 -0
- package/lib/personal.d.cts +10 -0
- package/lib/presign.cjs +954 -0
- package/lib/presign.d.cts +242 -0
- package/lib/rlp-eip1559.cjs +303 -0
- package/lib/rlp-eip1559.d.cts +75 -0
- package/lib/secp256k1-scalar.cjs +527 -0
- package/lib/secp256k1-scalar.d.cts +141 -0
- package/lib/sign.cjs +233 -0
- package/lib/sign.d.cts +113 -0
- package/lib/steel-mnemonic.cjs +253 -0
- package/lib/steel-mnemonic.d.cts +55 -0
- package/lib/tweak.cjs +178 -0
- package/lib/tweak.d.cts +100 -0
- package/lib/ves-backup.cjs +1053 -0
- package/lib/ves-backup.d.cts +296 -0
- package/lib/x402-agent-guard.cjs +788 -0
- package/lib/x402-agent-guard.d.cts +111 -0
- package/package.json +80 -4
- package/README.md +0 -3
package/lib/sign.cjs
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_secp256k1_scalar = require('./secp256k1-scalar.cjs');
|
|
3
|
+
const require_tweak = require('./tweak.cjs');
|
|
4
|
+
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
5
|
+
|
|
6
|
+
//#region src/sign.ts
|
|
7
|
+
/**
|
|
8
|
+
* Internal-only symbol for deterministic unit-vector tests (e.g. `delta_rerand = 1n`).
|
|
9
|
+
* NOT exported from the `@ocap/mpc-hsm` package barrel (`src/index.ts`) and non-serializable over JSON/IPC.
|
|
10
|
+
*/
|
|
11
|
+
const VECTOR_TEST_DELTA_RERAND_SYMBOL = Symbol("ocap.mpc-hsm.vector-test-delta-rerand");
|
|
12
|
+
function deriveGrothShoupRerandScalar(params) {
|
|
13
|
+
const { presigId32, subset, rPointComp33, childPublicKeyComp33, msgDigest32, tweakScalar } = params;
|
|
14
|
+
if (presigId32.length !== 32 || msgDigest32.length !== 32) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "presigId32 and msgDigest32 must be 32 bytes");
|
|
15
|
+
if (tweakScalar < 0n || tweakScalar >= require_constants.SECP256K1_N) throw new require_constants.MpcProtocolError("INVALID_SCALAR", "tweakScalar out of field range");
|
|
16
|
+
return require_secp256k1_scalar.H_scalar(require_constants.DST_PRESIG_RERAND_V1, presigId32, require_secp256k1_scalar.sortedSubsetBytes(subset), rPointComp33, childPublicKeyComp33, msgDigest32, require_secp256k1_scalar.scalarToBytes32(tweakScalar));
|
|
17
|
+
}
|
|
18
|
+
function computeRerandomizedNoncePoint(rPointOrComp33, deltaRerand) {
|
|
19
|
+
require_secp256k1_scalar.assertScalarNonZero(deltaRerand, "deltaRerand");
|
|
20
|
+
const rPoint = rPointOrComp33 instanceof Uint8Array ? require_secp256k1_scalar.decodePointCompressed(rPointOrComp33, false) : rPointOrComp33;
|
|
21
|
+
if (rPoint.is0()) throw new require_constants.MpcProtocolError("INVALID_POINT", "Presignature R cannot be infinity");
|
|
22
|
+
const rPrimePoint = require_secp256k1_scalar.mulPoint(rPoint, deltaRerand);
|
|
23
|
+
if (rPrimePoint.is0()) throw new require_constants.MpcProtocolError("PRESIG_RERAND_BURN_RETRY", "Re-randomized R_prime hit infinity; burn presigId and retry with a fresh presignature");
|
|
24
|
+
const { x, y } = rPrimePoint.toAffine();
|
|
25
|
+
if (x >= require_constants.SECP256K1_N || x % require_constants.SECP256K1_N === 0n) throw new require_constants.MpcProtocolError("PRESIG_RERAND_BURN_RETRY", "Re-randomized R_prime_x >= n or == 0; burn presigId and retry with a fresh presignature");
|
|
26
|
+
return {
|
|
27
|
+
deltaRerand,
|
|
28
|
+
rPrimePoint,
|
|
29
|
+
rPrimePointComp33: require_secp256k1_scalar.encodePointCompressed(rPrimePoint),
|
|
30
|
+
rPrime: x,
|
|
31
|
+
yPrimeParity: Number(y & 1n)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function verifyAndDeriveRequestChildKey(params) {
|
|
35
|
+
const { rootPublicKeyComp33, rootId, childDerivation } = params;
|
|
36
|
+
if (!childDerivation || typeof childDerivation !== "object") throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Missing childDerivation in sign request");
|
|
37
|
+
let derived;
|
|
38
|
+
if (childDerivation.tweakMode === "untweaked") derived = require_tweak.deriveUntweakedChild(rootPublicKeyComp33);
|
|
39
|
+
else if (childDerivation.tweakMode === "role-bound") derived = require_tweak.deriveRoleBoundChild({
|
|
40
|
+
rootPublicKey: rootPublicKeyComp33,
|
|
41
|
+
rootId,
|
|
42
|
+
role: childDerivation.role,
|
|
43
|
+
ethChainId: childDerivation.ethChainId,
|
|
44
|
+
tweakLabel: childDerivation.tweakLabel
|
|
45
|
+
});
|
|
46
|
+
else if (childDerivation.tweakMode === "rule-bound") derived = require_tweak.deriveRuleBoundChild({
|
|
47
|
+
rootPublicKey: rootPublicKeyComp33,
|
|
48
|
+
rootId,
|
|
49
|
+
controllerDid: childDerivation.controllerDid,
|
|
50
|
+
ethChainId: childDerivation.ethChainId,
|
|
51
|
+
ruleHash: childDerivation.ruleHash
|
|
52
|
+
});
|
|
53
|
+
else throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Unsupported childDerivation tweakMode");
|
|
54
|
+
if (!(childDerivation.childPublicKeyComp33 instanceof Uint8Array) || !require_secp256k1_scalar.constantTimeEqual(derived.childPublicKeyComp33, childDerivation.childPublicKeyComp33)) throw new require_constants.MpcProtocolError("DEGENERATE_TWEAK", "childDerivation.childPublicKeyComp33 does not match canonically derived P_child");
|
|
55
|
+
return derived;
|
|
56
|
+
}
|
|
57
|
+
function validateRequestAgainstPublicPresig(request, pub) {
|
|
58
|
+
if (request.rootId !== pub.rootId) throw new require_constants.MpcProtocolError("PRESIG_SUBSET_MISMATCH", `Sign request rootId ${request.rootId} != presig rootId ${pub.rootId}`);
|
|
59
|
+
if (!require_secp256k1_scalar.subsetsEqual(request.subset, pub.subset)) throw new require_constants.MpcProtocolError("PRESIG_SUBSET_MISMATCH", `Sign request subset [${request.subset.join(",")}] != presig subset [${pub.subset.join(",")}]`);
|
|
60
|
+
if (!require_secp256k1_scalar.constantTimeEqual(require_secp256k1_scalar.normalizeHexBytes(request.presigId, 32), pub.presigId32)) throw new require_constants.MpcProtocolError("PRESIG_SUBSET_MISMATCH", "presigId mismatch");
|
|
61
|
+
const msgDigest32 = require_secp256k1_scalar.normalizeHexBytes(request.msgDigest32, 32);
|
|
62
|
+
const derivedChild = verifyAndDeriveRequestChildKey({
|
|
63
|
+
rootPublicKeyComp33: pub.rootPublicKeyComp33,
|
|
64
|
+
rootId: pub.rootId,
|
|
65
|
+
childDerivation: request.childDerivation
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
msgDigest32,
|
|
69
|
+
msgScalarModN: require_secp256k1_scalar.modN(require_secp256k1_scalar.bytesToBigIntBE(msgDigest32)),
|
|
70
|
+
derivedChild
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Evaluates a Groth-Shoup partial signature share on an already-consumed `PresignatureShareRecord`
|
|
75
|
+
* and unconditionally zeroizes `(kShareBytes32, chiShareBytes32)` in `finally`.
|
|
76
|
+
*/
|
|
77
|
+
function evaluateConsumedPresigShareWithRerand(params) {
|
|
78
|
+
const { consumedRecord, request } = params;
|
|
79
|
+
const vectorOverride = params[VECTOR_TEST_DELTA_RERAND_SYMBOL];
|
|
80
|
+
try {
|
|
81
|
+
const { msgDigest32, msgScalarModN, derivedChild } = validateRequestAgainstPublicPresig(request, consumedRecord.publicVerification);
|
|
82
|
+
const kI = require_secp256k1_scalar.bytes32ToScalarNonZero(consumedRecord.kShareBytes32);
|
|
83
|
+
const chiI = require_secp256k1_scalar.bytes32ToScalarNonZero(consumedRecord.chiShareBytes32);
|
|
84
|
+
const deltaRerand = vectorOverride ?? deriveGrothShoupRerandScalar({
|
|
85
|
+
presigId32: consumedRecord.publicVerification.presigId32,
|
|
86
|
+
subset: consumedRecord.publicVerification.subset,
|
|
87
|
+
rPointComp33: consumedRecord.publicVerification.rPointComp33,
|
|
88
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
89
|
+
msgDigest32,
|
|
90
|
+
tweakScalar: derivedChild.tweakScalar
|
|
91
|
+
});
|
|
92
|
+
const rerand = computeRerandomizedNoncePoint(consumedRecord.publicVerification.rPointComp33, deltaRerand);
|
|
93
|
+
const inner = require_secp256k1_scalar.addModN(require_secp256k1_scalar.mulModN(kI, require_secp256k1_scalar.addModN(msgScalarModN, require_secp256k1_scalar.mulModN(rerand.rPrime, derivedChild.tweakScalar))), require_secp256k1_scalar.mulModN(rerand.rPrime, chiI));
|
|
94
|
+
const sI = require_secp256k1_scalar.mulModN(require_secp256k1_scalar.invModN(deltaRerand), inner);
|
|
95
|
+
return {
|
|
96
|
+
share: {
|
|
97
|
+
slot: consumedRecord.slot,
|
|
98
|
+
presigId32: new Uint8Array(consumedRecord.publicVerification.presigId32),
|
|
99
|
+
sShareBytes32: require_secp256k1_scalar.scalarToBytes32(sI)
|
|
100
|
+
},
|
|
101
|
+
rerand
|
|
102
|
+
};
|
|
103
|
+
} finally {
|
|
104
|
+
require_secp256k1_scalar.zeroizeBytes(consumedRecord.kShareBytes32, consumedRecord.chiShareBytes32);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Synchronously validates the request preimage (blocking `"escape-canary"` before pool touch),
|
|
109
|
+
* consumes `presigId` from `pool`, validates `request.subset === presig.subset`, computes `s_i`,
|
|
110
|
+
* and wipes `k_i` & `\chi_i` from RAM.
|
|
111
|
+
*/
|
|
112
|
+
function createOnlineSignShare(params) {
|
|
113
|
+
const { pool, request } = params;
|
|
114
|
+
return evaluateConsumedPresigShareWithRerand({
|
|
115
|
+
consumedRecord: pool.consume({
|
|
116
|
+
rootId: request.rootId,
|
|
117
|
+
presigId: request.presigId,
|
|
118
|
+
requestedSubset: request.subset,
|
|
119
|
+
preValidateRecord: (rec) => {
|
|
120
|
+
validateRequestAgainstPublicPresig(request, rec.publicVerification);
|
|
121
|
+
}
|
|
122
|
+
}),
|
|
123
|
+
request
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Verifies a single party's partial signature share `s_i` (`design.md` §4.5(2)):
|
|
128
|
+
* `\delta_{rerand} * s_i * R == (m + r' * t_tweak) * K_i + r' * Z_i`.
|
|
129
|
+
* Attributes exact `culpritSlot = share.slot` on mismatch!
|
|
130
|
+
*/
|
|
131
|
+
function verifyOnlineSignShare(params) {
|
|
132
|
+
const { share, publicVerification, request } = params;
|
|
133
|
+
const vectorOverride = params[VECTOR_TEST_DELTA_RERAND_SYMBOL];
|
|
134
|
+
const { msgDigest32, msgScalarModN, derivedChild } = validateRequestAgainstPublicPresig(request, publicVerification);
|
|
135
|
+
if (!publicVerification.subset.includes(share.slot)) throw new require_constants.MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Unexpected sign share slot ${share.slot}`, share.slot);
|
|
136
|
+
if (!require_secp256k1_scalar.constantTimeEqual(share.presigId32, publicVerification.presigId32)) throw new require_constants.MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `presigId mismatch in sign share from slot ${share.slot}`, share.slot);
|
|
137
|
+
let sI;
|
|
138
|
+
try {
|
|
139
|
+
sI = require_secp256k1_scalar.bytes32ToScalarStrict(share.sShareBytes32);
|
|
140
|
+
} catch {
|
|
141
|
+
throw new require_constants.MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Malformed s_${share.slot} scalar`, share.slot);
|
|
142
|
+
}
|
|
143
|
+
const deltaRerand = vectorOverride ?? deriveGrothShoupRerandScalar({
|
|
144
|
+
presigId32: publicVerification.presigId32,
|
|
145
|
+
subset: publicVerification.subset,
|
|
146
|
+
rPointComp33: publicVerification.rPointComp33,
|
|
147
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
148
|
+
msgDigest32,
|
|
149
|
+
tweakScalar: derivedChild.tweakScalar
|
|
150
|
+
});
|
|
151
|
+
const rPoint = require_secp256k1_scalar.decodePointCompressed(publicVerification.rPointComp33, false);
|
|
152
|
+
const rerand = computeRerandomizedNoncePoint(rPoint, deltaRerand);
|
|
153
|
+
const kIComp33 = publicVerification.kPointsComp33BySlot.get(share.slot);
|
|
154
|
+
const zIComp33 = publicVerification.zPointsComp33BySlot.get(share.slot);
|
|
155
|
+
if (!kIComp33 || !zIComp33) throw new require_constants.MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Missing K_${share.slot} or Z_${share.slot}`, share.slot);
|
|
156
|
+
const kIPoint = require_secp256k1_scalar.decodePointCompressed(kIComp33, false);
|
|
157
|
+
const zIPoint = require_secp256k1_scalar.decodePointCompressed(zIComp33, false);
|
|
158
|
+
const lhs = require_secp256k1_scalar.mulPoint(rPoint, require_secp256k1_scalar.mulModN(deltaRerand, sI));
|
|
159
|
+
const rhs = require_secp256k1_scalar.mulPoint(kIPoint, require_secp256k1_scalar.addModN(msgScalarModN, require_secp256k1_scalar.mulModN(rerand.rPrime, derivedChild.tweakScalar))).add(require_secp256k1_scalar.mulPoint(zIPoint, rerand.rPrime));
|
|
160
|
+
if (!lhs.equals(rhs)) throw new require_constants.MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Identifiable abort: corrupted partial signature s_${share.slot}`, share.slot);
|
|
161
|
+
return rerand;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Verifies all `t` partial signature shares, aggregates `s_raw = \sum_{i \in S} s_i mod n`,
|
|
165
|
+
* enforces EIP-2 low-S (`s_sig <= n / 2`, toggling `yParity ^= 1` when negated), and self-verifies
|
|
166
|
+
* `ecrecover(z, r', s_sig, yParity) === P_child`.
|
|
167
|
+
*/
|
|
168
|
+
function aggregateOnlineSignature(params) {
|
|
169
|
+
const { shares, publicVerification, request } = params;
|
|
170
|
+
const vectorOverride = params[VECTOR_TEST_DELTA_RERAND_SYMBOL];
|
|
171
|
+
const { msgDigest32, derivedChild } = validateRequestAgainstPublicPresig(request, publicVerification);
|
|
172
|
+
const pChild = derivedChild.childPoint;
|
|
173
|
+
const sorted = require_secp256k1_scalar.normalizeSortedSubset(publicVerification.subset);
|
|
174
|
+
if (shares.length !== sorted.length) throw new require_constants.MpcProtocolError("SIGN_AGGREGATION_FAILED", `Expected ${sorted.length} partial shares, got ${shares.length}`);
|
|
175
|
+
const seenSlots = /* @__PURE__ */ new Set();
|
|
176
|
+
let sRaw = 0n;
|
|
177
|
+
let rerand = null;
|
|
178
|
+
for (const share of shares) {
|
|
179
|
+
if (seenSlots.has(share.slot)) throw new require_constants.MpcProtocolError("SIGN_AGGREGATION_FAILED", `Duplicate sign share from slot ${share.slot}`, share.slot);
|
|
180
|
+
seenSlots.add(share.slot);
|
|
181
|
+
rerand = verifyOnlineSignShare({
|
|
182
|
+
share,
|
|
183
|
+
publicVerification,
|
|
184
|
+
request,
|
|
185
|
+
[VECTOR_TEST_DELTA_RERAND_SYMBOL]: vectorOverride
|
|
186
|
+
});
|
|
187
|
+
sRaw = require_secp256k1_scalar.addModN(sRaw, require_secp256k1_scalar.bytes32ToScalarStrict(share.sShareBytes32));
|
|
188
|
+
}
|
|
189
|
+
if (sRaw === 0n || !rerand) throw new require_constants.MpcProtocolError("SIGN_AGGREGATION_FAILED", "Aggregated s_raw == 0");
|
|
190
|
+
let sSig = sRaw;
|
|
191
|
+
let yParity = rerand.yPrimeParity;
|
|
192
|
+
if (sRaw > require_constants.SECP256K1_HALF_N) {
|
|
193
|
+
sSig = require_secp256k1_scalar.subModN(0n, sRaw);
|
|
194
|
+
yParity = yParity ^ 1;
|
|
195
|
+
}
|
|
196
|
+
const v = 27 + yParity;
|
|
197
|
+
const expectedAddr = require_tweak.pointToEthAddress(pChild);
|
|
198
|
+
const recovered = require_tweak.ecrecoverEthAddress({
|
|
199
|
+
msgDigest32,
|
|
200
|
+
r: rerand.rPrime,
|
|
201
|
+
s: sSig,
|
|
202
|
+
yParity
|
|
203
|
+
});
|
|
204
|
+
if (!recovered.recoveredPoint.equals(pChild) || recovered.checksumAddress !== expectedAddr.checksumAddress) throw new require_constants.MpcProtocolError("SIGN_AGGREGATION_FAILED", "Internal invariant violation: ecrecover mismatch against P_child");
|
|
205
|
+
const rBytes32 = require_secp256k1_scalar.scalarToBytes32(rerand.rPrime);
|
|
206
|
+
const sBytes32 = require_secp256k1_scalar.scalarToBytes32(sSig);
|
|
207
|
+
const signature65Bytes = require_secp256k1_scalar.concatBytes(rBytes32, sBytes32, Uint8Array.of(v));
|
|
208
|
+
return {
|
|
209
|
+
r: rerand.rPrime,
|
|
210
|
+
s: sSig,
|
|
211
|
+
rBytes32,
|
|
212
|
+
sBytes32,
|
|
213
|
+
rHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(rBytes32)}`,
|
|
214
|
+
sHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(sBytes32)}`,
|
|
215
|
+
yParity,
|
|
216
|
+
v,
|
|
217
|
+
signature65Bytes,
|
|
218
|
+
signature65Hex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(signature65Bytes)}`,
|
|
219
|
+
deltaRerand: rerand.deltaRerand,
|
|
220
|
+
rPrimePointComp33: rerand.rPrimePointComp33,
|
|
221
|
+
signerAddress: expectedAddr.checksumAddress
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
//#endregion
|
|
226
|
+
exports.VECTOR_TEST_DELTA_RERAND_SYMBOL = VECTOR_TEST_DELTA_RERAND_SYMBOL;
|
|
227
|
+
exports.aggregateOnlineSignature = aggregateOnlineSignature;
|
|
228
|
+
exports.computeRerandomizedNoncePoint = computeRerandomizedNoncePoint;
|
|
229
|
+
exports.createOnlineSignShare = createOnlineSignShare;
|
|
230
|
+
exports.deriveGrothShoupRerandScalar = deriveGrothShoupRerandScalar;
|
|
231
|
+
exports.evaluateConsumedPresigShareWithRerand = evaluateConsumedPresigShareWithRerand;
|
|
232
|
+
exports.verifyAndDeriveRequestChildKey = verifyAndDeriveRequestChildKey;
|
|
233
|
+
exports.verifyOnlineSignShare = verifyOnlineSignShare;
|
package/lib/sign.d.cts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Secp256k1Point } from "./secp256k1-scalar.cjs";
|
|
2
|
+
import { DerivedChildKey, MpcChildDerivationSpec } from "./tweak.cjs";
|
|
3
|
+
import { PresignaturePublicVerification, PresignatureShareRecord, VolatilePresignaturePool } from "./presign.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/sign.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal-only symbol for deterministic unit-vector tests (e.g. `delta_rerand = 1n`).
|
|
9
|
+
* NOT exported from the `@ocap/mpc-hsm` package barrel (`src/index.ts`) and non-serializable over JSON/IPC.
|
|
10
|
+
*/
|
|
11
|
+
declare const VECTOR_TEST_DELTA_RERAND_SYMBOL: unique symbol;
|
|
12
|
+
interface VectorTestRerandBag {
|
|
13
|
+
readonly [VECTOR_TEST_DELTA_RERAND_SYMBOL]?: bigint;
|
|
14
|
+
}
|
|
15
|
+
interface MpcOnlineSignRequest {
|
|
16
|
+
readonly rootId: string;
|
|
17
|
+
readonly presigId: Uint8Array | string;
|
|
18
|
+
readonly subset: ReadonlyArray<number>;
|
|
19
|
+
readonly msgDigest32: Uint8Array | string;
|
|
20
|
+
/**
|
|
21
|
+
* Authenticated derivation preimage (`untweaked | role-bound | rule-bound`).
|
|
22
|
+
* Online signing re-derives `(t_tweak, P_child)` from `(P_root, rootId, childDerivation)`,
|
|
23
|
+
* eliminating raw unauthenticated `tweakScalar` oracles and strictly enforcing the
|
|
24
|
+
* `"escape-canary"` hard-ban across all modes.
|
|
25
|
+
*/
|
|
26
|
+
readonly childDerivation: MpcChildDerivationSpec;
|
|
27
|
+
}
|
|
28
|
+
interface MpcOnlineSignShare {
|
|
29
|
+
readonly slot: number;
|
|
30
|
+
readonly presigId32: Uint8Array;
|
|
31
|
+
readonly sShareBytes32: Uint8Array;
|
|
32
|
+
}
|
|
33
|
+
interface RerandomizedNonceResult {
|
|
34
|
+
readonly deltaRerand: bigint;
|
|
35
|
+
readonly rPrimePoint: Secp256k1Point;
|
|
36
|
+
readonly rPrimePointComp33: Uint8Array;
|
|
37
|
+
readonly rPrime: bigint;
|
|
38
|
+
readonly yPrimeParity: 0 | 1;
|
|
39
|
+
}
|
|
40
|
+
interface MpcAggregatedSignature {
|
|
41
|
+
readonly r: bigint;
|
|
42
|
+
readonly s: bigint;
|
|
43
|
+
readonly rBytes32: Uint8Array;
|
|
44
|
+
readonly sBytes32: Uint8Array;
|
|
45
|
+
readonly rHex: string;
|
|
46
|
+
readonly sHex: string;
|
|
47
|
+
readonly yParity: 0 | 1;
|
|
48
|
+
readonly v: 27 | 28;
|
|
49
|
+
readonly signature65Bytes: Uint8Array;
|
|
50
|
+
readonly signature65Hex: string;
|
|
51
|
+
readonly deltaRerand: bigint;
|
|
52
|
+
readonly rPrimePointComp33: Uint8Array;
|
|
53
|
+
readonly signerAddress: string;
|
|
54
|
+
}
|
|
55
|
+
declare function deriveGrothShoupRerandScalar(params: {
|
|
56
|
+
presigId32: Uint8Array;
|
|
57
|
+
subset: ReadonlyArray<number>;
|
|
58
|
+
rPointComp33: Uint8Array;
|
|
59
|
+
childPublicKeyComp33: Uint8Array;
|
|
60
|
+
msgDigest32: Uint8Array;
|
|
61
|
+
tweakScalar: bigint;
|
|
62
|
+
}): bigint;
|
|
63
|
+
declare function computeRerandomizedNoncePoint(rPointOrComp33: Secp256k1Point | Uint8Array, deltaRerand: bigint): RerandomizedNonceResult;
|
|
64
|
+
declare function verifyAndDeriveRequestChildKey(params: {
|
|
65
|
+
rootPublicKeyComp33: Uint8Array;
|
|
66
|
+
rootId: string;
|
|
67
|
+
childDerivation: MpcChildDerivationSpec;
|
|
68
|
+
}): DerivedChildKey;
|
|
69
|
+
/**
|
|
70
|
+
* Evaluates a Groth-Shoup partial signature share on an already-consumed `PresignatureShareRecord`
|
|
71
|
+
* and unconditionally zeroizes `(kShareBytes32, chiShareBytes32)` in `finally`.
|
|
72
|
+
*/
|
|
73
|
+
declare function evaluateConsumedPresigShareWithRerand(params: {
|
|
74
|
+
consumedRecord: PresignatureShareRecord;
|
|
75
|
+
request: MpcOnlineSignRequest;
|
|
76
|
+
} & VectorTestRerandBag): {
|
|
77
|
+
share: MpcOnlineSignShare;
|
|
78
|
+
rerand: RerandomizedNonceResult;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Synchronously validates the request preimage (blocking `"escape-canary"` before pool touch),
|
|
82
|
+
* consumes `presigId` from `pool`, validates `request.subset === presig.subset`, computes `s_i`,
|
|
83
|
+
* and wipes `k_i` & `\chi_i` from RAM.
|
|
84
|
+
*/
|
|
85
|
+
declare function createOnlineSignShare(params: {
|
|
86
|
+
pool: VolatilePresignaturePool;
|
|
87
|
+
request: MpcOnlineSignRequest;
|
|
88
|
+
}): {
|
|
89
|
+
share: MpcOnlineSignShare;
|
|
90
|
+
rerand: RerandomizedNonceResult;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Verifies a single party's partial signature share `s_i` (`design.md` §4.5(2)):
|
|
94
|
+
* `\delta_{rerand} * s_i * R == (m + r' * t_tweak) * K_i + r' * Z_i`.
|
|
95
|
+
* Attributes exact `culpritSlot = share.slot` on mismatch!
|
|
96
|
+
*/
|
|
97
|
+
declare function verifyOnlineSignShare(params: {
|
|
98
|
+
share: MpcOnlineSignShare;
|
|
99
|
+
publicVerification: PresignaturePublicVerification;
|
|
100
|
+
request: MpcOnlineSignRequest;
|
|
101
|
+
} & VectorTestRerandBag): RerandomizedNonceResult;
|
|
102
|
+
/**
|
|
103
|
+
* Verifies all `t` partial signature shares, aggregates `s_raw = \sum_{i \in S} s_i mod n`,
|
|
104
|
+
* enforces EIP-2 low-S (`s_sig <= n / 2`, toggling `yParity ^= 1` when negated), and self-verifies
|
|
105
|
+
* `ecrecover(z, r', s_sig, yParity) === P_child`.
|
|
106
|
+
*/
|
|
107
|
+
declare function aggregateOnlineSignature(params: {
|
|
108
|
+
shares: ReadonlyArray<MpcOnlineSignShare>;
|
|
109
|
+
publicVerification: PresignaturePublicVerification;
|
|
110
|
+
request: MpcOnlineSignRequest;
|
|
111
|
+
} & VectorTestRerandBag): MpcAggregatedSignature;
|
|
112
|
+
//#endregion
|
|
113
|
+
export { MpcAggregatedSignature, MpcOnlineSignRequest, MpcOnlineSignShare, RerandomizedNonceResult, VECTOR_TEST_DELTA_RERAND_SYMBOL, VectorTestRerandBag, aggregateOnlineSignature, computeRerandomizedNoncePoint, createOnlineSignShare, deriveGrothShoupRerandScalar, evaluateConsumedPresigShareWithRerand, verifyAndDeriveRequestChildKey, verifyOnlineSignShare };
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_secp256k1_scalar = require('./secp256k1-scalar.cjs');
|
|
3
|
+
const require_bip39_english_wordlist = require('./bip39-english-wordlist.cjs');
|
|
4
|
+
const require_tweak = require('./tweak.cjs');
|
|
5
|
+
let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
|
|
6
|
+
let _noble_hashes_hmac_js = require("@noble/hashes/hmac.js");
|
|
7
|
+
let _noble_hashes_pbkdf2_js = require("@noble/hashes/pbkdf2.js");
|
|
8
|
+
|
|
9
|
+
//#region src/steel-mnemonic.ts
|
|
10
|
+
/**
|
|
11
|
+
* Pure 3-Plate 24-Word Standard `BIP-39` Stainless-Steel Cold Backup (`design.md` §5.2):
|
|
12
|
+
* - ZERO human-memorized passphrases (`25th word`) anywhere in cold-backup encoding, decoding, or derivation.
|
|
13
|
+
* - Standard 256-bit entropy (`coldShareBytes32` in `[1, secp256k1.n - 1]`) + 8-bit `SHA-256` BIP-39 checksum
|
|
14
|
+
* -> 24 lowercase English BIP-39 words (`Plate A`, `Plate B`, `Plate C`).
|
|
15
|
+
* - Feldman index identification (`g(k) * G == Y_k`) and 24th-word 8-sibling `m/44'/60'/0'/0/0` decoy address
|
|
16
|
+
* enumeration for outgoing-nonce physical-burglar tripwires.
|
|
17
|
+
*/
|
|
18
|
+
const BIP44_ETH_STANDARD_PATH = "m/44'/60'/0'/0/0";
|
|
19
|
+
const BIP44_ETH_INDICES = [
|
|
20
|
+
2147483692,
|
|
21
|
+
2147483708,
|
|
22
|
+
2147483648,
|
|
23
|
+
0,
|
|
24
|
+
0
|
|
25
|
+
];
|
|
26
|
+
const BIP39_WORD_RE = /^[a-z]+$/;
|
|
27
|
+
function guardianIndexToPlateLabel(guardianIndex) {
|
|
28
|
+
if (!Number.isInteger(guardianIndex) || guardianIndex < 1) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Invalid guardianIndex: ${guardianIndex}`);
|
|
29
|
+
if (guardianIndex <= 26) return `Plate ${String.fromCharCode(64 + guardianIndex)}`;
|
|
30
|
+
return `Plate #${guardianIndex}`;
|
|
31
|
+
}
|
|
32
|
+
function normalizeMnemonicWordList(wordsInput, expectedCount) {
|
|
33
|
+
let rawTokens;
|
|
34
|
+
if (typeof wordsInput === "string") {
|
|
35
|
+
const trimmed = wordsInput.trim();
|
|
36
|
+
if (trimmed.length === 0) throw new require_constants.MpcProtocolError("BIP39_INVALID_MNEMONIC", "Mnemonic input is empty");
|
|
37
|
+
rawTokens = trimmed.split(/\s+/);
|
|
38
|
+
} else if (Array.isArray(wordsInput)) rawTokens = wordsInput.map((w) => {
|
|
39
|
+
if (typeof w !== "string") throw new require_constants.MpcProtocolError("BIP39_INVALID_MNEMONIC", "Mnemonic words must be strings");
|
|
40
|
+
return w.trim();
|
|
41
|
+
});
|
|
42
|
+
else throw new require_constants.MpcProtocolError("BIP39_INVALID_MNEMONIC", "Expected 24-word string or string array");
|
|
43
|
+
if (rawTokens.length !== expectedCount) throw new require_constants.MpcProtocolError("BIP39_INVALID_MNEMONIC", `Expected exactly ${expectedCount} standard BIP-39 words (zero passphrases), got ${rawTokens.length}`);
|
|
44
|
+
return rawTokens.map((token, idx) => {
|
|
45
|
+
const lower = token.toLowerCase();
|
|
46
|
+
if (!BIP39_WORD_RE.test(lower) || !require_bip39_english_wordlist.BIP39_ENGLISH_WORD_TO_INDEX.has(lower)) throw new require_constants.MpcProtocolError("BIP39_INVALID_MNEMONIC", `Invalid BIP-39 English word at position ${idx + 1}: "${token}"`);
|
|
47
|
+
return lower;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Encodes a 32-byte cold Shamir share (`1 <= scalar < secp256k1.n`) into 24 standard English BIP-39 words
|
|
52
|
+
* (`256` entropy bits + `8` SHA-256 checksum bits = `264` bits = `24 x 11` bits).
|
|
53
|
+
*/
|
|
54
|
+
function encodeColdShareToBip39Words24(coldShareBytes32) {
|
|
55
|
+
require_secp256k1_scalar.bytes32ToScalarNonZero(coldShareBytes32);
|
|
56
|
+
const digest32 = (0, _noble_hashes_sha2_js.sha256)(coldShareBytes32);
|
|
57
|
+
const words = new Array(24);
|
|
58
|
+
try {
|
|
59
|
+
const checksumByte = digest32[0];
|
|
60
|
+
for (let w = 0; w < 24; w++) {
|
|
61
|
+
let wordIndex = 0;
|
|
62
|
+
for (let bit = 0; bit < 11; bit++) {
|
|
63
|
+
const bitPos = w * 11 + bit;
|
|
64
|
+
let bitVal;
|
|
65
|
+
if (bitPos < 256) {
|
|
66
|
+
const byteIdx = bitPos >>> 3;
|
|
67
|
+
const shift = 7 - (bitPos & 7);
|
|
68
|
+
bitVal = coldShareBytes32[byteIdx] >>> shift & 1;
|
|
69
|
+
} else bitVal = checksumByte >>> 7 - (bitPos - 256) & 1;
|
|
70
|
+
wordIndex = wordIndex << 1 | bitVal;
|
|
71
|
+
}
|
|
72
|
+
words[w] = require_bip39_english_wordlist.BIP39_ENGLISH_WORDLIST[wordIndex];
|
|
73
|
+
}
|
|
74
|
+
return words;
|
|
75
|
+
} finally {
|
|
76
|
+
require_secp256k1_scalar.zeroizeBytes(digest32);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Decodes 24 standard English BIP-39 words into a validated 32-byte `secp256k1` scalar (`1 <= scalar < n`),
|
|
81
|
+
* verifying exact 24-word count (zero passphrases) and the 8-bit SHA-256 BIP-39 checksum.
|
|
82
|
+
*/
|
|
83
|
+
function decodeBip39Words24ToColdShare(wordsInput) {
|
|
84
|
+
const words = normalizeMnemonicWordList(wordsInput, 24);
|
|
85
|
+
const entropy32 = new Uint8Array(32);
|
|
86
|
+
let givenChecksum = 0;
|
|
87
|
+
for (let w = 0; w < 24; w++) {
|
|
88
|
+
const wordIndex = require_bip39_english_wordlist.BIP39_ENGLISH_WORD_TO_INDEX.get(words[w]);
|
|
89
|
+
for (let bit = 0; bit < 11; bit++) {
|
|
90
|
+
const bitVal = wordIndex >>> 10 - bit & 1;
|
|
91
|
+
const bitPos = w * 11 + bit;
|
|
92
|
+
if (bitPos < 256) {
|
|
93
|
+
const byteIdx = bitPos >>> 3;
|
|
94
|
+
const shift = 7 - (bitPos & 7);
|
|
95
|
+
entropy32[byteIdx] |= bitVal << shift;
|
|
96
|
+
} else {
|
|
97
|
+
const chkShift = 7 - (bitPos - 256);
|
|
98
|
+
givenChecksum |= bitVal << chkShift;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const digest32 = (0, _noble_hashes_sha2_js.sha256)(entropy32);
|
|
103
|
+
const expectedChecksum = digest32[0];
|
|
104
|
+
require_secp256k1_scalar.zeroizeBytes(digest32);
|
|
105
|
+
if (givenChecksum !== expectedChecksum) {
|
|
106
|
+
require_secp256k1_scalar.zeroizeBytes(entropy32);
|
|
107
|
+
throw new require_constants.MpcProtocolError("BIP39_CHECKSUM_MISMATCH", "24-word BIP-39 mnemonic failed 8-bit SHA-256 checksum verification");
|
|
108
|
+
}
|
|
109
|
+
const scalar = require_secp256k1_scalar.bytesToBigIntBE(entropy32);
|
|
110
|
+
if (scalar < 1n || scalar >= require_constants.SECP256K1_N) {
|
|
111
|
+
require_secp256k1_scalar.zeroizeBytes(entropy32);
|
|
112
|
+
throw new require_constants.MpcProtocolError("INVALID_SCALAR", "Decoded 24-word entropy is outside [1, secp256k1.n - 1]");
|
|
113
|
+
}
|
|
114
|
+
return entropy32;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Identifies which guardian index `k in 1..nCold` a physical 24-word cold share belongs to by
|
|
118
|
+
* evaluating the aggregated cold Feldman commitments `Y_k = \sum_{l=0}^{t_c-1} k^l C^{cold}_l`
|
|
119
|
+
* and checking `scalar * G == Y_k`. Fails closed if zero indices match (e.g. wrong 24th sibling word).
|
|
120
|
+
*/
|
|
121
|
+
function identifyColdShareIndexViaFeldman(coldShareBytes32, coldFeldmanCommitmentsComp33, nCold) {
|
|
122
|
+
if (!Number.isInteger(nCold) || nCold < 1 || nCold > require_constants.MAX_MPC_CLUSTER_NODES) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", `Invalid nCold: ${nCold}`);
|
|
123
|
+
if (!Array.isArray(coldFeldmanCommitmentsComp33) || coldFeldmanCommitmentsComp33.length === 0) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "coldFeldmanCommitmentsComp33 must be non-empty");
|
|
124
|
+
const scalar = require_secp256k1_scalar.bytes32ToScalarNonZero(coldShareBytes32);
|
|
125
|
+
const coldPoints = coldFeldmanCommitmentsComp33.map((c) => require_secp256k1_scalar.decodePointCompressed(require_secp256k1_scalar.normalizeHexBytes(c, 33), false));
|
|
126
|
+
const candidatePub = require_secp256k1_scalar.mulBaseG(scalar);
|
|
127
|
+
let matchedIndex = null;
|
|
128
|
+
for (let k = 1; k <= nCold; k++) {
|
|
129
|
+
const yk = require_secp256k1_scalar.evaluateFeldmanCommitments(coldPoints, k);
|
|
130
|
+
if (!yk.is0() && candidatePub.equals(yk)) {
|
|
131
|
+
if (matchedIndex !== null) throw new require_constants.MpcProtocolError("COLD_SHARE_UNKNOWN_INDEX", "Ambiguous Feldman cold share index match");
|
|
132
|
+
matchedIndex = k;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (matchedIndex === null) throw new require_constants.MpcProtocolError("COLD_SHARE_UNKNOWN_INDEX", `Cold share scalar * G does not match any guardian Feldman commitment Y_1..Y_${nCold}`);
|
|
136
|
+
return {
|
|
137
|
+
guardianIndex: matchedIndex,
|
|
138
|
+
plateLabel: guardianIndexToPlateLabel(matchedIndex),
|
|
139
|
+
coldPublicShareComp33: require_secp256k1_scalar.encodePointCompressed(candidatePub)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Derives the standard `m/44'/60'/0'/0/0` Ethereum EOA address (zero passphrase: salt = `"mnemonic"`)
|
|
144
|
+
* for a 24-word BIP-39 mnemonic so honeypot monitoring matches standard hardware/software wallets
|
|
145
|
+
* (`design.md` §5.2(2)).
|
|
146
|
+
*/
|
|
147
|
+
function deriveBip44EthAddressFromWords24(wordsInput) {
|
|
148
|
+
const words = normalizeMnemonicWordList(wordsInput, 24);
|
|
149
|
+
const entropy32 = new Uint8Array(32);
|
|
150
|
+
let givenChecksum = 0;
|
|
151
|
+
for (let w = 0; w < 24; w++) {
|
|
152
|
+
const wordIndex = require_bip39_english_wordlist.BIP39_ENGLISH_WORD_TO_INDEX.get(words[w]);
|
|
153
|
+
for (let bit = 0; bit < 11; bit++) {
|
|
154
|
+
const bitVal = wordIndex >>> 10 - bit & 1;
|
|
155
|
+
const bitPos = w * 11 + bit;
|
|
156
|
+
if (bitPos < 256) entropy32[bitPos >>> 3] |= bitVal << 7 - (bitPos & 7);
|
|
157
|
+
else givenChecksum |= bitVal << 7 - (bitPos - 256);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const chkDigest = (0, _noble_hashes_sha2_js.sha256)(entropy32);
|
|
161
|
+
const expectedChecksum = chkDigest[0];
|
|
162
|
+
require_secp256k1_scalar.zeroizeBytes(entropy32, chkDigest);
|
|
163
|
+
if (givenChecksum !== expectedChecksum) throw new require_constants.MpcProtocolError("BIP39_CHECKSUM_MISMATCH", "Invalid 24-word BIP-39 checksum");
|
|
164
|
+
const sentenceBytes = require_secp256k1_scalar.utf8Bytes(words.join(" "));
|
|
165
|
+
const seed64 = (0, _noble_hashes_pbkdf2_js.pbkdf2)(_noble_hashes_sha2_js.sha512, sentenceBytes, require_secp256k1_scalar.utf8Bytes("mnemonic"), {
|
|
166
|
+
c: 2048,
|
|
167
|
+
dkLen: 64
|
|
168
|
+
});
|
|
169
|
+
const masterI = (0, _noble_hashes_hmac_js.hmac)(_noble_hashes_sha2_js.sha512, require_secp256k1_scalar.utf8Bytes("Bitcoin seed"), seed64);
|
|
170
|
+
const chainCode32 = new Uint8Array(masterI.subarray(32, 64));
|
|
171
|
+
let keyScalar = require_secp256k1_scalar.bytesToBigIntBE(masterI.subarray(0, 32));
|
|
172
|
+
try {
|
|
173
|
+
if (keyScalar === 0n || keyScalar >= require_constants.SECP256K1_N) throw new require_constants.MpcProtocolError("INVALID_SCALAR", "Degenerate BIP-32 master key derived from mnemonic");
|
|
174
|
+
for (const childIndex of BIP44_ETH_INDICES) {
|
|
175
|
+
let data;
|
|
176
|
+
if (childIndex >= 2147483648) {
|
|
177
|
+
const keyBytes = require_secp256k1_scalar.scalarToBytes32(keyScalar);
|
|
178
|
+
data = require_secp256k1_scalar.concatBytes(require_secp256k1_scalar.u8(0), keyBytes, require_secp256k1_scalar.u32be(childIndex));
|
|
179
|
+
require_secp256k1_scalar.zeroizeBytes(keyBytes);
|
|
180
|
+
} else data = require_secp256k1_scalar.concatBytes(require_secp256k1_scalar.encodePointCompressed(require_secp256k1_scalar.mulBaseG(keyScalar)), require_secp256k1_scalar.u32be(childIndex));
|
|
181
|
+
const childI = (0, _noble_hashes_hmac_js.hmac)(_noble_hashes_sha2_js.sha512, chainCode32, data);
|
|
182
|
+
require_secp256k1_scalar.zeroizeBytes(data);
|
|
183
|
+
try {
|
|
184
|
+
const il = require_secp256k1_scalar.bytesToBigIntBE(childI.subarray(0, 32));
|
|
185
|
+
const nextKey = require_secp256k1_scalar.addModN(il, keyScalar);
|
|
186
|
+
if (il >= require_constants.SECP256K1_N || nextKey === 0n) throw new require_constants.MpcProtocolError("INVALID_SCALAR", "Degenerate BIP-32 child key derived along m/44h/60h/0h/0/0");
|
|
187
|
+
keyScalar = nextKey;
|
|
188
|
+
chainCode32.set(childI.subarray(32, 64));
|
|
189
|
+
} finally {
|
|
190
|
+
require_secp256k1_scalar.zeroizeBytes(childI);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return require_tweak.pointToEthAddress(require_secp256k1_scalar.mulBaseG(keyScalar)).checksumAddress;
|
|
194
|
+
} finally {
|
|
195
|
+
keyScalar = 0n;
|
|
196
|
+
require_secp256k1_scalar.zeroizeBytes(sentenceBytes, seed64, masterI, chainCode32);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Given the first 23 standard BIP-39 words (`253` bits), enumerates all `2^3 = 8` checksum-valid
|
|
201
|
+
* 24th words (`3` entropy bits + `8` SHA-256 checksum bits) and derives each candidate's standard
|
|
202
|
+
* `m/44'/60'/0'/0/0` `0x...` Ethereum canary/honeypot address (`design.md` §5.2(2)).
|
|
203
|
+
*/
|
|
204
|
+
function enumerateValid24thBip39Siblings(first23WordsInput) {
|
|
205
|
+
const first23Words = normalizeMnemonicWordList(first23WordsInput, 23);
|
|
206
|
+
const baseEntropy32 = new Uint8Array(32);
|
|
207
|
+
for (let w = 0; w < 23; w++) {
|
|
208
|
+
const wordIndex = require_bip39_english_wordlist.BIP39_ENGLISH_WORD_TO_INDEX.get(first23Words[w]);
|
|
209
|
+
for (let bit = 0; bit < 11; bit++) {
|
|
210
|
+
const bitVal = wordIndex >>> 10 - bit & 1;
|
|
211
|
+
const bitPos = w * 11 + bit;
|
|
212
|
+
baseEntropy32[bitPos >>> 3] |= bitVal << 7 - (bitPos & 7);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const results = [];
|
|
216
|
+
const candidateEntropy32 = new Uint8Array(32);
|
|
217
|
+
try {
|
|
218
|
+
for (let tail3 = 0; tail3 < 8; tail3++) {
|
|
219
|
+
candidateEntropy32.set(baseEntropy32);
|
|
220
|
+
candidateEntropy32[31] = baseEntropy32[31] & 248 | tail3;
|
|
221
|
+
const digest32 = (0, _noble_hashes_sha2_js.sha256)(candidateEntropy32);
|
|
222
|
+
const checksumByte = digest32[0];
|
|
223
|
+
require_secp256k1_scalar.zeroizeBytes(digest32);
|
|
224
|
+
const wordIndex = tail3 << 8 | checksumByte;
|
|
225
|
+
const word24 = require_bip39_english_wordlist.BIP39_ENGLISH_WORDLIST[wordIndex];
|
|
226
|
+
const words24 = [...first23Words, word24];
|
|
227
|
+
const scalarVal = require_secp256k1_scalar.bytesToBigIntBE(candidateEntropy32);
|
|
228
|
+
const isValidColdScalar = scalarVal >= 1n && scalarVal < require_constants.SECP256K1_N;
|
|
229
|
+
const ethAddress = deriveBip44EthAddressFromWords24(words24);
|
|
230
|
+
results.push({
|
|
231
|
+
tail3Bits: tail3,
|
|
232
|
+
wordIndex,
|
|
233
|
+
word24,
|
|
234
|
+
words24,
|
|
235
|
+
derivationPath: BIP44_ETH_STANDARD_PATH,
|
|
236
|
+
ethAddress,
|
|
237
|
+
isValidColdScalar
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return results;
|
|
241
|
+
} finally {
|
|
242
|
+
require_secp256k1_scalar.zeroizeBytes(baseEntropy32, candidateEntropy32);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
//#endregion
|
|
247
|
+
exports.BIP44_ETH_STANDARD_PATH = BIP44_ETH_STANDARD_PATH;
|
|
248
|
+
exports.decodeBip39Words24ToColdShare = decodeBip39Words24ToColdShare;
|
|
249
|
+
exports.deriveBip44EthAddressFromWords24 = deriveBip44EthAddressFromWords24;
|
|
250
|
+
exports.encodeColdShareToBip39Words24 = encodeColdShareToBip39Words24;
|
|
251
|
+
exports.enumerateValid24thBip39Siblings = enumerateValid24thBip39Siblings;
|
|
252
|
+
exports.guardianIndexToPlateLabel = guardianIndexToPlateLabel;
|
|
253
|
+
exports.identifyColdShareIndexViaFeldman = identifyColdShareIndexViaFeldman;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/steel-mnemonic.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Pure 3-Plate 24-Word Standard `BIP-39` Stainless-Steel Cold Backup (`design.md` §5.2):
|
|
4
|
+
* - ZERO human-memorized passphrases (`25th word`) anywhere in cold-backup encoding, decoding, or derivation.
|
|
5
|
+
* - Standard 256-bit entropy (`coldShareBytes32` in `[1, secp256k1.n - 1]`) + 8-bit `SHA-256` BIP-39 checksum
|
|
6
|
+
* -> 24 lowercase English BIP-39 words (`Plate A`, `Plate B`, `Plate C`).
|
|
7
|
+
* - Feldman index identification (`g(k) * G == Y_k`) and 24th-word 8-sibling `m/44'/60'/0'/0/0` decoy address
|
|
8
|
+
* enumeration for outgoing-nonce physical-burglar tripwires.
|
|
9
|
+
*/
|
|
10
|
+
declare const BIP44_ETH_STANDARD_PATH: "m/44'/60'/0'/0/0";
|
|
11
|
+
declare function guardianIndexToPlateLabel(guardianIndex: number): string;
|
|
12
|
+
/**
|
|
13
|
+
* Encodes a 32-byte cold Shamir share (`1 <= scalar < secp256k1.n`) into 24 standard English BIP-39 words
|
|
14
|
+
* (`256` entropy bits + `8` SHA-256 checksum bits = `264` bits = `24 x 11` bits).
|
|
15
|
+
*/
|
|
16
|
+
declare function encodeColdShareToBip39Words24(coldShareBytes32: Uint8Array): string[];
|
|
17
|
+
/**
|
|
18
|
+
* Decodes 24 standard English BIP-39 words into a validated 32-byte `secp256k1` scalar (`1 <= scalar < n`),
|
|
19
|
+
* verifying exact 24-word count (zero passphrases) and the 8-bit SHA-256 BIP-39 checksum.
|
|
20
|
+
*/
|
|
21
|
+
declare function decodeBip39Words24ToColdShare(wordsInput: ReadonlyArray<string> | string): Uint8Array;
|
|
22
|
+
interface IdentifiedColdShare {
|
|
23
|
+
readonly guardianIndex: number;
|
|
24
|
+
readonly plateLabel: string;
|
|
25
|
+
readonly coldPublicShareComp33: Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Identifies which guardian index `k in 1..nCold` a physical 24-word cold share belongs to by
|
|
29
|
+
* evaluating the aggregated cold Feldman commitments `Y_k = \sum_{l=0}^{t_c-1} k^l C^{cold}_l`
|
|
30
|
+
* and checking `scalar * G == Y_k`. Fails closed if zero indices match (e.g. wrong 24th sibling word).
|
|
31
|
+
*/
|
|
32
|
+
declare function identifyColdShareIndexViaFeldman(coldShareBytes32: Uint8Array, coldFeldmanCommitmentsComp33: ReadonlyArray<Uint8Array | string>, nCold: number): IdentifiedColdShare;
|
|
33
|
+
/**
|
|
34
|
+
* Derives the standard `m/44'/60'/0'/0/0` Ethereum EOA address (zero passphrase: salt = `"mnemonic"`)
|
|
35
|
+
* for a 24-word BIP-39 mnemonic so honeypot monitoring matches standard hardware/software wallets
|
|
36
|
+
* (`design.md` §5.2(2)).
|
|
37
|
+
*/
|
|
38
|
+
declare function deriveBip44EthAddressFromWords24(wordsInput: ReadonlyArray<string> | string): string;
|
|
39
|
+
interface Bip39TwentyFourthSiblingEntry {
|
|
40
|
+
readonly tail3Bits: number;
|
|
41
|
+
readonly wordIndex: number;
|
|
42
|
+
readonly word24: string;
|
|
43
|
+
readonly words24: ReadonlyArray<string>;
|
|
44
|
+
readonly derivationPath: typeof BIP44_ETH_STANDARD_PATH;
|
|
45
|
+
readonly ethAddress: string;
|
|
46
|
+
readonly isValidColdScalar: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Given the first 23 standard BIP-39 words (`253` bits), enumerates all `2^3 = 8` checksum-valid
|
|
50
|
+
* 24th words (`3` entropy bits + `8` SHA-256 checksum bits) and derives each candidate's standard
|
|
51
|
+
* `m/44'/60'/0'/0/0` `0x...` Ethereum canary/honeypot address (`design.md` §5.2(2)).
|
|
52
|
+
*/
|
|
53
|
+
declare function enumerateValid24thBip39Siblings(first23WordsInput: ReadonlyArray<string> | string): ReadonlyArray<Bip39TwentyFourthSiblingEntry>;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { BIP44_ETH_STANDARD_PATH, Bip39TwentyFourthSiblingEntry, IdentifiedColdShare, decodeBip39Words24ToColdShare, deriveBip44EthAddressFromWords24, encodeColdShareToBip39Words24, enumerateValid24thBip39Siblings, guardianIndexToPlateLabel, identifyColdShareIndexViaFeldman };
|