Skip to content

Commit

Permalink
ELIP-001:adjust contract size
Browse files Browse the repository at this point in the history
  • Loading branch information
daring5920 committed Jan 6, 2025
1 parent e806fa8 commit bec0900
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 58 deletions.
82 changes: 44 additions & 38 deletions src/BLSApkRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {IRegistryCoordinator} from "./interfaces/IRegistryCoordinator.sol";

import {BN254} from "./libraries/BN254.sol";

import {BitmapUtils} from "./libraries/BitmapUtils.sol";

contract BLSApkRegistry is BLSApkRegistryStorage {
using BN254 for BN254.G1Point;

Expand Down Expand Up @@ -44,7 +46,7 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
bytes memory quorumNumbers
) public virtual onlyRegistryCoordinator {
// Get the operator's pubkey. Reverts if they have not registered a key
(BN254.G1Point memory pubkey, ) = getRegisteredPubkey(operator);
(BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator);

// Update each quorum's aggregate pubkey
_processQuorumApkUpdate(quorumNumbers, pubkey);
Expand Down Expand Up @@ -89,9 +91,9 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
require(apkHistory[quorumNumber].length == 0, "BLSApkRegistry.initializeQuorum: quorum already exists");

apkHistory[quorumNumber].push(ApkUpdate({
apkHash: bytes24(0),
updateBlockNumber: uint32(block.number),
nextUpdateBlockNumber: 0
apkHash : bytes24(0),
updateBlockNumber : uint32(block.number),
nextUpdateBlockNumber : 0
}));
bytes24[] memory newApkHashes = new bytes24[](1);
newApkHashes[0] = bytes24(0);
Expand Down Expand Up @@ -124,23 +126,23 @@ contract BLSApkRegistry is BLSApkRegistryStorage {

// gamma = h(sigma, P, P', H(m))
uint256 gamma = uint256(keccak256(abi.encodePacked(
params.pubkeyRegistrationSignature.X,
params.pubkeyRegistrationSignature.Y,
params.pubkeyG1.X,
params.pubkeyG1.Y,
params.pubkeyG2.X,
params.pubkeyG2.Y,
pubkeyRegistrationMessageHash.X,
pubkeyRegistrationMessageHash.Y
))) % BN254.FR_MODULUS;
params.pubkeyRegistrationSignature.X,
params.pubkeyRegistrationSignature.Y,
params.pubkeyG1.X,
params.pubkeyG1.Y,
params.pubkeyG2.X,
params.pubkeyG2.Y,
pubkeyRegistrationMessageHash.X,
pubkeyRegistrationMessageHash.Y
))) % BN254.FR_MODULUS;

// e(sigma + P * gamma, [-1]_2) = e(H(m) + [1]_1 * gamma, P')
require(BN254.pairing(
params.pubkeyRegistrationSignature.plus(params.pubkeyG1.scalar_mul(gamma)),
BN254.negGeneratorG2(),
pubkeyRegistrationMessageHash.plus(BN254.generatorG1().scalar_mul(gamma)),
params.pubkeyG2
), "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match");
params.pubkeyRegistrationSignature.plus(params.pubkeyG1.scalar_mul(gamma)),
BN254.negGeneratorG2(),
pubkeyRegistrationMessageHash.plus(BN254.generatorG1().scalar_mul(gamma)),
params.pubkeyG2
), "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match");

operatorToPubkey[operator] = params.pubkeyG1;
operatorToPubkeyHash[operator] = pubkeyHash;
Expand All @@ -150,19 +152,27 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
return pubkeyHash;
}

function updateBLSPublicKey(PubkeyRegistrationParams calldata params) external override returns (bytes32 operatorId) {
address operator = msg.sender;
bytes32 operatorId = getOperatorId(operator);
require(operatorId != bytes32(0), "BLSApkRegistry.updateBLSPublicKey: operator is not registered");
IRegistryCoordinator __registryCoordinator = IRegistryCoordinator(registryCoordinator);
uint192 currentBitmap = __registryCoordinator.getCurrentQuorumBitmap(operatorId);
bytes memory quorumNumbers = BitmapUtils.bitmapToBytesArray(currentBitmap);
BN254.G1Point memory pubkeyRegistrationMessageHash = __registryCoordinator.pubkeyRegistrationMessageHash(operator);
return _updateBLSPublicKey(operator, quorumNumbers, params, pubkeyRegistrationMessageHash);
}

/**
* @notice Called by the RegistryCoordinator update an operator as the owner of a BLS public key.
* @param operator is the operator for whom the key is being registered
* @param params contains the G1 & G2 public keys of the operator, and a signature proving their ownership
* @param pubkeyRegistrationMessageHash is a hash that the operator must sign to prove key ownership
*/
function updateBLSPublicKey(
/*******************************************************************************
INTERNAL FUNCTIONS
*******************************************************************************/

function _updateBLSPublicKey(
address operator,
bytes memory quorumNumbers,
PubkeyRegistrationParams calldata params,
BN254.G1Point calldata pubkeyRegistrationMessageHash
) external onlyRegistryCoordinator returns (bytes32 operatorId) {
BN254.G1Point memory pubkeyRegistrationMessageHash
) internal returns (bytes32 operatorId) {
bytes32 pubkeyHash = BN254.hashG1Point(params.pubkeyG1);
require(
pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.updateBLSPublicKey: cannot update zero pubkey"
Expand Down Expand Up @@ -202,10 +212,6 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
return pubkeyHash;
}

/*******************************************************************************
INTERNAL FUNCTIONS
*******************************************************************************/

function _processQuorumApkUpdate(bytes memory quorumNumbers, BN254.G1Point memory point) internal {
BN254.G1Point memory newApk;
bytes24[] memory newApkHashes = new bytes24[](quorumNumbers.length);
Expand All @@ -230,9 +236,9 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
} else {
lastUpdate.nextUpdateBlockNumber = uint32(block.number);
apkHistory[quorumNumber].push(ApkUpdate({
apkHash: newApkHash,
updateBlockNumber: uint32(block.number),
nextUpdateBlockNumber: 0
apkHash : newApkHash,
updateBlockNumber : uint32(block.number),
nextUpdateBlockNumber : 0
}));
}
}
Expand All @@ -254,7 +260,7 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
pubkeyHash != bytes32(0),
"BLSApkRegistry.getRegisteredPubkey: operator is not registered"
);

return (pubkey, pubkeyHash);
}

Expand All @@ -267,10 +273,10 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
uint256 blockNumber
) external view returns (uint32[] memory) {
uint32[] memory indices = new uint32[](quorumNumbers.length);

for (uint256 i = 0; i < quorumNumbers.length; i++) {
uint8 quorumNumber = uint8(quorumNumbers[i]);

uint256 quorumApkUpdatesLength = apkHistory[quorumNumber].length;
if (quorumApkUpdatesLength == 0 || blockNumber < apkHistory[quorumNumber][0].updateBlockNumber) {
revert("BLSApkRegistry.getApkIndicesAtBlockNumber: blockNumber is before the first update");
Expand Down
8 changes: 0 additions & 8 deletions src/RegistryCoordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,4 @@ contract RegistryCoordinator is
return OwnableUpgradeable.owner();
}

function updateBLSPublicKey(IBLSApkRegistry.PubkeyRegistrationParams calldata params) external override {
address operator = msg.sender;
bytes32 operatorId = blsApkRegistry.getOperatorId(operator);
require(operatorId != bytes32(0), "RegistryCoordinator.updateBLSPublicKey: operator is not registered");
uint192 currentBitmap = _currentOperatorBitmap(operatorId);
bytes memory quorumsToUpdate = BitmapUtils.bitmapToBytesArray(currentBitmap);
blsApkRegistry.updateBLSPublicKey(operator, quorumsToUpdate, params, pubkeyRegistrationMessageHash(operator));
}
}
11 changes: 1 addition & 10 deletions src/interfaces/IBLSApkRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,8 @@ interface IBLSApkRegistry is IRegistry {
BN254.G1Point calldata pubkeyRegistrationMessageHash
) external returns (bytes32 operatorId);

/**
* @notice Called by the RegistryCoordinator update an operator as the owner of a BLS public key.
* @param operator is the operator for whom the key is being registered
* @param params contains the G1 & G2 public keys of the operator, and a signature proving their ownership
* @param pubkeyRegistrationMessageHash is a hash that the operator must sign to prove key ownership
*/
function updateBLSPublicKey(
address operator,
bytes memory quorumNumbers,
PubkeyRegistrationParams calldata params,
BN254.G1Point calldata pubkeyRegistrationMessageHash
PubkeyRegistrationParams calldata params
) external returns (bytes32 operatorId);

/**
Expand Down
2 changes: 0 additions & 2 deletions src/interfaces/IRegistryCoordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,4 @@ interface IRegistryCoordinator {
/// @notice The owner of the registry coordinator
function owner() external view returns (address);

function updateBLSPublicKey(IBLSApkRegistry.PubkeyRegistrationParams calldata params) external;

}
1 change: 1 addition & 0 deletions src/unaudited/ECDSAStakeRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ contract ECDSAStakeRegistry is

/// @notice Registers a new operator using a provided signature
/// @param _operatorSignature Contains the operator's signature, salt, and expiry
/// @param _signingKey The signing key to add to the operator's history
function registerOperatorWithSignature(
ISignatureUtils.SignatureWithSaltAndExpiry memory _operatorSignature,
address _signingKey
Expand Down

0 comments on commit bec0900

Please sign in to comment.