Skip to content

Commit 044e890

Browse files
authored
Merge pull request #5 from Manta-Network/feature/audit-fix
fix bls audit issues
2 parents aee8feb + 136c785 commit 044e890

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/bls/BLSApkRegistry.sol

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
4242
_transferOwnership(_initialOwner);
4343
finalityRelayerManager = _finalityRelayerManager;
4444
relayerManager = _relayerManager;
45+
totalNodes = 0;
4546
_initializeApk();
4647
}
4748

@@ -54,17 +55,32 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
5455
* @param operator The address of the operator to be registered.
5556
*/
5657
function registerOperator(address operator) public onlyFinalityRelayerManager {
58+
require(operator != address(0), "BLSApkRegistry.registerBLSPublicKey: Operator is zero address");
59+
60+
require(!operatorIsRegister[operator], "BLSApkRegistry.registerBLSPublicKey: Operator have already register");
61+
5762
(BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator);
5863

5964
_processApkUpdate(pubkey);
6065

66+
totalNodes += 1;
67+
68+
operatorIsRegister[operator] = true;
69+
6170
emit OperatorAdded(operator, operatorToPubkeyHash[operator]);
6271
}
6372

6473
function deregisterOperator(address operator) public onlyFinalityRelayerManager {
74+
require(operatorIsRegister[operator], "BLSApkRegistry.registerBLSPublicKey: Operator have already deregister");
75+
6576
(BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator);
6677

6778
_processApkUpdate(pubkey.negate());
79+
80+
operatorIsRegister[operator] = false;
81+
82+
totalNodes -= 1;
83+
6884
emit OperatorRemoved(operator, operatorToPubkeyHash[operator]);
6985
}
7086

@@ -73,6 +89,11 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
7389
PubkeyRegistrationParams calldata params,
7490
BN254.G1Point calldata pubkeyRegistrationMessageHash
7591
) external returns (bytes32) {
92+
require(
93+
msg.sender == operator,
94+
"BLSApkRegistry.registerBLSPublicKey: this caller is not operator"
95+
);
96+
7697
require(
7798
blsRegisterWhitelist[msg.sender],
7899
"BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key"
@@ -125,34 +146,41 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B
125146
return pubkeyHash;
126147
}
127148

128-
function checkSignatures(bytes32 msgHash, uint256 referenceBlockNumber, FinalityNonSignerAndSignature memory params)
129-
public
130-
view
131-
returns (StakeTotals memory, bytes32)
132-
{
149+
function checkSignatures(
150+
bytes32 msgHash,
151+
uint256 referenceBlockNumber,
152+
FinalityNonSignerAndSignature memory params
153+
) public view returns (StakeTotals memory, bytes32) {
154+
require(
155+
referenceBlockNumber < uint32(block.number),
156+
"BLSSignatureChecker.checkSignatures: invalid reference block"
157+
);
158+
uint256 nonSingerNode = params.nonSignerPubkeys.length;
159+
uint256 thresholdNodes = (totalNodes * 2) / 3;
160+
133161
require(
134-
referenceBlockNumber < uint32(block.number), "BLSSignatureChecker.checkSignatures: invalid reference block"
162+
totalNodes - nonSingerNode >= thresholdNodes,
163+
"BLSSignatureChecker.checkSignatures: sign node less than threshold node"
135164
);
136-
BN254.G1Point memory signerApk = BN254.G1Point(0, 0);
165+
166+
BN254.G1Point memory signerApk = currentApk;
137167
bytes32[] memory nonSignersPubkeyHashes;
168+
138169
if (params.nonSignerPubkeys.length > 0) {
139170
nonSignersPubkeyHashes = new bytes32[](params.nonSignerPubkeys.length);
140171
for (uint256 j = 0; j < params.nonSignerPubkeys.length; j++) {
141172
nonSignersPubkeyHashes[j] = params.nonSignerPubkeys[j].hashG1Point();
142-
signerApk = currentApk.plus(params.nonSignerPubkeys[j].negate());
173+
signerApk = signerApk.plus(params.nonSignerPubkeys[j].negate());
143174
}
144-
} else {
145-
signerApk = currentApk;
146175
}
147-
(bool pairingSuccessful, bool signatureIsValid) =
148-
trySignatureAndApkVerification(msgHash, signerApk, params.apkG2, params.sigma);
176+
177+
(bool pairingSuccessful, bool signatureIsValid) = trySignatureAndApkVerification(msgHash, signerApk, params.apkG2, params.sigma);
149178
require(pairingSuccessful, "BLSSignatureChecker.checkSignatures: pairing precompile call failed");
150179
require(signatureIsValid, "BLSSignatureChecker.checkSignatures: signature is invalid");
151180

152181
bytes32 signatoryRecordHash = keccak256(abi.encodePacked(referenceBlockNumber, nonSignersPubkeyHashes));
153182

154-
StakeTotals memory stakeTotals =
155-
StakeTotals({totalBtcStaking: params.totalBtcStake, totalMantaStaking: params.totalMantaStake});
183+
StakeTotals memory stakeTotals = StakeTotals({totalBtcStaking: params.totalBtcStake, totalMantaStaking: params.totalMantaStake});
156184

157185
return (stakeTotals, signatoryRecordHash);
158186
}

src/bls/BLSApkRegistryStorage.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ abstract contract BLSApkRegistryStorage is Initializable, IBLSApkRegistry {
2020
mapping(address => bytes32) public operatorToPubkeyHash;
2121
mapping(bytes32 => address) public pubkeyHashToOperator;
2222
mapping(address => BN254.G1Point) public operatorToPubkey;
23+
mapping(address => bool) public operatorIsRegister;
2324

2425
BN254.G1Point public currentApk;
2526
ApkUpdate[] public apkHistory;
2627

27-
mapping(address => bool) public blsRegisterWhitelist;
28-
28+
uint256 public totalNodes;
2929

30+
mapping(address => bool) public blsRegisterWhitelist;
3031
}

0 commit comments

Comments
 (0)