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

Move common fields into ModelParameters #365

Merged
merged 16 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add associated type bounds and other PR suggestions
  • Loading branch information
alexander-zw committed Dec 12, 2021
commit 6cc4a8fb1f133f324a4db318e21fee43d814e91c
2 changes: 1 addition & 1 deletion ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub trait AffineCurve:
#[must_use]
#[inline]
fn mul<S: Into<<Self::ScalarField as PrimeField>::BigInt>>(&self, by: S) -> Self::Projective {
self.mul_bits(BitIteratorBE::new(by.into()))
self.mul_bits(BitIteratorBE::without_leading_zeros(by.into()))
}

/// Multiplies this element by the cofactor and output the
Expand Down
14 changes: 8 additions & 6 deletions ec/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ pub mod short_weierstrass_jacobian;
pub mod twisted_edwards_extended;

/// Model parameters for an elliptic curve.
pub trait ModelParameters: Send + Sync + 'static {
pub trait ModelParameters: Send + Sync + Sized + 'static {
alexander-zw marked this conversation as resolved.
Show resolved Hide resolved
type BaseField: Field + SquareRootField;
type ScalarField: PrimeField + SquareRootField + Into<<Self::ScalarField as PrimeField>::BigInt>;
type Affine: AffineCurve;
type Affine: AffineCurve<BaseField = Self::BaseField, ScalarField = Self::ScalarField>;

const COFACTOR: &'static [u64];
const COFACTOR_INV: Self::ScalarField;

/// Checks that the current point is in the prime order subgroup given
/// the point on the curve.
/// Requires type parameters G: the type of point passed in, and H: the type of
/// point that results from multiplying G by a scalar.
fn is_in_correct_subgroup_assuming_on_curve(item: &Self::Affine) -> bool {
item.mul_bits(BitIteratorBE::new(Self::ScalarField::characteristic()))
.is_zero()
}
}

/// Model parameters for a Short Weierstrass curve.
pub trait SWModelParameters: ModelParameters {
pub trait SWModelParameters:
ModelParameters<Affine = short_weierstrass_jacobian::GroupAffine<Self>>
{
const COEFF_A: Self::BaseField;
const COEFF_B: Self::BaseField;
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField);
Expand All @@ -53,7 +53,9 @@ pub trait SWModelParameters: ModelParameters {
}

/// Model parameters for a Twisted Edwards curve.
pub trait TEModelParameters: ModelParameters {
pub trait TEModelParameters:
ModelParameters<Affine = twisted_edwards_extended::GroupAffine<Self>>
{
const COEFF_A: Self::BaseField;
const COEFF_D: Self::BaseField;
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField);
Expand Down
6 changes: 4 additions & 2 deletions ec/src/models/short_weierstrass_jacobian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ impl<P: Parameters> GroupAffine<P> {
y2 == x3b
}
}
}

/// Checks if `self` is in the subgroup having order that equaling that of
/// `P::ScalarField`.
impl<P: Parameters> GroupAffine<P> {
/// Checks if `self` is in the subgroup having order equaling that of
/// `P::ScalarField` given it is on the curve.
pub fn is_in_correct_subgroup_assuming_on_curve(&self) -> bool {
Pratyush marked this conversation as resolved.
Show resolved Hide resolved
P::is_in_correct_subgroup_assuming_on_curve(self)
}
Expand Down
8 changes: 5 additions & 3 deletions ec/src/models/twisted_edwards_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ impl<P: Parameters> GroupAffine<P> {

lhs == rhs
}
}

/// Checks that the current point is in the prime order subgroup given
/// the point on the curve.
impl<P: Parameters> GroupAffine<P> {
/// Checks if `self` is in the subgroup having order equaling that of
/// `P::ScalarField` given it is on the curve.
pub fn is_in_correct_subgroup_assuming_on_curve(&self) -> bool {
P::is_in_correct_subgroup_assuming_on_curve(self)
}
Expand Down Expand Up @@ -293,7 +295,7 @@ mod group_impl {

//////////////////////////////////////////////////////////////////////////////

/// `GroupProjective` implements Extended Twisted Edwards (Jacobian) Coordinates
/// `GroupProjective` implements Extended Twisted Edwards Coordinates
/// as described in [\[HKCD08\]](https://eprint.iacr.org/2008/522.pdf).
///
/// This implementation uses the unified addition formulae from that paper (see
Expand Down
1 change: 1 addition & 0 deletions test-curves/src/bls12_381/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Parameters;
impl ModelParameters for Parameters {
type BaseField = Fq;
type ScalarField = Fr;
type Affine = GroupAffine<Self>;

/// COFACTOR = (x - 1)^2 / 3 = 76329603384216526031706109802092473003
const COFACTOR: &'static [u64] = &[0x8c00aaab0000aaab, 0x396c8c005555e156];
Expand Down
1 change: 1 addition & 0 deletions test-curves/src/bn384_small_two_adicity/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Parameters;
impl ModelParameters for Parameters {
type BaseField = Fq;
type ScalarField = Fr;
type Affine = GroupAffine<Self>;

/// COFACTOR = 1
const COFACTOR: &'static [u64] = &[0x1];
Expand Down
1 change: 1 addition & 0 deletions test-curves/src/mnt4_753/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Parameters;
impl ModelParameters for Parameters {
type BaseField = Fq;
type ScalarField = Fr;
type Affine = GroupAffine<Self>;

/// COFACTOR = 1
const COFACTOR: &'static [u64] = &[1];
Expand Down