Skip to content

Commit fb188dd

Browse files
committed
Merge #726: key: don't use Hasher to generate fingerprints; just use hashes crate
b8ac971 keypair: use public key for Debug output (Andrew Poelstra) a16e5ec secret keys: debug output only when `hashes` is enabled (Andrew Poelstra) Pull request description: In addition to changing `SecretKey` and `SharedSecret` to use `hashes`, we also unconditionally use the public half of `KeyPair` as a fingerprint, since that's always available and does not need extra deps. This patches the existing unit tests but doesn't add more. Maybe they should be removed; it's a bit weird to have unit tests for `Debug` output. But in this case we're doing some nontrivial logic and I guess we wanted to double-check that it was taking effect. I'd also like to change the manual tagged-hash implementation to use `bitcoin_hashes` methods but those are under construction rust-bitcoin/rust-bitcoin#3184 and the existing stuff is neither faster nor less code than what's currently done. So we'll live with it. Fixes #725 ACKs for top commit: Kixunil: ACK b8ac971 Tree-SHA512: d0a65e0a0069bcbc663c1d3e7f98b75868355c4db48e9a9c905cdcd2af1606ac86090cdf0aae5caa23337c5d565e6420d7c956dd0a65a1877004840075bc08e9
2 parents 5d2149f + b8ac971 commit fb188dd

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/key.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
772772
/// [`cbor`]: https://docs.rs/cbor
773773
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
774774
pub struct Keypair(ffi::Keypair);
775-
impl_display_secret!(Keypair);
776775
impl_fast_comparisons!(Keypair);
777776

778777
impl Keypair {
@@ -972,6 +971,15 @@ impl Keypair {
972971
pub fn non_secure_erase(&mut self) { self.0.non_secure_erase(); }
973972
}
974973

974+
impl fmt::Debug for Keypair {
975+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
976+
f.debug_struct("Keypair")
977+
.field("pubkey", &self.public_key())
978+
.field("secret", &"<hidden>")
979+
.finish()
980+
}
981+
}
982+
975983
impl From<Keypair> for SecretKey {
976984
#[inline]
977985
fn from(pair: Keypair) -> Self { SecretKey::from_keypair(&pair) }
@@ -1705,12 +1713,15 @@ mod test {
17051713
}
17061714

17071715
#[test]
1708-
#[cfg(all(feature = "rand", feature = "alloc"))]
1716+
#[cfg(all(feature = "rand", feature = "alloc", not(feature = "hashes")))]
17091717
fn test_debug_output() {
17101718
let s = Secp256k1::new();
17111719
let (sk, _) = s.generate_keypair(&mut StepRng::new(1, 1));
17121720

1713-
assert_eq!(&format!("{:?}", sk), "SecretKey(#d3e0c51a23169bb5)");
1721+
assert_eq!(
1722+
&format!("{:?}", sk),
1723+
"<secret key; enable `hashes` feature of `secp256k1` to display fingerprint>"
1724+
);
17141725

17151726
let mut buf = [0u8; constants::SECRET_KEY_SIZE * 2];
17161727
assert_eq!(

src/secret.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,7 @@ use crate::to_hex;
1111
macro_rules! impl_display_secret {
1212
// Default hasher exists only in standard library and not alloc
1313
($thing:ident) => {
14-
#[cfg(feature = "std")]
15-
impl core::fmt::Debug for $thing {
16-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
17-
use core::hash::Hasher;
18-
const DEBUG_HASH_TAG: &[u8] = &[
19-
0x66, 0xa6, 0x77, 0x1b, 0x9b, 0x6d, 0xae, 0xa1, 0xb2, 0xee, 0x4e, 0x07, 0x49,
20-
0x4a, 0xac, 0x87, 0xa9, 0xb8, 0x5b, 0x4b, 0x35, 0x02, 0xaa, 0x6d, 0x0f, 0x79,
21-
0xcb, 0x63, 0xe6, 0xf8, 0x66, 0x22,
22-
]; // =SHA256(b"rust-secp256k1DEBUG");
23-
24-
let mut hasher = std::collections::hash_map::DefaultHasher::new();
25-
26-
hasher.write(DEBUG_HASH_TAG);
27-
hasher.write(DEBUG_HASH_TAG);
28-
hasher.write(&self.secret_bytes());
29-
let hash = hasher.finish();
30-
31-
f.debug_tuple(stringify!($thing)).field(&format_args!("#{:016x}", hash)).finish()
32-
}
33-
}
34-
35-
#[cfg(all(not(feature = "std"), feature = "hashes"))]
14+
#[cfg(feature = "hashes")]
3615
impl ::core::fmt::Debug for $thing {
3716
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3817
use hashes::{sha256, Hash, HashEngine};
@@ -50,10 +29,13 @@ macro_rules! impl_display_secret {
5029
}
5130
}
5231

53-
#[cfg(all(not(feature = "std"), not(feature = "hashes")))]
32+
#[cfg(not(feature = "hashes"))]
5433
impl ::core::fmt::Debug for $thing {
5534
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
56-
write!(f, "<secret requires std or hashes feature to display>")
35+
write!(
36+
f,
37+
"<secret key; enable `hashes` feature of `secp256k1` to display fingerprint>"
38+
)
5739
}
5840
}
5941
};

0 commit comments

Comments
 (0)