Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions acvm-repo/bn254_blackbox_solver/src/embedded_curve_ops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO(https://github.com/noir-lang/noir/issues/4932): rename this file to something more generic
use ark_ec::AffineRepr;
use ark_ff::MontConfig;
use num_bigint::BigUint;
Expand All @@ -9,6 +8,19 @@ use acir::BlackBoxFunc;

use crate::BlackBoxResolutionError;

/// Converts a field element to u128, returning an error if it doesn't fit.
fn field_to_u128_limb(
limb: &FieldElement,
func: BlackBoxFunc,
) -> Result<u128, BlackBoxResolutionError> {
limb.try_into_u128().ok_or_else(|| {
BlackBoxResolutionError::Failed(
func,
format!("Limb {} is not less than 2^128", limb.to_hex()),
)
})
}

/// Performs multi scalar multiplication of points with scalars.
pub fn multi_scalar_mul(
points: &[FieldElement],
Expand All @@ -33,19 +45,11 @@ pub fn multi_scalar_mul(
create_point(points[i], points[i + 1], points[i + 2] == FieldElement::from(1_u128))
.map_err(|e| BlackBoxResolutionError::Failed(BlackBoxFunc::MultiScalarMul, e))?;

let scalar_low: u128 = scalars_lo[i / 3].try_into_u128().ok_or_else(|| {
BlackBoxResolutionError::Failed(
BlackBoxFunc::MultiScalarMul,
format!("Limb {} is not less than 2^128", scalars_lo[i].to_hex()),
)
})?;
let scalar_low: u128 =
field_to_u128_limb(&scalars_lo[i / 3], BlackBoxFunc::MultiScalarMul)?;

let scalar_high: u128 = scalars_hi[i / 3].try_into_u128().ok_or_else(|| {
BlackBoxResolutionError::Failed(
BlackBoxFunc::MultiScalarMul,
format!("Limb {} is not less than 2^128", scalars_hi[i].to_hex()),
)
})?;
let scalar_high: u128 =
field_to_u128_limb(&scalars_hi[i / 3], BlackBoxFunc::MultiScalarMul)?;

let mut bytes = scalar_high.to_be_bytes().to_vec();
bytes.extend_from_slice(&scalar_low.to_be_bytes());
Expand Down
7 changes: 3 additions & 4 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use acvm_blackbox_solver::{BlackBoxFunctionSolver, BlackBoxResolutionError};
mod embedded_curve_ops;
mod generator;
mod poseidon2;
mod poseidon2_constants;

pub use embedded_curve_ops::{embedded_curve_add, multi_scalar_mul};
pub use generator::generators::derive_generators;
pub use poseidon2::{
POSEIDON2_CONFIG, Poseidon2Config, Poseidon2Sponge, field_from_hex, poseidon_hash,
poseidon2_permutation,
};
pub use poseidon2::{Poseidon2Sponge, poseidon_hash, poseidon2_permutation};
pub use poseidon2_constants::{POSEIDON2_CONFIG, Poseidon2Config, field_from_hex};

// Temporary hack, this ensure that we always use a bn254 field here
// without polluting the feature flags of the `acir_field` crate.
Expand Down
Loading
Loading