tornado-core/contracts/MerkleTreeWithHistory.sol

132 lines
3.1 KiB
Solidity
Raw Normal View History

2019-07-09 09:05:30 -04:00
pragma solidity ^0.5.8;
library MiMC {
function MiMCSponge(uint256 in_xL, uint256 in_xR, uint256 in_k) public pure returns (uint256 xL, uint256 xR);
}
contract MerkleTreeWithHistory {
uint8 levels;
uint8 constant ROOT_HISTORY_SIZE = 100;
2019-07-10 12:58:21 -04:00
uint256[] private _roots;
2019-07-09 09:05:30 -04:00
uint256 public current_root = 0;
2019-07-10 12:58:21 -04:00
uint256[] private _filled_subtrees;
uint256[] private _zeros;
2019-07-09 09:05:30 -04:00
uint32 public next_index = 0;
event LeafAdded(uint256 leaf, uint32 leaf_index);
constructor(uint8 tree_levels, uint256 zero_value) public {
levels = tree_levels;
2019-07-10 12:58:21 -04:00
_zeros.push(zero_value);
_filled_subtrees.push(_zeros[0]);
2019-07-09 09:05:30 -04:00
for (uint8 i = 1; i < levels; i++) {
2019-07-10 12:58:21 -04:00
_zeros.push(hashLeftRight(_zeros[i-1], _zeros[i-1]));
_filled_subtrees.push(_zeros[i]);
2019-07-09 09:05:30 -04:00
}
2019-07-10 12:58:21 -04:00
_roots = new uint256[](ROOT_HISTORY_SIZE);
_roots[0] = hashLeftRight(_zeros[levels - 1], _zeros[levels - 1]);
2019-07-09 09:05:30 -04:00
}
2019-07-10 12:58:21 -04:00
function hashLeftRight(uint256 left, uint256 right) public pure returns (uint256 mimc_hash) {
2019-07-09 09:05:30 -04:00
uint256 k = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 R = 0;
uint256 C = 0;
R = addmod(R, left, k);
(R, C) = MiMC.MiMCSponge(R, C, 0);
R = addmod(R, right, k);
(R, C) = MiMC.MiMCSponge(R, C, 0);
mimc_hash = R;
}
2019-07-10 12:58:21 -04:00
function _insert(uint256 leaf) internal {
2019-07-09 09:05:30 -04:00
uint32 leaf_index = next_index;
uint32 current_index = next_index;
next_index += 1;
uint256 current_level_hash = leaf;
uint256 left;
uint256 right;
for (uint8 i = 0; i < levels; i++) {
if (current_index % 2 == 0) {
left = current_level_hash;
2019-07-10 12:58:21 -04:00
right = _zeros[i];
2019-07-09 09:05:30 -04:00
2019-07-10 12:58:21 -04:00
_filled_subtrees[i] = current_level_hash;
2019-07-09 09:05:30 -04:00
} else {
2019-07-10 12:58:21 -04:00
left = _filled_subtrees[i];
2019-07-09 09:05:30 -04:00
right = current_level_hash;
}
2019-07-10 12:58:21 -04:00
current_level_hash = hashLeftRight(left, right);
2019-07-09 09:05:30 -04:00
current_index /= 2;
}
current_root = (current_root + 1) % ROOT_HISTORY_SIZE;
2019-07-10 12:58:21 -04:00
_roots[current_root] = current_level_hash;
2019-07-09 09:05:30 -04:00
emit LeafAdded(leaf, leaf_index);
}
2019-07-10 12:58:21 -04:00
function isKnownRoot(uint root) public view returns(bool) {
if (root == 0) {
2019-07-09 09:05:30 -04:00
return false;
}
// search most recent first
uint256 i;
2019-07-15 11:04:48 -04:00
for(i = current_root; i < 2**256 - 1; i--) {
2019-07-10 12:58:21 -04:00
if (root == _roots[i]) {
2019-07-09 09:05:30 -04:00
return true;
}
}
2019-07-15 11:33:46 -04:00
// process the rest of roots
2019-07-09 09:05:30 -04:00
for(i = ROOT_HISTORY_SIZE - 1; i > current_root; i--) {
2019-07-10 12:58:21 -04:00
if (root == _roots[i]) {
2019-07-09 09:05:30 -04:00
return true;
}
}
return false;
2019-07-15 11:33:46 -04:00
// or we can do that in other way
// uint256 i = _current_root;
// do {
// if (root == _roots[i]) {
// return true;
// }
// if (i == 0) {
// i = ROOT_HISTORY_SIZE;
// }
// i--;
// } while (i != _current_root);
2019-07-09 09:05:30 -04:00
}
function getLastRoot() public view returns(uint256) {
2019-07-10 12:58:21 -04:00
return _roots[current_root];
}
function roots() public view returns(uint256[] memory) {
return _roots;
}
function filled_subtrees() public view returns(uint256[] memory) {
return _filled_subtrees;
}
function zeros() public view returns(uint256[] memory) {
return _zeros;
2019-07-09 09:05:30 -04:00
}
}