diff --git a/circuits/smt/smtinsert.circom b/circuits/smt/smtinsert.circom index 9c5d37e..4206ef9 100644 --- a/circuits/smt/smtinsert.circom +++ b/circuits/smt/smtinsert.circom @@ -66,7 +66,7 @@ Insert to a used leaf. │ │ │ │ ┌────┴────┐ ┏━━━┻━━━┓ ┌───────┐ - old1 │Old1Leaf │ ┌─────▶┃ Hash ┃◀──┼─ 0 │ + bot │Old1Leaf │ ┌─────▶┃ Hash ┃◀──┼─ 0 │ └─────────┘ │ ┗━━━━━━━┛ └───────┘ │ │ @@ -159,7 +159,6 @@ template SMTInsert(nLevels) { sm[i] = SMTInsertSM(); if (i==0) { 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; @@ -167,7 +166,6 @@ template SMTInsert(nLevels) { 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; @@ -187,7 +185,6 @@ template SMTInsert(nLevels) { levels[i] = SMTInsertLevel(); levels[i].st_top <== sm[i].st_top; - levels[i].st_old1 <== sm[i].st_old1; levels[i].st_old0 <== sm[i].st_old0; levels[i].st_bot <== sm[i].st_bot; levels[i].st_new1 <== sm[i].st_new1; diff --git a/circuits/smt/smtinsertlevel.circom b/circuits/smt/smtinsertlevel.circom index 91fba93..85757aa 100644 --- a/circuits/smt/smtinsertlevel.circom +++ b/circuits/smt/smtinsertlevel.circom @@ -11,10 +11,9 @@ Outputs according to the state. State oldRoot newRoot ===== ======= ======= top H'(oldChild, sibling) H'(newChild, sibling) -old1 old1leaf H'(newChild, 0) old0 0 new1leaf -bot 0 H'(newChild, 0) -new1 0 H'(new1leaf, old1leaf) +bot old1leaf H'(newChild, 0) +new1 old1leaf H'(new1leaf, old1leaf) na 0 0 upd old1leaf new1leaf @@ -26,7 +25,6 @@ H' is the Hash function with the inputs shifted acordingly. template SMTInsertLevel() { signal input st_top; - signal input st_old1; signal input st_old0; signal input st_bot; signal input st_new1; @@ -59,12 +57,12 @@ template SMTInsertLevel() { oldProofHash.L <== oldSwitcher.outL; oldProofHash.R <== oldSwitcher.outR; - aux[0] <== old1leaf * (st_old1 + st_upd); + aux[0] <== old1leaf * (st_bot + st_new1 + st_upd); oldRoot <== aux[0] + oldProofHash.out * st_top; // New side - aux[1] <== newChild * ( st_top + st_old1 + st_bot); + aux[1] <== newChild * ( st_top + st_bot); newSwitcher.L <== aux[1] + new1leaf*st_new1; aux[2] <== sibling*st_top; @@ -74,6 +72,6 @@ template SMTInsertLevel() { newProofHash.L <== newSwitcher.outL; newProofHash.R <== newSwitcher.outR; - aux[3] <== newProofHash.out * (st_top + st_old1 + st_bot + st_new1); + aux[3] <== newProofHash.out * (st_top + st_bot + st_new1); newRoot <== aux[3] + new1leaf * (st_old0 + st_upd); } diff --git a/circuits/smt/smtinsertsm.circom b/circuits/smt/smtinsertsm.circom index 2a8aaf4..5a51536 100644 --- a/circuits/smt/smtinsertsm.circom +++ b/circuits/smt/smtinsertsm.circom @@ -31,14 +31,13 @@ fnc[0] fnc[1] 1 1 DELETE - ########### # # ┌────────────────────────────▶# upd #─────────────────────┐ │ ## ## │ │ ######### │ levIns=1 │ │ - fnc[0]=0 │ │ any + fnc[0]=0 │ │ any │ │ │ │ │ │ @@ -51,18 +50,18 @@ fnc[0] fnc[1] │ ########### │ │ ########### │ │ # # ────────────┘ └────────▶# #│ └──# top # # na # - ## ## ────┐ ┌──▶## ## - ######### │ │ ######### - │ │ - │ ########### ########### │ any - levIns=1 │ # # xor=1 # # │ - is0=0 └───▶# old1 #─────────────▶# new1 #──┘ - fnc[0]=1 ## ## ## ## - #########│ ######### - │ ▲ - └───┐ ┌─────┘ - xor=0 │ ###########│ xor=1 - │ # # + ## ## ───────────────────┐ levIns=1 ┌──▶## ## + ######### │ is0=0 │ ######### + │ │ fnc[0]=1 │ + │ │ xor=1 ########### │ any + │ └──────────────────▶# # │ + │ # new1 #──┘ + │ ## ## + └────────────────────────────────┐ ######### + levIns=1 │ ▲ + is0=0 │ ┌─────┘ + fnc[0]=1 │ ###########│ xor=1 + xor=0 │ # # ▼# btn # ## ## #########◀───────┐ @@ -81,7 +80,6 @@ template SMTInsertSM() { signal input prev_top; signal input prev_old0; - signal input prev_old1; signal input prev_bot; signal input prev_new1; signal input prev_na; @@ -89,7 +87,6 @@ template SMTInsertSM() { signal output st_top; signal output st_old0; - signal output st_old1; signal output st_bot; signal output st_new1; signal output st_na; @@ -98,21 +95,51 @@ template SMTInsertSM() { signal aux1; signal aux2; - aux1 <== prev_top * levIns; - aux2 <== aux1*is0; - st_top <== prev_top - aux1 // prev_top * (1-levIns) = - // = prev_top - aux1; - st_old0 <== aux2 * fnc[0]; // (prev_top * levIns * is0)*fnc[0] = aux2 - st_old1 <== (aux1 - aux2)*fnc[0]; // (prev_top * levIns * (1-is0))*fnc[0] = - // = (aux1 * (1-is0))*fnc[0] = - // = (aux1 - aux2) * fnc[0] - st_new1 <== (prev_old1 + prev_bot)*xor // prev_old1*xor + prev_bot*xor = - // = (prev_old1 + prev_bot)*xor; - 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 + aux1 <== prev_top * levIns; + aux2 <== aux1*fnc[0]; // prev_top * levIns * fnc[0] + + // st_top = prev_top*(1-levIns) + // = + prev_top + // - prev_top * levIns = aux1 + + st_top <== prev_top - aux1; + + // st_old0 = prev_top * levIns * is0 * fnc[0] + // = + prev_top * levIns * is0 * fnc[0] = aux2 * is0 + + st_old0 <== aux2 * is0; // prev_top * levIns * is0 * fnc[0] + + // st_new1 = prev_top * levIns * (1-is0)*fnc[0] * xor + prev_bot*xor = + // = + prev_top * levIns * fnc[0] * xor = aux2 * xor + // - prev_top * levIns * is0 * fnc[0] * xor = st_old0 * xor + // + prev_bot * xor = prev_bot * xor + + st_new1 <== (aux2 - st_old0 + prev_bot)*xor; + + + // st_bot = prev_top * levIns * (1-is0)*fnc[0] * (1-xor) + prev_bot*(1-xor); + // = + prev_top * levIns * fnc[0] + // - prev_top * levIns * is0 * fnc[0] + // - prev_top * levIns * fnc[0] * xor + // + prev_top * levIns * is0 * fnc[0] * xor + // + prev_bot + // - prev_bot * xor + + st_bot <== (1-xor) * (aux2 - st_old0 + prev_bot) + + + // st_upd = prev_top * (1-fnc[0]) *levIns; + // = + prev_top * levIns + // - prev_top * levIns * fnc[0] + + st_upd <== aux1 - aux2 + + // st_na = prev_new1 + prev_old0 + prev_na + prev_upd; + // = + prev_new1 + // + prev_old0 + // + prev_na + // + prev_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]) } diff --git a/test/smt.js b/test/smt.js index 50c202a..4459645 100644 --- a/test/smt.js +++ b/test/smt.js @@ -108,8 +108,8 @@ describe("SMT test", function () { const tree6 = await smt.newMemEmptyTrie(); await testInsert(tree1,keys[0],values[0], circuit); - await testInsert(tree1,keys[1],values[1], circuit, console.log); -/* await testInsert(tree1,keys[2],values[2], circuit); + await testInsert(tree1,keys[1],values[1], circuit); + await testInsert(tree1,keys[2],values[2], circuit); await testInsert(tree2,keys[0],values[0], circuit); await testInsert(tree2,keys[2],values[2], circuit); @@ -153,7 +153,7 @@ describe("SMT test", function () { await testDelete(tree3, keys[1], circuit); await testDelete(tree4, keys[1], circuit); await testDelete(tree5, keys[0], circuit); - await testDelete(tree6, keys[0], circuit); */ + await testDelete(tree6, keys[0], circuit); }); it("Should match a NOp with random vals", async () => {