@@ -11,8 +11,6 @@ import "../interfaces/IBLSApkRegistry.sol";
11
11
12
12
import "./BLSApkRegistryStorage.sol " ;
13
13
14
-
15
-
16
14
contract BLSApkRegistry is Initializable , OwnableUpgradeable , IBLSApkRegistry , BLSApkRegistryStorage , EIP712 {
17
15
using BN254 for BN254.G1Point;
18
16
@@ -28,43 +26,43 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
28
26
29
27
modifier onlyRelayerManager () {
30
28
require (
31
- msg .sender == relayerManager,
32
- "BLSApkRegistry.onlyRelayerManager: caller is not the relayer manager address "
29
+ msg .sender == relayerManager, "BLSApkRegistry.onlyRelayerManager: caller is not the relayer manager address "
33
30
);
34
31
_;
35
32
}
36
33
37
- constructor ()
38
- EIP712 ("BLSApkRegistry " , "v0.0.1 " )
39
- {
34
+ constructor () EIP712 ("BLSApkRegistry " , "v0.0.1 " ) {
40
35
_disableInitializers ();
41
36
}
42
37
43
- function initialize (
44
- address _initialOwner ,
45
- address _finalityRelayerManager ,
46
- address _relayerManager
47
- ) external initializer {
38
+ function initialize (address _initialOwner , address _finalityRelayerManager , address _relayerManager )
39
+ external
40
+ initializer
41
+ {
48
42
_transferOwnership (_initialOwner);
49
43
finalityRelayerManager = _finalityRelayerManager;
50
44
relayerManager = _relayerManager;
51
45
_initializeApk ();
52
46
}
53
47
54
- function registerOperator (
55
- address operator
56
- ) public onlyFinalityRelayerManager {
57
- (BN254.G1Point memory pubkey , ) = getRegisteredPubkey (operator);
48
+ /**
49
+ * @dev Registers an operator within the system.
50
+ *
51
+ * This function allows the Finality Relayer Manager to add an operator to the system.
52
+ * The operator's public key is retrieved and used to update the associated proof (APK).
53
+ *
54
+ * @param operator The address of the operator to be registered.
55
+ */
56
+ function registerOperator (address operator ) public onlyFinalityRelayerManager {
57
+ (BN254.G1Point memory pubkey ,) = getRegisteredPubkey (operator);
58
58
59
59
_processApkUpdate (pubkey);
60
60
61
61
emit OperatorAdded (operator, operatorToPubkeyHash[operator]);
62
62
}
63
63
64
- function deregisterOperator (
65
- address operator
66
- ) public onlyFinalityRelayerManager {
67
- (BN254.G1Point memory pubkey , ) = getRegisteredPubkey (operator);
64
+ function deregisterOperator (address operator ) public onlyFinalityRelayerManager {
65
+ (BN254.G1Point memory pubkey ,) = getRegisteredPubkey (operator);
68
66
69
67
_processApkUpdate (pubkey.negate ());
70
68
emit OperatorRemoved (operator, operatorToPubkeyHash[operator]);
@@ -82,10 +80,7 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
82
80
83
81
bytes32 pubkeyHash = BN254.hashG1Point (params.pubkeyG1);
84
82
85
- require (
86
- pubkeyHash != ZERO_PK_HASH,
87
- "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey "
88
- );
83
+ require (pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey " );
89
84
require (
90
85
operatorToPubkeyHash[operator] == bytes32 (0 ),
91
86
"BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey "
@@ -96,23 +91,30 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
96
91
"BLSApkRegistry.registerBLSPublicKey: public key already registered "
97
92
);
98
93
99
- uint256 gamma = uint256 (keccak256 (abi.encodePacked (
100
- params.pubkeyRegistrationSignature.X,
101
- params.pubkeyRegistrationSignature.Y,
102
- params.pubkeyG1.X,
103
- params.pubkeyG1.Y,
104
- params.pubkeyG2.X,
105
- params.pubkeyG2.Y,
106
- pubkeyRegistrationMessageHash.X,
107
- pubkeyRegistrationMessageHash.Y
108
- ))) % BN254.FR_MODULUS;
109
-
110
- require (BN254.pairing (
111
- params.pubkeyRegistrationSignature.plus (params.pubkeyG1.scalar_mul (gamma)),
112
- BN254.negGeneratorG2 (),
113
- pubkeyRegistrationMessageHash.plus (BN254.generatorG1 ().scalar_mul (gamma)),
114
- params.pubkeyG2
115
- ), "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match " );
94
+ uint256 gamma = uint256 (
95
+ keccak256 (
96
+ abi.encodePacked (
97
+ params.pubkeyRegistrationSignature.X,
98
+ params.pubkeyRegistrationSignature.Y,
99
+ params.pubkeyG1.X,
100
+ params.pubkeyG1.Y,
101
+ params.pubkeyG2.X,
102
+ params.pubkeyG2.Y,
103
+ pubkeyRegistrationMessageHash.X,
104
+ pubkeyRegistrationMessageHash.Y
105
+ )
106
+ )
107
+ ) % BN254.FR_MODULUS;
108
+
109
+ require (
110
+ BN254.pairing (
111
+ params.pubkeyRegistrationSignature.plus (params.pubkeyG1.scalar_mul (gamma)),
112
+ BN254.negGeneratorG2 (),
113
+ pubkeyRegistrationMessageHash.plus (BN254.generatorG1 ().scalar_mul (gamma)),
114
+ params.pubkeyG2
115
+ ),
116
+ "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match "
117
+ );
116
118
117
119
operatorToPubkey[operator] = params.pubkeyG1;
118
120
operatorToPubkeyHash[operator] = pubkeyHash;
@@ -123,12 +125,14 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
123
125
return pubkeyHash;
124
126
}
125
127
126
- function checkSignatures (
127
- bytes32 msgHash ,
128
- uint256 referenceBlockNumber ,
129
- FinalityNonSignerAndSignature memory params
130
- ) public view returns (StakeTotals memory , bytes32 ) {
131
- require (referenceBlockNumber < uint32 (block .number ), "BLSSignatureChecker.checkSignatures: invalid reference block " );
128
+ function checkSignatures (bytes32 msgHash , uint256 referenceBlockNumber , FinalityNonSignerAndSignature memory params )
129
+ public
130
+ view
131
+ returns (StakeTotals memory , bytes32 )
132
+ {
133
+ require (
134
+ referenceBlockNumber < uint32 (block .number ), "BLSSignatureChecker.checkSignatures: invalid reference block "
135
+ );
132
136
BN254.G1Point memory signerApk = BN254.G1Point (0 , 0 );
133
137
bytes32 [] memory nonSignersPubkeyHashes;
134
138
if (params.nonSignerPubkeys.length > 0 ) {
@@ -140,25 +144,21 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
140
144
} else {
141
145
signerApk = currentApk;
142
146
}
143
- (bool pairingSuccessful , bool signatureIsValid ) = trySignatureAndApkVerification (msgHash, signerApk, params.apkG2, params.sigma);
147
+ (bool pairingSuccessful , bool signatureIsValid ) =
148
+ trySignatureAndApkVerification (msgHash, signerApk, params.apkG2, params.sigma);
144
149
require (pairingSuccessful, "BLSSignatureChecker.checkSignatures: pairing precompile call failed " );
145
150
require (signatureIsValid, "BLSSignatureChecker.checkSignatures: signature is invalid " );
146
151
147
152
bytes32 signatoryRecordHash = keccak256 (abi.encodePacked (referenceBlockNumber, nonSignersPubkeyHashes));
148
153
149
- StakeTotals memory stakeTotals = StakeTotals ({
150
- totalBtcStaking: params.totalBtcStake,
151
- totalMantaStaking: params.totalMantaStake
152
- });
154
+ StakeTotals memory stakeTotals =
155
+ StakeTotals ({totalBtcStaking: params.totalBtcStake, totalMantaStaking: params.totalMantaStake});
153
156
154
157
return (stakeTotals, signatoryRecordHash);
155
158
}
156
159
157
160
function addOrRemoveBlsRegisterWhitelist (address register , bool isAdd ) external onlyRelayerManager {
158
- require (
159
- register != address (0 ),
160
- "BLSApkRegistry.addOrRemoverBlsRegisterWhitelist: operator address is zero "
161
- );
161
+ require (register != address (0 ), "BLSApkRegistry.addOrRemoverBlsRegisterWhitelist: operator address is zero " );
162
162
blsRegisterWhitelist[register] = isAdd;
163
163
}
164
164
@@ -167,8 +167,14 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
167
167
BN254.G1Point memory apk ,
168
168
BN254.G2Point memory apkG2 ,
169
169
BN254.G1Point memory sigma
170
- ) public view returns (bool pairingSuccessful , bool siganatureIsValid ) {
171
- uint256 gamma = uint256 (keccak256 (abi.encodePacked (msgHash, apk.X, apk.Y, apkG2.X[0 ], apkG2.X[1 ], apkG2.Y[0 ], apkG2.Y[1 ], sigma.X, sigma.Y))) % BN254.FR_MODULUS;
170
+ ) public view returns (bool pairingSuccessful , bool siganatureIsValid ) {
171
+ uint256 gamma = uint256 (
172
+ keccak256 (
173
+ abi.encodePacked (
174
+ msgHash, apk.X, apk.Y, apkG2.X[0 ], apkG2.X[1 ], apkG2.Y[0 ], apkG2.Y[1 ], sigma.X, sigma.Y
175
+ )
176
+ )
177
+ ) % BN254.FR_MODULUS;
172
178
(pairingSuccessful, siganatureIsValid) = BN254.safePairing (
173
179
sigma.plus (apk.scalar_mul (gamma)),
174
180
BN254.negGeneratorG2 (),
@@ -194,42 +200,34 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
194
200
lastUpdate.apkHash = newApkHash;
195
201
} else {
196
202
lastUpdate.nextUpdateBlockNumber = uint32 (block .number );
197
- apkHistory.push (ApkUpdate ({
198
- apkHash: newApkHash,
199
- updateBlockNumber: uint32 (block .number ),
200
- nextUpdateBlockNumber: 0
201
- }));
203
+ apkHistory.push (
204
+ ApkUpdate ({apkHash: newApkHash, updateBlockNumber: uint32 (block .number ), nextUpdateBlockNumber: 0 })
205
+ );
202
206
}
203
207
}
204
208
205
209
function getRegisteredPubkey (address operator ) public view returns (BN254.G1Point memory , bytes32 ) {
206
210
BN254.G1Point memory pubkey = operatorToPubkey[operator];
207
211
bytes32 pubkeyHash = operatorToPubkeyHash[operator];
208
212
209
- require (
210
- pubkeyHash != bytes32 (0 ),
211
- "BLSApkRegistry.getRegisteredPubkey: operator is not registered "
212
- );
213
+ require (pubkeyHash != bytes32 (0 ), "BLSApkRegistry.getRegisteredPubkey: operator is not registered " );
213
214
214
215
return (pubkey, pubkeyHash);
215
216
}
216
217
217
- function pubkeyRegistrationMessageHash (address operator ) public view returns (BN254.G1Point memory ) {
218
- return BN254.hashToG1 (
219
- _hashTypedDataV4 (
220
- keccak256 (abi.encode (PUBKEY_REGISTRATION_TYPEHASH, operator))
221
- )
222
- );
218
+ function getPubkeyRegMessageHash (address operator ) public view returns (BN254.G1Point memory ) {
219
+ return BN254.hashToG1 (_hashTypedDataV4 (keccak256 (abi.encode (PUBKEY_REGISTRATION_TYPEHASH, operator))));
223
220
}
224
221
225
222
function _initializeApk () internal {
226
223
require (apkHistory.length == 0 , "BLSApkRegistry.initializeApk: apk already exists " );
227
224
228
- apkHistory.push (ApkUpdate ({
229
- apkHash: bytes24 (0 ),
230
- updateBlockNumber: uint32 (block .number ),
231
- nextUpdateBlockNumber: 0
232
- }));
225
+ apkHistory.push (
226
+ ApkUpdate ({apkHash: bytes24 (0 ), updateBlockNumber: uint32 (block .number ), nextUpdateBlockNumber: 0 })
227
+ );
233
228
}
234
229
230
+ function getPubkeyHash (address operator ) public view returns (bytes32 ) {
231
+ return operatorToPubkeyHash[operator];
232
+ }
235
233
}
0 commit comments