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.

75 lines
2.2 KiB

  1. const path = require("path");
  2. const tester = require("circom").tester;
  3. const chai = require("chai");
  4. const assert = chai.assert;
  5. const circomlib = require("circomlib");
  6. const smt = require("circomlib").smt;
  7. export {};
  8. describe("deposit test", function () {
  9. this.timeout(200000);
  10. it("Test Deposit", async () => {
  11. const circuit = await tester(
  12. path.join(__dirname, "../../circuits", "deposit.circom"),
  13. {reduceConstraints: false}
  14. );
  15. const nLevels = 5;
  16. const secret = "1234567890";
  17. const coinCode = "0";
  18. const amount = '1000000000000000000';
  19. const nullifier = "567891234";
  20. const poseidon = circomlib.poseidon.createHash(6, 8, 57);
  21. const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
  22. // add commitment into SMT
  23. let tree = await smt.newMemEmptyTrie();
  24. await tree.insert(1, 0);
  25. // await tree.insert(2, 0);
  26. let rootOld = tree.root;
  27. let res = await tree.find(commitment);
  28. // console.log(res);
  29. assert(!res.found);
  30. let siblingsOld = res.siblings;
  31. while (siblingsOld.length < nLevels) {
  32. siblingsOld.push("0");
  33. };
  34. console.log("siblingsOld", siblingsOld);
  35. await tree.insert(2, commitment);
  36. let rootNew = tree.root;
  37. res = await tree.find(2);
  38. // console.log(res);
  39. assert(res.found);
  40. let siblingsNew = res.siblings;
  41. while (siblingsNew.length < nLevels) {
  42. siblingsNew.push("0");
  43. };
  44. console.log("siblingsNew", siblingsNew);
  45. console.log("rootOld", rootOld);
  46. console.log("rootNew", rootNew);
  47. const witness = await circuit.calculateWitness({
  48. "coinCode": coinCode,
  49. "amount": amount,
  50. "secret": secret,
  51. "nullifier": nullifier,
  52. "oldKey": "1",
  53. "oldValue": "0",
  54. "siblingsOld": siblingsOld,
  55. "siblingsNew": siblingsNew,
  56. "rootOld": rootOld,
  57. "rootNew": rootNew,
  58. "commitment": commitment,
  59. "key": 2
  60. });
  61. await circuit.checkConstraints(witness);
  62. });
  63. });