// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; import {Secp256k1} from "./crypto/Secp256k1.sol"; import {CDSThreshold} from "./crypto/CDSThreshold.sol"; /// @title MultisigChallenge /// @notice A k-of-n threshold challenge on secp256k1. /// @dev The contract holds `n` signer public keys and a record of their heartbeats (one /// ECDSA signature and one liveness point per signer and epoch). The deployer uploads /// the record and seals it. To win, a challenger calls {verifyAndExecute} with a /// zero-knowledge proof of knowing at least `k` of the private keys; the proof does /// not reveal which keys, and the keys can only be obtained by analysing the record. contract MultisigChallenge { /// @notice One published heartbeat: an epoch-bound ECDSA signature and a liveness point. /// @dev `livenessPoint` is `[d] basePoint` for the signer's private key `d`. The /// contract stores heartbeats as plain data and does not check them. struct Heartbeat { uint256 epoch; bytes32 messageHash; uint8 v; uint256 r; uint256 s; Secp256k1.Point basePoint; Secp256k1.Point livenessPoint; } uint256 public immutable threshold; // k address public immutable deployer; Secp256k1.Point[] private _signers; // the n fixed public keys mapping(uint256 => Heartbeat[]) private _history; // slot => chronological heartbeats bool public isSealed; // once true, the published record is immutable uint256 public execNonce; bool public solved; address public firstSolver; mapping(bytes32 => bool) public executedMessages; event RecordSealed(uint256 signerCount); event ActionExecuted(address indexed caller, uint256 indexed execNonce, bytes32 messageHash); error InvalidParameters(); error InvalidPublicKey(uint256 index); error DuplicatePublicKey(uint256 index); error NotDeployer(); error AlreadySealed(); error NotSealed(); error UnknownSlot(uint256 slot); error EmptyMessage(); error MessageAlreadyExecuted(bytes32 messageHash); error InvalidThresholdProof(); modifier onlyDeployer() { if (msg.sender != deployer) revert NotDeployer(); _; } /// @param signers_ The `n` fixed signer public keys (must be valid and distinct in x). /// @param threshold_ The number `k` of keys a winning proof must demonstrate knowledge of. constructor(Secp256k1.Point[] memory signers_, uint256 threshold_) { uint256 n = signers_.length; if (threshold_ == 0 || threshold_ > n) revert InvalidParameters(); deployer = msg.sender; threshold = threshold_; for (uint256 i = 0; i < n; ++i) { if (!Secp256k1.isValidRecoverablePoint(signers_[i])) revert InvalidPublicKey(i); // Distinct x-coordinates reject both a duplicate key Q and its negation -Q. for (uint256 j = 0; j < i; ++j) { if (signers_[j].x == signers_[i].x) revert DuplicatePublicKey(i); } _signers.push(signers_[i]); } } /// @notice Append heartbeats to the record of signer `slot`. Deployer only, before sealing. function publishHeartbeats(uint256 slot, Heartbeat[] calldata beats) external onlyDeployer { if (isSealed) revert AlreadySealed(); if (slot >= _signers.length) revert UnknownSlot(slot); Heartbeat[] storage history = _history[slot]; for (uint256 i = 0; i < beats.length; ++i) { history.push(beats[i]); } } /// @notice Freeze the record. Proofs are accepted only after this. function sealRecord() external onlyDeployer { if (isSealed) revert AlreadySealed(); isSealed = true; emit RecordSealed(_signers.length); } /// @notice Check a proof of knowledge of `k` of the signer keys and record the solve. /// @dev The proof must have been built for `msg.sender` and the current `execNonce`, so /// it is useless to anyone who copies it and cannot be submitted twice. On success /// the message is marked as executed and the first solver is recorded. function verifyAndExecute(CDSThreshold.Proof calldata proof, bytes calldata message) external { if (!isSealed) revert NotSealed(); if (message.length == 0) revert EmptyMessage(); bytes32 messageHash = keccak256(message); if (executedMessages[messageHash]) revert MessageAlreadyExecuted(messageHash); CDSThreshold.Context memory ctx = CDSThreshold.Context({ chainId: block.chainid, verifyingContract: address(this), caller: msg.sender, execNonce: execNonce, threshold: threshold, messageHash: messageHash }); if (!CDSThreshold.verify(_signers, proof, ctx)) revert InvalidThresholdProof(); executedMessages[messageHash] = true; if (!solved) { solved = true; firstSolver = msg.sender; } emit ActionExecuted(msg.sender, execNonce++, messageHash); } function signerCount() external view returns (uint256) { return _signers.length; } function signer(uint256 slot) external view returns (Secp256k1.Point memory) { if (slot >= _signers.length) revert UnknownSlot(slot); return _signers[slot]; } function signers() external view returns (Secp256k1.Point[] memory) { return _signers; } function heartbeatCount(uint256 slot) external view returns (uint256) { if (slot >= _signers.length) revert UnknownSlot(slot); return _history[slot].length; } function heartbeats(uint256 slot) external view returns (Heartbeat[] memory) { if (slot >= _signers.length) revert UnknownSlot(slot); return _history[slot]; } function heartbeatAt(uint256 slot, uint256 index) external view returns (Heartbeat memory) { if (slot >= _signers.length) revert UnknownSlot(slot); return _history[slot][index]; } }