Skip to content

Commit aeccaf6

Browse files
authored
Merge pull request #18 from tcharding/word-counts
Allow word count multiples of 3 instead of 6
2 parents 520e15a + af958a2 commit aeccaf6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl Mnemonic {
250250
where
251251
R: rand_core::RngCore + rand_core::CryptoRng,
252252
{
253-
if word_count < MIN_NB_WORDS || word_count % 6 != 0 || word_count > MAX_NB_WORDS {
253+
if is_invalid_word_count(word_count) {
254254
return Err(Error::BadWordCount(word_count));
255255
}
256256

@@ -377,7 +377,7 @@ impl Mnemonic {
377377
/// Parse a mnemonic in normalized UTF8 in the given language.
378378
pub fn parse_in_normalized(language: Language, s: &str) -> Result<Mnemonic, Error> {
379379
let nb_words = s.split_whitespace().count();
380-
if nb_words < MIN_NB_WORDS || nb_words % 6 != 0 || nb_words > MAX_NB_WORDS {
380+
if is_invalid_word_count(nb_words) {
381381
return Err(Error::BadWordCount(nb_words));
382382
}
383383

@@ -558,6 +558,10 @@ impl str::FromStr for Mnemonic {
558558
}
559559
}
560560

561+
fn is_invalid_word_count(word_count: usize) -> bool {
562+
word_count < MIN_NB_WORDS || word_count % 3 != 0 || word_count > MAX_NB_WORDS
563+
}
564+
561565
#[cfg(test)]
562566
mod tests {
563567
use super::*;
@@ -605,6 +609,14 @@ mod tests {
605609
let _ = Mnemonic::generate_in_with(&mut rand::thread_rng(), Language::English, 24).unwrap();
606610
}
607611

612+
#[cfg(feature = "rand")]
613+
#[test]
614+
fn test_generate_word_counts() {
615+
for word_count in [12, 15, 18, 21, 24].iter() {
616+
let _ = Mnemonic::generate(*word_count).unwrap();
617+
}
618+
}
619+
608620
#[test]
609621
fn test_vectors_english() {
610622
// These vectors are tuples of

0 commit comments

Comments
 (0)