@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/sign.d.mts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Secp256k1Point } from "./secp256k1-scalar.mjs";
|
|
2
|
+
import { DerivedChildKey, MpcChildDerivationSpec } from "./tweak.mjs";
|
|
3
|
+
import { PresignaturePublicVerification, PresignatureShareRecord, VolatilePresignaturePool } from "./presign.mjs";
|
|
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 };
|
package/esm/sign.mjs
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { DST_PRESIG_RERAND_V1, MpcProtocolError, SECP256K1_HALF_N, SECP256K1_N } from "./constants.mjs";
|
|
2
|
+
import { H_scalar, addModN, assertScalarNonZero, bytes32ToScalarNonZero, bytes32ToScalarStrict, bytesToBigIntBE, bytesToHex, concatBytes, constantTimeEqual, decodePointCompressed, encodePointCompressed, invModN, modN, mulModN, mulPoint, normalizeHexBytes, normalizeSortedSubset, scalarToBytes32, sortedSubsetBytes, subModN, subsetsEqual, zeroizeBytes } from "./secp256k1-scalar.mjs";
|
|
3
|
+
import { deriveRoleBoundChild, deriveRuleBoundChild, deriveUntweakedChild, ecrecoverEthAddress, pointToEthAddress } from "./tweak.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/sign.ts
|
|
6
|
+
/**
|
|
7
|
+
* Internal-only symbol for deterministic unit-vector tests (e.g. `delta_rerand = 1n`).
|
|
8
|
+
* NOT exported from the `@ocap/mpc-hsm` package barrel (`src/index.ts`) and non-serializable over JSON/IPC.
|
|
9
|
+
*/
|
|
10
|
+
const VECTOR_TEST_DELTA_RERAND_SYMBOL = Symbol("ocap.mpc-hsm.vector-test-delta-rerand");
|
|
11
|
+
function deriveGrothShoupRerandScalar(params) {
|
|
12
|
+
const { presigId32, subset, rPointComp33, childPublicKeyComp33, msgDigest32, tweakScalar } = params;
|
|
13
|
+
if (presigId32.length !== 32 || msgDigest32.length !== 32) throw new MpcProtocolError("INVALID_PARAMETER", "presigId32 and msgDigest32 must be 32 bytes");
|
|
14
|
+
if (tweakScalar < 0n || tweakScalar >= SECP256K1_N) throw new MpcProtocolError("INVALID_SCALAR", "tweakScalar out of field range");
|
|
15
|
+
return H_scalar(DST_PRESIG_RERAND_V1, presigId32, sortedSubsetBytes(subset), rPointComp33, childPublicKeyComp33, msgDigest32, scalarToBytes32(tweakScalar));
|
|
16
|
+
}
|
|
17
|
+
function computeRerandomizedNoncePoint(rPointOrComp33, deltaRerand) {
|
|
18
|
+
assertScalarNonZero(deltaRerand, "deltaRerand");
|
|
19
|
+
const rPoint = rPointOrComp33 instanceof Uint8Array ? decodePointCompressed(rPointOrComp33, false) : rPointOrComp33;
|
|
20
|
+
if (rPoint.is0()) throw new MpcProtocolError("INVALID_POINT", "Presignature R cannot be infinity");
|
|
21
|
+
const rPrimePoint = mulPoint(rPoint, deltaRerand);
|
|
22
|
+
if (rPrimePoint.is0()) throw new MpcProtocolError("PRESIG_RERAND_BURN_RETRY", "Re-randomized R_prime hit infinity; burn presigId and retry with a fresh presignature");
|
|
23
|
+
const { x, y } = rPrimePoint.toAffine();
|
|
24
|
+
if (x >= SECP256K1_N || x % SECP256K1_N === 0n) throw new MpcProtocolError("PRESIG_RERAND_BURN_RETRY", "Re-randomized R_prime_x >= n or == 0; burn presigId and retry with a fresh presignature");
|
|
25
|
+
return {
|
|
26
|
+
deltaRerand,
|
|
27
|
+
rPrimePoint,
|
|
28
|
+
rPrimePointComp33: encodePointCompressed(rPrimePoint),
|
|
29
|
+
rPrime: x,
|
|
30
|
+
yPrimeParity: Number(y & 1n)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function verifyAndDeriveRequestChildKey(params) {
|
|
34
|
+
const { rootPublicKeyComp33, rootId, childDerivation } = params;
|
|
35
|
+
if (!childDerivation || typeof childDerivation !== "object") throw new MpcProtocolError("INVALID_PARAMETER", "Missing childDerivation in sign request");
|
|
36
|
+
let derived;
|
|
37
|
+
if (childDerivation.tweakMode === "untweaked") derived = deriveUntweakedChild(rootPublicKeyComp33);
|
|
38
|
+
else if (childDerivation.tweakMode === "role-bound") derived = deriveRoleBoundChild({
|
|
39
|
+
rootPublicKey: rootPublicKeyComp33,
|
|
40
|
+
rootId,
|
|
41
|
+
role: childDerivation.role,
|
|
42
|
+
ethChainId: childDerivation.ethChainId,
|
|
43
|
+
tweakLabel: childDerivation.tweakLabel
|
|
44
|
+
});
|
|
45
|
+
else if (childDerivation.tweakMode === "rule-bound") derived = deriveRuleBoundChild({
|
|
46
|
+
rootPublicKey: rootPublicKeyComp33,
|
|
47
|
+
rootId,
|
|
48
|
+
controllerDid: childDerivation.controllerDid,
|
|
49
|
+
ethChainId: childDerivation.ethChainId,
|
|
50
|
+
ruleHash: childDerivation.ruleHash
|
|
51
|
+
});
|
|
52
|
+
else throw new MpcProtocolError("INVALID_PARAMETER", "Unsupported childDerivation tweakMode");
|
|
53
|
+
if (!(childDerivation.childPublicKeyComp33 instanceof Uint8Array) || !constantTimeEqual(derived.childPublicKeyComp33, childDerivation.childPublicKeyComp33)) throw new MpcProtocolError("DEGENERATE_TWEAK", "childDerivation.childPublicKeyComp33 does not match canonically derived P_child");
|
|
54
|
+
return derived;
|
|
55
|
+
}
|
|
56
|
+
function validateRequestAgainstPublicPresig(request, pub) {
|
|
57
|
+
if (request.rootId !== pub.rootId) throw new MpcProtocolError("PRESIG_SUBSET_MISMATCH", `Sign request rootId ${request.rootId} != presig rootId ${pub.rootId}`);
|
|
58
|
+
if (!subsetsEqual(request.subset, pub.subset)) throw new MpcProtocolError("PRESIG_SUBSET_MISMATCH", `Sign request subset [${request.subset.join(",")}] != presig subset [${pub.subset.join(",")}]`);
|
|
59
|
+
if (!constantTimeEqual(normalizeHexBytes(request.presigId, 32), pub.presigId32)) throw new MpcProtocolError("PRESIG_SUBSET_MISMATCH", "presigId mismatch");
|
|
60
|
+
const msgDigest32 = normalizeHexBytes(request.msgDigest32, 32);
|
|
61
|
+
const derivedChild = verifyAndDeriveRequestChildKey({
|
|
62
|
+
rootPublicKeyComp33: pub.rootPublicKeyComp33,
|
|
63
|
+
rootId: pub.rootId,
|
|
64
|
+
childDerivation: request.childDerivation
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
msgDigest32,
|
|
68
|
+
msgScalarModN: modN(bytesToBigIntBE(msgDigest32)),
|
|
69
|
+
derivedChild
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Evaluates a Groth-Shoup partial signature share on an already-consumed `PresignatureShareRecord`
|
|
74
|
+
* and unconditionally zeroizes `(kShareBytes32, chiShareBytes32)` in `finally`.
|
|
75
|
+
*/
|
|
76
|
+
function evaluateConsumedPresigShareWithRerand(params) {
|
|
77
|
+
const { consumedRecord, request } = params;
|
|
78
|
+
const vectorOverride = params[VECTOR_TEST_DELTA_RERAND_SYMBOL];
|
|
79
|
+
try {
|
|
80
|
+
const { msgDigest32, msgScalarModN, derivedChild } = validateRequestAgainstPublicPresig(request, consumedRecord.publicVerification);
|
|
81
|
+
const kI = bytes32ToScalarNonZero(consumedRecord.kShareBytes32);
|
|
82
|
+
const chiI = bytes32ToScalarNonZero(consumedRecord.chiShareBytes32);
|
|
83
|
+
const deltaRerand = vectorOverride ?? deriveGrothShoupRerandScalar({
|
|
84
|
+
presigId32: consumedRecord.publicVerification.presigId32,
|
|
85
|
+
subset: consumedRecord.publicVerification.subset,
|
|
86
|
+
rPointComp33: consumedRecord.publicVerification.rPointComp33,
|
|
87
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
88
|
+
msgDigest32,
|
|
89
|
+
tweakScalar: derivedChild.tweakScalar
|
|
90
|
+
});
|
|
91
|
+
const rerand = computeRerandomizedNoncePoint(consumedRecord.publicVerification.rPointComp33, deltaRerand);
|
|
92
|
+
const inner = addModN(mulModN(kI, addModN(msgScalarModN, mulModN(rerand.rPrime, derivedChild.tweakScalar))), mulModN(rerand.rPrime, chiI));
|
|
93
|
+
const sI = mulModN(invModN(deltaRerand), inner);
|
|
94
|
+
return {
|
|
95
|
+
share: {
|
|
96
|
+
slot: consumedRecord.slot,
|
|
97
|
+
presigId32: new Uint8Array(consumedRecord.publicVerification.presigId32),
|
|
98
|
+
sShareBytes32: scalarToBytes32(sI)
|
|
99
|
+
},
|
|
100
|
+
rerand
|
|
101
|
+
};
|
|
102
|
+
} finally {
|
|
103
|
+
zeroizeBytes(consumedRecord.kShareBytes32, consumedRecord.chiShareBytes32);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Synchronously validates the request preimage (blocking `"escape-canary"` before pool touch),
|
|
108
|
+
* consumes `presigId` from `pool`, validates `request.subset === presig.subset`, computes `s_i`,
|
|
109
|
+
* and wipes `k_i` & `\chi_i` from RAM.
|
|
110
|
+
*/
|
|
111
|
+
function createOnlineSignShare(params) {
|
|
112
|
+
const { pool, request } = params;
|
|
113
|
+
return evaluateConsumedPresigShareWithRerand({
|
|
114
|
+
consumedRecord: pool.consume({
|
|
115
|
+
rootId: request.rootId,
|
|
116
|
+
presigId: request.presigId,
|
|
117
|
+
requestedSubset: request.subset,
|
|
118
|
+
preValidateRecord: (rec) => {
|
|
119
|
+
validateRequestAgainstPublicPresig(request, rec.publicVerification);
|
|
120
|
+
}
|
|
121
|
+
}),
|
|
122
|
+
request
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Verifies a single party's partial signature share `s_i` (`design.md` §4.5(2)):
|
|
127
|
+
* `\delta_{rerand} * s_i * R == (m + r' * t_tweak) * K_i + r' * Z_i`.
|
|
128
|
+
* Attributes exact `culpritSlot = share.slot` on mismatch!
|
|
129
|
+
*/
|
|
130
|
+
function verifyOnlineSignShare(params) {
|
|
131
|
+
const { share, publicVerification, request } = params;
|
|
132
|
+
const vectorOverride = params[VECTOR_TEST_DELTA_RERAND_SYMBOL];
|
|
133
|
+
const { msgDigest32, msgScalarModN, derivedChild } = validateRequestAgainstPublicPresig(request, publicVerification);
|
|
134
|
+
if (!publicVerification.subset.includes(share.slot)) throw new MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Unexpected sign share slot ${share.slot}`, share.slot);
|
|
135
|
+
if (!constantTimeEqual(share.presigId32, publicVerification.presigId32)) throw new MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `presigId mismatch in sign share from slot ${share.slot}`, share.slot);
|
|
136
|
+
let sI;
|
|
137
|
+
try {
|
|
138
|
+
sI = bytes32ToScalarStrict(share.sShareBytes32);
|
|
139
|
+
} catch {
|
|
140
|
+
throw new MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Malformed s_${share.slot} scalar`, share.slot);
|
|
141
|
+
}
|
|
142
|
+
const deltaRerand = vectorOverride ?? deriveGrothShoupRerandScalar({
|
|
143
|
+
presigId32: publicVerification.presigId32,
|
|
144
|
+
subset: publicVerification.subset,
|
|
145
|
+
rPointComp33: publicVerification.rPointComp33,
|
|
146
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
147
|
+
msgDigest32,
|
|
148
|
+
tweakScalar: derivedChild.tweakScalar
|
|
149
|
+
});
|
|
150
|
+
const rPoint = decodePointCompressed(publicVerification.rPointComp33, false);
|
|
151
|
+
const rerand = computeRerandomizedNoncePoint(rPoint, deltaRerand);
|
|
152
|
+
const kIComp33 = publicVerification.kPointsComp33BySlot.get(share.slot);
|
|
153
|
+
const zIComp33 = publicVerification.zPointsComp33BySlot.get(share.slot);
|
|
154
|
+
if (!kIComp33 || !zIComp33) throw new MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Missing K_${share.slot} or Z_${share.slot}`, share.slot);
|
|
155
|
+
const kIPoint = decodePointCompressed(kIComp33, false);
|
|
156
|
+
const zIPoint = decodePointCompressed(zIComp33, false);
|
|
157
|
+
const lhs = mulPoint(rPoint, mulModN(deltaRerand, sI));
|
|
158
|
+
const rhs = mulPoint(kIPoint, addModN(msgScalarModN, mulModN(rerand.rPrime, derivedChild.tweakScalar))).add(mulPoint(zIPoint, rerand.rPrime));
|
|
159
|
+
if (!lhs.equals(rhs)) throw new MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", `Identifiable abort: corrupted partial signature s_${share.slot}`, share.slot);
|
|
160
|
+
return rerand;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Verifies all `t` partial signature shares, aggregates `s_raw = \sum_{i \in S} s_i mod n`,
|
|
164
|
+
* enforces EIP-2 low-S (`s_sig <= n / 2`, toggling `yParity ^= 1` when negated), and self-verifies
|
|
165
|
+
* `ecrecover(z, r', s_sig, yParity) === P_child`.
|
|
166
|
+
*/
|
|
167
|
+
function aggregateOnlineSignature(params) {
|
|
168
|
+
const { shares, publicVerification, request } = params;
|
|
169
|
+
const vectorOverride = params[VECTOR_TEST_DELTA_RERAND_SYMBOL];
|
|
170
|
+
const { msgDigest32, derivedChild } = validateRequestAgainstPublicPresig(request, publicVerification);
|
|
171
|
+
const pChild = derivedChild.childPoint;
|
|
172
|
+
const sorted = normalizeSortedSubset(publicVerification.subset);
|
|
173
|
+
if (shares.length !== sorted.length) throw new MpcProtocolError("SIGN_AGGREGATION_FAILED", `Expected ${sorted.length} partial shares, got ${shares.length}`);
|
|
174
|
+
const seenSlots = /* @__PURE__ */ new Set();
|
|
175
|
+
let sRaw = 0n;
|
|
176
|
+
let rerand = null;
|
|
177
|
+
for (const share of shares) {
|
|
178
|
+
if (seenSlots.has(share.slot)) throw new MpcProtocolError("SIGN_AGGREGATION_FAILED", `Duplicate sign share from slot ${share.slot}`, share.slot);
|
|
179
|
+
seenSlots.add(share.slot);
|
|
180
|
+
rerand = verifyOnlineSignShare({
|
|
181
|
+
share,
|
|
182
|
+
publicVerification,
|
|
183
|
+
request,
|
|
184
|
+
[VECTOR_TEST_DELTA_RERAND_SYMBOL]: vectorOverride
|
|
185
|
+
});
|
|
186
|
+
sRaw = addModN(sRaw, bytes32ToScalarStrict(share.sShareBytes32));
|
|
187
|
+
}
|
|
188
|
+
if (sRaw === 0n || !rerand) throw new MpcProtocolError("SIGN_AGGREGATION_FAILED", "Aggregated s_raw == 0");
|
|
189
|
+
let sSig = sRaw;
|
|
190
|
+
let yParity = rerand.yPrimeParity;
|
|
191
|
+
if (sRaw > SECP256K1_HALF_N) {
|
|
192
|
+
sSig = subModN(0n, sRaw);
|
|
193
|
+
yParity = yParity ^ 1;
|
|
194
|
+
}
|
|
195
|
+
const v = 27 + yParity;
|
|
196
|
+
const expectedAddr = pointToEthAddress(pChild);
|
|
197
|
+
const recovered = ecrecoverEthAddress({
|
|
198
|
+
msgDigest32,
|
|
199
|
+
r: rerand.rPrime,
|
|
200
|
+
s: sSig,
|
|
201
|
+
yParity
|
|
202
|
+
});
|
|
203
|
+
if (!recovered.recoveredPoint.equals(pChild) || recovered.checksumAddress !== expectedAddr.checksumAddress) throw new MpcProtocolError("SIGN_AGGREGATION_FAILED", "Internal invariant violation: ecrecover mismatch against P_child");
|
|
204
|
+
const rBytes32 = scalarToBytes32(rerand.rPrime);
|
|
205
|
+
const sBytes32 = scalarToBytes32(sSig);
|
|
206
|
+
const signature65Bytes = concatBytes(rBytes32, sBytes32, Uint8Array.of(v));
|
|
207
|
+
return {
|
|
208
|
+
r: rerand.rPrime,
|
|
209
|
+
s: sSig,
|
|
210
|
+
rBytes32,
|
|
211
|
+
sBytes32,
|
|
212
|
+
rHex: `0x${bytesToHex(rBytes32)}`,
|
|
213
|
+
sHex: `0x${bytesToHex(sBytes32)}`,
|
|
214
|
+
yParity,
|
|
215
|
+
v,
|
|
216
|
+
signature65Bytes,
|
|
217
|
+
signature65Hex: `0x${bytesToHex(signature65Bytes)}`,
|
|
218
|
+
deltaRerand: rerand.deltaRerand,
|
|
219
|
+
rPrimePointComp33: rerand.rPrimePointComp33,
|
|
220
|
+
signerAddress: expectedAddr.checksumAddress
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
//#endregion
|
|
225
|
+
export { VECTOR_TEST_DELTA_RERAND_SYMBOL, aggregateOnlineSignature, computeRerandomizedNoncePoint, createOnlineSignShare, deriveGrothShoupRerandScalar, evaluateConsumedPresigShareWithRerand, verifyAndDeriveRequestChildKey, verifyOnlineSignShare };
|
package/esm/tweak.d.mts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Secp256k1Point } from "./secp256k1-scalar.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/tweak.d.ts
|
|
4
|
+
type MpcTweakMode = 'untweaked' | 'role-bound' | 'rule-bound';
|
|
5
|
+
type MpcChildDerivationSpec = {
|
|
6
|
+
readonly tweakMode: 'untweaked';
|
|
7
|
+
readonly childPublicKeyComp33: Uint8Array;
|
|
8
|
+
} | {
|
|
9
|
+
readonly tweakMode: 'role-bound';
|
|
10
|
+
readonly role: string;
|
|
11
|
+
readonly ethChainId: bigint;
|
|
12
|
+
readonly tweakLabel: string;
|
|
13
|
+
readonly childPublicKeyComp33: Uint8Array;
|
|
14
|
+
} | {
|
|
15
|
+
readonly tweakMode: 'rule-bound';
|
|
16
|
+
readonly controllerDid: string;
|
|
17
|
+
readonly ethChainId: bigint;
|
|
18
|
+
readonly ruleHash: Uint8Array | string;
|
|
19
|
+
readonly childPublicKeyComp33: Uint8Array;
|
|
20
|
+
};
|
|
21
|
+
interface DerivedChildKey {
|
|
22
|
+
readonly tweakMode: MpcTweakMode;
|
|
23
|
+
readonly tweakScalar: bigint;
|
|
24
|
+
readonly tweakBytes32: Uint8Array;
|
|
25
|
+
readonly tweakHex: string;
|
|
26
|
+
readonly childPoint: Secp256k1Point;
|
|
27
|
+
readonly childPublicKeyComp33: Uint8Array;
|
|
28
|
+
readonly ethAddress: string;
|
|
29
|
+
readonly ethAddressLower: string;
|
|
30
|
+
readonly derivationSpec: MpcChildDerivationSpec;
|
|
31
|
+
}
|
|
32
|
+
declare function toEip55ChecksumAddress(lowerHexWithoutPrefix: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Validates an Ethereum `0x` address (rejects non-hex, all-uppercase, bad EIP-55 mixed-case
|
|
35
|
+
* checksums, and the zero address `0x0000...0000`) and returns canonical EIP-55 + lowercase forms.
|
|
36
|
+
*/
|
|
37
|
+
declare function parseStrictEthAddress(address: string): {
|
|
38
|
+
checksumAddress: string;
|
|
39
|
+
lowercaseAddress: string;
|
|
40
|
+
addressBytes20: Uint8Array;
|
|
41
|
+
};
|
|
42
|
+
declare function pointToEthAddress(pointOrComp33: Secp256k1Point | Uint8Array): {
|
|
43
|
+
checksumAddress: string;
|
|
44
|
+
lowercaseAddress: string;
|
|
45
|
+
};
|
|
46
|
+
declare function assertAllowedOnlineRole(role: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Applies a non-hardened additive scalar tweak `P_child = P_root + t_tweak * G`,
|
|
49
|
+
* strictly rejecting `t_tweak == 0`, `t_tweak >= n`, or `P_child == O`.
|
|
50
|
+
*/
|
|
51
|
+
declare function applyAdditiveTweak(rootPublicKey: Secp256k1Point | Uint8Array, tweakScalar: bigint, tweakMode: Exclude<MpcTweakMode, 'untweaked'>, derivationMeta?: {
|
|
52
|
+
readonly kind: 'role-bound';
|
|
53
|
+
readonly role: string;
|
|
54
|
+
readonly ethChainId: bigint;
|
|
55
|
+
readonly tweakLabel: string;
|
|
56
|
+
} | {
|
|
57
|
+
readonly kind: 'rule-bound';
|
|
58
|
+
readonly controllerDid: string;
|
|
59
|
+
readonly ethChainId: bigint;
|
|
60
|
+
readonly ruleHashBytes32: Uint8Array;
|
|
61
|
+
}): DerivedChildKey;
|
|
62
|
+
declare function deriveUntweakedChild(rootPublicKey: Secp256k1Point | Uint8Array): DerivedChildKey;
|
|
63
|
+
/**
|
|
64
|
+
* `role-bound` EOA child derivation (`design.md` §4.2):
|
|
65
|
+
* `t_tweak = H_scalar("ARC-MPC-CHILD-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(role), u64be(ethChainId), utf8(tweakLabel))`.
|
|
66
|
+
*/
|
|
67
|
+
declare function deriveRoleBoundChild(params: {
|
|
68
|
+
rootPublicKey: Secp256k1Point | Uint8Array;
|
|
69
|
+
rootId: string;
|
|
70
|
+
role: string;
|
|
71
|
+
ethChainId: bigint;
|
|
72
|
+
tweakLabel: string;
|
|
73
|
+
}): DerivedChildKey;
|
|
74
|
+
/**
|
|
75
|
+
* `rule-bound` EOA child derivation (`design.md` §4.2):
|
|
76
|
+
* `t_tweak = H_scalar("ARC-MPC-RULE-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(controllerDid), u64be(ethChainId), bytes32(ruleHash))`.
|
|
77
|
+
*/
|
|
78
|
+
declare function deriveRuleBoundChild(params: {
|
|
79
|
+
rootPublicKey: Secp256k1Point | Uint8Array;
|
|
80
|
+
rootId: string;
|
|
81
|
+
controllerDid: string;
|
|
82
|
+
ethChainId: bigint;
|
|
83
|
+
ruleHash: Uint8Array | string;
|
|
84
|
+
}): DerivedChildKey;
|
|
85
|
+
/**
|
|
86
|
+
* Recovers the EIP-55 Ethereum address from a 32-byte digest and EIP-2 low-S `(r, s, yParity)`
|
|
87
|
+
* signature, matching Solidity `ecrecover` and Type-2 EIP-1559 transaction recovery.
|
|
88
|
+
*/
|
|
89
|
+
declare function ecrecoverEthAddress(params: {
|
|
90
|
+
msgDigest32: Uint8Array;
|
|
91
|
+
r: bigint;
|
|
92
|
+
s: bigint;
|
|
93
|
+
yParity: 0 | 1;
|
|
94
|
+
}): {
|
|
95
|
+
recoveredPoint: Secp256k1Point;
|
|
96
|
+
checksumAddress: string;
|
|
97
|
+
lowercaseAddress: string;
|
|
98
|
+
};
|
|
99
|
+
//#endregion
|
|
100
|
+
export { DerivedChildKey, MpcChildDerivationSpec, MpcTweakMode, applyAdditiveTweak, assertAllowedOnlineRole, deriveRoleBoundChild, deriveRuleBoundChild, deriveUntweakedChild, ecrecoverEthAddress, parseStrictEthAddress, pointToEthAddress, toEip55ChecksumAddress };
|
package/esm/tweak.mjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { DST_CHILD_TWEAK_V1, DST_RULE_TWEAK_V1, FORBIDDEN_ONLINE_ROLE_ESCAPE_CANARY, MpcProtocolError, SECP256K1_N } from "./constants.mjs";
|
|
2
|
+
import { H_scalar, bytesToHex, decodePointCompressed, encodePointCompressed, hexToBytes, mulBaseG, normalizeHexBytes, scalarToBytes32, secp256k1, u64be, utf8Bytes } from "./secp256k1-scalar.mjs";
|
|
3
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
4
|
+
|
|
5
|
+
//#region src/tweak.ts
|
|
6
|
+
const ETH_ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
|
|
7
|
+
function toEip55ChecksumAddress(lowerHexWithoutPrefix) {
|
|
8
|
+
if (!/^[0-9a-f]{40}$/.test(lowerHexWithoutPrefix)) throw new MpcProtocolError("INVALID_ETH_ADDRESS", "Expected 40 lowercase hex chars");
|
|
9
|
+
const digestHex = bytesToHex(keccak_256(utf8Bytes(lowerHexWithoutPrefix)));
|
|
10
|
+
let out = "0x";
|
|
11
|
+
for (let i = 0; i < 40; i++) {
|
|
12
|
+
const ch = lowerHexWithoutPrefix[i];
|
|
13
|
+
out += Number.parseInt(digestHex[i], 16) >= 8 ? ch.toUpperCase() : ch;
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validates an Ethereum `0x` address (rejects non-hex, all-uppercase, bad EIP-55 mixed-case
|
|
19
|
+
* checksums, and the zero address `0x0000...0000`) and returns canonical EIP-55 + lowercase forms.
|
|
20
|
+
*/
|
|
21
|
+
function parseStrictEthAddress(address) {
|
|
22
|
+
if (typeof address !== "string" || !ETH_ADDRESS_RE.test(address)) throw new MpcProtocolError("INVALID_ETH_ADDRESS", `Invalid Ethereum address format: ${String(address)}`);
|
|
23
|
+
const body = address.slice(2);
|
|
24
|
+
const lower = body.toLowerCase();
|
|
25
|
+
if (/^0+$/.test(lower)) throw new MpcProtocolError("INVALID_ETH_ADDRESS", "Zero Ethereum address is forbidden");
|
|
26
|
+
const hasLower = /[a-f]/.test(body);
|
|
27
|
+
const hasUpper = /[A-F]/.test(body);
|
|
28
|
+
if (hasUpper && !hasLower) throw new MpcProtocolError("INVALID_ETH_ADDRESS", "All-uppercase Ethereum address is invalid");
|
|
29
|
+
const checksumAddress = toEip55ChecksumAddress(lower);
|
|
30
|
+
if (hasUpper && hasLower && checksumAddress !== address) throw new MpcProtocolError("INVALID_ETH_ADDRESS", "EIP-55 checksum mismatch");
|
|
31
|
+
return {
|
|
32
|
+
checksumAddress,
|
|
33
|
+
lowercaseAddress: `0x${lower}`,
|
|
34
|
+
addressBytes20: hexToBytes(lower)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function pointToEthAddress(pointOrComp33) {
|
|
38
|
+
const pt = pointOrComp33 instanceof Uint8Array ? decodePointCompressed(pointOrComp33, false) : pointOrComp33;
|
|
39
|
+
if (pt.is0()) throw new MpcProtocolError("INVALID_POINT", "Cannot derive Ethereum address from infinity");
|
|
40
|
+
pt.assertValidity();
|
|
41
|
+
const addrLower = bytesToHex(keccak_256(pt.toBytes(false).subarray(1, 65)).subarray(12, 32));
|
|
42
|
+
if (/^0+$/.test(addrLower)) throw new MpcProtocolError("INVALID_ETH_ADDRESS", "Derived Ethereum address is zero");
|
|
43
|
+
return {
|
|
44
|
+
checksumAddress: toEip55ChecksumAddress(addrLower),
|
|
45
|
+
lowercaseAddress: `0x${addrLower}`
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function assertAllowedOnlineRole(role) {
|
|
49
|
+
if (typeof role !== "string" || role.trim().length === 0) throw new MpcProtocolError("INVALID_PARAMETER", "Role must be a non-empty string");
|
|
50
|
+
if (role === FORBIDDEN_ONLINE_ROLE_ESCAPE_CANARY) throw new MpcProtocolError("FORBIDDEN_ONLINE_ROLE", `Role "${FORBIDDEN_ONLINE_ROLE_ESCAPE_CANARY}" is hard-banned in online MPC derivation and signing`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Applies a non-hardened additive scalar tweak `P_child = P_root + t_tweak * G`,
|
|
54
|
+
* strictly rejecting `t_tweak == 0`, `t_tweak >= n`, or `P_child == O`.
|
|
55
|
+
*/
|
|
56
|
+
function applyAdditiveTweak(rootPublicKey, tweakScalar, tweakMode, derivationMeta) {
|
|
57
|
+
const pRoot = rootPublicKey instanceof Uint8Array ? decodePointCompressed(rootPublicKey, false) : rootPublicKey;
|
|
58
|
+
if (pRoot.is0()) throw new MpcProtocolError("INVALID_POINT", "Root public key cannot be O");
|
|
59
|
+
if (typeof tweakScalar !== "bigint" || tweakScalar <= 0n || tweakScalar >= SECP256K1_N) throw new MpcProtocolError("DEGENERATE_TWEAK", "Additive tweak scalar must satisfy 1 <= t_tweak < n");
|
|
60
|
+
const pChild = pRoot.add(mulBaseG(tweakScalar));
|
|
61
|
+
if (pChild.is0()) throw new MpcProtocolError("DEGENERATE_TWEAK", "Derived child public key collapsed to point at infinity O");
|
|
62
|
+
const childPublicKeyComp33 = encodePointCompressed(pChild);
|
|
63
|
+
const tweakBytes32 = scalarToBytes32(tweakScalar);
|
|
64
|
+
const { checksumAddress, lowercaseAddress } = pointToEthAddress(pChild);
|
|
65
|
+
const derivationSpec = derivationMeta?.kind === "role-bound" ? {
|
|
66
|
+
tweakMode: "role-bound",
|
|
67
|
+
role: derivationMeta.role,
|
|
68
|
+
ethChainId: derivationMeta.ethChainId,
|
|
69
|
+
tweakLabel: derivationMeta.tweakLabel,
|
|
70
|
+
childPublicKeyComp33
|
|
71
|
+
} : derivationMeta?.kind === "rule-bound" ? {
|
|
72
|
+
tweakMode: "rule-bound",
|
|
73
|
+
controllerDid: derivationMeta.controllerDid,
|
|
74
|
+
ethChainId: derivationMeta.ethChainId,
|
|
75
|
+
ruleHash: derivationMeta.ruleHashBytes32,
|
|
76
|
+
childPublicKeyComp33
|
|
77
|
+
} : {
|
|
78
|
+
tweakMode: "untweaked",
|
|
79
|
+
childPublicKeyComp33
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
tweakMode,
|
|
83
|
+
tweakScalar,
|
|
84
|
+
tweakBytes32,
|
|
85
|
+
tweakHex: `0x${bytesToHex(tweakBytes32)}`,
|
|
86
|
+
childPoint: pChild,
|
|
87
|
+
childPublicKeyComp33,
|
|
88
|
+
ethAddress: checksumAddress,
|
|
89
|
+
ethAddressLower: lowercaseAddress,
|
|
90
|
+
derivationSpec
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function deriveUntweakedChild(rootPublicKey) {
|
|
94
|
+
const pRoot = rootPublicKey instanceof Uint8Array ? decodePointCompressed(rootPublicKey, false) : rootPublicKey;
|
|
95
|
+
if (pRoot.is0()) throw new MpcProtocolError("INVALID_POINT", "Root public key cannot be O");
|
|
96
|
+
const childPublicKeyComp33 = encodePointCompressed(pRoot);
|
|
97
|
+
const tweakBytes32 = new Uint8Array(32);
|
|
98
|
+
const { checksumAddress, lowercaseAddress } = pointToEthAddress(pRoot);
|
|
99
|
+
return {
|
|
100
|
+
tweakMode: "untweaked",
|
|
101
|
+
tweakScalar: 0n,
|
|
102
|
+
tweakBytes32,
|
|
103
|
+
tweakHex: `0x${bytesToHex(tweakBytes32)}`,
|
|
104
|
+
childPoint: pRoot,
|
|
105
|
+
childPublicKeyComp33,
|
|
106
|
+
ethAddress: checksumAddress,
|
|
107
|
+
ethAddressLower: lowercaseAddress,
|
|
108
|
+
derivationSpec: {
|
|
109
|
+
tweakMode: "untweaked",
|
|
110
|
+
childPublicKeyComp33
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* `role-bound` EOA child derivation (`design.md` §4.2):
|
|
116
|
+
* `t_tweak = H_scalar("ARC-MPC-CHILD-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(role), u64be(ethChainId), utf8(tweakLabel))`.
|
|
117
|
+
*/
|
|
118
|
+
function deriveRoleBoundChild(params) {
|
|
119
|
+
const { rootPublicKey, rootId, role, ethChainId, tweakLabel } = params;
|
|
120
|
+
if (typeof rootId !== "string" || rootId.length === 0) throw new MpcProtocolError("INVALID_PARAMETER", "rootId must be non-empty");
|
|
121
|
+
assertAllowedOnlineRole(role);
|
|
122
|
+
if (typeof tweakLabel !== "string" || tweakLabel.length === 0) throw new MpcProtocolError("INVALID_PARAMETER", "tweakLabel must be non-empty");
|
|
123
|
+
const pRoot = rootPublicKey instanceof Uint8Array ? decodePointCompressed(rootPublicKey, false) : rootPublicKey;
|
|
124
|
+
return applyAdditiveTweak(pRoot, H_scalar(DST_CHILD_TWEAK_V1, encodePointCompressed(pRoot), utf8Bytes(rootId), utf8Bytes(role), u64be(ethChainId), utf8Bytes(tweakLabel)), "role-bound", {
|
|
125
|
+
kind: "role-bound",
|
|
126
|
+
role,
|
|
127
|
+
ethChainId,
|
|
128
|
+
tweakLabel
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* `rule-bound` EOA child derivation (`design.md` §4.2):
|
|
133
|
+
* `t_tweak = H_scalar("ARC-MPC-RULE-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(controllerDid), u64be(ethChainId), bytes32(ruleHash))`.
|
|
134
|
+
*/
|
|
135
|
+
function deriveRuleBoundChild(params) {
|
|
136
|
+
const { rootPublicKey, rootId, controllerDid, ethChainId, ruleHash } = params;
|
|
137
|
+
if (typeof rootId !== "string" || rootId.length === 0) throw new MpcProtocolError("INVALID_PARAMETER", "rootId must be non-empty");
|
|
138
|
+
if (typeof controllerDid !== "string" || controllerDid.length === 0) throw new MpcProtocolError("INVALID_PARAMETER", "controllerDid must be non-empty");
|
|
139
|
+
const ruleHashBytes32 = normalizeHexBytes(ruleHash, 32);
|
|
140
|
+
const pRoot = rootPublicKey instanceof Uint8Array ? decodePointCompressed(rootPublicKey, false) : rootPublicKey;
|
|
141
|
+
return applyAdditiveTweak(pRoot, H_scalar(DST_RULE_TWEAK_V1, encodePointCompressed(pRoot), utf8Bytes(rootId), utf8Bytes(controllerDid), u64be(ethChainId), ruleHashBytes32), "rule-bound", {
|
|
142
|
+
kind: "rule-bound",
|
|
143
|
+
controllerDid,
|
|
144
|
+
ethChainId,
|
|
145
|
+
ruleHashBytes32
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Recovers the EIP-55 Ethereum address from a 32-byte digest and EIP-2 low-S `(r, s, yParity)`
|
|
150
|
+
* signature, matching Solidity `ecrecover` and Type-2 EIP-1559 transaction recovery.
|
|
151
|
+
*/
|
|
152
|
+
function ecrecoverEthAddress(params) {
|
|
153
|
+
const { msgDigest32, r, s, yParity } = params;
|
|
154
|
+
if (!(msgDigest32 instanceof Uint8Array) || msgDigest32.length !== 32) throw new MpcProtocolError("INVALID_PARAMETER", "msgDigest32 must be 32 bytes");
|
|
155
|
+
if (yParity !== 0 && yParity !== 1) throw new MpcProtocolError("INVALID_PARAMETER", "yParity must be 0 or 1");
|
|
156
|
+
const sig = new secp256k1.Signature(r, s, yParity);
|
|
157
|
+
if (sig.hasHighS()) throw new MpcProtocolError("SIGN_AGGREGATION_FAILED", "High-S signature rejected by EIP-2");
|
|
158
|
+
const recoveredPoint = sig.recoverPublicKey(msgDigest32);
|
|
159
|
+
const { checksumAddress, lowercaseAddress } = pointToEthAddress(recoveredPoint);
|
|
160
|
+
return {
|
|
161
|
+
recoveredPoint,
|
|
162
|
+
checksumAddress,
|
|
163
|
+
lowercaseAddress
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
export { applyAdditiveTweak, assertAllowedOnlineRole, deriveRoleBoundChild, deriveRuleBoundChild, deriveUntweakedChild, ecrecoverEthAddress, parseStrictEthAddress, pointToEthAddress, toEip55ChecksumAddress };
|