Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Mar 22, 2024
1 parent 89df952 commit 171e5c0
Show file tree
Hide file tree
Showing 16 changed files with 1,213 additions and 52 deletions.
57 changes: 57 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/leb128.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_validator.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_delegation.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/blake2s/blake2s-ref.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand All @@ -136,10 +138,64 @@ target_include_directories(app_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
${CMAKE_CURRENT_SOURCE_DIR}/app/src/common
${CMAKE_CURRENT_SOURCE_DIR}/deps/picohash/
###
${CMAKE_CURRENT_SOURCE_DIR}/app/rust/include
###
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref
)

##############################################################
##############################################################
## Rust library for CPP tests
set(RUST_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/app/rust")

# Determine the Rust target triple based on the host system
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(RUST_TARGET_TRIPLE "aarch64-unknown-linux-gnu")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
set(RUST_TARGET_TRIPLE "x86_64-unknown-linux-gnu")
else()
message(FATAL_ERROR "Unsupported processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm64")
set(RUST_TARGET_TRIPLE "aarch64-apple-darwin")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
set(RUST_TARGET_TRIPLE "x86_64-apple-darwin")
else()
message(FATAL_ERROR "Unsupported processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
else()
message(FATAL_ERROR "Unsupported host system: ${CMAKE_HOST_SYSTEM_NAME}")
endif()

# Use debug mode for debugging tests
set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/${RUST_TARGET_TRIPLE}/debug")

# Custom target for the Rust library
add_custom_target(RustLibClean
COMMAND cargo clean
WORKING_DIRECTORY ${RUST_LIB_DIR}
)
add_custom_target(RustLibBuild
COMMAND cargo build --target ${RUST_TARGET_TRIPLE} --features cpp_tests
WORKING_DIRECTORY ${RUST_LIB_DIR}
DEPENDS RustLibClean
)

# Assuming the Rust library outputs a file named librslib.a
set(RUST_LIB "${RUST_TARGET_DIR}/librslib.a")

# Ensure the Rust library is built before the C++ project
add_library(rslib STATIC IMPORTED)
set_property(TARGET rslib PROPERTY IMPORTED_LOCATION ${RUST_LIB})
add_dependencies(rslib RustLibBuild)

# Ensure your C++ targets depend on the Rust library being built first
# For example, for your app_lib static library:
add_dependencies(app_lib rslib)
##############################################################
# Tests
file(GLOB_RECURSE TESTS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp)
Expand All @@ -159,6 +215,7 @@ target_include_directories(unittests PRIVATE
target_link_libraries(unittests PRIVATE
gtest_main
app_lib
rslib
CONAN_PKG::fmt
CONAN_PKG::jsoncpp)

Expand Down
19 changes: 10 additions & 9 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,22 @@ $(error ICONNAME is not set)
endif

include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform
CFLAGS += -Wvla
APP_CUSTOM_LINK_DEPENDENCIES = rust
LDLIBS += -Lrust/target/thumbv6m-none-eabi/release -lrslib
APP_SOURCE_PATH += $(CURDIR)/rust/include

# Include blake2s implementation from deps since it's not supported by the SDK
APP_SOURCE_PATH += $(CURDIR)/../deps/blake2/ref/blake2s-ref.c

# Add SDK BLAKE2b
DEFINES += HAVE_HASH HAVE_BLAKE2
INCLUDES_PATH += $(BOLOS_SDK)/lib_cxng/src


# Building Rust
LDFLAGS += -z muldefs
LDLIBS += -L$(MY_DIR)rust/target/$(RUST_TARGET)/release -lrslib

APP_SOURCE_PATH += $(CURDIR)/rust/include
APP_CUSTOM_LINK_DEPENDENCIES = rust
RUST_TARGET:=thumbv6m-none-eabi

.PHONY: rust
rust:
cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target thumbv6m-none-eabi --release
cd rust && RUSTC_BOOTSTRAP=1 CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target $(RUST_TARGET) --release

# Before linking, we need to be sure rust lib is there
bin/app.elf: rust
Expand Down
130 changes: 130 additions & 0 deletions app/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@
authors = ["Zondax AG <[email protected]>"]
name = "rslib"
version = "0.0.1"
edition = "2018"
edition = "2021"
readme = "README.md"
resolver = "2"

[lib]
name = "rslib"
crate-type = ["staticlib"]

[dependencies]
jubjub = { version = "0.10.0", default-features = false }
aes = { version = "0.8", default-features = false }
binary-ff1 = { version = "0.2.0", default-features = false }
fpe = { version = "0.6", default-features = false }


[target.thumbv6m-none-eabi.dev-dependencies]
panic-halt = "0.2.0"

[profile.release]
lto = false
codegen-units = 1
debug = false
opt-level = "z"

[profile.dev]
lto = "fat"
codegen-units = 1
debug=true
opt-level = "s"
panic = "abort"

[profile.dev]
panic = "abort"
debug=true

[features]
default = []
# use when compiling this crate as a lib for the cpp_tests suite
cpp_tests = []
1 change: 1 addition & 0 deletions app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/* Interface functions with jubjub crate */
parser_error_t from_bytes_wide(const uint8_t input[64], uint8_t output[32]);
parser_error_t scalar_multiplication(const uint8_t input[32], constant_key_t key, uint8_t output[32]);
parser_error_t get_default_diversifier_list_start_index(const uint8_t dk[32], uint8_t di[11], uint8_t d_l[44]);
Loading

0 comments on commit 171e5c0

Please sign in to comment.