arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/circuits/smt/smtverifier.circom
circuits/smt/smtverifier.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
 22SMTVerifier is a component to verify inclusion/exclusion of an element in the tree
 23
 24
 25fnc:  0 -> VERIFY INCLUSION
 26      1 -> VERIFY NOT INCLUSION
 27
 28 */
 29
 30
 31include "../gates.circom";
 32include "../bitify.circom";
 33include "../comparators.circom";
 34include "../switcher.circom";
 35include "smtlevins.circom";
 36include "smtverifierlevel.circom";
 37include "smtverifiersm.circom";
 38include "smthash.circom";
 39
 40template SMTVerifier(nLevels) {
 41    signal input enabled;
 42    signal input root;
 43    signal input siblings[nLevels];
 44    signal input oldKey;
 45    signal input oldValue;
 46    signal input isOld0;
 47    signal input key;
 48    signal input value;
 49    signal input fnc;
 50
 51    component hash1Old = SMTHash1();
 52    hash1Old.key <== oldKey;
 53    hash1Old.value <== oldValue;
 54
 55    component hash1New = SMTHash1();
 56    hash1New.key <== key;
 57    hash1New.value <== value;
 58
 59    component n2bOld = Num2Bits_strict();
 60    component n2bNew = Num2Bits_strict();
 61
 62    n2bOld.in <== oldKey;
 63    n2bNew.in <== key;
 64
 65    component smtLevIns = SMTLevIns(nLevels);
 66    for (var i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
 67    smtLevIns.enabled <== enabled;
 68
 69    component sm[nLevels];
 70    for (var i=0; i<nLevels; i++) {
 71        sm[i] = SMTVerifierSM();
 72        if (i==0) {
 73            sm[i].prev_top <== enabled;
 74            sm[i].prev_i0 <== 0;
 75            sm[i].prev_inew <== 0;
 76            sm[i].prev_iold <== 0;
 77            sm[i].prev_na <== 1-enabled;
 78        } else {
 79            sm[i].prev_top <== sm[i-1].st_top;
 80            sm[i].prev_i0 <== sm[i-1].st_i0;
 81            sm[i].prev_inew <== sm[i-1].st_inew;
 82            sm[i].prev_iold <== sm[i-1].st_iold;
 83            sm[i].prev_na <== sm[i-1].st_na;
 84        }
 85        sm[i].is0 <== isOld0;
 86        sm[i].fnc <== fnc;
 87        sm[i].levIns <== smtLevIns.levIns[i];
 88    }
 89    sm[nLevels-1].st_na + sm[nLevels-1].st_iold + sm[nLevels-1].st_inew + sm[nLevels-1].st_i0 === 1;
 90
 91    component levels[nLevels];
 92    for (var i=nLevels-1; i != -1; i--) {
 93        levels[i] = SMTVerifierLevel();
 94
 95        levels[i].st_top <== sm[i].st_top;
 96        levels[i].st_i0 <== sm[i].st_i0;
 97        levels[i].st_inew <== sm[i].st_inew;
 98        levels[i].st_iold <== sm[i].st_iold;
 99        levels[i].st_na <== sm[i].st_na;
100
101        levels[i].sibling <== siblings[i];
102        levels[i].old1leaf <== hash1Old.out;
103        levels[i].new1leaf <== hash1New.out;
104
105        levels[i].lrbit <== n2bNew.out[i];
106        if (i==nLevels-1) {
107            levels[i].child <== 0;
108        } else {
109            levels[i].child <== levels[i+1].root;
110        }
111    }
112
113
114    // Check that if checking for non inclussuin and isOld0==0 then key!=old
115    component areKeyEquals = IsEqual();
116    areKeyEquals.in[0] <== oldKey;
117    areKeyEquals.in[1] <== key;
118
119    component keysOk = MultiAND(4);
120    keysOk.in[0] <== fnc;
121    keysOk.in[1] <== 1-isOld0;
122    keysOk.in[2] <== areKeyEquals.out;
123    keysOk.in[3] <== enabled;
124
125    keysOk.out === 0;
126
127    // Check the root
128    component checkRoot = ForceEqualIfEnabled();
129    checkRoot.enabled <== enabled;
130    checkRoot.in[0] <== levels[0].root;
131    checkRoot.in[1] <== root;
132
133    // levels[0].root === root;
134
135}