Skip to content

Commit

Permalink
target nanos feature to adjust heap size (#23)
Browse files Browse the repository at this point in the history
* target nanos feature to adjust heap size

* Remove duplicated setting
  • Loading branch information
neithanmo authored Aug 14, 2024
1 parent 1401ccd commit 2b90eb8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
8 changes: 7 additions & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
11 changes: 8 additions & 3 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -40,18 +42,21 @@ overflow-checks = true
[profile.dev]
lto = "fat"
codegen-units = 1
debug=true
debug = true
opt-level = 3
panic = "abort"

[features]
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" }
radium = { git = "https://github.com/iron-fish/radium", rev = "674c8faf1e74f931a58671f70586e6435353e9b6" }

38 changes: 25 additions & 13 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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) -> ! {
Expand Down

0 comments on commit 2b90eb8

Please sign in to comment.