Tornado.sol 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // SPDX-License-Identifier: MIT
  12. pragma solidity ^0.7.0;
  13. import "./MerkleTreeWithHistory.sol";
  14. import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
  15. interface IVerifier {
  16. function verifyProof(bytes memory _proof, uint256[6] memory _input) external returns (bool);
  17. }
  18. abstract contract Tornado is MerkleTreeWithHistory, ReentrancyGuard {
  19. IVerifier public immutable verifier;
  20. uint256 public denomination;
  21. mapping(bytes32 => bool) public nullifierHashes;
  22. // we store all commitments just to prevent accidental deposits with the same commitment
  23. mapping(bytes32 => bool) public commitments;
  24. event Deposit(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp);
  25. event Withdrawal(address to, bytes32 nullifierHash, address indexed relayer, uint256 fee);
  26. /**
  27. @dev The constructor
  28. @param _verifier the address of SNARK verifier for this contract
  29. @param _hasher the address of MiMC hash contract
  30. @param _denomination transfer amount for each deposit
  31. @param _merkleTreeHeight the height of deposits' Merkle Tree
  32. */
  33. constructor(
  34. IVerifier _verifier,
  35. IHasher _hasher,
  36. uint256 _denomination,
  37. uint32 _merkleTreeHeight
  38. ) MerkleTreeWithHistory(_merkleTreeHeight, _hasher) {
  39. require(_denomination > 0, "denomination should be greater than 0");
  40. verifier = _verifier;
  41. denomination = _denomination;
  42. }
  43. /**
  44. @dev Deposit funds into the contract. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this instance.
  45. @param _commitment the note commitment, which is PedersenHash(nullifier + secret)
  46. */
  47. function deposit(bytes32 _commitment) external payable nonReentrant {
  48. require(!commitments[_commitment], "The commitment has been submitted");
  49. uint32 insertedIndex = _insert(_commitment);
  50. commitments[_commitment] = true;
  51. _processDeposit();
  52. emit Deposit(_commitment, insertedIndex, block.timestamp);
  53. }
  54. /** @dev this function is defined in a child contract */
  55. function _processDeposit() internal virtual;
  56. /**
  57. @dev Withdraw a deposit from the contract. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs
  58. `input` array consists of:
  59. - merkle root of all deposits in the contract
  60. - hash of unique deposit nullifier to prevent double spends
  61. - the recipient of funds
  62. - optional fee that goes to the transaction sender (usually a relay)
  63. */
  64. function withdraw(
  65. bytes calldata _proof,
  66. bytes32 _root,
  67. bytes32 _nullifierHash,
  68. address payable _recipient,
  69. address payable _relayer,
  70. uint256 _fee,
  71. uint256 _refund
  72. ) external payable nonReentrant {
  73. require(_fee <= denomination, "Fee exceeds transfer value");
  74. require(!nullifierHashes[_nullifierHash], "The note has been already spent");
  75. require(isKnownRoot(_root), "Cannot find your merkle root"); // Make sure to use a recent one
  76. require(
  77. verifier.verifyProof(
  78. _proof,
  79. [uint256(_root), uint256(_nullifierHash), uint256(_recipient), uint256(_relayer), _fee, _refund]
  80. ),
  81. "Invalid withdraw proof"
  82. );
  83. nullifierHashes[_nullifierHash] = true;
  84. _processWithdraw(_recipient, _relayer, _fee, _refund);
  85. emit Withdrawal(_recipient, _nullifierHash, _relayer, _fee);
  86. }
  87. /** @dev this function is defined in a child contract */
  88. function _processWithdraw(
  89. address payable _recipient,
  90. address payable _relayer,
  91. uint256 _fee,
  92. uint256 _refund
  93. ) internal virtual;
  94. /** @dev whether a note is already spent */
  95. function isSpent(bytes32 _nullifierHash) public view returns (bool) {
  96. return nullifierHashes[_nullifierHash];
  97. }
  98. /** @dev whether an array of notes is already spent */
  99. function isSpentArray(bytes32[] calldata _nullifierHashes) external view returns (bool[] memory spent) {
  100. spent = new bool[](_nullifierHashes.length);
  101. for (uint256 i = 0; i < _nullifierHashes.length; i++) {
  102. if (isSpent(_nullifierHashes[i])) {
  103. spent[i] = true;
  104. }
  105. }
  106. }
  107. }