Skip to content

Commit

Permalink
chore: move fixed_base_scalar_mul back onto BlackBoxFunctionSolver
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Oct 16, 2023
1 parent 5408704 commit 83cfaac
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion acvm-repo/acvm/src/pwg/blackbox/fixed_base_scalar_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions acvm-repo/acvm/tests/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions acvm-repo/barretenberg_blackbox_solver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions acvm-repo/barretenberg_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions acvm-repo/blackbox_solver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 5 additions & 4 deletions acvm-repo/blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand All @@ -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> {
Expand Down
6 changes: 3 additions & 3 deletions acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -143,7 +143,7 @@ pub(crate) fn evaluate_black_box<Solver: BlackBoxFunctionSolver>(
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(())
}
Expand Down
7 changes: 7 additions & 0 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
7 changes: 7 additions & 0 deletions compiler/noirc_evaluator/src/brillig/brillig_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tooling/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 83cfaac

Please sign in to comment.