From ecc986c202201e683a89395bf16dfe48a089e53d Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Tue, 21 Feb 2023 15:42:12 +0000 Subject: [PATCH 1/8] chore: create a consolidated options struct for `Driver` --- Cargo.lock | 33 +++++++++++++++++++++++ crates/nargo/src/cli/check_cmd.rs | 5 +++- crates/nargo/src/cli/compile_cmd.rs | 8 +++++- crates/nargo/src/cli/test_cmd.rs | 17 +++++++----- crates/noirc_driver/Cargo.toml | 1 + crates/noirc_driver/src/lib.rs | 41 ++++++++++++++++++++--------- crates/noirc_driver/src/main.rs | 5 ++-- 7 files changed, 88 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42476cea2a4..1c7078c9c09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1016,6 +1016,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", + "strsim", "syn", ] @@ -1050,6 +1051,37 @@ dependencies = [ "syn", ] +[[package]] +name = "derive_builder" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8" +dependencies = [ + "derive_builder_macro", +] + +[[package]] +name = "derive_builder_core" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "derive_builder_macro" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e" +dependencies = [ + "derive_builder_core", + "syn", +] + [[package]] name = "digest" version = "0.9.0" @@ -2034,6 +2066,7 @@ name = "noirc_driver" version = "0.2.0" dependencies = [ "acvm 0.4.1", + "derive_builder", "dirs 4.0.0", "fm", "noirc_abi", diff --git a/crates/nargo/src/cli/check_cmd.rs b/crates/nargo/src/cli/check_cmd.rs index fd37a841952..26a3a18fcea 100644 --- a/crates/nargo/src/cli/check_cmd.rs +++ b/crates/nargo/src/cli/check_cmd.rs @@ -3,6 +3,7 @@ use acvm::ProofSystemCompiler; use clap::Args; use iter_extended::btree_map; use noirc_abi::{Abi, AbiParameter, AbiType}; +use noirc_driver::CompileOptionsBuilder; use std::{ collections::BTreeMap, path::{Path, PathBuf}, @@ -29,10 +30,12 @@ pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliErro pub fn check_from_path>(p: P, allow_warnings: bool) -> Result<(), CliError> { let backend = crate::backends::ConcreteBackend; + let config = CompileOptionsBuilder::default().allow_warnings(allow_warnings).build().unwrap(); + let mut driver = Resolver::resolve_root_config(p.as_ref(), backend.np_language())?; add_std_lib(&mut driver); - if driver.check_crate(allow_warnings).is_err() { + if driver.check_crate(&config).is_err() { std::process::exit(1); } diff --git a/crates/nargo/src/cli/compile_cmd.rs b/crates/nargo/src/cli/compile_cmd.rs index 70b9d22f73b..44d0fe7f4ae 100644 --- a/crates/nargo/src/cli/compile_cmd.rs +++ b/crates/nargo/src/cli/compile_cmd.rs @@ -1,5 +1,6 @@ use acvm::acir::circuit::Circuit; use acvm::ProofSystemCompiler; +use noirc_driver::CompileOptionsBuilder; use std::path::{Path, PathBuf}; use clap::Args; @@ -59,7 +60,12 @@ pub fn compile_circuit>( let mut driver = Resolver::resolve_root_config(program_dir.as_ref(), backend.np_language())?; add_std_lib(&mut driver); - driver.into_compiled_program(show_ssa, allow_warnings).map_err(|_| std::process::exit(1)) + let config = CompileOptionsBuilder::default() + .allow_warnings(allow_warnings) + .show_ssa(show_ssa) + .build() + .unwrap(); + driver.into_compiled_program(&config).map_err(|_| std::process::exit(1)) } fn preprocess_with_path>( diff --git a/crates/nargo/src/cli/test_cmd.rs b/crates/nargo/src/cli/test_cmd.rs index 3d0aa719691..878a9873488 100644 --- a/crates/nargo/src/cli/test_cmd.rs +++ b/crates/nargo/src/cli/test_cmd.rs @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, io::Write, path::Path}; use acvm::{PartialWitnessGenerator, ProofSystemCompiler}; use clap::Args; -use noirc_driver::Driver; +use noirc_driver::{CompileOptions, CompileOptionsBuilder, Driver}; use noirc_frontend::node_interner::FuncId; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -39,10 +39,16 @@ fn run_tests( ) -> Result<(), CliError> { let backend = crate::backends::ConcreteBackend; + let config = CompileOptionsBuilder::default() + .allow_warnings(allow_warnings) + .show_output(show_output) + .build() + .unwrap(); + let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?; add_std_lib(&mut driver); - if driver.check_crate(allow_warnings).is_err() { + if driver.check_crate(&config).is_err() { std::process::exit(1); } @@ -58,7 +64,7 @@ fn run_tests( writeln!(writer, "Testing {test_name}...").expect("Failed to write to stdout"); writer.flush().ok(); - match run_test(test_name, test_function, &driver, allow_warnings, show_output) { + match run_test(test_name, test_function, &driver, &config) { Ok(_) => { writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).ok(); writeln!(writer, "ok").ok(); @@ -87,13 +93,12 @@ fn run_test( test_name: &str, main: FuncId, driver: &Driver, - allow_warnings: bool, - show_output: bool, + config: &CompileOptions, ) -> Result<(), CliError> { let backend = crate::backends::ConcreteBackend; let program = driver - .compile_no_check(false, allow_warnings, Some(main), show_output) + .compile_no_check(config, Some(main)) .map_err(|_| CliError::Generic(format!("Test '{test_name}' failed to compile")))?; let mut solved_witness = BTreeMap::new(); diff --git a/crates/noirc_driver/Cargo.toml b/crates/noirc_driver/Cargo.toml index 67cf385c8c4..749bd86a39f 100644 --- a/crates/noirc_driver/Cargo.toml +++ b/crates/noirc_driver/Cargo.toml @@ -15,3 +15,4 @@ acvm.workspace = true fm.workspace = true serde.workspace = true dirs.workspace = true +derive_builder = "0.12.0" diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 2ba135abfab..3de473826d7 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -2,6 +2,7 @@ use acvm::acir::circuit::Circuit; use acvm::Language; +use derive_builder::Builder; use fm::FileType; use noirc_abi::Abi; use noirc_errors::{reporter, ReportedError}; @@ -19,6 +20,16 @@ pub struct Driver { language: Language, } +#[non_exhaustive] +#[derive(Debug, Default, Builder)] +#[builder(default)] +pub struct CompileOptions { + pub show_ssa: bool, + pub allow_warnings: bool, + + pub show_output: bool, +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CompiledProgram { pub circuit: Circuit, @@ -38,7 +49,9 @@ impl Driver { let mut driver = Driver::new(&np_language); driver.create_local_crate(root_file, CrateType::Binary); - driver.into_compiled_program(false, false).unwrap_or_else(|_| std::process::exit(1)) + driver + .into_compiled_program(&CompileOptions::default()) + .unwrap_or_else(|_| std::process::exit(1)) } /// Compiles a file and returns true if compilation was successful @@ -125,10 +138,11 @@ impl Driver { /// Run the lexing, parsing, name resolution, and type checking passes, /// returning Err(FrontendError) and printing any errors that were found. - pub fn check_crate(&mut self, allow_warnings: bool) -> Result<(), ReportedError> { + pub fn check_crate(&mut self, options: &CompileOptions) -> Result<(), ReportedError> { let mut errs = vec![]; CrateDefMap::collect_defs(LOCAL_CRATE, &mut self.context, &mut errs); - let error_count = reporter::report_all(&self.context.file_manager, &errs, allow_warnings); + let error_count = + reporter::report_all(&self.context.file_manager, &errs, options.allow_warnings); reporter::finish_report(error_count) } @@ -145,22 +159,19 @@ impl Driver { pub fn into_compiled_program( mut self, - show_ssa: bool, - allow_warnings: bool, + options: &CompileOptions, ) -> Result { - self.check_crate(allow_warnings)?; - self.compile_no_check(show_ssa, allow_warnings, None, true) + self.check_crate(options)?; + self.compile_no_check(options, None) } /// Compile the current crate. Assumes self.check_crate is called beforehand! #[allow(deprecated)] pub fn compile_no_check( &self, - show_ssa: bool, - allow_warnings: bool, + options: &CompileOptions, // Optional override to provide a different `main` function to start execution main_function: Option, - show_output: bool, ) -> Result { // Find the local crate, one should always be present let local_crate = self.context.def_map(LOCAL_CRATE).unwrap(); @@ -183,14 +194,20 @@ impl Driver { let np_language = self.language.clone(); let blackbox_supported = acvm::default_is_black_box_supported(np_language.clone()); - match create_circuit(program, np_language, blackbox_supported, show_ssa, show_output) { + match create_circuit( + program, + np_language, + blackbox_supported, + options.show_ssa, + options.show_output, + ) { Ok((circuit, abi)) => Ok(CompiledProgram { circuit, abi }), Err(err) => { // The FileId here will be the file id of the file with the main file // Errors will be shown at the call site without a stacktrace let file = err.location.map(|loc| loc.file); let files = &self.context.file_manager; - let error = reporter::report(files, &err.into(), file, allow_warnings); + let error = reporter::report(files, &err.into(), file, options.allow_warnings); reporter::finish_report(error as u32)?; Err(ReportedError) } diff --git a/crates/noirc_driver/src/main.rs b/crates/noirc_driver/src/main.rs index e7f99ced95f..98a51780854 100644 --- a/crates/noirc_driver/src/main.rs +++ b/crates/noirc_driver/src/main.rs @@ -1,4 +1,4 @@ -use noirc_driver::Driver; +use noirc_driver::{CompileOptions, Driver}; use noirc_frontend::graph::{CrateType, LOCAL_CRATE}; fn main() { const EXTERNAL_DIR: &str = "dep_b/lib.nr"; @@ -18,5 +18,6 @@ fn main() { driver.add_dep(LOCAL_CRATE, crate_id1, "coo4"); driver.add_dep(LOCAL_CRATE, crate_id2, "coo3"); - driver.into_compiled_program(false, false).ok(); + let config = CompileOptions::default(); + driver.into_compiled_program(&config).ok(); } From 09085c32db2efb1f97d439847bcb36a2e74afba4 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Fri, 24 Feb 2023 17:09:22 +0000 Subject: [PATCH 2/8] chore: remove builder and group compile options in clap --- Cargo.lock | 33 ----------------------- crates/nargo/src/cli/check_cmd.rs | 38 +++++++++++++++----------- crates/nargo/src/cli/compile_cmd.rs | 22 +++++---------- crates/nargo/src/cli/contract_cmd.rs | 12 ++++----- crates/nargo/src/cli/execute_cmd.rs | 17 +++--------- crates/nargo/src/cli/gates_cmd.rs | 20 +++++--------- crates/nargo/src/cli/mod.rs | 40 +++++++++++++++++++++++++--- crates/nargo/src/cli/prove_cmd.rs | 19 ++++--------- crates/nargo/src/cli/test_cmd.rs | 25 ++++------------- crates/nargo/src/cli/verify_cmd.rs | 18 ++++++------- crates/noirc_driver/Cargo.toml | 1 - crates/noirc_driver/src/lib.rs | 4 +-- 12 files changed, 100 insertions(+), 149 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c7078c9c09..42476cea2a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1016,7 +1016,6 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", "syn", ] @@ -1051,37 +1050,6 @@ dependencies = [ "syn", ] -[[package]] -name = "derive_builder" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8" -dependencies = [ - "derive_builder_macro", -] - -[[package]] -name = "derive_builder_core" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f" -dependencies = [ - "darling", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "derive_builder_macro" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e" -dependencies = [ - "derive_builder_core", - "syn", -] - [[package]] name = "digest" version = "0.9.0" @@ -2066,7 +2034,6 @@ name = "noirc_driver" version = "0.2.0" dependencies = [ "acvm 0.4.1", - "derive_builder", "dirs 4.0.0", "fm", "noirc_abi", diff --git a/crates/nargo/src/cli/check_cmd.rs b/crates/nargo/src/cli/check_cmd.rs index 26a3a18fcea..73af6b30f8e 100644 --- a/crates/nargo/src/cli/check_cmd.rs +++ b/crates/nargo/src/cli/check_cmd.rs @@ -3,7 +3,7 @@ use acvm::ProofSystemCompiler; use clap::Args; use iter_extended::btree_map; use noirc_abi::{Abi, AbiParameter, AbiType}; -use noirc_driver::CompileOptionsBuilder; +use noirc_driver::CompileOptions; use std::{ collections::BTreeMap, path::{Path, PathBuf}, @@ -15,27 +15,24 @@ use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE}; /// Checks the constraint system for errors #[derive(Debug, Clone, Args)] -pub(crate) struct CheckCommand { - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, -} +pub(crate) struct CheckCommand {} -pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { - check_from_path(config.program_dir, args.allow_warnings)?; +pub(crate) fn run(_args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { + check_from_path(config.program_dir, &CompileOptions::from(config.compile_options))?; println!("Constraint system successfully built!"); Ok(()) } // This is exposed so that we can run the examples and verify that they pass -pub fn check_from_path>(p: P, allow_warnings: bool) -> Result<(), CliError> { +pub(crate) fn check_from_path>( + p: P, + compile_options: &CompileOptions, +) -> Result<(), CliError> { let backend = crate::backends::ConcreteBackend; - let config = CompileOptionsBuilder::default().allow_warnings(allow_warnings).build().unwrap(); - let mut driver = Resolver::resolve_root_config(p.as_ref(), backend.np_language())?; add_std_lib(&mut driver); - if driver.check_crate(&config).is_err() { + if driver.check_crate(compile_options).is_err() { std::process::exit(1); } @@ -75,6 +72,8 @@ fn build_empty_map(abi: Abi) -> BTreeMap { #[cfg(test)] mod tests { + use noirc_driver::CompileOptions; + const TEST_DATA_DIR: &str = "tests/target_tests_data"; #[test] @@ -82,11 +81,12 @@ mod tests { let mut pass_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); pass_dir.push(&format!("{TEST_DATA_DIR}/pass")); + let config = CompileOptions::default(); let paths = std::fs::read_dir(pass_dir).unwrap(); for path in paths.flatten() { let path = path.path(); assert!( - super::check_from_path(path.clone(), false).is_ok(), + super::check_from_path(path.clone(), &config).is_ok(), "path: {}", path.display() ); @@ -99,11 +99,12 @@ mod tests { let mut fail_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); fail_dir.push(&format!("{TEST_DATA_DIR}/fail")); + let config = CompileOptions::default(); let paths = std::fs::read_dir(fail_dir).unwrap(); for path in paths.flatten() { let path = path.path(); assert!( - super::check_from_path(path.clone(), false).is_err(), + super::check_from_path(path.clone(), &config).is_err(), "path: {}", path.display() ); @@ -115,10 +116,17 @@ mod tests { let mut pass_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); pass_dir.push(&format!("{TEST_DATA_DIR}/pass_dev_mode")); + let mut config = CompileOptions::default(); + config.allow_warnings = true; + let paths = std::fs::read_dir(pass_dir).unwrap(); for path in paths.flatten() { let path = path.path(); - assert!(super::check_from_path(path.clone(), true).is_ok(), "path: {}", path.display()); + assert!( + super::check_from_path(path.clone(), &config).is_ok(), + "path: {}", + path.display() + ); } } } diff --git a/crates/nargo/src/cli/compile_cmd.rs b/crates/nargo/src/cli/compile_cmd.rs index 44d0fe7f4ae..248ff8bff7f 100644 --- a/crates/nargo/src/cli/compile_cmd.rs +++ b/crates/nargo/src/cli/compile_cmd.rs @@ -1,6 +1,6 @@ use acvm::acir::circuit::Circuit; use acvm::ProofSystemCompiler; -use noirc_driver::CompileOptionsBuilder; +use noirc_driver::CompileOptions; use std::path::{Path, PathBuf}; use clap::Args; @@ -15,10 +15,6 @@ use super::{add_std_lib, NargoConfig}; pub(crate) struct CompileCommand { /// The name of the ACIR file circuit_name: String, - - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, } pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> { @@ -29,7 +25,7 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr &args.circuit_name, config.program_dir, circuit_path, - args.allow_warnings, + &CompileOptions::from(config.compile_options), )?; println!("Generated ACIR code into {}", circuit_path.display()); @@ -41,9 +37,9 @@ fn compile_and_preprocess_circuit>( circuit_name: &str, program_dir: P, circuit_dir: P, - allow_warnings: bool, + compile_options: &CompileOptions, ) -> Result { - let compiled_program = compile_circuit(program_dir, false, allow_warnings)?; + let compiled_program = compile_circuit(program_dir, compile_options)?; let circuit_path = save_acir_to_dir(&compiled_program.circuit, circuit_name, &circuit_dir); preprocess_with_path(circuit_name, circuit_dir, compiled_program.circuit)?; @@ -53,19 +49,13 @@ fn compile_and_preprocess_circuit>( pub fn compile_circuit>( program_dir: P, - show_ssa: bool, - allow_warnings: bool, + compile_options: &CompileOptions, ) -> Result { let backend = crate::backends::ConcreteBackend; let mut driver = Resolver::resolve_root_config(program_dir.as_ref(), backend.np_language())?; add_std_lib(&mut driver); - let config = CompileOptionsBuilder::default() - .allow_warnings(allow_warnings) - .show_ssa(show_ssa) - .build() - .unwrap(); - driver.into_compiled_program(&config).map_err(|_| std::process::exit(1)) + driver.into_compiled_program(compile_options).map_err(|_| std::process::exit(1)) } fn preprocess_with_path>( diff --git a/crates/nargo/src/cli/contract_cmd.rs b/crates/nargo/src/cli/contract_cmd.rs index 1aa2711d679..4b78c98607c 100644 --- a/crates/nargo/src/cli/contract_cmd.rs +++ b/crates/nargo/src/cli/contract_cmd.rs @@ -3,17 +3,15 @@ use super::NargoConfig; use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError}; use acvm::SmartContract; use clap::Args; +use noirc_driver::CompileOptions; /// Generates a Solidity verifier smart contract for the program #[derive(Debug, Clone, Args)] -pub(crate) struct ContractCommand { - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, -} +pub(crate) struct ContractCommand {} -pub(crate) fn run(args: ContractCommand, config: NargoConfig) -> Result<(), CliError> { - let compiled_program = compile_circuit(config.program_dir.clone(), false, args.allow_warnings)?; +pub(crate) fn run(_args: ContractCommand, config: NargoConfig) -> Result<(), CliError> { + let compiled_program = + compile_circuit(config.program_dir.clone(), &CompileOptions::from(config.compile_options))?; let backend = crate::backends::ConcreteBackend; let smart_contract_string = backend.eth_contract_from_cs(compiled_program.circuit); diff --git a/crates/nargo/src/cli/execute_cmd.rs b/crates/nargo/src/cli/execute_cmd.rs index 761f376d8f8..1fb783f657b 100644 --- a/crates/nargo/src/cli/execute_cmd.rs +++ b/crates/nargo/src/cli/execute_cmd.rs @@ -4,7 +4,7 @@ use acvm::PartialWitnessGenerator; use clap::Args; use noirc_abi::input_parser::{Format, InputValue}; use noirc_abi::{InputMap, WitnessMap}; -use noirc_driver::CompiledProgram; +use noirc_driver::{CompileOptions, CompiledProgram}; use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir}; use super::NargoConfig; @@ -19,19 +19,11 @@ use crate::{ pub(crate) struct ExecuteCommand { /// Write the execution witness to named file witness_name: Option, - - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, - - /// Emit debug information for the intermediate SSA IR - #[arg(short, long)] - show_ssa: bool, } pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> { let (return_value, solved_witness) = - execute_with_path(&config.program_dir, args.show_ssa, args.allow_warnings)?; + execute_with_path(&config.program_dir, &CompileOptions::from(config.compile_options))?; println!("Circuit witness successfully solved"); if let Some(return_value) = return_value { @@ -50,10 +42,9 @@ pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliEr fn execute_with_path>( program_dir: P, - show_ssa: bool, - allow_warnings: bool, + compile_options: &CompileOptions, ) -> Result<(Option, WitnessMap), CliError> { - let compiled_program = compile_circuit(&program_dir, show_ssa, allow_warnings)?; + let compiled_program = compile_circuit(&program_dir, compile_options)?; // Parse the initial witness values from Prover.toml let (inputs_map, _) = read_inputs_from_file( diff --git a/crates/nargo/src/cli/gates_cmd.rs b/crates/nargo/src/cli/gates_cmd.rs index bae5dd22953..fa38d599d83 100644 --- a/crates/nargo/src/cli/gates_cmd.rs +++ b/crates/nargo/src/cli/gates_cmd.rs @@ -1,5 +1,6 @@ use acvm::ProofSystemCompiler; use clap::Args; +use noirc_driver::CompileOptions; use std::path::Path; use crate::cli::compile_cmd::compile_circuit; @@ -9,26 +10,17 @@ use super::NargoConfig; /// Counts the occurrences of different gates in circuit #[derive(Debug, Clone, Args)] -pub(crate) struct GatesCommand { - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, - - /// Emit debug information for the intermediate SSA IR - #[arg(short, long)] - show_ssa: bool, -} +pub(crate) struct GatesCommand {} -pub(crate) fn run(args: GatesCommand, config: NargoConfig) -> Result<(), CliError> { - count_gates_with_path(config.program_dir, args.show_ssa, args.allow_warnings) +pub(crate) fn run(_args: GatesCommand, config: NargoConfig) -> Result<(), CliError> { + count_gates_with_path(config.program_dir, &CompileOptions::from(config.compile_options)) } pub fn count_gates_with_path>( program_dir: P, - show_ssa: bool, - allow_warnings: bool, + compile_options: &CompileOptions, ) -> Result<(), CliError> { - let compiled_program = compile_circuit(program_dir.as_ref(), show_ssa, allow_warnings)?; + let compiled_program = compile_circuit(program_dir.as_ref(), compile_options)?; let num_opcodes = compiled_program.circuit.opcodes.len(); let backend = crate::backends::ConcreteBackend; diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index b0b778ad226..2a21472e5e2 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -1,8 +1,7 @@ -pub use check_cmd::check_from_path; use clap::{Args, Parser, Subcommand}; use const_format::formatcp; use noirc_abi::InputMap; -use noirc_driver::Driver; +use noirc_driver::{CompileOptions, Driver}; use noirc_frontend::graph::{CrateName, CrateType}; use std::path::{Path, PathBuf}; extern crate tempdir; @@ -42,6 +41,34 @@ struct NargoCli { pub(crate) struct NargoConfig { #[arg(short, long, hide=true, default_value_os_t = std::env::current_dir().unwrap())] program_dir: PathBuf, + + #[clap(flatten)] + compile_options: NargoCompileOptions, +} + +#[derive(Args, Clone, Debug, Default)] +pub(crate) struct NargoCompileOptions { + /// Emit debug information for the intermediate SSA IR + #[arg(short, long)] + pub show_ssa: bool, + + /// Issue a warning for each unused variable instead of an error + #[arg(short, long)] + pub allow_warnings: bool, + + /// Display output of println statements during tests + #[arg(long)] + pub show_output: bool, +} + +impl From for CompileOptions { + fn from(value: NargoCompileOptions) -> Self { + let mut options = CompileOptions::default(); + options.show_ssa = value.show_ssa; + options.allow_warnings = value.allow_warnings; + options.show_output = value.show_output; + options + } } #[non_exhaustive] @@ -80,14 +107,19 @@ pub fn start_cli() { // helper function which tests noir programs by trying to generate a proof and verify it pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> bool { let tmp_dir = TempDir::new("p_and_v_tests").unwrap(); + let compile_options = CompileOptions::from(NargoCompileOptions { + show_ssa, + allow_warnings: false, + show_output: false, + }); + match prove_cmd::prove_with_path( Some(proof_name.to_owned()), prg_dir, &tmp_dir.into_path(), None, true, - show_ssa, - false, + &compile_options, ) { Ok(_) => true, Err(error) => { diff --git a/crates/nargo/src/cli/prove_cmd.rs b/crates/nargo/src/cli/prove_cmd.rs index e71185fa732..61acda76b91 100644 --- a/crates/nargo/src/cli/prove_cmd.rs +++ b/crates/nargo/src/cli/prove_cmd.rs @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf}; use acvm::ProofSystemCompiler; use clap::Args; use noirc_abi::input_parser::Format; +use noirc_driver::CompileOptions; use super::fs::{ inputs::{read_inputs_from_file, write_inputs_to_file}, @@ -16,7 +17,7 @@ use crate::{ errors::CliError, }; -// Create proof for this program. The proof is returned as a hex encoded string. +/// Create proof for this program. The proof is returned as a hex encoded string. #[derive(Debug, Clone, Args)] pub(crate) struct ProveCommand { /// The name of the proof @@ -28,14 +29,6 @@ pub(crate) struct ProveCommand { /// Verify proof after proving #[arg(short, long)] verify: bool, - - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, - - /// Emit debug information for the intermediate SSA IR - #[arg(short, long)] - show_ssa: bool, } pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliError> { @@ -57,8 +50,7 @@ pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliErro proof_dir, circuit_build_path, args.verify, - args.show_ssa, - args.allow_warnings, + &CompileOptions::from(config.compile_options), )?; Ok(()) @@ -70,11 +62,10 @@ pub fn prove_with_path>( proof_dir: P, circuit_build_path: Option

, check_proof: bool, - show_ssa: bool, - allow_warnings: bool, + compile_options: &CompileOptions, ) -> Result, CliError> { let compiled_program = - super::compile_cmd::compile_circuit(program_dir.as_ref(), show_ssa, allow_warnings)?; + super::compile_cmd::compile_circuit(program_dir.as_ref(), compile_options)?; let (proving_key, verification_key) = match circuit_build_path { Some(circuit_build_path) => { fetch_pk_and_vk(&compiled_program.circuit, circuit_build_path, true, true)? diff --git a/crates/nargo/src/cli/test_cmd.rs b/crates/nargo/src/cli/test_cmd.rs index 878a9873488..de2f2ca9b4f 100644 --- a/crates/nargo/src/cli/test_cmd.rs +++ b/crates/nargo/src/cli/test_cmd.rs @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, io::Write, path::Path}; use acvm::{PartialWitnessGenerator, ProofSystemCompiler}; use clap::Args; -use noirc_driver::{CompileOptions, CompileOptionsBuilder, Driver}; +use noirc_driver::{CompileOptions, Driver}; use noirc_frontend::node_interner::FuncId; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -15,40 +15,25 @@ use super::{add_std_lib, NargoConfig}; pub(crate) struct TestCommand { /// If given, only tests with names containing this string will be run test_name: Option, - - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, - - /// Display output of println statements during tests - #[arg(long)] - show_logs: bool, } pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError> { let test_name: String = args.test_name.unwrap_or_else(|| "".to_owned()); - run_tests(&config.program_dir, &test_name, args.allow_warnings, args.show_logs) + run_tests(&config.program_dir, &test_name, &CompileOptions::from(config.compile_options)) } fn run_tests( program_dir: &Path, test_name: &str, - allow_warnings: bool, - show_output: bool, + compile_options: &CompileOptions, ) -> Result<(), CliError> { let backend = crate::backends::ConcreteBackend; - let config = CompileOptionsBuilder::default() - .allow_warnings(allow_warnings) - .show_output(show_output) - .build() - .unwrap(); - let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?; add_std_lib(&mut driver); - if driver.check_crate(&config).is_err() { + if driver.check_crate(&compile_options).is_err() { std::process::exit(1); } @@ -64,7 +49,7 @@ fn run_tests( writeln!(writer, "Testing {test_name}...").expect("Failed to write to stdout"); writer.flush().ok(); - match run_test(test_name, test_function, &driver, &config) { + match run_test(test_name, test_function, &driver, &compile_options) { Ok(_) => { writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).ok(); writeln!(writer, "ok").ok(); diff --git a/crates/nargo/src/cli/verify_cmd.rs b/crates/nargo/src/cli/verify_cmd.rs index 8f5a1cc5789..f222c4454f9 100644 --- a/crates/nargo/src/cli/verify_cmd.rs +++ b/crates/nargo/src/cli/verify_cmd.rs @@ -7,7 +7,7 @@ use crate::{ use acvm::{FieldElement, ProofSystemCompiler}; use clap::Args; use noirc_abi::input_parser::{Format, InputValue}; -use noirc_driver::CompiledProgram; +use noirc_driver::{CompileOptions, CompiledProgram}; use std::path::{Path, PathBuf}; /// Given a proof and a program, verify whether the proof is valid @@ -18,10 +18,6 @@ pub(crate) struct VerifyCommand { /// The name of the circuit build files (ACIR, proving and verification keys) circuit_name: Option, - - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - allow_warnings: bool, } pub(crate) fn run(args: VerifyCommand, config: NargoConfig) -> Result<(), CliError> { @@ -37,17 +33,21 @@ pub(crate) fn run(args: VerifyCommand, config: NargoConfig) -> Result<(), CliErr circuit_build_path }); - verify_with_path(config.program_dir, proof_path, circuit_build_path, false, args.allow_warnings) + verify_with_path( + config.program_dir, + proof_path, + circuit_build_path, + CompileOptions::from(config.compile_options), + ) } fn verify_with_path>( program_dir: P, proof_path: PathBuf, circuit_build_path: Option

, - show_ssa: bool, - allow_warnings: bool, + compile_options: CompileOptions, ) -> Result<(), CliError> { - let compiled_program = compile_circuit(program_dir.as_ref(), show_ssa, allow_warnings)?; + let compiled_program = compile_circuit(program_dir.as_ref(), &compile_options)?; let (_, verification_key) = match circuit_build_path { Some(circuit_build_path) => { fetch_pk_and_vk(&compiled_program.circuit, circuit_build_path, false, true)? diff --git a/crates/noirc_driver/Cargo.toml b/crates/noirc_driver/Cargo.toml index 749bd86a39f..67cf385c8c4 100644 --- a/crates/noirc_driver/Cargo.toml +++ b/crates/noirc_driver/Cargo.toml @@ -15,4 +15,3 @@ acvm.workspace = true fm.workspace = true serde.workspace = true dirs.workspace = true -derive_builder = "0.12.0" diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 3de473826d7..4cf7ce8569a 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -2,7 +2,6 @@ use acvm::acir::circuit::Circuit; use acvm::Language; -use derive_builder::Builder; use fm::FileType; use noirc_abi::Abi; use noirc_errors::{reporter, ReportedError}; @@ -21,8 +20,7 @@ pub struct Driver { } #[non_exhaustive] -#[derive(Debug, Default, Builder)] -#[builder(default)] +#[derive(Debug, Default)] pub struct CompileOptions { pub show_ssa: bool, pub allow_warnings: bool, From 4b8f03a197605eb25f1cd0685aedb7cb30af848a Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Fri, 24 Feb 2023 17:40:20 +0000 Subject: [PATCH 3/8] chore: remove unused CliError variants --- crates/nargo/src/errors.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/nargo/src/errors.rs b/crates/nargo/src/errors.rs index 09de29e558d..004d3621874 100644 --- a/crates/nargo/src/errors.rs +++ b/crates/nargo/src/errors.rs @@ -6,7 +6,7 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use thiserror::Error; #[derive(Debug, Error)] -pub enum CliError { +pub(crate) enum CliError { #[error("{0}")] Generic(String), #[error("Error: destination {} already exists", .0.display())] @@ -19,10 +19,6 @@ pub enum CliError { " Error: cannot find {0}.toml file.\n Expected location: {1:?} \n Please generate this file at the expected location." )] MissingTomlFile(String, PathBuf), - #[error("Error: cannot find proving key located at {}\nEither run `nargo compile` to generate the missing proving key or check that the correct file name has been provided", .0.display())] - MissingProvingKey(PathBuf), - #[error("Error: cannot find verification key located at {}\nEither run `nargo compile` to generate the missing verification key or check that the correct file name has been provided", .0.display())] - MissingVerificationkey(PathBuf), #[error("Error: the circuit you are trying to prove differs from the build artifact at {}\nYou must call `nargo compile` to generate the correct proving and verification keys for this circuit", .0.display())] MismatchedAcir(PathBuf), #[error("Failed to verify proof {}", .0.display())] From 44f3b76bf20fb10f758a003681513d5792ac9f35 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Fri, 24 Feb 2023 22:01:33 +0000 Subject: [PATCH 4/8] chore: clippy --- crates/nargo/src/cli/test_cmd.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/nargo/src/cli/test_cmd.rs b/crates/nargo/src/cli/test_cmd.rs index de2f2ca9b4f..76d9e56f5ef 100644 --- a/crates/nargo/src/cli/test_cmd.rs +++ b/crates/nargo/src/cli/test_cmd.rs @@ -33,7 +33,7 @@ fn run_tests( let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?; add_std_lib(&mut driver); - if driver.check_crate(&compile_options).is_err() { + if driver.check_crate(compile_options).is_err() { std::process::exit(1); } @@ -49,7 +49,7 @@ fn run_tests( writeln!(writer, "Testing {test_name}...").expect("Failed to write to stdout"); writer.flush().ok(); - match run_test(test_name, test_function, &driver, &compile_options) { + match run_test(test_name, test_function, &driver, compile_options) { Ok(_) => { writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).ok(); writeln!(writer, "ok").ok(); From a32a7e55c8137ab9862fe101a62be10375f0ab91 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Tue, 28 Feb 2023 14:02:55 +0000 Subject: [PATCH 5/8] chore: move compile options back onto `args` from `config` --- crates/nargo/src/cli/check_cmd.rs | 11 +++++++---- crates/nargo/src/cli/compile_cmd.rs | 7 +++++-- crates/nargo/src/cli/contract_cmd.rs | 11 +++++++---- crates/nargo/src/cli/execute_cmd.rs | 7 +++++-- crates/nargo/src/cli/gates_cmd.rs | 11 +++++++---- crates/nargo/src/cli/mod.rs | 3 --- crates/nargo/src/cli/prove_cmd.rs | 7 +++++-- crates/nargo/src/cli/test_cmd.rs | 7 +++++-- crates/nargo/src/cli/verify_cmd.rs | 7 +++++-- 9 files changed, 46 insertions(+), 25 deletions(-) diff --git a/crates/nargo/src/cli/check_cmd.rs b/crates/nargo/src/cli/check_cmd.rs index e36cd29dd6e..22ca375d6fe 100644 --- a/crates/nargo/src/cli/check_cmd.rs +++ b/crates/nargo/src/cli/check_cmd.rs @@ -10,15 +10,18 @@ use std::{ }; use super::fs::write_to_file; -use super::{add_std_lib, NargoConfig}; +use super::{add_std_lib, NargoCompileOptions, NargoConfig}; use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE}; /// Checks the constraint system for errors #[derive(Debug, Clone, Args)] -pub(crate) struct CheckCommand {} +pub(crate) struct CheckCommand { + #[clap(flatten)] + compile_options: NargoCompileOptions, +} -pub(crate) fn run(_args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { - check_from_path(config.program_dir, &CompileOptions::from(config.compile_options))?; +pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { + check_from_path(config.program_dir, &CompileOptions::from(args.compile_options))?; println!("Constraint system successfully built!"); Ok(()) } diff --git a/crates/nargo/src/cli/compile_cmd.rs b/crates/nargo/src/cli/compile_cmd.rs index 07f53cb17cb..8b5d4052aea 100644 --- a/crates/nargo/src/cli/compile_cmd.rs +++ b/crates/nargo/src/cli/compile_cmd.rs @@ -8,13 +8,16 @@ use clap::Args; use crate::{constants::TARGET_DIR, errors::CliError, resolver::Resolver}; use super::fs::{keys::save_key_to_dir, program::save_program_to_file}; -use super::{add_std_lib, NargoConfig}; +use super::{add_std_lib, NargoCompileOptions, NargoConfig}; /// Compile the program and its secret execution trace into ACIR format #[derive(Debug, Clone, Args)] pub(crate) struct CompileCommand { /// The name of the ACIR file circuit_name: String, + + #[clap(flatten)] + compile_options: NargoCompileOptions, } pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> { @@ -25,7 +28,7 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr &args.circuit_name, config.program_dir, circuit_path, - &CompileOptions::from(config.compile_options), + &CompileOptions::from(args.compile_options), )?; println!("Generated ACIR code into {}", circuit_path.display()); diff --git a/crates/nargo/src/cli/contract_cmd.rs b/crates/nargo/src/cli/contract_cmd.rs index 68446926b32..1d28be7035e 100644 --- a/crates/nargo/src/cli/contract_cmd.rs +++ b/crates/nargo/src/cli/contract_cmd.rs @@ -1,5 +1,5 @@ use super::fs::{create_named_dir, write_to_file}; -use super::NargoConfig; +use super::{NargoCompileOptions, NargoConfig}; use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError}; use acvm::SmartContract; use clap::Args; @@ -7,11 +7,14 @@ use noirc_driver::CompileOptions; /// Generates a Solidity verifier smart contract for the program #[derive(Debug, Clone, Args)] -pub(crate) struct ContractCommand {} +pub(crate) struct ContractCommand { + #[clap(flatten)] + compile_options: NargoCompileOptions, +} -pub(crate) fn run(_args: ContractCommand, config: NargoConfig) -> Result<(), CliError> { +pub(crate) fn run(args: ContractCommand, config: NargoConfig) -> Result<(), CliError> { let compiled_program = - compile_circuit(config.program_dir.clone(), &CompileOptions::from(config.compile_options))?; + compile_circuit(config.program_dir.clone(), &CompileOptions::from(args.compile_options))?; let backend = crate::backends::ConcreteBackend; #[allow(deprecated)] diff --git a/crates/nargo/src/cli/execute_cmd.rs b/crates/nargo/src/cli/execute_cmd.rs index 1fb783f657b..bd466d174cb 100644 --- a/crates/nargo/src/cli/execute_cmd.rs +++ b/crates/nargo/src/cli/execute_cmd.rs @@ -7,7 +7,7 @@ use noirc_abi::{InputMap, WitnessMap}; use noirc_driver::{CompileOptions, CompiledProgram}; use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir}; -use super::NargoConfig; +use super::{NargoCompileOptions, NargoConfig}; use crate::{ cli::compile_cmd::compile_circuit, constants::{PROVER_INPUT_FILE, TARGET_DIR}, @@ -19,11 +19,14 @@ use crate::{ pub(crate) struct ExecuteCommand { /// Write the execution witness to named file witness_name: Option, + + #[clap(flatten)] + compile_options: NargoCompileOptions, } pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> { let (return_value, solved_witness) = - execute_with_path(&config.program_dir, &CompileOptions::from(config.compile_options))?; + execute_with_path(&config.program_dir, &CompileOptions::from(args.compile_options))?; println!("Circuit witness successfully solved"); if let Some(return_value) = return_value { diff --git a/crates/nargo/src/cli/gates_cmd.rs b/crates/nargo/src/cli/gates_cmd.rs index c7155738738..e04b640a45a 100644 --- a/crates/nargo/src/cli/gates_cmd.rs +++ b/crates/nargo/src/cli/gates_cmd.rs @@ -6,14 +6,17 @@ use std::path::Path; use crate::cli::compile_cmd::compile_circuit; use crate::errors::CliError; -use super::NargoConfig; +use super::{NargoCompileOptions, NargoConfig}; /// Counts the occurrences of different gates in circuit #[derive(Debug, Clone, Args)] -pub(crate) struct GatesCommand {} +pub(crate) struct GatesCommand { + #[clap(flatten)] + compile_options: NargoCompileOptions, +} -pub(crate) fn run(_args: GatesCommand, config: NargoConfig) -> Result<(), CliError> { - count_gates_with_path(config.program_dir, &CompileOptions::from(config.compile_options)) +pub(crate) fn run(args: GatesCommand, config: NargoConfig) -> Result<(), CliError> { + count_gates_with_path(config.program_dir, &CompileOptions::from(args.compile_options)) } fn count_gates_with_path>( diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index 398c4033f72..1d1063c1e1f 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -39,9 +39,6 @@ struct NargoCli { pub(crate) struct NargoConfig { #[arg(short, long, hide=true, default_value_os_t = std::env::current_dir().unwrap())] program_dir: PathBuf, - - #[clap(flatten)] - compile_options: NargoCompileOptions, } #[derive(Args, Clone, Debug, Default)] diff --git a/crates/nargo/src/cli/prove_cmd.rs b/crates/nargo/src/cli/prove_cmd.rs index afa3196c529..10404023132 100644 --- a/crates/nargo/src/cli/prove_cmd.rs +++ b/crates/nargo/src/cli/prove_cmd.rs @@ -11,7 +11,7 @@ use super::fs::{ program::read_program_from_file, proof::save_proof_to_dir, }; -use super::NargoConfig; +use super::{NargoCompileOptions, NargoConfig}; use crate::{ cli::{execute_cmd::execute_program, verify_cmd::verify_proof}, constants::{PROOFS_DIR, PROVER_INPUT_FILE, TARGET_DIR, VERIFIER_INPUT_FILE}, @@ -30,6 +30,9 @@ pub(crate) struct ProveCommand { /// Verify proof after proving #[arg(short, long)] verify: bool, + + #[clap(flatten)] + compile_options: NargoCompileOptions, } pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliError> { @@ -51,7 +54,7 @@ pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliErro proof_dir, circuit_build_path, args.verify, - &CompileOptions::from(config.compile_options), + &CompileOptions::from(args.compile_options), )?; Ok(()) diff --git a/crates/nargo/src/cli/test_cmd.rs b/crates/nargo/src/cli/test_cmd.rs index 76d9e56f5ef..3183c8f0365 100644 --- a/crates/nargo/src/cli/test_cmd.rs +++ b/crates/nargo/src/cli/test_cmd.rs @@ -8,19 +8,22 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use crate::{errors::CliError, resolver::Resolver}; -use super::{add_std_lib, NargoConfig}; +use super::{add_std_lib, NargoCompileOptions, NargoConfig}; /// Run the tests for this program #[derive(Debug, Clone, Args)] pub(crate) struct TestCommand { /// If given, only tests with names containing this string will be run test_name: Option, + + #[clap(flatten)] + compile_options: NargoCompileOptions, } pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError> { let test_name: String = args.test_name.unwrap_or_else(|| "".to_owned()); - run_tests(&config.program_dir, &test_name, &CompileOptions::from(config.compile_options)) + run_tests(&config.program_dir, &test_name, &CompileOptions::from(args.compile_options)) } fn run_tests( diff --git a/crates/nargo/src/cli/verify_cmd.rs b/crates/nargo/src/cli/verify_cmd.rs index edfe391e221..fded324f71c 100644 --- a/crates/nargo/src/cli/verify_cmd.rs +++ b/crates/nargo/src/cli/verify_cmd.rs @@ -2,7 +2,7 @@ use super::fs::{ inputs::read_inputs_from_file, keys::fetch_pk_and_vk, load_hex_data, program::read_program_from_file, }; -use super::{compile_cmd::compile_circuit, InputMap, NargoConfig}; +use super::{compile_cmd::compile_circuit, InputMap, NargoCompileOptions, NargoConfig}; use crate::{ constants::{PROOFS_DIR, PROOF_EXT, TARGET_DIR, VERIFIER_INPUT_FILE}, errors::CliError, @@ -21,6 +21,9 @@ pub(crate) struct VerifyCommand { /// The name of the circuit build files (ACIR, proving and verification keys) circuit_name: Option, + + #[clap(flatten)] + compile_options: NargoCompileOptions, } pub(crate) fn run(args: VerifyCommand, config: NargoConfig) -> Result<(), CliError> { @@ -40,7 +43,7 @@ pub(crate) fn run(args: VerifyCommand, config: NargoConfig) -> Result<(), CliErr config.program_dir, proof_path, circuit_build_path, - CompileOptions::from(config.compile_options), + CompileOptions::from(args.compile_options), ) } From ae8c2f0f91414b10936f0b38bdcb514f8b03d007 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Tue, 28 Feb 2023 14:05:31 +0000 Subject: [PATCH 6/8] chore: copy documentation onto `CompileOptions` --- crates/nargo/src/cli/mod.rs | 2 +- crates/noirc_driver/src/lib.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index 1d1063c1e1f..4629adfb2ce 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -51,7 +51,7 @@ pub(crate) struct NargoCompileOptions { #[arg(short, long)] pub(crate) allow_warnings: bool, - /// Display output of println statements during tests + /// Display output of `println` statements during tests #[arg(long)] pub(crate) show_output: bool, } diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 5faf668cf20..64d8495a6be 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -25,9 +25,13 @@ pub struct Driver { #[non_exhaustive] #[derive(Debug, Default)] pub struct CompileOptions { + /// Emit debug information for the intermediate SSA IR pub show_ssa: bool, + + /// Issue a warning for each unused variable instead of an error pub allow_warnings: bool, + /// Display output of `println` statements during tests pub show_output: bool, } From 0083de3a24eab07ac3e4568f41b4a40d04b9c515 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Mon, 6 Mar 2023 10:24:38 +0000 Subject: [PATCH 7/8] chore: remove abstraction between nargo and driver interfaces --- Cargo.lock | 1 + Cargo.toml | 1 + crates/nargo/Cargo.toml | 5 +++-- crates/nargo/src/cli/check_cmd.rs | 4 ++-- crates/nargo/src/cli/compile_cmd.rs | 4 ++-- crates/nargo/src/cli/contract_cmd.rs | 4 ++-- crates/nargo/src/cli/execute_cmd.rs | 4 ++-- crates/nargo/src/cli/gates_cmd.rs | 4 ++-- crates/nargo/src/cli/mod.rs | 31 +--------------------------- crates/nargo/src/cli/prove_cmd.rs | 4 ++-- crates/nargo/src/cli/test_cmd.rs | 4 ++-- crates/nargo/src/cli/verify_cmd.rs | 4 ++-- crates/noirc_driver/Cargo.toml | 1 + crates/noirc_driver/src/lib.rs | 7 +++++-- 14 files changed, 28 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b421ab9de6..8ea523fded8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2196,6 +2196,7 @@ name = "noirc_driver" version = "0.2.0" dependencies = [ "acvm 0.5.0", + "clap 4.1.4", "fm", "noirc_abi", "noirc_errors", diff --git a/Cargo.toml b/Cargo.toml index dc61f50eb4d..0c3915374a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ noirc_frontend = { path = "crates/noirc_frontend" } noir_wasm = { path = "crates/wasm" } cfg-if = "1.0.0" +clap = { version = "4.1.4", features = ["derive"]} codespan = "0.9.5" codespan-reporting = "0.9.5" chumsky = { git = "https://github.com/jfecher/chumsky", rev = "ad9d312" } diff --git a/crates/nargo/Cargo.toml b/crates/nargo/Cargo.toml index 92146850827..cbfe3b67a5a 100644 --- a/crates/nargo/Cargo.toml +++ b/crates/nargo/Cargo.toml @@ -13,6 +13,7 @@ rustc_version = "0.4.0" build-data = "0.1.3" [dependencies] +clap.workspace = true dirs.workspace = true url.workspace = true iter-extended.workspace = true @@ -25,18 +26,18 @@ cfg-if.workspace = true toml.workspace = true serde.workspace = true thiserror.workspace = true -clap = { version = "4.1.4", features = ["derive"]} const_format = "0.2.30" hex = "0.4.2" serde_json = "1.0" termcolor = "1.1.2" tempdir = "0.3.7" +color-eyre = "0.6.2" + # Backends aztec_backend = { optional = true, package = "barretenberg_static_lib", git = "https://github.com/noir-lang/aztec_backend", rev = "74b4d8d8b118e4477880c04149e5e9d93d388384" } aztec_wasm_backend = { optional = true, package = "barretenberg_wasm", git = "https://github.com/noir-lang/aztec_backend", rev = "74b4d8d8b118e4477880c04149e5e9d93d388384" } marlin_arkworks_backend = { optional = true, git = "https://github.com/noir-lang/marlin_arkworks_backend", rev = "144378edad821bfaa52bf2cacca8ecc87514a4fc" } -color-eyre = "0.6.2" [features] default = ["plonk_bn254"] diff --git a/crates/nargo/src/cli/check_cmd.rs b/crates/nargo/src/cli/check_cmd.rs index 22ca375d6fe..8fb2a59b148 100644 --- a/crates/nargo/src/cli/check_cmd.rs +++ b/crates/nargo/src/cli/check_cmd.rs @@ -10,14 +10,14 @@ use std::{ }; use super::fs::write_to_file; -use super::{add_std_lib, NargoCompileOptions, NargoConfig}; +use super::{add_std_lib, NargoConfig}; use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE}; /// Checks the constraint system for errors #[derive(Debug, Clone, Args)] pub(crate) struct CheckCommand { #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/compile_cmd.rs b/crates/nargo/src/cli/compile_cmd.rs index 8b5d4052aea..21bc4715bbd 100644 --- a/crates/nargo/src/cli/compile_cmd.rs +++ b/crates/nargo/src/cli/compile_cmd.rs @@ -8,7 +8,7 @@ use clap::Args; use crate::{constants::TARGET_DIR, errors::CliError, resolver::Resolver}; use super::fs::{keys::save_key_to_dir, program::save_program_to_file}; -use super::{add_std_lib, NargoCompileOptions, NargoConfig}; +use super::{add_std_lib, NargoConfig}; /// Compile the program and its secret execution trace into ACIR format #[derive(Debug, Clone, Args)] @@ -17,7 +17,7 @@ pub(crate) struct CompileCommand { circuit_name: String, #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/contract_cmd.rs b/crates/nargo/src/cli/contract_cmd.rs index 1d28be7035e..a94bb43bc28 100644 --- a/crates/nargo/src/cli/contract_cmd.rs +++ b/crates/nargo/src/cli/contract_cmd.rs @@ -1,5 +1,5 @@ use super::fs::{create_named_dir, write_to_file}; -use super::{NargoCompileOptions, NargoConfig}; +use super::NargoConfig; use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError}; use acvm::SmartContract; use clap::Args; @@ -9,7 +9,7 @@ use noirc_driver::CompileOptions; #[derive(Debug, Clone, Args)] pub(crate) struct ContractCommand { #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: ContractCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/execute_cmd.rs b/crates/nargo/src/cli/execute_cmd.rs index bd466d174cb..98efb1ad721 100644 --- a/crates/nargo/src/cli/execute_cmd.rs +++ b/crates/nargo/src/cli/execute_cmd.rs @@ -7,7 +7,7 @@ use noirc_abi::{InputMap, WitnessMap}; use noirc_driver::{CompileOptions, CompiledProgram}; use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir}; -use super::{NargoCompileOptions, NargoConfig}; +use super::NargoConfig; use crate::{ cli::compile_cmd::compile_circuit, constants::{PROVER_INPUT_FILE, TARGET_DIR}, @@ -21,7 +21,7 @@ pub(crate) struct ExecuteCommand { witness_name: Option, #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/gates_cmd.rs b/crates/nargo/src/cli/gates_cmd.rs index e04b640a45a..6e6124cd665 100644 --- a/crates/nargo/src/cli/gates_cmd.rs +++ b/crates/nargo/src/cli/gates_cmd.rs @@ -6,13 +6,13 @@ use std::path::Path; use crate::cli::compile_cmd::compile_circuit; use crate::errors::CliError; -use super::{NargoCompileOptions, NargoConfig}; +use super::NargoConfig; /// Counts the occurrences of different gates in circuit #[derive(Debug, Clone, Args)] pub(crate) struct GatesCommand { #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: GatesCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index 4629adfb2ce..bca636b313d 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -41,31 +41,6 @@ pub(crate) struct NargoConfig { program_dir: PathBuf, } -#[derive(Args, Clone, Debug, Default)] -pub(crate) struct NargoCompileOptions { - /// Emit debug information for the intermediate SSA IR - #[arg(short, long)] - pub(crate) show_ssa: bool, - - /// Issue a warning for each unused variable instead of an error - #[arg(short, long)] - pub(crate) allow_warnings: bool, - - /// Display output of `println` statements during tests - #[arg(long)] - pub(crate) show_output: bool, -} - -impl From for CompileOptions { - fn from(value: NargoCompileOptions) -> Self { - let mut options = CompileOptions::default(); - options.show_ssa = value.show_ssa; - options.allow_warnings = value.allow_warnings; - options.show_output = value.show_output; - options - } -} - #[non_exhaustive] #[derive(Subcommand, Clone, Debug)] enum NargoCommand { @@ -104,11 +79,7 @@ pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> boo use tempdir::TempDir; let tmp_dir = TempDir::new("p_and_v_tests").unwrap(); - let compile_options = CompileOptions::from(NargoCompileOptions { - show_ssa, - allow_warnings: false, - show_output: false, - }); + let compile_options = CompileOptions { show_ssa, allow_warnings: false, show_output: false }; match prove_cmd::prove_with_path( Some(proof_name.to_owned()), diff --git a/crates/nargo/src/cli/prove_cmd.rs b/crates/nargo/src/cli/prove_cmd.rs index 10404023132..7fb8f55b72d 100644 --- a/crates/nargo/src/cli/prove_cmd.rs +++ b/crates/nargo/src/cli/prove_cmd.rs @@ -11,7 +11,7 @@ use super::fs::{ program::read_program_from_file, proof::save_proof_to_dir, }; -use super::{NargoCompileOptions, NargoConfig}; +use super::NargoConfig; use crate::{ cli::{execute_cmd::execute_program, verify_cmd::verify_proof}, constants::{PROOFS_DIR, PROVER_INPUT_FILE, TARGET_DIR, VERIFIER_INPUT_FILE}, @@ -32,7 +32,7 @@ pub(crate) struct ProveCommand { verify: bool, #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/test_cmd.rs b/crates/nargo/src/cli/test_cmd.rs index 3183c8f0365..6b30db78857 100644 --- a/crates/nargo/src/cli/test_cmd.rs +++ b/crates/nargo/src/cli/test_cmd.rs @@ -8,7 +8,7 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use crate::{errors::CliError, resolver::Resolver}; -use super::{add_std_lib, NargoCompileOptions, NargoConfig}; +use super::{add_std_lib, NargoConfig}; /// Run the tests for this program #[derive(Debug, Clone, Args)] @@ -17,7 +17,7 @@ pub(crate) struct TestCommand { test_name: Option, #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/nargo/src/cli/verify_cmd.rs b/crates/nargo/src/cli/verify_cmd.rs index fded324f71c..c4a728401ec 100644 --- a/crates/nargo/src/cli/verify_cmd.rs +++ b/crates/nargo/src/cli/verify_cmd.rs @@ -2,7 +2,7 @@ use super::fs::{ inputs::read_inputs_from_file, keys::fetch_pk_and_vk, load_hex_data, program::read_program_from_file, }; -use super::{compile_cmd::compile_circuit, InputMap, NargoCompileOptions, NargoConfig}; +use super::{compile_cmd::compile_circuit, InputMap, NargoConfig}; use crate::{ constants::{PROOFS_DIR, PROOF_EXT, TARGET_DIR, VERIFIER_INPUT_FILE}, errors::CliError, @@ -23,7 +23,7 @@ pub(crate) struct VerifyCommand { circuit_name: Option, #[clap(flatten)] - compile_options: NargoCompileOptions, + compile_options: CompileOptions, } pub(crate) fn run(args: VerifyCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/crates/noirc_driver/Cargo.toml b/crates/noirc_driver/Cargo.toml index d70ec16f3af..3f75dc7da8e 100644 --- a/crates/noirc_driver/Cargo.toml +++ b/crates/noirc_driver/Cargo.toml @@ -7,6 +7,7 @@ edition.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap.workspace = true noirc_errors.workspace = true noirc_frontend.workspace = true noirc_evaluator.workspace = true diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 64d8495a6be..99c11879e72 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -3,6 +3,7 @@ #![warn(unreachable_pub)] use acvm::Language; +use clap::Args; use fm::FileType; use noirc_abi::Abi; use noirc_errors::{reporter, ReportedError}; @@ -22,16 +23,18 @@ pub struct Driver { language: Language, } -#[non_exhaustive] -#[derive(Debug, Default)] +#[derive(Args, Clone, Debug, Default)] pub struct CompileOptions { /// Emit debug information for the intermediate SSA IR + #[arg(short, long)] pub show_ssa: bool, /// Issue a warning for each unused variable instead of an error + #[arg(short, long)] pub allow_warnings: bool, /// Display output of `println` statements during tests + #[arg(long)] pub show_output: bool, } From a1037211698d1216708f06b6731541ab3f1bb9a2 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Mon, 6 Mar 2023 10:29:09 +0000 Subject: [PATCH 8/8] chore: remove noop `from`s for `CompileOptions` --- crates/nargo/src/cli/check_cmd.rs | 2 +- crates/nargo/src/cli/compile_cmd.rs | 2 +- crates/nargo/src/cli/contract_cmd.rs | 3 +-- crates/nargo/src/cli/execute_cmd.rs | 2 +- crates/nargo/src/cli/gates_cmd.rs | 2 +- crates/nargo/src/cli/prove_cmd.rs | 2 +- crates/nargo/src/cli/test_cmd.rs | 2 +- crates/nargo/src/cli/verify_cmd.rs | 7 +------ 8 files changed, 8 insertions(+), 14 deletions(-) diff --git a/crates/nargo/src/cli/check_cmd.rs b/crates/nargo/src/cli/check_cmd.rs index f7d8adc6570..2dc1fc7ff21 100644 --- a/crates/nargo/src/cli/check_cmd.rs +++ b/crates/nargo/src/cli/check_cmd.rs @@ -21,7 +21,7 @@ pub(crate) struct CheckCommand { } pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { - check_from_path(config.program_dir, &CompileOptions::from(args.compile_options))?; + check_from_path(config.program_dir, &args.compile_options)?; println!("Constraint system successfully built!"); Ok(()) } diff --git a/crates/nargo/src/cli/compile_cmd.rs b/crates/nargo/src/cli/compile_cmd.rs index 61db1a72729..f8be06beb5f 100644 --- a/crates/nargo/src/cli/compile_cmd.rs +++ b/crates/nargo/src/cli/compile_cmd.rs @@ -28,7 +28,7 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr &args.circuit_name, config.program_dir, circuit_path, - &CompileOptions::from(args.compile_options), + &args.compile_options, )?; println!("Generated ACIR code into {}", circuit_path.display()); diff --git a/crates/nargo/src/cli/contract_cmd.rs b/crates/nargo/src/cli/contract_cmd.rs index a94bb43bc28..a8f2fb3a8b1 100644 --- a/crates/nargo/src/cli/contract_cmd.rs +++ b/crates/nargo/src/cli/contract_cmd.rs @@ -13,8 +13,7 @@ pub(crate) struct ContractCommand { } pub(crate) fn run(args: ContractCommand, config: NargoConfig) -> Result<(), CliError> { - let compiled_program = - compile_circuit(config.program_dir.clone(), &CompileOptions::from(args.compile_options))?; + let compiled_program = compile_circuit(config.program_dir.clone(), &args.compile_options)?; let backend = crate::backends::ConcreteBackend; #[allow(deprecated)] diff --git a/crates/nargo/src/cli/execute_cmd.rs b/crates/nargo/src/cli/execute_cmd.rs index 98efb1ad721..6ebc3ece62a 100644 --- a/crates/nargo/src/cli/execute_cmd.rs +++ b/crates/nargo/src/cli/execute_cmd.rs @@ -26,7 +26,7 @@ pub(crate) struct ExecuteCommand { pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> { let (return_value, solved_witness) = - execute_with_path(&config.program_dir, &CompileOptions::from(args.compile_options))?; + execute_with_path(&config.program_dir, &args.compile_options)?; println!("Circuit witness successfully solved"); if let Some(return_value) = return_value { diff --git a/crates/nargo/src/cli/gates_cmd.rs b/crates/nargo/src/cli/gates_cmd.rs index 6e6124cd665..71edd4101fe 100644 --- a/crates/nargo/src/cli/gates_cmd.rs +++ b/crates/nargo/src/cli/gates_cmd.rs @@ -16,7 +16,7 @@ pub(crate) struct GatesCommand { } pub(crate) fn run(args: GatesCommand, config: NargoConfig) -> Result<(), CliError> { - count_gates_with_path(config.program_dir, &CompileOptions::from(args.compile_options)) + count_gates_with_path(config.program_dir, &args.compile_options) } fn count_gates_with_path>( diff --git a/crates/nargo/src/cli/prove_cmd.rs b/crates/nargo/src/cli/prove_cmd.rs index 7fb8f55b72d..02bad3a301d 100644 --- a/crates/nargo/src/cli/prove_cmd.rs +++ b/crates/nargo/src/cli/prove_cmd.rs @@ -54,7 +54,7 @@ pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliErro proof_dir, circuit_build_path, args.verify, - &CompileOptions::from(args.compile_options), + &args.compile_options, )?; Ok(()) diff --git a/crates/nargo/src/cli/test_cmd.rs b/crates/nargo/src/cli/test_cmd.rs index 6b30db78857..65e997c53a8 100644 --- a/crates/nargo/src/cli/test_cmd.rs +++ b/crates/nargo/src/cli/test_cmd.rs @@ -23,7 +23,7 @@ pub(crate) struct TestCommand { pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError> { let test_name: String = args.test_name.unwrap_or_else(|| "".to_owned()); - run_tests(&config.program_dir, &test_name, &CompileOptions::from(args.compile_options)) + run_tests(&config.program_dir, &test_name, &args.compile_options) } fn run_tests( diff --git a/crates/nargo/src/cli/verify_cmd.rs b/crates/nargo/src/cli/verify_cmd.rs index c4a728401ec..66a1214da52 100644 --- a/crates/nargo/src/cli/verify_cmd.rs +++ b/crates/nargo/src/cli/verify_cmd.rs @@ -39,12 +39,7 @@ pub(crate) fn run(args: VerifyCommand, config: NargoConfig) -> Result<(), CliErr circuit_build_path }); - verify_with_path( - config.program_dir, - proof_path, - circuit_build_path, - CompileOptions::from(args.compile_options), - ) + verify_with_path(config.program_dir, proof_path, circuit_build_path, args.compile_options) } fn verify_with_path>(