Skip to content

Commit

Permalink
Merge branch 'master' into glv-for-simple-curves
Browse files Browse the repository at this point in the history
  • Loading branch information
weikengchen authored Feb 17, 2024
2 parents be71d04 + 3a61567 commit d57f22f
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 96 deletions.
42 changes: 22 additions & 20 deletions ec/src/models/short_weierstrass/group.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use super::{Affine, SWCurveConfig};
use crate::{
scalar_mul::{variable_base::VariableBaseMSM, ScalarMul},
AffineRepr, CurveGroup, PrimeGroup,
};
use ark_ff::{fields::Field, AdditiveGroup, PrimeField, ToConstraintField, UniformRand};
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
};
Expand All @@ -14,20 +20,10 @@ use ark_std::{
vec::Vec,
One, Zero,
};

use ark_ff::{fields::Field, AdditiveGroup, PrimeField, ToConstraintField, UniformRand};

use derivative::Derivative;
use zeroize::Zeroize;

#[cfg(feature = "parallel")]
use rayon::prelude::*;

use super::{Affine, SWCurveConfig};
use crate::{
scalar_mul::{variable_base::VariableBaseMSM, ScalarMul},
AffineRepr, CurveGroup, PrimeGroup,
};
use zeroize::Zeroize;

/// Jacobian coordinates for a point on an elliptic curve in short Weierstrass
/// form, over the base field `P::BaseField`. This struct implements arithmetic
Expand Down Expand Up @@ -362,12 +358,15 @@ impl<P: SWCurveConfig, T: Borrow<Affine<P>>> AddAssign<T> for Projective<P> {
s2 *= &other_y;
s2 *= &z1z1;

if self.x == u2 && self.y == s2 {
// The two points are equal, so we double.
self.double_in_place();
if self.x == u2 {
if self.y == s2 {
// The two points are equal, so we double.
self.double_in_place();
} else {
// a + (-a) = 0
*self = Self::zero()
}
} else {
// If we're adding -a and a together, self.z becomes zero as H becomes zero.

// H = U2-X1
let mut h = u2;
h -= &self.x;
Expand Down Expand Up @@ -487,11 +486,14 @@ impl<'a, P: SWCurveConfig> AddAssign<&'a Self> for Projective<P> {
s2 *= &z1z1;

if u1 == u2 && s1 == s2 {
// The two points are equal, so we double.
self.double_in_place();
if s1 == s2 {
// The two points are equal, so we double.
self.double_in_place();
} else {
// a + (-a) = 0
*self = Self::zero();
}
} else {
// If we're adding -a and a together, self.z becomes zero as H becomes zero.

// H = U2-U1
let mut h = u2;
h -= &u1;
Expand Down
4 changes: 2 additions & 2 deletions ff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ to implement the [`PrimeField`][prime_field] trait for it. This provides access
additional APIs:

```rust
use ark_ff::{Field, PrimeField, FpConfig, BigInteger};
use ark_ff::{Field, PrimeField, FpConfig, BigInteger, Zero};
// Now we'll use the prime field underlying the BLS12-381 G1 curve.
use ark_test_curves::bls12_381::Fq as F;
use ark_std::{One, Zero, UniformRand};
use ark_std::{One, UniformRand};

let mut rng = ark_std::test_rng();
let a = F::rand(&mut rng);
Expand Down
2 changes: 1 addition & 1 deletion ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ark_std::{
};

pub use ark_ff_macros;
use num_traits::{One, Zero};
pub use num_traits::{One, Zero};
use zeroize::Zeroize;

pub mod utils;
Expand Down
20 changes: 8 additions & 12 deletions ff/src/fields/models/cubic_extension.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::{
fields::{Field, PrimeField},
AdditiveGroup, LegendreSymbol, One, SqrtPrecomputation, ToConstraintField, UniformRand, Zero,
};
use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags, Compress, EmptyFlags, Flags, SerializationError, Valid, Validate,
Expand All @@ -8,22 +12,14 @@ use ark_std::{
io::{Read, Write},
iter::{Chain, IntoIterator},
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
rand::{
distributions::{Distribution, Standard},
Rng,
},
vec::Vec,
};

use num_traits::{One, Zero};
use zeroize::Zeroize;

use ark_std::rand::{
distributions::{Distribution, Standard},
Rng,
};

use crate::{
fields::{Field, PrimeField},
AdditiveGroup, LegendreSymbol, SqrtPrecomputation, ToConstraintField, UniformRand,
};

/// Defines a Cubic extension field from a cubic non-residue.
pub trait CubicExtConfig: 'static + Send + Sync + Sized {
/// The prime field that this cubic extension is eventually an extension of.
Expand Down
12 changes: 5 additions & 7 deletions ff/src/fields/models/fp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::iter;

use crate::{
AdditiveGroup, BigInt, BigInteger, FftField, Field, LegendreSymbol, One, PrimeField,
SqrtPrecomputation, Zero,
};
use ark_serialize::{
buffer_byte_size, CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags, Compress, EmptyFlags, Flags, SerializationError, Valid, Validate,
Expand All @@ -11,17 +13,13 @@ use ark_std::{
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
str::FromStr,
string::ToString,
One, Zero,
};
use core::iter;

#[macro_use]
mod montgomery_backend;
pub use montgomery_backend::*;

use crate::{
AdditiveGroup, BigInt, BigInteger, FftField, Field, LegendreSymbol, PrimeField,
SqrtPrecomputation,
};
/// A trait that specifies the configuration of a prime field.
/// Also specifies how to perform arithmetic on field elements.
pub trait FpConfig<const N: usize>: Send + Sync + 'static + Sized {
Expand Down
13 changes: 7 additions & 6 deletions ff/src/fields/models/fp/montgomery_backend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ark_std::{marker::PhantomData, Zero};

use super::{Fp, FpConfig};
use crate::{biginteger::arithmetic as fa, BigInt, BigInteger, PrimeField, SqrtPrecomputation};
use crate::{
biginteger::arithmetic as fa, BigInt, BigInteger, PrimeField, SqrtPrecomputation, Zero,
};
use ark_ff_macros::unroll_for_loops;
use ark_std::marker::PhantomData;

/// A trait that specifies the constants and arithmetic procedures
/// for Montgomery arithmetic over the prime field defined by `MODULUS`.
Expand Down Expand Up @@ -161,9 +162,9 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
{
#[cfg(
all(
feature = "asm",
target_feature = "bmi2",
target_feature = "adx",
feature = "asm",
target_feature = "bmi2",
target_feature = "adx",
target_arch = "x86_64"
)
)]
Expand Down
4 changes: 1 addition & 3 deletions ff/src/fields/models/fp12_2over3over2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use ark_std::Zero;

use super::quadratic_extension::{QuadExtConfig, QuadExtField};
use crate::{
fields::{
fp6_3over2::{Fp6, Fp6Config},
Field, Fp2, Fp2Config as Fp2ConfigTrait,
},
AdditiveGroup, CyclotomicMultSubgroup,
AdditiveGroup, CyclotomicMultSubgroup, Zero,
};
use core::{
marker::PhantomData,
Expand Down
4 changes: 1 addition & 3 deletions ff/src/fields/models/fp2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use ark_std::Zero;

use super::quadratic_extension::{QuadExtConfig, QuadExtField};
use crate::{fields::PrimeField, CyclotomicMultSubgroup};
use crate::{fields::PrimeField, CyclotomicMultSubgroup, Zero};
use core::{marker::PhantomData, ops::Not};

/// Trait that specifies constants and methods for defining degree-two extension fields.
Expand Down
7 changes: 2 additions & 5 deletions ff/src/fields/models/fp4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use ark_std::Zero;

use super::quadratic_extension::{QuadExtConfig, QuadExtField};
use core::{marker::PhantomData, ops::Not};

use crate::{
fields::{Fp2, Fp2Config},
CyclotomicMultSubgroup,
CyclotomicMultSubgroup, Zero,
};
use core::{marker::PhantomData, ops::Not};

pub trait Fp4Config: 'static + Send + Sync {
type Fp2Config: Fp2Config;
Expand Down
11 changes: 4 additions & 7 deletions ff/src/fields/models/fp6_2over3.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use ark_std::Zero;

use super::quadratic_extension::{QuadExtConfig, QuadExtField};
use crate::{
fields::{Fp3, Fp3Config},
CyclotomicMultSubgroup, Zero,
};
use core::{
marker::PhantomData,
ops::{MulAssign, Not},
};

use crate::{
fields::{Fp3, Fp3Config},
CyclotomicMultSubgroup,
};

pub trait Fp6Config: 'static + Send + Sync {
type Fp3Config: Fp3Config;

Expand Down
22 changes: 9 additions & 13 deletions ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use crate::{
biginteger::BigInteger,
fields::{Field, LegendreSymbol, PrimeField},
AdditiveGroup, One, SqrtPrecomputation, ToConstraintField, UniformRand, Zero,
};
use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags, Compress, EmptyFlags, Flags, SerializationError, Valid, Validate,
Expand All @@ -8,23 +13,14 @@ use ark_std::{
io::{Read, Write},
iter::{Chain, IntoIterator},
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
rand::{
distributions::{Distribution, Standard},
Rng,
},
vec::Vec,
};

use num_traits::{One, Zero};
use zeroize::Zeroize;

use ark_std::rand::{
distributions::{Distribution, Standard},
Rng,
};

use crate::{
biginteger::BigInteger,
fields::{Field, LegendreSymbol, PrimeField},
AdditiveGroup, SqrtPrecomputation, ToConstraintField, UniformRand,
};

/// Defines a Quadratic extension field from a quadratic non-residue.
pub trait QuadExtConfig: 'static + Send + Sync + Sized {
/// The prime field that this quadratic extension is eventually an extension of.
Expand Down
6 changes: 3 additions & 3 deletions ff/src/fields/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::{BigInteger, FftField, Field};
use ark_std::{cmp::min, str::FromStr};
use num_bigint::BigUint;

/// The interface for a prime field, i.e. the field of integers modulo a prime $p$.
/// The interface for a prime field, i.e. the field of integers modulo a prime $p$.
/// In the following example we'll use the prime field underlying the BLS12-381 G1 curve.
/// ```rust
/// use ark_ff::{BigInteger, Field, PrimeField};
/// use ark_std::{test_rng, One, UniformRand, Zero};
/// use ark_ff::{BigInteger, Field, PrimeField, Zero};
/// use ark_std::{test_rng, One, UniformRand};
/// use ark_test_curves::bls12_381::Fq as F;
///
/// let mut rng = test_rng();
Expand Down
13 changes: 5 additions & 8 deletions ff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,16 @@ pub use ark_std::UniformRand;
mod to_field_vec;
pub use to_field_vec::ToConstraintField;

pub use num_traits::{One, Zero};

#[doc(hidden)]
pub use ark_ff_asm::*;
#[doc(hidden)]
pub use ark_std::vec;

pub mod prelude {
pub use crate::biginteger::BigInteger;

pub use crate::fields::{Field, PrimeField};

pub use crate::{
biginteger::BigInteger,
fields::{Field, PrimeField},
One, Zero,
};
pub use ark_std::UniformRand;

pub use num_traits::{One, Zero};
}
6 changes: 0 additions & 6 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
edition = "2021"

condense_wildcard_suffixes = true
match_block_trailing_comma = true

reorder_imports = true
imports_granularity = "Crate"

use_field_init_shorthand = true
use_try_shorthand = true

normalize_comments = true

format_macro_bodies = true
format_code_in_doc_comments = true

0 comments on commit d57f22f

Please sign in to comment.