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.

189 lines
5.7 KiB

  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 testInsert(tree, key, value, circuit, log ) {
  12. const res = await tree.insert(key,value);
  13. let siblings = res.sibblings;
  14. while (siblings.length<10) siblings.push(bigInt(0));
  15. const w = circuit.calculateWitness({
  16. fnc: [1,0],
  17. oldRoot: res.oldRoot,
  18. newRoot: res.newRoot,
  19. siblings: siblings,
  20. oldKey: res.oldKey,
  21. oldValue: res.oldValue,
  22. isOld0: res.isOld0 ? 1 : 0,
  23. newKey: key,
  24. newValue: value
  25. }, log);
  26. const root1 = w[circuit.getSignalIdx("main.topSwitcher.outR")];
  27. assert(circuit.checkWitness(w));
  28. assert(root1.equals(res.newRoot));
  29. }
  30. async function testDelete(tree, key, circuit) {
  31. const res = await tree.delete(key);
  32. let siblings = res.sibblings;
  33. while (siblings.length<10) siblings.push(bigInt(0));
  34. const w = circuit.calculateWitness({
  35. fnc: [1,1],
  36. oldRoot: res.oldRoot,
  37. newRoot: res.newRoot,
  38. siblings: siblings,
  39. oldKey: res.oldKey,
  40. oldValue: res.oldValue,
  41. isOld0: res.isOld0 ? 1 : 0,
  42. newKey: res.delKey,
  43. newValue: res.delValue
  44. });
  45. const root1 = w[circuit.getSignalIdx("main.topSwitcher.outR")];
  46. assert(circuit.checkWitness(w));
  47. assert(root1.equals(res.newRoot));
  48. }
  49. describe("SMT test", function () {
  50. let circuit;
  51. let tree;
  52. this.timeout(100000);
  53. before( async () => {
  54. const cirDef = await compiler(path.join(__dirname, "circuits", "smtinsert10_test.circom"));
  55. circuit = new snarkjs.Circuit(cirDef);
  56. console.log("NConstrains SMTInsert: " + circuit.nConstraints);
  57. tree = await smt.newMemEmptyTrie();
  58. });
  59. it("Should verify an insert to an empty tree", async () => {
  60. const key = bigInt(111);
  61. const value = bigInt(222);
  62. await testInsert(tree, key, value, circuit);
  63. });
  64. it("It should add another element", async () => {
  65. const key = bigInt(333);
  66. const value = bigInt(444);
  67. await testInsert(tree, key, value, circuit);
  68. });
  69. it("Should remove an element", async () => {
  70. await testDelete(tree, 111, circuit);
  71. await testDelete(tree, 333, circuit);
  72. });
  73. it("Should test convination of adding and removing 3 elements", async () => {
  74. const keys = [bigInt(8), bigInt(9), bigInt(32)];
  75. const values = [bigInt(88), bigInt(99), bigInt(3232)];
  76. const tree1 = await smt.newMemEmptyTrie();
  77. const tree2 = await smt.newMemEmptyTrie();
  78. const tree3 = await smt.newMemEmptyTrie();
  79. const tree4 = await smt.newMemEmptyTrie();
  80. const tree5 = await smt.newMemEmptyTrie();
  81. const tree6 = await smt.newMemEmptyTrie();
  82. await testInsert(tree1,keys[0],values[0], circuit);
  83. await testInsert(tree1,keys[1],values[1], circuit, console.log);
  84. /* await testInsert(tree1,keys[2],values[2], circuit);
  85. await testInsert(tree2,keys[0],values[0], circuit);
  86. await testInsert(tree2,keys[2],values[2], circuit);
  87. await testInsert(tree2,keys[1],values[1], circuit);
  88. await testInsert(tree3,keys[1],values[1], circuit);
  89. await testInsert(tree3,keys[0],values[0], circuit);
  90. await testInsert(tree3,keys[2],values[2], circuit);
  91. await testInsert(tree4,keys[1],values[1], circuit);
  92. await testInsert(tree4,keys[2],values[2], circuit);
  93. await testInsert(tree4,keys[0],values[0], circuit);
  94. await testInsert(tree5,keys[2],values[2], circuit);
  95. await testInsert(tree5,keys[0],values[0], circuit);
  96. await testInsert(tree5,keys[1],values[1], circuit);
  97. await testInsert(tree6,keys[2],values[2], circuit);
  98. await testInsert(tree6,keys[1],values[1], circuit);
  99. await testInsert(tree6,keys[0],values[0], circuit);
  100. await testDelete(tree1, keys[0], circuit);
  101. await testDelete(tree1, keys[1], circuit);
  102. await testDelete(tree2, keys[1], circuit);
  103. await testDelete(tree2, keys[0], circuit);
  104. await testDelete(tree3, keys[0], circuit);
  105. await testDelete(tree3, keys[2], circuit);
  106. await testDelete(tree4, keys[2], circuit);
  107. await testDelete(tree4, keys[0], circuit);
  108. await testDelete(tree5, keys[1], circuit);
  109. await testDelete(tree5, keys[2], circuit);
  110. await testDelete(tree6, keys[2], circuit);
  111. await testDelete(tree6, keys[1], circuit);
  112. await testDelete(tree1, keys[2], circuit);
  113. await testDelete(tree2, keys[2], circuit);
  114. await testDelete(tree3, keys[1], circuit);
  115. await testDelete(tree4, keys[1], circuit);
  116. await testDelete(tree5, keys[0], circuit);
  117. await testDelete(tree6, keys[0], circuit); */
  118. });
  119. it("Should match a NOp with random vals", async () => {
  120. let siblings = [];
  121. while (siblings.length<10) siblings.push(bigInt(88));
  122. const w = circuit.calculateWitness({
  123. fnc: [0,0],
  124. oldRoot: 11,
  125. newRoot: 22,
  126. siblings: siblings,
  127. oldKey: 33,
  128. oldValue: 44,
  129. isOld0: 55,
  130. newKey: 66,
  131. newValue: 77
  132. });
  133. assert(circuit.checkWitness(w));
  134. });
  135. it("Should update an element", async () => {
  136. });
  137. it("Should verify existance of an element", async () => {
  138. });
  139. it("Should verify non existance of an element", async () => {
  140. });
  141. });