Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip32 support #99

Merged
merged 6 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ 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}/app/src/c_api/rust.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/blake2s/blake2s-ref.c
)
Expand Down Expand Up @@ -195,15 +196,15 @@ else()
endif()

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

# 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
COMMAND cargo build --release --target ${RUST_TARGET_TRIPLE} --features cpp_tests
WORKING_DIRECTORY ${RUST_LIB_DIR}
DEPENDS RustLibClean
)
Expand Down
4 changes: 2 additions & 2 deletions app/Makefile.version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is the `transaction_version` field of `Runtime`
APPVERSION_M=1
APPVERSION_M=2
# This is the `spec_version` field of `Runtime`
APPVERSION_N=0
# This is the patch version of this release
APPVERSION_P=6
APPVERSION_P=0
11 changes: 11 additions & 0 deletions app/rust/.cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ rustflags = [
"-C", "relocation-model=ropi",
"-C", "link-arg=-nostartfiles",
"-C", "link-arg=-Tlink.ld",
"-C", "panic=abort",
]
[unstable]
build-std=["core"]
build-std-features=["panic_immediate_abort"]

[target.'cfg(target_os = "linux")']
rustflags = [
"-C",
"link-arg=-Wl,--gc-sections",
"-C",
"link-arg=-Wl,--as-needed",
"-C",
"panic=abort", # Add this line to disable unwinding
]
101 changes: 101 additions & 0 deletions app/rust/Cargo.lock

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

20 changes: 17 additions & 3 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,42 @@ name = "rslib"
crate-type = ["staticlib"]

[dependencies]
ztruct = { path = "../ztruct", version = "*" }
jubjub = { version = "0.10.0", default-features = false }
aes = { version = "0.7", default-features = false }
binary-ff1 = { version = "0.2", default-features = false }
blake2s_simd = { version = "0.5", default-features = false }
blake2b_simd = { version = "0.5", default-features = false }
byteorder = { version = "1.5", default-features = false }
log = "0.4"


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

[profile.release]
lto = false
codegen-units = 1
debug = false
opt-level = "z"
panic = "abort"
# lto = false
# Settings below aimed to reduce
# binary size due to code
lto = "fat"
overflow-checks = false
strip = "symbols"

[profile.dev]
lto = true
lto = false
codegen-units = 1
debug=true
opt-level = "z"
panic = "abort"
strip = true
strip = true

[features]
default = []
clippy = []
fuzzing = []
# use when compiling this crate as a lib for the cpp_tests suite
cpp_tests = []
10 changes: 5 additions & 5 deletions app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
/* 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(const uint8_t dk[32], uint8_t start_index[11], uint8_t d_l[44]);
parser_error_t get_default_diversifier(const uint8_t dk[32], uint8_t start_index[11], uint8_t d[11]);
parser_error_t get_pkd(const uint8_t ivk_ptr[32], const uint8_t hash[32], uint8_t pk_d[32]);
parser_error_t get_pkd(const uint8_t ivk_ptr[32], const uint8_t hash[32], uint8_t pk_d[32]);
bool is_valid_diversifier(const uint8_t hash[32]);
parser_error_t randomized_secret_from_seed(const uint8_t ask[32], const uint8_t alpha[32], uint8_t output[32]);
parser_error_t compute_sbar(const uint8_t s[32], uint8_t r[32], uint8_t rsk[32], uint8_t sbar[32]);
parser_error_t add_points(const uint8_t hash[32], const uint8_t value[32], const uint8_t scalar[32], uint8_t cv[32]);
parser_error_t is_valid_diversifier(const uint8_t hash[32]);
void get_pkd(uint32_t zip32_account, const uint8_t *diversifier_ptr, uint8_t *pkd);
void zip32_child_ask_nsk(uint32_t account, uint8_t *ask, uint8_t *nsk);
void diversifier_find_valid(uint32_t zip32_account, uint8_t *default_diversifier);
void zip32_xfvk(uint32_t zip32_account, uint8_t *fvk_tag, uint8_t *chain_code, uint8_t *fvk, uint8_t *dk);
46 changes: 46 additions & 0 deletions app/rust/src/bolos/aes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use aes::cipher::generic_array::typenum::{U16, U32, U8};
use aes::cipher::generic_array::GenericArray;
use aes::cipher::BlockEncrypt;
use aes::cipher::NewBlockCipher;
use aes::cipher::{BlockCipher, BlockCipherKey};
use aes::Aes256;

use super::c_zemu_log_stack;

/// Encrypts a block using AES-256.
/// This function uses the Rust `aes` crate for encryption in test environments.
pub fn aes256_encrypt_block(k: &[u8], a: &[u8]) -> Result<[u8; 16], i32> {
let cipher = Aes256::new(GenericArray::from_slice(k));

let mut b = GenericArray::clone_from_slice(a);
cipher.encrypt_block(&mut b);

// Attempt to convert to [u8; 16], return error if conversion fails
b.as_slice().try_into().map_err(|_| -1)
}

pub struct AesBOLOS {
key: [u8; 32],
}

impl AesBOLOS {
pub fn new(k: &[u8; 32]) -> AesBOLOS {
AesBOLOS { key: *k }
}
}

impl BlockCipher for AesBOLOS {
type BlockSize = U16;
type ParBlocks = U8;
}

impl BlockEncrypt for AesBOLOS {
#[inline(never)]
fn encrypt_block(&self, block: &mut GenericArray<u8, Self::BlockSize>) {
let x: [u8; 16] = block.as_slice().try_into().expect("err");
let y = aes256_encrypt_block(&self.key, &x);
if let Ok(y) = y {
block.copy_from_slice(&y);
}
}
}
Loading
Loading