diff --git a/src/IValidatorRegistry.sol b/src/IValidatorRegistry.sol index 8a302a6..2bf166f 100644 --- a/src/IValidatorRegistry.sol +++ b/src/IValidatorRegistry.sol @@ -1,89 +1,89 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.16; - -import {IScribe} from "scribe/IScribe.sol"; -import {LibSecp256k1} from "scribe/libs/LibSecp256k1.sol"; - -interface IValidatorRegistry { - /// @notice Thrown if validator `validator` not lifted. - error ValidatorNotLifted(address validator); - - /// @notice Thrown if validator id `validatorId` not lifted. - error ValidatorIdNotLifted(uint8 validatorId); - - /// @notice Emitted when new validator lifted. - /// @param caller The caller's address. - /// @param validator The validator address lifted. - event ValidatorLifted(address indexed caller, address indexed validator); - - /// @notice Emitted when validator dropped. - /// @param caller The caller's address. - /// @param validator The validator address dropped. - event ValidatorDropped(address indexed caller, address indexed validator); - - // -- Public Read Functions -- - - /// @notice Returns the Chronicle Validator Registration Message V2 for - /// public key `pubKey`. - /// - /// @dev The Chronicle Validator Registration Message V2 acts as a proof of - /// possession scheme proving a validator possesses the private key of - /// its publicly claimed public key. The proof of possession defends - /// the Scribe oracle system against rogue key attacks. - /// - /// A validator MUST provide a valid Chronicle Validator Registration - /// Message V2 in order to be lifted on this registry. - /// - /// Note that the proof of possession scheme integrated into the Scribe - /// smart contract, ie the Chronicle Feed Registration Message, is - /// vulnerable to an "extended" rogue key attack. - /// - /// For more info, see https://github.com/chronicleprotocol/scribe/blob/main/audits/Cantina%40v2.0.0_2.pdf - /// - /// @return The Chronicle Validator Registration Message V2 for given public - /// key. - function constructValidatorRegistrationMessageV2( - LibSecp256k1.Point memory pubKey - ) external view returns (bytes32); - - /// @notice Returns all of Chronicle Protocol's validators. - /// @return Chronicle Protocol's validators. - function validators() external view returns (address[] memory); - - /// @notice Returns whether address `validator` is a validator. - /// @param validator Validator address. - /// @return True if address `validator` is validator, false otherwise. - function validators(address validator) external view returns (bool); - - // -- Auth'ed Functionality -- - - /// @notice Lifts validator with public key `pubKey`. - /// @dev Only callable by auth'ed address. - /// @param pubKey The validator's public key. - /// @param sig The validator's Chronicle Validator Registration Message V2 - /// signature. - function lift( - LibSecp256k1.Point memory pubKey, - IScribe.ECDSAData memory sig - ) external; - - /// @notice Lifts validators with public keys `pubKeys`. - /// @dev Only callable by auth'ed address. - /// @param pubKeys The validators' public keys. - /// @param sigs The validator's Chronicle Validator Registration Message V2 - /// signature. - function lift( - LibSecp256k1.Point[] memory pubKeys, - IScribe.ECDSAData[] memory sigs - ) external; - - /// @notice Drops validator with validator id `validatorId`. - /// @dev Only callable by auth'ed address. - /// @param validatorId The validator id to drop. - function drop(uint8 validatorId) external; - - /// @notice Drops validators with validator ids `validatorIds`. - /// @dev Only callable by auth'ed address. - /// @param validatorIds The validator ids to drop. - function drop(uint8[] memory validatorIds) external; -} +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.16; + +import {IScribe} from "scribe/IScribe.sol"; +import {LibSecp256k1} from "scribe/libs/LibSecp256k1.sol"; + +interface IValidatorRegistry { + /// @notice Thrown if validator `validator` not lifted. + error ValidatorNotLifted(address validator); + + /// @notice Thrown if validator id `validatorId` not lifted. + error ValidatorIdNotLifted(uint8 validatorId); + + /// @notice Emitted when new validator lifted. + /// @param caller The caller's address. + /// @param validator The validator address lifted. + event ValidatorLifted(address indexed caller, address indexed validator); + + /// @notice Emitted when validator dropped. + /// @param caller The caller's address. + /// @param validator The validator address dropped. + event ValidatorDropped(address indexed caller, address indexed validator); + + // -- Public Read Functions -- + + /// @notice Returns the Chronicle Validator Registration Message V2 for + /// public key `pubKey`. + /// + /// @dev The Chronicle Validator Registration Message V2 acts as a proof of + /// possession scheme proving a validator possesses the private key of + /// its publicly claimed public key. The proof of possession defends + /// the Scribe oracle system against rogue key attacks. + /// + /// A validator MUST provide a valid Chronicle Validator Registration + /// Message V2 in order to be lifted on this registry. + /// + /// Note that the proof of possession scheme integrated into the Scribe + /// smart contract, ie the Chronicle Feed Registration Message, is + /// vulnerable to an "extended" rogue key attack. + /// + /// For more info, see https://github.com/chronicleprotocol/scribe/blob/main/audits/Cantina%40v2.0.0_2.pdf + /// + /// @return The Chronicle Validator Registration Message V2 for given public + /// key. + function constructValidatorRegistrationMessageV2( + LibSecp256k1.Point memory pubKey + ) external view returns (bytes32); + + /// @notice Returns all of Chronicle Protocol's validators. + /// @return Chronicle Protocol's validators. + function validators() external view returns (address[] memory); + + /// @notice Returns whether address `validator` is a validator. + /// @param validator Validator address. + /// @return True if address `validator` is validator, false otherwise. + function validators(address validator) external view returns (bool); + + // -- Auth'ed Functionality -- + + /// @notice Lifts validator with public key `pubKey`. + /// @dev Only callable by auth'ed address. + /// @param pubKey The validator's public key. + /// @param sig The validator's Chronicle Validator Registration Message V2 + /// signature. + function lift( + LibSecp256k1.Point memory pubKey, + IScribe.ECDSAData memory sig + ) external; + + /// @notice Lifts validators with public keys `pubKeys`. + /// @dev Only callable by auth'ed address. + /// @param pubKeys The validators' public keys. + /// @param sigs The validator's Chronicle Validator Registration Message V2 + /// signature. + function lift( + LibSecp256k1.Point[] memory pubKeys, + IScribe.ECDSAData[] memory sigs + ) external; + + /// @notice Drops validator with validator id `validatorId`. + /// @dev Only callable by auth'ed address. + /// @param validatorId The validator id to drop. + function drop(uint8 validatorId) external; + + /// @notice Drops validators with validator ids `validatorIds`. + /// @dev Only callable by auth'ed address. + /// @param validatorIds The validator ids to drop. + function drop(uint8[] memory validatorIds) external; +} diff --git a/src/ValidatorRegistry.sol b/src/ValidatorRegistry.sol index 21eab13..7c8ba30 100644 --- a/src/ValidatorRegistry.sol +++ b/src/ValidatorRegistry.sol @@ -1,193 +1,229 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.16; - -import {Auth} from "chronicle-std/auth/Auth.sol"; - -import {IScribe} from "scribe/IScribe.sol"; -import {LibSecp256k1} from "scribe/libs/LibSecp256k1.sol"; - -import {IValidatorRegistry} from "./IValidatorRegistry.sol"; - -/** - * @title ValidatorRegistry - * @custom:version 2.0.0 - * - * @notice Single source of truth for all of Chronicle Protocol's validators - * - * @dev This contract provides a registry for validators via 1-byte validator - * ids. - * - * A validators validator id is computed as the highest-order byte of the - * validators address: - * - * uint8 validatorId = uint8(uint(uint160(validator)) >> 152); - * - * Due to validator ids being 1 byte, the maximum number of validators - * supported is 256. - * - * Note that a set of lifted validators can be encoded in a single uint. - * The code refers to it as `uint bloom`. - * - * @dev Due to a vulnerability in Scribe this registry also implements a proof - * of possession to defend against rogue key attacks. - * - * The proof of possession is an ECDSA signature signing a message derived - * from the validator's public key, the Chronicle Validator Registration - * Message V2. - * - * Without a valid ECDSA signature a validator cannot be lifted! - * - * @author Chronicle Labs, Inc - * @custom:security-contact security@chroniclelabs.org - */ -contract ValidatorRegistry is IValidatorRegistry, Auth { - using LibSecp256k1 for LibSecp256k1.Point; - - /// @dev The prime over which the secp256k1 curve is defined. - /// - /// Defines the upper bound for a secp256k1 field element. - uint private constant _SECP256K1_P = - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F; - - /// @dev The domain separating context string of the Chronicle Validator - /// Registration V2 message. - string private constant _CONTEXT = "Chronicle Validator Registration V2"; - - /// @dev Statically allocated array for validators. - /// Indexed via validator's validator id. - address[256] internal _validators; - - constructor(address initialAuthed) Auth(initialAuthed) {} - - /// @inheritdoc IValidatorRegistry - function constructValidatorRegistrationMessageV2( - LibSecp256k1.Point memory pubKey - ) public pure returns (bytes32) { - return keccak256( - abi.encodePacked( - "\x19Ethereum Signed Message:\n32", - keccak256(abi.encodePacked(_CONTEXT, pubKey.x, pubKey.y)) - ) - ); - } - - /// @inheritdoc IValidatorRegistry - function validators() external view returns (address[] memory) { - address[] memory validators_ = new address[](256); - uint ctr; - for (uint i; i < 256; i++) { - address validator = _validators[i]; - - if (validator == address(0)) { - continue; - } - - validators_[ctr++] = validator; - } - - assembly ("memory-safe") { - mstore(validators_, ctr) - } - - return validators_; - } - - /// @inheritdoc IValidatorRegistry - function validators(address validator) public view returns (bool) { - if (validator == address(0)) { - return false; - } - - uint8 validatorId = uint8(uint(uint160(validator)) >> 152); - - return _validators[validatorId] == validator; - } - - // -- Auth'ed Functionality -- - - /// @inheritdoc IValidatorRegistry - function lift( - LibSecp256k1.Point memory pubKey, - IScribe.ECDSAData memory sig - ) external auth { - _lift(pubKey, sig); - } - - /// @inheritdoc IValidatorRegistry - function lift( - LibSecp256k1.Point[] memory pubKeys, - IScribe.ECDSAData[] memory sigs - ) external auth { - require(pubKeys.length == sigs.length); - - for (uint i; i < pubKeys.length; i++) { - _lift(pubKeys[i], sigs[i]); - } - } - - /// @inheritdoc IValidatorRegistry - function drop(uint8 validatorId) external auth { - _drop(validatorId); - } - - /// @inheritdoc IValidatorRegistry - function drop(uint8[] memory validatorIds) external auth { - for (uint i; i < validatorIds.length; i++) { - _drop(validatorIds[i]); - } - } - - // -- Internal Helpers -- - - function _lift( - LibSecp256k1.Point memory pubKey, - IScribe.ECDSAData memory feedRegistrationSigV2 - ) internal { - address validator = pubKey.toAddress(); - // assert(validator != address(0)); - - // Verify public key's coordinates are field elements. - require(pubKey.x < _SECP256K1_P); - require(pubKey.y < _SECP256K1_P); - - // Verify public key is valid. - require(pubKey.isOnCurve()); - - // Verify feed registration signature v2 is valid. - address signer = ecrecover( - constructValidatorRegistrationMessageV2(pubKey), - feedRegistrationSigV2.v, - feedRegistrationSigV2.r, - feedRegistrationSigV2.s - ); - require(validator == signer); - - uint8 validatorId = uint8(uint(uint160(validator)) >> 152); - - address current = _validators[validatorId]; - if (current != validator) { - require(current == address(0)); - - _validators[validatorId] = validator; - emit ValidatorLifted(msg.sender, validator); - } - } - - function _drop(uint8 validatorId) internal { - address current = _validators[validatorId]; - if (current != address(0)) { - delete _validators[validatorId]; - emit ValidatorDropped(msg.sender, current); - } - } -} - -/** - * @dev Contract overwrite to deploy contract instances with specific naming. - * - * For more info, see docs/Deployment.md. - */ -contract ValidatorRegistry_COUNTER is ValidatorRegistry { - // @todo ^^^^^^^ Adjust counter of ValidatorRegistry instance. - constructor(address initialAuthed) ValidatorRegistry(initialAuthed) {} -} +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.16; + +import {Auth} from "chronicle-std/auth/Auth.sol"; + +import {IScribe} from "scribe/IScribe.sol"; +import {LibSecp256k1} from "scribe/libs/LibSecp256k1.sol"; + +import {IValidatorRegistry} from "./IValidatorRegistry.sol"; + +/** + * @title ValidatorRegistry + * @custom:version 2.0.0 + * + * @notice Single source of truth for all of Chronicle Protocol's validators + * + * @dev This contract provides a registry for validators via 1-byte validator + * ids. + * + * A validators validator id is computed as the highest-order byte of the + * validators address: + * + * uint8 validatorId = uint8(uint(uint160(validator)) >> 152); + * + * Due to validator ids being 1 byte, the maximum number of validators + * supported is 256. + * + * Note that a set of lifted validators can be encoded in a single uint. + * The code refers to it as `uint bloom`. + * + * @dev Due to a vulnerability in Scribe this registry also implements a proof + * of possession to defend against rogue key attacks. + * + * The proof of possession is an ECDSA signature signing a message derived + * from the validator's public key, the Chronicle Validator Registration + * Message V2. + * + * Without a valid ECDSA signature a validator cannot be lifted! + * + * @author Chronicle Labs, Inc + * @custom:security-contact security@chroniclelabs.org + */ +contract ValidatorRegistry is IValidatorRegistry, Auth { + using LibSecp256k1 for LibSecp256k1.Point; + + /// @dev The prime over which the secp256k1 curve is defined. + /// + /// Defines the upper bound for a secp256k1 field element. + uint private constant _SECP256K1_P = + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F; + + /// @dev The domain separating context string of the Chronicle Validator + /// Registration V2 message. + string private constant _CONTEXT = "Chronicle Validator Registration V2"; + + /// @dev Statically allocated array for validators. + /// Indexed via validator's validator id. + address[256] internal _validators; + + constructor(address initialAuthed) Auth(initialAuthed) {} + + /// @inheritdoc IValidatorRegistry + function constructValidatorRegistrationMessageV2( + LibSecp256k1.Point memory pubKey + ) public view returns (bytes32) { + // Note the domain separation via `address(this)` and `block.chainid`. + // + // Without them the signed payload is fully determined by the public key + // alone, so a proof-of-possession signature is valid on every chain and + // for every registry deployment. An observer who captures one signature + // could then present it to any other ValidatorRegistry to have that + // validator id registered without ever holding the private key at the + // time of that registry's own registration process. Binding the message + // to this registry and chain prevents that replay. + return keccak256( + abi.encodePacked( + "\x19Ethereum Signed Message:\n32", + keccak256( + abi.encodePacked( + address(this), + block.chainid, + _CONTEXT, + pubKey.x, + pubKey.y + ) + ) + ) + ); + } + + /// @inheritdoc IValidatorRegistry + function validators() external view returns (address[] memory) { + address[] memory validators_ = new address[](256); + uint ctr; + for (uint i; i < 256; i++) { + address validator = _validators[i]; + + if (validator == address(0)) { + continue; + } + + validators_[ctr++] = validator; + } + + assembly ("memory-safe") { + mstore(validators_, ctr) + } + + return validators_; + } + + /// @inheritdoc IValidatorRegistry + function validators(address validator) public view returns (bool) { + if (validator == address(0)) { + return false; + } + + uint8 validatorId = uint8(uint(uint160(validator)) >> 152); + + return _validators[validatorId] == validator; + } + + // -- Auth'ed Functionality -- + + /// @inheritdoc IValidatorRegistry + function lift( + LibSecp256k1.Point memory pubKey, + IScribe.ECDSAData memory sig + ) external auth { + _lift(pubKey, sig); + } + + /// @inheritdoc IValidatorRegistry + function lift( + LibSecp256k1.Point[] memory pubKeys, + IScribe.ECDSAData[] memory sigs + ) external auth { + require(pubKeys.length == sigs.length); + + for (uint i; i < pubKeys.length; i++) { + _lift(pubKeys[i], sigs[i]); + } + } + + /// @inheritdoc IValidatorRegistry + function drop(uint8 validatorId) external auth { + _drop(validatorId); + } + + /// @inheritdoc IValidatorRegistry + function drop(uint8[] memory validatorIds) external auth { + for (uint i; i < validatorIds.length; i++) { + _drop(validatorIds[i]); + } + } + + // -- Internal Helpers -- + + function _lift( + LibSecp256k1.Point memory pubKey, + IScribe.ECDSAData memory feedRegistrationSigV2 + ) internal { + address validator = pubKey.toAddress(); + + // Verify public key's coordinates are field elements. + require(pubKey.x < _SECP256K1_P); + require(pubKey.y < _SECP256K1_P); + + // Verify public key is valid. + require(pubKey.isOnCurve()); + + // Reject signature malleability. + // + // `ecrecover` accepts both (r, s) and (r, Q - s) for the same signer, so + // without these checks a single valid proof of possession has two + // distinct on-chain encodings. That breaks any off-chain uniqueness + // assumption (e.g. de-duplicating registrations by signature bytes) and + // is the standard defence-in-depth guard required for ECDSA verification. + // + // Note that `ecrecover` itself reverts on r == 0 or s == 0, and returns + // address(0) on any other invalid input, which the `signer` check below + // already rejects because `validator` is never address(0) here. + require( + feedRegistrationSigV2.s + <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, + "ValidatorRegistry: signature s not canonical" + ); + + // Verify feed registration signature v2 is valid. + address signer = ecrecover( + constructValidatorRegistrationMessageV2(pubKey), + feedRegistrationSigV2.v, + feedRegistrationSigV2.r, + feedRegistrationSigV2.s + ); + require(validator == signer); + + uint8 validatorId = uint8(uint(uint160(validator)) >> 152); + + address current = _validators[validatorId]; + if (current != validator) { + // Reject rather than silently overwriting: an occupied slot means a + // different validator already holds this id. Overwriting it would + // silently evict a live validator. + require(current == address(0), "ValidatorRegistry: id occupied"); + + _validators[validatorId] = validator; + emit ValidatorLifted(msg.sender, validator); + } + } + + function _drop(uint8 validatorId) internal { + address current = _validators[validatorId]; + if (current != address(0)) { + delete _validators[validatorId]; + emit ValidatorDropped(msg.sender, current); + } + } +} + +/** + * @dev Contract overwrite to deploy contract instances with specific naming. + * + * For more info, see docs/Deployment.md. + */ +contract ValidatorRegistry_COUNTER is ValidatorRegistry { + // @todo ^^^^^^^ Adjust counter of ValidatorRegistry instance. + constructor(address initialAuthed) ValidatorRegistry(initialAuthed) {} +}