Skip to Content
oxcrypto

BIP-39

Mnemonic phrases in all official wordlists and PBKDF2 seed derivation.

from oxcrypto.hd.bip39 import BIP39

mnemonic = BIP39.ENGLISH.load_entropy(bytes(16))
assert mnemonic.to_phrase() == "abandon " * 11 + "about"
assert BIP39.ENGLISH.load_phrase(mnemonic.to_phrase()).to_entropy() == bytes(16)
seed = mnemonic.to_seed(passphrase="TREZOR")
assert seed.hex().startswith("c55257c360c07c72")

A mnemonic encodes 16, 20, 24, 28 or 32 bytes of entropy as 12, 15, 18, 21 or 24 words, with a checksum of the first bits of its SHA-256 hash. generate draws the entropy from the operating system CSPRNG. load_phrase applies NFKD normalization, splits on whitespace, and raises ValueError for a wrong word count, a word outside the selected wordlist, or a failed checksum.

to_seed derives the 64-byte seed with PBKDF2-HMAC-SHA512, 2048 iterations, and the salt "mnemonic" + passphrase, both NFKD-normalized. Any passphrase gives a valid seed, so a mistyped passphrase is not detected. to_phrase joins Japanese words with U+3000 (ideographic space) and other languages with a space, as BIP-39 recommends.

Variants

  • BIP39.ENGLISH
  • BIP39.CHINESE_SIMPLIFIED
  • BIP39.CHINESE_TRADITIONAL
  • BIP39.CZECH
  • BIP39.FRENCH
  • BIP39.ITALIAN
  • BIP39.JAPANESE
  • BIP39.KOREAN
  • BIP39.PORTUGUESE
  • BIP39.SPANISH

Methods

BIP39.generate(*, words: int = 24) -> Mnemonic
BIP39.load_entropy(entropy: bytes, /) -> Mnemonic
BIP39.load_phrase(phrase: str, /) -> Mnemonic

Mnemonic.language -> BIP39
Mnemonic.words -> int
Mnemonic.to_entropy() -> bytes
Mnemonic.to_phrase() -> str
Mnemonic.to_seed(*, passphrase: str = "") -> bytes

Upstream

  • Standard: BIP-39 (Deployed).
  • Crate: coins-bip39 0.17.2 with the ten official BIP-39 wordlists.
  • Tests use the trezor/python-mnemonic reference vectors.json for every official wordlist and the bip32JP Japanese vectors with non-ASCII passphrases.

See Hierarchical deterministic keys for zeroization notes.

On this page