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.

142 lines
5.4 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("0x112bae1c89a7a51a9a09e88c2f095bfe8a7d94d7c0cf5ba017a491c3e0b95c8f");
  40. });
  41. it("Add two claims", async () => {
  42. const firstClaim = [bigInt(12), bigInt(45), bigInt(78), bigInt(41)];
  43. const firstEntries = newEntry(firstClaim);
  44. const secondClaim = [bigInt(33), bigInt(44), bigInt(55), bigInt(66)];
  45. const secondEntries = newEntry(secondClaim);
  46. const tree = await smt.newMemEmptyTrie();
  47. const key1 = firstEntries.hi;
  48. const value1 = firstEntries.hv;
  49. const key2 = secondEntries.hi;
  50. const value2 = secondEntries.hv;
  51. await tree.insert(key1,value1);
  52. await tree.insert(key2,value2);
  53. const root = tree.root;
  54. const rootBuff = bigIntToBuffer(root);
  55. expect(bytesToHex(rootBuff)).to.be.equal("0x1fb755a3677f8fd6c47b5462b69778ef6383c31d2d498b765e953f8cacaa6744");
  56. });
  57. it("Add claims in different orders", async () => {
  58. const tree1 = await smt.newMemEmptyTrie();
  59. for (let i = 0; i < 16; i++) {
  60. const claim = [bigInt(0), bigInt(i), bigInt(0), bigInt(i)];
  61. const entries = newEntry(claim);
  62. await tree1.insert(entries.hi, entries.hv);
  63. }
  64. const tree2 = await smt.newMemEmptyTrie();
  65. for (let i = 0; i < 16; i++) {
  66. const claim = [bigInt(0), bigInt(i), bigInt(0), bigInt(i)];
  67. const entries = newEntry(claim);
  68. await tree2.insert(entries.hi, entries.hv);
  69. }
  70. const root1 = bigIntToBuffer(tree1.root);
  71. const root2 = bigIntToBuffer(tree2.root);
  72. expect(bytesToHex(root1)).to.be.equal(bytesToHex(root2));
  73. expect(bytesToHex(root1)).to.be.equal("0x173fd27f6622526dfb21c4d8d83e3c95adba5d8f46a397113e4e80e629c6de76");
  74. });
  75. it("Insert 3 elements in different order into different trees", async () => {
  76. const claim1 = [bigInt(33), bigInt(44), bigInt(55), bigInt(66)];
  77. const entries1 = newEntry(claim1);
  78. const claim2 = [bigInt(1111), bigInt(2222), bigInt(3333), bigInt(4444)];
  79. const entries2 = newEntry(claim2);
  80. const claim3 = [bigInt(5555), bigInt(6666), bigInt(7777), bigInt(8888)];
  81. const entries3 = newEntry(claim3);
  82. const tree1 = await smt.newMemEmptyTrie();
  83. const tree2 = await smt.newMemEmptyTrie();
  84. const tree3 = await smt.newMemEmptyTrie();
  85. const tree4 = await smt.newMemEmptyTrie();
  86. const tree5 = await smt.newMemEmptyTrie();
  87. const tree6 = await smt.newMemEmptyTrie();
  88. await tree1.insert(entries1.hi,entries1.hv);
  89. await tree1.insert(entries2.hi,entries2.hv);
  90. await tree1.insert(entries3.hi,entries3.hv);
  91. await tree2.insert(entries1.hi,entries1.hv);
  92. await tree2.insert(entries3.hi,entries3.hv);
  93. await tree2.insert(entries2.hi,entries2.hv);
  94. await tree3.insert(entries2.hi,entries2.hv);
  95. await tree3.insert(entries1.hi,entries1.hv);
  96. await tree3.insert(entries3.hi,entries3.hv);
  97. await tree4.insert(entries2.hi,entries2.hv);
  98. await tree4.insert(entries3.hi,entries3.hv);
  99. await tree4.insert(entries1.hi,entries1.hv);
  100. await tree5.insert(entries3.hi,entries3.hv);
  101. await tree5.insert(entries1.hi,entries1.hv);
  102. await tree5.insert(entries2.hi,entries2.hv);
  103. await tree6.insert(entries3.hi,entries3.hv);
  104. await tree6.insert(entries2.hi,entries2.hv);
  105. await tree6.insert(entries1.hi,entries1.hv);
  106. const root1 = bigIntToBuffer(tree1.root);
  107. const root2 = bigIntToBuffer(tree2.root);
  108. const root3 = bigIntToBuffer(tree3.root);
  109. const root4 = bigIntToBuffer(tree4.root);
  110. const root5 = bigIntToBuffer(tree5.root);
  111. const root6 = bigIntToBuffer(tree6.root);
  112. expect(bytesToHex(root1)).to.be.equal(bytesToHex(root2));
  113. expect(bytesToHex(root2)).to.be.equal(bytesToHex(root3));
  114. expect(bytesToHex(root3)).to.be.equal(bytesToHex(root4));
  115. expect(bytesToHex(root4)).to.be.equal(bytesToHex(root5));
  116. expect(bytesToHex(root5)).to.be.equal(bytesToHex(root6));
  117. expect(bytesToHex(root1)).to.be.equal("0x27990ef22656f49f010b2b48b2418c46f2bc93e4afb2e3377a1eb09f129e9802");
  118. });
  119. });