Skip to content

Commit

Permalink
feat: compile without a backend (#3437)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <[email protected]>
Co-authored-by: Tom French <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2023
1 parent eec92e4 commit d69cf5d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 24 deletions.
28 changes: 28 additions & 0 deletions tooling/backend_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ impl BackendOpcodeSupport {
}
}
}

pub fn all() -> BackendOpcodeSupport {
BackendOpcodeSupport {
opcodes: HashSet::from([
"arithmetic".to_string(),
"directive".to_string(),
"brillig".to_string(),
"memory_init".to_string(),
"memory_op".to_string(),
]),
black_box_functions: HashSet::from([
"sha256".to_string(),
"schnorr_verify".to_string(),
"blake2s".to_string(),
"pedersen".to_string(),
"pedersen_hash".to_string(),
"hash_to_field_128_security".to_string(),
"ecdsa_secp256k1".to_string(),
"fixed_base_scalar_mul".to_string(),
"and".to_string(),
"xor".to_string(),
"range".to_string(),
"keccak256".to_string(),
"recursive_aggregation".to_string(),
"ecdsa_secp256r1".to_string(),
]),
}
}
}

#[cfg(test)]
Expand Down
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 prints a message saying we could not find a backend
pub fn get_backend_info_or_default(&self) -> (Language, BackendOpcodeSupport) {
if let Ok(backend_info) = self.get_backend_info() {
(backend_info.0, backend_info.1)
} else {
println!("No valid backend found, defaulting to Plonk with width 3 and all opcodes supported");
(Language::PLONKCSat { width: 3 }, BackendOpcodeSupport::all())
}
}

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),
&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: &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
12 changes: 7 additions & 5 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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 @@ -102,19 +102,19 @@ pub(super) fn compile_workspace(
opcode_support: &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| {
let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode);
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| {
let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode);
compile_contract(package, compile_options, np_language, &is_opcode_supported)
})
.collect();
Expand Down Expand Up @@ -151,14 +151,16 @@ pub(crate) fn compile_bin_package(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
opcode_support: &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);
compile_program(workspace, package, compile_options, np_language, &|opcode| {
opcode_support.is_opcode_supported(opcode)
});

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,
&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 @@ -56,14 +56,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,
)?;

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 @@ -67,7 +67,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),
&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),
&opcode_support,
)?;

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

0 comments on commit d69cf5d

Please sign in to comment.