From be36c1e816e685f4882538eb3dec4b8e81f61bc2 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Fri, 7 Jul 2023 11:27:16 +0100 Subject: [PATCH] chore(nargo)!: Make proving and verification keys optional (#1880) Make proving and verification keys optional --- crates/nargo/src/artifacts/contract.rs | 4 ++-- crates/nargo/src/artifacts/program.rs | 4 ++-- crates/nargo/src/ops/preprocess.rs | 21 +++++++++++++++---- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 7 +++++-- crates/nargo_cli/src/cli/compile_cmd.rs | 18 ++++++++++++---- crates/nargo_cli/src/cli/prove_cmd.rs | 7 ++++++- crates/nargo_cli/src/cli/verify_cmd.rs | 4 +++- 7 files changed, 49 insertions(+), 16 deletions(-) diff --git a/crates/nargo/src/artifacts/contract.rs b/crates/nargo/src/artifacts/contract.rs index 95f1ce9576d..4174207692f 100644 --- a/crates/nargo/src/artifacts/contract.rs +++ b/crates/nargo/src/artifacts/contract.rs @@ -36,6 +36,6 @@ pub struct PreprocessedContractFunction { )] pub bytecode: Circuit, - pub proving_key: Vec, - pub verification_key: Vec, + pub proving_key: Option>, + pub verification_key: Option>, } diff --git a/crates/nargo/src/artifacts/program.rs b/crates/nargo/src/artifacts/program.rs index 288a5dba99b..6ca49b35dd9 100644 --- a/crates/nargo/src/artifacts/program.rs +++ b/crates/nargo/src/artifacts/program.rs @@ -18,6 +18,6 @@ pub struct PreprocessedProgram { )] pub bytecode: Circuit, - pub proving_key: Vec, - pub verification_key: Vec, + pub proving_key: Option>, + pub verification_key: Option>, } diff --git a/crates/nargo/src/ops/preprocess.rs b/crates/nargo/src/ops/preprocess.rs index b545980b963..90364bec603 100644 --- a/crates/nargo/src/ops/preprocess.rs +++ b/crates/nargo/src/ops/preprocess.rs @@ -8,14 +8,21 @@ const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; pub fn preprocess_program( backend: &B, + include_keys: bool, common_reference_string: &[u8], compiled_program: CompiledProgram, ) -> Result { // TODO: currently `compiled_program`'s bytecode is already optimized for the backend. // In future we'll need to apply those optimizations here. let optimized_bytecode = compiled_program.circuit; - let (proving_key, verification_key) = - backend.preprocess(common_reference_string, &optimized_bytecode)?; + + let (proving_key, verification_key) = if include_keys { + let (proving_key, verification_key) = + backend.preprocess(common_reference_string, &optimized_bytecode)?; + (Some(proving_key), Some(verification_key)) + } else { + (None, None) + }; Ok(PreprocessedProgram { backend: String::from(BACKEND_IDENTIFIER), @@ -28,14 +35,20 @@ pub fn preprocess_program( pub fn preprocess_contract_function( backend: &B, + include_keys: bool, common_reference_string: &[u8], func: ContractFunction, ) -> Result { // TODO: currently `func`'s bytecode is already optimized for the backend. // In future we'll need to apply those optimizations here. let optimized_bytecode = func.bytecode; - let (proving_key, verification_key) = - backend.preprocess(common_reference_string, &optimized_bytecode)?; + let (proving_key, verification_key) = if include_keys { + let (proving_key, verification_key) = + backend.preprocess(common_reference_string, &optimized_bytecode)?; + (Some(proving_key), Some(verification_key)) + } else { + (None, None) + }; Ok(PreprocessedContractFunction { name: func.name, diff --git a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs index b65d64bb917..ae6fa411c7c 100644 --- a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -56,14 +56,17 @@ pub(crate) fn run( let common_reference_string = update_common_reference_string(backend, &common_reference_string, &program.circuit) .map_err(CliError::CommonReferenceStringError)?; - let program = preprocess_program(backend, &common_reference_string, program) + let program = preprocess_program(backend, true, &common_reference_string, program) .map_err(CliError::ProofSystemCompilerError)?; (common_reference_string, program) } }; + let verification_key = preprocessed_program + .verification_key + .expect("Verification key should exist as `true` is passed to `preprocess_program`"); let smart_contract_string = - codegen_verifier(backend, &common_reference_string, &preprocessed_program.verification_key) + codegen_verifier(backend, &common_reference_string, &verification_key) .map_err(CliError::SmartContractError)?; write_cached_common_reference_string(&common_reference_string); diff --git a/crates/nargo_cli/src/cli/compile_cmd.rs b/crates/nargo_cli/src/cli/compile_cmd.rs index 64ee7bd44ee..aec124770e5 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -32,6 +32,10 @@ pub(crate) struct CompileCommand { /// The name of the ACIR file circuit_name: String, + /// Include Proving and Verification keys in the build artifacts. + #[arg(long)] + include_keys: bool, + /// Compile each contract function used within the program #[arg(short, long)] contracts: bool, @@ -75,8 +79,13 @@ pub(crate) fn run( ) .map_err(CliError::CommonReferenceStringError)?; - preprocess_contract_function(backend, &common_reference_string, func) - .map_err(CliError::ProofSystemCompilerError) + preprocess_contract_function( + backend, + args.include_keys, + &common_reference_string, + func, + ) + .map_err(CliError::ProofSystemCompilerError) })?; Ok(PreprocessedContract { @@ -98,8 +107,9 @@ pub(crate) fn run( update_common_reference_string(backend, &common_reference_string, &program.circuit) .map_err(CliError::CommonReferenceStringError)?; - let preprocessed_program = preprocess_program(backend, &common_reference_string, program) - .map_err(CliError::ProofSystemCompilerError)?; + let preprocessed_program = + preprocess_program(backend, args.include_keys, &common_reference_string, program) + .map_err(CliError::ProofSystemCompilerError)?; save_program_to_file(&preprocessed_program, &args.circuit_name, circuit_dir); } diff --git a/crates/nargo_cli/src/cli/prove_cmd.rs b/crates/nargo_cli/src/cli/prove_cmd.rs index 44f3bf62484..0200b417396 100644 --- a/crates/nargo_cli/src/cli/prove_cmd.rs +++ b/crates/nargo_cli/src/cli/prove_cmd.rs @@ -107,7 +107,7 @@ pub(crate) fn prove_with_path>( let common_reference_string = update_common_reference_string(backend, &common_reference_string, &program.circuit) .map_err(CliError::CommonReferenceStringError)?; - let program = preprocess_program(backend, &common_reference_string, program) + let program = preprocess_program(backend, true, &common_reference_string, program) .map_err(CliError::ProofSystemCompilerError)?; (common_reference_string, program) } @@ -137,12 +137,17 @@ pub(crate) fn prove_with_path>( Format::Toml, )?; + let proving_key = + proving_key.expect("Proving key should exist as `true` is passed to `preprocess_program`"); + let proof = prove_execution(backend, &common_reference_string, &bytecode, solved_witness, &proving_key) .map_err(CliError::ProofSystemCompilerError)?; if check_proof { let public_inputs = public_abi.encode(&public_inputs, return_value)?; + let verification_key = verification_key + .expect("Verification key should exist as `true` is passed to `preprocess_program`"); let valid_proof = verify_proof( backend, &common_reference_string, diff --git a/crates/nargo_cli/src/cli/verify_cmd.rs b/crates/nargo_cli/src/cli/verify_cmd.rs index e326aafbc52..c962e9fd081 100644 --- a/crates/nargo_cli/src/cli/verify_cmd.rs +++ b/crates/nargo_cli/src/cli/verify_cmd.rs @@ -87,7 +87,7 @@ fn verify_with_path>( let common_reference_string = update_common_reference_string(backend, &common_reference_string, &program.circuit) .map_err(CliError::CommonReferenceStringError)?; - let program = preprocess_program(backend, &common_reference_string, program) + let program = preprocess_program(backend, true, &common_reference_string, program) .map_err(CliError::ProofSystemCompilerError)?; (common_reference_string, program) } @@ -105,6 +105,8 @@ fn verify_with_path>( let public_inputs = public_abi.encode(&public_inputs_map, return_value)?; let proof = load_hex_data(&proof_path)?; + let verification_key = verification_key + .expect("Verification key should exist as `true` is passed to `preprocess_program`"); let valid_proof = verify_proof( backend, &common_reference_string,