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