{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}

-- |
-- Module: Lightning.Protocol.BOLT7.Hash
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Signature digests, signature verification and checksums for BOLT #7
-- messages.
--
-- Signatures cover the encoded message after its signature fields,
-- extension included. Since decoding keeps every byte, the digest of a
-- decoded message is the digest of the bytes received.

module Lightning.Protocol.BOLT7.Hash (
    channel_announcement_hash
  , node_announcement_hash
  , channel_update_hash
  , verify_channel_announcement
  , verify_node_announcement
  , verify_channel_update
  , channel_update_checksum
  ) where

import qualified Crypto.Curve.Secp256k1 as Secp256k1
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.ByteString as BS
import Data.Word (Word32)
import Lightning.Protocol.BOLT1 (Point, Signature)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT7.CRC32C (crc32c)
import Lightning.Protocol.BOLT7.Codec
import Lightning.Protocol.BOLT7.Messages

-- the signed bytes of each message
announcement_signed :: ChannelAnnouncement -> Either EncodeError BS.ByteString
announcement_signed :: ChannelAnnouncement -> Either EncodeError ByteString
announcement_signed = (ByteString -> ByteString)
-> Either EncodeError ByteString -> Either EncodeError ByteString
forall a b.
(a -> b) -> Either EncodeError a -> Either EncodeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> ByteString -> ByteString
BS.drop Int
256) (Either EncodeError ByteString -> Either EncodeError ByteString)
-> (ChannelAnnouncement -> Either EncodeError ByteString)
-> ChannelAnnouncement
-> Either EncodeError ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChannelAnnouncement -> Either EncodeError ByteString
encode_channel_announcement
{-# INLINE announcement_signed #-}

node_signed :: NodeAnnouncement -> Either EncodeError BS.ByteString
node_signed :: NodeAnnouncement -> Either EncodeError ByteString
node_signed = (ByteString -> ByteString)
-> Either EncodeError ByteString -> Either EncodeError ByteString
forall a b.
(a -> b) -> Either EncodeError a -> Either EncodeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> ByteString -> ByteString
BS.drop Int
64) (Either EncodeError ByteString -> Either EncodeError ByteString)
-> (NodeAnnouncement -> Either EncodeError ByteString)
-> NodeAnnouncement
-> Either EncodeError ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NodeAnnouncement -> Either EncodeError ByteString
encode_node_announcement
{-# INLINE node_signed #-}

update_signed :: ChannelUpdate -> BS.ByteString
update_signed :: ChannelUpdate -> ByteString
update_signed = Int -> ByteString -> ByteString
BS.drop Int
64 (ByteString -> ByteString)
-> (ChannelUpdate -> ByteString) -> ChannelUpdate -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChannelUpdate -> ByteString
encode_channel_update
{-# INLINE update_signed #-}

double_sha256 :: BS.ByteString -> BS.ByteString
double_sha256 :: ByteString -> ByteString
double_sha256 = ByteString -> ByteString
SHA256.hash (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
SHA256.hash
{-# INLINE double_sha256 #-}

-- | The digest the four signatures of a t'ChannelAnnouncement' sign: the
--   double SHA-256 of its encoding from offset 256 (after the
--   signatures) to the end. Fails as 'encode_channel_announcement' does.
channel_announcement_hash
  :: ChannelAnnouncement -> Either EncodeError BS.ByteString
channel_announcement_hash :: ChannelAnnouncement -> Either EncodeError ByteString
channel_announcement_hash = (ByteString -> ByteString)
-> Either EncodeError ByteString -> Either EncodeError ByteString
forall a b.
(a -> b) -> Either EncodeError a -> Either EncodeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
double_sha256 (Either EncodeError ByteString -> Either EncodeError ByteString)
-> (ChannelAnnouncement -> Either EncodeError ByteString)
-> ChannelAnnouncement
-> Either EncodeError ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChannelAnnouncement -> Either EncodeError ByteString
announcement_signed

-- | The digest the signature of a t'NodeAnnouncement' signs: the double
--   SHA-256 of its encoding after the signature. Fails as
--   'encode_node_announcement' does.
node_announcement_hash
  :: NodeAnnouncement -> Either EncodeError BS.ByteString
node_announcement_hash :: NodeAnnouncement -> Either EncodeError ByteString
node_announcement_hash = (ByteString -> ByteString)
-> Either EncodeError ByteString -> Either EncodeError ByteString
forall a b.
(a -> b) -> Either EncodeError a -> Either EncodeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
double_sha256 (Either EncodeError ByteString -> Either EncodeError ByteString)
-> (NodeAnnouncement -> Either EncodeError ByteString)
-> NodeAnnouncement
-> Either EncodeError ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NodeAnnouncement -> Either EncodeError ByteString
node_signed

-- | The digest the signature of a t'ChannelUpdate' signs: the double
--   SHA-256 of its encoding after the signature.
channel_update_hash :: ChannelUpdate -> BS.ByteString
channel_update_hash :: ChannelUpdate -> ByteString
channel_update_hash = ByteString -> ByteString
double_sha256 (ByteString -> ByteString)
-> (ChannelUpdate -> ByteString) -> ChannelUpdate -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChannelUpdate -> ByteString
update_signed

-- | Verify the four signatures of a t'ChannelAnnouncement': both node
--   signatures and both bitcoin signatures, each over
--   'channel_announcement_hash' under the corresponding key.
--
--   As with libsecp256k1, signatures must be in low-s form. A key that
--   isn't a point on the curve fails verification.
verify_channel_announcement :: ChannelAnnouncement -> Bool
verify_channel_announcement :: ChannelAnnouncement -> Bool
verify_channel_announcement ChannelAnnouncement
m = case ChannelAnnouncement -> Either EncodeError ByteString
announcement_signed ChannelAnnouncement
m of
  Left EncodeError
_ -> Bool
False
  Right ByteString
signed ->
    let !d :: ByteString
d = ByteString -> ByteString
SHA256.hash ByteString
signed
    in  ByteString -> Point -> Signature -> Bool
verify ByteString
d (ChannelAnnouncement -> Point
ca_node_id_1 ChannelAnnouncement
m) (ChannelAnnouncement -> Signature
ca_node_signature_1 ChannelAnnouncement
m)
          Bool -> Bool -> Bool
&& ByteString -> Point -> Signature -> Bool
verify ByteString
d (ChannelAnnouncement -> Point
ca_node_id_2 ChannelAnnouncement
m) (ChannelAnnouncement -> Signature
ca_node_signature_2 ChannelAnnouncement
m)
          Bool -> Bool -> Bool
&& ByteString -> Point -> Signature -> Bool
verify ByteString
d (ChannelAnnouncement -> Point
ca_bitcoin_key_1 ChannelAnnouncement
m) (ChannelAnnouncement -> Signature
ca_bitcoin_signature_1 ChannelAnnouncement
m)
          Bool -> Bool -> Bool
&& ByteString -> Point -> Signature -> Bool
verify ByteString
d (ChannelAnnouncement -> Point
ca_bitcoin_key_2 ChannelAnnouncement
m) (ChannelAnnouncement -> Signature
ca_bitcoin_signature_2 ChannelAnnouncement
m)

-- | Verify the signature of a t'NodeAnnouncement' under its node id.
--
--   As with libsecp256k1, the signature must be in low-s form.
verify_node_announcement :: NodeAnnouncement -> Bool
verify_node_announcement :: NodeAnnouncement -> Bool
verify_node_announcement NodeAnnouncement
m = case NodeAnnouncement -> Either EncodeError ByteString
node_signed NodeAnnouncement
m of
  Left EncodeError
_       -> Bool
False
  Right ByteString
signed -> ByteString -> Point -> Signature -> Bool
verify (ByteString -> ByteString
SHA256.hash ByteString
signed) (NodeAnnouncement -> Point
na_node_id NodeAnnouncement
m) (NodeAnnouncement -> Signature
na_signature NodeAnnouncement
m)

-- | Verify the signature of a t'ChannelUpdate' under the given node id.
--
--   The key is that of the channel's t'ChannelAnnouncement' selected by
--   the update's direction: 'ca_node_id_1' for
--   'Lightning.Protocol.BOLT7.NodeOne', 'ca_node_id_2' for
--   'Lightning.Protocol.BOLT7.NodeTwo'. As with libsecp256k1, the
--   signature must be in low-s form.
verify_channel_update :: Point -> ChannelUpdate -> Bool
verify_channel_update :: Point -> ChannelUpdate -> Bool
verify_channel_update Point
k ChannelUpdate
m =
  ByteString -> Point -> Signature -> Bool
verify (ByteString -> ByteString
SHA256.hash (ChannelUpdate -> ByteString
update_signed ChannelUpdate
m)) Point
k (ChannelUpdate -> Signature
cu_signature ChannelUpdate
m)

-- verify a signature over the double SHA-256 of some bytes, given their
-- single SHA-256 (secp256k1's verify_ecdsa hashes once more)
verify :: BS.ByteString -> Point -> Signature -> Bool
verify :: ByteString -> Point -> Signature -> Bool
verify ByteString
d Point
k Signature
s =
  case ( ByteString -> Maybe Projective
Secp256k1.parse_point (Point -> ByteString
BOLT1.un_point Point
k)
       , ByteString -> Maybe ECDSA
Secp256k1.parse_sig (Signature -> ByteString
BOLT1.un_signature Signature
s) ) of
    (Just Projective
p, Just ECDSA
sig) -> Context -> ByteString -> Projective -> ECDSA -> Bool
Secp256k1.verify_ecdsa' Context
Secp256k1.precompute ByteString
d Projective
p ECDSA
sig
    (Maybe Projective, Maybe ECDSA)
_                  -> Bool
False

-- | The checksum of a t'ChannelUpdate', as used in the @checksums_tlv@ of
--   @reply_channel_range@: the CRC-32C (RFC 3720) of the update without
--   its signature and timestamp. Extension records are included.
channel_update_checksum :: ChannelUpdate -> Word32
channel_update_checksum :: ChannelUpdate -> Word32
channel_update_checksum ChannelUpdate
m =
  let !bs :: ByteString
bs = ChannelUpdate -> ByteString
encode_channel_update ChannelUpdate
m
  in  ByteString -> Word32
crc32c (Int -> ByteString -> ByteString
BS.take Int
40 (Int -> ByteString -> ByteString
BS.drop Int
64 ByteString
bs) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> ByteString -> ByteString
BS.drop Int
108 ByteString
bs)