zkjson 0.1.10 → 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/contracts/ZKRollup.sol +8 -11
- package/db.js +73 -7
- package/encoder.js +5 -0
- package/index.js +2 -1
- package/package.json +1 -1
package/contracts/ZKRollup.sol
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
pragma solidity >=0.7.0 <0.9.0;
|
|
4
4
|
import "hardhat/console.sol";
|
|
5
|
-
import "./ZKJson.sol";
|
|
6
5
|
import "./ZKQuery.sol";
|
|
7
6
|
|
|
8
7
|
interface VerifierRU {
|
|
@@ -12,7 +11,7 @@ interface VerifierRU {
|
|
|
12
11
|
|
|
13
12
|
contract ZKRollup is ZKQuery {
|
|
14
13
|
address public verifierRU;
|
|
15
|
-
address public
|
|
14
|
+
address public committer;
|
|
16
15
|
uint public root;
|
|
17
16
|
|
|
18
17
|
function _verifyRU(uint[] calldata zkp) internal view returns (bool) {
|
|
@@ -29,29 +28,27 @@ contract ZKRollup is ZKQuery {
|
|
|
29
28
|
return true;
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
function _validateQueryRU(uint[] memory path, uint[] calldata zkp, uint
|
|
31
|
+
function _validateQueryRU(uint[] memory path, uint[] calldata zkp, uint size_path, uint size_val) internal view returns(uint[] memory){
|
|
33
32
|
require(zkp[19] == root, "root mismatch");
|
|
34
|
-
require(zkp[
|
|
35
|
-
require(zkp[
|
|
33
|
+
require(zkp[size_path + size_val + 10] == path[0], "wrong collection");
|
|
34
|
+
require(zkp[size_path + size_val + 11] == path[1], "wrong doc");
|
|
36
35
|
require(zkp[8] == 1, "value doesn't exist");
|
|
37
|
-
require(path.length <=
|
|
36
|
+
require(path.length <= size_path + size_val, "path too long");
|
|
38
37
|
for(uint i = 9; i < 9 + path.length - 2; i++) require(path[i - 7] == zkp[i], "wrong path");
|
|
39
|
-
uint[] memory value = new uint[](
|
|
40
|
-
for(uint i = 9 +
|
|
38
|
+
uint[] memory value = new uint[](size_val);
|
|
39
|
+
for(uint i = 9 + size_path; i < 9 + size_path + size_val; i++) value[i - (9 + size_path)] = zkp[i];
|
|
41
40
|
return toArr(value);
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
function commit (uint[] calldata zkp) public returns (uint) {
|
|
45
44
|
require (zkp[9] == root, "wrong merkle root");
|
|
46
|
-
require(msg.sender ==
|
|
45
|
+
require(msg.sender == committer, "sender is not committer");
|
|
47
46
|
root = zkp[8];
|
|
48
47
|
verifyRU(zkp);
|
|
49
48
|
return root;
|
|
50
|
-
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
function verifyRU(uint[] calldata zkp) public view returns (bool) {
|
|
54
52
|
return _verifyRU(zkp);
|
|
55
53
|
}
|
|
56
|
-
|
|
57
54
|
}
|
package/db.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const newMemEmptyTrie = require("./circomlibjs").newMemEmptyTrie
|
|
2
|
+
const snarkjs = require("snarkjs")
|
|
2
3
|
const { range } = require("ramda")
|
|
3
4
|
const {
|
|
4
5
|
pad,
|
|
@@ -7,7 +8,7 @@ const {
|
|
|
7
8
|
val2str,
|
|
8
9
|
id2str,
|
|
9
10
|
encode,
|
|
10
|
-
|
|
11
|
+
toIndex,
|
|
11
12
|
} = require("./encoder")
|
|
12
13
|
const Collection = require("./collection")
|
|
13
14
|
|
|
@@ -19,7 +20,15 @@ class DB {
|
|
|
19
20
|
size_json = 256,
|
|
20
21
|
size_txs = 10,
|
|
21
22
|
level_col = 8,
|
|
23
|
+
wasm,
|
|
24
|
+
zkey,
|
|
25
|
+
wasmRU,
|
|
26
|
+
zkeyRU,
|
|
22
27
|
}) {
|
|
28
|
+
this.wasm = wasm
|
|
29
|
+
this.zkey = zkey
|
|
30
|
+
this.wasmRU = wasmRU
|
|
31
|
+
this.zkeyRU = zkeyRU
|
|
23
32
|
this.level_col = level_col
|
|
24
33
|
this.size = size_val
|
|
25
34
|
this.size_path = size_path
|
|
@@ -47,7 +56,63 @@ class DB {
|
|
|
47
56
|
siblings = siblings.map(s => s.toString())
|
|
48
57
|
return { isOld0, oldRoot, oldKey, oldValue, siblings, newRoot }
|
|
49
58
|
}
|
|
59
|
+
_getVal(j, p) {
|
|
60
|
+
if (p.length === 0) {
|
|
61
|
+
return j
|
|
62
|
+
} else {
|
|
63
|
+
const sp = p[0].split("[")
|
|
64
|
+
for (let v of sp) {
|
|
65
|
+
if (/]$/.test(v)) {
|
|
66
|
+
j = j[v.replace(/]$/, "") * 1]
|
|
67
|
+
} else {
|
|
68
|
+
j = j[v]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return this._getVal(j, p.slice(1))
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getVal(j, p) {
|
|
75
|
+
if (p === "") return j
|
|
76
|
+
return this._getVal(j, p.split("."))
|
|
77
|
+
}
|
|
50
78
|
|
|
79
|
+
async genProof({ json, col_id, path, id }) {
|
|
80
|
+
const inputs = await this.getInputs({
|
|
81
|
+
id,
|
|
82
|
+
col_id,
|
|
83
|
+
json,
|
|
84
|
+
path,
|
|
85
|
+
val: this.getVal(json, path),
|
|
86
|
+
})
|
|
87
|
+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
|
|
88
|
+
inputs,
|
|
89
|
+
this.wasm,
|
|
90
|
+
this.zkey
|
|
91
|
+
)
|
|
92
|
+
return [
|
|
93
|
+
...proof.pi_a.slice(0, 2),
|
|
94
|
+
...proof.pi_b[0].slice(0, 2).reverse(),
|
|
95
|
+
...proof.pi_b[1].slice(0, 2).reverse(),
|
|
96
|
+
...proof.pi_c.slice(0, 2),
|
|
97
|
+
...publicSignals,
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async genRollupProof(txs) {
|
|
102
|
+
const inputs = await this.getRollupInputs({ queries: txs })
|
|
103
|
+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
|
|
104
|
+
inputs,
|
|
105
|
+
this.wasmRU,
|
|
106
|
+
this.zkeyRU
|
|
107
|
+
)
|
|
108
|
+
return [
|
|
109
|
+
...proof.pi_a.slice(0, 2),
|
|
110
|
+
...proof.pi_b[0].slice(0, 2).reverse(),
|
|
111
|
+
...proof.pi_b[1].slice(0, 2).reverse(),
|
|
112
|
+
...proof.pi_c.slice(0, 2),
|
|
113
|
+
...publicSignals,
|
|
114
|
+
]
|
|
115
|
+
}
|
|
51
116
|
async getRollupInputs({ queries }) {
|
|
52
117
|
let write, _json
|
|
53
118
|
let oldRoot = []
|
|
@@ -79,6 +144,7 @@ class DB {
|
|
|
79
144
|
siblings.push(range(0, this.level).map(() => "0"))
|
|
80
145
|
isOld0.push("0")
|
|
81
146
|
oldRoot_db.push(newRoot_db[i - 1])
|
|
147
|
+
newRoot_db.push(newRoot_db[i - 1])
|
|
82
148
|
oldKey_db.push("0")
|
|
83
149
|
oldValue_db.push("0")
|
|
84
150
|
siblings_db.push(range(0, this.level_col).map(() => "0"))
|
|
@@ -92,9 +158,9 @@ class DB {
|
|
|
92
158
|
const icol = this.parse(res, tree, this.level)
|
|
93
159
|
const idb = this.parse(res2, this.tree, this.level_col)
|
|
94
160
|
_res = idb
|
|
95
|
-
const _newKey =
|
|
161
|
+
const _newKey = toIndex(v[1])
|
|
96
162
|
json.push(pad(val2str(encode(_json)), this.size_json))
|
|
97
|
-
const _newKey_db = v[0]
|
|
163
|
+
const _newKey_db = v[0].toString()
|
|
98
164
|
fnc.push(update ? [0, 1] : [1, 0])
|
|
99
165
|
newRoot.push(idb.newRoot)
|
|
100
166
|
oldRoot.push(icol.oldRoot)
|
|
@@ -140,8 +206,8 @@ class DB {
|
|
|
140
206
|
} = await this.insert(col_id, id, json)
|
|
141
207
|
const icol = this.parse(res, tree, this.level)
|
|
142
208
|
const idb = this.parse(res2, this.tree, this.level_col)
|
|
143
|
-
const newKey =
|
|
144
|
-
const newKey_db = col_id
|
|
209
|
+
const newKey = toIndex(id)
|
|
210
|
+
const newKey_db = col_id.toString()
|
|
145
211
|
return {
|
|
146
212
|
fnc: update ? [0, 1] : [1, 0],
|
|
147
213
|
oldRoot: icol.oldRoot,
|
|
@@ -179,7 +245,7 @@ class DB {
|
|
|
179
245
|
json: col_inputs.json,
|
|
180
246
|
root: col_inputs.root,
|
|
181
247
|
siblings: col_inputs.siblings,
|
|
182
|
-
key:
|
|
248
|
+
key: toIndex(id),
|
|
183
249
|
col_key,
|
|
184
250
|
col_siblings,
|
|
185
251
|
col_root,
|
|
@@ -244,7 +310,7 @@ class DB {
|
|
|
244
310
|
return await _col.get(_key)
|
|
245
311
|
}
|
|
246
312
|
async getCol(_key) {
|
|
247
|
-
const id =
|
|
313
|
+
const id = toIndex(_key)
|
|
248
314
|
return await this.tree.find(id)
|
|
249
315
|
}
|
|
250
316
|
}
|
package/encoder.js
CHANGED
|
@@ -453,6 +453,9 @@ const fromSignal = str2val
|
|
|
453
453
|
const toIndex = str2id
|
|
454
454
|
const fromIndex = id2str
|
|
455
455
|
|
|
456
|
+
const path = p => toSignal(encodePath(p))
|
|
457
|
+
const val = v => toSignal(encodeVal(v))
|
|
458
|
+
|
|
456
459
|
module.exports = {
|
|
457
460
|
encode,
|
|
458
461
|
decode,
|
|
@@ -471,4 +474,6 @@ module.exports = {
|
|
|
471
474
|
fromSignal,
|
|
472
475
|
toIndex,
|
|
473
476
|
fromIndex,
|
|
477
|
+
path,
|
|
478
|
+
val,
|
|
474
479
|
}
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const encoder = require("./encoder")
|
|
2
2
|
const DB = require("./db")
|
|
3
3
|
const Doc = require("./doc")
|
|
4
|
+
const Rollup = require("./rollup")
|
|
4
5
|
const Collection = require("./collection")
|
|
5
6
|
|
|
6
|
-
module.exports = { ...encoder, DB, Collection, Doc }
|
|
7
|
+
module.exports = { ...encoder, DB, Collection, Doc, Rollup }
|