Utility Functions

Cryptographic utility functions provide essential supporting functionality for key management, wallet operations, and protocol implementation.

Available Utility Functions

Mnemonic and Key Management

BIP39

Bitcoin Improvement Proposal 39 for mnemonic phrase generation and seed derivation.

  • Mnemonic Length: 12, 15, 18, 21, or 24 words
  • Entropy: 128-256 bits
  • Languages: Multiple language support
  • Use Cases: Cryptocurrency wallets, key backup, seed phrases

Algorithm Features

BIP39 Characteristics

  • Human-Readable: Easy to write down and remember
  • Error Detection: Built-in checksum for error detection
  • Standardized: Widely adopted across cryptocurrency ecosystem
  • Deterministic: Same entropy produces same mnemonic
  • Reversible: Mnemonic can be converted back to seed

Security Properties

Entropy and Security

| Mnemonic Length | Entropy Bits | Security Level | Checksum Bits | |—————–|————–|—————-|—————| | 12 words | 128 | 128-bit | 4 | | 15 words | 160 | 160-bit | 5 | | 18 words | 192 | 192-bit | 6 | | 21 words | 224 | 224-bit | 7 | | 24 words | 256 | 256-bit | 8 |

Cryptographic Properties

  • Entropy Source: High-quality randomness required
  • Key Derivation: PBKDF2 with 2048 iterations
  • Passphrase: Optional additional security layer
  • Deterministic: Reproducible key generation

Use Cases

Cryptocurrency Wallets

# Generate wallet mnemonic
bip39 = BIP39()
mnemonic = bip39.generate_mnemonic(strength=256)  # 24 words
print(f"Mnemonic: {mnemonic}")

# Derive seed from mnemonic
seed = bip39.mnemonic_to_seed(mnemonic, passphrase="optional_passphrase")

Key Backup and Recovery

# Backup cryptographic keys as mnemonic
entropy = generate_high_entropy(256)  # 256 bits
mnemonic = bip39.entropy_to_mnemonic(entropy)

# Store mnemonic securely (paper, metal, etc.)
# Later recover the key
recovered_entropy = bip39.mnemonic_to_entropy(mnemonic)
assert entropy == recovered_entropy

Hierarchical Deterministic Wallets

# Generate master seed for HD wallet
mnemonic = bip39.generate_mnemonic()
master_seed = bip39.mnemonic_to_seed(mnemonic)

# Use with BIP32/BIP44 for key derivation
hd_wallet = HDWallet(master_seed)
account_key = hd_wallet.derive_account_key(0)

Implementation Considerations

Entropy Generation

  • Quality: Use cryptographically secure random sources
  • Quantity: Sufficient entropy for security level
  • Sources: Hardware RNG, OS entropy, multiple sources
  • Testing: Validate entropy quality

Mnemonic Handling

  • Display: Clear, readable presentation
  • Input: Robust parsing and validation
  • Storage: Secure temporary storage
  • Transmission: Avoid network transmission

Language Support

  • Wordlists: Official BIP39 wordlists for each language
  • Validation: Ensure words exist in wordlist
  • Normalization: Unicode normalization (NFKD)
  • Compatibility: Cross-language compatibility

Security Best Practices

Mnemonic Generation

  • Use high-quality entropy sources
  • Validate entropy before mnemonic generation
  • Never use predictable or low-entropy sources
  • Test mnemonic generation process

Mnemonic Storage

  • Physical: Paper, metal, secure storage
  • Digital: Encrypted storage only
  • Backup: Multiple secure copies
  • Access Control: Limit access to authorized users

Mnemonic Usage

  • Validation: Always validate mnemonic before use
  • Checksum: Verify checksum integrity
  • Passphrase: Consider additional passphrase protection
  • Recovery: Test recovery process regularly

Common Pitfalls

# DON'T: Use weak entropy
weak_entropy = b"password123" * 4  # Predictable!
# WRONG: Weak entropy leads to predictable mnemonics

# DO: Use cryptographically secure entropy
strong_entropy = os.urandom(32)  # 256 bits of entropy
mnemonic = bip39.entropy_to_mnemonic(strong_entropy)

# DON'T: Store mnemonic in plain text
# with open("mnemonic.txt", "w") as f:
#     f.write(mnemonic)  # WRONG: Plain text storage

# DO: Use secure storage or physical backup
# Write mnemonic on paper and store securely
# Or use encrypted digital storage with strong password

Integration Patterns

Wallet Applications

class SecureWallet:
    def __init__(self):
        self.bip39 = BIP39()
    
    def create_wallet(self, strength=256):
        """Create new wallet with mnemonic backup"""
        mnemonic = self.bip39.generate_mnemonic(strength)
        seed = self.bip39.mnemonic_to_seed(mnemonic)
        
        return {
            'mnemonic': mnemonic,
            'seed': seed,
            'instructions': 'Write down mnemonic and store securely'
        }
    
    def restore_wallet(self, mnemonic, passphrase=""):
        """Restore wallet from mnemonic"""
        if not self.bip39.validate_mnemonic(mnemonic):
            raise ValueError("Invalid mnemonic")
        
        seed = self.bip39.mnemonic_to_seed(mnemonic, passphrase)
        return {'seed': seed, 'status': 'restored'}

Key Management Systems

class MnemonicKeyManager:
    def __init__(self):
        self.bip39 = BIP39()
    
    def backup_key(self, key_material):
        """Convert key material to mnemonic backup"""
        if len(key_material) not in [16, 20, 24, 28, 32]:
            raise ValueError("Invalid key length for BIP39")
        
        mnemonic = self.bip39.entropy_to_mnemonic(key_material)
        return mnemonic
    
    def restore_key(self, mnemonic):
        """Restore key material from mnemonic"""
        return self.bip39.mnemonic_to_entropy(mnemonic)

Standards Compliance

BIP39 Specification

  • Entropy: 128-256 bits in 32-bit increments
  • Checksum: Entropy length / 32 bits
  • Wordlist: 2048 words per language
  • PBKDF2: 2048 iterations with “mnemonic” + passphrase
  • BIP32: Hierarchical Deterministic Wallets
  • BIP44: Multi-Account Hierarchy for Deterministic Wallets
  • BIP49: Derivation scheme for P2WPKH-nested-in-P2SH
  • BIP84: Derivation scheme for P2WPKH

Language Support

Available Languages

  • English (primary)
  • Japanese
  • Chinese (Simplified)
  • Chinese (Traditional)
  • French
  • Italian
  • Korean
  • Spanish
  • Czech

Implementation Notes

  • Each language has exactly 2048 words
  • Words are chosen to avoid ambiguity
  • Unicode normalization required
  • Cross-language compatibility considerations

Testing and Validation

Test Vectors

  • Official BIP39 test vectors
  • Cross-implementation compatibility
  • Edge case testing
  • Error condition handling

Quality Assurance

  • Entropy quality validation
  • Mnemonic generation testing
  • Seed derivation verification
  • Cross-platform compatibility