11// SPDX-License-Identifier: MIT
22pragma solidity ^ 0.8.20 ;
33
4- import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol " ;
5- import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol " ;
6- import "@openzeppelin/contracts/utils/cryptography/EIP712.sol " ;
4+ import {OwnableUpgradeable} from "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol " ;
5+ import {Initializable} from "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol " ;
6+ import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol " ;
77
8- import "../../libraries/BN254.sol " ;
9- import "../../interface/IBLSApkRegistry.sol " ;
10- import "./BLSApkRegistryStorage.sol " ;
8+ import {BN254} from "../../libraries/BN254.sol " ;
9+ import {IBLSApkRegistry} from "../../interface/IBLSApkRegistry.sol " ;
10+ import {BLSApkRegistryStorage} from "./BLSApkRegistryStorage.sol " ;
1111
1212contract BLSApkRegistry is Initializable , OwnableUpgradeable , IBLSApkRegistry , BLSApkRegistryStorage , EIP712 {
1313 using BN254 for BN254.G1Point;
@@ -16,17 +16,25 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
1616
1717
1818 modifier onlyWhitelistManagerManager () {
19+ _onlyWhitelistManagerManager ();
20+ _;
21+ }
22+
23+ function _onlyWhitelistManagerManager () internal view {
1924 require (
2025 msg .sender == whitelistManager, "BLSApkRegistry.onlyRelayerManager: caller is not the relayer manager address "
2126 );
22- _;
2327 }
2428
2529 modifier onlyVrfManager () {
30+ _onlyVrfManager ();
31+ _;
32+ }
33+
34+ function _onlyVrfManager () internal view {
2635 require (
2736 msg .sender == vrfManagerAddress, "BLSApkRegistry.onlyRelayerManager: caller is not the relayer manager address "
2837 );
29- _;
3038 }
3139
3240
@@ -64,27 +72,27 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
6472 emit OperatorRemoved (operator, operatorToPubkeyHash[operator]);
6573 }
6674
67- function registerBLSPublicKey (
75+ function registerBlsPublicKey (
6876 address operator ,
6977 PubkeyRegistrationParams calldata params ,
7078 BN254.G1Point calldata pubkeyRegistrationMessageHash
7179 ) external returns (bytes32 ) {
7280 require (
7381 blsRegisterWhitelist[msg .sender ],
74- "BLSApkRegistry.registerBLSPublicKey : this address have not permission to register bls key "
82+ "BLSApkRegistry.registerBlsPublicKey : this address have not permission to register bls key "
7583 );
7684
7785 bytes32 pubkeyHash = BN254.hashG1Point (params.pubkeyG1);
7886
79- require (pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBLSPublicKey : cannot register zero pubkey " );
87+ require (pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBlsPublicKey : cannot register zero pubkey " );
8088 require (
8189 operatorToPubkeyHash[operator] == bytes32 (0 ),
82- "BLSApkRegistry.registerBLSPublicKey : operator already registered pubkey "
90+ "BLSApkRegistry.registerBlsPublicKey : operator already registered pubkey "
8391 );
8492
8593 require (
8694 pubkeyHashToOperator[pubkeyHash] == address (0 ),
87- "BLSApkRegistry.registerBLSPublicKey : public key already registered "
95+ "BLSApkRegistry.registerBlsPublicKey : public key already registered "
8896 );
8997
9098 uint256 gamma = uint256 (
@@ -104,12 +112,12 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
104112
105113 require (
106114 BN254.pairing (
107- params.pubkeyRegistrationSignature.plus (params.pubkeyG1.scalar_mul (gamma)),
115+ params.pubkeyRegistrationSignature.plus (params.pubkeyG1.scalarMul (gamma)),
108116 BN254.negGeneratorG2 (),
109- pubkeyRegistrationMessageHash.plus (BN254.generatorG1 ().scalar_mul (gamma)),
117+ pubkeyRegistrationMessageHash.plus (BN254.generatorG1 ().scalarMul (gamma)),
110118 params.pubkeyG2
111119 ),
112- "BLSApkRegistry.registerBLSPublicKey : either the G1 signature is wrong, or G1 and G2 private key do not match "
120+ "BLSApkRegistry.registerBlsPublicKey : either the G1 signature is wrong, or G1 and G2 private key do not match "
113121 );
114122
115123 operatorToPubkey[operator] = params.pubkeyG1;
@@ -141,7 +149,22 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
141149 require (pairingSuccessful, "BLSSignatureChecker.checkSignatures: pairing precompile call failed " );
142150 require (signatureIsValid, "BLSSignatureChecker.checkSignatures: signature is invalid " );
143151
144- bytes32 signatoryRecordHash = keccak256 (abi.encodePacked (referenceBlockNumber, nonSignersPubkeyHashes));
152+ bytes32 signatoryRecordHash;
153+ assembly {
154+ let ptr := mload (0x40 )
155+ let startPtr := ptr
156+ mstore (ptr, referenceBlockNumber)
157+ ptr := add (ptr, 0x20 )
158+ let arrayPtr := nonSignersPubkeyHashes
159+ let arrayLength := mload (arrayPtr)
160+ for { let i := 0 } lt (i, arrayLength) { i := add (i, 1 ) } {
161+ mstore (ptr, mload (add (arrayPtr, add (0x20 , mul (i, 0x20 )))))
162+ ptr := add (ptr, 0x20 )
163+ }
164+ let dataLength := sub (ptr, startPtr)
165+ signatoryRecordHash := keccak256 (startPtr, dataLength)
166+ mstore (0x40 , ptr)
167+ }
145168
146169 StakeTotals memory stakeTotals = StakeTotals ({totalDappLinkStake: params.totalDappLinkStake, totalBtcStake: params.totalBtcStake});
147170
@@ -167,9 +190,9 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
167190 )
168191 ) % BN254.FR_MODULUS;
169192 (pairingSuccessful, siganatureIsValid) = BN254.safePairing (
170- sigma.plus (apk.scalar_mul (gamma)),
193+ sigma.plus (apk.scalarMul (gamma)),
171194 BN254.negGeneratorG2 (),
172- BN254.hashToG1 (msgHash).plus (BN254.generatorG1 ().scalar_mul (gamma)),
195+ BN254.hashToG1 (msgHash).plus (BN254.generatorG1 ().scalarMul (gamma)),
173196 apkG2,
174197 PAIRING_EQUALITY_CHECK_GAS
175198 );
0 commit comments