bitcoin: Replace gated no_std with blanket no_std
What changed, and why it matters
This is a routine code cleanup that makes the crate's no_std setup consistent with other crates in the same repository. It does not fix a security bug and does not change behavior for users.
No security action needed; treat as normal maintenance. Reviewers may verify that CI still passes for both std and no_std feature combinations.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit replaces a conditional #![cfg_attr(all(not(feature = “std”), not(test)), no_std)] with an unconditional #![no_std] and adds an explicit extern crate std gated behind the std feature. It also adjusts imports in test modules so std/alloc types are explicitly brought in. This is a build-system and import hygiene refactor with no functional changes to cryptographic, consensus, or network code.
Changed components
bitcoin/src/lib.rs build configurationbitcoin crate no_std/std feature plumbingtest module imports across bitcoin crateInspect captured patch +48 / −8
diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs
index 844c62a5..204e9eaa 100644
--- a/bitcoin/src/address/mod.rs
+++ b/bitcoin/src/address/mod.rs
@@ -1023,6 +1023,8 @@ fn segwit_redeem_hash(pubkey_hash: PubkeyHash) -> hash160::Hash {
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use hex_lit::hex;
use super::*;
diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs
index 0277048e..92c7789f 100644
--- a/bitcoin/src/bip158.rs
+++ b/bitcoin/src/bip158.rs
@@ -533,16 +533,21 @@ impl<'a, W: Write> BitStreamWriter<'a, W> {
#[cfg(test)]
mod test {
+ #[cfg(feature = "std")]
use std::collections::HashMap;
use hex_lit::hex;
+ #[cfg(feature = "std")]
use serde_json::Value;
use super::*;
+ #[cfg(feature = "std")]
use crate::consensus::encode::deserialize;
+ #[cfg(feature = "std")]
use crate::ScriptPubKeyBuf;
#[test]
+ #[cfg(feature = "std")]
fn blockfilters() {
let hex = |b| <Vec<u8> as hex_unstable::FromHex>::from_hex(b).unwrap();
diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs
index c39c6dbe..e336100d 100644
--- a/bitcoin/src/bip32.rs
+++ b/bitcoin/src/bip32.rs
@@ -1108,6 +1108,8 @@ impl Common {
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use hex_lit::hex;
#[cfg(feature = "serde")]
use internals::serde_round_trip;
diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs
index 7a6e1d4c..ab938f97 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -382,6 +382,8 @@ impl std::error::Error for ValidationError {
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use hex_lit::hex;
use internals::ToU64 as _;
use primitives::Wtxid;
diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs
index 92cc1784..5fec1961 100644
--- a/bitcoin/src/blockdata/constants.rs
+++ b/bitcoin/src/blockdata/constants.rs
@@ -284,6 +284,8 @@ impl ChainHash {
#[cfg(test)]
mod test {
+ use alloc::string::ToString;
+
use hex_lit::hex;
use super::*;
diff --git a/bitcoin/src/blockdata/opcodes.rs b/bitcoin/src/blockdata/opcodes.rs
index 764d37f8..d69c4790 100644
--- a/bitcoin/src/blockdata/opcodes.rs
+++ b/bitcoin/src/blockdata/opcodes.rs
@@ -611,10 +611,12 @@ impl Ordinary {
#[cfg(test)]
mod tests {
+ #[cfg(feature = "std")]
use std::collections::HashSet;
use super::*;
+ #[cfg(feature = "std")]
macro_rules! roundtrip {
($unique:expr, $op:ident) => {
assert_eq!($op, Opcode::from($op.to_u8()));
@@ -707,6 +709,7 @@ mod tests {
}
#[test]
+ #[cfg(feature = "std")]
fn str_roundtrip() {
let mut unique = HashSet::new();
roundtrip!(unique, OP_PUSHBYTES_0);
diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs
index 5864cfde..b644417e 100644
--- a/bitcoin/src/blockdata/script/tests.rs
+++ b/bitcoin/src/blockdata/script/tests.rs
@@ -1,5 +1,9 @@
// SPDX-License-Identifier: CC0-1.0
+#[cfg(feature = "serde")]
+use alloc::borrow::ToOwned;
+use alloc::string::ToString;
+
use hex_lit::hex;
use super::*;
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index ef025ae3..55ab2f73 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -1274,6 +1274,8 @@ impl<'a> Arbitrary<'a> for InputWeightPrediction {
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use hex_unstable::FromHex;
use hex_lit::hex;
@@ -1642,7 +1644,7 @@ mod tests {
}
#[test]
- #[cfg(feature = "bitcoinconsensus")]
+ #[cfg(all(feature = "std", feature = "bitcoinconsensus"))]
fn transaction_verify() {
use std::collections::HashMap;
diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs
index 66b34eaf..4798f1c6 100644
--- a/bitcoin/src/blockdata/witness.rs
+++ b/bitcoin/src/blockdata/witness.rs
@@ -237,6 +237,8 @@ mod sealed {
#[cfg(test)]
mod test {
+ use alloc::vec::Vec;
+
use hex_lit::hex;
use hex_unstable::DisplayHex;
diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs
index 2b202c71..b0c6bd62 100644
--- a/bitcoin/src/consensus/encode.rs
+++ b/bitcoin/src/consensus/encode.rs
@@ -695,6 +695,7 @@ impl Decodable for TapLeafHash {
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
use core::fmt;
use core::mem::discriminant;
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 872557d4..1aca9b95 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -1543,6 +1543,8 @@ impl std::error::Error for TweakXOnlyPublicKeyError {}
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use super::*;
use crate::address::Address;
diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs
index 58310706..fa388f73 100644
--- a/bitcoin/src/crypto/sighash.rs
+++ b/bitcoin/src/crypto/sighash.rs
@@ -1516,6 +1516,8 @@ impl<'a> Arbitrary<'a> for TapSighashType {
#[cfg(test)]
mod tests {
+ use alloc::{string::ToString, vec::Vec};
+
use hashes::HashEngine;
use hex_unstable::FromHex;
use hex_lit::hex;
diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs
index 00aac645..cf754dff 100644
--- a/bitcoin/src/hash_types.rs
+++ b/bitcoin/src/hash_types.rs
@@ -9,6 +9,8 @@ pub use crate::{BlockHash, TxMerkleNode, Txid, WitnessCommitment, WitnessMerkleN
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use super::*;
use crate::key::{PubkeyHash, WPubkeyHash};
use crate::script::{ScriptHash, WScriptHash};
diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs
index 55d85929..143cc3ce 100644
--- a/bitcoin/src/lib.rs
+++ b/bitcoin/src/lib.rs
@@ -22,7 +22,7 @@
//! * `secp-recovery` - enables calculating public key from a signature and message.
//! * `std` - the usual dependency on `std`.
-#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
+#![no_std]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_notable_trait))]
// Coding conventions.
@@ -55,6 +55,9 @@ internals::const_assert!(
#[macro_use]
extern crate alloc;
+#[cfg(feature = "std")]
+extern crate std;
+
/// Encodes and decodes base64 as bytes or utf8.
#[cfg(feature = "base64")]
pub extern crate base64;
@@ -202,19 +205,19 @@ pub use crate::{
#[rustfmt::skip]
#[allow(unused_imports)]
mod prelude {
- #[cfg(all(not(feature = "std"), not(test)))]
+ #[cfg(not(feature = "std"))]
pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, BorrowMut, Cow, ToOwned}, slice, rc};
- #[cfg(all(not(feature = "std"), not(test), target_has_atomic = "ptr"))]
+ #[cfg(all(not(feature = "std"), target_has_atomic = "ptr"))]
pub use alloc::sync;
- #[cfg(any(feature = "std", test))]
+ #[cfg(feature = "std")]
pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, BorrowMut, Cow, ToOwned}, rc, sync};
- #[cfg(all(not(feature = "std"), not(test)))]
+ #[cfg(not(feature = "std"))]
pub use alloc::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
- #[cfg(any(feature = "std", test))]
+ #[cfg(feature = "std")]
pub use std::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
pub use crate::io::sink;
diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs
index 95425542..d6d71122 100644
--- a/bitcoin/src/pow.rs
+++ b/bitcoin/src/pow.rs
@@ -2207,6 +2207,7 @@ mod tests {
macro_rules! check_from_str {
($ty:ident, $err_ty:ident, $mod_name:ident) => {
mod $mod_name {
+ use alloc::string::ToString;
use core::str::FromStr;
use super::{$err_ty, $ty, ParseU256Error, U256};
diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs
index 9b5a68d8..e2e222f5 100644
--- a/bitcoin/src/psbt/mod.rs
+++ b/bitcoin/src/psbt/mod.rs
@@ -1295,7 +1295,8 @@ pub use self::display_from_str::PsbtParseError;
#[cfg(test)]
mod tests {
- use std::str::FromStr;
+ use alloc::string::ToString;
+ use core::str::FromStr;
use hashes::{hash160, ripemd160, sha256};
use hex_unstable::FromHex;
diff --git a/bitcoin/src/sign_message.rs b/bitcoin/src/sign_message.rs
index a3ed532b..29263d08 100644
--- a/bitcoin/src/sign_message.rs
+++ b/bitcoin/src/sign_message.rs
@@ -218,6 +218,8 @@ pub fn sign(msg: impl AsRef<[u8]>, privkey: SecretKey) -> MessageSignature {
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use super::*;
#[test]
diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs
index 945f11c3..9220ea02 100644
--- a/bitcoin/src/taproot/mod.rs
+++ b/bitcoin/src/taproot/mod.rs
@@ -1653,6 +1653,8 @@ impl std::error::Error for InvalidControlBlockSizeError {}
#[cfg(test)]
mod test {
+ use alloc::string::ToString;
+
use hashes::sha256;
use hex_unstable::DisplayHex;
Why this scored 15/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.