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.

147 lines
3.5 KiB

  1. const chai = require("chai");
  2. const Fr = require("ffjavascript").bn128.Fr;
  3. const smt = require("circomlib").smt;
  4. const assert = chai.assert;
  5. describe("SMT javascript testvectors", function () {
  6. this.timeout(100000);
  7. before( async () => {
  8. });
  9. it("Should insert 2 elements and empty them", async () => {
  10. const tree = await smt.newMemEmptyTrie();
  11. console.log(tree);
  12. let k = Fr.e(1);
  13. let v = Fr.e(2);
  14. await tree.insert(k,v);
  15. console.log(tree.root);
  16. k = Fr.e(33);
  17. v = Fr.e(44);
  18. await tree.insert(k,v);
  19. console.log(tree.root);
  20. k = Fr.e(1234);
  21. v = Fr.e(9876);
  22. await tree.insert(k,v);
  23. console.log(tree.root);
  24. await tree.delete(Fr.e(33));
  25. console.log(tree.root);
  26. await tree.delete(Fr.e(1234));
  27. await tree.delete(Fr.e(1));
  28. console.log(tree.root);
  29. assert(Fr.isZero(tree.root));
  30. });
  31. it("TestDelete3", async () => {
  32. const tree = await smt.newMemEmptyTrie();
  33. console.log(tree);
  34. let k = Fr.e(1);
  35. let v = Fr.e(1);
  36. await tree.insert(k,v);
  37. k = Fr.e(2);
  38. v = Fr.e(2);
  39. await tree.insert(k,v);
  40. console.log(tree.root);
  41. console.log("TestDelete3", tree.root);
  42. await tree.delete(Fr.e(1));
  43. console.log("TestDelete3", tree.root);
  44. });
  45. it("TestDelete4", async () => {
  46. const tree = await smt.newMemEmptyTrie();
  47. console.log(tree);
  48. let k = Fr.e(1);
  49. let v = Fr.e(1);
  50. await tree.insert(k,v);
  51. k = Fr.e(2);
  52. v = Fr.e(2);
  53. await tree.insert(k,v);
  54. console.log(tree.root);
  55. k = Fr.e(3);
  56. v = Fr.e(3);
  57. await tree.insert(k,v);
  58. console.log(tree.root);
  59. console.log("TestDelete4", tree.root);
  60. await tree.delete(Fr.e(1));
  61. console.log("TestDelete4", tree.root);
  62. });
  63. it("TestDelete5", async () => {
  64. const tree = await smt.newMemEmptyTrie();
  65. console.log(tree);
  66. let k = Fr.e(1);
  67. let v = Fr.e(2);
  68. await tree.insert(k,v);
  69. k = Fr.e(33);
  70. v = Fr.e(44);
  71. await tree.insert(k,v);
  72. console.log(tree.root);
  73. console.log("TestDelete5", tree.root);
  74. await tree.delete(Fr.e(1));
  75. console.log("TestDelete5", tree.root);
  76. });
  77. it("TestAddAndGetCircomProof", async () => {
  78. const tree = await smt.newMemEmptyTrie();
  79. let key = Fr.e(1);
  80. let value = Fr.e(2);
  81. let res = await tree.insert(key,value);
  82. res = fixRes(res);
  83. console.log(res);
  84. key = Fr.e(33);
  85. value = Fr.e(44);
  86. res = await tree.insert(key,value);
  87. res = fixRes(res);
  88. console.log(res);
  89. key = Fr.e(55);
  90. value = Fr.e(66);
  91. res = await tree.insert(key,value);
  92. res = fixRes(res);
  93. console.log(res);
  94. });
  95. it("TestUpdateAndCircomProcessorProof", async () => {
  96. const tree = await smt.newMemEmptyTrie();
  97. console.log(tree);
  98. for (let i=0; i<16; i++) {
  99. let k = Fr.e(i);
  100. let v = Fr.e(i*2);
  101. await tree.insert(k,v);
  102. }
  103. console.log(tree.root);
  104. k = Fr.e(10);
  105. v = Fr.e(1024);
  106. let res = await tree.update(k, v)
  107. console.log(res);
  108. });
  109. })
  110. function fixRes(res) {
  111. while (res.siblings.length<10) res.siblings.push(Fr.e(0));
  112. res.oldKey = res.isOld0 ? 0 : res.oldKey;
  113. res.oldValue = res.isOld0 ? 0 : res.oldValue;
  114. res.isOld0 = res.isOld0 ? 1 : 0;
  115. return res;
  116. }