-- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
-- SPDX-License-Identifier: MPL-2.0

{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE RoleAnnotations #-}

-- | Types used for the high-level interface.
module Crypto.BLST.Internal.Types
  ( module Crypto.BLST.Internal.Types
  ) where

import Control.DeepSeq (NFData)
import Data.Kind (Type)
import Data.Proxy (Proxy(..))
import GHC.TypeLits (KnownNat, Nat, natVal)

import Crypto.BLST.Internal.Bindings.Types
import Crypto.BLST.Internal.Classy
  (CompressedSize, Curve, CurveToMsgPoint, CurveToPkPoint, SerializedSize)

-- | Data kind flag for 'ByteSize'.
data SerializeOrCompress = Serialize | Compress

type ByteSize :: SerializeOrCompress -> Type -> Nat
-- | Size in bytes of serialized/compressed representations of basic types.
type family ByteSize soc a

-- | Convenience function to get byte size as an 'Int' value.
byteSize :: forall soc a. KnownNat (ByteSize soc a) => Int
byteSize :: forall (soc :: SerializeOrCompress) a.
KnownNat (ByteSize soc a) =>
Int
byteSize = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal @(ByteSize soc a) Proxy (ByteSize soc a)
forall {k} (t :: k). Proxy t
Proxy

-- | Representation for the secret key.
newtype SecretKey = SecretKey Scalar
  deriving stock (SecretKey -> SecretKey -> Bool
(SecretKey -> SecretKey -> Bool)
-> (SecretKey -> SecretKey -> Bool) -> Eq SecretKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SecretKey -> SecretKey -> Bool
== :: SecretKey -> SecretKey -> Bool
$c/= :: SecretKey -> SecretKey -> Bool
/= :: SecretKey -> SecretKey -> Bool
Eq, Int -> SecretKey -> ShowS
[SecretKey] -> ShowS
SecretKey -> String
(Int -> SecretKey -> ShowS)
-> (SecretKey -> String)
-> ([SecretKey] -> ShowS)
-> Show SecretKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretKey -> ShowS
showsPrec :: Int -> SecretKey -> ShowS
$cshow :: SecretKey -> String
show :: SecretKey -> String
$cshowList :: [SecretKey] -> ShowS
showList :: [SecretKey] -> ShowS
Show)
  deriving newtype SecretKey -> ()
(SecretKey -> ()) -> NFData SecretKey
forall a. (a -> ()) -> NFData a
$crnf :: SecretKey -> ()
rnf :: SecretKey -> ()
NFData

type instance ByteSize 'Serialize SecretKey = SkSerializeSize

type role PublicKey nominal
-- | Public key representation.
type PublicKey :: Curve -> Type
newtype PublicKey c = PublicKey (Affine (CurveToPkPoint c))
  deriving stock (PublicKey c -> PublicKey c -> Bool
(PublicKey c -> PublicKey c -> Bool)
-> (PublicKey c -> PublicKey c -> Bool) -> Eq (PublicKey c)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (c :: Curve). PublicKey c -> PublicKey c -> Bool
$c== :: forall (c :: Curve). PublicKey c -> PublicKey c -> Bool
== :: PublicKey c -> PublicKey c -> Bool
$c/= :: forall (c :: Curve). PublicKey c -> PublicKey c -> Bool
/= :: PublicKey c -> PublicKey c -> Bool
Eq, Int -> PublicKey c -> ShowS
[PublicKey c] -> ShowS
PublicKey c -> String
(Int -> PublicKey c -> ShowS)
-> (PublicKey c -> String)
-> ([PublicKey c] -> ShowS)
-> Show (PublicKey c)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (c :: Curve). Int -> PublicKey c -> ShowS
forall (c :: Curve). [PublicKey c] -> ShowS
forall (c :: Curve). PublicKey c -> String
$cshowsPrec :: forall (c :: Curve). Int -> PublicKey c -> ShowS
showsPrec :: Int -> PublicKey c -> ShowS
$cshow :: forall (c :: Curve). PublicKey c -> String
show :: PublicKey c -> String
$cshowList :: forall (c :: Curve). [PublicKey c] -> ShowS
showList :: [PublicKey c] -> ShowS
Show)
  deriving newtype PublicKey c -> ()
(PublicKey c -> ()) -> NFData (PublicKey c)
forall a. (a -> ()) -> NFData a
forall (c :: Curve). PublicKey c -> ()
$crnf :: forall (c :: Curve). PublicKey c -> ()
rnf :: PublicKey c -> ()
NFData

type instance ByteSize 'Serialize (PublicKey c) = SerializedSize (CurveToPkPoint c)
type instance ByteSize 'Compress (PublicKey c) = CompressedSize (CurveToPkPoint c)

type role Signature nominal phantom
-- | Signature representation.
type Signature :: Curve -> EncodeMethod -> Type
newtype Signature c m = Signature (Affine (CurveToMsgPoint c))
  deriving stock (Signature c m -> Signature c m -> Bool
(Signature c m -> Signature c m -> Bool)
-> (Signature c m -> Signature c m -> Bool) -> Eq (Signature c m)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (c :: Curve) (m :: EncodeMethod).
Signature c m -> Signature c m -> Bool
$c== :: forall (c :: Curve) (m :: EncodeMethod).
Signature c m -> Signature c m -> Bool
== :: Signature c m -> Signature c m -> Bool
$c/= :: forall (c :: Curve) (m :: EncodeMethod).
Signature c m -> Signature c m -> Bool
/= :: Signature c m -> Signature c m -> Bool
Eq, Int -> Signature c m -> ShowS
[Signature c m] -> ShowS
Signature c m -> String
(Int -> Signature c m -> ShowS)
-> (Signature c m -> String)
-> ([Signature c m] -> ShowS)
-> Show (Signature c m)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (c :: Curve) (m :: EncodeMethod).
Int -> Signature c m -> ShowS
forall (c :: Curve) (m :: EncodeMethod). [Signature c m] -> ShowS
forall (c :: Curve) (m :: EncodeMethod). Signature c m -> String
$cshowsPrec :: forall (c :: Curve) (m :: EncodeMethod).
Int -> Signature c m -> ShowS
showsPrec :: Int -> Signature c m -> ShowS
$cshow :: forall (c :: Curve) (m :: EncodeMethod). Signature c m -> String
show :: Signature c m -> String
$cshowList :: forall (c :: Curve) (m :: EncodeMethod). [Signature c m] -> ShowS
showList :: [Signature c m] -> ShowS
Show)
  deriving newtype Signature c m -> ()
(Signature c m -> ()) -> NFData (Signature c m)
forall a. (a -> ()) -> NFData a
forall (c :: Curve) (m :: EncodeMethod). Signature c m -> ()
$crnf :: forall (c :: Curve) (m :: EncodeMethod). Signature c m -> ()
rnf :: Signature c m -> ()
NFData

type instance ByteSize 'Serialize (Signature c _) = SerializedSize (CurveToMsgPoint c)
type instance ByteSize 'Compress (Signature c _) = CompressedSize (CurveToMsgPoint c)