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, "main", "deposit.circom"),
  13. {reduceConstraints: false}
  14. );
  15. const nLevels = 4;
  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 nullifier = poseidon([2, secret]);
  22. const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
  23. // add commitment into SMT
  24. let tree = await smt.newMemEmptyTrie();
  25. await tree.insert(1, 0);
  26. // await tree.insert(2, 0);
  27. let rootOld = tree.root;
  28. let res = await tree.find(2);
  29. // console.log(res);
  30. assert(!res.found);
  31. let siblingsOld = res.siblings;
  32. while (siblingsOld.length < nLevels) {
  33. siblingsOld.push("0");
  34. };
  35. console.log("siblingsOld", siblingsOld);
  36. await tree.insert(2, commitment);
  37. let rootNew = tree.root;
  38. res = await tree.find(2);
  39. // console.log(res);
  40. assert(res.found);
  41. let siblingsNew = res.siblings;
  42. while (siblingsNew.length < nLevels) {
  43. siblingsNew.push("0");
  44. };
  45. console.log("siblingsNew", siblingsNew);
  46. console.log("rootOld", rootOld);
  47. console.log("rootNew", rootNew);
  48. const witness = await circuit.calculateWitness({
  49. "coinCode": coinCode,
  50. "amount": amount,
  51. "secret": secret,
  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. });