From ee84a28c17a68b7e13c7e790898b3c49bbeae0f6 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Mon, 11 May 2020 15:30:33 +0200 Subject: [PATCH] Add spec draft to README --- README.md | 32 ++++++++++++++++++++++++++++++++ test/contracts/miksi.test.ts | 13 ++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a8362f8..f5ea805 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,35 @@ npm run test-sc ./compile-circuits.sh ``` + +## Spec draft + +### Deposit +- user generates a random `secret` & `nullifier` +- computes the `commitment`, which is the Poseidon hash: `commitment = H(coinCode, amount, secret, nullifier)`, where: + - `coinCode`: code that specifies which currency is being used (`0`==ETH) + - `amount`: the amount to be deposited + - `secret`: random, private + - `nullifier`: random +- get all the commitments from the SmartContract +- build the MerkleTree with the getted commitments +- add the new computed `commitment` into the MerkleTree +- generate zkSNARK proof, where is proving: + - prover knows the `secret` & `nullifier` for the `commitment` + - the transition from `RootOld` (the current one in the Smart Contract) to `RootNew` has been done following the rules (only one addition, no deletion) +- user sends ETH to the smart contract `deposit` call, together with the zkProof data + +Deposit circuit can be found [here](https://github.com/miksi-labs/miksi-core/blob/master/circuits/deposit.circom). + +### Withdraw +- user gets all the commitments from the SmartContract +- build the MerkleTree with the getted commitments +- generate the siblings for the `commitment` of which the user knows the `secret` & `nullifier` +- generate zkSNARK proof, where is proving: + - user knows a `secret` for a public `nullifier` + - which `commitment` is in the MerkleTree + - which MerkleTree `root` is the one that knows the SmartContract +- if the zkProof verification passes, and the nullifier was not already used, the Smart Contract sends the ETH to the specified address + +Withdraw circuit can be found [here](https://github.com/miksi-labs/miksi-core/blob/master/circuits/withdraw.circom). + diff --git a/test/contracts/miksi.test.ts b/test/contracts/miksi.test.ts index 61ac6b3..e64412c 100644 --- a/test/contracts/miksi.test.ts +++ b/test/contracts/miksi.test.ts @@ -41,9 +41,9 @@ contract("miksi", (accounts) => { let commitment; let proof; let publicSignals; + let commitmentsArray; before(async () => { - insDepositVerifier = await DepositVerifier.new(); insWithdrawVerifier = await WithdrawVerifier.new(); insMiksi = await Miksi.new(insDepositVerifier.address, insWithdrawVerifier.address); @@ -138,6 +138,17 @@ contract("miksi", (accounts) => { let res = await insMiksi.getCommitments(); expect(res[0][0].toString()).to.be.equal('189025084074544266465422070282645213792582195466360448472858620722286781863'); expect(res[1].toString()).to.be.equal('9328869343897770565751281504295758914771207504252217956739346620422361279598'); + console.log(res[0]); + commitmentsArray = res[0]; + }); + + it("Rebuild the tree from sc commitments", async () => { + let treeTmp = await smt.newMemEmptyTrie(); + await treeTmp.insert(1, 0); + for (let i=0; i {