diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index 2faf739585e..7efcc46aaab 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -64,7 +64,7 @@ pub fn start_cli() { .subcommand( App::new("prove") .about("Create proof for this program") - .arg(Arg::with_name("proof_name").help("The name of the proof").required(true)) + .arg(Arg::with_name("proof_name").help("The name of the proof")) .arg(show_ssa.clone()) .arg(allow_warnings.clone()), ) @@ -174,7 +174,7 @@ fn write_inputs_to_file>( 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 proof_path = match prove_cmd::prove_with_path( - proof_name, + Some(proof_name), prg_dir, &tmp_dir.into_path(), show_ssa, @@ -187,7 +187,7 @@ pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> boo } }; - verify_cmd::verify_with_path(prg_dir, &proof_path, show_ssa, false).unwrap() + verify_cmd::verify_with_path(prg_dir, &proof_path.unwrap(), show_ssa, false).unwrap() } fn add_std_lib(driver: &mut Driver) { diff --git a/crates/nargo/src/cli/prove_cmd.rs b/crates/nargo/src/cli/prove_cmd.rs index 81d19b0dccc..bc8667ff605 100644 --- a/crates/nargo/src/cli/prove_cmd.rs +++ b/crates/nargo/src/cli/prove_cmd.rs @@ -17,9 +17,10 @@ use crate::{ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> { let args = args.subcommand_matches("prove").unwrap(); - let proof_name = args.value_of("proof_name").unwrap(); + let proof_name = args.value_of("proof_name"); let show_ssa = args.is_present("show-ssa"); let allow_warnings = args.is_present("allow-warnings"); + prove(proof_name, show_ssa, allow_warnings) } @@ -27,14 +28,39 @@ 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 prove(proof_name: &str, show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> { +fn prove(proof_name: Option<&str>, show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> { let curr_dir = std::env::current_dir().unwrap(); - let mut proof_path = PathBuf::new(); - proof_path.push(PROOFS_DIR); - let result = prove_with_path(proof_name, curr_dir, proof_path, show_ssa, allow_warnings); - match result { - Ok(_) => Ok(()), - Err(e) => Err(e), + + let mut proof_dir = PathBuf::new(); + proof_dir.push(PROOFS_DIR); + + prove_with_path(proof_name, curr_dir, proof_dir, show_ssa, allow_warnings)?; + + Ok(()) +} + +pub fn prove_with_path>( + proof_name: Option<&str>, + program_dir: P, + proof_dir: P, + show_ssa: bool, + allow_warnings: bool, +) -> Result, CliError> { + let (compiled_program, solved_witness) = + compile_circuit_and_witness(program_dir, show_ssa, allow_warnings)?; + + let backend = crate::backends::ConcreteBackend; + let proof = backend.prove_with_meta(compiled_program.circuit, solved_witness); + + println!("Proof successfully created"); + if let Some(proof_name) = proof_name { + let proof_path = save_proof_to_dir(proof, proof_name, proof_dir)?; + + println!("Proof saved to {}", proof_path.display()); + Ok(Some(proof_path)) + } else { + println!("{}", hex::encode(&proof)); + Ok(None) } } @@ -112,28 +138,16 @@ fn solve_witness( Ok(solved_witness) } -pub fn prove_with_path>( +fn save_proof_to_dir>( + proof: Vec, proof_name: &str, - program_dir: P, proof_dir: P, - show_ssa: bool, - allow_warnings: bool, ) -> Result { - let (compiled_program, solved_witness) = - compile_circuit_and_witness(program_dir, show_ssa, allow_warnings)?; - - let backend = crate::backends::ConcreteBackend; - let proof = backend.prove_with_meta(compiled_program.circuit, solved_witness); - let mut proof_path = create_named_dir(proof_dir.as_ref(), "proof"); proof_path.push(proof_name); proof_path.set_extension(PROOF_EXT); - println!("proof : {}", hex::encode(&proof)); - - let path = write_to_file(hex::encode(&proof).as_bytes(), &proof_path); - println!("Proof successfully created and located at {path}"); - println!("{:?}", std::fs::canonicalize(&proof_path)); + write_to_file(hex::encode(proof).as_bytes(), &proof_path); Ok(proof_path) }