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.

141 lines
4.3 KiB

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