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.

163 lines
5.4 KiB

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