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.

136 lines
3.9 KiB

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