Skip to content

Commit 4c43d5e

Browse files
committed
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
1 parent ecb6261 commit 4c43d5e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

secp256k1-sys/src/recovery.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
//! # FFI of the recovery module
1717
1818
use ::types::*;
19-
use {Context, Signature, NonceFn, PublicKey};
19+
use ::core::fmt;
20+
use {Context, Signature, NonceFn, PublicKey, CPtr};
2021

2122
/// Library-internal representation of a Secp256k1 signature + recovery ID
2223
#[repr(C)]
2324
pub struct RecoverableSignature([c_uchar; 65]);
2425
impl_array_newtype!(RecoverableSignature, c_uchar, 65);
25-
impl_raw_debug!(RecoverableSignature);
2626

2727
impl RecoverableSignature {
2828
/// Create a new (zeroed) signature usable for the FFI interface
@@ -35,6 +35,30 @@ impl Default for RecoverableSignature {
3535
}
3636
}
3737

38+
impl fmt::Debug for RecoverableSignature {
39+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40+
let mut ret = [0u8; 64];
41+
let mut recid = 0i32;
42+
43+
unsafe {
44+
let err = secp256k1_ecdsa_recoverable_signature_serialize_compact(
45+
super::secp256k1_context_no_precomp,
46+
ret.as_mut_c_ptr(),
47+
&mut recid,
48+
self,
49+
);
50+
assert!(err == 1);
51+
}
52+
53+
for byte in ret.iter() {
54+
write!(f, "{:02x}", byte)?;
55+
}
56+
write!(f, "{:02x}", recid as u8)?;
57+
58+
Ok(())
59+
}
60+
}
61+
3862
extern "C" {
3963
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_4_1_ecdsa_recoverable_signature_parse_compact")]
4064
pub fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature,

src/ecdsa/recovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ mod tests {
337337
0xff, 0x20, 0x80, 0xc4, 0xa3, 0x9a, 0xae, 0x06,
338338
0x8d, 0x12, 0xee, 0xd0, 0x09, 0xb6, 0x8c, 0x89],
339339
RecoveryId(1)).unwrap();
340-
assert_eq!(&format!("{:?}", sig), "RecoverableSignature(98882e09f4ed6dc3659e43fc771e0cafa60b1f926f2b77041f744721adff7366898cb609d0ee128d06ae9aa3c48020ff9f705e02f80e1280a8ade05216971a4c01)");
340+
assert_eq!(&format!("{:?}", sig), "RecoverableSignature(6673ffad2147741f04772b6f921f0ba6af0c1e77fc439e65c36dedf4092e88984c1a971652e0ada880120ef8025e709fff2080c4a39aae068d12eed009b68c8901)");
341341
}
342342

343343
#[test]

0 commit comments

Comments
 (0)