{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
module Lightning.Protocol.BOLT3.Keys (
derive_per_commitment_point
, derive_per_commitment_point'
, derive_pubkey
, derive_revocationpubkey
, CommitmentKeys(..)
, derive_commitment_keys
, derive_commitment_keys'
, derive_privkey
, derive_revocationprivkey
, generate_from_seed
, SecretStore
, empty_secret_store
, insert_secret
, derive_old_secret
, secret_store
, un_secret_store
, obscured_commitment_number
) where
import Control.DeepSeq (NFData(..))
import qualified Crypto.Curve.Secp256k1 as S
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.Choice as C
import Data.Bits ((.&.), (.|.), complement, complementBit, shiftL, shiftR,
testBit, xor)
import qualified Data.ByteString as BS
import Data.Word (Word64)
import GHC.Generics (Generic)
import Lightning.Protocol.BOLT1 (Point, PerCommitmentSecret)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3.Types
import qualified Numeric.Montgomery.Secp256k1.Scalar as SC
to_point :: S.Projective -> Maybe Point
to_point :: Projective -> Maybe Point
to_point Projective
p
| Projective
p Projective -> Projective -> Bool
forall a. Eq a => a -> a -> Bool
== Projective
S._CURVE_ZERO = Maybe Point
forall a. Maybe a
Nothing
| Bool
otherwise = case ByteString -> Maybe Point
BOLT1.point (Projective -> ByteString
S.serialize_point Projective
p) of
Just Point
pt -> Point -> Maybe Point
forall a. a -> Maybe a
Just Point
pt
Maybe Point
Nothing -> Maybe Point
forall a. a
internal_error
{-# INLINE to_point #-}
derive_per_commitment_point
:: PerCommitmentSecret
-> Maybe PerCommitmentPoint
derive_per_commitment_point :: PerCommitmentSecret -> Maybe PerCommitmentPoint
derive_per_commitment_point PerCommitmentSecret
s = do
sk <- ByteString -> Maybe Wider
S.parse_int256 (PerCommitmentSecret -> ByteString
BOLT1.un_per_commitment_secret PerCommitmentSecret
s)
pk <- S.derive_pub sk
PerCommitmentPoint <$> to_point pk
derive_per_commitment_point'
:: S.Context
-> PerCommitmentSecret
-> Maybe PerCommitmentPoint
derive_per_commitment_point' :: Context -> PerCommitmentSecret -> Maybe PerCommitmentPoint
derive_per_commitment_point' Context
tex PerCommitmentSecret
s = do
sk <- ByteString -> Maybe Wider
S.parse_int256 (PerCommitmentSecret -> ByteString
BOLT1.un_per_commitment_secret PerCommitmentSecret
s)
pk <- S.derive_pub' tex sk
PerCommitmentPoint <$> to_point pk
derive_pubkey :: Point -> PerCommitmentPoint -> Maybe Point
derive_pubkey :: Point -> PerCommitmentPoint -> Maybe Point
derive_pubkey = (ByteString -> Maybe Projective)
-> Point -> PerCommitmentPoint -> Maybe Point
derive_pubkey_with ByteString -> Maybe Projective
mul_g
{-# INLINE derive_pubkey #-}
mul_g :: BS.ByteString -> Maybe S.Projective
mul_g :: ByteString -> Maybe Projective
mul_g ByteString
bs = Wider -> Maybe Projective
S.derive_pub (Wider -> Maybe Projective) -> Maybe Wider -> Maybe Projective
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ByteString -> Maybe Wider
S.parse_int256 ByteString
bs
{-# INLINE mul_g #-}
mul_g' :: S.Context -> BS.ByteString -> Maybe S.Projective
mul_g' :: Context -> ByteString -> Maybe Projective
mul_g' Context
tex ByteString
bs = Context -> Wider -> Maybe Projective
S.derive_pub' Context
tex (Wider -> Maybe Projective) -> Maybe Wider -> Maybe Projective
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ByteString -> Maybe Wider
S.parse_int256 ByteString
bs
{-# INLINE mul_g' #-}
derive_pubkey_with
:: (BS.ByteString -> Maybe S.Projective)
-> Point
-> PerCommitmentPoint
-> Maybe Point
derive_pubkey_with :: (ByteString -> Maybe Projective)
-> Point -> PerCommitmentPoint -> Maybe Point
derive_pubkey_with ByteString -> Maybe Projective
mul Point
bp (PerCommitmentPoint Point
pcp) = do
let !bp_bs :: ByteString
bp_bs = Point -> ByteString
BOLT1.un_point Point
bp
base <- ByteString -> Maybe Projective
S.parse_point ByteString
bp_bs
t <- mul (SHA256.hash (BOLT1.un_point pcp <> bp_bs))
to_point (S.add base t)
{-# INLINE derive_pubkey_with #-}
derive_revocationpubkey
:: RevocationBasepoint
-> PerCommitmentPoint
-> Maybe RevocationPubkey
derive_revocationpubkey :: RevocationBasepoint -> PerCommitmentPoint -> Maybe RevocationPubkey
derive_revocationpubkey
(RevocationBasepoint Point
rbp)
(PerCommitmentPoint Point
pcp) = do
let !rbp_bs :: ByteString
rbp_bs = Point -> ByteString
BOLT1.un_point Point
rbp
!pcp_bs :: ByteString
pcp_bs = Point -> ByteString
BOLT1.un_point Point
pcp
r <- ByteString -> Maybe Projective
S.parse_point ByteString
rbp_bs
p <- S.parse_point pcp_bs
s1 <- S.parse_int256 (SHA256.hash (rbp_bs <> pcp_bs))
s2 <- S.parse_int256 (SHA256.hash (pcp_bs <> rbp_bs))
p1 <- S.mul r s1
p2 <- S.mul p s2
RevocationPubkey <$> to_point (S.add p1 p2)
data CommitmentKeys = CommitmentKeys
{ CommitmentKeys -> RevocationPubkey
ck_revocation_pubkey :: !RevocationPubkey
, CommitmentKeys -> LocalDelayedPubkey
ck_local_delayed :: !LocalDelayedPubkey
, CommitmentKeys -> LocalHtlcPubkey
ck_local_htlc :: !LocalHtlcPubkey
, CommitmentKeys -> RemoteHtlcPubkey
ck_remote_htlc :: !RemoteHtlcPubkey
, CommitmentKeys -> RemotePubkey
ck_remote_payment :: !RemotePubkey
, CommitmentKeys -> FundingPubkey
ck_local_funding :: !FundingPubkey
, CommitmentKeys -> FundingPubkey
ck_remote_funding :: !FundingPubkey
} deriving (CommitmentKeys -> CommitmentKeys -> Bool
(CommitmentKeys -> CommitmentKeys -> Bool)
-> (CommitmentKeys -> CommitmentKeys -> Bool) -> Eq CommitmentKeys
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommitmentKeys -> CommitmentKeys -> Bool
== :: CommitmentKeys -> CommitmentKeys -> Bool
$c/= :: CommitmentKeys -> CommitmentKeys -> Bool
/= :: CommitmentKeys -> CommitmentKeys -> Bool
Eq, Int -> CommitmentKeys -> ShowS
[CommitmentKeys] -> ShowS
CommitmentKeys -> String
(Int -> CommitmentKeys -> ShowS)
-> (CommitmentKeys -> String)
-> ([CommitmentKeys] -> ShowS)
-> Show CommitmentKeys
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommitmentKeys -> ShowS
showsPrec :: Int -> CommitmentKeys -> ShowS
$cshow :: CommitmentKeys -> String
show :: CommitmentKeys -> String
$cshowList :: [CommitmentKeys] -> ShowS
showList :: [CommitmentKeys] -> ShowS
Show, (forall x. CommitmentKeys -> Rep CommitmentKeys x)
-> (forall x. Rep CommitmentKeys x -> CommitmentKeys)
-> Generic CommitmentKeys
forall x. Rep CommitmentKeys x -> CommitmentKeys
forall x. CommitmentKeys -> Rep CommitmentKeys x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CommitmentKeys -> Rep CommitmentKeys x
from :: forall x. CommitmentKeys -> Rep CommitmentKeys x
$cto :: forall x. Rep CommitmentKeys x -> CommitmentKeys
to :: forall x. Rep CommitmentKeys x -> CommitmentKeys
Generic)
instance NFData CommitmentKeys
derive_commitment_keys
:: Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys :: Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys = (ByteString -> Maybe Projective)
-> Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys_with ByteString -> Maybe Projective
mul_g
derive_commitment_keys'
:: S.Context
-> Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys' :: Context
-> Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys' Context
tex = (ByteString -> Maybe Projective)
-> Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys_with (Context -> ByteString -> Maybe Projective
mul_g' Context
tex)
derive_commitment_keys_with
:: (BS.ByteString -> Maybe S.Projective)
-> Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys_with :: (ByteString -> Maybe Projective)
-> Basepoints
-> FundingPubkey
-> Basepoints
-> FundingPubkey
-> PerCommitmentPoint
-> Maybe CommitmentKeys
derive_commitment_keys_with ByteString -> Maybe Projective
mul Basepoints
local FundingPubkey
local_fund Basepoints
remote FundingPubkey
remote_fund PerCommitmentPoint
pcp = do
let DelayedPaymentBasepoint Point
local_delayed_bp = Basepoints -> DelayedPaymentBasepoint
bp_delayed_payment Basepoints
local
HtlcBasepoint Point
local_htlc_bp = Basepoints -> HtlcBasepoint
bp_htlc Basepoints
local
HtlcBasepoint Point
remote_htlc_bp = Basepoints -> HtlcBasepoint
bp_htlc Basepoints
remote
PaymentBasepoint Point
remote_payment_bp = Basepoints -> PaymentBasepoint
bp_payment Basepoints
remote
revocation <- RevocationBasepoint -> PerCommitmentPoint -> Maybe RevocationPubkey
derive_revocationpubkey (Basepoints -> RevocationBasepoint
bp_revocation Basepoints
remote) PerCommitmentPoint
pcp
delayed <- derive_pubkey_with mul local_delayed_bp pcp
local_htlc <- derive_pubkey_with mul local_htlc_bp pcp
remote_htlc <- derive_pubkey_with mul remote_htlc_bp pcp
pure CommitmentKeys
{ ck_revocation_pubkey = revocation
, ck_local_delayed = LocalDelayedPubkey delayed
, ck_local_htlc = LocalHtlcPubkey local_htlc
, ck_remote_htlc = RemoteHtlcPubkey remote_htlc
, ck_remote_payment = RemotePubkey remote_payment_bp
, ck_local_funding = local_fund
, ck_remote_funding = remote_fund
}
derive_privkey :: Seckey -> PerCommitmentPoint -> Maybe Seckey
derive_privkey :: Seckey -> PerCommitmentPoint -> Maybe Seckey
derive_privkey (Seckey ByteString
sk_bs) (PerCommitmentPoint Point
pcp) = do
sk <- ByteString -> Maybe Wider
S.parse_int256 ByteString
sk_bs
bp <- to_point =<< S.derive_pub sk
let !h = ByteString -> ByteString
SHA256.hash (Point -> ByteString
BOLT1.un_point Point
pcp ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Point -> ByteString
BOLT1.un_point Point
bp)
to_seckey (SC.to sk + SC.to (S.unsafe_roll32 h))
derive_revocationprivkey
:: Seckey
-> PerCommitmentSecret
-> Maybe Seckey
derive_revocationprivkey :: Seckey -> PerCommitmentSecret -> Maybe Seckey
derive_revocationprivkey (Seckey ByteString
rbs_bs) PerCommitmentSecret
pcs = do
let !pcs_bs :: ByteString
pcs_bs = PerCommitmentSecret -> ByteString
BOLT1.un_per_commitment_secret PerCommitmentSecret
pcs
rbs <- ByteString -> Maybe Wider
S.parse_int256 ByteString
rbs_bs
ps <- S.parse_int256 pcs_bs
rbp <- to_point =<< S.derive_pub rbs
pcp <- to_point =<< S.derive_pub ps
let !rbp_bs = Point -> ByteString
BOLT1.un_point Point
rbp
!pcp_bs = Point -> ByteString
BOLT1.un_point Point
pcp
!h1 = Wider -> Montgomery
SC.to (ByteString -> Wider
S.unsafe_roll32 (ByteString -> ByteString
SHA256.hash (ByteString
rbp_bs ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
pcp_bs)))
!h2 = Wider -> Montgomery
SC.to (ByteString -> Wider
S.unsafe_roll32 (ByteString -> ByteString
SHA256.hash (ByteString
pcp_bs ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
rbp_bs)))
to_seckey (SC.to rbs * h1 + SC.to ps * h2)
to_seckey :: SC.Montgomery -> Maybe Seckey
to_seckey :: Montgomery -> Maybe Seckey
to_seckey Montgomery
r
| Choice -> Bool
C.decide (Montgomery -> Montgomery -> Choice
SC.eq Montgomery
r Montgomery
0) = Maybe Seckey
forall a. Maybe a
Nothing
| Bool
otherwise = Seckey -> Maybe Seckey
forall a. a -> Maybe a
Just (ByteString -> Seckey
Seckey (Wider -> ByteString
S.unroll32 (Montgomery -> Wider
SC.retr Montgomery
r)))
to_secret :: BS.ByteString -> PerCommitmentSecret
to_secret :: ByteString -> PerCommitmentSecret
to_secret ByteString
bs = case ByteString -> Maybe PerCommitmentSecret
BOLT1.per_commitment_secret ByteString
bs of
Just PerCommitmentSecret
s -> PerCommitmentSecret
s
Maybe PerCommitmentSecret
Nothing -> PerCommitmentSecret
forall a. a
internal_error
{-# INLINE to_secret #-}
flip_bit :: Int -> BS.ByteString -> BS.ByteString
flip_bit :: Int -> ByteString -> ByteString
flip_bit Int
b ByteString
bs =
let !(ByteString
pre, ByteString
post) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (Int
b Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
3) ByteString
bs
in case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
post of
Just (Word8
h, ByteString
t) -> ByteString
pre ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Word8 -> ByteString -> ByteString
BS.cons (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
complementBit Word8
h (Int
b Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
7)) ByteString
t
Maybe (Word8, ByteString)
Nothing -> ByteString
bs
{-# INLINE flip_bit #-}
derive_secret :: BS.ByteString -> Int -> Word64 -> BS.ByteString
derive_secret :: ByteString -> Int -> Word64 -> ByteString
derive_secret ByteString
base Int
bits Word64
i = Int -> ByteString -> ByteString
go (Int
bits Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ByteString
base where
go :: Int -> ByteString -> ByteString
go !Int
b !ByteString
p
| Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = ByteString
p
| Word64 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word64
i Int
b = Int -> ByteString -> ByteString
go (Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (ByteString -> ByteString
SHA256.hash (Int -> ByteString -> ByteString
flip_bit Int
b ByteString
p))
| Bool
otherwise = Int -> ByteString -> ByteString
go (Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ByteString
p
generate_from_seed :: Seed -> SecretIndex -> PerCommitmentSecret
generate_from_seed :: Seed -> SecretIndex -> PerCommitmentSecret
generate_from_seed (Seed ByteString
s) (SecretIndex Word64
i) = ByteString -> PerCommitmentSecret
to_secret (ByteString -> Int -> Word64 -> ByteString
derive_secret ByteString
s Int
48 Word64
i)
newtype SecretStore = SecretStore [Entry]
data Entry = Entry
{-# UNPACK #-} !Int
{-# UNPACK #-} !Word64
!BS.ByteString
instance Show SecretStore where
showsPrec :: Int -> SecretStore -> ShowS
showsPrec Int
d SecretStore
_ = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"SecretStore <redacted>"
instance NFData SecretStore where
rnf :: SecretStore -> ()
rnf (SecretStore [Entry]
es) = [Entry] -> ()
rnf_entries [Entry]
es where
rnf_entries :: [Entry] -> ()
rnf_entries [] = ()
rnf_entries (Entry Int
_ Word64
_ ByteString
s : [Entry]
rest) = ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
s () -> () -> ()
forall a b. a -> b -> b
`seq` [Entry] -> ()
rnf_entries [Entry]
rest
empty_secret_store :: SecretStore
empty_secret_store :: SecretStore
empty_secret_store = [Entry] -> SecretStore
SecretStore []
bucket_of :: Word64 -> Int
bucket_of :: Word64 -> Int
bucket_of Word64
i = Int -> Int
go Int
0 where
go :: Int -> Int
go !Int
b
| Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
47 = Int
48
| Word64 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word64
i Int
b = Int
b
| Bool
otherwise = Int -> Int
go (Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
can_derive :: Int -> Word64 -> Word64 -> Bool
can_derive :: Int -> Word64 -> Word64 -> Bool
can_derive Int
b Word64
j Word64
i = Word64
i Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64 -> Word64
forall a. Bits a => a -> a
complement ((Word64
1 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
b) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
j
{-# INLINE can_derive #-}
insert_secret
:: PerCommitmentSecret
-> SecretIndex
-> SecretStore
-> Maybe SecretStore
insert_secret :: PerCommitmentSecret
-> SecretIndex -> SecretStore -> Maybe SecretStore
insert_secret PerCommitmentSecret
pcs (SecretIndex Word64
i) (SecretStore [Entry]
known)
| (Entry -> Bool) -> [Entry] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Entry -> Bool
derives [Entry]
known = SecretStore -> Maybe SecretStore
forall a. a -> Maybe a
Just (SecretStore -> Maybe SecretStore)
-> SecretStore -> Maybe SecretStore
forall a b. (a -> b) -> a -> b
$! [Entry] -> SecretStore
SecretStore ([Entry] -> [Entry]
put [Entry]
known)
| Bool
otherwise = Maybe SecretStore
forall a. Maybe a
Nothing
where
!s :: ByteString
s = PerCommitmentSecret -> ByteString
BOLT1.un_per_commitment_secret PerCommitmentSecret
pcs
!b :: Int
b = Word64 -> Int
bucket_of Word64
i
!e :: Entry
e = Int -> Word64 -> ByteString -> Entry
Entry Int
b Word64
i ByteString
s
derives :: Entry -> Bool
derives (Entry Int
kb Word64
ki ByteString
ks)
| Int
kb Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
b = ByteString -> PerCommitmentSecret
to_secret (ByteString -> Int -> Word64 -> ByteString
derive_secret ByteString
s Int
b Word64
ki) PerCommitmentSecret -> PerCommitmentSecret -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> PerCommitmentSecret
to_secret ByteString
ks
| Bool
otherwise = Bool
True
put :: [Entry] -> [Entry]
put [] = [Entry
e]
put (k :: Entry
k@(Entry Int
kb Word64
_ ByteString
_) : [Entry]
ks) = case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
kb Int
b of
Ordering
LT -> Entry
k Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
: [Entry] -> [Entry]
put [Entry]
ks
Ordering
EQ -> Entry
e Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
: [Entry]
ks
Ordering
GT -> Entry
e Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
: Entry
k Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
: [Entry]
ks
derive_old_secret
:: SecretIndex
-> SecretStore
-> Maybe PerCommitmentSecret
derive_old_secret :: SecretIndex -> SecretStore -> Maybe PerCommitmentSecret
derive_old_secret (SecretIndex Word64
i) (SecretStore [Entry]
known) = [Entry] -> Maybe PerCommitmentSecret
go [Entry]
known where
go :: [Entry] -> Maybe PerCommitmentSecret
go [] = Maybe PerCommitmentSecret
forall a. Maybe a
Nothing
go (Entry Int
b Word64
j ByteString
s : [Entry]
rest)
| Int -> Word64 -> Word64 -> Bool
can_derive Int
b Word64
j Word64
i = PerCommitmentSecret -> Maybe PerCommitmentSecret
forall a. a -> Maybe a
Just (PerCommitmentSecret -> Maybe PerCommitmentSecret)
-> PerCommitmentSecret -> Maybe PerCommitmentSecret
forall a b. (a -> b) -> a -> b
$! ByteString -> PerCommitmentSecret
to_secret (ByteString -> Int -> Word64 -> ByteString
derive_secret ByteString
s Int
b Word64
i)
| Bool
otherwise = [Entry] -> Maybe PerCommitmentSecret
go [Entry]
rest
secret_store
:: [(SecretIndex, PerCommitmentSecret)]
-> Maybe SecretStore
secret_store :: [(SecretIndex, PerCommitmentSecret)] -> Maybe SecretStore
secret_store = [Entry]
-> [(SecretIndex, PerCommitmentSecret)] -> Maybe SecretStore
go [] where
go :: [Entry]
-> [(SecretIndex, PerCommitmentSecret)] -> Maybe SecretStore
go [Entry]
acc [] = SecretStore -> Maybe SecretStore
forall a. a -> Maybe a
Just ([Entry] -> SecretStore
SecretStore [Entry]
acc)
go [Entry]
acc ((SecretIndex Word64
i, PerCommitmentSecret
pcs) : [(SecretIndex, PerCommitmentSecret)]
rest) = do
let !e :: Entry
e = Int -> Word64 -> ByteString -> Entry
Entry (Word64 -> Int
bucket_of Word64
i) Word64
i (PerCommitmentSecret -> ByteString
BOLT1.un_per_commitment_secret PerCommitmentSecret
pcs)
acc' <- Entry -> [Entry] -> Maybe [Entry]
place Entry
e [Entry]
acc
go acc' rest
place :: Entry -> [Entry] -> Maybe [Entry]
place e :: Entry
e@(Entry Int
b Word64
i ByteString
s) [Entry]
es
| (Entry -> Bool) -> [Entry] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Entry -> Entry -> Bool
forall {p}. p -> Entry -> Bool
consistent Entry
e) [Entry]
es = [Entry] -> Maybe [Entry]
put [Entry]
es
| Bool
otherwise = Maybe [Entry]
forall a. Maybe a
Nothing
where
put :: [Entry] -> Maybe [Entry]
put [] = [Entry] -> Maybe [Entry]
forall a. a -> Maybe a
Just [Entry
e]
put (k :: Entry
k@(Entry Int
kb Word64
_ ByteString
_) : [Entry]
ks) = case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
kb Int
b of
Ordering
LT -> (Entry
k Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
:) ([Entry] -> [Entry]) -> Maybe [Entry] -> Maybe [Entry]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Entry] -> Maybe [Entry]
put [Entry]
ks
Ordering
EQ -> Maybe [Entry]
forall a. Maybe a
Nothing
Ordering
GT -> [Entry] -> Maybe [Entry]
forall a. a -> Maybe a
Just (Entry
e Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
: Entry
k Entry -> [Entry] -> [Entry]
forall a. a -> [a] -> [a]
: [Entry]
ks)
consistent :: p -> Entry -> Bool
consistent p
_ (Entry Int
kb Word64
ki ByteString
ks)
| Int
kb Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
b Bool -> Bool -> Bool
&& Int -> Word64 -> Word64 -> Bool
can_derive Int
kb Word64
ki Word64
i =
ByteString -> PerCommitmentSecret
to_secret (ByteString -> Int -> Word64 -> ByteString
derive_secret ByteString
ks Int
kb Word64
i) PerCommitmentSecret -> PerCommitmentSecret -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> PerCommitmentSecret
to_secret ByteString
s
| Int
kb Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
b Bool -> Bool -> Bool
&& Int -> Word64 -> Word64 -> Bool
can_derive Int
b Word64
i Word64
ki =
ByteString -> PerCommitmentSecret
to_secret (ByteString -> Int -> Word64 -> ByteString
derive_secret ByteString
s Int
b Word64
ki) PerCommitmentSecret -> PerCommitmentSecret -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> PerCommitmentSecret
to_secret ByteString
ks
| Bool
otherwise = Bool
True
un_secret_store :: SecretStore -> [(SecretIndex, PerCommitmentSecret)]
un_secret_store :: SecretStore -> [(SecretIndex, PerCommitmentSecret)]
un_secret_store (SecretStore [Entry]
es) =
[ (Word64 -> SecretIndex
SecretIndex Word64
i, ByteString -> PerCommitmentSecret
to_secret ByteString
s) | Entry Int
_ Word64
i ByteString
s <- [Entry]
es ]
obscured_commitment_number
:: PaymentBasepoint
-> PaymentBasepoint
-> CommitmentNumber
-> Word64
obscured_commitment_number :: PaymentBasepoint -> PaymentBasepoint -> CommitmentNumber -> Word64
obscured_commitment_number
(PaymentBasepoint Point
opener)
(PaymentBasepoint Point
accepter)
CommitmentNumber
cn =
let !h :: ByteString
h = ByteString -> ByteString
SHA256.hash (Point -> ByteString
BOLT1.un_point Point
opener ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Point -> ByteString
BOLT1.un_point Point
accepter)
!mask :: Word64
mask = (Word64 -> Word8 -> Word64) -> Word64 -> ByteString -> Word64
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl' (\Word64
acc Word8
x -> (Word64
acc Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x) Word64
0
(Int -> ByteString -> ByteString
BS.drop Int
26 ByteString
h)
in CommitmentNumber -> Word64
un_commitment_number CommitmentNumber
cn Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
mask