Skip to content
ppad-techPublic

About

BIP39 mnemonic codes in Haskell

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

72 Commits

Folders and files

Repository files navigation

bip39

An implementation of BIP39 mnemonics for deterministic key generation.

Usage

A sample GHCi session:

  > -- pragma and bip32/TIO imports not required; just for illustration
  > :set -XOverloadedStrings
  > import qualified Crypto.HDKey.BIP32 as BIP32
  > import qualified Data.Text.IO as TIO
  >
  > import Crypto.KDF.BIP39
  >
  > -- bring your own entropy, e.g. via System.Entropy
  > -- for now we'll use an ultra-secret source
  > let trop = "my ultra secret entropy!"
  >
  > -- use 'mnemonic' to create a BIP39 mnemonic
  > let Just mnem = mnemonic trop
  > mnem
  "hope simple bubble suggest elbow correct limb hole gloom nasty fringe dolphin finger demand situate unlock junior autumn"
  >
  > -- you can use other wordlists; the bip39 'defaults' are included
  > -- to supply an alternative one, use '_mnemonic' (with a leading underscore)
  > let fr_mnem = _mnemonic french trop
  > let jp_mnem = _mnemonic japanese trop
  >
  > TIO.putStrLn fr_mnem
  frivole reculer bilingue secouer diriger chose ingérer frémir fatal luxueux étonnant défiler éolien crayon réflexe tournage hélium appareil
  >
  > TIO.putStrLn jp_mnem
  ずあん はんらん えんえん へいしゃ けむし きかんしゃ そめる しんせいじ したて ちきゅう さとし けいたい こんだて ぐうせい ひかく もちろん せんげん いやがる
  >
  > -- use 'seed' to create a seed
  > let master_seed = seed mnem mempty
  >
  > -- the second argument is a passphrase, which can be arbitrary UTF-8
  > let phrase_seed = seed jp_mnem "㍍ガバヴァぱばぐゞちぢ十人十色"
  >
  > -- use a seed to derive a bip32 hd wallet
  > let hd_wallet = BIP32.master phrase_seed

Documentation

Haddocks (API documentation, etc.) are hosted at docs.ppad.tech/bip39.

Security

This library aims at the maximum security achievable in a garbage-collected language under an optimizing compiler such as GHC, in which strict constant-timeness can be challenging to achieve.

Mnemonic encoding ('mnemonic'), validation ('valid'), and seed derivation ('seed') under the default English wordlist execute in constant time on x86-64 and aarch64 under GHC's LLVM backend, except for the length of the mnemonic in bytes, which its words determine.

Seed derivation normalizes the mnemonic and passphrase to NFKD via text-icu, and ICU's normalization does not run in constant time on non-ASCII text. Seed derivation is thus constant-time only for ASCII mnemonics and passphrases, as English mnemonics are; under other wordlists, or with a non-ASCII passphrase, it may leak information about them through timing.

The implementation within passes the official BIP39 test vectors for every "default" wordlist, including the additional Japanese vectors with heavily-normalized passphrases.

If you discover any vulnerabilities, please disclose them via [email protected].

Performance

Current benchmark figures on an M4 Silicon MacBook Air look like (use cabal bench -f+llvm to run the benchmark suite):

  benchmarking ppad-bip39/mnemonic
  time                 43.10 μs   (43.01 μs .. 43.25 μs)
                       1.000 R²   (1.000 R² .. 1.000 R²)
  mean                 43.24 μs   (43.16 μs .. 43.35 μs)
  std dev              364.1 ns   (315.1 ns .. 463.8 ns)

  benchmarking ppad-bip39/valid
  time                 137.5 μs   (137.5 μs .. 137.6 μs)
                       1.000 R²   (1.000 R² .. 1.000 R²)
  mean                 138.0 μs   (137.9 μs .. 138.3 μs)
  std dev              796.4 ns   (482.6 ns .. 1.436 μs)

  benchmarking ppad-bip39/seed
  time                 925.0 μs   (924.3 μs .. 925.8 μs)
                       1.000 R²   (1.000 R² .. 1.000 R²)
  mean                 928.9 μs   (926.6 μs .. 934.5 μs)
  std dev              13.46 μs   (6.296 μs .. 27.38 μs)

Development

You'll require Nix with flake support enabled. Enter a development shell with:

$ nix develop

Then do e.g.:

$ cabal repl ppad-bip39

to get a REPL for the main library.

About

BIP39 mnemonic codes in Haskell

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages