Skip to content

Commit

Permalink
feat: remove conditional compilation of bn254_blackbox_solver (#5058)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## 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.
  • Loading branch information
TomAFrench authored May 22, 2024
1 parent 533d10e commit 9420d7c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 77 deletions.
57 changes: 18 additions & 39 deletions acvm-repo/acvm_js/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -47,15 +47,9 @@ pub async fn execute_circuit(
) -> Result<JsWitnessMap, Error> {
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())
Expand All @@ -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<u8>,
initial_witness: JsWitnessMap,
foreign_call_handler: ForeignCallHandler,
Expand All @@ -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,
Expand All @@ -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<u8>,
initial_witness: JsWitnessMap,
foreign_call_handler: ForeignCallHandler,
) -> Result<JsWitnessMap, Error> {
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())
Expand All @@ -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<u8>,
initial_witness: JsWitnessMap,
foreign_call_executor: &ForeignCallHandler,
) -> Result<JsWitnessStack, Error> {
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<u8>,
initial_witness: JsWitnessMap,
foreign_call_executor: &ForeignCallHandler,
Expand All @@ -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<WitnessStack, Error> {
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?;
Expand Down
12 changes: 5 additions & 7 deletions acvm-repo/bn254_blackbox_solver/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand All @@ -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),
Expand Down
19 changes: 1 addition & 18 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions tooling/acvm_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ pub(crate) fn execute_program_from_witness(
bytecode: &[u8],
foreign_call_resolver_url: Option<&str>,
) -> Result<WitnessStack, CliError> {
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)
Expand Down
5 changes: 2 additions & 3 deletions tooling/nargo_cli/src/cli/dap_cmd.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -193,11 +194,9 @@ fn loop_uninitialized_dap<R: Read, W: Write>(
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,
)?;
Expand Down
4 changes: 1 addition & 3 deletions tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ pub(crate) fn debug_program(
compiled_program: &CompiledProgram,
inputs_map: &InputMap,
) -> Result<Option<WitnessMap>, CliError> {
let blackbox_solver = Bn254BlackBoxSolver::new();

let initial_witness = compiled_program.abi.encode(inputs_map, None)?;

let debug_artifact = DebugArtifact {
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,12 @@ pub(crate) fn execute_program(
inputs_map: &InputMap,
foreign_call_resolver_url: Option<&str>,
) -> Result<WitnessStack, CliError> {
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 {
Expand Down
3 changes: 1 addition & 2 deletions tooling/nargo_cli/src/cli/lsp_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 9420d7c

Please sign in to comment.