bolt8

Encrypted and authenticated transport, per BOLT #8 (docs.ppad.tech/bolt8).
git clone git://git.ppad.tech/bolt8.git
Log | Files | Refs | README | LICENSE

Weight.hs (2642B)


      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 qualified Data.ByteString as BS
      9 import qualified Lightning.Protocol.BOLT8 as BOLT8
     10 import Weigh
     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.Session
     18 instance NFData BOLT8.HandshakeState
     19 instance NFData BOLT8.Handshake
     20 
     21 -- note that 'weigh' doesn't work properly in a repl
     22 main :: IO ()
     23 main = mainWith $ do
     24   keys
     25   handshake
     26   messages
     27 
     28 -- test keys (from BOLT #8 spec)
     29 i_s_ent, i_e_ent, r_s_ent, r_e_ent :: BS.ByteString
     30 i_s_ent = BS.replicate 32 0x11
     31 i_e_ent = BS.replicate 32 0x12
     32 r_s_ent = BS.replicate 32 0x21
     33 r_e_ent = BS.replicate 32 0x22
     34 
     35 keys :: Weigh ()
     36 keys =
     37   let Just (_, !r_s_pub) = BOLT8.keypair r_s_ent
     38       !r_s_pub_bs = BOLT8.serialize_pub r_s_pub
     39   in  wgroup "keys" $ do
     40         func "keypair" BOLT8.keypair i_s_ent
     41         func "parse_pub" BOLT8.parse_pub r_s_pub_bs
     42         func "serialize_pub" BOLT8.serialize_pub r_s_pub
     43 
     44 handshake :: Weigh ()
     45 handshake =
     46   let Just (!i_s_sec, !i_s_pub) = BOLT8.keypair i_s_ent
     47       Just (!r_s_sec, !r_s_pub) = BOLT8.keypair r_s_ent
     48       Right (!msg1, !i_hs) = BOLT8.act1 i_s_sec i_s_pub r_s_pub i_e_ent
     49       Right (!msg2, !r_hs) = BOLT8.act2 r_s_sec r_s_pub r_e_ent msg1
     50       Right (!msg3, _) = BOLT8.act3 i_hs msg2
     51   in  wgroup "handshake" $ do
     52         func "act1" (BOLT8.act1 i_s_sec i_s_pub r_s_pub) i_e_ent
     53         func "act2" (BOLT8.act2 r_s_sec r_s_pub r_e_ent) msg1
     54         func "act3" (BOLT8.act3 i_hs) msg2
     55         func "finalize" (BOLT8.finalize r_hs) msg3
     56 
     57 messages :: Weigh ()
     58 messages =
     59   let Just (!i_s_sec, !i_s_pub) = BOLT8.keypair i_s_ent
     60       Just (!r_s_sec, !r_s_pub) = BOLT8.keypair r_s_ent
     61       Right (msg1, i_hs) = BOLT8.act1 i_s_sec i_s_pub r_s_pub i_e_ent
     62       Right (msg2, r_hs) = BOLT8.act2 r_s_sec r_s_pub r_e_ent msg1
     63       Right (msg3, i_result) = BOLT8.act3 i_hs msg2
     64       Right r_result = BOLT8.finalize r_hs msg3
     65       !i_sess = BOLT8.session i_result
     66       !r_sess = BOLT8.session r_result
     67       !small_msg = BS.replicate 32 0x00
     68       !large_msg = BS.replicate 1024 0x00
     69       Right (!ct_small, _) = BOLT8.encrypt i_sess small_msg
     70       Right (!ct_large, _) = BOLT8.encrypt i_sess large_msg
     71   in  wgroup "messages" $ do
     72         func "encrypt (32B)" (BOLT8.encrypt i_sess) small_msg
     73         func "encrypt (1KB)" (BOLT8.encrypt i_sess) large_msg
     74         func "decrypt (32B)" (BOLT8.decrypt r_sess) ct_small
     75         func "decrypt (1KB)" (BOLT8.decrypt r_sess) ct_large