From 4c43d5e20fa7f0a195563539da3b33c0285ec998 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Tue, 8 Feb 2022 07:28:49 +0000 Subject: [PATCH] Add custom Debug impl for RecoverableSignature Currently when debug printing the `RecoverableSignature` we do so byte by byte, this means that the output differs depending on the endianess of the machine. If instead we serialize the signature in compact form then the output is the same irrespective of the endianess. With this applied the following two commands now pass: ``` cargo test test_debug_output --features=recovery ``` cross test --target powerpc-unknown-linux-gnu test_debug_output --features=recovery ``` Fixes: #375 --- secp256k1-sys/src/recovery.rs | 28 ++++++++++++++++++++++++++-- src/ecdsa/recovery.rs | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/secp256k1-sys/src/recovery.rs b/secp256k1-sys/src/recovery.rs index 52dde8406..793d96a62 100644 --- a/secp256k1-sys/src/recovery.rs +++ b/secp256k1-sys/src/recovery.rs @@ -16,13 +16,13 @@ //! # FFI of the recovery module use ::types::*; -use {Context, Signature, NonceFn, PublicKey}; +use ::core::fmt; +use {Context, Signature, NonceFn, PublicKey, CPtr}; /// Library-internal representation of a Secp256k1 signature + recovery ID #[repr(C)] pub struct RecoverableSignature([c_uchar; 65]); impl_array_newtype!(RecoverableSignature, c_uchar, 65); -impl_raw_debug!(RecoverableSignature); impl RecoverableSignature { /// Create a new (zeroed) signature usable for the FFI interface @@ -35,6 +35,30 @@ impl Default for RecoverableSignature { } } +impl fmt::Debug for RecoverableSignature { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut ret = [0u8; 64]; + let mut recid = 0i32; + + unsafe { + let err = secp256k1_ecdsa_recoverable_signature_serialize_compact( + super::secp256k1_context_no_precomp, + ret.as_mut_c_ptr(), + &mut recid, + self, + ); + assert!(err == 1); + } + + for byte in ret.iter() { + write!(f, "{:02x}", byte)?; + } + write!(f, "{:02x}", recid as u8)?; + + Ok(()) + } +} + extern "C" { #[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_4_1_ecdsa_recoverable_signature_parse_compact")] pub fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature, diff --git a/src/ecdsa/recovery.rs b/src/ecdsa/recovery.rs index ca367def9..a3c1c9064 100644 --- a/src/ecdsa/recovery.rs +++ b/src/ecdsa/recovery.rs @@ -337,7 +337,7 @@ mod tests { 0xff, 0x20, 0x80, 0xc4, 0xa3, 0x9a, 0xae, 0x06, 0x8d, 0x12, 0xee, 0xd0, 0x09, 0xb6, 0x8c, 0x89], RecoveryId(1)).unwrap(); - assert_eq!(&format!("{:?}", sig), "RecoverableSignature(98882e09f4ed6dc3659e43fc771e0cafa60b1f926f2b77041f744721adff7366898cb609d0ee128d06ae9aa3c48020ff9f705e02f80e1280a8ade05216971a4c01)"); + assert_eq!(&format!("{:?}", sig), "RecoverableSignature(6673ffad2147741f04772b6f921f0ba6af0c1e77fc439e65c36dedf4092e88984c1a971652e0ada880120ef8025e709fff2080c4a39aae068d12eed009b68c8901)"); } #[test]