Skip to content

Commit

Permalink
fix challenge for new encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wdy1 committed Dec 20, 2023
1 parent 15e8563 commit 13237ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/zkbob/manager/MPCGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract MPCGuard is Ownable, CustomABIDecoder {

function setGuards(address[] calldata _guards) external onlyOwner {
for (uint256 i = 0; i < _guards.length - 1; i++) {
require(_guards[i] > _guards[i + 1], "must be sorted in descending order") ;
require(_guards[i] > _guards[i + 1], "must be sorted in descending order");
}
guards = _guards;
}
Expand Down
9 changes: 3 additions & 6 deletions test/zkbob/ZkBobPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {

uint256 constant initialRoot = 11469701942666298368112882412133877458305516134926649826543144744382391691533;

address[] guardians;

enum PoolType {
BOB,
ETH,
Expand Down Expand Up @@ -167,10 +165,9 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
address operatorEOA = makeAddr("operatorEOA");
address operatorContract = address(new MPCGuard(operatorEOA, address(pool)));
operatorManager = new MutableOperatorManager(operatorContract, user3, "https://example.com");
(address guard1Addr,) = makeAddrAndKey("guard1");
(address guard2Addr,) = makeAddrAndKey("guard2");
guardians.push(guard1Addr);
guardians.push(guard2Addr);
address[] memory guardians = new address[](2);
guardians[0] = makeAddr("guard2");
guardians[1] = makeAddr("guard1");
MPCGuard(operatorContract).setGuards(guardians);
address[] memory users = new address[](2);
users[0] = operatorContract;
Expand Down
42 changes: 21 additions & 21 deletions test/zkbob/manager/MPCGuard.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ contract MPCOperatorManagerTest is AbstractZkBobPoolTest, AbstractPolygonForkTes
uint256 current_root = poolContract.roots(poolContract.pool_index());
uint48 transfer_index = uint48(0);
uint256 transfer_root = poolContract.roots(transfer_index);
bytes memory challenge = abi.encodePacked(data, transfer_root, current_root, poolContract.pool_id());
bytes32 challenge = digest(abi.encodePacked(data, transfer_root, current_root, poolContract.pool_id()));
(, uint256 guard1Key) = makeAddrAndKey("guard1");
(, uint256 guard2Key) = makeAddrAndKey("guard2");
return abi.encodePacked(
data,
uint8(2), //753
sign(challenge, guard1Key), //817
sign(challenge, guard2Key) //881
sign(challenge, guard2Key), //817
sign(challenge, guard1Key) //881
);
}

Expand Down Expand Up @@ -130,34 +130,34 @@ contract MPCOperatorManagerTest is AbstractZkBobPoolTest, AbstractPolygonForkTes
uint256 root_afer = _randFR();
uint256[8] memory batch_deposit_proof = _randProof();
uint256[8] memory tree_proof = _randProof();
bytes memory mpcMessage = abi.encodePacked(
ZkBobPool.appendDirectDeposits.selector,
bytes memory mpc_message = abi.encodeWithSelector(
poolContract.appendDirectDeposits.selector,
root_afer,
indices,
outCommitment,
batch_deposit_proof,
tree_proof,
poolContract.roots(poolContract.pool_index()),
poolContract.pool_id()
tree_proof
);

(, uint256 guard1Key) = makeAddrAndKey("guard1");
(, uint256 guard2Key) = makeAddrAndKey("guard2");

bytes memory signatures;
{
uint256 currentRoot = poolContract.roots(poolContract.pool_index());
bytes32 challenge = digest(abi.encodePacked(mpc_message, currentRoot, poolContract.pool_id()));
(, uint256 guard1Key) = makeAddrAndKey("guard1");
(, uint256 guard2Key) = makeAddrAndKey("guard2");
signatures = abi.encodePacked(sign(challenge, guard2Key), sign(challenge, guard1Key));
}
vm.prank(makeAddr("operatorEOA"));
MPCGuard(wrapper).appendDirectDepositsMPC(
root_afer,
indices,
outCommitment,
batch_deposit_proof,
tree_proof,
abi.encodePacked(sign(mpcMessage, guard1Key), sign(mpcMessage, guard2Key))
root_afer, indices, outCommitment, batch_deposit_proof, tree_proof, signatures
);
}

function sign(bytes memory data, uint256 key) internal pure returns (bytes memory signatureData) {
bytes32 digest = ECDSA.toEthSignedMessageHash(keccak256(data));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(key, digest);
function sign(bytes32 _digest, uint256 key) internal pure returns (bytes memory signatureData) {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(key, _digest);
signatureData = abi.encodePacked(r, uint256(s) + (v == 28 ? (1 << 255) : 0));
}

function digest(bytes memory data) internal pure returns (bytes32) {
return ECDSA.toEthSignedMessageHash(keccak256(data));
}
}

0 comments on commit 13237ca

Please sign in to comment.