Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wdy1 committed Dec 15, 2023
1 parent 7233afc commit 25d442b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
27 changes: 14 additions & 13 deletions src/zkbob/manager/MPCWrapper.sol → src/zkbob/manager/MPCGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import "../utils/CustomABIDecoder.sol";

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

contract MPCWrapper is Ownable, CustomABIDecoder {
address[] private signers;
contract MPCGuard is Ownable, CustomABIDecoder {

address[] private guards;

address operator;

address public immutable pool;

uint256 constant SIGNATURE_SIZE = 64;

constructor(address _operator, address _pool) {
pool = _pool;
_setOperator(_operator);
Expand All @@ -33,31 +36,30 @@ contract MPCWrapper is Ownable, CustomABIDecoder {
_setOperator(_operator);
}

function setSigners(address[] calldata _signers) external onlyOwner {
signers = _signers;
function setGuards(address[] calldata _guards) external onlyOwner {
guards = _guards;
}


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

function checkQuorum(
uint8 count,
bytes calldata signatures,
bytes32 _digest
) internal returns (bool) {
) internal view returns (bool) {
uint256 offset = 0;
assembly {
offset := signatures.offset
}
for (uint256 index = 0; index < signers.length; index++) {
for (uint256 index = 0; index < guards.length; index++) {
bytes32 r;
bytes32 vs;
assembly {
Expand All @@ -66,7 +68,7 @@ contract MPCWrapper is Ownable, CustomABIDecoder {
offset := add(offset, 64)
}
address signer = ECDSA.recover(_digest, r, vs);
if (signer != signers[index]) {
if (signer != guards[index]) {
return false;
}
}
Expand All @@ -87,10 +89,9 @@ contract MPCWrapper is Ownable, CustomABIDecoder {
uint256 _out_commit,
uint256[8] calldata _batch_deposit_proof,
uint256[8] memory _tree_proof,
uint8 mpc_count,
bytes calldata signatures
) external {
require(mpc_count == signers.length, "MPCWrapper: wrong quorum");
require(signatures.length == guards.length * SIGNATURE_SIZE, "MPCWrapper: wrong quorum");

bytes memory mpc_message = abi.encodePacked(
_root_after,
Expand All @@ -102,7 +103,7 @@ contract MPCWrapper is Ownable, CustomABIDecoder {

bytes32 digest = ECDSA.toEthSignedMessageHash(keccak256(mpc_message));

require(checkQuorum(mpc_count, signatures, digest));
require(checkQuorum(signatures, digest));
IZkBobPool(pool).appendDirectDeposits(
_root_after,
_indices,
Expand Down
16 changes: 8 additions & 8 deletions test/zkbob/ZkBobPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import "../mocks/DummyImpl.sol";
import "../../src/proxy/EIP1967Proxy.sol";
import "../../src/zkbob/ZkBobPool.sol";
import "../../src/zkbob/ZkBobDirectDepositQueue.sol";
import "../../src/zkbob/manager/MPCWrapper.sol";
import "../../src/zkbob/manager/MPCGuard.sol";
import "../../src/zkbob/manager/MutableOperatorManager.sol";
import "../../src/zkbob/manager/kyc/SimpleKYCProviderManager.sol";
import "../interfaces/IZkBobDirectDepositsAdmin.sol";
Expand Down Expand Up @@ -147,15 +147,15 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
0
);
pool.setAccounting(accounting);
address operatorEOA = makeAddr("operatorEOA");
if(isMPC) {
address operatorContract = address(new MPCWrapper(operatorEOA, address(pool)));
address operatorEOA = makeAddr("operatorEOA");
address operatorContract = address(new MPCGuard(operatorEOA, address(pool)));
operatorManager = new MutableOperatorManager(operatorContract, user3, "https://example.com");
(address signer1Addr, uint256 signer1Key) = makeAddrAndKey("signer1");
(address signer2Addr, uint256 signer2Key) = makeAddrAndKey("signer2");
signers.push(signer1Addr);
signers.push(signer2Addr);
MPCWrapper(operatorContract).setSigners(signers);
(address guard1Addr, ) = makeAddrAndKey("guard1");
(address guard2Addr, ) = makeAddrAndKey("guard2");
signers.push(guard1Addr);
signers.push(guard2Addr);
MPCGuard(operatorContract).setGuards(signers);
} else {
operatorManager = new MutableOperatorManager(user2, user3, "https://example.com");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "../../shared/Env.t.sol";

import "../../shared/ForkTests.t.sol";

import "../../../src/zkbob/manager/MPCWrapper.sol";
import "../../../src/zkbob/manager/MPCGuard.sol";

contract MPCOperatorManagerTest is
AbstractZkBobPoolTest,
Expand Down Expand Up @@ -48,14 +48,14 @@ contract MPCOperatorManagerTest is
}

function withMPC(bytes memory data) internal returns (bytes memory) {
(address signer1Addr, uint256 signer1Key) = makeAddrAndKey("signer1");
(address signer2Addr, uint256 signer2Key) = makeAddrAndKey("signer2");
(address guard1Addr, uint256 guard1Key) = makeAddrAndKey("guard1");
(address guard2Addr, uint256 guard2Key) = makeAddrAndKey("guard2");
return
abi.encodePacked(
data,
uint8(2), //753
sign(data, signer1Key), //817
sign(data, signer2Key) //881
sign(data, guard1Key), //817
sign(data, guard2Key) //881
);
}

Expand Down Expand Up @@ -127,26 +127,25 @@ contract MPCOperatorManagerTest is
tree_proof
);

(, uint256 signer1Key) = makeAddrAndKey("signer1");
(, uint256 signer2Key) = makeAddrAndKey("signer2");
(, uint256 guard1Key) = makeAddrAndKey("guard1");
(, uint256 guard2Key) = makeAddrAndKey("guard2");


MPCWrapper(wrapper).appendDirectDepositsMPC(
MPCGuard(wrapper).appendDirectDepositsMPC(
root_afer,
indices,
outCommitment,
batch_deposit_proof,
tree_proof,
2,
abi.encodePacked(sign(mpcMessage, signer1Key), sign(mpcMessage, signer2Key))
abi.encodePacked(sign(mpcMessage, guard1Key), sign(mpcMessage, guard2Key))
);

}

function sign(
bytes memory data,
uint256 key
) internal returns (bytes memory signatureData) {
) internal pure returns (bytes memory signatureData) {

bytes32 digest = ECDSA.toEthSignedMessageHash(keccak256(data));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(key, digest);
Expand Down

0 comments on commit 25d442b

Please sign in to comment.