zkjson 0.1.11 → 0.1.12
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/package.json +1 -1
- package/rollup.js +0 -224
package/package.json
CHANGED
package/rollup.js
DELETED
@@ -1,224 +0,0 @@
|
|
1
|
-
const snarkjs = require("snarkjs")
|
2
|
-
const { resolve } = require("path")
|
3
|
-
const { pad, toSignal, encode, encodePath, encodeVal } = require("./encoder")
|
4
|
-
const DB = require("./db")
|
5
|
-
const { range } = require("ramda")
|
6
|
-
|
7
|
-
module.exports = class Rollup {
|
8
|
-
constructor({
|
9
|
-
size_val = 5,
|
10
|
-
size_path = 5,
|
11
|
-
size_json = 256,
|
12
|
-
level = 100,
|
13
|
-
size_txs = 10,
|
14
|
-
level_col = 8,
|
15
|
-
wasm,
|
16
|
-
zkey,
|
17
|
-
}) {
|
18
|
-
this.size_val = size_val
|
19
|
-
this.size_path = size_path
|
20
|
-
this.size_json = size_json
|
21
|
-
this.size_txs = size_txs
|
22
|
-
this.level = level
|
23
|
-
this.level_col = level_col
|
24
|
-
this.wasm = wasm
|
25
|
-
this.zkey = zkey
|
26
|
-
}
|
27
|
-
async init() {
|
28
|
-
this.db = new DB({
|
29
|
-
level: this.level,
|
30
|
-
size_path: this.size_path,
|
31
|
-
size_val: this.size_val,
|
32
|
-
size_json: this.size_json,
|
33
|
-
size_txs: this.size_txs,
|
34
|
-
level_col: this.level_col,
|
35
|
-
})
|
36
|
-
await this.db.init()
|
37
|
-
}
|
38
|
-
async getInputs(queries) {
|
39
|
-
let write, _json
|
40
|
-
let oldRoot = []
|
41
|
-
let newRoot = []
|
42
|
-
let oldKey = []
|
43
|
-
let oldValue = []
|
44
|
-
let siblings = []
|
45
|
-
let isOld0 = []
|
46
|
-
let oldRoot_db = []
|
47
|
-
let newRoot_db = []
|
48
|
-
let oldKey_db = []
|
49
|
-
let oldValue_db = []
|
50
|
-
let siblings_db = []
|
51
|
-
let isOld0_db = []
|
52
|
-
let newKey_db = []
|
53
|
-
let newKey = []
|
54
|
-
let _res
|
55
|
-
let json = []
|
56
|
-
let fnc = []
|
57
|
-
for (let i = 0; i < this.size_txs; i++) {
|
58
|
-
const v = queries[i]
|
59
|
-
if (!v) {
|
60
|
-
json.push(range(0, this.size_json).map(() => "0"))
|
61
|
-
fnc.push([0, 0])
|
62
|
-
newRoot.push(newRoot[i - 1])
|
63
|
-
oldRoot.push("0")
|
64
|
-
oldKey.push("0")
|
65
|
-
oldValue.push("0")
|
66
|
-
siblings.push(range(0, this.level).map(() => "0"))
|
67
|
-
isOld0.push("0")
|
68
|
-
oldRoot_db.push(newRoot_db[i - 1])
|
69
|
-
oldKey_db.push("0")
|
70
|
-
oldValue_db.push("0")
|
71
|
-
siblings_db.push(range(0, this.level_col).map(() => "0"))
|
72
|
-
isOld0_db.push("0")
|
73
|
-
newKey_db.push("0")
|
74
|
-
newKey.push("0")
|
75
|
-
continue
|
76
|
-
}
|
77
|
-
_json = v[2]
|
78
|
-
const { update, tree, col: res2, doc: res } = await this.insert(...v)
|
79
|
-
const icol = this.parse(res, tree, this.level)
|
80
|
-
const idb = this.parse(res2, this.tree, this.level_col)
|
81
|
-
_res = idb
|
82
|
-
const _newKey = str2id(v[1])
|
83
|
-
json.push(pad(val2str(encode(_json)), this.size_json))
|
84
|
-
const _newKey_db = v[0]
|
85
|
-
fnc.push(update ? [0, 1] : [1, 0])
|
86
|
-
newRoot.push(idb.newRoot)
|
87
|
-
oldRoot.push(icol.oldRoot)
|
88
|
-
oldKey.push(icol.oldKey)
|
89
|
-
oldValue.push(icol.oldValue)
|
90
|
-
siblings.push(icol.siblings)
|
91
|
-
isOld0.push(icol.isOld0)
|
92
|
-
oldRoot_db.push(idb.oldRoot)
|
93
|
-
newRoot_db.push(idb.newRoot)
|
94
|
-
oldKey_db.push(idb.oldKey)
|
95
|
-
oldValue_db.push(idb.oldValue)
|
96
|
-
siblings_db.push(idb.siblings)
|
97
|
-
isOld0_db.push(idb.isOld0)
|
98
|
-
newKey_db.push(_newKey_db)
|
99
|
-
newKey.push(_newKey)
|
100
|
-
}
|
101
|
-
|
102
|
-
return {
|
103
|
-
fnc,
|
104
|
-
oldRoot,
|
105
|
-
newRoot,
|
106
|
-
oldKey,
|
107
|
-
oldValue,
|
108
|
-
siblings,
|
109
|
-
isOld0,
|
110
|
-
oldRoot_db,
|
111
|
-
oldKey_db,
|
112
|
-
oldValue_db,
|
113
|
-
siblings_db,
|
114
|
-
isOld0_db,
|
115
|
-
newKey_db,
|
116
|
-
newKey,
|
117
|
-
json,
|
118
|
-
}
|
119
|
-
}
|
120
|
-
async genProof(queries) {
|
121
|
-
const inputs = await this.getInputs(queries)
|
122
|
-
console.log(inputs)
|
123
|
-
return 3
|
124
|
-
}
|
125
|
-
async genProof2(col, doc, tar, path) {
|
126
|
-
const val = this.getVal(tar, path)
|
127
|
-
|
128
|
-
const col_root = this.db.tree.F.toObject(this.db.tree.root).toString()
|
129
|
-
const col_res = await this.db.getCol(doc)
|
130
|
-
|
131
|
-
let col_siblings = col_res.siblings
|
132
|
-
for (let i = 0; i < col_siblings.length; i++)
|
133
|
-
col_siblings[i] = this.db.tree.F.toObject(col_siblings[i])
|
134
|
-
while (col_siblings.length < this.level_col) col_siblings.push(0)
|
135
|
-
col_siblings = col_siblings.map(s => s.toString())
|
136
|
-
const col_key = col
|
137
|
-
|
138
|
-
const _col = this.db.getColTree(col)
|
139
|
-
const root = _col.tree.F.toObject(_col.tree.root).toString()
|
140
|
-
const res = await _col.get(doc)
|
141
|
-
let _siblings = res.siblings
|
142
|
-
for (let i = 0; i < _siblings.length; i++)
|
143
|
-
_siblings[i] = _col.tree.F.toObject(_siblings[i])
|
144
|
-
while (_siblings.length < this.level) _siblings.push(0)
|
145
|
-
_siblings = _siblings.map(s => s.toString())
|
146
|
-
const key = toIndex(doc)
|
147
|
-
|
148
|
-
const _json = pad(toSignal(encode(tar)), this.size_json)
|
149
|
-
const _path = pad(toSignal(encodePath(path)), this.size_path)
|
150
|
-
const _val = pad(toSignal(encodeVal(val)), this.size)
|
151
|
-
const _write = {
|
152
|
-
json: _json,
|
153
|
-
path: _path,
|
154
|
-
val: _val,
|
155
|
-
root,
|
156
|
-
siblings: _siblings,
|
157
|
-
key,
|
158
|
-
col_key,
|
159
|
-
col_siblings,
|
160
|
-
col_root,
|
161
|
-
}
|
162
|
-
const { proof: proof2, publicSignals: sigs } =
|
163
|
-
await snarkjs.groth16.fullProve(
|
164
|
-
_write,
|
165
|
-
resolve(
|
166
|
-
__dirname,
|
167
|
-
"../../circom/build/circuits/db/index_js/index.wasm"
|
168
|
-
),
|
169
|
-
resolve(__dirname, "../../circom/build/circuits/db/index_0001.zkey")
|
170
|
-
)
|
171
|
-
return [
|
172
|
-
...proof2.pi_a.slice(0, 2),
|
173
|
-
...proof2.pi_b[0].slice(0, 2).reverse(),
|
174
|
-
...proof2.pi_b[1].slice(0, 2).reverse(),
|
175
|
-
...proof2.pi_c.slice(0, 2),
|
176
|
-
...sigs,
|
177
|
-
]
|
178
|
-
}
|
179
|
-
|
180
|
-
async getInputs2({ json, path }) {
|
181
|
-
return {
|
182
|
-
json: pad(toSignal(encode(json)), this.size_json),
|
183
|
-
path: pad(toSignal(encodePath(path)), this.size_path),
|
184
|
-
val: pad(toSignal(encodeVal(this.getVal(json, path))), this.size_val),
|
185
|
-
}
|
186
|
-
}
|
187
|
-
|
188
|
-
_getVal(j, p) {
|
189
|
-
if (p.length === 0) {
|
190
|
-
return j
|
191
|
-
} else {
|
192
|
-
const sp = p[0].split("[")
|
193
|
-
for (let v of sp) {
|
194
|
-
if (/]$/.test(v)) {
|
195
|
-
j = j[v.replace(/]$/, "") * 1]
|
196
|
-
} else {
|
197
|
-
j = j[v]
|
198
|
-
}
|
199
|
-
}
|
200
|
-
return this._getVal(j, p.slice(1))
|
201
|
-
}
|
202
|
-
}
|
203
|
-
|
204
|
-
getVal(j, p) {
|
205
|
-
if (p === "") return j
|
206
|
-
return this._getVal(j, p.split("."))
|
207
|
-
}
|
208
|
-
|
209
|
-
async genProof2(json, path) {
|
210
|
-
const inputs = await this.getInputs(json, path)
|
211
|
-
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
|
212
|
-
inputs,
|
213
|
-
this.wasm,
|
214
|
-
this.zkey
|
215
|
-
)
|
216
|
-
return [
|
217
|
-
...proof.pi_a.slice(0, 2),
|
218
|
-
...proof.pi_b[0].slice(0, 2).reverse(),
|
219
|
-
...proof.pi_b[1].slice(0, 2).reverse(),
|
220
|
-
...proof.pi_c.slice(0, 2),
|
221
|
-
...publicSignals,
|
222
|
-
]
|
223
|
-
}
|
224
|
-
}
|