diff --git a/circuits/deposit.circom b/circuits/deposit.circom index ce414e5..39ce451 100644 --- a/circuits/deposit.circom +++ b/circuits/deposit.circom @@ -1,41 +1,38 @@ /* # deposit.circom - +----------+ | | PRI_secret+--------->+ Poseidon +<----+PUB_key | | | + - | +----------+ | - | nullifier | - | + | - | | | +----------+ - | v | | | - | +----+-----+ +---->+ SMT | - +--------->+ | | Poseidon +<------+PUB_rootOld - | +-----+------->+ Verifier | -PUB_coinCode+------->+ Poseidon | | | Non | - | | | | Existance+<------+PRI_siblings -PUB_amount+--------->+ | | | | + - +----------+ | +----------+ | - | | - | | - | +----------+ | - | | | | - | | | | - +----+ | | SMT +<---------+ -PUB_commitment+----> == +<------------+------->+ Poseidon | - +----+ | Verifier | - | +<------+PUB_rootNew - | | - +----------+ + | +----------+ | +----------+ + | nullifier | | +<------+PUB_rootOld + | + | | | + | | | | +<------+PUB_rootNew + | v | | SMT | + | +----+-----+ +---->+ Poseidon +<------+PRI_oldKey + +--------->+ | | Verifier | + | +-----+------->+ (insert) +<------+PRI_oldValue +PUB_coinCode+------->+ Poseidon | | | | + | | | | +<------+PRI_isOld0 +PUB_amount+--------->+ | | | | + +----------+ | | +<------+PRI_siblings + | +----------+ + | + | + | + | + +----+ | +PUB_commitment+----> == +<------------+ + +----+ + */ include "../node_modules/circomlib/circuits/comparators.circom"; include "../node_modules/circomlib/circuits/poseidon.circom"; -include "../node_modules/circomlib/circuits/smt/smtverifier.circom"; +include "../node_modules/circomlib/circuits/smt/smtprocessor.circom"; template Deposit(nLevels) { signal input coinCode; @@ -43,8 +40,8 @@ template Deposit(nLevels) { signal private input secret; signal private input oldKey; signal private input oldValue; - signal private input siblingsOld[nLevels]; - signal private input siblingsNew[nLevels]; + signal private input isOld0; + signal private input siblings[nLevels]; signal input rootOld; signal input rootNew; signal input commitment; @@ -65,54 +62,18 @@ template Deposit(nLevels) { comCheck.in[1] <== commitment; comCheck.out === 1; - // TODO instead of 2 siblings input, get siblingsOld from - // siblingsNew[len-1] both siblingsOld & siblingsNew have same values - // except for one, can be merged into one, to ensure that the circuit - // checks that the leaf non existing under rootOld is in the same - // position than the check that the leaf exists under the rootNew - - // check that nLevels-1 siblings match from siblingsOld & siblingsNew - component siblEq[nLevels]; - signal count[nLevels]; - for (var i=0; i await tree.insert(i+1, commitments[i]); } - // old root - const rootOld = tree.root; - const resOld = await tree.find(key); - console.log("FIND old", resOld); - let oldKey = "0"; - let oldValue = "0"; - if (!resOld.found) { - oldKey = resOld.notFoundKey.toString(); - oldValue = resOld.notFoundValue.toString(); - } - console.log("oldKey", oldKey); - console.log("oldValue", oldValue); - // if (resOld.found) { - // console.error("leaf expect to not exist but exists"); - // } - let siblingsOld = resOld.siblings; - while (siblingsOld.length < nLevels) { - siblingsOld.push("0"); - }; - - await tree.insert(key, commitment); + let res = await tree.insert(key, commitment); // new root - const rootNew = tree.root; - const resNew = await tree.find(key); - console.log("FIND new", resNew); - if (!resNew.found) { - console.error("leaf with the new commitment expect to exist but not exists"); - } - let siblingsNew = resNew.siblings; - while (siblingsNew.length < nLevels) { - siblingsNew.push("0"); - }; + let siblings = res.siblings; + console.log(res); + // while (siblings.length < nLevels) { + // siblings.push("0"); + // }; // calculate witness const input = unstringifyBigInts({ "coinCode": coinCode, "amount": amount, "secret": secret, - "oldKey": oldKey, - "oldValue": oldValue, - "siblingsOld": siblingsOld, - "siblingsNew": siblingsNew, - "rootOld": rootOld, - "rootNew": rootNew, + "oldKey": res.isOld0 ? 0 : res.oldKey, + "oldValue": res.isOld0 ? 0 : res.oldValue, + "isOld0": res.isOld0 ? 1 : 0, + "siblings": siblings, + "rootOld": res.oldRoot, + "rootNew": res.newRoot, "commitment": commitment, "key": key }); @@ -113,7 +88,7 @@ exports.calcDepositWitness = async (wasm, nLevels, key, secret, commitments) => witness: wBuff, publicInputs: { commitment:commitment, - root:rootNew + root:res.newRoot } }; } diff --git a/test/circuits/deposit.test.ts b/test/circuits/deposit.test.ts index 173506d..032021a 100644 --- a/test/circuits/deposit.test.ts +++ b/test/circuits/deposit.test.ts @@ -22,7 +22,6 @@ describe("deposit test", function () { const coinCode = "0"; const amount = '1000000000000000000'; - // const nullifier = "567891234"; const poseidon = circomlib.poseidon.createHash(6, 8, 57); const nullifier = poseidon([2, secret]); @@ -31,42 +30,31 @@ describe("deposit test", function () { // add commitment into SMT let tree = await smt.newMemEmptyTrie(); await tree.insert(1, 0); - // await tree.insert(2, 0); let rootOld = tree.root; - let res = await tree.find(2); - // console.log(res); - assert(!res.found); - let siblingsOld = res.siblings; - while (siblingsOld.length < nLevels) { - siblingsOld.push("0"); - }; - console.log("siblingsOld", siblingsOld); - await tree.insert(2, commitment); + let res = await tree.insert(2, commitment); + console.log("INSERT", res); let rootNew = tree.root; - res = await tree.find(2); - // console.log(res); - assert(res.found); - let siblingsNew = res.siblings; - while (siblingsNew.length < nLevels) { - siblingsNew.push("0"); + let siblings = res.siblings; + while (siblings.length < nLevels) { + siblings.push("0"); }; - console.log("siblingsNew", siblingsNew); - console.log("rootOld", rootOld); - console.log("rootNew", rootNew); + console.log("siblings", siblings); + + console.log(res); const witness = await circuit.calculateWitness({ "coinCode": coinCode, "amount": amount, "secret": secret, - "oldKey": "1", - "oldValue": "0", - "siblingsOld": siblingsOld, - "siblingsNew": siblingsNew, - "rootOld": rootOld, - "rootNew": rootNew, + "oldKey": res.isOld0 ? 0 : res.oldKey, + "oldValue": res.isOld0 ? 0 : res.oldValue, + "isOld0": res.isOld0 ? 1 : 0, + "siblings": siblings, + "rootOld": res.oldRoot, + "rootNew": res.newRoot, "commitment": commitment, "key": 2 }); diff --git a/test/contracts/miksi.test.ts b/test/contracts/miksi.test.ts index c146dec..d339d3b 100644 --- a/test/contracts/miksi.test.ts +++ b/test/contracts/miksi.test.ts @@ -77,11 +77,11 @@ contract("miksi", (accounts) => { // expect(balance_wei).to.be.equal('98993526980000000000'); }); it("Make second deposit", async () => { - await computeTree(1); + // await computeTree(1); await makeDeposit(1, addr3); }); it("Make 3rd deposit", async () => { - await computeTree(2); + // await computeTree(2); await makeDeposit(2, addr3); }); @@ -183,12 +183,9 @@ async function computeTree(u) { } async function makeDeposit(u, addr) { - const resC = await tree.find(currKey); - assert(resC.found); - siblingsNew[u] = resC.siblings; - while (siblingsNew[u].length < nLevels) { - siblingsNew[u].push("0"); - }; + let resInsert = await tree.insert(currKey+1, commitment[u]); + rootNew[u] = tree.root; + currKey += 1; // calculate witness const wasm = await fs.promises.readFile("./test/build/deposit.wasm"); @@ -196,12 +193,12 @@ async function makeDeposit(u, addr) { "coinCode": coinCode, "amount": amount, "secret": secret[u], - "oldKey": oldKey[u], - "oldValue": oldValue[u], - "siblingsOld": siblingsOld[u], - "siblingsNew": siblingsNew[u], - "rootOld": rootOld[u], - "rootNew": rootNew[u], + "oldKey": resInsert.isOld0 ? 0 : resInsert.oldKey, + "oldValue": resInsert.isOld0 ? 0 : resInsert.oldValue, + "isOld0": resInsert.isOld0 ? 1 : 0, + "siblings": resInsert.siblings, + "rootOld": resInsert.oldRoot, + "rootNew": resInsert.newRoot, "commitment": commitment[u], "key": currKey }); @@ -220,7 +217,7 @@ async function makeDeposit(u, addr) { publicSignals[u] = res.publicSignals; const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./test/build/deposit-verification_key.json", "utf8"))); - let pubI = unstringifyBigInts([coinCode, amount, rootOld[u].toString(), rootNew[u].toString(), commitment[u], currKey]); + let pubI = unstringifyBigInts([coinCode, amount, res.oldRoot.toString(), res.newRoot.toString(), commitment[u], currKey]); let validCheck = groth.isValid(verificationKey, proof[u], pubI); assert(validCheck); await insMiksi.deposit(