Skip to content

Commit

Permalink
no_std support
Browse files Browse the repository at this point in the history
Based on the original work by Justin Moon.

*MSRV unchanged from 1.29.0.*

When `std` is off, `no-std` must be on, and we use the [`alloc`](https://doc.rust-lang.org/alloc/) and core2 crates. The `alloc` crate requires the user define a global allocator.

* Import from `core` and `alloc` instead of `std`
* `alloc` only used if `no-std` is on
* Create `std` feature
* Create `no-std` feature which adds a core2 dependency to polyfill `std::io` features. This is an experimental feature and should be
used with caution.
* CI runs tests `no-std`
* MSRV for `no-std` is 1.51 or so
  • Loading branch information
devrandom committed Jul 6, 2021
1 parent abff973 commit df023b5
Show file tree
Hide file tree
Showing 42 changed files with 429 additions and 185 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ jobs:
env:
DO_COV: true
AS_DEPENDENCY: true
DO_NO_STD: true
- rust: beta
env:
AS_DEPENDENCY: true
DO_NO_STD: true
- rust: nightly
env:
DO_FUZZ: true
DO_BENCH: true
AS_DEPENDENCY: true
DO_NO_STD: true
- rust: 1.29.0
env:
AS_DEPENDENCY: true
Expand Down
21 changes: 17 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"


[features]
default = [ "secp-recovery" ]
default = [ "std", "secp-recovery" ]
base64 = [ "base64-compat" ]
fuzztarget = []
unstable = []
Expand All @@ -21,14 +21,20 @@ use-serde = ["serde", "bitcoin_hashes/serde", "secp256k1/serde"]
secp-lowmemory = ["secp256k1/lowmemory"]
secp-recovery = ["secp256k1/recovery"]

# either std or no-std must be selected
std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std"]
no-std = ["hashbrown", "core2/alloc", "bitcoin_hashes/alloc"]

[dependencies]
bech32 = "0.8.0"
bitcoin_hashes = "0.9.6"
secp256k1 = "0.20.2"
bech32 = { version = "0.8.1", default-features = false }
bitcoin_hashes = { version = "0.10.0", default-features = false }
secp256k1 = { version = "0.20.2", default-features = false }
core2 = { version = "0.3.0", optional = true, default-features = false }

base64-compat = { version = "1.0.0", optional = true }
bitcoinconsensus = { version = "0.19.0-3", optional = true }
serde = { version = "1", features = [ "derive" ], optional = true }
hashbrown = { version = "0.8", optional = true }

[dev-dependencies]
serde_json = "<1.0.45"
Expand All @@ -37,3 +43,10 @@ secp256k1 = { version = "0.20.0", features = [ "recovery", "rand-std" ] }
bincode = "1.3.1"
# We need to pin ryu (transitive dep from serde_json) to stay compatible with Rust 1.22.0
ryu = "<1.0.5"

[[example]]
name = "bip32"

[[example]]
name = "handshake"
required-features = ["std"]
49 changes: 39 additions & 10 deletions contrib/test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/bin/sh -ex

FEATURES="base64 bitcoinconsensus use-serde rand"
FEATURES="base64 bitcoinconsensus use-serde rand secp-recovery"

# Use toolchain if explicitly specified
if [ -n "$TOOLCHAIN" ]
then
alias cargo="cargo +$TOOLCHAIN"
fi

pin_common_verions() {
cargo generate-lockfile --verbose
Expand All @@ -10,7 +16,7 @@ pin_common_verions() {
}

# Pin `cc` for Rust 1.29
if [ -n "$PIN_VERSIONS" ]; then
if [ "$PIN_VERSIONS" = true ]; then
pin_common_verions
cargo update -p byteorder --precise "1.3.4"
fi
Expand All @@ -21,20 +27,43 @@ then
fi


# Use toolchain if explicitly specified
if [ -n "$TOOLCHAIN" ]
then
alias cargo="cargo +$TOOLCHAIN"
fi
echo "********* Testing std *************"
# Test without any features first. std is required for tests
cargo test --verbose --no-default-features --features="std"

# Test without any features first
cargo test --verbose --no-default-features
echo "********* Testing default *************"
# Then test with the default features
cargo test --verbose

if [ "$DO_NO_STD" = true ]
then
echo "********* Testing no-std build *************"
# Build no_std, to make sure that cfg(test) doesn't hide any issues
cargo build --verbose --features="no-std" --no-default-features

# Build std + no_std, to make sure they are not incompatible
cargo build --verbose --features="no-std"

# Test no_std
cargo test --verbose --features="no-std" --no-default-features

# Build all features
cargo build --verbose --features="no-std $FEATURES" --no-default-features

# Build specific features
for feature in ${FEATURES}
do
cargo build --verbose --features="no-std $feature"
done

cargo run --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
cargo run --no-default-features --features no-std --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
fi

# Test each feature
for feature in ${FEATURES}
do
echo "********* Testing "$feature" *************"
cargo test --verbose --features="$feature"
done

Expand All @@ -55,7 +84,7 @@ then
fi

# Use as dependency if told to
if [ -n "$AS_DEPENDENCY" ]
if [ "$AS_DEPENDENCY" = true ]
then
cargo new dep_test
cd dep_test
Expand Down
5 changes: 4 additions & 1 deletion examples/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bitcoin::util::bip32::ExtendedPubKey;
use bitcoin::util::bip32::DerivationPath;
use bitcoin::util::bip32::ChildNumber;
use bitcoin::util::address::Address;
use bitcoin::secp256k1::ffi::types::AlignedType;

fn main() {
// This example derives root xprv
Expand All @@ -36,7 +37,9 @@ fn main() {
let seed = wif.to_bytes();

// we need secp256k1 context for key derivation
let secp = Secp256k1::new();
let mut buf: Vec<AlignedType> = Vec::new();
buf.resize(Secp256k1::preallocate_size(), AlignedType::zeroed());
let secp = Secp256k1::preallocated_new(buf.as_mut_slice()).unwrap();

// calculate root key from seed
let root = ExtendedPrivKey::new_master(network, &seed).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
//! these blocks and the blockchain.
//!

use prelude::*;

use core::fmt;

use util;
Expand Down Expand Up @@ -308,6 +310,7 @@ impl fmt::Display for Bip34Error {
}
}

#[cfg(feature = "std")]
impl ::std::error::Error for Bip34Error {}

#[cfg(test)]
Expand Down
9 changes: 7 additions & 2 deletions src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
//! single transaction
//!

use prelude::*;

use core::default::Default;

use hashes::hex::FromHex;
use hashes::hex::{HexIterator, Error as HexError};
use hashes::sha256d;
use blockdata::opcodes;
use blockdata::script;
Expand Down Expand Up @@ -84,8 +86,11 @@ fn bitcoin_genesis_tx() -> Transaction {
});

// Outputs
let script_bytes: Result<Vec<u8>, HexError> =
HexIterator::new("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").unwrap()
.collect();
let out_script = script::Builder::new()
.push_slice(&Vec::from_hex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").unwrap())
.push_slice(script_bytes.unwrap().as_slice())
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
ret.output.push(TxOut {
Expand Down
2 changes: 2 additions & 0 deletions src/blockdata/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#[cfg(feature = "serde")] use serde;

#[cfg(feature = "serde")] use prelude::*;

use core::{fmt, convert::From};

// Note: I am deliberately not implementing PartialOrd or Ord on the
Expand Down
12 changes: 7 additions & 5 deletions src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
//! This module provides the structures and functions needed to support scripts.
//!

use io;
use prelude::*;

use io;
use core::{fmt, default::Default};

#[cfg(feature = "serde")] use serde;
Expand All @@ -36,7 +37,7 @@ use consensus::{encode, Decodable, Encodable};
use hashes::{Hash, hex};
use policy::DUST_RELAY_TX_FEE;
#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use std::convert;
#[cfg(feature="bitcoinconsensus")] use core::convert::From;
#[cfg(feature="bitcoinconsensus")] use OutPoint;

use util::ecdsa::PublicKey;
Expand Down Expand Up @@ -145,11 +146,12 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl ::std::error::Error for Error {}

#[cfg(feature="bitcoinconsensus")]
#[doc(hidden)]
impl convert::From<bitcoinconsensus::Error> for Error {
impl From<bitcoinconsensus::Error> for Error {
fn from(err: bitcoinconsensus::Error) -> Error {
match err {
_ => Error::BitcoinConsensus(err)
Expand Down Expand Up @@ -421,11 +423,11 @@ impl Script {
} else if self.is_witness_program() {
32 + 4 + 1 + (107 / 4) + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut ::std::io::sink()).unwrap() as u64 // The serialized size of this script_pubkey
self.consensus_encode(&mut sink()).unwrap() as u64 // The serialized size of this script_pubkey
} else {
32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut ::std::io::sink()).unwrap() as u64 // The serialized size of this script_pubkey
self.consensus_encode(&mut sink()).unwrap() as u64 // The serialized size of this script_pubkey
};

::Amount::from_sat(sats)
Expand Down
11 changes: 8 additions & 3 deletions src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
//! This module provides the structures and functions needed to support transactions.
//!

use prelude::*;

use io;
use core::{fmt, str, default::Default};
use std::error;
#[cfg(feature = "std")] use std::error;

use hashes::{self, Hash, sha256d};
use hashes::hex::FromHex;
Expand Down Expand Up @@ -130,8 +132,9 @@ impl fmt::Display for ParseOutPointError {
}
}

impl error::Error for ParseOutPointError {
fn cause(&self) -> Option<&dyn error::Error> {
#[cfg(feature = "std")]
impl error::Error for ParseOutPointError {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
ParseOutPointError::Txid(ref e) => Some(e),
ParseOutPointError::Vout(ref e) => Some(e),
Expand Down Expand Up @@ -630,6 +633,7 @@ impl fmt::Display for NonStandardSigHashType {
}
}

#[cfg(feature = "std")]
impl error::Error for NonStandardSigHashType {}

/// Hashtype of an input's signature, encoded in the last byte of the signature
Expand Down Expand Up @@ -1360,6 +1364,7 @@ mod tests {
use hashes::hex::FromHex;
use std::collections::HashMap;
use blockdata::script;

// a random recent segwit transaction from blockchain using both old and segwit inputs
let mut spending: Transaction = deserialize(Vec::from_hex("020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700")
.unwrap().as_slice()).unwrap();
Expand Down

0 comments on commit df023b5

Please sign in to comment.