Rename from_slice to from_bytes on CompressedPublicKey
What changed, and why it matters
This is a routine API cleanup, not a security fix. The developers renamed a function from from_slice to from_bytes on CompressedPublicKey so the name better matches that it now accepts a fixed 33-byte array. The old from_slice is kept but marked deprecated, and internally it now checks the slice length before delegating. No vulnerability is described or fixed.
No security action required. Treat as normal API maintenance. Users of CompressedPublicKey::from_slice should migrate to from_bytes when convenient, as the old method is deprecated.
Security signals we found
No security-relevant signals in commit title or message
No mention of vulnerability, CVE, bug, crash, or exploit
Change is an API rename/deprecation with stricter input typing
Length check in deprecated from_slice is a defensive hardening side effect, not a disclosed fix
Evidence from the diff
The commit refactors CompressedPublicKey deserialization. It adds from_bytes([u8; 33]), deprecates from_slice(&[u8]), and updates callers. The deprecated from_slice now converts the slice to a fixed-size array via try_into, returning secp256k1::Error::InvalidPublicKey on length mismatch, then calls from_bytes, which uses secp256k1::PublicKey::from_byte_array_compressed. This is a type-safety/API-consistency change; it does not alter secp256k1 validation logic or address a disclosed security defect.
Changed components
bitcoin/src/crypto/key.rsbitcoin/examples/sighash.rsInspect captured patch +18 / −5
diff --git a/bitcoin/examples/sighash.rs b/bitcoin/examples/sighash.rs
index 50788365..3088d159 100644
--- a/bitcoin/examples/sighash.rs
+++ b/bitcoin/examples/sighash.rs
@@ -36,7 +36,8 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
//BIP-0143: "The item 5 : For P2WPKH witness program, the scriptCode is 0x1976a914{20-byte-pubkey-hash}88ac"
//this is nothing but a standard P2PKH script OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG:
- let pk = CompressedPublicKey::from_slice(pk_bytes).expect("failed to parse pubkey");
+ let pk_byte_arr = pk_bytes.try_into().expect("there should be 33 bytes for a compressed key");
+ let pk = CompressedPublicKey::from_bytes(pk_byte_arr).expect("failed to parse pubkey");
let wpkh = pk.wpubkey_hash();
println!("Script pubkey hash: {wpkh:x}");
let spk = ScriptPubKeyBuf::new_p2wpkh(wpkh);
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index cd738f0f..5f874926 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -796,7 +796,7 @@ impl CompressedPublicKey {
reader.read_exact(&mut bytes)?;
#[allow(unused_variables)] // e when std not enabled
- Self::from_slice(&bytes).map_err(|e| {
+ Self::from_bytes(bytes).map_err(|e| {
// Need a static string for no-std io
#[cfg(feature = "std")]
let reason = e;
@@ -819,8 +819,19 @@ impl CompressedPublicKey {
/// # Errors
///
/// See [`secp256k1::PublicKey::from_slice`].
+ #[deprecated(since = "TBD", note = "use `from_bytes` instead; if you only have a slice, use `<&[u8; 33]>::try_from` first")]
pub fn from_slice(data: &[u8]) -> Result<Self, secp256k1::Error> {
- secp256k1::PublicKey::from_slice(data).map(Self::from_secp)
+ let bytes_arr = data.try_into().map_err(|_| secp256k1::Error::InvalidPublicKey)?;
+ Self::from_bytes(bytes_arr)
+ }
+
+ /// Deserializes a public key from compressed pubkey bytes.
+ ///
+ /// # Errors
+ ///
+ /// See [`secp256k1::PublicKey::from_byte_array_compressed`].
+ pub fn from_bytes(data: [u8; 33]) -> Result<Self, secp256k1::Error> {
+ secp256k1::PublicKey::from_byte_array_compressed(data).map(Self::from_secp)
}
/// Computes the public key as supposed to be used with this secret.
@@ -862,7 +873,7 @@ impl FromStr for CompressedPublicKey {
type Err = ParseCompressedPublicKeyError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
- Self::from_slice(&hex::decode_to_array::<33>(s)?).map_err(Into::into)
+ Self::from_bytes(hex::decode_to_array::<33>(s)?).map_err(Into::into)
}
}
@@ -1254,7 +1265,8 @@ impl<'de> serde::Deserialize<'de> for CompressedPublicKey {
where
E: serde::de::Error,
{
- CompressedPublicKey::from_slice(v).map_err(E::custom)
+ let arr = v.try_into().map_err(E::custom)?;
+ CompressedPublicKey::from_bytes(arr).map_err(E::custom)
}
}
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.