// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; import {Secp256k1} from "./Secp256k1.sol"; // import {CurveArithmetic} from "./CurveArithmetic.sol"; /// @title CDSThreshold /// @notice Verifier for a non-interactive Cramer-Damgard-Schoenmakers (CDS) k-of-n /// threshold Schnorr proof over secp256k1. /// @dev Proves knowledge of the private keys of at least `k` of the `n` public keys /// `Q[0..n-1]` without revealing which subset was used. The prover ties the `n` /// Schnorr proofs together with a degree-`(n-k)` polynomial `F` over F_N such that /// `c_i = F(i+1)` is each slot's challenge and `F(0)` is the Fiat-Shamir root /// challenge. Because the hash fixes `F(0)`, a cheating prover can freely pick only /// `n-k` of the `n` challenges, so the remaining `k` must be answered with real keys. /// /// Verification per slot is one `ecrecover` call (`R_i = z_i*G - c_i*Q_i`), and the /// polynomial is evaluated by Horner's rule, so no modular inverse is ever needed. library CDSThreshold { bytes32 internal constant DOMAIN_CDS = keccak256("MULTISIG_CHALLENGE_CDS"); /// @param polyCoeffs `[a_0, ..., a_{n-k}]` of `F(X)` over F_N; `a_0 = F(0)` is the root. /// @param responses `[z_0, ..., z_{n-1}]`, aligned with the public-key ordering. struct Proof { uint256[] polyCoeffs; uint256[] responses; } /// @param caller Bound into the challenge, so a copied proof fails for a new sender. /// @param execNonce Monotone counter, so a proof cannot be replayed after execution. struct Context { uint256 chainId; address verifyingContract; address caller; uint256 execNonce; uint256 threshold; bytes32 messageHash; } /// @notice Verify a k-of-n CDS proof against the ordered public keys `Q`. function verify(Secp256k1.Point[] memory Q, Proof memory proof, Context memory ctx) internal pure returns (bool) { uint256 n = Q.length; uint256 k = ctx.threshold; if (k == 0 || n < k) return false; uint256 numCoeffs = n - k + 1; if (proof.polyCoeffs.length != numCoeffs || proof.responses.length != n) return false; uint256 root = proof.polyCoeffs[0]; if (root == 0 || root >= Secp256k1.N) return false; for (uint256 j = 1; j < numCoeffs; ++j) { if (proof.polyCoeffs[j] >= Secp256k1.N) return false; } // Rebuild each slot's commitment address R_i = addr(z_i*G - c_i*Q_i). uint256[] memory commitments = new uint256[](n); uint256 degree = numCoeffs - 1; for (uint256 i = 0; i < n; ++i) { if (!Secp256k1.isValidRecoverablePoint(Q[i])) return false; uint256 zi = proof.responses[i]; if (zi == 0 || zi >= Secp256k1.N) return false; uint256 ci = _evalPoly(proof.polyCoeffs, degree, i + 1); if (ci == 0) return false; // R_i = z_i*G - c_i*Q_i, through `ecrecover`. The commented line computes the // same address with explicit point arithmetic. It does not depend on the // curve being secp256k1, but a 4-of-7 verification then costs about 8.7M gas // instead of 180k. To use it, swap the two lines and uncomment the import // at the top of this file. address r = Secp256k1.linearCombAddress(Q[i], zi, ci); // address r = CurveArithmetic.linearCombAddress(Q[i], zi, ci); if (r == address(0)) return false; commitments[i] = uint256(uint160(r)); } // Recompute the Fiat-Shamir root and require it to equal F(0). uint256 expectedRoot = _rootChallenge(ctx, n, _hashPoints(Q), keccak256(abi.encodePacked(commitments))); return expectedRoot != 0 && expectedRoot == root; } /// @dev Fiat-Shamir root challenge `F(0) = H(context, message, keys, commitments) mod N`. function _rootChallenge( Context memory ctx, uint256 n, bytes32 keysHash, bytes32 commitmentsHash ) private pure returns (uint256) { bytes memory encoded = abi.encode( DOMAIN_CDS, ctx.chainId, ctx.verifyingContract, ctx.caller, ctx.execNonce, ctx.threshold, n, ctx.messageHash, keysHash, commitmentsHash ); return uint256(keccak256(encoded)) % Secp256k1.N; } /// @dev Evaluate `F(x) = sum_j coeffs[j] * x^j (mod N)` by backwards Horner. function _evalPoly(uint256[] memory coeffs, uint256 degree, uint256 x) private pure returns (uint256 acc) { acc = coeffs[degree]; for (uint256 j = degree; j > 0; --j) { acc = addmod(mulmod(acc, x, Secp256k1.N), coeffs[j - 1], Secp256k1.N); } } /// @dev `keccak256` of the packed `(x, y)` coordinates of every point (32 bytes each). function _hashPoints(Secp256k1.Point[] memory pts) private pure returns (bytes32) { uint256 len = pts.length; uint256[] memory coords = new uint256[](len * 2); for (uint256 i = 0; i < len; ++i) { coords[2 * i] = pts[i].x; coords[2 * i + 1] = pts[i].y; } return keccak256(abi.encodePacked(coords)); } }