Skip to content

Commit

Permalink
working with some errors yet
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaylina committed Dec 13, 2018
1 parent 38fc4b7 commit ccaa7ff
Show file tree
Hide file tree
Showing 11 changed files with 24,595 additions and 146 deletions.
36 changes: 31 additions & 5 deletions circuits/smt/smtinsert.circom
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ Insert to a used leaf.
na ┃ Hash ┃ ┃ Hash ┃
┗━━━━━━━┛ ┗━━━━━━━┛
Fnction
fnc[0] fnc[1]
0 0 NOP
0 1 UPDATE
1 0 INSERT
1 1 DELETE
***************************************************************************************************/

include "../gates.circom";
Expand All @@ -114,6 +123,11 @@ template SMTInsert(nLevels) {
signal input isOld0;
signal input newKey;
signal input newValue;
signal input fnc[2];

signal enabled;

enabled <== fnc[0] + fnc[1] - fnc[0]*fnc[1]

component hash1Old = SMTHash1();
hash1Old.key <== oldKey;
Expand All @@ -131,6 +145,7 @@ template SMTInsert(nLevels) {

component smtLevIns = SMTLevIns(nLevels);
for (var i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
smtLevIns.enabled <== enabled;

component xors[nLevels];
for (var i=0; i<nLevels; i++) {
Expand All @@ -143,22 +158,26 @@ template SMTInsert(nLevels) {
for (var i=0; i<nLevels; i++) {
sm[i] = SMTInsertSM();
if (i==0) {
sm[i].prev_top <== 1;
sm[i].prev_top <== enabled;
sm[i].prev_old1 <== 0;
sm[i].prev_old0 <== 0;
sm[i].prev_bot <== 0;
sm[i].prev_new1 <== 0;
sm[i].prev_na <== 0;
sm[i].prev_na <== 1-enabled;
sm[i].prev_upd <== 0;
} else {
sm[i].prev_top <== sm[i-1].st_top;
sm[i].prev_old1 <== sm[i-1].st_old1;
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;
sm[i].prev_upd <== sm[i-1].st_upd;
}
sm[i].is0 <== isOld0;
sm[i].xor <== xors[i].out;
sm[i].fnc[0] <== fnc[0];
sm[i].fnc[1] <== fnc[1];
sm[i].levIns <== smtLevIns.levIns[i];
}
sm[nLevels-1].st_na === 1;
Expand All @@ -172,7 +191,8 @@ template SMTInsert(nLevels) {
levels[i].st_old0 <== sm[i].st_old0;
levels[i].st_bot <== sm[i].st_bot;
levels[i].st_new1 <== sm[i].st_new1;
levels[i].st_na <==sm[i].st_na;
levels[i].st_na <== sm[i].st_na;
levels[i].st_upd <== sm[i].st_upd;

levels[i].sibling <== siblings[i];
levels[i].old1leaf <== hash1Old.out;
Expand All @@ -188,6 +208,12 @@ template SMTInsert(nLevels) {
}
}

levels[0].oldRoot === oldRoot;
levels[0].newRoot === newRoot;
component topSwitcher = Switcher();

topSwitcher.sel <== fnc[0]*fnc[1];
topSwitcher.L <== levels[0].oldRoot;
topSwitcher.R <== levels[0].newRoot;

topSwitcher.outL === oldRoot*enabled;
topSwitcher.outR === newRoot*enabled;
}
7 changes: 5 additions & 2 deletions circuits/smt/smtinsertlevel.circom
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ bot 0 H'(newChild, 0)
new1 0 H'(new1leaf, old1leaf)
na 0 0
upd old1leaf new1leaf
H' is the Hash function with the inputs shifted acordingly.
*****/
Expand All @@ -29,6 +31,7 @@ template SMTInsertLevel() {
signal input st_bot;
signal input st_new1;
signal input st_na;
signal input st_upd;

signal output oldRoot;
signal output newRoot;
Expand Down Expand Up @@ -56,7 +59,7 @@ template SMTInsertLevel() {
oldProofHash.L <== oldSwitcher.outL;
oldProofHash.R <== oldSwitcher.outR;

aux[0] <== old1leaf * st_old1;
aux[0] <== old1leaf * (st_old1 + st_upd);
oldRoot <== aux[0] + oldProofHash.out * st_top;

// New side
Expand All @@ -72,5 +75,5 @@ template SMTInsertLevel() {
newProofHash.R <== newSwitcher.outR;

aux[3] <== newProofHash.out * (st_top + st_old1 + st_bot + st_new1);
newRoot <== aux[3] + new1leaf * st_old0;
newRoot <== aux[3] + new1leaf * (st_old0 + st_upd);
}
69 changes: 39 additions & 30 deletions circuits/smt/smtinsertsm.circom
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,51 @@ fnc[0] fnc[1]
###########
levIns=1 # #
levIns=0 is0=1 ┌────────────▶# old0 #────────┐ any
┌─────┐ │ ## ## │ ┌──────┐
│ │ │ ######### │ any │ │
│ ▼ │ │ ▼ │
│ ########### │ │ ########### │
│ # # ────────────┘ └────────▶# #│
└──# top # # na #
## ## ────┐ ┌──▶## ##
######### │ │ #########
│ │
│ ########### ########### │ any
levIns=1 │ # # xor=1 # # │
is0=0 └───▶# old1 #─────────────▶# new1 #──┘
## ## ## ##
#########│ #########
│ ▲
└───┐ ┌─────┘
xor=0 │ ###########│ xor=1
│ # #
▼# btn #
## ##
#########◀───────┐
│ │
│ │
└────────────┘
xor=0
# #
┌────────────────────────────▶# upd #─────────────────────┐
│ ## ## │
│ ######### │
levIns=1 │ │
fnc[0]=0 │ │ any
│ │
│ │
│ │
│ ########### │
│ levIns=1 # # │
levIns=0 │ is0=1 ┌────────────▶# old0 #────────┐ │ any
┌─────┐ │ fnc[0]=1│ ## ## │ │ ┌──────┐
│ │ │ │ ######### │ any │ │ │
│ ▼ │ │ │ ▼ ▼ │
│ ########### │ │ ########### │
│ # # ────────────┘ └────────▶# #│
└──# top # # na #
## ## ────┐ ┌──▶## ##
######### │ │ #########
│ │
│ ########### ########### │ any
levIns=1 │ # # xor=1 # # │
is0=0 └───▶# old1 #─────────────▶# new1 #──┘
fnc[0]=1 ## ## ## ##
#########│ #########
│ ▲
└───┐ ┌─────┘
xor=0 │ ###########│ xor=1
│ # #
▼# btn #
## ##
#########◀───────┐
│ │
│ │
└────────────┘
xor=0
***************************************************************************************************/

template SMTInsertSM() {
signal input xor;
signal input is0;
signal input levIns;
signal input fnc[0];
signal input fnc[1];
signal input fnc[2];

signal input prev_top;
signal input prev_old0;
Expand Down Expand Up @@ -102,7 +111,7 @@ template SMTInsertSM() {
st_bot <== -st_new1 + prev_old1 + prev_bot // prev_old1*(1-xor) + prev_bot*(1-xor) =
// = - prev_old1*xor -prev_bot*xor + prev_old1 + prev_bot =
// = -st_new1 + prev_old1 + prev_bot
st_na <== prev_new1 + prev_old0 + prev_na + st_upd;
st_na <== prev_new1 + prev_old0 + prev_na + prev_upd;
st_upd <== aux1*(1-fnc[0]); // prev_top*levIns*(1-fnc[0]) =
// = aux1 * (1-fnc[0])

Expand Down
3 changes: 2 additions & 1 deletion circuits/smt/smtlevins.circom
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ a parent with a sibling != 0.
*/

template SMTLevIns(nLevels) {
signal input enabled;
signal input siblings[nLevels];
signal output levIns[nLevels];
signal done[nLevels-1]; // Indicates if the insLevel has aready been detecetd.
Expand All @@ -67,7 +68,7 @@ template SMTLevIns(nLevels) {
}

// The last level must always have a sibling of 0. If not, then it cannot be inserted.
isZero[nLevels-2].out === 1;
(isZero[nLevels-2].out - 1) * enabled === 0;

levIns[nLevels-1] <== (1-isZero[nLevels-2].out);
done[nLevels-2] <== levIns[nLevels-1];
Expand Down
Loading

0 comments on commit ccaa7ff

Please sign in to comment.