Skip to content

Commit

Permalink
chore: create a consolidated options struct for Driver (#908)
Browse files Browse the repository at this point in the history
* chore: create a consolidated options struct for `Driver`

* chore: remove builder and group compile options in clap

* chore: remove unused CliError variants

* chore: clippy

* chore: move compile options back onto `args` from `config`

* chore: copy documentation onto `CompileOptions`

* chore: remove abstraction between nargo and driver interfaces

* chore: remove noop `from`s for `CompileOptions`
  • Loading branch information
TomAFrench authored Mar 6, 2023
1 parent 140762f commit 7264e03
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 105 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 3 additions & 2 deletions crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,17 +27,17 @@ hex.workspace = true
toml.workspace = true
serde.workspace = true
thiserror.workspace = true
clap = { version = "4.1.4", features = ["derive"]}
const_format = "0.2.30"
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"]
Expand Down
31 changes: 21 additions & 10 deletions crates/nargo/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use acvm::ProofSystemCompiler;
use clap::Args;
use iter_extended::btree_map;
use noirc_abi::{AbiParameter, AbiType, MAIN_RETURN_NAME};
use noirc_driver::CompileOptions;
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
Expand All @@ -15,24 +16,23 @@ 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,
#[clap(flatten)]
compile_options: CompileOptions,
}

pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> {
check_from_path(config.program_dir, args.allow_warnings)?;
check_from_path(config.program_dir, &args.compile_options)?;
println!("Constraint system successfully built!");
Ok(())
}
// This is exposed so that we can run the examples and verify that they pass
fn check_from_path<P: AsRef<Path>>(p: P, allow_warnings: bool) -> Result<(), CliError> {

fn check_from_path<P: AsRef<Path>>(p: P, compile_options: &CompileOptions) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;

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(compile_options).is_err() {
std::process::exit(1);
}

Expand Down Expand Up @@ -90,18 +90,21 @@ fn build_placeholder_input_map(

#[cfg(test)]
mod tests {
use noirc_driver::CompileOptions;

const TEST_DATA_DIR: &str = "tests/target_tests_data";

#[test]
fn pass() {
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()
);
Expand All @@ -114,11 +117,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()
);
Expand All @@ -130,10 +134,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()
);
}
}
}
17 changes: 8 additions & 9 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use acvm::acir::circuit::Circuit;
use acvm::ProofSystemCompiler;
use noirc_driver::CompileOptions;
use std::path::{Path, PathBuf};

use clap::Args;
Expand All @@ -15,9 +16,8 @@ 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,
#[clap(flatten)]
compile_options: CompileOptions,
}

pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> {
Expand All @@ -28,7 +28,7 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr
&args.circuit_name,
config.program_dir,
circuit_path,
args.allow_warnings,
&args.compile_options,
)?;

println!("Generated ACIR code into {}", circuit_path.display());
Expand All @@ -40,9 +40,9 @@ fn compile_and_preprocess_circuit<P: AsRef<Path>>(
circuit_name: &str,
program_dir: P,
circuit_dir: P,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<PathBuf, CliError> {
let compiled_program = compile_circuit(program_dir, false, allow_warnings)?;
let compiled_program = compile_circuit(program_dir, compile_options)?;
let circuit_path = save_program_to_file(&compiled_program, circuit_name, &circuit_dir);

preprocess_with_path(circuit_name, circuit_dir, &compiled_program.circuit)?;
Expand All @@ -52,14 +52,13 @@ fn compile_and_preprocess_circuit<P: AsRef<Path>>(

pub(crate) fn compile_circuit<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<noirc_driver::CompiledProgram, CliError> {
let backend = crate::backends::ConcreteBackend;
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(|_| CliError::CompilationError)
driver.into_compiled_program(compile_options).map_err(|_| CliError::CompilationError)
}

fn preprocess_with_path<P: AsRef<Path>>(
Expand Down
8 changes: 4 additions & 4 deletions crates/nargo/src/cli/contract_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ 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,
#[clap(flatten)]
compile_options: CompileOptions,
}

pub(crate) fn run(args: ContractCommand, config: NargoConfig) -> Result<(), CliError> {
let compiled_program = compile_circuit(config.program_dir.clone(), false, args.allow_warnings)?;
let compiled_program = compile_circuit(config.program_dir.clone(), &args.compile_options)?;

let backend = crate::backends::ConcreteBackend;
#[allow(deprecated)]
Expand Down
18 changes: 6 additions & 12 deletions crates/nargo/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,18 +20,13 @@ pub(crate) struct ExecuteCommand {
/// Write the execution witness to named file
witness_name: Option<String>,

/// 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,
#[clap(flatten)]
compile_options: CompileOptions,
}

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, &args.compile_options)?;

println!("Circuit witness successfully solved");
if let Some(return_value) = return_value {
Expand All @@ -50,10 +45,9 @@ pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliEr

fn execute_with_path<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<(Option<InputValue>, 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(
Expand Down
17 changes: 6 additions & 11 deletions crates/nargo/src/cli/gates_cmd.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,25 +11,19 @@ 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,
#[clap(flatten)]
compile_options: CompileOptions,
}

pub(crate) fn run(args: GatesCommand, config: NargoConfig) -> Result<(), CliError> {
count_gates_with_path(config.program_dir, args.show_ssa, args.allow_warnings)
count_gates_with_path(config.program_dir, &args.compile_options)
}

fn count_gates_with_path<P: AsRef<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;

Expand Down
7 changes: 4 additions & 3 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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};

Expand Down Expand Up @@ -80,14 +80,15 @@ 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 { 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) => {
Expand Down
23 changes: 7 additions & 16 deletions crates/nargo/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -30,13 +31,8 @@ pub(crate) struct ProveCommand {
#[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,
#[clap(flatten)]
compile_options: CompileOptions,
}

pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliError> {
Expand All @@ -58,8 +54,7 @@ pub(crate) fn run(args: ProveCommand, config: NargoConfig) -> Result<(), CliErro
proof_dir,
circuit_build_path,
args.verify,
args.show_ssa,
args.allow_warnings,
&args.compile_options,
)?;

Ok(())
Expand All @@ -71,8 +66,7 @@ pub(crate) fn prove_with_path<P: AsRef<Path>>(
proof_dir: P,
circuit_build_path: Option<PathBuf>,
check_proof: bool,
show_ssa: bool,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<Option<PathBuf>, CliError> {
let (compiled_program, proving_key, verification_key) = match circuit_build_path {
Some(circuit_build_path) => {
Expand All @@ -83,11 +77,8 @@ pub(crate) fn prove_with_path<P: AsRef<Path>>(
(compiled_program, proving_key, verification_key)
}
None => {
let compiled_program = super::compile_cmd::compile_circuit(
program_dir.as_ref(),
show_ssa,
allow_warnings,
)?;
let compiled_program =
super::compile_cmd::compile_circuit(program_dir.as_ref(), compile_options)?;

let backend = crate::backends::ConcreteBackend;
let (proving_key, verification_key) = backend.preprocess(&compiled_program.circuit);
Expand Down
Loading

0 comments on commit 7264e03

Please sign in to comment.