diff --git a/Cargo.lock b/Cargo.lock index d932b6d5f53..7fadf637e31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,12 +56,8 @@ name = "acvm_blackbox_solver" version = "0.28.0" dependencies = [ "acir", - "ark-ec", - "ark-ff", "blake2", - "grumpkin", "k256", - "num-bigint", "p256", "sha2", "sha3", @@ -457,9 +453,13 @@ version = "0.28.0" dependencies = [ "acir", "acvm_blackbox_solver", + "ark-ec", + "ark-ff", "flate2", "getrandom", + "grumpkin", "js-sys", + "num-bigint", "pkg-config", "reqwest", "rust-embed", diff --git a/acvm-repo/acvm/src/pwg/blackbox/fixed_base_scalar_mul.rs b/acvm-repo/acvm/src/pwg/blackbox/fixed_base_scalar_mul.rs index b489d997339..582ed56584b 100644 --- a/acvm-repo/acvm/src/pwg/blackbox/fixed_base_scalar_mul.rs +++ b/acvm-repo/acvm/src/pwg/blackbox/fixed_base_scalar_mul.rs @@ -2,10 +2,12 @@ use acir::{ circuit::opcodes::FunctionInput, native_types::{Witness, WitnessMap}, }; +use acvm_blackbox_solver::BlackBoxFunctionSolver; use crate::pwg::{insert_value, witness_to_value, OpcodeResolutionError}; pub(super) fn fixed_base_scalar_mul( + backend: &impl BlackBoxFunctionSolver, initial_witness: &mut WitnessMap, low: FunctionInput, high: FunctionInput, @@ -14,7 +16,7 @@ pub(super) fn fixed_base_scalar_mul( let low = witness_to_value(initial_witness, low.witness)?; let high = witness_to_value(initial_witness, high.witness)?; - let (pub_x, pub_y) = crate::blackbox_solver::fixed_base_scalar_mul(low, high)?; + let (pub_x, pub_y) = backend.fixed_base_scalar_mul(low, high)?; insert_value(&outputs.0, pub_x, initial_witness)?; insert_value(&outputs.1, pub_y, initial_witness)?; diff --git a/acvm-repo/acvm/src/pwg/blackbox/mod.rs b/acvm-repo/acvm/src/pwg/blackbox/mod.rs index 14ded91707d..c4d9d561f46 100644 --- a/acvm-repo/acvm/src/pwg/blackbox/mod.rs +++ b/acvm-repo/acvm/src/pwg/blackbox/mod.rs @@ -149,7 +149,7 @@ pub(crate) fn solve( *output, ), BlackBoxFuncCall::FixedBaseScalarMul { low, high, outputs } => { - fixed_base_scalar_mul(initial_witness, *low, *high, *outputs) + fixed_base_scalar_mul(backend, initial_witness, *low, *high, *outputs) } BlackBoxFuncCall::RecursiveAggregation { output_aggregation_object, .. } => { // Solve the output of the recursive aggregation to zero to prevent missing assignment errors diff --git a/acvm-repo/acvm/tests/solver.rs b/acvm-repo/acvm/tests/solver.rs index a16bb78c81f..1d287d70c1b 100644 --- a/acvm-repo/acvm/tests/solver.rs +++ b/acvm-repo/acvm/tests/solver.rs @@ -36,6 +36,13 @@ impl BlackBoxFunctionSolver for StubbedBackend { ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { panic!("Path not trodden by this test") } + fn fixed_base_scalar_mul( + &self, + _low: &FieldElement, + _high: &FieldElement, + ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { + panic!("Path not trodden by this test") + } } // Reenable these test cases once we move the brillig implementation of inversion down into the acvm stdlib. diff --git a/acvm-repo/barretenberg_blackbox_solver/Cargo.toml b/acvm-repo/barretenberg_blackbox_solver/Cargo.toml index 96bf3e7dcff..97e58c2804b 100644 --- a/acvm-repo/barretenberg_blackbox_solver/Cargo.toml +++ b/acvm-repo/barretenberg_blackbox_solver/Cargo.toml @@ -23,6 +23,12 @@ rust-embed = { version = "6.6.0", features = [ "include-exclude", ] } +# BN254 fixed base scalar multiplication solver +grumpkin = { git = "https://github.com/noir-lang/grumpkin", rev = "56d99799381f79e42148aaef0de2b0cf9a4b9a5d", features = ["std"] } +ark-ec = { version = "^0.4.0", default-features = false } +ark-ff = { version = "^0.4.0", default-features = false } +num-bigint.workspace = true + [target.'cfg(target_arch = "wasm32")'.dependencies] wasmer = { version = "3.3", default-features = false, features = [ "js-default", diff --git a/acvm-repo/blackbox_solver/src/fixed_base_scalar_mul.rs b/acvm-repo/barretenberg_blackbox_solver/src/fixed_base_scalar_mul.rs similarity index 91% rename from acvm-repo/blackbox_solver/src/fixed_base_scalar_mul.rs rename to acvm-repo/barretenberg_blackbox_solver/src/fixed_base_scalar_mul.rs index 5904439d960..f8e53e2b404 100644 --- a/acvm-repo/blackbox_solver/src/fixed_base_scalar_mul.rs +++ b/acvm-repo/barretenberg_blackbox_solver/src/fixed_base_scalar_mul.rs @@ -2,18 +2,6 @@ use acir::{BlackBoxFunc, FieldElement}; use crate::BlackBoxResolutionError; -#[cfg(not(feature = "bn254"))] -pub fn fixed_base_scalar_mul( - _low: &FieldElement, - _high: &FieldElement, -) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { - Err(BlackBoxResolutionError::Failed( - BlackBoxFunc::FixedBaseScalarMul, - "This solver is only defined over the bn254 curve currently".into(), - )) -} - -#[cfg(feature = "bn254")] pub fn fixed_base_scalar_mul( low: &FieldElement, high: &FieldElement, diff --git a/acvm-repo/barretenberg_blackbox_solver/src/lib.rs b/acvm-repo/barretenberg_blackbox_solver/src/lib.rs index d5cfc9591f7..b9486e97bd9 100644 --- a/acvm-repo/barretenberg_blackbox_solver/src/lib.rs +++ b/acvm-repo/barretenberg_blackbox_solver/src/lib.rs @@ -5,8 +5,10 @@ use acir::{BlackBoxFunc, FieldElement}; use acvm_blackbox_solver::{BlackBoxFunctionSolver, BlackBoxResolutionError}; +mod fixed_base_scalar_mul; mod wasm; +pub use fixed_base_scalar_mul::fixed_base_scalar_mul; use wasm::Barretenberg; use self::wasm::{Pedersen, SchnorrSig}; @@ -71,4 +73,12 @@ impl BlackBoxFunctionSolver for BarretenbergSolver { .encrypt(inputs.to_vec(), domain_separator) .map_err(|err| BlackBoxResolutionError::Failed(BlackBoxFunc::Pedersen, err.to_string())) } + + fn fixed_base_scalar_mul( + &self, + low: &FieldElement, + high: &FieldElement, + ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { + fixed_base_scalar_mul(low, high) + } } diff --git a/acvm-repo/blackbox_solver/Cargo.toml b/acvm-repo/blackbox_solver/Cargo.toml index ba08393a2b9..f0295456977 100644 --- a/acvm-repo/blackbox_solver/Cargo.toml +++ b/acvm-repo/blackbox_solver/Cargo.toml @@ -34,13 +34,8 @@ p256 = { version = "0.11.0", features = [ "arithmetic", ] } -# BN254 fixed base scalar multiplication solver -grumpkin = { git = "https://github.com/noir-lang/grumpkin", rev = "56d99799381f79e42148aaef0de2b0cf9a4b9a5d", optional = true, features = ["std"] } -ark-ec = { version = "^0.4.0", optional = true, default-features = false } -ark-ff = { version = "^0.4.0", optional = true, default-features = false } -num-bigint = { workspace = true, optional = true } [features] default = ["bn254"] -bn254 = ["acir/bn254", "dep:grumpkin", "dep:ark-ec", "dep:ark-ff", "dep:num-bigint"] +bn254 = ["acir/bn254"] bls12_381 = ["acir/bls12_381"] diff --git a/acvm-repo/blackbox_solver/src/lib.rs b/acvm-repo/blackbox_solver/src/lib.rs index 23ef0aad68d..0c0fbae2bde 100644 --- a/acvm-repo/blackbox_solver/src/lib.rs +++ b/acvm-repo/blackbox_solver/src/lib.rs @@ -14,10 +14,6 @@ use sha2::Sha256; use sha3::Keccak256; use thiserror::Error; -mod fixed_base_scalar_mul; - -pub use fixed_base_scalar_mul::fixed_base_scalar_mul; - #[derive(Clone, PartialEq, Eq, Debug, Error)] pub enum BlackBoxResolutionError { #[error("unsupported blackbox function: {0}")] @@ -43,6 +39,11 @@ pub trait BlackBoxFunctionSolver { inputs: &[FieldElement], domain_separator: u32, ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError>; + fn fixed_base_scalar_mul( + &self, + low: &FieldElement, + high: &FieldElement, + ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError>; } pub fn sha256(inputs: &[u8]) -> Result<[u8; 32], BlackBoxResolutionError> { diff --git a/acvm-repo/brillig_vm/src/black_box.rs b/acvm-repo/brillig_vm/src/black_box.rs index dc789d88b6a..ada8a2f5993 100644 --- a/acvm-repo/brillig_vm/src/black_box.rs +++ b/acvm-repo/brillig_vm/src/black_box.rs @@ -1,8 +1,8 @@ use acir::brillig::{BlackBoxOp, HeapArray, HeapVector, Value}; use acir::{BlackBoxFunc, FieldElement}; use acvm_blackbox_solver::{ - blake2s, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify, fixed_base_scalar_mul, - hash_to_field_128_security, keccak256, sha256, BlackBoxFunctionSolver, BlackBoxResolutionError, + blake2s, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify, hash_to_field_128_security, keccak256, + sha256, BlackBoxFunctionSolver, BlackBoxResolutionError, }; use crate::{Memory, Registers}; @@ -143,7 +143,7 @@ pub(crate) fn evaluate_black_box( BlackBoxOp::FixedBaseScalarMul { low, high, result } => { let low = registers.get(*low).to_field(); let high = registers.get(*high).to_field(); - let (x, y) = fixed_base_scalar_mul(&low, &high)?; + let (x, y) = solver.fixed_base_scalar_mul(&low, &high)?; memory.write_slice(registers.get(result.pointer).to_usize(), &[x.into(), y.into()]); Ok(()) } diff --git a/acvm-repo/brillig_vm/src/lib.rs b/acvm-repo/brillig_vm/src/lib.rs index e5239dad021..48f6bf5f1c4 100644 --- a/acvm-repo/brillig_vm/src/lib.rs +++ b/acvm-repo/brillig_vm/src/lib.rs @@ -424,6 +424,13 @@ impl BlackBoxFunctionSolver for DummyBlackBoxSolver { ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { Ok((2_u128.into(), 3_u128.into())) } + fn fixed_base_scalar_mul( + &self, + _low: &FieldElement, + _high: &FieldElement, + ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { + Ok((4_u128.into(), 5_u128.into())) + } } #[cfg(test)] diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir.rs index 46161a86199..a4ea0362f06 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir.rs @@ -1039,6 +1039,13 @@ pub(crate) mod tests { ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { Ok((2_u128.into(), 3_u128.into())) } + fn fixed_base_scalar_mul( + &self, + _low: &FieldElement, + _high: &FieldElement, + ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { + Ok((4_u128.into(), 5_u128.into())) + } } pub(crate) fn create_context() -> BrilligContext { diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index cf8b35e5822..d846ede566f 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1271,6 +1271,13 @@ fn execute_brillig( ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { Err(BlackBoxResolutionError::Unsupported(BlackBoxFunc::Pedersen)) } + fn fixed_base_scalar_mul( + &self, + _low: &FieldElement, + _high: &FieldElement, + ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { + Err(BlackBoxResolutionError::Unsupported(BlackBoxFunc::FixedBaseScalarMul)) + } } // Set input values diff --git a/tooling/lsp/src/lib.rs b/tooling/lsp/src/lib.rs index 910efa928b9..48ffefb7f7a 100644 --- a/tooling/lsp/src/lib.rs +++ b/tooling/lsp/src/lib.rs @@ -65,6 +65,14 @@ impl BlackBoxFunctionSolver for WrapperSolver { ) -> Result<(acvm::FieldElement, acvm::FieldElement), acvm::BlackBoxResolutionError> { self.0.pedersen(inputs, domain_separator) } + + fn fixed_base_scalar_mul( + &self, + low: &acvm::FieldElement, + high: &acvm::FieldElement, + ) -> Result<(acvm::FieldElement, acvm::FieldElement), acvm::BlackBoxResolutionError> { + self.0.fixed_base_scalar_mul(low, high) + } } // State for the LSP gets implemented on this struct and is internal to the implementation @@ -425,6 +433,15 @@ mod lsp_tests { { unimplemented!() } + + fn fixed_base_scalar_mul( + &self, + _low: &acvm::FieldElement, + _high: &acvm::FieldElement, + ) -> Result<(acvm::FieldElement, acvm::FieldElement), acvm::BlackBoxResolutionError> + { + unimplemented!() + } } let client = ClientSocket::new_closed();