From bfb44292f42455de960ff16405a02d6898a28266 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 21 Dec 2023 15:02:59 -0500 Subject: [PATCH] keyfork-mnemonic-util: allow seeds of arbitrary size --- keyfork-mnemonic-util/src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/keyfork-mnemonic-util/src/lib.rs b/keyfork-mnemonic-util/src/lib.rs index 6534fa4..9dc9e00 100644 --- a/keyfork-mnemonic-util/src/lib.rs +++ b/keyfork-mnemonic-util/src/lib.rs @@ -170,6 +170,11 @@ impl Mnemonic { return Err(MnemonicGenerationError::InvalidByteLength(bit_count)); } + Ok(unsafe {Self::from_raw_entropy(bytes, wordlist)}) + } + + pub unsafe fn from_raw_entropy(bytes: &[u8], wordlist: Arc) -> Mnemonic { + let bit_count = bytes.len() * 8; let mut bits = vec![false; bit_count + bit_count / 32]; for byte_index in 0..bit_count / 8 { @@ -197,7 +202,7 @@ impl Mnemonic { }) .collect::>(); - Ok(Mnemonic { words, wordlist }) + Mnemonic { words, wordlist } } pub fn entropy(&self) -> Vec { @@ -342,4 +347,15 @@ mod tests { count / f64::from(tests) ); } + + #[test] + fn can_do_up_to_1024_bits() { + let entropy = &mut [0u8; 128]; + let wordlist = Wordlist::default().arc(); + let mut random = std::fs::File::open("/dev/urandom").unwrap(); + random.read_exact(&mut entropy[..]).unwrap(); + let mnemonic = unsafe { Mnemonic::from_raw_entropy(&entropy[..], wordlist.clone()) }; + let (words, _) = mnemonic.into_inner(); + assert!(words.len() == 96); + } }