@solarity/zkit 0.1.1 → 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/core/CircuitZKit.d.ts +23 -90
- package/dist/core/CircuitZKit.d.ts.map +1 -1
- package/dist/core/CircuitZKit.js +65 -235
- 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 -285
- package/src/index.ts +1 -6
- package/src/types/{types.ts → circuit-zkit.ts} +8 -12
- package/src/config/config.ts +0 -43
- package/src/core/CircomZKit.ts +0 -110
- package/src/core/ManagerZKit.ts +0 -231
- package/src/utils/utils.ts +0 -60
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CircuitZKit.js","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"CircuitZKit.js","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8CAAsB;AACtB,4CAAoB;AACpB,gDAAwB;AACxB,iDAAmC;AAWnC;;GAEG;AACH,MAAa,WAAW;IACO;IAA7B,YAA6B,OAA0B;QAA1B,YAAO,GAAP,OAAO,CAAmB;IAAG,CAAC;IAE3D;;;;;OAKG;IACI,MAAM,CAAC,WAAW,CAAC,YAAkC;QAC1D,QAAQ,YAAY,EAAE,CAAC;YACrB,KAAK,SAAS;gBACZ,OAAO,YAAE,CAAC,YAAY,CAAC,cAAI,CAAC,IA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ACzB,MAAM,IAAI,KAAK,CAAC,sBAAsB,IAAI,YAAY,CAAC,CAAC;QAC1D,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACI,oBAAoB,CAAC,QAA2B;QACrD,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;QAE1C,IAAI,QAAgB,CAAC;QACrB,IAAI,OAAO,GAAW,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC;QAExD,QAAQ,QAAQ,EAAE,CAAC;YACjB,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,WAAW,OAAO,CAAC;gBACjC,MAAM;YACR,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,WAAW,OAAO,CAAC;gBACjC,MAAM;YACR,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,WAAW,YAAY,CAAC;gBACtC,MAAM;YACR,KAAK,KAAK;gBACR,QAAQ,GAAG,GAAG,WAAW,MAAM,CAAC;gBAChC,MAAM;YACR,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,WAAW,mBAAmB,CAAC;gBAC7C,MAAM;YACR,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,WAAW,OAAO,CAAC;gBACjC,OAAO,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,WAAW,KAAK,CAAC,CAAC;gBAClD,MAAM;YACR;gBACE,MAAM,IAAI,KAAK,CAAC,wBAAwB,QAAQ,GAAG,CAAC,CAAC;QACzD,CAAC;QAED,OAAO,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACtC,CAAC;CACF;AAtKD,kCAsKC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
export * from "./core/CircomZKit";
|
|
2
1
|
export * from "./core/CircuitZKit";
|
|
3
|
-
export * from "./
|
|
4
|
-
export { NumericString, PublicSignals, Groth16Proof, Calldata, ProofStruct, Inputs, CircuitInfo } from "./types/types";
|
|
5
|
-
export { CompileOptions, ManagerZKitConfig, defaultCompileOptions, defaultManagerOptions } from "./config/config";
|
|
2
|
+
export * from "./types/circuit-zkit";
|
|
6
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -14,11 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.defaultManagerOptions = exports.defaultCompileOptions = void 0;
|
|
18
|
-
__exportStar(require("./core/CircomZKit"), exports);
|
|
19
17
|
__exportStar(require("./core/CircuitZKit"), exports);
|
|
20
|
-
__exportStar(require("./
|
|
21
|
-
var config_1 = require("./config/config");
|
|
22
|
-
Object.defineProperty(exports, "defaultCompileOptions", { enumerable: true, get: function () { return config_1.defaultCompileOptions; } });
|
|
23
|
-
Object.defineProperty(exports, "defaultManagerOptions", { enumerable: true, get: function () { return config_1.defaultManagerOptions; } });
|
|
18
|
+
__exportStar(require("./types/circuit-zkit"), exports);
|
|
24
19
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,uDAAqC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type NumericString = `${number}` | string;
|
|
2
|
+
export type PublicSignals = NumericString[];
|
|
3
|
+
export type Groth16Proof = {
|
|
4
|
+
pi_a: [NumericString, NumericString];
|
|
5
|
+
pi_b: [[NumericString, NumericString], [NumericString, NumericString]];
|
|
6
|
+
pi_c: [NumericString, NumericString];
|
|
7
|
+
protocol: string;
|
|
8
|
+
curve: string;
|
|
9
|
+
};
|
|
10
|
+
export type Calldata = [
|
|
11
|
+
[
|
|
12
|
+
NumericString,
|
|
13
|
+
NumericString
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
[NumericString, NumericString],
|
|
17
|
+
[NumericString, NumericString]
|
|
18
|
+
],
|
|
19
|
+
[
|
|
20
|
+
NumericString,
|
|
21
|
+
NumericString
|
|
22
|
+
],
|
|
23
|
+
[
|
|
24
|
+
NumericString
|
|
25
|
+
]
|
|
26
|
+
];
|
|
27
|
+
export type ProofStruct = {
|
|
28
|
+
proof: Groth16Proof;
|
|
29
|
+
publicSignals: PublicSignals;
|
|
30
|
+
};
|
|
31
|
+
export type NumberLike = number | bigint | string;
|
|
32
|
+
export type ArrayLike = NumberLike[] | ArrayLike[];
|
|
33
|
+
export type InputLike = NumberLike | ArrayLike;
|
|
34
|
+
export type Inputs = Record<string, InputLike>;
|
|
35
|
+
export type ArtifactsFileType = "r1cs" | "zkey" | "vkey" | "sym" | "json" | "wasm";
|
|
36
|
+
export type VerifierTemplateType = "groth16";
|
|
37
|
+
export type CircuitZKitConfig = {
|
|
38
|
+
circuitName: string;
|
|
39
|
+
circuitArtifactsPath: string;
|
|
40
|
+
verifierDirPath: string;
|
|
41
|
+
templateType?: VerifierTemplateType;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=circuit-zkit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"circuit-zkit.d.ts","sourceRoot":"","sources":["../../src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G;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,oBAAoB,CAAC;CACrC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"circuit-zkit.js","sourceRoot":"","sources":["../../src/types/circuit-zkit.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solarity/zkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Distributed Lab",
|
|
6
6
|
"readme": "README.md",
|
|
@@ -26,25 +26,36 @@
|
|
|
26
26
|
"scripts": {
|
|
27
27
|
"prepare": "husky",
|
|
28
28
|
"build": "tsc",
|
|
29
|
-
"test": "
|
|
30
|
-
"
|
|
29
|
+
"test": "mocha --recursive 'test/**/*.ts' --exit",
|
|
30
|
+
"coverage": "nyc mocha --recursive 'test/**/*.ts' --exit",
|
|
31
|
+
"lint-fix": "prettier --write \"./**/*.ts\"",
|
|
31
32
|
"publish-to-npm": "npm run lint-fix && bash ./scripts/publish.sh"
|
|
32
33
|
},
|
|
34
|
+
"nyc": {
|
|
35
|
+
"reporter": [
|
|
36
|
+
"html",
|
|
37
|
+
"text"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
33
40
|
"dependencies": {
|
|
34
|
-
"@distributedlab/circom2": "0.2.18-rc.2",
|
|
35
41
|
"ejs": "3.1.10",
|
|
36
|
-
"snarkjs": "0.7.3"
|
|
37
|
-
"uuid": "9.0.1"
|
|
42
|
+
"snarkjs": "0.7.3"
|
|
38
43
|
},
|
|
39
44
|
"devDependencies": {
|
|
45
|
+
"@nomicfoundation/hardhat-ethers": "3.0.5",
|
|
40
46
|
"@types/ejs": "^3.1.5",
|
|
41
|
-
"@types/jest": "^29.5.12",
|
|
42
47
|
"@types/snarkjs": "^0.7.8",
|
|
43
|
-
"@types/
|
|
48
|
+
"@types/chai": "^4.3.12",
|
|
49
|
+
"@types/chai-as-promised": "^7.1.8",
|
|
50
|
+
"@types/mocha": "^10.0.6",
|
|
51
|
+
"chai": "^4.4.1",
|
|
52
|
+
"chai-as-promised": "^7.1.1",
|
|
53
|
+
"mocha": "^10.3.0",
|
|
54
|
+
"nyc": "^15.1.0",
|
|
55
|
+
"ethers": "6.11.1",
|
|
56
|
+
"hardhat": "2.20.1",
|
|
44
57
|
"husky": "^9.0.11",
|
|
45
|
-
"jest": "^29.7.0",
|
|
46
58
|
"prettier": "^3.2.5",
|
|
47
|
-
"ts-jest": "^29.1.2",
|
|
48
59
|
"ts-node": "^10.9.2",
|
|
49
60
|
"typescript": "^5.4.5"
|
|
50
61
|
}
|
package/src/core/CircuitZKit.ts
CHANGED
|
@@ -1,89 +1,57 @@
|
|
|
1
|
-
import { randomBytes } from "crypto";
|
|
2
1
|
import ejs from "ejs";
|
|
3
2
|
import fs from "fs";
|
|
4
3
|
import path from "path";
|
|
5
4
|
import * as snarkjs from "snarkjs";
|
|
6
5
|
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
import {
|
|
7
|
+
ArtifactsFileType,
|
|
8
|
+
Calldata,
|
|
9
|
+
CircuitZKitConfig,
|
|
10
|
+
Inputs,
|
|
11
|
+
ProofStruct,
|
|
12
|
+
VerifierTemplateType,
|
|
13
|
+
} from "../types/circuit-zkit";
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
|
|
16
|
-
*
|
|
17
|
-
* @dev This class is not meant to be used directly. Use the `CircomZKit` to create its instance.
|
|
18
17
|
*/
|
|
19
18
|
export class CircuitZKit {
|
|
20
|
-
|
|
21
|
-
* Creates a new instance of `CircuitZKit`.
|
|
22
|
-
*
|
|
23
|
-
* @param {string} _circuit - The path to the circuit.
|
|
24
|
-
* @param {ManagerZKit} _manager - The manager that maintains the global state.
|
|
25
|
-
*/
|
|
26
|
-
constructor(
|
|
27
|
-
private readonly _circuit: string,
|
|
28
|
-
private readonly _manager: ManagerZKit,
|
|
29
|
-
) {}
|
|
19
|
+
constructor(private readonly _config: CircuitZKitConfig) {}
|
|
30
20
|
|
|
31
21
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @dev If compilation fails, the latest valid artifacts will be preserved.
|
|
35
|
-
* @dev Doesn't show the compilation error if `quiet` is set to `true`.
|
|
22
|
+
* Returns the Solidity verifier template for the specified proving system.
|
|
36
23
|
*
|
|
37
|
-
* @param {
|
|
24
|
+
* @param {VerifierTemplateType} templateType - The template type.
|
|
25
|
+
* @returns {string} The Solidity verifier template.
|
|
38
26
|
*/
|
|
39
|
-
public
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
fs.mkdirSync(tempDir, { recursive: true });
|
|
46
|
-
|
|
47
|
-
const overriddenOptions: CompileOptions = { ...defaultCompileOptions, ...options };
|
|
48
|
-
|
|
49
|
-
await this._compile(overriddenOptions, tempDir);
|
|
50
|
-
|
|
51
|
-
await this._generateZKey(overriddenOptions, tempDir);
|
|
52
|
-
await this._generateVKey(tempDir);
|
|
53
|
-
|
|
54
|
-
this._moveFromTempDirToOutDir(tempDir, artifactDir);
|
|
55
|
-
} finally {
|
|
56
|
-
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}.`);
|
|
57
33
|
}
|
|
58
34
|
}
|
|
59
35
|
|
|
60
36
|
/**
|
|
61
|
-
* Creates a verifier contract.
|
|
37
|
+
* Creates a Solidity verifier contract.
|
|
62
38
|
*/
|
|
63
39
|
public async createVerifier(): Promise<void> {
|
|
64
|
-
const
|
|
40
|
+
const vKeyFilePath: string = this.mustGetArtifactsFilePath("vkey");
|
|
41
|
+
const verifierFilePath = path.join(this._config.verifierDirPath, `${this.getVerifierName()}.sol`);
|
|
65
42
|
|
|
66
|
-
|
|
67
|
-
const verifierDir = this._getDir("verifier");
|
|
43
|
+
const verifierTemplate: string = CircuitZKit.getTemplate(this.getTemplateType());
|
|
68
44
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const verifierFile = this._getFile("sol", tempDir);
|
|
73
|
-
|
|
74
|
-
const groth16Template = this._manager.getTemplate("groth16");
|
|
75
|
-
|
|
76
|
-
const templateParams = JSON.parse(fs.readFileSync(vKeyFile, "utf-8"));
|
|
77
|
-
templateParams["verifier_id"] = this.getVerifierId();
|
|
45
|
+
if (!fs.existsSync(this._config.verifierDirPath)) {
|
|
46
|
+
fs.mkdirSync(this._config.verifierDirPath, { recursive: true });
|
|
47
|
+
}
|
|
78
48
|
|
|
79
|
-
|
|
49
|
+
const templateParams = JSON.parse(fs.readFileSync(vKeyFilePath, "utf-8"));
|
|
50
|
+
templateParams["verifier_id"] = this.getVerifierName();
|
|
80
51
|
|
|
81
|
-
|
|
52
|
+
const verifierCode = ejs.render(verifierTemplate, templateParams);
|
|
82
53
|
|
|
83
|
-
|
|
84
|
-
} finally {
|
|
85
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
86
|
-
}
|
|
54
|
+
fs.writeFileSync(verifierFilePath, verifierCode, "utf-8");
|
|
87
55
|
}
|
|
88
56
|
|
|
89
57
|
/**
|
|
@@ -96,8 +64,8 @@ export class CircuitZKit {
|
|
|
96
64
|
* @todo Add support for other proving systems.
|
|
97
65
|
*/
|
|
98
66
|
public async generateProof(inputs: Inputs): Promise<ProofStruct> {
|
|
99
|
-
const zKeyFile = this.
|
|
100
|
-
const wasmFile = this.
|
|
67
|
+
const zKeyFile = this.mustGetArtifactsFilePath("zkey");
|
|
68
|
+
const wasmFile = this.mustGetArtifactsFilePath("wasm");
|
|
101
69
|
|
|
102
70
|
return (await snarkjs.groth16.fullProve(inputs, wasmFile, zKeyFile)) as ProofStruct;
|
|
103
71
|
}
|
|
@@ -112,7 +80,7 @@ export class CircuitZKit {
|
|
|
112
80
|
* @returns {Promise<boolean>} Whether the proof is valid.
|
|
113
81
|
*/
|
|
114
82
|
public async verifyProof(proof: ProofStruct): Promise<boolean> {
|
|
115
|
-
const vKeyFile = this.
|
|
83
|
+
const vKeyFile = this.mustGetArtifactsFilePath("vkey");
|
|
116
84
|
|
|
117
85
|
const verifier = JSON.parse(fs.readFileSync(vKeyFile).toString());
|
|
118
86
|
|
|
@@ -133,264 +101,84 @@ export class CircuitZKit {
|
|
|
133
101
|
}
|
|
134
102
|
|
|
135
103
|
/**
|
|
136
|
-
* Returns the circuit
|
|
137
|
-
*
|
|
138
|
-
* @returns {string} The circuit ID.
|
|
139
|
-
*/
|
|
140
|
-
public getCircuitId(): string {
|
|
141
|
-
return path.parse(this._circuit).name;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Returns the verifier ID. The verifier ID is the name of the circuit file without the extension, suffixed with "Verifier".
|
|
146
|
-
*
|
|
147
|
-
* @returns {string} The verifier ID.
|
|
148
|
-
*/
|
|
149
|
-
public getVerifierId(): string {
|
|
150
|
-
return `${path.parse(this._circuit).name}Verifier`;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Generates zero-knowledge key for the circuit.
|
|
104
|
+
* Returns the circuit name. The circuit name is the name of the circuit file without the extension.
|
|
155
105
|
*
|
|
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
|
|
106
|
+
* @returns {string} The circuit name.
|
|
159
107
|
*/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const zKeyFile = this._getFile("zkey", outDir);
|
|
163
|
-
|
|
164
|
-
const constraints = await this._getConstraints(outDir);
|
|
165
|
-
const ptauFile = await this._manager.fetchPtauFile(constraints);
|
|
166
|
-
|
|
167
|
-
if (options.setup == "groth16") {
|
|
168
|
-
await snarkjs.zKey.newZKey(r1csFile, ptauFile, zKeyFile);
|
|
169
|
-
|
|
170
|
-
const zKeyFileNext = `${zKeyFile}.next.zkey`;
|
|
171
|
-
|
|
172
|
-
for (let i = 0; i < options.contributions; ++i) {
|
|
173
|
-
await snarkjs.zKey.contribute(
|
|
174
|
-
zKeyFile,
|
|
175
|
-
zKeyFileNext,
|
|
176
|
-
`${zKeyFile}_contribution_${i}`,
|
|
177
|
-
randomBytes(32).toString("hex"),
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
fs.rmSync(zKeyFile);
|
|
181
|
-
fs.renameSync(zKeyFileNext, zKeyFile);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
108
|
+
public getCircuitName(): string {
|
|
109
|
+
return this._config.circuitName;
|
|
184
110
|
}
|
|
185
111
|
|
|
186
112
|
/**
|
|
187
|
-
*
|
|
113
|
+
* Returns the verifier name. The verifier name is the name of the circuit file without the extension, suffixed with "Verifier".
|
|
188
114
|
*
|
|
189
|
-
* @
|
|
115
|
+
* @returns {string} The verifier name.
|
|
190
116
|
*/
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const vKeyFile = this._getFile("vkey", outDir);
|
|
194
|
-
|
|
195
|
-
const vKeyData = await snarkjs.zKey.exportVerificationKey(zKeyFile);
|
|
196
|
-
|
|
197
|
-
fs.writeFileSync(vKeyFile, JSON.stringify(vKeyData));
|
|
117
|
+
public getVerifierName(): string {
|
|
118
|
+
return `${this._config.circuitName}Verifier`;
|
|
198
119
|
}
|
|
199
120
|
|
|
200
121
|
/**
|
|
201
|
-
* Returns the
|
|
122
|
+
* Returns the type of verifier template that was stored in the config
|
|
202
123
|
*
|
|
203
|
-
* @
|
|
204
|
-
* @param {string} outDir - The directory to save the compiled artifacts.
|
|
205
|
-
* @returns {string[]} The arguments to compile the circuit.
|
|
124
|
+
* @returns {VerifierTemplateType} The verifier template type.
|
|
206
125
|
*/
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
options.sym && args.push("--sym");
|
|
211
|
-
options.json && args.push("--json");
|
|
212
|
-
options.c && args.push("--c");
|
|
213
|
-
|
|
214
|
-
args.push("-o", outDir);
|
|
215
|
-
|
|
216
|
-
return args;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Compiles the circuit.
|
|
221
|
-
*
|
|
222
|
-
* @param {CompileOptions} options - Compilation options.
|
|
223
|
-
* @param {string} outDir - The directory to save the compiled artifacts.
|
|
224
|
-
*/
|
|
225
|
-
private async _compile(options: CompileOptions, outDir: string): Promise<void> {
|
|
226
|
-
const args = this._getCompileArgs(options, outDir);
|
|
227
|
-
|
|
228
|
-
try {
|
|
229
|
-
await this._getCircomRunner(args, options.quiet).execute(this._manager.getCompiler());
|
|
230
|
-
} catch (err) {
|
|
231
|
-
if (options.quiet) {
|
|
232
|
-
throw new Error(
|
|
233
|
-
'Compilation failed with an unknown error. Consider passing "quiet=false" flag to see the compilation error.',
|
|
234
|
-
{ cause: err },
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
throw new Error("Compilation failed.", { cause: err });
|
|
239
|
-
}
|
|
126
|
+
public getTemplateType(): VerifierTemplateType {
|
|
127
|
+
return this._config.templateType ?? "groth16";
|
|
240
128
|
}
|
|
241
129
|
|
|
242
130
|
/**
|
|
243
|
-
* 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.
|
|
244
132
|
*
|
|
245
|
-
* @param {
|
|
246
|
-
* @returns {
|
|
133
|
+
* @param {ArtifactsFileType} fileType - The type of the file.
|
|
134
|
+
* @returns {string} The path to the file.
|
|
247
135
|
*/
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
const r1csDescriptor = fs.openSync(r1csFile, "r");
|
|
252
|
-
|
|
253
|
-
const readBytes = (position: number, length: number): bigint => {
|
|
254
|
-
const buffer = Buffer.alloc(length);
|
|
255
|
-
|
|
256
|
-
fs.readSync(r1csDescriptor, buffer, { length, position });
|
|
257
|
-
|
|
258
|
-
return BigInt(`0x${buffer.reverse().toString("hex")}`);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
/// @dev https://github.com/iden3/r1csfile/blob/d82959da1f88fbd06db0407051fde94afbf8824a/doc/r1cs_bin_format.md#format-of-the-file
|
|
262
|
-
const numberOfSections = readBytes(8, 4);
|
|
263
|
-
let sectionStart = 12;
|
|
136
|
+
public mustGetArtifactsFilePath(fileType: ArtifactsFileType): string {
|
|
137
|
+
const file = this.getArtifactsFilePath(fileType);
|
|
264
138
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const sectionSize = Number(readBytes(sectionStart + 4, 8));
|
|
268
|
-
|
|
269
|
-
/// @dev Reading header section
|
|
270
|
-
if (sectionType == 1) {
|
|
271
|
-
const totalConstraintsOffset = 4 + 8 + 4 + 32 + 4 + 4 + 4 + 4 + 8;
|
|
272
|
-
|
|
273
|
-
return Number(readBytes(sectionStart + totalConstraintsOffset, 4));
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
sectionStart += 4 + 8 + sectionSize;
|
|
139
|
+
if (!fs.existsSync(file)) {
|
|
140
|
+
throw new Error(`Expected the file "${file}" to exist`);
|
|
277
141
|
}
|
|
278
142
|
|
|
279
|
-
|
|
143
|
+
return file;
|
|
280
144
|
}
|
|
281
145
|
|
|
282
146
|
/**
|
|
283
|
-
* Returns the path to the file of the given type.
|
|
147
|
+
* Returns the path to the file of the given type inside artifacts directory.
|
|
284
148
|
*
|
|
285
|
-
* @param {
|
|
286
|
-
* @param {string | undefined} temp - The temporary directory to use.
|
|
149
|
+
* @param {ArtifactsFileType} fileType - The type of the file.
|
|
287
150
|
* @returns {string} The path to the file.
|
|
288
151
|
*/
|
|
289
|
-
|
|
290
|
-
const
|
|
152
|
+
public getArtifactsFilePath(fileType: ArtifactsFileType): string {
|
|
153
|
+
const circuitName = this.getCircuitName();
|
|
154
|
+
|
|
155
|
+
let fileName: string;
|
|
156
|
+
let fileDir: string = this._config.circuitArtifactsPath;
|
|
291
157
|
|
|
292
158
|
switch (fileType) {
|
|
293
159
|
case "r1cs":
|
|
294
|
-
|
|
160
|
+
fileName = `${circuitName}.r1cs`;
|
|
161
|
+
break;
|
|
295
162
|
case "zkey":
|
|
296
|
-
|
|
163
|
+
fileName = `${circuitName}.zkey`;
|
|
164
|
+
break;
|
|
297
165
|
case "vkey":
|
|
298
|
-
|
|
166
|
+
fileName = `${circuitName}.vkey.json`;
|
|
167
|
+
break;
|
|
299
168
|
case "sym":
|
|
300
|
-
|
|
169
|
+
fileName = `${circuitName}.sym`;
|
|
170
|
+
break;
|
|
301
171
|
case "json":
|
|
302
|
-
|
|
172
|
+
fileName = `${circuitName}_constraints.json`;
|
|
173
|
+
break;
|
|
303
174
|
case "wasm":
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
175
|
+
fileName = `${circuitName}.wasm`;
|
|
176
|
+
fileDir = path.join(fileDir, `${circuitName}_js`);
|
|
177
|
+
break;
|
|
307
178
|
default:
|
|
308
179
|
throw new Error(`Ambiguous file type: ${fileType}.`);
|
|
309
180
|
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Returns the path to the directory of the given type.
|
|
314
|
-
*
|
|
315
|
-
* @param {DirType} dirType - The type of the directory.
|
|
316
|
-
* @returns {string} The path to the directory.
|
|
317
|
-
*/
|
|
318
|
-
private _getDir(dirType: DirType): string {
|
|
319
|
-
const circuitRelativePath = path.relative(this._manager.getCircuitsDir(), this._circuit);
|
|
320
|
-
|
|
321
|
-
switch (dirType) {
|
|
322
|
-
case "circuit":
|
|
323
|
-
return path.join(this._manager.getCircuitsDir(), circuitRelativePath, "..");
|
|
324
|
-
case "artifact":
|
|
325
|
-
return path.join(this._manager.getArtifactsDir(), circuitRelativePath);
|
|
326
|
-
case "verifier":
|
|
327
|
-
return path.join(this._manager.getVerifiersDir(), circuitRelativePath, "..");
|
|
328
|
-
default:
|
|
329
|
-
throw new Error(`Ambiguous dir type: ${dirType}.`);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Returns the path to the file of the given type. Throws an error if the file doesn't exist.
|
|
335
|
-
*
|
|
336
|
-
* @param {FileType} fileType - The type of the file.
|
|
337
|
-
* @param {string | undefined} temp - The temporary directory to use.
|
|
338
|
-
* @returns {string} The path to the file.
|
|
339
|
-
*/
|
|
340
|
-
private _mustGetFile(fileType: FileType, temp?: string): string {
|
|
341
|
-
const file = this._getFile(fileType, temp);
|
|
342
|
-
|
|
343
|
-
if (!fs.existsSync(file)) {
|
|
344
|
-
throw new Error(`Expected the file "${file}" to exist`);
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
return file;
|
|
348
|
-
}
|
|
349
181
|
|
|
350
|
-
|
|
351
|
-
* Moves the files from the temporary directory to the output directory.
|
|
352
|
-
*
|
|
353
|
-
* @param {string} tempDir - The temporary directory.
|
|
354
|
-
* @param {string} outDir - The output directory.
|
|
355
|
-
*/
|
|
356
|
-
private _moveFromTempDirToOutDir(tempDir: string, outDir: string): void {
|
|
357
|
-
fs.mkdirSync(outDir, { recursive: true });
|
|
358
|
-
|
|
359
|
-
readDirRecursively(tempDir, (dir: string, file: string) => {
|
|
360
|
-
const correspondingOutDir = path.join(outDir, path.relative(tempDir, dir));
|
|
361
|
-
const correspondingOutFile = path.join(outDir, path.relative(tempDir, file));
|
|
362
|
-
|
|
363
|
-
if (!fs.existsSync(correspondingOutDir)) {
|
|
364
|
-
fs.mkdirSync(correspondingOutDir);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
if (fs.existsSync(correspondingOutFile)) {
|
|
368
|
-
fs.rmSync(correspondingOutFile);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
fs.copyFileSync(file, correspondingOutFile);
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Returns a new instance of `CircomRunner`. The `CircomRunner` is used to compile the circuit.
|
|
377
|
-
*
|
|
378
|
-
* @param {string[]} args - The arguments to run the `circom` compiler.
|
|
379
|
-
* @param {boolean} quiet - Whether to suppress the compilation error.
|
|
380
|
-
* @returns {typeof CircomRunner} The `CircomRunner` instance.
|
|
381
|
-
*/
|
|
382
|
-
private _getCircomRunner(args: string[], quiet: boolean): typeof CircomRunner {
|
|
383
|
-
return new CircomRunner({
|
|
384
|
-
args,
|
|
385
|
-
preopens: { "/": "/" },
|
|
386
|
-
bindings: {
|
|
387
|
-
...bindings,
|
|
388
|
-
exit(code: number) {
|
|
389
|
-
throw new Error(`Compilation error. Exit code: ${code}.`);
|
|
390
|
-
},
|
|
391
|
-
fs,
|
|
392
|
-
},
|
|
393
|
-
quiet,
|
|
394
|
-
});
|
|
182
|
+
return path.join(fileDir, fileName);
|
|
395
183
|
}
|
|
396
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";
|