@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.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +120 -0
  3. package/dist/config/config.d.ts +24 -0
  4. package/dist/config/config.d.ts.map +1 -0
  5. package/dist/config/config.js +17 -0
  6. package/dist/config/config.js.map +1 -0
  7. package/dist/core/CircomZKit.d.ts +39 -0
  8. package/dist/core/CircomZKit.d.ts.map +1 -0
  9. package/dist/core/CircomZKit.js +94 -0
  10. package/dist/core/CircomZKit.js.map +1 -0
  11. package/dist/core/CircuitZKit.d.ts +146 -0
  12. package/dist/core/CircuitZKit.d.ts.map +1 -0
  13. package/dist/core/CircuitZKit.js +342 -0
  14. package/dist/core/CircuitZKit.js.map +1 -0
  15. package/dist/core/ManagerZKit.d.ts +97 -0
  16. package/dist/core/ManagerZKit.d.ts.map +1 -0
  17. package/dist/core/ManagerZKit.js +222 -0
  18. package/dist/core/ManagerZKit.js.map +1 -0
  19. package/dist/core/templates/verifier_groth16.sol.ejs +164 -0
  20. package/dist/index.d.ts +6 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +24 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/types/types.d.ts +46 -0
  25. package/dist/types/types.d.ts.map +1 -0
  26. package/dist/types/types.js +3 -0
  27. package/dist/types/types.js.map +1 -0
  28. package/dist/utils/utils.d.ts +18 -0
  29. package/dist/utils/utils.d.ts.map +1 -0
  30. package/dist/utils/utils.js +58 -0
  31. package/dist/utils/utils.js.map +1 -0
  32. package/package.json +51 -0
  33. package/src/config/config.ts +37 -0
  34. package/src/core/CircomZKit.ts +110 -0
  35. package/src/core/CircuitZKit.ts +375 -0
  36. package/src/core/ManagerZKit.ts +231 -0
  37. package/src/core/templates/verifier_groth16.sol.ejs +164 -0
  38. package/src/index.ts +7 -0
  39. package/src/types/types.ts +43 -0
  40. package/src/utils/utils.ts +60 -0
@@ -0,0 +1,342 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.CircuitZKit = void 0;
30
+ const ejs_1 = __importDefault(require("ejs"));
31
+ const fs_1 = __importDefault(require("fs"));
32
+ const path_1 = __importDefault(require("path"));
33
+ const snarkjs = __importStar(require("snarkjs"));
34
+ const config_1 = require("../config/config");
35
+ const utils_1 = require("../utils/utils");
36
+ const { CircomRunner, bindings } = require("@distributedlab/circom2");
37
+ /**
38
+ * `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
39
+ *
40
+ * @dev This class is not meant to be used directly. Use the `CircomZKit` to create its instance.
41
+ */
42
+ class CircuitZKit {
43
+ _circuit;
44
+ _manager;
45
+ /**
46
+ * Creates a new instance of `CircuitZKit`.
47
+ *
48
+ * @param {string} _circuit - The path to the circuit.
49
+ * @param {ManagerZKit} _manager - The manager that maintains the global state.
50
+ */
51
+ constructor(_circuit, _manager) {
52
+ this._circuit = _circuit;
53
+ this._manager = _manager;
54
+ }
55
+ /**
56
+ * Compiles the circuit and generates the artifacts.
57
+ *
58
+ * @dev If compilation fails, the latest valid artifacts will be preserved.
59
+ * @dev Doesn't show the compilation error if `quiet` is set to `true`.
60
+ *
61
+ * @param {Partial<CompileOptions>} [options=defaultCompileOptions] - Compilation options.
62
+ */
63
+ async compile(options = config_1.defaultCompileOptions) {
64
+ const tempDir = this._manager.getTempDir();
65
+ try {
66
+ const artifactDir = this._getDir("artifact");
67
+ fs_1.default.mkdirSync(tempDir, { recursive: true });
68
+ const overriddenOptions = { ...config_1.defaultCompileOptions, ...options };
69
+ await this._compile(overriddenOptions, tempDir);
70
+ await this._generateZKey(tempDir);
71
+ await this._generateVKey(tempDir);
72
+ this._moveFromTempDirToOutDir(tempDir, artifactDir);
73
+ }
74
+ finally {
75
+ fs_1.default.rmSync(tempDir, { recursive: true, force: true });
76
+ }
77
+ }
78
+ /**
79
+ * Creates a verifier contract.
80
+ */
81
+ async createVerifier() {
82
+ const tempDir = this._manager.getTempDir();
83
+ try {
84
+ const verifierDir = this._getDir("verifier");
85
+ fs_1.default.mkdirSync(tempDir, { recursive: true });
86
+ const vKeyFile = this._mustGetFile("vkey");
87
+ const verifierFile = this._getFile("sol", tempDir);
88
+ const groth16Template = this._manager.getTemplate("groth16");
89
+ const templateParams = JSON.parse(fs_1.default.readFileSync(vKeyFile, "utf-8"));
90
+ templateParams["verifier_id"] = this.getVerifierId();
91
+ const verifierCode = ejs_1.default.render(groth16Template, templateParams);
92
+ fs_1.default.writeFileSync(verifierFile, verifierCode, "utf-8");
93
+ this._moveFromTempDirToOutDir(tempDir, verifierDir);
94
+ }
95
+ finally {
96
+ fs_1.default.rmSync(tempDir, { recursive: true, force: true });
97
+ }
98
+ }
99
+ /**
100
+ * Generates a proof for the given inputs.
101
+ *
102
+ * @dev The `inputs` should be in the same order as the circuit expects them.
103
+ *
104
+ * @param {Inputs} inputs - The inputs for the circuit.
105
+ * @returns {Promise<ProofStruct>} The generated proof.
106
+ * @todo Add support for other proving systems.
107
+ */
108
+ async generateProof(inputs) {
109
+ const zKeyFile = this._mustGetFile("zkey");
110
+ const wasmFile = this._mustGetFile("wasm");
111
+ return (await snarkjs.groth16.fullProve(inputs, wasmFile, zKeyFile));
112
+ }
113
+ /**
114
+ * Verifies the given proof.
115
+ *
116
+ * @dev The `proof` can be generated using the `generateProof` method.
117
+ * @dev The `proof.publicSignals` should be in the same order as the circuit expects them.
118
+ *
119
+ * @param {ProofStruct} proof - The proof to verify.
120
+ * @returns {Promise<boolean>} Whether the proof is valid.
121
+ */
122
+ async verifyProof(proof) {
123
+ const vKeyFile = this._mustGetFile("vkey");
124
+ const verifier = JSON.parse(fs_1.default.readFileSync(vKeyFile).toString());
125
+ return await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);
126
+ }
127
+ /**
128
+ * Generates the calldata for the given proof. The calldata can be used to verify the proof on-chain.
129
+ *
130
+ * @param {ProofStruct} proof - The proof to generate calldata for.
131
+ * @returns {Promise<Calldata>} - The generated calldata.
132
+ * @todo Add other types of calldata.
133
+ */
134
+ async generateCalldata(proof) {
135
+ const calldata = await snarkjs.groth16.exportSolidityCallData(proof.proof, proof.publicSignals);
136
+ return JSON.parse(`[${calldata}]`);
137
+ }
138
+ /**
139
+ * Returns the circuit ID. The circuit ID is the name of the circuit file without the extension.
140
+ *
141
+ * @returns {string} The circuit ID.
142
+ */
143
+ getCircuitId() {
144
+ return path_1.default.parse(this._circuit).name;
145
+ }
146
+ /**
147
+ * Returns the verifier ID. The verifier ID is the name of the circuit file without the extension, suffixed with "Verifier".
148
+ *
149
+ * @returns {string} The verifier ID.
150
+ */
151
+ getVerifierId() {
152
+ return `${path_1.default.parse(this._circuit).name}Verifier`;
153
+ }
154
+ /**
155
+ * Generates zero-knowledge key for the circuit.
156
+ *
157
+ * @param {string} outDir - The directory to save the generated key.
158
+ * @todo This method may cause issues https://github.com/iden3/snarkjs/issues/494
159
+ */
160
+ async _generateZKey(outDir) {
161
+ const r1csFile = this._getFile("r1cs", outDir);
162
+ const zKeyFile = this._getFile("zkey", outDir);
163
+ const constraints = await this._getConstraints(outDir);
164
+ const ptauFile = await this._manager.fetchPtauFile(constraints);
165
+ await snarkjs.zKey.newZKey(r1csFile, ptauFile, zKeyFile);
166
+ }
167
+ /**
168
+ * Generates verification key for the circuit.
169
+ *
170
+ * @param {string} outDir - The directory to save the generated key.
171
+ */
172
+ async _generateVKey(outDir) {
173
+ const zKeyFile = this._getFile("zkey", outDir);
174
+ const vKeyFile = this._getFile("vkey", outDir);
175
+ const vKeyData = await snarkjs.zKey.exportVerificationKey(zKeyFile);
176
+ fs_1.default.writeFileSync(vKeyFile, JSON.stringify(vKeyData));
177
+ }
178
+ /**
179
+ * Returns the arguments to compile the circuit.
180
+ *
181
+ * @param {CompileOptions} options - Compilation options.
182
+ * @param {string} outDir - The directory to save the compiled artifacts.
183
+ * @returns {string[]} The arguments to compile the circuit.
184
+ */
185
+ _getCompileArgs(options, outDir) {
186
+ let args = [this._circuit, "--r1cs", "--wasm"];
187
+ options.sym && args.push("--sym");
188
+ options.json && args.push("--json");
189
+ options.c && args.push("--c");
190
+ args.push("-o", outDir);
191
+ return args;
192
+ }
193
+ /**
194
+ * Compiles the circuit.
195
+ *
196
+ * @param {CompileOptions} options - Compilation options.
197
+ * @param {string} outDir - The directory to save the compiled artifacts.
198
+ */
199
+ async _compile(options, outDir) {
200
+ const args = this._getCompileArgs(options, outDir);
201
+ try {
202
+ await this._getCircomRunner(args, options.quiet).execute(this._manager.getCompiler());
203
+ }
204
+ catch (err) {
205
+ if (options.quiet) {
206
+ throw new Error('Compilation failed with an unknown error. Consider passing "quiet=false" flag to see the compilation error.', { cause: err });
207
+ }
208
+ throw new Error("Compilation failed.", { cause: err });
209
+ }
210
+ }
211
+ /**
212
+ * Returns the number of constraints in the circuit. This value is used to fetch the correct `ptau` file.
213
+ *
214
+ * @param {string} outDir - The directory where the compiled artifacts are saved.
215
+ * @returns {Promise<number>} The number of constraints in the circuit.
216
+ */
217
+ async _getConstraints(outDir) {
218
+ const r1csFile = this._getFile("r1cs", outDir);
219
+ const r1csDescriptor = fs_1.default.openSync(r1csFile, "r");
220
+ const readBytes = (position, length) => {
221
+ const buffer = Buffer.alloc(length);
222
+ fs_1.default.readSync(r1csDescriptor, buffer, { length, position });
223
+ return BigInt(`0x${buffer.reverse().toString("hex")}`);
224
+ };
225
+ /// @dev https://github.com/iden3/r1csfile/blob/d82959da1f88fbd06db0407051fde94afbf8824a/doc/r1cs_bin_format.md#format-of-the-file
226
+ const numberOfSections = readBytes(8, 4);
227
+ let sectionStart = 12;
228
+ for (let i = 0; i < numberOfSections; ++i) {
229
+ const sectionType = Number(readBytes(sectionStart, 4));
230
+ const sectionSize = Number(readBytes(sectionStart + 4, 8));
231
+ /// @dev Reading header section
232
+ if (sectionType == 1) {
233
+ const totalConstraintsOffset = 4 + 8 + 4 + 32 + 4 + 4 + 4 + 4 + 8;
234
+ return Number(readBytes(sectionStart + totalConstraintsOffset, 4));
235
+ }
236
+ sectionStart += 4 + 8 + sectionSize;
237
+ }
238
+ throw new Error("Header section is not found.");
239
+ }
240
+ /**
241
+ * Returns the path to the file of the given type.
242
+ *
243
+ * @param {FileType} fileType - The type of the file.
244
+ * @param {string | undefined} temp - The temporary directory to use.
245
+ * @returns {string} The path to the file.
246
+ */
247
+ _getFile(fileType, temp) {
248
+ const circuitId = this.getCircuitId();
249
+ switch (fileType) {
250
+ case "r1cs":
251
+ return path_1.default.join(temp ?? this._getDir("artifact"), `${circuitId}.r1cs`);
252
+ case "zkey":
253
+ return path_1.default.join(temp ?? this._getDir("artifact"), `${circuitId}.zkey`);
254
+ case "vkey":
255
+ return path_1.default.join(temp ?? this._getDir("artifact"), `${circuitId}.vkey.json`);
256
+ case "sym":
257
+ return path_1.default.join(temp ?? this._getDir("artifact"), `${circuitId}.sym`);
258
+ case "json":
259
+ return path_1.default.join(temp ?? this._getDir("artifact"), `${circuitId}.json`);
260
+ case "wasm":
261
+ return path_1.default.join(temp ?? this._getDir("artifact"), `${circuitId}_js`, `${circuitId}.wasm`);
262
+ case "sol":
263
+ return path_1.default.join(temp ?? this._getDir("verifier"), `${circuitId}Verifier.sol`);
264
+ default:
265
+ throw new Error(`Ambiguous file type: ${fileType}.`);
266
+ }
267
+ }
268
+ /**
269
+ * Returns the path to the directory of the given type.
270
+ *
271
+ * @param {DirType} dirType - The type of the directory.
272
+ * @returns {string} The path to the directory.
273
+ */
274
+ _getDir(dirType) {
275
+ const circuitRelativePath = path_1.default.relative(this._manager.getCircuitsDir(), this._circuit);
276
+ switch (dirType) {
277
+ case "circuit":
278
+ return path_1.default.join(this._manager.getCircuitsDir(), circuitRelativePath, "..");
279
+ case "artifact":
280
+ return path_1.default.join(this._manager.getArtifactsDir(), circuitRelativePath);
281
+ case "verifier":
282
+ return path_1.default.join(this._manager.getVerifiersDir(), circuitRelativePath, "..");
283
+ default:
284
+ throw new Error(`Ambiguous dir type: ${dirType}.`);
285
+ }
286
+ }
287
+ /**
288
+ * Returns the path to the file of the given type. Throws an error if the file doesn't exist.
289
+ *
290
+ * @param {FileType} fileType - The type of the file.
291
+ * @param {string | undefined} temp - The temporary directory to use.
292
+ * @returns {string} The path to the file.
293
+ */
294
+ _mustGetFile(fileType, temp) {
295
+ const file = this._getFile(fileType, temp);
296
+ if (!fs_1.default.existsSync(file)) {
297
+ throw new Error(`Expected the file "${file}" to exist`);
298
+ }
299
+ return file;
300
+ }
301
+ /**
302
+ * Moves the files from the temporary directory to the output directory.
303
+ *
304
+ * @param {string} tempDir - The temporary directory.
305
+ * @param {string} outDir - The output directory.
306
+ */
307
+ _moveFromTempDirToOutDir(tempDir, outDir) {
308
+ fs_1.default.rmSync(outDir, { recursive: true, force: true });
309
+ fs_1.default.mkdirSync(outDir, { recursive: true });
310
+ (0, utils_1.readDirRecursively)(tempDir, (dir, file) => {
311
+ const correspondingOutDir = path_1.default.join(outDir, path_1.default.relative(tempDir, dir));
312
+ const correspondingOutFile = path_1.default.join(outDir, path_1.default.relative(tempDir, file));
313
+ if (!fs_1.default.existsSync(correspondingOutDir)) {
314
+ fs_1.default.mkdirSync(correspondingOutDir);
315
+ }
316
+ fs_1.default.copyFileSync(file, correspondingOutFile);
317
+ });
318
+ }
319
+ /**
320
+ * Returns a new instance of `CircomRunner`. The `CircomRunner` is used to compile the circuit.
321
+ *
322
+ * @param {string[]} args - The arguments to run the `circom` compiler.
323
+ * @param {boolean} quiet - Whether to suppress the compilation error.
324
+ * @returns {typeof CircomRunner} The `CircomRunner` instance.
325
+ */
326
+ _getCircomRunner(args, quiet) {
327
+ return new CircomRunner({
328
+ args,
329
+ preopens: { "/": "/" },
330
+ bindings: {
331
+ ...bindings,
332
+ exit(code) {
333
+ throw new Error(`Compilation error. Exit code: ${code}.`);
334
+ },
335
+ fs: fs_1.default,
336
+ },
337
+ quiet,
338
+ });
339
+ }
340
+ }
341
+ exports.CircuitZKit = CircuitZKit;
342
+ //# sourceMappingURL=CircuitZKit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CircuitZKit.js","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8CAAsB;AACtB,4CAAoB;AACpB,gDAAwB;AACxB,iDAAmC;AAEnC,6CAAyE;AAGzE,0CAAoD;AAEpD,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;AAEtE;;;;GAIG;AACH,MAAa,WAAW;IAQH;IACA;IARnB;;;;;OAKG;IACH,YACmB,QAAgB,EAChB,QAAqB;QADrB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,aAAQ,GAAR,QAAQ,CAAa;IACrC,CAAC;IAEJ;;;;;;;OAOG;IACI,KAAK,CAAC,OAAO,CAAC,UAAmC,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AI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACpD,YAAE,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAE1C,IAAA,0BAAkB,EAAC,OAAO,EAAE,CAAC,GAAW,EAAE,IAAY,EAAE,EAAE;YACxD,MAAM,mBAAmB,GAAG,cAAI,CAAC,IAAI,CAAC,MAAM,EAAE,cAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;YAC3E,MAAM,oBAAoB,GAAG,cAAI,CAAC,IAAI,CAAC,MAAM,EAAE,cAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YAE7E,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,mBAAmB,CAAC,EAAE,CAAC;gBACxC,YAAE,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;YACpC,CAAC;YAED,YAAE,CAAC,YAAY,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACK,gBAAgB,CAAC,IAAc,EAAE,KAAc;QACrD,OAAO,IAAI,YAAY,CAAC;YACtB,IAAI;YACJ,QAAQ,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;YACtB,QAAQ,EAAE;gBACR,GAAG,QAAQ;gBACX,IAAI,CAAC,IAAY;oBACf,MAAM,IAAI,KAAK,CAAC,iCAAiC,IAAI,GAAG,CAAC,CAAC;gBAC5D,CAAC;gBACD,EAAE,EAAF,YAAE;aACH;YACD,KAAK;SACN,CAAC,CAAC;IACL,CAAC;CACF;AArWD,kCAqWC"}
@@ -0,0 +1,97 @@
1
+ import { ManagerZKitConfig } from "../config/config";
2
+ import { TemplateType } from "../types/types";
3
+ /**
4
+ * `ManagerZKit` provides configuration options and utility methods used by the `CircomZKit` and `CircuitZKit` classes.
5
+ */
6
+ export declare class ManagerZKit {
7
+ private _config;
8
+ /**
9
+ * Creates a new `ManagerZKit` instance.
10
+ *
11
+ * @param {Partial<ManagerZKitConfig>} [config=defaultManagerOptions] - The configuration options to use.
12
+ */
13
+ constructor(config?: Partial<ManagerZKitConfig>);
14
+ /**
15
+ * Fetches the `ptau` file.
16
+ *
17
+ * @dev If `ptau` file is not found, this method will try to download it. Use `allowDownload=false` to disable this behavior.
18
+ *
19
+ * @param {number} minConstraints - The minimum number of constraints the `ptau` file must support.
20
+ * @returns {Promise<string>} The path to the `ptau` file.
21
+ */
22
+ fetchPtauFile(minConstraints: number): Promise<string>;
23
+ /**
24
+ * Returns the path to the artifacts' directory.
25
+ *
26
+ * @returns {string} The path to the artifacts' directory.
27
+ */
28
+ getArtifactsDir(): string;
29
+ /**
30
+ * Returns the path to the circuits' directory.
31
+ *
32
+ * @returns {string} The path to the circuits' directory.
33
+ */
34
+ getCircuitsDir(): string;
35
+ /**
36
+ * Returns the path to the verifiers' directory.
37
+ *
38
+ * @returns {string} The path to the verifiers' directory.
39
+ */
40
+ getVerifiersDir(): string;
41
+ /**
42
+ * Returns the path to the `ptau` directory.
43
+ *
44
+ * @dev The default `ptau` directory is located at `${HOME}/.zkit/.ptau`.
45
+ *
46
+ * @returns {string} The path to the `ptau` directory.
47
+ */
48
+ getPtauDir(): string;
49
+ /**
50
+ * Returns a temporary directory path.
51
+ *
52
+ * @dev Temporary files are stored in the OS's temporary directory.
53
+ *
54
+ * @returns {string} A temporary directory path.
55
+ */
56
+ getTempDir(): string;
57
+ /**
58
+ * Returns the circom compiler's wasm binary.
59
+ *
60
+ * @returns {string} The circom compiler's wasm binary.
61
+ */
62
+ getCompiler(): string;
63
+ /**
64
+ * Returns the Solidity verifier template for the specified proving system.
65
+ *
66
+ * @param {TemplateType} templateType - The template type.
67
+ * @returns {string} The Solidity verifier template.
68
+ */
69
+ getTemplate(templateType: TemplateType): string;
70
+ /**
71
+ * Returns whether the download of the `ptau` file is allowed.
72
+ *
73
+ * @returns {boolean} Whether the download of the `ptau` file is allowed.
74
+ */
75
+ getAllowDownload(): boolean;
76
+ /**
77
+ * Downloads the `ptau` file. The download is allowed only if the user confirms it.
78
+ *
79
+ * @param {PtauInfo} ptauInfo - The `ptau` file and download url.
80
+ */
81
+ private _downloadPtau;
82
+ /**
83
+ * Searches for the `ptau` file that supports the specified number of constraints.
84
+ *
85
+ * @param {number} ptauId - The `ptau` file id.
86
+ * @returns {PtauInfo} The `ptau` file path and download url if the file doesn't exist.
87
+ */
88
+ private _searchPtau;
89
+ /**
90
+ * Prompts the user to allow the download of the `ptau` file.
91
+ *
92
+ * @param {PtauInfo} ptauInfo - The `ptau` file and download url.
93
+ * @returns {Promise<boolean>} Whether the download is allowed.
94
+ */
95
+ private _askForDownloadAllowance;
96
+ }
97
+ //# sourceMappingURL=ManagerZKit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ManagerZKit.d.ts","sourceRoot":"","sources":["../../src/core/ManagerZKit.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,iBAAiB,EAAmD,MAAM,kBAAkB,CAAC;AACtG,OAAO,EAAY,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGxD;;GAEG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAA2B;IAE1C;;;;OAIG;gBACS,MAAM,GAAE,OAAO,CAAC,iBAAiB,CAAyB;IAsBtE;;;;;;;OAOG;IACU,aAAa,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAkBnE;;;;OAIG;IACI,eAAe,IAAI,MAAM;IAIhC;;;;OAIG;IACI,cAAc,IAAI,MAAM;IAI/B;;;;OAIG;IACI,eAAe,IAAI,MAAM;IAIhC;;;;;;OAMG;IACI,UAAU,IAAI,MAAM;IAI3B;;;;;;OAMG;IACI,UAAU,IAAI,MAAM;IAI3B;;;;OAIG;IACI,WAAW,IAAI,MAAM;IAI5B;;;;;OAKG;IACI,WAAW,CAAC,YAAY,EAAE,YAAY,GAAG,MAAM;IAStD;;;;OAIG;IACI,gBAAgB,IAAI,OAAO;IAIlC;;;;OAIG;YACW,aAAa;IAc3B;;;;;OAKG;IACH,OAAO,CAAC,WAAW;IA+BnB;;;;;OAKG;IACH,OAAO,CAAC,wBAAwB;CAgBjC"}
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.ManagerZKit = void 0;
30
+ const fs_1 = __importDefault(require("fs"));
31
+ const os_1 = __importDefault(require("os"));
32
+ const path_1 = __importDefault(require("path"));
33
+ const process_1 = __importDefault(require("process"));
34
+ const readline = __importStar(require("readline"));
35
+ const uuid_1 = require("uuid");
36
+ const config_1 = require("../config/config");
37
+ const utils_1 = require("../utils/utils");
38
+ /**
39
+ * `ManagerZKit` provides configuration options and utility methods used by the `CircomZKit` and `CircuitZKit` classes.
40
+ */
41
+ class ManagerZKit {
42
+ _config;
43
+ /**
44
+ * Creates a new `ManagerZKit` instance.
45
+ *
46
+ * @param {Partial<ManagerZKitConfig>} [config=defaultManagerOptions] - The configuration options to use.
47
+ */
48
+ constructor(config = config_1.defaultManagerOptions) {
49
+ const overriddenConfig = { ...config_1.defaultManagerOptions, ...config };
50
+ overriddenConfig.circuitsDir = path_1.default.join(process_1.default.cwd(), overriddenConfig.circuitsDir);
51
+ overriddenConfig.artifactsDir = path_1.default.join(process_1.default.cwd(), overriddenConfig.artifactsDir);
52
+ overriddenConfig.verifiersDir = path_1.default.join(process_1.default.cwd(), overriddenConfig.verifiersDir);
53
+ if (overriddenConfig.ptauDir) {
54
+ overriddenConfig.ptauDir = path_1.default.join(process_1.default.cwd(), overriddenConfig.ptauDir);
55
+ }
56
+ else {
57
+ overriddenConfig.ptauDir = path_1.default.join(os_1.default.homedir(), ".zkit", ".ptau");
58
+ }
59
+ this._config = {
60
+ ...overriddenConfig,
61
+ compiler: fs_1.default.readFileSync(require.resolve("@distributedlab/circom2/circom.wasm")),
62
+ templates: {
63
+ groth16: fs_1.default.readFileSync(path_1.default.join(__dirname, "templates", "verifier_groth16.sol.ejs"), "utf8"),
64
+ },
65
+ };
66
+ }
67
+ /**
68
+ * Fetches the `ptau` file.
69
+ *
70
+ * @dev If `ptau` file is not found, this method will try to download it. Use `allowDownload=false` to disable this behavior.
71
+ *
72
+ * @param {number} minConstraints - The minimum number of constraints the `ptau` file must support.
73
+ * @returns {Promise<string>} The path to the `ptau` file.
74
+ */
75
+ async fetchPtauFile(minConstraints) {
76
+ const ptauId = Math.max(Math.ceil(Math.log2(minConstraints)), 8);
77
+ if (ptauId > 20) {
78
+ throw new Error('Circuit has too many constraints. The maximum number of constraints is 2^20. Consider passing "ptauDir=PATH_TO_LOCAL_DIR".');
79
+ }
80
+ const ptauInfo = this._searchPtau(ptauId);
81
+ if (ptauInfo.url) {
82
+ await this._downloadPtau(ptauInfo);
83
+ }
84
+ return ptauInfo.file;
85
+ }
86
+ /**
87
+ * Returns the path to the artifacts' directory.
88
+ *
89
+ * @returns {string} The path to the artifacts' directory.
90
+ */
91
+ getArtifactsDir() {
92
+ return this._config.artifactsDir;
93
+ }
94
+ /**
95
+ * Returns the path to the circuits' directory.
96
+ *
97
+ * @returns {string} The path to the circuits' directory.
98
+ */
99
+ getCircuitsDir() {
100
+ return this._config.circuitsDir;
101
+ }
102
+ /**
103
+ * Returns the path to the verifiers' directory.
104
+ *
105
+ * @returns {string} The path to the verifiers' directory.
106
+ */
107
+ getVerifiersDir() {
108
+ return this._config.verifiersDir;
109
+ }
110
+ /**
111
+ * Returns the path to the `ptau` directory.
112
+ *
113
+ * @dev The default `ptau` directory is located at `${HOME}/.zkit/.ptau`.
114
+ *
115
+ * @returns {string} The path to the `ptau` directory.
116
+ */
117
+ getPtauDir() {
118
+ return this._config.ptauDir;
119
+ }
120
+ /**
121
+ * Returns a temporary directory path.
122
+ *
123
+ * @dev Temporary files are stored in the OS's temporary directory.
124
+ *
125
+ * @returns {string} A temporary directory path.
126
+ */
127
+ getTempDir() {
128
+ return path_1.default.join(os_1.default.tmpdir(), ".zkit", (0, uuid_1.v4)());
129
+ }
130
+ /**
131
+ * Returns the circom compiler's wasm binary.
132
+ *
133
+ * @returns {string} The circom compiler's wasm binary.
134
+ */
135
+ getCompiler() {
136
+ return this._config.compiler;
137
+ }
138
+ /**
139
+ * Returns the Solidity verifier template for the specified proving system.
140
+ *
141
+ * @param {TemplateType} templateType - The template type.
142
+ * @returns {string} The Solidity verifier template.
143
+ */
144
+ getTemplate(templateType) {
145
+ switch (templateType) {
146
+ case "groth16":
147
+ return this._config.templates.groth16;
148
+ default:
149
+ throw new Error(`Ambiguous template type: ${templateType}.`);
150
+ }
151
+ }
152
+ /**
153
+ * Returns whether the download of the `ptau` file is allowed.
154
+ *
155
+ * @returns {boolean} Whether the download of the `ptau` file is allowed.
156
+ */
157
+ getAllowDownload() {
158
+ return this._config.allowDownload;
159
+ }
160
+ /**
161
+ * Downloads the `ptau` file. The download is allowed only if the user confirms it.
162
+ *
163
+ * @param {PtauInfo} ptauInfo - The `ptau` file and download url.
164
+ */
165
+ async _downloadPtau(ptauInfo) {
166
+ if (!this.getAllowDownload() && !(await this._askForDownloadAllowance(ptauInfo))) {
167
+ throw new Error('Download is cancelled. Allow download or consider passing "ptauDir=PATH_TO_LOCAL_DIR" to the existing ptau files');
168
+ }
169
+ fs_1.default.mkdirSync(this.getPtauDir(), { recursive: true });
170
+ if (!(await (0, utils_1.downloadFile)(ptauInfo.file, ptauInfo.url))) {
171
+ throw new Error("Something went wrong while downloading the ptau file.");
172
+ }
173
+ }
174
+ /**
175
+ * Searches for the `ptau` file that supports the specified number of constraints.
176
+ *
177
+ * @param {number} ptauId - The `ptau` file id.
178
+ * @returns {PtauInfo} The `ptau` file path and download url if the file doesn't exist.
179
+ */
180
+ _searchPtau(ptauId) {
181
+ let entries = [];
182
+ if (fs_1.default.existsSync(this.getPtauDir())) {
183
+ entries = fs_1.default.readdirSync(this.getPtauDir(), { withFileTypes: true });
184
+ }
185
+ const entry = entries.find((entry) => {
186
+ if (!entry.isFile()) {
187
+ return false;
188
+ }
189
+ const match = entry.name.match(/^powers-of-tau-(\d+)\.ptau$/);
190
+ if (!match) {
191
+ return false;
192
+ }
193
+ const entryPtauId = parseInt(match[1]);
194
+ return ptauId <= entryPtauId;
195
+ });
196
+ const file = path_1.default.join(this.getPtauDir(), entry ? entry.name : `powers-of-tau-${ptauId}.ptau`);
197
+ const url = entry
198
+ ? null
199
+ : `https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_${ptauId.toString().padStart(2, "0")}.ptau`;
200
+ return { file, url };
201
+ }
202
+ /**
203
+ * Prompts the user to allow the download of the `ptau` file.
204
+ *
205
+ * @param {PtauInfo} ptauInfo - The `ptau` file and download url.
206
+ * @returns {Promise<boolean>} Whether the download is allowed.
207
+ */
208
+ _askForDownloadAllowance(ptauInfo) {
209
+ return new Promise((resolve) => {
210
+ const readLine = readline.createInterface({
211
+ input: process_1.default.stdin,
212
+ output: process_1.default.stdout,
213
+ });
214
+ readLine.question(`No ptau found. Press [Y] to download it from "${ptauInfo.url}" to ${ptauInfo.file}: `, (response) => {
215
+ readLine.close();
216
+ resolve(response.toUpperCase() == "Y");
217
+ });
218
+ });
219
+ }
220
+ }
221
+ exports.ManagerZKit = ManagerZKit;
222
+ //# sourceMappingURL=ManagerZKit.js.map