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

Conversation

alexander-zw
Copy link
Collaborator

@alexander-zw alexander-zw commented Dec 11, 2021

Description

In addition to some simple changes to move COFACTOR, COFACTOR_INV, and is_in_correct_subgroup_assuming_on_curve(), a new trait MultipliablePoint had to be created because each type of curve has its own GroupAffine and GroupProjective. As a result, is_in_correct_subgroup_assuming_on_curve() now has more generic types, but fortunately if the corresponding GroupAffine struct has an identically named function, that one does not require any generics. Because of this change, some duplicate code in ec/src/models/twisted_edwards_extended.rs was removed.

closes: #326


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (master)
  • Linked to GitHub issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • Re-reviewed Files changed in the GitHub PR explorer

@alexander-zw alexander-zw added D-easy Difficulty: easy T-refactor Type: cleanup/refactor breaking-change This PR contains a breaking change labels Dec 11, 2021
@alexander-zw alexander-zw self-assigned this Dec 11, 2021
@alexander-zw alexander-zw marked this pull request as ready for review December 11, 2021 05:11
@Pratyush
Copy link
Member

Hm I think you don't need to introduce a new trait; just add Affine and Projective types into the ModelParameters trait.

@alexander-zw
Copy link
Collaborator Author

alexander-zw commented Dec 12, 2021

Hm I think you don't need to introduce a new trait; just add Affine and Projective types into the ModelParameters trait.

Ah I see, still getting used to Rust

Wait, wouldn't I still need to define a trait to bound Affine so it has the mul_bits function?

@alexander-zw
Copy link
Collaborator Author

Well I used the AffineCurve trait, but now I get the compile error

expected associated type, found struct `models::short_weierstrass_jacobian::GroupAffine`

note: expected reference `&<P as models::ModelParameters>::Affine`
         found reference `&models::short_weierstrass_jacobian::GroupAffine<P>`
help: consider constraining the associated type `<P as models::ModelParameters>::Affine` to `models::short_weierstrass_jacobian::GroupAffine<P>` or calling a method that returns `<P as models::ModelParameters>::Affine`

On the line

P::is_in_correct_subgroup_assuming_on_curve(self)

ec/src/lib.rs Outdated
-> Self::Projective;
#[inline]
fn mul<S: Into<<Self::ScalarField as PrimeField>::BigInt>>(&self, by: S) -> Self::Projective {
self.mul_bits(BitIteratorBE::new(by.into()))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.mul_bits(BitIteratorBE::new(by.into()))
self.mul_bits(BitIteratorBE::without_leading_zeros(by.into()))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this more efficient? Why isn't it the default new implementation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It doesn't seem that this does anything in this case, because mul_bits() calls skip_while() anyway

ec/src/models/mod.rs Outdated Show resolved Hide resolved
@Pratyush
Copy link
Member

Pratyush commented Dec 12, 2021

Well I used the AffineCurve trait, but now I get the compile error

expected associated type, found struct `models::short_weierstrass_jacobian::GroupAffine`

note: expected reference `&<P as models::ModelParameters>::Affine`
         found reference `&models::short_weierstrass_jacobian::GroupAffine<P>`
help: consider constraining the associated type `<P as models::ModelParameters>::Affine` to `models::short_weierstrass_jacobian::GroupAffine<P>` or calling a method that returns `<P as models::ModelParameters>::Affine`

On the line

P::is_in_correct_subgroup_assuming_on_curve(self)

You have to constrain the Parameters associated type to match the respective Affine struct. You can do that by changing the definition of TEParameters to trait TEParameters: ModelParameters<Affine = twisted_edwards::GroupAffine<Self>>, and similarly for SWParameters (i.e., see the suggestions below).

@Pratyush Pratyush marked this pull request as draft December 12, 2021 08:13
ec/src/models/mod.rs Outdated Show resolved Hide resolved
ec/src/models/mod.rs Outdated Show resolved Hide resolved
@alexander-zw
Copy link
Collaborator Author

Well I used the AffineCurve trait, but now I get the compile error

expected associated type, found struct `models::short_weierstrass_jacobian::GroupAffine`

note: expected reference `&<P as models::ModelParameters>::Affine`
         found reference `&models::short_weierstrass_jacobian::GroupAffine<P>`
help: consider constraining the associated type `<P as models::ModelParameters>::Affine` to `models::short_weierstrass_jacobian::GroupAffine<P>` or calling a method that returns `<P as models::ModelParameters>::Affine`

On the line

P::is_in_correct_subgroup_assuming_on_curve(self)

You have to constrain the Parameters associated type to match the respective Affine struct. You can do that by changing the definition of TEParameters to trait TEParameters: ModelParameters<Affine = twisted_edwards::GroupAffine<Self>>, and similarly for SWParameters (i.e., see the suggestions below).

Oh I was going to change every instance of Parameters into Parameters<Affine = Self>, your suggestion is much easier

CHANGELOG.md Outdated Show resolved Hide resolved
ec/src/lib.rs Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
let bits = BitIteratorBE::new(by.into());
self.mul_bits(bits)
}

#[inline]
fn mul_by_cofactor_to_projective(&self) -> Self::Projective {
self.scale_by_cofactor()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why do they have both mul_by_cofactor_to_projective and scale_by_cofactor if they do the exact same thing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Update: I removed scale_by_cofactor

@alexander-zw alexander-zw marked this pull request as ready for review December 16, 2021 00:44
@Pratyush Pratyush changed the title [#326] Move some fields into the parent trait ModelParameters Move common fields into ModelParameters Dec 22, 2021
@Pratyush Pratyush merged commit 1b44dbb into arkworks-rs:master Dec 22, 2021
Pratyush added a commit that referenced this pull request Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This PR contains a breaking change D-easy Difficulty: easy T-refactor Type: cleanup/refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move COFACTOR, COFACTOR_INV, and is_in_correct_subgroup_assuming_on_curve() to ModelParameters
2 participants