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.
Files changed (187) hide show
  1. package/README.md +537 -0
  2. package/assets/collection.png +0 -0
  3. package/assets/db.png +0 -0
  4. package/assets/encode.png +0 -0
  5. package/assets/query.png +0 -0
  6. package/assets/rollup.png +0 -0
  7. package/assets/structure.png +0 -0
  8. package/assets/weavedb.png +0 -0
  9. package/assets/zkjson.png +0 -0
  10. package/benchmark/components/ui/avatar.jsx +49 -0
  11. package/benchmark/components/ui/checkbox.jsx +17 -0
  12. package/benchmark/components/ui/close-button.jsx +20 -0
  13. package/benchmark/components/ui/color-mode.jsx +89 -0
  14. package/benchmark/components/ui/dialog.jsx +54 -0
  15. package/benchmark/components/ui/drawer.jsx +44 -0
  16. package/benchmark/components/ui/field.jsx +22 -0
  17. package/benchmark/components/ui/input-group.jsx +39 -0
  18. package/benchmark/components/ui/popover.jsx +49 -0
  19. package/benchmark/components/ui/provider.jsx +12 -0
  20. package/benchmark/components/ui/radio.jsx +17 -0
  21. package/benchmark/components/ui/slider.jsx +107 -0
  22. package/benchmark/components/ui/tooltip.jsx +35 -0
  23. package/benchmark/jsconfig.json +7 -0
  24. package/benchmark/lib/cbor.js +387 -0
  25. package/benchmark/lib/msgpack.js +651 -0
  26. package/benchmark/next.config.mjs +6 -0
  27. package/benchmark/package.json +22 -0
  28. package/benchmark/pages/_app.js +9 -0
  29. package/benchmark/pages/_document.js +13 -0
  30. package/benchmark/pages/api/hello.js +5 -0
  31. package/benchmark/pages/index.js +438 -0
  32. package/benchmark/public/favicon.ico +0 -0
  33. package/benchmark/public/file.svg +1 -0
  34. package/benchmark/public/globe.svg +1 -0
  35. package/benchmark/public/next.svg +1 -0
  36. package/benchmark/public/vercel.svg +1 -0
  37. package/benchmark/public/window.svg +1 -0
  38. package/benchmark/yarn.lock +2448 -0
  39. package/circom/collection/collection.circom +32 -0
  40. package/circom/collection/compile.sh +10 -0
  41. package/circom/collection/gen.js +34 -0
  42. package/circom/collection/generateInput.js +10 -0
  43. package/circom/collection/index.circom +4 -0
  44. package/circom/collection/test.js +21 -0
  45. package/circom/collection/verifier.sol +247 -0
  46. package/circom/db/compile.sh +10 -0
  47. package/circom/db/db.circom +39 -0
  48. package/circom/db/gen.js +30 -0
  49. package/circom/db/generateInput.js +10 -0
  50. package/circom/db/index.circom +4 -0
  51. package/circom/db/test.js +21 -0
  52. package/circom/db/verifier.sol +261 -0
  53. package/circom/ipfs/calculate_total.circom +17 -0
  54. package/circom/ipfs/compile.sh +10 -0
  55. package/circom/ipfs/gen.js +18 -0
  56. package/circom/ipfs/generateInput.js +10 -0
  57. package/circom/ipfs/index.circom +4 -0
  58. package/circom/ipfs/ipfs.circom +62 -0
  59. package/circom/ipfs/parse.circom +289 -0
  60. package/circom/ipfs/sha256.circom +99 -0
  61. package/circom/ipfs/test.js +167 -0
  62. package/circom/ipfs/verifier.sol +261 -0
  63. package/circom/json/compile.sh +10 -0
  64. package/circom/json/gen.js +22 -0
  65. package/circom/json/gen2.js +21 -0
  66. package/circom/json/generateInput.js +20 -0
  67. package/circom/json/index.circom +4 -0
  68. package/circom/json/json.circom +81 -0
  69. package/circom/json/test.js +100 -0
  70. package/circom/json/verifier.sol +247 -0
  71. package/circom/query/compile.sh +10 -0
  72. package/circom/query/gen.js +28 -0
  73. package/circom/query/generateInput.js +11 -0
  74. package/circom/query/index.circom +4 -0
  75. package/circom/query/query.circom +58 -0
  76. package/circom/query/test.js +20 -0
  77. package/circom/query/verifier.sol +177 -0
  78. package/circom/rollup/compile.sh +10 -0
  79. package/circom/rollup/gen.js +36 -0
  80. package/circom/rollup/generateInput.js +11 -0
  81. package/circom/rollup/index.circom +4 -0
  82. package/circom/rollup/rollup.circom +43 -0
  83. package/circom/rollup/test.js +35 -0
  84. package/circom/rollup/verifier.sol +240 -0
  85. package/circom/scripts/ceremony.js +39 -0
  86. package/circom/scripts/ceremony.sh +6 -0
  87. package/circom/scripts/compile.js +113 -0
  88. package/circom/scripts/compile.sh +16 -0
  89. package/circom/utils/uint.circom +512 -0
  90. package/circom/utils/utils.circom +628 -0
  91. package/demos/arweave/jsconfig.json +7 -0
  92. package/demos/arweave/lib/ZKArweave.json +617 -0
  93. package/demos/arweave/lib/encoder.js +375 -0
  94. package/demos/arweave/next.config.js +6 -0
  95. package/demos/arweave/package.json +26 -0
  96. package/demos/arweave/pages/_app.js +9 -0
  97. package/demos/arweave/pages/_document.js +19 -0
  98. package/demos/arweave/pages/api/arweave.js +31 -0
  99. package/demos/arweave/pages/api/hello.js +5 -0
  100. package/demos/arweave/pages/index.js +869 -0
  101. package/demos/arweave/public/circuit.wasm +0 -0
  102. package/demos/arweave/public/circuit_final.zkey +0 -0
  103. package/demos/arweave/public/verification_key.json +149 -0
  104. package/demos/arweave/vercel.json +7 -0
  105. package/demos/arweave/yarn.lock +2510 -0
  106. package/demos/simple/jsconfig.json +7 -0
  107. package/demos/simple/lib/encoder.js +375 -0
  108. package/demos/simple/next.config.js +6 -0
  109. package/demos/simple/package.json +24 -0
  110. package/demos/simple/pages/_app.js +9 -0
  111. package/demos/simple/pages/_document.js +13 -0
  112. package/demos/simple/pages/api/hello.js +5 -0
  113. package/demos/simple/pages/index.js +342 -0
  114. package/demos/simple/public/circuit.wasm +0 -0
  115. package/demos/simple/public/circuit_final.zkey +0 -0
  116. package/demos/simple/public/verification_key.json +149 -0
  117. package/demos/simple/yarn.lock +2519 -0
  118. package/docs/README.md +17 -0
  119. package/docs/simple-zkjson.md +210 -0
  120. package/docs/solidity-contracts.md +216 -0
  121. package/docs/zk-circuits.md +125 -0
  122. package/docs/zkdb-rollup.md +218 -0
  123. package/docs/zkjson-sdk.md +254 -0
  124. package/docs/zkjson-v1_5.md +365 -0
  125. package/package.json +16 -7
  126. package/{contracts → sdk/contracts}/NORollup.sol +0 -2
  127. package/sdk/package.json +15 -0
  128. package/sdk/yarn.lock +881 -0
  129. package/solidity/README.md +13 -0
  130. package/solidity/arguments.js +4 -0
  131. package/solidity/contracts/NORollup.sol +13 -0
  132. package/solidity/contracts/OPRollup.sol +14 -0
  133. package/solidity/contracts/ZKIPFS.sol +95 -0
  134. package/solidity/contracts/ZKJson.sol +21 -0
  135. package/solidity/contracts/ZKQuery.sol +286 -0
  136. package/solidity/contracts/ZKRollup.sol +35 -0
  137. package/solidity/contracts/apps/NORU.sol +66 -0
  138. package/solidity/contracts/apps/SimpleJSON.sol +64 -0
  139. package/solidity/contracts/apps/SimpleOPRU.sol +67 -0
  140. package/solidity/contracts/apps/SimpleRU.sol +67 -0
  141. package/solidity/contracts/apps/Token.sol +12 -0
  142. package/solidity/contracts/apps/ZKArweave.sol +89 -0
  143. package/solidity/contracts/apps/ZKBridge.sol +74 -0
  144. package/solidity/contracts/apps/ZKNFT.sol +63 -0
  145. package/solidity/contracts/verifiers/verifier_db.sol +275 -0
  146. package/solidity/contracts/verifiers/verifier_ipfs.sol +464 -0
  147. package/solidity/contracts/verifiers/verifier_json.sol +261 -0
  148. package/solidity/contracts/verifiers/verifier_rollup.sol +240 -0
  149. package/solidity/package.json +27 -0
  150. package/solidity/scripts/deploy.js +33 -0
  151. package/solidity/test/NORU.js +66 -0
  152. package/solidity/test/ZKArweave.js +97 -0
  153. package/solidity/test/arweave.js +55 -0
  154. package/solidity/test/bridge.js +71 -0
  155. package/solidity/test/simple.js +76 -0
  156. package/solidity/test/simpleOPRU.js +98 -0
  157. package/solidity/test/simpleRU.js +94 -0
  158. package/solidity/test/zknft.js +98 -0
  159. package/solidity/yarn.lock +4152 -0
  160. package/test/test.js +304 -0
  161. /package/{circomlibjs.js → sdk/circomlibjs.js} +0 -0
  162. /package/{collection.js → sdk/collection.js} +0 -0
  163. /package/{contracts → sdk/contracts}/OPRollup.sol +0 -0
  164. /package/{contracts → sdk/contracts}/ZKIPFS.sol +0 -0
  165. /package/{contracts → sdk/contracts}/ZKJson.sol +0 -0
  166. /package/{contracts → sdk/contracts}/ZKQuery.sol +0 -0
  167. /package/{contracts → sdk/contracts}/ZKRollup.sol +0 -0
  168. /package/{contracts → sdk/contracts}/apps/SimpleJSON.sol +0 -0
  169. /package/{contracts → sdk/contracts}/apps/SimpleOPRU.sol +0 -0
  170. /package/{contracts → sdk/contracts}/apps/SimpleRU.sol +0 -0
  171. /package/{contracts → sdk/contracts}/apps/Token.sol +0 -0
  172. /package/{contracts → sdk/contracts}/apps/ZKArweave.sol +0 -0
  173. /package/{contracts → sdk/contracts}/apps/ZKBridge.sol +0 -0
  174. /package/{contracts → sdk/contracts}/apps/ZKNFT.sol +0 -0
  175. /package/{contracts → sdk/contracts}/verifiers/verifier_db.sol +0 -0
  176. /package/{contracts → sdk/contracts}/verifiers/verifier_ipfs.sol +0 -0
  177. /package/{contracts → sdk/contracts}/verifiers/verifier_json.sol +0 -0
  178. /package/{contracts → sdk/contracts}/verifiers/verifier_rollup.sol +0 -0
  179. /package/{db.js → sdk/db.js} +0 -0
  180. /package/{doc.js → sdk/doc.js} +0 -0
  181. /package/{encoder-v1_5.js → sdk/encoder-v1_5.js} +0 -0
  182. /package/{encoder.js → sdk/encoder.js} +0 -0
  183. /package/{index.js → sdk/index.js} +0 -0
  184. /package/{json.js → sdk/json.js} +0 -0
  185. /package/{nft.js → sdk/nft.js} +0 -0
  186. /package/{parse.js → sdk/parse.js} +0 -0
  187. /package/{uint.js → sdk/uint.js} +0 -0
@@ -0,0 +1,97 @@
1
+ const snarkjs = require("snarkjs")
2
+ const { utils } = require("ethers")
3
+ const { newMemEmptyTrie, buildPoseidon } = require("../../sdk/circomlibjs")
4
+ const { resolve } = require("path")
5
+ const { range, splitEvery } = require("ramda")
6
+ const {
7
+ pad,
8
+ encode,
9
+ decode,
10
+ encodePath,
11
+ decodePath,
12
+ encodeVal,
13
+ decodeVal,
14
+ str2id,
15
+ toSignal,
16
+ } = require("../../sdk")
17
+
18
+ module.exports = class ZKArweave {
19
+ constructor(zkdb, size_path, size_val, size_json) {
20
+ this.zkdb = zkdb
21
+ this.size_path = size_path
22
+ this.size_val = size_val
23
+ this.size_json = size_json
24
+ }
25
+ async genProof(_path, _json) {
26
+ const json = pad(toSignal(encode(_json)), this.size_json)
27
+ const path = pad(toSignal(encodePath(_path)), this.size_path)
28
+ const val = pad(toSignal(encodeVal(_json[_path])), this.size_val)
29
+ const _inputs = { json, path, val }
30
+ const { proof, publicSignals } = await snarkjs.groth16.fullProve(
31
+ _inputs,
32
+ resolve(
33
+ __dirname,
34
+ "../../circom/build/circuits/json/index_js/index.wasm",
35
+ ),
36
+ resolve(__dirname, "../../circom/build/circuits/json/index_0001.zkey"),
37
+ )
38
+ return [
39
+ ...proof.pi_a.slice(0, 2),
40
+ ...proof.pi_b[0].slice(0, 2).reverse(),
41
+ ...proof.pi_b[1].slice(0, 2).reverse(),
42
+ ...proof.pi_c.slice(0, 2),
43
+ ...publicSignals,
44
+ ]
45
+ }
46
+ async genHash(_json) {
47
+ const _value = pad(toSignal(encode(_json)), this.size_json)
48
+ const tree = await newMemEmptyTrie()
49
+ const poseidon = await buildPoseidon()
50
+ let _hash_value = _value
51
+ if (_value.length === 256) {
52
+ _hash_value = []
53
+ for (let v of splitEvery(16, _value)) {
54
+ const poseidon = await buildPoseidon()
55
+ const value = poseidon(v)
56
+ _hash_value.push(value)
57
+ }
58
+ }
59
+ const value = poseidon(_hash_value)
60
+ return tree.F.toObject(value).toString()
61
+ }
62
+
63
+ async query(txid, _path, _json, pkp) {
64
+ const val = _json[_path]
65
+ const inputs = await this.genProof(_path, _json)
66
+ const hash = await this.zkdb.getMessageHash(txid, inputs[9])
67
+ const sig = await pkp.signMessage(utils.arrayify(hash))
68
+ const sigs = inputs.slice(8)
69
+ const params = [txid, sigs.slice(2, 7), inputs, sig]
70
+ let type =
71
+ val === null
72
+ ? 0
73
+ : typeof val === "string"
74
+ ? 3
75
+ : typeof val === "boolean"
76
+ ? 1
77
+ : typeof val === "number"
78
+ ? Number.isInteger(val)
79
+ ? 2
80
+ : 2.5
81
+ : 4
82
+ switch (type) {
83
+ case 0:
84
+ return await this.zkdb.qNull(...params)
85
+ case 1:
86
+ return await this.zkdb.qBool(...params)
87
+ case 2:
88
+ return (await this.zkdb.qInt(...params)).toString() * 1
89
+ case 2.5:
90
+ return (await this.zkdb.qFloat(...params)).map(n => n.toString() * 1)
91
+ case 3:
92
+ return await this.zkdb.qString(...params)
93
+ case 4:
94
+ return (await this.zkdb.qRaw(...params)).map(n => n.toString() * 1)
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,55 @@
1
+ const snarkjs = require("snarkjs")
2
+ const { splitEvery } = require("ramda")
3
+ const { newMemEmptyTrie, buildPoseidon } = require("../../sdk/circomlibjs")
4
+ const {
5
+ pad,
6
+ encode,
7
+ decode,
8
+ encodePath,
9
+ decodePath,
10
+ encodeVal,
11
+ decodeVal,
12
+ toSignal,
13
+ } = require("../../sdk")
14
+ const { Wallet, utils } = require("ethers")
15
+ const { resolve } = require("path")
16
+ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
17
+ const { expect } = require("chai")
18
+ const ZKAR = require("./ZKArweave")
19
+ async function deploy() {
20
+ const [owner, otherAccount] = await ethers.getSigners()
21
+ const pkp = Wallet.createRandom()
22
+ const VerifierJSON = await ethers.getContractFactory("Groth16VerifierJSON")
23
+ const verifierJSON = await VerifierJSON.deploy()
24
+ const ZKAR = await ethers.getContractFactory("ZKArweave")
25
+ const zkdb = await ZKAR.deploy(verifierJSON.address, pkp.address)
26
+ return { owner, otherAccount, zkdb, pkp }
27
+ }
28
+
29
+ describe("zkArweave", function () {
30
+ let zkdb, verifier, pkp
31
+ this.timeout(1000000000)
32
+
33
+ beforeEach(async () => {
34
+ const dep = await loadFixture(deploy)
35
+ zkdb = dep.zkdb
36
+ const verifier = dep.verifier
37
+ pkp = dep.pkp
38
+ })
39
+
40
+ it("Should verify rollup transactions", async function () {
41
+ const zkar = new ZKAR(zkdb, 4, 8, 256)
42
+
43
+ const _json = { a: "Hello", b: true, c: null, d: 1.1, e: 5, f: [1, 2, 3] }
44
+ const _path = "a"
45
+ const inputs = await zkar.genProof(_path, _json)
46
+ const txid = "ArweaveTxID"
47
+ expect(await zkar.query(txid, "a", _json, pkp)).to.eql("Hello")
48
+ expect(await zkar.query(txid, "b", _json, pkp)).to.eql(true)
49
+ expect(await zkar.query(txid, "c", _json, pkp)).to.eql(true)
50
+ expect(await zkar.query(txid, "d", _json, pkp)).to.eql([1, 1, 11])
51
+ expect(await zkar.query(txid, "e", _json, pkp)).to.eql(5)
52
+ expect(await zkar.genHash(_json)).to.eql(inputs[9])
53
+ return
54
+ })
55
+ })
@@ -0,0 +1,71 @@
1
+ const { DB } = require("../../sdk")
2
+ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
3
+ const { expect } = require("chai")
4
+ const { resolve } = require("path")
5
+ async function deploy() {
6
+ const [owner, user] = await ethers.getSigners()
7
+ const VerifierDB = await ethers.getContractFactory("Groth16VerifierDB")
8
+ const verifierDB = await VerifierDB.deploy()
9
+ const Verifier = await ethers.getContractFactory("Groth16VerifierRU")
10
+ const verifier = await Verifier.deploy()
11
+ const ZKDB = await ethers.getContractFactory("SimpleOPRU")
12
+ const zkdb = await ZKDB.deploy(
13
+ verifier.address,
14
+ verifierDB.address,
15
+ owner.address,
16
+ )
17
+ const Token = await ethers.getContractFactory("Token")
18
+ const token = await Token.deploy()
19
+ const ZKBridge = await ethers.getContractFactory("ZKBridge")
20
+ const bridge = await ZKBridge.deploy(zkdb.address, token.address)
21
+ return { bridge, verifierDB, verifier, owner, user, zkdb, token }
22
+ }
23
+
24
+ describe("zkDB", function () {
25
+ this.timeout(0)
26
+ it("Should transfer point token", async function () {
27
+ const { token, user, owner, bridge, zkdb } = await loadFixture(deploy)
28
+
29
+ const addr = owner.address.toLowerCase().slice(2, 10)
30
+ const addr2 = user.address.toLowerCase().slice(2, 10)
31
+ const json = { amount: 100, to: user.address.toLowerCase() }
32
+
33
+ const db = new DB({
34
+ wasm: resolve(
35
+ __dirname,
36
+ "../../circom/build/circuits/db/index_js/index.wasm",
37
+ ),
38
+ zkey: resolve(
39
+ __dirname,
40
+ "../../circom/build/circuits/db/index_0001.zkey",
41
+ ),
42
+ })
43
+ await db.init()
44
+ const col = await db.addCollection()
45
+ let txs = [[col, "abc", json]]
46
+ for (const v of txs) await db.insert(...v)
47
+ const root = db.tree.F.toObject(db.tree.root).toString()
48
+ await zkdb.commitRoot(root)
49
+
50
+ const proof = await db.genProof({
51
+ col_id: col,
52
+ id: "abc",
53
+ json: json,
54
+ path: "to",
55
+ })
56
+
57
+ const proof2 = await db.genProof({
58
+ col_id: col,
59
+ id: "abc",
60
+ json: json,
61
+ path: "amount",
62
+ })
63
+ const sigs = proof.slice(8)
64
+ const _col = sigs[14]
65
+ const _doc = sigs[15]
66
+
67
+ expect((await token.balanceOf(user.address)).toNumber()).to.eql(0)
68
+ await bridge.bridge(_col, _doc, proof, proof2)
69
+ expect((await token.balanceOf(user.address)).toNumber()).to.eql(100)
70
+ })
71
+ })
@@ -0,0 +1,76 @@
1
+ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
2
+ const { path, Doc } = require("../../sdk")
3
+ const { resolve } = require("path")
4
+ const { expect } = require("chai")
5
+
6
+ async function deploy() {
7
+ const Verifier = await ethers.getContractFactory("Groth16VerifierJSON")
8
+ const verifier = await Verifier.deploy()
9
+ const MyApp = await ethers.getContractFactory("SimpleJSON")
10
+ const myapp = await MyApp.deploy(verifier.address)
11
+ return { myapp }
12
+ }
13
+
14
+ describe("MyApp", function () {
15
+ let myapp
16
+ this.timeout(0)
17
+
18
+ beforeEach(async () => {
19
+ const dep = await loadFixture(deploy)
20
+ myapp = dep.myapp
21
+ })
22
+
23
+ it("should verify JSON", async function () {
24
+ const doc = new Doc({
25
+ wasm: resolve(
26
+ __dirname,
27
+ "../../circom/build/circuits/json/index_js/index.wasm",
28
+ ),
29
+ zkey: resolve(
30
+ __dirname,
31
+ "../../circom/build/circuits/json/index_0001.zkey",
32
+ ),
33
+ })
34
+ const json = {
35
+ num: 1,
36
+ float: 1.23,
37
+ str: "string",
38
+ bool: true,
39
+ null: null,
40
+ array: [1, 2, 3],
41
+ }
42
+
43
+ // query number
44
+ const zkp = await doc.genProof({ json, path: "num" })
45
+ expect((await myapp.qInt(path("num"), zkp)).toNumber()).to.eql(1)
46
+
47
+ // query string
48
+ const zkp2 = await doc.genProof({ json, path: "str" })
49
+ expect(await myapp.qString(path("str"), zkp2)).to.eql("string")
50
+
51
+ // query bool
52
+ const zkp3 = await doc.genProof({ json, path: "bool" })
53
+ expect(await myapp.qBool(path("bool"), zkp3)).to.eql(true)
54
+
55
+ // query null
56
+ const zkp4 = await doc.genProof({ json, path: "null" })
57
+ expect(await myapp.qNull(path("null"), zkp4)).to.eql(true)
58
+
59
+ // query float
60
+ const zkp5 = await doc.genProof({ json, path: "float" })
61
+ expect(
62
+ (await myapp.qFloat(path("float"), zkp5)).map(f => f.toNumber()),
63
+ ).to.eql([1, 2, 123])
64
+
65
+ // query array and get number
66
+ const zkp6 = await doc.genProof({ json, path: "array" })
67
+ expect(
68
+ (await myapp.qCustom(path("array"), path("[1]"), zkp6)).toNumber(),
69
+ ).to.eql(2)
70
+
71
+ // conditional operator
72
+ const zkp7 = await doc.genProof({ json, path: "num", query: ["$gt", 0] })
73
+ expect(await myapp.qCond(path("num"), zkp7.slice(14), zkp7)).to.eql(true)
74
+ return
75
+ })
76
+ })
@@ -0,0 +1,98 @@
1
+ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
2
+ const { toIndex, path, DB } = require("../../sdk")
3
+ const { resolve } = require("path")
4
+ const { expect } = require("chai")
5
+
6
+ async function deploy() {
7
+ const [committer] = await ethers.getSigners()
8
+ const VerifierRU = await ethers.getContractFactory("Groth16VerifierRU")
9
+ const verifierRU = await VerifierRU.deploy()
10
+ const VerifierDB = await ethers.getContractFactory("Groth16VerifierDB")
11
+ const verifierDB = await VerifierDB.deploy()
12
+
13
+ const MyRU = await ethers.getContractFactory("SimpleOPRU")
14
+ const myru = await MyRU.deploy(
15
+ verifierRU.address,
16
+ verifierDB.address,
17
+ committer.address,
18
+ )
19
+ return { myru, committer }
20
+ }
21
+
22
+ describe("MyRollup", function () {
23
+ let myru, committer, db, col_id, ru
24
+ this.timeout(0)
25
+
26
+ beforeEach(async () => {
27
+ const dep = await loadFixture(deploy)
28
+ myru = dep.myru
29
+ committer = dep.committer
30
+ })
31
+
32
+ it("should verify rollup transactions", async function () {
33
+ db = new DB({
34
+ level: 168,
35
+ size_path: 4,
36
+ size_val: 8,
37
+ size_json: 256,
38
+ size_txs: 10,
39
+ level_col: 8,
40
+ wasmRU: resolve(
41
+ __dirname,
42
+ "../../circom/build/circuits/rollup/index_js/index.wasm",
43
+ ),
44
+ zkeyRU: resolve(
45
+ __dirname,
46
+ "../../circom/build/circuits/rollup/index_0001.zkey",
47
+ ),
48
+ wasm: resolve(
49
+ __dirname,
50
+ "../../circom/build/circuits/db/index_js/index.wasm",
51
+ ),
52
+ zkey: resolve(
53
+ __dirname,
54
+ "../../circom/build/circuits/db/index_0001.zkey",
55
+ ),
56
+ })
57
+ await db.init()
58
+ col_id = await db.addCollection()
59
+ const people = [{ name: "Bob", age: 5 }]
60
+ let txs = people.map(v => {
61
+ return [col_id, v.name, v]
62
+ })
63
+
64
+ for (const v of txs) {
65
+ await db.insert(...v)
66
+ }
67
+ const root = db.tree.F.toObject(db.tree.root).toString()
68
+
69
+ await myru.commitRoot(root)
70
+
71
+ const zkp2 = await db.genProof({
72
+ json: people[0],
73
+ col_id,
74
+ path: "name",
75
+ id: "Bob",
76
+ })
77
+
78
+ expect(
79
+ await myru.qString([col_id, toIndex("Bob"), ...path("name")], zkp2),
80
+ ).to.eql("Bob")
81
+
82
+ const zkp3 = await db.genProof({
83
+ json: people[0],
84
+ col_id,
85
+ path: "age",
86
+ id: "Bob",
87
+ query: ["$gt", 3],
88
+ })
89
+
90
+ expect(
91
+ await myru.qCond(
92
+ [col_id, toIndex("Bob"), ...path("age")],
93
+ zkp3.slice(13, 14),
94
+ zkp3,
95
+ ),
96
+ ).to.eql(true)
97
+ })
98
+ })
@@ -0,0 +1,94 @@
1
+ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
2
+ const { toIndex, path, DB } = require("../../sdk")
3
+ const { resolve } = require("path")
4
+ const { expect } = require("chai")
5
+
6
+ async function deploy() {
7
+ const [committer] = await ethers.getSigners()
8
+ const VerifierRU = await ethers.getContractFactory("Groth16VerifierRU")
9
+ const verifierRU = await VerifierRU.deploy()
10
+ const VerifierDB = await ethers.getContractFactory("Groth16VerifierDB")
11
+ const verifierDB = await VerifierDB.deploy()
12
+
13
+ const MyRU = await ethers.getContractFactory("SimpleRU")
14
+ const myru = await MyRU.deploy(
15
+ verifierRU.address,
16
+ verifierDB.address,
17
+ committer.address,
18
+ )
19
+ return { myru, committer }
20
+ }
21
+
22
+ describe("MyRollup", function () {
23
+ let myru, committer, db, col_id, ru
24
+ this.timeout(0)
25
+
26
+ beforeEach(async () => {
27
+ const dep = await loadFixture(deploy)
28
+ myru = dep.myru
29
+ committer = dep.committer
30
+ })
31
+
32
+ it("should verify rollup transactions", async function () {
33
+ db = new DB({
34
+ level: 168,
35
+ size_path: 4,
36
+ size_val: 8,
37
+ size_json: 256,
38
+ size_txs: 10,
39
+ level_col: 8,
40
+ wasmRU: resolve(
41
+ __dirname,
42
+ "../../circom/build/circuits/rollup/index_js/index.wasm",
43
+ ),
44
+ zkeyRU: resolve(
45
+ __dirname,
46
+ "../../circom/build/circuits/rollup/index_0001.zkey",
47
+ ),
48
+ wasm: resolve(
49
+ __dirname,
50
+ "../../circom/build/circuits/db/index_js/index.wasm",
51
+ ),
52
+ zkey: resolve(
53
+ __dirname,
54
+ "../../circom/build/circuits/db/index_0001.zkey",
55
+ ),
56
+ })
57
+ await db.init()
58
+ col_id = await db.addCollection()
59
+ const people = [
60
+ { name: "Bob", age: 10 },
61
+ { name: "Alice", age: 20 },
62
+ { name: "Mike", age: 30 },
63
+ { name: "Beth", age: 40 },
64
+ ]
65
+ let txs = people.map(v => {
66
+ return [col_id, v.name, v]
67
+ })
68
+ const zkp = await db.genRollupProof(txs)
69
+ await myru.commit(zkp)
70
+
71
+ const zkp2 = await db.genProof({
72
+ json: people[0],
73
+ col_id,
74
+ path: "age",
75
+ id: "Bob",
76
+ })
77
+
78
+ expect(
79
+ (
80
+ await myru.qInt([col_id, toIndex("Bob"), ...path("age")], zkp2)
81
+ ).toNumber(),
82
+ ).to.eql(10)
83
+
84
+ const zkp3 = await db.genProof({
85
+ json: people[3],
86
+ col_id,
87
+ path: "name",
88
+ id: "Beth",
89
+ })
90
+ expect(
91
+ await myru.qString([col_id, toIndex("Beth"), ...path("name")], zkp3),
92
+ ).to.eql("Beth")
93
+ })
94
+ })
@@ -0,0 +1,98 @@
1
+ const NFT = require("../../sdk/nft")
2
+ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers")
3
+ const { expect } = require("chai")
4
+ const { resolve } = require("path")
5
+
6
+ const wasm = resolve(
7
+ __dirname,
8
+ "../../circom/build/circuits/ipfs/index_js/index.wasm",
9
+ )
10
+ const zkey = resolve(
11
+ __dirname,
12
+ "../../circom/build/circuits/ipfs/index_0001.zkey",
13
+ )
14
+
15
+ async function deploy() {
16
+ const [owner, user] = await ethers.getSigners()
17
+ const VerifierIPFS = await ethers.getContractFactory("Groth16VerifierIPFS")
18
+ const verifierIPFS = await VerifierIPFS.deploy()
19
+ const ZKNFT = await ethers.getContractFactory("ZKNFT")
20
+ const zknft = await ZKNFT.deploy(verifierIPFS.address)
21
+ return { zknft, owner, user }
22
+ }
23
+
24
+ describe("zkNFT", function () {
25
+ this.timeout(0)
26
+ it("Should query metadata", async function () {
27
+ const { user, owner, zknft } = await loadFixture(deploy)
28
+ const json = {
29
+ str: "Hello, World!",
30
+ int: 123,
31
+ bool: true,
32
+ null: null,
33
+ float: 1.23,
34
+ arr: [1, 2, 3, true, null, "hello", 3.14],
35
+ }
36
+ const nft = new NFT({ wasm, zkey, json })
37
+ const cid = nft.cid()
38
+ await zknft.mint(user.address, `ipfs://${cid}`)
39
+
40
+ // query string
41
+ expect(
42
+ await zknft.qString(0, nft.path("str"), await nft.zkp("str")),
43
+ ).to.eql(json["str"])
44
+
45
+ // query int
46
+ expect(
47
+ (await zknft.qInt(0, nft.path("int"), await nft.zkp("int"))).toNumber(),
48
+ ).to.eql(json["int"])
49
+
50
+ // query bool
51
+ expect(
52
+ await zknft.qBool(0, nft.path("bool"), await nft.zkp("bool")),
53
+ ).to.eql(json["bool"])
54
+
55
+ // query null
56
+ expect(
57
+ await zknft.qNull(0, nft.path("null"), await nft.zkp("null")),
58
+ ).to.eql(true)
59
+
60
+ // query float
61
+ expect(
62
+ (await zknft.qFloat(0, nft.path("float"), await nft.zkp("float"))).map(
63
+ n => n.toNumber(),
64
+ ),
65
+ ).to.eql([1, 2, 123])
66
+
67
+ // custom
68
+ expect(
69
+ (
70
+ await zknft.qCustom(
71
+ 0,
72
+ nft.path("arr"),
73
+ nft.path("[1]"),
74
+ await nft.zkp("arr"),
75
+ )
76
+ ).toNumber(),
77
+ ).to.eql(2)
78
+
79
+ // query cond
80
+ expect(
81
+ await zknft.qCond(
82
+ 0,
83
+ nft.path("int"),
84
+ nft.query("int", ["$gt", 100]),
85
+ await nft.zkp("int", ["$gt", 100]),
86
+ ),
87
+ ).to.eql(true)
88
+
89
+ expect(
90
+ await zknft.qCond(
91
+ 0,
92
+ nft.path("arr"),
93
+ nft.query("arr", ["$contains", 2]),
94
+ await nft.zkp("arr", ["$contains", 2]),
95
+ ),
96
+ ).to.eql(true)
97
+ })
98
+ })