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

Implement public-facing ops traits on all combos of &T/T #101

Merged
merged 1 commit into from
Jan 25, 2018

Conversation

hdevalence
Copy link
Contributor

The public-facing types with arithmetic operations are:

  • Scalars
  • ExtendedPoints
  • RistrettoPoints

For these types we define operators with all combinations of borrowed and
non-borrowed inputs, to avoid forcing API consumers to write extra ampersands.
Since all of the operations involved with these types are expensive relative to
the cost of an unnecessary copy, this isn't a big deal.

The MontgomeryPoint struct isn't included in the above because it's only
useful for scalar multiplication.

This commit is based on work by @UnlawfulMonad, and this PR supersedes #57 (which I think doesn't apply anymore).

The public-facing types with arithmetic operations are:

- `Scalar`s
- `ExtendedPoint`s
- `RistrettoPoint`s

For these types we define operators with all combinations of borrowed and
non-borrowed inputs, to avoid forcing API consumers to write extra ampersands.
Since all of the operations involved with these types are expensive relative to
the cost of an unnecessary copy, this isn't a big deal.

The `MontgomeryPoint` struct isn't included in the above because it's only
useful for scalar multiplication.

This commit is based on work by @UnlawfulMonad.

/// Define borrow and non-borrow variants of `Add`.
macro_rules! define_add_variants {
(LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
Copy link
Member

Choose a reason for hiding this comment

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

macros are cool

@isislovecruft
Copy link
Member

isislovecruft commented Jan 25, 2018

Just to reiterate what we talked about in person, so that it's recorded here for posterity:

  • Users who wish to reduce copying should prioritise using &T wherever possible.
  • We should continue to work with @rust-lang developers to try to find a solution which is both ergonomic and efficient for the majority of use cases (and hopefully ours as well). Making the compiler better makes everything better for everybody, not just our super-niche case.
  • For now, all combinations of &T/T are accepted to provide better ergonomics, and also to be more friendly to people who have cryptography experience but maybe not as much Rust experience. E.g. one could imagine the use-case of a student/researcher prototyping something who just wants a general-purpose ECC library, and that the Eye of Sauron (&(&(&(&T )))) would be pretty unintuitive if they weren't already familiar with Rust and ownership.
  • Later we may remove support for some combinations (depending on how the previous two things pan out), but—since it seems it may possibly be something which won't happen until the next Rust epoch—we're fine with having that be the change which causes us to release a curve25519-dalek 2.0.

@isislovecruft
Copy link
Member

Oh, I guess there's another @rust-lang wishlist item… (I have no idea if there's already a way to do this in Rust)

I'd like a way to spit out a programming warning (not really "warning", since IMHO compiler warnings shouldn't really be ignored, so maybe more like "a programming info") at compile time, just once, so that if we see someone compiling something which depends on our types and they've got an let foo = T + T in there, we can just give them a friendly reminder that they may want let foo = &T + &T instead. Or also like "hey, we see you are doing a boat-load of additions with ExtendedPoints, you might be interested to learn that you could micro-optimise by using ProjectivePoints and CompletedPointss."

@isislovecruft isislovecruft merged commit 52d600d into develop Jan 25, 2018
@tarcieri
Copy link
Contributor

"hey, we see you are doing a boat-load of additions with ExtendedPoints, you might be interested to learn that you could micro-optimise by using ProjectivePoints and CompletedPointss."

clippy plugins? /cc @Manishearth

@hdevalence
Copy link
Contributor Author

hdevalence commented Jan 25, 2018

People can't use the other curve models, since those types are specific to a particular backend, and are (intentionally) not exported as part of the public API. Just my 2c, but I don't feel that this is a significant performance issue, and I'd rather focus on making API that removes the need for people to try to micro-optimize their borrows, which are probably going to be elided by the compiler anyways.

@hdevalence hdevalence deleted the feature/borrow-operators branch January 29, 2018 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants