From 1f9d08cc703c8b79ca2a71ff2d0a0729b975ea06 Mon Sep 17 00:00:00 2001 From: Tom French Date: Fri, 11 Aug 2023 11:42:35 +0100 Subject: [PATCH] chore: remove keys from preprocessed artifacts --- crates/nargo/src/artifacts/contract.rs | 3 - crates/nargo/src/artifacts/program.rs | 3 - crates/nargo/src/ops/mod.rs | 2 - crates/nargo/src/ops/preprocess.rs | 67 ------------------- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 45 +++++++------ crates/nargo_cli/src/cli/compile_cmd.rs | 28 ++++---- crates/nargo_cli/src/cli/prove_cmd.rs | 54 +++++++-------- crates/nargo_cli/src/cli/verify_cmd.rs | 56 ++++++++-------- 8 files changed, 96 insertions(+), 162 deletions(-) delete mode 100644 crates/nargo/src/ops/preprocess.rs diff --git a/crates/nargo/src/artifacts/contract.rs b/crates/nargo/src/artifacts/contract.rs index a718eac9796..4db7d95731e 100644 --- a/crates/nargo/src/artifacts/contract.rs +++ b/crates/nargo/src/artifacts/contract.rs @@ -37,7 +37,4 @@ pub struct PreprocessedContractFunction { deserialize_with = "super::deserialize_circuit" )] pub bytecode: Circuit, - - 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 6ca49b35dd9..be01b7bdec1 100644 --- a/crates/nargo/src/artifacts/program.rs +++ b/crates/nargo/src/artifacts/program.rs @@ -17,7 +17,4 @@ pub struct PreprocessedProgram { deserialize_with = "super::deserialize_circuit" )] pub bytecode: Circuit, - - pub proving_key: Option>, - pub verification_key: Option>, } diff --git a/crates/nargo/src/ops/mod.rs b/crates/nargo/src/ops/mod.rs index 6d55e5b0dad..06d1cb01c38 100644 --- a/crates/nargo/src/ops/mod.rs +++ b/crates/nargo/src/ops/mod.rs @@ -1,12 +1,10 @@ pub use self::codegen_verifier::codegen_verifier; pub use self::execute::execute_circuit; -pub use self::preprocess::{preprocess_contract_function, preprocess_program}; pub use self::prove::prove_execution; pub use self::verify::verify_proof; mod codegen_verifier; mod execute; mod foreign_calls; -mod preprocess; mod prove; mod verify; diff --git a/crates/nargo/src/ops/preprocess.rs b/crates/nargo/src/ops/preprocess.rs deleted file mode 100644 index d07da256ede..00000000000 --- a/crates/nargo/src/ops/preprocess.rs +++ /dev/null @@ -1,67 +0,0 @@ -use acvm::ProofSystemCompiler; -use noirc_driver::{CompiledProgram, ContractFunction}; -use noirc_errors::debug_info::DebugInfo; - -use crate::artifacts::{contract::PreprocessedContractFunction, program::PreprocessedProgram}; - -// TODO(#1388): pull this from backend. -const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; - -pub fn preprocess_program( - backend: &B, - include_keys: bool, - common_reference_string: &[u8], - compiled_program: CompiledProgram, -) -> Result<(PreprocessedProgram, DebugInfo), B::Error> { - // 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) = 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), - abi: compiled_program.abi, - bytecode: optimized_bytecode, - proving_key, - verification_key, - }, - compiled_program.debug, - )) -} - -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) = 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, - function_type: func.function_type, - is_internal: func.is_internal, - abi: func.abi, - - bytecode: optimized_bytecode, - proving_key, - verification_key, - }) -} diff --git a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs index bc7e1c80c0f..6bfd7e35e7f 100644 --- a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -16,14 +16,15 @@ use super::{ use crate::errors::CliError; use acvm::Backend; use clap::Args; -use nargo::{ - ops::{codegen_verifier, preprocess_program}, - package::Package, -}; +use nargo::artifacts::program::PreprocessedProgram; +use nargo::{ops::codegen_verifier, package::Package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_driver::CompileOptions; use noirc_frontend::graph::CrateName; +// TODO(#1388): pull this from backend. +const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; + /// Generates a Solidity verifier smart contract for the program #[derive(Debug, Clone, Args)] pub(crate) struct CodegenVerifierCommand { @@ -70,26 +71,30 @@ fn smart_contract_for_package( circuit_build_path: PathBuf, compile_options: &CompileOptions, ) -> Result> { - let common_reference_string = read_cached_common_reference_string(); - let (common_reference_string, preprocessed_program) = if circuit_build_path.exists() { - let program = read_program_from_file(circuit_build_path)?; - let common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.bytecode) - .map_err(CliError::CommonReferenceStringError)?; - (common_reference_string, program) + let preprocessed_program = if circuit_build_path.exists() { + read_program_from_file(circuit_build_path)? } else { let (_, program) = compile_package(backend, package, compile_options)?; - let common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.circuit) - .map_err(CliError::CommonReferenceStringError)?; - let (program, _) = preprocess_program(backend, true, &common_reference_string, program) - .map_err(CliError::ProofSystemCompilerError)?; - (common_reference_string, program) + + PreprocessedProgram { + backend: String::from(BACKEND_IDENTIFIER), + abi: program.abi, + bytecode: program.circuit, + } }; - let verification_key = preprocessed_program - .verification_key - .expect("Verification key should exist as `true` is passed to `preprocess_program`"); + let common_reference_string = read_cached_common_reference_string(); + let common_reference_string = update_common_reference_string( + backend, + &common_reference_string, + &preprocessed_program.bytecode, + ) + .map_err(CliError::CommonReferenceStringError)?; + + let (_, verification_key) = backend + .preprocess(&common_reference_string, &preprocessed_program.bytecode) + .map_err(CliError::ProofSystemCompilerError)?; + let smart_contract_string = codegen_verifier( backend, &common_reference_string, diff --git a/crates/nargo_cli/src/cli/compile_cmd.rs b/crates/nargo_cli/src/cli/compile_cmd.rs index fad9783e654..246f9b5416a 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -2,6 +2,8 @@ use acvm::acir::circuit::OpcodeLabel; use acvm::{acir::circuit::Circuit, Backend}; use iter_extended::try_vecmap; use iter_extended::vecmap; +use nargo::artifacts::contract::PreprocessedContractFunction; +use nargo::artifacts::program::PreprocessedProgram; use nargo::package::Package; use nargo::prepare_package; use nargo::{artifacts::contract::PreprocessedContract, NargoError}; @@ -15,8 +17,6 @@ use noirc_frontend::hir::Context; use clap::Args; -use nargo::ops::{preprocess_contract_function, preprocess_program}; - use crate::errors::{CliError, CompileError}; use super::fs::{ @@ -80,13 +80,14 @@ pub(crate) fn run( ) .map_err(CliError::CommonReferenceStringError)?; - preprocess_contract_function( - backend, - args.include_keys, - &common_reference_string, - func, - ) - .map_err(CliError::ProofSystemCompilerError) + Ok::<_, CliError>(PreprocessedContractFunction { + name: func.name, + function_type: func.function_type, + is_internal: func.is_internal, + abi: func.abi, + + bytecode: func.bytecode, + }) })?; Ok(PreprocessedContract { @@ -109,9 +110,12 @@ pub(crate) fn run( update_common_reference_string(backend, &common_reference_string, &program.circuit) .map_err(CliError::CommonReferenceStringError)?; - let (preprocessed_program, _) = - preprocess_program(backend, args.include_keys, &common_reference_string, program) - .map_err(CliError::ProofSystemCompilerError)?; + let preprocessed_program = PreprocessedProgram { + backend: String::from(BACKEND_IDENTIFIER), + abi: program.abi, + bytecode: program.circuit, + }; + save_program_to_file(&preprocessed_program, &package.name, &circuit_dir); } } diff --git a/crates/nargo_cli/src/cli/prove_cmd.rs b/crates/nargo_cli/src/cli/prove_cmd.rs index 018e150f89f..aaf0737b362 100644 --- a/crates/nargo_cli/src/cli/prove_cmd.rs +++ b/crates/nargo_cli/src/cli/prove_cmd.rs @@ -4,7 +4,7 @@ use acvm::Backend; use clap::Args; use nargo::artifacts::program::PreprocessedProgram; use nargo::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE}; -use nargo::ops::{preprocess_program, prove_execution, verify_proof}; +use nargo::ops::{prove_execution, verify_proof}; use nargo::package::Package; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_abi::input_parser::Format; @@ -12,11 +12,9 @@ use noirc_driver::CompileOptions; use noirc_frontend::graph::CrateName; use super::compile_cmd::compile_package; +use super::fs::common_reference_string::update_common_reference_string; use super::fs::{ - common_reference_string::{ - read_cached_common_reference_string, update_common_reference_string, - write_cached_common_reference_string, - }, + common_reference_string::read_cached_common_reference_string, inputs::{read_inputs_from_file, write_inputs_to_file}, program::read_program_from_file, proof::save_proof_to_dir, @@ -24,6 +22,9 @@ use super::fs::{ use super::NargoConfig; use crate::{cli::execute_cmd::execute_program, errors::CliError}; +// TODO(#1388): pull this from backend. +const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; + /// Create proof for this program. The proof is returned as a hex encoded string. #[derive(Debug, Clone, Args)] pub(crate) struct ProveCommand { @@ -85,29 +86,33 @@ pub(crate) fn prove_package( check_proof: bool, compile_options: &CompileOptions, ) -> Result<(), CliError> { - let common_reference_string = read_cached_common_reference_string(); - - let (common_reference_string, preprocessed_program, debug_data) = if circuit_build_path.exists() - { + let (preprocessed_program, debug_data) = if circuit_build_path.exists() { let program = read_program_from_file(circuit_build_path)?; - let common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.bytecode) - .map_err(CliError::CommonReferenceStringError)?; - (common_reference_string, program, None) + + (program, None) } else { let (context, program) = compile_package(backend, package, compile_options)?; - let common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.circuit) - .map_err(CliError::CommonReferenceStringError)?; - let (program, debug) = preprocess_program(backend, true, &common_reference_string, program) - .map_err(CliError::ProofSystemCompilerError)?; - (common_reference_string, program, Some((debug, context))) + let preprocessed_program = PreprocessedProgram { + backend: String::from(BACKEND_IDENTIFIER), + abi: program.abi, + bytecode: program.circuit, + }; + (preprocessed_program, Some((program.debug, context))) }; - write_cached_common_reference_string(&common_reference_string); + let common_reference_string = read_cached_common_reference_string(); + let common_reference_string = update_common_reference_string( + backend, + &common_reference_string, + &preprocessed_program.bytecode, + ) + .map_err(CliError::CommonReferenceStringError)?; + + let (proving_key, verification_key) = backend + .preprocess(&common_reference_string, &preprocessed_program.bytecode) + .map_err(CliError::ProofSystemCompilerError)?; - let PreprocessedProgram { abi, bytecode, proving_key, verification_key, .. } = - preprocessed_program; + let PreprocessedProgram { abi, bytecode, .. } = preprocessed_program; // Parse the initial witness values from Prover.toml let (inputs_map, _) = @@ -128,17 +133,12 @@ pub(crate) fn prove_package( 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 5af28b4e25b..67ae5584322 100644 --- a/crates/nargo_cli/src/cli/verify_cmd.rs +++ b/crates/nargo_cli/src/cli/verify_cmd.rs @@ -1,22 +1,17 @@ +use super::fs::common_reference_string::{ + read_cached_common_reference_string, update_common_reference_string, +}; use super::NargoConfig; use super::{ compile_cmd::compile_package, - fs::{ - common_reference_string::{ - read_cached_common_reference_string, update_common_reference_string, - write_cached_common_reference_string, - }, - inputs::read_inputs_from_file, - load_hex_data, - program::read_program_from_file, - }, + fs::{inputs::read_inputs_from_file, load_hex_data, program::read_program_from_file}, }; use crate::errors::CliError; use acvm::Backend; use clap::Args; use nargo::constants::{PROOF_EXT, VERIFIER_INPUT_FILE}; -use nargo::ops::{preprocess_program, verify_proof}; +use nargo::ops::verify_proof; use nargo::{artifacts::program::PreprocessedProgram, package::Package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_abi::input_parser::Format; @@ -24,6 +19,9 @@ use noirc_driver::CompileOptions; use noirc_frontend::graph::CrateName; use std::path::{Path, PathBuf}; +// TODO(#1388): pull this from backend. +const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; + /// Given a proof and a program, verify whether the proof is valid #[derive(Debug, Clone, Args)] pub(crate) struct VerifyCommand { @@ -74,27 +72,31 @@ fn verify_package( verifier_name: &str, compile_options: &CompileOptions, ) -> Result<(), CliError> { - let common_reference_string = read_cached_common_reference_string(); - - let (common_reference_string, preprocessed_program) = if circuit_build_path.exists() { - let program = read_program_from_file(circuit_build_path)?; - let common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.bytecode) - .map_err(CliError::CommonReferenceStringError)?; - (common_reference_string, program) + let preprocessed_program = if circuit_build_path.exists() { + read_program_from_file(circuit_build_path)? } else { let (_, program) = compile_package(backend, package, compile_options)?; - let common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.circuit) - .map_err(CliError::CommonReferenceStringError)?; - let (program, _) = preprocess_program(backend, true, &common_reference_string, program) - .map_err(CliError::ProofSystemCompilerError)?; - (common_reference_string, program) + + PreprocessedProgram { + backend: String::from(BACKEND_IDENTIFIER), + abi: program.abi, + bytecode: program.circuit, + } }; - write_cached_common_reference_string(&common_reference_string); + let common_reference_string = read_cached_common_reference_string(); + let common_reference_string = update_common_reference_string( + backend, + &common_reference_string, + &preprocessed_program.bytecode, + ) + .map_err(CliError::CommonReferenceStringError)?; + + let (_, verification_key) = backend + .preprocess(&common_reference_string, &preprocessed_program.bytecode) + .map_err(CliError::ProofSystemCompilerError)?; - let PreprocessedProgram { abi, bytecode, verification_key, .. } = preprocessed_program; + let PreprocessedProgram { abi, bytecode, .. } = preprocessed_program; // Load public inputs (if any) from `verifier_name`. let public_abi = abi.public_abi(); @@ -104,8 +106,6 @@ fn verify_package( 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,