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.

63 lines
1.5 KiB

4 years ago
  1. const Scalar = require("ffjavascript").Scalar;
  2. const ZqField = require("ffjavascript").ZqField;
  3. // Prime 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
  4. const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
  5. class SMTMemDb {
  6. constructor() {
  7. this.nodes = {};
  8. this.root = F.zero;
  9. }
  10. async getRoot() {
  11. return this.root;
  12. }
  13. _key2str(k) {
  14. // const keyS = bigInt(key).leInt2Buff(32).toString("hex");
  15. const keyS = k.toString();
  16. return keyS;
  17. }
  18. _normalize(n) {
  19. for (let i=0; i<n.length; i++) {
  20. n[i] = F.e(n[i]);
  21. }
  22. }
  23. async get(key) {
  24. const keyS = this._key2str(key);
  25. return this.nodes[keyS];
  26. }
  27. async multiGet(keys) {
  28. const promises = [];
  29. for (let i=0; i<keys.length; i++) {
  30. promises.push(this.get(keys[i]));
  31. }
  32. return await Promise.all(promises);
  33. }
  34. async setRoot(rt) {
  35. this.root = rt;
  36. }
  37. async multiIns(inserts) {
  38. for (let i=0; i<inserts.length; i++) {
  39. const keyS = this._key2str(inserts[i][0]);
  40. this._normalize(inserts[i][1]);
  41. this.nodes[keyS] = inserts[i][1];
  42. }
  43. }
  44. async multiDel(dels) {
  45. for (let i=0; i<dels.length; i++) {
  46. const keyS = this._key2str(dels[i]);
  47. delete this.nodes[keyS];
  48. }
  49. }
  50. }
  51. module.exports = SMTMemDb;