Skip to content

Commit

Permalink
Merge pull request #92 from Zondax/nanos_optimizations
Browse files Browse the repository at this point in the history
Nanos optimizations
  • Loading branch information
ftheirs committed Apr 15, 2024
2 parents d72efa5 + ad6e444 commit eeb91ce
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ app/glyphs/glyphs.h
/tests_zemu/yarn.lock

node_modules/.yarn-integrity
app/rust/.cargo/.package-cache-mutate
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=3
# This is the minor version of this release
APPVERSION_N=3
# This is the patch version of this release
APPVERSION_P=5
APPVERSION_P=6
5 changes: 3 additions & 2 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ serde = { version = "1.0.110", features = ["derive"] }
[profile.release]
lto = false
codegen-units = 1
debug = true
opt-level = "s"
debug = false
opt-level = "z"
overflow-checks = true

[profile.dev]
panic = "abort"
22 changes: 22 additions & 0 deletions app/rust/src/zip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn c_zemu_log_stack(_s: &[u8]) {}

#[inline(always)]
pub fn prf_expand(sk: &[u8], t: &[u8]) -> [u8; 64] {
crate::heart_beat();
bolos::blake2b_expand_seed(sk, t)
}

Expand Down Expand Up @@ -311,12 +312,18 @@ pub fn outgoingviewingkey(key: &[u8; 32]) -> [u8; 32] {
#[inline(never)]
pub fn full_viewingkey(key: &[u8; 32]) -> [u8; 96] {
let ask = sapling_derive_dummy_ask(key);
crate::heart_beat();
let ak = sapling_ask_to_ak(&ask);
crate::heart_beat();

let nsk = sapling_derive_dummy_nsk(key);
crate::heart_beat();
let nk = sapling_nsk_to_nk(&nsk);
crate::heart_beat();

let ovk = outgoingviewingkey(key);
crate::heart_beat();

let mut result = [0u8; 96];
result[0..32].copy_from_slice(&ak);
result[32..64].copy_from_slice(&nk);
Expand Down Expand Up @@ -385,6 +392,7 @@ pub fn derive_zip32_ovk_fromseedandpath(seed: &[u8; 32], path: &[u32]) -> [u8; 3
let mut tmp = master_spending_key_zip32(seed); //64
let mut key = [0u8; 32]; //32
let mut chain = [0u8; 32]; //32
crate::heart_beat();

key.copy_from_slice(&tmp[..32]);
chain.copy_from_slice(&tmp[32..]);
Expand All @@ -408,13 +416,15 @@ pub fn derive_zip32_ovk_fromseedandpath(seed: &[u8; 32], path: &[u32]) -> [u8; 3
if hardened {
let mut le_i = [0; 4];
LittleEndian::write_u32(&mut le_i, c + (1 << 31));
crate::heart_beat();
//make index LE
//zip32 child derivation
tmp = bolos::blake2b_expand_vec_four(&chain, &[0x11], &expkey, &divkey, &le_i);
//64
} else {
//WARNING: CURRENTLY COMPUTING NON-HARDENED PATHS DO NOT FIT IN MEMORY
let fvk = full_viewingkey(&key);
crate::heart_beat();
let mut le_i = [0; 4];
LittleEndian::write_u32(&mut le_i, c);
tmp = bolos::blake2b_expand_vec_four(&chain, &[0x12], &fvk, &divkey, &le_i);
Expand All @@ -431,6 +441,7 @@ pub fn derive_zip32_ovk_fromseedandpath(seed: &[u8; 32], path: &[u32]) -> [u8; 3
nsk += nsk_cur;

//new divkey from old divkey and key
crate::heart_beat();
update_dk_zip32(&key, &mut divkey);
update_exk_zip32(&key, &mut expkey);
}
Expand All @@ -443,18 +454,22 @@ pub fn derive_zip32_ovk_fromseedandpath(seed: &[u8; 32], path: &[u32]) -> [u8; 3
pub fn derive_zip32_fvk_fromseedandpath(seed: &[u8; 32], path: &[u32]) -> [u8; 96] {
//ASSERT: len(path) == len(harden)

crate::heart_beat();
let mut tmp = master_spending_key_zip32(seed); //64
let mut key = [0u8; 32]; //32
let mut chain = [0u8; 32]; //32

key.copy_from_slice(&tmp[..32]);
chain.copy_from_slice(&tmp[32..]);

crate::heart_beat();
let mut ask = Fr::from_bytes_wide(&prf_expand(&key, &[0x00]));

crate::heart_beat();
let mut nsk = Fr::from_bytes_wide(&prf_expand(&key, &[0x01]));

let mut expkey: [u8; 96];
crate::heart_beat();
expkey = expandedspendingkey_zip32(&key); //96
//master divkey
let mut divkey = [0u8; 32];
Expand All @@ -467,22 +482,27 @@ pub fn derive_zip32_fvk_fromseedandpath(seed: &[u8; 32], path: &[u32]) -> [u8; 9
if hardened {
let mut le_i = [0; 4];
LittleEndian::write_u32(&mut le_i, c + (1 << 31));
crate::heart_beat();
//make index LE
//zip32 child derivation
tmp = bolos::blake2b_expand_vec_four(&chain, &[0x11], &expkey, &divkey, &le_i);
//64
} else {
//WARNING: CURRENTLY COMPUTING NON-HARDENED PATHS DO NOT FIT IN MEMORY
crate::heart_beat();
let fvk = full_viewingkey(&key);
let mut le_i = [0; 4];
LittleEndian::write_u32(&mut le_i, c);
crate::heart_beat();
tmp = bolos::blake2b_expand_vec_four(&chain, &[0x12], &fvk, &divkey, &le_i);
}
//extract key and chainkey
key.copy_from_slice(&tmp[..32]);
chain.copy_from_slice(&tmp[32..]);

crate::heart_beat();
let ask_cur = Fr::from_bytes_wide(&prf_expand(&key, &[0x13]));
crate::heart_beat();
let nsk_cur = Fr::from_bytes_wide(&prf_expand(&key, &[0x14]));

ask += ask_cur;
Expand Down Expand Up @@ -654,6 +674,7 @@ pub extern "C" fn zip32_ivk(
let seed = unsafe { &*seed_ptr };
let ivk = unsafe { &mut *ivk_ptr };

crate::heart_beat();
let k = derive_zip32_child_fromseedandpath(seed,
&[FIRSTVALUE, COIN_TYPE, pos],
AK_NK); //consistent with zecwallet
Expand Down Expand Up @@ -723,6 +744,7 @@ pub extern "C" fn zip32_ovk(seed_ptr: *const [u8; 32], ovk_ptr: *mut [u8; 32], p

const FIRSTVALUE: u32 = 32 ^ 0x8000_0000;
const COIN_TYPE: u32 = 133 ^ 0x8000_0000; //hardened, fixed value from https://github.com/adityapk00/librustzcash/blob/master/zcash_client_backend/src/constants/mainnet.rs
crate::heart_beat();
let k = derive_zip32_ovk_fromseedandpath(seed, &[FIRSTVALUE, COIN_TYPE, pos]); //consistent with zecwallet
ovk.copy_from_slice(&k[0..32]);
}
Expand Down
2 changes: 1 addition & 1 deletion deps/ledger-zxlib
2 changes: 1 addition & 1 deletion deps/nanosplus-secure-sdk
2 changes: 1 addition & 1 deletion deps/nanox-secure-sdk
Submodule nanox-secure-sdk updated 2079 files
2 changes: 1 addition & 1 deletion deps/stax-secure-sdk
Submodule stax-secure-sdk updated 992 files
24 changes: 12 additions & 12 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@
},
"homepage": "https://github.com/zondax/ledger-zcash",
"dependencies": {
"@babel/runtime": "^7.23.9",
"@ledgerhq/hw-transport": "^6.30.2"
"@babel/runtime": "^7.24.4",
"@ledgerhq/hw-transport": "^6.30.5"
},
"devDependencies": {
"@babel/cli": "^7.23.9",
"@babel/core": "^7.23.9",
"@babel/eslint-parser": "^7.23.9",
"@babel/cli": "^7.24.1",
"@babel/core": "^7.24.4",
"@babel/eslint-parser": "^7.24.1",
"@babel/node": "^7.23.9",
"@babel/plugin-transform-runtime": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"@babel/plugin-transform-runtime": "^7.24.3",
"@babel/preset-env": "^7.24.4",
"babel-jest": "^29.1.2",
"bip32": "^4.0.0",
"bip39": "^3.0.4",
"core-js": "^3.35.1",
"core-js": "^3.36.1",
"crypto-js": "4.2.0",
"eslint": "^8.56.0",
"eslint": "^9.0.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-jest": "^28.2.0",
"eslint-plugin-prettier": "^5.1.3",
"index.js": "^0.0.3",
"jest": "^29.1.2",
"jest-serial-runner": "^1.2.0",
"prettier": "^3.2.4",
"prettier": "^3.2.5",
"secp256k1": "^5.0.0",
"typescript": "^5.3.3"
"typescript": "^5.4.5"
},
"scripts": {
"build": "babel src --out-dir dist && yarn build:types",
Expand Down
18 changes: 9 additions & 9 deletions tests_zemu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@
"dependencies": {
"@zondax/ledger-zcash": "link:../js",
"@zondax/zcashtools": "link:../zcashtools/neon",
"@zondax/zemu": "^0.46.2"
"@zondax/zemu": "^0.48.0"
},
"devDependencies": {
"@ledgerhq/hw-transport-node-hid": "^6.28.2",
"@ledgerhq/hw-transport-node-hid": "^6.28.5",
"@ledgerhq/logs": "^6.10.1",
"@types/jest": "^29.5.11",
"@types/jest": "^29.5.12",
"@types/ledgerhq__hw-transport": "^4.21.4",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.56.0",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-jest": "^28.2.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.5.0",
"jest-serial-runner": "^1.2.0",
"prettier": "^3.2.4",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.4.5"
}
}
Binary file modified tests_zemu/snapshots/s-mainmenu/00004.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-mainmenu/00010.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00004.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00010.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00001.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00002.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00003.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00004.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00004.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00010.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eeb91ce

Please sign in to comment.