Skip to content

Commit

Permalink
k256: impl Eq/PartialEq/ConstantTimeEq for SigningKey (#359)
Browse files Browse the repository at this point in the history
The `Eq`/`PartialEq` impls use `ConstantTimeEq` internally.

This is useful for writing tests that need to compare keys.
  • Loading branch information
tarcieri authored Jun 14, 2021
1 parent 08a3f6f commit 4fa72c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ keywords = ["bitcoin", "crypto", "ecc", "ethereum", "secp256k1"]

[dependencies]
cfg-if = "1.0"
elliptic-curve = { version = "0.10", default-features = false, features = ["hazmat"] }
elliptic-curve = { version = "0.10.2", default-features = false, features = ["hazmat"] }

# optional dependencies
hex-literal = { version = "0.3", optional = true }
Expand Down
15 changes: 15 additions & 0 deletions k256/src/ecdsa/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use elliptic_curve::{
consts::U32,
ops::Invert,
rand_core::{CryptoRng, RngCore},
subtle::{Choice, ConstantTimeEq},
};

#[cfg(any(feature = "keccak256", feature = "sha256"))]
Expand Down Expand Up @@ -189,13 +190,27 @@ impl RecoverableSignPrimitive<Secp256k1> for Scalar {
}
}

impl ConstantTimeEq for SigningKey {
fn ct_eq(&self, other: &Self) -> Choice {
self.inner.ct_eq(&other.inner)
}
}

impl Debug for SigningKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO(tarcieri): use `finish_non_exhaustive` when stable
f.debug_tuple("SigningKey").field(&"...").finish()
}
}

impl Eq for SigningKey {}

impl PartialEq for SigningKey {
fn eq(&self, other: &SigningKey) -> bool {
self.ct_eq(other).into()
}
}

impl From<SecretKey> for SigningKey {
fn from(secret_key: SecretKey) -> SigningKey {
Self::from(&secret_key)
Expand Down

0 comments on commit 4fa72c0

Please sign in to comment.