Skip to content

Commit

Permalink
chore(nargo): abstract paths from nargo's working directory (#761)
Browse files Browse the repository at this point in the history
* chore(nargo): handle paths more consistently

* fix(nargo): correct `witness_name` argument usage

* chore(nargo): clippy

* chore: make `run_tests` take a &Path only

---------

Co-authored-by: Kevaundray Wedderburn <kevtheappdev@gmail.com>
  • Loading branch information
TomAFrench and kevaundray authored Feb 11, 2023
1 parent 87fef67 commit 79d34fc
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 64 deletions.
6 changes: 4 additions & 2 deletions crates/nargo/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("check").unwrap();
let allow_warnings = args.is_present("allow-warnings");

let package_dir = std::env::current_dir().unwrap();
check_from_path(package_dir, allow_warnings)?;
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

check_from_path(program_dir, allow_warnings)?;
println!("Constraint system successfully built!");
Ok(())
}
Expand Down
7 changes: 4 additions & 3 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let circuit_name = args.value_of("circuit_name").unwrap();
let witness = args.is_present("witness");
let allow_warnings = args.is_present("allow-warnings");
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let current_dir = std::env::current_dir().unwrap();
let mut circuit_path = PathBuf::new();
let mut circuit_path = program_dir.clone();
circuit_path.push(TARGET_DIR);

generate_circuit_and_witness_to_disk(
circuit_name,
current_dir,
program_dir,
circuit_path,
witness,
allow_warnings,
Expand Down
18 changes: 10 additions & 8 deletions crates/nargo/src/cli/contract_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::path::PathBuf;

use super::{create_named_dir, write_to_file};
use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError};
use acvm::SmartContract;
use clap::ArgMatches;

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let cmd = args.subcommand_matches("contract").unwrap();

let package_dir = match cmd.value_of("path") {
Some(path) => std::path::PathBuf::from(path),
None => std::env::current_dir().unwrap(),
};
let args = args.subcommand_matches("contract").unwrap();

let allow_warnings = args.is_present("allow-warnings");
let compiled_program = compile_circuit(package_dir, false, allow_warnings)?;
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let compiled_program = compile_circuit(program_dir.clone(), false, allow_warnings)?;

let backend = crate::backends::ConcreteBackend;
let smart_contract_string = backend.eth_contract_from_cs(compiled_program.circuit);

let mut contract_path = create_named_dir(CONTRACT_DIR.as_ref(), "contract");
let mut contract_dir = program_dir;
contract_dir.push(CONTRACT_DIR);
let mut contract_path = create_named_dir(contract_dir.as_ref(), "contract");
contract_path.push("plonk_vk");
contract_path.set_extension("sol");

Expand Down
21 changes: 7 additions & 14 deletions crates/nargo/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use noirc_driver::CompiledProgram;
use super::{create_named_dir, read_inputs_from_file, write_to_file};
use super::{InputMap, WitnessMap};
use crate::{
cli::compile_cmd::compile_circuit,
constants::{PROVER_INPUT_FILE, TARGET_DIR, WITNESS_EXT},
errors::CliError,
};
Expand All @@ -21,14 +22,18 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let witness_name = args.value_of("witness_name");
let show_ssa = args.is_present("show-ssa");
let allow_warnings = args.is_present("allow-warnings");
let (return_value, solved_witness) = execute(show_ssa, allow_warnings)?;
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let compiled_program = compile_circuit(&program_dir, show_ssa, allow_warnings)?;
let (return_value, solved_witness) = execute_program(&program_dir, &compiled_program)?;

println!("Circuit witness successfully solved");
if let Some(return_value) = return_value {
println!("Circuit output: {return_value:?}");
}
if let Some(witness_name) = witness_name {
let mut witness_dir = std::env::current_dir().unwrap();
let mut witness_dir = program_dir;
witness_dir.push(TARGET_DIR);

let witness_path = save_witness_to_dir(solved_witness, witness_name, witness_dir)?;
Expand All @@ -42,18 +47,6 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
/// So when we add witness values, their index start from 1.
const WITNESS_OFFSET: u32 = 1;

fn execute(
show_ssa: bool,
allow_warnings: bool,
) -> Result<(Option<InputValue>, WitnessMap), CliError> {
let current_dir = std::env::current_dir().unwrap();

let compiled_program =
super::compile_cmd::compile_circuit(&current_dir, show_ssa, allow_warnings)?;

execute_program(current_dir, &compiled_program)
}

pub(crate) fn execute_program<P: AsRef<Path>>(
inputs_dir: P,
compiled_program: &CompiledProgram,
Expand Down
10 changes: 4 additions & 6 deletions crates/nargo/src/cli/gates_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use acvm::ProofSystemCompiler;
use clap::ArgMatches;
use std::path::Path;
use std::path::{Path, PathBuf};

use crate::cli::compile_cmd::compile_circuit;
use crate::errors::CliError;
Expand All @@ -9,12 +9,10 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("gates").unwrap();
let show_ssa = args.is_present("show-ssa");
let allow_warnings = args.is_present("allow-warnings");
count_gates(show_ssa, allow_warnings)
}
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

pub fn count_gates(show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> {
let current_dir = std::env::current_dir().unwrap();
count_gates_with_path(current_dir, show_ssa, allow_warnings)
count_gates_with_path(program_dir, show_ssa, allow_warnings)
}

pub fn count_gates_with_path<P: AsRef<Path>>(
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub fn start_cli() {
.arg(
Arg::with_name("witness_name")
.long("witness_name")
.help("Write the execution witness to named file"),
.help("Write the execution witness to named file")
.takes_value(true),
)
.arg(show_ssa)
.arg(allow_warnings),
Expand Down
9 changes: 4 additions & 5 deletions crates/nargo/src/cli/new_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use crate::{

use super::{create_named_dir, write_to_file};
use clap::ArgMatches;
use std::path::Path;
use std::path::{Path, PathBuf};

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let cmd = args.subcommand_matches("new").unwrap();

let package_name = cmd.value_of("package_name").unwrap();

let mut package_dir = match cmd.value_of("path") {
Some(path) => std::path::PathBuf::from(path),
None => std::env::current_dir().unwrap(),
};
let mut package_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

package_dir.push(Path::new(package_name));
if package_dir.exists() {
return Err(CliError::DestinationAlreadyExists(package_dir));
Expand Down
11 changes: 4 additions & 7 deletions crates/nargo/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let show_ssa = args.is_present("show-ssa");
let allow_warnings = args.is_present("allow-warnings");

prove(proof_name, show_ssa, allow_warnings)
}

fn prove(proof_name: Option<&str>, show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> {
let current_dir = std::env::current_dir().unwrap();
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let mut proof_dir = PathBuf::new();
let mut proof_dir = program_dir.clone();
proof_dir.push(PROOFS_DIR);

prove_with_path(proof_name, current_dir, proof_dir, show_ssa, allow_warnings)?;
prove_with_path(proof_name, program_dir, proof_dir, show_ssa, allow_warnings)?;

Ok(())
}
Expand Down
16 changes: 11 additions & 5 deletions crates/nargo/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{collections::BTreeMap, io::Write};
use std::{
collections::BTreeMap,
io::Write,
path::{Path, PathBuf},
};

use acvm::{PartialWitnessGenerator, ProofSystemCompiler};
use clap::ArgMatches;
Expand All @@ -14,14 +18,16 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("test").unwrap();
let test_name = args.value_of("test_name").unwrap_or("");
let allow_warnings = args.is_present("allow-warnings");
run_tests(test_name, allow_warnings)
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

run_tests(&program_dir, test_name, allow_warnings)
}

fn run_tests(test_name: &str, allow_warnings: bool) -> Result<(), CliError> {
fn run_tests(program_dir: &Path, test_name: &str, allow_warnings: bool) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;

let package_dir = std::env::current_dir().unwrap();
let mut driver = Resolver::resolve_root_config(&package_dir, backend.np_language())?;
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() {
Expand Down
22 changes: 9 additions & 13 deletions crates/nargo/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,29 @@ use clap::ArgMatches;
use noirc_abi::errors::AbiError;
use noirc_abi::input_parser::Format;
use noirc_driver::CompiledProgram;
use std::{collections::BTreeMap, path::Path, path::PathBuf};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("verify").unwrap();

let proof_name = args.value_of("proof").unwrap();
let mut proof_path = std::path::PathBuf::new();
proof_path.push(Path::new(PROOFS_DIR));
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let mut proof_path = program_dir.clone();
proof_path.push(Path::new(PROOFS_DIR));
proof_path.push(Path::new(proof_name));
proof_path.set_extension(PROOF_EXT);

let allow_warnings = args.is_present("allow-warnings");
let result = verify(proof_name, allow_warnings)?;
let result = verify_with_path(program_dir, proof_path, false, allow_warnings)?;
println!("Proof verified : {result}\n");
Ok(())
}

fn verify(proof_name: &str, allow_warnings: bool) -> Result<bool, CliError> {
let current_dir = std::env::current_dir().unwrap();
let mut proof_path = PathBuf::new(); //or cur_dir?
proof_path.push(PROOFS_DIR);
proof_path.push(Path::new(proof_name));
proof_path.set_extension(PROOF_EXT);
verify_with_path(&current_dir, &proof_path, false, allow_warnings)
}

pub fn verify_with_path<P: AsRef<Path>>(
program_dir: P,
proof_path: P,
Expand Down

0 comments on commit 79d34fc

Please sign in to comment.