@solarity/zkit 0.3.6 → 0.3.7-rc.1
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/dist/core/CircuitZKit.d.ts +24 -4
- package/dist/core/CircuitZKit.d.ts.map +1 -1
- package/dist/core/CircuitZKit.js +51 -15
- package/dist/core/CircuitZKit.js.map +1 -1
- package/dist/core/protocols/AbstractImplementer.d.ts +1 -2
- package/dist/core/protocols/AbstractImplementer.d.ts.map +1 -1
- package/dist/core/protocols/AbstractImplementer.js.map +1 -1
- package/dist/core/protocols/Groth16Implementer.d.ts +1 -2
- package/dist/core/protocols/Groth16Implementer.d.ts.map +1 -1
- package/dist/core/protocols/Groth16Implementer.js +3 -3
- package/dist/core/protocols/Groth16Implementer.js.map +1 -1
- package/dist/core/protocols/PlonkImplementer.d.ts +1 -2
- package/dist/core/protocols/PlonkImplementer.d.ts.map +1 -1
- package/dist/core/protocols/PlonkImplementer.js +3 -3
- package/dist/core/protocols/PlonkImplementer.js.map +1 -1
- package/dist/types/protocols/index.d.ts +1 -2
- package/dist/types/protocols/index.d.ts.map +1 -1
- package/dist/types/protocols/index.js.map +1 -1
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/{utils.js → utils/index.js} +30 -2
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/witness-utils.d.ts +38 -0
- package/dist/utils/witness-utils.d.ts.map +1 -0
- package/dist/utils/witness-utils.js +139 -0
- package/dist/utils/witness-utils.js.map +1 -0
- package/package.json +4 -1
- package/src/core/CircuitZKit.ts +60 -16
- package/src/core/protocols/AbstractImplementer.ts +1 -6
- package/src/core/protocols/Groth16Implementer.ts +3 -4
- package/src/core/protocols/PlonkImplementer.ts +3 -4
- package/src/types/protocols/index.ts +1 -2
- package/src/utils/index.ts +33 -0
- package/src/utils/witness-utils.ts +133 -0
- package/dist/utils.d.ts +0 -2
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/utils.ts +0 -6
|
@@ -24,20 +24,32 @@ export declare class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
24
24
|
/**
|
|
25
25
|
* Calculates a witness for the given inputs.
|
|
26
26
|
*
|
|
27
|
+
* If `witnessOverrides` are provided, the corresponding witness values will be substituted in the result.
|
|
28
|
+
*
|
|
29
|
+
* Signal names in `witnessOverrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
30
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
31
|
+
*
|
|
27
32
|
* @param {Signals} inputs - The inputs for the circuit.
|
|
33
|
+
* @param {Record<string, bigint>} [witnessOverrides] - Optional map of signal names to override their witness values.
|
|
28
34
|
* @returns {Promise<bigint[]>} The generated witness.
|
|
29
35
|
*/
|
|
30
|
-
calculateWitness(inputs: Signals): Promise<bigint[]>;
|
|
36
|
+
calculateWitness(inputs: Signals, witnessOverrides?: Record<string, bigint>): Promise<bigint[]>;
|
|
31
37
|
/**
|
|
32
38
|
* Generates a proof for the given inputs.
|
|
33
39
|
*
|
|
34
40
|
* @dev The `inputs` should be in the same order as the circuit expects them.
|
|
35
41
|
*
|
|
42
|
+
* If `witnessOverrides` are provided, the witness will be calculated from the inputs and overridden accordingly.
|
|
43
|
+
* Otherwise, a standard witness will be calculated and used.
|
|
44
|
+
*
|
|
45
|
+
* Signal names in `witnessOverrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
46
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
47
|
+
*
|
|
36
48
|
* @param {Signals} inputs - The inputs for the circuit.
|
|
49
|
+
* @param {Record<string, bigint>} [witnessOverrides] - Optional map of signal names to override their witness values.
|
|
37
50
|
* @returns {Promise<ProofStructByProtocol<Type>>} The generated proof.
|
|
38
|
-
* @todo Add support for other proving systems.
|
|
39
51
|
*/
|
|
40
|
-
generateProof(inputs: Signals): Promise<ProofStructByProtocol<Type>>;
|
|
52
|
+
generateProof(inputs: Signals, witnessOverrides?: Record<string, bigint>): Promise<ProofStructByProtocol<Type>>;
|
|
41
53
|
/**
|
|
42
54
|
* Verifies the given proof.
|
|
43
55
|
*
|
|
@@ -53,7 +65,6 @@ export declare class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
53
65
|
*
|
|
54
66
|
* @param {ProofStructByProtocol<Type>} proof - The proof to generate calldata for.
|
|
55
67
|
* @returns {Promise<CalldataByProtocol<Type>>} - The generated calldata.
|
|
56
|
-
* @todo Add other types of calldata.
|
|
57
68
|
*/
|
|
58
69
|
generateCalldata(proof: ProofStructByProtocol<Type>): Promise<CalldataByProtocol<Type>>;
|
|
59
70
|
/**
|
|
@@ -83,6 +94,15 @@ export declare class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
83
94
|
* @returns {string} The Solidity verifier template.
|
|
84
95
|
*/
|
|
85
96
|
getVerifierTemplate(languageExtension: VerifierLanguageType): string;
|
|
97
|
+
/**
|
|
98
|
+
* Returns the path to the temporary witness file.
|
|
99
|
+
*
|
|
100
|
+
* The file is stored in the system temporary directory and is named after the circuit.
|
|
101
|
+
* This file is used for intermediate witness generation and may be deleted after usage.
|
|
102
|
+
*
|
|
103
|
+
* @returns {string} The full path to the temporary `.wtns` file.
|
|
104
|
+
*/
|
|
105
|
+
getTemporaryWitnessPath(): string;
|
|
86
106
|
/**
|
|
87
107
|
* Returns the path to the file of the given type inside artifacts directory. Throws an error if the file doesn't exist.
|
|
88
108
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CircuitZKit.d.ts","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"CircuitZKit.d.ts","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACnG,OAAO,EAAE,OAAO,EAAE,MAAM,s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BAAoB,GAAG,MAAM;IAI3E;;;;;;;OAOG;IACI,uBAAuB,IAAI,MAAM;IAIxC;;;;;OAKG;IACI,wBAAwB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM;IAUpE;;;;;OAKG;IACI,oBAAoB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM;CAgCjE"}
|
package/dist/core/CircuitZKit.js
CHANGED
|
@@ -39,10 +39,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.CircuitZKit = void 0;
|
|
40
40
|
const fs_1 = __importDefault(require("fs"));
|
|
41
41
|
const path_1 = __importDefault(require("path"));
|
|
42
|
-
const os = __importStar(require("os"));
|
|
43
42
|
const snarkjs = __importStar(require("snarkjs"));
|
|
44
43
|
const crypto_1 = require("crypto");
|
|
45
44
|
const constants_1 = require("../constants");
|
|
45
|
+
const utils_1 = require("../utils");
|
|
46
46
|
/**
|
|
47
47
|
* `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
|
|
48
48
|
*/
|
|
@@ -78,38 +78,64 @@ class CircuitZKit {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
const verifierFilePath = path_1.default.join(this._config.verifierDirPath, verifierFileName);
|
|
81
|
-
this._implementer.createVerifier(vKeyFilePath, verifierFilePath, languageExtension);
|
|
81
|
+
await this._implementer.createVerifier(vKeyFilePath, verifierFilePath, languageExtension);
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
84
|
* Calculates a witness for the given inputs.
|
|
85
85
|
*
|
|
86
|
+
* If `witnessOverrides` are provided, the corresponding witness values will be substituted in the result.
|
|
87
|
+
*
|
|
88
|
+
* Signal names in `witnessOverrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
89
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
90
|
+
*
|
|
86
91
|
* @param {Signals} inputs - The inputs for the circuit.
|
|
92
|
+
* @param {Record<string, bigint>} [witnessOverrides] - Optional map of signal names to override their witness values.
|
|
87
93
|
* @returns {Promise<bigint[]>} The generated witness.
|
|
88
94
|
*/
|
|
89
|
-
async calculateWitness(inputs) {
|
|
90
|
-
const
|
|
91
|
-
if (!fs_1.default.existsSync(tmpDir)) {
|
|
92
|
-
fs_1.default.mkdirSync(tmpDir, { recursive: true });
|
|
93
|
-
}
|
|
94
|
-
const wtnsFile = path_1.default.join(tmpDir, `${this.getCircuitName()}.wtns`);
|
|
95
|
+
async calculateWitness(inputs, witnessOverrides) {
|
|
96
|
+
const wtnsFile = this.getTemporaryWitnessPath();
|
|
95
97
|
const wasmFile = this.mustGetArtifactsFilePath("wasm");
|
|
98
|
+
let signalIndexes = {};
|
|
99
|
+
if (witnessOverrides) {
|
|
100
|
+
const symFile = this.mustGetArtifactsFilePath("sym");
|
|
101
|
+
signalIndexes = await (0, utils_1.checkWitnessOverrides)(symFile, witnessOverrides);
|
|
102
|
+
}
|
|
96
103
|
await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);
|
|
97
|
-
const wtnsJson = await snarkjs.wtns.exportJson(wtnsFile);
|
|
98
|
-
return wtnsJson;
|
|
104
|
+
const wtnsJson = (await snarkjs.wtns.exportJson(wtnsFile));
|
|
105
|
+
return witnessOverrides ? (0, utils_1.modifyWitnessArray)(wtnsJson, signalIndexes, witnessOverrides) : wtnsJson;
|
|
99
106
|
}
|
|
100
107
|
/**
|
|
101
108
|
* Generates a proof for the given inputs.
|
|
102
109
|
*
|
|
103
110
|
* @dev The `inputs` should be in the same order as the circuit expects them.
|
|
104
111
|
*
|
|
112
|
+
* If `witnessOverrides` are provided, the witness will be calculated from the inputs and overridden accordingly.
|
|
113
|
+
* Otherwise, a standard witness will be calculated and used.
|
|
114
|
+
*
|
|
115
|
+
* Signal names in `witnessOverrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
116
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
117
|
+
*
|
|
105
118
|
* @param {Signals} inputs - The inputs for the circuit.
|
|
119
|
+
* @param {Record<string, bigint>} [witnessOverrides] - Optional map of signal names to override their witness values.
|
|
106
120
|
* @returns {Promise<ProofStructByProtocol<Type>>} The generated proof.
|
|
107
|
-
* @todo Add support for other proving systems.
|
|
108
121
|
*/
|
|
109
|
-
async generateProof(inputs) {
|
|
122
|
+
async generateProof(inputs, witnessOverrides) {
|
|
110
123
|
const zKeyFile = this.mustGetArtifactsFilePath("zkey");
|
|
111
|
-
const
|
|
112
|
-
|
|
124
|
+
const witnessFile = this.getTemporaryWitnessPath();
|
|
125
|
+
let proof;
|
|
126
|
+
try {
|
|
127
|
+
const witness = await this.calculateWitness(inputs, witnessOverrides);
|
|
128
|
+
if (witnessOverrides) {
|
|
129
|
+
await (0, utils_1.writeWitnessFile)(witnessFile, witness);
|
|
130
|
+
}
|
|
131
|
+
proof = await this._implementer.generateProof(zKeyFile, witnessFile);
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
if (fs_1.default.existsSync(witnessFile)) {
|
|
135
|
+
fs_1.default.rmSync(witnessFile);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return proof;
|
|
113
139
|
}
|
|
114
140
|
/**
|
|
115
141
|
* Verifies the given proof.
|
|
@@ -129,7 +155,6 @@ class CircuitZKit {
|
|
|
129
155
|
*
|
|
130
156
|
* @param {ProofStructByProtocol<Type>} proof - The proof to generate calldata for.
|
|
131
157
|
* @returns {Promise<CalldataByProtocol<Type>>} - The generated calldata.
|
|
132
|
-
* @todo Add other types of calldata.
|
|
133
158
|
*/
|
|
134
159
|
async generateCalldata(proof) {
|
|
135
160
|
return await this._implementer.generateCalldata(proof);
|
|
@@ -169,6 +194,17 @@ class CircuitZKit {
|
|
|
169
194
|
getVerifierTemplate(languageExtension) {
|
|
170
195
|
return this._implementer.getTemplate(languageExtension);
|
|
171
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* Returns the path to the temporary witness file.
|
|
199
|
+
*
|
|
200
|
+
* The file is stored in the system temporary directory and is named after the circuit.
|
|
201
|
+
* This file is used for intermediate witness generation and may be deleted after usage.
|
|
202
|
+
*
|
|
203
|
+
* @returns {string} The full path to the temporary `.wtns` file.
|
|
204
|
+
*/
|
|
205
|
+
getTemporaryWitnessPath() {
|
|
206
|
+
return path_1.default.join((0, utils_1.getTmpDir)(), `${this.getCircuitName()}.wtns`);
|
|
207
|
+
}
|
|
172
208
|
/**
|
|
173
209
|
* Returns the path to the file of the given type inside artifacts directory. Throws an error if the file doesn't exist.
|
|
174
210
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CircuitZKit.js","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,
|
|
1
|
+
{"version":3,"file":"CircuitZKit.js","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,iDAAmC;AACnC,mCAAoC;AAMpC,4CAAoD;AACpD,oCAAkG;AAElG;;GAEG;AACH,MAAa,WAAW;IAEH;IACA;IAFnB,YACmB,OAA0B,EAC1B,YAAwC;QADxC,YAAO,GAAP,OAAO,CAAmB;QAC1B,iBAAY,GAAZ,YAAY,CAA4B;IACxD,CAAC;IAEJ;;;;;;;;;;;OAWG;IACI,KAAK,CAAC,cAAc,CAAC,iBAAuC,EAAE,kBAA2B;QAC9F,MAAM,YAAY,GAAW,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC;QAEnE,IAAI,gBAAgB,GAAW,GAAG,IAAI,CAAC,eAAe,CAAC,kBAAkB,CAAC,IAAI,iBAAiB,EAAE,CAAC;QAElG,IAAI,gBAAgB,CAAC,MAAM,IAAI,gCAAoB,EAAE,CAAC;YACpD,MAAM,cAAc,GAAW,kBAAkB;gBAC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C,MAAM;YACR,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,CAAC;gBAC/D,MAAM;YACR,KAAK,MAAM;gBACT,QAAQ,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,CAAC;gBAC/D,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;AAlPD,kCAkPC"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Signals } from "../../types/proof-utils";
|
|
2
1
|
import { IProtocolImplementer, ProvingSystemType, ProofStructByProtocol, CalldataByProtocol } from "../../types/protocols";
|
|
3
2
|
import { VerifierLanguageType } from "../../types/circuit-zkit";
|
|
4
3
|
export declare abstract class AbstractProtocolImplementer<T extends ProvingSystemType> implements IProtocolImplementer<T> {
|
|
5
4
|
createVerifier(vKeyFilePath: string, verifierFilePath: string, languageExtension: VerifierLanguageType): Promise<void>;
|
|
6
|
-
abstract generateProof(
|
|
5
|
+
abstract generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<ProofStructByProtocol<T>>;
|
|
7
6
|
abstract verifyProof(proof: ProofStructByProtocol<T>, vKeyFilePath: string): Promise<boolean>;
|
|
8
7
|
abstract generateCalldata(proof: ProofStructByProtocol<T>): Promise<CalldataByProtocol<T>>;
|
|
9
8
|
abstract getProvingSystemType(): ProvingSystemType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractImplementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/AbstractImplementer.ts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"AbstractImplementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/AbstractImplementer.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EACjB,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAEhE,8BAAsB,2BAA2B,CAAC,CAAC,SAAS,iBAAiB,CAAE,YAAW,oBAAoB,CAAC,CAAC,CAAC;IAClG,cAAc,CACzB,YAAY,EAAE,MAAM,EACpB,gBAAgB,EAAE,MAAM,EACxB,iBAAiB,EAAE,oBAAoB,GACtC,OAAO,CAAC,IAAI,CAAC;aAeA,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;aAE/F,WAAW,CAAC,KAAK,EAAE,qBAAqB,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;aAEpF,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC;aAEjF,oBAAoB,IAAI,iBAAiB;IAElD,WAAW,CAAC,iBAAiB,EAAE,oBAAoB,GAAG,MAAM;IAO5D,eAAe,CAAC,WAAW,EAAE,MAAM,EAAE,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM;IAOzE,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IAI5C,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;CAGpD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractImplementer.js","sourceRoot":"","sources":["../../../src/core/protocols/AbstractImplementer.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,8CAAsB;AACtB,gDAAwB;
|
|
1
|
+
{"version":3,"file":"AbstractImplementer.js","sourceRoot":"","sources":["../../../src/core/protocols/AbstractImplementer.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,8CAAsB;AACtB,gDAAwB;AAUxB,MAAsB,2BAA2B;IACxC,KAAK,CAAC,cAAc,CACzB,YAAoB,EACpB,gBAAwB,EACxB,iBAAuC;QAEvC,MAAM,gBAAgB,GAAW,IAAI,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;QAErE,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,cAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC;YACnD,YAAE,CAAC,SAAS,CAAC,cAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACpE,CAAC;QAED,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC;QAC1E,cAAc,CAAC,aAAa,CAAC,GAAG,cAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC;QAElE,MAAM,YAAY,GAAG,aAAG,CAAC,MAAM,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAC;QAElE,YAAE,CAAC,aAAa,CAAC,gBAAgB,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC;IAC5D,CAAC;IAUM,WAAW,CAAC,iBAAuC;QACxD,OAAO,YAAE,CAAC,YAAY,CACpB,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,WAAW,EAAE,YAAY,IAAI,CAAC,oBAAoB,EAAE,IAAI,iBAAiB,MAAM,CAAC,EAC3G,MAAM,CACP,CAAC;IACJ,CAAC;IAEM,eAAe,CAAC,WAAmB,EAAE,kBAA2B;QACrE,MAAM,YAAY,GAAsB,IAAI,CAAC,oBAAoB,EAAE,CAAC;QACpE,MAAM,UAAU,GAAW,kBAAkB,IAAI,EAAE,CAAC;QAEpD,OAAO,GAAG,WAAW,GAAG,UAAU,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC;IAC9G,CAAC;IAEM,eAAe,CAAC,WAAmB;QACxC,OAAO,GAAG,WAAW,IAAI,IAAI,CAAC,oBAAoB,EAAE,OAAO,CAAC;IAC9D,CAAC;IAEM,eAAe,CAAC,WAAmB;QACxC,OAAO,GAAG,WAAW,IAAI,IAAI,CAAC,oBAAoB,EAAE,YAAY,CAAC;IACnE,CAAC;CACF;AAjDD,kEAiDC"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { AbstractProtocolImplementer } from "./AbstractImplementer";
|
|
2
|
-
import { Signals } from "../../types/proof-utils";
|
|
3
2
|
import { Groth16ProofStruct, Groth16CalldataStruct, ProvingSystemType } from "../../types/protocols";
|
|
4
3
|
export declare class Groth16Implementer extends AbstractProtocolImplementer<"groth16"> {
|
|
5
|
-
generateProof(
|
|
4
|
+
generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<Groth16ProofStruct>;
|
|
6
5
|
verifyProof(proof: Groth16ProofStruct, vKeyFilePath: string): Promise<boolean>;
|
|
7
6
|
generateCalldata(proof: Groth16ProofStruct): Promise<Groth16CalldataStruct>;
|
|
8
7
|
getProvingSystemType(): ProvingSystemType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Groth16Implementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Groth16Implementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAIrG,qBAAa,kBAAmB,SAAQ,2BAA2B,CAAC,SAAS,CAAC;IAC/D,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAQzF,WAAW,CAAC,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU9E,gBAAgB,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAejF,oBAAoB,IAAI,iBAAiB;CAGjD"}
|
|
@@ -42,10 +42,10 @@ const snarkjs = __importStar(require("snarkjs"));
|
|
|
42
42
|
const AbstractImplementer_1 = require("./AbstractImplementer");
|
|
43
43
|
const utils_1 = require("../../utils");
|
|
44
44
|
class Groth16Implementer extends AbstractImplementer_1.AbstractProtocolImplementer {
|
|
45
|
-
async generateProof(
|
|
46
|
-
const
|
|
45
|
+
async generateProof(zKeyFilePath, witnessFilePath) {
|
|
46
|
+
const proof = await snarkjs.groth16.prove(zKeyFilePath, witnessFilePath);
|
|
47
47
|
await (0, utils_1.terminateCurve)();
|
|
48
|
-
return
|
|
48
|
+
return proof;
|
|
49
49
|
}
|
|
50
50
|
async verifyProof(proof, vKeyFilePath) {
|
|
51
51
|
const verifier = JSON.parse(fs_1.default.readFileSync(vKeyFilePath).toString());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Groth16Implementer.js","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;
|
|
1
|
+
{"version":3,"file":"Groth16Implementer.js","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;AAIpE,uCAA6C;AAE7C,MAAa,kBAAmB,SAAQ,iDAAsC;IACrE,KAAK,CAAC,aAAa,CAAC,YAAoB,EAAE,eAAuB;QACtE,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC;QAEzE,MAAM,IAAA,sBAAc,GAAE,CAAC;QAEvB,OAAO,KAA2B,CAAC;IACrC,CAAC;IAEM,KAAK,CAAC,WAAW,CAAC,KAAyB,EAAE,YAAoB;QACtE,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEtE,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QAEnG,MAAM,IAAA,sBAAc,GAAE,CAAC;QAEvB,OAAO,iBAAiB,CAAC;IAC3B,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,KAAyB;QACrD,MAAM,gBAAgB,GAAG,IAAI,CAAC,KAAK,CACjC,IAAI,MAAM,OAAO,CAAC,OAAO,CAAC,sBAAsB,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,aAAa,CAAC,GAAG,CACtF,CAAC;QAEF,OAAO;YACL,WAAW,EAAE;gBACX,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;gBACtB,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;gBACtB,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;aACvB;YACD,aAAa,EAAE,gBAAgB,CAAC,CAAC,CAAC;SACnC,CAAC;IACJ,CAAC;IAEM,oBAAoB;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AArCD,gDAqCC"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { AbstractProtocolImplementer } from "./AbstractImplementer";
|
|
2
|
-
import { Signals } from "../../types/proof-utils";
|
|
3
2
|
import { PlonkProofStruct, PlonkCalldataStruct, ProvingSystemType } from "../../types/protocols";
|
|
4
3
|
export declare class PlonkImplementer extends AbstractProtocolImplementer<"plonk"> {
|
|
5
|
-
generateProof(
|
|
4
|
+
generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<PlonkProofStruct>;
|
|
6
5
|
verifyProof(proof: PlonkProofStruct, vKeyFilePath: string): Promise<boolean>;
|
|
7
6
|
generateCalldata(proof: PlonkProofStruct): Promise<PlonkCalldataStruct>;
|
|
8
7
|
getProvingSystemType(): ProvingSystemType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlonkImplementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"PlonkImplementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAIjG,qBAAa,gBAAiB,SAAQ,2BAA2B,CAAC,OAAO,CAAC;IAC3D,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAQvF,WAAW,CAAC,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU5E,gBAAgB,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAgB7E,oBAAoB,IAAI,iBAAiB;CAGjD"}
|
|
@@ -42,10 +42,10 @@ const snarkjs = __importStar(require("snarkjs"));
|
|
|
42
42
|
const AbstractImplementer_1 = require("./AbstractImplementer");
|
|
43
43
|
const utils_1 = require("../../utils");
|
|
44
44
|
class PlonkImplementer extends AbstractImplementer_1.AbstractProtocolImplementer {
|
|
45
|
-
async generateProof(
|
|
46
|
-
const
|
|
45
|
+
async generateProof(zKeyFilePath, witnessFilePath) {
|
|
46
|
+
const proof = await snarkjs.plonk.prove(zKeyFilePath, witnessFilePath);
|
|
47
47
|
await (0, utils_1.terminateCurve)();
|
|
48
|
-
return
|
|
48
|
+
return proof;
|
|
49
49
|
}
|
|
50
50
|
async verifyProof(proof, vKeyFilePath) {
|
|
51
51
|
const verifier = JSON.parse(fs_1.default.readFileSync(vKeyFilePath).toString());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlonkImplementer.js","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;
|
|
1
|
+
{"version":3,"file":"PlonkImplementer.js","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;AAIpE,uCAA6C;AAE7C,MAAa,gBAAiB,SAAQ,iDA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OAAO,CAAC;IACjB,CAAC;CACF;AAtCD,4CAsCC"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { Groth16ProofStruct, Groth16CalldataStruct } from "./groth16";
|
|
2
2
|
import { PlonkProofStruct, PlonkCalldataStruct } from "./plonk";
|
|
3
|
-
import { Signals } from "../proof-utils";
|
|
4
3
|
import { VerifierLanguageType } from "../circuit-zkit";
|
|
5
4
|
export * from "./groth16";
|
|
6
5
|
export * from "./plonk";
|
|
7
6
|
export interface IProtocolImplementer<T extends ProvingSystemType> {
|
|
8
7
|
createVerifier(vKeyFilePath: string, verifierFilePath: string, languageExtension: VerifierLanguageType): Promise<void>;
|
|
9
|
-
generateProof(
|
|
8
|
+
generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<ProofStructByProtocol<T>>;
|
|
10
9
|
verifyProof(proof: ProofStructByProtocol<T>, vKeyFilePath: string): Promise<boolean>;
|
|
11
10
|
generateCalldata(proof: ProofStructByProtocol<T>): Promise<CalldataByProtocol<T>>;
|
|
12
11
|
getProvingSystemType(): ProvingSystemType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/protocols/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEhE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/protocols/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEhE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAEvD,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CA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M,kBAAkB,CAAC,CAAC,SAAS,iBAAiB,IAAI,sBAAsB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/protocols/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/protocols/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAKA,4CAA0B;AAC1B,0CAAwB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminates the BN128 curve instance used by SnarkJS.
|
|
3
|
+
*/
|
|
4
|
+
export declare function terminateCurve(): Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* Returns the path to the temporary directory used by ZKit.
|
|
7
|
+
*
|
|
8
|
+
* Creates the directory if it does not exist.
|
|
9
|
+
*
|
|
10
|
+
* @returns {string} The path to the temporary `.zkit` directory inside the OS temp folder.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getTmpDir(): string;
|
|
13
|
+
export * from "./witness-utils";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,wBAAsB,cAAc,kBAEnC;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAQlC;AAED,cAAc,iBAAiB,CAAC"}
|
|
@@ -32,11 +32,39 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
35
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
42
|
exports.terminateCurve = terminateCurve;
|
|
43
|
+
exports.getTmpDir = getTmpDir;
|
|
44
|
+
const os_1 = __importDefault(require("os"));
|
|
45
|
+
const fs_1 = __importDefault(require("fs"));
|
|
46
|
+
const path_1 = __importDefault(require("path"));
|
|
47
|
+
const constants_1 = require("../constants");
|
|
37
48
|
const snarkjs = __importStar(require("snarkjs"));
|
|
38
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Terminates the BN128 curve instance used by SnarkJS.
|
|
51
|
+
*/
|
|
39
52
|
async function terminateCurve() {
|
|
40
53
|
await (await snarkjs.curves.getCurveFromName(constants_1.BN128_CURVE_NAME)).terminate();
|
|
41
54
|
}
|
|
42
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Returns the path to the temporary directory used by ZKit.
|
|
57
|
+
*
|
|
58
|
+
* Creates the directory if it does not exist.
|
|
59
|
+
*
|
|
60
|
+
* @returns {string} The path to the temporary `.zkit` directory inside the OS temp folder.
|
|
61
|
+
*/
|
|
62
|
+
function getTmpDir() {
|
|
63
|
+
const tmpDir = path_1.default.join(os_1.default.tmpdir(), ".zkit");
|
|
64
|
+
if (!fs_1.default.existsSync(tmpDir)) {
|
|
65
|
+
fs_1.default.mkdirSync(tmpDir, { recursive: true });
|
|
66
|
+
}
|
|
67
|
+
return tmpDir;
|
|
68
|
+
}
|
|
69
|
+
__exportStar(require("./witness-utils"), exports);
|
|
70
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,wCAEC;AASD,8BAQC;AA9BD,4CAAoB;AACpB,4CAAoB;AACpB,gDAAwB;AAExB,4CAAgD;AAEhD,iDAAmC;AAEnC;;GAEG;AACI,KAAK,UAAU,cAAc;IAClC,MAAM,CAAC,MAAO,OAAe,CAAC,MAAM,CAAC,gBAAgB,CAAC,4BAAgB,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AACvF,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS;IACvB,MAAM,MAAM,GAAG,cAAI,CAAC,IAAI,CAAC,YAAE,CAAC,MAAM,EAAE,EAAE,OAAO,CAAC,CAAC;IAE/C,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3B,YAAE,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,kDAAgC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates the provided witness overrides against the `.sym` file and returns the signal-to-index map.
|
|
3
|
+
*
|
|
4
|
+
* Reads the `.sym` file line by line and builds a mapping of signal names to their witness indices.
|
|
5
|
+
* Ensures that all keys in `overrides` exist in the `.sym` file.
|
|
6
|
+
* Throws an error listing all missing signals if any override key is not found.
|
|
7
|
+
*
|
|
8
|
+
* Signal names in `overrides` must be in their full form as represented in the `.sym` file, e.g.,
|
|
9
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} symFile - Path to the `.sym` file.
|
|
12
|
+
* @param {Record<string, bigint>} overrides - Map of signal names to new witness values.
|
|
13
|
+
* @returns {Promise<Record<string, number>>} Map of signal names to their corresponding witness indices.
|
|
14
|
+
*/
|
|
15
|
+
export declare function checkWitnessOverrides(symFile: string, overrides: Record<string, bigint>): Promise<Record<string, number>>;
|
|
16
|
+
/**
|
|
17
|
+
* Modifies specific signal values in a witness array.
|
|
18
|
+
* Substitutes signal from `overrides` in the witness array at positions defined in `signalIndexes`.
|
|
19
|
+
*
|
|
20
|
+
* Signal names in `overrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
21
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
22
|
+
*
|
|
23
|
+
* @param {bigint[]} witness - The original witness array.
|
|
24
|
+
* @param {Record<string, number>} signalIndexes - Map of signal names to their witness indices.
|
|
25
|
+
* @param {Record<string, bigint>} overrides - Map of signal names to new witness values.
|
|
26
|
+
* @returns {Promise<bigint[]>} The modified witness array.
|
|
27
|
+
*/
|
|
28
|
+
export declare function modifyWitnessArray(witness: bigint[], signalIndexes: Record<string, number>, overrides: Record<string, bigint>): Promise<bigint[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Writes a witness array to a `.wtns` binary file.
|
|
31
|
+
*
|
|
32
|
+
* Reference: https://github.com/iden3/snarkjs/blob/bf28b1cb5aefcefab7e0f70f1fa5e40f764cca72/src/wtns_utils.js#L25C42-L25C47
|
|
33
|
+
*
|
|
34
|
+
* @param {string} witnessPath - Path to the existing `.wtns` file to read prime and overwrite with new witness.
|
|
35
|
+
* @param {bigint[]} witness - The witness array to write.
|
|
36
|
+
*/
|
|
37
|
+
export declare function writeWitnessFile(witnessPath: string, witness: bigint[]): Promise<void>;
|
|
38
|
+
//# sourceMappingURL=witness-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"witness-utils.d.ts","sourceRoot":"","sources":["../../src/utils/witness-utils.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;GAaG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAyBjC;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,MAAM,EAAE,EACjB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACrC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,OAAO,CAAC,MAAM,EAAE,CAAC,CAQnB;AAED;;;;;;;GAOG;AACH,wBAAsB,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,iBAwB5E"}
|
|
@@ -0,0 +1,139 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.checkWitnessOverrides = checkWitnessOverrides;
|
|
40
|
+
exports.modifyWitnessArray = modifyWitnessArray;
|
|
41
|
+
exports.writeWitnessFile = writeWitnessFile;
|
|
42
|
+
const fs_1 = __importDefault(require("fs"));
|
|
43
|
+
const readline = __importStar(require("readline"));
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
const ffjavascript_1 = require("ffjavascript");
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
const binFileUtils = __importStar(require("@iden3/binfileutils"));
|
|
48
|
+
/**
|
|
49
|
+
* Validates the provided witness overrides against the `.sym` file and returns the signal-to-index map.
|
|
50
|
+
*
|
|
51
|
+
* Reads the `.sym` file line by line and builds a mapping of signal names to their witness indices.
|
|
52
|
+
* Ensures that all keys in `overrides` exist in the `.sym` file.
|
|
53
|
+
* Throws an error listing all missing signals if any override key is not found.
|
|
54
|
+
*
|
|
55
|
+
* Signal names in `overrides` must be in their full form as represented in the `.sym` file, e.g.,
|
|
56
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} symFile - Path to the `.sym` file.
|
|
59
|
+
* @param {Record<string, bigint>} overrides - Map of signal names to new witness values.
|
|
60
|
+
* @returns {Promise<Record<string, number>>} Map of signal names to their corresponding witness indices.
|
|
61
|
+
*/
|
|
62
|
+
async function checkWitnessOverrides(symFile, overrides) {
|
|
63
|
+
const signalToWitnessIndex = {};
|
|
64
|
+
const missingSignals = new Set(Object.keys(overrides));
|
|
65
|
+
const fileStream = fs_1.default.createReadStream(symFile, { encoding: "utf8" });
|
|
66
|
+
const signals = readline.createInterface({ input: fileStream, crlfDelay: Infinity });
|
|
67
|
+
for await (const signal of signals) {
|
|
68
|
+
const signalInfo = signal.split(",");
|
|
69
|
+
if (signalInfo.length != 4 || Number(signalInfo[1]) < 0) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
signalToWitnessIndex[signalInfo[3]] = Number(signalInfo[1]);
|
|
73
|
+
missingSignals.delete(signalInfo[3]);
|
|
74
|
+
}
|
|
75
|
+
if (missingSignals.size > 0) {
|
|
76
|
+
throw new Error(`Signals not found in .sym file: ${Array.from(missingSignals).join(", ")}`);
|
|
77
|
+
}
|
|
78
|
+
return signalToWitnessIndex;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Modifies specific signal values in a witness array.
|
|
82
|
+
* Substitutes signal from `overrides` in the witness array at positions defined in `signalIndexes`.
|
|
83
|
+
*
|
|
84
|
+
* Signal names in `overrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
85
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
86
|
+
*
|
|
87
|
+
* @param {bigint[]} witness - The original witness array.
|
|
88
|
+
* @param {Record<string, number>} signalIndexes - Map of signal names to their witness indices.
|
|
89
|
+
* @param {Record<string, bigint>} overrides - Map of signal names to new witness values.
|
|
90
|
+
* @returns {Promise<bigint[]>} The modified witness array.
|
|
91
|
+
*/
|
|
92
|
+
async function modifyWitnessArray(witness, signalIndexes, overrides) {
|
|
93
|
+
for (const [signal, value] of Object.entries(overrides)) {
|
|
94
|
+
const index = signalIndexes[signal];
|
|
95
|
+
witness[index] = value;
|
|
96
|
+
}
|
|
97
|
+
return witness;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Writes a witness array to a `.wtns` binary file.
|
|
101
|
+
*
|
|
102
|
+
* Reference: https://github.com/iden3/snarkjs/blob/bf28b1cb5aefcefab7e0f70f1fa5e40f764cca72/src/wtns_utils.js#L25C42-L25C47
|
|
103
|
+
*
|
|
104
|
+
* @param {string} witnessPath - Path to the existing `.wtns` file to read prime and overwrite with new witness.
|
|
105
|
+
* @param {bigint[]} witness - The witness array to write.
|
|
106
|
+
*/
|
|
107
|
+
async function writeWitnessFile(witnessPath, witness) {
|
|
108
|
+
const prime = await getWitnessPrime(witnessPath);
|
|
109
|
+
const fd = await binFileUtils.createBinFile(witnessPath, "wtns", 2, 2);
|
|
110
|
+
await binFileUtils.startWriteSection(fd, 1);
|
|
111
|
+
const n8 = (Math.floor((ffjavascript_1.Scalar.bitLength(prime) - 1) / 64) + 1) * 8;
|
|
112
|
+
await fd.writeULE32(n8);
|
|
113
|
+
await binFileUtils.writeBigInt(fd, prime, n8);
|
|
114
|
+
await fd.writeULE32(witness.length);
|
|
115
|
+
await binFileUtils.endWriteSection(fd);
|
|
116
|
+
await binFileUtils.startWriteSection(fd, 2);
|
|
117
|
+
for (let i = 0; i < witness.length; i++) {
|
|
118
|
+
await binFileUtils.writeBigInt(fd, witness[i], n8);
|
|
119
|
+
}
|
|
120
|
+
await binFileUtils.endWriteSection(fd, 2);
|
|
121
|
+
await fd.close();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Extracts the prime field value from a `.wtns` witness file.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} wtnsPath - Full path to the `.wtns` witness file.
|
|
127
|
+
* @returns {Promise<bigint>} The prime field value used in the witness file.
|
|
128
|
+
*/
|
|
129
|
+
async function getWitnessPrime(wtnsPath) {
|
|
130
|
+
const { fd, sections } = await binFileUtils.readBinFile(wtnsPath, "wtns", 2);
|
|
131
|
+
await binFileUtils.startReadUniqueSection(fd, sections, 1);
|
|
132
|
+
const n8 = await fd.readULE32();
|
|
133
|
+
const prime = await binFileUtils.readBigInt(fd, n8);
|
|
134
|
+
await fd.readULE32();
|
|
135
|
+
await binFileUtils.endReadSection(fd);
|
|
136
|
+
await fd.close();
|
|
137
|
+
return prime;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=witness-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"witness-utils.js","sourceRoot":"","sources":["../../src/utils/witness-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,sDA4BC;AAcD,gDAYC;AAUD,4CAwBC;AA/GD,4CAAoB;AAEpB,mDAAqC;AAErC,aAAa;AACb,+CAAsC;AACtC,aAAa;AACb,kEAAoD;AAEpD;;;;;;;;;;;;;GAaG;AACI,KAAK,UAAU,qBAAqB,CACzC,OAAe,EACf,SAAiC;IAEjC,MAAM,oBAAoB,GAA2B,EAAE,CAAC;IAExD,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IAEvD,MAAM,UAAU,GAAG,YAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IACtE,MAAM,OAAO,GAAG,QAAQ,CAAC,eAAe,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,CAAC;IAErF,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QACnC,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAErC,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC;YACxD,SAAS;QACX,CAAC;QAED,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAE5D,cAAc,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IACvC,CAAC;IAED,IAAI,cAAc,CAAC,IAAI,GAAG,CAA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|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"snarkjs": "0.7.5"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
+
"@iden3/binfileutils": "^0.0.12",
|
|
45
46
|
"@nomicfoundation/hardhat-ethers": "3.0.5",
|
|
46
47
|
"@nomiclabs/hardhat-vyper": "^3.0.7",
|
|
47
48
|
"@solarity/solidity-lib": "3.0.0-rc.3",
|
|
@@ -53,11 +54,13 @@
|
|
|
53
54
|
"chai": "^4.4.1",
|
|
54
55
|
"chai-as-promised": "^7.1.1",
|
|
55
56
|
"ethers": "^6.11.1",
|
|
57
|
+
"ffjavascript": "^0.3.1",
|
|
56
58
|
"hardhat": "2.22.7",
|
|
57
59
|
"husky": "^9.0.11",
|
|
58
60
|
"mocha": "^10.3.0",
|
|
59
61
|
"nyc": "^15.1.0",
|
|
60
62
|
"prettier": "^3.2.5",
|
|
63
|
+
"readline": "^1.3.0",
|
|
61
64
|
"ts-node": "^10.9.2",
|
|
62
65
|
"typescript": "^5.4.5"
|
|
63
66
|
}
|
package/src/core/CircuitZKit.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
-
import * as os from "os";
|
|
4
3
|
import * as snarkjs from "snarkjs";
|
|
5
4
|
import { createHash } from "crypto";
|
|
6
5
|
|
|
@@ -9,6 +8,7 @@ import { Signals } from "../types/proof-utils";
|
|
|
9
8
|
import { CalldataByProtocol, IProtocolImplementer, ProofStructByProtocol, ProvingSystemType } from "../types/protocols";
|
|
10
9
|
|
|
11
10
|
import { MAX_FILE_NAME_LENGTH } from "../constants";
|
|
11
|
+
import { getTmpDir, modifyWitnessArray, checkWitnessOverrides, writeWitnessFile } from "../utils";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
|
|
@@ -50,30 +50,38 @@ export class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
50
50
|
|
|
51
51
|
const verifierFilePath = path.join(this._config.verifierDirPath, verifierFileName);
|
|
52
52
|
|
|
53
|
-
this._implementer.createVerifier(vKeyFilePath, verifierFilePath, languageExtension);
|
|
53
|
+
await this._implementer.createVerifier(vKeyFilePath, verifierFilePath, languageExtension);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* Calculates a witness for the given inputs.
|
|
58
58
|
*
|
|
59
|
+
* If `witnessOverrides` are provided, the corresponding witness values will be substituted in the result.
|
|
60
|
+
*
|
|
61
|
+
* Signal names in `witnessOverrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
62
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
63
|
+
*
|
|
59
64
|
* @param {Signals} inputs - The inputs for the circuit.
|
|
65
|
+
* @param {Record<string, bigint>} [witnessOverrides] - Optional map of signal names to override their witness values.
|
|
60
66
|
* @returns {Promise<bigint[]>} The generated witness.
|
|
61
67
|
*/
|
|
62
|
-
public async calculateWitness(inputs: Signals): Promise<bigint[]> {
|
|
63
|
-
const
|
|
68
|
+
public async calculateWitness(inputs: Signals, witnessOverrides?: Record<string, bigint>): Promise<bigint[]> {
|
|
69
|
+
const wtnsFile = this.getTemporaryWitnessPath();
|
|
70
|
+
const wasmFile = this.mustGetArtifactsFilePath("wasm");
|
|
64
71
|
|
|
65
|
-
|
|
66
|
-
fs.mkdirSync(tmpDir, { recursive: true });
|
|
67
|
-
}
|
|
72
|
+
let signalIndexes: Record<string, number> = {};
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
if (witnessOverrides) {
|
|
75
|
+
const symFile = this.mustGetArtifactsFilePath("sym");
|
|
76
|
+
|
|
77
|
+
signalIndexes = await checkWitnessOverrides(symFile, witnessOverrides);
|
|
78
|
+
}
|
|
71
79
|
|
|
72
80
|
await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);
|
|
73
81
|
|
|
74
|
-
const wtnsJson = await snarkjs.wtns.exportJson(wtnsFile);
|
|
82
|
+
const wtnsJson = (await snarkjs.wtns.exportJson(wtnsFile)) as bigint[];
|
|
75
83
|
|
|
76
|
-
return wtnsJson
|
|
84
|
+
return witnessOverrides ? modifyWitnessArray(wtnsJson, signalIndexes, witnessOverrides) : wtnsJson;
|
|
77
85
|
}
|
|
78
86
|
|
|
79
87
|
/**
|
|
@@ -81,15 +89,40 @@ export class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
81
89
|
*
|
|
82
90
|
* @dev The `inputs` should be in the same order as the circuit expects them.
|
|
83
91
|
*
|
|
92
|
+
* If `witnessOverrides` are provided, the witness will be calculated from the inputs and overridden accordingly.
|
|
93
|
+
* Otherwise, a standard witness will be calculated and used.
|
|
94
|
+
*
|
|
95
|
+
* Signal names in `witnessOverrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
96
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
97
|
+
*
|
|
84
98
|
* @param {Signals} inputs - The inputs for the circuit.
|
|
99
|
+
* @param {Record<string, bigint>} [witnessOverrides] - Optional map of signal names to override their witness values.
|
|
85
100
|
* @returns {Promise<ProofStructByProtocol<Type>>} The generated proof.
|
|
86
|
-
* @todo Add support for other proving systems.
|
|
87
101
|
*/
|
|
88
|
-
public async generateProof(
|
|
102
|
+
public async generateProof(
|
|
103
|
+
inputs: Signals,
|
|
104
|
+
witnessOverrides?: Record<string, bigint>,
|
|
105
|
+
): Promise<ProofStructByProtocol<Type>> {
|
|
89
106
|
const zKeyFile = this.mustGetArtifactsFilePath("zkey");
|
|
90
|
-
const
|
|
107
|
+
const witnessFile = this.getTemporaryWitnessPath();
|
|
108
|
+
|
|
109
|
+
let proof: ProofStructByProtocol<Type>;
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
const witness = await this.calculateWitness(inputs, witnessOverrides);
|
|
91
113
|
|
|
92
|
-
|
|
114
|
+
if (witnessOverrides) {
|
|
115
|
+
await writeWitnessFile(witnessFile, witness);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
proof = await this._implementer.generateProof(zKeyFile, witnessFile);
|
|
119
|
+
} finally {
|
|
120
|
+
if (fs.existsSync(witnessFile)) {
|
|
121
|
+
fs.rmSync(witnessFile);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return proof;
|
|
93
126
|
}
|
|
94
127
|
|
|
95
128
|
/**
|
|
@@ -112,7 +145,6 @@ export class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
112
145
|
*
|
|
113
146
|
* @param {ProofStructByProtocol<Type>} proof - The proof to generate calldata for.
|
|
114
147
|
* @returns {Promise<CalldataByProtocol<Type>>} - The generated calldata.
|
|
115
|
-
* @todo Add other types of calldata.
|
|
116
148
|
*/
|
|
117
149
|
public async generateCalldata(proof: ProofStructByProtocol<Type>): Promise<CalldataByProtocol<Type>> {
|
|
118
150
|
return await this._implementer.generateCalldata(proof);
|
|
@@ -157,6 +189,18 @@ export class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
157
189
|
return this._implementer.getTemplate(languageExtension);
|
|
158
190
|
}
|
|
159
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Returns the path to the temporary witness file.
|
|
194
|
+
*
|
|
195
|
+
* The file is stored in the system temporary directory and is named after the circuit.
|
|
196
|
+
* This file is used for intermediate witness generation and may be deleted after usage.
|
|
197
|
+
*
|
|
198
|
+
* @returns {string} The full path to the temporary `.wtns` file.
|
|
199
|
+
*/
|
|
200
|
+
public getTemporaryWitnessPath(): string {
|
|
201
|
+
return path.join(getTmpDir(), `${this.getCircuitName()}.wtns`);
|
|
202
|
+
}
|
|
203
|
+
|
|
160
204
|
/**
|
|
161
205
|
* Returns the path to the file of the given type inside artifacts directory. Throws an error if the file doesn't exist.
|
|
162
206
|
*
|
|
@@ -2,7 +2,6 @@ import fs from "fs";
|
|
|
2
2
|
import ejs from "ejs";
|
|
3
3
|
import path from "path";
|
|
4
4
|
|
|
5
|
-
import { Signals } from "../../types/proof-utils";
|
|
6
5
|
import {
|
|
7
6
|
IProtocolImplementer,
|
|
8
7
|
ProvingSystemType,
|
|
@@ -31,11 +30,7 @@ export abstract class AbstractProtocolImplementer<T extends ProvingSystemType> i
|
|
|
31
30
|
fs.writeFileSync(verifierFilePath, verifierCode, "utf-8");
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
public abstract generateProof(
|
|
35
|
-
inputs: Signals,
|
|
36
|
-
zKeyFilePath: string,
|
|
37
|
-
wasmFilePath: string,
|
|
38
|
-
): Promise<ProofStructByProtocol<T>>;
|
|
33
|
+
public abstract generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<ProofStructByProtocol<T>>;
|
|
39
34
|
|
|
40
35
|
public abstract verifyProof(proof: ProofStructByProtocol<T>, vKeyFilePath: string): Promise<boolean>;
|
|
41
36
|
|
|
@@ -3,18 +3,17 @@ import * as snarkjs from "snarkjs";
|
|
|
3
3
|
|
|
4
4
|
import { AbstractProtocolImplementer } from "./AbstractImplementer";
|
|
5
5
|
|
|
6
|
-
import { Signals } from "../../types/proof-utils";
|
|
7
6
|
import { Groth16ProofStruct, Groth16CalldataStruct, ProvingSystemType } from "../../types/protocols";
|
|
8
7
|
|
|
9
8
|
import { terminateCurve } from "../../utils";
|
|
10
9
|
|
|
11
10
|
export class Groth16Implementer extends AbstractProtocolImplementer<"groth16"> {
|
|
12
|
-
public async generateProof(
|
|
13
|
-
const
|
|
11
|
+
public async generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<Groth16ProofStruct> {
|
|
12
|
+
const proof = await snarkjs.groth16.prove(zKeyFilePath, witnessFilePath);
|
|
14
13
|
|
|
15
14
|
await terminateCurve();
|
|
16
15
|
|
|
17
|
-
return
|
|
16
|
+
return proof as Groth16ProofStruct;
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
public async verifyProof(proof: Groth16ProofStruct, vKeyFilePath: string): Promise<boolean> {
|
|
@@ -3,18 +3,17 @@ import * as snarkjs from "snarkjs";
|
|
|
3
3
|
|
|
4
4
|
import { AbstractProtocolImplementer } from "./AbstractImplementer";
|
|
5
5
|
|
|
6
|
-
import { Signals } from "../../types/proof-utils";
|
|
7
6
|
import { PlonkProofStruct, PlonkCalldataStruct, ProvingSystemType } from "../../types/protocols";
|
|
8
7
|
|
|
9
8
|
import { terminateCurve } from "../../utils";
|
|
10
9
|
|
|
11
10
|
export class PlonkImplementer extends AbstractProtocolImplementer<"plonk"> {
|
|
12
|
-
public async generateProof(
|
|
13
|
-
const
|
|
11
|
+
public async generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<PlonkProofStruct> {
|
|
12
|
+
const proof = await snarkjs.plonk.prove(zKeyFilePath, witnessFilePath);
|
|
14
13
|
|
|
15
14
|
await terminateCurve();
|
|
16
15
|
|
|
17
|
-
return
|
|
16
|
+
return proof as PlonkProofStruct;
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
public async verifyProof(proof: PlonkProofStruct, vKeyFilePath: string): Promise<boolean> {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Groth16ProofStruct, Groth16CalldataStruct } from "./groth16";
|
|
2
2
|
import { PlonkProofStruct, PlonkCalldataStruct } from "./plonk";
|
|
3
3
|
|
|
4
|
-
import { Signals } from "../proof-utils";
|
|
5
4
|
import { VerifierLanguageType } from "../circuit-zkit";
|
|
6
5
|
|
|
7
6
|
export * from "./groth16";
|
|
@@ -14,7 +13,7 @@ export interface IProtocolImplementer<T extends ProvingSystemType> {
|
|
|
14
13
|
languageExtension: VerifierLanguageType,
|
|
15
14
|
): Promise<void>;
|
|
16
15
|
|
|
17
|
-
generateProof(
|
|
16
|
+
generateProof(zKeyFilePath: string, witnessFilePath: string): Promise<ProofStructByProtocol<T>>;
|
|
18
17
|
|
|
19
18
|
verifyProof(proof: ProofStructByProtocol<T>, vKeyFilePath: string): Promise<boolean>;
|
|
20
19
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
import { BN128_CURVE_NAME } from "../constants";
|
|
6
|
+
|
|
7
|
+
import * as snarkjs from "snarkjs";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Terminates the BN128 curve instance used by SnarkJS.
|
|
11
|
+
*/
|
|
12
|
+
export async function terminateCurve() {
|
|
13
|
+
await (await (snarkjs as any).curves.getCurveFromName(BN128_CURVE_NAME)).terminate();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Returns the path to the temporary directory used by ZKit.
|
|
18
|
+
*
|
|
19
|
+
* Creates the directory if it does not exist.
|
|
20
|
+
*
|
|
21
|
+
* @returns {string} The path to the temporary `.zkit` directory inside the OS temp folder.
|
|
22
|
+
*/
|
|
23
|
+
export function getTmpDir(): string {
|
|
24
|
+
const tmpDir = path.join(os.tmpdir(), ".zkit");
|
|
25
|
+
|
|
26
|
+
if (!fs.existsSync(tmpDir)) {
|
|
27
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return tmpDir;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export * from "./witness-utils";
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
import * as readline from "readline";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import { Scalar } from "ffjavascript";
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
import * as binFileUtils from "@iden3/binfileutils";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validates the provided witness overrides against the `.sym` file and returns the signal-to-index map.
|
|
12
|
+
*
|
|
13
|
+
* Reads the `.sym` file line by line and builds a mapping of signal names to their witness indices.
|
|
14
|
+
* Ensures that all keys in `overrides` exist in the `.sym` file.
|
|
15
|
+
* Throws an error listing all missing signals if any override key is not found.
|
|
16
|
+
*
|
|
17
|
+
* Signal names in `overrides` must be in their full form as represented in the `.sym` file, e.g.,
|
|
18
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} symFile - Path to the `.sym` file.
|
|
21
|
+
* @param {Record<string, bigint>} overrides - Map of signal names to new witness values.
|
|
22
|
+
* @returns {Promise<Record<string, number>>} Map of signal names to their corresponding witness indices.
|
|
23
|
+
*/
|
|
24
|
+
export async function checkWitnessOverrides(
|
|
25
|
+
symFile: string,
|
|
26
|
+
overrides: Record<string, bigint>,
|
|
27
|
+
): Promise<Record<string, number>> {
|
|
28
|
+
const signalToWitnessIndex: Record<string, number> = {};
|
|
29
|
+
|
|
30
|
+
const missingSignals = new Set(Object.keys(overrides));
|
|
31
|
+
|
|
32
|
+
const fileStream = fs.createReadStream(symFile, { encoding: "utf8" });
|
|
33
|
+
const signals = readline.createInterface({ input: fileStream, crlfDelay: Infinity });
|
|
34
|
+
|
|
35
|
+
for await (const signal of signals) {
|
|
36
|
+
const signalInfo = signal.split(",");
|
|
37
|
+
|
|
38
|
+
if (signalInfo.length != 4 || Number(signalInfo[1]) < 0) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
signalToWitnessIndex[signalInfo[3]] = Number(signalInfo[1]);
|
|
43
|
+
|
|
44
|
+
missingSignals.delete(signalInfo[3]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (missingSignals.size > 0) {
|
|
48
|
+
throw new Error(`Signals not found in .sym file: ${Array.from(missingSignals).join(", ")}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return signalToWitnessIndex;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Modifies specific signal values in a witness array.
|
|
56
|
+
* Substitutes signal from `overrides` in the witness array at positions defined in `signalIndexes`.
|
|
57
|
+
*
|
|
58
|
+
* Signal names in `overrides` must be provided in their full form as represented in the `.sym` file, e.g.,
|
|
59
|
+
* `main.signal`, `main.component.signal`, or `main.component.signal[n][m]`.
|
|
60
|
+
*
|
|
61
|
+
* @param {bigint[]} witness - The original witness array.
|
|
62
|
+
* @param {Record<string, number>} signalIndexes - Map of signal names to their witness indices.
|
|
63
|
+
* @param {Record<string, bigint>} overrides - Map of signal names to new witness values.
|
|
64
|
+
* @returns {Promise<bigint[]>} The modified witness array.
|
|
65
|
+
*/
|
|
66
|
+
export async function modifyWitnessArray(
|
|
67
|
+
witness: bigint[],
|
|
68
|
+
signalIndexes: Record<string, number>,
|
|
69
|
+
overrides: Record<string, bigint>,
|
|
70
|
+
): Promise<bigint[]> {
|
|
71
|
+
for (const [signal, value] of Object.entries(overrides)) {
|
|
72
|
+
const index = signalIndexes[signal];
|
|
73
|
+
|
|
74
|
+
witness[index] = value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return witness;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Writes a witness array to a `.wtns` binary file.
|
|
82
|
+
*
|
|
83
|
+
* Reference: https://github.com/iden3/snarkjs/blob/bf28b1cb5aefcefab7e0f70f1fa5e40f764cca72/src/wtns_utils.js#L25C42-L25C47
|
|
84
|
+
*
|
|
85
|
+
* @param {string} witnessPath - Path to the existing `.wtns` file to read prime and overwrite with new witness.
|
|
86
|
+
* @param {bigint[]} witness - The witness array to write.
|
|
87
|
+
*/
|
|
88
|
+
export async function writeWitnessFile(witnessPath: string, witness: bigint[]) {
|
|
89
|
+
const prime = await getWitnessPrime(witnessPath);
|
|
90
|
+
|
|
91
|
+
const fd = await binFileUtils.createBinFile(witnessPath, "wtns", 2, 2);
|
|
92
|
+
|
|
93
|
+
await binFileUtils.startWriteSection(fd, 1);
|
|
94
|
+
|
|
95
|
+
const n8 = (Math.floor((Scalar.bitLength(prime) - 1) / 64) + 1) * 8;
|
|
96
|
+
await fd.writeULE32(n8);
|
|
97
|
+
await binFileUtils.writeBigInt(fd, prime, n8);
|
|
98
|
+
|
|
99
|
+
await fd.writeULE32(witness.length);
|
|
100
|
+
|
|
101
|
+
await binFileUtils.endWriteSection(fd);
|
|
102
|
+
|
|
103
|
+
await binFileUtils.startWriteSection(fd, 2);
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < witness.length; i++) {
|
|
106
|
+
await binFileUtils.writeBigInt(fd, witness[i], n8);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
await binFileUtils.endWriteSection(fd, 2);
|
|
110
|
+
|
|
111
|
+
await fd.close();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Extracts the prime field value from a `.wtns` witness file.
|
|
116
|
+
*
|
|
117
|
+
* @param {string} wtnsPath - Full path to the `.wtns` witness file.
|
|
118
|
+
* @returns {Promise<bigint>} The prime field value used in the witness file.
|
|
119
|
+
*/
|
|
120
|
+
async function getWitnessPrime(wtnsPath: string): Promise<bigint> {
|
|
121
|
+
const { fd, sections } = await binFileUtils.readBinFile(wtnsPath, "wtns", 2);
|
|
122
|
+
|
|
123
|
+
await binFileUtils.startReadUniqueSection(fd, sections, 1);
|
|
124
|
+
|
|
125
|
+
const n8 = await fd.readULE32();
|
|
126
|
+
const prime = await binFileUtils.readBigInt(fd, n8);
|
|
127
|
+
await fd.readULE32();
|
|
128
|
+
|
|
129
|
+
await binFileUtils.endReadSection(fd);
|
|
130
|
+
await fd.close();
|
|
131
|
+
|
|
132
|
+
return prime;
|
|
133
|
+
}
|
package/dist/utils.d.ts
DELETED
package/dist/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,wBAAsB,cAAc,kBAEnC"}
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wCAEC;AALD,iDAAmC;AACnC,2CAA+C;AAExC,KAAK,UAAU,cAAc;IAClC,MAAM,CAAC,MAAO,OAAe,CAAC,MAAM,CAAC,gBAAgB,CAAC,4BAAgB,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AACvF,CAAC"}
|