tornado-nova/circuits/merkleProof.circom

31 lines
1019 B
Plaintext
Raw Normal View History

include "../node_modules/circomlib/circuits/poseidon.circom";
include "../node_modules/circomlib/circuits/switcher.circom";
// Verifies that merkle proof is correct for given merkle root and a leaf
// pathIndices bits is an array of 0/1 selectors telling whether given pathElement is on the left or right side of merkle path
template MerkleProof(levels) {
signal input leaf;
signal input pathElements[levels];
signal input pathIndices;
signal output root;
component switcher[levels];
component hasher[levels];
component indexBits = Num2Bits(levels);
indexBits.in <== pathIndices;
for (var i = 0; i < levels; i++) {
switcher[i] = Switcher();
switcher[i].L <== i == 0 ? leaf : hasher[i - 1].out;
switcher[i].R <== pathElements[i];
switcher[i].sel <== indexBits.out[i];
hasher[i] = Poseidon(2);
hasher[i].inputs[0] <== switcher[i].outL;
hasher[i].inputs[1] <== switcher[i].outR;
}
root <== hasher[levels - 1].out;
}