zkjson 0.1.12 → 0.1.14
Sign up to get free protection for your applications and to get access to all the features.
- package/contracts/ZKJson.sol +9 -5
- package/contracts/ZKQuery.sol +4 -4
- package/contracts/ZKRollup.sol +3 -2
- package/db.js +36 -10
- package/index.js +1 -2
- package/package.json +1 -1
- package/contracts/ZKDB.sol +0 -70
package/contracts/ZKJson.sol
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
2
2
|
|
3
3
|
pragma solidity >=0.7.0 <0.9.0;
|
4
|
-
import "hardhat/console.sol";
|
5
4
|
import "./ZKQuery.sol";
|
6
5
|
|
7
6
|
contract ZKJson is ZKQuery{
|
7
|
+
address public verifierJSON;
|
8
8
|
|
9
|
-
function _validateQueryJSON(uint[] memory path, uint[] calldata zkp, uint
|
9
|
+
function _validateQueryJSON(uint[] memory path, uint[] calldata zkp, uint size_path, uint size_val) internal pure returns(uint[] memory){
|
10
10
|
require(zkp[8] == 1, "value doesn't exist");
|
11
|
-
for(uint i = 10; i <
|
12
|
-
|
13
|
-
|
11
|
+
for(uint i = 10; i < 10 + size_path; i++){
|
12
|
+
require((path.length <= i - 10 && zkp[i] == 0) || path[i - 10] == zkp[i], "wrong path");
|
13
|
+
}
|
14
|
+
uint[] memory value = new uint[](size_val);
|
15
|
+
for(uint i = 10 + size_path; i < 10 + size_path + size_val; i++){
|
16
|
+
value[i - (10 + size_val)] = zkp[i];
|
17
|
+
}
|
14
18
|
return toArr(value);
|
15
19
|
}
|
16
20
|
|
package/contracts/ZKQuery.sol
CHANGED
@@ -315,22 +315,22 @@ contract ZKQuery {
|
|
315
315
|
return (_pA, _pB, _pC, sigs);
|
316
316
|
}
|
317
317
|
|
318
|
-
function getInt (uint[] memory path, uint[] memory raw)
|
318
|
+
function getInt (uint[] memory path, uint[] memory raw) internal pure returns (int) {
|
319
319
|
uint[] memory value = getVal(path, raw);
|
320
320
|
return _qInt(value);
|
321
321
|
}
|
322
322
|
|
323
|
-
function getString (uint[] memory path, uint[] memory raw)
|
323
|
+
function getString (uint[] memory path, uint[] memory raw) internal pure returns (string memory) {
|
324
324
|
uint[] memory value = getVal(path, raw);
|
325
325
|
_qString(value);
|
326
326
|
}
|
327
327
|
|
328
|
-
function getBool (uint[] memory path, uint[] memory raw)
|
328
|
+
function getBool (uint[] memory path, uint[] memory raw) internal pure returns (bool) {
|
329
329
|
uint[] memory value = getVal(path, raw);
|
330
330
|
_qBool(value);
|
331
331
|
}
|
332
332
|
|
333
|
-
function getNull (uint[] memory path, uint[] memory raw)
|
333
|
+
function getNull (uint[] memory path, uint[] memory raw) internal pure returns (bool) {
|
334
334
|
uint[] memory value = getVal(path, raw);
|
335
335
|
_qNull(value);
|
336
336
|
}
|
package/contracts/ZKRollup.sol
CHANGED
@@ -14,7 +14,7 @@ contract ZKRollup is ZKQuery {
|
|
14
14
|
address public committer;
|
15
15
|
uint public root;
|
16
16
|
|
17
|
-
function _verifyRU(uint[] calldata zkp)
|
17
|
+
function _verifyRU(uint[] calldata zkp) private view returns (bool) {
|
18
18
|
uint[2] memory _pA;
|
19
19
|
uint[2][2] memory _pB;
|
20
20
|
uint[2] memory _pC;
|
@@ -48,7 +48,8 @@ contract ZKRollup is ZKQuery {
|
|
48
48
|
return root;
|
49
49
|
}
|
50
50
|
|
51
|
-
function verifyRU(uint[] calldata zkp)
|
51
|
+
function verifyRU(uint[] calldata zkp) private view returns (bool) {
|
52
52
|
return _verifyRU(zkp);
|
53
53
|
}
|
54
|
+
|
54
55
|
}
|
package/db.js
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
const newMemEmptyTrie = require("./circomlibjs").newMemEmptyTrie
|
2
2
|
const snarkjs = require("snarkjs")
|
3
3
|
const { range } = require("ramda")
|
4
|
-
const {
|
5
|
-
pad,
|
6
|
-
toSignal,
|
7
|
-
str2val,
|
8
|
-
val2str,
|
9
|
-
id2str,
|
10
|
-
encode,
|
11
|
-
toIndex,
|
12
|
-
} = require("./encoder")
|
4
|
+
const { pad, toSignal, encode, toIndex } = require("./encoder")
|
13
5
|
const Collection = require("./collection")
|
14
6
|
|
15
7
|
class DB {
|
@@ -113,6 +105,40 @@ class DB {
|
|
113
105
|
...publicSignals,
|
114
106
|
]
|
115
107
|
}
|
108
|
+
|
109
|
+
async query(col_id, id, path, json) {
|
110
|
+
const val = tar[path]
|
111
|
+
const inputs = await this.genProof({ col_id, id, json, path })
|
112
|
+
const sigs = inputs.slice(8)
|
113
|
+
const params = [[sigs[12], sigs[13], ...sigs.slice(1, 6)], inputs]
|
114
|
+
let type =
|
115
|
+
val === null
|
116
|
+
? 0
|
117
|
+
: typeof val === "string"
|
118
|
+
? 3
|
119
|
+
: typeof val === "boolean"
|
120
|
+
? 1
|
121
|
+
: typeof val === "number"
|
122
|
+
? Number.isInteger(val)
|
123
|
+
? 2
|
124
|
+
: 2.5
|
125
|
+
: 4
|
126
|
+
switch (type) {
|
127
|
+
case 0:
|
128
|
+
return await this.zkdb.qNull(...params)
|
129
|
+
case 1:
|
130
|
+
return await this.zkdb.qBool(...params)
|
131
|
+
case 2:
|
132
|
+
return (await this.zkdb.qInt(...params)).toString() * 1
|
133
|
+
case 2.5:
|
134
|
+
return (await this.zkdb.qFloat(...params)).map(n => n.toString() * 1)
|
135
|
+
case 3:
|
136
|
+
return await this.zkdb.qString(...params)
|
137
|
+
case 4:
|
138
|
+
return (await this.zkdb.qRaw(...params)).map(n => n.toString() * 1)
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
116
142
|
async getRollupInputs({ queries }) {
|
117
143
|
let write, _json
|
118
144
|
let oldRoot = []
|
@@ -159,7 +185,7 @@ class DB {
|
|
159
185
|
const idb = this.parse(res2, this.tree, this.level_col)
|
160
186
|
_res = idb
|
161
187
|
const _newKey = toIndex(v[1])
|
162
|
-
json.push(pad(
|
188
|
+
json.push(pad(toSignal(encode(_json)), this.size_json))
|
163
189
|
const _newKey_db = v[0].toString()
|
164
190
|
fnc.push(update ? [0, 1] : [1, 0])
|
165
191
|
newRoot.push(idb.newRoot)
|
package/index.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
const encoder = require("./encoder")
|
2
2
|
const DB = require("./db")
|
3
3
|
const Doc = require("./doc")
|
4
|
-
const Rollup = require("./rollup")
|
5
4
|
const Collection = require("./collection")
|
6
5
|
|
7
|
-
module.exports = { ...encoder, DB, Collection, Doc
|
6
|
+
module.exports = { ...encoder, DB, Collection, Doc }
|
package/package.json
CHANGED
package/contracts/ZKDB.sol
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
2
|
-
|
3
|
-
pragma solidity >=0.7.0 <0.9.0;
|
4
|
-
|
5
|
-
import "hardhat/console.sol";
|
6
|
-
import "./ZKRollup.sol";
|
7
|
-
|
8
|
-
interface VerifierDB {
|
9
|
-
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[14] calldata _pubSignals) view external returns (bool);
|
10
|
-
}
|
11
|
-
|
12
|
-
contract ZKDB is ZKRollup {
|
13
|
-
uint constant public SIZE = 5;
|
14
|
-
address public verifierDB;
|
15
|
-
|
16
|
-
constructor (address _verifierRU, address _verifierDB, address _comitter){
|
17
|
-
verifierRU = _verifierRU;
|
18
|
-
verifierDB = _verifierDB;
|
19
|
-
comitter = _comitter;
|
20
|
-
}
|
21
|
-
|
22
|
-
function verify(uint[] calldata zkp) public view returns (bool) {
|
23
|
-
uint[SIZE * 2 + 4] memory sigs;
|
24
|
-
(
|
25
|
-
uint[2] memory _pA,
|
26
|
-
uint[2][2] memory _pB,
|
27
|
-
uint[2] memory _pC,
|
28
|
-
uint[] memory _sigs
|
29
|
-
) = _parseZKP(zkp);
|
30
|
-
for(uint i = 0; i < sigs.length; i++) sigs[i] = _sigs[i];
|
31
|
-
require(VerifierDB(verifierDB).verifyProof(_pA, _pB, _pC, sigs), "invalid proof");
|
32
|
-
return true;
|
33
|
-
}
|
34
|
-
|
35
|
-
function validateQuery(uint[] memory path, uint[] calldata zkp) public view returns(uint[] memory){
|
36
|
-
verify(zkp);
|
37
|
-
return _validateQueryRU(path, zkp, SIZE);
|
38
|
-
}
|
39
|
-
|
40
|
-
function qInt (uint[] memory path, uint[] calldata zkp) public view returns (int) {
|
41
|
-
uint[] memory value = validateQuery(path, zkp);
|
42
|
-
return _qInt(value);
|
43
|
-
}
|
44
|
-
|
45
|
-
function qFloat (uint[] memory path, uint[] calldata zkp) public view returns (uint[3] memory) {
|
46
|
-
uint[] memory value = validateQuery(path, zkp);
|
47
|
-
return _qFloat(value);
|
48
|
-
}
|
49
|
-
|
50
|
-
function qRaw (uint[] memory path, uint[] calldata zkp) public view returns (uint[] memory) {
|
51
|
-
uint[] memory value = validateQuery(path, zkp);
|
52
|
-
return _qRaw(value);
|
53
|
-
}
|
54
|
-
|
55
|
-
function qString (uint[] memory path, uint[] calldata zkp) public view returns (string memory) {
|
56
|
-
uint[] memory value = validateQuery(path, zkp);
|
57
|
-
return _qString(value);
|
58
|
-
}
|
59
|
-
|
60
|
-
function qBool (uint[] memory path, uint[] calldata zkp) public view returns (bool) {
|
61
|
-
uint[] memory value = validateQuery(path, zkp);
|
62
|
-
return _qBool(value);
|
63
|
-
}
|
64
|
-
|
65
|
-
function qNull (uint[] memory path, uint[] calldata zkp) public view returns (bool) {
|
66
|
-
uint[] memory value = validateQuery(path, zkp);
|
67
|
-
return _qNull(value);
|
68
|
-
}
|
69
|
-
|
70
|
-
}
|