From 626684a7b899d5c6beee9bebf88dc184c1e473f1 Mon Sep 17 00:00:00 2001 From: brianp Date: Fri, 12 Jul 2024 00:14:02 +0200 Subject: [PATCH 1/2] Count the unique keys involved by bytes --- .../wallet/src/handlers/get_script_offset.rs | 25 +++++++++++++++++++ .../minotari_ledger_wallet/wallet/src/main.rs | 2 ++ 2 files changed, 27 insertions(+) diff --git a/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs b/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs index ff79c8cf75..b2844bee9b 100644 --- a/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs +++ b/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs @@ -1,6 +1,7 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause +use alloc::vec::Vec; use core::ops::Deref; use ledger_device_sdk::io::Comm; @@ -15,12 +16,15 @@ use crate::{ STATIC_ALPHA_INDEX, }; +const MIN_UNIQUE_KEYS: usize = 2; + pub struct ScriptOffsetCtx { total_sender_offset_private_key: Zeroizing, total_script_private_key: Zeroizing, account: u64, total_offset_indexes: u64, total_commitment_keys: u64, + all_keys: Vec>, } // Implement constructor for TxInfo with default values @@ -32,6 +36,7 @@ impl ScriptOffsetCtx { account: 0, total_offset_indexes: 0, total_commitment_keys: 0, + all_keys: Vec::new(), } } @@ -42,6 +47,19 @@ impl ScriptOffsetCtx { self.account = 0; self.total_offset_indexes = 0; self.total_commitment_keys = 0; + self.all_keys = Vec::new(); + } + + fn count_unique(&self) -> usize { + let mut unique = Vec::with_capacity(self.all_keys.len()); + + for item in self.all_keys.iter() { + if !unique.contains(item) { + unique.push(item.clone()); + } + } + + unique.len() } } @@ -99,6 +117,7 @@ pub fn handler_get_script_offset( let index = u64::from_le_bytes(index_bytes); let offset = derive_from_bip32_key(offset_ctx.account, index, KeyType::SenderOffset)?; + offset_ctx.all_keys.push(offset.clone()); offset_ctx.total_sender_offset_private_key = Zeroizing::new(offset_ctx.total_sender_offset_private_key.deref() + offset.deref()); } @@ -111,6 +130,8 @@ pub fn handler_get_script_offset( get_key_from_canonical_bytes::(&data[0..32])?.into(); let k = alpha_hasher(alpha, blinding_factor)?; + + offset_ctx.all_keys.push(k.clone()); offset_ctx.total_script_private_key = Zeroizing::new(offset_ctx.total_script_private_key.deref() + k.deref()); } @@ -118,6 +139,10 @@ pub fn handler_get_script_offset( return Ok(()); } + if offset_ctx.count_unique() < MIN_UNIQUE_KEYS { + return Err(AppSW::ScriptOffsetNotUnique); + } + let script_offset = Zeroizing::new( offset_ctx.total_script_private_key.deref() - offset_ctx.total_sender_offset_private_key.deref(), ); diff --git a/applications/minotari_ledger_wallet/wallet/src/main.rs b/applications/minotari_ledger_wallet/wallet/src/main.rs index 4eda1cdd54..53bec7f96c 100644 --- a/applications/minotari_ledger_wallet/wallet/src/main.rs +++ b/applications/minotari_ledger_wallet/wallet/src/main.rs @@ -95,12 +95,14 @@ pub enum AppSW { ClaNotSupported = 0x6E00, ScriptSignatureFail = 0xB001, MetadataSignatureFail = 0xB002, + ScriptOffsetNotUnique = 0xB004, KeyDeriveFail = 0xB009, KeyDeriveFromCanonical = 0xB010, KeyDeriveFromUniform = 0xB011, VersionParsingFail = 0xB00A, TooManyPayloads = 0xB003, WrongApduLength = StatusWords::BadLen as u16, + UserCancelled = StatusWords::UserCancelled as u16, } impl From for Reply { From 5f9f48eeec96fa04b9f28a855882ff1b5de27195 Mon Sep 17 00:00:00 2001 From: brianp Date: Tue, 16 Jul 2024 12:44:57 +0200 Subject: [PATCH 2/2] Store less data by only ever keeping the unique keys --- .../wallet/src/handlers/get_script_offset.rs | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs b/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs index b2844bee9b..0e43aa1214 100644 --- a/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs +++ b/applications/minotari_ledger_wallet/wallet/src/handlers/get_script_offset.rs @@ -24,7 +24,7 @@ pub struct ScriptOffsetCtx { account: u64, total_offset_indexes: u64, total_commitment_keys: u64, - all_keys: Vec>, + unique_keys: Vec>, } // Implement constructor for TxInfo with default values @@ -36,7 +36,7 @@ impl ScriptOffsetCtx { account: 0, total_offset_indexes: 0, total_commitment_keys: 0, - all_keys: Vec::new(), + unique_keys: Vec::new(), } } @@ -47,19 +47,13 @@ impl ScriptOffsetCtx { self.account = 0; self.total_offset_indexes = 0; self.total_commitment_keys = 0; - self.all_keys = Vec::new(); + self.unique_keys = Vec::new(); } - fn count_unique(&self) -> usize { - let mut unique = Vec::with_capacity(self.all_keys.len()); - - for item in self.all_keys.iter() { - if !unique.contains(item) { - unique.push(item.clone()); - } + fn add_unique_key(&mut self, secret_key: Zeroizing) { + if !self.unique_keys.contains(&secret_key) { + self.unique_keys.push(secret_key); } - - unique.len() } } @@ -117,7 +111,7 @@ pub fn handler_get_script_offset( let index = u64::from_le_bytes(index_bytes); let offset = derive_from_bip32_key(offset_ctx.account, index, KeyType::SenderOffset)?; - offset_ctx.all_keys.push(offset.clone()); + offset_ctx.add_unique_key(offset.clone()); offset_ctx.total_sender_offset_private_key = Zeroizing::new(offset_ctx.total_sender_offset_private_key.deref() + offset.deref()); } @@ -131,7 +125,7 @@ pub fn handler_get_script_offset( let k = alpha_hasher(alpha, blinding_factor)?; - offset_ctx.all_keys.push(k.clone()); + offset_ctx.add_unique_key(k.clone()); offset_ctx.total_script_private_key = Zeroizing::new(offset_ctx.total_script_private_key.deref() + k.deref()); } @@ -139,7 +133,7 @@ pub fn handler_get_script_offset( return Ok(()); } - if offset_ctx.count_unique() < MIN_UNIQUE_KEYS { + if offset_ctx.unique_keys.len() < MIN_UNIQUE_KEYS { return Err(AppSW::ScriptOffsetNotUnique); }