Skip to content

Commit

Permalink
Merge branch 'master' into SparsePolynomial-refac
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Nov 14, 2024
2 parents be3b9ea + e6d2f33 commit 3d362da
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 68 deletions.
8 changes: 4 additions & 4 deletions curves/curve-constraint-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ pub mod curves {

let cs = ConstraintSystem::<<P::BaseField as Field>::BasePrimeField>::new_ref();

let a = SWProjective::<P>::rand(&mut rng);
let b = SWProjective::<P>::rand(&mut rng);
let a = SWProjective::rand(&mut rng);
let b = SWProjective::rand(&mut rng);
let a_affine = a.into_affine();
let b_affine = b.into_affine();

Expand Down Expand Up @@ -477,8 +477,8 @@ pub mod curves {

let cs = ConstraintSystem::<<P::BaseField as Field>::BasePrimeField>::new_ref();

let a = TEProjective::<P>::rand(&mut rng);
let b = TEProjective::<P>::rand(&mut rng);
let a = TEProjective::rand(&mut rng);
let b = TEProjective::rand(&mut rng);
let a_affine = a.into_affine();
let b_affine = b.into_affine();

Expand Down
2 changes: 1 addition & 1 deletion ec/src/hashing/curve_maps/elligator2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<P: Elligator2Config> MapToCurve<Projective<P>> for Elligator2Map<P> {
(v, w)
};

let point_on_curve = Affine::<P>::new_unchecked(v, w);
let point_on_curve = Affine::new_unchecked(v, w);
debug_assert!(
point_on_curve.is_on_curve(),
"Elligator2 mapped to a point off the curve"
Expand Down
2 changes: 1 addition & 1 deletion ec/src/hashing/curve_maps/swu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<P: SWUConfig> MapToCurve<Projective<P>> for SWUMap<P> {
} else {
y
};
let point_on_curve = Affine::<P>::new_unchecked(x_affine, y_affine);
let point_on_curve = Affine::new_unchecked(x_affine, y_affine);
debug_assert!(
point_on_curve.is_on_curve(),
"swu mapped to a point off the curve"
Expand Down
2 changes: 1 addition & 1 deletion ec/src/hashing/curve_maps/wb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ where
batch_inversion(&mut v);
let img_x = x_num.evaluate(&x) * v[0];
let img_y = (y_num.evaluate(&x) * y) * v[1];
Ok(Affine::<Codomain>::new_unchecked(img_x, img_y))
Ok(Affine::new_unchecked(img_x, img_y))
},
None => Ok(Affine::identity()),
}
Expand Down
4 changes: 2 additions & 2 deletions ec/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ark_ff::{Field, PrimeField};
use ark_ff::{Field, PrimeField, Zero};

pub mod bls12;
pub mod bn;
Expand Down Expand Up @@ -27,6 +27,6 @@ pub trait CurveConfig: Send + Sync + Sized + 'static {
const COFACTOR_INV: Self::ScalarField;

fn cofactor_is_one() -> bool {
Self::COFACTOR[0] == 1 && Self::COFACTOR.iter().skip(1).all(|&e| e == 0)
Self::COFACTOR[0] == 1 && Self::COFACTOR.iter().skip(1).all(Zero::is_zero)
}
}
2 changes: 1 addition & 1 deletion ec/src/models/short_weierstrass/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl<P: SWCurveConfig> CanonicalSerialize for Projective<P> {
writer: W,
compress: Compress,
) -> Result<(), SerializationError> {
let aff = Affine::<P>::from(*self);
let aff = Affine::from(*self);
P::serialize_with_mode(&aff, writer, compress)
}

Expand Down
4 changes: 2 additions & 2 deletions ec/src/models/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ pub trait SWCurveConfig: super::CurveConfig {
},
};
if flags.is_infinity() {
Ok(Affine::<Self>::identity())
Ok(Affine::identity())
} else {
let point = Affine::<Self>::new_unchecked(x, y);
let point = Affine::new_unchecked(x, y);
if let Validate::Yes = validate {
point.check()?;
}
Expand Down
2 changes: 1 addition & 1 deletion ec/src/models/twisted_edwards/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl<P: TECurveConfig> CanonicalSerialize for Projective<P> {
writer: W,
compress: Compress,
) -> Result<(), SerializationError> {
let aff = Affine::<P>::from(*self);
let aff = Affine::from(*self);
P::serialize_with_mode(&aff, writer, compress)
}

Expand Down
6 changes: 3 additions & 3 deletions ec/src/models/twisted_edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait TECurveConfig: super::CurveConfig {
/// Default implementation of group multiplication for projective
/// coordinates
fn mul_projective(base: &Projective<Self>, scalar: &[u64]) -> Projective<Self> {
let mut res = Projective::<Self>::zero();
let mut res = Projective::zero();
for b in ark_ff::BitIteratorBE::without_leading_zeros(scalar) {
res.double_in_place();
if b {
Expand All @@ -74,7 +74,7 @@ pub trait TECurveConfig: super::CurveConfig {
/// Default implementation of group multiplication for affine
/// coordinates
fn mul_affine(base: &Affine<Self>, scalar: &[u64]) -> Projective<Self> {
let mut res = Projective::<Self>::zero();
let mut res = Projective::zero();
for b in ark_ff::BitIteratorBE::without_leading_zeros(scalar) {
res.double_in_place();
if b {
Expand Down Expand Up @@ -141,7 +141,7 @@ pub trait TECurveConfig: super::CurveConfig {
(x, y)
},
};
let point = Affine::<Self>::new_unchecked(x, y);
let point = Affine::new_unchecked(x, y);
if let Validate::Yes = validate {
point.check()?;
}
Expand Down
4 changes: 2 additions & 2 deletions ec/src/scalar_mul/glv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub trait GLVConfig: Send + Sync + 'static + SWCurveConfig {
let iter_k1 = ark_ff::BitIteratorBE::new(k1.into_bigint());
let iter_k2 = ark_ff::BitIteratorBE::new(k2.into_bigint());

let mut res = Projective::<Self>::zero();
let mut res = Projective::zero();
let mut skip_zeros = true;
for pair in iter_k1.zip(iter_k2) {
if skip_zeros && pair == (false, false) {
Expand Down Expand Up @@ -141,7 +141,7 @@ pub trait GLVConfig: Send + Sync + 'static + SWCurveConfig {
let iter_k1 = ark_ff::BitIteratorBE::new(k1.into_bigint());
let iter_k2 = ark_ff::BitIteratorBE::new(k2.into_bigint());

let mut res = Projective::<Self>::zero();
let mut res = Projective::zero();
let mut skip_zeros = true;
for pair in iter_k1.zip(iter_k2) {
if skip_zeros && pair == (false, false) {
Expand Down
4 changes: 2 additions & 2 deletions ec/src/scalar_mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn sw_double_and_add_affine<P: SWCurveConfig>(
base: &Affine<P>,
scalar: impl AsRef<[u64]>,
) -> Projective<P> {
let mut res = Projective::<P>::zero();
let mut res = Projective::zero();
for b in ark_ff::BitIteratorBE::without_leading_zeros(scalar) {
res.double_in_place();
if b {
Expand All @@ -49,7 +49,7 @@ pub fn sw_double_and_add_projective<P: SWCurveConfig>(
base: &Projective<P>,
scalar: impl AsRef<[u64]>,
) -> Projective<P> {
let mut res = Projective::<P>::zero();
let mut res = Projective::zero();
for b in ark_ff::BitIteratorBE::without_leading_zeros(scalar) {
res.double_in_place();
if b {
Expand Down
3 changes: 2 additions & 1 deletion ff/src/biginteger/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ark_std::Zero;
use ark_std::{vec, vec::*};

macro_rules! adc {
Expand Down Expand Up @@ -152,7 +153,7 @@ pub fn mac_with_carry(a: u64, b: u64, c: u64, carry: &mut u64) -> u64 {

/// Compute the NAF (non-adjacent form) of num
pub fn find_naf(num: &[u64]) -> Vec<i8> {
let is_zero = |num: &[u64]| num.iter().all(|x| *x == 0u64);
let is_zero = |num: &[u64]| num.iter().all(Zero::is_zero);
let is_odd = |num: &[u64]| num[0] & 1 == 1;
let sub_noborrow = |num: &mut [u64], z: u64| {
let mut other = vec![0u64; num.len()];
Expand Down
3 changes: 2 additions & 1 deletion ff/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use ark_std::{
},
str::FromStr,
vec::*,
Zero,
};
use num_bigint::BigUint;
use zeroize::Zeroize;
Expand Down Expand Up @@ -475,7 +476,7 @@ impl<const N: usize> BigInteger for BigInt<N> {

#[inline]
fn is_zero(&self) -> bool {
self.0.iter().all(|&e| e == 0)
self.0.iter().all(Zero::is_zero)
}

#[inline]
Expand Down
117 changes: 115 additions & 2 deletions ff/src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct BitIteratorBE<Slice: AsRef<[u64]>> {
impl<Slice: AsRef<[u64]>> BitIteratorBE<Slice> {
pub fn new(s: Slice) -> Self {
let n = s.as_ref().len() * 64;
BitIteratorBE { s, n }
Self { s, n }
}

/// Construct an iterator that automatically skips any leading zeros.
Expand Down Expand Up @@ -46,7 +46,7 @@ impl<Slice: AsRef<[u64]>> BitIteratorLE<Slice> {
pub fn new(s: Slice) -> Self {
let n = 0;
let max_len = s.as_ref().len() * 64;
BitIteratorLE { s, n, max_len }
Self { s, n, max_len }
}

/// Construct an iterator that automatically skips any trailing zeros.
Expand Down Expand Up @@ -80,3 +80,116 @@ impl<Slice: AsRef<[u64]>> Iterator for BitIteratorLE<Slice> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use ark_std::vec::Vec;

#[test]
fn test_bit_iterator_be() {
// Test with a simple case of a single 64-bit integer: 0b1010
let data = [0b1010u64];
let mut iter = BitIteratorBE::new(&data);

// The iterator should return the bits in big-endian order
// The first 60 bits are zeros
for _ in 0..60 {
assert_eq!(iter.next(), Some(false));
}
assert_eq!(iter.next(), Some(true)); // 3rd bit
assert_eq!(iter.next(), Some(false)); // 2nd bit
assert_eq!(iter.next(), Some(true)); // 1st bit
assert_eq!(iter.next(), Some(false)); // 0th bit
assert_eq!(iter.next(), None); // End of iteration

// Test with the without_leading_zeros method
let data = [0b0000_0000_0000_0000_0000_0000_0000_1010u64];
let iter: Vec<bool> = BitIteratorBE::without_leading_zeros(&data).collect();
assert_eq!(iter, vec![true, false, true, false]); // Only the significant bits

// Test with all zeros
let data = [0u64];
let iter: Vec<bool> = BitIteratorBE::without_leading_zeros(&data).collect();
assert!(iter.is_empty()); // Should be empty because all bits are zeros
}

#[test]
fn test_bit_iterator_le() {
// Test with a simple case of a single 64-bit integer: 0b1010
let data = [0b1010u64];
let mut iter = BitIteratorLE::new(&data);

// The iterator should return the bits in little-endian order
assert_eq!(iter.next(), Some(false)); // 0th bit
assert_eq!(iter.next(), Some(true)); // 1st bit
assert_eq!(iter.next(), Some(false)); // 2nd bit
assert_eq!(iter.next(), Some(true)); // 3rd bit
for _ in 4..64 {
assert_eq!(iter.next(), Some(false)); // The remaining bits are zeros
}
assert_eq!(iter.next(), None); // End of iteration

// Test with the without_trailing_zeros method
let data = [0b0000_0000_0000_0000_0000_0000_0000_1010u64];
let iter: Vec<bool> = BitIteratorLE::without_trailing_zeros(&data).collect();
assert_eq!(iter, vec![false, true, false, true]); // Only the significant bits

// Test with all zeros
let data = [0u64];
let iter: Vec<bool> = BitIteratorLE::without_trailing_zeros(&data).collect();
assert!(iter.is_empty()); // Should be empty because all bits are zeros
}

#[test]
fn test_bit_iterator_be_multiple_integers() {
// Test with multiple 64-bit integers: [0b1010, 0b1111]
let data = [0b1010u64, 0b1111u64];
let mut iter = BitIteratorBE::new(&data);

// First integer (0b1111) in big-endian order
for _ in 0..60 {
assert_eq!(iter.next(), Some(false));
}
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(true));

// Second integer (0b1010) in big-endian order
for _ in 0..60 {
assert_eq!(iter.next(), Some(false));
}
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(false));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(false));
assert_eq!(iter.next(), None); // End of iteration
}

#[test]
fn test_bit_iterator_le_multiple_integers() {
// Test with multiple 64-bit integers: [0b1010, 0b1111]
let data = [0b1010u64, 0b1111u64];
let mut iter = BitIteratorLE::new(&data);

// First integer (0b1010) in little-endian order
assert_eq!(iter.next(), Some(false));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(false));
assert_eq!(iter.next(), Some(true));
for _ in 4..64 {
assert_eq!(iter.next(), Some(false));
}

// Second integer (0b1111) in little-endian order
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(true));
for _ in 4..64 {
assert_eq!(iter.next(), Some(false));
}
assert_eq!(iter.next(), None); // End of iteration
}
}
2 changes: 1 addition & 1 deletion ff/src/fields/models/fp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl<P: FpConfig<N>, const N: usize> From<u128> for Fp<P, N> {
let mut result = BigInt::default();
if N == 1 {
result.0[0] = (other % u128::from(P::MODULUS.0[0])) as u64;
} else if N == 2 || P::MODULUS.0[2..].iter().all(|&x| x == 0) {
} else if N == 2 || P::MODULUS.0[2..].iter().all(Zero::is_zero) {
let mod_as_u128 = P::MODULUS.0[0] as u128 + ((P::MODULUS.0[1] as u128) << 64);
other %= mod_as_u128;
result.0[0] = ((other << 64) >> 64) as u64;
Expand Down
Loading

0 comments on commit 3d362da

Please sign in to comment.