tornado-core/circuits/merkleTree.circom

59 lines
1.5 KiB
Plaintext

include "../node_modules/circomlib/circuits/mimcsponge.circom";
// Computes MiMC(left + right)
template HashLeftRight() {
signal input left;
signal input right;
signal output hash;
component hasher = MiMCSponge(2, 220, 1);
hasher.ins[0] <== left;
hasher.ins[1] <== right;
hasher.k <== 0;
hash <== hasher.outs[0];
}
// if s == 0 returns [in[0], in[1]]
// if s == 1 returns [in[1], in[0]]
template Mux() {
signal input in[2];
signal input s;
signal output out[2];
out[0] <== (in[1] - in[0])*s + in[0];
out[1] <== (in[0] - in[1])*s + in[1];
}
// Verifies that merkle proof is correct for given merkle root and a leaf
// pathIndex input is an array of 0/1 selectors telling whether given pathElement is on the left or right side of merkle path
template MerkleTree(levels) {
signal input leaf;
signal input root;
signal private input pathElements[levels];
signal private input pathIndex[levels];
component selectors[levels];
component hashers[levels];
for (var i = 0; i < levels; i++) {
selectors[i] = Mux();
hashers[i] = HashLeftRight();
selectors[i].in[1] <== pathElements[i];
selectors[i].s <== pathIndex[i];
hashers[i].left <== selectors[i].out[0];
hashers[i].right <== selectors[i].out[1];
}
selectors[0].in[0] <== leaf;
for (var i = 1; i < levels; i++) {
selectors[i].in[0] <== hashers[i-1].hash;
}
root === hashers[levels - 1].hash;
}