zkjson 0.6.1 → 1.0.2
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/README.md +537 -0
- package/assets/collection.png +0 -0
- package/assets/db.png +0 -0
- package/assets/encode.png +0 -0
- package/assets/query.png +0 -0
- package/assets/rollup.png +0 -0
- package/assets/structure.png +0 -0
- package/assets/weavedb.png +0 -0
- package/assets/zkjson.png +0 -0
- package/benchmark/components/ui/avatar.jsx +49 -0
- package/benchmark/components/ui/checkbox.jsx +17 -0
- package/benchmark/components/ui/close-button.jsx +20 -0
- package/benchmark/components/ui/color-mode.jsx +89 -0
- package/benchmark/components/ui/dialog.jsx +54 -0
- package/benchmark/components/ui/drawer.jsx +44 -0
- package/benchmark/components/ui/field.jsx +22 -0
- package/benchmark/components/ui/input-group.jsx +39 -0
- package/benchmark/components/ui/popover.jsx +49 -0
- package/benchmark/components/ui/provider.jsx +12 -0
- package/benchmark/components/ui/radio.jsx +17 -0
- package/benchmark/components/ui/slider.jsx +107 -0
- package/benchmark/components/ui/tooltip.jsx +35 -0
- package/benchmark/jsconfig.json +7 -0
- package/benchmark/lib/cbor.js +387 -0
- package/benchmark/lib/msgpack.js +651 -0
- package/benchmark/next.config.mjs +6 -0
- package/benchmark/package.json +22 -0
- package/benchmark/pages/_app.js +9 -0
- package/benchmark/pages/_document.js +13 -0
- package/benchmark/pages/api/hello.js +5 -0
- package/benchmark/pages/index.js +438 -0
- package/benchmark/public/favicon.ico +0 -0
- package/benchmark/public/file.svg +1 -0
- package/benchmark/public/globe.svg +1 -0
- package/benchmark/public/next.svg +1 -0
- package/benchmark/public/vercel.svg +1 -0
- package/benchmark/public/window.svg +1 -0
- package/benchmark/yarn.lock +2448 -0
- package/circom/collection/collection.circom +32 -0
- package/circom/collection/compile.sh +10 -0
- package/circom/collection/gen.js +34 -0
- package/circom/collection/generateInput.js +10 -0
- package/circom/collection/index.circom +4 -0
- package/circom/collection/test.js +21 -0
- package/circom/collection/verifier.sol +247 -0
- package/circom/db/compile.sh +10 -0
- package/circom/db/db.circom +39 -0
- package/circom/db/gen.js +30 -0
- package/circom/db/generateInput.js +10 -0
- package/circom/db/index.circom +4 -0
- package/circom/db/test.js +21 -0
- package/circom/db/verifier.sol +261 -0
- package/circom/ipfs/calculate_total.circom +17 -0
- package/circom/ipfs/compile.sh +10 -0
- package/circom/ipfs/gen.js +18 -0
- package/circom/ipfs/generateInput.js +10 -0
- package/circom/ipfs/index.circom +4 -0
- package/circom/ipfs/ipfs.circom +62 -0
- package/circom/ipfs/parse.circom +289 -0
- package/circom/ipfs/sha256.circom +99 -0
- package/circom/ipfs/test.js +167 -0
- package/circom/ipfs/verifier.sol +261 -0
- package/circom/json/compile.sh +10 -0
- package/circom/json/gen.js +22 -0
- package/circom/json/gen2.js +21 -0
- package/circom/json/generateInput.js +20 -0
- package/circom/json/index.circom +4 -0
- package/circom/json/json.circom +81 -0
- package/circom/json/test.js +100 -0
- package/circom/json/verifier.sol +247 -0
- package/circom/query/compile.sh +10 -0
- package/circom/query/gen.js +28 -0
- package/circom/query/generateInput.js +11 -0
- package/circom/query/index.circom +4 -0
- package/circom/query/query.circom +58 -0
- package/circom/query/test.js +20 -0
- package/circom/query/verifier.sol +177 -0
- package/circom/rollup/compile.sh +10 -0
- package/circom/rollup/gen.js +36 -0
- package/circom/rollup/generateInput.js +11 -0
- package/circom/rollup/index.circom +4 -0
- package/circom/rollup/rollup.circom +43 -0
- package/circom/rollup/test.js +35 -0
- package/circom/rollup/verifier.sol +240 -0
- package/circom/scripts/ceremony.js +39 -0
- package/circom/scripts/ceremony.sh +6 -0
- package/circom/scripts/compile.js +113 -0
- package/circom/scripts/compile.sh +16 -0
- package/circom/utils/uint.circom +512 -0
- package/circom/utils/utils.circom +628 -0
- package/demos/arweave/jsconfig.json +7 -0
- package/demos/arweave/lib/ZKArweave.json +617 -0
- package/demos/arweave/lib/encoder.js +375 -0
- package/demos/arweave/next.config.js +6 -0
- package/demos/arweave/package.json +26 -0
- package/demos/arweave/pages/_app.js +9 -0
- package/demos/arweave/pages/_document.js +19 -0
- package/demos/arweave/pages/api/arweave.js +31 -0
- package/demos/arweave/pages/api/hello.js +5 -0
- package/demos/arweave/pages/index.js +869 -0
- package/demos/arweave/public/circuit.wasm +0 -0
- package/demos/arweave/public/circuit_final.zkey +0 -0
- package/demos/arweave/public/verification_key.json +149 -0
- package/demos/arweave/vercel.json +7 -0
- package/demos/arweave/yarn.lock +2510 -0
- package/demos/simple/jsconfig.json +7 -0
- package/demos/simple/lib/encoder.js +375 -0
- package/demos/simple/next.config.js +6 -0
- package/demos/simple/package.json +24 -0
- package/demos/simple/pages/_app.js +9 -0
- package/demos/simple/pages/_document.js +13 -0
- package/demos/simple/pages/api/hello.js +5 -0
- package/demos/simple/pages/index.js +342 -0
- package/demos/simple/public/circuit.wasm +0 -0
- package/demos/simple/public/circuit_final.zkey +0 -0
- package/demos/simple/public/verification_key.json +149 -0
- package/demos/simple/yarn.lock +2519 -0
- package/docs/README.md +17 -0
- package/docs/simple-zkjson.md +210 -0
- package/docs/solidity-contracts.md +216 -0
- package/docs/zk-circuits.md +125 -0
- package/docs/zkdb-rollup.md +218 -0
- package/docs/zkjson-sdk.md +254 -0
- package/docs/zkjson-v1_5.md +365 -0
- package/package.json +16 -7
- package/{contracts → sdk/contracts}/NORollup.sol +0 -2
- package/sdk/package.json +15 -0
- package/sdk/yarn.lock +881 -0
- package/solidity/README.md +13 -0
- package/solidity/arguments.js +4 -0
- package/solidity/contracts/NORollup.sol +13 -0
- package/solidity/contracts/OPRollup.sol +14 -0
- package/solidity/contracts/ZKIPFS.sol +95 -0
- package/solidity/contracts/ZKJson.sol +21 -0
- package/solidity/contracts/ZKQuery.sol +286 -0
- package/solidity/contracts/ZKRollup.sol +35 -0
- package/solidity/contracts/apps/NORU.sol +66 -0
- package/solidity/contracts/apps/SimpleJSON.sol +64 -0
- package/solidity/contracts/apps/SimpleOPRU.sol +67 -0
- package/solidity/contracts/apps/SimpleRU.sol +67 -0
- package/solidity/contracts/apps/Token.sol +12 -0
- package/solidity/contracts/apps/ZKArweave.sol +89 -0
- package/solidity/contracts/apps/ZKBridge.sol +74 -0
- package/solidity/contracts/apps/ZKNFT.sol +63 -0
- package/solidity/contracts/verifiers/verifier_db.sol +275 -0
- package/solidity/contracts/verifiers/verifier_ipfs.sol +464 -0
- package/solidity/contracts/verifiers/verifier_json.sol +261 -0
- package/solidity/contracts/verifiers/verifier_rollup.sol +240 -0
- package/solidity/package.json +27 -0
- package/solidity/scripts/deploy.js +33 -0
- package/solidity/test/NORU.js +66 -0
- package/solidity/test/ZKArweave.js +97 -0
- package/solidity/test/arweave.js +55 -0
- package/solidity/test/bridge.js +71 -0
- package/solidity/test/simple.js +76 -0
- package/solidity/test/simpleOPRU.js +98 -0
- package/solidity/test/simpleRU.js +94 -0
- package/solidity/test/zknft.js +98 -0
- package/solidity/yarn.lock +4152 -0
- package/test/test.js +304 -0
- /package/{circomlibjs.js → sdk/circomlibjs.js} +0 -0
- /package/{collection.js → sdk/collection.js} +0 -0
- /package/{contracts → sdk/contracts}/OPRollup.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKIPFS.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKJson.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKQuery.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKRollup.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/SimpleJSON.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/SimpleOPRU.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/SimpleRU.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/Token.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/ZKArweave.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/ZKBridge.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/ZKNFT.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_db.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_ipfs.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_json.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_rollup.sol +0 -0
- /package/{db.js → sdk/db.js} +0 -0
- /package/{doc.js → sdk/doc.js} +0 -0
- /package/{encoder-v1_5.js → sdk/encoder-v1_5.js} +0 -0
- /package/{encoder.js → sdk/encoder.js} +0 -0
- /package/{index.js → sdk/index.js} +0 -0
- /package/{json.js → sdk/json.js} +0 -0
- /package/{nft.js → sdk/nft.js} +0 -0
- /package/{parse.js → sdk/parse.js} +0 -0
- /package/{uint.js → sdk/uint.js} +0 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
# Sample Hardhat Project
|
2
|
+
|
3
|
+
This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.
|
4
|
+
|
5
|
+
Try running some of the following tasks:
|
6
|
+
|
7
|
+
```shell
|
8
|
+
npx hardhat help
|
9
|
+
npx hardhat test
|
10
|
+
REPORT_GAS=true npx hardhat test
|
11
|
+
npx hardhat node
|
12
|
+
npx hardhat run scripts/deploy.js
|
13
|
+
```
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "./ZKQuery.sol";
|
5
|
+
|
6
|
+
contract NORollup is ZKQuery {
|
7
|
+
function _validateQuery(uint[] memory zkp, uint size_path, uint size_val) internal view returns(uint[] memory){
|
8
|
+
require(zkp[8] == 1, "value doesn't exist");
|
9
|
+
uint[] memory value = new uint[](size_val);
|
10
|
+
for(uint i = 9 + size_path; i < 9 + size_path + size_val; i++) value[i - (9 + size_path)] = zkp[i];
|
11
|
+
return toArr(value);
|
12
|
+
}
|
13
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "./ZKRollup.sol";
|
5
|
+
|
6
|
+
|
7
|
+
contract OPRollup is ZKRollup {
|
8
|
+
|
9
|
+
function commitRoot (uint _root) public returns (uint) {
|
10
|
+
require(msg.sender == committer, "sender is not committer");
|
11
|
+
root = _root;
|
12
|
+
return root;
|
13
|
+
}
|
14
|
+
}
|
@@ -0,0 +1,95 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "./ZKQuery.sol";
|
5
|
+
|
6
|
+
interface VerifierIPFS {
|
7
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[43] calldata _pubSignals) external view returns (bool);
|
8
|
+
}
|
9
|
+
|
10
|
+
contract ZKIPFS is ZKQuery{
|
11
|
+
uint constant SIZE_PATH = 5;
|
12
|
+
uint constant SIZE_VAL = 5;
|
13
|
+
address public verifierIPFS;
|
14
|
+
bytes constant ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
15
|
+
|
16
|
+
function charArrayToString(bytes1[46] memory charArray) public pure returns (string memory) {
|
17
|
+
bytes memory byteArray = new bytes(charArray.length);
|
18
|
+
for(uint i = 0; i < charArray.length; i++) {
|
19
|
+
byteArray[i] = charArray[i];
|
20
|
+
}
|
21
|
+
return string(byteArray);
|
22
|
+
}
|
23
|
+
|
24
|
+
function concat(string memory a, string memory b) public pure returns (string memory) {
|
25
|
+
return string(abi.encodePacked(a, b));
|
26
|
+
}
|
27
|
+
|
28
|
+
function uriEqual(string memory a, string memory b) public pure returns (bool) {
|
29
|
+
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
|
30
|
+
}
|
31
|
+
|
32
|
+
function toCID(uint[34] memory source) public pure returns (string memory) {
|
33
|
+
uint zeroes = 0;
|
34
|
+
uint length = 0;
|
35
|
+
uint pbegin = 0;
|
36
|
+
uint pend = source.length;
|
37
|
+
while (pbegin != pend && source[pbegin] == 0) {
|
38
|
+
pbegin++;
|
39
|
+
zeroes++;
|
40
|
+
}
|
41
|
+
uint size = 46;
|
42
|
+
uint[46] memory b58;
|
43
|
+
while (pbegin != pend) {
|
44
|
+
uint carry = source[pbegin];
|
45
|
+
uint i = 0;
|
46
|
+
for ( uint it1 = size - 1; (carry != 0 || i < length); it1-- ) {
|
47
|
+
carry += (256 * b58[it1]);
|
48
|
+
b58[it1] = carry % 58;
|
49
|
+
carry = (carry / 58);
|
50
|
+
i++;
|
51
|
+
if(it1 == 0) break;
|
52
|
+
}
|
53
|
+
length = i;
|
54
|
+
pbegin++;
|
55
|
+
}
|
56
|
+
uint it2 = size - length;
|
57
|
+
while (it2 != size && b58[it2] == 0) it2++;
|
58
|
+
bytes1[46] memory str;
|
59
|
+
uint i = 0;
|
60
|
+
for (; it2 < size; ++it2){
|
61
|
+
str[i] = ALPHABET[b58[it2]];
|
62
|
+
i++;
|
63
|
+
}
|
64
|
+
return charArrayToString(str);
|
65
|
+
}
|
66
|
+
|
67
|
+
function ipfsURI(uint[34] memory source) public pure returns (string memory) {
|
68
|
+
return concat("ipfs://", toCID(source));
|
69
|
+
}
|
70
|
+
|
71
|
+
function _validateQueryIPFS(uint[] memory path, uint[] memory zkp, uint size_path, uint size_val) internal pure returns(uint[] memory){
|
72
|
+
require(zkp[8] == 1, "value doesn't exist");
|
73
|
+
uint len = 41;
|
74
|
+
for(uint i = len; i < len + size_path; i++){
|
75
|
+
require((path.length <= i - len && zkp[i] == 0) || path[i - len] == zkp[i], "wrong path");
|
76
|
+
}
|
77
|
+
uint[] memory value = new uint[](size_val);
|
78
|
+
for(uint i = len + size_path; i < len + size_path + size_val; i++){
|
79
|
+
value[i - (len + size_val)] = zkp[i];
|
80
|
+
}
|
81
|
+
return toArr(value);
|
82
|
+
}
|
83
|
+
|
84
|
+
function validateQuery(string memory URI, uint[] memory path, uint[] memory zkp) internal view returns(uint[] memory){
|
85
|
+
uint[34] memory hash;
|
86
|
+
hash[0] = 18;
|
87
|
+
hash[1] = 32;
|
88
|
+
for(uint i = 9; i < 41; i++) hash[i - 7] = zkp[i];
|
89
|
+
string memory CID = ipfsURI(hash);
|
90
|
+
require(uriEqual(CID,URI), "wrong CID");
|
91
|
+
verify(zkp, VerifierIPFS.verifyProof.selector, verifierIPFS);
|
92
|
+
return _validateQueryIPFS(path, zkp, SIZE_PATH, SIZE_VAL);
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "./ZKQuery.sol";
|
5
|
+
|
6
|
+
contract ZKJson is ZKQuery{
|
7
|
+
address public verifierJSON;
|
8
|
+
|
9
|
+
function _validateQueryJSON(uint[] memory path, uint[] memory zkp, uint size_path, uint size_val) internal pure returns(uint[] memory){
|
10
|
+
require(zkp[8] == 1, "value doesn't exist");
|
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_path)] = zkp[i];
|
17
|
+
}
|
18
|
+
return toArr(value);
|
19
|
+
}
|
20
|
+
|
21
|
+
}
|
@@ -0,0 +1,286 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
|
5
|
+
contract ZKQuery {
|
6
|
+
|
7
|
+
function getPath(uint i, uint[] memory _json) private pure returns(uint[] memory, uint){
|
8
|
+
uint[] memory _path;
|
9
|
+
assembly {
|
10
|
+
let json := add(_json, 0x20)
|
11
|
+
let len := mload(add(json, mul(i, 0x20)))
|
12
|
+
i := add(i, 1)
|
13
|
+
_path := msize()
|
14
|
+
mstore(_path, sub(mload(_json), i))
|
15
|
+
let _path0 := add(_path, 0x20)
|
16
|
+
mstore(_path0, len)
|
17
|
+
let pi := 0x20
|
18
|
+
for { let i2 := 0 } lt(i2, len) { i2 := add(i2, 1) } {
|
19
|
+
let plen := mload(add(json, mul(i, 0x20)))
|
20
|
+
mstore(add(_path0, pi), plen)
|
21
|
+
pi := add(pi, 0x20)
|
22
|
+
i := add(i, 1)
|
23
|
+
let plen2 := 1
|
24
|
+
if iszero(plen) {
|
25
|
+
if iszero(mload(add(json, mul(i, 0x20)))){
|
26
|
+
plen2 := 2
|
27
|
+
}
|
28
|
+
}
|
29
|
+
for { let i3 := 0 } lt(i3, plen2) { i3 := add(i3, 1) } {
|
30
|
+
mstore(add(_path0, pi), mload(add(json, mul(i, 0x20))))
|
31
|
+
pi := add(pi, 0x20)
|
32
|
+
i := add(i, 1)
|
33
|
+
}
|
34
|
+
}
|
35
|
+
mstore(_path, div(pi, 0x20))
|
36
|
+
mstore(0x40, add(_path, add(0x20, pi)))
|
37
|
+
}
|
38
|
+
return (_path, i);
|
39
|
+
}
|
40
|
+
|
41
|
+
function _getVal(uint i, uint[] memory _json) private pure returns(uint[] memory, uint){
|
42
|
+
uint[] memory _val;
|
43
|
+
assembly {
|
44
|
+
let json := add(_json, 0x20)
|
45
|
+
_val := msize()
|
46
|
+
mstore(_val, sub(mload(_json), i))
|
47
|
+
let _val0 := add(_val, 0x20)
|
48
|
+
let _type := mload(add(json, mul(i, 0x20)))
|
49
|
+
i := add(i, 1)
|
50
|
+
let vlen := 0x20
|
51
|
+
mstore(_val0, _type)
|
52
|
+
let len := 0
|
53
|
+
if eq(_type, 1) {
|
54
|
+
len := 1
|
55
|
+
}
|
56
|
+
if eq(_type, 2) {
|
57
|
+
len := 3
|
58
|
+
}
|
59
|
+
if eq(_type, 3) {
|
60
|
+
len := add(mload(add(json, mul(i, 0x20))), 1)
|
61
|
+
}
|
62
|
+
for { let i2 := 0 } lt(i2, len) { i2 := add(i2, 1) } {
|
63
|
+
mstore(add(_val0, vlen), mload(add(json, mul(i, 0x20))))
|
64
|
+
vlen := add(vlen, 0x20)
|
65
|
+
i := add(i, 1)
|
66
|
+
}
|
67
|
+
mstore(_val, div(vlen, 0x20))
|
68
|
+
mstore(0x40, add(_val, add(0x20, vlen)))
|
69
|
+
}
|
70
|
+
return (_val, i);
|
71
|
+
}
|
72
|
+
|
73
|
+
function getVal(uint[] memory path, uint[] memory _json) private pure returns(uint[] memory){
|
74
|
+
require (_json[0] == 4, "not raw value");
|
75
|
+
uint[] memory path2 = toArr(path);
|
76
|
+
uint i = 1;
|
77
|
+
while(i < _json.length){
|
78
|
+
(uint[] memory _path, uint i2) = getPath(i, _json);
|
79
|
+
(uint[] memory _val2, uint i3) = _getVal(i2, _json);
|
80
|
+
i = i3;
|
81
|
+
uint path_match = 1;
|
82
|
+
if(_path.length != path2.length){
|
83
|
+
path_match = 0;
|
84
|
+
}else{
|
85
|
+
for(uint i4 = 0; i4 < path2.length; i4++){
|
86
|
+
if(_path[i4] != path2[i4]) path_match = 0;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
if(path_match == 1) return _val2;
|
90
|
+
}
|
91
|
+
require(false, "value not found");
|
92
|
+
}
|
93
|
+
|
94
|
+
function toArr(uint[] memory json) internal pure returns(uint[] memory){
|
95
|
+
uint[] memory _json;
|
96
|
+
assembly {
|
97
|
+
let ji := 0x0
|
98
|
+
let prev := 0
|
99
|
+
let start := add(json, 0x20)
|
100
|
+
_json := msize()
|
101
|
+
mstore(_json, mload(json))
|
102
|
+
let _json0 := add(_json, 0x20)
|
103
|
+
for { let i := 0 } lt(i, mload(json)) { i := add(i, 1) } {
|
104
|
+
let v := mload(add(start, mul(i, 0x20)))
|
105
|
+
if gt(v,0) {
|
106
|
+
let p := 0
|
107
|
+
let x := v
|
108
|
+
let on := 0 // 0 = first, 1 = off, 2 = on, 3 = is9, 4 = to set zero, 5 = zero
|
109
|
+
let cur := 0
|
110
|
+
let len := 0
|
111
|
+
let num := 0
|
112
|
+
for { } gt(v, 0) { } {
|
113
|
+
v := div(v, 10)
|
114
|
+
p := add(p, 1)
|
115
|
+
}
|
116
|
+
for { } gt(p, 0) { } {
|
117
|
+
let n := div(x, exp(10, sub(p, 1)))
|
118
|
+
let _on := on
|
119
|
+
if iszero(_on){
|
120
|
+
on := 1
|
121
|
+
}
|
122
|
+
if and(eq(_on, 1), iszero(n)) {
|
123
|
+
on := 4
|
124
|
+
}
|
125
|
+
if eq(_on, 4) {
|
126
|
+
on := 5
|
127
|
+
len := n
|
128
|
+
}
|
129
|
+
if and(eq(_on, 1), gt(n, 0)) {
|
130
|
+
if eq(n, 9) {
|
131
|
+
len := 8
|
132
|
+
on := 3
|
133
|
+
}
|
134
|
+
if and(iszero(iszero(n)), iszero(eq(n,9))) {
|
135
|
+
on := 2
|
136
|
+
len := n
|
137
|
+
}
|
138
|
+
cur := 0
|
139
|
+
}
|
140
|
+
if gt(_on, 1) {
|
141
|
+
if eq(_on, 5){
|
142
|
+
mstore(add(_json0, ji), n)
|
143
|
+
len := sub(len, 1)
|
144
|
+
ji := add(ji, 0x20)
|
145
|
+
if iszero(len) {
|
146
|
+
cur := 0
|
147
|
+
on := 1
|
148
|
+
len := 0
|
149
|
+
num := 0
|
150
|
+
}
|
151
|
+
}
|
152
|
+
if iszero(eq(_on, 5)){
|
153
|
+
num := add(num, mul(n, exp(10, sub(sub(len, cur), 1))))
|
154
|
+
cur := add(cur, 1)
|
155
|
+
if eq(cur, len) {
|
156
|
+
prev := mul(prev, exp(10, len))
|
157
|
+
if eq(_on, 3) {
|
158
|
+
prev := add(prev, num)
|
159
|
+
}
|
160
|
+
if iszero(eq(_on, 3)) {
|
161
|
+
num := add(num, prev)
|
162
|
+
prev := 0
|
163
|
+
mstore(add(_json0, ji), num)
|
164
|
+
ji := add(ji, 0x20)
|
165
|
+
}
|
166
|
+
cur := 0
|
167
|
+
on := 1
|
168
|
+
len := 0
|
169
|
+
num := 0
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
x := sub(x, mul(exp(10, sub(p, 1)), n))
|
174
|
+
p := sub(p, 1)
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
mstore(_json, div(ji, 0x20))
|
179
|
+
mstore(0x40, add(_json, add(0x20, ji)))
|
180
|
+
}
|
181
|
+
return _json;
|
182
|
+
}
|
183
|
+
|
184
|
+
function _toString(uint8[] memory charCodes) private pure returns (string memory) {
|
185
|
+
bytes memory stringBytes = new bytes(charCodes.length);
|
186
|
+
for (uint i = 0; i < charCodes.length; i++) stringBytes[i] = bytes1(charCodes[i]);
|
187
|
+
return string(stringBytes);
|
188
|
+
}
|
189
|
+
|
190
|
+
function _qInt (uint[] memory value) internal pure returns (int) {
|
191
|
+
require(value[0] == 2 && value[2] == 0, "not int");
|
192
|
+
return int(value[3]) * (value[1] == 1 ? int(1) : int(-1));
|
193
|
+
}
|
194
|
+
|
195
|
+
function _qFloat (uint[] memory value) internal pure returns (uint[3] memory) {
|
196
|
+
require(value[0] == 2 && value[2] != 0, "not float");
|
197
|
+
uint[3] memory float;
|
198
|
+
float[0] = value[1];
|
199
|
+
float[1] = value[2];
|
200
|
+
float[2] = value[3];
|
201
|
+
return float;
|
202
|
+
}
|
203
|
+
|
204
|
+
function _qRaw (uint[] memory value) internal pure returns (uint[] memory) {
|
205
|
+
require(value[0] == 4, "not object or array");
|
206
|
+
return value;
|
207
|
+
}
|
208
|
+
|
209
|
+
function _qString (uint[] memory value) internal pure returns (string memory) {
|
210
|
+
require(value[0] == 3, "not string");
|
211
|
+
uint8[] memory charCodes = new uint8[](value[1]);
|
212
|
+
for(uint i = 0; i < value[1];i++) charCodes[i] = uint8(value[i+2]);
|
213
|
+
string memory str = _toString(charCodes);
|
214
|
+
return str;
|
215
|
+
}
|
216
|
+
|
217
|
+
function _qBool (uint[] memory value) internal pure returns (bool) {
|
218
|
+
require(value[0] == 1, "not bool");
|
219
|
+
return value[1] == 1 ? true : false;
|
220
|
+
}
|
221
|
+
|
222
|
+
function _qNull (uint[] memory value) internal pure returns (bool) {
|
223
|
+
require(value[0] == 0, "not null");
|
224
|
+
return true;
|
225
|
+
}
|
226
|
+
|
227
|
+
function _qCond (uint[] memory value, uint[] memory cond) internal pure returns (bool) {
|
228
|
+
uint[] memory _cond = toArr(cond);
|
229
|
+
require(value.length == _cond.length, "wrong query");
|
230
|
+
for(uint i = 0; i < value.length; i++) require(_cond[i] == value[i], "wrong query");
|
231
|
+
return true;
|
232
|
+
}
|
233
|
+
|
234
|
+
function getInt (uint[] memory path, uint[] memory raw) internal pure returns (int) {
|
235
|
+
uint[] memory value = getVal(path, raw);
|
236
|
+
return _qInt(value);
|
237
|
+
}
|
238
|
+
|
239
|
+
function getString (uint[] memory path, uint[] memory raw) internal pure returns (string memory) {
|
240
|
+
uint[] memory value = getVal(path, raw);
|
241
|
+
_qString(value);
|
242
|
+
}
|
243
|
+
|
244
|
+
function getBool (uint[] memory path, uint[] memory raw) internal pure returns (bool) {
|
245
|
+
uint[] memory value = getVal(path, raw);
|
246
|
+
_qBool(value);
|
247
|
+
}
|
248
|
+
|
249
|
+
function getNull (uint[] memory path, uint[] memory raw) internal pure returns (bool) {
|
250
|
+
uint[] memory value = getVal(path, raw);
|
251
|
+
_qNull(value);
|
252
|
+
}
|
253
|
+
|
254
|
+
function verify(uint[] memory zkp, bytes4 selector, address addr) internal view returns (bool) {
|
255
|
+
uint size;
|
256
|
+
assembly {
|
257
|
+
size := extcodesize(addr)
|
258
|
+
}
|
259
|
+
require(size > 0, "contract doesn't exist");
|
260
|
+
bool valid;
|
261
|
+
assembly{
|
262
|
+
let callData := mload(0x40)
|
263
|
+
let zlen := mload(zkp)
|
264
|
+
let clen := add(0x4, mul(0x20, zlen))
|
265
|
+
mstore(callData, clen)
|
266
|
+
mstore(add(callData, 0x20), selector)
|
267
|
+
for { let i := 1 } lt(i, add(1, zlen)) { i := add(i, 1) } {
|
268
|
+
mstore(add(callData, add(0x4, mul(i, 0x20))), mload(add(zkp, mul(i, 0x20))))
|
269
|
+
}
|
270
|
+
let success := staticcall(
|
271
|
+
gas(),
|
272
|
+
addr,
|
273
|
+
add(callData, 0x20),
|
274
|
+
clen,
|
275
|
+
callData,
|
276
|
+
0x20
|
277
|
+
)
|
278
|
+
if iszero(success) {
|
279
|
+
revert(0, 0)
|
280
|
+
}
|
281
|
+
valid := mload(callData)
|
282
|
+
}
|
283
|
+
require(valid, "invalid proof");
|
284
|
+
return true;
|
285
|
+
}
|
286
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "./ZKQuery.sol";
|
5
|
+
|
6
|
+
interface VerifierRU {
|
7
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[11] calldata _pubSignals) view external returns (bool);
|
8
|
+
|
9
|
+
}
|
10
|
+
|
11
|
+
contract ZKRollup is ZKQuery {
|
12
|
+
address public verifierRU;
|
13
|
+
address public committer;
|
14
|
+
uint public root;
|
15
|
+
|
16
|
+
function _validateQueryRU(uint[] memory path, uint[] memory zkp, uint size_path, uint size_val) internal view returns(uint[] memory){
|
17
|
+
require(zkp[size_path + size_val + 9] == root, "root mismatch");
|
18
|
+
require(zkp[size_path + size_val + 10] == path[0], "wrong collection");
|
19
|
+
require(zkp[size_path + size_val + 11] == path[1], "wrong doc");
|
20
|
+
require(zkp[8] == 1, "value doesn't exist");
|
21
|
+
require(path.length <= size_path + size_val, "path too long");
|
22
|
+
for(uint i = 9; i < 9 + path.length - 2; i++) require(path[i - 7] == zkp[i], "wrong path");
|
23
|
+
uint[] memory value = new uint[](size_val);
|
24
|
+
for(uint i = 9 + size_path; i < 9 + size_path + size_val; i++) value[i - (9 + size_path)] = zkp[i];
|
25
|
+
return toArr(value);
|
26
|
+
}
|
27
|
+
|
28
|
+
function commit (uint[] memory zkp) public returns (uint) {
|
29
|
+
require (zkp[9] == root, "wrong merkle root");
|
30
|
+
require(msg.sender == committer, "sender is not committer");
|
31
|
+
root = zkp[8];
|
32
|
+
verify(zkp,VerifierRU.verifyProof.selector, verifierRU);
|
33
|
+
return root;
|
34
|
+
}
|
35
|
+
}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
|
5
|
+
import "../NORollup.sol";
|
6
|
+
|
7
|
+
interface VerifierDB {
|
8
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[16] calldata _pubSignals) view external returns (bool);
|
9
|
+
}
|
10
|
+
|
11
|
+
contract NORU is NORollup {
|
12
|
+
uint constant SIZE_PATH = 4;
|
13
|
+
uint constant SIZE_VAL = 8;
|
14
|
+
address public verifierDB;
|
15
|
+
|
16
|
+
constructor (address _verifierDB, address _committer){
|
17
|
+
verifierDB = _verifierDB;
|
18
|
+
committer = _committer;
|
19
|
+
}
|
20
|
+
|
21
|
+
function validateQuery(uint[] memory zkp) private view returns(uint[] memory){
|
22
|
+
verify(zkp, VerifierDB.verifyProof.selector, verifierDB);
|
23
|
+
return _validateQuery(zkp, SIZE_PATH, SIZE_VAL);
|
24
|
+
}
|
25
|
+
|
26
|
+
function qInt (uint[] memory zkp) public view returns (int) {
|
27
|
+
uint[] memory value = validateQuery(zkp);
|
28
|
+
return _qInt(value);
|
29
|
+
}
|
30
|
+
|
31
|
+
function qFloat (uint[] memory zkp) public view returns (uint[3] memory) {
|
32
|
+
uint[] memory value = validateQuery(zkp);
|
33
|
+
return _qFloat(value);
|
34
|
+
}
|
35
|
+
|
36
|
+
function qRaw (uint[] memory zkp) public view returns (uint[] memory) {
|
37
|
+
uint[] memory value = validateQuery(zkp);
|
38
|
+
return _qRaw(value);
|
39
|
+
}
|
40
|
+
|
41
|
+
function qString (uint[] memory zkp) public view returns (string memory) {
|
42
|
+
uint[] memory value = validateQuery(zkp);
|
43
|
+
return _qString(value);
|
44
|
+
}
|
45
|
+
|
46
|
+
function qBool (uint[] memory zkp) public view returns (bool) {
|
47
|
+
uint[] memory value = validateQuery(zkp);
|
48
|
+
return _qBool(value);
|
49
|
+
}
|
50
|
+
|
51
|
+
function qNull (uint[] memory zkp) public view returns (bool) {
|
52
|
+
uint[] memory value = validateQuery(zkp);
|
53
|
+
return _qNull(value);
|
54
|
+
}
|
55
|
+
|
56
|
+
function qCond (uint[] memory cond, uint[] memory zkp) public view returns (bool) {
|
57
|
+
uint[] memory value = validateQuery(zkp);
|
58
|
+
return _qCond(value, cond);
|
59
|
+
}
|
60
|
+
|
61
|
+
function qCustom (uint[] memory path, uint[] memory zkp) public view returns (int) {
|
62
|
+
uint[] memory value = validateQuery(zkp);
|
63
|
+
return getInt(path, value);
|
64
|
+
}
|
65
|
+
|
66
|
+
}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
|
5
|
+
import "../ZKJson.sol";
|
6
|
+
|
7
|
+
interface VerifierJSON {
|
8
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[14] calldata _pubSignals) view external returns (bool);
|
9
|
+
}
|
10
|
+
|
11
|
+
contract SimpleJSON is ZKJson {
|
12
|
+
uint constant SIZE_PATH = 4;
|
13
|
+
uint constant SIZE_VAL = 8;
|
14
|
+
|
15
|
+
constructor (address _verifierJSON){
|
16
|
+
verifierJSON = _verifierJSON;
|
17
|
+
}
|
18
|
+
|
19
|
+
function validateQuery(uint[] memory path, uint[] memory zkp) private view returns(uint[] memory){
|
20
|
+
verify(zkp, VerifierJSON.verifyProof.selector, verifierJSON);
|
21
|
+
return _validateQueryJSON(path, zkp, SIZE_PATH, SIZE_VAL);
|
22
|
+
}
|
23
|
+
|
24
|
+
function qInt (uint[] memory path, uint[] memory zkp) public view returns (int) {
|
25
|
+
uint[] memory value = validateQuery(path, zkp);
|
26
|
+
return _qInt(value);
|
27
|
+
}
|
28
|
+
|
29
|
+
function qFloat (uint[] memory path, uint[] memory zkp) public view returns (uint[3] memory) {
|
30
|
+
uint[] memory value = validateQuery(path, zkp);
|
31
|
+
return _qFloat(value);
|
32
|
+
}
|
33
|
+
|
34
|
+
function qRaw (uint[] memory path, uint[] memory zkp) public view returns (uint[] memory) {
|
35
|
+
uint[] memory value = validateQuery(path, zkp);
|
36
|
+
return _qRaw(value);
|
37
|
+
}
|
38
|
+
|
39
|
+
function qString (uint[] memory path, uint[] memory zkp) public view returns (string memory) {
|
40
|
+
uint[] memory value = validateQuery(path, zkp);
|
41
|
+
return _qString(value);
|
42
|
+
}
|
43
|
+
|
44
|
+
function qBool (uint[] memory path, uint[] memory zkp) public view returns (bool) {
|
45
|
+
uint[] memory value = validateQuery(path, zkp);
|
46
|
+
return _qBool(value);
|
47
|
+
}
|
48
|
+
|
49
|
+
function qNull (uint[] memory path, uint[] memory zkp) public view returns (bool) {
|
50
|
+
uint[] memory value = validateQuery(path, zkp);
|
51
|
+
return _qNull(value);
|
52
|
+
}
|
53
|
+
|
54
|
+
function qCond (uint[] memory path, uint[] memory cond, uint[] memory zkp) public view returns (bool) {
|
55
|
+
uint[] memory value = validateQuery(path, zkp);
|
56
|
+
return _qCond(value, cond);
|
57
|
+
}
|
58
|
+
|
59
|
+
function qCustom (uint[] memory path, uint[] memory path2, uint[] memory zkp) public view returns (int) {
|
60
|
+
uint[] memory value = validateQuery(path, zkp);
|
61
|
+
return getInt(path2, value);
|
62
|
+
}
|
63
|
+
|
64
|
+
}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
|
5
|
+
import "../OPRollup.sol";
|
6
|
+
|
7
|
+
interface VerifierDB {
|
8
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[16] calldata _pubSignals) view external returns (bool);
|
9
|
+
}
|
10
|
+
|
11
|
+
contract SimpleOPRU is OPRollup {
|
12
|
+
uint constant SIZE_PATH = 4;
|
13
|
+
uint constant SIZE_VAL = 8;
|
14
|
+
address public verifierDB;
|
15
|
+
|
16
|
+
constructor (address _verifierRU, address _verifierDB, address _committer){
|
17
|
+
verifierRU = _verifierRU;
|
18
|
+
verifierDB = _verifierDB;
|
19
|
+
committer = _committer;
|
20
|
+
}
|
21
|
+
|
22
|
+
function validateQuery(uint[] memory path, uint[] memory zkp) private view returns(uint[] memory){
|
23
|
+
verify(zkp, VerifierDB.verifyProof.selector, verifierDB);
|
24
|
+
return _validateQueryRU(path, zkp, SIZE_PATH, SIZE_VAL);
|
25
|
+
}
|
26
|
+
|
27
|
+
function qInt (uint[] memory path, uint[] memory zkp) public view returns (int) {
|
28
|
+
uint[] memory value = validateQuery(path, zkp);
|
29
|
+
return _qInt(value);
|
30
|
+
}
|
31
|
+
|
32
|
+
function qFloat (uint[] memory path, uint[] memory zkp) public view returns (uint[3] memory) {
|
33
|
+
uint[] memory value = validateQuery(path, zkp);
|
34
|
+
return _qFloat(value);
|
35
|
+
}
|
36
|
+
|
37
|
+
function qRaw (uint[] memory path, uint[] memory zkp) public view returns (uint[] memory) {
|
38
|
+
uint[] memory value = validateQuery(path, zkp);
|
39
|
+
return _qRaw(value);
|
40
|
+
}
|
41
|
+
|
42
|
+
function qString (uint[] memory path, uint[] memory zkp) public view returns (string memory) {
|
43
|
+
uint[] memory value = validateQuery(path, zkp);
|
44
|
+
return _qString(value);
|
45
|
+
}
|
46
|
+
|
47
|
+
function qBool (uint[] memory path, uint[] memory zkp) public view returns (bool) {
|
48
|
+
uint[] memory value = validateQuery(path, zkp);
|
49
|
+
return _qBool(value);
|
50
|
+
}
|
51
|
+
|
52
|
+
function qNull (uint[] memory path, uint[] memory zkp) public view returns (bool) {
|
53
|
+
uint[] memory value = validateQuery(path, zkp);
|
54
|
+
return _qNull(value);
|
55
|
+
}
|
56
|
+
|
57
|
+
function qCond (uint[] memory path, uint[] memory cond, uint[] memory zkp) public view returns (bool) {
|
58
|
+
uint[] memory value = validateQuery(path, zkp);
|
59
|
+
return _qCond(value, cond);
|
60
|
+
}
|
61
|
+
|
62
|
+
function qCustom (uint[] memory path, uint[] memory path2, uint[] memory zkp) public view returns (int) {
|
63
|
+
uint[] memory value = validateQuery(path, zkp);
|
64
|
+
return getInt(path2, value);
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|