grapevine-sonobe/circom/node_modules/circomlib/circuits/smt/smtverifiersm.circom
circom/node_modules/circomlib/circuits/smt/smtverifiersm.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/*
21Each level in the SMTVerifier has a state.
22
23This is the state machine.
24
25The signals are
26
27levIns: 1 if we are in the level where the insertion should happen
28xor: 1 if the bitKey of the old and new keys are different in this level
29is0: Input that indicates that the oldKey is 0
30fnc: 0 -> VERIFY INCLUSION
31 1 -> VERIFY NOT INCLUSION
32
33err state is not a state itself. It's a lack of state.
34
35The end of the last level will have to be `na`
36
37 levIns=0 any
38 ┌────┐ ┌────┐
39 │ │ │ │
40 │ ▼ levIns=1 ▼ │
41 │ ########### is0=1 ########### ########### │
42 │ # # fnc=1 # # any # # │
43 └──# top # ─────────────────────▶# i0 #───────────────▶# na #──┘
44 ## ## ──────────┐ ## ## ┌───────▶## ##
45 ########─────────────┐│ ######### │┌────────▶#########
46 ││ levIns=1 ││
47 ││ is0=0 ########### ││
48 ││ fnc=1 # # any│
49 │└──────────▶ # iold #────────┘│
50 │ ## ## │
51 │ ######### │
52 │ │
53 │ levIns=1 ########### │
54 │ fnc=0 # # any
55 └────────────▶# inew #─────────┘
56 ## ##
57 #########
58
59 */
60 pragma circom 2.0.0;
61
62
63template SMTVerifierSM() {
64 signal input is0;
65 signal input levIns;
66 signal input fnc;
67
68 signal input prev_top;
69 signal input prev_i0;
70 signal input prev_iold;
71 signal input prev_inew;
72 signal input prev_na;
73
74 signal output st_top;
75 signal output st_i0;
76 signal output st_iold;
77 signal output st_inew;
78 signal output st_na;
79
80 signal prev_top_lev_ins;
81 signal prev_top_lev_ins_fnc;
82
83 prev_top_lev_ins <== prev_top * levIns;
84 prev_top_lev_ins_fnc <== prev_top_lev_ins*fnc; // prev_top * levIns * fnc
85
86 // st_top = prev_top * (1-levIns)
87 // = + prev_top
88 // - prev_top * levIns
89 st_top <== prev_top - prev_top_lev_ins;
90
91 // st_inew = prev_top * levIns * (1-fnc)
92 // = + prev_top * levIns
93 // - prev_top * levIns * fnc
94 st_inew <== prev_top_lev_ins - prev_top_lev_ins_fnc;
95
96 // st_iold = prev_top * levIns * (1-is0)*fnc
97 // = + prev_top * levIns * fnc
98 // - prev_top * levIns * fnc * is0
99 st_iold <== prev_top_lev_ins_fnc * (1 - is0);
100
101 // st_i0 = prev_top * levIns * is0
102 // = + prev_top * levIns * is0
103 st_i0 <== prev_top_lev_ins * is0;
104
105 st_na <== prev_na + prev_inew + prev_iold + prev_i0;
106}