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.

153 lines
5.9 KiB

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