You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
9.6 KiB

  1. const DepositVerifier = artifacts.require("../../contracts/DepositVerifier");
  2. const WithdrawVerifier = artifacts.require("../../contracts/WithdrawVerifier");
  3. const Miksi = artifacts.require("../../contracts/Miksi.sol");
  4. const chai = require("chai");
  5. const expect = chai.expect;
  6. const truffleAssert = require('truffle-assertions');
  7. const fs = require("fs");
  8. const { groth } = require('snarkjs');
  9. const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
  10. const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
  11. const circomlib = require("circomlib");
  12. const smt = require("circomlib").smt;
  13. let insVerifier;
  14. let insMiksi;
  15. const nLevels = 17;
  16. const secret = ["1234567890", "987654321", "123"];
  17. const coinCode = "0"; // refearing to ETH
  18. const ethAmount = '1';
  19. const amount = web3.utils.toWei(ethAmount, 'ether');
  20. const nullifier = ["567891234", "432198765", "321"];
  21. let commitment = [];
  22. let tree;
  23. let oldKey = [];
  24. let oldValue = [];
  25. let siblingsOld = [];
  26. let siblingsNew = [];
  27. let rootOld = [];
  28. let rootNew = [];
  29. // let commitment = [];
  30. let proof = [];
  31. let publicSignals = [];
  32. let commitmentsArray = [];
  33. let currKey=0;
  34. let u = 0;
  35. contract("miksi", (accounts) => {
  36. const {
  37. 0: owner,
  38. 1: addr1, // used for the deposit
  39. 2: addr2, // used for the withdraw
  40. 3: addr3,
  41. 4: addr4,
  42. } = accounts;
  43. before(async () => {
  44. insDepositVerifier = await DepositVerifier.new();
  45. insWithdrawVerifier = await WithdrawVerifier.new();
  46. insMiksi = await Miksi.new(insDepositVerifier.address, insWithdrawVerifier.address);
  47. });
  48. before(async() => {
  49. let balance_wei = await web3.eth.getBalance(addr1);
  50. // console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether'));
  51. expect(balance_wei).to.be.equal('100000000000000000000');
  52. tree = await smt.newMemEmptyTrie();
  53. await tree.insert(currKey, 0);
  54. await computeTree(0);
  55. expect(rootOld[0].toString()).to.be.equal('7191590165524151132621032034309259185021876706372059338263145339926209741311');
  56. // expect(rootNew[0].toString()).to.be.equal('9328869343897770565751281504295758914771207504252217956739346620422361279598');
  57. });
  58. it("Make first deposit", async () => {
  59. await makeDeposit(0, addr1);
  60. balance_wei = await web3.eth.getBalance(addr1);
  61. // console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether'));
  62. // expect(balance_wei).to.be.equal('98993526980000000000');
  63. });
  64. it("Make second deposit", async () => {
  65. await computeTree(1);
  66. await makeDeposit(1, addr3);
  67. });
  68. it("Make 3rd deposit", async () => {
  69. await computeTree(2);
  70. await makeDeposit(2, addr3);
  71. });
  72. it("Get the commitments data", async () => {
  73. // getCommitments data
  74. let res = await insMiksi.getCommitments();
  75. expect(res[0][0].toString()).to.be.equal('189025084074544266465422070282645213792582195466360448472858620722286781863');
  76. // expect(res[1].toString()).to.be.equal('9328869343897770565751281504295758914771207504252217956739346620422361279598');
  77. console.log(res[0]);
  78. commitmentsArray[0] = res[0];
  79. currKey = res[2];
  80. });
  81. it("Rebuild the tree from sc commitments", async () => {
  82. let treeTmp = await smt.newMemEmptyTrie();
  83. await treeTmp.insert(0, 0);
  84. for (let i=0; i<commitmentsArray[0].length; i++) {
  85. await treeTmp.insert(i+1, commitmentsArray[0][i]);
  86. }
  87. expect(treeTmp.root).to.be.equal(tree.root);
  88. });
  89. it("Calculate witness and generate the zkProof", async () => {
  90. this.timeout(10000000);
  91. await genZKProof(0, addr2, "1");
  92. await genZKProof(1, addr4, "2");
  93. await genZKProof(2, addr4, "3");
  94. });
  95. it("Try to use the zkProof with another address and get revert", async () => {
  96. // console.log("Try to reuse the zkproof and expect revert");
  97. await truffleAssert.fails(
  98. withdrawSC(0, addr1),
  99. truffleAssert.ErrorType.REVERT,
  100. "zkProof withdraw could not be verified"
  101. );
  102. });
  103. it("Withdraw 1 ETH with the zkProof of the 1st deposit to addr2", async () => {
  104. // withdraw
  105. // console.log("Withdraw of " + ethAmount + " ETH to " + addr2);
  106. let resW = await withdrawSC(0, addr2);
  107. // console.log("resW", resW);
  108. balance_wei = await web3.eth.getBalance(addr2);
  109. // console.log("Balance at " + addr2, web3.utils.fromWei(balance_wei, 'ether'));
  110. expect(balance_wei).to.be.equal('101000000000000000000');
  111. });
  112. it("Try to reuse the zkProof and get revert", async () => {
  113. // console.log("Try to reuse the zkproof and expect revert");
  114. await truffleAssert.fails(
  115. withdrawSC(0, addr2),
  116. truffleAssert.ErrorType.REVERT,
  117. "nullifier already used"
  118. );
  119. balance_wei = await web3.eth.getBalance(addr2);
  120. expect(balance_wei).to.be.equal('101000000000000000000');
  121. });
  122. it("Withdraw 1 ETH with the zkProof of the 2nd deposit to addr4", async () => {
  123. let resW = await withdrawSC(1, addr4);
  124. balance_wei = await web3.eth.getBalance(addr4);
  125. expect(balance_wei).to.be.equal('101000000000000000000');
  126. });
  127. it("Withdraw 1 ETH with the zkProof of the 3rd deposit to addr4", async () => {
  128. let resW = await withdrawSC(2, addr4);
  129. balance_wei = await web3.eth.getBalance(addr4);
  130. expect(balance_wei).to.be.equal('102000000000000000000');
  131. });
  132. });
  133. async function computeTree(u) {
  134. const poseidon = circomlib.poseidon.createHash(6, 8, 57);
  135. commitment[u] = poseidon([coinCode, amount, secret[u], nullifier[u]]).toString();
  136. // deposit
  137. // add commitment into SMT
  138. // console.log("currKey", currKey);
  139. rootOld[u] = tree.root;
  140. const resC = await tree.find(currKey+1);
  141. assert(!resC.found);
  142. oldKey[u] = "0";
  143. oldValue[u] = "0";
  144. if (!resC.found) {
  145. oldKey[u] = resC.notFoundKey.toString();
  146. oldValue[u] = resC.notFoundValue.toString();
  147. }
  148. // console.log(oldValue[u]);
  149. // console.log("FIND", resC);
  150. siblingsOld[u] = resC.siblings;
  151. while (siblingsOld[u].length < nLevels) {
  152. siblingsOld[u].push("0");
  153. };
  154. await tree.insert(currKey+1, commitment[u]);
  155. rootNew[u] = tree.root;
  156. currKey += 1;
  157. // console.log("currKey", currKey);
  158. }
  159. async function makeDeposit(u, addr) {
  160. const resC = await tree.find(currKey);
  161. assert(resC.found);
  162. siblingsNew[u] = resC.siblings;
  163. while (siblingsNew[u].length < nLevels) {
  164. siblingsNew[u].push("0");
  165. };
  166. // calculate witness
  167. const wasm = await fs.promises.readFile("./build/deposit.wasm");
  168. const input = unstringifyBigInts({
  169. "coinCode": coinCode,
  170. "amount": amount,
  171. "secret": secret[u],
  172. "nullifier": nullifier[u],
  173. "oldKey": oldKey[u],
  174. "oldValue": oldValue[u],
  175. "siblingsOld": siblingsOld[u],
  176. "siblingsNew": siblingsNew[u],
  177. "rootOld": rootOld[u],
  178. "rootNew": rootNew[u],
  179. "commitment": commitment[u],
  180. "key": currKey
  181. });
  182. const options = {};
  183. // console.log("Calculate witness");
  184. const wc = await WitnessCalculatorBuilder(wasm, options);
  185. const w = await wc.calculateWitness(input);
  186. const witness = unstringifyBigInts(stringifyBigInts(w));
  187. // generate zkproof of commitment using snarkjs (as is a test)
  188. const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/deposit-proving_key.json", "utf8")));
  189. // console.log("Generate zkSNARK proof");
  190. const res = groth.genProof(provingKey, witness);
  191. proof[u] = res.proof;
  192. publicSignals[u] = res.publicSignals;
  193. const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/deposit-verification_key.json", "utf8")));
  194. let pubI = unstringifyBigInts([coinCode, amount, rootOld[u].toString(), rootNew[u].toString(), commitment[u], currKey]);
  195. let validCheck = groth.isValid(verificationKey, proof[u], pubI);
  196. assert(validCheck);
  197. await insMiksi.deposit(
  198. commitment[u],
  199. tree.root.toString(),
  200. [proof[u].pi_a[0].toString(), proof[u].pi_a[1].toString()],
  201. [
  202. [proof[u].pi_b[0][1].toString(), proof[u].pi_b[0][0].toString()],
  203. [proof[u].pi_b[1][1].toString(), proof[u].pi_b[1][0].toString()]
  204. ],
  205. [proof[u].pi_c[0].toString(), proof[u].pi_c[1].toString()],
  206. {from: addr, value: amount}
  207. );
  208. }
  209. async function genZKProof(u, addr, k) {
  210. const resC = await tree.find(k);
  211. assert(resC.found);
  212. let siblings = resC.siblings;
  213. while (siblings.length < nLevels) {
  214. siblings.push("0");
  215. };
  216. // console.log("siblings", siblings);
  217. // calculate witness
  218. const wasm = await fs.promises.readFile("./build/withdraw.wasm");
  219. const input = unstringifyBigInts({
  220. "coinCode": coinCode,
  221. "amount": amount,
  222. "secret": secret[u],
  223. "nullifier": nullifier[u],
  224. "siblings": siblings,
  225. "root": tree.root,
  226. "address": addr,
  227. "key": k
  228. });
  229. const options = {};
  230. // console.log("Calculate witness");
  231. const wc = await WitnessCalculatorBuilder(wasm, options);
  232. const w = await wc.calculateWitness(input);
  233. const witness = unstringifyBigInts(stringifyBigInts(w));
  234. // generate zkproof of commitment using snarkjs (as is a test)
  235. const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/withdraw-proving_key.json", "utf8")));
  236. // console.log("Generate zkSNARK proof");
  237. const res = groth.genProof(provingKey, witness);
  238. proof[u] = res.proof;
  239. publicSignals[u] = res.publicSignals;
  240. }
  241. async function withdrawSC(u, addr) {
  242. return insMiksi.withdraw(
  243. addr,
  244. nullifier[u],
  245. [proof[u].pi_a[0].toString(), proof[u].pi_a[1].toString()],
  246. [
  247. [proof[u].pi_b[0][1].toString(), proof[u].pi_b[0][0].toString()],
  248. [proof[u].pi_b[1][1].toString(), proof[u].pi_b[1][0].toString()]
  249. ],
  250. [proof[u].pi_c[0].toString(), proof[u].pi_c[1].toString()]
  251. );
  252. }