Skip to content

Commit

Permalink
chore: forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
0xClandestine committed Jan 7, 2025
1 parent 3488b39 commit b757d5a
Show file tree
Hide file tree
Showing 99 changed files with 6,477 additions and 4,438 deletions.
16 changes: 10 additions & 6 deletions script/utils/OperatorSetUpgradeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pragma solidity ^0.8.0;
// Deploy L2AVS proxy

import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {TransparentUpgradeableProxy} from
"@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";

import {Vm} from "forge-std/Vm.sol";
Expand All @@ -27,8 +28,7 @@ library OperatorSetUpgradeLib {
* This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1.
*/
bytes32 internal constant ADMIN_SLOT =
0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

function upgrade(address proxy, address implementation, bytes memory data) internal {
ProxyAdmin admin = ProxyAdmin(getAdmin(proxy));
Expand All @@ -40,13 +40,17 @@ library OperatorSetUpgradeLib {
admin.upgrade(TransparentUpgradeableProxy(payable(proxy)), implementation);
}

function getAdmin(address proxy) internal view returns (address) {
function getAdmin(
address proxy
) internal view returns (address) {
bytes32 value = vm.load(proxy, ADMIN_SLOT);
return address(uint160(uint256(value)));
}

function getImplementation(address proxy) internal view returns (address) {
function getImplementation(
address proxy
) internal view returns (address) {
bytes32 value = vm.load(proxy, IMPLEMENTATION_SLOT);
return address(uint160(uint256(value)));
}
}
}
152 changes: 99 additions & 53 deletions src/BLSApkRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
IRegistryCoordinator _registryCoordinator
) BLSApkRegistryStorage(_registryCoordinator) {}

/*******************************************************************************
EXTERNAL FUNCTIONS - REGISTRY COORDINATOR
*******************************************************************************/
/**
*
* EXTERNAL FUNCTIONS - REGISTRY COORDINATOR
*
*/

/**
* @notice Registers the `operator`'s pubkey for the specified `quorumNumbers`.
Expand All @@ -41,7 +43,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 All @@ -67,7 +69,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.negate());
Expand All @@ -78,14 +80,21 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
* @notice Initializes a new quorum by pushing its first apk update
* @param quorumNumber The number of the new quorum
*/
function initializeQuorum(uint8 quorumNumber) public virtual onlyRegistryCoordinator {
require(apkHistory[quorumNumber].length == 0, "BLSApkRegistry.initializeQuorum: quorum already exists");

apkHistory[quorumNumber].push(ApkUpdate({
apkHash: bytes24(0),
updateBlockNumber: uint32(block.number),
nextUpdateBlockNumber: 0
}));
function initializeQuorum(
uint8 quorumNumber
) public virtual onlyRegistryCoordinator {
require(
apkHistory[quorumNumber].length == 0,
"BLSApkRegistry.initializeQuorum: quorum already exists"
);

apkHistory[quorumNumber].push(
ApkUpdate({
apkHash: bytes24(0),
updateBlockNumber: uint32(block.number),
nextUpdateBlockNumber: 0
})
);
}

/**
Expand All @@ -101,7 +110,8 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
) external onlyRegistryCoordinator returns (bytes32 operatorId) {
bytes32 pubkeyHash = BN254.hashG1Point(params.pubkeyG1);
require(
pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey"
pubkeyHash != ZERO_PK_HASH,
"BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey"
);
require(
operatorToPubkeyHash[operator] == bytes32(0),
Expand All @@ -113,24 +123,31 @@ 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;
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;

// 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");
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"
);

operatorToPubkey[operator] = params.pubkeyG1;
operatorToPubkeyHash[operator] = pubkeyHash;
Expand All @@ -140,18 +157,24 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
return pubkeyHash;
}

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

function _processQuorumApkUpdate(bytes memory quorumNumbers, BN254.G1Point memory point) internal {
/**
*
* INTERNAL FUNCTIONS
*
*/
function _processQuorumApkUpdate(
bytes memory quorumNumbers,
BN254.G1Point memory point
) internal {
BN254.G1Point memory newApk;

for (uint256 i = 0; i < quorumNumbers.length; i++) {
// Validate quorum exists and get history length
uint8 quorumNumber = uint8(quorumNumbers[i]);
uint256 historyLength = apkHistory[quorumNumber].length;
require(historyLength != 0, "BLSApkRegistry._processQuorumApkUpdate: quorum does not exist");
require(
historyLength != 0, "BLSApkRegistry._processQuorumApkUpdate: quorum does not exist"
);

// Update aggregate public key for this quorum
newApk = currentApk[quorumNumber].plus(point);
Expand All @@ -165,23 +188,29 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
lastUpdate.apkHash = newApkHash;
} else {
lastUpdate.nextUpdateBlockNumber = uint32(block.number);
apkHistory[quorumNumber].push(ApkUpdate({
apkHash: newApkHash,
updateBlockNumber: uint32(block.number),
nextUpdateBlockNumber: 0
}));
apkHistory[quorumNumber].push(
ApkUpdate({
apkHash: newApkHash,
updateBlockNumber: uint32(block.number),
nextUpdateBlockNumber: 0
})
);
}
}
}

/*******************************************************************************
VIEW FUNCTIONS
*******************************************************************************/
/**
*
* VIEW FUNCTIONS
*
*/
/**
* @notice Returns the pubkey and pubkey hash of an operator
* @dev Reverts if the operator has not registered a valid pubkey
*/
function getRegisteredPubkey(address operator) public view returns (BN254.G1Point memory, bytes32) {
function getRegisteredPubkey(
address operator
) public view returns (BN254.G1Point memory, bytes32) {
BN254.G1Point memory pubkey = operatorToPubkey[operator];
bytes32 pubkeyHash = operatorToPubkeyHash[operator];

Expand All @@ -207,8 +236,13 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
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");
if (
quorumApkUpdatesLength == 0
|| blockNumber < apkHistory[quorumNumber][0].updateBlockNumber
) {
revert(
"BLSApkRegistry.getApkIndicesAtBlockNumber: blockNumber is before the first update"
);
}

// Loop backward through apkHistory until we find an entry that preceeds `blockNumber`
Expand All @@ -223,12 +257,17 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
}

/// @notice Returns the current APK for the provided `quorumNumber `
function getApk(uint8 quorumNumber) external view returns (BN254.G1Point memory) {
function getApk(
uint8 quorumNumber
) external view returns (BN254.G1Point memory) {
return currentApk[quorumNumber];
}

/// @notice Returns the `ApkUpdate` struct at `index` in the list of APK updates for the `quorumNumber`
function getApkUpdateAtIndex(uint8 quorumNumber, uint256 index) external view returns (ApkUpdate memory) {
function getApkUpdateAtIndex(
uint8 quorumNumber,
uint256 index
) external view returns (ApkUpdate memory) {
return apkHistory[quorumNumber][index];
}

Expand Down Expand Up @@ -256,26 +295,33 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
"BLSApkRegistry.getApkHashAtBlockNumberAndIndex: index too recent"
);
require(
quorumApkUpdate.nextUpdateBlockNumber == 0 || blockNumber < quorumApkUpdate.nextUpdateBlockNumber,
quorumApkUpdate.nextUpdateBlockNumber == 0
|| blockNumber < quorumApkUpdate.nextUpdateBlockNumber,
"BLSApkRegistry.getApkHashAtBlockNumberAndIndex: not latest apk update"
);

return quorumApkUpdate.apkHash;
}

/// @notice Returns the length of ApkUpdates for the provided `quorumNumber`
function getApkHistoryLength(uint8 quorumNumber) external view returns (uint32) {
function getApkHistoryLength(
uint8 quorumNumber
) external view returns (uint32) {
return uint32(apkHistory[quorumNumber].length);
}

/// @notice Returns the operator address for the given `pubkeyHash`
function getOperatorFromPubkeyHash(bytes32 pubkeyHash) public view returns (address) {
function getOperatorFromPubkeyHash(
bytes32 pubkeyHash
) public view returns (address) {
return pubkeyHashToOperator[pubkeyHash];
}

/// @notice returns the ID used to identify the `operator` within this AVS
/// @dev Returns zero in the event that the `operator` has never registered for the AVS
function getOperatorId(address operator) public view returns (bytes32) {
function getOperatorId(
address operator
) public view returns (bytes32) {
return operatorToPubkeyHash[operator];
}

Expand Down
7 changes: 5 additions & 2 deletions src/BLSApkRegistryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {BN254} from "./libraries/BN254.sol";

abstract contract BLSApkRegistryStorage is Initializable, IBLSApkRegistry {
/// @notice the hash of the zero pubkey aka BN254.G1Point(0,0)
bytes32 internal constant ZERO_PK_HASH = hex"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5";
bytes32 internal constant ZERO_PK_HASH =
hex"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5";

/// @notice the registry coordinator contract
address public immutable registryCoordinator;
Expand All @@ -29,7 +30,9 @@ abstract contract BLSApkRegistryStorage is Initializable, IBLSApkRegistry {
/// @notice maps quorumNumber => current aggregate pubkey of quorum
mapping(uint8 => BN254.G1Point) public currentApk;

constructor(IRegistryCoordinator _registryCoordinator) {
constructor(
IRegistryCoordinator _registryCoordinator
) {
registryCoordinator = address(_registryCoordinator);
// disable initializers so that the implementation contract cannot be initialized
_disableInitializers();
Expand Down
Loading

0 comments on commit b757d5a

Please sign in to comment.