Mixer.sol 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // https://tornado.cash
  2. /*
  3. * d888888P dP a88888b. dP
  4. * 88 88 d8' `88 88
  5. * 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b.
  6. * 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88
  7. * 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88
  8. * dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP
  9. * ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
  10. */
  11. pragma solidity ^0.5.8;
  12. import "./MerkleTreeWithHistory.sol";
  13. contract IVerifier {
  14. function verifyProof(uint256[8] memory proof, uint256[6] memory input) public returns(bool);
  15. }
  16. contract Mixer is MerkleTreeWithHistory {
  17. uint256 public denomination;
  18. mapping(uint256 => bool) public nullifierHashes;
  19. // we store all commitments just to prevent accidental deposits with the same commitment
  20. mapping(uint256 => bool) public commitments;
  21. IVerifier public verifier;
  22. // operator can
  23. // - receive a relayer fee
  24. // - disable new deposits in case of emergency
  25. // - update snark verification key until this ability is permanently disabled
  26. address payable public operator;
  27. bool public isDepositsEnabled = true;
  28. bool public isVerifierUpdateAllowed = true;
  29. modifier onlyOperator {
  30. require(msg.sender == operator, "Only operator can call this function.");
  31. _;
  32. }
  33. event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp);
  34. event Withdraw(address to, uint256 nullifierHash, address indexed relayer, uint256 fee);
  35. /**
  36. @dev The constructor
  37. @param _verifier the address of SNARK verifier for this contract
  38. @param _merkleTreeHeight the height of deposits' Merkle Tree
  39. @param _emptyElement default element of the deposits' Merkle Tree
  40. @param _operator operator address (see operator above)
  41. */
  42. constructor(
  43. address _verifier,
  44. uint256 _denomination,
  45. uint8 _merkleTreeHeight,
  46. uint256 _emptyElement,
  47. address payable _operator
  48. ) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
  49. verifier = IVerifier(_verifier);
  50. operator = _operator;
  51. denomination = _denomination;
  52. }
  53. /**
  54. @dev Deposit funds into mixer. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this mixer.
  55. @param commitment the note commitment, which is PedersenHash(nullifier + secret)
  56. */
  57. function deposit(uint256 commitment) external payable {
  58. require(isDepositsEnabled, "deposits are disabled");
  59. require(!commitments[commitment], "The commitment has been submitted");
  60. _processDeposit();
  61. _insert(commitment);
  62. commitments[commitment] = true;
  63. emit Deposit(commitment, next_index - 1, block.timestamp);
  64. }
  65. /** @dev this function is defined in a child contract */
  66. function _processDeposit() internal {}
  67. /**
  68. @dev Withdraw deposit from the mixer. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs
  69. `input` array consists of:
  70. - merkle root of all deposits in the mixer
  71. - hash of unique deposit nullifier to prevent double spends
  72. - the receiver of funds
  73. - optional fee that goes to the transaction sender (usually a relay)
  74. */
  75. function withdraw(uint256[8] calldata proof, uint256[6] calldata input) external payable {
  76. uint256 root = input[0];
  77. uint256 nullifierHash = input[1];
  78. address payable receiver = address(input[2]);
  79. address payable relayer = address(input[3]);
  80. uint256 fee = input[4];
  81. uint256 refund = input[5];
  82. require(fee < denomination, "Fee exceeds transfer value");
  83. require(!nullifierHashes[nullifierHash], "The note has been already spent");
  84. require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
  85. require(verifier.verifyProof(proof, input), "Invalid withdraw proof");
  86. nullifierHashes[nullifierHash] = true;
  87. _processWithdraw(receiver, relayer, fee, refund);
  88. emit Withdraw(receiver, nullifierHash, relayer, fee);
  89. }
  90. // todo: use this function in withdraw?
  91. /**
  92. @dev same checks as `withdraw` implemented as a view function. Used for relayers.
  93. */
  94. function checkWithdrawalValidity(uint256[8] calldata proof, uint256[6] calldata input) external view {
  95. uint256 root = input[0];
  96. uint256 nullifierHash = input[1];
  97. //address payable receiver = address(input[2]);
  98. //address payable relayer = address(input[3]);
  99. uint256 fee = input[4];
  100. uint256 refund = input[5];
  101. require(fee < denomination, "Fee exceeds transfer value");
  102. require(!nullifierHashes[nullifierHash], "The note has been already spent");
  103. require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
  104. require(verifier.verifyProof(proof, input), "Invalid withdraw proof");
  105. }
  106. /** @dev this function is defined in a child contract */
  107. function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal {}
  108. /** @dev whether a note is already spent */
  109. function isSpent(uint256 nullifier) external view returns(bool) {
  110. return nullifierHashes[nullifier];
  111. }
  112. /**
  113. @dev Allow operator to temporarily disable new deposits. This is needed to protect users funds in case a vulnerability is discovered.
  114. It does not affect existing deposits.
  115. */
  116. function toggleDeposits() external onlyOperator {
  117. isDepositsEnabled = !isDepositsEnabled;
  118. }
  119. /**
  120. @dev allow operator to update SNARK verification keys. This is needed to update keys after the final trusted setup ceremony is held.
  121. After that operator is supposed to permanently disable this ability.
  122. */
  123. function updateVerifier(address newVerifier) external onlyOperator {
  124. require(isVerifierUpdateAllowed, "Verifier updates have been disabled.");
  125. verifier = IVerifier(newVerifier);
  126. }
  127. /**
  128. @dev an option for operator to permanently disable verification keys update ability.
  129. This is supposed to be called after the final trusted setup ceremony is held.
  130. */
  131. function disableVerifierUpdate() external onlyOperator {
  132. isVerifierUpdateAllowed = false;
  133. }
  134. /** @dev operator can change his address */
  135. function changeOperator(address payable _newAccount) external onlyOperator {
  136. operator = _newAccount;
  137. }
  138. }