circomlib/circuits/smt/smtprocessorlevel.circom
circuits/smt/smtprocessorlevel.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
22SMTProcessorLevel
23
24This circuit has 2 hash
25
26Outputs according to the state.
27
28State oldRoot newRoot
29===== ======= =======
30top H'(oldChild, sibling) H'(newChild, sibling)
31old0 0 new1leaf
32bot old1leaf H'(newChild, 0)
33new1 old1leaf H'(new1leaf, old1leaf)
34na 0 0
35
36upd old1leaf new1leaf
37
38H' is the Hash function with the inputs shifted acordingly.
39
40*****/
41
42
43template SMTProcessorLevel() {
44 signal input st_top;
45 signal input st_old0;
46 signal input st_bot;
47 signal input st_new1;
48 signal input st_na;
49 signal input st_upd;
50
51 signal output oldRoot;
52 signal output newRoot;
53 signal input sibling;
54 signal input old1leaf;
55 signal input new1leaf;
56 signal input newlrbit;
57 signal input oldChild;
58 signal input newChild;
59
60 signal aux[4];
61
62 component oldProofHash = SMTHash2();
63 component newProofHash = SMTHash2();
64
65 component oldSwitcher = Switcher();
66 component newSwitcher = Switcher();
67
68 // Old side
69
70 oldSwitcher.L <== oldChild;
71 oldSwitcher.R <== sibling;
72
73 oldSwitcher.sel <== newlrbit;
74 oldProofHash.L <== oldSwitcher.outL;
75 oldProofHash.R <== oldSwitcher.outR;
76
77 aux[0] <== old1leaf * (st_bot + st_new1 + st_upd);
78 oldRoot <== aux[0] + oldProofHash.out * st_top;
79
80 // New side
81
82 aux[1] <== newChild * ( st_top + st_bot);
83 newSwitcher.L <== aux[1] + new1leaf*st_new1;
84
85 aux[2] <== sibling*st_top;
86 newSwitcher.R <== aux[2] + old1leaf*st_new1;
87
88 newSwitcher.sel <== newlrbit;
89 newProofHash.L <== newSwitcher.outL;
90 newProofHash.R <== newSwitcher.outR;
91
92 aux[3] <== newProofHash.out * (st_top + st_bot + st_new1);
93 newRoot <== aux[3] + new1leaf * (st_old0 + st_upd);
94}