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.

208 lines
6.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const tester = require("circom").tester;
  4. const Fr = require("ffjavascript").bn128.Fr;
  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 testInsert(tree, key, value, circuit ) {
  11. const res = await tree.insert(key,value);
  12. let siblings = res.siblings;
  13. while (siblings.length<10) siblings.push(Fr.e(0));
  14. const w = await circuit.calculateWitness({
  15. fnc: [1,0],
  16. oldRoot: res.oldRoot,
  17. siblings: siblings,
  18. oldKey: res.isOld0 ? 0 : res.oldKey,
  19. oldValue: res.isOld0 ? 0 : res.oldValue,
  20. isOld0: res.isOld0 ? 1 : 0,
  21. newKey: key,
  22. newValue: value
  23. }, true);
  24. await circuit.checkConstraints(w);
  25. await circuit.assertOut(w, {newRoot: res.newRoot});
  26. }
  27. async function testDelete(tree, key, circuit) {
  28. const res = await tree.delete(key);
  29. let siblings = res.siblings;
  30. while (siblings.length<10) siblings.push(Fr.e(0));
  31. const w = await circuit.calculateWitness({
  32. fnc: [1,1],
  33. oldRoot: res.oldRoot,
  34. siblings: siblings,
  35. oldKey: res.isOld0 ? 0 : res.oldKey,
  36. oldValue: res.isOld0 ? 0 : res.oldValue,
  37. isOld0: res.isOld0 ? 1 : 0,
  38. newKey: res.delKey,
  39. newValue: res.delValue
  40. }, true);
  41. await circuit.checkConstraints(w);
  42. await circuit.assertOut(w, {newRoot: res.newRoot});
  43. }
  44. async function testUpdate(tree, key, newValue, circuit) {
  45. const res = await tree.update(key, newValue);
  46. let siblings = res.siblings;
  47. while (siblings.length<10) siblings.push(Fr.e(0));
  48. const w = await circuit.calculateWitness({
  49. fnc: [0,1],
  50. oldRoot: res.oldRoot,
  51. siblings: siblings,
  52. oldKey: res.oldKey,
  53. oldValue: res.oldValue,
  54. isOld0: 0,
  55. newKey: res.newKey,
  56. newValue: res.newValue
  57. });
  58. await circuit.checkConstraints(w);
  59. await circuit.assertOut(w, {newRoot: res.newRoot});
  60. }
  61. describe("SMT Processor test", function () {
  62. let circuit;
  63. let tree;
  64. this.timeout(10000000);
  65. before( async () => {
  66. circuit = await tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
  67. await circuit.loadSymbols();
  68. tree = await smt.newMemEmptyTrie();
  69. });
  70. it("Should verify an insert to an empty tree", async () => {
  71. const key = Fr.e(111);
  72. const value = Fr.e(222);
  73. await testInsert(tree, key, value, circuit);
  74. });
  75. it("It should add another element", async () => {
  76. const key = Fr.e(333);
  77. const value = Fr.e(444);
  78. await testInsert(tree, key, value, circuit);
  79. });
  80. it("Should remove an element", async () => {
  81. await testDelete(tree, 111, circuit);
  82. await testDelete(tree, 333, circuit);
  83. });
  84. it("Should test convination of adding and removing 3 elements", async () => {
  85. const keys = [Fr.e(8), Fr.e(9), Fr.e(32)];
  86. const values = [Fr.e(88), Fr.e(99), Fr.e(3232)];
  87. const tree1 = await smt.newMemEmptyTrie();
  88. const tree2 = await smt.newMemEmptyTrie();
  89. const tree3 = await smt.newMemEmptyTrie();
  90. const tree4 = await smt.newMemEmptyTrie();
  91. const tree5 = await smt.newMemEmptyTrie();
  92. const tree6 = await smt.newMemEmptyTrie();
  93. await testInsert(tree1,keys[0],values[0], circuit);
  94. await testInsert(tree1,keys[1],values[1], circuit);
  95. await testInsert(tree1,keys[2],values[2], circuit);
  96. await testInsert(tree2,keys[0],values[0], circuit);
  97. await testInsert(tree2,keys[2],values[2], circuit);
  98. await testInsert(tree2,keys[1],values[1], circuit);
  99. await testInsert(tree3,keys[1],values[1], circuit);
  100. await testInsert(tree3,keys[0],values[0], circuit);
  101. await testInsert(tree3,keys[2],values[2], circuit);
  102. await testInsert(tree4,keys[1],values[1], circuit);
  103. await testInsert(tree4,keys[2],values[2], circuit);
  104. await testInsert(tree4,keys[0],values[0], circuit);
  105. await testInsert(tree5,keys[2],values[2], circuit);
  106. await testInsert(tree5,keys[0],values[0], circuit);
  107. await testInsert(tree5,keys[1],values[1], circuit);
  108. await testInsert(tree6,keys[2],values[2], circuit);
  109. await testInsert(tree6,keys[1],values[1], circuit);
  110. await testInsert(tree6,keys[0],values[0], circuit);
  111. await testDelete(tree1, keys[0], circuit);
  112. await testDelete(tree1, keys[1], circuit);
  113. await testDelete(tree2, keys[1], circuit);
  114. await testDelete(tree2, keys[0], circuit);
  115. await testDelete(tree3, keys[0], circuit);
  116. await testDelete(tree3, keys[2], circuit);
  117. await testDelete(tree4, keys[2], circuit);
  118. await testDelete(tree4, keys[0], circuit);
  119. await testDelete(tree5, keys[1], circuit);
  120. await testDelete(tree5, keys[2], circuit);
  121. await testDelete(tree6, keys[2], circuit);
  122. await testDelete(tree6, keys[1], circuit);
  123. await testDelete(tree1, keys[2], circuit);
  124. await testDelete(tree2, keys[2], circuit);
  125. await testDelete(tree3, keys[1], circuit);
  126. await testDelete(tree4, keys[1], circuit);
  127. await testDelete(tree5, keys[0], circuit);
  128. await testDelete(tree6, keys[0], circuit);
  129. });
  130. it("Should match a NOp with random vals", async () => {
  131. let siblings = [];
  132. while (siblings.length<10) siblings.push(Fr.e(88));
  133. const w = await circuit.calculateWitness({
  134. fnc: [0,0],
  135. oldRoot: 11,
  136. siblings: siblings,
  137. oldKey: 33,
  138. oldValue: 44,
  139. isOld0: 55,
  140. newKey: 66,
  141. newValue: 77
  142. });
  143. const root1 = w[circuit.symbols["main.oldRoot"].varIdx];
  144. const root2 = w[circuit.symbols["main.newRoot"].varIdx];
  145. await circuit.checkConstraints(w);
  146. assert(Fr.eq(root1, root2));
  147. });
  148. it("Should update an element", async () => {
  149. const tree1 = await smt.newMemEmptyTrie();
  150. const tree2 = await smt.newMemEmptyTrie();
  151. await testInsert(tree1,8,88, circuit);
  152. await testInsert(tree1,9,99, circuit);
  153. await testInsert(tree1,32,3232, circuit);
  154. await testInsert(tree2,8,888, circuit);
  155. await testInsert(tree2,9,999, circuit);
  156. await testInsert(tree2,32,323232, circuit);
  157. await testUpdate(tree1, 8, 888, circuit);
  158. await testUpdate(tree1, 9, 999, circuit);
  159. await testUpdate(tree1, 32, 323232, circuit);
  160. });
  161. });