Skip to content

Commit

Permalink
wip: feat: Randomize secret keys with offset
Browse files Browse the repository at this point in the history
The secret keys are still insecure because the offset is only 32-bits
long.
  • Loading branch information
uncomputable committed Oct 24, 2024
1 parent 85fa924 commit 27f6181
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/components/run_window/key_store_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@ use leptos::{
SignalWith, View,
};
use simfony::{elements, simplicity};
use elements::secp256k1_zkp::rand;

use crate::components::copy_to_clipboard::CopyToClipboard;

#[derive(Copy, Clone, Debug)]
pub struct SigningKeys {
pub key_offset: RwSignal<u32>,
pub key_count: RwSignal<u32>,
pub secret_keys: Signal<Vec<secp256k1_zkp::Keypair>>,
}

impl Default for SigningKeys {
fn default() -> Self {
Self::new(1)
Self::new(rand::random(), 1)
}
}

impl SigningKeys {
pub fn new(key_count: u32) -> Self {
let key_count = create_rw_signal(key_count);
pub fn new(key_offset: u32, key_count: u32) -> Self {
let secret_keys = Signal::derive(move || -> Vec<secp256k1_zkp::Keypair> {
let mut index = 0;
(0..key_count.get())
(0..key_count)
.map(|_| {
let (key, new_index) = new_key(index);
let (key, new_index) = new_key(key_offset, index);
index = new_index;
key
})
.collect()
});
Self {
key_count,
key_offset: create_rw_signal(key_offset),
key_count: create_rw_signal(key_count),
secret_keys,
}
}
Expand Down Expand Up @@ -82,11 +84,12 @@ impl SigningKeys {
}
}

fn new_key(start_index: u32) -> (secp256k1_zkp::Keypair, u32) {
fn new_key(start_offset: u32, start_index: u32) -> (secp256k1_zkp::Keypair, u32) {
let mut offset = 1;
loop {
let index = start_index + offset;
let mut secret_key_bytes = [0u8; 32];
secret_key_bytes[24..28].copy_from_slice(&start_offset.to_be_bytes());
secret_key_bytes[28..].copy_from_slice(&index.to_be_bytes());
match secp256k1_zkp::Keypair::from_seckey_slice(secp256k1_zkp::SECP256K1, &secret_key_bytes)
{
Expand Down
11 changes: 7 additions & 4 deletions src/components/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ pub trait ToParams {

impl FromParams for SigningKeys {
fn from_map(map: &ParamsMap) -> Option<Self> {
map.get("keys")
.and_then(|s| s.parse::<u32>().ok())
.map(Self::new)
let key_offset = map.get("seed").and_then(|s| s.parse::<u32>().ok())?;
let key_count = map.get("keys").and_then(|s| s.parse::<u32>().ok())?;
Some(Self::new(key_offset, key_count))
}
}

impl ToParams for SigningKeys {
fn to_params(&self) -> impl Iterator<Item = (&'static str, String)> {
[("keys", self.key_count.get_untracked().to_string())].into_iter()
[
("seed", self.key_offset.get_untracked().to_string()),
("keys", self.key_count.get_untracked().to_string()),
].into_iter()
}
}

Expand Down

0 comments on commit 27f6181

Please sign in to comment.