Merge rust-bitcoin/rust-bitcoin#7127: key_expression: Reduce `alloc` gating to minimal types
What changed, and why it matters
This commit is a code cleanup and feature-gating refactor for the rust-bitcoin library's key_expression crate. It makes more BIP32 (Bitcoin wallet key derivation) types available when the 'alloc' memory allocator feature is disabled, and switches Xpriv/Xpub string parsing to use a no-alloc base58 decoder. There is no direct security vulnerability visible in the diff; it is primarily a maintainability and no-alloc compatibility improvement.
No immediate security action required. Reviewers should verify that the new no-alloc base58 encode/decode APIs correctly enforce the 78-byte payload length and checksum validation, and that the removed InvalidBase58PayloadLengthError does not hide useful diagnostic information. Confirm test coverage for malformed-length inputs remains equivalent.
Security signals we found
Refactor of error types and parsing paths for Xpriv/Xpub base58 decoding
Switch from unbounded base58 decode to fixed-size array decode (78 bytes)
Removal of explicit length-check error path in favor of decoder-reported length error
Feature-gating changes that expose more BIP32 functionality in no-alloc builds
Evidence from the diff
The merge refactors alloc feature gating in key_expression/src/bip32.rs and the impl_array_newtype macro. Previously the entire bip32 module was gated behind #[cfg(feature = “alloc”)]; now only types that truly require allocation (RelativeDerivationPath, AbsoluteDerivationPath, KeySource, serde impls, arbitrary impls, tests) are gated. Xpriv and Xpub Display/FromStr are ungated and switched from base58::decode_check (allocating Vec) to base58::decode_check_to_array::<78> and Base58CkString::encode, which are no-alloc APIs. The ParseXprivError and ParseXpubError variants are simplified by removing InvalidBase58PayloadLengthError and using DecodeCheckArrayError instead. The InvalidBase58PayloadLengthError type is deleted. The macro now accepts an optional trailing attribute to gate allocating methods like to_vec.
Changed components
key_expression/src/bip32.rskey_expression/src/lib.rsinclude/array_newtype.rsbase58 decoding/encoding paths for Xpriv and XpubParseXprivError / ParseXpubError typesInspect captured patch +60 / −82
### include/array_newtype.rs
@@ -139,9 +139,14 @@ macro_rules! impl_array_newtype_stringify {
pub(crate) use impl_array_newtype_stringify;
/// Implements standard array methods for a given wrapper type.
+///
+/// Crates with an optional `alloc` feature should pass the attribute that gates the allocating
+/// methods (e.g. `to_vec`) as a trailing argument:
+///
+/// `impl_array_newtype!(Foo, u8, 32, #[cfg(feature = "alloc")]);`
#[allow(unused_macros)]
macro_rules! impl_array_newtype {
- ($thing:ident, $ty:ty, $len:literal) => {
+ ($thing:ident, $ty:ty, $len:literal $(, #[$alloc_attr:meta])?) => {
impl $thing {
/// Constructs a new `Self` by wrapping `bytes`.
#[inline]
@@ -164,6 +169,7 @@ macro_rules! impl_array_newtype {
/// Copies the underlying bytes into a new `Vec`.
#[inline]
+ $(#[$alloc_attr])?
pub fn to_vec(self) -> alloc::vec::Vec<u8> { self.0.to_vec() }
/// Returns a slice of the underlying bytes.
### key_expression/src/bip32.rs
@@ -5,8 +5,11 @@
//! Implementation of BIP-0032 hierarchical deterministic wallets, as defined
//! at <https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki>.
+#[cfg(feature = "alloc")]
use alloc::vec;
+#[cfg(feature = "alloc")]
use alloc::vec::Vec;
+#[cfg(feature = "alloc")]
use core::ops::Index;
use core::str::FromStr;
use core::{fmt, slice};
@@ -22,10 +25,9 @@ use network::NetworkKind;
#[doc(no_inline)]
pub use self::error::{
CannotDeriveHardenedChildError, DeriveXpubError, IndexOutOfRangeError,
- InvalidBase58PayloadLengthError, InvalidSeedLengthError, InvalidXkeyPartsError,
- MaximumDepthExceededError, ParseAbsoluteDerivationPathError, ParseChildNumberError,
- ParseRelativeDerivationPathError, ParseXprivError, ParseXpubError, XkeyDecodeError,
- XprivDecodeError, XpubDecodeError
+ InvalidSeedLengthError, InvalidXkeyPartsError, MaximumDepthExceededError,
+ ParseAbsoluteDerivationPathError, ParseChildNumberError, ParseRelativeDerivationPathError,
+ ParseXprivError, ParseXpubError, XkeyDecodeError, XprivDecodeError, XpubDecodeError,
};
/// Version bytes for extended public keys on Bitcoin Mainnet.
@@ -48,7 +50,7 @@ pub type ExtendedPrivKey = Xpriv;
/// A chain code
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainCode([u8; 32]);
-impl_array_newtype!(ChainCode, u8, 32);
+impl_array_newtype!(ChainCode, u8, 32, #[cfg(feature = "alloc")]);
crate::impl_array_newtype_stringify!(ChainCode, 32);
impl ChainCode {
@@ -58,14 +60,15 @@ impl ChainCode {
/// Copies the underlying bytes into a new `Vec`.
#[inline]
+ #[cfg(feature = "alloc")]
#[deprecated(since = "TBD", note = "use to_vec instead")]
pub fn to_bytes(self) -> alloc::vec::Vec<u8> { self.to_vec() }
}
/// A fingerprint
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Fingerprint([u8; 4]);
-impl_array_newtype!(Fingerprint, u8, 4);
+impl_array_newtype!(Fingerprint, u8, 4, #[cfg(feature = "alloc")]);
crate::impl_array_newtype_stringify!(Fingerprint, 4);
hash_newtype! {
@@ -178,6 +181,7 @@ pub struct Xpriv {
/// Chain code
chain_code: ChainCode,
}
+#[cfg(feature = "alloc")]
#[cfg(feature = "serde")]
internals::serde_string_impl!(Xpriv, "a BIP-0032 extended private key");
@@ -213,6 +217,7 @@ pub struct Xpub {
/// Chain code
chain_code: ChainCode,
}
+#[cfg(feature = "alloc")]
#[cfg(feature = "serde")]
internals::serde_string_impl!(Xpub, "a BIP-0032 extended public key");
@@ -488,12 +493,15 @@ impl serde::Serialize for ChildNumber {
}
/// A relative BIP-0032 derivation path.
+#[cfg(feature = "alloc")]
#[derive(Default, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct RelativeDerivationPath(Vec<ChildNumber>);
+#[cfg(feature = "alloc")]
#[cfg(feature = "serde")]
internals::serde_string_impl!(RelativeDerivationPath, "a relative BIP-0032 derivation path");
+#[cfg(feature = "alloc")]
impl<I> Index<I> for RelativeDerivationPath
where
Vec<ChildNumber>: Index<I>,
@@ -504,18 +512,22 @@ where
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
}
+#[cfg(feature = "alloc")]
impl From<Vec<ChildNumber>> for RelativeDerivationPath {
fn from(numbers: Vec<ChildNumber>) -> Self { Self(numbers) }
}
+#[cfg(feature = "alloc")]
impl From<RelativeDerivationPath> for Vec<ChildNumber> {
fn from(path: RelativeDerivationPath) -> Self { path.0 }
}
+#[cfg(feature = "alloc")]
impl<'a> From<&'a [ChildNumber]> for RelativeDerivationPath {
fn from(numbers: &'a [ChildNumber]) -> Self { Self(numbers.to_vec()) }
}
+#[cfg(feature = "alloc")]
impl core::iter::FromIterator<ChildNumber> for RelativeDerivationPath {
fn from_iter<T>(iter: T) -> Self
where
@@ -525,17 +537,20 @@ impl core::iter::FromIterator<ChildNumber> for RelativeDerivationPath {
}
}
+#[cfg(feature = "alloc")]
#[allow(clippy::into_iter_without_iter)]
impl<'a> core::iter::IntoIterator for &'a RelativeDerivationPath {
type Item = &'a ChildNumber;
type IntoIter = slice::Iter<'a, ChildNumber>;
fn into_iter(self) -> Self::IntoIter { self.0.iter() }
}
+#[cfg(feature = "alloc")]
impl AsRef<[ChildNumber]> for RelativeDerivationPath {
fn as_ref(&self) -> &[ChildNumber] { &self.0 }
}
+#[cfg(feature = "alloc")]
impl FromStr for RelativeDerivationPath {
type Err = ParseRelativeDerivationPathError;
@@ -570,12 +585,15 @@ impl FromStr for RelativeDerivationPath {
///
/// [PR #2451]: https://github.com/rust-bitcoin/rust-bitcoin/pull/2451
/// [PR #2677]: https://github.com/rust-bitcoin/rust-bitcoin/pull/2677
+#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct AbsoluteDerivationPath(RelativeDerivationPath);
+#[cfg(feature = "alloc")]
#[cfg(feature = "serde")]
internals::serde_string_impl!(AbsoluteDerivationPath, "an absolute BIP-0032 derivation path");
+#[cfg(feature = "alloc")]
impl AbsoluteDerivationPath {
/// Returns the absolute derivation path for a master key.
pub fn master() -> Self { Self(RelativeDerivationPath::default()) }
@@ -603,10 +621,12 @@ impl AbsoluteDerivationPath {
pub fn join<T: AsRef<[ChildNumber]>>(&self, path: T) -> Self { Self(self.0.join(path)) }
}
+#[cfg(feature = "alloc")]
impl Default for AbsoluteDerivationPath {
fn default() -> Self { Self::master() }
}
+#[cfg(feature = "alloc")]
impl FromStr for AbsoluteDerivationPath {
type Err = ParseAbsoluteDerivationPathError;
@@ -632,6 +652,7 @@ impl FromStr for AbsoluteDerivationPath {
}
}
+#[cfg(feature = "alloc")]
impl fmt::Display for AbsoluteDerivationPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("m")?;
@@ -647,10 +668,12 @@ impl fmt::Display for AbsoluteDerivationPath {
}
}
+#[cfg(feature = "alloc")]
impl From<RelativeDerivationPath> for AbsoluteDerivationPath {
fn from(path: RelativeDerivationPath) -> Self { Self(path) }
}
+#[cfg(feature = "alloc")]
impl From<AbsoluteDerivationPath> for RelativeDerivationPath {
fn from(path: AbsoluteDerivationPath) -> Self { path.0 }
}
@@ -659,18 +682,21 @@ impl From<AbsoluteDerivationPath> for RelativeDerivationPath {
///
/// It is returned by the methods [`RelativeDerivationPath::children_from`],
/// [`RelativeDerivationPath::normal_children`] and [`RelativeDerivationPath::hardened_children`].
+#[cfg(feature = "alloc")]
pub struct RelativeDerivationPathIterator<'a> {
base: &'a RelativeDerivationPath,
next_child: Option<ChildNumber>,
}
+#[cfg(feature = "alloc")]
impl<'a> RelativeDerivationPathIterator<'a> {
/// Starts a new [`RelativeDerivationPathIterator`] at the given child.
pub fn start_from(path: &'a RelativeDerivationPath, start: ChildNumber) -> Self {
RelativeDerivationPathIterator { base: path, next_child: Some(start) }
}
}
+#[cfg(feature = "alloc")]
impl Iterator for RelativeDerivationPathIterator<'_> {
type Item = RelativeDerivationPath;
@@ -681,6 +707,7 @@ impl Iterator for RelativeDerivationPathIterator<'_> {
}
}
+#[cfg(feature = "alloc")]
impl RelativeDerivationPath {
/// Returns length of the derivation path
pub fn len(&self) -> usize { self.0.len() }
@@ -772,6 +799,7 @@ impl RelativeDerivationPath {
}
}
+#[cfg(feature = "alloc")]
impl fmt::Display for RelativeDerivationPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut iter = self.0.iter();
@@ -794,12 +822,14 @@ impl fmt::Display for RelativeDerivationPath {
}
}
+#[cfg(feature = "alloc")]
impl fmt::Debug for RelativeDerivationPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self, f) }
}
/// Full information on the used extended public key: fingerprint of the
/// master extended public key and a derivation path from it.
+#[cfg(feature = "alloc")]
pub type KeySource = (Fingerprint, RelativeDerivationPath);
impl Xpriv {
@@ -1232,41 +1262,35 @@ impl Xpub {
impl fmt::Display for Xpriv {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- base58::Base58CkString::encode_unbounded(&self.encode()).fmt(fmt)
+ base58::Base58CkString::encode(&self.encode())
+ .expect("78 bytes encode to less than the length limit")
+ .fmt(fmt)
}
}
impl FromStr for Xpriv {
type Err = ParseXprivError;
fn from_str(inp: &str) -> Result<Self, ParseXprivError> {
- let data = base58::decode_check(inp)?;
-
- if data.len() != 78 {
- return Err(InvalidBase58PayloadLengthError { length: data.len() })
- .map_err(ParseXprivError::InvalidBase58PayloadLength);
- }
+ let data = base58::decode_check_to_array::<78>(inp).map_err(ParseXprivError::Base58)?;
Self::decode(&data).map_err(ParseXprivError::Decode)
}
}
impl fmt::Display for Xpub {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- base58::Base58CkString::encode_unbounded(&self.encode()[..]).fmt(fmt)
+ base58::Base58CkString::encode(&self.encode())
+ .expect("78 bytes encode to less than the length limit")
+ .fmt(fmt)
}
}
impl FromStr for Xpub {
type Err = ParseXpubError;
fn from_str(inp: &str) -> Result<Self, ParseXpubError> {
- let data = base58::decode_check(inp)?;
-
- if data.len() != 78 {
- return Err(InvalidBase58PayloadLengthError { length: data.len() })
- .map_err(ParseXpubError::InvalidBase58PayloadLength);
- }
+ let data = base58::decode_check_to_array::<78>(inp).map_err(ParseXpubError::Base58)?;
Self::decode(&data).map_err(ParseXpubError::Decode)
}
@@ -1523,9 +1547,7 @@ pub mod error {
#[non_exhaustive]
pub enum ParseXprivError {
/// Base58 encoding error.
- Base58(base58::DecodeCheckError),
- /// Base58 decoded data was an invalid length.
- InvalidBase58PayloadLength(InvalidBase58PayloadLengthError),
+ Base58(base58::DecodeCheckArrayError),
/// Binary xpriv decode error.
Decode(XprivDecodeError),
}
@@ -1539,7 +1561,6 @@ pub mod error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Base58(ref e) => Some(e),
- Self::InvalidBase58PayloadLength(ref e) => Some(e),
Self::Decode(ref e) => Some(e),
}
}
@@ -1549,24 +1570,17 @@ pub mod error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Base58(ref e) => write_err!(f, "base58 encoding error"; e),
- Self::InvalidBase58PayloadLength(ref e) => write_err!(f, "base58 payload"; e),
Self::Decode(ref e) => e.fmt(f),
}
}
}
- impl From<base58::DecodeCheckError> for ParseXprivError {
- fn from(e: base58::DecodeCheckError) -> Self { Self::Base58(e) }
- }
-
/// Error parsing a base58check BIP-0032 xpub string.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseXpubError {
/// Base58 encoding error.
- Base58(base58::DecodeCheckError),
- /// Base58 decoded data was an invalid length.
- InvalidBase58PayloadLength(InvalidBase58PayloadLengthError),
+ Base58(base58::DecodeCheckArrayError),
/// Binary xpub decode error.
Decode(XpubDecodeError),
}
@@ -1580,7 +1594,6 @@ pub mod error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Base58(ref e) => Some(e),
- Self::InvalidBase58PayloadLength(ref e) => Some(e),
Self::Decode(ref e) => Some(e),
}
}
@@ -1590,16 +1603,11 @@ pub mod error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Base58(ref e) => write_err!(f, "base58 encoding error"; e),
- Self::InvalidBase58PayloadLength(ref e) => write_err!(f, "base58 payload"; e),
Self::Decode(ref e) => e.fmt(f),
}
}
}
- impl From<base58::DecodeCheckError> for ParseXpubError {
- fn from(e: base58::DecodeCheckError) -> Self { Self::Base58(e) }
- }
-
/// Attempted to derive a child of depth 256 or higher.
///
/// There is no way to encode such xkeys.
@@ -1818,40 +1826,6 @@ pub mod error {
}
}
- /// Decoded base58 data was an invalid length.
- #[derive(Debug, Clone, PartialEq, Eq)]
- pub struct InvalidBase58PayloadLengthError {
- /// The base58 payload length we got after decoding xpriv/xpub string.
- pub(crate) length: usize,
- }
-
- impl From<Infallible> for InvalidBase58PayloadLengthError {
- fn from(never: Infallible) -> Self { match never {} }
- }
-
- #[cfg(feature = "std")]
- impl std::error::Error for InvalidBase58PayloadLengthError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- let Self { length: _ } = self;
- None
- }
- }
-
- impl InvalidBase58PayloadLengthError {
- /// Returns the invalid payload length.
- pub fn invalid_base58_payload_length(&self) -> usize { self.length }
- }
-
- impl fmt::Display for InvalidBase58PayloadLengthError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(
- f,
- "decoded base58 xpriv/xpub data was an invalid length: {} (expected 78)",
- self.length
- )
- }
- }
-
/// Master seed had an invalid length.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidSeedLengthError {
@@ -1886,6 +1860,7 @@ pub mod error {
}
}
+#[cfg(feature = "alloc")]
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for RelativeDerivationPath {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
@@ -1894,6 +1869,7 @@ impl<'a> Arbitrary<'a> for RelativeDerivationPath {
}
}
+#[cfg(feature = "alloc")]
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for AbsoluteDerivationPath {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
@@ -1990,6 +1966,7 @@ impl<'a> Arbitrary<'a> for Xpriv {
}
}
+#[cfg(feature = "alloc")]
#[cfg(test)]
mod tests {
use alloc::format;
@@ -2638,15 +2615,13 @@ mod tests {
let encoded = base58::Base58CkString::encode(&vec![0u8; len]).unwrap();
match encoded.as_str().parse::<Xpriv>() {
- Err(ParseXprivError::InvalidBase58PayloadLength(e)) =>
- assert_eq!(e.invalid_base58_payload_length(), len),
- result => panic!("Expected InvalidBase58PayloadLength error, got {:?}", result),
+ Err(ParseXprivError::Base58(e)) => assert_eq!(e.invalid_length(), Some((len, 78))),
+ result => panic!("Expected Base58 invalid length error, got {:?}", result),
}
match encoded.as_str().parse::<Xpub>() {
- Err(ParseXpubError::InvalidBase58PayloadLength(e)) =>
- assert_eq!(e.invalid_base58_payload_length(), len),
- result => panic!("Expected InvalidBase58PayloadLength error, got {:?}", result),
+ Err(ParseXpubError::Base58(e)) => assert_eq!(e.invalid_length(), Some((len, 78))),
+ result => panic!("Expected Base58 invalid length error, got {:?}", result),
}
}
}
### key_expression/src/lib.rs
@@ -30,11 +30,8 @@ extern crate serde;
// Pull in shared macros from include
// The impl_array_newtype_stringify requires crate::serde, $crate::hex and
// crate::hashes to exist.
-#[cfg(feature = "alloc")]
include!("../include/array_newtype.rs");
-#[cfg(feature = "alloc")]
include!("../include/newtype.rs");
-#[cfg(feature = "alloc")]
pub mod bip32;Why this scored 19/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.