circomlibjs-hinkal-fork 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/smt.js DELETED
@@ -1,309 +0,0 @@
1
- import { Scalar } from "ffjavascript";
2
- import SMTMemDB from "./smt_memdb.js";
3
- import getHashes from "./smt_hashes_poseidon.js";
4
-
5
- export async function buildSMT(db, root) {
6
-
7
- const {hash0, hash1,F} = await getHashes();
8
-
9
- return new SMT(db, root, hash0, hash1, F);
10
- }
11
-
12
- export class SMT {
13
-
14
- constructor(db, root, hash0, hash1, F) {
15
- this.db = db;
16
- this.root = root;
17
- this.hash0 = hash0;
18
- this.hash1 = hash1;
19
- this.F = F;
20
- }
21
-
22
- _splitBits(_key) {
23
- const F = this.F;
24
- const res = Scalar.bits(F.toObject(_key));
25
-
26
- while (res.length<256) res.push(false);
27
-
28
- return res;
29
- }
30
-
31
- async update(_key, _newValue) {
32
- const F = this.F;
33
- const key = F.e(_key);
34
- const newValue = F.e(_newValue);
35
-
36
- const resFind = await this.find(key);
37
- const res = {};
38
- res.oldRoot = this.root;
39
- res.oldKey = key;
40
- res.oldValue = resFind.foundValue;
41
- res.newKey = key;
42
- res.newValue = newValue;
43
- res.siblings = resFind.siblings;
44
-
45
- const ins = [];
46
- const dels = [];
47
-
48
- let rtOld = this.hash1(key, resFind.foundValue);
49
- let rtNew = this.hash1(key, newValue);
50
- ins.push([rtNew, [1, key, newValue ]]);
51
- dels.push(rtOld);
52
-
53
- const keyBits = this._splitBits(key);
54
- for (let level = resFind.siblings.length-1; level >=0; level--) {
55
- let oldNode, newNode;
56
- const sibling = resFind.siblings[level];
57
- if (keyBits[level]) {
58
- oldNode = [sibling, rtOld];
59
- newNode = [sibling, rtNew];
60
- } else {
61
- oldNode = [rtOld, sibling];
62
- newNode = [rtNew, sibling];
63
- }
64
- rtOld = this.hash0(oldNode[0], oldNode[1]);
65
- rtNew = this.hash0(newNode[0], newNode[1]);
66
- dels.push(rtOld);
67
- ins.push([rtNew, newNode]);
68
- }
69
-
70
- res.newRoot = rtNew;
71
-
72
- await this.db.multiDel(dels);
73
- await this.db.multiIns(ins);
74
- await this.db.setRoot(rtNew);
75
- this.root = rtNew;
76
-
77
- return res;
78
- }
79
-
80
- async delete(_key) {
81
- const F = this.F;
82
- const key = F.e(_key);
83
-
84
- const resFind = await this.find(key);
85
- if (!resFind.found) throw new Error("Key does not exists");
86
-
87
- const res = {
88
- siblings: [],
89
- delKey: key,
90
- delValue: resFind.foundValue
91
- };
92
-
93
- const dels = [];
94
- const ins = [];
95
- let rtOld = this.hash1(key, resFind.foundValue);
96
- let rtNew;
97
- dels.push(rtOld);
98
-
99
- let mixed;
100
- if (resFind.siblings.length > 0) {
101
- const record = await this.db.get(resFind.siblings[resFind.siblings.length - 1]);
102
- if ((record.length == 3)&&(F.eq(record[0], F.one))) {
103
- mixed = false;
104
- res.oldKey = record[1];
105
- res.oldValue = record[2];
106
- res.isOld0 = false;
107
- rtNew = resFind.siblings[resFind.siblings.length - 1];
108
- } else if (record.length == 2) {
109
- mixed = true;
110
- res.oldKey = key;
111
- res.oldValue = F.zero;
112
- res.isOld0 = true;
113
- rtNew = F.zero;
114
- } else {
115
- throw new Error("Invalid node. Database corrupted");
116
- }
117
- } else {
118
- rtNew = F.zero;
119
- res.oldKey = key;
120
- res.oldValue = F.zero;
121
- res.isOld0 = true;
122
- }
123
-
124
- const keyBits = this._splitBits(key);
125
-
126
- for (let level = resFind.siblings.length-1; level >=0; level--) {
127
- let newSibling = resFind.siblings[level];
128
- if ((level == resFind.siblings.length-1)&&(!res.isOld0)) {
129
- newSibling = F.zero;
130
- }
131
- const oldSibling = resFind.siblings[level];
132
- if (keyBits[level]) {
133
- rtOld = this.hash0(oldSibling, rtOld);
134
- } else {
135
- rtOld = this.hash0(rtOld, oldSibling);
136
- }
137
- dels.push(rtOld);
138
- if (!F.isZero(newSibling)) {
139
- mixed = true;
140
- }
141
-
142
- if (mixed) {
143
- res.siblings.unshift(resFind.siblings[level]);
144
- let newNode;
145
- if (keyBits[level]) {
146
- newNode = [newSibling, rtNew];
147
- } else {
148
- newNode = [rtNew, newSibling];
149
- }
150
- rtNew = this.hash0(newNode[0], newNode[1]);
151
- ins.push([rtNew, newNode]);
152
- }
153
- }
154
-
155
- await this.db.multiIns(ins);
156
- await this.db.setRoot(rtNew);
157
- this.root = rtNew;
158
- await this.db.multiDel(dels);
159
-
160
- res.newRoot = rtNew;
161
- res.oldRoot = rtOld;
162
-
163
- return res;
164
- }
165
-
166
- async insert(_key, _value) {
167
- const F = this.F;
168
- const key = F.e(_key);
169
- const value = F.e(_value);
170
- let addedOne = false;
171
- const res = {};
172
- res.oldRoot = this.root;
173
- const newKeyBits = this._splitBits(key);
174
-
175
- let rtOld;
176
-
177
- const resFind = await this.find(key);
178
-
179
- if (resFind.found) throw new Error("Key already exists");
180
-
181
- res.siblings = resFind.siblings;
182
- let mixed;
183
-
184
- if (!resFind.isOld0) {
185
- const oldKeyits = this._splitBits(resFind.notFoundKey);
186
- for (let i= res.siblings.length; oldKeyits[i] == newKeyBits[i]; i++) {
187
- res.siblings.push(F.zero);
188
- }
189
- rtOld = this.hash1(resFind.notFoundKey, resFind.notFoundValue);
190
- res.siblings.push(rtOld);
191
- addedOne = true;
192
- mixed = false;
193
- } else if (res.siblings.length >0) {
194
- mixed = true;
195
- rtOld = F.zero;
196
- }
197
-
198
- const inserts = [];
199
- const dels = [];
200
-
201
- let rt = this.hash1(key, value);
202
- inserts.push([rt,[1, key, value]] );
203
-
204
- for (let i=res.siblings.length-1; i>=0; i--) {
205
- if ((i<res.siblings.length-1)&&(!F.isZero(res.siblings[i]))) {
206
- mixed = true;
207
- }
208
- if (mixed) {
209
- const oldSibling = resFind.siblings[i];
210
- if (newKeyBits[i]) {
211
- rtOld = this.hash0(oldSibling, rtOld);
212
- } else {
213
- rtOld = this.hash0(rtOld, oldSibling);
214
- }
215
- dels.push(rtOld);
216
- }
217
-
218
-
219
- let newRt;
220
- if (newKeyBits[i]) {
221
- newRt = this.hash0(res.siblings[i], rt);
222
- inserts.push([newRt,[res.siblings[i], rt]] );
223
- } else {
224
- newRt = this.hash0(rt, res.siblings[i]);
225
- inserts.push([newRt,[rt, res.siblings[i]]] );
226
- }
227
- rt = newRt;
228
- }
229
-
230
- if (addedOne) res.siblings.pop();
231
- while ((res.siblings.length>0) && (F.isZero(res.siblings[res.siblings.length-1]))) {
232
- res.siblings.pop();
233
- }
234
- res.oldKey = resFind.notFoundKey;
235
- res.oldValue = resFind.notFoundValue;
236
- res.newRoot = rt;
237
- res.isOld0 = resFind.isOld0;
238
-
239
-
240
- await this.db.multiIns(inserts);
241
- await this.db.setRoot(rt);
242
- this.root = rt;
243
- await this.db.multiDel(dels);
244
-
245
- return res;
246
- }
247
-
248
- async find(_key) {
249
- const key = this.F.e(_key);
250
- const keyBits = this._splitBits(key);
251
- return await this._find(key, keyBits, this.root, 0);
252
- }
253
-
254
- async _find(key, keyBits, root, level) {
255
- const F = this.F;
256
- if (typeof root === "undefined") root = this.root;
257
-
258
- let res;
259
- if (F.isZero(root)) {
260
- res = {
261
- found: false,
262
- siblings: [],
263
- notFoundKey: key,
264
- notFoundValue: F.zero,
265
- isOld0: true
266
- };
267
- return res;
268
- }
269
-
270
- const record = await this.db.get(root);
271
-
272
- if ((record.length==3)&&(F.eq(record[0],F.one))) {
273
- if (F.eq(record[1],key)) {
274
- res = {
275
- found: true,
276
- siblings: [],
277
- foundValue: record[2],
278
- isOld0: false
279
- };
280
- } else {
281
- res = {
282
- found: false,
283
- siblings: [],
284
- notFoundKey: record[1],
285
- notFoundValue: record[2],
286
- isOld0: false
287
- };
288
- }
289
- } else {
290
- if (keyBits[level] == 0) {
291
- res = await this._find(key, keyBits, record[0], level+1);
292
- res.siblings.unshift(record[1]);
293
- } else {
294
- res = await this._find(key, keyBits, record[1], level+1);
295
- res.siblings.unshift(record[0]);
296
- }
297
- }
298
- return res;
299
- }
300
- }
301
-
302
-
303
- export async function newMemEmptyTrie() {
304
- const {hash0, hash1,F} = await getHashes();
305
- const db = new SMTMemDB(F);
306
- const rt = await db.getRoot();
307
- const smt = new SMT(db, rt, hash0, hash1, F);
308
- return smt;
309
- }
@@ -1,16 +0,0 @@
1
-
2
- import buildMimc7 from "./mimc7.js";
3
-
4
- export default async function getHashes() {
5
- const mimc7 = await buildMimc7();
6
- return {
7
- hash0: function (left, right) {
8
- return mimc7.hash(left, right);
9
- },
10
- hash1: function(key, value) {
11
- return mimc7.multiHash([key, value], F.one);
12
- },
13
- F: mimc7.F
14
- }
15
- }
16
-
@@ -1,17 +0,0 @@
1
- import { buildPoseidon } from "./poseidon_wasm.js";
2
- import { getCurveFromName } from "ffjavascript";
3
-
4
-
5
- export default async function getHashes() {
6
- const bn128 = await getCurveFromName("bn128", true);
7
- const poseidon = await buildPoseidon();
8
- return {
9
- hash0: function (left, right) {
10
- return poseidon([left, right]);
11
- },
12
- hash1: function(key, value) {
13
- return poseidon([key, value, bn128.Fr.one]);
14
- },
15
- F: bn128.Fr
16
- }
17
- }
package/src/smt_memdb.js DELETED
@@ -1,57 +0,0 @@
1
- export default class SMTMemDb {
2
- constructor(F) {
3
- this.nodes = {};
4
- this.root = F.zero;
5
- this.F = F;
6
- }
7
-
8
- async getRoot() {
9
- return this.root;
10
- }
11
-
12
- _key2str(k) {
13
- const F = this.F;
14
- const keyS = this.F.toString(k);
15
- return keyS;
16
- }
17
-
18
- _normalize(n) {
19
- const F = this.F;
20
- for (let i=0; i<n.length; i++) {
21
- n[i] = this.F.e(n[i]);
22
- }
23
- }
24
-
25
- async get(key) {
26
- const keyS = this._key2str(key);
27
- return this.nodes[keyS];
28
- }
29
-
30
- async multiGet(keys) {
31
- const promises = [];
32
- for (let i=0; i<keys.length; i++) {
33
- promises.push(this.get(keys[i]));
34
- }
35
- return await Promise.all(promises);
36
- }
37
-
38
- async setRoot(rt) {
39
- this.root = rt;
40
- }
41
-
42
- async multiIns(inserts) {
43
- for (let i=0; i<inserts.length; i++) {
44
- const keyS = this._key2str(inserts[i][0]);
45
- this._normalize(inserts[i][1]);
46
- this.nodes[keyS] = inserts[i][1];
47
- }
48
- }
49
-
50
- async multiDel(dels) {
51
- for (let i=0; i<dels.length; i++) {
52
- const keyS = this._key2str(dels[i]);
53
- delete this.nodes[keyS];
54
- }
55
- }
56
- }
57
-