libcrypto.mnemonic

BIP39 mnemonic phrase generation, validation, and seed derivation. The bundled English 2048-word list is the canonical BIP39 wordlist.

Functions

generate_mnemonic(word_count=12) -> str

Generate a cryptographically secure mnemonic. Valid word counts: 12, 15, 18, 21, 24. Raises ValueError for other counts.

validate_mnemonic(mnemonic) -> bool

Return True when the phrase has a valid length, known words, and a correct checksum.

mnemonic_to_entropy(mnemonic) -> bytes

Recover the original entropy, verifying the checksum. Raises InvalidMnemonicError on any failure.

entropy_to_mnemonic(entropy) -> str

Convert entropy (bytes or hex str) to a mnemonic phrase. Raises InvalidEntropyError for invalid entropy lengths.

mnemonic_to_seed(mnemonic, passphrase="") -> bytes

Derive the 64-byte BIP39 seed using PBKDF2-HMAC-SHA512 (2048 iterations). The mnemonic is NFKD-normalized before derivation.

Exceptions

Exception Meaning
InvalidMnemonicError Bad length, unknown word, or checksum failure.
InvalidEntropyError Invalid entropy length or hex.

Example

from libcrypto import generate_mnemonic, validate_mnemonic, mnemonic_to_seed

phrase = generate_mnemonic(12)
assert validate_mnemonic(phrase)

seed = mnemonic_to_seed(phrase, passphrase="optional")
print(len(seed))  # 64

Standard test vector

from libcrypto import mnemonic_to_seed

phrase = "abandon " * 11 + "about"
seed = mnemonic_to_seed(phrase.strip(), passphrase="TREZOR")
assert seed.hex().startswith("c55257c360c07c72029aebc1b53c05ed")