From 9b4fa8bdc05ef20f4fadd7f1fb73a51cb41d1854 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 12 Jun 2025 13:24:52 +1000 Subject: [PATCH 1/2] Add RecoveryId getter The `RecoveryId` is an enum but is typically serialized as an integer. We used to provide a `to_i32` function before #743. Add a `to_u8` getter to the `RecoveryId` --- src/ecdsa/recovery.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ecdsa/recovery.rs b/src/ecdsa/recovery.rs index b1d013b9e..f3d61b87e 100644 --- a/src/ecdsa/recovery.rs +++ b/src/ecdsa/recovery.rs @@ -36,6 +36,16 @@ impl RecoveryId { _ => RecoveryId::Three, } } + + /// Returns the `RecoveryId` as an integer. + pub const fn to_u8(self) -> u8 { + match self { + RecoveryId::Zero => 0, + RecoveryId::One => 1, + RecoveryId::Two => 2, + RecoveryId::Three => 3, + } + } } impl TryFrom for RecoveryId { From fed623ef9803fe28f44e7cd331e25b9149a1154b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 12 Jun 2025 13:26:34 +1000 Subject: [PATCH 2/2] Implement From on u8 for RecoveryId instead of i32 The `i32` is a hangover from before #743, now we have a nice enum and a `from_u8_masked` constructor (along with a `to_u8` getter) lets make the `From` impl be on `u8` instead of on `i32`. --- examples/sign_verify_recovery.rs | 5 +---- src/ecdsa/recovery.rs | 10 +++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/sign_verify_recovery.rs b/examples/sign_verify_recovery.rs index a82aa98a8..984184def 100644 --- a/examples/sign_verify_recovery.rs +++ b/examples/sign_verify_recovery.rs @@ -47,8 +47,5 @@ fn main() { let (recovery_id, serialize_sig) = signature.serialize_compact(); - assert_eq!( - recover(&secp, msg, serialize_sig, Into::::into(recovery_id) as u8), - Ok(pubkey) - ); + assert_eq!(recover(&secp, msg, serialize_sig, recovery_id.to_u8()), Ok(pubkey)); } diff --git a/src/ecdsa/recovery.rs b/src/ecdsa/recovery.rs index f3d61b87e..4893b24e1 100644 --- a/src/ecdsa/recovery.rs +++ b/src/ecdsa/recovery.rs @@ -62,7 +62,7 @@ impl TryFrom for RecoveryId { } } -impl From for i32 { +impl From for u8 { #[inline] fn from(val: RecoveryId) -> Self { match val { @@ -96,7 +96,7 @@ impl RecoverableSignature { super_ffi::secp256k1_context_no_precomp, &mut ret, data.as_c_ptr(), - recid.into(), + i32::from(recid.to_u8()), ) == 1 { Ok(RecoverableSignature(ret)) @@ -123,7 +123,7 @@ impl RecoverableSignature { /// Serializes the recoverable signature in compact format. pub fn serialize_compact(&self) -> (RecoveryId, [u8; 64]) { let mut ret = [0u8; 64]; - let mut recid = RecoveryId::Zero.into(); + let mut recid = i32::from(RecoveryId::Zero.to_u8()); unsafe { let err = ffi::secp256k1_ecdsa_recoverable_signature_serialize_compact( super_ffi::secp256k1_context_no_precomp, @@ -461,9 +461,9 @@ mod tests { assert!(RecoveryId::try_from(3i32).is_ok()); assert!(RecoveryId::try_from(4i32).is_err()); let id0 = RecoveryId::Zero; - assert_eq!(Into::::into(id0), 0i32); + assert_eq!(Into::::into(id0), 0u8); let id1 = RecoveryId::One; - assert_eq!(Into::::into(id1), 1i32); + assert_eq!(Into::::into(id1), 1u8); } }