Skip to content

Commit

Permalink
feat: make AffineRepr::xy() more flexible (arkworks-rs#593)
Browse files Browse the repository at this point in the history
* feat: make AffineRepr::xy() more flexible

Currently [`AffineRepr::xy()` returns the field elements as references]. This is
only possible if the underlying data structure that implements that traits owns
those field elements and can directly have a reference to it.

Though there are cases, where providing those direct references is not possible.
For example if the underlying structure has a different layout, or if the fields
elements you're returning need to be wrapped in a newtype struct (that's my case).

With this change, `AffineRepr::xy()` now returns owned data. It's more flexible
as now the underlying data structure can have any shape as long as it can return
those field elements somehow.

[`AffineRepr::xy()` returns the field elements as references]: https://github.com/arkworks-rs/algebra/blob/4e4fc17e69178ab7f929602f17a5e769ebaf34c7/ec/src/lib.rs#L208

* chore: add changelog entry

---------

Co-authored-by: Pratyush Mishra <[email protected]>
Co-authored-by: mmagician <[email protected]>
  • Loading branch information
3 people authored and aleasims committed Oct 18, 2023
1 parent d343e2e commit 0dd47b3
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Breaking changes

- [\#593](https://github.com/arkworks-rs/algebra/pull/593) (`ark-ec`) Change `AffineRepr::xy()` to return owned values.

### Features

### Improvements
Expand Down
6 changes: 3 additions & 3 deletions ec/src/hashing/curve_maps/wb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ where
let y_num = DensePolynomial::from_coefficients_slice(self.y_map_numerator);
let y_den = DensePolynomial::from_coefficients_slice(self.y_map_denominator);

let mut v: [BaseField<Domain>; 2] = [x_den.evaluate(x), y_den.evaluate(x)];
let mut v: [BaseField<Domain>; 2] = [x_den.evaluate(&x), y_den.evaluate(&x)];
batch_inversion(&mut v);
let img_x = x_num.evaluate(x) * v[0];
let img_y = (y_num.evaluate(x) * y) * v[1];
let img_x = x_num.evaluate(&x) * v[0];
let img_y = (y_num.evaluate(&x) * y) * v[1];
Ok(Affine::<Codomain>::new_unchecked(img_x, img_y))
},
None => Ok(Affine::identity()),
Expand Down
6 changes: 3 additions & 3 deletions ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ pub trait AffineRepr:
+ MulAssign<Self::ScalarField>; // needed due to https://github.com/rust-lang/rust/issues/69640

/// Returns the x and y coordinates of this affine point.
fn xy(&self) -> Option<(&Self::BaseField, &Self::BaseField)>;
fn xy(&self) -> Option<(Self::BaseField, Self::BaseField)>;

/// Returns the x coordinate of this affine point.
fn x(&self) -> Option<&Self::BaseField> {
fn x(&self) -> Option<Self::BaseField> {
self.xy().map(|(x, _)| x)
}

/// Returns the y coordinate of this affine point.
fn y(&self) -> Option<&Self::BaseField> {
fn y(&self) -> Option<Self::BaseField> {
self.xy().map(|(_, y)| y)
}

Expand Down
4 changes: 2 additions & 2 deletions ec/src/models/bls12/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<P: Bls12Config> From<G2Affine<P>> for G2Prepared<P> {
ell_coeffs: vec![],
infinity: true,
};
q.xy().map_or(zero, |(&q_x, &q_y)| {
q.xy().map_or(zero, |(q_x, q_y)| {
let mut ell_coeffs = vec![];
let mut r = G2HomProjective::<P> {
x: q_x,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<P: Bls12Config> G2HomProjective<P> {
}

fn add_in_place(&mut self, q: &G2Affine<P>) -> EllCoeff<P> {
let (&qx, &qy) = q.xy().unwrap();
let (qx, qy) = q.xy().unwrap();
// Formula for line function when working with
// homogeneous projective coordinates.
let theta = self.y - &(qy * &self.z);
Expand Down
8 changes: 4 additions & 4 deletions ec/src/models/bls12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ impl<P: Bls12Config> Bls12<P> {

match P::TWIST_TYPE {
TwistType::M => {
c2.mul_assign_by_fp(py);
c1.mul_assign_by_fp(px);
c2.mul_assign_by_fp(&py);
c1.mul_assign_by_fp(&px);
f.mul_by_014(&c0, &c1, &c2);
},
TwistType::D => {
c0.mul_assign_by_fp(py);
c1.mul_assign_by_fp(px);
c0.mul_assign_by_fp(&py);
c1.mul_assign_by_fp(&px);
f.mul_by_034(&c0, &c1, &c2);
},
}
Expand Down
4 changes: 2 additions & 2 deletions ec/src/models/short_weierstrass/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ impl<P: SWCurveConfig> AffineRepr for Affine<P> {
type ScalarField = P::ScalarField;
type Group = Projective<P>;

fn xy(&self) -> Option<(&Self::BaseField, &Self::BaseField)> {
(!self.infinity).then(|| (&self.x, &self.y))
fn xy(&self) -> Option<(Self::BaseField, Self::BaseField)> {
(!self.infinity).then(|| (self.x, self.y))
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions ec/src/models/short_weierstrass/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl<P: SWCurveConfig, T: Borrow<Affine<P>>> AddAssign<T> for Projective<P> {
/// Using <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl>
fn add_assign(&mut self, other: T) {
let other = other.borrow();
if let Some((&other_x, &other_y)) = other.xy() {
if let Some((other_x, other_y)) = other.xy() {
if self.is_zero() {
self.x = other_x;
self.y = other_y;
Expand Down Expand Up @@ -564,7 +564,7 @@ impl<P: SWCurveConfig, T: Borrow<P::ScalarField>> Mul<T> for Projective<P> {
impl<P: SWCurveConfig> From<Affine<P>> for Projective<P> {
#[inline]
fn from(p: Affine<P>) -> Projective<P> {
p.xy().map_or(Projective::zero(), |(&x, &y)| Self {
p.xy().map_or(Projective::zero(), |(x, y)| Self {
x,
y,
z: P::BaseField::one(),
Expand Down
4 changes: 2 additions & 2 deletions ec/src/models/twisted_edwards/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ impl<P: TECurveConfig> AffineRepr for Affine<P> {
type ScalarField = P::ScalarField;
type Group = Projective<P>;

fn xy(&self) -> Option<(&Self::BaseField, &Self::BaseField)> {
(!self.is_zero()).then(|| (&self.x, &self.y))
fn xy(&self) -> Option<(Self::BaseField, Self::BaseField)> {
(!self.is_zero()).then(|| (self.x, self.y))
}

fn generator() -> Self {
Expand Down

0 comments on commit 0dd47b3

Please sign in to comment.