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,32 @@
1
+ pragma circom 2.1.5;
2
+
3
+ include "../../node_modules/circomlib/circuits/smt/smtverifier.circom";
4
+ include "../json/json.circom";
5
+
6
+ template Collection (level, size_json, size_path, size_val) {
7
+ signal input path[size_path];
8
+ signal input val[size_val];
9
+ signal input siblings[level];
10
+ signal input root;
11
+ signal input key;
12
+ signal output exist;
13
+ signal input json[size_json];
14
+
15
+ component smtVerifier = SMTVerifier(level);
16
+
17
+ component _json = JSON(size_json, size_path, size_val);
18
+ _json.json <== json;
19
+ _json.path <== path;
20
+ _json.val <== val;
21
+ exist <== _json.exist;
22
+
23
+ smtVerifier.enabled <== 1;
24
+ smtVerifier.fnc <== 0;
25
+ smtVerifier.oldKey <== 0;
26
+ smtVerifier.oldValue <== 0;
27
+ smtVerifier.isOld0 <== 0;
28
+ smtVerifier.root <== root;
29
+ smtVerifier.siblings <== siblings;
30
+ smtVerifier.key <== key;
31
+ smtVerifier.value <== _json.hash;
32
+ }
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ circom index.circom --r1cs --wasm --sym --c \
3
+ && cd index_js \
4
+ && node generate_witness.js index.wasm ../input.json witness.wtns \
5
+ && cd .. \
6
+ && snarkjs groth16 setup index.r1cs ../pot12_final.ptau index_0000.zkey \
7
+ && snarkjs zkey contribute index_0000.zkey index_0001.zkey --name="zkJSON" -v \
8
+ && snarkjs zkey export verificationkey index_0001.zkey verification_key.json \
9
+ && snarkjs groth16 prove index_0001.zkey index_js/witness.wtns proof.json public.json \
10
+ && snarkjs groth16 verify verification_key.json public.json proof.json;
@@ -0,0 +1,34 @@
1
+ const _json = { a: 5 }
2
+ const _path = "a"
3
+ const _val = _json[_path]
4
+ const { Collection } = require("../../sdk")
5
+
6
+ const gen = async ({
7
+ size_val = 8,
8
+ size_path = 4,
9
+ size_json = 256,
10
+ level = 168,
11
+ }) => {
12
+ const col = new Collection({
13
+ size_val,
14
+ size_path,
15
+ size_json,
16
+ level,
17
+ })
18
+ await col.init()
19
+ await col.insert("docA", _json)
20
+ await col.insert("docB", { b: 2 })
21
+ await col.insert("docC", { c: 3 })
22
+ await col.insert("docD", { c: 4 })
23
+ const id = "docA"
24
+ return {
25
+ inputs: await col.getInputs({
26
+ json: _json,
27
+ path: _path,
28
+ val: _val,
29
+ id,
30
+ }),
31
+ }
32
+ }
33
+
34
+ module.exports = gen
@@ -0,0 +1,10 @@
1
+ const { writeFileSync } = require("fs")
2
+ const { resolve } = require("path")
3
+ const gen = require("./gen")
4
+
5
+ const main = async () => {
6
+ const { inputs } = await gen({})
7
+ writeFileSync(resolve(__dirname, "input.json"), JSON.stringify(inputs))
8
+ }
9
+
10
+ main()
@@ -0,0 +1,4 @@
1
+ pragma circom 2.1.5;
2
+ include "./collection.circom";
3
+
4
+ component main {public [key, path, val]} = Collection(168, 256, 4, 8);
@@ -0,0 +1,21 @@
1
+ const chai = require("chai")
2
+ const { join } = require("path")
3
+ const wasm_tester = require("circom_tester").wasm
4
+ const gen = require("./gen")
5
+
6
+ describe("JSON circuit", function () {
7
+ let circuit
8
+ this.timeout(0)
9
+
10
+ before(async () => {
11
+ circuit = await wasm_tester(join(__dirname, "index.circom"))
12
+ await circuit.loadSymbols()
13
+ })
14
+
15
+ it("should insert docs", async () => {
16
+ const { inputs } = await gen({})
17
+ const w = await circuit.calculateWitness(inputs, true)
18
+ await circuit.checkConstraints(w)
19
+ await circuit.assertOut(w, { exist: 1 })
20
+ })
21
+ })
@@ -0,0 +1,247 @@
1
+ // SPDX-License-Identifier: GPL-3.0
2
+ /*
3
+ Copyright 2021 0KIMS association.
4
+
5
+ This file is generated with [snarkJS](https://github.com/iden3/snarkjs).
6
+
7
+ snarkJS is a free software: you can redistribute it and/or modify it
8
+ under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ snarkJS is distributed in the hope that it will be useful, but WITHOUT
13
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15
+ License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with snarkJS. If not, see <https://www.gnu.org/licenses/>.
19
+ */
20
+
21
+ pragma solidity >=0.7.0 <0.9.0;
22
+
23
+ contract Groth16Verifier {
24
+ // Scalar field size
25
+ uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
26
+ // Base field size
27
+ uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
28
+
29
+ // Verification Key data
30
+ uint256 constant alphax = 7332885377758315278340183047560982420908393795468599595091895974752420296340;
31
+ uint256 constant alphay = 332255058049377868589021735422241784808251989994698755461004804814851540428;
32
+ uint256 constant betax1 = 14918511796604743115529252945397806548811923425844505517068409231790256866971;
33
+ uint256 constant betax2 = 211413574403138532859071680094500506786935898681671422463746292741380691281;
34
+ uint256 constant betay1 = 19126853899579995366417647673795374157635727056721981778430611935842415927913;
35
+ uint256 constant betay2 = 19131403844100444556608656382423150552404085133882015610549223179149801306375;
36
+ uint256 constant gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634;
37
+ uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
38
+ uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
39
+ uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;
40
+ uint256 constant deltax1 = 1338748370198218127575268387508409076312941480250234669165702900883542427254;
41
+ uint256 constant deltax2 = 5944519461583765592851695002897468212676384258280428393756094763574287417307;
42
+ uint256 constant deltay1 = 502536598324740699596535780070532709536020500042060714783027661602727712951;
43
+ uint256 constant deltay2 = 18604985036767475398484738404701177229276307989963732975970409681957772187637;
44
+
45
+
46
+ uint256 constant IC0x = 12403800331726348421485640070023823674423206913681342265537374572723902438217;
47
+ uint256 constant IC0y = 9448446283939777777860253421282454156647268766498360160531919705425123698857;
48
+
49
+ uint256 constant IC1x = 15576564573564337854961672998545961514634758398973599327962564171707696834151;
50
+ uint256 constant IC1y = 5262346094625873450038844553075540554542007555767500405058289737565527489009;
51
+
52
+ uint256 constant IC2x = 10969658266647358812276284449166837678231213014459689420652237677753544983747;
53
+ uint256 constant IC2y = 7815350903241609389479934750983079763221188661235945317868173266207778849494;
54
+
55
+ uint256 constant IC3x = 2190462487630150441465491502046887077137573928331005749524337986206689239784;
56
+ uint256 constant IC3y = 5196966567897683498745807303085686340912100613229321591168645871824519900699;
57
+
58
+ uint256 constant IC4x = 13743864360016643454947843941734596008538234629967485215156659750800018033583;
59
+ uint256 constant IC4y = 2480658609495528933038953851011442448229382871562386869529232812038859930259;
60
+
61
+ uint256 constant IC5x = 4698140306949664573910200748410143500855195408076701668513073073959293394792;
62
+ uint256 constant IC5y = 16181527901865738311219438263179815840626779864479450145711461840262179234658;
63
+
64
+ uint256 constant IC6x = 8602461937060587514265805846050143283747014914417066819791186825462308442579;
65
+ uint256 constant IC6y = 16442414688506275512040219630494172806806666556240381789168433934346167892930;
66
+
67
+ uint256 constant IC7x = 8954617776432910660935815853652552727464950945007296801597482409715061547923;
68
+ uint256 constant IC7y = 17637370258347805673970409752145208764778860578047268439605095278060676070627;
69
+
70
+ uint256 constant IC8x = 4733080996892757586529045952377928505759850862133226564301027185519660458631;
71
+ uint256 constant IC8y = 17352496398181433992961284695567035265509108020083490025176867779972235365065;
72
+
73
+ uint256 constant IC9x = 17398999233559211664223731570239950305911742845108770169583083012151386183231;
74
+ uint256 constant IC9y = 12564914675255880752158572558491151591068311488322000410629376147300655450200;
75
+
76
+ uint256 constant IC10x = 5270491944564058027296880391446395145216970292592565612174405926744425173235;
77
+ uint256 constant IC10y = 4203536651892326235386110668203832038718145647596880902178461711561508488571;
78
+
79
+ uint256 constant IC11x = 13158441926038700298066530994184173406073929549976388593303023695505812062946;
80
+ uint256 constant IC11y = 14619979931751583852414460104045450663624193354802220701629872747110819935972;
81
+
82
+ uint256 constant IC12x = 7000815153245707003529630883037888665946548933509849207036041346269367213694;
83
+ uint256 constant IC12y = 19011068219780929174717592903953400203826277024944767972697552713542012163814;
84
+
85
+
86
+ // Memory data
87
+ uint16 constant pVk = 0;
88
+ uint16 constant pPairing = 128;
89
+
90
+ uint16 constant pLastMem = 896;
91
+
92
+ function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[12] calldata _pubSignals) public view returns (bool) {
93
+ assembly {
94
+ function checkField(v) {
95
+ if iszero(lt(v, q)) {
96
+ mstore(0, 0)
97
+ return(0, 0x20)
98
+ }
99
+ }
100
+
101
+ // G1 function to multiply a G1 value(x,y) to value in an address
102
+ function g1_mulAccC(pR, x, y, s) {
103
+ let success
104
+ let mIn := mload(0x40)
105
+ mstore(mIn, x)
106
+ mstore(add(mIn, 32), y)
107
+ mstore(add(mIn, 64), s)
108
+
109
+ success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
110
+
111
+ if iszero(success) {
112
+ mstore(0, 0)
113
+ return(0, 0x20)
114
+ }
115
+
116
+ mstore(add(mIn, 64), mload(pR))
117
+ mstore(add(mIn, 96), mload(add(pR, 32)))
118
+
119
+ success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
120
+
121
+ if iszero(success) {
122
+ mstore(0, 0)
123
+ return(0, 0x20)
124
+ }
125
+ }
126
+
127
+ function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
128
+ let _pPairing := add(pMem, pPairing)
129
+ let _pVk := add(pMem, pVk)
130
+
131
+ mstore(_pVk, IC0x)
132
+ mstore(add(_pVk, 32), IC0y)
133
+
134
+ // Compute the linear combination vk_x
135
+
136
+ g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))
137
+
138
+ g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))
139
+
140
+ g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))
141
+
142
+ g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))
143
+
144
+ g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128)))
145
+
146
+ g1_mulAccC(_pVk, IC6x, IC6y, calldataload(add(pubSignals, 160)))
147
+
148
+ g1_mulAccC(_pVk, IC7x, IC7y, calldataload(add(pubSignals, 192)))
149
+
150
+ g1_mulAccC(_pVk, IC8x, IC8y, calldataload(add(pubSignals, 224)))
151
+
152
+ g1_mulAccC(_pVk, IC9x, IC9y, calldataload(add(pubSignals, 256)))
153
+
154
+ g1_mulAccC(_pVk, IC10x, IC10y, calldataload(add(pubSignals, 288)))
155
+
156
+ g1_mulAccC(_pVk, IC11x, IC11y, calldataload(add(pubSignals, 320)))
157
+
158
+ g1_mulAccC(_pVk, IC12x, IC12y, calldataload(add(pubSignals, 352)))
159
+
160
+
161
+ // -A
162
+ mstore(_pPairing, calldataload(pA))
163
+ mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
164
+
165
+ // B
166
+ mstore(add(_pPairing, 64), calldataload(pB))
167
+ mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
168
+ mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
169
+ mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
170
+
171
+ // alpha1
172
+ mstore(add(_pPairing, 192), alphax)
173
+ mstore(add(_pPairing, 224), alphay)
174
+
175
+ // beta2
176
+ mstore(add(_pPairing, 256), betax1)
177
+ mstore(add(_pPairing, 288), betax2)
178
+ mstore(add(_pPairing, 320), betay1)
179
+ mstore(add(_pPairing, 352), betay2)
180
+
181
+ // vk_x
182
+ mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
183
+ mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
184
+
185
+
186
+ // gamma2
187
+ mstore(add(_pPairing, 448), gammax1)
188
+ mstore(add(_pPairing, 480), gammax2)
189
+ mstore(add(_pPairing, 512), gammay1)
190
+ mstore(add(_pPairing, 544), gammay2)
191
+
192
+ // C
193
+ mstore(add(_pPairing, 576), calldataload(pC))
194
+ mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
195
+
196
+ // delta2
197
+ mstore(add(_pPairing, 640), deltax1)
198
+ mstore(add(_pPairing, 672), deltax2)
199
+ mstore(add(_pPairing, 704), deltay1)
200
+ mstore(add(_pPairing, 736), deltay2)
201
+
202
+
203
+ let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
204
+
205
+ isOk := and(success, mload(_pPairing))
206
+ }
207
+
208
+ let pMem := mload(0x40)
209
+ mstore(0x40, add(pMem, pLastMem))
210
+
211
+ // Validate that all evaluations ∈ F
212
+
213
+ checkField(calldataload(add(_pubSignals, 0)))
214
+
215
+ checkField(calldataload(add(_pubSignals, 32)))
216
+
217
+ checkField(calldataload(add(_pubSignals, 64)))
218
+
219
+ checkField(calldataload(add(_pubSignals, 96)))
220
+
221
+ checkField(calldataload(add(_pubSignals, 128)))
222
+
223
+ checkField(calldataload(add(_pubSignals, 160)))
224
+
225
+ checkField(calldataload(add(_pubSignals, 192)))
226
+
227
+ checkField(calldataload(add(_pubSignals, 224)))
228
+
229
+ checkField(calldataload(add(_pubSignals, 256)))
230
+
231
+ checkField(calldataload(add(_pubSignals, 288)))
232
+
233
+ checkField(calldataload(add(_pubSignals, 320)))
234
+
235
+ checkField(calldataload(add(_pubSignals, 352)))
236
+
237
+ checkField(calldataload(add(_pubSignals, 384)))
238
+
239
+
240
+ // Validate all evaluations
241
+ let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
242
+
243
+ mstore(0, isValid)
244
+ return(0, 0x20)
245
+ }
246
+ }
247
+ }
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ circom index.circom --r1cs --wasm --sym --c \
3
+ && cd index_js \
4
+ && node generate_witness.js index.wasm ../input.json witness.wtns \
5
+ && cd .. \
6
+ && snarkjs groth16 setup index.r1cs ../pot12_final.ptau index_0000.zkey \
7
+ && snarkjs zkey contribute index_0000.zkey index_0001.zkey --name="zkJSON" -v \
8
+ && snarkjs zkey export verificationkey index_0001.zkey verification_key.json \
9
+ && snarkjs groth16 prove index_0001.zkey index_js/witness.wtns proof.json public.json \
10
+ && snarkjs groth16 verify verification_key.json public.json proof.json;
@@ -0,0 +1,39 @@
1
+ pragma circom 2.1.5;
2
+ include "../../node_modules/circomlib/circuits/smt/smtverifier.circom";
3
+ include "../collection/collection.circom";
4
+ include "../../node_modules/circomlib/circuits/poseidon.circom";
5
+
6
+ template DB (level_col, level, size_json, size_path, size_val) {
7
+ signal input path[size_path];
8
+ signal input val[size_val];
9
+ signal input json[size_json];
10
+ signal input siblings[level];
11
+ signal input col_siblings[level_col];
12
+ signal input col_root;
13
+ signal input col_key;
14
+ signal input root;
15
+ signal input key;
16
+ signal output exist;
17
+
18
+ component smtVerifier = SMTVerifier(level_col);
19
+ component hash = Poseidon(1);
20
+ hash.inputs[0] <== root;
21
+ smtVerifier.enabled <== 1;
22
+ smtVerifier.fnc <== 0;
23
+ smtVerifier.oldKey <== 0;
24
+ smtVerifier.oldValue <== 0;
25
+ smtVerifier.isOld0 <== 0;
26
+ smtVerifier.root <== col_root;
27
+ smtVerifier.siblings <== col_siblings;
28
+ smtVerifier.key <== col_key;
29
+ smtVerifier.value <== hash.out;
30
+
31
+ component _coll = Collection(level, size_json, size_path, size_val);
32
+ _coll.path <== path;
33
+ _coll.val <== val;
34
+ _coll.siblings <== siblings;
35
+ _coll.json <== json;
36
+ _coll.root <== root;
37
+ _coll.key <== key;
38
+ exist <== _coll.exist;
39
+ }
@@ -0,0 +1,30 @@
1
+ const { DB } = require("../../sdk")
2
+
3
+ const _json = { a: 5 }
4
+ const _path = "a"
5
+ const _val = _json[_path]
6
+
7
+ const gen = async ({
8
+ size_val = 8,
9
+ size_path = 4,
10
+ size_json = 256,
11
+ level = 168,
12
+ level_col = 8,
13
+ }) => {
14
+ const db = new DB({ size_val, size_path, size_json, level, level_col })
15
+ await db.init()
16
+ const col_index = await db.addCollection()
17
+ await db.insert(col_index, "docA", _json)
18
+ await db.insert(col_index, "docB", { b: 2 })
19
+ return {
20
+ inputs: await db.getInputs({
21
+ col_id: col_index,
22
+ id: "docA",
23
+ json: _json,
24
+ path: _path,
25
+ val: _val,
26
+ }),
27
+ }
28
+ }
29
+
30
+ module.exports = gen
@@ -0,0 +1,10 @@
1
+ const { writeFileSync } = require("fs")
2
+ const { resolve } = require("path")
3
+ const gen = require("./gen")
4
+
5
+ const main = async () => {
6
+ const { inputs } = await gen({})
7
+ writeFileSync(resolve(__dirname, "input.json"), JSON.stringify(inputs))
8
+ }
9
+
10
+ main()
@@ -0,0 +1,4 @@
1
+ pragma circom 2.1.5;
2
+ include "./db.circom";
3
+
4
+ component main {public [col_key, key, path, val, col_root]} = DB(8, 168, 256, 4, 8);
@@ -0,0 +1,21 @@
1
+ const chai = require("chai")
2
+ const { join } = require("path")
3
+ const wasm_tester = require("circom_tester").wasm
4
+ const gen = require("./gen")
5
+
6
+ describe("JSON circuit", function () {
7
+ let circuit
8
+ this.timeout(1000000000)
9
+
10
+ before(async () => {
11
+ circuit = await wasm_tester(join(__dirname, "index.circom"))
12
+ await circuit.loadSymbols()
13
+ })
14
+
15
+ it("should insert docs", async () => {
16
+ const { inputs } = await gen({})
17
+ const w = await circuit.calculateWitness(inputs, true)
18
+ await circuit.checkConstraints(w)
19
+ await circuit.assertOut(w, { exist: 1 })
20
+ })
21
+ })