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,218 @@
|
|
1
|
+
## zkDB Rollup Tutorial
|
2
|
+
|
3
|
+
*zkJSON is still under active development, and neither the circuits nor the contracts have been audited. Please use it for only experimental purposes.*
|
4
|
+
|
5
|
+
### Install `zkjson` Package
|
6
|
+
|
7
|
+
Make sure you have [Circom](https://docs.circom.io/getting-started/installation/) and [Hardhat](https://hardhat.org/hardhat-runner/docs/getting-started#installation) installed globally.
|
8
|
+
|
9
|
+
```bash
|
10
|
+
git clone https://github.com/weavedb/zkjson.git
|
11
|
+
cd zkjson
|
12
|
+
yarn
|
13
|
+
```
|
14
|
+
|
15
|
+
### Generate Powers of Tau
|
16
|
+
|
17
|
+
For the `db` circuit,
|
18
|
+
|
19
|
+
```bash
|
20
|
+
yarn ceremony --power 16
|
21
|
+
```
|
22
|
+
|
23
|
+
For the `rollup` circuit,
|
24
|
+
|
25
|
+
```bash
|
26
|
+
yarn ceremony --power 20
|
27
|
+
```
|
28
|
+
|
29
|
+
*Beware that power 20 takes hours.*
|
30
|
+
|
31
|
+
### Compile Circuits
|
32
|
+
|
33
|
+
We need to compile 2 circuits.
|
34
|
+
|
35
|
+
```bash
|
36
|
+
yarn compile --power 16 --circuit db
|
37
|
+
yarn compile --power 20 --circuit rollup
|
38
|
+
```
|
39
|
+
|
40
|
+
### Create Solidity Project with Hardhat
|
41
|
+
|
42
|
+
```bash
|
43
|
+
cd ..
|
44
|
+
mkdir myrollup
|
45
|
+
cd myrollup
|
46
|
+
npx hardhat init
|
47
|
+
yarn add zkjson
|
48
|
+
```
|
49
|
+
|
50
|
+
### Copy Verifier Contracts
|
51
|
+
|
52
|
+
```bash
|
53
|
+
cp ../zkjson/circom/build/circuit/db/verifier.sol contracts/verifier_db.sol
|
54
|
+
cp ../zkjson/circom/build/circuit/rollup/verifier.sol contracts/verifier_rollup.sol
|
55
|
+
```
|
56
|
+
|
57
|
+
Rename the contract name to `Groth16VerifierJSON` as we will have multiple verifiers with the same name.
|
58
|
+
|
59
|
+
Open `myrollup/contracts/verifier_db.sol` and rename
|
60
|
+
|
61
|
+
```solidity
|
62
|
+
contract Groth16Verifier {
|
63
|
+
```
|
64
|
+
|
65
|
+
to
|
66
|
+
|
67
|
+
```solidity
|
68
|
+
contract Groth16VerifierDB {
|
69
|
+
```
|
70
|
+
|
71
|
+
Then open `myrollup/contracts/verifier_rollup.sol` and rename
|
72
|
+
|
73
|
+
```solidity
|
74
|
+
contract Groth16Verifier {
|
75
|
+
```
|
76
|
+
|
77
|
+
to
|
78
|
+
|
79
|
+
```solidity
|
80
|
+
contract Groth16VerifierRU {
|
81
|
+
```
|
82
|
+
|
83
|
+
### Write Solidity Contract
|
84
|
+
|
85
|
+
`myrollup/contracts/MyRollup.sol`
|
86
|
+
|
87
|
+
```solidity
|
88
|
+
// SPDX-License-Identifier: UNLICENSED
|
89
|
+
pragma solidity >=0.7.0 <0.9.0;
|
90
|
+
import "../node_modules/zkrjson/contracts/ZKRollup.sol";
|
91
|
+
|
92
|
+
interface VerifierDB {
|
93
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[14] calldata _pubSignals) view external returns (bool);
|
94
|
+
}
|
95
|
+
|
96
|
+
contract MyRollup is ZKRollup {
|
97
|
+
uint constant SIZE_PATH = 5;
|
98
|
+
uint constant SIZE_VAL = 5;
|
99
|
+
address public verifierDB;
|
100
|
+
|
101
|
+
constructor (address _verifierRU, address _verifierDB, address _committer){
|
102
|
+
verifierRU = _verifierRU;
|
103
|
+
verifierDB = _verifierDB;
|
104
|
+
committer = _committer;
|
105
|
+
}
|
106
|
+
|
107
|
+
function validateQuery(uint[] memory path, uint[] memory zkp) private view returns(uint[] memory){
|
108
|
+
verify(zkp, VerifierDB.verifyProof.selector, verifierDB);
|
109
|
+
return _validateQueryRU(path, zkp, SIZE_PATH, SIZE_VAL);
|
110
|
+
}
|
111
|
+
|
112
|
+
function qInt (uint[] memory path, uint[] memory zkp) public view returns (int) {
|
113
|
+
uint[] memory value = validateQuery(path, zkp);
|
114
|
+
return _qInt(value);
|
115
|
+
}
|
116
|
+
|
117
|
+
function qFloat (uint[] memory path, uint[] memory zkp) public view returns (uint[3] memory) {
|
118
|
+
uint[] memory value = validateQuery(path, zkp);
|
119
|
+
return _qFloat(value);
|
120
|
+
}
|
121
|
+
|
122
|
+
function qRaw (uint[] memory path, uint[] memory zkp) public view returns (uint[] memory) {
|
123
|
+
uint[] memory value = validateQuery(path, zkp);
|
124
|
+
return _qRaw(value);
|
125
|
+
}
|
126
|
+
|
127
|
+
function qString (uint[] memory path, uint[] memory zkp) public view returns (string memory) {
|
128
|
+
uint[] memory value = validateQuery(path, zkp);
|
129
|
+
return _qString(value);
|
130
|
+
}
|
131
|
+
|
132
|
+
function qBool (uint[] memory path, uint[] memory zkp) public view returns (bool) {
|
133
|
+
uint[] memory value = validateQuery(path, zkp);
|
134
|
+
return _qBool(value);
|
135
|
+
}
|
136
|
+
|
137
|
+
function qNull (uint[] memory path, uint[] memory zkp) public view returns (bool) {
|
138
|
+
uint[] memory value = validateQuery(path, zkp);
|
139
|
+
return _qNull(value);
|
140
|
+
}
|
141
|
+
}
|
142
|
+
```
|
143
|
+
|
144
|
+
### Write Tests
|
145
|
+
|
146
|
+
`myrollup/test/MyRollup.js`
|
147
|
+
|
148
|
+
```javascript
|
149
|
+
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
|
150
|
+
const { toIndex, path, DB } = require("zkjson")
|
151
|
+
const { resolve } = require("path")
|
152
|
+
const { expect } = require("chai")
|
153
|
+
|
154
|
+
async function deploy() {
|
155
|
+
const [committer] = await ethers.getSigners()
|
156
|
+
const VerifierRU = await ethers.getContractFactory("Groth16VerifierRU")
|
157
|
+
const verifierRU = await VerifierRU.deploy()
|
158
|
+
const VerifierDB = await ethers.getContractFactory("Groth16VerifierDB")
|
159
|
+
const verifierDB = await VerifierDB.deploy()
|
160
|
+
|
161
|
+
const MyRU = await ethers.getContractFactory("MyRollup")
|
162
|
+
const myru = await MyRU.deploy( verifierRU.address, verifierDB.address, committer.address )
|
163
|
+
return { myru, committer }
|
164
|
+
}
|
165
|
+
|
166
|
+
describe("MyRollup", function () {
|
167
|
+
let myru, committer, db, col_id, ru
|
168
|
+
this.timeout(0)
|
169
|
+
|
170
|
+
beforeEach(async () => {
|
171
|
+
const dep = await loadFixture(deploy)
|
172
|
+
myru = dep.myru
|
173
|
+
committer = dep.committer
|
174
|
+
})
|
175
|
+
|
176
|
+
it("should verify rollup transactions", async function () {
|
177
|
+
db = new DB({
|
178
|
+
level: 100,
|
179
|
+
size_path: 5,
|
180
|
+
size_val: 5,
|
181
|
+
size_json: 256,
|
182
|
+
size_txs: 10,
|
183
|
+
level_col: 8,
|
184
|
+
wasmRU: resolve( __dirname, "../../zkjson/circom/build/circuits/rollup/index_js/index.wasm" ),
|
185
|
+
zkeyRU: resolve( __dirname, "../../zkjson/circom/build/circuits/rollup/index_0001.zkey" ),
|
186
|
+
wasm: resolve( __dirname, "../../zkjson/circom/build/circuits/db/index_js/index.wasm" ),
|
187
|
+
zkey: resolve( __dirname, "../../zkjson/circom/build/circuits/db/index_0001.zkey" ),
|
188
|
+
})
|
189
|
+
await db.init()
|
190
|
+
col_id = await db.addCollection()
|
191
|
+
const people = [
|
192
|
+
{ name: "Bob", age: 10 },
|
193
|
+
{ name: "Alice", age: 20 },
|
194
|
+
{ name: "Mike", age: 30 },
|
195
|
+
{ name: "Beth", age: 40 },
|
196
|
+
]
|
197
|
+
let txs = people.map(v => { return [col_id, v.name, v] })
|
198
|
+
|
199
|
+
// batch commit write queries
|
200
|
+
const zkp = await db.genRollupProof(txs)
|
201
|
+
await myru.commit(zkp)
|
202
|
+
|
203
|
+
// query Bob's age
|
204
|
+
const zkp2 = await db.genProof({ json: people[0], col_id, path: "age", id: "Bob" })
|
205
|
+
expect((await myru.qInt([col_id, toIndex("Bob"), ...path("age")], zkp2)).toNumber()).to.eql(10)
|
206
|
+
|
207
|
+
// query Beth's name
|
208
|
+
const zkp3 = await db.genProof({ json: people[3], col_id, path: "name", id: "Beth" })
|
209
|
+
expect(await myru.qString([col_id, toIndex("Beth"), ...path("name")], zkp3)).to.eql("Beth")
|
210
|
+
})
|
211
|
+
})
|
212
|
+
```
|
213
|
+
|
214
|
+
Then run the tests.
|
215
|
+
|
216
|
+
```bash
|
217
|
+
yarn hardhat test test/MyRollup.js
|
218
|
+
```
|
@@ -0,0 +1,254 @@
|
|
1
|
+
## zkJSON SDK
|
2
|
+
|
3
|
+
### Install
|
4
|
+
|
5
|
+
```bash
|
6
|
+
yarn add zkjson
|
7
|
+
```
|
8
|
+
|
9
|
+
### Encoder / Decoder
|
10
|
+
|
11
|
+
Encode / Decode JSON
|
12
|
+
|
13
|
+
```javascript
|
14
|
+
const { encode, decode, toSignal, fromSignal } = require("zkjson")
|
15
|
+
|
16
|
+
const json = { a : 1 }
|
17
|
+
const encoded = encode(json) // [ 1, 1, 97, 2, 1, 0, 1 ]
|
18
|
+
const signal = toSignal(encoded) // [ '111129712111011' ]
|
19
|
+
const encoded2 = fromSignal(signal) // [ 1, 1, 97, 2, 1, 0, 1 ]
|
20
|
+
const decoded = decode(encoded2) // { a : 1 }
|
21
|
+
```
|
22
|
+
|
23
|
+
Encode / Decode paths
|
24
|
+
|
25
|
+
```javascript
|
26
|
+
const { toSignal, fromSignal, encodePath, decodePath, path } = require("zkjson")
|
27
|
+
|
28
|
+
const _path = "a"
|
29
|
+
const encodedPath = encodePath(_path) // [ 1, 1, 97 ]
|
30
|
+
const signalPath = toSignal(encodedPath) // [ "1111297" ]
|
31
|
+
const encodedPath2 = fromSignal(signalPath) // [ 1, 1, 97 ]
|
32
|
+
const decodedPath = decodePath(encodedPath) // "a"
|
33
|
+
|
34
|
+
const signalPath2 = path(_path) // [ "1111297" ]
|
35
|
+
```
|
36
|
+
|
37
|
+
Encode / Decode values
|
38
|
+
|
39
|
+
```javascript
|
40
|
+
const { toSignal, fromSignal, encodeVal, decodeVal, val } = require("zkjson")
|
41
|
+
|
42
|
+
const _val = 1
|
43
|
+
const encodedVal = encodeVal(_val) // [ 2, 1, 0, 1 ]
|
44
|
+
const signalVal = toSignal(encodedVal) // [ "12111011" ]
|
45
|
+
const encodedVal2 = fromSignal(signalVal) // [ 2, 1, 0, 1 ]
|
46
|
+
const decodedVal = decodeVal(encodedVal) // 1
|
47
|
+
|
48
|
+
const signalVal2 = val(_val) // [ "12111011" ]
|
49
|
+
```
|
50
|
+
|
51
|
+
Encode / Decode conditional queries
|
52
|
+
|
53
|
+
```javascript
|
54
|
+
const { toSignal, fromSignal, encodeQuery, decodeQuery, query } = require("zkjson")
|
55
|
+
|
56
|
+
const _query = [ "$gt", 1 ]
|
57
|
+
const encodedQuery = encodeQuery(_query) // [ 12, 2, 1, 0, 1 ]
|
58
|
+
const signalQuery = toSignal(encodedQuery) // [ "21212111011" ]
|
59
|
+
const encodedQuery2 = fromSignal(signalQuery) // [ 12, 2, 1, 0, 1 ]
|
60
|
+
const decodedQuery = decodeQuery(encodedQuery) // [ "$gt", 1 ]
|
61
|
+
|
62
|
+
const signalQuery2 = query(_query) // [ "21212111011" ]
|
63
|
+
```
|
64
|
+
|
65
|
+
### Document ID <> Index Conversion
|
66
|
+
|
67
|
+
```javascript
|
68
|
+
const { toIndex, fromIndexs } = require("zkjson")
|
69
|
+
|
70
|
+
const index = toIndex("zkJSON") // 1513609181413
|
71
|
+
const str = fromIndex(index) // "zkJSON"
|
72
|
+
```
|
73
|
+
|
74
|
+
### Doc
|
75
|
+
|
76
|
+
```javascript
|
77
|
+
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
|
78
|
+
const { path, Doc } = require("../../sdk")
|
79
|
+
const { resolve } = require("path")
|
80
|
+
const { expect } = require("chai")
|
81
|
+
|
82
|
+
async function deploy() {
|
83
|
+
const Verifier = await ethers.getContractFactory("Groth16VerifierJSON")
|
84
|
+
const verifier = await Verifier.deploy()
|
85
|
+
const MyApp = await ethers.getContractFactory("SimpleJSON")
|
86
|
+
const myapp = await MyApp.deploy(verifier.address)
|
87
|
+
return { myapp }
|
88
|
+
}
|
89
|
+
|
90
|
+
describe("MyApp", function () {
|
91
|
+
let myapp
|
92
|
+
this.timeout(0)
|
93
|
+
|
94
|
+
beforeEach(async () => {
|
95
|
+
const dep = await loadFixture(deploy)
|
96
|
+
myapp = dep.myapp
|
97
|
+
})
|
98
|
+
|
99
|
+
it("should verify JSON", async function () {
|
100
|
+
const doc = new Doc({
|
101
|
+
wasm: resolve(
|
102
|
+
__dirname,
|
103
|
+
"../../circom/build/circuits/json/index_js/index.wasm"
|
104
|
+
),
|
105
|
+
zkey: resolve(
|
106
|
+
__dirname,
|
107
|
+
"../../circom/build/circuits/json/index_0001.zkey"
|
108
|
+
),
|
109
|
+
})
|
110
|
+
const json = {
|
111
|
+
num: 1,
|
112
|
+
float: 1.23,
|
113
|
+
str: "string",
|
114
|
+
bool: true,
|
115
|
+
null: null,
|
116
|
+
array: [1, 2, 3],
|
117
|
+
}
|
118
|
+
|
119
|
+
// query number
|
120
|
+
const zkp = await doc.genProof({ json, path: "num" })
|
121
|
+
expect((await myapp.qInt(path("num"), zkp)).toNumber()).to.eql(1)
|
122
|
+
|
123
|
+
// query string
|
124
|
+
const zkp2 = await doc.genProof({ json, path: "str" })
|
125
|
+
expect(await myapp.qString(path("str"), zkp2)).to.eql("string")
|
126
|
+
|
127
|
+
// query bool
|
128
|
+
const zkp3 = await doc.genProof({ json, path: "bool" })
|
129
|
+
expect(await myapp.qBool(path("bool"), zkp3)).to.eql(true)
|
130
|
+
|
131
|
+
// query null
|
132
|
+
const zkp4 = await doc.genProof({ json, path: "null" })
|
133
|
+
expect(await myapp.qNull(path("null"), zkp4)).to.eql(true)
|
134
|
+
|
135
|
+
// query float
|
136
|
+
const zkp5 = await doc.genProof({ json, path: "float" })
|
137
|
+
expect(
|
138
|
+
(await myapp.qFloat(path("float"), zkp5)).map(f => f.toNumber())
|
139
|
+
).to.eql([1, 2, 123])
|
140
|
+
|
141
|
+
// query array and get number
|
142
|
+
const zkp6 = await doc.genProof({ json, path: "array" })
|
143
|
+
expect(
|
144
|
+
(await myapp.qCustom(path("array"), path("[1]"), zkp6)).toNumber()
|
145
|
+
).to.eql(2)
|
146
|
+
|
147
|
+
// conditional operator
|
148
|
+
const zkp7 = await doc.genProof({ json, path: "num", query: ["$gt", 0] })
|
149
|
+
expect(await myapp.qCond(path("num"), zkp7.slice(15, 21), zkp7)).to.eql(
|
150
|
+
true
|
151
|
+
)
|
152
|
+
|
153
|
+
})
|
154
|
+
})
|
155
|
+
```
|
156
|
+
|
157
|
+
### DB
|
158
|
+
|
159
|
+
```javascript
|
160
|
+
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
|
161
|
+
const { toIndex, path, DB } = require("../../sdk")
|
162
|
+
const { resolve } = require("path")
|
163
|
+
const { expect } = require("chai")
|
164
|
+
|
165
|
+
async function deploy() {
|
166
|
+
const [committer] = await ethers.getSigners()
|
167
|
+
const VerifierRU = await ethers.getContractFactory("Groth16VerifierRU")
|
168
|
+
const verifierRU = await VerifierRU.deploy()
|
169
|
+
const VerifierDB = await ethers.getContractFactory("Groth16VerifierDB")
|
170
|
+
const verifierDB = await VerifierDB.deploy()
|
171
|
+
|
172
|
+
const MyRU = await ethers.getContractFactory("SimpleRU")
|
173
|
+
const myru = await MyRU.deploy(
|
174
|
+
verifierRU.address,
|
175
|
+
verifierDB.address,
|
176
|
+
committer.address
|
177
|
+
)
|
178
|
+
return { myru, committer }
|
179
|
+
}
|
180
|
+
|
181
|
+
describe("MyRollup", function () {
|
182
|
+
let myru, committer, db, col_id, ru
|
183
|
+
this.timeout(0)
|
184
|
+
|
185
|
+
beforeEach(async () => {
|
186
|
+
const dep = await loadFixture(deploy)
|
187
|
+
myru = dep.myru
|
188
|
+
committer = dep.committer
|
189
|
+
})
|
190
|
+
|
191
|
+
it("should verify rollup transactions", async function () {
|
192
|
+
db = new DB({
|
193
|
+
level: 100,
|
194
|
+
size_path: 5,
|
195
|
+
size_val: 5,
|
196
|
+
size_json: 256,
|
197
|
+
size_txs: 10,
|
198
|
+
level_col: 8,
|
199
|
+
wasmRU: resolve(
|
200
|
+
__dirname,
|
201
|
+
"../../circom/build/circuits/rollup/index_js/index.wasm"
|
202
|
+
),
|
203
|
+
zkeyRU: resolve(
|
204
|
+
__dirname,
|
205
|
+
"../../circom/build/circuits/rollup/index_0001.zkey"
|
206
|
+
),
|
207
|
+
wasm: resolve(
|
208
|
+
__dirname,
|
209
|
+
"../../circom/build/circuits/db/index_js/index.wasm"
|
210
|
+
),
|
211
|
+
zkey: resolve(
|
212
|
+
__dirname,
|
213
|
+
"../../circom/build/circuits/db/index_0001.zkey"
|
214
|
+
),
|
215
|
+
})
|
216
|
+
await db.init()
|
217
|
+
col_id = await db.addCollection()
|
218
|
+
const people = [
|
219
|
+
{ name: "Bob", age: 10 },
|
220
|
+
{ name: "Alice", age: 20 },
|
221
|
+
{ name: "Mike", age: 30 },
|
222
|
+
{ name: "Beth", age: 40 },
|
223
|
+
]
|
224
|
+
let txs = people.map(v => {
|
225
|
+
return [col_id, v.name, v]
|
226
|
+
})
|
227
|
+
const zkp = await db.genRollupProof(txs)
|
228
|
+
await myru.commit(zkp)
|
229
|
+
|
230
|
+
const zkp2 = await db.genProof({
|
231
|
+
json: people[0],
|
232
|
+
col_id,
|
233
|
+
path: "age",
|
234
|
+
id: "Bob",
|
235
|
+
})
|
236
|
+
|
237
|
+
expect(
|
238
|
+
(
|
239
|
+
await myru.qInt([col_id, toIndex("Bob"), ...path("age")], zkp2)
|
240
|
+
).toNumber()
|
241
|
+
).to.eql(10)
|
242
|
+
|
243
|
+
const zkp3 = await db.genProof({
|
244
|
+
json: people[3],
|
245
|
+
col_id,
|
246
|
+
path: "name",
|
247
|
+
id: "Beth",
|
248
|
+
})
|
249
|
+
expect(
|
250
|
+
await myru.qString([col_id, toIndex("Beth"), ...path("name")], zkp3)
|
251
|
+
).to.eql("Beth")
|
252
|
+
})
|
253
|
+
})
|
254
|
+
```
|