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.

115 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const smt = require("../src/smt.js");
  6. const assert = chai.assert;
  7. const bigInt = snarkjs.bigInt;
  8. function print(circuit, w, s) {
  9. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  10. }
  11. async function testInclusion(tree, key, circuit) {
  12. const res = await tree.find(key);
  13. assert(res.found);
  14. let siblings = res.siblings;
  15. while (siblings.length<10) siblings.push(bigInt(0));
  16. const w = circuit.calculateWitness({
  17. enabled: 1,
  18. fnc: 0,
  19. root: tree.root,
  20. siblings: siblings,
  21. oldKey: 0,
  22. oldValue: 0,
  23. isOld0: 0,
  24. key: key,
  25. value: res.foundValue
  26. });
  27. assert(circuit.checkWitness(w));
  28. }
  29. async function testExclusion(tree, key, circuit) {
  30. const res = await tree.find(key);
  31. assert(!res.found);
  32. let siblings = res.siblings;
  33. while (siblings.length<10) siblings.push(bigInt(0));
  34. const w = circuit.calculateWitness({
  35. enabled: 1,
  36. fnc: 1,
  37. root: tree.root,
  38. siblings: siblings,
  39. oldKey: res.isOld0 ? 0 : res.notFoundKey,
  40. oldValue: res.isOld0 ? 0 : res.notFoundValue,
  41. isOld0: res.isOld0 ? 1 : 0,
  42. key: key,
  43. value: 0
  44. });
  45. assert(circuit.checkWitness(w));
  46. }
  47. describe("SMT test", function () {
  48. let circuit;
  49. let tree;
  50. this.timeout(100000);
  51. before( async () => {
  52. const cirDef = await compiler(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
  53. circuit = new snarkjs.Circuit(cirDef);
  54. console.log("NConstrains SMTVerifier: " + circuit.nConstraints);
  55. tree = await smt.newMemEmptyTrie();
  56. await tree.insert(7,77);
  57. await tree.insert(8,88);
  58. await tree.insert(32,3232);
  59. });
  60. it("Check inclussion in a tree of 3", async () => {
  61. await testInclusion(tree, 7, circuit);
  62. await testInclusion(tree, 8, circuit);
  63. await testInclusion(tree, 32, circuit);
  64. });
  65. it("Check exclussion in a tree of 3", async () => {
  66. await testExclusion(tree, 0, circuit);
  67. await testExclusion(tree, 6, circuit);
  68. await testExclusion(tree, 9, circuit);
  69. await testExclusion(tree, 33, circuit);
  70. await testExclusion(tree, 31, circuit);
  71. await testExclusion(tree, 16, circuit);
  72. await testExclusion(tree, 64, circuit);
  73. });
  74. it("Check not enabled accepts any thing", async () => {
  75. let siblings = [];
  76. for (let i=0; i<10; i++) siblings.push(i);
  77. const w = circuit.calculateWitness({
  78. enabled: 0,
  79. fnc: 0,
  80. root: 1,
  81. siblings: siblings,
  82. oldKey: 22,
  83. oldValue: 33,
  84. isOld0: 0,
  85. key: 44,
  86. value: 0
  87. });
  88. assert(circuit.checkWitness(w));
  89. });
  90. });