@solarity/zkit 0.1.0
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/LICENSE +21 -0
- package/README.md +120 -0
- package/dist/config/config.d.ts +24 -0
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +17 -0
- package/dist/config/config.js.map +1 -0
- package/dist/core/CircomZKit.d.ts +39 -0
- package/dist/core/CircomZKit.d.ts.map +1 -0
- package/dist/core/CircomZKit.js +94 -0
- package/dist/core/CircomZKit.js.map +1 -0
- package/dist/core/CircuitZKit.d.ts +146 -0
- package/dist/core/CircuitZKit.d.ts.map +1 -0
- package/dist/core/CircuitZKit.js +342 -0
- package/dist/core/CircuitZKit.js.map +1 -0
- package/dist/core/ManagerZKit.d.ts +97 -0
- package/dist/core/ManagerZKit.d.ts.map +1 -0
- package/dist/core/ManagerZKit.js +222 -0
- package/dist/core/ManagerZKit.js.map +1 -0
- package/dist/core/templates/verifier_groth16.sol.ejs +164 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/types/types.d.ts +46 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/types.js +3 -0
- package/dist/types/types.js.map +1 -0
- package/dist/utils/utils.d.ts +18 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +58 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +51 -0
- package/src/config/config.ts +37 -0
- package/src/core/CircomZKit.ts +110 -0
- package/src/core/CircuitZKit.ts +375 -0
- package/src/core/ManagerZKit.ts +231 -0
- package/src/core/templates/verifier_groth16.sol.ejs +164 -0
- package/src/index.ts +7 -0
- package/src/types/types.ts +43 -0
- package/src/utils/utils.ts +60 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity >=0.7.0 <0.9.0;
|
|
3
|
+
|
|
4
|
+
contract <%=verifier_id%> {
|
|
5
|
+
/// @dev Base field size
|
|
6
|
+
uint256 public constant BASE_FIELD_SIZE =
|
|
7
|
+
21888242871839275222246405745257275088696311157297823662689037894645226208583;
|
|
8
|
+
|
|
9
|
+
/// @dev Verification Key data
|
|
10
|
+
uint256 public constant ALPHA_X =
|
|
11
|
+
<%=vk_alpha_1[0]%>;
|
|
12
|
+
uint256 public constant ALPHA_Y =
|
|
13
|
+
<%=vk_alpha_1[1]%>;
|
|
14
|
+
uint256 public constant BETA_X1 =
|
|
15
|
+
<%=vk_beta_2[0][1]%>;
|
|
16
|
+
uint256 public constant BETA_X2 =
|
|
17
|
+
<%=vk_beta_2[0][0]%>;
|
|
18
|
+
uint256 public constant BETA_Y1 =
|
|
19
|
+
<%=vk_beta_2[1][1]%>;
|
|
20
|
+
uint256 public constant BETA_Y2 =
|
|
21
|
+
<%=vk_beta_2[1][0]%>;
|
|
22
|
+
uint256 public constant GAMMA_X1 =
|
|
23
|
+
<%=vk_gamma_2[0][1]%>;
|
|
24
|
+
uint256 public constant GAMMA_X2 =
|
|
25
|
+
<%=vk_gamma_2[0][0]%>;
|
|
26
|
+
uint256 public constant GAMMA_Y1 =
|
|
27
|
+
<%=vk_gamma_2[1][1]%>;
|
|
28
|
+
uint256 public constant GAMMA_Y2 =
|
|
29
|
+
<%=vk_gamma_2[1][0]%>;
|
|
30
|
+
uint256 public constant DELTA_X1 =
|
|
31
|
+
<%=vk_delta_2[0][1]%>;
|
|
32
|
+
uint256 public constant DELTA_X2 =
|
|
33
|
+
<%=vk_delta_2[0][0]%>;
|
|
34
|
+
uint256 public constant DELTA_Y1 =
|
|
35
|
+
<%=vk_delta_2[1][1]%>;
|
|
36
|
+
uint256 public constant DELTA_Y2 =
|
|
37
|
+
<%=vk_delta_2[1][0] -%>;
|
|
38
|
+
|
|
39
|
+
<% for (let i=0; i<IC.length; i++) { %>uint256 public constant IC<%=i%>_X =
|
|
40
|
+
<%=IC[i][0]%>;
|
|
41
|
+
uint256 public constant IC<%=i%>_Y =
|
|
42
|
+
<%=IC[i][1]%>;
|
|
43
|
+
<% } -%>
|
|
44
|
+
|
|
45
|
+
/// @dev Memory data
|
|
46
|
+
uint16 public constant P_VK = 0;
|
|
47
|
+
uint16 public constant P_PAIRING = 128;
|
|
48
|
+
uint16 public constant P_LAST_MEM = 896;
|
|
49
|
+
|
|
50
|
+
function verifyProof(
|
|
51
|
+
uint256[2] calldata pA_,
|
|
52
|
+
uint256[2][2] calldata pB_,
|
|
53
|
+
uint256[2] calldata pC_,
|
|
54
|
+
uint256[<%=IC.length-1%>] calldata pubSignals_
|
|
55
|
+
) public view returns (bool) {
|
|
56
|
+
assembly {
|
|
57
|
+
function checkField(v) {
|
|
58
|
+
if iszero(lt(v, BASE_FIELD_SIZE)) {
|
|
59
|
+
mstore(0, 0)
|
|
60
|
+
return(0, 0x20)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// @dev G1 function to multiply a G1 value(x,y) to value in an address
|
|
65
|
+
function g1MulAccC(pR, x, y, s) {
|
|
66
|
+
let success
|
|
67
|
+
let mIn := mload(0x40)
|
|
68
|
+
|
|
69
|
+
mstore(mIn, x)
|
|
70
|
+
mstore(add(mIn, 32), y)
|
|
71
|
+
mstore(add(mIn, 64), s)
|
|
72
|
+
|
|
73
|
+
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
|
|
74
|
+
|
|
75
|
+
if iszero(success) {
|
|
76
|
+
mstore(0, 0)
|
|
77
|
+
return(0, 0x20)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
mstore(add(mIn, 64), mload(pR))
|
|
81
|
+
mstore(add(mIn, 96), mload(add(pR, 32)))
|
|
82
|
+
|
|
83
|
+
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
|
|
84
|
+
|
|
85
|
+
if iszero(success) {
|
|
86
|
+
mstore(0, 0)
|
|
87
|
+
return(0, 0x20)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
|
|
92
|
+
let pPairing_ := add(pMem, P_PAIRING)
|
|
93
|
+
let pVk_ := add(pMem, P_VK)
|
|
94
|
+
|
|
95
|
+
mstore(pVk_, IC0_X)
|
|
96
|
+
mstore(add(pVk_, 32), IC0_Y)
|
|
97
|
+
|
|
98
|
+
/// @dev Compute the linear combination vk_x
|
|
99
|
+
<% for (let i = 1; i <= nPublic; i++) { %>g1MulAccC(pVk_, IC<%=i%>_X, IC<%=i%>_Y, calldataload(add(pubSignals, <%=(i-1)*32%>)))
|
|
100
|
+
<% } -%>
|
|
101
|
+
|
|
102
|
+
/// @dev -A
|
|
103
|
+
mstore(pPairing_, calldataload(pA))
|
|
104
|
+
mstore(
|
|
105
|
+
add(pPairing_, 32),
|
|
106
|
+
mod(sub(BASE_FIELD_SIZE, calldataload(add(pA, 32))), BASE_FIELD_SIZE)
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
/// @dev B
|
|
110
|
+
mstore(add(pPairing_, 64), calldataload(pB))
|
|
111
|
+
mstore(add(pPairing_, 96), calldataload(add(pB, 32)))
|
|
112
|
+
mstore(add(pPairing_, 128), calldataload(add(pB, 64)))
|
|
113
|
+
mstore(add(pPairing_, 160), calldataload(add(pB, 96)))
|
|
114
|
+
|
|
115
|
+
/// @dev alpha1
|
|
116
|
+
mstore(add(pPairing_, 192), ALPHA_X)
|
|
117
|
+
mstore(add(pPairing_, 224), ALPHA_Y)
|
|
118
|
+
|
|
119
|
+
/// @dev beta2
|
|
120
|
+
mstore(add(pPairing_, 256), BETA_X1)
|
|
121
|
+
mstore(add(pPairing_, 288), BETA_X2)
|
|
122
|
+
mstore(add(pPairing_, 320), BETA_Y1)
|
|
123
|
+
mstore(add(pPairing_, 352), BETA_Y2)
|
|
124
|
+
|
|
125
|
+
/// @dev vk_x
|
|
126
|
+
mstore(add(pPairing_, 384), mload(add(pMem, P_VK)))
|
|
127
|
+
mstore(add(pPairing_, 416), mload(add(pMem, add(P_VK, 32))))
|
|
128
|
+
|
|
129
|
+
/// @dev gamma2
|
|
130
|
+
mstore(add(pPairing_, 448), GAMMA_X1)
|
|
131
|
+
mstore(add(pPairing_, 480), GAMMA_X2)
|
|
132
|
+
mstore(add(pPairing_, 512), GAMMA_Y1)
|
|
133
|
+
mstore(add(pPairing_, 544), GAMMA_Y2)
|
|
134
|
+
|
|
135
|
+
/// @dev C
|
|
136
|
+
mstore(add(pPairing_, 576), calldataload(pC))
|
|
137
|
+
mstore(add(pPairing_, 608), calldataload(add(pC, 32)))
|
|
138
|
+
|
|
139
|
+
/// @dev delta2
|
|
140
|
+
mstore(add(pPairing_, 640), DELTA_X1)
|
|
141
|
+
mstore(add(pPairing_, 672), DELTA_X2)
|
|
142
|
+
mstore(add(pPairing_, 704), DELTA_Y1)
|
|
143
|
+
mstore(add(pPairing_, 736), DELTA_Y2)
|
|
144
|
+
|
|
145
|
+
let success_ := staticcall(sub(gas(), 2000), 8, pPairing_, 768, pPairing_, 0x20)
|
|
146
|
+
|
|
147
|
+
isOk := and(success_, mload(pPairing_))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let pMem_ := mload(0x40)
|
|
151
|
+
mstore(0x40, add(pMem_, P_LAST_MEM))
|
|
152
|
+
|
|
153
|
+
/// @dev Validate that all evaluations ∈ F
|
|
154
|
+
<% for (let i = 0; i < IC.length; i++) { %>checkField(calldataload(add(pubSignals_, <%=i*32%>)))
|
|
155
|
+
<% } -%>
|
|
156
|
+
|
|
157
|
+
/// @dev Validate all evaluations
|
|
158
|
+
let isValid := checkPairing(pA_, pB_, pC_, pubSignals_, pMem_)
|
|
159
|
+
|
|
160
|
+
mstore(0, isValid)
|
|
161
|
+
return(0, 0x20)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./core/CircomZKit";
|
|
2
|
+
export * from "./core/CircuitZKit";
|
|
3
|
+
export * from "./core/ManagerZKit";
|
|
4
|
+
|
|
5
|
+
export { NumericString, PublicSignals, Groth16Proof, Calldata, ProofStruct, Inputs, CircuitInfo } from "./types/types";
|
|
6
|
+
|
|
7
|
+
export { CompileOptions, ManagerZKitConfig, defaultCompileOptions, defaultManagerOptions } from "./config/config";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type NumericString = `${number}` | string;
|
|
2
|
+
|
|
3
|
+
export type PublicSignals = NumericString[];
|
|
4
|
+
|
|
5
|
+
export type Groth16Proof = {
|
|
6
|
+
pi_a: [NumericString, NumericString];
|
|
7
|
+
pi_b: [[NumericString, NumericString], [NumericString, NumericString]];
|
|
8
|
+
pi_c: [NumericString, NumericString];
|
|
9
|
+
protocol: string;
|
|
10
|
+
curve: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type Calldata = [
|
|
14
|
+
[NumericString, NumericString],
|
|
15
|
+
[[NumericString, NumericString], [NumericString, NumericString]],
|
|
16
|
+
[NumericString, NumericString],
|
|
17
|
+
[NumericString],
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export type ProofStruct = {
|
|
21
|
+
proof: Groth16Proof;
|
|
22
|
+
publicSignals: PublicSignals;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type NumberLike = number | bigint | string;
|
|
26
|
+
export type ArrayLike = NumberLike[] | ArrayLike[];
|
|
27
|
+
export type InputLike = NumberLike | ArrayLike;
|
|
28
|
+
|
|
29
|
+
export type Inputs = Record<string, InputLike>;
|
|
30
|
+
|
|
31
|
+
export type CircuitInfo = {
|
|
32
|
+
path: string;
|
|
33
|
+
id: string | null;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type FileType = "r1cs" | "zkey" | "vkey" | "sym" | "json" | "wasm" | "sol";
|
|
37
|
+
export type DirType = "circuit" | "artifact" | "verifier";
|
|
38
|
+
export type TemplateType = "groth16";
|
|
39
|
+
|
|
40
|
+
export type PtauInfo = {
|
|
41
|
+
file: string;
|
|
42
|
+
url: string | null;
|
|
43
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import https from "https";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reads a directory recursively and calls the callback for each file.
|
|
7
|
+
*
|
|
8
|
+
* @dev After Node.js 20.0.0 the `recursive` option is available.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} dir - The directory to read.
|
|
11
|
+
* @param {(dir: string, file: string) => void} callback - The callback function.
|
|
12
|
+
*/
|
|
13
|
+
export function readDirRecursively(dir: string, callback: (dir: string, file: string) => void): void {
|
|
14
|
+
if (!fs.existsSync(dir)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
19
|
+
|
|
20
|
+
for (const entry of entries) {
|
|
21
|
+
const entryPath = path.join(dir, entry.name);
|
|
22
|
+
|
|
23
|
+
if (entry.isDirectory()) {
|
|
24
|
+
readDirRecursively(entryPath, callback);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (entry.isFile()) {
|
|
28
|
+
callback(dir, entryPath);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Downloads a file from the specified URL.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} file - The path to save the file to.
|
|
37
|
+
* @param {string} url - The URL to download the file from.
|
|
38
|
+
* @returns {Promise<boolean>} Whether the file was downloaded successfully.
|
|
39
|
+
*/
|
|
40
|
+
export async function downloadFile(file: string, url: string): Promise<boolean> {
|
|
41
|
+
const fileStream = fs.createWriteStream(file);
|
|
42
|
+
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const request = https.get(url, (response) => {
|
|
45
|
+
response.pipe(fileStream);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
fileStream.on("finish", () => resolve(true));
|
|
49
|
+
|
|
50
|
+
request.on("error", (err) => {
|
|
51
|
+
fs.unlink(file, () => reject(err));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
fileStream.on("error", (err) => {
|
|
55
|
+
fs.unlink(file, () => reject(err));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
request.end();
|
|
59
|
+
});
|
|
60
|
+
}
|