diff --git a/crates/nargo/Cargo.toml b/crates/nargo/Cargo.toml index 25c73a3c025..cefc7ffb154 100644 --- a/crates/nargo/Cargo.toml +++ b/crates/nargo/Cargo.toml @@ -15,10 +15,10 @@ acvm.workspace = true fm.workspace = true noirc_abi.workspace = true noirc_driver.workspace = true +noirc_errors.workspace = true noirc_frontend.workspace = true noirc_printable_type.workspace = true iter-extended.workspace = true serde.workspace = true thiserror.workspace = true -noirc_errors.workspace = true base64.workspace = true 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/debug.rs b/crates/nargo/src/artifacts/debug.rs index b6eefd17189..78f1821b6d9 100644 --- a/crates/nargo/src/artifacts/debug.rs +++ b/crates/nargo/src/artifacts/debug.rs @@ -1,3 +1,4 @@ +use noirc_errors::debug_info::DebugInfo; use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, BTreeSet}, @@ -5,7 +6,6 @@ use std::{ }; use fm::FileId; -use noirc_errors::debug_info::DebugInfo; use noirc_frontend::hir::Context; /// For a given file, we store the source code and the path to the file 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 10a0aebe019..bf500e8f166 100644 --- a/crates/nargo/src/ops/mod.rs +++ b/crates/nargo/src/ops/mod.rs @@ -1,6 +1,5 @@ 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::test::{run_test, TestStatus}; pub use self::verify::verify_proof; @@ -8,7 +7,6 @@ pub use self::verify::verify_proof; mod codegen_verifier; mod execute; mod foreign_calls; -mod preprocess; mod prove; mod test; mod verify; diff --git a/crates/nargo/src/ops/preprocess.rs b/crates/nargo/src/ops/preprocess.rs deleted file mode 100644 index 0ee4e2590f9..00000000000 --- a/crates/nargo/src/ops/preprocess.rs +++ /dev/null @@ -1,70 +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<(PreprocessedContractFunction, DebugInfo), B::Error> { - // 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, - }, - func.debug, - )) -} diff --git a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs index 7662eaa8401..8f6f613897b 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::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; 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 { @@ -77,26 +78,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 0ccf8765975..878d27362a7 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -1,6 +1,8 @@ use acvm::{acir::circuit::Circuit, compiler::AcirTransformationMap, Backend}; use iter_extended::try_vecmap; +use nargo::artifacts::contract::PreprocessedContractFunction; use nargo::artifacts::debug::DebugArtifact; +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::program::save_debug_artifact_to_file; @@ -94,13 +94,17 @@ 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, + }, + func.debug, + )) })?; let (preprocessed_contract_functions, debug_infos): (Vec<_>, Vec<_>) = @@ -138,13 +142,16 @@ pub(crate) fn run( update_common_reference_string(backend, &common_reference_string, &program.circuit) .map_err(CliError::CommonReferenceStringError)?; - let (preprocessed_program, debug_info) = - 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); if args.output_debug { - let debug_artifact = DebugArtifact::new(vec![debug_info], &context); + let debug_artifact = DebugArtifact::new(vec![program.debug], &context); let circuit_name: String = (&package.name).into(); save_debug_artifact_to_file(&debug_artifact, &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 e4766828a5b..2f647123629 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::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; 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 { @@ -92,29 +93,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, _) = @@ -135,17 +140,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 c2f21a2123b..c13bb9cdafa 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::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; 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 { @@ -81,27 +79,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(); @@ -111,8 +113,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, diff --git a/crates/nargo_cli/tests/execution_success/workspace/target/a.json b/crates/nargo_cli/tests/execution_success/workspace/target/a.json deleted file mode 100644 index 1c9071208c7..00000000000 --- a/crates/nargo_cli/tests/execution_success/workspace/target/a.json +++ /dev/null @@ -1 +0,0 @@ -{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"},{"name":"y","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"x":[1],"y":[2]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/82TQQ4DIQhFcWacZc8CoiPuepWaOvc/QpPGJmTqrpiUDcTFg//BHQA8fMfa871n/C3IKRbjEWPLoRHTA0OpkjCmeggJJUnPIMxNouRSS8ZCkRudqfDZYYsha7XT+Ga5gZfO2EvLmfW826BeBjexT9AElz5XH2+DN9PmM5a0TeB6sDv+Wbq9/Y5QIf/aU6dm/NT6E70AIbviMnEFAAA=","proving_key":null,"verification_key":null} \ No newline at end of file diff --git a/crates/nargo_cli/tests/execution_success/workspace/target/b.json b/crates/nargo_cli/tests/execution_success/workspace/target/b.json deleted file mode 100644 index 5b013404f52..00000000000 --- a/crates/nargo_cli/tests/execution_success/workspace/target/b.json +++ /dev/null @@ -1 +0,0 @@ -{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"},{"name":"y","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"x":[1],"y":[2]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/7WTMRLEIAhFMYkp9ywgGrHbq6yz5v5H2JkdCyaxC9LgWDw+H9gBwMM91p7fPeOzIKdYjEeMLYdGTB8MpUrCmOohJJQkfYMwN4mSSy0ZC0VudKbCZ4cthqzVrsc/yw28dMZeWmrWerfBexnsxD6hJ7jUufr4GvyZFp8xpG0C14Pd8s/q29vPCBXypvmpDx7sD8opnfqIfsM1RNtxBQAA","proving_key":null,"verification_key":null} \ No newline at end of file