From 1e0f3b570ee989c80d97e3dd796b3075a05133c7 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 14 Jun 2021 12:17:33 -0700 Subject: [PATCH] k256: impl Eq/PartialEq/ConstantTimeEq for SigningKey The `Eq`/`PartialEq` impls use `ConstantTimeEq` internally. This is useful for writing tests that need to compare keys. --- Cargo.lock | 4 ++-- k256/Cargo.toml | 2 +- k256/src/ecdsa/sign.rs | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dea0d907..6329acd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -323,9 +323,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "elliptic-curve" -version = "0.10.1" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4b6eb4853ca589eb02b325b0cff21e560180907a3640f8bfc8f5969ddd0c6ee" +checksum = "59029dd05f60215bbe37eda4b32ba1a142abc8b01a938955b20b92ff0d713e8e" dependencies = [ "base64ct", "crypto-bigint", diff --git a/k256/Cargo.toml b/k256/Cargo.toml index e8f09fa7..bb28cdfa 100644 --- a/k256/Cargo.toml +++ b/k256/Cargo.toml @@ -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 } diff --git a/k256/src/ecdsa/sign.rs b/k256/src/ecdsa/sign.rs index 479656aa..d969ed04 100644 --- a/k256/src/ecdsa/sign.rs +++ b/k256/src/ecdsa/sign.rs @@ -18,6 +18,7 @@ use elliptic_curve::{ consts::U32, ops::Invert, rand_core::{CryptoRng, RngCore}, + subtle::{Choice, ConstantTimeEq}, }; #[cfg(any(feature = "keccak256", feature = "sha256"))] @@ -189,6 +190,12 @@ impl RecoverableSignPrimitive 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 @@ -196,6 +203,14 @@ impl Debug for SigningKey { } } +impl Eq for SigningKey {} + +impl PartialEq for SigningKey { + fn eq(&self, other: &SigningKey) -> bool { + self.ct_eq(other).into() + } +} + impl From for SigningKey { fn from(secret_key: SecretKey) -> SigningKey { Self::from(&secret_key)