2020-04-08 11:41:12 +02:00
|
|
|
include "../node_modules/circomlib/circuits/mimcsponge.circom";
|
|
|
|
|
|
|
|
// Computes MiMC([left, right])
|
|
|
|
template HashLeftRight() {
|
|
|
|
signal input left;
|
|
|
|
signal input right;
|
|
|
|
signal output hash;
|
|
|
|
|
2020-04-09 11:04:06 +02:00
|
|
|
component hasher = MiMCSponge(2, 1);
|
2020-04-08 11:41:12 +02:00
|
|
|
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 DualMux() {
|
|
|
|
signal input in[2];
|
|
|
|
signal input s;
|
|
|
|
signal output out[2];
|
|
|
|
|
|
|
|
s * (1 - s) === 0
|
|
|
|
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
|
|
|
|
// pathIndices 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 pathElements[levels];
|
|
|
|
signal input pathIndices;
|
|
|
|
|
2020-04-09 20:38:10 +02:00
|
|
|
signal output root;
|
|
|
|
|
2020-04-08 11:41:12 +02:00
|
|
|
component selectors[levels];
|
|
|
|
component hashers[levels];
|
|
|
|
|
|
|
|
component indexBits = Num2Bits(levels);
|
|
|
|
indexBits.in <== pathIndices;
|
|
|
|
|
|
|
|
for (var i = 0; i < levels; i++) {
|
|
|
|
selectors[i] = DualMux();
|
|
|
|
selectors[i].in[0] <== i == 0 ? leaf : hashers[i - 1].hash;
|
|
|
|
selectors[i].in[1] <== pathElements[i];
|
|
|
|
selectors[i].s <== indexBits.out[i];
|
|
|
|
|
|
|
|
hashers[i] = HashLeftRight();
|
|
|
|
hashers[i].left <== selectors[i].out[0];
|
|
|
|
hashers[i].right <== selectors[i].out[1];
|
|
|
|
}
|
|
|
|
|
2020-04-09 20:38:10 +02:00
|
|
|
root <== hashers[levels - 1].hash;
|
2020-04-08 11:41:12 +02:00
|
|
|
}
|