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.

231 lines
7.4 KiB

5 years ago
  1. const chai = require("chai");
  2. const snarkjs = require("snarkjs");
  3. const smt = require("../src/smt.js");
  4. const mimcjs = require("../src/mimc7.js");
  5. const assert = chai.assert;
  6. const expect = chai.expect;
  7. const bigInt = snarkjs.bigInt;
  8. function stringifyBigInts(o) {
  9. if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
  10. return o.toString(10);
  11. } else if (Array.isArray(o)) {
  12. return o.map(stringifyBigInts);
  13. } else if (typeof o == "object") {
  14. const res = {};
  15. for (let k in o) {
  16. res[k] = stringifyBigInts(o[k]);
  17. }
  18. return res;
  19. } else {
  20. return o;
  21. }
  22. }
  23. function newEntryFromInts(a, b, c, d) {
  24. return {
  25. hi: mimcjs.hash(c, d),
  26. hv: mimcjs.hash(a, b),
  27. };
  28. }
  29. function smtHash(arr) {
  30. let r = bigInt(0);
  31. for (let i=0; i<arr.length; i++) {
  32. r = mimcjs.hash(r, bigInt(arr[i]), 91 );
  33. }
  34. return r;
  35. }
  36. describe("SMT Javascript test", function () {
  37. this.timeout(100000);
  38. before( async () => {
  39. });
  40. it("test MIMC7", async() => {
  41. //const h = mimcjs.multiHash(1,2,3);
  42. //const h = smtHash([1,2,3]);
  43. //console.log(h.toString(10));
  44. const mimcjs = require("../src/mimc7.js");
  45. const snarkjs = require("snarkjs");
  46. const bigInt = snarkjs.bigInt;
  47. const smt = require("../src/smt.js");
  48. h = mimcjs.hash(1,2,91);
  49. console.log(h);
  50. function smtHash(arr) {
  51. let r = bigInt(0);
  52. for (let i=0; i<arr.length; i++) {
  53. r = mimcjs.hash(r, bigInt(arr[i]), 91 );
  54. }
  55. return r;
  56. }
  57. console.log(smtHash([1, 2, 3]));
  58. // Test from old ver
  59. });
  60. //it("Should calculate hindex", async() => {
  61. // const entry = newEntryFromInts(12, 45, 78, 41);
  62. // //console.log(entry.hi.toString(16));
  63. // expect(entry.hi.toString(16)).to.be.
  64. // equal("114438e8321f62c4a1708f443a5a66f9c8fcb0958e7b7008332b71442610b7a0");
  65. //});
  66. //it("Should insert 2 elements and empty them", async () => {
  67. // const tree = await smt.newMemEmptyTrie();
  68. // const key1 = bigInt(111);
  69. // const value1 = bigInt(222);
  70. // const key2 = bigInt(333);
  71. // const value2 = bigInt(444);
  72. // await tree.insert(key1,value1);
  73. // await tree.insert(key2,value2);
  74. // await tree.delete(key2);
  75. // await tree.delete(key1);
  76. // assert(tree.root.isZero());
  77. //});
  78. //it("Should insert 3 elements in dferent order and should be the same", async () => {
  79. // const keys = [bigInt(8), bigInt(9), bigInt(32)];
  80. // const values = [bigInt(88), bigInt(99), bigInt(3232)];
  81. // const tree1 = await smt.newMemEmptyTrie();
  82. // const tree2 = await smt.newMemEmptyTrie();
  83. // const tree3 = await smt.newMemEmptyTrie();
  84. // const tree4 = await smt.newMemEmptyTrie();
  85. // const tree5 = await smt.newMemEmptyTrie();
  86. // const tree6 = await smt.newMemEmptyTrie();
  87. // await tree1.insert(keys[0],values[0]);
  88. // await tree1.insert(keys[1],values[1]);
  89. // await tree1.insert(keys[2],values[2]);
  90. // await tree2.insert(keys[0],values[0]);
  91. // await tree2.insert(keys[2],values[2]);
  92. // await tree2.insert(keys[1],values[1]);
  93. // await tree3.insert(keys[1],values[1]);
  94. // await tree3.insert(keys[0],values[0]);
  95. // await tree3.insert(keys[2],values[2]);
  96. // await tree4.insert(keys[1],values[1]);
  97. // await tree4.insert(keys[2],values[2]);
  98. // await tree4.insert(keys[0],values[0]);
  99. // await tree5.insert(keys[2],values[2]);
  100. // await tree5.insert(keys[0],values[0]);
  101. // await tree5.insert(keys[1],values[1]);
  102. // await tree6.insert(keys[2],values[2]);
  103. // await tree6.insert(keys[1],values[1]);
  104. // await tree6.insert(keys[0],values[0]);
  105. // assert(tree1.root.equals(tree2.root));
  106. // assert(tree2.root.equals(tree3.root));
  107. // assert(tree3.root.equals(tree4.root));
  108. // assert(tree4.root.equals(tree5.root));
  109. // assert(tree5.root.equals(tree6.root));
  110. // assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length);
  111. // assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length);
  112. // assert.equal(Object.keys(tree3.db.nodes).length, Object.keys(tree4.db.nodes).length);
  113. // assert.equal(Object.keys(tree4.db.nodes).length, Object.keys(tree5.db.nodes).length);
  114. // assert.equal(Object.keys(tree5.db.nodes).length, Object.keys(tree6.db.nodes).length);
  115. // await tree1.delete(keys[0]);
  116. // await tree1.delete(keys[1]);
  117. // await tree2.delete(keys[1]);
  118. // await tree2.delete(keys[0]);
  119. // assert(tree1.root.equals(tree2.root));
  120. // await tree3.delete(keys[0]);
  121. // await tree3.delete(keys[2]);
  122. // await tree4.delete(keys[2]);
  123. // await tree4.delete(keys[0]);
  124. // assert(tree3.root.equals(tree4.root));
  125. // await tree5.delete(keys[1]);
  126. // await tree5.delete(keys[2]);
  127. // await tree6.delete(keys[2]);
  128. // await tree6.delete(keys[1]);
  129. // assert(tree5.root.equals(tree6.root));
  130. // await tree1.delete(keys[2]);
  131. // await tree2.delete(keys[2]);
  132. // await tree3.delete(keys[1]);
  133. // await tree4.delete(keys[1]);
  134. // await tree5.delete(keys[0]);
  135. // await tree6.delete(keys[0]);
  136. // assert(tree1.root.isZero());
  137. // assert(tree2.root.isZero());
  138. // assert(tree3.root.isZero());
  139. // assert(tree4.root.isZero());
  140. // assert(tree5.root.isZero());
  141. // assert(tree6.root.isZero());
  142. // assert.equal(Object.keys(tree1.db.nodes).length, 0);
  143. // assert.equal(Object.keys(tree2.db.nodes).length, 0);
  144. // assert.equal(Object.keys(tree3.db.nodes).length, 0);
  145. // assert.equal(Object.keys(tree4.db.nodes).length, 0);
  146. // assert.equal(Object.keys(tree5.db.nodes).length, 0);
  147. // assert.equal(Object.keys(tree6.db.nodes).length, 0);
  148. //});
  149. //it("Insert and remove 100 numbers randomly", async () => {
  150. // function perm(a) {
  151. // const arr = a.slice();
  152. // const rArr = [];
  153. // for (let i=0; i<arr.length; i++) {
  154. // let rIdx = Math.floor(Math.random() * (arr.length - i));
  155. // rArr.push(arr[rIdx]);
  156. // arr[rIdx] = arr[arr.length - i - 1];
  157. // }
  158. // return rArr;
  159. // }
  160. // const tree = await smt.newMemEmptyTrie();
  161. // const arr = [];
  162. // const N = 100;
  163. // for (let i=0; i<N; i++) {
  164. // arr.push(bigInt(i));
  165. // }
  166. // const insArr = perm(arr);
  167. // for (let i=0; i<N; i++) {
  168. // await tree.insert(insArr[i], i);
  169. // }
  170. // const delArr = perm(insArr);
  171. // for (let i=0; i<N; i++) {
  172. // await tree.delete(delArr[i]);
  173. // }
  174. // assert(tree.root.isZero());
  175. // assert.equal(Object.keys(tree.db.nodes).length, 0);
  176. //});
  177. //it("Should test update", async () => {
  178. // const tree1 = await smt.newMemEmptyTrie();
  179. // const tree2 = await smt.newMemEmptyTrie();
  180. // await tree1.insert(8,88);
  181. // await tree1.insert(9,99,);
  182. // await tree1.insert(32,3232);
  183. // await tree2.insert(8,888);
  184. // await tree2.insert(9,999);
  185. // await tree2.insert(32,323232);
  186. // await tree1.update(8, 888);
  187. // await tree1.update(9, 999);
  188. // await tree1.update(32, 323232);
  189. // assert(tree1.root.equals(tree2.root));
  190. //});
  191. });