keyfork-mnemonic-from-seed: trim unnecessary

This commit is contained in:
Ryan Heywood 2023-10-02 22:04:05 -05:00
parent 4e83be47c0
commit de6d8e4b56
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
8 changed files with 44 additions and 3570 deletions

3
Cargo.lock generated
View File

@ -865,9 +865,8 @@ name = "keyfork"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"keyfork-derive-key",
"keyfork-entropy", "keyfork-entropy",
"keyfork-mnemonic-from-seed", "keyfork-mnemonic-util",
"smex", "smex",
"thiserror", "thiserror",
] ]

View File

@ -1,49 +0,0 @@
use keyfork_mnemonic_util::{Mnemonic, MnemonicGenerationError, Wordlist};
pub fn generate_mnemonic(entropy: &[u8]) -> Result<Mnemonic, MnemonicGenerationError> {
let wordlist = Wordlist::default().arc();
Mnemonic::from_entropy(entropy, wordlist)
}
#[cfg(test)]
mod tests {
use keyfork_mnemonic_util::{Mnemonic, Wordlist};
use std::{collections::HashSet, io::Read};
#[test]
fn count_to_get_duplicate_words() {
let tests = 100_000;
let mut count = 0.;
let entropy = &mut [0u8; 256 / 8];
let wordlist = Wordlist::default().arc();
let mut random = std::fs::File::open("/dev/urandom").unwrap();
let mut hs = HashSet::<usize>::with_capacity(24);
for _ in 0..tests {
random.read_exact(&mut entropy[..]).unwrap();
let mnemonic = Mnemonic::from_entropy(&entropy[..256 / 8], wordlist.clone()).unwrap();
let (words, _) = mnemonic.into_inner();
hs.clear();
hs.extend(words);
if hs.len() != 24 {
count += 1.;
}
}
// NOTE: Birthday problem math is: 0.126532
// Set values to (about) 1 below, 1 above
// Source: https://en.wikipedia.org/wiki/Birthday_problem
let min = 11.5;
let max = 13.5;
assert!(
count > f64::from(tests) * min / 100.,
"{count} probability should be more than {min}%: {}",
count / f64::from(tests)
);
assert!(
count < f64::from(tests) * max / 100.,
"{count} probability should be more than {max}%: {}",
count / f64::from(tests)
);
}
}

View File

@ -1,5 +1,4 @@
#[allow(clippy::wildcard_imports)] use keyfork_mnemonic_util::Mnemonic;
use keyfork_mnemonic_from_seed::*;
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = std::io::stdin(); let input = std::io::stdin();
@ -7,7 +6,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
input.read_line(&mut line)?; input.read_line(&mut line)?;
let decoded = smex::decode(line.trim())?; let decoded = smex::decode(line.trim())?;
let mnemonic = generate_mnemonic(&decoded)?; let mnemonic = Mnemonic::from_entropy(&decoded, Default::default())?;
println!("{mnemonic}"); println!("{mnemonic}");

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -225,7 +225,7 @@ impl Mnemonic {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs::File, io::Read}; use std::{collections::HashSet, fs::File, io::Read};
use super::*; use super::*;
@ -278,4 +278,41 @@ mod tests {
let their_mnemonic = bip39::Mnemonic::from_entropy(&entropy[..256 / 8]).unwrap(); let their_mnemonic = bip39::Mnemonic::from_entropy(&entropy[..256 / 8]).unwrap();
assert_eq!(my_mnemonic.to_string(), their_mnemonic.to_string()); assert_eq!(my_mnemonic.to_string(), their_mnemonic.to_string());
} }
#[test]
fn count_to_get_duplicate_words() {
let tests = 100_000;
let mut count = 0.;
let entropy = &mut [0u8; 256 / 8];
let wordlist = Wordlist::default().arc();
let mut random = std::fs::File::open("/dev/urandom").unwrap();
let mut hs = HashSet::<usize>::with_capacity(24);
for _ in 0..tests {
random.read_exact(&mut entropy[..]).unwrap();
let mnemonic = Mnemonic::from_entropy(&entropy[..256 / 8], wordlist.clone()).unwrap();
let (words, _) = mnemonic.into_inner();
hs.clear();
hs.extend(words);
if hs.len() != 24 {
count += 1.;
}
}
// NOTE: Birthday problem math is: 0.126532
// Set values to (about) 1 below, 1 above
// Source: https://en.wikipedia.org/wiki/Birthday_problem
let min = 11.5;
let max = 13.5;
assert!(
count > f64::from(tests) * min / 100.,
"{count} probability should be more than {min}%: {}",
count / f64::from(tests)
);
assert!(
count < f64::from(tests) * max / 100.,
"{count} probability should be more than {max}%: {}",
count / f64::from(tests)
);
}
} }

View File

@ -6,9 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
keyfork-entropy = { version = "0.1.0", path = "../keyfork-entropy" }
keyfork-mnemonic-util = { version = "0.1.0", path = "../keyfork-mnemonic-util" }
clap = { version = "4.4.2", features = ["derive", "env"] } clap = { version = "4.4.2", features = ["derive", "env"] }
keyfork-mnemonic-from-seed = { version = "0.1.0", path = "../keyfork-mnemonic-from-seed" }
keyfork-derive-key = { version = "0.1.0", path = "../keyfork-derive-key" }
thiserror = "1.0.48" thiserror = "1.0.48"
smex = { version = "0.1.0", path = "../smex" } smex = { version = "0.1.0", path = "../smex" }
keyfork-entropy = { version = "0.1.0", path = "../keyfork-entropy" }

View File

@ -96,7 +96,7 @@ impl MnemonicSeedSource {
MnemonicSeedSource::Tarot => todo!(), MnemonicSeedSource::Tarot => todo!(),
MnemonicSeedSource::Dice => todo!(), MnemonicSeedSource::Dice => todo!(),
}; };
let mnemonic = keyfork_mnemonic_from_seed::generate_mnemonic(&seed)?; let mnemonic = keyfork_mnemonic_util::Mnemonic::from_entropy(&seed, Default::default())?;
Ok(mnemonic.to_string()) Ok(mnemonic.to_string())
} }
} }