Skip to content

Commit be36c1e

Browse files
authored
chore(nargo)!: Make proving and verification keys optional (#1880)
Make proving and verification keys optional
1 parent 28d43dc commit be36c1e

File tree

7 files changed

+49
-16
lines changed

7 files changed

+49
-16
lines changed

crates/nargo/src/artifacts/contract.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ pub struct PreprocessedContractFunction {
3636
)]
3737
pub bytecode: Circuit,
3838

39-
pub proving_key: Vec<u8>,
40-
pub verification_key: Vec<u8>,
39+
pub proving_key: Option<Vec<u8>>,
40+
pub verification_key: Option<Vec<u8>>,
4141
}

crates/nargo/src/artifacts/program.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ pub struct PreprocessedProgram {
1818
)]
1919
pub bytecode: Circuit,
2020

21-
pub proving_key: Vec<u8>,
22-
pub verification_key: Vec<u8>,
21+
pub proving_key: Option<Vec<u8>>,
22+
pub verification_key: Option<Vec<u8>>,
2323
}

crates/nargo/src/ops/preprocess.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg";
88

99
pub fn preprocess_program<B: ProofSystemCompiler>(
1010
backend: &B,
11+
include_keys: bool,
1112
common_reference_string: &[u8],
1213
compiled_program: CompiledProgram,
1314
) -> Result<PreprocessedProgram, B::Error> {
1415
// TODO: currently `compiled_program`'s bytecode is already optimized for the backend.
1516
// In future we'll need to apply those optimizations here.
1617
let optimized_bytecode = compiled_program.circuit;
17-
let (proving_key, verification_key) =
18-
backend.preprocess(common_reference_string, &optimized_bytecode)?;
18+
19+
let (proving_key, verification_key) = if include_keys {
20+
let (proving_key, verification_key) =
21+
backend.preprocess(common_reference_string, &optimized_bytecode)?;
22+
(Some(proving_key), Some(verification_key))
23+
} else {
24+
(None, None)
25+
};
1926

2027
Ok(PreprocessedProgram {
2128
backend: String::from(BACKEND_IDENTIFIER),
@@ -28,14 +35,20 @@ pub fn preprocess_program<B: ProofSystemCompiler>(
2835

2936
pub fn preprocess_contract_function<B: ProofSystemCompiler>(
3037
backend: &B,
38+
include_keys: bool,
3139
common_reference_string: &[u8],
3240
func: ContractFunction,
3341
) -> Result<PreprocessedContractFunction, B::Error> {
3442
// TODO: currently `func`'s bytecode is already optimized for the backend.
3543
// In future we'll need to apply those optimizations here.
3644
let optimized_bytecode = func.bytecode;
37-
let (proving_key, verification_key) =
38-
backend.preprocess(common_reference_string, &optimized_bytecode)?;
45+
let (proving_key, verification_key) = if include_keys {
46+
let (proving_key, verification_key) =
47+
backend.preprocess(common_reference_string, &optimized_bytecode)?;
48+
(Some(proving_key), Some(verification_key))
49+
} else {
50+
(None, None)
51+
};
3952

4053
Ok(PreprocessedContractFunction {
4154
name: func.name,

crates/nargo_cli/src/cli/codegen_verifier_cmd.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ pub(crate) fn run<B: Backend>(
5656
let common_reference_string =
5757
update_common_reference_string(backend, &common_reference_string, &program.circuit)
5858
.map_err(CliError::CommonReferenceStringError)?;
59-
let program = preprocess_program(backend, &common_reference_string, program)
59+
let program = preprocess_program(backend, true, &common_reference_string, program)
6060
.map_err(CliError::ProofSystemCompilerError)?;
6161
(common_reference_string, program)
6262
}
6363
};
6464

65+
let verification_key = preprocessed_program
66+
.verification_key
67+
.expect("Verification key should exist as `true` is passed to `preprocess_program`");
6568
let smart_contract_string =
66-
codegen_verifier(backend, &common_reference_string, &preprocessed_program.verification_key)
69+
codegen_verifier(backend, &common_reference_string, &verification_key)
6770
.map_err(CliError::SmartContractError)?;
6871

6972
write_cached_common_reference_string(&common_reference_string);

crates/nargo_cli/src/cli/compile_cmd.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ pub(crate) struct CompileCommand {
3232
/// The name of the ACIR file
3333
circuit_name: String,
3434

35+
/// Include Proving and Verification keys in the build artifacts.
36+
#[arg(long)]
37+
include_keys: bool,
38+
3539
/// Compile each contract function used within the program
3640
#[arg(short, long)]
3741
contracts: bool,
@@ -75,8 +79,13 @@ pub(crate) fn run<B: Backend>(
7579
)
7680
.map_err(CliError::CommonReferenceStringError)?;
7781

78-
preprocess_contract_function(backend, &common_reference_string, func)
79-
.map_err(CliError::ProofSystemCompilerError)
82+
preprocess_contract_function(
83+
backend,
84+
args.include_keys,
85+
&common_reference_string,
86+
func,
87+
)
88+
.map_err(CliError::ProofSystemCompilerError)
8089
})?;
8190

8291
Ok(PreprocessedContract {
@@ -98,8 +107,9 @@ pub(crate) fn run<B: Backend>(
98107
update_common_reference_string(backend, &common_reference_string, &program.circuit)
99108
.map_err(CliError::CommonReferenceStringError)?;
100109

101-
let preprocessed_program = preprocess_program(backend, &common_reference_string, program)
102-
.map_err(CliError::ProofSystemCompilerError)?;
110+
let preprocessed_program =
111+
preprocess_program(backend, args.include_keys, &common_reference_string, program)
112+
.map_err(CliError::ProofSystemCompilerError)?;
103113
save_program_to_file(&preprocessed_program, &args.circuit_name, circuit_dir);
104114
}
105115

crates/nargo_cli/src/cli/prove_cmd.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(crate) fn prove_with_path<B: Backend, P: AsRef<Path>>(
107107
let common_reference_string =
108108
update_common_reference_string(backend, &common_reference_string, &program.circuit)
109109
.map_err(CliError::CommonReferenceStringError)?;
110-
let program = preprocess_program(backend, &common_reference_string, program)
110+
let program = preprocess_program(backend, true, &common_reference_string, program)
111111
.map_err(CliError::ProofSystemCompilerError)?;
112112
(common_reference_string, program)
113113
}
@@ -137,12 +137,17 @@ pub(crate) fn prove_with_path<B: Backend, P: AsRef<Path>>(
137137
Format::Toml,
138138
)?;
139139

140+
let proving_key =
141+
proving_key.expect("Proving key should exist as `true` is passed to `preprocess_program`");
142+
140143
let proof =
141144
prove_execution(backend, &common_reference_string, &bytecode, solved_witness, &proving_key)
142145
.map_err(CliError::ProofSystemCompilerError)?;
143146

144147
if check_proof {
145148
let public_inputs = public_abi.encode(&public_inputs, return_value)?;
149+
let verification_key = verification_key
150+
.expect("Verification key should exist as `true` is passed to `preprocess_program`");
146151
let valid_proof = verify_proof(
147152
backend,
148153
&common_reference_string,

crates/nargo_cli/src/cli/verify_cmd.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn verify_with_path<B: Backend, P: AsRef<Path>>(
8787
let common_reference_string =
8888
update_common_reference_string(backend, &common_reference_string, &program.circuit)
8989
.map_err(CliError::CommonReferenceStringError)?;
90-
let program = preprocess_program(backend, &common_reference_string, program)
90+
let program = preprocess_program(backend, true, &common_reference_string, program)
9191
.map_err(CliError::ProofSystemCompilerError)?;
9292
(common_reference_string, program)
9393
}
@@ -105,6 +105,8 @@ fn verify_with_path<B: Backend, P: AsRef<Path>>(
105105
let public_inputs = public_abi.encode(&public_inputs_map, return_value)?;
106106
let proof = load_hex_data(&proof_path)?;
107107

108+
let verification_key = verification_key
109+
.expect("Verification key should exist as `true` is passed to `preprocess_program`");
108110
let valid_proof = verify_proof(
109111
backend,
110112
&common_reference_string,

0 commit comments

Comments
 (0)