From 8adc5cdeb442f9c6874b1a49d0888c230d76588b Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 6 Nov 2023 14:46:54 +0000 Subject: [PATCH 1/5] compile without a backend --- tooling/backend_interface/src/proof_system.rs | 12 +++++ .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 15 ++---- tooling/nargo_cli/src/cli/compile_cmd.rs | 46 +++++++++++++++---- tooling/nargo_cli/src/cli/debug_cmd.rs | 11 +++-- tooling/nargo_cli/src/cli/execute_cmd.rs | 4 +- tooling/nargo_cli/src/cli/info_cmd.rs | 2 +- tooling/nargo_cli/src/cli/prove_cmd.rs | 2 +- tooling/nargo_cli/src/cli/verify_cmd.rs | 2 +- 8 files changed, 66 insertions(+), 28 deletions(-) diff --git a/tooling/backend_interface/src/proof_system.rs b/tooling/backend_interface/src/proof_system.rs index 95da1462d4e..c35e7d51d50 100644 --- a/tooling/backend_interface/src/proof_system.rs +++ b/tooling/backend_interface/src/proof_system.rs @@ -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 + pub fn get_backend_info_or_default(&self) -> (Language, Option) { + 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, diff --git a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs index 59143526b24..b111946e02c 100644 --- a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -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; @@ -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); @@ -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 { - 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)?; diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index 156359dab04..08fdc0618dd 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -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, @@ -98,23 +98,46 @@ pub(super) fn compile_workspace( binary_packages: &[Package], contract_packages: &[Package], np_language: Language, - opcode_support: &BackendOpcodeSupport, + opcode_support: &Option, compile_options: &CompileOptions, ) -> Result<(Vec, Vec), 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)> = 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)> = 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(); @@ -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 { 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, diff --git a/tooling/nargo_cli/src/cli/debug_cmd.rs b/tooling/nargo_cli/src/cli/debug_cmd.rs index 1d344058312..564f31ee070 100644 --- a/tooling/nargo_cli/src/cli/debug_cmd.rs +++ b/tooling/nargo_cli/src/cli/debug_cmd.rs @@ -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) = diff --git a/tooling/nargo_cli/src/cli/execute_cmd.rs b/tooling/nargo_cli/src/cli/execute_cmd.rs index 3a0da2b1134..61bc6516c1e 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -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) = diff --git a/tooling/nargo_cli/src/cli/info_cmd.rs b/tooling/nargo_cli/src/cli/info_cmd.rs index de0c63c3ab3..18292b21fad 100644 --- a/tooling/nargo_cli/src/cli/info_cmd.rs +++ b/tooling/nargo_cli/src/cli/info_cmd.rs @@ -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, diff --git a/tooling/nargo_cli/src/cli/prove_cmd.rs b/tooling/nargo_cli/src/cli/prove_cmd.rs index 3586e73ff2e..161bbce84be 100644 --- a/tooling/nargo_cli/src/cli/prove_cmd.rs +++ b/tooling/nargo_cli/src/cli/prove_cmd.rs @@ -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( diff --git a/tooling/nargo_cli/src/cli/verify_cmd.rs b/tooling/nargo_cli/src/cli/verify_cmd.rs index 8c6d92b3d2f..afb5e1a0f56 100644 --- a/tooling/nargo_cli/src/cli/verify_cmd.rs +++ b/tooling/nargo_cli/src/cli/verify_cmd.rs @@ -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)?; From 507215cd5eafed002da191ed2a863ae083e68f3a Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 6 Nov 2023 16:25:54 +0000 Subject: [PATCH 2/5] typo --- tooling/backend_interface/src/proof_system.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/backend_interface/src/proof_system.rs b/tooling/backend_interface/src/proof_system.rs index c35e7d51d50..e6583a6b114 100644 --- a/tooling/backend_interface/src/proof_system.rs +++ b/tooling/backend_interface/src/proof_system.rs @@ -38,7 +38,7 @@ impl Backend { /// 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 + /// The function also prints a message saying we could not find a backend pub fn get_backend_info_or_default(&self) -> (Language, Option) { if let Ok(backend_info) = self.get_backend_info() { (backend_info.0, Some(backend_info.1)) From cb53c441fc8d7b0aba241063299019b6cb91ce1e Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 6 Nov 2023 16:27:43 +0000 Subject: [PATCH 3/5] update message --- tooling/backend_interface/src/proof_system.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/backend_interface/src/proof_system.rs b/tooling/backend_interface/src/proof_system.rs index e6583a6b114..95c3af6b5d4 100644 --- a/tooling/backend_interface/src/proof_system.rs +++ b/tooling/backend_interface/src/proof_system.rs @@ -43,7 +43,7 @@ impl Backend { if let Ok(backend_info) = self.get_backend_info() { (backend_info.0, Some(backend_info.1)) } else { - println!("No valid backend found"); + println!("No valid backend found, defaulting to Plonk with width 3 and all opcodes supported"); (Language::PLONKCSat { width: 3 }, None) } } From ac1bbac8b76a420ae5d97bae5791ebcaa5eef2e3 Mon Sep 17 00:00:00 2001 From: guipublic Date: Wed, 22 Nov 2023 11:00:57 +0000 Subject: [PATCH 4/5] code review --- tooling/backend_interface/src/lib.rs | 32 ++++++++++++++ tooling/backend_interface/src/proof_system.rs | 6 +-- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 4 +- tooling/nargo_cli/src/cli/compile_cmd.rs | 42 ++++--------------- tooling/nargo_cli/src/cli/debug_cmd.rs | 2 +- tooling/nargo_cli/src/cli/execute_cmd.rs | 2 +- tooling/nargo_cli/src/cli/prove_cmd.rs | 2 +- tooling/nargo_cli/src/cli/verify_cmd.rs | 2 +- 8 files changed, 49 insertions(+), 43 deletions(-) diff --git a/tooling/backend_interface/src/lib.rs b/tooling/backend_interface/src/lib.rs index 6c91c181a92..62cabf29ac3 100644 --- a/tooling/backend_interface/src/lib.rs +++ b/tooling/backend_interface/src/lib.rs @@ -153,6 +153,38 @@ impl BackendOpcodeSupport { } } } + + pub fn all() -> BackendOpcodeSupport { + BackendOpcodeSupport { + opcodes: vec![ + "arithmetic".to_string(), + "directive".to_string(), + "brillig".to_string(), + "memory_init".to_string(), + "memory_op".to_string(), + ] + .into_iter() + .collect(), + black_box_functions: vec![ + "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(), + ] + .into_iter() + .collect(), + } + } } #[cfg(test)] diff --git a/tooling/backend_interface/src/proof_system.rs b/tooling/backend_interface/src/proof_system.rs index 95c3af6b5d4..dcf1dad56b0 100644 --- a/tooling/backend_interface/src/proof_system.rs +++ b/tooling/backend_interface/src/proof_system.rs @@ -39,12 +39,12 @@ impl Backend { /// 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, Option) { + pub fn get_backend_info_or_default(&self) -> (Language, BackendOpcodeSupport) { if let Ok(backend_info) = self.get_backend_info() { - (backend_info.0, Some(backend_info.1)) + (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 }, None) + (Language::PLONKCSat { width: 3 }, BackendOpcodeSupport::all()) } } diff --git a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs index b111946e02c..02c83adb59a 100644 --- a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -54,7 +54,7 @@ pub(crate) fn run( package, &args.compile_options, np_language, - Some(&opcode_support), + &opcode_support, )?; let contract_dir = workspace.contracts_directory_path(package); @@ -74,7 +74,7 @@ fn smart_contract_for_package( package: &Package, compile_options: &CompileOptions, np_language: Language, - opcode_support: Option<&BackendOpcodeSupport>, + opcode_support: &BackendOpcodeSupport, ) -> Result { let program = compile_bin_package(workspace, package, compile_options, np_language, opcode_support)?; diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index 4a427542325..69533292bbd 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -99,46 +99,23 @@ pub(super) fn compile_workspace( binary_packages: &[Package], contract_packages: &[Package], np_language: Language, - opcode_support: &Option, + opcode_support: &BackendOpcodeSupport, compile_options: &CompileOptions, ) -> Result<(Vec, Vec), CliError> { // Compile all of the packages in parallel. let program_results: Vec<(FileManager, CompilationResult)> = binary_packages .par_iter() .map(|package| { - 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, - ) - } + 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)> = contract_packages .par_iter() .map(|package| { - 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) - } + let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode); + compile_contract(package, compile_options, np_language, &is_opcode_supported) }) .collect(); @@ -174,19 +151,16 @@ pub(crate) fn compile_bin_package( package: &Package, compile_options: &CompileOptions, np_language: Language, - opcode_support: Option<&BackendOpcodeSupport>, + opcode_support: &BackendOpcodeSupport, ) -> Result { if package.is_library() { return Err(CompileError::LibraryCrate(package.name.clone()).into()); } - let (file_manager, compilation_result) = if let Some(opcode_support) = opcode_support { + let (file_manager, compilation_result) = 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, diff --git a/tooling/nargo_cli/src/cli/debug_cmd.rs b/tooling/nargo_cli/src/cli/debug_cmd.rs index 564f31ee070..0e7579b0721 100644 --- a/tooling/nargo_cli/src/cli/debug_cmd.rs +++ b/tooling/nargo_cli/src/cli/debug_cmd.rs @@ -61,7 +61,7 @@ pub(crate) fn run( package, &args.compile_options, np_language, - Some(&opcode_support), + &opcode_support, )?; println!("[{}] Starting debugger", package.name); diff --git a/tooling/nargo_cli/src/cli/execute_cmd.rs b/tooling/nargo_cli/src/cli/execute_cmd.rs index 693e36e2576..2f69b4c7df7 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -63,7 +63,7 @@ pub(crate) fn run( package, &args.compile_options, np_language, - opcode_support.as_ref(), + &opcode_support, )?; let (return_value, solved_witness) = diff --git a/tooling/nargo_cli/src/cli/prove_cmd.rs b/tooling/nargo_cli/src/cli/prove_cmd.rs index 161bbce84be..54b148ec3a2 100644 --- a/tooling/nargo_cli/src/cli/prove_cmd.rs +++ b/tooling/nargo_cli/src/cli/prove_cmd.rs @@ -64,7 +64,7 @@ pub(crate) fn run( package, &args.compile_options, np_language, - Some(&opcode_support), + &opcode_support, )?; prove_package( diff --git a/tooling/nargo_cli/src/cli/verify_cmd.rs b/tooling/nargo_cli/src/cli/verify_cmd.rs index afb5e1a0f56..2f8a6efbba4 100644 --- a/tooling/nargo_cli/src/cli/verify_cmd.rs +++ b/tooling/nargo_cli/src/cli/verify_cmd.rs @@ -55,7 +55,7 @@ pub(crate) fn run( package, &args.compile_options, np_language, - Some(&opcode_support), + &opcode_support, )?; verify_package(backend, &workspace, package, program, &args.verifier_name)?; From 0541a53021d16c7034f2f0659aa51f0769193da0 Mon Sep 17 00:00:00 2001 From: Tom French Date: Wed, 22 Nov 2023 11:10:09 +0000 Subject: [PATCH 5/5] chore: simplify `BackendOpcodeSupport::all` --- tooling/backend_interface/src/lib.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tooling/backend_interface/src/lib.rs b/tooling/backend_interface/src/lib.rs index 62cabf29ac3..36ebe5ebb91 100644 --- a/tooling/backend_interface/src/lib.rs +++ b/tooling/backend_interface/src/lib.rs @@ -156,16 +156,14 @@ impl BackendOpcodeSupport { pub fn all() -> BackendOpcodeSupport { BackendOpcodeSupport { - opcodes: vec![ + opcodes: HashSet::from([ "arithmetic".to_string(), "directive".to_string(), "brillig".to_string(), "memory_init".to_string(), "memory_op".to_string(), - ] - .into_iter() - .collect(), - black_box_functions: vec![ + ]), + black_box_functions: HashSet::from([ "sha256".to_string(), "schnorr_verify".to_string(), "blake2s".to_string(), @@ -180,9 +178,7 @@ impl BackendOpcodeSupport { "keccak256".to_string(), "recursive_aggregation".to_string(), "ecdsa_secp256r1".to_string(), - ] - .into_iter() - .collect(), + ]), } } }