arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/circuits/smt/smtinsertlevel.circom
circuits/smt/smtinsertlevel.circomRaw
 1
 2
 3/******
 4
 5SMTInsertLevel
 6
 7This circuit has 2 has
 8
 9Outputs according to the state.
10
11State        oldRoot                    newRoot
12=====        =======                    =======
13top          H'(oldChild, sibling)       H'(newChild, sibling)
14old0         0                           new1leaf
15bot          old1leaf                    H'(newChild, 0)
16new1         old1leaf                    H'(new1leaf, old1leaf)
17na           0                           0
18
19upd          old1leaf                    new1leaf
20
21H' is the Hash function with the inputs shifted acordingly.
22
23*****/
24
25
26template SMTInsertLevel() {
27    signal input st_top;
28    signal input st_old0;
29    signal input st_bot;
30    signal input st_new1;
31    signal input st_na;
32    signal input st_upd;
33
34    signal output oldRoot;
35    signal output newRoot;
36    signal input sibling;
37    signal input old1leaf;
38    signal input new1leaf;
39    signal input newlrbit;
40    signal input oldChild;
41    signal input newChild;
42
43    signal aux[4];
44
45    component oldProofHash = SMTHash2();
46    component newProofHash = SMTHash2();
47
48    component oldSwitcher = Switcher();
49    component newSwitcher = Switcher();
50
51    // Old side
52
53    oldSwitcher.L <== oldChild;
54    oldSwitcher.R <== sibling;
55
56    oldSwitcher.sel <== newlrbit;
57    oldProofHash.L <== oldSwitcher.outL;
58    oldProofHash.R <== oldSwitcher.outR;
59
60    aux[0] <== old1leaf * (st_bot + st_new1 + st_upd);
61    oldRoot <== aux[0] +  oldProofHash.out * st_top;
62
63    // New side
64
65    aux[1] <== newChild * ( st_top + st_bot);
66    newSwitcher.L <== aux[1] + new1leaf*st_new1;
67
68    aux[2] <== sibling*st_top;
69    newSwitcher.R <== aux[2] + old1leaf*st_new1;
70
71    newSwitcher.sel <== newlrbit;
72    newProofHash.L <== newSwitcher.outL;
73    newProofHash.R <== newSwitcher.outR;
74
75    aux[3] <== newProofHash.out * (st_top + st_bot + st_new1);
76    newRoot <==  aux[3] + new1leaf * (st_old0 + st_upd);
77}