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
package/test/test.js ADDED
@@ -0,0 +1,304 @@
1
+ const { resolve } = require("path")
2
+ const {
3
+ toIndex,
4
+ fromIndex,
5
+ path,
6
+ val,
7
+ toSignal,
8
+ fromSignal,
9
+ pad,
10
+ encode,
11
+ decode,
12
+ encodePath,
13
+ decodePath,
14
+ encodeVal,
15
+ decodeVal,
16
+ encodeQuery,
17
+ decodeQuery,
18
+ DB,
19
+ Doc,
20
+ } = require("../sdk")
21
+
22
+ const {
23
+ insert,
24
+ slice,
25
+ unshift,
26
+ shift,
27
+ toArray,
28
+ pop,
29
+ length,
30
+ push,
31
+ next,
32
+ arr,
33
+ last,
34
+ replace,
35
+ get,
36
+ pushArray,
37
+ arrPush,
38
+ arrGet,
39
+ popArray,
40
+ remove,
41
+ bn,
42
+ digits,
43
+ } = require("../sdk/uint")
44
+ const { parse } = require("../sdk/parse")
45
+ const { expect } = require("chai")
46
+
47
+ describe("zkJSON", function () {
48
+ this.timeout(0)
49
+ it("should generate proofs", async () => {
50
+ const wasm = resolve(
51
+ __dirname,
52
+ "../circom/build/circuits/db/index_js/index.wasm",
53
+ )
54
+ const zkey = resolve(
55
+ __dirname,
56
+ "../circom/build/circuits/db/index_0001.zkey",
57
+ )
58
+ const bob = { name: "Bob", age: 5 }
59
+ const alice = { name: "Alice", age: 10 }
60
+ const zkdb = new DB({ wasm, zkey })
61
+ await zkdb.init()
62
+ await zkdb.addCollection()
63
+ await zkdb.insert(0, "Bob", bob)
64
+ const zkp = await zkdb.genProof({
65
+ json: bob,
66
+ col_id: 0,
67
+ path: "name",
68
+ id: "Bob",
69
+ })
70
+ await zkdb.addCollection()
71
+ await zkdb.insert(1, "Alice", alice)
72
+ const zkp2 = await zkdb.genProof({
73
+ json: alice,
74
+ col_id: 1,
75
+ path: "age",
76
+ id: "Alice",
77
+ query: ["$gt", 5],
78
+ })
79
+ expect(zkp2[8]).to.eql("1")
80
+ const zkp3 = await zkdb.genProof({
81
+ json: alice,
82
+ col_id: 1,
83
+ path: "age",
84
+ id: "Alice",
85
+ query: ["$lt", 5],
86
+ })
87
+ expect(zkp3[8]).to.eql("0")
88
+ })
89
+ it("should operate on uints 3", () => {
90
+ let len = 256
91
+ const json = {
92
+ str: "Hello, World!",
93
+ int: 123,
94
+ float: 1.23,
95
+ bool: true,
96
+ null: null,
97
+ arr: [1, 2, 3],
98
+ }
99
+ const str = new TextEncoder().encode(JSON.stringify(json))
100
+ let encoded = arr(len)
101
+ for (let v of Array.from(str)) encoded = push(encoded, len, 76, v)
102
+ const enc = parse(encoded, len, 76)
103
+ expect(decode(toArray(enc, len))).to.eql(json)
104
+ })
105
+
106
+ it("should operate on uints 2", () => {
107
+ const json = { a: [{ b: 1 }] }
108
+ const len = 1
109
+ const str = new TextEncoder().encode(JSON.stringify(json))
110
+ let encoded = arr(len)
111
+ for (let v of Array.from(str)) encoded = push(encoded, len, 76, v)
112
+ const enc = parse(encoded, len, 76)
113
+ })
114
+ it("should operate on uints", () => {
115
+ let c = bn([0, 1, 0, 0, 0, 0, 0, 0, 0])
116
+ let nums = bn([2, 32, 4])
117
+ let str = "1"
118
+ for (let v of nums) {
119
+ str += v.toString().length
120
+ str += v.toString()
121
+ }
122
+ for (let i = 0; i < nums.length; i++) {
123
+ c = next([BigInt(str)], c)
124
+ expect(c[0]).to.eql(nums[i])
125
+ }
126
+ let nums2 = bn([1, 2, 3])
127
+ let c2 = bn([0, 1, 0, 0, 0, 0, 0, 0, 0])
128
+ for (let i = 0; i < nums2.length; i++) {
129
+ c2 = next([103123], c2)
130
+ expect(c2[0]).to.eql(nums2[i])
131
+ }
132
+ })
133
+
134
+ it("should parse", () => {
135
+ const len = 2
136
+ const digit = 76
137
+ const j = { test: 3 }
138
+ const uint = new TextEncoder().encode(JSON.stringify(j))
139
+ let json = arr(len)
140
+ for (let v of Array.from(uint)) {
141
+ json = push(json, len, digit, v)
142
+ }
143
+ const enc = parse(json, len, digit)
144
+ expect(decode(toArray(enc, len))).to.eql(j)
145
+ })
146
+
147
+ it("should handle arrays", () => {
148
+ let json = arr(5)
149
+ for (let i = 1; i <= 5; i++) {
150
+ json = push(json, 5, 9, i)
151
+ }
152
+ let json2 = arr(3)
153
+ for (let i = 1; i <= 3; i++) {
154
+ json2 = push(json2, 3, 9, i)
155
+ }
156
+
157
+ let ajson = arr(10)
158
+ ajson = pushArray(ajson, 10, 9, json, 5)
159
+ ajson = pushArray(ajson, 10, 9, json2, 3)
160
+ ajson = arrPush(ajson, 10, 9, 1, 10)
161
+ expect(arrGet(ajson, 10, 9, 1, 3)).to.eql(10n)
162
+ ajson = popArray(ajson, 10, 9)
163
+ expect(toArray(ajson, 9)).to.eql([1, 5, 1, 2, 3, 4, 5])
164
+ })
165
+
166
+ it("should push/pop/shift/unshift", () => {
167
+ let json = arr(5)
168
+ for (let i = 1; i <= 15; i++) {
169
+ json = push(json, 5, 9, i)
170
+ }
171
+ let c = bn([0, 5, 0, 0, 0, 0, 0, 0, 0])
172
+ for (let i = 1n; i <= 15; i++) {
173
+ c = next(json, c)
174
+ expect(c[0]).to.eql(i)
175
+ }
176
+ expect(length(json, 5)).to.eql(15n)
177
+ expect(last(json, 5)).to.eql(15n)
178
+ for (let i = 0; i < 9; i++) json = pop(json, 5)
179
+ expect(json).to.eql(bn([106123456, 0, 0, 0, 0]))
180
+ let json2 = arr(1)
181
+ json2 = push(json2, 1, 9, 2)
182
+ json2 = push(json2, 1, 9, 1234)
183
+ json2 = pop(json2, 1)
184
+ expect(json2).to.eql(bn([112]))
185
+
186
+ json2 = arr(2)
187
+ json2 = push(json2, 2, 9, 2)
188
+ json2 = push(json2, 2, 9, 123456)
189
+ json2 = pop(json2, 2)
190
+ expect(json2).to.eql(bn([112, 0]))
191
+
192
+ let json3 = arr(1)
193
+ json3 = unshift(json3, 1, 9, 5)
194
+ json3 = unshift(json3, 1, 9, 6)
195
+ json3 = unshift(json3, 1, 9, 7)
196
+ expect(json3).to.eql(bn([103765]))
197
+ json3 = shift(json3, 1, 9)
198
+ expect(json3).to.eql(bn([11615]))
199
+ json3 = shift(json3, 1, 9)
200
+ expect(json3).to.eql(bn([115]))
201
+
202
+ let json4 = arr(1)
203
+ json4 = unshift(json4, 1, 9, 5)
204
+ json4 = unshift(json4, 1, 9, 6)
205
+ json4 = unshift(json4, 1, 9, 7)
206
+ expect(json4).to.eql(bn([103765]))
207
+ json4 = slice(json4, 1, 9, 1, 2)
208
+ expect(json4).to.eql(bn([116]))
209
+
210
+ json4 = insert(json4, 1, 9, 0, 8)
211
+ expect(json4).to.eql(bn([11816]))
212
+ json4 = replace(json4, 1, 9, 1, 9)
213
+ expect(json4).to.eql(bn([11819]))
214
+
215
+ expect(get(json4, 1, 1)).to.eql(9n)
216
+ })
217
+
218
+ it("should push with digit overflow", () => {
219
+ let json = arr(4)
220
+ let len = 10
221
+ json = push(json, 4, len, 3)
222
+ json = push(json, 4, len, 12345678)
223
+ json = push(json, 4, len, 12345678)
224
+ expect(json).to.eql(bn([113, 1812345678, 1812345678, 0]))
225
+ })
226
+
227
+ it("should push with single digit", () => {
228
+ let json = arr(1)
229
+ json = push(json, 1, 9, 1)
230
+ json = push(json, 1, 9, 2)
231
+ json = push(json, 1, 9, 3)
232
+ json = push(json, 1, 9, 4)
233
+ expect(json[0]).to.eql(1041234n)
234
+ })
235
+
236
+ it("should encode/decode path", () => {
237
+ let paths = ["a....b", "[1][2]", ""]
238
+ for (const p of paths) {
239
+ const encoded = encodePath(p)
240
+ //console.log(encoded)
241
+ expect(decodePath(encoded)).to.eql(p)
242
+ }
243
+ })
244
+
245
+ it("should encode/decode value", () => {
246
+ let vals = [-3, "", null, false, 10, "str"]
247
+ for (const v of vals) {
248
+ const encoded = encodeVal(v)
249
+ //console.log(encoded)
250
+ expect(decodeVal(encoded)).to.eql(v)
251
+ }
252
+ })
253
+
254
+ it("should encode/decode JSON", () => {
255
+ const enc = encode({
256
+ a: 1,
257
+ c: false,
258
+ b: { e: null, d: "four" },
259
+ f: 3.14,
260
+ ghi: [5, 6, 7],
261
+ })
262
+ let jsons = [
263
+ null,
264
+ false,
265
+ 10,
266
+ "str",
267
+ [1.0, 2, 3],
268
+ {
269
+ "": -1,
270
+ bc: [null, false, 3],
271
+ cd: { def: [4, 5, "str", { e: null, "": 6 }] },
272
+ },
273
+ { "": -3.3 },
274
+ ]
275
+ for (const j of jsons) {
276
+ const encoded = encode(j)
277
+ //console.log(encoded)
278
+ expect(fromSignal(toSignal(encoded))).to.eql(encoded)
279
+ expect(decode(encoded)).to.eql(j)
280
+ }
281
+ })
282
+
283
+ it("should encode/decode query", () => {
284
+ let vals = [
285
+ ["$eq", 3],
286
+ ["$ne", 3],
287
+ ["$gt", 3],
288
+ ["$gte", 3],
289
+ ["$lt", 3],
290
+ ["$lte", 3],
291
+ ["$in", [1, 2, 3]],
292
+ ["$nin", [1, 2, 3]],
293
+ ]
294
+ for (const v of vals) {
295
+ const encoded = encodeQuery(v)
296
+ expect(decodeQuery(encoded)).to.eql(v)
297
+ }
298
+ })
299
+ it("should convert to/from index", () => {
300
+ const index = toIndex("bob_1")
301
+ expect(index).to.eql("422775512881")
302
+ expect(fromIndex(index)).to.eql("bob_1")
303
+ })
304
+ })
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes