Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wdy1 committed Dec 15, 2023
1 parent ea116c3 commit 54e6e06
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 124 deletions.
45 changes: 12 additions & 33 deletions src/zkbob/manager/MPCGuard.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pragma solidity 0.8.15;

import "../../../src/zkbob/ZkBobPool.sol";
import "../../utils/Ownable.sol";
import "../utils/CustomABIDecoder.sol";

import "../../interfaces/IZkBobPool.sol";

contract MPCGuard is Ownable, CustomABIDecoder {

address[] private guards;

address operator;
Expand Down Expand Up @@ -40,21 +40,15 @@ contract MPCGuard is Ownable, CustomABIDecoder {
guards = _guards;
}


modifier calldataVerified() {
(uint8 count, bytes calldata signatures) = _mpc_signatures();
require(count == guards.length, "MPCWrapper: wrong quorum");
bytes32 digest = ECDSA.toEthSignedMessageHash(
keccak256(_mpc_message())
);
bytes32 digest = ECDSA.toEthSignedMessageHash(keccak256(_mpc_message()));
require(checkQuorum(signatures, digest));
_;
}

function checkQuorum(
bytes calldata signatures,
bytes32 _digest
) internal view returns (bool) {
function checkQuorum(bytes calldata signatures, bytes32 _digest) internal view returns (bool) {
uint256 offset = 0;
assembly {
offset := signatures.offset
Expand All @@ -75,7 +69,7 @@ contract MPCGuard is Ownable, CustomABIDecoder {
return true;
}

function transact() external calldataVerified {
function transact() external calldataVerified onlyOperator {
return propagate();
}

Expand All @@ -90,7 +84,10 @@ contract MPCGuard is Ownable, CustomABIDecoder {
uint256[8] calldata _batch_deposit_proof,
uint256[8] memory _tree_proof,
bytes calldata signatures
) external {
)
external
onlyOperator
{
require(signatures.length == guards.length * SIGNATURE_SIZE, "MPCWrapper: wrong quorum");

bytes memory mpc_message = abi.encodePacked(
Expand All @@ -105,13 +102,7 @@ contract MPCGuard is Ownable, CustomABIDecoder {
bytes32 digest = ECDSA.toEthSignedMessageHash(keccak256(mpc_message));

require(checkQuorum(signatures, digest));
IZkBobPool(pool).appendDirectDeposits(
_root_after,
_indices,
_out_commit,
_batch_deposit_proof,
_tree_proof
);
IZkBobPool(pool).appendDirectDeposits(_root_after, _indices, _out_commit, _batch_deposit_proof, _tree_proof);
}

function propagate() internal {
Expand All @@ -125,27 +116,15 @@ contract MPCGuard is Ownable, CustomABIDecoder {

// Call the implementation.
// out and outsize are 0 because we don't know the size yet.
let result := call(
gas(),
contractAddress,
0,
0,
_calldatasize,
0,
0
)
let result := call(gas(), contractAddress, 0, 0, _calldatasize, 0, 0)

// Copy the returned data.
returndatacopy(0, 0, returndatasize())

switch result
// delegatecall returns 0 on error.
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}
}
17 changes: 7 additions & 10 deletions src/zkbob/utils/CustomABIDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,25 @@ contract CustomABIDecoder {
r = address(uint160(_loaduint256(memo_permit_holder_pos + memo_permit_holder_size - uint256_size)));
}

function _mpc_signatures_pos() internal pure returns ( uint256 pos) {
uint256 t = _tx_type();
if (t==3 || t == 0) {
pos = _sign_r_vs_pos() + sign_r_vs_size;
function _mpc_signatures_pos() internal pure returns (uint256 pos) {
uint256 t = _tx_type();
if (t == 3 || t == 0) {
pos = _sign_r_vs_pos() + sign_r_vs_size;
} else {
pos = _sign_r_vs_pos();
}
}


function _mpc_message() internal pure returns (bytes calldata message) {
uint256 message_length = _mpc_signatures_pos();
assembly
{
assembly {
message.offset := 0
message.length := message_length
}

}

uint256 constant signatures_count_size = 1;

function _mpc_signatures() internal pure returns (uint8 count, bytes calldata signatures) {
uint256 offset = _mpc_signatures_pos();
count = uint8(_loaduint256(offset + signatures_count_size - uint256_size));
Expand All @@ -222,6 +220,5 @@ contract CustomABIDecoder {
signatures.offset := offset
signatures.length := length
}

}
}
85 changes: 50 additions & 35 deletions test/zkbob/ZkBobPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {

uint256 constant initialRoot = 11469701942666298368112882412133877458305516134926649826543144744382391691533;

address [] signers;
address[] guardians;

enum PoolType {
BOB,
ETH,
Expand Down Expand Up @@ -98,27 +99,42 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
ZkBobPool impl;
if (poolType == PoolType.ETH) {
impl = new ZkBobPoolETH(
0, token,
new TransferVerifierMock(), new TreeUpdateVerifierMock(), new BatchDepositVerifierMock(),
address(queueProxy), permit2
0,
token,
new TransferVerifierMock(),
new TreeUpdateVerifierMock(),
new BatchDepositVerifierMock(),
address(queueProxy),
permit2
);
} else if (poolType == PoolType.BOB) {
impl = new ZkBobPoolBOB(
0, token,
new TransferVerifierMock(), new TreeUpdateVerifierMock(), new BatchDepositVerifierMock(),
0,
token,
new TransferVerifierMock(),
new TreeUpdateVerifierMock(),
new BatchDepositVerifierMock(),
address(queueProxy)
);
} else if (poolType == PoolType.USDC) {
impl = new ZkBobPoolUSDC(
0, token,
new TransferVerifierMock(), new TreeUpdateVerifierMock(), new BatchDepositVerifierMock(),
0,
token,
new TransferVerifierMock(),
new TreeUpdateVerifierMock(),
new BatchDepositVerifierMock(),
address(queueProxy)
);
} else if (poolType == PoolType.ERC20) {
impl = new ZkBobPoolERC20(
0, token,
new TransferVerifierMock(), new TreeUpdateVerifierMock(), new BatchDepositVerifierMock(),
address(queueProxy), permit2, 1_000_000_000
0,
token,
new TransferVerifierMock(),
new TreeUpdateVerifierMock(),
new BatchDepositVerifierMock(),
address(queueProxy),
permit2,
1_000_000_000
);
}

Expand Down Expand Up @@ -147,17 +163,17 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
0
);
pool.setAccounting(accounting);
if(isMPC) {
if (isMPC) {
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");
signers.push(guard1Addr);
signers.push(guard2Addr);
MPCGuard(operatorContract).setGuards(signers);
(address guard1Addr,) = makeAddrAndKey("guard1");
(address guard2Addr,) = makeAddrAndKey("guard2");
guardians.push(guard1Addr);
guardians.push(guard2Addr);
MPCGuard(operatorContract).setGuards(guardians);
} else {
operatorManager = new MutableOperatorManager(user2, user3, "https://example.com");
operatorManager = new MutableOperatorManager(user2, user3, "https://example.com");
}
pool.setOperatorManager(operatorManager);
queue.setOperatorManager(operatorManager);
Expand Down Expand Up @@ -738,30 +754,29 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(pk1, ECDSA.toEthSignedMessageHash(nullifier));
bytes memory data = abi.encodePacked(
ZkBobPool.transact.selector, //4
nullifier,//32
_randFR(),//32
uint48(0),//6
uint112(0),//14
int64(_amount / int256(denominator))//8
);//96
for (uint256 i = 0; i < 17; i++) {//32*17 = 544
nullifier, //32
_randFR(), //32
uint48(0), //6
uint112(0), //14
int64(_amount / int256(denominator)) //8
); //96
for (uint256 i = 0; i < 17; i++) {
//32*17 = 544
data = abi.encodePacked(data, _randFR());
}
data = abi.encodePacked(
data,
uint16(0)//2
data,
uint16(0) //2
); //642
bytes memory memo = abi.encodePacked(
uint16(44), //2
uint64(_fee / denominator), //8
bytes4(0x01000000),//4
_randFR()//32
);
data = abi.encodePacked(data,memo);//688
data = abi.encodePacked(data, r, uint256(s) + (v == 28 ? (1 << 255) : 0));//688+64=752
uint64(_fee / denominator), //8
bytes4(0x01000000), //4
_randFR() //32
);
data = abi.encodePacked(data, memo); //688
data = abi.encodePacked(data, r, uint256(s) + (v == 28 ? (1 << 255) : 0)); //688+64=752
return data;


}

function _encodeWithdrawal(
Expand Down
Loading

0 comments on commit 54e6e06

Please sign in to comment.