Skip to content

Commit

Permalink
wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wdy1 committed Dec 13, 2023
1 parent 9c1be99 commit 9f3a9c2
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 154 deletions.
75 changes: 0 additions & 75 deletions src/zkbob/manager/MPCOperatorManager.sol

This file was deleted.

106 changes: 106 additions & 0 deletions src/zkbob/manager/MPCWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
pragma solidity 0.8.15;
import "../../../src/zkbob/ZkBobPool.sol";
import "../../utils/Ownable.sol";
import "../utils/CustomABIDecoder.sol";

contract MPCWrapper is Ownable, CustomABIDecoder {

address[] private signers;

address operator;

address public immutable pool;

constructor(
address _operator,
address _pool
) {
pool = _pool;
_setOperator(_operator);
}

function _setOperator(address _operator) internal {
operator = _operator;
}
function setOperator(address _operator) external onlyOwner {
_setOperator(_operator);
}


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

modifier requiresProofVerification() {
require(isVerified(), "MPCWrapper: proof verification failed");
_;
}

function isVerified() internal view returns (bool) {
(uint8 count, bytes calldata signatures) = _mpc_signatures();
uint256 _signersCount = signers.length;
require(count == _signersCount, "MPCWrapper: wrong quorum");
uint256 offset = 0;
assembly {
offset := signatures.offset
}
for (uint256 index = 0; index < _signersCount; index++) {
bytes32 r;
bytes32 vs;
assembly {
r := calldataload(offset)
vs := calldataload(add(32, offset))
offset := add(offset, 64)
}
console2.log("step", index);
console2.logBytes32(r);
console2.logBytes32(vs);
address signer = ECDSA.recover(
ECDSA.toEthSignedMessageHash(keccak256(_mpc_message())),
r,
vs
);
if (signer != signers[index]) {
console2.log("signer", signer);
console2.log("signers[index]", signers[index]);
return false;
}
}
return true;
}

function transact() external requiresProofVerification {
return propagate();
}

function appendDirectDeposit() external requiresProofVerification {
return propagate();
}

function propagate() internal {
address contractAddress = pool;
assembly {
// Copy msg.data. We take full control of memory in this inline assembly
// block because it will not return to Solidity code. We overwrite the
// Solidity scratch pad at memory position 0.
calldatacopy(0, 0, calldatasize())

// Call the implementation.
// out and outsize are 0 because we don't know the size yet.
let result := delegatecall(gas(), contractAddress, 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())
}
}
}

}
14 changes: 9 additions & 5 deletions src/zkbob/utils/CustomABIDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ contract CustomABIDecoder {

function _mpc_signatures_pos() internal pure returns ( uint256 pos) {
uint256 t = _tx_type();
if (t==3) {
if (t==3 || t == 0) {
pos = _sign_r_vs_pos() + sign_r_vs_size;
} else {
pos = _sign_r_vs_pos();
Expand All @@ -208,14 +208,18 @@ contract CustomABIDecoder {
message.offset := 0
message.length := message_length
}

console2.log("_mpc_message");
console2.logBytes(message);
}

uint256 constant signatures_count_size = 1;
function _mpc_signatures() internal pure returns (uint8 count, bytes calldata signatures) {
uint256 countPos = _mpc_signatures_pos();
console2.log("mpc_signatures_pos", mpc_signatures_pos());
count = uint8(_loaduint256(countPos+8-uint256_size));
uint256 offset = _mpc_signatures_pos();
count = uint8(_loaduint256(offset + signatures_count_size - uint256_size));
uint256 length = count * sign_r_vs_size;
uint256 offset = countPos + 8;

offset = offset + signatures_count_size;
assembly {
signatures.offset := offset
signatures.length := length
Expand Down
47 changes: 36 additions & 11 deletions test/zkbob/ZkBobPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import "../mocks/DummyImpl.sol";
import "../../src/proxy/EIP1967Proxy.sol";
import "../../src/zkbob/ZkBobPool.sol";
import "../../src/zkbob/ZkBobDirectDepositQueue.sol";
import "../../src/zkbob/manager/MPCOperatorManager.sol";
import "../../src/zkbob/manager/MPCWrapper.sol";
import "../../src/zkbob/manager/MutableOperatorManager.sol";
import "../../src/zkbob/manager/kyc/SimpleKYCProviderManager.sol";
import "../interfaces/IZkBobDirectDepositsAdmin.sol";
Expand All @@ -29,6 +31,7 @@ import "../../src/zkbob/ZkBobPoolBOB.sol";
import "../../src/zkbob/ZkBobPoolETH.sol";
import "../../src/infra/UniswapV3Seller.sol";
import {EnergyRedeemer} from "../../src/infra/EnergyRedeemer.sol";
import "forge-std/console2.sol";

abstract contract AbstractZkBobPoolTest is AbstractForkTest {
address constant permit2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
Expand All @@ -37,6 +40,7 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {

uint256 constant initialRoot = 11469701942666298368112882412133877458305516134926649826543144744382391691533;

address [] signers;
enum PoolType {
BOB,
ETH,
Expand All @@ -53,6 +57,7 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
address token;
address weth;
address tempToken;
address wrapper;
bool autoApproveQueue;
PoolType poolType;
PermitType permitType;
Expand Down Expand Up @@ -144,7 +149,14 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
0
);
pool.setAccounting(accounting);
operatorManager = new MutableOperatorManager(user2, user3, "https://example.com");
address relayer = makeAddr("relayer");
wrapper = address(new MPCWrapper(relayer, address(pool)));
(address signer1Addr, uint256 signer1Key) = makeAddrAndKey("signer1");
(address signer2Addr, uint256 signer2Key) = makeAddrAndKey("signer2");
signers.push(signer1Addr);
signers.push(signer2Addr);
MPCWrapper(wrapper).setSigners(signers);
operatorManager = new MutableOperatorManager(wrapper, user3, "https://example.com");
pool.setOperatorManager(operatorManager);
queue.setOperatorManager(operatorManager);
queue.setDirectDepositFee(uint64(0.1 ether / D));
Expand Down Expand Up @@ -723,18 +735,31 @@ abstract contract AbstractZkBobPoolTest is AbstractForkTest {
bytes32 nullifier = bytes32(_randFR());
(uint8 v, bytes32 r, bytes32 s) = vm.sign(pk1, ECDSA.toEthSignedMessageHash(nullifier));
bytes memory data = abi.encodePacked(
ZkBobPool.transact.selector,
nullifier,
_randFR(),
uint48(0),
uint112(0),
int64(_amount / int256(denominator))
);
for (uint256 i = 0; i < 17; i++) {
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
data = abi.encodePacked(data, _randFR());
}
data = abi.encodePacked(data, uint16(0), uint16(44), uint64(_fee / denominator), bytes4(0x01000000), _randFR());
return abi.encodePacked(data, r, uint256(s) + (v == 28 ? (1 << 255) : 0));
data = abi.encodePacked(
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
return data;


}

function _encodeWithdrawal(
Expand Down
63 changes: 0 additions & 63 deletions test/zkbob/manager/MPCOperatorManager.t.sol

This file was deleted.

Loading

0 comments on commit 9f3a9c2

Please sign in to comment.