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.

63 lines
1.8 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 poseidon = circomlib.poseidon.createHash(6, 8, 57);
  20. const nullifier = poseidon([2, secret]);
  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. let rootOld = tree.root;
  26. let res = await tree.insert(2, commitment);
  27. console.log("INSERT", res);
  28. let rootNew = tree.root;
  29. let siblings = res.siblings;
  30. while (siblings.length < nLevels) {
  31. siblings.push("0");
  32. };
  33. console.log("siblings", siblings);
  34. console.log(res);
  35. const witness = await circuit.calculateWitness({
  36. "coinCode": coinCode,
  37. "amount": amount,
  38. "secret": secret,
  39. "oldKey": res.isOld0 ? 0 : res.oldKey,
  40. "oldValue": res.isOld0 ? 0 : res.oldValue,
  41. "isOld0": res.isOld0 ? 1 : 0,
  42. "siblings": siblings,
  43. "rootOld": res.oldRoot,
  44. "rootNew": res.newRoot,
  45. "commitment": commitment,
  46. "key": 2
  47. });
  48. await circuit.checkConstraints(witness);
  49. });
  50. });