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)
14old1         old1leaf                    H'(newChild, 0)
15old0         0                           new1leaf
16bot          0                           H'(newChild, 0)
17new1         0                           H'(new1leaf, old1leaf)
18na           0                           0
19
20H' is the Hash function with the inputs shifted acordingly.
21
22*****/
23
24
25template SMTInsertLevel() {
26    signal input st_top;
27    signal input st_old1;
28    signal input st_old0;
29    signal input st_bot;
30    signal input st_new1;
31    signal input st_na;
32
33    signal output oldRoot;
34    signal output newRoot;
35    signal input sibling;
36    signal input old1leaf;
37    signal input new1leaf;
38    signal input newlrbit;
39    signal input oldChild;
40    signal input newChild;
41
42    signal aux[4];
43
44    component oldProofHash = SMTHash2();
45    component newProofHash = SMTHash2();
46
47    component oldSwitcher = Switcher();
48    component newSwitcher = Switcher();
49
50    // Old side
51
52    oldSwitcher.inL <== oldChild;
53    oldSwitcher.inR <== sibling;
54
55    oldSwitcher.sel <== newlrbit;
56    oldProofHash.L <== oldSwitcher.outL;
57    oldProofHash.R <== oldSwitcher.outR;
58
59    aux[0] <== old1 * st_old1;
60    oldRoot <== aux[0] +  oldProofHash.out * st_top;
61
62    // New side
63
64    aux[1] <== newChild * ( st_top + st_old1 + st_but);
65    oldSwitcher.inL <== aux[1] + new1leaf*st_new1;
66
67    aux[2] <== sibling*st_top;
68    oldSwitcher.inR <== aux[2] + old1leaf*st_new1;
69
70    newProofHash.sel <== newlrbit;
71    newProofHash.L <== newProofHash.outL;
72    newProofHash.R <== newProofHash.outR;
73
74    aux[3] <== newProofHash.out * (st_top + st_old1 + st_bot + st_new1);
75    newRoot <==  aux[3] + new1leaf * st_old0;
76}