diff --git a/src/ecdh.rs b/src/ecdh.rs index 4fd90b788..8594dff8b 100644 --- a/src/ecdh.rs +++ b/src/ecdh.rs @@ -78,6 +78,20 @@ impl SharedSecret { } } +impl ffi::CPtr for SharedSecret { + type Target = u8; + + fn as_c_ptr(&self) -> *const Self::Target { + let SharedSecret(dat) = self; + dat.as_ptr() + } + + fn as_mut_c_ptr(&mut self) -> *mut Self::Target { + let &mut SharedSecret(ref mut dat) = self; + dat.as_mut_ptr() + } +} + impl str::FromStr for SharedSecret { type Err = Error; fn from_str(s: &str) -> Result { diff --git a/src/secret.rs b/src/secret.rs index f5fc1060d..02ff9c25d 100644 --- a/src/secret.rs +++ b/src/secret.rs @@ -8,34 +8,29 @@ use crate::constants::SECRET_KEY_SIZE; use crate::ecdh::SharedSecret; use crate::key::{Keypair, SecretKey}; use crate::to_hex; + macro_rules! impl_display_secret { - // Default hasher exists only in standard library and not alloc ($thing:ident) => { - #[cfg(feature = "hashes")] impl ::core::fmt::Debug for $thing { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use hashes::{sha256, Hash, HashEngine}; - - let tag = "rust-secp256k1DEBUG"; + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + use crate::ffi; + use secp256k1_sys::CPtr; - let mut engine = sha256::Hash::engine(); - let tag_hash = sha256::Hash::hash(tag.as_bytes()); - engine.input(&tag_hash.as_ref()); - engine.input(&tag_hash.as_ref()); - engine.input(&self.secret_bytes()); - let hash = sha256::Hash::from_engine(engine); - - f.debug_tuple(stringify!($thing)).field(&format_args!("#{:.16}", hash)).finish() - } - } - - #[cfg(not(feature = "hashes"))] - impl ::core::fmt::Debug for $thing { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!( - f, - "" - ) + let mut finger_print = [0; 32]; + let msg = b"rust-secp256k1-DEBUG-MESSAGE-000"; + unsafe { + let nonce_fn= ffi::secp256k1_nonce_function_rfc6979.expect("nonce fn"); + let ret = nonce_fn( + finger_print.as_mut_c_ptr(), + msg.as_c_ptr(), + self.as_c_ptr(), + core::ptr::null(), + core::ptr::null_mut(), + 0 + ); + debug_assert_eq!(ret, 1) + } + f.debug_tuple(stringify!($thing)).field(&format_args!("{:?}", finger_print)).finish() } } };