Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedSecret, Error> {
Expand Down
43 changes: 19 additions & 24 deletions src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
"<secret key; enable `hashes` feature of `secp256k1` to display fingerprint>"
)
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()
}
}
};
Expand Down
Loading