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 PartialEq and Eq for algorithm types #553

Conversation

rodentrabies
Copy link
Contributor

PR for #542

#[derive(Eq)] seems to not work for algorithm types because of other fields (with fn types for example), so explicit impl Eq for Type {} were added.

Also, some unintended diff in aead/chacha20_poly1305.rs and agreement.rs, because I use rustfmt tool in my emacs. If this is a problem, I will restore it.

@coveralls
Copy link

coveralls commented Jun 23, 2017

Coverage Status

Coverage decreased (-0.3%) to 95.575% when pulling 9ec19b6dd827608b80336b71a4a7aaf9bd770d59 on mrwhythat:algorithms-eq-implementation into 67c526b on briansmith:master.

Copy link
Owner

@briansmith briansmith left a 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 {}
Copy link
Owner

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 ....

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Owner

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.

};

/// Copies |key| into |ctx_buf|.
pub fn chacha20_poly1305_init(ctx_buf: &mut [u8], key: &[u8])
-> Result<(), error::Unspecified> {
-> Result<(), error::Unspecified> {
Copy link
Owner

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'
Copy link
Owner

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)]
Copy link
Owner

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.

@@ -50,6 +66,12 @@ enum ECDSAVerificationAlgorithmID {
ECDSA_P384_SHA384_FIXED,
}

impl PartialEq for ECDSAVerificationAlgorithm {
fn eq(&self, other: &Self) -> bool { self.id == other.id }
}
Copy link
Owner

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?

Copy link
Contributor Author

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:)

Copy link
Owner

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 VerificationAlgorithms, 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.

@rodentrabies rodentrabies force-pushed the algorithms-eq-implementation branch from 9ec19b6 to 89bef08 Compare June 30, 2017 20:51
@rodentrabies
Copy link
Contributor Author

@briansmith , just committed requested changes

fn eq(&self, other: &Self) -> bool { self.id == other.id }
}

impl Eq for Algorithm {}
Copy link
Owner

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,
}
Copy link
Owner

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.

Copy link
Contributor Author

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.

@@ -50,6 +66,12 @@ enum ECDSAVerificationAlgorithmID {
ECDSA_P384_SHA384_FIXED,
}

impl PartialEq for ECDSAVerificationAlgorithm {
fn eq(&self, other: &Self) -> bool { self.id == other.id }
}
Copy link
Owner

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 VerificationAlgorithms, 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.

@rodentrabies rodentrabies force-pushed the algorithms-eq-implementation branch from 89bef08 to e62a72d Compare July 24, 2017 21:17
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
@rodentrabies rodentrabies force-pushed the algorithms-eq-implementation branch from e62a72d to 0fdb9b4 Compare July 24, 2017 21:19
@coveralls
Copy link

coveralls commented Jul 25, 2017

Coverage Status

Coverage decreased (-0.08%) to 95.596% when pulling 0fdb9b4 on mrwhythat:algorithms-eq-implementation into 5f8d332 on briansmith:master.

@rodentrabies
Copy link
Contributor Author

rodentrabies commented Aug 20, 2017

Any additional changes required?

@ctz
Copy link
Contributor

ctz commented Aug 20, 2017

This will let me delete some code that computes digest::Algorithm equality the dumb way 👍

@rodentrabies rodentrabies changed the title Implement PartialEq and Eq for agorithm types Implement PartialEq and Eq for algorithm types Aug 25, 2017
@briansmith
Copy link
Owner

This landed with modifications and was released in ring 0.12.1. The commits are:

d8134d6
eb668d5
b3cd49c

Thanks for submitting this and sorry for the long delay in getting it landed.

@ctz wrote:

This will let me delete some code that computes digest::Algorithm equality the dumb way 👍

I just noticed that, and just sent you a PR: rustls/rustls#117

@briansmith briansmith closed this Sep 4, 2017
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.

4 participants