2019-08-02 13:12:30 -04:00
|
|
|
// https://tornado.cash
|
|
|
|
/*
|
|
|
|
* d888888P dP a88888b. dP
|
|
|
|
* 88 88 d8' `88 88
|
|
|
|
* 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b.
|
|
|
|
* 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88
|
|
|
|
* 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88
|
|
|
|
* dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP
|
|
|
|
* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
|
|
|
|
*/
|
|
|
|
|
2019-07-09 09:05:30 -04:00
|
|
|
pragma solidity ^0.5.8;
|
|
|
|
|
|
|
|
import "./MerkleTreeWithHistory.sol";
|
|
|
|
|
|
|
|
contract IVerifier {
|
2019-11-04 14:42:41 -05:00
|
|
|
function verifyProof(bytes memory _proof, uint256[6] memory _input) public returns(bool);
|
2019-07-09 09:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
contract Mixer is MerkleTreeWithHistory {
|
2019-10-04 10:27:47 -04:00
|
|
|
uint256 public denomination;
|
2019-07-25 09:58:21 -04:00
|
|
|
mapping(uint256 => bool) public nullifierHashes;
|
2019-07-15 11:04:48 -04:00
|
|
|
// we store all commitments just to prevent accidental deposits with the same commitment
|
|
|
|
mapping(uint256 => bool) public commitments;
|
2019-08-04 13:57:01 -04:00
|
|
|
IVerifier public verifier;
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
// operator can
|
|
|
|
// - disable new deposits in case of emergency
|
|
|
|
// - update snark verification key until this ability is permanently disabled
|
2019-10-31 20:56:24 -04:00
|
|
|
address public operator;
|
2019-10-31 21:11:08 -04:00
|
|
|
bool public isDepositsDisabled;
|
|
|
|
bool public isVerifierUpdateDisabled;
|
2019-10-04 10:27:47 -04:00
|
|
|
modifier onlyOperator {
|
|
|
|
require(msg.sender == operator, "Only operator can call this function.");
|
|
|
|
_;
|
|
|
|
}
|
2019-07-09 09:05:30 -04:00
|
|
|
|
2019-11-03 05:11:22 -05:00
|
|
|
event Deposit(uint256 indexed commitment, uint32 leafIndex, uint256 timestamp);
|
2019-10-31 21:12:32 -04:00
|
|
|
event Withdrawal(address to, uint256 nullifierHash, address indexed relayer, uint256 fee);
|
2019-07-09 09:05:30 -04:00
|
|
|
|
2019-07-10 08:35:46 -04:00
|
|
|
/**
|
|
|
|
@dev The constructor
|
|
|
|
@param _verifier the address of SNARK verifier for this contract
|
2019-11-04 14:45:56 -05:00
|
|
|
@param _denomination transfer amount for each deposit
|
2019-08-27 16:42:24 -04:00
|
|
|
@param _merkleTreeHeight the height of deposits' Merkle Tree
|
2019-11-04 14:45:56 -05:00
|
|
|
@param _operator operator address (see operator comment above)
|
2019-07-10 08:35:46 -04:00
|
|
|
*/
|
2019-07-12 11:04:45 -04:00
|
|
|
constructor(
|
2019-10-31 21:14:01 -04:00
|
|
|
IVerifier _verifier,
|
2019-10-04 10:27:47 -04:00
|
|
|
uint256 _denomination,
|
2019-11-03 05:11:22 -05:00
|
|
|
uint32 _merkleTreeHeight,
|
2019-10-31 20:56:24 -04:00
|
|
|
address _operator
|
2019-11-02 08:35:22 -04:00
|
|
|
) MerkleTreeWithHistory(_merkleTreeHeight) public {
|
2019-10-31 21:15:55 -04:00
|
|
|
require(_denomination > 0, "denomination should be greater than 0");
|
2019-10-31 21:14:01 -04:00
|
|
|
verifier = _verifier;
|
2019-08-02 13:12:30 -04:00
|
|
|
operator = _operator;
|
2019-10-04 10:27:47 -04:00
|
|
|
denomination = _denomination;
|
2019-07-09 09:05:30 -04:00
|
|
|
}
|
2019-10-04 10:27:47 -04:00
|
|
|
|
2019-09-06 17:22:30 -04:00
|
|
|
/**
|
2019-10-04 10:27:47 -04:00
|
|
|
@dev Deposit funds into mixer. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this mixer.
|
2019-11-02 08:48:22 -04:00
|
|
|
@param _commitment the note commitment, which is PedersenHash(nullifier + secret)
|
2019-09-06 17:22:30 -04:00
|
|
|
*/
|
2019-11-02 08:48:22 -04:00
|
|
|
function deposit(uint256 _commitment) public payable {
|
2019-10-31 21:11:08 -04:00
|
|
|
require(!isDepositsDisabled, "deposits are disabled");
|
2019-11-02 08:48:22 -04:00
|
|
|
require(!commitments[_commitment], "The commitment has been submitted");
|
2019-11-04 14:42:41 -05:00
|
|
|
|
2019-11-03 05:11:22 -05:00
|
|
|
uint32 insertedIndex = _insert(_commitment);
|
2019-11-02 08:48:22 -04:00
|
|
|
commitments[_commitment] = true;
|
2019-10-31 21:00:32 -04:00
|
|
|
_processDeposit();
|
2019-07-09 09:05:30 -04:00
|
|
|
|
2019-11-02 08:48:22 -04:00
|
|
|
emit Deposit(_commitment, insertedIndex, block.timestamp);
|
2019-09-06 17:22:30 -04:00
|
|
|
}
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
/** @dev this function is defined in a child contract */
|
2019-10-31 21:17:00 -04:00
|
|
|
function _processDeposit() internal;
|
2019-10-04 10:27:47 -04:00
|
|
|
|
2019-09-06 17:22:30 -04:00
|
|
|
/**
|
2019-11-04 14:45:56 -05:00
|
|
|
@dev Withdraw a deposit from the mixer. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs
|
2019-09-06 17:22:30 -04:00
|
|
|
`input` array consists of:
|
|
|
|
- merkle root of all deposits in the mixer
|
|
|
|
- hash of unique deposit nullifier to prevent double spends
|
|
|
|
- the receiver of funds
|
|
|
|
- optional fee that goes to the transaction sender (usually a relay)
|
|
|
|
*/
|
2019-11-04 14:42:41 -05:00
|
|
|
function withdraw(bytes memory _proof, uint256 _root, uint256 _nullifierHash, address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) public payable {
|
|
|
|
require(_fee <= denomination, "Fee exceeds transfer value");
|
|
|
|
require(!nullifierHashes[_nullifierHash], "The note has been already spent");
|
|
|
|
require(isKnownRoot(_root), "Cannot find your merkle root"); // Make sure to use a recent one
|
|
|
|
require(verifier.verifyProof(_proof, [_root, _nullifierHash, uint256(_receiver), uint256(_relayer), _fee, _refund]), "Invalid withdraw proof");
|
|
|
|
|
|
|
|
nullifierHashes[_nullifierHash] = true;
|
|
|
|
_processWithdraw(_receiver, _relayer, _fee, _refund);
|
|
|
|
emit Withdrawal(_receiver, _nullifierHash, _relayer, _fee);
|
2019-07-09 09:05:30 -04:00
|
|
|
}
|
2019-07-15 12:15:06 -04:00
|
|
|
|
2019-10-04 10:27:47 -04:00
|
|
|
/** @dev this function is defined in a child contract */
|
2019-10-31 21:17:00 -04:00
|
|
|
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal;
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
/** @dev whether a note is already spent */
|
2019-11-02 08:48:22 -04:00
|
|
|
function isSpent(uint256 _nullifierHash) public view returns(bool) {
|
|
|
|
return nullifierHashes[_nullifierHash];
|
2019-10-04 10:27:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@dev Allow operator to temporarily disable new deposits. This is needed to protect users funds in case a vulnerability is discovered.
|
|
|
|
It does not affect existing deposits.
|
|
|
|
*/
|
2019-10-31 21:11:08 -04:00
|
|
|
function toggleDeposits(bool _state) external onlyOperator {
|
|
|
|
isDepositsDisabled = _state;
|
2019-08-01 04:41:22 -04:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:27:47 -04:00
|
|
|
/**
|
|
|
|
@dev allow operator to update SNARK verification keys. This is needed to update keys after the final trusted setup ceremony is held.
|
|
|
|
After that operator is supposed to permanently disable this ability.
|
|
|
|
*/
|
2019-11-02 08:48:22 -04:00
|
|
|
function updateVerifier(address _newVerifier) external onlyOperator {
|
2019-10-31 21:11:08 -04:00
|
|
|
require(!isVerifierUpdateDisabled, "Verifier updates have been disabled.");
|
2019-11-02 08:48:22 -04:00
|
|
|
verifier = IVerifier(_newVerifier);
|
2019-10-04 09:43:15 -04:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:27:47 -04:00
|
|
|
/**
|
|
|
|
@dev an option for operator to permanently disable verification keys update ability.
|
|
|
|
This is supposed to be called after the final trusted setup ceremony is held.
|
|
|
|
*/
|
|
|
|
function disableVerifierUpdate() external onlyOperator {
|
2019-10-31 21:11:08 -04:00
|
|
|
isVerifierUpdateDisabled = true;
|
2019-10-04 09:43:15 -04:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:27:47 -04:00
|
|
|
/** @dev operator can change his address */
|
2019-10-31 21:28:46 -04:00
|
|
|
function changeOperator(address _newOperator) external onlyOperator {
|
|
|
|
operator = _newOperator;
|
2019-08-01 12:58:34 -04:00
|
|
|
}
|
2019-07-10 12:58:21 -04:00
|
|
|
}
|