Skip to content

Commit

Permalink
chore(nargo): be explicit in how to handle all package types
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Sep 11, 2023
1 parent 6db3bda commit 7afc69c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 88 deletions.
50 changes: 26 additions & 24 deletions crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(()),
}
}

Expand Down
5 changes: 5 additions & 0 deletions crates/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 22 additions & 13 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 => (),
}
}

Expand Down
23 changes: 14 additions & 9 deletions crates/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 23 additions & 19 deletions crates/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
}
}

Expand Down
33 changes: 20 additions & 13 deletions crates/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
Expand Down
28 changes: 18 additions & 10 deletions crates/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 7afc69c

Please sign in to comment.