2022-09-16 01:52:57 +00:00
|
|
|
#![cfg_attr(feature = "alloc", feature(alloc_error_handler))]
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate bitcoin_hashes;
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")] extern crate alloc;
|
|
|
|
#[cfg(feature = "alloc")] use alloc_cortex_m::CortexMHeap;
|
2023-01-07 15:39:11 +00:00
|
|
|
#[cfg(feature = "alloc")] use alloc::string::ToString;
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
use bitcoin_hashes::{sha256, Hash, HashEngine};
|
Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.
Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.
Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.
Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.
This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-10-04 05:51:26 +00:00
|
|
|
use bitcoin_io::io::Write;
|
2022-09-16 01:52:57 +00:00
|
|
|
use core::str::FromStr;
|
|
|
|
use cortex_m_rt::entry;
|
|
|
|
use cortex_m_semihosting::{debug, hprintln};
|
|
|
|
use panic_halt as _;
|
|
|
|
|
2023-02-22 08:20:28 +00:00
|
|
|
hash_newtype! {
|
|
|
|
struct TestType(sha256::Hash);
|
|
|
|
}
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
// this is the allocator the application will use
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
const HEAP_SIZE: usize = 1024; // in bytes
|
|
|
|
|
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
|
|
|
|
|
|
|
|
let mut engine = TestType::engine();
|
|
|
|
engine.write_all(b"abc").unwrap();
|
|
|
|
check_result(engine);
|
|
|
|
|
|
|
|
let mut engine = TestType::engine();
|
|
|
|
engine.input(b"abc");
|
|
|
|
check_result(engine);
|
|
|
|
|
|
|
|
debug::exit(debug::EXIT_SUCCESS);
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_result(engine: sha256::HashEngine) {
|
|
|
|
let hash = TestType::from_engine(engine);
|
|
|
|
|
|
|
|
let hash_check =
|
|
|
|
TestType::from_str("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
|
|
|
|
.unwrap();
|
|
|
|
hprintln!("hash:{} hash_check:{}", hash, hash_check).unwrap();
|
|
|
|
if hash != hash_check {
|
|
|
|
debug::exit(debug::EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")]
|
2023-01-07 15:39:11 +00:00
|
|
|
if hash.to_string() != hash_check.to_string() {
|
2022-09-16 01:52:57 +00:00
|
|
|
debug::exit(debug::EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|