HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulecardano-crypto-1.3.0Haskell2010

Cardano.Crypto.Encoding.Seed

implementation of the proposal specification for Paper Wallet see https://github.com/input-output-hk/cardano-specs/blob/master/proposals/0001-PaperWallet.md

however we allow more genericity in the implementation to allow not only 12 mnemonic words to freeze but also 15, 18 and 21.

because the output mnemonic words are always 3 words longer (for the IV) we cannot use 24 words long mnemonic sentence.

assumption:

  • we use PBKDF2 with 'HMAC 512' to generate the OTP.

  • we use 10000 iteration for the PBKDF2

  • we use the 4 bytes IOHK for the CONSTANT

  • 7 types
  • 4 values
datadata Entropy (n :: Nat)
#

BIP39's entropy is a byte array of a given size (in bits, see ValidEntropySize for the valid size).

To it is associated

Instances9Eq, Show, NormalForm, Arbitrary, …
  • Eq (Entropy n)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Show (Entropy n)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • NormalForm (Entropy n)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Arbitrary (Entropy 128)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Arbitrary (Entropy 160)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Arbitrary (Entropy 192)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Arbitrary (Entropy 224)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Arbitrary (Entropy 256)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
  • Arbitrary (Entropy 96)Defined in cardano-crypto-1.3.0 · Crypto.Encoding.BIP39
newtypenewtype MnemonicSentence (mw :: Nat)
#

Mnemonic Sentence is a list of WordIndex.

This is the generic representation of a mnemonic phrase that can be used for transalating to a different dictionary (example: English to Japanese).

This is mainly used to convert from/to the Entropy and for cardanoSlSeed

Instances6IsList, Eq, Ord, Show, NormalForm, Item
typetype ConsistentEntropy (ent :: Nat) (mw :: Nat) (csz :: Nat) = (ValidEntropySize ent, ValidChecksumSize ent csz, ValidMnemonicSentence mw, MnemonicWords ent ~ mw)
#

Type Constraint Alias to check the entropy size, the number of mnemonic words and the checksum size is consistent. i.e. that the following is true:

| entropysize | checksumsize | entropysize + checksumsize | mnemonicsize | +---------------+--------------+----------------------------+--------------+ | 96 | 3 | 99 | 9 | | 128 | 4 | 132 | 12 | | 160 | 5 | 165 | 15 | | 192 | 6 | 198 | 18 | | 224 | 7 | 231 | 21 | | 256 | 8 | 264 | 24 |

This type constraint alias also perform all the GHC's cumbersome type level literal handling.

newtypenewtype ScrambleIV
#
Instances5Eq, Ord, Show, ByteArrayAccess, Arbitrary
valuescramble
  1. :: (ConsistentEntropy entropysizeI mnemonicsize csI, ConsistentEntropy entropysizeO scramblesize csO, (mnemonicsize + IVSizeWords) ~ scramblesize, (entropysizeI + IVSizeBits) ~ entropysizeO)
  2. => ScrambleIV
  3. -> Entropy entropysizeI
  4. -> Passphrase
  5. -> Entropy entropysizeO
#

scramble the given entropy into an entropy slighly larger.

This entropy can then be converted to a mnemonic sentence:

freeze iv mnemonics passphrase = entropyToWords . scramble iv entropy passphrase
  where
    entropy = case wordsToEntropy mnemonics of
        Nothing -> error "mnemonic to entropy failed"
        Just e  -> e
valueunscramble
  1. :: (ConsistentEntropy entropysizeI scramblesize csI, ConsistentEntropy entropysizeO mnemonicsize csO, (mnemonicsize + IVSizeWords) ~ scramblesize, (entropysizeO + IVSizeBits) ~ entropysizeI)
  2. => Entropy entropysizeI
  3. -> Passphrase
  4. -> Entropy entropysizeO
#

The reverse operation of scramble

This function recovers the original entropy from the given scrambled entropy and the associated password.

recover scrambled passphrase = entropyToWords entropysizeO .
    unscramble entropysizeI entropysizeO entropyScrambled passphrase
  where
    entropyScrambled = case wordsToEntropy entropysizeI scrambled of
        Nothing -> error "mnemonic to entropy failed"
        Just e  -> e