@solarity/zkit 0.1.0 → 0.2.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/README.md +23 -51
- package/dist/config/config.d.ts +3 -0
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +2 -0
- package/dist/config/config.js.map +1 -1
- package/dist/core/CircuitZKit.d.ts +23 -89
- package/dist/core/CircuitZKit.d.ts.map +1 -1
- package/dist/core/CircuitZKit.js +65 -223
- package/dist/core/CircuitZKit.js.map +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -6
- package/dist/index.js.map +1 -1
- package/dist/types/circuit-zkit.d.ts +43 -0
- package/dist/types/circuit-zkit.d.ts.map +1 -0
- package/dist/types/circuit-zkit.js +3 -0
- package/dist/types/circuit-zkit.js.map +1 -0
- package/package.json +21 -10
- package/src/core/CircuitZKit.ts +73 -264
- package/src/index.ts +1 -6
- package/src/types/{types.ts → circuit-zkit.ts} +8 -12
- package/src/config/config.ts +0 -37
- package/src/core/CircomZKit.ts +0 -110
- package/src/core/ManagerZKit.ts +0 -231
- package/src/utils/utils.ts +0 -60
package/src/core/CircuitZKit.ts
CHANGED
|
@@ -3,86 +3,55 @@ import fs from "fs";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import * as snarkjs from "snarkjs";
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
import {
|
|
7
|
+
ArtifactsFileType,
|
|
8
|
+
Calldata,
|
|
9
|
+
CircuitZKitConfig,
|
|
10
|
+
Inputs,
|
|
11
|
+
ProofStruct,
|
|
12
|
+
VerifierTemplateType,
|
|
13
|
+
} from "../types/circuit-zkit";
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
|
|
15
|
-
*
|
|
16
|
-
* @dev This class is not meant to be used directly. Use the `CircomZKit` to create its instance.
|
|
17
17
|
*/
|
|
18
18
|
export class CircuitZKit {
|
|
19
|
-
|
|
20
|
-
* Creates a new instance of `CircuitZKit`.
|
|
21
|
-
*
|
|
22
|
-
* @param {string} _circuit - The path to the circuit.
|
|
23
|
-
* @param {ManagerZKit} _manager - The manager that maintains the global state.
|
|
24
|
-
*/
|
|
25
|
-
constructor(
|
|
26
|
-
private readonly _circuit: string,
|
|
27
|
-
private readonly _manager: ManagerZKit,
|
|
28
|
-
) {}
|
|
19
|
+
constructor(private readonly _config: CircuitZKitConfig) {}
|
|
29
20
|
|
|
30
21
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @dev If compilation fails, the latest valid artifacts will be preserved.
|
|
34
|
-
* @dev Doesn't show the compilation error if `quiet` is set to `true`.
|
|
22
|
+
* Returns the Solidity verifier template for the specified proving system.
|
|
35
23
|
*
|
|
36
|
-
* @param {
|
|
24
|
+
* @param {VerifierTemplateType} templateType - The template type.
|
|
25
|
+
* @returns {string} The Solidity verifier template.
|
|
37
26
|
*/
|
|
38
|
-
public
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
fs.mkdirSync(tempDir, { recursive: true });
|
|
45
|
-
|
|
46
|
-
const overriddenOptions: CompileOptions = { ...defaultCompileOptions, ...options };
|
|
47
|
-
|
|
48
|
-
await this._compile(overriddenOptions, tempDir);
|
|
49
|
-
|
|
50
|
-
await this._generateZKey(tempDir);
|
|
51
|
-
await this._generateVKey(tempDir);
|
|
52
|
-
|
|
53
|
-
this._moveFromTempDirToOutDir(tempDir, artifactDir);
|
|
54
|
-
} finally {
|
|
55
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
27
|
+
public static getTemplate(templateType: VerifierTemplateType): string {
|
|
28
|
+
switch (templateType) {
|
|
29
|
+
case "groth16":
|
|
30
|
+
return fs.readFileSync(path.join(__dirname, "templates", "verifier_groth16.sol.ejs"), "utf8");
|
|
31
|
+
default:
|
|
32
|
+
throw new Error(`Ambiguous template type: ${templateType}.`);
|
|
56
33
|
}
|
|
57
34
|
}
|
|
58
35
|
|
|
59
36
|
/**
|
|
60
|
-
* Creates a verifier contract.
|
|
37
|
+
* Creates a Solidity verifier contract.
|
|
61
38
|
*/
|
|
62
39
|
public async createVerifier(): Promise<void> {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
const verifierDir = this._getDir("verifier");
|
|
67
|
-
|
|
68
|
-
fs.mkdirSync(tempDir, { recursive: true });
|
|
40
|
+
const vKeyFilePath: string = this.mustGetArtifactsFilePath("vkey");
|
|
41
|
+
const verifierFilePath = path.join(this._config.verifierDirPath, `${this.getVerifierName()}.sol`);
|
|
69
42
|
|
|
70
|
-
|
|
71
|
-
const verifierFile = this._getFile("sol", tempDir);
|
|
43
|
+
const verifierTemplate: string = CircuitZKit.getTemplate(this.getTemplateType());
|
|
72
44
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
templateParams["verifier_id"] = this.getVerifierId();
|
|
45
|
+
if (!fs.existsSync(this._config.verifierDirPath)) {
|
|
46
|
+
fs.mkdirSync(this._config.verifierDirPath, { recursive: true });
|
|
47
|
+
}
|
|
77
48
|
|
|
78
|
-
|
|
49
|
+
const templateParams = JSON.parse(fs.readFileSync(vKeyFilePath, "utf-8"));
|
|
50
|
+
templateParams["verifier_id"] = this.getVerifierName();
|
|
79
51
|
|
|
80
|
-
|
|
52
|
+
const verifierCode = ejs.render(verifierTemplate, templateParams);
|
|
81
53
|
|
|
82
|
-
|
|
83
|
-
} finally {
|
|
84
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
85
|
-
}
|
|
54
|
+
fs.writeFileSync(verifierFilePath, verifierCode, "utf-8");
|
|
86
55
|
}
|
|
87
56
|
|
|
88
57
|
/**
|
|
@@ -95,8 +64,8 @@ export class CircuitZKit {
|
|
|
95
64
|
* @todo Add support for other proving systems.
|
|
96
65
|
*/
|
|
97
66
|
public async generateProof(inputs: Inputs): Promise<ProofStruct> {
|
|
98
|
-
const zKeyFile = this.
|
|
99
|
-
const wasmFile = this.
|
|
67
|
+
const zKeyFile = this.mustGetArtifactsFilePath("zkey");
|
|
68
|
+
const wasmFile = this.mustGetArtifactsFilePath("wasm");
|
|
100
69
|
|
|
101
70
|
return (await snarkjs.groth16.fullProve(inputs, wasmFile, zKeyFile)) as ProofStruct;
|
|
102
71
|
}
|
|
@@ -111,7 +80,7 @@ export class CircuitZKit {
|
|
|
111
80
|
* @returns {Promise<boolean>} Whether the proof is valid.
|
|
112
81
|
*/
|
|
113
82
|
public async verifyProof(proof: ProofStruct): Promise<boolean> {
|
|
114
|
-
const vKeyFile = this.
|
|
83
|
+
const vKeyFile = this.mustGetArtifactsFilePath("vkey");
|
|
115
84
|
|
|
116
85
|
const verifier = JSON.parse(fs.readFileSync(vKeyFile).toString());
|
|
117
86
|
|
|
@@ -132,244 +101,84 @@ export class CircuitZKit {
|
|
|
132
101
|
}
|
|
133
102
|
|
|
134
103
|
/**
|
|
135
|
-
* Returns the circuit
|
|
136
|
-
*
|
|
137
|
-
* @returns {string} The circuit ID.
|
|
138
|
-
*/
|
|
139
|
-
public getCircuitId(): string {
|
|
140
|
-
return path.parse(this._circuit).name;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Returns the verifier ID. The verifier ID is the name of the circuit file without the extension, suffixed with "Verifier".
|
|
104
|
+
* Returns the circuit name. The circuit name is the name of the circuit file without the extension.
|
|
145
105
|
*
|
|
146
|
-
* @returns {string} The
|
|
106
|
+
* @returns {string} The circuit name.
|
|
147
107
|
*/
|
|
148
|
-
public
|
|
149
|
-
return
|
|
108
|
+
public getCircuitName(): string {
|
|
109
|
+
return this._config.circuitName;
|
|
150
110
|
}
|
|
151
111
|
|
|
152
112
|
/**
|
|
153
|
-
*
|
|
113
|
+
* Returns the verifier name. The verifier name is the name of the circuit file without the extension, suffixed with "Verifier".
|
|
154
114
|
*
|
|
155
|
-
* @
|
|
156
|
-
* @todo This method may cause issues https://github.com/iden3/snarkjs/issues/494
|
|
115
|
+
* @returns {string} The verifier name.
|
|
157
116
|
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const zKeyFile = this._getFile("zkey", outDir);
|
|
161
|
-
|
|
162
|
-
const constraints = await this._getConstraints(outDir);
|
|
163
|
-
const ptauFile = await this._manager.fetchPtauFile(constraints);
|
|
164
|
-
|
|
165
|
-
await snarkjs.zKey.newZKey(r1csFile, ptauFile, zKeyFile);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Generates verification key for the circuit.
|
|
170
|
-
*
|
|
171
|
-
* @param {string} outDir - The directory to save the generated key.
|
|
172
|
-
*/
|
|
173
|
-
private async _generateVKey(outDir: string): Promise<void> {
|
|
174
|
-
const zKeyFile = this._getFile("zkey", outDir);
|
|
175
|
-
const vKeyFile = this._getFile("vkey", outDir);
|
|
176
|
-
|
|
177
|
-
const vKeyData = await snarkjs.zKey.exportVerificationKey(zKeyFile);
|
|
178
|
-
|
|
179
|
-
fs.writeFileSync(vKeyFile, JSON.stringify(vKeyData));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Returns the arguments to compile the circuit.
|
|
184
|
-
*
|
|
185
|
-
* @param {CompileOptions} options - Compilation options.
|
|
186
|
-
* @param {string} outDir - The directory to save the compiled artifacts.
|
|
187
|
-
* @returns {string[]} The arguments to compile the circuit.
|
|
188
|
-
*/
|
|
189
|
-
private _getCompileArgs(options: CompileOptions, outDir: string): string[] {
|
|
190
|
-
let args = [this._circuit, "--r1cs", "--wasm"];
|
|
191
|
-
|
|
192
|
-
options.sym && args.push("--sym");
|
|
193
|
-
options.json && args.push("--json");
|
|
194
|
-
options.c && args.push("--c");
|
|
195
|
-
|
|
196
|
-
args.push("-o", outDir);
|
|
197
|
-
|
|
198
|
-
return args;
|
|
117
|
+
public getVerifierName(): string {
|
|
118
|
+
return `${this._config.circuitName}Verifier`;
|
|
199
119
|
}
|
|
200
120
|
|
|
201
121
|
/**
|
|
202
|
-
*
|
|
122
|
+
* Returns the type of verifier template that was stored in the config
|
|
203
123
|
*
|
|
204
|
-
* @
|
|
205
|
-
* @param {string} outDir - The directory to save the compiled artifacts.
|
|
124
|
+
* @returns {VerifierTemplateType} The verifier template type.
|
|
206
125
|
*/
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
try {
|
|
211
|
-
await this._getCircomRunner(args, options.quiet).execute(this._manager.getCompiler());
|
|
212
|
-
} catch (err) {
|
|
213
|
-
if (options.quiet) {
|
|
214
|
-
throw new Error(
|
|
215
|
-
'Compilation failed with an unknown error. Consider passing "quiet=false" flag to see the compilation error.',
|
|
216
|
-
{ cause: err },
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
throw new Error("Compilation failed.", { cause: err });
|
|
221
|
-
}
|
|
126
|
+
public getTemplateType(): VerifierTemplateType {
|
|
127
|
+
return this._config.templateType ?? "groth16";
|
|
222
128
|
}
|
|
223
129
|
|
|
224
130
|
/**
|
|
225
|
-
* Returns the
|
|
131
|
+
* Returns the path to the file of the given type inside artifacts directory. Throws an error if the file doesn't exist.
|
|
226
132
|
*
|
|
227
|
-
* @param {
|
|
228
|
-
* @returns {
|
|
133
|
+
* @param {ArtifactsFileType} fileType - The type of the file.
|
|
134
|
+
* @returns {string} The path to the file.
|
|
229
135
|
*/
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
const r1csDescriptor = fs.openSync(r1csFile, "r");
|
|
136
|
+
public mustGetArtifactsFilePath(fileType: ArtifactsFileType): string {
|
|
137
|
+
const file = this.getArtifactsFilePath(fileType);
|
|
234
138
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
fs.readSync(r1csDescriptor, buffer, { length, position });
|
|
239
|
-
|
|
240
|
-
return BigInt(`0x${buffer.reverse().toString("hex")}`);
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
/// @dev https://github.com/iden3/r1csfile/blob/d82959da1f88fbd06db0407051fde94afbf8824a/doc/r1cs_bin_format.md#format-of-the-file
|
|
244
|
-
const numberOfSections = readBytes(8, 4);
|
|
245
|
-
let sectionStart = 12;
|
|
246
|
-
|
|
247
|
-
for (let i = 0; i < numberOfSections; ++i) {
|
|
248
|
-
const sectionType = Number(readBytes(sectionStart, 4));
|
|
249
|
-
const sectionSize = Number(readBytes(sectionStart + 4, 8));
|
|
250
|
-
|
|
251
|
-
/// @dev Reading header section
|
|
252
|
-
if (sectionType == 1) {
|
|
253
|
-
const totalConstraintsOffset = 4 + 8 + 4 + 32 + 4 + 4 + 4 + 4 + 8;
|
|
254
|
-
|
|
255
|
-
return Number(readBytes(sectionStart + totalConstraintsOffset, 4));
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
sectionStart += 4 + 8 + sectionSize;
|
|
139
|
+
if (!fs.existsSync(file)) {
|
|
140
|
+
throw new Error(`Expected the file "${file}" to exist`);
|
|
259
141
|
}
|
|
260
142
|
|
|
261
|
-
|
|
143
|
+
return file;
|
|
262
144
|
}
|
|
263
145
|
|
|
264
146
|
/**
|
|
265
|
-
* Returns the path to the file of the given type.
|
|
147
|
+
* Returns the path to the file of the given type inside artifacts directory.
|
|
266
148
|
*
|
|
267
|
-
* @param {
|
|
268
|
-
* @param {string | undefined} temp - The temporary directory to use.
|
|
149
|
+
* @param {ArtifactsFileType} fileType - The type of the file.
|
|
269
150
|
* @returns {string} The path to the file.
|
|
270
151
|
*/
|
|
271
|
-
|
|
272
|
-
const
|
|
152
|
+
public getArtifactsFilePath(fileType: ArtifactsFileType): string {
|
|
153
|
+
const circuitName = this.getCircuitName();
|
|
154
|
+
|
|
155
|
+
let fileName: string;
|
|
156
|
+
let fileDir: string = this._config.circuitArtifactsPath;
|
|
273
157
|
|
|
274
158
|
switch (fileType) {
|
|
275
159
|
case "r1cs":
|
|
276
|
-
|
|
160
|
+
fileName = `${circuitName}.r1cs`;
|
|
161
|
+
break;
|
|
277
162
|
case "zkey":
|
|
278
|
-
|
|
163
|
+
fileName = `${circuitName}.zkey`;
|
|
164
|
+
break;
|
|
279
165
|
case "vkey":
|
|
280
|
-
|
|
166
|
+
fileName = `${circuitName}.vkey.json`;
|
|
167
|
+
break;
|
|
281
168
|
case "sym":
|
|
282
|
-
|
|
169
|
+
fileName = `${circuitName}.sym`;
|
|
170
|
+
break;
|
|
283
171
|
case "json":
|
|
284
|
-
|
|
172
|
+
fileName = `${circuitName}_constraints.json`;
|
|
173
|
+
break;
|
|
285
174
|
case "wasm":
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
175
|
+
fileName = `${circuitName}.wasm`;
|
|
176
|
+
fileDir = path.join(fileDir, `${circuitName}_js`);
|
|
177
|
+
break;
|
|
289
178
|
default:
|
|
290
179
|
throw new Error(`Ambiguous file type: ${fileType}.`);
|
|
291
180
|
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Returns the path to the directory of the given type.
|
|
296
|
-
*
|
|
297
|
-
* @param {DirType} dirType - The type of the directory.
|
|
298
|
-
* @returns {string} The path to the directory.
|
|
299
|
-
*/
|
|
300
|
-
private _getDir(dirType: DirType): string {
|
|
301
|
-
const circuitRelativePath = path.relative(this._manager.getCircuitsDir(), this._circuit);
|
|
302
181
|
|
|
303
|
-
|
|
304
|
-
case "circuit":
|
|
305
|
-
return path.join(this._manager.getCircuitsDir(), circuitRelativePath, "..");
|
|
306
|
-
case "artifact":
|
|
307
|
-
return path.join(this._manager.getArtifactsDir(), circuitRelativePath);
|
|
308
|
-
case "verifier":
|
|
309
|
-
return path.join(this._manager.getVerifiersDir(), circuitRelativePath, "..");
|
|
310
|
-
default:
|
|
311
|
-
throw new Error(`Ambiguous dir type: ${dirType}.`);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Returns the path to the file of the given type. Throws an error if the file doesn't exist.
|
|
317
|
-
*
|
|
318
|
-
* @param {FileType} fileType - The type of the file.
|
|
319
|
-
* @param {string | undefined} temp - The temporary directory to use.
|
|
320
|
-
* @returns {string} The path to the file.
|
|
321
|
-
*/
|
|
322
|
-
private _mustGetFile(fileType: FileType, temp?: string): string {
|
|
323
|
-
const file = this._getFile(fileType, temp);
|
|
324
|
-
|
|
325
|
-
if (!fs.existsSync(file)) {
|
|
326
|
-
throw new Error(`Expected the file "${file}" to exist`);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return file;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Moves the files from the temporary directory to the output directory.
|
|
334
|
-
*
|
|
335
|
-
* @param {string} tempDir - The temporary directory.
|
|
336
|
-
* @param {string} outDir - The output directory.
|
|
337
|
-
*/
|
|
338
|
-
private _moveFromTempDirToOutDir(tempDir: string, outDir: string): void {
|
|
339
|
-
fs.rmSync(outDir, { recursive: true, force: true });
|
|
340
|
-
fs.mkdirSync(outDir, { recursive: true });
|
|
341
|
-
|
|
342
|
-
readDirRecursively(tempDir, (dir: string, file: string) => {
|
|
343
|
-
const correspondingOutDir = path.join(outDir, path.relative(tempDir, dir));
|
|
344
|
-
const correspondingOutFile = path.join(outDir, path.relative(tempDir, file));
|
|
345
|
-
|
|
346
|
-
if (!fs.existsSync(correspondingOutDir)) {
|
|
347
|
-
fs.mkdirSync(correspondingOutDir);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
fs.copyFileSync(file, correspondingOutFile);
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Returns a new instance of `CircomRunner`. The `CircomRunner` is used to compile the circuit.
|
|
356
|
-
*
|
|
357
|
-
* @param {string[]} args - The arguments to run the `circom` compiler.
|
|
358
|
-
* @param {boolean} quiet - Whether to suppress the compilation error.
|
|
359
|
-
* @returns {typeof CircomRunner} The `CircomRunner` instance.
|
|
360
|
-
*/
|
|
361
|
-
private _getCircomRunner(args: string[], quiet: boolean): typeof CircomRunner {
|
|
362
|
-
return new CircomRunner({
|
|
363
|
-
args,
|
|
364
|
-
preopens: { "/": "/" },
|
|
365
|
-
bindings: {
|
|
366
|
-
...bindings,
|
|
367
|
-
exit(code: number) {
|
|
368
|
-
throw new Error(`Compilation error. Exit code: ${code}.`);
|
|
369
|
-
},
|
|
370
|
-
fs,
|
|
371
|
-
},
|
|
372
|
-
quiet,
|
|
373
|
-
});
|
|
182
|
+
return path.join(fileDir, fileName);
|
|
374
183
|
}
|
|
375
184
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
export * from "./core/CircomZKit";
|
|
2
1
|
export * from "./core/CircuitZKit";
|
|
3
|
-
export * from "./
|
|
4
|
-
|
|
5
|
-
export { NumericString, PublicSignals, Groth16Proof, Calldata, ProofStruct, Inputs, CircuitInfo } from "./types/types";
|
|
6
|
-
|
|
7
|
-
export { CompileOptions, ManagerZKitConfig, defaultCompileOptions, defaultManagerOptions } from "./config/config";
|
|
2
|
+
export * from "./types/circuit-zkit";
|
|
@@ -28,16 +28,12 @@ export type InputLike = NumberLike | ArrayLike;
|
|
|
28
28
|
|
|
29
29
|
export type Inputs = Record<string, InputLike>;
|
|
30
30
|
|
|
31
|
-
export type
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
export type PtauInfo = {
|
|
41
|
-
file: string;
|
|
42
|
-
url: string | null;
|
|
31
|
+
export type ArtifactsFileType = "r1cs" | "zkey" | "vkey" | "sym" | "json" | "wasm";
|
|
32
|
+
export type VerifierTemplateType = "groth16";
|
|
33
|
+
|
|
34
|
+
export type CircuitZKitConfig = {
|
|
35
|
+
circuitName: string;
|
|
36
|
+
circuitArtifactsPath: string;
|
|
37
|
+
verifierDirPath: string;
|
|
38
|
+
templateType?: VerifierTemplateType;
|
|
43
39
|
};
|
package/src/config/config.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const { Context } = require("@distributedlab/circom2");
|
|
2
|
-
|
|
3
|
-
export type ManagerZKitConfig = {
|
|
4
|
-
circuitsDir: string;
|
|
5
|
-
artifactsDir: string;
|
|
6
|
-
verifiersDir: string;
|
|
7
|
-
ptauDir: string;
|
|
8
|
-
allowDownload: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export const defaultManagerOptions: Partial<ManagerZKitConfig> = {
|
|
12
|
-
circuitsDir: "circuits",
|
|
13
|
-
artifactsDir: "zkit-artifacts",
|
|
14
|
-
verifiersDir: "contracts/verifiers",
|
|
15
|
-
allowDownload: true,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type CompileOptions = {
|
|
19
|
-
sym: boolean;
|
|
20
|
-
json: boolean;
|
|
21
|
-
c: boolean;
|
|
22
|
-
quiet: boolean;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const defaultCompileOptions: CompileOptions = {
|
|
26
|
-
sym: false,
|
|
27
|
-
json: false,
|
|
28
|
-
c: false,
|
|
29
|
-
quiet: false,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type ManagerZKitPrivateConfig = ManagerZKitConfig & {
|
|
33
|
-
compiler: typeof Context;
|
|
34
|
-
templates: {
|
|
35
|
-
groth16: string;
|
|
36
|
-
};
|
|
37
|
-
};
|
package/src/core/CircomZKit.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import os from "os";
|
|
2
|
-
import path from "path";
|
|
3
|
-
|
|
4
|
-
import { CircuitZKit } from "./CircuitZKit";
|
|
5
|
-
import { ManagerZKit } from "./ManagerZKit";
|
|
6
|
-
import { CircuitInfo } from "../types/types";
|
|
7
|
-
import { readDirRecursively } from "../utils/utils";
|
|
8
|
-
import { defaultManagerOptions, ManagerZKitConfig } from "../config/config";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* `CircomZKit` acts as a factory for `CircuitZKit` instances.
|
|
12
|
-
*/
|
|
13
|
-
export class CircomZKit {
|
|
14
|
-
private readonly _manager: ManagerZKit;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new `CircomZKit` instance.
|
|
18
|
-
*
|
|
19
|
-
* @param {Partial<ManagerZKitConfig>} [options=defaultManagerOptions] - The configuration options to use.
|
|
20
|
-
*/
|
|
21
|
-
constructor(options: Partial<ManagerZKitConfig> = defaultManagerOptions) {
|
|
22
|
-
this._manager = new ManagerZKit({ ...defaultManagerOptions, ...options });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Returns a `CircuitZKit` instance for the specified circuit.
|
|
27
|
-
*
|
|
28
|
-
* @dev If the circuit id is not unique, the path to the circuit file must be provided.
|
|
29
|
-
*
|
|
30
|
-
* @param {string} circuit - The path to the circuit file or the circuit id (filename without extension).
|
|
31
|
-
* @returns {CircomZKit} The `CircuitZKit` instance.
|
|
32
|
-
*/
|
|
33
|
-
public getCircuit(circuit: string): CircuitZKit {
|
|
34
|
-
const circuits = this._getAllCircuits();
|
|
35
|
-
|
|
36
|
-
const candidates = circuits.filter((file) => {
|
|
37
|
-
if (circuit.endsWith(".circom")) {
|
|
38
|
-
return file == path.normalize(circuit);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return path.basename(file) == `${circuit}.circom`;
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
if (candidates.length == 0) {
|
|
45
|
-
throw Error(`No circuits with name \"${circuit}\" found`);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (candidates.length > 1) {
|
|
49
|
-
throw Error(
|
|
50
|
-
`Found multiple entries for the circuit "${circuit}".
|
|
51
|
-
|
|
52
|
-
\rConsider replacing \"${circuit}\" with one of the valid paths:
|
|
53
|
-
\r${candidates.map((candidate) => `"${candidate}"`).join(os.EOL)}`,
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return new CircuitZKit(path.join(this._manager.getCircuitsDir(), candidates[0]), this._manager);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Returns an array of all circuits available in the circuits directory.
|
|
62
|
-
*
|
|
63
|
-
* @dev If a circuit id is not unique, the id will be set to `null`.
|
|
64
|
-
*
|
|
65
|
-
* @returns {CircuitInfo[]} An array of circuit information objects.
|
|
66
|
-
*/
|
|
67
|
-
public getCircuits(): CircuitInfo[] {
|
|
68
|
-
const circuits = this._getAllCircuits();
|
|
69
|
-
|
|
70
|
-
let circuitsCount = {} as Record<string, number>;
|
|
71
|
-
|
|
72
|
-
for (const circuit of circuits) {
|
|
73
|
-
const circuitId = path.parse(circuit).name;
|
|
74
|
-
|
|
75
|
-
circuitsCount[circuitId] = (circuitsCount[circuitId] || 0) + 1;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
let result = [] as CircuitInfo[];
|
|
79
|
-
|
|
80
|
-
for (const circuit of circuits) {
|
|
81
|
-
const circuitId = path.parse(circuit).name;
|
|
82
|
-
|
|
83
|
-
result.push({
|
|
84
|
-
path: circuit,
|
|
85
|
-
id: circuitsCount[circuitId] > 1 ? null : circuitId,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Returns an array of all circuits paths available in the circuits directory.
|
|
94
|
-
*
|
|
95
|
-
* @returns {string[]} An array of circuit paths.
|
|
96
|
-
*/
|
|
97
|
-
private _getAllCircuits(): string[] {
|
|
98
|
-
const circuitsDir = this._manager.getCircuitsDir();
|
|
99
|
-
|
|
100
|
-
let circuits = [] as string[];
|
|
101
|
-
|
|
102
|
-
readDirRecursively(circuitsDir, (_dir: string, file: string) => {
|
|
103
|
-
if (path.extname(file) == ".circom") {
|
|
104
|
-
circuits.push(path.relative(circuitsDir, file));
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
return circuits;
|
|
109
|
-
}
|
|
110
|
-
}
|