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 (2962B)


      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.Internal 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.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 -- note that 'weigh' doesn't work properly in a repl
     26 main :: IO ()
     27 main = mainWith $ do
     28   keys
     29   handshake
     30   messages
     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 :: Weigh ()
     40 keys =
     41   let Just (_, !r_s_pub) = BOLT8.keypair r_s_ent
     42       !r_s_pub_bs = BOLT8.serialize_pub r_s_pub
     43   in  wgroup "keys" $ do
     44         func "keypair" BOLT8.keypair i_s_ent
     45         func "parse_pub" BOLT8.parse_pub r_s_pub_bs
     46         func "serialize_pub"
     47           BOLT8.serialize_pub r_s_pub
     48 
     49 handshake :: Weigh ()
     50 handshake =
     51   let Just (!i_s_sec, !i_s_pub) =
     52         BOLT8.keypair i_s_ent
     53       Just (!r_s_sec, !r_s_pub) =
     54         BOLT8.keypair r_s_ent
     55       Right (!msg1, !i_hs) =
     56         BOLT8.act1 i_s_sec i_s_pub r_s_pub i_e_ent
     57       Right (!msg2, !r_hs) =
     58         BOLT8.act2 r_s_sec r_s_pub r_e_ent msg1
     59       Right (!msg3, _) = BOLT8.act3 i_hs msg2
     60   in  wgroup "handshake" $ do
     61         func "act1"
     62           (BOLT8.act1 i_s_sec i_s_pub r_s_pub) i_e_ent
     63         func "act2"
     64           (BOLT8.act2 r_s_sec r_s_pub r_e_ent) msg1
     65         func "act3" (BOLT8.act3 i_hs) msg2
     66         func "finalize" (BOLT8.finalize r_hs) msg3
     67 
     68 messages :: Weigh ()
     69 messages =
     70   let Just (!i_s_sec, !i_s_pub) =
     71         BOLT8.keypair i_s_ent
     72       Just (!r_s_sec, !r_s_pub) =
     73         BOLT8.keypair r_s_ent
     74       Right (msg1, i_hs) =
     75         BOLT8.act1 i_s_sec i_s_pub r_s_pub i_e_ent
     76       Right (msg2, r_hs) =
     77         BOLT8.act2 r_s_sec r_s_pub r_e_ent msg1
     78       Right (msg3, i_result) =
     79         BOLT8.act3 i_hs msg2
     80       Right r_result =
     81         BOLT8.finalize r_hs msg3
     82       !i_sess = BOLT8.session i_result
     83       !r_sess = BOLT8.session r_result
     84       !small_msg = BS.replicate 32 0x00
     85       !large_msg = BS.replicate 1024 0x00
     86       Right (!ct_small, _) =
     87         BOLT8.encrypt i_sess small_msg
     88       Right (!ct_large, _) =
     89         BOLT8.encrypt i_sess large_msg
     90   in  wgroup "messages" $ do
     91         func "encrypt (32B)"
     92           (BOLT8.encrypt i_sess) small_msg
     93         func "encrypt (1KB)"
     94           (BOLT8.encrypt i_sess) large_msg
     95         func "decrypt (32B)"
     96           (BOLT8.decrypt r_sess) ct_small
     97         func "decrypt (1KB)"
     98           (BOLT8.decrypt r_sess) ct_large