Design and mathematical specification
This document describes MultisigChallenge: what the contract stores, the proof it
verifies and how that proof is checked on-chain. It assumes familiarity with elliptic
curves and ECDSA.
1. Notation
| Symbol | Meaning | In the code |
|---|---|---|
| p | Field prime 2^{256}-2^{32}-977 | Secp256k1.P |
| E | The curve secp256k1, y^2 = x^3 + 7 over \mathbb{F}_p | |
| N | Order of E(\mathbb{F}_p), a prime | Secp256k1.N |
| G | Standard generator of E(\mathbb{F}_p) | G_X, G_Y |
| n, k | Number of signers and threshold; here n=7, k=4 | signerCount(), threshold |
| d_i, Q_i | Private and public key of signer i, Q_i = d_i G | signers() |
| (v,r,s) | ECDSA signature with 0 < s \le \lfloor N/2 \rfloor | Heartbeat.v/r/s |
| C, Y | Liveness base point and liveness point Y = d_i C | basePoint, livenessPoint |
| F | Challenge polynomial of the threshold proof | Proof.polyCoeffs |
| \mathrm{addr}(P) | Last 20 bytes of \mathrm{keccak256}(P_x \,\Vert\, P_y) | Secp256k1.toAddress |
2. The contract
The contract is deployed with two pieces of public data.
-
Seven public keys Q_0,\dots,Q_6. The constructor checks that each is a point of E and that their x-coordinates are pairwise distinct, which excludes both a repeated key Q and its opposite -Q.
-
A heartbeat record. For every signer i and every epoch e = 0,\dots,95 there is one entry
\big(e,\; h_e,\; (v,r,s),\; C,\; Y\big)where h_e is the 32-byte message hash of the epoch, the same for all seven signers, (v,r,s) is signer i's ECDSA signature on h_e, and Y = d_i C is a liveness point: the signer's key applied to a base point C, as in a Diffie-Hellman exchange.
The deployer, who is the organiser of the challenge, uploads the record with
publishHeartbeats and then calls sealRecord, after which it cannot change. The contract stores the record but never checks it.
The only way to win is
function verifyAndExecute(
CDSThreshold.Proof calldata proof,
bytes calldata message
) external;
which succeeds if and only if the record is sealed, message is non-empty and has not
been executed before, and proof is a valid proof that the caller knows at least k = 4
of the seven private keys (section 4). On success the contract marks message as
executed, records the first solver, and increments execNonce. Nothing else is executed:
the message is hashed into the proof, and its hash is stored so that the same message
cannot be used again.
2.1 The published data
The keys and the heartbeats are published in two files with the same values.
data/heartbeats.jsonhas the fieldschain_id,contract,n_slots,threshold,num_epochsandslots. Each entry ofslotsis one signer, with its indexslot, its public keypubkey(xandy) and its 96heartbeatsin order of epoch.data/instance.flat.jsonhas the fieldsthreshold,pubkeysandheartbeats. The keys are 14 words,xthenyof each signer. The heartbeats are one array of words per signer, under"0"to"6", with nine words per heartbeat in the order of the table below. A word is a string of0xand 64 hexadecimal digits. The deployment script reads this file.
The fields of one heartbeat are
| In the contract | In heartbeats.json |
Type | Meaning |
|---|---|---|---|
epoch |
epoch |
uint256 |
epoch e, from 0 to 95 |
messageHash |
msg_hash |
bytes32 |
message hash h_e |
v |
v |
uint8 |
signature: 27 or 28, in the form that ecrecover takes |
r |
r |
uint256 |
signature: 0 < r < N |
s |
s |
uint256 |
signature: 0 < s \le \lfloor N/2 \rfloor |
basePoint.x |
cx |
uint256 |
x-coordinate of C |
basePoint.y |
cy |
uint256 |
y-coordinate of C |
livenessPoint.x |
yx |
uint256 |
x-coordinate of Y |
livenessPoint.y |
yy |
uint256 |
y-coordinate of Y |
In heartbeats.json, msg_hash is a string (0x and 64 hexadecimal digits) and the
other eight fields of a heartbeat are integers written in decimal. The coordinates and
the values r, s exceed 53 bits, so the file must be read with a JSON parser that
keeps integers exact. Python's does; JavaScript's JSON.parse rounds them.
A message hash is an arbitrary 32-byte value: its preimage is not published and is not needed. The record uses h_e directly as the ECDSA hash, read as a big-endian integer, with no prefix and no further hashing.
2.2 External functions
| Function | Who may call it | What it does |
|---|---|---|
publishHeartbeats(slot, beats) |
deployer, before sealing | appends beats to the record of signer slot |
sealRecord() |
deployer, once | freezes the record and emits RecordSealed |
verifyAndExecute(proof, message) |
anyone, after sealing | checks the proof; on success emits ActionExecuted |
signerCount() |
anyone | returns n |
signer(slot), signers() |
anyone | return one public key, or all of them |
heartbeatCount(slot) |
anyone | returns the number of heartbeats of signer slot |
heartbeats(slot), heartbeatAt(slot, index) |
anyone | return all the heartbeats of signer slot, or one of them |
threshold(), deployer(), isSealed() |
anyone | return k, the deployer's address, and whether the record is sealed |
execNonce(), solved(), firstSolver() |
anyone | return the number of accepted proofs, whether there is one, and who sent the first |
executedMessages(messageHash) |
anyone | returns whether the message with this keccak256 hash has been executed |
2.3 Errors
| Error | Raised by | When |
|---|---|---|
InvalidParameters() |
constructor | the threshold is 0 or larger than the number of keys |
InvalidPublicKey(index) |
constructor | key index is not a point of E with 0 < x < N and y < p |
DuplicatePublicKey(index) |
constructor | key index has the x-coordinate of an earlier key |
NotDeployer() |
publishHeartbeats, sealRecord |
the caller is not the deployer |
AlreadySealed() |
publishHeartbeats, sealRecord |
the record is already sealed |
UnknownSlot(slot) |
publishHeartbeats and the views with a slot |
slot is not smaller than n |
NotSealed() |
verifyAndExecute |
the record is not sealed yet |
EmptyMessage() |
verifyAndExecute |
message is empty |
MessageAlreadyExecuted(messageHash) |
verifyAndExecute |
the same message has been executed before |
InvalidThresholdProof() |
verifyAndExecute |
the proof does not verify (section 4.2) |
The functions check these conditions in the order of the table; the constructor checks
the keys one after the other. heartbeatAt with an index out of range reverts without
one of these errors.
3. Curve arithmetic through ecrecover
The verifier of section 4 needs scalar multiplication on secp256k1, for which the EVM has
no instruction. The contract implements it through the precompiled contract ecrecover, in
Secp256k1.sol. The function
linearCombAddress(Q, z, c) returns \mathrm{addr}(zG - cQ), for scalars z, c and a
public key Q. The verifier needs no other curve computation.
4. The threshold proof
4.1 Goal
The caller must prove knowledge of k of the n private keys without revealing which ones. The contract uses the proof of partial knowledge of Cramer, Damgård and Schoenmakers (CRYPTO 1994), made non-interactive with the Fiat-Shamir transform. It needs nothing beyond Schnorr proofs and one polynomial over \mathbb{F}_N.
Recall the Schnorr proof of knowledge of d with Q = dG: the prover sends R = \alpha G, receives a challenge c, and answers z = \alpha + cd; the verifier checks zG - cQ = R. Anyone can produce an accepting triple if they may choose c first: pick c and z at random and set R = zG - cQ. The construction below lets the prover do this for n - k keys and no more.
4.2 Protocol
Let S be the set of the k indices whose keys the prover knows.
-
Commitments. For i \in S, choose \alpha_i at random and set R_i = \alpha_i G. For i \notin S, choose c_i and z_i at random and set R_i = z_i G - c_i Q_i.
-
Root challenge. Compute
\begin{aligned} c_0 = H\big(&\text{tag},\ \text{chain id},\ \text{contract},\ \text{caller},\ \text{execNonce},\ k,\ n,\\ &H(\text{message}),\ H(Q_0,\dots,Q_{n-1}),\\ &H(\mathrm{addr}(R_0),\dots,\mathrm{addr}(R_{n-1}))\big) \bmod N, \end{aligned}with H = keccak256.
-
Polynomial. Let F \in \mathbb{F}_N[X] be the polynomial of degree at most n - k with F(0) = c_0 and F(i+1) = c_i for every i \notin S. These are n - k + 1 conditions, so F exists and is unique.
-
Responses. For i \in S the challenge is now forced: c_i = F(i+1). Answer it honestly with z_i = \alpha_i + c_i d_i.
-
Proof. Send the coefficients a_0,\dots,a_{n-k} of F and the responses z_0,\dots,z_{n-1}.
Verification (CDSThreshold.verify). For each i compute c_i = F(i+1) by
Horner's rule and \mathrm{addr}(z_i G - c_i Q_i) as in section 3. Hash
these addresses as in step 2 and accept if and only if the result equals a_0 = F(0).
The verifier also rejects the case where z_i G - c_i Q_i is
the point at infinity, c_i = 0, z_i = 0 and a_0 = 0, values not reduced modulo
N, and a proof that does not have exactly n - k + 1 coefficients and n responses.
Encoding. The tag is the keccak256 hash of the string MULTISIG_CHALLENGE_CDS. The
ten arguments of the outer hash are 32-byte words, as produced by abi.encode: integers
are big-endian and addresses are padded with zeros on the left. The hash of the keys is
taken over x then y of each key in turn and the hash of the commitments over the n
addresses, again with one 32-byte word per value. The coefficients go into
Proof.polyCoeffs from a_0 up, the responses into Proof.responses in the order of
the keys.
4.3 Why it is sound and hides the subset
Soundness. Take two accepting proofs with the same commitments but different root challenges, with polynomials F and F'. Their difference F - F' is nonzero at 0 and has degree at most n - k, so it vanishes at no more than n - k of the points 1,\dots,n. For each of the remaining indices, at least k of them, c_i \ne c_i' and the two transcripts give the key:
So whoever can answer more than one root challenge knows k keys (special soundness), and in the random oracle model this carries over to the non-interactive proof.
Hiding. For a fixed c_0, the polynomial F is uniformly distributed among those of degree at most n - k with F(0) = c_0, whatever S is, and each triple (R_i, c_i, z_i) has the same distribution whether it was simulated or computed with the key. The proof therefore carries no information about S.
Binding to the caller. The root challenge includes the caller's address, the
contract, the chain id and execNonce. A proof copied from a pending transaction is
invalid for any other sender, and a proof that has been accepted is invalid afterwards
because execNonce has changed.
5. Files
| Part | File |
|---|---|
| Point validation and scalar multiplication | src/crypto/Secp256k1.sol |
| The same computation with explicit point arithmetic | src/crypto/CurveArithmetic.sol |
| Threshold proof verifier | src/crypto/CDSThreshold.sol |
| Contract and record | src/MultisigChallenge.sol |
| Deployment script | script/DeployChallenge.s.sol |
| Published instance | data/heartbeats.json |
| Tests | test/ |