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

feat: make AffineRepr::xy() more flexible #593

Merged
merged 7 commits into from
Apr 24, 2023

Conversation

vmx
Copy link
Contributor

@vmx vmx commented Feb 3, 2023

Description

I decided to open a PR instead of an issue, to also illustrate that the impact on the current code base won't be that big (besides it being a breaking change).


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.

closes: #XXXX


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

@vmx vmx requested review from a team as code owners February 3, 2023 12:33
@vmx vmx requested review from Pratyush, mmagician and weikengchen and removed request for a team February 3, 2023 12:33
@Pratyush
Copy link
Member

Pratyush commented Feb 3, 2023

Hm can you leave a snippet of your code here? Would like to see an example use case. Also, I believe the curve traits require the underlying data to be owned (ie any type implementing the AffineRepr trait must not contain any non-static references)

@vmx
Copy link
Contributor Author

vmx commented Feb 6, 2023

My actual code looks like this:

pub struct G1Affine(blstrs::G1Affine);

impl AffineRepr for G1Affine {fn xy(&self) -> Option<(Self::BaseField, Self::BaseField)> {
        if self.0.is_identity().into() {
            None
        } else {
            Some((Fp(self.0.x()), Fp(self.0.y())))
        }
    }}

What it shows: I indeed own the underlying data. Though I'm not able to return a reference. One thing is that self.0.x() doesn't return a reference, but even if it would, I'd need to wrap it in a type (Fp in this case) that implements the arkworks traits. I can't do that directly on the blstrs types (without forking) as they would be foreign. This Fp cannot be a reference as it is only temporarily created.

Copy link
Member

@Pratyush Pratyush left a comment

Choose a reason for hiding this comment

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

This looks good, could you just add an entry in the CHANGELOG under the Breaking changes section? Thanks!

@vmx vmx mentioned this pull request Feb 7, 2023
6 tasks
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
@vmx
Copy link
Contributor Author

vmx commented Feb 8, 2023

I've rebased it. If I should also squash the commits into a single one, let me know.

@Pratyush
Copy link
Member

Pratyush commented Feb 8, 2023

I can handle that, don't worry about it! Thanks :)

@Pratyush Pratyush added breaking-change This PR contains a breaking change T-refactor Type: cleanup/refactor labels Mar 28, 2023
@weikengchen weikengchen merged commit 2977690 into arkworks-rs:master Apr 24, 2023
aleasims pushed a commit to NilFoundation/arkworks-algebra that referenced this pull request Oct 18, 2023
* 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]>
aleasims added a commit to NilFoundation/arkworks-algebra that referenced this pull request Oct 18, 2023
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 T-refactor Type: cleanup/refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants