zkjson 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- package/circomlibjs.js +22 -3
- package/collection.js +8 -3
- package/contracts/ZKDB.sol +70 -0
- package/contracts/ZKJson.sol +17 -0
- package/contracts/ZKQuery.sol +338 -0
- package/contracts/ZKRollup.sol +57 -0
- package/db.js +31 -19
- package/doc.js +5 -4
- package/json.js +1 -1
- package/package.json +1 -1
package/circomlibjs.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict"
|
2
2
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true })
|
4
|
-
|
4
|
+
const { splitEvery } = require("ramda")
|
5
5
|
var ffjavascript = require("ffjavascript")
|
6
6
|
var blake2b = require("blake2b")
|
7
7
|
var createBlakeHash = require("blake-hash")
|
@@ -40201,9 +40201,18 @@ class SMT {
|
|
40201
40201
|
|
40202
40202
|
async update(_key, _newValue) {
|
40203
40203
|
const poseidon = await buildPoseidon()
|
40204
|
+
let _hash_value = _newValue
|
40205
|
+
if (_newValue.length === 256) {
|
40206
|
+
_hash_value = []
|
40207
|
+
for (let v of splitEvery(16, _newValue)) {
|
40208
|
+
const poseidon = await buildPoseidon()
|
40209
|
+
const value = poseidon(v)
|
40210
|
+
_hash_value.push(value)
|
40211
|
+
}
|
40212
|
+
}
|
40213
|
+
const newValue = poseidon(_hash_value)
|
40204
40214
|
const F = this.F
|
40205
40215
|
const key = F.e(_key)
|
40206
|
-
const newValue = poseidon(_newValue)
|
40207
40216
|
const resFind = await this.find(key)
|
40208
40217
|
const res = {}
|
40209
40218
|
res.oldRoot = this.root
|
@@ -40338,9 +40347,19 @@ class SMT {
|
|
40338
40347
|
|
40339
40348
|
async insert(_key, _value) {
|
40340
40349
|
const poseidon = await buildPoseidon()
|
40350
|
+
let _hash_value = _value
|
40351
|
+
if (_value.length === 256) {
|
40352
|
+
_hash_value = []
|
40353
|
+
for (let v of splitEvery(16, _value)) {
|
40354
|
+
const poseidon = await buildPoseidon()
|
40355
|
+
const value = poseidon(v)
|
40356
|
+
_hash_value.push(value)
|
40357
|
+
}
|
40358
|
+
}
|
40359
|
+
|
40360
|
+
const value = poseidon(_hash_value)
|
40341
40361
|
const F = this.F
|
40342
40362
|
const key = F.e(_key)
|
40343
|
-
const value = poseidon(_value)
|
40344
40363
|
let addedOne = false
|
40345
40364
|
const res = {}
|
40346
40365
|
res.oldRoot = this.root
|
package/collection.js
CHANGED
@@ -3,11 +3,16 @@ const { pad, str2val, val2str, id2str, encode, str2id } = require("./encoder")
|
|
3
3
|
const Doc = require("./doc")
|
4
4
|
|
5
5
|
class Collection {
|
6
|
-
constructor({
|
7
|
-
this.
|
6
|
+
constructor({ size_val = 5, size_path = 5, size_json = 16, level = 100 }) {
|
7
|
+
this.size_val = size_val
|
8
|
+
this.size_path = size_path
|
8
9
|
this.size_json = size_json
|
9
10
|
this.level = level
|
10
|
-
this.doc = new Doc({
|
11
|
+
this.doc = new Doc({
|
12
|
+
size_val: this.size_val,
|
13
|
+
size_path: this.size_path,
|
14
|
+
size_json: this.size_json,
|
15
|
+
})
|
11
16
|
}
|
12
17
|
async getInputs({ id, json, path, val }) {
|
13
18
|
const doc_inputs = await this.doc.getInputs({ json, path, val })
|
@@ -0,0 +1,70 @@
|
|
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
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "hardhat/console.sol";
|
5
|
+
import "./ZKQuery.sol";
|
6
|
+
|
7
|
+
contract ZKJson is ZKQuery{
|
8
|
+
|
9
|
+
function _validateQueryJSON(uint[] memory path, uint[] calldata zkp, uint size) public pure returns(uint[] memory){
|
10
|
+
require(zkp[8] == 1, "value doesn't exist");
|
11
|
+
for(uint i = 10; i < 15; i++) require(path[i - 10] == zkp[i], "wrong path");
|
12
|
+
uint[] memory value = new uint[](size);
|
13
|
+
for(uint i = 15; i < 20; i++) value[i - 15] = zkp[i];
|
14
|
+
return toArr(value);
|
15
|
+
}
|
16
|
+
|
17
|
+
}
|
@@ -0,0 +1,338 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "hardhat/console.sol";
|
5
|
+
|
6
|
+
contract ZKQuery {
|
7
|
+
|
8
|
+
function digits (uint x) private pure returns(uint) {
|
9
|
+
uint p = 0;
|
10
|
+
while(x > 0){
|
11
|
+
x /= 10;
|
12
|
+
p++;
|
13
|
+
}
|
14
|
+
return p;
|
15
|
+
}
|
16
|
+
|
17
|
+
function getValLen(uint[] memory path, uint[] memory _json) private pure returns(uint, uint){
|
18
|
+
require (_json[0] == 4, "not raw value");
|
19
|
+
uint i = 1;
|
20
|
+
uint start;
|
21
|
+
uint[] memory path2 = toArr(path);
|
22
|
+
uint vallen;
|
23
|
+
while(i < _json.length){
|
24
|
+
start = i;
|
25
|
+
uint pathlen = getPathLen(i, _json);
|
26
|
+
uint[] memory _path = new uint[](pathlen);
|
27
|
+
uint len = _json[i];
|
28
|
+
i++;
|
29
|
+
_path[0] = len;
|
30
|
+
uint pi = 1;
|
31
|
+
for(uint i2=0;i2 < len; i2++){
|
32
|
+
uint plen = _json[i];
|
33
|
+
_path[pi] = plen;
|
34
|
+
pi++;
|
35
|
+
i++;
|
36
|
+
uint plen2 = plen;
|
37
|
+
if(plen == 0){
|
38
|
+
plen2 = _json[i] == 0 ? 2 : 1;
|
39
|
+
}
|
40
|
+
for(uint i3 = 0; i3 < plen2; i3++){
|
41
|
+
_path[pi] = _json[i];
|
42
|
+
pi++;
|
43
|
+
i++;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
uint _type = _json[i];
|
47
|
+
i++;
|
48
|
+
uint vlen = 1;
|
49
|
+
if(_type == 1){
|
50
|
+
vlen++;
|
51
|
+
i++;
|
52
|
+
}else if (_type == 2){
|
53
|
+
vlen += 3;
|
54
|
+
i += 3;
|
55
|
+
}else if(_type == 3){
|
56
|
+
uint slen = _json[i];
|
57
|
+
vlen += slen + 1;
|
58
|
+
i += slen + 1;
|
59
|
+
}
|
60
|
+
uint path_match = 1;
|
61
|
+
if(pathlen != path2.length){
|
62
|
+
path_match = 0;
|
63
|
+
}else{
|
64
|
+
for(uint i4 = 0; i4 < path2.length; i4++){
|
65
|
+
if(_path[i4] != path2[i4]) path_match = 0;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
if(path_match == 1){
|
69
|
+
vallen = vlen;
|
70
|
+
break;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
return (vallen, start);
|
74
|
+
}
|
75
|
+
|
76
|
+
function getPathLen(uint i, uint[] memory _json) private pure returns(uint){
|
77
|
+
uint len = _json[i];
|
78
|
+
i++;
|
79
|
+
uint pi = 1;
|
80
|
+
for(uint i2=0;i2 < len; i2++){
|
81
|
+
uint plen = _json[i];
|
82
|
+
pi++;
|
83
|
+
i++;
|
84
|
+
uint plen2 = plen;
|
85
|
+
if(plen == 0) plen2 = _json[i] == 0 ? 2 : 1;
|
86
|
+
pi += plen2;
|
87
|
+
i += plen2;
|
88
|
+
}
|
89
|
+
return pi;
|
90
|
+
}
|
91
|
+
|
92
|
+
function getVal(uint[] memory path, uint[] memory _json) private pure returns(uint[] memory){
|
93
|
+
require (_json[0] == 4, "not raw value");
|
94
|
+
(uint vallen, uint i) = getValLen(path, _json);
|
95
|
+
uint[] memory val = new uint[](vallen);
|
96
|
+
uint[] memory path2 = toArr(path);
|
97
|
+
while(i < _json.length){
|
98
|
+
uint pathlen = getPathLen(i, _json);
|
99
|
+
uint[] memory _path = new uint[](pathlen);
|
100
|
+
uint len = _json[i];
|
101
|
+
i++;
|
102
|
+
_path[0] = len;
|
103
|
+
uint pi = 1;
|
104
|
+
for(uint i2=0;i2 < len; i2++){
|
105
|
+
uint plen = _json[i];
|
106
|
+
_path[pi] = plen;
|
107
|
+
pi++;
|
108
|
+
i++;
|
109
|
+
uint plen2 = plen;
|
110
|
+
if(plen == 0){
|
111
|
+
plen2 = _json[i] == 0 ? 2 : 1;
|
112
|
+
}
|
113
|
+
for(uint i3 = 0; i3 < plen2; i3++){
|
114
|
+
_path[pi] = _json[i];
|
115
|
+
pi++;
|
116
|
+
i++;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
uint _type = _json[i];
|
121
|
+
i++;
|
122
|
+
uint[] memory _val = new uint[](vallen);
|
123
|
+
_val[0] = _type;
|
124
|
+
if(_type == 1){
|
125
|
+
_val[1] = _json[i];
|
126
|
+
i++;
|
127
|
+
}else if (_type == 2){
|
128
|
+
_val[1] = _json[i];
|
129
|
+
i++;
|
130
|
+
_val[2] = _json[i];
|
131
|
+
i++;
|
132
|
+
_val[3] = _json[i];
|
133
|
+
i++;
|
134
|
+
}else if(_type == 3){
|
135
|
+
uint slen = _json[i];
|
136
|
+
_val[1] = slen;
|
137
|
+
i++;
|
138
|
+
for(uint i3 = 0;i3 < slen; i3++){
|
139
|
+
_val[i3 + 2] = _json[i];
|
140
|
+
i++;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
uint path_match = 1;
|
144
|
+
if(pathlen != path2.length){
|
145
|
+
path_match = 0;
|
146
|
+
}else{
|
147
|
+
for(uint i4 = 0; i4 < path2.length; i4++){
|
148
|
+
if(_path[i4] != path2[i4]) path_match = 0;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
if(path_match == 1){
|
152
|
+
val = _val;
|
153
|
+
break;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
return val;
|
157
|
+
}
|
158
|
+
|
159
|
+
function getLen(uint[] memory json) private pure returns(uint, uint){
|
160
|
+
uint ji = 0;
|
161
|
+
uint prev = 0;
|
162
|
+
uint jlen = 0;
|
163
|
+
for(uint j = 0; j < json.length; j++){
|
164
|
+
if(json[j] > 0){
|
165
|
+
jlen = j + 1;
|
166
|
+
uint p = digits(json[j]);
|
167
|
+
uint x = json[j];
|
168
|
+
uint on = 0;
|
169
|
+
uint cur = 0;
|
170
|
+
uint len = 0;
|
171
|
+
uint num = 0;
|
172
|
+
uint is9 = 0;
|
173
|
+
while(p > 0){
|
174
|
+
uint n = x / 10 ** (p - 1);
|
175
|
+
if(on == 0 && n > 0){
|
176
|
+
on = 1;
|
177
|
+
if(n == 9){
|
178
|
+
len = 8;
|
179
|
+
is9 = 0;
|
180
|
+
}else{
|
181
|
+
len = n;
|
182
|
+
}
|
183
|
+
cur = 0;
|
184
|
+
}else if(on == 1){
|
185
|
+
num += n * 10 ** (len - cur - 1);
|
186
|
+
cur++;
|
187
|
+
if(cur == len){
|
188
|
+
prev *= 10 ** len;
|
189
|
+
if(is9 == 1){
|
190
|
+
prev += num;
|
191
|
+
}else{
|
192
|
+
num += prev;
|
193
|
+
prev = 0;
|
194
|
+
ji++;
|
195
|
+
}
|
196
|
+
cur = 0;
|
197
|
+
on = 0;
|
198
|
+
len = 0;
|
199
|
+
num = 0;
|
200
|
+
is9 = 0;
|
201
|
+
}
|
202
|
+
}
|
203
|
+
x -= 10 ** (p - 1) * n;
|
204
|
+
p--;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
208
|
+
return (ji, jlen);
|
209
|
+
}
|
210
|
+
|
211
|
+
function toArr(uint[] memory json) internal pure returns(uint[] memory){
|
212
|
+
(uint _len, uint _jlen) = getLen(json);
|
213
|
+
uint[] memory _json = new uint[](_len);
|
214
|
+
uint ji = 0;
|
215
|
+
uint prev = 0;
|
216
|
+
for(uint j = 0; j < _jlen; j++){
|
217
|
+
uint p = digits(json[j]);
|
218
|
+
uint x = json[j];
|
219
|
+
uint on = 0;
|
220
|
+
uint cur = 0;
|
221
|
+
uint len = 0;
|
222
|
+
uint num = 0;
|
223
|
+
uint is9 = 0;
|
224
|
+
while(p > 0){
|
225
|
+
uint n = x / 10 ** (p - 1);
|
226
|
+
if(on == 0 && n > 0){
|
227
|
+
on = 1;
|
228
|
+
if(n == 9){
|
229
|
+
len = 8;
|
230
|
+
is9 = 0;
|
231
|
+
}else{
|
232
|
+
len = n;
|
233
|
+
}
|
234
|
+
cur = 0;
|
235
|
+
}else if(on == 1){
|
236
|
+
num += n * 10 ** (len - cur - 1);
|
237
|
+
cur++;
|
238
|
+
if(cur == len){
|
239
|
+
prev *= 10 ** len;
|
240
|
+
if(is9 == 1){
|
241
|
+
prev += num;
|
242
|
+
}else{
|
243
|
+
num += prev;
|
244
|
+
prev = 0;
|
245
|
+
_json[ji] = num;
|
246
|
+
ji++;
|
247
|
+
}
|
248
|
+
cur = 0;
|
249
|
+
on = 0;
|
250
|
+
len = 0;
|
251
|
+
num = 0;
|
252
|
+
is9 = 0;
|
253
|
+
}
|
254
|
+
}
|
255
|
+
x -= 10 ** (p - 1) * n;
|
256
|
+
p--;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
return _json;
|
260
|
+
}
|
261
|
+
|
262
|
+
function _toString(uint8[] memory charCodes) private pure returns (string memory) {
|
263
|
+
bytes memory stringBytes = new bytes(charCodes.length);
|
264
|
+
for (uint i = 0; i < charCodes.length; i++) stringBytes[i] = bytes1(charCodes[i]);
|
265
|
+
return string(stringBytes);
|
266
|
+
}
|
267
|
+
|
268
|
+
function _qInt (uint[] memory value) internal pure returns (int) {
|
269
|
+
require(value[0] == 2 && value[2] == 0, "not int");
|
270
|
+
return int(value[3]) * (value[1] == 1 ? int(1) : int(-1));
|
271
|
+
}
|
272
|
+
|
273
|
+
function _qFloat (uint[] memory value) internal pure returns (uint[3] memory) {
|
274
|
+
require(value[0] == 2 && value[2] == 1, "not float");
|
275
|
+
uint[3] memory float;
|
276
|
+
float[0] = value[1];
|
277
|
+
float[1] = value[2];
|
278
|
+
float[2] = value[3];
|
279
|
+
return float;
|
280
|
+
}
|
281
|
+
|
282
|
+
function _qRaw (uint[] memory value) internal pure returns (uint[] memory) {
|
283
|
+
require(value[0] == 4, "not object or array");
|
284
|
+
return value;
|
285
|
+
}
|
286
|
+
|
287
|
+
function _qString (uint[] memory value) internal pure returns (string memory) {
|
288
|
+
require(value[0] == 3, "not string");
|
289
|
+
uint8[] memory charCodes = new uint8[](value[1]);
|
290
|
+
for(uint i = 0; i < value[1];i++) charCodes[i] = uint8(value[i+2]);
|
291
|
+
string memory str = _toString(charCodes);
|
292
|
+
return str;
|
293
|
+
}
|
294
|
+
|
295
|
+
function _qBool (uint[] memory value) internal pure returns (bool) {
|
296
|
+
require(value[0] == 1, "not bool");
|
297
|
+
return value[1] == 1 ? true : false;
|
298
|
+
}
|
299
|
+
|
300
|
+
function _qNull (uint[] memory value) internal pure returns (bool) {
|
301
|
+
require(value[0] == 0, "not null");
|
302
|
+
return true;
|
303
|
+
}
|
304
|
+
|
305
|
+
function _parseZKP(uint[] calldata zkp) internal pure returns (uint[2] memory, uint[2][2] memory, uint[2] memory, uint[] memory) {
|
306
|
+
uint[2] memory _pA;
|
307
|
+
uint[2][2] memory _pB;
|
308
|
+
uint[2] memory _pC;
|
309
|
+
uint[] memory sigs = new uint[](zkp.length - 8);
|
310
|
+
for(uint i = 0; i < 2; i++) _pA[i] = zkp[i];
|
311
|
+
for(uint i = 2; i < 4; i++) _pB[0][i - 2] = zkp[i];
|
312
|
+
for(uint i = 4; i < 6; i++) _pB[1][i - 4] = zkp[i];
|
313
|
+
for(uint i = 6; i < 8; i++) _pC[i - 6] = zkp[i];
|
314
|
+
for(uint i = 8; i < zkp.length; i++) sigs[i - 8] = zkp[i];
|
315
|
+
return (_pA, _pB, _pC, sigs);
|
316
|
+
}
|
317
|
+
|
318
|
+
function getInt (uint[] memory path, uint[] memory raw) public pure returns (int) {
|
319
|
+
uint[] memory value = getVal(path, raw);
|
320
|
+
return _qInt(value);
|
321
|
+
}
|
322
|
+
|
323
|
+
function getString (uint[] memory path, uint[] memory raw) public pure returns (string memory) {
|
324
|
+
uint[] memory value = getVal(path, raw);
|
325
|
+
_qString(value);
|
326
|
+
}
|
327
|
+
|
328
|
+
function getBool (uint[] memory path, uint[] memory raw) public pure returns (bool) {
|
329
|
+
uint[] memory value = getVal(path, raw);
|
330
|
+
_qBool(value);
|
331
|
+
}
|
332
|
+
|
333
|
+
function getNull (uint[] memory path, uint[] memory raw) public pure returns (bool) {
|
334
|
+
uint[] memory value = getVal(path, raw);
|
335
|
+
_qNull(value);
|
336
|
+
}
|
337
|
+
|
338
|
+
}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "hardhat/console.sol";
|
5
|
+
import "./ZKJson.sol";
|
6
|
+
import "./ZKQuery.sol";
|
7
|
+
|
8
|
+
interface VerifierRU {
|
9
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[11] calldata _pubSignals) view external returns (bool);
|
10
|
+
|
11
|
+
}
|
12
|
+
|
13
|
+
contract ZKRollup is ZKQuery {
|
14
|
+
address public verifierRU;
|
15
|
+
address public comitter;
|
16
|
+
uint public root;
|
17
|
+
|
18
|
+
function _verifyRU(uint[] calldata zkp) internal view returns (bool) {
|
19
|
+
uint[2] memory _pA;
|
20
|
+
uint[2][2] memory _pB;
|
21
|
+
uint[2] memory _pC;
|
22
|
+
uint[11] memory sigs;
|
23
|
+
for(uint i = 0; i < 2; i++) _pA[i] = zkp[i];
|
24
|
+
for(uint i = 2; i < 4; i++) _pB[0][i - 2] = zkp[i];
|
25
|
+
for(uint i = 4; i < 6; i++) _pB[1][i - 4] = zkp[i];
|
26
|
+
for(uint i = 6; i < 8; i++) _pC[i - 6] = zkp[i];
|
27
|
+
for(uint i = 8; i < 19; i++) sigs[i - 8] = zkp[i];
|
28
|
+
require(VerifierRU(verifierRU).verifyProof(_pA, _pB, _pC, sigs), "invalid proof");
|
29
|
+
return true;
|
30
|
+
}
|
31
|
+
|
32
|
+
function _validateQueryRU(uint[] memory path, uint[] calldata zkp, uint size) internal view returns(uint[] memory){
|
33
|
+
require(zkp[19] == root, "root mismatch");
|
34
|
+
require(zkp[size * 2 + 10] == path[0], "wrong collection");
|
35
|
+
require(zkp[size * 2 + 11] == path[1], "wrong doc");
|
36
|
+
require(zkp[8] == 1, "value doesn't exist");
|
37
|
+
require(path.length <= size + 2, "path too long");
|
38
|
+
for(uint i = 9; i < 9 + path.length - 2; i++) require(path[i - 7] == zkp[i], "wrong path");
|
39
|
+
uint[] memory value = new uint[](size);
|
40
|
+
for(uint i = 9 + size; i < 9 + size * 2; i++) value[i - (9 + size)] = zkp[i];
|
41
|
+
return toArr(value);
|
42
|
+
}
|
43
|
+
|
44
|
+
function commit (uint[] calldata zkp) public returns (uint) {
|
45
|
+
require (zkp[9] == root, "wrong merkle root");
|
46
|
+
require(msg.sender == comitter, "sender is not comitter");
|
47
|
+
root = zkp[8];
|
48
|
+
verifyRU(zkp);
|
49
|
+
return root;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
function verifyRU(uint[] calldata zkp) public view returns (bool) {
|
54
|
+
return _verifyRU(zkp);
|
55
|
+
}
|
56
|
+
|
57
|
+
}
|
package/db.js
CHANGED
@@ -12,19 +12,29 @@ const {
|
|
12
12
|
const Collection = require("./collection")
|
13
13
|
|
14
14
|
class DB {
|
15
|
-
constructor({
|
16
|
-
|
15
|
+
constructor({
|
16
|
+
size_val = 5,
|
17
|
+
size_path = 5,
|
18
|
+
level = 100,
|
19
|
+
size_json = 256,
|
20
|
+
size_txs = 10,
|
21
|
+
level_col = 8,
|
22
|
+
}) {
|
23
|
+
this.level_col = level_col
|
24
|
+
this.size = size_val
|
25
|
+
this.size_path = size_path
|
17
26
|
this.level = level
|
18
27
|
this.size_json = size_json
|
19
28
|
this.size_txs = size_txs
|
29
|
+
this.count = 0
|
20
30
|
}
|
21
31
|
|
22
32
|
async init() {
|
23
33
|
this.tree = await newMemEmptyTrie()
|
24
|
-
this.cols =
|
34
|
+
this.cols = []
|
25
35
|
}
|
26
36
|
|
27
|
-
parse(res, tree) {
|
37
|
+
parse(res, tree, level) {
|
28
38
|
const isOld0 = res.isOld0 ? "1" : "0"
|
29
39
|
const oldRoot = tree.F.toObject(res.oldRoot).toString()
|
30
40
|
const newRoot = tree.F.toObject(res.newRoot).toString()
|
@@ -33,7 +43,7 @@ class DB {
|
|
33
43
|
let siblings = res.siblings
|
34
44
|
for (let i = 0; i < siblings.length; i++)
|
35
45
|
siblings[i] = tree.F.toObject(siblings[i])
|
36
|
-
while (siblings.length <
|
46
|
+
while (siblings.length < level) siblings.push(0)
|
37
47
|
siblings = siblings.map(s => s.toString())
|
38
48
|
return { isOld0, oldRoot, oldKey, oldValue, siblings, newRoot }
|
39
49
|
}
|
@@ -71,7 +81,7 @@ class DB {
|
|
71
81
|
oldRoot_db.push(newRoot_db[i - 1])
|
72
82
|
oldKey_db.push("0")
|
73
83
|
oldValue_db.push("0")
|
74
|
-
siblings_db.push(range(0, this.
|
84
|
+
siblings_db.push(range(0, this.level_col).map(() => "0"))
|
75
85
|
isOld0_db.push("0")
|
76
86
|
newKey_db.push("0")
|
77
87
|
newKey.push("0")
|
@@ -79,12 +89,12 @@ class DB {
|
|
79
89
|
}
|
80
90
|
_json = v[2]
|
81
91
|
const { update, tree, col: res2, doc: res } = await this.insert(...v)
|
82
|
-
const icol = this.parse(res, tree)
|
83
|
-
const idb = this.parse(res2, this.tree)
|
92
|
+
const icol = this.parse(res, tree, this.level)
|
93
|
+
const idb = this.parse(res2, this.tree, this.level_col)
|
84
94
|
_res = idb
|
85
95
|
const _newKey = str2id(v[1])
|
86
96
|
json.push(pad(val2str(encode(_json)), this.size_json))
|
87
|
-
const _newKey_db =
|
97
|
+
const _newKey_db = v[0]
|
88
98
|
fnc.push(update ? [0, 1] : [1, 0])
|
89
99
|
newRoot.push(idb.newRoot)
|
90
100
|
oldRoot.push(icol.oldRoot)
|
@@ -128,10 +138,10 @@ class DB {
|
|
128
138
|
col: res2,
|
129
139
|
doc: res,
|
130
140
|
} = await this.insert(col_id, id, json)
|
131
|
-
const icol = this.parse(res, tree)
|
132
|
-
const idb = this.parse(res2, this.tree)
|
141
|
+
const icol = this.parse(res, tree, this.level)
|
142
|
+
const idb = this.parse(res2, this.tree, this.level_col)
|
133
143
|
const newKey = str2id(id)
|
134
|
-
const newKey_db =
|
144
|
+
const newKey_db = col_id
|
135
145
|
return {
|
136
146
|
fnc: update ? [0, 1] : [1, 0],
|
137
147
|
oldRoot: icol.oldRoot,
|
@@ -158,9 +168,9 @@ class DB {
|
|
158
168
|
let col_siblings = col_res.siblings
|
159
169
|
for (let i = 0; i < col_siblings.length; i++)
|
160
170
|
col_siblings[i] = this.tree.F.toObject(col_siblings[i])
|
161
|
-
while (col_siblings.length < this.
|
171
|
+
while (col_siblings.length < this.level_col) col_siblings.push(0)
|
162
172
|
col_siblings = col_siblings.map(s => s.toString())
|
163
|
-
const col_key =
|
173
|
+
const col_key = col_id
|
164
174
|
const col = this.getColTree(col_id)
|
165
175
|
const col_inputs = await col.getInputs({ id, json, path, val })
|
166
176
|
return {
|
@@ -176,19 +186,21 @@ class DB {
|
|
176
186
|
}
|
177
187
|
}
|
178
188
|
|
179
|
-
async addCollection(
|
180
|
-
const id =
|
189
|
+
async addCollection() {
|
190
|
+
const id = this.count++
|
181
191
|
const col = await this.tree.find(id)
|
182
192
|
if (col.found) throw Error("collection exists")
|
183
193
|
const _col = new Collection({
|
184
|
-
|
194
|
+
size_val: this.size_val,
|
195
|
+
size_path: this.size_path,
|
185
196
|
level: this.level,
|
186
197
|
size_json: this.size_json,
|
187
198
|
})
|
188
199
|
await _col.init()
|
189
|
-
this.cols[
|
200
|
+
this.cols[id] = _col
|
190
201
|
const root = _col.tree.F.toObject(_col.tree.root).toString()
|
191
202
|
await this.tree.insert(id, [root])
|
203
|
+
return id
|
192
204
|
}
|
193
205
|
getColTree(col) {
|
194
206
|
const _col = this.cols[col]
|
@@ -210,7 +222,7 @@ class DB {
|
|
210
222
|
}
|
211
223
|
async updateDB(_col, col) {
|
212
224
|
const root = _col.tree.F.toObject(_col.tree.root).toString()
|
213
|
-
const colD =
|
225
|
+
const colD = col
|
214
226
|
return await this.tree.update(colD, [root])
|
215
227
|
}
|
216
228
|
async update(col, _key, _val) {
|
package/doc.js
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
const { pad, toSignal, encode, encodePath, encodeVal } = require("./encoder")
|
2
2
|
|
3
3
|
module.exports = class Doc {
|
4
|
-
constructor({
|
5
|
-
this.
|
4
|
+
constructor({ size_val = 5, size_path = 5, size_json = 256 }) {
|
5
|
+
this.size_val = size_val
|
6
|
+
this.size_path = size_path
|
6
7
|
this.size_json = size_json
|
7
8
|
}
|
8
9
|
async getInputs({ json, path, val }) {
|
9
10
|
return {
|
10
11
|
json: pad(toSignal(encode(json)), this.size_json),
|
11
|
-
path: pad(toSignal(encodePath(path)), this.
|
12
|
-
val: pad(toSignal(encodeVal(val)), this.
|
12
|
+
path: pad(toSignal(encodePath(path)), this.size_path),
|
13
|
+
val: pad(toSignal(encodeVal(val)), this.size_val),
|
13
14
|
}
|
14
15
|
}
|
15
16
|
}
|
package/json.js
CHANGED