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