Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Mar 21, 2024
1 parent 89df952 commit 2c3fce8
Show file tree
Hide file tree
Showing 16 changed files with 1,221 additions and 53 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
111 changes: 111 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 }
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]);
40 changes: 24 additions & 16 deletions app/rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ use jubjub::{AffineNielsPoint, AffinePoint, Fq};

pub const SPENDING_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_unchecked(
Fq::from_raw([
0x47bf_4692_0a95_a753,
0xd5b9_a7d3_ef8e_2827,
0xd418_a7ff_2675_3b6a,
0x0926_d4f3_2059_c712,
0xec75_293d_8124_8452,
0x39f5_b033_80af_6020,
0xf831_c2b1_9fec_6026,
0x5b38_9522_a9e8_1532,
]),
Fq::from_raw([
0x3056_32ad_aaf2_b530,
0x6d65_674d_cedb_ddbc,
0x53bb_37d0_c21c_fd05,
0x57a1_019e_6de9_b675,
0x14b6_2623_a186_b4b1,
0x2012_d031_f624_fd52,
0x75de_fecf_f1f4_9ef2,
0x0cbc_5f9f_1e52_e0ab,
]),
)
.to_niels();

pub const PROOF_GENERATION_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_unchecked(
Fq::from_raw([
0x3af2_dbef_b96e_2571,
0xadf2_d038_f2fb_b820,
0x7043_03f1_e890_6081,
0x1457_a502_31cd_e2df,
0x5f3c_723a_a253_1b66,
0x1e24_f832_67f1_5abd,
0x4ba1_f065_e719_fd03,
0x4caa_eaca_af28_ed4b,
]),
Fq::from_raw([
0x467a_f9f7_e05d_e8e7,
0x50df_51ea_f5a1_49d2,
0xdec9_0184_0f49_48cc,
0x54b6_d107_18df_2a7a,
0xfe6f_96be_c575_bff8,
0x36b4_9c71_a2af_0708,
0xc654_dfdd_3600_4de9,
0x0093_0d67_d690_6365,
]),
)
.to_niels();
Expand All @@ -63,3 +63,11 @@ pub const PUBLIC_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_uncheck
]),
)
.to_niels();


pub const DIV_SIZE: usize = 11;
pub const DIV_DEFAULT_LIST_LEN: usize = 4;
/// BLAKE2s Personalization for the group hash for key diversification
pub const KEY_DIVERSIFICATION_PERSONALIZATION: &[u8; 8] = b"MASP__gd";
pub const GH_FIRST_BLOCK: &[u8; 64] =
b"096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0";
Loading

0 comments on commit 2c3fce8

Please sign in to comment.