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.

94 lines
3.1 KiB

  1. const fs = require("fs");
  2. const path = require("path");
  3. const tester = require("circom").tester;
  4. const assert = require('assert');
  5. const circomlib = require("circomlib");
  6. const smt = require("circomlib").smt;
  7. describe("merkletreetree circom-proof-verifier", function () {
  8. this.timeout(0);
  9. const nLevels = 4;
  10. let circuit;
  11. let circuitPath = path.join(__dirname, "circuits", "mt-proof-verifier-main.test.circom");
  12. before( async() => {
  13. const circuitCode = `
  14. include "smt-proof-verifier_test.circom";
  15. component main = SMTVerifierTest(4);
  16. `;
  17. fs.writeFileSync(circuitPath, circuitCode, "utf8");
  18. circuit = await tester(circuitPath, {reduceConstraints:false});
  19. await circuit.loadConstraints();
  20. console.log("Constraints: " + circuit.constraints.length + "\n");
  21. });
  22. after( async() => {
  23. fs.unlinkSync(circuitPath);
  24. });
  25. let inputsVerifier, inputsVerifierNonExistence;
  26. before("generate smt-verifier js inputs", async () => {
  27. let tree = await smt.newMemEmptyTrie();
  28. await tree.insert(1, 11);
  29. await tree.insert(2, 22);
  30. await tree.insert(3, 33);
  31. await tree.insert(4, 44);
  32. const res = await tree.find(2);
  33. assert(res.found);
  34. let root = tree.root;
  35. let siblings = res.siblings;
  36. while (siblings.length < nLevels) {
  37. siblings.push("0");
  38. };
  39. inputsVerifier = {
  40. "fnc": 0,
  41. "key": 2,
  42. "value": 22,
  43. "siblings": siblings,
  44. "root": root,
  45. };
  46. const res2 = await tree.find(5);
  47. assert(!res2.found);
  48. let siblings2 = res2.siblings;
  49. while (siblings2.length < nLevels) {
  50. siblings2.push("0");
  51. };
  52. inputsVerifierNonExistence = {
  53. "fnc": 1,
  54. "oldKey": 1,
  55. "oldValue": 11,
  56. "key": 5,
  57. "value": 11,
  58. "siblings": siblings2,
  59. "root": root,
  60. };
  61. });
  62. it("Test smt-verifier proof of existence go inputs", async () => {
  63. // fromGo is a json CircomVerifierProof generated from Go code using
  64. // https://github.com/vocdoni/arbo
  65. let rawdata = fs.readFileSync('go-data-generator/go-smt-verifier-inputs.json');
  66. let fromGo = JSON.parse(rawdata);
  67. inputsVerifier=fromGo;
  68. // console.log("smtverifier js inputs:\n", inputsVerifier);
  69. const witness = await circuit.calculateWitness(inputsVerifier);
  70. await circuit.checkConstraints(witness);
  71. });
  72. it("Test smt-verifier proof of non-existence go inputs", async () => {
  73. // fromGo is a json CircomVerifierProof generated from Go code using
  74. // https://github.com/vocdoni/arbo
  75. let rawdata = fs.readFileSync('go-data-generator/go-smt-verifier-non-existence-inputs.json');
  76. let fromGo = JSON.parse(rawdata);
  77. inputsVerifierNonExistence=fromGo;
  78. // console.log("smtverifier js inputs:\n", inputsVerifierNonExistence);
  79. const witness = await circuit.calculateWitness(inputsVerifierNonExistence);
  80. await circuit.checkConstraints(witness);
  81. });
  82. });