Skip to content

Commit

Permalink
Merge #778: Add from_u8_masked RecoveryId constructor
Browse files Browse the repository at this point in the history
252053c Add `from_u8_masked` `RecoveryId` constructor (Jamil Lambert, PhD)

Pull request description:

  Add an infallible constructor that takes a u8 and masks off the top 6 bits.

  Mentioned in rust-bitcoin/rust-bitcoin#3965

ACKs for top commit:
  tcharding:
    ACK 252053c
  Kixunil:
    ACK 252053c
  apoelstra:
    ACK 252053c; successfully ran local tests

Tree-SHA512: 698b5289358e39c9cf3137d5291dc08de9bc65c0d36aaad839b9e00e1414a72901c02dd101354422019f9fe396c8c7c4e28465ba38700e67a790c9fa29bd682b
  • Loading branch information
apoelstra committed Jan 28, 2025
2 parents b7e8ba7 + 252053c commit e66bd11
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ecdsa/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ pub enum RecoveryId {
Three,
}

impl RecoveryId {
/// Creates a `RecoveryId` from a `u8` value by masking off the top 6 bits.
#[inline]
pub const fn from_u8_masked(id: u8) -> RecoveryId {
match id & 0x03 {
0 => RecoveryId::Zero,
1 => RecoveryId::One,
2 => RecoveryId::Two,
_ => RecoveryId::Three,
}
}
}

impl TryFrom<i32> for RecoveryId {
type Error = Error;
#[inline]
Expand Down

0 comments on commit e66bd11

Please sign in to comment.