Skip to content

Commit

Permalink
Cleanup compilation warnings (#82)
Browse files Browse the repository at this point in the history
* Cleanup compilation warnings
* Make Clippy happy
* Cleanup ragger tests
  • Loading branch information
siy authored Oct 31, 2024
1 parent 0a79569 commit 3bd9ae0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
1 change: 0 additions & 1 deletion ragger_tests/test_sign_preauth_hash_secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def sign_preauth_hash_secp256k1(firmware, backend, navigator, test_name, vector)
# Note that Prehashed parameter is irrelevant here, we just need to pass something known to the library
pubkey.verify(signature, bytes(rc[98:130]), ec.ECDSA(utils.Prehashed(hashes.SHA256())))
print("Success")
assert rc[98:130].hex() == vector[0], "Invalid calculated hash\nExpected: " + vector[0] + "\nReceived: " + rc[98:130].hex()
except Exception as e:
print("Invalid signature ", e)

Expand Down
4 changes: 3 additions & 1 deletion ragger_tests/test_sign_tx_ed25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
DATA_PATH = str(Path(__file__).parent.joinpath("data").absolute()) + "/"
ROOT_SCREENSHOT_PATH = Path(__file__).parent.resolve()


def read_file(file):
with open(DATA_PATH + file, "rb") as f:
return f.read()
Expand Down Expand Up @@ -53,12 +52,15 @@ def sign_tx_ed25519(firmware, backend, navigator, click_count, file_name, test_n
except Exception as e:
if click_count == 0:
return
print("Communication error ", e)
raise

pubkey = ed25519.Ed25519PublicKey.from_public_bytes(bytes(rc[64:96]))
try:
pubkey.verify(bytes(rc[0:64]), bytes(rc[96:128]))
except Exception as e:
print("Invalid signature ", e)
raise


def test_sign_tx_ed25519_call_function(firmware, backend, navigator, test_name):
Expand Down
4 changes: 3 additions & 1 deletion ragger_tests/test_sign_tx_secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def sign_tx_secp256k1(firmware, backend, navigator, click_count, file_name, test
except Exception as e:
if click_count == 0:
return
print("Communication error ", e)
raise

r = int.from_bytes(rc[1:33], byteorder='big', signed=False)
s = int.from_bytes(rc[33:65], byteorder='big', signed=False)
Expand All @@ -64,9 +66,9 @@ def sign_tx_secp256k1(firmware, backend, navigator, click_count, file_name, test
# Note that Prehashed parameter is irrelevant here, we just need to pass something known to the library
pubkey.verify(signature, bytes(rc[98:130]), ec.ECDSA(utils.Prehashed(hashes.SHA256())))
print("Success")
assert rc[98:130].hex() == vector[0], "Invalid calculated hash\nExpected: " + vector[0] + "\nReceived: " + rc[98:130].hex()
except Exception as e:
print("Invalid signature ", e)
raise


def test_sign_tx_secp256k1_call_function(firmware, backend, navigator, test_name):
Expand Down
7 changes: 3 additions & 4 deletions src/crypto/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl KeyPair25519 {
0x00
};

for i in 0..32 {
for i in 0..ED25519_PUBLIC_KEY_LEN {
if i == 31 {
comm.append(&[self.origin.public.W[64 - i] ^ flip_bit]);
} else {
Expand All @@ -88,13 +88,12 @@ impl KeyPair25519 {
// To build compressed version of the public key we need to do following:
// 1. Reverse the order of the bytes (we need only Y coordinate and in opposite byte order)
// 2. Flip bit in the last byte, depending on the flag which is attached to X coordinate.
#[allow(clippy::needless_range_loop)]
pub fn public_bytes(&self) -> [u8; ED25519_PUBLIC_KEY_LEN] {
let mut pk = [0u8; ED25519_PUBLIC_KEY_LEN];

for i in 0..32 {
(0..ED25519_PUBLIC_KEY_LEN).for_each(|i| {
pk[i] = self.origin.public.W[64 - i];
}
});

if self.origin.public.W[32] & 1u8 == 1 {
pk[31] |= 0x80;
Expand Down
62 changes: 26 additions & 36 deletions src/utilities.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
// The code in this file is used only for debugging. Under normal circumstances, it is not invoked nor referenced.
#![allow(dead_code)]
#[cfg(debug_assertions)]
use core::str::from_utf8;

#[cfg(debug_assertions)]
use ledger_device_sdk::testing::debug_print;
#[cfg(debug_assertions)]
use sbor::utilities::conversion::{to_hex_str, to_str};

pub mod version;

#[cfg(debug_assertions)]
pub fn debug_u32(value: u32) {
debug_prepared_message(&to_str(value));
}

#[cfg(debug_assertions)]
pub fn debug_u32_hex(value: u32) {
debug_prepared_message(&to_hex_str(value));
}

#[cfg(debug_assertions)]
pub fn debug_prepared_message(message: &[u8]) {
debug_print(from_utf8(message).unwrap());
debug_print("\n");
}
// Useful helper function which prints current stack position.
// When necessary, uncomment code below and make module sbor::print::primitives public to make code compile

#[cfg(debug_assertions)]
pub fn debug_print_byte(byte: u8) {
let mut buffer = [0u8; 1];
buffer[0] = byte;
debug_print(from_utf8(&buffer).unwrap());
}
/*
// Useful helper function which prints current stack position
// requires making module sbor::print::primitives public
#[cfg(debug_assertions)]
pub mod debug {
use core::str::from_utf8;
use nanos_ui::ui::clear_screen;
use ledger_device_sdk::testing::debug_print;
use ledger_device_sdk::ui::gadgets::clear_screen;
use sbor::static_vec::StaticVec;
use sbor::utilities::conversion::{to_hex_str, to_str};
use crate::ui::single_message::SingleMessage;
pub fn debug_u32(value: u32) {
debug_prepared_message(&to_str(value));
}
pub fn debug_u32_hex(value: u32) {
debug_prepared_message(&to_hex_str(value));
}
pub fn debug_prepared_message(message: &[u8]) {
debug_print(from_utf8(message).unwrap());
debug_print("\n");
}
pub fn debug_print_byte(byte: u8) {
let mut buffer = [0u8; 1];
buffer[0] = byte;
debug_print(from_utf8(&buffer).unwrap());
}
pub fn display_memory(lead_byte: u8) {
clear_screen();
let mut number = StaticVec::<u8, 16>::new(0);
Expand All @@ -52,4 +42,4 @@ pub mod debug {
SingleMessage::new(from_utf8(number.as_slice()).unwrap()).show_and_wait();
}
}
*/
*/

0 comments on commit 3bd9ae0

Please sign in to comment.