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

{-# OPTIONS_HADDOCK not-home #-}

-- | 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 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal @(ByteSize soc a) forall {k} (t :: k). Proxy t
Proxy

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

type instance ByteSize 'Serialize SecretKey = SkSerializeSize

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

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

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

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