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.

146 lines
5.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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 expect = chai.expect;
  6. const bigInt = snarkjs.bigInt;
  7. function bytesToHex(buff) {
  8. return `0x${buff.toString("hex")}`;
  9. }
  10. function bigIntToBuffer(number) {
  11. const buff = Buffer.alloc(32);
  12. let pos = buff.length - 1;
  13. while (!number.isZero()) {
  14. buff[pos] = number.and(255);
  15. number = number.shiftRight(8);
  16. pos -= 1;
  17. }
  18. return buff;
  19. }
  20. function newEntry(arr) {
  21. return {
  22. hi: mimcjs.multiHash(arr.slice(2)),
  23. hv: mimcjs.multiHash(arr.slice(0, 2)),
  24. };
  25. }
  26. describe("[sparse-merkle-tree] Javascript test", function () {
  27. this.timeout(100000);
  28. before( async () => {
  29. });
  30. it("Add one claim", async () => {
  31. const claim = [bigInt(12), bigInt(45), bigInt(78), bigInt(41)];
  32. const entries = newEntry(claim);
  33. const tree = await smt.newMemEmptyTrie();
  34. const key1 = entries.hi;
  35. const value1 = entries.hv;
  36. await tree.insert(key1,value1);
  37. const root = tree.root;
  38. const rootBuff = bigIntToBuffer(root);
  39. expect(bytesToHex(rootBuff)).to.be.equal("0x2bf39430aa2482fc1e2f170179c8cab126b0f55f71edc8d333f4c80cb4e798f5");
  40. });
  41. /*
  42. it("Add two claims", async () => {
  43. const firstClaim = [bigInt(12), bigInt(45), bigInt(78), bigInt(41)];
  44. const firstEntries = newEntry(firstClaim);
  45. const secondClaim = [bigInt(33), bigInt(44), bigInt(55), bigInt(66)];
  46. const secondEntries = newEntry(secondClaim);
  47. const tree = await smt.newMemEmptyTrie();
  48. const key1 = firstEntries.hi;
  49. const value1 = firstEntries.hv;
  50. const key2 = secondEntries.hi;
  51. const value2 = secondEntries.hv;
  52. await tree.insert(key1,value1);
  53. await tree.insert(key2,value2);
  54. const root = tree.root;
  55. const rootBuff = bigIntToBuffer(root);
  56. expect(bytesToHex(rootBuff)).to.be.equal("0x1fb755a3677f8fd6c47b5462b69778ef6383c31d2d498b765e953f8cacaa6744");
  57. });
  58. it("Add claims in different orders", async () => {
  59. const tree1 = await smt.newMemEmptyTrie();
  60. for (let i = 0; i < 16; i++) {
  61. const claim = [bigInt(0), bigInt(i), bigInt(0), bigInt(i)];
  62. const entries = newEntry(claim);
  63. await tree1.insert(entries.hi, entries.hv);
  64. }
  65. const tree2 = await smt.newMemEmptyTrie();
  66. for (let i = 0; i < 16; i++) {
  67. const claim = [bigInt(0), bigInt(i), bigInt(0), bigInt(i)];
  68. const entries = newEntry(claim);
  69. await tree2.insert(entries.hi, entries.hv);
  70. }
  71. const root1 = bigIntToBuffer(tree1.root);
  72. const root2 = bigIntToBuffer(tree2.root);
  73. expect(bytesToHex(root1)).to.be.equal(bytesToHex(root2));
  74. expect(bytesToHex(root1)).to.be.equal("0x173fd27f6622526dfb21c4d8d83e3c95adba5d8f46a397113e4e80e629c6de76");
  75. });
  76. it("Insert 3 elements in different order into different trees", async () => {
  77. const claim1 = [bigInt(33), bigInt(44), bigInt(55), bigInt(66)];
  78. const entries1 = newEntry(claim1);
  79. const claim2 = [bigInt(1111), bigInt(2222), bigInt(3333), bigInt(4444)];
  80. const entries2 = newEntry(claim2);
  81. const claim3 = [bigInt(5555), bigInt(6666), bigInt(7777), bigInt(8888)];
  82. const entries3 = newEntry(claim3);
  83. const tree1 = await smt.newMemEmptyTrie();
  84. const tree2 = await smt.newMemEmptyTrie();
  85. const tree3 = await smt.newMemEmptyTrie();
  86. const tree4 = await smt.newMemEmptyTrie();
  87. const tree5 = await smt.newMemEmptyTrie();
  88. const tree6 = await smt.newMemEmptyTrie();
  89. await tree1.insert(entries1.hi,entries1.hv);
  90. await tree1.insert(entries2.hi,entries2.hv);
  91. await tree1.insert(entries3.hi,entries3.hv);
  92. await tree2.insert(entries1.hi,entries1.hv);
  93. await tree2.insert(entries3.hi,entries3.hv);
  94. await tree2.insert(entries2.hi,entries2.hv);
  95. await tree3.insert(entries2.hi,entries2.hv);
  96. await tree3.insert(entries1.hi,entries1.hv);
  97. await tree3.insert(entries3.hi,entries3.hv);
  98. await tree4.insert(entries2.hi,entries2.hv);
  99. await tree4.insert(entries3.hi,entries3.hv);
  100. await tree4.insert(entries1.hi,entries1.hv);
  101. await tree5.insert(entries3.hi,entries3.hv);
  102. await tree5.insert(entries1.hi,entries1.hv);
  103. await tree5.insert(entries2.hi,entries2.hv);
  104. await tree6.insert(entries3.hi,entries3.hv);
  105. await tree6.insert(entries2.hi,entries2.hv);
  106. await tree6.insert(entries1.hi,entries1.hv);
  107. const root1 = bigIntToBuffer(tree1.root);
  108. const root2 = bigIntToBuffer(tree2.root);
  109. const root3 = bigIntToBuffer(tree3.root);
  110. const root4 = bigIntToBuffer(tree4.root);
  111. const root5 = bigIntToBuffer(tree5.root);
  112. const root6 = bigIntToBuffer(tree6.root);
  113. expect(bytesToHex(root1)).to.be.equal(bytesToHex(root2));
  114. expect(bytesToHex(root2)).to.be.equal(bytesToHex(root3));
  115. expect(bytesToHex(root3)).to.be.equal(bytesToHex(root4));
  116. expect(bytesToHex(root4)).to.be.equal(bytesToHex(root5));
  117. expect(bytesToHex(root5)).to.be.equal(bytesToHex(root6));
  118. expect(bytesToHex(root1)).to.be.equal("0x27990ef22656f49f010b2b48b2418c46f2bc93e4afb2e3377a1eb09f129e9802");
  119. });
  120. <<<<<<< HEAD
  121. */
  122. =======
  123. >>>>>>> 5b7abbc4ec56447e5e2baa31e3b4ce3a0c0617a8
  124. });