-
Notifications
You must be signed in to change notification settings - Fork 711
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 PartialEq and Eq for algorithm types #553
Implement PartialEq and Eq for algorithm types #553
Conversation
Coverage decreased (-0.3%) to 95.575% when pulling 9ec19b6dd827608b80336b71a4a7aaf9bd770d59 on mrwhythat:algorithms-eq-implementation into 67c526b on briansmith:master. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
fn eq(&self, other: &Self) -> bool { self.id == other.id } | ||
} | ||
|
||
impl Eq for Algorithm {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Eq
can be derived and only PartialEq
needs to be explicitly implemented, right? Assuming so, let's always derive Eq
instead of doing impl Eq for ...
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I tried deriving Eq
first, but it broke the build. It seems that non-Eq
fields make it impossible to derive. Or maybe it's the ordering. I'll research.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some research and it seems that derive(Eq)
simply checks for every field, it does not look for existing required impls, like Haskell does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it. It used to work, but now it doesn't, and that is intentional: rust-lang/rust#36830.
src/aead/chacha20_poly1305.rs
Outdated
}; | ||
|
||
/// Copies |key| into |ctx_buf|. | ||
pub fn chacha20_poly1305_init(ctx_buf: &mut [u8], key: &[u8]) | ||
-> Result<(), error::Unspecified> { | ||
-> Result<(), error::Unspecified> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we should reformat the code, but not in this PR. Please remove all the unrelated formatting changes.
src/agreement.rs
Outdated
ECDH_P256, | ||
/// Defined in 'ec/suite_b/ecdh.rs' | ||
ECDH_P384, | ||
/// Defined in 'ec/curve25519/x25519.rs' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop all the "Defined in" comments.
src/agreement.rs
Outdated
|
||
/// Key agreement algorithm types. | ||
#[derive(PartialEq, Eq)] | ||
#[allow(non_camel_case_types)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put the allow(...)
ahead of derive(...)
in this and the other ones. No order seems better than any other, so let's things in alphabetical order so we don't have to decide another order.
src/ec/suite_b/ecdsa.rs
Outdated
@@ -50,6 +66,12 @@ enum ECDSAVerificationAlgorithmID { | |||
ECDSA_P384_SHA384_FIXED, | |||
} | |||
|
|||
impl PartialEq for ECDSAVerificationAlgorithm { | |||
fn eq(&self, other: &Self) -> bool { self.id == other.id } | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not implement PartialEq for this type. Instead we should implement PartialEq for signature::VerificationAlgorithm
in some way. Maybe we need to make signature::VerificationAlgorithm
a struct that can hold the id field first. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there is PartialEq
implementation for ECDSASigningAlgorithm
, so implementing it for ECDSAVerificationAlgorithm
only benefits the symmetricity. Also, signature::VerificationAlgorithm
is a very elegant single-method protocol, which I'm a big fan of, and which modern cryptographic libraries lack, so I think coercing it to struct is a bad idea, but it may just be my irrational thinking:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there is PartialEq implementation for ECDSASigningAlgorithm
Yes, but that's because we've not yet found a solution for creating a unifying ring::signature::SigningAlgorithm
interface.
However, we already have ring::signature::VerificationAlgorithm
, and ultimatenly for these comparison operators to be most useful, we need to be able to compare two VerificationAlgorithm
s, including comparing an RSA algorithm to an ECDSA algorithm. That means that ring::signature::VerificationAlgorithm
is what ultimately needs to implement Eq
/PartialEq
. Let's try to find a way to do that. For now, I'll remove this part.
9ec19b6
to
89bef08
Compare
@briansmith , just committed requested changes |
fn eq(&self, other: &Self) -> bool { self.id == other.id } | ||
} | ||
|
||
impl Eq for Algorithm {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it. It used to work, but now it doesn't, and that is intentional: rust-lang/rust#36830.
src/agreement.rs
Outdated
pub enum AlgorithmID { | ||
ECDH_P256, | ||
ECDH_P384, | ||
X25519, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CurveID
already exists, so we don't need this. I'll remove this in favor of an approach using CurveID
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed it in favor of algorithm.i.curve.id
comparison.
src/ec/suite_b/ecdsa.rs
Outdated
@@ -50,6 +66,12 @@ enum ECDSAVerificationAlgorithmID { | |||
ECDSA_P384_SHA384_FIXED, | |||
} | |||
|
|||
impl PartialEq for ECDSAVerificationAlgorithm { | |||
fn eq(&self, other: &Self) -> bool { self.id == other.id } | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there is PartialEq implementation for ECDSASigningAlgorithm
Yes, but that's because we've not yet found a solution for creating a unifying ring::signature::SigningAlgorithm
interface.
However, we already have ring::signature::VerificationAlgorithm
, and ultimatenly for these comparison operators to be most useful, we need to be able to compare two VerificationAlgorithm
s, including comparing an RSA algorithm to an ECDSA algorithm. That means that ring::signature::VerificationAlgorithm
is what ultimately needs to implement Eq
/PartialEq
. Let's try to find a way to do that. For now, I'll remove this part.
89bef08
to
e62a72d
Compare
I agree to license my contributions to each file under the terms given at the top of each file I changed.
e62a72d
to
0fdb9b4
Compare
Any additional changes required? |
This will let me delete some code that computes |
This landed with modifications and was released in ring 0.12.1. The commits are: Thanks for submitting this and sorry for the long delay in getting it landed. @ctz wrote:
I just noticed that, and just sent you a PR: rustls/rustls#117 |
PR for #542
#[derive(Eq)]
seems to not work for algorithm types because of other fields (withfn
types for example), so explicitimpl Eq for Type {}
were added.Also, some unintended diff in
aead/chacha20_poly1305.rs
andagreement.rs
, because I userustfmt
tool in my emacs. If this is a problem, I will restore it.