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

feat: count unique keys #6401

Merged
merged 2 commits into from
Jul 18, 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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,12 +16,15 @@ use crate::{
STATIC_ALPHA_INDEX,
};

const MIN_UNIQUE_KEYS: usize = 2;

pub struct ScriptOffsetCtx {
total_sender_offset_private_key: Zeroizing<RistrettoSecretKey>,
total_script_private_key: Zeroizing<RistrettoSecretKey>,
account: u64,
total_offset_indexes: u64,
total_commitment_keys: u64,
unique_keys: Vec<Zeroizing<RistrettoSecretKey>>,
}

// Implement constructor for TxInfo with default values
Expand All @@ -32,6 +36,7 @@ impl ScriptOffsetCtx {
account: 0,
total_offset_indexes: 0,
total_commitment_keys: 0,
unique_keys: Vec::new(),
}
}

Expand All @@ -42,6 +47,13 @@ impl ScriptOffsetCtx {
self.account = 0;
self.total_offset_indexes = 0;
self.total_commitment_keys = 0;
self.unique_keys = Vec::new();
}

fn add_unique_key(&mut self, secret_key: Zeroizing<RistrettoSecretKey>) {
if !self.unique_keys.contains(&secret_key) {
self.unique_keys.push(secret_key);
}
}
}

Expand Down Expand Up @@ -99,6 +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.add_unique_key(offset.clone());
offset_ctx.total_sender_offset_private_key =
Zeroizing::new(offset_ctx.total_sender_offset_private_key.deref() + offset.deref());
}
Expand All @@ -111,13 +124,19 @@ pub fn handler_get_script_offset(
get_key_from_canonical_bytes::<RistrettoSecretKey>(&data[0..32])?.into();

let k = alpha_hasher(alpha, blinding_factor)?;

offset_ctx.add_unique_key(k.clone());
offset_ctx.total_script_private_key = Zeroizing::new(offset_ctx.total_script_private_key.deref() + k.deref());
}

if more {
return Ok(());
}

if offset_ctx.unique_keys.len() < 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(),
);
Expand Down
2 changes: 2 additions & 0 deletions applications/minotari_ledger_wallet/wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppSW> for Reply {
Expand Down
Loading