Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor execute_cmd #2656

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use acvm::acir::circuit::{Opcode, OpcodeLocation};
use acvm::acir::circuit::OpcodeLocation;
use acvm::acir::{circuit::Circuit, native_types::WitnessMap};
use acvm::pwg::{ErrorLocation, OpcodeResolutionError};
use acvm::Language;
use clap::Args;

use nargo::artifacts::debug::DebugArtifact;
Expand Down Expand Up @@ -53,49 +52,45 @@ pub(crate) fn run(
if args.workspace { PackageSelection::All } else { PackageSelection::DefaultOrAll };
let selection = args.package.map_or(default_selection, PackageSelection::Selected);
let workspace = resolve_workspace_from_toml(&toml_path, selection)?;
let witness_dir = &workspace.target_directory_path();
let target_dir = &workspace.target_directory_path();

let (np_language, is_opcode_supported) = backend.get_backend_info()?;
for package in &workspace {
let (return_value, solved_witness) = execute_package(
backend,
let (compiled_program, debug_artifact) =
compile_bin_package(package, &args.compile_options, np_language, &is_opcode_supported)?;

let (return_value, solved_witness) = execute_program_and_decode(
compiled_program,
debug_artifact,
package,
&args.prover_name,
&args.compile_options,
np_language,
&is_opcode_supported,
)?;

println!("[{}] Circuit witness successfully solved", package.name);
if let Some(return_value) = return_value {
println!("[{}] Circuit output: {return_value:?}", package.name);
}
if let Some(witness_name) = &args.witness_name {
let witness_path = save_witness_to_dir(solved_witness, witness_name, witness_dir)?;
let witness_path = save_witness_to_dir(solved_witness, witness_name, target_dir)?;

println!("[{}] Witness saved to {}", package.name, witness_path.display());
}
}
Ok(())
}

fn execute_package(
backend: &Backend,
fn execute_program_and_decode(
program: CompiledProgram,
debug_artifact: DebugArtifact,
package: &Package,
prover_name: &str,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> Result<(Option<InputValue>, WitnessMap), CliError> {
let (compiled_program, debug_artifact) =
compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?;
let CompiledProgram { abi, circuit, .. } = compiled_program;
let CompiledProgram { abi, circuit, .. } = program;

// Parse the initial witness values from Prover.toml
let (inputs_map, _) =
read_inputs_from_file(&package.root_dir, prover_name, Format::Toml, &abi)?;
let solved_witness =
execute_program(backend, circuit, &abi, &inputs_map, Some(debug_artifact))?;
let solved_witness = execute_program(circuit, &abi, &inputs_map, Some(debug_artifact))?;
let public_abi = abi.public_abi();
let (_, return_value) = public_abi.decode(&solved_witness)?;

Expand Down Expand Up @@ -193,7 +188,6 @@ fn report_error_with_opcode_locations(
}

pub(crate) fn execute_program(
_backend: &Backend,
circuit: Circuit,
abi: &Abi,
inputs_map: &InputMap,
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) fn prove_package(
let (inputs_map, _) =
read_inputs_from_file(&package.root_dir, prover_name, Format::Toml, &abi)?;

let solved_witness = execute_program(backend, bytecode.clone(), &abi, &inputs_map, debug_data)?;
let solved_witness = execute_program(bytecode.clone(), &abi, &inputs_map, debug_data)?;

// Write public inputs into Verifier.toml
let public_abi = abi.public_abi();
Expand Down