circomlib/circuits/smt/smtverifier.circom
circuits/smt/smtverifier.circomRaw
1/*
2 Copyright 2018 0KIMS association.
3
4 This file is part of circom (Zero Knowledge Circuit Compiler).
5
6 circom is a free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 circom is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with circom. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20/*
21
22SMTVerifier is a component to verify inclusion/exclusion of an element in the tree
23
24
25fnc: 0 -> VERIFY INCLUSION
26 1 -> VERIFY NOT INCLUSION
27
28 */
29
30
31include "../gates.circom";
32include "../bitify.circom";
33include "../comparators.circom";
34include "../switcher.circom";
35include "smtlevins.circom";
36include "smtverifierlevel.circom";
37include "smtverifiersm.circom";
38include "smthash_poseidon.circom";
39
40template SMTVerifier(nLevels) {
41 signal input enabled;
42 signal input root;
43 signal input siblings[nLevels];
44 signal input oldKey;
45 signal input oldValue;
46 signal input isOld0;
47 signal input key;
48 signal input value;
49 signal input fnc;
50
51 var i;
52
53 component hash1Old = SMTHash1();
54 hash1Old.key <== oldKey;
55 hash1Old.value <== oldValue;
56
57 component hash1New = SMTHash1();
58 hash1New.key <== key;
59 hash1New.value <== value;
60
61 component n2bOld = Num2Bits_strict();
62 component n2bNew = Num2Bits_strict();
63
64 n2bOld.in <== oldKey;
65 n2bNew.in <== key;
66
67 component smtLevIns = SMTLevIns(nLevels);
68 for (i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
69 smtLevIns.enabled <== enabled;
70
71 component sm[nLevels];
72 for (i=0; i<nLevels; i++) {
73 sm[i] = SMTVerifierSM();
74 if (i==0) {
75 sm[i].prev_top <== enabled;
76 sm[i].prev_i0 <== 0;
77 sm[i].prev_inew <== 0;
78 sm[i].prev_iold <== 0;
79 sm[i].prev_na <== 1-enabled;
80 } else {
81 sm[i].prev_top <== sm[i-1].st_top;
82 sm[i].prev_i0 <== sm[i-1].st_i0;
83 sm[i].prev_inew <== sm[i-1].st_inew;
84 sm[i].prev_iold <== sm[i-1].st_iold;
85 sm[i].prev_na <== sm[i-1].st_na;
86 }
87 sm[i].is0 <== isOld0;
88 sm[i].fnc <== fnc;
89 sm[i].levIns <== smtLevIns.levIns[i];
90 }
91 sm[nLevels-1].st_na + sm[nLevels-1].st_iold + sm[nLevels-1].st_inew + sm[nLevels-1].st_i0 === 1;
92
93 component levels[nLevels];
94 for (i=nLevels-1; i != -1; i--) {
95 levels[i] = SMTVerifierLevel();
96
97 levels[i].st_top <== sm[i].st_top;
98 levels[i].st_i0 <== sm[i].st_i0;
99 levels[i].st_inew <== sm[i].st_inew;
100 levels[i].st_iold <== sm[i].st_iold;
101 levels[i].st_na <== sm[i].st_na;
102
103 levels[i].sibling <== siblings[i];
104 levels[i].old1leaf <== hash1Old.out;
105 levels[i].new1leaf <== hash1New.out;
106
107 levels[i].lrbit <== n2bNew.out[i];
108 if (i==nLevels-1) {
109 levels[i].child <== 0;
110 } else {
111 levels[i].child <== levels[i+1].root;
112 }
113 }
114
115
116 // Check that if checking for non inclussuin and isOld0==0 then key!=old
117 component areKeyEquals = IsEqual();
118 areKeyEquals.in[0] <== oldKey;
119 areKeyEquals.in[1] <== key;
120
121 component keysOk = MultiAND(4);
122 keysOk.in[0] <== fnc;
123 keysOk.in[1] <== 1-isOld0;
124 keysOk.in[2] <== areKeyEquals.out;
125 keysOk.in[3] <== enabled;
126
127 keysOk.out === 0;
128
129 // Check the root
130 component checkRoot = ForceEqualIfEnabled();
131 checkRoot.enabled <== enabled;
132 checkRoot.in[0] <== levels[0].root;
133 checkRoot.in[1] <== root;
134
135 // levels[0].root === root;
136
137}