Main.hs (2496B)
1 {-# LANGUAGE OverloadedStrings #-} 2 3 module Main where 4 5 import Control.DeepSeq 6 import Criterion.Main 7 import qualified Crypto.Curve.Secp256k1 as S 8 import qualified Crypto.Curve.Secp256k1.Internal as SI 9 import qualified Data.ByteString as BS 10 11 instance NFData S.Context 12 instance NFData S.KeyPair 13 instance NFData S.Pub 14 instance NFData S.Sig 15 instance NFData S.XOnlyPub 16 17 main :: IO () 18 main = defaultMain [ 19 suite 20 ] 21 22 suite :: Benchmark 23 suite = envWithCleanup setup destroy $ \ ~(tex, fen, pub, sig) -> 24 bgroup "csecp256k1" [ 25 bgroup "ecdsa" [ 26 bench "sign" . nfIO $ S.sign_ecdsa tex _SEC _HAS 27 , bench "verify" . nfIO $ S.verify_ecdsa tex pub _HAS sig 28 ] 29 , bgroup "schnorr" [ 30 bench "sign" . nfIO $ S.sign_schnorr tex _HAS _SEC fen 31 , bench "verify" . nfIO $ S.verify_schnorr tex pub _HAS _SIG_SCHNORR 32 ] 33 , bgroup "ecdh" [ 34 bench "ecdh" . nfIO $ S.ecdh tex pub _SEC 35 ] 36 ] 37 where 38 setup = do 39 ptr <- SI.secp256k1_context_create SI._SECP256K1_CONTEXT_NONE 40 pub <- SI.wcontext $ \tex -> S.parse_pub (S.Context tex) _PUB_COMPRESSED 41 sig <- SI.wcontext $ \tex -> S.parse_der (S.Context tex) _DER 42 pure (S.Context ptr, BS.replicate 32 0, pub, sig) 43 44 destroy (S.Context tex, _, _, _) = SI.secp256k1_context_destroy tex 45 46 -- inputs 47 48 -- a 32-byte message hash 49 _HAS :: BS.ByteString 50 _HAS = mconcat [ 51 "\245\203\231\216\129\130\164\184\228\NUL\249k\ACK\DC2\137!\134J" 52 , "\CAN\CAN}\DC1L\138\232T\ESCVl\138\206\NUL" 53 ] 54 55 -- a 32-byte secret key 56 _SEC :: BS.ByteString 57 _SEC = mconcat [ 58 "\246RU\tMws\237\141\212\ETB\186\220\159\192E\193\248\SI\220[-%\ETB" 59 , "+\ETX\FS\230\147>\ETX\154" 60 ] 61 62 -- 33-byte (compressed) public key 63 _PUB_COMPRESSED :: BS.ByteString 64 _PUB_COMPRESSED = mconcat [ 65 "\ETX\221\237B\ETX\218\201j~\133\242\195t\163|\227\233\201\161U" 66 , "\167+d\180U\ESC\v\254w\157\212G\ENQ" 67 ] 68 69 -- DER-encoded signature 70 _DER :: BS.ByteString 71 _DER = mconcat [ 72 "0E\STX!\NUL\245\STX\191\160z\244>~\242ea\139\r\146\154v\EM\238\SOH\214" 73 , "\NAK\SO7\235n\170\242\200\189\&7\251\"\STX o\EOT\NAK\171\SO\154\151z" 74 , "\253x\178\194n\243\155\&9R\tm1\159\212\177\SOH\199h\173l\DC3.0E" 75 ] 76 77 -- 64-byte schnorr signature 78 _SIG_SCHNORR :: BS.ByteString 79 _SIG_SCHNORR = mconcat [ 80 "\214\185AtJ\189\250Gp\NAK2\221\DC2[\182\209\192j{\140^\222R\NUL~" 81 , "\139d@<\138\163rh\247\152\r\228\175\236\219\156\151\214~\135\&7" 82 , "\225\&6\234\220;\164R\191\170\186\243\NAK\147\f\144\156ez" 83 ] 84