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: wrap backend info response in a struct instead of boxed closure #2737

Merged
merged 1 commit into from
Sep 18, 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
37 changes: 14 additions & 23 deletions tooling/acvm_backend_barretenberg/src/cli/info.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use acvm::acir::circuit::opcodes::Opcode;
use acvm::Language;
use serde::Deserialize;
use std::collections::HashSet;
use std::path::{Path, PathBuf};

use crate::BackendError;
use crate::{BackendError, BackendOpcodeSupport};

pub(crate) struct InfoCommand {
pub(crate) crs_path: PathBuf,
Expand All @@ -23,11 +22,20 @@
width: Option<usize>,
}

impl BackendOpcodeSupport {
fn new(info: InfoResponse) -> Self {
let opcodes: HashSet<String> = info.opcodes_supported.into_iter().collect();
let black_box_functions: HashSet<String> =
info.black_box_functions_supported.into_iter().collect();
Self { opcodes, black_box_functions }
}
}

impl InfoCommand {
pub(crate) fn run(
self,
binary_path: &Path,
) -> Result<(Language, Box<impl Fn(&Opcode) -> bool>), BackendError> {
) -> Result<(Language, BackendOpcodeSupport), BackendError> {
let mut command = std::process::Command::new(binary_path);

command.arg("info").arg("-c").arg(self.crs_path).arg("-o").arg("-");
Expand All @@ -41,32 +49,15 @@
let backend_info: InfoResponse =
serde_json::from_slice(&output.stdout).expect("Backend should return valid json");
let language: Language = match backend_info.language.name.as_str() {
"PLONK-CSAT" => {

Check warning on line 52 in tooling/acvm_backend_barretenberg/src/cli/info.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (CSAT)
let width = backend_info.language.width.unwrap();
Language::PLONKCSat { width }
}
"R1CS" => Language::R1CS,
_ => panic!("Unknown langauge"),

Check warning on line 57 in tooling/acvm_backend_barretenberg/src/cli/info.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (langauge)
};

let opcodes_set: HashSet<String> = backend_info.opcodes_supported.into_iter().collect();
let black_box_functions_set: HashSet<String> =
backend_info.black_box_functions_supported.into_iter().collect();

let is_opcode_supported = move |opcode: &Opcode| -> bool {
match opcode {
Opcode::Arithmetic(_) => opcodes_set.contains("arithmetic"),
Opcode::Directive(_) => opcodes_set.contains("directive"),
Opcode::Brillig(_) => opcodes_set.contains("brillig"),
Opcode::MemoryInit { .. } => opcodes_set.contains("memory_init"),
Opcode::MemoryOp { .. } => opcodes_set.contains("memory_op"),
Opcode::BlackBoxFuncCall(func) => {
black_box_functions_set.contains(func.get_black_box_func().name())
}
}
};

Ok((language, Box::new(is_opcode_supported)))
Ok((language, BackendOpcodeSupport::new(backend_info)))
}
}

Expand All @@ -79,10 +70,10 @@
let backend = crate::get_mock_backend()?;
let crs_path = backend.backend_directory();

let (language, is_opcode_supported) = InfoCommand { crs_path }.run(backend.binary_path())?;
let (language, opcode_support) = InfoCommand { crs_path }.run(backend.binary_path())?;

assert!(matches!(language, Language::PLONKCSat { width: 3 }));
assert!(is_opcode_supported(&Opcode::Arithmetic(Expression::default())));
assert!(opcode_support.is_opcode_supported(&Opcode::Arithmetic(Expression::default())));

Ok(())
}
23 changes: 22 additions & 1 deletion tooling/acvm_backend_barretenberg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![warn(unused_crate_dependencies, unused_extern_crates)]
#![warn(unreachable_pub)]

use std::path::PathBuf;
use std::{collections::HashSet, path::PathBuf};

mod cli;
mod download;
mod proof_system;
mod smart_contract;

use acvm::acir::circuit::Opcode;
pub use download::download_backend;

const BACKENDS_DIR: &str = ".nargo/backends";
Expand Down Expand Up @@ -96,13 +97,33 @@
}
}

pub struct BackendOpcodeSupport {
opcodes: HashSet<String>,
black_box_functions: HashSet<String>,
}

impl BackendOpcodeSupport {
pub fn is_opcode_supported(&self, opcode: &Opcode) -> bool {
match opcode {
Opcode::Arithmetic(_) => self.opcodes.contains("arithmetic"),
Opcode::Directive(_) => self.opcodes.contains("directive"),
Opcode::Brillig(_) => self.opcodes.contains("brillig"),
Opcode::MemoryInit { .. } => self.opcodes.contains("memory_init"),
Opcode::MemoryOp { .. } => self.opcodes.contains("memory_op"),
Opcode::BlackBoxFuncCall(func) => {
self.black_box_functions.contains(func.get_black_box_func().name())
}
}
}
}

#[cfg(test)]
mod backend {
use crate::{Backend, BackendError};

#[test]
fn raises_error_on_missing_binary() {
let bad_backend = Backend::new("i_dont_exist".to_string());

Check warning on line 126 in tooling/acvm_backend_barretenberg/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (dont)

let binary_path = bad_backend.assert_binary_exists();

Expand Down
7 changes: 2 additions & 5 deletions tooling/acvm_backend_barretenberg/src/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

use acvm::acir::circuit::Opcode;
use acvm::acir::{circuit::Circuit, native_types::WitnessMap};
use acvm::FieldElement;
use acvm::Language;
use tempfile::tempdir;

use crate::cli::{GatesCommand, InfoCommand, ProveCommand, VerifyCommand, WriteVkCommand};
use crate::{Backend, BackendError};
use crate::{Backend, BackendError, BackendOpcodeSupport};

impl Backend {
pub fn get_exact_circuit_size(&self, circuit: &Circuit) -> Result<u32, BackendError> {
Expand All @@ -27,9 +26,7 @@ impl Backend {
.run(binary_path)
}

pub fn get_backend_info(
&self,
) -> Result<(Language, Box<impl Fn(&Opcode) -> bool>), BackendError> {
pub fn get_backend_info(&self) -> Result<(Language, BackendOpcodeSupport), BackendError> {
let binary_path = self.assert_binary_exists()?;
InfoCommand { crs_path: self.crs_directory() }.run(binary_path)
}
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) fn run(
let selection = args.package.map_or(default_selection, PackageSelection::Selected);
let workspace = resolve_workspace_from_toml(&toml_path, selection)?;

let (np_language, is_opcode_supported) = backend.get_backend_info()?;
let (np_language, opcode_support) = backend.get_backend_info()?;
for package in &workspace {
let circuit_build_path = workspace.package_build_path(package);

Expand All @@ -56,7 +56,7 @@ pub(crate) fn run(
circuit_build_path,
&args.compile_options,
np_language,
&is_opcode_supported,
&|opcode| opcode_support.is_opcode_supported(opcode),
)?;

let contract_dir = workspace.contracts_directory_path(package);
Expand Down
16 changes: 12 additions & 4 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::Path;

use acvm::acir::circuit::Opcode;
use acvm::Language;
use acvm_backend_barretenberg::BackendOpcodeSupport;
use fm::FileManager;
use iter_extended::vecmap;
use nargo::artifacts::contract::PreprocessedContract;
Expand Down Expand Up @@ -69,8 +70,14 @@ pub(crate) fn run(
.cloned()
.partition(|package| package.is_binary());

let (compiled_programs, compiled_contracts) =
compile_workspace(backend, &binary_packages, &contract_packages, &args.compile_options)?;
let (np_language, opcode_support) = backend.get_backend_info()?;
let (compiled_programs, compiled_contracts) = compile_workspace(
&binary_packages,
&contract_packages,
np_language,
&opcode_support,
&args.compile_options,
)?;

// Save build artifacts to disk.
for (package, program) in binary_packages.into_iter().zip(compiled_programs) {
Expand All @@ -84,12 +91,13 @@ pub(crate) fn run(
}

pub(super) fn compile_workspace(
backend: &Backend,
binary_packages: &[Package],
contract_packages: &[Package],
np_language: Language,
opcode_support: &BackendOpcodeSupport,
compile_options: &CompileOptions,
) -> Result<(Vec<CompiledProgram>, Vec<CompiledContract>), CliError> {
let (np_language, is_opcode_supported) = backend.get_backend_info()?;
let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode);

// Compile all of the packages in parallel.
let program_results: Vec<(FileManager, CompilationResult<CompiledProgram>)> = binary_packages
Expand Down
6 changes: 4 additions & 2 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ pub(crate) fn run(
let workspace = resolve_workspace_from_toml(&toml_path, selection)?;
let target_dir = &workspace.target_directory_path();

let (np_language, is_opcode_supported) = backend.get_backend_info()?;
let (np_language, opcode_support) = backend.get_backend_info()?;
for package in &workspace {
let compiled_program =
compile_bin_package(package, &args.compile_options, np_language, &is_opcode_supported)?;
compile_bin_package(package, &args.compile_options, np_language, &|opcode| {
opcode_support.is_opcode_supported(opcode)
})?;

let (return_value, solved_witness) =
execute_program_and_decode(compiled_program, package, &args.prover_name)?;
Expand Down
11 changes: 8 additions & 3 deletions tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ pub(crate) fn run(
.cloned()
.partition(|package| package.is_binary());

let (compiled_programs, compiled_contracts) =
compile_workspace(backend, &binary_packages, &contract_packages, &args.compile_options)?;
let (np_language, opcode_support) = backend.get_backend_info()?;
let (compiled_programs, compiled_contracts) = compile_workspace(
&binary_packages,
&contract_packages,
np_language,
&opcode_support,
&args.compile_options,
)?;

let (np_language, _) = backend.get_backend_info()?;
let program_info = binary_packages
.into_par_iter()
.zip(compiled_programs)
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn run(
let workspace = resolve_workspace_from_toml(&toml_path, selection)?;
let proof_dir = workspace.proofs_directory_path();

let (np_language, is_opcode_supported) = backend.get_backend_info()?;
let (np_language, opcode_support) = backend.get_backend_info()?;
for package in &workspace {
let circuit_build_path = workspace.package_build_path(package);

Expand All @@ -77,7 +77,7 @@ pub(crate) fn run(
args.verify,
&args.compile_options,
np_language,
&is_opcode_supported,
&|opcode| opcode_support.is_opcode_supported(opcode),
)?;
}

Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn run(
let workspace = resolve_workspace_from_toml(&toml_path, selection)?;
let proofs_dir = workspace.proofs_directory_path();

let (np_language, is_opcode_supported) = backend.get_backend_info()?;
let (np_language, opcode_support) = backend.get_backend_info()?;
for package in &workspace {
let circuit_build_path = workspace.package_build_path(package);

Expand All @@ -64,7 +64,7 @@ pub(crate) fn run(
&args.verifier_name,
&args.compile_options,
np_language,
&is_opcode_supported,
&|opcode| opcode_support.is_opcode_supported(opcode),
)?;
}

Expand Down