zkjson 0.6.0 → 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/sdk/contracts/NORollup.sol +13 -0
  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,100 @@
1
+ const chai = require("chai")
2
+ const { join } = require("path")
3
+ const wasm_tester = require("circom_tester").wasm
4
+ const gen = require("./gen")
5
+ const gen2 = require("./gen2")
6
+ const json = {
7
+ num: 5,
8
+ str: "str",
9
+ bool: false,
10
+ bool2: true,
11
+ null: null,
12
+ arr: [1, 2, 3],
13
+ arr2: [null, 1.5, false, "str"],
14
+ }
15
+ const cases = [
16
+ ["num", ["$eq", 5]],
17
+ ["num", ["$gt", 4]],
18
+ ["num", ["$lt", 6]],
19
+ ["num", ["$gte", 5]],
20
+ ["num", ["$lte", 5]],
21
+ ["num", ["$ne", 6]],
22
+ ["num", ["$in", [5, 6]]],
23
+ ["num", ["$nin", [6, 7]]],
24
+
25
+ ["num", ["$eq", 4], true],
26
+ ["num", ["$gt", 5], true],
27
+ ["num", ["$lt", 4], true],
28
+ ["num", ["$gte", 6], true],
29
+ ["num", ["$lte", 4], true],
30
+ ["num", ["$ne", 5], true],
31
+ ["num", ["$in", [6, 7]], true],
32
+ ["num", ["$nin", [5, 6, 7]], true],
33
+
34
+ ["str", ["$eq", "str"]],
35
+ ["str", ["$gt", "sta"]],
36
+ ["str", ["$lt", "str2"]],
37
+ ["str", ["$gte", "str"]],
38
+ ["str", ["$lte", "str"]],
39
+ ["str", ["$ne", "st"]],
40
+
41
+ ["bool", ["$eq", false]],
42
+ ["bool2", ["$gt", false]],
43
+ ["bool", ["$lt", true]],
44
+ ["bool", ["$gte", false]],
45
+ ["bool", ["$lte", false]],
46
+ ["bool", ["$ne", true]],
47
+
48
+ ["arr", ["$contains", 2]],
49
+ ["arr", ["$contains", 0], true],
50
+ ["arr", ["$contains_any", [0, 2]]],
51
+ ["arr", ["$contains_any", [0, 5]], true],
52
+ ["arr", ["$contains_all", [1, 2]]],
53
+ ["arr", ["$contains_all", [1, 2, 5]], true],
54
+ ["arr", ["$contains_none", [0, 4, 5]]],
55
+ ["arr", ["$contains_none", [0, 2, 4, 5]], true],
56
+
57
+ ["arr2", ["$contains", null]],
58
+ ["arr2", ["$contains", 1.5]],
59
+ ["arr2", ["$contains", false]],
60
+ ["arr2", ["$contains", "str"]],
61
+ ["arr2", ["$contains", true], true],
62
+ ["arr2", ["$contains_any", [1.2, "str"]]],
63
+ ["arr2", ["$contains_any", [0, true]], true],
64
+ ["arr2", ["$contains_all", [null, false]]],
65
+ ["arr2", ["$contains_all", [null, false, 5]], true],
66
+ ["arr2", ["$contains_none", ["str2", true, 1.4]]],
67
+ ["arr2", ["$contains_none", [1.4, "str2", null]], true],
68
+ ]
69
+ describe("JSON circuit", function () {
70
+ let circuit
71
+ this.timeout(0)
72
+
73
+ before(async () => {
74
+ circuit = await wasm_tester(join(__dirname, "index.circom"))
75
+ await circuit.loadSymbols()
76
+ })
77
+
78
+ it.only("should insert docs", async () => {
79
+ const { inputs } = await gen({})
80
+ const w = await circuit.calculateWitness(inputs, true)
81
+ await circuit.checkConstraints(w)
82
+ await circuit.assertOut(w, { exist: 1 })
83
+
84
+ for (let v of cases) {
85
+ console.log(v)
86
+ const { inputs } = await gen2({ json, path: v[0], query: v[1] })
87
+ try {
88
+ const w = await circuit.calculateWitness(inputs, true)
89
+ await circuit.checkConstraints(w)
90
+ await circuit.assertOut(w, { exist: 1 })
91
+ if (v[2]) throw Error("should throw error")
92
+ } catch (e) {
93
+ if (!v[2]) {
94
+ console.log(e)
95
+ throw Error("should not throw error")
96
+ }
97
+ }
98
+ }
99
+ })
100
+ })
@@ -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 = 8778953153895167140801159972138887693000611366500157505127608636174382054606;
41
+ uint256 constant deltax2 = 7156402624745626817088873620667212892373193048534817918048444344582127808199;
42
+ uint256 constant deltay1 = 15409977163028627745606562064378045641943443717311465348895192400331775112959;
43
+ uint256 constant deltay2 = 6127749910612865802775348493533411564760857202041068074885879457830771131731;
44
+
45
+
46
+ uint256 constant IC0x = 14149961698706401474756013637219762589886084844706330955521170102253661785782;
47
+ uint256 constant IC0y = 12234408390949945565728151697713577721072768882766707772269268661650572015929;
48
+
49
+ uint256 constant IC1x = 15287398227625205558818431588910290770512662117774041546723640633129931357061;
50
+ uint256 constant IC1y = 16012938837910940678360804184951691699846514147461899602384360632673883428896;
51
+
52
+ uint256 constant IC2x = 9418709953143181098924564786961115384655969340686778743507647523173886049039;
53
+ uint256 constant IC2y = 11367527508005828024999408976465266676043307816796847517725311003575198690479;
54
+
55
+ uint256 constant IC3x = 6030863567192013675941022755318414592630768333244345100569879259036699787359;
56
+ uint256 constant IC3y = 14659074011411032793161383706226526849424995839698828898401936143049675949239;
57
+
58
+ uint256 constant IC4x = 19963179709991794771873805927403251489690353439768757800630946653626635161707;
59
+ uint256 constant IC4y = 1470766587651703535085614791279830514388462305603574340659937640856761424395;
60
+
61
+ uint256 constant IC5x = 14659390081523554628219805325675728281784994838522534096490755865319885123083;
62
+ uint256 constant IC5y = 11287441218915253838012709899838816796039024083285099779106808382644409454441;
63
+
64
+ uint256 constant IC6x = 16250390867585764345415023080703876700586439759793624872982224204111668882485;
65
+ uint256 constant IC6y = 8035681254387642287763926439378674113266797781140623660108466958190513530084;
66
+
67
+ uint256 constant IC7x = 16179522891432586899572983586580199373249206719523536115810454935385979852482;
68
+ uint256 constant IC7y = 21493117163996505943733751819663993908203969619214657020729691663220708780018;
69
+
70
+ uint256 constant IC8x = 10514168627673971656312201849246481479452043276699188440350351264194471133025;
71
+ uint256 constant IC8y = 20133917426168149360907990301877459703907720340598371306707853823386928860250;
72
+
73
+ uint256 constant IC9x = 6266965211637046181111806809564867246649620928402563041803484933568280189957;
74
+ uint256 constant IC9y = 7946958101666049254953231611540370717046038943359039062825104454493066303159;
75
+
76
+ uint256 constant IC10x = 19760592053490991883913906245948207730828356105180720589710426327543408434351;
77
+ uint256 constant IC10y = 2794087805839231510776045227837416461273508102639790771777910214138852571223;
78
+
79
+ uint256 constant IC11x = 18199503593569846044109884213575086619142399095780120064324920465410206678544;
80
+ uint256 constant IC11y = 19237589446350435847357191463973661320008591906666182081064297336131201934242;
81
+
82
+ uint256 constant IC12x = 19573137937282925164552262830726242817408707332199773844604230285268555301250;
83
+ uint256 constant IC12y = 2662418044136441320926682626787705028452732305078998815313537787219417917203;
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,28 @@
1
+ const { DB } = require("../../sdk")
2
+
3
+ const gen = async ({
4
+ size_val = 256,
5
+ size_path = 256,
6
+ size_json = 256,
7
+ level = 168,
8
+ level_col = 8,
9
+ }) => {
10
+ const db = new DB({ size_val, size_path, size_json, level, level_col })
11
+ await db.init()
12
+ const col1 = await db.addCollection()
13
+ const col2 = await db.addCollection()
14
+ await db.insert(col2, "docB", { b: 2 })
15
+ await db.insert(col1, "docB", { b: 2 })
16
+ await db.insert(col1, "docC", { c: 3 })
17
+ await db.insert(col1, "docA", { c: 4 })
18
+ return {
19
+ db,
20
+ inputs: await db.getQueryInputs({
21
+ col_id: col1,
22
+ id: "docA",
23
+ json: { a: 5 },
24
+ }),
25
+ }
26
+ }
27
+
28
+ module.exports = gen
@@ -0,0 +1,11 @@
1
+ const gen = require("./gen")
2
+
3
+ const { writeFileSync } = require("fs")
4
+ const { resolve } = require("path")
5
+
6
+ const main = async () => {
7
+ const { inputs } = await gen({})
8
+ writeFileSync(resolve(__dirname, "input.json"), JSON.stringify(inputs))
9
+ }
10
+
11
+ main()
@@ -0,0 +1,4 @@
1
+ pragma circom 2.1.5;
2
+ include "./query.circom";
3
+
4
+ component main {public [oldRoot]} = Query(8, 168, 256);
@@ -0,0 +1,58 @@
1
+ pragma circom 2.1.5;
2
+ include "../../node_modules/circomlib/circuits/smt/smtprocessor.circom";
3
+ include "../../node_modules/circomlib/circuits/poseidon.circom";
4
+
5
+ template Query (level_col, level, size_json) {
6
+ signal input siblings[level];
7
+ signal input json[size_json];
8
+ signal input newRoot;
9
+ signal input newKey;
10
+ signal input oldRoot;
11
+ signal input isOld0;
12
+ signal input oldValue;
13
+ signal input oldKey;
14
+ signal input oldRoot_db;
15
+ signal input oldKey_db;
16
+ signal input oldValue_db;
17
+ signal input isOld0_db;
18
+ signal input fnc[2];
19
+ signal input siblings_db[level_col];
20
+ signal input newKey_db;
21
+
22
+ signal output new_root;
23
+
24
+ component colVerifier = SMTProcessor(level);
25
+ component all_hash = Poseidon(16);
26
+ component _hash[16];
27
+ for(var i = 0; i < 16; i++){
28
+ _hash[i] = Poseidon(16);
29
+ for(var i2 = 0; i2 < 16; i2++){
30
+ _hash[i].inputs[i2] <== json[i * 16 + i2];
31
+ }
32
+ all_hash.inputs[i] <== _hash[i].out;
33
+ }
34
+ colVerifier.fnc[0] <== fnc[0];
35
+ colVerifier.fnc[1] <== fnc[1];
36
+ colVerifier.oldRoot <== oldRoot;
37
+ colVerifier.oldKey <== oldKey;
38
+ colVerifier.oldValue <== oldValue;
39
+ colVerifier.isOld0 <== isOld0;
40
+ colVerifier.siblings <== siblings;
41
+ colVerifier.newKey <== newKey;
42
+ colVerifier.newValue <== all_hash.out;
43
+ component hash2 = Poseidon(1);
44
+ hash2.inputs[0] <== colVerifier.newRoot;
45
+ component dbVerifier = SMTProcessor(level_col);
46
+ var any = fnc[0] + fnc[1];
47
+ dbVerifier.fnc[0] <== 0;
48
+ dbVerifier.fnc[1] <== any * 1;
49
+ dbVerifier.oldRoot <== oldRoot_db;
50
+ dbVerifier.oldKey <== oldKey_db;
51
+ dbVerifier.oldValue <== oldValue_db;
52
+ dbVerifier.isOld0 <== isOld0_db;
53
+ dbVerifier.siblings <== siblings_db;
54
+ dbVerifier.newKey <== newKey_db;
55
+ dbVerifier.newValue <== hash2.out;
56
+ new_root <== dbVerifier.newRoot;
57
+ new_root === newRoot;
58
+ }
@@ -0,0 +1,20 @@
1
+ const chai = require("chai")
2
+ const path = require("path")
3
+ const wasm_tester = require("circom_tester").wasm
4
+ const gen = require("./gen")
5
+ describe("query circuit", function () {
6
+ let circuit
7
+ this.timeout(1000000000)
8
+
9
+ before(async () => {
10
+ circuit = await wasm_tester(path.join(__dirname, "index.circom"))
11
+ await circuit.loadSymbols()
12
+ })
13
+
14
+ it("should insert docs", async () => {
15
+ const { db, inputs } = await gen({})
16
+ const w = await circuit.calculateWitness(inputs, true)
17
+ await circuit.checkConstraints(w)
18
+ await circuit.assertOut(w, { new_root: db.tree.F.toObject(db.tree.root) })
19
+ })
20
+ })
@@ -0,0 +1,177 @@
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 = 6704900427955721678758249586638916368303811029141592603225412697299455304124;
41
+ uint256 constant deltax2 = 7353370855884931878069291728168185650437182775914092542980559495866480496664;
42
+ uint256 constant deltay1 = 11993435523663888440828749392165960602415880311508676804499497708067210110962;
43
+ uint256 constant deltay2 = 6647277312542926665214557020539072978288588959904606351601139925324959408075;
44
+
45
+
46
+ uint256 constant IC0x = 15289253754091277893525318954574291839301483693173283906134742452767902470222;
47
+ uint256 constant IC0y = 21846493062663745247147161219210004718490873346242532444087558099461060079247;
48
+
49
+ uint256 constant IC1x = 1066269071380619866827478946725442020082923214111010110980211378031199352747;
50
+ uint256 constant IC1y = 21715751807275805052602634140853298892411968663460947996382374612101110991602;
51
+
52
+ uint256 constant IC2x = 13253810294448854222388704169143440151357962805508403809356153136174677926728;
53
+ uint256 constant IC2y = 12010731304231692950197793520089992987779171074691595369454035297053991871556;
54
+
55
+
56
+ // Memory data
57
+ uint16 constant pVk = 0;
58
+ uint16 constant pPairing = 128;
59
+
60
+ uint16 constant pLastMem = 896;
61
+
62
+ function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[2] calldata _pubSignals) public view returns (bool) {
63
+ assembly {
64
+ function checkField(v) {
65
+ if iszero(lt(v, q)) {
66
+ mstore(0, 0)
67
+ return(0, 0x20)
68
+ }
69
+ }
70
+
71
+ // G1 function to multiply a G1 value(x,y) to value in an address
72
+ function g1_mulAccC(pR, x, y, s) {
73
+ let success
74
+ let mIn := mload(0x40)
75
+ mstore(mIn, x)
76
+ mstore(add(mIn, 32), y)
77
+ mstore(add(mIn, 64), s)
78
+
79
+ success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
80
+
81
+ if iszero(success) {
82
+ mstore(0, 0)
83
+ return(0, 0x20)
84
+ }
85
+
86
+ mstore(add(mIn, 64), mload(pR))
87
+ mstore(add(mIn, 96), mload(add(pR, 32)))
88
+
89
+ success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
90
+
91
+ if iszero(success) {
92
+ mstore(0, 0)
93
+ return(0, 0x20)
94
+ }
95
+ }
96
+
97
+ function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
98
+ let _pPairing := add(pMem, pPairing)
99
+ let _pVk := add(pMem, pVk)
100
+
101
+ mstore(_pVk, IC0x)
102
+ mstore(add(_pVk, 32), IC0y)
103
+
104
+ // Compute the linear combination vk_x
105
+
106
+ g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))
107
+
108
+ g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))
109
+
110
+
111
+ // -A
112
+ mstore(_pPairing, calldataload(pA))
113
+ mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
114
+
115
+ // B
116
+ mstore(add(_pPairing, 64), calldataload(pB))
117
+ mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
118
+ mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
119
+ mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
120
+
121
+ // alpha1
122
+ mstore(add(_pPairing, 192), alphax)
123
+ mstore(add(_pPairing, 224), alphay)
124
+
125
+ // beta2
126
+ mstore(add(_pPairing, 256), betax1)
127
+ mstore(add(_pPairing, 288), betax2)
128
+ mstore(add(_pPairing, 320), betay1)
129
+ mstore(add(_pPairing, 352), betay2)
130
+
131
+ // vk_x
132
+ mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
133
+ mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
134
+
135
+
136
+ // gamma2
137
+ mstore(add(_pPairing, 448), gammax1)
138
+ mstore(add(_pPairing, 480), gammax2)
139
+ mstore(add(_pPairing, 512), gammay1)
140
+ mstore(add(_pPairing, 544), gammay2)
141
+
142
+ // C
143
+ mstore(add(_pPairing, 576), calldataload(pC))
144
+ mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
145
+
146
+ // delta2
147
+ mstore(add(_pPairing, 640), deltax1)
148
+ mstore(add(_pPairing, 672), deltax2)
149
+ mstore(add(_pPairing, 704), deltay1)
150
+ mstore(add(_pPairing, 736), deltay2)
151
+
152
+
153
+ let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
154
+
155
+ isOk := and(success, mload(_pPairing))
156
+ }
157
+
158
+ let pMem := mload(0x40)
159
+ mstore(0x40, add(pMem, pLastMem))
160
+
161
+ // Validate that all evaluations ∈ F
162
+
163
+ checkField(calldataload(add(_pubSignals, 0)))
164
+
165
+ checkField(calldataload(add(_pubSignals, 32)))
166
+
167
+ checkField(calldataload(add(_pubSignals, 64)))
168
+
169
+
170
+ // Validate all evaluations
171
+ let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
172
+
173
+ mstore(0, isValid)
174
+ return(0, 0x20)
175
+ }
176
+ }
177
+ }
@@ -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;