Skip to content

Commit

Permalink
feat(nargo): add option to save witness to file in execute command
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 1, 2023
1 parent fadf1b6 commit 42a649c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
9 changes: 3 additions & 6 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::path::PathBuf;

use acvm::acir::native_types::Witness;
use acvm::ProofSystemCompiler;

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,
};
Expand Down Expand Up @@ -59,12 +59,9 @@ pub fn generate_circuit_and_witness_to_disk<P: AsRef<Path>>(
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)
Expand Down
36 changes: 32 additions & 4 deletions crates/nargo/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use acvm::acir::native_types::Witness;
use acvm::FieldElement;
Expand All @@ -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(())
}

Expand Down Expand Up @@ -110,3 +122,19 @@ pub(crate) fn solve_witness(

Ok(solved_witness)
}

pub(crate) fn save_witness_to_dir<P: AsRef<Path>>(
witness: BTreeMap<Witness, FieldElement>,
witness_name: &str,
witness_dir: P,
) -> Result<PathBuf, CliError> {
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)
}
5 changes: 5 additions & 0 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down

0 comments on commit 42a649c

Please sign in to comment.