From d1d4ff9f01695526c01198dee643d273f0c32b9d Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Fri, 14 Feb 2025 21:55:44 +0100 Subject: [PATCH 1/2] doc: Keypair constructors accept odd keys Clarify the conditions for returning the InvalidSecretKey error. Fixes #758 --- src/key.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/key.rs b/src/key.rs index 779436f51..f050dc797 100644 --- a/src/key.rs +++ b/src/key.rs @@ -858,8 +858,8 @@ impl Keypair { /// /// # Errors /// - /// [`Error::InvalidSecretKey`] if the provided data has an incorrect length, exceeds Secp256k1 - /// field `p` value or the corresponding public key is not even. + /// [`Error::InvalidSecretKey`] if the slice is not exactly 32 bytes long, + /// or if the encoded number exceeds the Secp256k1 field `p` value. #[inline] pub fn from_seckey_slice( secp: &Secp256k1, @@ -883,7 +883,8 @@ impl Keypair { /// /// # Errors /// - /// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even. + /// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters, + /// or if the encoded number exceeds the Secp256k1 field `p` value. #[inline] pub fn from_seckey_str(secp: &Secp256k1, s: &str) -> Result { let mut res = [0u8; constants::SECRET_KEY_SIZE]; @@ -898,7 +899,8 @@ impl Keypair { /// /// # Errors /// - /// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even. + /// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters, + /// or if the encoded number exceeds the Secp256k1 field `p` value. #[inline] #[cfg(feature = "global-context")] pub fn from_seckey_str_global(s: &str) -> Result { From aaf5fee92e4200bb42c72cd67dd42b3dde07fb12 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Fri, 14 Feb 2025 21:57:35 +0100 Subject: [PATCH 2/2] fix: Invalid secret key The constructor of a secret key should return InvalidSecretKey in case of error. I guess there was a typo / copy-and-paste error that was never tested. --- src/key.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/key.rs b/src/key.rs index f050dc797..308b93ada 100644 --- a/src/key.rs +++ b/src/key.rs @@ -891,7 +891,7 @@ impl Keypair { match from_hex(s, &mut res) { Ok(constants::SECRET_KEY_SIZE) => Keypair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE]), - _ => Err(Error::InvalidPublicKey), + _ => Err(Error::InvalidSecretKey), } }