generated from socathie/zkApp
/
circuit.circom
64 lines (44 loc) · 1.42 KB
/
circuit.circom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
pragma circom 2.0.0;
include "../node_modules/circomlib/circuits/poseidon.circom";
include "../node_modules/circomlib/circuits/mux1.circom";
template HashLeftRight() {
signal input left;
signal input right;
signal output hash;
component hasher = Poseidon(2);
left ==> hasher.inputs[0];
right ==> hasher.inputs[1];
hash <== hasher.out;
}
template OtpMerkleTreeInclusionProof(n) {
signal input time;
signal input otp;
signal input path_elements[n];
signal input path_index[n];
signal output root;
component leafHasher = HashLeftRight();
leafHasher.left <== time;
leafHasher.right <== otp;
signal leaf;
leaf <== leafHasher.hash;
component hashers[n];
component mux[n];
signal levelHashes[n + 1];
levelHashes[0] <== leaf;
for (var i = 0; i < n; i++) {
// Should be 0 or 1
path_index[i] * (1 - path_index[i]) === 0;
hashers[i] = HashLeftRight();
mux[i] = MultiMux1(2);
mux[i].c[0][0] <== levelHashes[i];
mux[i].c[0][1] <== path_elements[i];
mux[i].c[1][0] <== path_elements[i];
mux[i].c[1][1] <== levelHashes[i];
mux[i].s <== path_index[i];
hashers[i].left <== mux[i].out[0];
hashers[i].right <== mux[i].out[1];
levelHashes[i + 1] <== hashers[i].hash;
}
root <== levelHashes[n];
}
component main { public [time] } = OtpMerkleTreeInclusionProof(7);