circomlib/circuits/smt/smtprocessor.circom

259 lines
13 KiB
Plaintext
Raw Permalink Normal View History

2018-12-16 13:27:29 +03:00
/*
Copyright 2018 0KIMS association.
This file is part of circom (Zero Knowledge Circuit Compiler).
circom is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
circom is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with circom. If not, see <https://www.gnu.org/licenses/>.
*/
2018-12-06 19:32:49 +03:00
/***************************************************************************************************
2018-12-15 11:00:35 +03:00
SMTProcessor: Sparse Merkle Tree processor is a component to verify an insert/update/delete elements
into the Sparse Merkle tree.
2018-12-06 19:32:49 +03:00
Insert to an empty leaf
=======================
STATE OLD STATE NEW STATE
===== ========= =========
oldRoot newRoot
▲ ▲
│ │
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
top │Sibling├────▶┃ Hash ┃◀─┐ │Sibling├────▶┃ Hash ┃◀─┐
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
│ │
│ │
┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓ ┌───────┐
top ┌─────▶┃ Hash ┃◀──┤Sibling│ ┌─────▶┃ Hash ┃◀──┤Sibling│
│ ┗━━━━━━━┛ └───────┘ │ ┗━━━━━━━┛ └───────┘
│ │
│ │
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
top │Sibling├──▶┃ Hash ┃◀─────┐ │Sibling├──▶┃ Hash ┃◀─────┐
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
│ │
│ │
┌────┴────┐ ┌────┴────┐
old0 │ 0 │ │New1Leaf │
└─────────┘ └─────────┘
┏━━━━━━━┓ ┏━━━━━━━┓
na ┃ Hash ┃ ┃ Hash ┃
┗━━━━━━━┛ ┗━━━━━━━┛
┏━━━━━━━┓ ┏━━━━━━━┓
na ┃ Hash ┃ ┃ Hash ┃
┗━━━━━━━┛ ┗━━━━━━━┛
Insert to a used leaf.
=====================
STATE OLD STATE NEW STATE
===== ========= =========
oldRoot newRoot
▲ ▲
│ │
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
top │Sibling├────▶┃ Hash ┃◀─┐ │Sibling├────▶┃ Hash ┃◀─┐
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
│ │
│ │
┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓ ┌───────┐
top ┌─────▶┃ Hash ┃◀──┤Sibling│ ┌─────▶┃ Hash ┃◀──┤Sibling│
│ ┗━━━━━━━┛ └───────┘ │ ┗━━━━━━━┛ └───────┘
│ │
│ │
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
top │Sibling├──▶┃ Hash ┃◀─────┐ │Sibling├──▶┃ Hash ┃◀─────┐
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
│ │
│ │
┌────┴────┐ ┏━━━┻━━━┓ ┌───────┐
2018-12-13 23:04:37 +03:00
bot │Old1Leaf │ ┌─────▶┃ Hash ┃◀──┼─ 0 │
2018-12-06 19:32:49 +03:00
└─────────┘ │ ┗━━━━━━━┛ └───────┘
┏━━━━━━━┓ ┌───────┐ ┏━━━┻━━━┓
bot ┃ Hash ┃ │ 0 ─┼──▶┃ Hash ┃◀─────┐
┗━━━━━━━┛ └───────┘ ┗━━━━━━━┛ │
┏━━━━━━━┓ ┏━━━┻━━━┓ ┌───────┐
bot ┃ Hash ┃ ┌─────▶┃ Hash ┃◀──│ 0 │
┗━━━━━━━┛ │ ┗━━━━━━━┛ └───────┘
┏━━━━━━━┓ ┌─────────┐ ┏━━━┻━━━┓ ┌─────────┐
new1 ┃ Hash ┃ │Old1Leaf ├──▶┃ Hash ┃◀──│New1Leaf │
┗━━━━━━━┛ └─────────┘ ┗━━━━━━━┛ └─────────┘
┏━━━━━━━┓ ┏━━━━━━━┓
na ┃ Hash ┃ ┃ Hash ┃
┗━━━━━━━┛ ┗━━━━━━━┛
┏━━━━━━━┓ ┏━━━━━━━┓
na ┃ Hash ┃ ┃ Hash ┃
┗━━━━━━━┛ ┗━━━━━━━┛
2018-12-13 21:53:32 +03:00
Fnction
fnc[0] fnc[1]
0 0 NOP
0 1 UPDATE
1 0 INSERT
1 1 DELETE
2018-12-06 19:32:49 +03:00
***************************************************************************************************/
2018-12-11 19:25:21 +03:00
include "../gates.circom";
include "../bitify.circom";
include "../comparators.circom";
include "../switcher.circom";
include "smtlevins.circom";
2018-12-14 16:24:30 +03:00
include "smtprocessorlevel.circom";
include "smtprocessorsm.circom";
2019-06-04 18:32:28 +03:00
include "smthash_poseidon.circom";
2018-12-06 19:32:49 +03:00
2018-12-14 16:24:30 +03:00
template SMTProcessor(nLevels) {
2018-12-06 19:32:49 +03:00
signal input oldRoot;
2018-12-23 01:54:25 +03:00
signal output newRoot;
2018-12-06 19:32:49 +03:00
signal input siblings[nLevels];
2018-12-11 19:25:21 +03:00
signal input oldKey;
signal input oldValue;
signal input isOld0;
signal input newKey;
signal input newValue;
2018-12-13 21:53:32 +03:00
signal input fnc[2];
signal enabled;
enabled <== fnc[0] + fnc[1] - fnc[0]*fnc[1]
2018-12-06 19:32:49 +03:00
component hash1Old = SMTHash1();
2018-12-11 19:25:21 +03:00
hash1Old.key <== oldKey;
hash1Old.value <== oldValue;
2018-12-06 19:32:49 +03:00
component hash1New = SMTHash1();
2018-12-11 19:25:21 +03:00
hash1New.key <== newKey;
hash1New.value <== newValue;
2018-12-06 19:32:49 +03:00
2018-12-11 19:25:21 +03:00
component n2bOld = Num2Bits_strict();
component n2bNew = Num2Bits_strict();
2018-12-06 19:32:49 +03:00
2018-12-11 19:25:21 +03:00
n2bOld.in <== oldKey;
n2bNew.in <== newKey;
component smtLevIns = SMTLevIns(nLevels);
for (var i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
2018-12-13 21:53:32 +03:00
smtLevIns.enabled <== enabled;
2018-12-06 19:32:49 +03:00
component xors[nLevels];
for (var i=0; i<nLevels; i++) {
xors[i] = XOR();
xors[i].a <== n2bOld.out[i];
2018-12-11 19:25:21 +03:00
xors[i].b <== n2bNew.out[i];
2018-12-06 19:32:49 +03:00
}
component sm[nLevels];
2018-12-11 19:25:21 +03:00
for (var i=0; i<nLevels; i++) {
2018-12-14 16:24:30 +03:00
sm[i] = SMTProcessorSM();
2018-12-06 19:32:49 +03:00
if (i==0) {
2018-12-13 21:53:32 +03:00
sm[i].prev_top <== enabled;
2018-12-11 19:25:21 +03:00
sm[i].prev_old0 <== 0;
sm[i].prev_bot <== 0;
sm[i].prev_new1 <== 0;
2018-12-13 21:53:32 +03:00
sm[i].prev_na <== 1-enabled;
sm[i].prev_upd <== 0;
2018-12-06 19:32:49 +03:00
} else {
2018-12-11 19:25:21 +03:00
sm[i].prev_top <== sm[i-1].st_top;
sm[i].prev_old0 <== sm[i-1].st_old0;
sm[i].prev_bot <== sm[i-1].st_bot;
sm[i].prev_new1 <== sm[i-1].st_new1;
sm[i].prev_na <== sm[i-1].st_na;
2018-12-13 21:53:32 +03:00
sm[i].prev_upd <== sm[i-1].st_upd;
2018-12-06 19:32:49 +03:00
}
2018-12-11 19:25:21 +03:00
sm[i].is0 <== isOld0;
2018-12-06 19:32:49 +03:00
sm[i].xor <== xors[i].out;
2018-12-13 21:53:32 +03:00
sm[i].fnc[0] <== fnc[0];
sm[i].fnc[1] <== fnc[1];
2018-12-11 19:25:21 +03:00
sm[i].levIns <== smtLevIns.levIns[i];
2018-12-06 19:32:49 +03:00
}
2018-12-23 01:54:25 +03:00
sm[nLevels-1].st_na + sm[nLevels-1].st_new1 + sm[nLevels-1].st_old0 +sm[nLevels-1].st_upd === 1;
2018-12-06 19:32:49 +03:00
component levels[nLevels];
for (var i=nLevels-1; i != -1; i--) {
2018-12-14 16:24:30 +03:00
levels[i] = SMTProcessorLevel();
2018-12-06 19:32:49 +03:00
levels[i].st_top <== sm[i].st_top;
levels[i].st_old0 <== sm[i].st_old0;
levels[i].st_bot <== sm[i].st_bot;
levels[i].st_new1 <== sm[i].st_new1;
2018-12-13 21:53:32 +03:00
levels[i].st_na <== sm[i].st_na;
levels[i].st_upd <== sm[i].st_upd;
2018-12-06 19:32:49 +03:00
levels[i].sibling <== siblings[i];
levels[i].old1leaf <== hash1Old.out;
levels[i].new1leaf <== hash1New.out;
2018-12-11 19:25:21 +03:00
levels[i].newlrbit <== n2bNew.out[i];
2018-12-06 19:32:49 +03:00
if (i==nLevels-1) {
levels[i].oldChild <== 0;
levels[i].newChild <== 0;
} else {
levels[i].oldChild <== levels[i+1].oldRoot;
levels[i].newChild <== levels[i+1].newRoot;
}
}
2018-12-13 21:53:32 +03:00
component topSwitcher = Switcher();
topSwitcher.sel <== fnc[0]*fnc[1];
topSwitcher.L <== levels[0].oldRoot;
topSwitcher.R <== levels[0].newRoot;
2018-12-23 01:54:25 +03:00
component checkOldInput = ForceEqualIfEnabled();
checkOldInput.enabled <== enabled;
checkOldInput.in[0] <== oldRoot;
checkOldInput.in[1] <== topSwitcher.outL;
newRoot <== enabled * (topSwitcher.outR - oldRoot) + oldRoot;
// topSwitcher.outL === oldRoot*enabled;
// topSwitcher.outR === newRoot*enabled;
2018-12-15 11:00:35 +03:00
// Ckeck keys are equal if updating
component areKeyEquals = IsEqual();
areKeyEquals.in[0] <== oldKey;
areKeyEquals.in[1] <== newKey;
component keysOk = MultiAND(3);
keysOk.in[0] <== 1-fnc[0];
keysOk.in[1] <== fnc[1];
keysOk.in[2] <== 1-areKeyEquals.out;
keysOk.out === 0;
2018-12-06 19:32:49 +03:00
}