arnaucube
arnaucube/grapevine-sonobePublic Self-hosted repository
grapevine-sonobe/circom/node_modules/circomlib/circuits/smt/smtprocessorlevel.circom
circom/node_modules/circomlib/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*****/
41pragma circom 2.0.0;
42
43
44template SMTProcessorLevel() {
45    signal input st_top;
46    signal input st_old0;
47    signal input st_bot;
48    signal input st_new1;
49    signal input st_na;
50    signal input st_upd;
51
52    signal output oldRoot;
53    signal output newRoot;
54    signal input sibling;
55    signal input old1leaf;
56    signal input new1leaf;
57    signal input newlrbit;
58    signal input oldChild;
59    signal input newChild;
60
61    signal aux[4];
62
63    component oldProofHash = SMTHash2();
64    component newProofHash = SMTHash2();
65
66    component oldSwitcher = Switcher();
67    component newSwitcher = Switcher();
68
69    // Old side
70
71    oldSwitcher.L <== oldChild;
72    oldSwitcher.R <== sibling;
73
74    oldSwitcher.sel <== newlrbit;
75    oldProofHash.L <== oldSwitcher.outL;
76    oldProofHash.R <== oldSwitcher.outR;
77
78    aux[0] <== old1leaf * (st_bot + st_new1 + st_upd);
79    oldRoot <== aux[0] +  oldProofHash.out * st_top;
80
81    // New side
82
83    aux[1] <== newChild * ( st_top + st_bot);
84    newSwitcher.L <== aux[1] + new1leaf*st_new1;
85
86    aux[2] <== sibling*st_top;
87    newSwitcher.R <== aux[2] + old1leaf*st_new1;
88
89    newSwitcher.sel <== newlrbit;
90    newProofHash.L <== newSwitcher.outL;
91    newProofHash.R <== newSwitcher.outR;
92
93    aux[3] <== newProofHash.out * (st_top + st_bot + st_new1);
94    newRoot <==  aux[3] + new1leaf * (st_old0 + st_upd);
95}