Main.hs (3406B)
1 {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} 2 {-# LANGUAGE BangPatterns #-} 3 {-# LANGUAGE OverloadedStrings #-} 4 5 module Main where 6 7 import Control.DeepSeq 8 import Criterion.Main 9 import qualified Data.ByteString as BS 10 import qualified Lightning.Protocol.BOLT8.Internal as BOLT8 11 12 instance NFData BOLT8.Pub where 13 rnf p = rnf (BOLT8.serialize_pub p) 14 15 instance NFData BOLT8.Sec 16 instance NFData BOLT8.Error 17 instance NFData BOLT8.Key32 18 instance NFData BOLT8.SessionNonce 19 instance NFData BOLT8.Session 20 instance NFData BOLT8.HandshakeState 21 instance NFData BOLT8.Handshake 22 instance NFData (BOLT8.HandshakeFor a) where 23 rnf (BOLT8.HandshakeFor s) = rnf s 24 25 main :: IO () 26 main = defaultMain [ 27 keys 28 , handshake 29 , messages 30 ] 31 32 -- test keys (from BOLT #8 spec) 33 i_s_ent, i_e_ent, r_s_ent, r_e_ent :: BS.ByteString 34 i_s_ent = BS.replicate 32 0x11 35 i_e_ent = BS.replicate 32 0x12 36 r_s_ent = BS.replicate 32 0x21 37 r_e_ent = BS.replicate 32 0x22 38 39 keys :: Benchmark 40 keys = bgroup "keys" [ 41 bench "keypair" $ nf BOLT8.keypair i_s_ent 42 , bench "parse_pub" $ nf BOLT8.parse_pub r_s_pub_bs 43 , bench "serialize_pub" $ 44 nf BOLT8.serialize_pub r_s_pub 45 ] 46 where 47 Just (_, r_s_pub) = BOLT8.keypair r_s_ent 48 r_s_pub_bs = BOLT8.serialize_pub r_s_pub 49 50 handshake :: Benchmark 51 handshake = env setup $ 52 \ ~(i_s_sec, i_s_pub, r_s_sec, r_s_pub, 53 msg1, i_hs, msg2, r_hs, msg3) -> 54 bgroup "handshake" [ 55 bench "act1" $ 56 nf (BOLT8.act1 i_s_sec i_s_pub r_s_pub) i_e_ent 57 , bench "act2" $ 58 nf (BOLT8.act2 r_s_sec r_s_pub r_e_ent) msg1 59 , bench "act3" $ 60 nf (BOLT8.act3 i_hs) msg2 61 , bench "finalize" $ 62 nf (BOLT8.finalize r_hs) msg3 63 ] 64 where 65 setup = do 66 let Just (!i_s_sec, !i_s_pub) = 67 BOLT8.keypair i_s_ent 68 Just (!r_s_sec, !r_s_pub) = 69 BOLT8.keypair r_s_ent 70 Right (!msg1, !i_hs) = 71 BOLT8.act1 i_s_sec i_s_pub r_s_pub i_e_ent 72 Right (!msg2, !r_hs) = 73 BOLT8.act2 r_s_sec r_s_pub r_e_ent msg1 74 Right (!msg3, _) = BOLT8.act3 i_hs msg2 75 pure ( i_s_sec, i_s_pub, r_s_sec, r_s_pub 76 , msg1, i_hs, msg2, r_hs, msg3 ) 77 78 messages :: Benchmark 79 messages = env setup $ 80 \ ~(i_sess, r_sess, ct_small, ct_large) -> 81 bgroup "messages" [ 82 bench "encrypt (32B)" $ 83 nf (BOLT8.encrypt i_sess) small_msg 84 , bench "encrypt (1KB)" $ 85 nf (BOLT8.encrypt i_sess) large_msg 86 , bench "decrypt (32B)" $ 87 nf (BOLT8.decrypt r_sess) ct_small 88 , bench "decrypt (1KB)" $ 89 nf (BOLT8.decrypt r_sess) ct_large 90 ] 91 where 92 small_msg = BS.replicate 32 0x00 93 large_msg = BS.replicate 1024 0x00 94 setup = do 95 let Just (!i_s_sec, !i_s_pub) = 96 BOLT8.keypair i_s_ent 97 Just (!r_s_sec, !r_s_pub) = 98 BOLT8.keypair r_s_ent 99 Right (msg1, i_hs) = 100 BOLT8.act1 i_s_sec i_s_pub r_s_pub i_e_ent 101 Right (msg2, r_hs) = 102 BOLT8.act2 r_s_sec r_s_pub r_e_ent msg1 103 Right (msg3, i_result) = 104 BOLT8.act3 i_hs msg2 105 Right r_result = 106 BOLT8.finalize r_hs msg3 107 !i_sess = BOLT8.session i_result 108 !r_sess = BOLT8.session r_result 109 Right (!ct_small, _) = 110 BOLT8.encrypt i_sess small_msg 111 Right (!ct_large, _) = 112 BOLT8.encrypt i_sess large_msg 113 pure (i_sess, r_sess, ct_small, ct_large)