From 252053c3d63788d40226a7b3a2c95b487f1b2ac5 Mon Sep 17 00:00:00 2001
From: "Jamil Lambert, PhD" <Jamil.Lambert@proton.me>
Date: Mon, 27 Jan 2025 20:42:33 +0000
Subject: [PATCH] Add `from_u8_masked` `RecoveryId` constructor

Add an infallible constructor that takes a u8 and masks off the top 6
bits.
---
 src/ecdsa/recovery.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/ecdsa/recovery.rs b/src/ecdsa/recovery.rs
index 04aaf3690..6a2ebd49f 100644
--- a/src/ecdsa/recovery.rs
+++ b/src/ecdsa/recovery.rs
@@ -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]