This commit is contained in:
poma 2021-03-20 23:48:26 +03:00
parent a8b93f6d5b
commit 87ce87532e
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
3 changed files with 26 additions and 6 deletions

@ -3,6 +3,7 @@ include "../node_modules/circomlib/circuits/bitify.circom";
include "./MerkleTreeUpdater.circom"; include "./MerkleTreeUpdater.circom";
include "./TreeUpdateArgsHasher.circom"; include "./TreeUpdateArgsHasher.circom";
// Computes hashes of the next tree layer
template TreeLayer(height) { template TreeLayer(height) {
var nItems = 1 << height; var nItems = 1 << height;
signal input ins[nItems * 2]; signal input ins[nItems * 2];
@ -19,6 +20,7 @@ template TreeLayer(height) {
// Inserts a leaf batch into a tree // Inserts a leaf batch into a tree
// Checks that tree previously contained zero leaves in the same position // Checks that tree previously contained zero leaves in the same position
// Hashes leaves with Poseidon hash
template BatchTreeUpdate(levels, batchLevels, zeroBatchLeaf) { template BatchTreeUpdate(levels, batchLevels, zeroBatchLeaf) {
var height = levels - batchLevels; var height = levels - batchLevels;
var nLeaves = 1 << batchLevels; var nLeaves = 1 << batchLevels;

@ -1,13 +1,15 @@
include "../node_modules/circomlib/circuits/bitify.circom"; include "../node_modules/circomlib/circuits/bitify.circom";
include "../node_modules/circomlib/circuits/sha256/sha256.circom"; include "../node_modules/circomlib/circuits/sha256/sha256.circom";
// Computes a SHA256 hash of all inputs packed into a byte array
// Field elements are padded to 256 bits with zeroes
template TreeUpdateArgsHasher(nLeaves) { template TreeUpdateArgsHasher(nLeaves) {
signal private input oldRoot; signal input oldRoot;
signal private input newRoot; signal input newRoot;
signal private input pathIndices; signal input pathIndices;
signal private input instances[nLeaves]; signal input instances[nLeaves];
signal private input hashes[nLeaves]; signal input hashes[nLeaves];
signal private input blocks[nLeaves]; signal input blocks[nLeaves];
signal output out; signal output out;
var header = 256 + 256 + 32; var header = 256 + 256 + 32;

@ -100,6 +100,7 @@ contract TornadoTrees is Initializable {
withdrawalsLength = withdrawalsV1Length; withdrawalsLength = withdrawalsV1Length;
} }
/// @dev Queue a new deposit data to be inserted into a merkle tree
function registerDeposit(address _instance, bytes32 _commitment) public onlyTornadoProxy { function registerDeposit(address _instance, bytes32 _commitment) public onlyTornadoProxy {
uint256 _depositsLength = depositsLength; uint256 _depositsLength = depositsLength;
deposits[_depositsLength] = keccak256(abi.encode(_instance, _commitment, blockNumber())); deposits[_depositsLength] = keccak256(abi.encode(_instance, _commitment, blockNumber()));
@ -107,6 +108,7 @@ contract TornadoTrees is Initializable {
depositsLength = _depositsLength + 1; depositsLength = _depositsLength + 1;
} }
/// @dev Queue a new withdrawal data to be inserted into a merkle tree
function registerWithdrawal(address _instance, bytes32 _nullifierHash) public onlyTornadoProxy { function registerWithdrawal(address _instance, bytes32 _nullifierHash) public onlyTornadoProxy {
uint256 _withdrawalsLength = withdrawalsLength; uint256 _withdrawalsLength = withdrawalsLength;
withdrawals[_withdrawalsLength] = keccak256(abi.encode(_instance, _nullifierHash, blockNumber())); withdrawals[_withdrawalsLength] = keccak256(abi.encode(_instance, _nullifierHash, blockNumber()));
@ -114,6 +116,13 @@ contract TornadoTrees is Initializable {
withdrawalsLength = _withdrawalsLength + 1; withdrawalsLength = _withdrawalsLength + 1;
} }
/// @dev Insert a full batch of queued deposits into a merkle tree
/// @param _proof A snark proof that elements were inserted correctly
/// @param _argsHash A hash of snark inputs
/// @param _argsHash Current merkle tree root
/// @param _newRoot Updated merkle tree root
/// @param _pathIndices Merkle path to inserted batch
/// @param _events A batch of inserted events (leaves)
function updateDepositTree( function updateDepositTree(
bytes calldata _proof, bytes calldata _proof,
bytes32 _argsHash, bytes32 _argsHash,
@ -158,6 +167,13 @@ contract TornadoTrees is Initializable {
lastProcessedDepositLeaf = offset + CHUNK_SIZE; lastProcessedDepositLeaf = offset + CHUNK_SIZE;
} }
/// @dev Insert a full batch of queued withdrawals into a merkle tree
/// @param _proof A snark proof that elements were inserted correctly
/// @param _argsHash A hash of snark inputs
/// @param _argsHash Current merkle tree root
/// @param _newRoot Updated merkle tree root
/// @param _pathIndices Merkle path to inserted batch
/// @param _events A batch of inserted events (leaves)
function updateWithdrawalTree( function updateWithdrawalTree(
bytes calldata _proof, bytes calldata _proof,
bytes32 _argsHash, bytes32 _argsHash,