packages feed

ppad-bip39-0.4.0: test/Properties.hs

{-# LANGUAGE OverloadedStrings #-}

module Properties (
    properties
  ) where

import qualified Crypto.Hash.SHA256 as SHA256
import qualified Crypto.KDF.BIP39 as BIP39
import Data.Bits (testBit)
import qualified Data.ByteString as BS
import qualified Data.Text as T
import Test.Tasty
import Test.Tasty.HUnit
import qualified Test.Tasty.QuickCheck as Q

-- entropy of a valid length
newtype Entropy = Entropy BS.ByteString
  deriving Show

instance Q.Arbitrary Entropy where
  arbitrary = do
    n <- Q.elements [16, 20, 24, 28, 32]
    Entropy . BS.pack <$> Q.vectorOf n Q.arbitrary

-- element i of a list, if any
at :: [a] -> Int -> Maybe a
at xs i = case drop i xs of
  (x : _) -> Just x
  []      -> Nothing

-- the mnemonic a wordlist encodes entropy as, per BIP39: the entropy
-- and the top bits of its SHA-256, in 11-bit groups, as words
reference :: [T.Text] -> BS.ByteString -> Maybe T.Text
reference ws e = T.intercalate " " <$> traverse (at ws) (groups bits)
  where
    n = BS.length e `quot` 4
    bits = concatMap byte (BS.unpack e)
        ++ take n (concatMap byte (BS.unpack (SHA256.hash e)))
    byte b = [ testBit b i | i <- [ 7, 6 .. 0 ] ]
    groups [] = []
    groups bs =
      let (a, r) = splitAt 11 bs
      in  foldl (\acc x -> acc * 2 + fromEnum x) 0 a : groups r

-- the validity of a phrase: a valid word count, every word listed
reference_valid :: [T.Text] -> T.Text -> Bool
reference_valid ws p =
  let tw = T.words p
  in  length tw `elem` [12, 15, 18, 21, 24] && all (`elem` ws) tw

-- a phrase of listed words, at a valid count or not, under various
-- separators, perhaps with one word corrupted
phrase :: [T.Text] -> Q.Gen T.Text
phrase ws = do
  c <- Q.frequency
    [ (4, Q.elements [12, 15, 18, 21, 24]), (1, Q.choose (0, 30)) ]
  is <- Q.vectorOf c (Q.choose (0, 2047))
  bad <- Q.frequency [ (3, pure Nothing), (1, Just <$> Q.choose (0, c - 1)) ]
  let sep = Q.elements [ " ", "  ", "\t", "\n", "\r\n", "\x3000", " \x3000 " ]
      end = Q.frequency [ (3, pure ""), (1, sep) ]
      corrupt w = Q.elements [ w <> "x", T.take 1 w, "zzzz", w <> w, "" ]
  ws' <- sequence
    [ case at ws i of
        Nothing -> pure ""
        Just w
          | Just k == bad -> corrupt w
          | otherwise -> pure w
    | (k, i) <- zip [0 ..] is ]
  seps <- Q.vectorOf (max 0 (c - 1)) sep
  pre <- end
  post <- end
  pure (pre <> T.concat (interleave ws' seps) <> post)
  where
    interleave (a : as) (b : bs) = a : b : interleave as bs
    interleave as [] = as
    interleave [] _ = []

properties :: TestTree
properties = testGroup "properties" [
    testGroup "mnemonic matches the reference" [
      mnemonic_matches "english" BIP39.english
    , mnemonic_matches "japanese" BIP39.japanese
    , mnemonic_matches "korean" BIP39.korean
    , mnemonic_matches "spanish" BIP39.spanish
    ]
  , testGroup "_valid matches the reference" [
      valid_matches "english" BIP39.english
    , valid_matches "japanese" BIP39.japanese
    , valid_matches "korean" BIP39.korean
    , valid_matches "french" BIP39.french
    , valid_matches "chinese_simplified" BIP39.chinese_simplified
    ]
  , testCase "empty and blank phrases are invalid" $ do
      assertBool mempty (not (BIP39.valid ""))
      assertBool mempty (not (BIP39.valid " \t\n\x3000 "))
  ]
  where
    mnemonic_matches name wl =
      let ws = BIP39._words wl
      in  Q.testProperty name $ \(Entropy e) ->
            BIP39._mnemonic wl e == reference ws e
    valid_matches name wl =
      let ws = BIP39._words wl
      in  Q.testProperty name $ Q.checkCoverage $
            Q.forAll (phrase ws) $ \p ->
              let r = reference_valid ws p
              in  Q.cover 20 r "valid" $ Q.cover 20 (not r) "invalid" $
                    BIP39._valid wl p == r