File: bench.hs

package info (click to toggle)
haskell-ed25519 0.0.5.0-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: ansic: 3,905; haskell: 303; makefile: 4
file content (44 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# LANGUAGE CPP #-}
module Main
       ( main -- :: IO ()
       ) where

import Criterion.Main
import Crypto.Sign.Ed25519

import Data.Maybe (fromJust)
import Control.DeepSeq
import qualified Data.ByteString as B

--------------------------------------------------------------------------------

#if !MIN_VERSION_bytestring(0,10,0)
instance NFData B.ByteString
#endif

instance NFData SecretKey
instance NFData PublicKey

--------------------------------------------------------------------------------

main :: IO ()
main = do
  -- Don't use `createKeypair`, since that will incur a ton of calls
  -- to the OS to generate randomness. Simply generate a bogus Ed25519
  -- seed instead.
  let seed         = B.pack [0..31]
      keys@(pk,sk) = fromJust (createKeypairFromSeed_ seed)

      -- Generate a dummy message to sign, and a signature to verify
      -- against.
      dummy = B.pack [0..255]
      msg = sign sk dummy
  defaultMain
    [ bench "deterministic key generation"   $ nf createKeypairFromSeed_ seed
    , bench "signing a 256 byte message"     $ nf (sign sk)              dummy
    , bench "verifying a signature"          $ nf (verify pk)            msg
    , bench "roundtrip 256-byte sign/verify" $ nf (signBench keys)       dummy
    ]

signBench :: (PublicKey, SecretKey) -> B.ByteString -> Bool
signBench (pk, sk) xs = verify pk (sign sk xs)