From 9420d7c2ba6bbbf5ecb9a066837c505310955b6c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 22 May 2024 14:55:28 +0100 Subject: [PATCH] feat: remove conditional compilation of `bn254_blackbox_solver` (#5058) # Description ## Problem\* Resolves ## Summary\* This PR removes the conditional compilation on `Bn254BlackBoxSolver` as it's no longer necessary. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- acvm-repo/acvm_js/src/execute.rs | 57 ++++++------------- .../benches/criterion.rs | 12 ++-- acvm-repo/bn254_blackbox_solver/src/lib.rs | 19 +------ tooling/acvm_cli/src/cli/execute_cmd.rs | 3 +- tooling/nargo_cli/src/cli/dap_cmd.rs | 5 +- tooling/nargo_cli/src/cli/debug_cmd.rs | 4 +- tooling/nargo_cli/src/cli/execute_cmd.rs | 4 +- tooling/nargo_cli/src/cli/lsp_cmd.rs | 3 +- 8 files changed, 30 insertions(+), 77 deletions(-) diff --git a/acvm-repo/acvm_js/src/execute.rs b/acvm-repo/acvm_js/src/execute.rs index 338511874c9..9f2b07b31fc 100644 --- a/acvm-repo/acvm_js/src/execute.rs +++ b/acvm-repo/acvm_js/src/execute.rs @@ -20,11 +20,11 @@ use crate::{ }; #[wasm_bindgen] -pub struct WasmBlackBoxFunctionSolver(Bn254BlackBoxSolver); +pub struct WasmBlackBoxFunctionSolver; impl WasmBlackBoxFunctionSolver { async fn initialize() -> WasmBlackBoxFunctionSolver { - WasmBlackBoxFunctionSolver(Bn254BlackBoxSolver::initialize().await) + WasmBlackBoxFunctionSolver } } @@ -47,15 +47,9 @@ pub async fn execute_circuit( ) -> Result { console_error_panic_hook::set_once(); - let solver = WasmBlackBoxFunctionSolver::initialize().await; - - let mut witness_stack = execute_program_with_native_type_return( - &solver, - program, - initial_witness, - &foreign_call_handler, - ) - .await?; + let mut witness_stack = + execute_program_with_native_type_return(program, initial_witness, &foreign_call_handler) + .await?; let witness_map = witness_stack.pop().expect("Should have at least one witness on the stack").witness; Ok(witness_map.into()) @@ -71,7 +65,7 @@ pub async fn execute_circuit( /// @returns {SolvedAndReturnWitness} The solved witness calculated by executing the circuit on the provided inputs, as well as the return witness indices as specified by the circuit. #[wasm_bindgen(js_name = executeCircuitWithReturnWitness, skip_jsdoc)] pub async fn execute_circuit_with_return_witness( - solver: &WasmBlackBoxFunctionSolver, + _solver: &WasmBlackBoxFunctionSolver, program: Vec, initial_witness: JsWitnessMap, foreign_call_handler: ForeignCallHandler, @@ -82,7 +76,6 @@ pub async fn execute_circuit_with_return_witness( .map_err(|_| JsExecutionError::new("Failed to deserialize circuit. This is likely due to differing serialization formats between ACVM_JS and your compiler".to_string(), None, None))?; let mut witness_stack = execute_program_with_native_program_and_return( - solver, &program, initial_witness, &foreign_call_handler, @@ -108,20 +101,16 @@ pub async fn execute_circuit_with_return_witness( /// @returns {WitnessMap} The solved witness calculated by executing the circuit on the provided inputs. #[wasm_bindgen(js_name = executeCircuitWithBlackBoxSolver, skip_jsdoc)] pub async fn execute_circuit_with_black_box_solver( - solver: &WasmBlackBoxFunctionSolver, + _solver: &WasmBlackBoxFunctionSolver, program: Vec, initial_witness: JsWitnessMap, foreign_call_handler: ForeignCallHandler, ) -> Result { console_error_panic_hook::set_once(); - let mut witness_stack = execute_program_with_native_type_return( - solver, - program, - initial_witness, - &foreign_call_handler, - ) - .await?; + let mut witness_stack = + execute_program_with_native_type_return(program, initial_witness, &foreign_call_handler) + .await?; let witness_map = witness_stack.pop().expect("Should have at least one witness on the stack").witness; Ok(witness_map.into()) @@ -143,24 +132,19 @@ pub async fn execute_program( #[wasm_bindgen(js_name = executeProgramWithBlackBoxSolver, skip_jsdoc)] pub async fn execute_program_with_black_box_solver( - solver: &WasmBlackBoxFunctionSolver, + _solver: &WasmBlackBoxFunctionSolver, program: Vec, initial_witness: JsWitnessMap, foreign_call_executor: &ForeignCallHandler, ) -> Result { - let witness_stack = execute_program_with_native_type_return( - solver, - program, - initial_witness, - foreign_call_executor, - ) - .await?; + let witness_stack = + execute_program_with_native_type_return(program, initial_witness, foreign_call_executor) + .await?; Ok(witness_stack.into()) } async fn execute_program_with_native_type_return( - solver: &WasmBlackBoxFunctionSolver, program: Vec, initial_witness: JsWitnessMap, foreign_call_executor: &ForeignCallHandler, @@ -171,25 +155,20 @@ async fn execute_program_with_native_type_return( None, None))?; - execute_program_with_native_program_and_return( - solver, - &program, - initial_witness, - foreign_call_executor, - ) - .await + execute_program_with_native_program_and_return(&program, initial_witness, foreign_call_executor) + .await } async fn execute_program_with_native_program_and_return( - solver: &WasmBlackBoxFunctionSolver, program: &Program, initial_witness: JsWitnessMap, foreign_call_executor: &ForeignCallHandler, ) -> Result { + let blackbox_solver = Bn254BlackBoxSolver; let executor = ProgramExecutor::new( &program.functions, &program.unconstrained_functions, - &solver.0, + &blackbox_solver, foreign_call_executor, ); let witness_stack = executor.execute(initial_witness.into()).await?; diff --git a/acvm-repo/bn254_blackbox_solver/benches/criterion.rs b/acvm-repo/bn254_blackbox_solver/benches/criterion.rs index a8fa7d8aae4..b86414423cf 100644 --- a/acvm-repo/bn254_blackbox_solver/benches/criterion.rs +++ b/acvm-repo/bn254_blackbox_solver/benches/criterion.rs @@ -15,23 +15,21 @@ fn bench_poseidon2(c: &mut Criterion) { fn bench_pedersen_commitment(c: &mut Criterion) { let inputs = [FieldElement::one(); 2]; - let solver = Bn254BlackBoxSolver::new(); c.bench_function("pedersen_commitment", |b| { - b.iter(|| solver.pedersen_commitment(black_box(&inputs), 0)) + b.iter(|| Bn254BlackBoxSolver.pedersen_commitment(black_box(&inputs), 0)) }); } fn bench_pedersen_hash(c: &mut Criterion) { let inputs = [FieldElement::one(); 2]; - let solver = Bn254BlackBoxSolver::new(); - c.bench_function("pedersen_hash", |b| b.iter(|| solver.pedersen_hash(black_box(&inputs), 0))); + c.bench_function("pedersen_hash", |b| { + b.iter(|| Bn254BlackBoxSolver.pedersen_hash(black_box(&inputs), 0)) + }); } fn bench_schnorr_verify(c: &mut Criterion) { - let solver = Bn254BlackBoxSolver::new(); - let pub_key_x = FieldElement::from_hex( "0x04b260954662e97f00cab9adb773a259097f7a274b83b113532bce27fa3fb96a", ) @@ -51,7 +49,7 @@ fn bench_schnorr_verify(c: &mut Criterion) { c.bench_function("schnorr_verify", |b| { b.iter(|| { - solver.schnorr_verify( + Bn254BlackBoxSolver.schnorr_verify( black_box(&pub_key_x), black_box(&pub_key_y), black_box(&sig_bytes), diff --git a/acvm-repo/bn254_blackbox_solver/src/lib.rs b/acvm-repo/bn254_blackbox_solver/src/lib.rs index 43b86e083d5..ae5a1c3db6d 100644 --- a/acvm-repo/bn254_blackbox_solver/src/lib.rs +++ b/acvm-repo/bn254_blackbox_solver/src/lib.rs @@ -15,26 +15,9 @@ use ark_ec::AffineRepr; pub use embedded_curve_ops::{embedded_curve_add, multi_scalar_mul}; pub use poseidon2::poseidon2_permutation; +#[derive(Default)] pub struct Bn254BlackBoxSolver; -impl Bn254BlackBoxSolver { - pub async fn initialize() -> Bn254BlackBoxSolver { - Bn254BlackBoxSolver - } - - #[cfg(not(target_arch = "wasm32"))] - pub fn new() -> Bn254BlackBoxSolver { - Bn254BlackBoxSolver - } -} - -#[cfg(not(target_arch = "wasm32"))] -impl Default for Bn254BlackBoxSolver { - fn default() -> Self { - Self::new() - } -} - impl BlackBoxFunctionSolver for Bn254BlackBoxSolver { fn schnorr_verify( &self, diff --git a/tooling/acvm_cli/src/cli/execute_cmd.rs b/tooling/acvm_cli/src/cli/execute_cmd.rs index 4e36dbd1f22..5f9651c9138 100644 --- a/tooling/acvm_cli/src/cli/execute_cmd.rs +++ b/tooling/acvm_cli/src/cli/execute_cmd.rs @@ -67,13 +67,12 @@ pub(crate) fn execute_program_from_witness( bytecode: &[u8], foreign_call_resolver_url: Option<&str>, ) -> Result { - let blackbox_solver = Bn254BlackBoxSolver::new(); let program: Program = Program::deserialize_program(bytecode) .map_err(|_| CliError::CircuitDeserializationError())?; execute_program( &program, inputs_map, - &blackbox_solver, + &Bn254BlackBoxSolver, &mut DefaultForeignCallExecutor::new(true, foreign_call_resolver_url), ) .map_err(CliError::CircuitExecutionError) diff --git a/tooling/nargo_cli/src/cli/dap_cmd.rs b/tooling/nargo_cli/src/cli/dap_cmd.rs index 124e30069ae..eded2bfd8d2 100644 --- a/tooling/nargo_cli/src/cli/dap_cmd.rs +++ b/tooling/nargo_cli/src/cli/dap_cmd.rs @@ -1,5 +1,6 @@ use acvm::acir::circuit::ExpressionWidth; use acvm::acir::native_types::WitnessMap; +use bn254_blackbox_solver::Bn254BlackBoxSolver; use clap::Args; use nargo::constants::PROVER_INPUT_FILE; use nargo::workspace::Workspace; @@ -193,11 +194,9 @@ fn loop_uninitialized_dap( Ok((compiled_program, initial_witness)) => { server.respond(req.ack()?)?; - let blackbox_solver = bn254_blackbox_solver::Bn254BlackBoxSolver::new(); - noir_debugger::run_dap_loop( server, - &blackbox_solver, + &Bn254BlackBoxSolver, compiled_program, initial_witness, )?; diff --git a/tooling/nargo_cli/src/cli/debug_cmd.rs b/tooling/nargo_cli/src/cli/debug_cmd.rs index f950cd0405c..7865b608261 100644 --- a/tooling/nargo_cli/src/cli/debug_cmd.rs +++ b/tooling/nargo_cli/src/cli/debug_cmd.rs @@ -219,8 +219,6 @@ pub(crate) fn debug_program( compiled_program: &CompiledProgram, inputs_map: &InputMap, ) -> Result, CliError> { - let blackbox_solver = Bn254BlackBoxSolver::new(); - let initial_witness = compiled_program.abi.encode(inputs_map, None)?; let debug_artifact = DebugArtifact { @@ -230,7 +228,7 @@ pub(crate) fn debug_program( }; noir_debugger::debug_circuit( - &blackbox_solver, + &Bn254BlackBoxSolver, &compiled_program.program.functions[0], debug_artifact, initial_witness, diff --git a/tooling/nargo_cli/src/cli/execute_cmd.rs b/tooling/nargo_cli/src/cli/execute_cmd.rs index 862a46884ef..3fcedbb8f54 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -110,14 +110,12 @@ pub(crate) fn execute_program( inputs_map: &InputMap, foreign_call_resolver_url: Option<&str>, ) -> Result { - let blackbox_solver = Bn254BlackBoxSolver::new(); - let initial_witness = compiled_program.abi.encode(inputs_map, None)?; let solved_witness_stack_err = nargo::ops::execute_program( &compiled_program.program, initial_witness, - &blackbox_solver, + &Bn254BlackBoxSolver, &mut DefaultForeignCallExecutor::new(true, foreign_call_resolver_url), ); match solved_witness_stack_err { diff --git a/tooling/nargo_cli/src/cli/lsp_cmd.rs b/tooling/nargo_cli/src/cli/lsp_cmd.rs index 45ac02ea552..9ff7a42e5f5 100644 --- a/tooling/nargo_cli/src/cli/lsp_cmd.rs +++ b/tooling/nargo_cli/src/cli/lsp_cmd.rs @@ -25,8 +25,7 @@ pub(crate) fn run(_args: LspCommand, _config: NargoConfig) -> Result<(), CliErro runtime.block_on(async { let (server, _) = async_lsp::MainLoop::new_server(|client| { - let blackbox_solver = Bn254BlackBoxSolver::new(); - let router = NargoLspService::new(&client, blackbox_solver); + let router = NargoLspService::new(&client, Bn254BlackBoxSolver); ServiceBuilder::new() .layer(TracingLayer::default())