TeachMeBitcoin

Custom Python Mnemonic Auditor

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python Mnemonic Auditor

In this final guide, we will build a Python script that implements the Entropy to Mnemonic conversion. We will generate a checksum from a random hex string and map it to words.

The Mnemonic Auditor

import hashlib

# Simplified wordlist for demonstration
# In a real auditor, this would contain all 2048 words
WORDLIST = [
    "abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract", 
    "absurd", "abuse", "access", "accident", "account", "accuse", "achieve", "acid",
    # ... (skipping to index 2047)
] + ["word"] * 2031 + ["zoo"]

def audit_mnemonic_generation(entropy_hex):
    print(f"--- BIP39 Mnemonic Audit ---")
    print(f"[*] Entropy Hex: {entropy_hex}")

    # 1. Convert hex to bits
    entropy_bytes = bytes.fromhex(entropy_hex)
    entropy_bits = bin(int.from_bytes(entropy_bytes, 'big'))[2:].zfill(len(entropy_bytes) * 8)

    # 2. Calculate Checksum (SHA256)
    hash_result = hashlib.sha256(entropy_bytes).digest()
    hash_bits = bin(int.from_bytes(hash_result, 'big'))[2:].zfill(256)

    # 128 bits entropy -> 4 bits checksum
    checksum_len = len(entropy_bytes) * 8 // 32
    checksum = hash_bits[:checksum_len]

    # 3. Combine
    total_bits = entropy_bits + checksum
    print(f"[*] Total Bits:  {len(total_bits)} (Entropy + {checksum_len} Checksum)")

    # 4. Split into 11-bit chunks and map to words
    words = []
    for i in range(0, len(total_bits), 11):
        index = int(total_bits[i:i+11], 2)
        # Using a fallback for the simplified wordlist
        word = WORDLIST[index] if index \u003c len(WORDLIST) else f"WORD_{index}"
        words.append(word)

    print(f"[SUCCESS] Mnemonic: {' '.join(words)}")

# --- Simulation ---

# Case 1: 128 bits of entropy (32 hex chars)
# This will result in 12 words
test_entropy = "00000000000000000000000000000000"
audit_mnemonic_generation(test_entropy)

How to Run the Auditor

  1. Ensure you have Python 3 installed.

  2. Copy the code into a file named mnemonic_auditor.py.

  3. Run it using python3 mnemonic_auditor.py.

Technical Takeaways

  1. Padding: The zfill function is critical. If your random number starts with zeroes, the binary representation must preserve those leading bits, or the mnemonic will be wrong.

  2. Checksum Power: The last word is the only one that "knows" about the others. If you change even one bit in the entropy, the checksum will change, and the last word will be different.

  3. Human Trust: BIP39 is the bridge between a machine's 256-bit reality and a human's 12-word reality. It is the single most important safety feature of the modern Bitcoin era.

Congratulations! You have completed the Mnemonic Phrases (BIP39 Standard) module. You now understand how a few words can secure a billion-dollar fortune.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!