keyfork-mnemonic-from-seed: trim unnecessary
This commit is contained in:
parent
4e83be47c0
commit
de6d8e4b56
|
@ -865,9 +865,8 @@ name = "keyfork"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"keyfork-derive-key",
|
||||
"keyfork-entropy",
|
||||
"keyfork-mnemonic-from-seed",
|
||||
"keyfork-mnemonic-util",
|
||||
"smex",
|
||||
"thiserror",
|
||||
]
|
||||
|
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use keyfork_mnemonic_from_seed::*;
|
||||
use keyfork_mnemonic_util::Mnemonic;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = std::io::stdin();
|
||||
|
@ -7,7 +6,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
input.read_line(&mut line)?;
|
||||
let decoded = smex::decode(line.trim())?;
|
||||
|
||||
let mnemonic = generate_mnemonic(&decoded)?;
|
||||
let mnemonic = Mnemonic::from_entropy(&decoded, Default::default())?;
|
||||
|
||||
println!("{mnemonic}");
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -225,7 +225,7 @@ impl Mnemonic {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{fs::File, io::Read};
|
||||
use std::{collections::HashSet, fs::File, io::Read};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
@ -278,4 +278,41 @@ mod tests {
|
|||
let their_mnemonic = bip39::Mnemonic::from_entropy(&entropy[..256 / 8]).unwrap();
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,9 +6,8 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[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"] }
|
||||
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"
|
||||
smex = { version = "0.1.0", path = "../smex" }
|
||||
keyfork-entropy = { version = "0.1.0", path = "../keyfork-entropy" }
|
||||
|
|
|
@ -96,7 +96,7 @@ impl MnemonicSeedSource {
|
|||
MnemonicSeedSource::Tarot => 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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue