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