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.
- package/README.md +537 -0
- package/assets/collection.png +0 -0
- package/assets/db.png +0 -0
- package/assets/encode.png +0 -0
- package/assets/query.png +0 -0
- package/assets/rollup.png +0 -0
- package/assets/structure.png +0 -0
- package/assets/weavedb.png +0 -0
- package/assets/zkjson.png +0 -0
- package/benchmark/components/ui/avatar.jsx +49 -0
- package/benchmark/components/ui/checkbox.jsx +17 -0
- package/benchmark/components/ui/close-button.jsx +20 -0
- package/benchmark/components/ui/color-mode.jsx +89 -0
- package/benchmark/components/ui/dialog.jsx +54 -0
- package/benchmark/components/ui/drawer.jsx +44 -0
- package/benchmark/components/ui/field.jsx +22 -0
- package/benchmark/components/ui/input-group.jsx +39 -0
- package/benchmark/components/ui/popover.jsx +49 -0
- package/benchmark/components/ui/provider.jsx +12 -0
- package/benchmark/components/ui/radio.jsx +17 -0
- package/benchmark/components/ui/slider.jsx +107 -0
- package/benchmark/components/ui/tooltip.jsx +35 -0
- package/benchmark/jsconfig.json +7 -0
- package/benchmark/lib/cbor.js +387 -0
- package/benchmark/lib/msgpack.js +651 -0
- package/benchmark/next.config.mjs +6 -0
- package/benchmark/package.json +22 -0
- package/benchmark/pages/_app.js +9 -0
- package/benchmark/pages/_document.js +13 -0
- package/benchmark/pages/api/hello.js +5 -0
- package/benchmark/pages/index.js +438 -0
- package/benchmark/public/favicon.ico +0 -0
- package/benchmark/public/file.svg +1 -0
- package/benchmark/public/globe.svg +1 -0
- package/benchmark/public/next.svg +1 -0
- package/benchmark/public/vercel.svg +1 -0
- package/benchmark/public/window.svg +1 -0
- package/benchmark/yarn.lock +2448 -0
- package/circom/collection/collection.circom +32 -0
- package/circom/collection/compile.sh +10 -0
- package/circom/collection/gen.js +34 -0
- package/circom/collection/generateInput.js +10 -0
- package/circom/collection/index.circom +4 -0
- package/circom/collection/test.js +21 -0
- package/circom/collection/verifier.sol +247 -0
- package/circom/db/compile.sh +10 -0
- package/circom/db/db.circom +39 -0
- package/circom/db/gen.js +30 -0
- package/circom/db/generateInput.js +10 -0
- package/circom/db/index.circom +4 -0
- package/circom/db/test.js +21 -0
- package/circom/db/verifier.sol +261 -0
- package/circom/ipfs/calculate_total.circom +17 -0
- package/circom/ipfs/compile.sh +10 -0
- package/circom/ipfs/gen.js +18 -0
- package/circom/ipfs/generateInput.js +10 -0
- package/circom/ipfs/index.circom +4 -0
- package/circom/ipfs/ipfs.circom +62 -0
- package/circom/ipfs/parse.circom +289 -0
- package/circom/ipfs/sha256.circom +99 -0
- package/circom/ipfs/test.js +167 -0
- package/circom/ipfs/verifier.sol +261 -0
- package/circom/json/compile.sh +10 -0
- package/circom/json/gen.js +22 -0
- package/circom/json/gen2.js +21 -0
- package/circom/json/generateInput.js +20 -0
- package/circom/json/index.circom +4 -0
- package/circom/json/json.circom +81 -0
- package/circom/json/test.js +100 -0
- package/circom/json/verifier.sol +247 -0
- package/circom/query/compile.sh +10 -0
- package/circom/query/gen.js +28 -0
- package/circom/query/generateInput.js +11 -0
- package/circom/query/index.circom +4 -0
- package/circom/query/query.circom +58 -0
- package/circom/query/test.js +20 -0
- package/circom/query/verifier.sol +177 -0
- package/circom/rollup/compile.sh +10 -0
- package/circom/rollup/gen.js +36 -0
- package/circom/rollup/generateInput.js +11 -0
- package/circom/rollup/index.circom +4 -0
- package/circom/rollup/rollup.circom +43 -0
- package/circom/rollup/test.js +35 -0
- package/circom/rollup/verifier.sol +240 -0
- package/circom/scripts/ceremony.js +39 -0
- package/circom/scripts/ceremony.sh +6 -0
- package/circom/scripts/compile.js +113 -0
- package/circom/scripts/compile.sh +16 -0
- package/circom/utils/uint.circom +512 -0
- package/circom/utils/utils.circom +628 -0
- package/demos/arweave/jsconfig.json +7 -0
- package/demos/arweave/lib/ZKArweave.json +617 -0
- package/demos/arweave/lib/encoder.js +375 -0
- package/demos/arweave/next.config.js +6 -0
- package/demos/arweave/package.json +26 -0
- package/demos/arweave/pages/_app.js +9 -0
- package/demos/arweave/pages/_document.js +19 -0
- package/demos/arweave/pages/api/arweave.js +31 -0
- package/demos/arweave/pages/api/hello.js +5 -0
- package/demos/arweave/pages/index.js +869 -0
- package/demos/arweave/public/circuit.wasm +0 -0
- package/demos/arweave/public/circuit_final.zkey +0 -0
- package/demos/arweave/public/verification_key.json +149 -0
- package/demos/arweave/vercel.json +7 -0
- package/demos/arweave/yarn.lock +2510 -0
- package/demos/simple/jsconfig.json +7 -0
- package/demos/simple/lib/encoder.js +375 -0
- package/demos/simple/next.config.js +6 -0
- package/demos/simple/package.json +24 -0
- package/demos/simple/pages/_app.js +9 -0
- package/demos/simple/pages/_document.js +13 -0
- package/demos/simple/pages/api/hello.js +5 -0
- package/demos/simple/pages/index.js +342 -0
- package/demos/simple/public/circuit.wasm +0 -0
- package/demos/simple/public/circuit_final.zkey +0 -0
- package/demos/simple/public/verification_key.json +149 -0
- package/demos/simple/yarn.lock +2519 -0
- package/docs/README.md +17 -0
- package/docs/simple-zkjson.md +210 -0
- package/docs/solidity-contracts.md +216 -0
- package/docs/zk-circuits.md +125 -0
- package/docs/zkdb-rollup.md +218 -0
- package/docs/zkjson-sdk.md +254 -0
- package/docs/zkjson-v1_5.md +365 -0
- package/package.json +16 -7
- package/sdk/contracts/NORollup.sol +13 -0
- package/sdk/package.json +15 -0
- package/sdk/yarn.lock +881 -0
- package/solidity/README.md +13 -0
- package/solidity/arguments.js +4 -0
- package/solidity/contracts/NORollup.sol +13 -0
- package/solidity/contracts/OPRollup.sol +14 -0
- package/solidity/contracts/ZKIPFS.sol +95 -0
- package/solidity/contracts/ZKJson.sol +21 -0
- package/solidity/contracts/ZKQuery.sol +286 -0
- package/solidity/contracts/ZKRollup.sol +35 -0
- package/solidity/contracts/apps/NORU.sol +66 -0
- package/solidity/contracts/apps/SimpleJSON.sol +64 -0
- package/solidity/contracts/apps/SimpleOPRU.sol +67 -0
- package/solidity/contracts/apps/SimpleRU.sol +67 -0
- package/solidity/contracts/apps/Token.sol +12 -0
- package/solidity/contracts/apps/ZKArweave.sol +89 -0
- package/solidity/contracts/apps/ZKBridge.sol +74 -0
- package/solidity/contracts/apps/ZKNFT.sol +63 -0
- package/solidity/contracts/verifiers/verifier_db.sol +275 -0
- package/solidity/contracts/verifiers/verifier_ipfs.sol +464 -0
- package/solidity/contracts/verifiers/verifier_json.sol +261 -0
- package/solidity/contracts/verifiers/verifier_rollup.sol +240 -0
- package/solidity/package.json +27 -0
- package/solidity/scripts/deploy.js +33 -0
- package/solidity/test/NORU.js +66 -0
- package/solidity/test/ZKArweave.js +97 -0
- package/solidity/test/arweave.js +55 -0
- package/solidity/test/bridge.js +71 -0
- package/solidity/test/simple.js +76 -0
- package/solidity/test/simpleOPRU.js +98 -0
- package/solidity/test/simpleRU.js +94 -0
- package/solidity/test/zknft.js +98 -0
- package/solidity/yarn.lock +4152 -0
- package/test/test.js +304 -0
- /package/{circomlibjs.js → sdk/circomlibjs.js} +0 -0
- /package/{collection.js → sdk/collection.js} +0 -0
- /package/{contracts → sdk/contracts}/OPRollup.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKIPFS.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKJson.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKQuery.sol +0 -0
- /package/{contracts → sdk/contracts}/ZKRollup.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/SimpleJSON.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/SimpleOPRU.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/SimpleRU.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/Token.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/ZKArweave.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/ZKBridge.sol +0 -0
- /package/{contracts → sdk/contracts}/apps/ZKNFT.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_db.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_ipfs.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_json.sol +0 -0
- /package/{contracts → sdk/contracts}/verifiers/verifier_rollup.sol +0 -0
- /package/{db.js → sdk/db.js} +0 -0
- /package/{doc.js → sdk/doc.js} +0 -0
- /package/{encoder-v1_5.js → sdk/encoder-v1_5.js} +0 -0
- /package/{encoder.js → sdk/encoder.js} +0 -0
- /package/{index.js → sdk/index.js} +0 -0
- /package/{json.js → sdk/json.js} +0 -0
- /package/{nft.js → sdk/nft.js} +0 -0
- /package/{parse.js → sdk/parse.js} +0 -0
- /package/{uint.js → sdk/uint.js} +0 -0
@@ -0,0 +1,99 @@
|
|
1
|
+
pragma circom 2.1.5;
|
2
|
+
|
3
|
+
include "../../node_modules/circomlib/circuits/sha256/constants.circom";
|
4
|
+
include "../../node_modules/circomlib/circuits/sha256/sha256compression.circom";
|
5
|
+
include "../../node_modules/circomlib/circuits/comparators.circom";
|
6
|
+
include "./calculate_total.circom";
|
7
|
+
|
8
|
+
/*
|
9
|
+
SHA256 Unsafe
|
10
|
+
Calculates the SHA256 hash of the input, using a signal to select the output round corresponding to the number of
|
11
|
+
non-empty input blocks. This implementation is referred to as "unsafe", as it relies upon the caller to ensure that
|
12
|
+
the input is padded correctly, and to ensure that the tBlock input corresponds to the actual terminating data block.
|
13
|
+
Crafted inputs could result in Length Extension Attacks.
|
14
|
+
|
15
|
+
Construction Parameters:
|
16
|
+
- nBlocks: Maximum number of 512-bit blocks for payload input
|
17
|
+
Inputs:
|
18
|
+
- in: An array of blocks exactly nBlocks in length, each block containing an array of exactly 512 bits.
|
19
|
+
Padding of the input according to RFC4634 Section 4.1 is left to the caller.
|
20
|
+
Blocks following tBlock must be supplied, and *should* contain all zeroes
|
21
|
+
- tBlock: An integer corresponding to the terminating block of the input, which contains the message padding
|
22
|
+
Outputs:
|
23
|
+
- out: An array of 256 bits corresponding to the SHA256 output as of the terminating block
|
24
|
+
*/
|
25
|
+
template Sha256_unsafe(nBlocks) {
|
26
|
+
signal input in[nBlocks][512];
|
27
|
+
signal input tBlock;
|
28
|
+
|
29
|
+
signal output out[256];
|
30
|
+
|
31
|
+
component ha0 = H(0);
|
32
|
+
component hb0 = H(1);
|
33
|
+
component hc0 = H(2);
|
34
|
+
component hd0 = H(3);
|
35
|
+
component he0 = H(4);
|
36
|
+
component hf0 = H(5);
|
37
|
+
component hg0 = H(6);
|
38
|
+
component hh0 = H(7);
|
39
|
+
|
40
|
+
component sha256compression[nBlocks];
|
41
|
+
|
42
|
+
for(var i=0; i < nBlocks; i++) {
|
43
|
+
|
44
|
+
sha256compression[i] = Sha256compression();
|
45
|
+
|
46
|
+
if (i==0) {
|
47
|
+
for(var k = 0; k < 32; k++) {
|
48
|
+
sha256compression[i].hin[0*32+k] <== ha0.out[k];
|
49
|
+
sha256compression[i].hin[1*32+k] <== hb0.out[k];
|
50
|
+
sha256compression[i].hin[2*32+k] <== hc0.out[k];
|
51
|
+
sha256compression[i].hin[3*32+k] <== hd0.out[k];
|
52
|
+
sha256compression[i].hin[4*32+k] <== he0.out[k];
|
53
|
+
sha256compression[i].hin[5*32+k] <== hf0.out[k];
|
54
|
+
sha256compression[i].hin[6*32+k] <== hg0.out[k];
|
55
|
+
sha256compression[i].hin[7*32+k] <== hh0.out[k];
|
56
|
+
}
|
57
|
+
} else {
|
58
|
+
for(var k = 0; k < 32; k++) {
|
59
|
+
sha256compression[i].hin[32*0+k] <== sha256compression[i-1].out[32*0+31-k];
|
60
|
+
sha256compression[i].hin[32*1+k] <== sha256compression[i-1].out[32*1+31-k];
|
61
|
+
sha256compression[i].hin[32*2+k] <== sha256compression[i-1].out[32*2+31-k];
|
62
|
+
sha256compression[i].hin[32*3+k] <== sha256compression[i-1].out[32*3+31-k];
|
63
|
+
sha256compression[i].hin[32*4+k] <== sha256compression[i-1].out[32*4+31-k];
|
64
|
+
sha256compression[i].hin[32*5+k] <== sha256compression[i-1].out[32*5+31-k];
|
65
|
+
sha256compression[i].hin[32*6+k] <== sha256compression[i-1].out[32*6+31-k];
|
66
|
+
sha256compression[i].hin[32*7+k] <== sha256compression[i-1].out[32*7+31-k];
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
for (var k = 0; k < 512; k++) {
|
71
|
+
sha256compression[i].inp[k] <== in[i][k];
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
// Collapse the hashing result at the terminating data block
|
76
|
+
// A modified Quin Selector allows us to select the block based on the tBlock signal
|
77
|
+
component calcTotal[256];
|
78
|
+
component eqs[256][nBlocks];
|
79
|
+
|
80
|
+
// For each bit of the output
|
81
|
+
for(var k = 0; k < 256; k++) {
|
82
|
+
calcTotal[k] = CalculateTotal(nBlocks);
|
83
|
+
|
84
|
+
// For each possible block
|
85
|
+
for (var i = 0; i < nBlocks; i++) {
|
86
|
+
// Determine if the given block index is equal to the terminating data block index
|
87
|
+
eqs[k][i] = IsEqual();
|
88
|
+
eqs[k][i].in[0] <== i;
|
89
|
+
eqs[k][i].in[1] <== tBlock - 1;
|
90
|
+
|
91
|
+
// eqs[k][i].out is 1 if the index matches. As such, at most one input to calcTotal is not 0.
|
92
|
+
// The bit corresponding to the terminating data block will be raised
|
93
|
+
calcTotal[k].nums[i] <== eqs[k][i].out * sha256compression[i].out[k];
|
94
|
+
}
|
95
|
+
|
96
|
+
out[k] <== calcTotal[k].sum;
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
@@ -0,0 +1,167 @@
|
|
1
|
+
const { expect } = require("chai")
|
2
|
+
const { push, arr, toArray } = require("../../sdk/uint")
|
3
|
+
const { pad, path, val } = require("../../sdk/encoder")
|
4
|
+
const chai = require("chai")
|
5
|
+
const { join } = require("path")
|
6
|
+
const wasm_tester = require("circom_tester").wasm
|
7
|
+
const crypto = require("crypto")
|
8
|
+
const { parse } = require("../../sdk/parse")
|
9
|
+
|
10
|
+
function bitArray2buffer(a) {
|
11
|
+
const len = Math.floor((a.length - 1) / 8) + 1
|
12
|
+
const b = new Buffer.alloc(len)
|
13
|
+
|
14
|
+
for (let i = 0; i < a.length; i++) {
|
15
|
+
const p = Math.floor(i / 8)
|
16
|
+
b[p] = b[p] | (Number(a[i]) << (7 - (i % 8)))
|
17
|
+
}
|
18
|
+
return b
|
19
|
+
}
|
20
|
+
|
21
|
+
function coerce(o) {
|
22
|
+
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") return o
|
23
|
+
if (o instanceof ArrayBuffer) return new Uint8Array(o)
|
24
|
+
if (ArrayBuffer.isView(o)) {
|
25
|
+
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength)
|
26
|
+
}
|
27
|
+
throw new Error("Unknown type, must be binary type")
|
28
|
+
}
|
29
|
+
|
30
|
+
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
31
|
+
const BASE = ALPHABET.length
|
32
|
+
const LEADER = ALPHABET.charAt(0)
|
33
|
+
const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
|
34
|
+
const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
|
35
|
+
|
36
|
+
function toCID2(source) {
|
37
|
+
var zeroes = 0
|
38
|
+
var length = 0
|
39
|
+
var pbegin = 0
|
40
|
+
var pend = source.length
|
41
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
42
|
+
pbegin++
|
43
|
+
zeroes++
|
44
|
+
}
|
45
|
+
var size = 47
|
46
|
+
var b58 = new Uint8Array(size)
|
47
|
+
while (pbegin !== pend) {
|
48
|
+
var carry = source[pbegin]
|
49
|
+
var i = 0
|
50
|
+
for (
|
51
|
+
var it1 = size - 1;
|
52
|
+
(carry !== 0 || i < length) && it1 !== -1;
|
53
|
+
it1--, i++
|
54
|
+
) {
|
55
|
+
carry += (256 * b58[it1]) >>> 0
|
56
|
+
b58[it1] = carry % 58 >>> 0
|
57
|
+
carry = (carry / 58) >>> 0
|
58
|
+
}
|
59
|
+
length = i
|
60
|
+
pbegin++
|
61
|
+
}
|
62
|
+
var it2 = size - length
|
63
|
+
while (it2 !== size && b58[it2] === 0) it2++
|
64
|
+
var str = LEADER.repeat(zeroes)
|
65
|
+
for (; it2 < size; ++it2) str += ALPHABET.charAt(b58[it2])
|
66
|
+
return str
|
67
|
+
}
|
68
|
+
|
69
|
+
function toCID(source) {
|
70
|
+
// @ts-ignore
|
71
|
+
if (source instanceof Uint8Array);
|
72
|
+
else if (ArrayBuffer.isView(source)) {
|
73
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)
|
74
|
+
} else if (Array.isArray(source)) {
|
75
|
+
source = Uint8Array.from(source)
|
76
|
+
}
|
77
|
+
if (!(source instanceof Uint8Array)) {
|
78
|
+
throw new TypeError("Expected Uint8Array")
|
79
|
+
}
|
80
|
+
if (source.length === 0) {
|
81
|
+
return ""
|
82
|
+
}
|
83
|
+
// Skip & count leading zeroes.
|
84
|
+
var zeroes = 0
|
85
|
+
var length = 0
|
86
|
+
var pbegin = 0
|
87
|
+
var pend = source.length
|
88
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
89
|
+
pbegin++
|
90
|
+
zeroes++
|
91
|
+
}
|
92
|
+
// Allocate enough space in big-endian base58 representation.
|
93
|
+
var size = ((pend - pbegin) * iFACTOR + 1) >>> 0
|
94
|
+
var b58 = new Uint8Array(size)
|
95
|
+
// Process the bytes.
|
96
|
+
while (pbegin !== pend) {
|
97
|
+
var carry = source[pbegin]
|
98
|
+
// Apply "b58 = b58 * 256 + ch".
|
99
|
+
var i = 0
|
100
|
+
for (
|
101
|
+
var it1 = size - 1;
|
102
|
+
(carry !== 0 || i < length) && it1 !== -1;
|
103
|
+
it1--, i++
|
104
|
+
) {
|
105
|
+
carry += (256 * b58[it1]) >>> 0
|
106
|
+
b58[it1] = carry % BASE >>> 0
|
107
|
+
carry = (carry / BASE) >>> 0
|
108
|
+
}
|
109
|
+
if (carry !== 0) {
|
110
|
+
throw new Error("Non-zero carry")
|
111
|
+
}
|
112
|
+
length = i
|
113
|
+
pbegin++
|
114
|
+
}
|
115
|
+
// Skip leading zeroes in base58 result.
|
116
|
+
var it2 = size - length
|
117
|
+
while (it2 !== size && b58[it2] === 0) {
|
118
|
+
it2++
|
119
|
+
}
|
120
|
+
// Translate the result into a string.
|
121
|
+
var str = LEADER.repeat(zeroes)
|
122
|
+
for (; it2 < size; ++it2) {
|
123
|
+
str += ALPHABET.charAt(b58[it2])
|
124
|
+
}
|
125
|
+
return str
|
126
|
+
}
|
127
|
+
|
128
|
+
describe("JSON circuit", function () {
|
129
|
+
let circuit
|
130
|
+
this.timeout(0)
|
131
|
+
|
132
|
+
before(async () => {
|
133
|
+
circuit = await wasm_tester(join(__dirname, "index.circom"))
|
134
|
+
await circuit.loadSymbols()
|
135
|
+
})
|
136
|
+
|
137
|
+
it("should insert docs", async () => {
|
138
|
+
const json = {
|
139
|
+
str: "Hello World!",
|
140
|
+
int: 123,
|
141
|
+
bool: true,
|
142
|
+
null: null,
|
143
|
+
float: 1.23,
|
144
|
+
arr: [1, 2, 3],
|
145
|
+
}
|
146
|
+
const str = new TextEncoder().encode(JSON.stringify(json))
|
147
|
+
const hash = coerce(crypto.createHash("sha256").update(str).digest())
|
148
|
+
|
149
|
+
let encoded = arr(256)
|
150
|
+
for (let v of Array.from(str)) encoded = push(encoded, 256, 76, v)
|
151
|
+
const enc = parse(encoded, 256, 76)
|
152
|
+
const _path = pad(path("str"), 5)
|
153
|
+
const _val = pad(val(json["str"]), 5)
|
154
|
+
|
155
|
+
const w = await circuit.calculateWitness(
|
156
|
+
{ encoded: encoded, path: _path, val: _val },
|
157
|
+
true,
|
158
|
+
)
|
159
|
+
await circuit.checkConstraints(w)
|
160
|
+
await circuit.assertOut(w, { exist: 1 })
|
161
|
+
const _arr2 = w.slice(2, 34).map(n => Number(n))
|
162
|
+
const cid2 = toCID2(new Uint8Array([18, hash.length, ..._arr2]))
|
163
|
+
const cid = toCID(new Uint8Array([18, hash.length, ...Array.from(hash)]))
|
164
|
+
expect(cid).to.eql(cid2)
|
165
|
+
return
|
166
|
+
})
|
167
|
+
})
|
@@ -0,0 +1,261 @@
|
|
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 = 16108517980506854297849182541741553709725147068726502437460795221662934054392;
|
41
|
+
uint256 constant deltax2 = 11108122055952200981463380103442015838004112520282166325679324656261172289310;
|
42
|
+
uint256 constant deltay1 = 15540443534206155738973728487806979316634787078945919829097599147377062022852;
|
43
|
+
uint256 constant deltay2 = 21379352474752452421616166568647842540503269819420348102486102247380909072561;
|
44
|
+
|
45
|
+
|
46
|
+
uint256 constant IC0x = 21491549690759898325084849607299564336640876765821749498156862035601257576731;
|
47
|
+
uint256 constant IC0y = 7673274862583779134920456438013707521756656044641568042313054191508669514090;
|
48
|
+
|
49
|
+
uint256 constant IC1x = 7384807594330870281878199565618965338344331100873316262082027153266713199801;
|
50
|
+
uint256 constant IC1y = 14876621143313435278104193218580147792502814126320367012799180884583971278043;
|
51
|
+
|
52
|
+
uint256 constant IC2x = 14168855774320618160297871496225220542802525303313492506646049444075434034461;
|
53
|
+
uint256 constant IC2y = 2468759921621309671933672856806282408346496190229767650695758108046431009056;
|
54
|
+
|
55
|
+
uint256 constant IC3x = 20882499572322978549523449110104123650836957036545445021640442961988931511616;
|
56
|
+
uint256 constant IC3y = 10781231220582947841663615195913878935048287628839154817279937306701845963832;
|
57
|
+
|
58
|
+
uint256 constant IC4x = 3551014538515980043254241443826671153636028870428071676154749646867482617102;
|
59
|
+
uint256 constant IC4y = 17513115597425155133040003702455782665905020659681301812064653011904364586605;
|
60
|
+
|
61
|
+
uint256 constant IC5x = 105515585129844888605937953967649955568230431552746130229062574355263145521;
|
62
|
+
uint256 constant IC5y = 20200666388807201898426694143171918805989526661219556885905461961107161411873;
|
63
|
+
|
64
|
+
uint256 constant IC6x = 19504246290594149902008126941831109015032250944240332802409169333280249886793;
|
65
|
+
uint256 constant IC6y = 17699531383222577195964406519381055857401242060823677129203944848736284577334;
|
66
|
+
|
67
|
+
uint256 constant IC7x = 15254965952105123078324026560946378979667618649209045648866104810507209861613;
|
68
|
+
uint256 constant IC7y = 3641619426903900212193517444566552500256221100745411003347851827295876272315;
|
69
|
+
|
70
|
+
uint256 constant IC8x = 4499605970446611025648671992837865142426045383246510513270735661446224104646;
|
71
|
+
uint256 constant IC8y = 15341853963424215265359126787955887881741741004410294373221073774379489499288;
|
72
|
+
|
73
|
+
uint256 constant IC9x = 4636178543162541151831226653997316232974453000168920950316536975489566766643;
|
74
|
+
uint256 constant IC9y = 2799228255066264685511548900627321262829537276926174168686910780679641714598;
|
75
|
+
|
76
|
+
uint256 constant IC10x = 6388673479423994256307474279305289225670011140384557756459873981364992797681;
|
77
|
+
uint256 constant IC10y = 15445971588389648358240399113305305960608235393782465515171348639813010141589;
|
78
|
+
|
79
|
+
uint256 constant IC11x = 16999109717455015622767449319493463705406770611876428205363721384440112529191;
|
80
|
+
uint256 constant IC11y = 8675643256310098492425698438900503035238176419524618195032951410590093388339;
|
81
|
+
|
82
|
+
uint256 constant IC12x = 16871502461507515213546155066931373929841527789003952441940247163262482018578;
|
83
|
+
uint256 constant IC12y = 4970662515578058992947951260883675496659331271578004380224271812057151496904;
|
84
|
+
|
85
|
+
uint256 constant IC13x = 13968526389162956068739649724104930691708514396451450078611693259360926530004;
|
86
|
+
uint256 constant IC13y = 8822824035572686629738946181419338699884848605068510614565951757702861630826;
|
87
|
+
|
88
|
+
uint256 constant IC14x = 13433257895962314784382997146377098286327147717667015994113571363174981683976;
|
89
|
+
uint256 constant IC14y = 16316358056856673776145141235445376046620537375366520262647177307024013423430;
|
90
|
+
|
91
|
+
|
92
|
+
// Memory data
|
93
|
+
uint16 constant pVk = 0;
|
94
|
+
uint16 constant pPairing = 128;
|
95
|
+
|
96
|
+
uint16 constant pLastMem = 896;
|
97
|
+
|
98
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[14] calldata _pubSignals) public view returns (bool) {
|
99
|
+
assembly {
|
100
|
+
function checkField(v) {
|
101
|
+
if iszero(lt(v, q)) {
|
102
|
+
mstore(0, 0)
|
103
|
+
return(0, 0x20)
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
// G1 function to multiply a G1 value(x,y) to value in an address
|
108
|
+
function g1_mulAccC(pR, x, y, s) {
|
109
|
+
let success
|
110
|
+
let mIn := mload(0x40)
|
111
|
+
mstore(mIn, x)
|
112
|
+
mstore(add(mIn, 32), y)
|
113
|
+
mstore(add(mIn, 64), s)
|
114
|
+
|
115
|
+
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
|
116
|
+
|
117
|
+
if iszero(success) {
|
118
|
+
mstore(0, 0)
|
119
|
+
return(0, 0x20)
|
120
|
+
}
|
121
|
+
|
122
|
+
mstore(add(mIn, 64), mload(pR))
|
123
|
+
mstore(add(mIn, 96), mload(add(pR, 32)))
|
124
|
+
|
125
|
+
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
|
126
|
+
|
127
|
+
if iszero(success) {
|
128
|
+
mstore(0, 0)
|
129
|
+
return(0, 0x20)
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
|
134
|
+
let _pPairing := add(pMem, pPairing)
|
135
|
+
let _pVk := add(pMem, pVk)
|
136
|
+
|
137
|
+
mstore(_pVk, IC0x)
|
138
|
+
mstore(add(_pVk, 32), IC0y)
|
139
|
+
|
140
|
+
// Compute the linear combination vk_x
|
141
|
+
|
142
|
+
g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))
|
143
|
+
|
144
|
+
g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))
|
145
|
+
|
146
|
+
g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))
|
147
|
+
|
148
|
+
g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))
|
149
|
+
|
150
|
+
g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128)))
|
151
|
+
|
152
|
+
g1_mulAccC(_pVk, IC6x, IC6y, calldataload(add(pubSignals, 160)))
|
153
|
+
|
154
|
+
g1_mulAccC(_pVk, IC7x, IC7y, calldataload(add(pubSignals, 192)))
|
155
|
+
|
156
|
+
g1_mulAccC(_pVk, IC8x, IC8y, calldataload(add(pubSignals, 224)))
|
157
|
+
|
158
|
+
g1_mulAccC(_pVk, IC9x, IC9y, calldataload(add(pubSignals, 256)))
|
159
|
+
|
160
|
+
g1_mulAccC(_pVk, IC10x, IC10y, calldataload(add(pubSignals, 288)))
|
161
|
+
|
162
|
+
g1_mulAccC(_pVk, IC11x, IC11y, calldataload(add(pubSignals, 320)))
|
163
|
+
|
164
|
+
g1_mulAccC(_pVk, IC12x, IC12y, calldataload(add(pubSignals, 352)))
|
165
|
+
|
166
|
+
g1_mulAccC(_pVk, IC13x, IC13y, calldataload(add(pubSignals, 384)))
|
167
|
+
|
168
|
+
g1_mulAccC(_pVk, IC14x, IC14y, calldataload(add(pubSignals, 416)))
|
169
|
+
|
170
|
+
|
171
|
+
// -A
|
172
|
+
mstore(_pPairing, calldataload(pA))
|
173
|
+
mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
|
174
|
+
|
175
|
+
// B
|
176
|
+
mstore(add(_pPairing, 64), calldataload(pB))
|
177
|
+
mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
|
178
|
+
mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
|
179
|
+
mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
|
180
|
+
|
181
|
+
// alpha1
|
182
|
+
mstore(add(_pPairing, 192), alphax)
|
183
|
+
mstore(add(_pPairing, 224), alphay)
|
184
|
+
|
185
|
+
// beta2
|
186
|
+
mstore(add(_pPairing, 256), betax1)
|
187
|
+
mstore(add(_pPairing, 288), betax2)
|
188
|
+
mstore(add(_pPairing, 320), betay1)
|
189
|
+
mstore(add(_pPairing, 352), betay2)
|
190
|
+
|
191
|
+
// vk_x
|
192
|
+
mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
|
193
|
+
mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
|
194
|
+
|
195
|
+
|
196
|
+
// gamma2
|
197
|
+
mstore(add(_pPairing, 448), gammax1)
|
198
|
+
mstore(add(_pPairing, 480), gammax2)
|
199
|
+
mstore(add(_pPairing, 512), gammay1)
|
200
|
+
mstore(add(_pPairing, 544), gammay2)
|
201
|
+
|
202
|
+
// C
|
203
|
+
mstore(add(_pPairing, 576), calldataload(pC))
|
204
|
+
mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
|
205
|
+
|
206
|
+
// delta2
|
207
|
+
mstore(add(_pPairing, 640), deltax1)
|
208
|
+
mstore(add(_pPairing, 672), deltax2)
|
209
|
+
mstore(add(_pPairing, 704), deltay1)
|
210
|
+
mstore(add(_pPairing, 736), deltay2)
|
211
|
+
|
212
|
+
|
213
|
+
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
|
214
|
+
|
215
|
+
isOk := and(success, mload(_pPairing))
|
216
|
+
}
|
217
|
+
|
218
|
+
let pMem := mload(0x40)
|
219
|
+
mstore(0x40, add(pMem, pLastMem))
|
220
|
+
|
221
|
+
// Validate that all evaluations ∈ F
|
222
|
+
|
223
|
+
checkField(calldataload(add(_pubSignals, 0)))
|
224
|
+
|
225
|
+
checkField(calldataload(add(_pubSignals, 32)))
|
226
|
+
|
227
|
+
checkField(calldataload(add(_pubSignals, 64)))
|
228
|
+
|
229
|
+
checkField(calldataload(add(_pubSignals, 96)))
|
230
|
+
|
231
|
+
checkField(calldataload(add(_pubSignals, 128)))
|
232
|
+
|
233
|
+
checkField(calldataload(add(_pubSignals, 160)))
|
234
|
+
|
235
|
+
checkField(calldataload(add(_pubSignals, 192)))
|
236
|
+
|
237
|
+
checkField(calldataload(add(_pubSignals, 224)))
|
238
|
+
|
239
|
+
checkField(calldataload(add(_pubSignals, 256)))
|
240
|
+
|
241
|
+
checkField(calldataload(add(_pubSignals, 288)))
|
242
|
+
|
243
|
+
checkField(calldataload(add(_pubSignals, 320)))
|
244
|
+
|
245
|
+
checkField(calldataload(add(_pubSignals, 352)))
|
246
|
+
|
247
|
+
checkField(calldataload(add(_pubSignals, 384)))
|
248
|
+
|
249
|
+
checkField(calldataload(add(_pubSignals, 416)))
|
250
|
+
|
251
|
+
checkField(calldataload(add(_pubSignals, 448)))
|
252
|
+
|
253
|
+
|
254
|
+
// Validate all evaluations
|
255
|
+
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
|
256
|
+
|
257
|
+
mstore(0, isValid)
|
258
|
+
return(0, 0x20)
|
259
|
+
}
|
260
|
+
}
|
261
|
+
}
|
@@ -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,22 @@
|
|
1
|
+
const { Doc } = require("../../sdk")
|
2
|
+
|
3
|
+
const gen = async ({ size_val = 8, size_path = 4, size_json = 256 }) => {
|
4
|
+
const doc = new Doc({ size_val, size_path, size_json })
|
5
|
+
const json = {
|
6
|
+
num: 1,
|
7
|
+
float: 1.23,
|
8
|
+
str: "string",
|
9
|
+
bool: true,
|
10
|
+
null: null,
|
11
|
+
array: [1, 2, 3],
|
12
|
+
}
|
13
|
+
|
14
|
+
return {
|
15
|
+
inputs: await doc.getInputs({
|
16
|
+
json,
|
17
|
+
path: "array",
|
18
|
+
}),
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
module.exports = gen
|
@@ -0,0 +1,21 @@
|
|
1
|
+
const { Doc, fromSignal } = require("../../sdk")
|
2
|
+
|
3
|
+
const gen2 = async ({
|
4
|
+
size_val = 256,
|
5
|
+
size_path = 256,
|
6
|
+
size_json = 256,
|
7
|
+
json,
|
8
|
+
path,
|
9
|
+
query,
|
10
|
+
}) => {
|
11
|
+
const doc = new Doc({ size_val, size_path, size_json })
|
12
|
+
return {
|
13
|
+
inputs: await doc.getInputs({
|
14
|
+
json: json ?? { c: [1, 2, 3, false], b: 3 },
|
15
|
+
path: path ?? "c",
|
16
|
+
query: query ?? ["$contains_none", [4, 5, 6, true]],
|
17
|
+
}),
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
module.exports = gen2
|
@@ -0,0 +1,20 @@
|
|
1
|
+
const { Doc } = require("../../sdk")
|
2
|
+
const { writeFileSync } = require("fs")
|
3
|
+
const { resolve } = require("path")
|
4
|
+
const gen = require("./gen")
|
5
|
+
|
6
|
+
let {
|
7
|
+
input = resolve(__dirname, "input.json"),
|
8
|
+
size_val = 5,
|
9
|
+
size_path = 5,
|
10
|
+
size_json = 256,
|
11
|
+
} = require("yargs")(process.argv.slice(2)).options({
|
12
|
+
size: { type: "number" },
|
13
|
+
size_json: { type: "number" },
|
14
|
+
}).argv
|
15
|
+
|
16
|
+
const main = async () => {
|
17
|
+
const { inputs } = await gen({ size_json, size_val, size_path })
|
18
|
+
writeFileSync(input, JSON.stringify(inputs))
|
19
|
+
}
|
20
|
+
main()
|
@@ -0,0 +1,81 @@
|
|
1
|
+
pragma circom 2.1.5;
|
2
|
+
|
3
|
+
include "../utils/utils.circom";
|
4
|
+
include "../../node_modules/circomlib/circuits/poseidon.circom";
|
5
|
+
|
6
|
+
template JSON (size_json, size_path, size_val) {
|
7
|
+
signal input json[size_json];
|
8
|
+
signal input path[size_path];
|
9
|
+
signal input val[size_val];
|
10
|
+
signal output exist;
|
11
|
+
signal output hash;
|
12
|
+
signal ex;
|
13
|
+
|
14
|
+
var _json_[9] = [0, size_json, 0,0,0,0,0,0,0];
|
15
|
+
|
16
|
+
var contains[101];
|
17
|
+
var nomatch = 0;
|
18
|
+
var _val_[9] = [0, size_val, 0,0,0,0,0,0,0];
|
19
|
+
_val_ = g(val, _val_);
|
20
|
+
var op = _val_[0];
|
21
|
+
var _exists = op == 21 ? 1 : 0;
|
22
|
+
var partial[9] = [0, size_val, 0,0,0,0,0,0,0];
|
23
|
+
var count = 0;
|
24
|
+
|
25
|
+
var _path_[9] = [0, size_path, 0,0,0,0,0,0,0];
|
26
|
+
|
27
|
+
while(_json_[5] == 0){
|
28
|
+
_json_ = g(json, _json_);
|
29
|
+
var _path_start[9] = _json_;
|
30
|
+
_json_ = getPath(json, _json_, 1);
|
31
|
+
var pi = _json_[6];
|
32
|
+
_json_ = g(json, _json_);
|
33
|
+
var _val_start[9] = _json_;
|
34
|
+
_json_ = getVal(json, _json_);
|
35
|
+
var vi = _json_[6];
|
36
|
+
var match[2] = checkPathMatch(json, path, _path_start, size_path, pi);
|
37
|
+
var path_match = match[0];
|
38
|
+
var path_partial_match = match[1];
|
39
|
+
var plus = op >= 10 ? 1 : 0;
|
40
|
+
if(path_match == 1){
|
41
|
+
var _val_match = 1;
|
42
|
+
if(op >= 12 && op <= 15){
|
43
|
+
_val_match = checkGtLt(op, _val_, _val_start, json, val, _val_match);
|
44
|
+
} else if(op == 16 || op == 17){
|
45
|
+
_val_match = checkInNin(op, _val_, _val_start, json, val, vi, _val_match);
|
46
|
+
} else {
|
47
|
+
_val_match = checkValMatch (json, val, _val_start, plus, size_val, vi, _val_match);
|
48
|
+
}
|
49
|
+
if(_val_match == 1){
|
50
|
+
if(op != 11) _exists = 1;
|
51
|
+
}else {
|
52
|
+
if(op == 11) _exists = 1;
|
53
|
+
}
|
54
|
+
}else if(path_partial_match == 1 && ((op == 21 && _exists == 1) || (op != 21 && _exists == 0))){
|
55
|
+
if(op == 19 || op == 20 || op == 21){
|
56
|
+
contains = checkArrayContains(_exists, op, _val_, _val_start, vi, val, json, contains);
|
57
|
+
_exists = contains[100];
|
58
|
+
} else if (op == 18){
|
59
|
+
_exists = checkContains(_exists, size_val, val, _val_start, vi, json, plus);
|
60
|
+
} else {
|
61
|
+
count++;
|
62
|
+
partial = checkPartialMatch(_exists, size_path, size_val, path, _path_start, pi, val, _val_start, vi, json, partial, count);
|
63
|
+
if(partial[6] == 0) nomatch = 1;
|
64
|
+
if(partial[6] == 2 && nomatch == 0) _exists = 1;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
ex <-- _exists;
|
69
|
+
exist <== ex * ex;
|
70
|
+
|
71
|
+
component all_hash = Poseidon(16);
|
72
|
+
component _hash[16];
|
73
|
+
for(var i = 0; i < 16; i++){
|
74
|
+
_hash[i] = Poseidon(16);
|
75
|
+
for(var i2 = 0; i2 < 16; i2++){
|
76
|
+
_hash[i].inputs[i2] <== json[i * 16 + i2];
|
77
|
+
}
|
78
|
+
all_hash.inputs[i] <== _hash[i].out;
|
79
|
+
}
|
80
|
+
hash <== all_hash.out;
|
81
|
+
}
|