bech32

Pure Haskell bech32, bech32m encodings (docs.ppad.tech/bech32).
git clone git://git.ppad.tech/bech32.git
Log | Files | Refs | README | LICENSE

Main.hs (1733B)


      1 {-# OPTIONS_GHC -fno-warn-orphans #-}
      2 {-# LANGUAGE DeriveGeneric #-}
      3 {-# LANGUAGE OverloadedStrings #-}
      4 {-# LANGUAGE StandaloneDeriving #-}
      5 
      6 module Main where
      7 
      8 import Criterion.Main
      9 import qualified Data.ByteString as BS
     10 import qualified Data.ByteString.Base32 as Base32
     11 import qualified Data.ByteString.Bech32 as Bech32
     12 import GHC.Generics
     13 import qualified Reference.Bech32 as R
     14 import Control.DeepSeq
     15 
     16 deriving instance Generic R.Word5
     17 instance NFData R.Word5
     18 
     19 main :: IO ()
     20 main = defaultMain [
     21     suite
     22   ]
     23 
     24 base32 :: Benchmark
     25 base32 = bgroup "base32 encode" [
     26     bench "120b" $ nf Base32.encode "jtobin was here"
     27   , bench "128b (non 40-bit multiple length)" $
     28       nf Base32.encode "jtobin was here!"
     29   , bench "240b" $ nf Base32.encode "jtobin was herejtobin was here"
     30   ]
     31 
     32 bech32 :: Benchmark
     33 bech32 = bgroup "bech32 encode" [
     34     bench "120b" $ nf (Bech32.encode "bc") "jtobin was here"
     35   , bench "128b (non 40-bit multiple length)" $
     36       nf (Bech32.encode "bc") "jtobin was here!"
     37   , bench "240b" $ nf (Bech32.encode "bc") "jtobin was herejtobin was here"
     38   ]
     39 
     40 suite :: Benchmark
     41 suite = env setup $ \ ~(a, b, c) -> bgroup "benchmarks" [
     42       bgroup "ppad-bech32" [
     43           base32
     44         , bech32
     45       ]
     46     , bgroup "reference" [
     47         bgroup "bech32" [
     48             bench "120b" $ nf (R.bech32Encode "bc") a
     49           , bench "128b (non 40-bit multiple length)" $
     50               nf (R.bech32Encode "bc") b
     51           , bench "240b" $ nf (R.bech32Encode "bc") c
     52         ]
     53       ]
     54     ]
     55   where
     56     setup = do
     57       let a = R.toBase32 (BS.unpack "jtobin was here")
     58           b = R.toBase32 (BS.unpack "jtobin was here!")
     59           c = R.toBase32 (BS.unpack "jtobin was herejtobin was here")
     60       pure (a, b, c)