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.

173 lines
5.8 KiB

6 years ago
6 years ago
4 years ago
  1. const chai = require("chai");
  2. const Fr = require("ffjavascript").bn128.Fr;
  3. const smt = require("../src/smt.js");
  4. const assert = chai.assert;
  5. describe("SMT Javascript test", function () {
  6. this.timeout(100000);
  7. before( async () => {
  8. });
  9. it("Should insert 2 elements and empty them", async () => {
  10. const tree = await smt.newMemEmptyTrie();
  11. const key1 = Fr.e(111);
  12. const value1 = Fr.e(222);
  13. const key2 = Fr.e(333);
  14. const value2 = Fr.e(444);
  15. await tree.insert(key1,value1);
  16. await tree.insert(key2,value2);
  17. await tree.delete(key2);
  18. await tree.delete(key1);
  19. assert(Fr.isZero(tree.root));
  20. });
  21. it("Should insert 3 elements in dferent order and should be the same", async () => {
  22. const keys = [Fr.e(8), Fr.e(9), Fr.e(32)];
  23. const values = [Fr.e(88), Fr.e(99), Fr.e(3232)];
  24. const tree1 = await smt.newMemEmptyTrie();
  25. const tree2 = await smt.newMemEmptyTrie();
  26. const tree3 = await smt.newMemEmptyTrie();
  27. const tree4 = await smt.newMemEmptyTrie();
  28. const tree5 = await smt.newMemEmptyTrie();
  29. const tree6 = await smt.newMemEmptyTrie();
  30. await tree1.insert(keys[0],values[0]);
  31. await tree1.insert(keys[1],values[1]);
  32. await tree1.insert(keys[2],values[2]);
  33. await tree2.insert(keys[0],values[0]);
  34. await tree2.insert(keys[2],values[2]);
  35. await tree2.insert(keys[1],values[1]);
  36. await tree3.insert(keys[1],values[1]);
  37. await tree3.insert(keys[0],values[0]);
  38. await tree3.insert(keys[2],values[2]);
  39. await tree4.insert(keys[1],values[1]);
  40. await tree4.insert(keys[2],values[2]);
  41. await tree4.insert(keys[0],values[0]);
  42. await tree5.insert(keys[2],values[2]);
  43. await tree5.insert(keys[0],values[0]);
  44. await tree5.insert(keys[1],values[1]);
  45. await tree6.insert(keys[2],values[2]);
  46. await tree6.insert(keys[1],values[1]);
  47. await tree6.insert(keys[0],values[0]);
  48. assert(Fr.eq(tree1.root, tree2.root));
  49. assert(Fr.eq(tree2.root, tree3.root));
  50. assert(Fr.eq(tree3.root, tree4.root));
  51. assert(Fr.eq(tree4.root, tree5.root));
  52. assert(Fr.eq(tree5.root, tree6.root));
  53. assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length);
  54. assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length);
  55. assert.equal(Object.keys(tree3.db.nodes).length, Object.keys(tree4.db.nodes).length);
  56. assert.equal(Object.keys(tree4.db.nodes).length, Object.keys(tree5.db.nodes).length);
  57. assert.equal(Object.keys(tree5.db.nodes).length, Object.keys(tree6.db.nodes).length);
  58. await tree1.delete(keys[0]);
  59. await tree1.delete(keys[1]);
  60. await tree2.delete(keys[1]);
  61. await tree2.delete(keys[0]);
  62. assert(Fr.eq(tree1.root, tree2.root));
  63. await tree3.delete(keys[0]);
  64. await tree3.delete(keys[2]);
  65. await tree4.delete(keys[2]);
  66. await tree4.delete(keys[0]);
  67. assert(Fr.eq(tree3.root, tree4.root));
  68. await tree5.delete(keys[1]);
  69. await tree5.delete(keys[2]);
  70. await tree6.delete(keys[2]);
  71. await tree6.delete(keys[1]);
  72. assert(Fr.eq(tree5.root, tree6.root));
  73. await tree1.delete(keys[2]);
  74. await tree2.delete(keys[2]);
  75. await tree3.delete(keys[1]);
  76. await tree4.delete(keys[1]);
  77. await tree5.delete(keys[0]);
  78. await tree6.delete(keys[0]);
  79. assert(Fr.isZero(tree1.root));
  80. assert(Fr.isZero(tree2.root));
  81. assert(Fr.isZero(tree3.root));
  82. assert(Fr.isZero(tree4.root));
  83. assert(Fr.isZero(tree5.root));
  84. assert(Fr.isZero(tree6.root));
  85. assert.equal(Object.keys(tree1.db.nodes).length, 0);
  86. assert.equal(Object.keys(tree2.db.nodes).length, 0);
  87. assert.equal(Object.keys(tree3.db.nodes).length, 0);
  88. assert.equal(Object.keys(tree4.db.nodes).length, 0);
  89. assert.equal(Object.keys(tree5.db.nodes).length, 0);
  90. assert.equal(Object.keys(tree6.db.nodes).length, 0);
  91. });
  92. it("Insert and remove 100 numbers randomly", async () => {
  93. function perm(a) {
  94. const arr = a.slice();
  95. const rArr = [];
  96. for (let i=0; i<arr.length; i++) {
  97. let rIdx = Math.floor(Math.random() * (arr.length - i));
  98. rArr.push(arr[rIdx]);
  99. arr[rIdx] = arr[arr.length - i - 1];
  100. }
  101. return rArr;
  102. }
  103. const tree = await smt.newMemEmptyTrie();
  104. const arr = [];
  105. const N = 100;
  106. for (let i=0; i<N; i++) {
  107. arr.push(Fr.e(i));
  108. }
  109. const insArr = perm(arr);
  110. for (let i=0; i<N; i++) {
  111. await tree.insert(insArr[i], i);
  112. }
  113. const delArr = perm(insArr);
  114. for (let i=0; i<N; i++) {
  115. await tree.delete(delArr[i]);
  116. }
  117. assert(Fr.isZero(tree.root));
  118. assert.equal(Object.keys(tree.db.nodes).length, 0);
  119. });
  120. it("Should test update", async () => {
  121. const tree1 = await smt.newMemEmptyTrie();
  122. const tree2 = await smt.newMemEmptyTrie();
  123. await tree1.insert(8,88);
  124. await tree1.insert(9,99,);
  125. await tree1.insert(32,3232);
  126. await tree2.insert(8,888);
  127. await tree2.insert(9,999);
  128. await tree2.insert(32,323232);
  129. await tree1.update(8, 888);
  130. await tree1.update(9, 999);
  131. await tree1.update(32, 323232);
  132. assert(Fr.eq(tree1.root, tree2.root));
  133. });
  134. it("Should test update with same key-value", async () => {
  135. const tree1 = await smt.newMemEmptyTrie();
  136. await tree1.insert(8,88);
  137. await tree1.update(8,88);
  138. const res = await tree1.db.get(tree1.root);
  139. assert.notEqual(res, undefined);
  140. });
  141. });