@aztec/bb-prover 0.39.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/dest/bb/cli.d.ts +9 -0
- package/dest/bb/cli.d.ts.map +1 -0
- package/dest/bb/cli.js +64 -0
- package/dest/bb/execute.d.ts +88 -0
- package/dest/bb/execute.d.ts.map +1 -0
- package/dest/bb/execute.js +288 -0
- package/dest/bb/index.d.ts +3 -0
- package/dest/bb/index.d.ts.map +1 -0
- package/dest/bb/index.js +18 -0
- package/dest/index.d.ts +3 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +3 -0
- package/dest/mappings/mappings.d.ts +12 -0
- package/dest/mappings/mappings.d.ts.map +1 -0
- package/dest/mappings/mappings.js +22 -0
- package/dest/prover/bb_native_proof_creator.d.ts +58 -0
- package/dest/prover/bb_native_proof_creator.d.ts.map +1 -0
- package/dest/prover/bb_native_proof_creator.js +238 -0
- package/dest/prover/bb_prover.d.ts +110 -0
- package/dest/prover/bb_prover.d.ts.map +1 -0
- package/dest/prover/bb_prover.js +321 -0
- package/dest/prover/index.d.ts +3 -0
- package/dest/prover/index.d.ts.map +1 -0
- package/dest/prover/index.js +3 -0
- package/dest/prover/verification_key_data.d.ts +16 -0
- package/dest/prover/verification_key_data.d.ts.map +1 -0
- package/dest/prover/verification_key_data.js +5 -0
- package/dest/stats.d.ts +9 -0
- package/dest/stats.d.ts.map +1 -0
- package/dest/stats.js +61 -0
- package/dest/test/index.d.ts +2 -0
- package/dest/test/index.d.ts.map +1 -0
- package/dest/test/index.js +2 -0
- package/dest/test/test_circuit_prover.d.ts +48 -0
- package/dest/test/test_circuit_prover.d.ts.map +1 -0
- package/dest/test/test_circuit_prover.js +128 -0
- package/package.json +85 -0
- package/src/bb/cli.ts +92 -0
- package/src/bb/execute.ts +359 -0
- package/src/bb/index.ts +23 -0
- package/src/index.ts +2 -0
- package/src/mappings/mappings.ts +41 -0
- package/src/prover/bb_native_proof_creator.ts +382 -0
- package/src/prover/bb_prover.ts +557 -0
- package/src/prover/index.ts +2 -0
- package/src/prover/verification_key_data.ts +16 -0
- package/src/stats.ts +82 -0
- package/src/test/index.ts +1 -0
- package/src/test/test_circuit_prover.ts +268 -0
package/src/bb/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --no-warnings
|
|
2
|
+
import { createConsoleLogger } from '@aztec/foundation/log';
|
|
3
|
+
|
|
4
|
+
import 'source-map-support/register.js';
|
|
5
|
+
|
|
6
|
+
import { getProgram } from './cli.js';
|
|
7
|
+
|
|
8
|
+
const log = createConsoleLogger();
|
|
9
|
+
|
|
10
|
+
/** CLI main entrypoint */
|
|
11
|
+
async function main() {
|
|
12
|
+
process.once('SIGINT', () => process.exit(0));
|
|
13
|
+
process.once('SIGTERM', () => process.exit(0));
|
|
14
|
+
|
|
15
|
+
const program = getProgram(log);
|
|
16
|
+
await program.parseAsync(process.argv);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
main().catch(err => {
|
|
20
|
+
log(`Error in command execution`);
|
|
21
|
+
log(err);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { PublicKernelType } from '@aztec/circuit-types';
|
|
2
|
+
import { type PublicKernelCircuitPrivateInputs, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
|
|
3
|
+
import {
|
|
4
|
+
type ServerProtocolArtifact,
|
|
5
|
+
convertPublicInnerRollupInputsToWitnessMap,
|
|
6
|
+
convertPublicInnerRollupOutputFromWitnessMap,
|
|
7
|
+
convertPublicSetupRollupInputsToWitnessMap,
|
|
8
|
+
convertPublicSetupRollupOutputFromWitnessMap,
|
|
9
|
+
convertPublicTeardownRollupInputsToWitnessMap,
|
|
10
|
+
convertPublicTeardownRollupOutputFromWitnessMap,
|
|
11
|
+
} from '@aztec/noir-protocol-circuits-types';
|
|
12
|
+
|
|
13
|
+
import { type WitnessMap } from '@noir-lang/types';
|
|
14
|
+
|
|
15
|
+
export type PublicKernelProvingOps = {
|
|
16
|
+
artifact: ServerProtocolArtifact;
|
|
17
|
+
convertInputs: (inputs: PublicKernelCircuitPrivateInputs) => WitnessMap;
|
|
18
|
+
convertOutputs: (outputs: WitnessMap) => PublicKernelCircuitPublicInputs;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type KernelTypeToArtifact = Record<PublicKernelType, PublicKernelProvingOps | undefined>;
|
|
22
|
+
|
|
23
|
+
export const PublicKernelArtifactMapping: KernelTypeToArtifact = {
|
|
24
|
+
[PublicKernelType.NON_PUBLIC]: undefined,
|
|
25
|
+
[PublicKernelType.APP_LOGIC]: {
|
|
26
|
+
artifact: 'PublicKernelAppLogicArtifact',
|
|
27
|
+
convertInputs: convertPublicInnerRollupInputsToWitnessMap,
|
|
28
|
+
convertOutputs: convertPublicInnerRollupOutputFromWitnessMap,
|
|
29
|
+
},
|
|
30
|
+
[PublicKernelType.SETUP]: {
|
|
31
|
+
artifact: 'PublicKernelSetupArtifact',
|
|
32
|
+
convertInputs: convertPublicSetupRollupInputsToWitnessMap,
|
|
33
|
+
convertOutputs: convertPublicSetupRollupOutputFromWitnessMap,
|
|
34
|
+
},
|
|
35
|
+
[PublicKernelType.TEARDOWN]: {
|
|
36
|
+
artifact: 'PublicKernelTeardownArtifact',
|
|
37
|
+
convertInputs: convertPublicTeardownRollupInputsToWitnessMap,
|
|
38
|
+
convertOutputs: convertPublicTeardownRollupOutputFromWitnessMap,
|
|
39
|
+
},
|
|
40
|
+
[PublicKernelType.TAIL]: undefined,
|
|
41
|
+
};
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { type AppCircuitProofOutput, type KernelProofOutput, type ProofCreator } from '@aztec/circuit-types';
|
|
2
|
+
import {
|
|
3
|
+
Fr,
|
|
4
|
+
NESTED_RECURSIVE_PROOF_LENGTH,
|
|
5
|
+
type PrivateCircuitPublicInputs,
|
|
6
|
+
type PrivateKernelCircuitPublicInputs,
|
|
7
|
+
type PrivateKernelInitCircuitPrivateInputs,
|
|
8
|
+
type PrivateKernelInnerCircuitPrivateInputs,
|
|
9
|
+
type PrivateKernelTailCircuitPrivateInputs,
|
|
10
|
+
type PrivateKernelTailCircuitPublicInputs,
|
|
11
|
+
Proof,
|
|
12
|
+
RECURSIVE_PROOF_LENGTH,
|
|
13
|
+
RecursiveProof,
|
|
14
|
+
type VERIFICATION_KEY_LENGTH_IN_FIELDS,
|
|
15
|
+
VerificationKeyAsFields,
|
|
16
|
+
} from '@aztec/circuits.js';
|
|
17
|
+
import { siloNoteHash } from '@aztec/circuits.js/hash';
|
|
18
|
+
import { randomBytes } from '@aztec/foundation/crypto';
|
|
19
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
20
|
+
import { type Tuple } from '@aztec/foundation/serialize';
|
|
21
|
+
import {
|
|
22
|
+
ClientCircuitArtifacts,
|
|
23
|
+
type ClientProtocolArtifact,
|
|
24
|
+
convertPrivateKernelInitInputsToWitnessMap,
|
|
25
|
+
convertPrivateKernelInitOutputsFromWitnessMap,
|
|
26
|
+
convertPrivateKernelInnerInputsToWitnessMap,
|
|
27
|
+
convertPrivateKernelInnerOutputsFromWitnessMap,
|
|
28
|
+
convertPrivateKernelTailForPublicOutputsFromWitnessMap,
|
|
29
|
+
convertPrivateKernelTailInputsToWitnessMap,
|
|
30
|
+
convertPrivateKernelTailOutputsFromWitnessMap,
|
|
31
|
+
convertPrivateKernelTailToPublicInputsToWitnessMap,
|
|
32
|
+
} from '@aztec/noir-protocol-circuits-types';
|
|
33
|
+
import { type ACVMField, WASMSimulator } from '@aztec/simulator';
|
|
34
|
+
import { type NoirCompiledCircuit } from '@aztec/types/noir';
|
|
35
|
+
|
|
36
|
+
import { serializeWitness } from '@noir-lang/noirc_abi';
|
|
37
|
+
import { type WitnessMap } from '@noir-lang/types';
|
|
38
|
+
import * as fs from 'fs/promises';
|
|
39
|
+
|
|
40
|
+
import {
|
|
41
|
+
BB_RESULT,
|
|
42
|
+
PROOF_FIELDS_FILENAME,
|
|
43
|
+
PROOF_FILENAME,
|
|
44
|
+
VK_FIELDS_FILENAME,
|
|
45
|
+
VK_FILENAME,
|
|
46
|
+
generateKeyForNoirCircuit,
|
|
47
|
+
generateProof,
|
|
48
|
+
verifyProof,
|
|
49
|
+
} from '../bb/execute.js';
|
|
50
|
+
import {
|
|
51
|
+
AGGREGATION_OBJECT_SIZE,
|
|
52
|
+
CIRCUIT_PUBLIC_INPUTS_INDEX,
|
|
53
|
+
CIRCUIT_RECURSIVE_INDEX,
|
|
54
|
+
CIRCUIT_SIZE_INDEX,
|
|
55
|
+
type VerificationKeyData,
|
|
56
|
+
} from './verification_key_data.js';
|
|
57
|
+
|
|
58
|
+
type PrivateKernelProvingOps = {
|
|
59
|
+
convertOutputs: (outputs: WitnessMap) => PrivateKernelCircuitPublicInputs | PrivateKernelTailCircuitPublicInputs;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const PrivateKernelArtifactMapping: Record<ClientProtocolArtifact, PrivateKernelProvingOps> = {
|
|
63
|
+
PrivateKernelInitArtifact: {
|
|
64
|
+
convertOutputs: convertPrivateKernelInitOutputsFromWitnessMap,
|
|
65
|
+
},
|
|
66
|
+
PrivateKernelInnerArtifact: {
|
|
67
|
+
convertOutputs: convertPrivateKernelInnerOutputsFromWitnessMap,
|
|
68
|
+
},
|
|
69
|
+
PrivateKernelTailArtifact: {
|
|
70
|
+
convertOutputs: convertPrivateKernelTailOutputsFromWitnessMap,
|
|
71
|
+
},
|
|
72
|
+
PrivateKernelTailToPublicArtifact: {
|
|
73
|
+
convertOutputs: convertPrivateKernelTailForPublicOutputsFromWitnessMap,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* This proof creator implementation uses the native bb binary.
|
|
79
|
+
* This is a temporary implementation until we make the WASM version work.
|
|
80
|
+
*/
|
|
81
|
+
export class BBNativeProofCreator implements ProofCreator {
|
|
82
|
+
private simulator = new WASMSimulator();
|
|
83
|
+
|
|
84
|
+
private verificationKeys: Map<ClientProtocolArtifact, Promise<VerificationKeyData>> = new Map<
|
|
85
|
+
ClientProtocolArtifact,
|
|
86
|
+
Promise<VerificationKeyData>
|
|
87
|
+
>();
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
private bbBinaryPath: string,
|
|
91
|
+
private bbWorkingDirectory: string,
|
|
92
|
+
private log = createDebugLogger('aztec:bb-native-prover'),
|
|
93
|
+
) {}
|
|
94
|
+
|
|
95
|
+
public getSiloedCommitments(publicInputs: PrivateCircuitPublicInputs) {
|
|
96
|
+
const contractAddress = publicInputs.callContext.storageContractAddress;
|
|
97
|
+
|
|
98
|
+
return Promise.resolve(
|
|
99
|
+
publicInputs.newNoteHashes.map(commitment => siloNoteHash(contractAddress, commitment.value)),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public async createProofInit(
|
|
104
|
+
inputs: PrivateKernelInitCircuitPrivateInputs,
|
|
105
|
+
): Promise<KernelProofOutput<PrivateKernelCircuitPublicInputs>> {
|
|
106
|
+
const witnessMap = convertPrivateKernelInitInputsToWitnessMap(inputs);
|
|
107
|
+
return await this.createSafeProof(witnessMap, 'PrivateKernelInitArtifact');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public async createProofInner(
|
|
111
|
+
inputs: PrivateKernelInnerCircuitPrivateInputs,
|
|
112
|
+
): Promise<KernelProofOutput<PrivateKernelCircuitPublicInputs>> {
|
|
113
|
+
const witnessMap = convertPrivateKernelInnerInputsToWitnessMap(inputs);
|
|
114
|
+
return await this.createSafeProof(witnessMap, 'PrivateKernelInnerArtifact');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async createProofTail(
|
|
118
|
+
inputs: PrivateKernelTailCircuitPrivateInputs,
|
|
119
|
+
): Promise<KernelProofOutput<PrivateKernelTailCircuitPublicInputs>> {
|
|
120
|
+
if (!inputs.isForPublic()) {
|
|
121
|
+
const witnessMap = convertPrivateKernelTailInputsToWitnessMap(inputs);
|
|
122
|
+
return await this.createSafeProof(witnessMap, 'PrivateKernelTailArtifact');
|
|
123
|
+
}
|
|
124
|
+
const witnessMap = convertPrivateKernelTailToPublicInputsToWitnessMap(inputs);
|
|
125
|
+
return await this.createSafeProof(witnessMap, 'PrivateKernelTailToPublicArtifact');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public async createAppCircuitProof(
|
|
129
|
+
partialWitness: Map<number, ACVMField>,
|
|
130
|
+
bytecode: Buffer,
|
|
131
|
+
): Promise<AppCircuitProofOutput> {
|
|
132
|
+
const directory = `${this.bbWorkingDirectory}/${randomBytes(8).toString('hex')}`;
|
|
133
|
+
await fs.mkdir(directory, { recursive: true });
|
|
134
|
+
this.log.debug(`Created directory: ${directory}`);
|
|
135
|
+
try {
|
|
136
|
+
this.log.debug(`Proving app circuit`);
|
|
137
|
+
const proofOutput = await this.createProof(directory, partialWitness, bytecode, 'App');
|
|
138
|
+
if (proofOutput.proof.proof.length != RECURSIVE_PROOF_LENGTH) {
|
|
139
|
+
throw new Error(`Incorrect proof length`);
|
|
140
|
+
}
|
|
141
|
+
const proof = proofOutput.proof as RecursiveProof<typeof RECURSIVE_PROOF_LENGTH>;
|
|
142
|
+
const output: AppCircuitProofOutput = {
|
|
143
|
+
proof,
|
|
144
|
+
verificationKey: proofOutput.verificationKey,
|
|
145
|
+
};
|
|
146
|
+
return output;
|
|
147
|
+
} finally {
|
|
148
|
+
await fs.rm(directory, { recursive: true, force: true });
|
|
149
|
+
this.log.debug(`Deleted directory: ${directory}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Verifies a proof, will generate the verification key if one is not cached internally
|
|
155
|
+
* @param circuitType - The type of circuit whose proof is to be verified
|
|
156
|
+
* @param proof - The proof to be verified
|
|
157
|
+
*/
|
|
158
|
+
public async verifyProofForProtocolCircuit(circuitType: ClientProtocolArtifact, proof: Proof) {
|
|
159
|
+
const verificationKey = await this.getVerificationKeyDataForCircuit(circuitType);
|
|
160
|
+
|
|
161
|
+
this.log.debug(`Verifying with key: ${verificationKey.hash.toString()}`);
|
|
162
|
+
|
|
163
|
+
const logFunction = (message: string) => {
|
|
164
|
+
this.log.debug(`${circuitType} BB out - ${message}`);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const result = await this.verifyProofFromKey(verificationKey.keyAsBytes, proof, logFunction);
|
|
168
|
+
|
|
169
|
+
if (result.status === BB_RESULT.FAILURE) {
|
|
170
|
+
const errorMessage = `Failed to verify ${circuitType} proof!`;
|
|
171
|
+
throw new Error(errorMessage);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this.log.info(`Successfully verified ${circuitType} proof in ${result.duration} ms`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private async verifyProofFromKey(
|
|
178
|
+
verificationKey: Buffer,
|
|
179
|
+
proof: Proof,
|
|
180
|
+
logFunction: (message: string) => void = () => {},
|
|
181
|
+
) {
|
|
182
|
+
// Create random directory to be used for temp files
|
|
183
|
+
const bbWorkingDirectory = `${this.bbWorkingDirectory}/${randomBytes(8).toString('hex')}`;
|
|
184
|
+
await fs.mkdir(bbWorkingDirectory, { recursive: true });
|
|
185
|
+
|
|
186
|
+
const proofFileName = `${bbWorkingDirectory}/proof`;
|
|
187
|
+
const verificationKeyPath = `${bbWorkingDirectory}/vk`;
|
|
188
|
+
|
|
189
|
+
await fs.writeFile(proofFileName, proof.buffer);
|
|
190
|
+
await fs.writeFile(verificationKeyPath, verificationKey);
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
return await verifyProof(this.bbBinaryPath, proofFileName, verificationKeyPath!, logFunction);
|
|
194
|
+
} finally {
|
|
195
|
+
await fs.rm(bbWorkingDirectory, { recursive: true, force: true });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Returns the verification key data for a circuit, will generate and cache it if not cached internally
|
|
201
|
+
* @param circuitType - The type of circuit for which the verification key is required
|
|
202
|
+
* @returns The verification key data
|
|
203
|
+
*/
|
|
204
|
+
private async getVerificationKeyDataForCircuit(circuitType: ClientProtocolArtifact): Promise<VerificationKeyData> {
|
|
205
|
+
let promise = this.verificationKeys.get(circuitType);
|
|
206
|
+
if (!promise) {
|
|
207
|
+
promise = generateKeyForNoirCircuit(
|
|
208
|
+
this.bbBinaryPath,
|
|
209
|
+
this.bbWorkingDirectory,
|
|
210
|
+
circuitType,
|
|
211
|
+
ClientCircuitArtifacts[circuitType],
|
|
212
|
+
'vk',
|
|
213
|
+
this.log.debug,
|
|
214
|
+
).then(result => {
|
|
215
|
+
if (result.status === BB_RESULT.FAILURE) {
|
|
216
|
+
throw new Error(`Failed to generate verification key for ${circuitType}, ${result.reason}`);
|
|
217
|
+
}
|
|
218
|
+
return this.convertVk(result.vkPath!);
|
|
219
|
+
});
|
|
220
|
+
this.verificationKeys.set(circuitType, promise);
|
|
221
|
+
}
|
|
222
|
+
return await promise;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Reads the verification key data stored at the specified location and parses into a VerificationKeyData
|
|
227
|
+
* @param filePath - The directory containing the verification key data files
|
|
228
|
+
* @returns The verification key data
|
|
229
|
+
*/
|
|
230
|
+
private async convertVk(filePath: string): Promise<VerificationKeyData> {
|
|
231
|
+
const [rawFields, rawBinary] = await Promise.all([
|
|
232
|
+
fs.readFile(`${filePath}/${VK_FIELDS_FILENAME}`, { encoding: 'utf-8' }),
|
|
233
|
+
fs.readFile(`${filePath}/${VK_FILENAME}`),
|
|
234
|
+
]);
|
|
235
|
+
const fieldsJson = JSON.parse(rawFields);
|
|
236
|
+
const fields = fieldsJson.map(Fr.fromString);
|
|
237
|
+
// The first item is the hash, this is not part of the actual VK
|
|
238
|
+
const vkHash = fields[0];
|
|
239
|
+
const actualVk = fields.slice(1);
|
|
240
|
+
const vk: VerificationKeyData = {
|
|
241
|
+
hash: vkHash,
|
|
242
|
+
keyAsFields: actualVk as Tuple<Fr, typeof VERIFICATION_KEY_LENGTH_IN_FIELDS>,
|
|
243
|
+
keyAsBytes: rawBinary,
|
|
244
|
+
numPublicInputs: Number(actualVk[CIRCUIT_PUBLIC_INPUTS_INDEX]),
|
|
245
|
+
circuitSize: Number(actualVk[CIRCUIT_SIZE_INDEX]),
|
|
246
|
+
isRecursive: actualVk[CIRCUIT_RECURSIVE_INDEX] == Fr.ONE,
|
|
247
|
+
};
|
|
248
|
+
return vk;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Ensures our verification key cache includes the key data located at the specified directory
|
|
253
|
+
* @param filePath - The directory containing the verification key data files
|
|
254
|
+
* @param circuitType - The type of circuit to which the verification key corresponds
|
|
255
|
+
*/
|
|
256
|
+
private async updateVerificationKeyAfterProof(filePath: string, circuitType: ClientProtocolArtifact) {
|
|
257
|
+
let promise = this.verificationKeys.get(circuitType);
|
|
258
|
+
if (!promise) {
|
|
259
|
+
promise = this.convertVk(filePath);
|
|
260
|
+
this.log.debug(`Updated verification key for circuit: ${circuitType}`);
|
|
261
|
+
this.verificationKeys.set(circuitType, promise);
|
|
262
|
+
}
|
|
263
|
+
return await promise;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private async createSafeProof<T>(
|
|
267
|
+
inputs: WitnessMap,
|
|
268
|
+
circuitType: ClientProtocolArtifact,
|
|
269
|
+
): Promise<KernelProofOutput<T>> {
|
|
270
|
+
const directory = `${this.bbWorkingDirectory}/${randomBytes(8).toString('hex')}`;
|
|
271
|
+
await fs.mkdir(directory, { recursive: true });
|
|
272
|
+
this.log.debug(`Created directory: ${directory}`);
|
|
273
|
+
try {
|
|
274
|
+
return await this.generateWitnessAndCreateProof(inputs, circuitType, directory);
|
|
275
|
+
} finally {
|
|
276
|
+
await fs.rm(directory, { recursive: true, force: true });
|
|
277
|
+
this.log.debug(`Deleted directory: ${directory}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private async generateWitnessAndCreateProof<T>(
|
|
282
|
+
inputs: WitnessMap,
|
|
283
|
+
circuitType: ClientProtocolArtifact,
|
|
284
|
+
directory: string,
|
|
285
|
+
): Promise<KernelProofOutput<T>> {
|
|
286
|
+
this.log.debug(`Generating witness for ${circuitType}`);
|
|
287
|
+
const compiledCircuit: NoirCompiledCircuit = ClientCircuitArtifacts[circuitType];
|
|
288
|
+
|
|
289
|
+
const outputWitness = await this.simulator.simulateCircuit(inputs, compiledCircuit);
|
|
290
|
+
|
|
291
|
+
this.log.debug(`Generated witness for ${circuitType}`);
|
|
292
|
+
|
|
293
|
+
const publicInputs = PrivateKernelArtifactMapping[circuitType].convertOutputs(outputWitness) as T;
|
|
294
|
+
|
|
295
|
+
const proofOutput = await this.createProof(
|
|
296
|
+
directory,
|
|
297
|
+
outputWitness,
|
|
298
|
+
Buffer.from(compiledCircuit.bytecode, 'base64'),
|
|
299
|
+
circuitType,
|
|
300
|
+
);
|
|
301
|
+
if (proofOutput.proof.proof.length != NESTED_RECURSIVE_PROOF_LENGTH) {
|
|
302
|
+
throw new Error(`Incorrect proof length`);
|
|
303
|
+
}
|
|
304
|
+
const nestedProof = proofOutput.proof as RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>;
|
|
305
|
+
|
|
306
|
+
const kernelOutput: KernelProofOutput<T> = {
|
|
307
|
+
publicInputs,
|
|
308
|
+
proof: nestedProof,
|
|
309
|
+
verificationKey: proofOutput.verificationKey,
|
|
310
|
+
};
|
|
311
|
+
return kernelOutput;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private async createProof(
|
|
315
|
+
directory: string,
|
|
316
|
+
partialWitness: WitnessMap,
|
|
317
|
+
bytecode: Buffer,
|
|
318
|
+
circuitType: ClientProtocolArtifact | 'App',
|
|
319
|
+
): Promise<{
|
|
320
|
+
proof: RecursiveProof<typeof RECURSIVE_PROOF_LENGTH> | RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>;
|
|
321
|
+
verificationKey: VerificationKeyAsFields;
|
|
322
|
+
}> {
|
|
323
|
+
const compressedBincodedWitness = serializeWitness(partialWitness);
|
|
324
|
+
|
|
325
|
+
const inputsWitnessFile = `${directory}/witness.gz`;
|
|
326
|
+
|
|
327
|
+
await fs.writeFile(inputsWitnessFile, compressedBincodedWitness);
|
|
328
|
+
|
|
329
|
+
this.log.debug(`Written ${inputsWitnessFile}`);
|
|
330
|
+
|
|
331
|
+
const provingResult = await generateProof(
|
|
332
|
+
this.bbBinaryPath,
|
|
333
|
+
directory,
|
|
334
|
+
circuitType,
|
|
335
|
+
bytecode,
|
|
336
|
+
inputsWitnessFile,
|
|
337
|
+
this.log.debug,
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
if (provingResult.status === BB_RESULT.FAILURE) {
|
|
341
|
+
this.log.error(`Failed to generate proof for ${circuitType}: ${provingResult.reason}`);
|
|
342
|
+
throw new Error(provingResult.reason);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (circuitType === 'App') {
|
|
346
|
+
const vkData = await this.convertVk(directory);
|
|
347
|
+
const proof = await this.readProofAsFields<typeof RECURSIVE_PROOF_LENGTH>(directory, circuitType, vkData);
|
|
348
|
+
return { proof, verificationKey: new VerificationKeyAsFields(vkData.keyAsFields, vkData.hash) };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const vkData = await this.updateVerificationKeyAfterProof(directory, circuitType);
|
|
352
|
+
const proof = await this.readProofAsFields<typeof NESTED_RECURSIVE_PROOF_LENGTH>(directory, circuitType, vkData);
|
|
353
|
+
return { proof, verificationKey: new VerificationKeyAsFields(vkData.keyAsFields, vkData.hash) };
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Parses and returns the proof data stored at the specified directory
|
|
358
|
+
* @param filePath - The directory containing the proof data
|
|
359
|
+
* @param circuitType - The type of circuit proven
|
|
360
|
+
* @returns The proof
|
|
361
|
+
*/
|
|
362
|
+
private async readProofAsFields<PROOF_LENGTH extends number>(
|
|
363
|
+
filePath: string,
|
|
364
|
+
circuitType: ClientProtocolArtifact | 'App',
|
|
365
|
+
vkData: VerificationKeyData,
|
|
366
|
+
): Promise<RecursiveProof<PROOF_LENGTH>> {
|
|
367
|
+
const [binaryProof, proofString] = await Promise.all([
|
|
368
|
+
fs.readFile(`${filePath}/${PROOF_FILENAME}`),
|
|
369
|
+
fs.readFile(`${filePath}/${PROOF_FIELDS_FILENAME}`, { encoding: 'utf-8' }),
|
|
370
|
+
]);
|
|
371
|
+
const json = JSON.parse(proofString);
|
|
372
|
+
const fields = json.map(Fr.fromString);
|
|
373
|
+
const numPublicInputs =
|
|
374
|
+
circuitType === 'App' ? vkData.numPublicInputs : vkData.numPublicInputs - AGGREGATION_OBJECT_SIZE;
|
|
375
|
+
const fieldsWithoutPublicInputs = fields.slice(numPublicInputs);
|
|
376
|
+
this.log.debug(
|
|
377
|
+
`Circuit type: ${circuitType}, complete proof length: ${fields.length}, without public inputs: ${fieldsWithoutPublicInputs.length}, num public inputs: ${numPublicInputs}, circuit size: ${vkData.circuitSize}, is recursive: ${vkData.isRecursive}, raw length: ${binaryProof.length}`,
|
|
378
|
+
);
|
|
379
|
+
const proof = new RecursiveProof<PROOF_LENGTH>(fieldsWithoutPublicInputs, new Proof(binaryProof));
|
|
380
|
+
return proof;
|
|
381
|
+
}
|
|
382
|
+
}
|