mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-05-06 08:15:41 -04:00
use bytes32 for hashes
This commit is contained in:
parent
74913e67b2
commit
ac8fc08cc2
7 changed files with 81 additions and 71 deletions
|
@ -24,18 +24,18 @@ contract MerkleTreeWithHistory {
|
|||
// the following variables are made public for easier testing and debugging and
|
||||
// are not supposed to be accessed in regular code
|
||||
uint32 public constant ROOT_HISTORY_SIZE = 100;
|
||||
uint256[ROOT_HISTORY_SIZE] public roots;
|
||||
bytes32[ROOT_HISTORY_SIZE] public roots;
|
||||
uint32 public currentRootIndex = 0;
|
||||
uint32 public nextIndex = 0;
|
||||
uint256[] public filledSubtrees;
|
||||
uint256[] public zeros;
|
||||
bytes32[] public filledSubtrees;
|
||||
bytes32[] public zeros;
|
||||
|
||||
constructor(uint32 _treeLevels) public {
|
||||
require(_treeLevels > 0, "_treeLevels should be greater than zero");
|
||||
require(_treeLevels < 32, "_treeLevels should be less than 32");
|
||||
levels = _treeLevels;
|
||||
|
||||
uint256 currentZero = ZERO_VALUE;
|
||||
bytes32 currentZero = bytes32(ZERO_VALUE);
|
||||
zeros.push(currentZero);
|
||||
filledSubtrees.push(currentZero);
|
||||
|
||||
|
@ -51,24 +51,24 @@ contract MerkleTreeWithHistory {
|
|||
/**
|
||||
@dev Hash 2 tree leaves, returns MiMC(_left, _right)
|
||||
*/
|
||||
function hashLeftRight(uint256 _left, uint256 _right) public pure returns (uint256) {
|
||||
require(_left < FIELD_SIZE, "_left should be inside the field");
|
||||
require(_right < FIELD_SIZE, "_right should be inside the field");
|
||||
uint256 R = _left;
|
||||
function hashLeftRight(bytes32 _left, bytes32 _right) public pure returns (bytes32) {
|
||||
require(uint256(_left) < FIELD_SIZE, "_left should be inside the field");
|
||||
require(uint256(_right) < FIELD_SIZE, "_right should be inside the field");
|
||||
uint256 R = uint256(_left);
|
||||
uint256 C = 0;
|
||||
(R, C) = Hasher.MiMCSponge(R, C, 0);
|
||||
R = addmod(R, _right, FIELD_SIZE);
|
||||
R = addmod(R, uint256(_right), FIELD_SIZE);
|
||||
(R, C) = Hasher.MiMCSponge(R, C, 0);
|
||||
return R;
|
||||
return bytes32(R);
|
||||
}
|
||||
|
||||
function _insert(uint256 _leaf) internal returns(uint32 index) {
|
||||
function _insert(bytes32 _leaf) internal returns(uint32 index) {
|
||||
uint32 currentIndex = nextIndex;
|
||||
require(currentIndex != uint32(2)**levels, "Merkle tree is full. No more leafs can be added");
|
||||
nextIndex += 1;
|
||||
uint256 currentLevelHash = _leaf;
|
||||
uint256 left;
|
||||
uint256 right;
|
||||
bytes32 currentLevelHash = _leaf;
|
||||
bytes32 left;
|
||||
bytes32 right;
|
||||
|
||||
for (uint32 i = 0; i < levels; i++) {
|
||||
if (currentIndex % 2 == 0) {
|
||||
|
@ -94,7 +94,7 @@ contract MerkleTreeWithHistory {
|
|||
/**
|
||||
@dev Whether the root is present in the root history
|
||||
*/
|
||||
function isKnownRoot(uint256 _root) public view returns(bool) {
|
||||
function isKnownRoot(bytes32 _root) public view returns(bool) {
|
||||
if (_root == 0) {
|
||||
return false;
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ contract MerkleTreeWithHistory {
|
|||
/**
|
||||
@dev Returns the last root
|
||||
*/
|
||||
function getLastRoot() public view returns(uint256) {
|
||||
function getLastRoot() public view returns(bytes32) {
|
||||
return roots[currentRootIndex];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ contract IVerifier {
|
|||
|
||||
contract Mixer is MerkleTreeWithHistory {
|
||||
uint256 public denomination;
|
||||
mapping(uint256 => bool) public nullifierHashes;
|
||||
mapping(bytes32 => bool) public nullifierHashes;
|
||||
// we store all commitments just to prevent accidental deposits with the same commitment
|
||||
mapping(uint256 => bool) public commitments;
|
||||
mapping(bytes32 => bool) public commitments;
|
||||
IVerifier public verifier;
|
||||
|
||||
// operator can
|
||||
|
@ -35,8 +35,8 @@ contract Mixer is MerkleTreeWithHistory {
|
|||
_;
|
||||
}
|
||||
|
||||
event Deposit(uint256 indexed commitment, uint32 leafIndex, uint256 timestamp);
|
||||
event Withdrawal(address to, uint256 nullifierHash, address indexed relayer, uint256 fee);
|
||||
event Deposit(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp);
|
||||
event Withdrawal(address to, bytes32 nullifierHash, address indexed relayer, uint256 fee);
|
||||
|
||||
/**
|
||||
@dev The constructor
|
||||
|
@ -61,7 +61,7 @@ contract Mixer is MerkleTreeWithHistory {
|
|||
@dev Deposit funds into mixer. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this mixer.
|
||||
@param _commitment the note commitment, which is PedersenHash(nullifier + secret)
|
||||
*/
|
||||
function deposit(uint256 _commitment) public payable {
|
||||
function deposit(bytes32 _commitment) public payable {
|
||||
require(!isDepositsDisabled, "deposits are disabled");
|
||||
require(!commitments[_commitment], "The commitment has been submitted");
|
||||
|
||||
|
@ -83,11 +83,11 @@ contract Mixer is MerkleTreeWithHistory {
|
|||
- the receiver of funds
|
||||
- optional fee that goes to the transaction sender (usually a relay)
|
||||
*/
|
||||
function withdraw(bytes memory _proof, uint256 _root, uint256 _nullifierHash, address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) public payable {
|
||||
function withdraw(bytes memory _proof, bytes32 _root, bytes32 _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");
|
||||
require(verifier.verifyProof(_proof, [uint256(_root), uint256(_nullifierHash), uint256(_receiver), uint256(_relayer), _fee, _refund]), "Invalid withdraw proof");
|
||||
|
||||
nullifierHashes[_nullifierHash] = true;
|
||||
_processWithdraw(_receiver, _relayer, _fee, _refund);
|
||||
|
@ -98,7 +98,7 @@ contract Mixer is MerkleTreeWithHistory {
|
|||
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal;
|
||||
|
||||
/** @dev whether a note is already spent */
|
||||
function isSpent(uint256 _nullifierHash) public view returns(bool) {
|
||||
function isSpent(bytes32 _nullifierHash) public view returns(bool) {
|
||||
return nullifierHashes[_nullifierHash];
|
||||
}
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ import '../MerkleTreeWithHistory.sol';
|
|||
|
||||
contract MerkleTreeWithHistoryMock is MerkleTreeWithHistory {
|
||||
|
||||
constructor (uint8 _treeLevels) MerkleTreeWithHistory(_treeLevels) public {}
|
||||
constructor (uint32 _treeLevels) MerkleTreeWithHistory(_treeLevels) public {}
|
||||
|
||||
function insert(uint256 _leaf) public {
|
||||
function insert(bytes32 _leaf) public {
|
||||
_insert(_leaf);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue