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.

149 lines
3.8 KiB

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