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

feat: compile without a backend #3437

Merged
merged 10 commits into from
Nov 22, 2023
12 changes: 12 additions & 0 deletions tooling/backend_interface/src/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ impl Backend {
InfoCommand { crs_path: self.crs_directory() }.run(binary_path)
}

/// If we cannot get a valid backend, returns the default backend which supports all the opcodes
/// and uses Plonk with width 3
/// The function also print a message saying we could not find a backend
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_backend_info_or_default(&self) -> (Language, Option<BackendOpcodeSupport>) {
if let Ok(backend_info) = self.get_backend_info() {
(backend_info.0, Some(backend_info.1))
} else {
println!("No valid backend found");
(Language::PLONKCSat { width: 3 }, None)
}
}

pub fn prove(
&self,
circuit: &Circuit,
Expand Down
15 changes: 5 additions & 10 deletions tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use super::{
use crate::backends::Backend;
use crate::errors::CliError;

use acvm::acir::circuit::Opcode;
use acvm::Language;
use backend_interface::BackendOpcodeSupport;
use bb_abstraction_leaks::ACVM_BACKEND_BARRETENBERG;
use clap::Args;
use nargo::package::Package;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub(crate) fn run(
package,
&args.compile_options,
np_language,
&|opcode| opcode_support.is_opcode_supported(opcode),
Some(&opcode_support),
)?;

let contract_dir = workspace.contracts_directory_path(package);
Expand All @@ -74,15 +74,10 @@ fn smart_contract_for_package(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
opcode_support: Option<&BackendOpcodeSupport>,
) -> Result<String, CliError> {
let program = compile_bin_package(
workspace,
package,
compile_options,
np_language,
&is_opcode_supported,
)?;
let program =
compile_bin_package(workspace, package, compile_options, np_language, opcode_support)?;

let mut smart_contract_string = backend.eth_contract(&program.circuit)?;

Expand Down
46 changes: 37 additions & 9 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub(crate) fn run(
.cloned()
.partition(|package| package.is_binary());

let (np_language, opcode_support) = backend.get_backend_info()?;
let (np_language, opcode_support) = backend.get_backend_info_or_default();
let (_, compiled_contracts) = compile_workspace(
&workspace,
&binary_packages,
Expand All @@ -98,23 +98,46 @@ pub(super) fn compile_workspace(
binary_packages: &[Package],
contract_packages: &[Package],
np_language: Language,
opcode_support: &BackendOpcodeSupport,
opcode_support: &Option<BackendOpcodeSupport>,
compile_options: &CompileOptions,
) -> Result<(Vec<CompiledProgram>, Vec<CompiledContract>), CliError> {
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
.par_iter()
.map(|package| {
compile_program(workspace, package, compile_options, np_language, &is_opcode_supported)
if let Some(opcode_support) = opcode_support {
let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode);
compile_program(
workspace,
package,
compile_options,
np_language,
&is_opcode_supported,
)
} else {
let is_opcode_supported = |_opcode: &_| true;
compile_program(
workspace,
package,
compile_options,
np_language,
&is_opcode_supported,
)
}
})
.collect();
let contract_results: Vec<(FileManager, CompilationResult<CompiledContract>)> =
contract_packages
.par_iter()
.map(|package| {
compile_contract(package, compile_options, np_language, &is_opcode_supported)
if let Some(opcode_support) = opcode_support {
let is_opcode_supported =
|opcode: &_| opcode_support.is_opcode_supported(opcode);
compile_contract(package, compile_options, np_language, &is_opcode_supported)
} else {
let is_opcode_supported = |_opcode: &_| true;
compile_contract(package, compile_options, np_language, &is_opcode_supported)
}
})
.collect();

Expand Down Expand Up @@ -150,14 +173,19 @@ pub(crate) fn compile_bin_package(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
opcode_support: Option<&BackendOpcodeSupport>,
) -> Result<CompiledProgram, CliError> {
if package.is_library() {
return Err(CompileError::LibraryCrate(package.name.clone()).into());
}

let (file_manager, compilation_result) =
compile_program(workspace, package, compile_options, np_language, &is_opcode_supported);
let (file_manager, compilation_result) = if let Some(opcode_support) = opcode_support {
compile_program(workspace, package, compile_options, np_language, &|opcode| {
opcode_support.is_opcode_supported(opcode)
})
} else {
compile_program(workspace, package, compile_options, np_language, &|_opcode| true)
};

let program = report_errors(
compilation_result,
Expand Down
11 changes: 7 additions & 4 deletions tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ pub(crate) fn run(
return Ok(());
};

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

println!("[{}] Starting debugger", package.name);
let (return_value, solved_witness) =
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ pub(crate) fn run(
)?;
let target_dir = &workspace.target_directory_path();

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

let (return_value, solved_witness) =
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) fn run(
.cloned()
.partition(|package| package.is_binary());

let (np_language, opcode_support) = backend.get_backend_info()?;
let (np_language, opcode_support) = backend.get_backend_info_or_default();
let (compiled_programs, compiled_contracts) = compile_workspace(
&workspace,
&binary_packages,
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 @@ -64,7 +64,7 @@ pub(crate) fn run(
package,
&args.compile_options,
np_language,
&|opcode| opcode_support.is_opcode_supported(opcode),
Some(&opcode_support),
)?;

prove_package(
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn run(
package,
&args.compile_options,
np_language,
&|opcode| opcode_support.is_opcode_supported(opcode),
Some(&opcode_support),
)?;

verify_package(backend, &workspace, package, program, &args.verifier_name)?;
Expand Down
Loading