Skip to content

Commit

Permalink
Merge #393: Add custom Debug impl for RecoverableSignature
Browse files Browse the repository at this point in the history
4c43d5e Add custom Debug impl for RecoverableSignature (Tobin Harding)

Pull request description:

  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

ACKs for top commit:
  apoelstra:
    ACK 4c43d5e

Tree-SHA512: 073c2e0e23ce41a2b35f1b1193b07a755b726bf565d61e6bcb23b6bdaab31ba3591f31aa92230b07f7dfc018de0401eba09a6858dc261e66dacb331355f40d76
  • Loading branch information
apoelstra committed Feb 9, 2022
2 parents b8615e2 + 4c43d5e commit bc278fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 26 additions & 2 deletions secp256k1-sys/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/ecdsa/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,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]
Expand Down

0 comments on commit bc278fa

Please sign in to comment.