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
Move is_in_correct_subgroup_assuming_on_curve() into the parent trait…
… ModelParameters
  • Loading branch information
alexander-zw committed Dec 11, 2021
commit 5c2613565087601fe5409505e9d020b7f7828332
25 changes: 16 additions & 9 deletions ec/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::models::short_weierstrass_jacobian::GroupAffine;
use ark_ff::{fields::BitIteratorBE, Field, PrimeField, SquareRootField, Zero};

pub mod bls12;
Expand All @@ -9,13 +8,29 @@ pub mod mnt6;
pub mod short_weierstrass_jacobian;
pub mod twisted_edwards_extended;

/// Required for the is_in_correct_subgroup_assuming_on_curve function.
pub trait MultipliablePoint<R: Zero> {
fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> R;
}

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

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

/// 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<G, H>(item: &G) -> bool
where
G: MultipliablePoint<H>,
H: Zero,
{
item.mul_bits(BitIteratorBE::new(Self::ScalarField::characteristic()))
.is_zero()
}
}

/// Model parameters for a Short Weierstrass curve.
Expand All @@ -40,14 +55,6 @@ pub trait SWModelParameters: ModelParameters {
}
*elem
}

fn is_in_correct_subgroup_assuming_on_curve(item: &GroupAffine<Self>) -> bool
where
Self: Sized,
{
item.mul_bits(BitIteratorBE::new(Self::ScalarField::characteristic()))
.is_zero()
}
}

/// Model parameters for a Twisted Edwards curve.
Expand Down
32 changes: 18 additions & 14 deletions ec/src/models/short_weierstrass_jacobian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use ark_ff::{
ToConstraintField, UniformRand,
};

use crate::{models::SWModelParameters as Parameters, AffineCurve, ProjectiveCurve};
use crate::{
models::SWModelParameters as Parameters, AffineCurve, MultipliablePoint, ProjectiveCurve,
};

use num_traits::{One, Zero};
use zeroize::Zeroize;
Expand Down Expand Up @@ -69,20 +71,10 @@ impl<P: Parameters> Display for GroupAffine<P> {
}
}

impl<P: Parameters> GroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField, infinity: bool) -> Self {
Self { x, y, infinity }
}

/// Multiply `self` by the cofactor of the curve, `P::COFACTOR`.
pub fn scale_by_cofactor(&self) -> GroupProjective<P> {
let cofactor = BitIteratorBE::new(P::COFACTOR);
self.mul_bits(cofactor)
}

Pratyush marked this conversation as resolved.
Show resolved Hide resolved
impl<P: Parameters> MultipliablePoint<GroupProjective<P>> for GroupAffine<P> {
/// Multiplies `self` by the scalar represented by `bits`. `bits` must be a
/// big-endian bit-wise decomposition of the scalar.
pub(crate) fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> GroupProjective<P> {
alexander-zw marked this conversation as resolved.
Show resolved Hide resolved
fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> GroupProjective<P> {
let mut res = GroupProjective::zero();
// Skip leading zeros.
for i in bits.skip_while(|b| !b) {
Expand All @@ -93,6 +85,18 @@ impl<P: Parameters> GroupAffine<P> {
}
res
}
}

impl<P: Parameters> GroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField, infinity: bool) -> Self {
Self { x, y, infinity }
}

/// Multiply `self` by the cofactor of the curve, `P::COFACTOR`.
pub fn scale_by_cofactor(&self) -> GroupProjective<P> {
let cofactor = BitIteratorBE::new(P::COFACTOR);
self.mul_bits(cofactor)
}

/// Attempts to construct an affine point given an x-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
Expand Down Expand Up @@ -137,7 +141,7 @@ impl<P: Parameters> GroupAffine<P> {
/// Checks if `self` is in the subgroup having order that equaling that of
/// `P::ScalarField`.
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)
P::is_in_correct_subgroup_assuming_on_curve::<GroupAffine<P>, GroupProjective<P>>(self)
}
}

Expand Down
33 changes: 18 additions & 15 deletions ec/src/models/twisted_edwards_extended.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
models::{MontgomeryModelParameters as MontgomeryParameters, TEModelParameters as Parameters},
AffineCurve, ProjectiveCurve,
AffineCurve, MultipliablePoint, ProjectiveCurve,
};
use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
Expand Down Expand Up @@ -29,6 +29,8 @@ use ark_ff::{
#[cfg(feature = "parallel")]
use rayon::prelude::*;

/// Affine coordinates for a point on a twisted Edwards curve, over the
/// base field `P::BaseField`.
#[derive(Derivative)]
#[derivative(
Copy(bound = "P: Parameters"),
Expand All @@ -50,19 +52,10 @@ impl<P: Parameters> Display for GroupAffine<P> {
}
}

impl<P: Parameters> GroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Self { x, y }
}

#[must_use]
pub fn scale_by_cofactor(&self) -> <Self as AffineCurve>::Projective {
self.mul_bits(BitIteratorBE::new(P::COFACTOR))
}

impl<P: Parameters> MultipliablePoint<GroupProjective<P>> for GroupAffine<P> {
/// Multiplies `self` by the scalar represented by `bits`. `bits` must be a
/// big-endian bit-wise decomposition of the scalar.
pub(crate) fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> GroupProjective<P> {
fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> GroupProjective<P> {
let mut res = GroupProjective::zero();
for i in bits.skip_while(|b| !b) {
res.double_in_place();
Expand All @@ -72,6 +65,17 @@ impl<P: Parameters> GroupAffine<P> {
}
res
}
}

impl<P: Parameters> GroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Self { x, y }
}

#[must_use]
pub fn scale_by_cofactor(&self) -> <Self as AffineCurve>::Projective {
self.mul_bits(BitIteratorBE::new(P::COFACTOR))
}

/// Attempts to construct an affine point given an y-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
Expand Down Expand Up @@ -115,8 +119,7 @@ impl<P: Parameters> GroupAffine<P> {
/// Checks that the current point is in the prime order subgroup given
/// the point on the curve.
pub fn is_in_correct_subgroup_assuming_on_curve(&self) -> bool {
self.mul_bits(BitIteratorBE::new(P::ScalarField::characteristic()))
.is_zero()
P::is_in_correct_subgroup_assuming_on_curve::<GroupAffine<P>, GroupProjective<P>>(self)
}
}

Expand Down Expand Up @@ -296,7 +299,7 @@ mod group_impl {

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

/// `GroupProjective` implements Extended Twisted Edwards Coordinates
/// `GroupProjective` implements Extended Twisted Edwards (Jacobian) Coordinates
alexander-zw marked this conversation as resolved.
Show resolved Hide resolved
/// as described in [\[HKCD08\]](https://eprint.iacr.org/2008/522.pdf).
///
/// This implementation uses the unified addition formulae from that paper (see
Expand Down