module Crypto.HPKE.PublicKey (
    SecretKey,
    PublicKey,
    serializePublicKey,
    deserializePublicKey,
    serializeSecretKey,
    deserializeSecretKey,
)
where

import Crypto.ECC (
    EllipticCurve (..),
    Point,
    Scalar,
    decodePoint,
    decodeScalar,
    encodePoint,
    encodeScalar,
 )

import Crypto.HPKE.Types

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> import Crypto.ECC
-- >>> import Crypto.Hash.Algorithms
-- >>> import Data.ByteString

----------------------------------------------------------------

type PublicKey group = Point group
type SecretKey group = Scalar group

----------------------------------------------------------------

serializePublicKey
    :: EllipticCurve group
    => Proxy group -> PublicKey group -> EncodedPublicKey
serializePublicKey :: forall group.
EllipticCurve group =>
Proxy group -> PublicKey group -> EncodedPublicKey
serializePublicKey Proxy group
proxy PublicKey group
pk = ByteString -> EncodedPublicKey
EncodedPublicKey (ByteString -> EncodedPublicKey) -> ByteString -> EncodedPublicKey
forall a b. (a -> b) -> a -> b
$ Proxy group -> PublicKey group -> ByteString
forall curve bs (proxy :: * -> *).
(EllipticCurve curve, ByteArray bs) =>
proxy curve -> Point curve -> bs
forall bs (proxy :: * -> *).
ByteArray bs =>
proxy group -> PublicKey group -> bs
encodePoint Proxy group
proxy PublicKey group
pk

deserializePublicKey
    :: EllipticCurve group
    => Proxy group -> EncodedPublicKey -> Either HPKEError (PublicKey group)
deserializePublicKey :: forall group.
EllipticCurve group =>
Proxy group
-> EncodedPublicKey -> Either HPKEError (PublicKey group)
deserializePublicKey Proxy group
proxy (EncodedPublicKey ByteString
pkm) =
    case Proxy group -> ByteString -> CryptoFailable (PublicKey group)
forall curve bs (proxy :: * -> *).
(EllipticCurve curve, ByteArray bs) =>
proxy curve -> bs -> CryptoFailable (Point curve)
forall bs (proxy :: * -> *).
ByteArray bs =>
proxy group -> bs -> CryptoFailable (PublicKey group)
decodePoint Proxy group
proxy ByteString
pkm of
        CryptoPassed PublicKey group
a -> PublicKey group -> Either HPKEError (PublicKey group)
forall a b. b -> Either a b
Right PublicKey group
a
        CryptoFailed CryptoError
_ -> HPKEError -> Either HPKEError (PublicKey group)
forall a b. a -> Either a b
Left (HPKEError -> Either HPKEError (PublicKey group))
-> HPKEError -> Either HPKEError (PublicKey group)
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
DeserializeError String
"deserializePublicKey"

serializeSecretKey
    :: EllipticCurve group
    => Proxy group -> SecretKey group -> EncodedSecretKey
serializeSecretKey :: forall group.
EllipticCurve group =>
Proxy group -> SecretKey group -> EncodedSecretKey
serializeSecretKey Proxy group
proxy SecretKey group
pk = ByteString -> EncodedSecretKey
EncodedSecretKey (ByteString -> EncodedSecretKey) -> ByteString -> EncodedSecretKey
forall a b. (a -> b) -> a -> b
$ Proxy group -> SecretKey group -> ByteString
forall curve bs (proxy :: * -> *).
(EllipticCurve curve, ByteArray bs) =>
proxy curve -> Scalar curve -> bs
forall bs (proxy :: * -> *).
ByteArray bs =>
proxy group -> SecretKey group -> bs
encodeScalar Proxy group
proxy SecretKey group
pk

deserializeSecretKey
    :: EllipticCurve group
    => Proxy group -> EncodedSecretKey -> Either HPKEError (SecretKey group)
deserializeSecretKey :: forall group.
EllipticCurve group =>
Proxy group
-> EncodedSecretKey -> Either HPKEError (SecretKey group)
deserializeSecretKey Proxy group
proxy (EncodedSecretKey ByteString
pkm) =
    case Proxy group -> ByteString -> CryptoFailable (SecretKey group)
forall curve bs (proxy :: * -> *).
(EllipticCurve curve, ByteArray bs) =>
proxy curve -> bs -> CryptoFailable (Scalar curve)
forall bs (proxy :: * -> *).
ByteArray bs =>
proxy group -> bs -> CryptoFailable (SecretKey group)
decodeScalar Proxy group
proxy ByteString
pkm of
        CryptoPassed SecretKey group
a -> SecretKey group -> Either HPKEError (SecretKey group)
forall a b. b -> Either a b
Right SecretKey group
a
        CryptoFailed CryptoError
_ -> HPKEError -> Either HPKEError (SecretKey group)
forall a b. a -> Either a b
Left (HPKEError -> Either HPKEError (SecretKey group))
-> HPKEError -> Either HPKEError (SecretKey group)
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
DeserializeError String
"deserializeSecretKey"