Skip to content

Conversation

@coreyphillips
Copy link
Contributor

@coreyphillips coreyphillips commented Nov 10, 2025

Summary

This PR introduces support for generating BIP39 mnemonics with configurable word counts - 12, 15, 18, 21, or 24 words.
The word_count parameter is optional and defaults to 24 words (256-bit entropy).

Motivation

Different use cases benefit from different mnemonic lengths:

  • 12 words: Simplified UX and easier memorization for users (default in Bitkit)
  • Supporting configurable lengths increases flexibility for downstream applications integrating with this library.

Changes

  • Added WordCount enum (Words12, Words15, Words18, Words21, Words24)
  • Updated generate_entropy_mnemonic() to accept an optional word_count parameter
  • Default: 24 words when no parameter is provided
  • Updated UDL bindings to expose the new enum and function signature
  • Updated tests to cover all variants and validate entropy mapping

Breaking Changes

⚠️ This is a breaking change - generate_entropy_mnemonic() now requires a word_count parameter.

Users will need to update their code from:

let mnemonic = generate_entropy_mnemonic();

To:

let mnemonic = generate_entropy_mnemonic(None);  // Uses default 24 words
// Or specify a word count:
let mnemonic = generate_entropy_mnemonic(Some(WordCount::Words12));

Testing

  • Verified default behavior (24 words)
  • Added tests for all word count variants
  • All tests pass (cargo test)

Summary was co-created with AI

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Nov 10, 2025

I've assigned @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think it makes sense to add this option.

Note that we appreciate human-written descriptions and when AI agent co-authorship is disclosed in the commit message.

src/io/utils.rs Outdated
let mut entropy = [0; 32];
pub fn generate_entropy_mnemonic(word_count: Option<MnemonicWordCount>) -> Mnemonic {
let word_count = word_count.unwrap_or(MnemonicWordCount::Words24);
let entropy_bytes = word_count.entropy_bytes();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, rather than dealing with different entropy byte lengths here, can we just use Mnemonic::generate which allows us to specify the word count? Having an enum for it still makes sense (actually we might want to switch to that in the bip39 API rust-bitcoin/rust-bip39#97).

Copy link
Contributor Author

@coreyphillips coreyphillips Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest commit here passes the WordCount directly to Mnemonic::generate() as suggested. However, I'm not sure how you may feel about the function name generate_entropy_mnemonic after this change. I've left it as is for now.

I've also created a PR adding the enum WordCount to rust-bip39 here.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please squash the second commit so we can land this.

Support generating BIP39 mnemonics with configurable word counts (12, 15, 18, 21, 24).
Defaults to 24 words (256-bit entropy) for backward compatibility.

- Add WordCount enum (12–24 variants)
- Update generate_entropy_mnemonic to accept optional word_count
- Remove need for entropy_bytes in generate_entropy_mnemonic by passing WordCount enum directly to generate() instead
- Add rand feature to bip39 dependency
- Extend tests for all word count options and defaults
- Expose enum and updated function in UDL bindings
@coreyphillips coreyphillips force-pushed the feat/configurable-mnemonic-word-count branch from 0bf8721 to 0a860d4 Compare November 18, 2025 15:32
@coreyphillips
Copy link
Contributor Author

Squashed as requested.

@tnull tnull merged commit 3034614 into lightningdevkit:main Nov 18, 2025
9 of 17 checks passed
@coreyphillips coreyphillips deleted the feat/configurable-mnemonic-word-count branch November 18, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants