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
Remove duplicate function scale_by_cofactor()
  • Loading branch information
alexander-zw committed Dec 15, 2021
commit c0f801ea30910d0db498dfef575475e5030a33c7
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
- [\#359](https://github.com/arkworks-rs/algebra/pull/359) (ark-test-templates) Simplify the field and curve test macros.
- [\#365](https://github.com/arkworks-rs/algebra/pull/365) (arc-ec)
- Move `COFACTOR`, `COFACTOR_INV`, and `is_in_correct_subgroup_assuming_on_curve()` from `{SW,TE}ModelParameters` to `ModelParameters`.
- Add `mul_bits` to `AffineCurve` and provide a default implementation of `mul` using this.
- Add `mul_bits()` to `AffineCurve` and provide a default implementation of `mul()` using this.
- Remove duplicate function `scale_by_cofactor()` from `short_weierstrass_jacobian::GroupAffine` and `twisted_edwards_extended::GroupAffine`

### Features

Expand Down
12 changes: 3 additions & 9 deletions ec/src/models/short_weierstrass_jacobian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ impl<P: Parameters> GroupAffine<P> {
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
/// 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 @@ -180,7 +174,7 @@ impl<P: Parameters> Distribution<GroupAffine<P>> for Standard {
let greatest = rng.gen();

if let Some(p) = GroupAffine::get_point_from_x(x, greatest) {
return p.scale_by_cofactor().into();
return p.mul_by_cofactor();
}
}
}
Expand Down Expand Up @@ -218,7 +212,7 @@ impl<P: Parameters> AffineCurve for GroupAffine<P> {

#[inline]
fn mul_by_cofactor_to_projective(&self) -> Self::Projective {
self.scale_by_cofactor()
self.mul_bits(BitIteratorBE::new(P::COFACTOR))
}
Pratyush marked this conversation as resolved.
Show resolved Hide resolved

fn mul_by_cofactor_inv(&self) -> Self {
Expand Down Expand Up @@ -342,7 +336,7 @@ impl<P: Parameters> Distribution<GroupProjective<P>> for Standard {
let greatest = rng.gen();

if let Some(p) = GroupAffine::get_point_from_x(x, greatest) {
return p.scale_by_cofactor().into();
return p.mul_by_cofactor_to_projective();
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions ec/src/models/twisted_edwards_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ impl<P: Parameters> GroupAffine<P> {
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 @@ -144,7 +139,7 @@ impl<P: Parameters> AffineCurve for GroupAffine<P> {

#[inline]
fn mul_by_cofactor_to_projective(&self) -> Self::Projective {
self.scale_by_cofactor()
self.mul_bits(BitIteratorBE::new(P::COFACTOR))
}

fn mul_by_cofactor_inv(&self) -> Self {
Expand Down Expand Up @@ -250,7 +245,7 @@ impl<P: Parameters> Distribution<GroupAffine<P>> for Standard {
let greatest = rng.gen();

if let Some(p) = GroupAffine::get_point_from_y(y, greatest) {
return p.scale_by_cofactor().into();
return p.mul_by_cofactor();
}
}
}
Expand Down Expand Up @@ -349,7 +344,7 @@ impl<P: Parameters> Distribution<GroupProjective<P>> for Standard {
let greatest = rng.gen();

if let Some(p) = GroupAffine::get_point_from_y(y, greatest) {
return p.scale_by_cofactor();
return p.mul_by_cofactor_to_projective();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions test-curves/src/bn384_small_two_adicity/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ark_ec::{
};
use ark_ff::{field_new, Zero};

use crate::bn384_small_two_adicity::{Fq, Fr};
use crate::bn384_small_two_adicity::{Fq, Fr, FR_ONE};

pub type G1Affine = GroupAffine<Parameters>;
pub type G1Projective = GroupProjective<Parameters>;
Expand All @@ -18,10 +18,10 @@ impl ModelParameters for Parameters {
type Affine = GroupAffine<Self>;

/// COFACTOR = 1
const COFACTOR: &'static [u64] = &[0x1];
const COFACTOR: &'static [u64] = &[1];

/// COFACTOR_INV = COFACTOR^{-1} mod r = 1
const COFACTOR_INV: Fr = field_new!(Fr, "1");
const COFACTOR_INV: Fr = FR_ONE;
}

impl SWModelParameters for Parameters {
Expand Down
3 changes: 1 addition & 2 deletions test-curves/src/mnt4_753/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl ModelParameters for Parameters {
/// COFACTOR = 1
const COFACTOR: &'static [u64] = &[1];

/// COFACTOR^(-1) mod r =
/// 1
/// COFACTOR^(-1) mod r = 1
#[rustfmt::skip]
const COFACTOR_INV: Fr = FR_ONE;
}
Expand Down
2 changes: 1 addition & 1 deletion test-templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ macro_rules! generate_g1_generator_raw_test {
let p = G1Affine::new(x, if y < -y { y } else { -y }, false);
assert!(!p.is_in_correct_subgroup_assuming_on_curve());

let g1 = p.scale_by_cofactor();
let g1 = p.mul_by_cofactor_to_projective();
if !g1.is_zero() {
assert_eq!(i, $const);
let g1 = G1Affine::from(g1);
Expand Down