From 2c06b96953bce2b390d52756d885f26b3129552f Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 17 Aug 2023 21:45:15 -0500 Subject: [PATCH] keyfork-mnemonic-generate: add failing test to track birthday paradox --- keyfork-mnemonic-generate/src/main.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/keyfork-mnemonic-generate/src/main.rs b/keyfork-mnemonic-generate/src/main.rs index ec31d2c..de974ad 100644 --- a/keyfork-mnemonic-generate/src/main.rs +++ b/keyfork-mnemonic-generate/src/main.rs @@ -94,7 +94,7 @@ fn ensure_offline() { // TODO: Can a Mnemonic be formatted using a wordlist, without allocating or without storing the // entire word list? struct Mnemonic { - words: Vec, + pub words: Vec, wordlist: Vec, } @@ -232,4 +232,20 @@ mod tests { } Ok(()) } + + #[test] + fn count_to_get_duplicate_words() { + let mut count = 0.; + let mut random_handle = File::open("/dev/urandom").unwrap(); + let entropy = &mut [0u8; 256 / 8]; + for _ in 0..100_000 { + random_handle.read_exact(&mut entropy[..]).unwrap(); + let mut mnemonic = Mnemonic::from_entropy(&entropy[..256 / 8]).unwrap(); + mnemonic.words.dedup(); + if mnemonic.words.len() != 24 { + count += 1.; + } + } + panic!("counts: {count} {}", count / 100_000.) + } }