diff --git a/crates/nargo/src/cli/compile_cmd.rs b/crates/nargo/src/cli/compile_cmd.rs index 9d25bfa7182..d60254a6a68 100644 --- a/crates/nargo/src/cli/compile_cmd.rs +++ b/crates/nargo/src/cli/compile_cmd.rs @@ -1,6 +1,5 @@ use std::path::PathBuf; -use acvm::acir::native_types::Witness; use acvm::ProofSystemCompiler; use clap::ArgMatches; @@ -8,7 +7,8 @@ use clap::ArgMatches; use std::path::Path; use crate::{ - constants::{ACIR_EXT, TARGET_DIR, WITNESS_EXT}, + cli::execute_cmd::save_witness_to_dir, + constants::{ACIR_EXT, TARGET_DIR}, errors::CliError, resolver::Resolver, }; @@ -59,12 +59,9 @@ pub fn generate_circuit_and_witness_to_disk>( if generate_witness { let (_, solved_witness) = super::execute_cmd::execute_program(program_dir, &compiled_program)?; - let buf = Witness::to_bytes(&solved_witness); circuit_path.pop(); - circuit_path.push(circuit_name); - circuit_path.set_extension(WITNESS_EXT); - write_to_file(buf.as_slice(), &circuit_path); + save_witness_to_dir(solved_witness, circuit_name, &circuit_path)?; } Ok(circuit_path) diff --git a/crates/nargo/src/cli/execute_cmd.rs b/crates/nargo/src/cli/execute_cmd.rs index db960365d09..60d819abd9b 100644 --- a/crates/nargo/src/cli/execute_cmd.rs +++ b/crates/nargo/src/cli/execute_cmd.rs @@ -1,4 +1,5 @@ use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; use acvm::acir::native_types::Witness; use acvm::FieldElement; @@ -8,22 +9,33 @@ use noirc_abi::errors::AbiError; use noirc_abi::input_parser::{Format, InputValue}; use noirc_abi::MAIN_RETURN_NAME; use noirc_driver::CompiledProgram; -use std::path::Path; -use crate::{constants::PROVER_INPUT_FILE, errors::CliError}; +use crate::{ + constants::{PROVER_INPUT_FILE, TARGET_DIR, WITNESS_EXT}, + errors::CliError, +}; -use super::read_inputs_from_file; +use super::{create_named_dir, read_inputs_from_file, write_to_file}; pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> { let args = args.subcommand_matches("execute").unwrap(); + 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, _) = execute(show_ssa, allow_warnings)?; + let (return_value, solved_witness) = execute(show_ssa, allow_warnings)?; 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(); + witness_dir.push(TARGET_DIR); + + let witness_path = save_witness_to_dir(solved_witness, witness_name, witness_dir)?; + + println!("Witness saved to {}", witness_path.display()); + } Ok(()) } @@ -110,3 +122,19 @@ pub(crate) fn solve_witness( Ok(solved_witness) } + +pub(crate) fn save_witness_to_dir>( + witness: BTreeMap, + witness_name: &str, + witness_dir: P, +) -> Result { + let mut witness_path = create_named_dir(witness_dir.as_ref(), "witness"); + witness_path.push(witness_name); + witness_path.set_extension(WITNESS_EXT); + + let buf = Witness::to_bytes(&witness); + + write_to_file(buf.as_slice(), &witness_path); + + Ok(witness_path) +} diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index f6471817898..2e1960afe49 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -96,6 +96,11 @@ pub fn start_cli() { .subcommand( App::new("execute") .about("Executes a circuit to calculate its return value") + .arg( + Arg::with_name("witness_name") + .long("witness_name") + .help("Write the execution witness to named file"), + ) .arg(show_ssa) .arg(allow_warnings), )