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

Change elliptic curve new point function #970

Merged
merged 26 commits into from
Feb 24, 2025
Merged

Change elliptic curve new point function #970

merged 26 commits into from
Feb 24, 2025

Conversation

nicole-graus
Copy link
Contributor

Change elliptic curve new point function

Description

This PR changes the function new for every elliptic curve in Lambdaworks. In the previous version, this function creates a new curve point but doesn't check if it satisfies the curve equation. In this PR the function checks if the point is valid returning a Result.

@nicole-graus nicole-graus requested a review from a team as a code owner February 11, 2025 14:16
@codecov-commenter
Copy link

codecov-commenter commented Feb 13, 2025

Codecov Report

Attention: Patch coverage is 90.58524% with 37 lines in your changes missing coverage. Please review.

Project coverage is 71.65%. Comparing base (d78d18a) to head (a4ef57d).

Files with missing lines Patch % Lines
math/src/elliptic_curve/short_weierstrass/point.rs 83.51% 15 Missing ⚠️
...ptic_curve/short_weierstrass/curves/stark_curve.rs 0.00% 8 Missing ⚠️
.../src/elliptic_curve/edwards/curves/tiny_jub_jub.rs 0.00% 6 Missing ⚠️
...c/elliptic_curve/montgomery/curves/tiny_jub_jub.rs 0.00% 6 Missing ⚠️
math/src/elliptic_curve/montgomery/traits.rs 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #970      +/-   ##
==========================================
+ Coverage   71.49%   71.65%   +0.15%     
==========================================
  Files         156      156              
  Lines       33969    34235     +266     
==========================================
+ Hits        24287    24531     +244     
- Misses       9682     9704      +22     

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

@@ -15,4 +17,16 @@ pub trait IsEdwards: IsEllipticCurve + Clone + Debug {
- FieldElement::<Self::BaseField>::one()
- Self::d() * x.pow(2_u16) * y.pow(2_u16)
}

// Edwards equation in projective coordinates.
// a * x^2 * z^2 + y^2 * z^2 - z^4 = d * x^2 * y^2
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be more efficient just by factoring out z^2. We can update that in future PR

@jotabulacios jotabulacios requested a review from Oppen February 19, 2025 18:33
Copy link
Contributor

@Oppen Oppen left a comment

Choose a reason for hiding this comment

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

Overall it looks correct, however:

  1. Did we check that the use of .unwrap_unchecked() actually provides any advantage over regular .unwrap()?
  2. I would suggest, regardless of which kind of unwrap we use, that we do something like the following:
let val = Curve::new(...);
debug_assert!(val.is_some());
val.unwrap()

That way we can make sure the condition holds in debug builds, without introducing any checking overhead in release builds.

Comment on lines 31 to 39
let a_g1 = BN254Curve::generator().operate_with_self(a_val).to_affine();
let b_g1 = BN254Curve::generator().operate_with_self(b_val).to_affine();

let a_g2 = BN254TwistCurve::generator()
.operate_with_self(b_val)
.to_affine();
let b_g2 = BN254TwistCurve::generator()
.operate_with_self(b_val)
.to_affine();
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this changes the whole meaning of the benchmark. E.g., the first two bench runs are precisely for converting to affine, which becomes trivial with the change.

Copy link
Contributor

@Oppen Oppen left a comment

Choose a reason for hiding this comment

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

Added a few suggestions. Those apply to other places where we changed the unwrap_uncheckeds to unwraps as well.

Comment on lines 34 to 35
// - `unwrap_unchecked()` avoids unnecessary checks, as we guarantee
// correctness based on external verification.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// - `unwrap_unchecked()` avoids unnecessary checks, as we guarantee
// correctness based on external verification.

No longer relevant, we now use the regular unwrap.

///
/// - The generator values are taken from the [Arkworks implementation](https://github.com/arkworks-rs/curves/blob/5a41d7f27a703a7ea9c48512a4148443ec6c747e/ed_on_bls12_381_bandersnatch/src/curves/mod.rs#L120)
/// and have been converted to hexadecimal.
/// - `unwrap_unchecked()` is safe because:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// - `unwrap_unchecked()` is safe because:
/// - `unwrap()` does not panic because:

FieldElement::<Self::BaseField>::new_base(
"29C132CC2C0B34C5743711777BBE42F32B79C022AD998465E1E71866A252AE18",
),
FieldElement::<Self::BaseField>::new_base(
"2A6C669EDA123E0F157D8B50BADCD586358CAD81EEE464605E3167B6CC974166",
),
FieldElement::one(),
])
]);
debug_assert!(point.is_ok());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
debug_assert!(point.is_ok());

No longer necessary, .unwrap() has the exact same effect.

@jotabulacios jotabulacios added this pull request to the merge queue Feb 24, 2025
Merged via the queue into main with commit 7e94a19 Feb 24, 2025
8 checks passed
@jotabulacios jotabulacios deleted the fix-curve-new branch February 24, 2025 20:09
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.

5 participants