-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
488 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
authors = [ | ||
"Riccardo Casatta <riccardo@casatta.it>", | ||
"Dev Random <c1.devrandom@niftybox.net>", | ||
] | ||
edition = "2018" | ||
readme = "README.md" | ||
name = "embedded" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
cortex-m = "0.6.0" | ||
cortex-m-rt = "0.6.10" | ||
cortex-m-semihosting = "0.3.3" | ||
panic-halt = "0.2.0" | ||
alloc-cortex-m = "0.4.1" | ||
miniscript = { path = "../", default-features = false, features = ["no-std"] } | ||
|
||
[[bin]] | ||
name = "embedded" | ||
test = false | ||
bench = false | ||
|
||
[profile.release] | ||
codegen-units = 1 # better optimizations | ||
debug = true # symbols are nice and they don't increase the size on Flash | ||
lto = true # better optimizations | ||
opt-level = "z" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Running | ||
|
||
To run the embedded test, first prepare your environment: | ||
|
||
```shell | ||
sudo ./scripts/install-deps | ||
rustup target add thumbv7m-none-eabi | ||
``` | ||
|
||
Then: | ||
|
||
```shell | ||
source ./scripts/env.sh && cargo run +nightly --target thumbv7m-none-eabi | ||
``` | ||
|
||
Output should be something like: | ||
|
||
```text | ||
heap size 1048576 | ||
descriptor sh(wsh(or_d(c:pk_k(020e0338c96a8870479f2396c373cc7696ba124e8635d41b0ea581112b67817261),c:pk_k(0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352)))) | ||
p2sh address 3CJxbQBfWAe1ZkKiGQNEYrioV73ZwvBWns | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
MEMORY | ||
{ | ||
FLASH : ORIGIN = 0x00000000, LENGTH = 2048K | ||
RAM : ORIGIN = 0x20000000, LENGTH = 512K | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export RUSTFLAGS="-C link-arg=-Tlink.x" | ||
export CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER="qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
apt install gcc-arm-none-eabi qemu-system-arm gdb-multiarch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#![no_std] | ||
#![no_main] | ||
#![feature(alloc_error_handler)] | ||
#![feature(panic_info_message)] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::string::ToString; | ||
use core::alloc::Layout; | ||
use core::panic::PanicInfo; | ||
|
||
use alloc_cortex_m::CortexMHeap; | ||
|
||
use core::str::FromStr; | ||
|
||
use cortex_m::asm; | ||
use cortex_m_rt::entry; | ||
use cortex_m_semihosting::{debug, hprintln}; | ||
|
||
// this is the allocator the application will use | ||
#[global_allocator] | ||
static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); | ||
|
||
const HEAP_SIZE: usize = 1024 * 256; // 256 KB | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
hprintln!("heap size {}", HEAP_SIZE).unwrap(); | ||
|
||
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) } | ||
|
||
// begin miniscript test | ||
let descriptor = "sh(wsh(or_d(\ | ||
c:pk_k(020e0338c96a8870479f2396c373cc7696ba124e8635d41b0ea581112b67817261),\ | ||
c:pk_k(0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352)\ | ||
)))"; | ||
hprintln!("descriptor {}", descriptor).unwrap(); | ||
let desc = | ||
miniscript::Descriptor::<miniscript::bitcoin::PublicKey>::from_str(descriptor).unwrap(); | ||
|
||
// Derive the P2SH address | ||
let p2sh_addr = desc | ||
.address(miniscript::bitcoin::Network::Bitcoin) | ||
.unwrap() | ||
.to_string(); | ||
hprintln!("p2sh address {}", p2sh_addr).unwrap(); | ||
assert_eq!(p2sh_addr, "3CJxbQBfWAe1ZkKiGQNEYrioV73ZwvBWns"); | ||
|
||
// Check whether the descriptor is safe | ||
// This checks whether all spend paths are accessible in bitcoin network. | ||
// It maybe possible that some of the spend require more than 100 elements in Wsh scripts | ||
// Or they contain a combination of timelock and heightlock. | ||
assert!(desc.sanity_check().is_ok()); | ||
|
||
// Estimate the satisfaction cost | ||
assert_eq!(desc.max_satisfaction_weight().unwrap(), 293); | ||
// end miniscript test | ||
|
||
// exit QEMU | ||
// NOTE do not run this on hardware; it can corrupt OpenOCD state | ||
debug::exit(debug::EXIT_SUCCESS); | ||
|
||
loop {} | ||
} | ||
|
||
// define what happens in an Out Of Memory (OOM) condition | ||
#[alloc_error_handler] | ||
fn alloc_error(_layout: Layout) -> ! { | ||
hprintln!("alloc error").unwrap(); | ||
debug::exit(debug::EXIT_FAILURE); | ||
asm::bkpt(); | ||
|
||
loop {} | ||
} | ||
|
||
#[inline(never)] | ||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
hprintln!("panic {:?}", info.message()).unwrap(); | ||
debug::exit(debug::EXIT_FAILURE); | ||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.