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.

217 lines
6.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
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 testInsert(tree, key, value, circuit, log ) {
  12. const res = await tree.insert(key,value);
  13. let siblings = res.siblings;
  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.isOld0 ? 0 : res.oldKey,
  21. oldValue: res.isOld0 ? 0 : 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.siblings;
  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.isOld0 ? 0 : res.oldKey,
  40. oldValue: res.isOld0 ? 0 : 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. async function testUpdate(tree, key, newValue, circuit) {
  50. const res = await tree.update(key, newValue);
  51. let siblings = res.siblings;
  52. while (siblings.length<10) siblings.push(bigInt(0));
  53. const w = circuit.calculateWitness({
  54. fnc: [0,1],
  55. oldRoot: res.oldRoot,
  56. newRoot: res.newRoot,
  57. siblings: siblings,
  58. oldKey: res.oldKey,
  59. oldValue: res.oldValue,
  60. isOld0: 0,
  61. newKey: res.newKey,
  62. newValue: res.newValue
  63. });
  64. const root1 = w[circuit.getSignalIdx("main.topSwitcher.outR")];
  65. assert(circuit.checkWitness(w));
  66. assert(root1.equals(res.newRoot));
  67. }
  68. describe("SMT test", function () {
  69. let circuit;
  70. let tree;
  71. this.timeout(100000);
  72. before( async () => {
  73. const cirDef = await compiler(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
  74. circuit = new snarkjs.Circuit(cirDef);
  75. console.log("NConstrains SMTProcessor: " + circuit.nConstraints);
  76. tree = await smt.newMemEmptyTrie();
  77. });
  78. it("Should verify an insert to an empty tree", async () => {
  79. const key = bigInt(111);
  80. const value = bigInt(222);
  81. await testInsert(tree, key, value, circuit);
  82. });
  83. it("It should add another element", async () => {
  84. const key = bigInt(333);
  85. const value = bigInt(444);
  86. await testInsert(tree, key, value, circuit);
  87. });
  88. it("Should remove an element", async () => {
  89. await testDelete(tree, 111, circuit);
  90. await testDelete(tree, 333, circuit);
  91. });
  92. it("Should test convination of adding and removing 3 elements", async () => {
  93. const keys = [bigInt(8), bigInt(9), bigInt(32)];
  94. const values = [bigInt(88), bigInt(99), bigInt(3232)];
  95. const tree1 = await smt.newMemEmptyTrie();
  96. const tree2 = await smt.newMemEmptyTrie();
  97. const tree3 = await smt.newMemEmptyTrie();
  98. const tree4 = await smt.newMemEmptyTrie();
  99. const tree5 = await smt.newMemEmptyTrie();
  100. const tree6 = await smt.newMemEmptyTrie();
  101. await testInsert(tree1,keys[0],values[0], circuit);
  102. await testInsert(tree1,keys[1],values[1], circuit);
  103. await testInsert(tree1,keys[2],values[2], circuit);
  104. await testInsert(tree2,keys[0],values[0], circuit);
  105. await testInsert(tree2,keys[2],values[2], circuit);
  106. await testInsert(tree2,keys[1],values[1], circuit);
  107. await testInsert(tree3,keys[1],values[1], circuit);
  108. await testInsert(tree3,keys[0],values[0], circuit);
  109. await testInsert(tree3,keys[2],values[2], circuit);
  110. await testInsert(tree4,keys[1],values[1], circuit);
  111. await testInsert(tree4,keys[2],values[2], circuit);
  112. await testInsert(tree4,keys[0],values[0], circuit);
  113. await testInsert(tree5,keys[2],values[2], circuit);
  114. await testInsert(tree5,keys[0],values[0], circuit);
  115. await testInsert(tree5,keys[1],values[1], circuit);
  116. await testInsert(tree6,keys[2],values[2], circuit);
  117. await testInsert(tree6,keys[1],values[1], circuit);
  118. await testInsert(tree6,keys[0],values[0], circuit);
  119. await testDelete(tree1, keys[0], circuit);
  120. await testDelete(tree1, keys[1], circuit);
  121. await testDelete(tree2, keys[1], circuit);
  122. await testDelete(tree2, keys[0], circuit);
  123. await testDelete(tree3, keys[0], circuit);
  124. await testDelete(tree3, keys[2], circuit);
  125. await testDelete(tree4, keys[2], circuit);
  126. await testDelete(tree4, keys[0], circuit);
  127. await testDelete(tree5, keys[1], circuit);
  128. await testDelete(tree5, keys[2], circuit);
  129. await testDelete(tree6, keys[2], circuit);
  130. await testDelete(tree6, keys[1], circuit);
  131. await testDelete(tree1, keys[2], circuit);
  132. await testDelete(tree2, keys[2], circuit);
  133. await testDelete(tree3, keys[1], circuit);
  134. await testDelete(tree4, keys[1], circuit);
  135. await testDelete(tree5, keys[0], circuit);
  136. await testDelete(tree6, keys[0], circuit);
  137. });
  138. it("Should match a NOp with random vals", async () => {
  139. let siblings = [];
  140. while (siblings.length<10) siblings.push(bigInt(88));
  141. const w = circuit.calculateWitness({
  142. fnc: [0,0],
  143. oldRoot: 11,
  144. newRoot: 22,
  145. siblings: siblings,
  146. oldKey: 33,
  147. oldValue: 44,
  148. isOld0: 55,
  149. newKey: 66,
  150. newValue: 77
  151. });
  152. assert(circuit.checkWitness(w));
  153. });
  154. it("Should update an element", async () => {
  155. const tree1 = await smt.newMemEmptyTrie();
  156. const tree2 = await smt.newMemEmptyTrie();
  157. await testInsert(tree1,8,88, circuit);
  158. await testInsert(tree1,9,99, circuit);
  159. await testInsert(tree1,32,3232, circuit);
  160. await testInsert(tree2,8,888, circuit);
  161. await testInsert(tree2,9,999, circuit);
  162. await testInsert(tree2,32,323232, circuit);
  163. await testUpdate(tree1, 8, 888, circuit);
  164. await testUpdate(tree1, 9, 999, circuit);
  165. await testUpdate(tree1, 32, 323232, circuit);
  166. });
  167. });