Skip to content

feat: twisted edwards curves #633

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

qalisander
Copy link
Member

Follows #589

PR Checklist

  • Tests
  • Documentation
  • Changelog

Copy link

netlify bot commented Apr 22, 2025

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit fcd9506
🔍 Latest deploy log https://app.netlify.com/projects/contracts-stylus/deploys/685e7ed86c11880008df7dbf

Copy link

codecov bot commented Apr 22, 2025

Codecov Report

Attention: Patch coverage is 54.13333% with 172 lines in your changes missing coverage. Please review.

Project coverage is 84.9%. Comparing base (e15e579) to head (bbcb612).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
lib/crypto/src/curve/te/projective.rs 53.2% 78 Missing ⚠️
lib/crypto/src/curve/te/affine.rs 31.6% 69 Missing ⚠️
lib/crypto/src/curve/te/mod.rs 81.6% 18 Missing ⚠️
lib/crypto/src/field/fp.rs 22.2% 7 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
lib/crypto/src/curve/mod.rs 16.3% <ø> (ø)
lib/crypto/src/field/fp.rs 73.2% <22.2%> (-1.1%) ⬇️
lib/crypto/src/curve/te/mod.rs 81.6% <81.6%> (ø)
lib/crypto/src/curve/te/affine.rs 31.6% <31.6%> (ø)
lib/crypto/src/curve/te/projective.rs 53.2% <53.2%> (ø)

... and 1 file with indirect coverage changes

@bidzyyys bidzyyys linked an issue May 27, 2025 that may be closed by this pull request
1 task
@qalisander qalisander marked this pull request as ready for review June 11, 2025 12:23
Comment on lines 48 to 52
/// Checks that the current point is in the prime order subgroup given
/// the point on the curve.
fn is_in_correct_subgroup_assuming_on_curve(item: &Affine<Self>) -> bool {
Self::mul_affine(item, Self::ScalarField::characteristic()).is_zero()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// Checks that the current point is in the prime order subgroup given
/// the point on the curve.
fn is_in_correct_subgroup_assuming_on_curve(item: &Affine<Self>) -> bool {
Self::mul_affine(item, Self::ScalarField::characteristic()).is_zero()
}
/// Checks that the current point is in the prime order subgroup given
/// the point on the curve.
fn is_in_prime_order_subgroup(item: &Affine<Self>) -> bool {
Self::mul_affine(item, Self::ScalarField::characteristic()).is_zero()
}

Judging by docs, it seems that this is what it's checking

Comment on lines +116 to +118
fn xy(&self) -> Option<(Self::BaseField, Self::BaseField)> {
(!self.is_zero()).then_some((self.x, self.y))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you think it's better to return None for zero than to return actual zero tuple ((P::BaseField::ZERO, P::BaseField::ONE))?

// See "Twisted Edwards Curves Revisited"
// Huseyin Hisil, Kenneth Koon-Ho Wong, Gary Carter, and Ed Dawson
// 3.3 Doubling in E^e
// Source: https://www.hyperelliptic.org/EFD/g1p/data/twisted/extended/doubling/dbl-2008-hwcd
Copy link
Collaborator

Choose a reason for hiding this comment

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

Better to put this comment above as fn docs

Comment on lines +247 to +278
fn add_assign(&mut self, other: T) {
let other = other.borrow();
// See "Twisted Edwards Curves Revisited"
// Huseyin Hisil, Kenneth Koon-Ho Wong, Gary Carter, and Ed Dawson
// 3.1 Unified Addition in E^e
// Source: https://www.hyperelliptic.org/EFD/g1p/data/twisted/extended/addition/madd-2008-hwcd

// A = X1*X2
let a = self.x * other.x;
// B = Y1*Y2
let b = self.y * other.y;
// C = T1*d*T2
let c = P::COEFF_D * self.t * other.x * other.y;

// D = Z1
let d = self.z;
// E = (X1+Y1)*(X2+Y2)-A-B
let e = (self.x + self.y) * (other.x + other.y) - a - b;
// F = D-C
let f = d - c;
// G = D+C
let g = d + c;
// H = B-a*A
let h = b - P::mul_by_a(a);
// X3 = E*F
self.x = e * f;
// Y3 = G*H
self.y = g * h;
// T3 = E*H
self.t = e * h;
// Z3 = F*G
self.z = f * g;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question as before for comments

Comment on lines +329 to +338
// See "Twisted Edwards Curves Revisited" (https://eprint.iacr.org/2008/522.pdf)
// by Huseyin Hisil, Kenneth Koon-Ho Wong, Gary Carter, and Ed Dawson
// 3.1 Unified Addition in E^e

// A = x1 * x2
let a = self.x * other.x;

// B = y1 * y2
let b = self.y * other.y;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question as before for comments


impl_additive_ops_from_ref!(Projective, TECurveConfig);

impl<'a, P: TECurveConfig> Add<&'a Self> for Projective<P> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to implement ref + ref?

///
/// * If point is not on curve.
/// * If point is not in the prime-order subgroup.
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Copy link
Collaborator

@0xNeshi 0xNeshi Jun 19, 2025

Choose a reason for hiding this comment

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

Some general comments:

  • could any of the new functions be marked with inline(always) or must_use?
  • some flows are not covered with unit tests
  • could we implement any proptests for this?
  • missing CHANGELOG

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree with @0xNeshi 💯

Copy link
Collaborator

@bidzyyys bidzyyys left a comment

Choose a reason for hiding this comment

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

Almost looks good, take care of @0xNeshi comments please.

///
/// * If point is not on curve.
/// * If point is not in the prime-order subgroup.
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree with @0xNeshi 💯

}

/// Default implementation of group multiplication for projective
/// coordinates
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// coordinates
/// coordinates.

}

/// Default implementation of group multiplication for 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.

Suggested change
/// coordinates
/// coordinates.


batch_inversion(&mut z_s);

// Perform affine transformations
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Perform affine transformations
// Perform affine transformations.

fn normalize_batch(v: &[Self]) -> Vec<Self::Affine> {
// A projective curve element (x, y, t, z) is normalized
// to its affine representation, by the conversion
// (x, y, t, z) -> (x/z, y/z, t/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.

Suggested change
// (x, y, t, z) -> (x/z, y/z, t/z, 1)
// (x, y, t, z) -> (x/z, y/z, t/z, 1).

// (x, y, t, z) -> (x/z, y/z, t/z, 1)
// Batch normalizing N twisted edwards curve elements costs:
// 1 inversion + 6N field multiplications
// (batch inversion requires 3N multiplications + 1 inversion)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// (batch inversion requires 3N multiplications + 1 inversion)
// (batch inversion requires 3N multiplications + 1 inversion).

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.

[Feature]: Twisted Edwards Curves
3 participants