Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify SquareRootField and Field #387

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
12 changes: 6 additions & 6 deletions ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern crate ark_std;

use ark_ff::{
bytes::{FromBytes, ToBytes},
fields::{BitIteratorBE, Field, PrimeField, SquareRootField},
fields::{BitIteratorBE, Field, PrimeField},
UniformRand,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
Expand All @@ -47,7 +47,7 @@ pub mod wnaf;

pub trait PairingEngine: Sized + 'static + Copy + Debug + Sync + Send + Eq + PartialEq {
/// This is the scalar field of the G1/G2 groups.
type Fr: PrimeField + SquareRootField;
type Fr: PrimeField;

/// The projective representation of an element in G1.
type G1Projective: ProjectiveCurve<BaseField = Self::Fq, ScalarField = Self::Fr, Affine = Self::G1Affine>
Expand Down Expand Up @@ -80,10 +80,10 @@ pub trait PairingEngine: Sized + 'static + Copy + Debug + Sync + Send + Eq + Par
type G2Prepared: ToBytes + Default + Clone + Send + Sync + Debug + From<Self::G2Affine>;

/// The base field that hosts G1.
type Fq: PrimeField + SquareRootField;
type Fq: PrimeField;

/// The extension field that hosts G2.
type Fqe: SquareRootField;
type Fqe: Field;

/// The extension field that hosts the target group of the pairing.
type Fqk: Field;
Expand Down Expand Up @@ -156,7 +156,7 @@ pub trait ProjectiveCurve:
+ From<<Self as ProjectiveCurve>::Affine>
{
type Parameters: ModelParameters<ScalarField = Self::ScalarField, BaseField = Self::BaseField>;
type ScalarField: PrimeField + SquareRootField;
type ScalarField: PrimeField;
type BaseField: Field;
type Affine: AffineCurve<
Parameters = Self::Parameters,
Expand Down Expand Up @@ -256,7 +256,7 @@ pub trait AffineCurve:
+ From<<Self as AffineCurve>::Projective>
{
type Parameters: ModelParameters<ScalarField = Self::ScalarField, BaseField = Self::BaseField>;
type ScalarField: PrimeField + SquareRootField + Into<<Self::ScalarField as PrimeField>::BigInt>;
type ScalarField: PrimeField + Into<<Self::ScalarField as PrimeField>::BigInt>;
type BaseField: Field;
type Projective: ProjectiveCurve<
Parameters = Self::Parameters,
Expand Down
12 changes: 6 additions & 6 deletions ec/src/models/bls12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
};
use ark_ff::fields::{
fp12_2over3over2::{Fp12, Fp12Parameters},
fp2::Fp2Config,
fp6_3over2::Fp6Config,
BitIteratorBE, Field, Fp2, PrimeField, SquareRootField,
fp2::Fp2Parameters,
fp6_3over2::Fp6Parameters,
BitIteratorBE, Field, Fp2, PrimeField,
};
use core::marker::PhantomData;
use num_traits::{One, Zero};
Expand All @@ -33,9 +33,9 @@ pub trait Bls12Parameters: 'static {
/// What kind of twist is this?
const TWIST_TYPE: TwistType;

type Fp: PrimeField + SquareRootField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fp2Params: Fp2Config<Fp = Self::Fp>;
type Fp6Params: Fp6Config<Fp2Params = Self::Fp2Params>;
type Fp: PrimeField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fp2Params: Fp2Parameters<Fp = Self::Fp>;
type Fp6Params: Fp6Parameters<Fp2Params = Self::Fp2Params>;
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved
type Fp12Params: Fp12Parameters<Fp6Params = Self::Fp6Params>;
type G1Parameters: SWModelParameters<BaseField = Self::Fp>;
type G2Parameters: SWModelParameters<
Expand Down
12 changes: 6 additions & 6 deletions ec/src/models/bn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
};
use ark_ff::fields::{
fp12_2over3over2::{Fp12, Fp12Parameters},
fp2::Fp2Config,
fp6_3over2::Fp6Config,
Field, Fp2, PrimeField, SquareRootField,
fp2::Fp2Parameters,
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved
fp6_3over2::Fp6Parameters,
Field, Fp2, PrimeField,
};
use num_traits::One;

Expand All @@ -31,9 +31,9 @@ pub trait BnParameters: 'static {
const TWIST_TYPE: TwistType;
const TWIST_MUL_BY_Q_X: Fp2<Self::Fp2Params>;
const TWIST_MUL_BY_Q_Y: Fp2<Self::Fp2Params>;
type Fp: PrimeField + SquareRootField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fp2Params: Fp2Config<Fp = Self::Fp>;
type Fp6Params: Fp6Config<Fp2Params = Self::Fp2Params>;
type Fp: PrimeField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fp2Params: Fp2Parameters<Fp = Self::Fp>;
type Fp6Params: Fp6Parameters<Fp2Params = Self::Fp2Params>;
type Fp12Params: Fp12Parameters<Fp6Params = Self::Fp6Params>;
type G1Parameters: SWModelParameters<BaseField = Self::Fp>;
type G2Parameters: SWModelParameters<
Expand Down
12 changes: 6 additions & 6 deletions ec/src/models/bw6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::{
PairingEngine,
};
use ark_ff::fields::{
fp3::Fp3Config,
fp6_2over3::{Fp6, Fp6Config},
BitIteratorBE, Field, PrimeField, SquareRootField,
fp3::Fp3Parameters,
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved
fp6_2over3::{Fp6, Fp6Parameters},
BitIteratorBE, Field, PrimeField,
};
use num_traits::One;

Expand All @@ -24,9 +24,9 @@ pub trait BW6Parameters: 'static + Eq + PartialEq {
const ATE_LOOP_COUNT_2: &'static [i8];
const ATE_LOOP_COUNT_2_IS_NEGATIVE: bool;
const TWIST_TYPE: TwistType;
type Fp: PrimeField + SquareRootField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fp3Params: Fp3Config<Fp = Self::Fp>;
type Fp6Params: Fp6Config<Fp3Params = Self::Fp3Params>;
type Fp: PrimeField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fp3Params: Fp3Parameters<Fp = Self::Fp>;
type Fp6Params: Fp6Parameters<Fp3Params = Self::Fp3Params>;
type G1Parameters: SWModelParameters<BaseField = Self::Fp>;
type G2Parameters: SWModelParameters<
BaseField = Self::Fp,
Expand Down
14 changes: 7 additions & 7 deletions ec/src/models/mnt4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::{
PairingEngine,
};
use ark_ff::{
fp2::{Fp2, Fp2Config},
fp4::{Fp4, Fp4Config},
BitIteratorBE, Field, PrimeField, SquareRootField,
fp2::{Fp2, Fp2Parameters},
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved
fp4::{Fp4, Fp4Parameters},
BitIteratorBE, Field, PrimeField,
};
use num_traits::{One, Zero};

Expand All @@ -30,10 +30,10 @@ pub trait MNT4Parameters: 'static {
const FINAL_EXPONENT_LAST_CHUNK_1: <Self::Fp as PrimeField>::BigInt;
const FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG: bool;
const FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0: <Self::Fp as PrimeField>::BigInt;
type Fp: PrimeField + SquareRootField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fr: PrimeField + SquareRootField + Into<<Self::Fr as PrimeField>::BigInt>;
type Fp2Params: Fp2Config<Fp = Self::Fp>;
type Fp4Params: Fp4Config<Fp2Params = Self::Fp2Params>;
type Fp: PrimeField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fr: PrimeField + Into<<Self::Fr as PrimeField>::BigInt>;
type Fp2Params: Fp2Parameters<Fp = Self::Fp>;
type Fp4Params: Fp4Parameters<Fp2Params = Self::Fp2Params>;
type G1Parameters: SWModelParameters<BaseField = Self::Fp, ScalarField = Self::Fr>;
type G2Parameters: SWModelParameters<
BaseField = Fp2<Self::Fp2Params>,
Expand Down
14 changes: 7 additions & 7 deletions ec/src/models/mnt6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::{
PairingEngine,
};
use ark_ff::{
fp3::{Fp3, Fp3Config},
fp6_2over3::{Fp6, Fp6Config},
BitIteratorBE, Field, PrimeField, SquareRootField,
fp3::{Fp3, Fp3Parameters},
fp6_2over3::{Fp6, Fp6Parameters},
BitIteratorBE, Field, PrimeField,
};
use num_traits::{One, Zero};

Expand All @@ -30,10 +30,10 @@ pub trait MNT6Parameters: 'static {
const FINAL_EXPONENT_LAST_CHUNK_1: <Self::Fp as PrimeField>::BigInt;
const FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG: bool;
const FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0: <Self::Fp as PrimeField>::BigInt;
type Fp: PrimeField + SquareRootField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fr: PrimeField + SquareRootField + Into<<Self::Fr as PrimeField>::BigInt>;
type Fp3Params: Fp3Config<Fp = Self::Fp>;
type Fp6Params: Fp6Config<Fp3Params = Self::Fp3Params>;
type Fp: PrimeField + Into<<Self::Fp as PrimeField>::BigInt>;
type Fr: PrimeField + Into<<Self::Fr as PrimeField>::BigInt>;
type Fp3Params: Fp3Parameters<Fp = Self::Fp>;
type Fp6Params: Fp6Parameters<Fp3Params = Self::Fp3Params>;
type G1Parameters: SWModelParameters<BaseField = Self::Fp, ScalarField = Self::Fr>;
type G2Parameters: SWModelParameters<
BaseField = Fp3<Self::Fp3Params>,
Expand Down
6 changes: 3 additions & 3 deletions ec/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::AffineCurve;
use ark_ff::{fields::BitIteratorBE, Field, PrimeField, SquareRootField, Zero};
use ark_ff::{fields::BitIteratorBE, Field, PrimeField, Zero};

pub mod bls12;
pub mod bn;
Expand All @@ -11,8 +11,8 @@ pub mod twisted_edwards_extended;

/// Model parameters for an elliptic curve.
pub trait ModelParameters: Send + Sync + Sized + 'static {
type BaseField: Field + SquareRootField;
type ScalarField: PrimeField + SquareRootField + Into<<Self::ScalarField as PrimeField>::BigInt>;
type BaseField: Field;
type ScalarField: PrimeField + Into<<Self::ScalarField as PrimeField>::BigInt>;

const COFACTOR: &'static [u64];
const COFACTOR_INV: Self::ScalarField;
Expand Down
2 changes: 1 addition & 1 deletion ec/src/models/short_weierstrass_jacobian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ark_std::{

use ark_ff::{
bytes::{FromBytes, ToBytes},
fields::{Field, PrimeField, SquareRootField},
fields::{Field, PrimeField},
ToConstraintField, UniformRand,
};

Expand Down
2 changes: 1 addition & 1 deletion ec/src/models/twisted_edwards_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use zeroize::Zeroize;

use ark_ff::{
bytes::{FromBytes, ToBytes},
fields::{Field, PrimeField, SquareRootField},
fields::{Field, PrimeField},
ToConstraintField, UniformRand,
};

Expand Down
38 changes: 17 additions & 21 deletions ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ pub trait Field:
/// random field elements from a hash-function or RNG output.
fn from_random_bytes_with_flags<F: Flags>(bytes: &[u8]) -> Option<(Self, F)>;

/// Returns a `LegendreSymbol`, which indicates whether this field element
/// is 1 : a quadratic residue
/// 0 : equal to 0
/// -1 : a quadratic non-residue
fn legendre(&self) -> LegendreSymbol;

/// Returns the square root of self, if it exists.
#[must_use]
fn sqrt(&self) -> Option<Self>;

/// Sets `self` to be the square root of `self`, if it exists.
fn sqrt_in_place(&mut self) -> Option<&mut Self>;

/// Returns `self * self`.
#[must_use]
fn square(&self) -> Self;
Expand Down Expand Up @@ -329,30 +342,13 @@ pub trait PrimeField:
}
}

/// The interface for a field that supports an efficient square-root operation.
pub trait SquareRootField: Field {
/// Returns a `LegendreSymbol`, which indicates whether this field element
/// is
/// - 1: a quadratic residue
/// - 0: equal to 0
/// - -1: a quadratic non-residue
fn legendre(&self) -> LegendreSymbol;

/// Returns the square root of self, if it exists.
#[must_use]
fn sqrt(&self) -> Option<Self>;

/// Sets `self` to be the square root of `self`, if it exists.
fn sqrt_in_place(&mut self) -> Option<&mut Self>;
}

/// Indication of the field element's quadratic residuosity
///
/// # Examples
/// ```
/// # use ark_std::test_rng;
/// # use ark_std::UniformRand;
/// # use ark_test_curves::{LegendreSymbol, Field, SquareRootField, bls12_381::Fq as Fp};
/// # use ark_test_curves::{LegendreSymbol, Field, bls12_381::Fq as Fp};
/// let a: Fp = Fp::rand(&mut test_rng());
/// let b = a.square();
/// assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue);
Expand All @@ -371,7 +367,7 @@ impl LegendreSymbol {
/// ```
/// # use ark_std::test_rng;
/// # use ark_std::UniformRand;
/// # use ark_test_curves::{LegendreSymbol, Field, SquareRootField, bls12_381::Fq as Fp};
/// # use ark_test_curves::{LegendreSymbol, Field, bls12_381::Fq as Fp};
/// let a: Fp = Fp::rand(&mut test_rng());
/// let b: Fp = a.square();
/// assert!(!b.legendre().is_zero());
Expand All @@ -384,7 +380,7 @@ impl LegendreSymbol {
///
/// # Examples
/// ```
/// # use ark_test_curves::{Fp2Config, LegendreSymbol, SquareRootField, bls12_381::{Fq, Fq2Config}};
/// # use ark_test_curves::{Fp2Config, LegendreSymbol, bls12_381::{Fq, Fq2Config}};
/// let a: Fq = Fq2Config::NONRESIDUE;
/// assert!(a.legendre().is_qnr());
/// ```
Expand All @@ -398,7 +394,7 @@ impl LegendreSymbol {
/// # use ark_std::test_rng;
/// # use ark_test_curves::bls12_381::Fq as Fp;
/// # use ark_std::UniformRand;
/// # use ark_ff::{LegendreSymbol, Field, SquareRootField};
/// # use ark_ff::{LegendreSymbol, Field};
/// let a: Fp = Fp::rand(&mut test_rng());
/// let b: Fp = a.square();
/// assert!(b.legendre().is_qr());
Expand Down
8 changes: 3 additions & 5 deletions ff/src/fields/models/fp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use super::cubic_extension::*;
use crate::fields::*;
use core::marker::PhantomData;

/// Trait that specifies constants and methods for defining degree-three extension fields.
pub trait Fp3Config: 'static + Send + Sync + Sized {
/// Base prime field underlying this extension.
type Fp: PrimeField + SquareRootField;
pub trait Fp3Parameters: 'static + Send + Sync {
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved
type Fp: PrimeField;

/// Cubic non-residue in `Self::Fp` used to construct the extension
/// field. That is, `NONRESIDUE` is such that the cubic polynomial
Expand Down Expand Up @@ -100,7 +98,7 @@ impl<P: Fp3Config> Fp3<P> {
}
}

impl<P: Fp3Config> SquareRootField for Fp3<P> {
impl<P: Fp3Parameters> Field for Fp3<P> {
/// Returns the Legendre symbol.
fn legendre(&self) -> LegendreSymbol {
self.norm().legendre()
Expand Down
6 changes: 3 additions & 3 deletions ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ark_std::rand::{
use crate::{
biginteger::BigInteger,
bytes::{FromBytes, ToBytes},
fields::{Field, LegendreSymbol, PrimeField, SquareRootField},
fields::{Field, LegendreSymbol, PrimeField},
ToConstraintField, UniformRand,
};

Expand Down Expand Up @@ -387,9 +387,9 @@ impl<P: QuadExtConfig> Field for QuadExtField<P> {
}
}

impl<'a, P: QuadExtConfig> SquareRootField for QuadExtField<P>
impl<'a, P: QuadExtParameters> Field for QuadExtField<P>
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved
where
P::BaseField: SquareRootField + From<P::BasePrimeField>,
P::BaseField: Field + From<P::BasePrimeField>,
{
fn legendre(&self) -> LegendreSymbol {
// The LegendreSymbol in a field of order q for an element x can be
Expand Down
2 changes: 1 addition & 1 deletion ff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use ark_std::vec;
pub mod prelude {
pub use crate::biginteger::BigInteger;

pub use crate::fields::{Field, PrimeField, SquareRootField};
pub use crate::fields::{Field, FpParameters, PrimeField};
solomonjoseph marked this conversation as resolved.
Show resolved Hide resolved

pub use ark_std::UniformRand;

Expand Down
2 changes: 1 addition & 1 deletion test-curves/src/bls12_381/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_imports)]
use ark_ec::{models::SWModelParameters, AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, SquareRootField, UniformRand, Zero};
use ark_ff::{Field, One, UniformRand, Zero};

use crate::bls12_381::{g1, Fq, Fq2, Fq6, FqConfig, Fr, FrConfig, G1Affine, G1Projective};
use ark_algebra_test_templates::{
Expand Down
2 changes: 1 addition & 1 deletion test-curves/src/bn384_small_two_adicity/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_imports)]
use ark_ec::{models::SWModelParameters, AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, SquareRootField, UniformRand, Zero};
use ark_ff::{Field, One, UniformRand, Zero};
use ark_std::{rand::Rng, test_rng};

use crate::bn384_small_two_adicity::{g1, Fq, FqConfig, Fr, FrConfig, G1Affine, G1Projective};
Expand Down
6 changes: 3 additions & 3 deletions test-templates/src/fields.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused)]
#![allow(clippy::eq_op)]
use ark_ff::{
fields::{FftField, Field, LegendreSymbol, PrimeField, SquareRootField},
fields::{FftField, Field, LegendreSymbol, PrimeField},
Fp, MontBackend, MontConfig,
};
use ark_serialize::{buffer_bit_byte_size, Flags, SWFlags};
Expand Down Expand Up @@ -190,7 +190,7 @@ fn random_field_tests<F: Field>() {
}
}

fn random_sqrt_tests<F: SquareRootField>() {
fn random_sqrt_tests<F: Field>() {
let mut rng = ark_std::test_rng();

for _ in 0..ITERATIONS {
Expand Down Expand Up @@ -397,7 +397,7 @@ pub fn montgomery_primefield_test<T: MontConfig<N>, const N: usize>() {
}
}

pub fn sqrt_field_test<F: SquareRootField>(elem: F) {
pub fn sqrt_field_test<F: Field>(elem: F) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should integrate these into the existing field_test

let square = elem.square();
let sqrt = square.sqrt().unwrap();
assert!(sqrt == elem || sqrt == -elem);
Expand Down