From 2b90eb88b3a4dbbf18c2d2b262037f150ddb9942 Mon Sep 17 00:00:00 2001 From: Natanael Mojica Date: Wed, 14 Aug 2024 11:53:54 -0500 Subject: [PATCH] target nanos feature to adjust heap size (#23) * target nanos feature to adjust heap size * Remove duplicated setting --- app/Makefile | 8 +++++++- app/rust/Cargo.toml | 11 ++++++++--- app/rust/src/lib.rs | 38 +++++++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/Makefile b/app/Makefile index 707dd8f..dc27cd1 100755 --- a/app/Makefile +++ b/app/Makefile @@ -62,6 +62,12 @@ include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.devices $(info TARGET_NAME = [$(TARGET_NAME)]) $(info ICONNAME = [$(ICONNAME)]) +ifeq ($(TARGET_NAME),TARGET_NANOS) + RUST_FEATURES += --features "target-nanos" +else + RUST_FEATURES += --features "dkg-support" +endif + ifndef ICONNAME $(error ICONNAME is not set) endif @@ -83,7 +89,7 @@ RUST_TARGET:=thumbv6m-none-eabi .PHONY: rust rust: - cd rust && RUSTC_BOOTSTRAP=1 CARGO_HOME="$(CURDIR)/rust/.cargo" TARGET_NAME=$(TARGET_NAME) cargo build --target $(RUST_TARGET) --release + cd rust && RUSTC_BOOTSTRAP=1 CARGO_HOME="$(CURDIR)/rust/.cargo" TARGET_NAME=$(TARGET_NAME) cargo build --target $(RUST_TARGET) --release --no-default-features $(RUST_FEATURES) .PHONY: rust_clean rust_clean: diff --git a/app/rust/Cargo.toml b/app/rust/Cargo.toml index 5293c77..22921ce 100644 --- a/app/rust/Cargo.toml +++ b/app/rust/Cargo.toml @@ -24,7 +24,9 @@ const-default = { version = "1.0.0", default-features = false, optional = true } bolos = { path = "../../deps/ledger-rust/bolos" } # Taken from https://github.com/iron-fish/ironfish-frost-ledger/tree/ad03ed83b5f2ebf4f494ae260fa1cf81c4599d58 -ironfish-frost = { git = "https://github.com/iron-fish/ironfish-frost.git", branch = "no-std", default-features = false, features = ["dkg"]} +ironfish-frost = { git = "https://github.com/iron-fish/ironfish-frost.git", branch = "no-std", default-features = false, features = [ + "dkg", +] } getrandom = { version = "0.2", features = ["custom"] } [target.thumbv6m-none-eabi.dev-dependencies] @@ -40,7 +42,7 @@ overflow-checks = true [profile.dev] lto = "fat" codegen-units = 1 -debug=true +debug = true opt-level = 3 panic = "abort" @@ -48,10 +50,13 @@ panic = "abort" default = [] # use when compiling this crate as a lib for the cpp_tests suite cpp_tests = [] +dkg-support = [] +target-nanos = [] # From https://github.com/iron-fish/ironfish-frost-ledger/tree/ad03ed83b5f2ebf4f494ae260fa1cf81c4599d58 # TODO: This change shouldn't be necessary, the ledger targets clearly define atomics as only supporting 32 not 64 (as seen in /opt/rustup/toolchains/1.75.0-aarch64-unknown-linux-musl/lib/rustlib/nanosplus/target.json) # solve why this is happening rather than using modified radium [patch.crates-io] -radium = { git = "https://github.com/iron-fish/radium", rev = "674c8faf1e74f931a58671f70586e6435353e9b6" } \ No newline at end of file +radium = { git = "https://github.com/iron-fish/radium", rev = "674c8faf1e74f931a58671f70586e6435353e9b6" } + diff --git a/app/rust/src/lib.rs b/app/rust/src/lib.rs index 52a7720..f6d3f29 100644 --- a/app/rust/src/lib.rs +++ b/app/rust/src/lib.rs @@ -19,27 +19,28 @@ use core::panic::PanicInfo; -use constants::{SPENDING_KEY_GENERATOR}; +use constants::SPENDING_KEY_GENERATOR; mod constants; mod heap; -use jubjub::{Fr, AffinePoint, ExtendedPoint}; +use jubjub::{AffinePoint, ExtendedPoint, Fr}; -use heap::Heap; -use critical_section::RawRestoreState; use core::mem::MaybeUninit; +use critical_section::RawRestoreState; +use heap::Heap; use bolos::{lazy_static, pic::PIC}; -// TODO increase this whenever dkg features are set as rust feature. Nano S device won't have this feature enabled -// TODO For now, if this is bigger, nano s build will fail. -const HEAP_SIZE :usize = 100; +#[cfg(not(feature = "target-nanos"))] +const HEAP_SIZE: usize = 8 * 1024; +#[cfg(feature = "target-nanos")] +const HEAP_SIZE: usize = 128; #[global_allocator] static HEAP: Heap = Heap::empty(); #[lazy_static] -static mut BUFFER: [u8;HEAP_SIZE] = [0u8;HEAP_SIZE]; +static mut BUFFER: [u8; HEAP_SIZE] = [0u8; HEAP_SIZE]; struct CriticalSection; critical_section::set_impl!(CriticalSection); @@ -83,7 +84,11 @@ pub extern "C" fn from_bytes_wide(input: &[u8; 64], output: &mut [u8; 32]) -> Pa } #[no_mangle] -pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, output: *mut [u8; 32]) -> ParserError { +pub extern "C" fn scalar_multiplication( + input: &[u8; 32], + key: ConstantKey, + output: *mut [u8; 32], +) -> ParserError { let key_point = match key { ConstantKey::SpendingKeyGenerator => constants::SPENDING_KEY_GENERATOR, ConstantKey::ProofGenerationKeyGenerator => constants::PROOF_GENERATION_KEY_GENERATOR, @@ -102,8 +107,11 @@ pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, outp } #[no_mangle] -pub extern "C" fn randomizeKey(key: &[u8; 32], randomness: &[u8; 32], output: &mut [u8; 32]) -> ParserError { - +pub extern "C" fn randomizeKey( + key: &[u8; 32], + randomness: &[u8; 32], + output: &mut [u8; 32], +) -> ParserError { let mut skfr = Fr::from_bytes(key).unwrap(); let alphafr = Fr::from_bytes(randomness).unwrap(); skfr += alphafr; @@ -113,7 +121,12 @@ pub extern "C" fn randomizeKey(key: &[u8; 32], randomness: &[u8; 32], output: &m } #[no_mangle] -pub extern "C" fn compute_sbar( s: &[u8; 32], r: &[u8; 32], rsk: &[u8; 32], sbar: &mut [u8; 32]) -> ParserError{ +pub extern "C" fn compute_sbar( + s: &[u8; 32], + r: &[u8; 32], + rsk: &[u8; 32], + sbar: &mut [u8; 32], +) -> ParserError { let s_point = Fr::from_bytes(s).unwrap(); let r_point = Fr::from_bytes(r).unwrap(); let rsk_point = Fr::from_bytes(rsk).unwrap(); @@ -124,7 +137,6 @@ pub extern "C" fn compute_sbar( s: &[u8; 32], r: &[u8; 32], rsk: &[u8; 32], s ParserError::ParserOk } - #[cfg(not(feature = "cpp_tests"))] #[panic_handler] fn panic(_info: &PanicInfo) -> ! {