Skip to content

Commit

Permalink
updating consensus keys revert if any VK is a zero point VK
Browse files Browse the repository at this point in the history
  • Loading branch information
alysiahuggins committed Jan 9, 2025
1 parent 5f3a988 commit 638ed78
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 62 deletions.
23 changes: 12 additions & 11 deletions contracts/src/StakeTable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -506,19 +506,20 @@ contract StakeTable is AbstractStakeTable {
);
EdOnBN254.EdOnBN254Point memory zeroSchnorrKey = EdOnBN254.EdOnBN254Point(0, 0);

// Update the node's bls key if the newBlsVK is not a zero point BLS key
if (!_isEqualBlsKey(newBlsVK, zeroBlsKey)) {
// Verify that the validator can sign for that newBlsVK, otherwise it inner reverts with
// BLSSigVerificationFailed
bytes memory message = abi.encode(msg.sender);
BLSSig.verifyBlsSig(message, newBlsSig, newBlsVK);
node.blsVK = newBlsVK;
}
if (_isEqualBlsKey(newBlsVK, zeroBlsKey)) revert InvalidBlsVK();

if (newSchnorrVK.isEqual(zeroSchnorrKey)) revert InvalidSchnorrVK();

// Verify that the validator can sign for that newBlsVK, otherwise it inner reverts with
// BLSSigVerificationFailed
bytes memory message = abi.encode(msg.sender);
BLSSig.verifyBlsSig(message, newBlsSig, newBlsVK);

// Update the node's bls key
node.blsVK = newBlsVK;

// Update the node's schnorr key if the newSchnorrVK is not a zero point Schnorr key
if (!newSchnorrVK.isEqual(zeroSchnorrKey)) {
node.schnorrVK = newSchnorrVK;
}
node.schnorrVK = newSchnorrVK;

// Update the node in the stake table
nodes[msg.sender] = node;
Expand Down
69 changes: 18 additions & 51 deletions contracts/test/StakeTable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ contract StakeTable_register_Test is Test {
vm.stopPrank();
}

function test_UpdateConsensusKeysWithEmptyKeys_CausesNoChange() public {
function test_RevertWhen_UpdateConsensusKeysWithEmptyKeys() public {
uint64 depositAmount = 10 ether;
uint64 validUntilEpoch = 5;
string memory seed = "123";
Expand Down Expand Up @@ -419,8 +419,7 @@ contract StakeTable_register_Test is Test {
EdOnBN254.EdOnBN254Point memory emptySchnorrVK = EdOnBN254.EdOnBN254Point(0, 0);

// Step 2: attempt to update the consensus keys with the same keys
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.UpdatedConsensusKeys(exampleTokenCreator, blsVK, schnorrVK);
vm.expectRevert(S.InvalidBlsVK.selector);
stakeTable.updateConsensusKeys(emptyBlsVK, emptySchnorrVK, sig);

vm.stopPrank();
Expand Down Expand Up @@ -462,7 +461,7 @@ contract StakeTable_register_Test is Test {
vm.stopPrank();
}

function test_UpdateConsensusKeysWithZeroBlsKeyButNewSchnorrVK_Succeeds() public {
function test_RevertWhen_UpdateConsensusKeysWithZeroBlsKeyButNewSchnorrVK() public {
uint64 depositAmount = 10 ether;
uint64 validUntilEpoch = 5;
string memory seed = "123";
Expand Down Expand Up @@ -499,21 +498,13 @@ contract StakeTable_register_Test is Test {

// Step 3: update the consensus keys with the new schnorr Key but zero bls key which
// indicates no change in the bls key
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.UpdatedConsensusKeys(exampleTokenCreator, blsVK, newSchnorrVK);
vm.expectRevert(S.InvalidBlsVK.selector);
stakeTable.updateConsensusKeys(emptyBlsVK, newSchnorrVK, sig);

// Step 4: verify the update
AbstractStakeTable.Node memory node = stakeTable.lookupNode(exampleTokenCreator);
assertTrue(stakeTable._isEqualBlsKey(node.blsVK, blsVK)); // same as current bls vk
assertTrue(EdOnBN254.isEqual(node.schnorrVK, newSchnorrVK)); // new schnorr vk
assertEq(node.balance, depositAmount); //same balance
assertEq(node.account, exampleTokenCreator); //same account

vm.stopPrank();
}

function test_UpdateConsensusKeysWithZeroSchnorrVKButNewBlsVK_Succeeds() public {
function test_RevertWhen_UpdateConsensusKeysWithZeroSchnorrVKButNewBlsVK() public {
uint64 depositAmount = 10 ether;
uint64 validUntilEpoch = 5;
string memory seed = "123";
Expand All @@ -539,35 +530,12 @@ contract StakeTable_register_Test is Test {
(BN254.G2Point memory newBlsVK,, BN254.G1Point memory newSig) =
genClientWallet(exampleTokenCreator, seed);

// Step 3: update the consensus keys with the same schnorrVK but new bls keys
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.UpdatedConsensusKeys(exampleTokenCreator, newBlsVK, schnorrVK);
stakeTable.updateConsensusKeys(newBlsVK, schnorrVK, newSig);

// Step 4: verify the update
AbstractStakeTable.Node memory node = stakeTable.lookupNode(exampleTokenCreator);
assertTrue(stakeTable._isEqualBlsKey(node.blsVK, newBlsVK)); // same as current bls vk
assertTrue(EdOnBN254.isEqual(node.schnorrVK, schnorrVK)); // same as current schnorr vk
assertEq(node.balance, depositAmount); //same balance
assertEq(node.account, exampleTokenCreator); //same account

// Step 5: update the consensus keys with the same bls keys but empty schnorrVK
// Step 3: generate empty schnorrVK
EdOnBN254.EdOnBN254Point memory emptySchnorrVK = EdOnBN254.EdOnBN254Point(0, 0);

// Step 6: generate a new blsVK
seed = "235";
(BN254.G2Point memory newBlsVK2,, BN254.G1Point memory newSig2) =
genClientWallet(exampleTokenCreator, seed);
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.UpdatedConsensusKeys(exampleTokenCreator, newBlsVK2, schnorrVK);
stakeTable.updateConsensusKeys(newBlsVK2, emptySchnorrVK, newSig2);

// Step 7: verify the update
node = stakeTable.lookupNode(exampleTokenCreator);
assertTrue(stakeTable._isEqualBlsKey(node.blsVK, newBlsVK2)); // same as current bls vk
assertTrue(EdOnBN254.isEqual(node.schnorrVK, schnorrVK)); // same as current schnorr vk
assertEq(node.balance, depositAmount); //same balance
assertEq(node.account, exampleTokenCreator); //same account
// Step 4: update the consensus keys with the new bls keys but empty schnorrVK
vm.expectRevert(S.InvalidSchnorrVK.selector);
stakeTable.updateConsensusKeys(newBlsVK, emptySchnorrVK, newSig);

vm.stopPrank();
}
Expand All @@ -581,7 +549,7 @@ contract StakeTable_register_Test is Test {
(
BN254.G2Point memory blsVK,
EdOnBN254.EdOnBN254Point memory schnorrVK,
BN254.G1Point memory sig
BN254.G1Point memory blsSig
) = genClientWallet(exampleTokenCreator, seed);

// Prepare for the token transfer by granting allowance to the contract
Expand All @@ -593,23 +561,22 @@ contract StakeTable_register_Test is Test {

vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.Registered(exampleTokenCreator, 1, depositAmount);
stakeTable.register(blsVK, schnorrVK, depositAmount, sig, validUntilEpoch);
stakeTable.register(blsVK, schnorrVK, depositAmount, blsSig, validUntilEpoch);

// Step 2: generate an empty schnorrVK and new blsVK
EdOnBN254.EdOnBN254Point memory emptySchnorrVK = EdOnBN254.EdOnBN254Point(0, 0);
// Step 2: generate a new schnorrVK
seed = "234";
(BN254.G2Point memory newBlsVK,, BN254.G1Point memory newSig) =
(, EdOnBN254.EdOnBN254Point memory newSchnorrVK,) =
genClientWallet(exampleTokenCreator, seed);

// Step 3: update the consensus keys with the new bls keys but empty schnorrVK
// Step 3: update the consensus keys with the new schnorrVK
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.UpdatedConsensusKeys(exampleTokenCreator, newBlsVK, schnorrVK);
stakeTable.updateConsensusKeys(newBlsVK, emptySchnorrVK, newSig);
emit AbstractStakeTable.UpdatedConsensusKeys(exampleTokenCreator, blsVK, newSchnorrVK);
stakeTable.updateConsensusKeys(blsVK, newSchnorrVK, blsSig);

// Step 4: verify the update
AbstractStakeTable.Node memory node = stakeTable.lookupNode(exampleTokenCreator);
assertTrue(stakeTable._isEqualBlsKey(node.blsVK, newBlsVK)); // same as current bls vk
assertTrue(EdOnBN254.isEqual(node.schnorrVK, schnorrVK)); // same as current schnorr vk
assertTrue(stakeTable._isEqualBlsKey(node.blsVK, blsVK)); // same as current bls vk
assertTrue(EdOnBN254.isEqual(node.schnorrVK, newSchnorrVK)); // new schnorr vk
assertEq(node.balance, depositAmount); //same balance
assertEq(node.account, exampleTokenCreator); //same account

Expand Down

0 comments on commit 638ed78

Please sign in to comment.