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.circom";
39
40template SMTVerifier(nLevels) {
41 signal input root;
42 signal input siblings[nLevels];
43 signal input oldKey;
44 signal input oldValue;
45 signal input isOld0;
46 signal input key;
47 signal input value;
48 signal input fnc;
49
50 component hash1Old = SMTHash1();
51 hash1Old.key <== oldKey;
52 hash1Old.value <== oldValue;
53
54 component hash1New = SMTHash1();
55 hash1New.key <== key;
56 hash1New.value <== value;
57
58 component n2bOld = Num2Bits_strict();
59 component n2bNew = Num2Bits_strict();
60
61 n2bOld.in <== oldKey;
62 n2bNew.in <== key;
63
64 component smtLevIns = SMTLevIns(nLevels);
65 for (var i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
66 smtLevIns.enabled <== 1;
67
68 component sm[nLevels];
69 for (var i=0; i<nLevels; i++) {
70 sm[i] = SMTVerifierSM();
71 if (i==0) {
72 sm[i].prev_top <== 1;
73 sm[i].prev_i0 <== 0;
74 sm[i].prev_inew <== 0;
75 sm[i].prev_iold <== 0;
76 sm[i].prev_na <== 0;
77 } else {
78 sm[i].prev_top <== sm[i-1].st_top;
79 sm[i].prev_i0 <== sm[i-1].st_i0;
80 sm[i].prev_inew <== sm[i-1].st_inew;
81 sm[i].prev_iold <== sm[i-1].st_iold;
82 sm[i].prev_na <== sm[i-1].st_na;
83 }
84 sm[i].is0 <== isOld0;
85 sm[i].fnc <== fnc;
86 sm[i].levIns <== smtLevIns.levIns[i];
87 }
88 sm[nLevels-1].st_na === 1;
89
90 component levels[nLevels];
91 for (var i=nLevels-1; i != -1; i--) {
92 levels[i] = SMTVerifierLevel();
93
94 levels[i].st_top <== sm[i].st_top;
95 levels[i].st_i0 <== sm[i].st_i0;
96 levels[i].st_inew <== sm[i].st_inew;
97 levels[i].st_iold <== sm[i].st_iold;
98 levels[i].st_na <== sm[i].st_na;
99
100 levels[i].sibling <== siblings[i];
101 levels[i].old1leaf <== hash1Old.out;
102 levels[i].new1leaf <== hash1New.out;
103
104 levels[i].lrbit <== n2bNew.out[i];
105 if (i==nLevels-1) {
106 levels[i].child <== 0;
107 } else {
108 levels[i].child <== levels[i+1].root;
109 }
110 }
111
112
113 // Check that if checking for non inclussuin and isOld0==0 then key!=old
114 component areKeyEquals = IsEqual();
115 areKeyEquals.in[0] <== oldKey;
116 areKeyEquals.in[1] <== key;
117
118 component keysOk = MultiAND(3);
119 keysOk.in[0] <== fnc;
120 keysOk.in[1] <== 1-isOld0;
121 keysOk.in[2] <== areKeyEquals.out;
122
123 keysOk.out === 0;
124
125 // Check the roots
126 levels[0].root === root;
127
128}