From 7afc69ce20e572858da2d520cc58b051adc033ba Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 11 Sep 2023 12:53:04 +0100 Subject: [PATCH] chore(nargo): be explicit in how to handle all package types --- crates/nargo_cli/src/cli/check_cmd.rs | 50 ++++++++++--------- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 5 ++ crates/nargo_cli/src/cli/compile_cmd.rs | 35 ++++++++----- crates/nargo_cli/src/cli/execute_cmd.rs | 23 +++++---- crates/nargo_cli/src/cli/info_cmd.rs | 42 +++++++++------- crates/nargo_cli/src/cli/prove_cmd.rs | 33 +++++++----- crates/nargo_cli/src/cli/verify_cmd.rs | 28 +++++++---- 7 files changed, 128 insertions(+), 88 deletions(-) diff --git a/crates/nargo_cli/src/cli/check_cmd.rs b/crates/nargo_cli/src/cli/check_cmd.rs index 37e28d4efa3..68db6b0f345 100644 --- a/crates/nargo_cli/src/cli/check_cmd.rs +++ b/crates/nargo_cli/src/cli/check_cmd.rs @@ -3,6 +3,7 @@ use crate::errors::{CliError, CompileError}; use clap::Args; use iter_extended::btree_map; +use nargo::package::PackageType; use nargo::{package::Package, prepare_package}; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_abi::{AbiParameter, AbiType, MAIN_RETURN_NAME}; @@ -52,32 +53,33 @@ fn check_package(package: &Package, compile_options: &CompileOptions) -> Result< let (mut context, crate_id) = prepare_package(package); check_crate_and_report_errors(&mut context, crate_id, compile_options.deny_warnings)?; - if package.is_library() || package.is_contract() { - // Libraries do not have ABIs while contracts have many, so we cannot generate a `Prover.toml` file. - Ok(()) - } else { - // XXX: We can have a --overwrite flag to determine if you want to overwrite the Prover/Verifier.toml files - if let Some((parameters, return_type)) = compute_function_abi(&context, &crate_id) { - let path_to_prover_input = package.prover_input_path(); - let path_to_verifier_input = package.verifier_input_path(); - - // If they are not available, then create them and populate them based on the ABI - if !path_to_prover_input.exists() { - let prover_toml = create_input_toml_template(parameters.clone(), None); - write_to_file(prover_toml.as_bytes(), &path_to_prover_input); - } - if !path_to_verifier_input.exists() { - let public_inputs = - parameters.into_iter().filter(|param| param.is_public()).collect(); - - let verifier_toml = create_input_toml_template(public_inputs, return_type); - write_to_file(verifier_toml.as_bytes(), &path_to_verifier_input); + match package.package_type { + PackageType::Binary => { + if let Some((parameters, return_type)) = compute_function_abi(&context, &crate_id) { + let path_to_prover_input = package.prover_input_path(); + let path_to_verifier_input = package.verifier_input_path(); + + // If they are not available, then create them and populate them based on the ABI + if !path_to_prover_input.exists() { + let prover_toml = create_input_toml_template(parameters.clone(), None); + write_to_file(prover_toml.as_bytes(), &path_to_prover_input); + } + if !path_to_verifier_input.exists() { + let public_inputs = + parameters.into_iter().filter(|param| param.is_public()).collect(); + + let verifier_toml = create_input_toml_template(public_inputs, return_type); + write_to_file(verifier_toml.as_bytes(), &path_to_verifier_input); + } + + Ok(()) + } else { + Err(CompileError::MissingMainFunction(package.name.clone())) } - - Ok(()) - } else { - Err(CompileError::MissingMainFunction(package.name.clone())) } + + // Libraries do not have ABIs while contracts have many, so we cannot generate a `Prover.toml` file. + PackageType::Contract | PackageType::Library => Ok(()), } } diff --git a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs index 36e660e02e0..7b233d75a83 100644 --- a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -48,6 +48,11 @@ pub(crate) fn run( let (np_language, is_opcode_supported) = backend.get_backend_info()?; for package in &workspace { + if !package.is_binary() { + // Contract and library packages cannot generate verifiers. + continue; + } + let circuit_build_path = workspace.package_build_path(package); let smart_contract_string = smart_contract_for_package( diff --git a/crates/nargo_cli/src/cli/compile_cmd.rs b/crates/nargo_cli/src/cli/compile_cmd.rs index 67cf5b49788..3b5c9da5099 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -9,6 +9,7 @@ use nargo::artifacts::contract::PreprocessedContractFunction; use nargo::artifacts::debug::DebugArtifact; use nargo::artifacts::program::PreprocessedProgram; use nargo::package::Package; +use nargo::package::PackageType; use nargo::prepare_package; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_driver::{ @@ -67,19 +68,27 @@ pub(crate) fn run( let (np_language, is_opcode_supported) = backend.get_backend_info()?; for package in &workspace { - // If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'. - if package.is_contract() { - let (file_manager, contracts) = compile_contracts( - package, - &args.compile_options, - np_language, - &is_opcode_supported, - )?; - save_contracts(&file_manager, contracts, package, &circuit_dir, args.output_debug); - } else { - let (file_manager, program) = - compile_package(package, &args.compile_options, np_language, &is_opcode_supported)?; - save_program(&file_manager, program, package, &circuit_dir, args.output_debug); + match package.package_type { + PackageType::Binary => { + let (file_manager, program) = compile_package( + package, + &args.compile_options, + np_language, + &is_opcode_supported, + )?; + save_program(&file_manager, program, package, &circuit_dir, args.output_debug); + } + PackageType::Contract => { + // If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'. + let (file_manager, contracts) = compile_contracts( + package, + &args.compile_options, + np_language, + &is_opcode_supported, + )?; + save_contracts(&file_manager, contracts, package, &circuit_dir, args.output_debug); + } + PackageType::Library => (), } } diff --git a/crates/nargo_cli/src/cli/execute_cmd.rs b/crates/nargo_cli/src/cli/execute_cmd.rs index ccbc5d03776..53655d58c79 100644 --- a/crates/nargo_cli/src/cli/execute_cmd.rs +++ b/crates/nargo_cli/src/cli/execute_cmd.rs @@ -6,7 +6,7 @@ use clap::Args; use fm::FileManager; use nargo::constants::PROVER_INPUT_FILE; use nargo::errors::{ExecutionError, NargoError}; -use nargo::package::Package; +use nargo::package::{Package, PackageType}; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_abi::input_parser::{Format, InputValue}; use noirc_abi::{Abi, InputMap}; @@ -56,14 +56,19 @@ pub(crate) fn run( let (np_language, is_opcode_supported) = backend.get_backend_info()?; for package in &workspace { - let (return_value, solved_witness) = execute_package( - backend, - package, - &args.prover_name, - &args.compile_options, - np_language, - &is_opcode_supported, - )?; + let (return_value, solved_witness) = match package.package_type { + PackageType::Binary => execute_package( + backend, + package, + &args.prover_name, + &args.compile_options, + np_language, + &is_opcode_supported, + )?, + + // Nargo does not support executing these package types + PackageType::Contract | PackageType::Library => continue, + }; println!("[{}] Circuit witness successfully solved", package.name); if let Some(return_value) = return_value { diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index 83e54fc109c..7bfeee57c55 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -3,7 +3,7 @@ use acvm::Language; use acvm_backend_barretenberg::BackendError; use clap::Args; use iter_extended::{try_vecmap, vecmap}; -use nargo::package::Package; +use nargo::package::{Package, PackageType}; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_driver::CompileOptions; use noirc_frontend::graph::CrateName; @@ -53,24 +53,28 @@ pub(crate) fn run( let (np_language, is_opcode_supported) = backend.get_backend_info()?; for package in &workspace { - if package.is_contract() { - let contract_info = count_opcodes_and_gates_in_contracts( - backend, - package, - &args.compile_options, - np_language, - &is_opcode_supported, - )?; - info_report.contracts.extend(contract_info); - } else { - let program_info = count_opcodes_and_gates_in_program( - backend, - package, - &args.compile_options, - np_language, - &is_opcode_supported, - )?; - info_report.programs.push(program_info); + match package.package_type { + PackageType::Binary => { + let program_info = count_opcodes_and_gates_in_program( + backend, + package, + &args.compile_options, + np_language, + &is_opcode_supported, + )?; + info_report.programs.push(program_info); + } + PackageType::Contract => { + let contract_info = count_opcodes_and_gates_in_contracts( + backend, + package, + &args.compile_options, + np_language, + &is_opcode_supported, + )?; + info_report.contracts.extend(contract_info); + } + PackageType::Library => (), // Libraries have no concrete circuit to display information on. } } diff --git a/crates/nargo_cli/src/cli/prove_cmd.rs b/crates/nargo_cli/src/cli/prove_cmd.rs index 417a0872d0a..e1659f4ea67 100644 --- a/crates/nargo_cli/src/cli/prove_cmd.rs +++ b/crates/nargo_cli/src/cli/prove_cmd.rs @@ -5,7 +5,7 @@ use acvm::Language; use clap::Args; use nargo::artifacts::program::PreprocessedProgram; use nargo::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE}; -use nargo::package::Package; +use nargo::package::{Package, PackageType}; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_abi::input_parser::Format; use noirc_driver::CompileOptions; @@ -66,18 +66,25 @@ pub(crate) fn run( for package in &workspace { let circuit_build_path = workspace.package_build_path(package); - prove_package( - backend, - package, - &args.prover_name, - &args.verifier_name, - &proof_dir, - circuit_build_path, - args.verify, - &args.compile_options, - np_language, - &is_opcode_supported, - )?; + match package.package_type { + PackageType::Binary => { + prove_package( + backend, + package, + &args.prover_name, + &args.verifier_name, + &proof_dir, + circuit_build_path, + args.verify, + &args.compile_options, + np_language, + &is_opcode_supported, + )?; + } + + // Nargo does not support creating proofs for these package types + PackageType::Contract | PackageType::Library => (), + } } Ok(()) diff --git a/crates/nargo_cli/src/cli/verify_cmd.rs b/crates/nargo_cli/src/cli/verify_cmd.rs index 595906171a2..d07104a60ba 100644 --- a/crates/nargo_cli/src/cli/verify_cmd.rs +++ b/crates/nargo_cli/src/cli/verify_cmd.rs @@ -9,6 +9,7 @@ use acvm::acir::circuit::Opcode; use acvm::Language; use clap::Args; use nargo::constants::{PROOF_EXT, VERIFIER_INPUT_FILE}; +use nargo::package::PackageType; use nargo::{artifacts::program::PreprocessedProgram, package::Package}; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_abi::input_parser::Format; @@ -56,16 +57,23 @@ pub(crate) fn run( let proof_path = proofs_dir.join(String::from(&package.name)).with_extension(PROOF_EXT); - verify_package( - backend, - package, - &proof_path, - circuit_build_path, - &args.verifier_name, - &args.compile_options, - np_language, - &is_opcode_supported, - )?; + match package.package_type { + PackageType::Binary => { + verify_package( + backend, + package, + &proof_path, + circuit_build_path, + &args.verifier_name, + &args.compile_options, + np_language, + &is_opcode_supported, + )?; + } + + // Nargo does not support verifying proofs for these package types + PackageType::Contract | PackageType::Library => (), + } } Ok(())