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.

172 lines
5.8 KiB

  1. const Verifier = artifacts.require("../../contracts/Verifier");
  2. const Miksi = artifacts.require("../../contracts/Miksi.sol");
  3. const chai = require("chai");
  4. const expect = chai.expect;
  5. const truffleAssert = require('truffle-assertions');
  6. const fs = require("fs");
  7. const { groth } = require('snarkjs');
  8. const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
  9. const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
  10. const circomlib = require("circomlib");
  11. const smt = require("circomlib").smt;
  12. contract("miksi", (accounts) => {
  13. const {
  14. 0: owner,
  15. 1: addr1, // used for the deposit
  16. 2: addr2, // used for the withdraw
  17. 3: addr3,
  18. } = accounts;
  19. let insVerifier;
  20. let insMiksi;
  21. const nLevels = 5;
  22. const secret = "1234567890";
  23. const coinCode = "0"; // refearing to ETH
  24. const ethAmount = '1';
  25. const amount = web3.utils.toWei(ethAmount, 'ether');
  26. const nullifier = "567891234";
  27. let tree;
  28. let commitment;
  29. let proof;
  30. let publicSignals;
  31. before(async () => {
  32. insVerifier = await Verifier.new();
  33. insMiksi = await Miksi.new(insVerifier.address);
  34. });
  35. before(async() => {
  36. let balance_wei = await web3.eth.getBalance(addr1);
  37. // console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether'));
  38. expect(balance_wei).to.be.equal('100000000000000000000');
  39. const poseidon = circomlib.poseidon.createHash(6, 8, 57);
  40. commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
  41. // deposit
  42. // add commitment into SMT
  43. tree = await smt.newMemEmptyTrie();
  44. await tree.insert(commitment, 0);
  45. await tree.insert(1, 0);
  46. await tree.insert(2, 0);
  47. expect(tree.root.toString()).to.be.equal('9712258649847843172766744803572924784812438285433990419902675958769413333474');
  48. });
  49. it("Make the deposit", async () => {
  50. // console.log("root", tree.root);
  51. // console.log("Deposit of " + ethAmount + " ETH from " + addr1 + ".\nCommitment "+commitment+", root: "+ tree.root);
  52. await insMiksi.deposit(commitment, tree.root.toString(), {from: addr1, value: amount});
  53. balance_wei = await web3.eth.getBalance(addr1);
  54. // console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether'));
  55. expect(balance_wei).to.be.equal('98998318640000000000');
  56. });
  57. it("Get the commitments data", async () => {
  58. // getCommitments data
  59. let res = await insMiksi.getCommitments();
  60. expect(res[0][0].toString()).to.be.equal('189025084074544266465422070282645213792582195466360448472858620722286781863');
  61. expect(res[1].toString()).to.be.equal('9712258649847843172766744803572924784812438285433990419902675958769413333474');
  62. });
  63. it("Calculate witness and generate the zkProof", async () => {
  64. const resC = await tree.find(commitment);
  65. assert(resC.found);
  66. let siblings = resC.siblings;
  67. while (siblings.length < nLevels) {
  68. siblings.push("0");
  69. };
  70. // console.log("siblings", siblings);
  71. // calculate witness
  72. const wasm = await fs.promises.readFile("./build/withdraw.wasm");
  73. const input = unstringifyBigInts({
  74. "coinCode": coinCode,
  75. "amount": amount,
  76. "secret": secret,
  77. "nullifier": nullifier,
  78. "siblings": siblings,
  79. "root": tree.root,
  80. "address": addr2
  81. });
  82. const options = {};
  83. // console.log("Calculate witness");
  84. const wc = await WitnessCalculatorBuilder(wasm, options);
  85. const w = await wc.calculateWitness(input);
  86. const witness = unstringifyBigInts(stringifyBigInts(w));
  87. // generate zkproof of commitment using snarkjs (as is a test)
  88. const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/proving_key.json", "utf8")));
  89. // console.log("Generate zkSNARK proof");
  90. const res = groth.genProof(provingKey, witness);
  91. proof = res.proof;
  92. publicSignals = res.publicSignals;
  93. });
  94. it("Try to use the zkProof with another address and get revert", async () => {
  95. // console.log("Try to reuse the zkproof and expect revert");
  96. await truffleAssert.fails(
  97. insMiksi.withdraw(
  98. addr1,
  99. nullifier,
  100. [proof.pi_a[0].toString(), proof.pi_a[1].toString()],
  101. [
  102. [proof.pi_b[0][1].toString(), proof.pi_b[0][0].toString()],
  103. [proof.pi_b[1][1].toString(), proof.pi_b[1][0].toString()]
  104. ],
  105. [proof.pi_c[0].toString(), proof.pi_c[1].toString()]
  106. ),
  107. truffleAssert.ErrorType.REVERT,
  108. "zkProof withdraw could not be verified"
  109. );
  110. });
  111. it("Withdraw 1 ETH with the zkProof", async () => {
  112. // withdraw
  113. // console.log("Withdraw of " + ethAmount + " ETH to " + addr2);
  114. let resW = await insMiksi.withdraw(
  115. addr2,
  116. nullifier,
  117. [proof.pi_a[0].toString(), proof.pi_a[1].toString()],
  118. [
  119. [proof.pi_b[0][1].toString(), proof.pi_b[0][0].toString()],
  120. [proof.pi_b[1][1].toString(), proof.pi_b[1][0].toString()]
  121. ],
  122. [proof.pi_c[0].toString(), proof.pi_c[1].toString()]
  123. );
  124. // console.log("resW", resW);
  125. balance_wei = await web3.eth.getBalance(addr2);
  126. // console.log("Balance at " + addr2, web3.utils.fromWei(balance_wei, 'ether'));
  127. expect(balance_wei).to.be.equal('101000000000000000000');
  128. });
  129. it("Try to reuse the zkProof and get revert", async () => {
  130. // console.log("Try to reuse the zkproof and expect revert");
  131. await truffleAssert.fails(
  132. insMiksi.withdraw(
  133. addr2,
  134. nullifier,
  135. [proof.pi_a[0].toString(), proof.pi_a[1].toString()],
  136. [
  137. [proof.pi_b[0][1].toString(), proof.pi_b[0][0].toString()],
  138. [proof.pi_b[1][1].toString(), proof.pi_b[1][0].toString()]
  139. ],
  140. [proof.pi_c[0].toString(), proof.pi_c[1].toString()]
  141. ),
  142. truffleAssert.ErrorType.REVERT,
  143. "nullifier already used"
  144. );
  145. balance_wei = await web3.eth.getBalance(addr2);
  146. expect(balance_wei).to.be.equal('101000000000000000000');
  147. });
  148. });