Skip to content

Add Jacobian coordinates for Short Weierstrass #917

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

Merged
merged 15 commits into from
Sep 25, 2024

Conversation

jotabulacios
Copy link
Contributor

Add Jacobian coordinates

Description

This PR adds Jacobian coordinates for Short Weierstrass elliptic curves.

Type of change

  • New feature

@codecov-commenter
Copy link

codecov-commenter commented Sep 18, 2024

Codecov Report

Attention: Patch coverage is 75.61837% with 69 lines in your changes missing coverage. Please review.

Project coverage is 73.36%. Comparing base (d663fdf) to head (899c1e1).

Files with missing lines Patch % Lines
math/src/elliptic_curve/short_weierstrass/point.rs 75.00% 58 Missing ⚠️
math/src/elliptic_curve/point.rs 78.43% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #917      +/-   ##
==========================================
+ Coverage   73.34%   73.36%   +0.01%     
==========================================
  Files         155      155              
  Lines       35328    35611     +283     
==========================================
+ Hits        25913    26127     +214     
- Misses       9415     9484      +69     
Flag Coverage Δ
73.36% <75.61%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -52,6 +52,27 @@ impl<E: IsShortWeierstrass> ShortWeierstrassProjectivePoint<E> {
Self(self.0.to_affine())
}

// Need to check if this is correct
// It think it is not
pub fn to_jacobian(&self) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is transforming to affine

@@ -272,6 +294,9 @@ where
bytes.extend(&x_bytes);
bytes.extend(&y_bytes);
}
PointFormat::Jacobian => {
panic!("Jacobian format is not supporter for ShortWeierstrassProjectivePoint");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should handle this without panicking

[self.x(), self.y(), self.z()]
}

pub fn to_affine(&self) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is already a function defined previously

@jotabulacios jotabulacios marked this pull request as ready for review September 18, 2024 14:26
@jotabulacios jotabulacios requested a review from a team as a code owner September 18, 2024 14:26

pub fn to_affine(&self) -> (FieldElement<E::BaseField>, FieldElement<E::BaseField>) {
if self.is_neutral_element() {
panic!("Cannot convert the point at infinity to affine coordinates");
Copy link
Collaborator

Choose a reason for hiding this comment

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

You have a representation for the point in affine

let e = &a * FieldElement::<E::BaseField>::from(3u64); // E = 3 * A
let f = e.square(); // F = E^2
let x3 = &f - &d.double(); // X3 = F - 2 * D
let y3 = &e * (&d - &x3) - &c * FieldElement::<E::BaseField>::from(8u64); // Y3 = E * (D - X3) - 8 * C
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps this is more efficient as double().double().double()

let [x, y, z] = self.coordinates();
// If it's the point at infinite
if z == &FieldElement::zero() {
// We make sure all the points in the infinite have the same values
Copy link
Collaborator

Choose a reason for hiding this comment

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

points at infinity


let a_coeff = E::a();
//http://www.hyperelliptic.org/EFD/g1p/data/shortw/jacobian-0/doubling/dbl-2009-l
if a_coeff == FieldElement::zero() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

no need to create a_coeff

let [x2, y2, z2] = other.coordinates();

if *z2 != FieldElement::one() {
panic!("The second point must be in affine coordinates (Z = 1)");
Copy link
Collaborator

Choose a reason for hiding this comment

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

we could leave as unchecked or handle the error

return self.clone();
}

let z1z1 = z1.square();
Copy link
Collaborator

Choose a reason for hiding this comment

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

The points could be the same, so we have to handle that fact

impl<E: IsShortWeierstrass> ShortWeierstrassJacobianPoint<E> {
/// Creates an elliptic curve point giving the projective [x: y: z] coordinates.
pub const fn new(value: [FieldElement<E::BaseField>; 3]) -> Self {
Self(JacobianPoint::new(value))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not check that the point is valid


#[cfg(feature = "alloc")]
use crate::{
elliptic_curve::short_weierstrass::curves::bls12_381::field_extension::BLS12381PrimeField,
field::element::FieldElement,
};
pub const SUBGROUP_ORDER: U256 = U256::from_hex_unchecked(
"73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't define new constants, either import them or create an integer

@diegokingston diegokingston added this pull request to the merge queue Sep 25, 2024
Merged via the queue into main with commit a591186 Sep 25, 2024
8 checks passed
@diegokingston diegokingston deleted the add_jacobian_coordinates_sw branch September 25, 2024 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants