-- |
-- Module      : Crypto.ECC
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- Elliptic Curve Cryptography
--
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Crypto.ECC
    ( Curve_P256R1(..)
    , Curve_P384R1(..)
    , Curve_P521R1(..)
    , Curve_X25519(..)
    , Curve_X448(..)
    , Curve_Edwards25519(..)
    , EllipticCurve(..)
    , EllipticCurveDH(..)
    , EllipticCurveArith(..)
    , EllipticCurveBasepointArith(..)
    , KeyPair(..)
    , SharedSecret(..)
    ) where

import qualified Crypto.PubKey.ECC.P256 as P256
import qualified Crypto.ECC.Edwards25519 as Edwards25519
import qualified Crypto.ECC.Simple.Types as Simple
import qualified Crypto.ECC.Simple.Prim as Simple
import           Crypto.Random
import           Crypto.Error
import           Crypto.Internal.Imports
import           Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes)
import qualified Crypto.Internal.ByteArray as B
import           Crypto.Number.Basic (numBits)
import           Crypto.Number.Serialize (i2ospOf_, os2ip)
import qualified Crypto.Number.Serialize.LE as LE
import qualified Crypto.PubKey.Curve25519 as X25519
import qualified Crypto.PubKey.Curve448 as X448
import           Data.ByteArray (convert)
import           Data.Data (Data())
import           Data.Kind (Type)
import           Data.Proxy

-- | An elliptic curve key pair composed of the private part (a scalar), and
-- the associated point.
data KeyPair curve = KeyPair
    { forall curve. KeyPair curve -> Point curve
keypairGetPublic  :: !(Point curve)
    , forall curve. KeyPair curve -> Scalar curve
keypairGetPrivate :: !(Scalar curve)
    }

newtype SharedSecret = SharedSecret ScrubbedBytes
    deriving (SharedSecret -> SharedSecret -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SharedSecret -> SharedSecret -> Bool
$c/= :: SharedSecret -> SharedSecret -> Bool
== :: SharedSecret -> SharedSecret -> Bool
$c== :: SharedSecret -> SharedSecret -> Bool
Eq, SharedSecret -> Int
forall p. SharedSecret -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. SharedSecret -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: forall p. SharedSecret -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. SharedSecret -> Ptr p -> IO ()
withByteArray :: forall p a. SharedSecret -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. SharedSecret -> (Ptr p -> IO a) -> IO a
length :: SharedSecret -> Int
$clength :: SharedSecret -> Int
ByteArrayAccess, SharedSecret -> ()
forall a. (a -> ()) -> NFData a
rnf :: SharedSecret -> ()
$crnf :: SharedSecret -> ()
NFData)

class EllipticCurve curve where
    -- | Point on an Elliptic Curve
    type Point curve  :: Type

    -- | Scalar in the Elliptic Curve domain
    type Scalar curve :: Type

    -- | Generate a new random scalar on the curve.
    -- The scalar will represent a number between 1 and the order of the curve non included
    curveGenerateScalar :: MonadRandom randomly => proxy curve -> randomly (Scalar curve)

    -- | Generate a new random keypair
    curveGenerateKeyPair :: MonadRandom randomly => proxy curve -> randomly (KeyPair curve)

    -- | Get the curve size in bits
    curveSizeBits :: proxy curve -> Int

    -- | Encode a elliptic curve point into binary form
    encodePoint :: ByteArray bs => proxy curve -> Point curve -> bs

    -- | Try to decode the binary form of an elliptic curve point
    decodePoint :: ByteArray bs => proxy curve -> bs -> CryptoFailable (Point curve)

class EllipticCurve curve => EllipticCurveDH curve where
    -- | Generate a Diffie hellman secret value.
    --
    -- This is generally just the .x coordinate of the resulting point, that
    -- is not hashed.
    --
    -- use `pointSmul` to keep the result in Point format.
    --
    -- /WARNING:/ Curve implementations may return a special value or an
    -- exception when the public point lies in a subgroup of small order.
    -- This function is adequate when the scalar is in expected range and
    -- contributory behaviour is not needed.  Otherwise use 'ecdh'.
    ecdhRaw :: proxy curve -> Scalar curve -> Point curve -> SharedSecret
    ecdhRaw proxy curve
prx Scalar curve
s = forall a. CryptoFailable a -> a
throwCryptoError forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall curve (proxy :: * -> *).
EllipticCurveDH curve =>
proxy curve
-> Scalar curve -> Point curve -> CryptoFailable SharedSecret
ecdh proxy curve
prx Scalar curve
s

    -- | Generate a Diffie hellman secret value and verify that the result
    -- is not the point at infinity.
    --
    -- This additional test avoids risks existing with function 'ecdhRaw'.
    -- Implementations always return a 'CryptoError' instead of a special
    -- value or an exception.
    ecdh :: proxy curve -> Scalar curve -> Point curve -> CryptoFailable SharedSecret

class (EllipticCurve curve, Eq (Point curve)) => EllipticCurveArith curve where
    -- | Add points on a curve
    pointAdd :: proxy curve -> Point curve -> Point curve -> Point curve

    -- | Negate a curve point
    pointNegate :: proxy curve -> Point curve -> Point curve

    -- | Scalar Multiplication on a curve
    pointSmul :: proxy curve -> Scalar curve -> Point curve -> Point curve

--   -- | Scalar Inverse
--   scalarInverse :: Scalar curve -> Scalar curve

class (EllipticCurveArith curve, Eq (Scalar curve)) => EllipticCurveBasepointArith curve where
    -- | Get the curve order size in bits
    curveOrderBits :: proxy curve -> Int

    -- | Multiply a scalar with the curve base point
    pointBaseSmul :: proxy curve -> Scalar curve -> Point curve

    -- | Multiply the point @p@ with @s2@ and add a lifted to curve value @s1@
    pointsSmulVarTime :: proxy curve -> Scalar curve -> Scalar curve -> Point curve -> Point curve
    pointsSmulVarTime proxy curve
prx Scalar curve
s1 Scalar curve
s2 Point curve
p = forall curve (proxy :: * -> *).
EllipticCurveArith curve =>
proxy curve -> Point curve -> Point curve -> Point curve
pointAdd proxy curve
prx (forall curve (proxy :: * -> *).
EllipticCurveBasepointArith curve =>
proxy curve -> Scalar curve -> Point curve
pointBaseSmul proxy curve
prx Scalar curve
s1) (forall curve (proxy :: * -> *).
EllipticCurveArith curve =>
proxy curve -> Scalar curve -> Point curve -> Point curve
pointSmul proxy curve
prx Scalar curve
s2 Point curve
p)

    -- | Encode an elliptic curve scalar into big-endian form
    encodeScalar :: ByteArray bs => proxy curve -> Scalar curve -> bs

    -- | Try to decode the big-endian form of an elliptic curve scalar
    decodeScalar :: ByteArray bs => proxy curve -> bs -> CryptoFailable (Scalar curve)

    -- | Convert an elliptic curve scalar to an integer
    scalarToInteger :: proxy curve -> Scalar curve -> Integer

    -- | Try to create an elliptic curve scalar from an integer
    scalarFromInteger :: proxy curve -> Integer -> CryptoFailable (Scalar curve)

    -- | Add two scalars and reduce modulo the curve order
    scalarAdd :: proxy curve -> Scalar curve -> Scalar curve -> Scalar curve

    -- | Multiply two scalars and reduce modulo the curve order
    scalarMul :: proxy curve -> Scalar curve -> Scalar curve -> Scalar curve

-- | P256 Curve
--
-- also known as P256
data Curve_P256R1 = Curve_P256R1
    deriving (Int -> Curve_P256R1 -> ShowS
[Curve_P256R1] -> ShowS
Curve_P256R1 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Curve_P256R1] -> ShowS
$cshowList :: [Curve_P256R1] -> ShowS
show :: Curve_P256R1 -> String
$cshow :: Curve_P256R1 -> String
showsPrec :: Int -> Curve_P256R1 -> ShowS
$cshowsPrec :: Int -> Curve_P256R1 -> ShowS
Show,Typeable Curve_P256R1
Curve_P256R1 -> DataType
Curve_P256R1 -> Constr
(forall b. Data b => b -> b) -> Curve_P256R1 -> Curve_P256R1
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Curve_P256R1 -> u
forall u. (forall d. Data d => d -> u) -> Curve_P256R1 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P256R1 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P256R1 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P256R1
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P256R1 -> c Curve_P256R1
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P256R1)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P256R1)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P256R1 -> m Curve_P256R1
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_P256R1 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_P256R1 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_P256R1 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_P256R1 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P256R1 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P256R1 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P256R1 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P256R1 -> r
gmapT :: (forall b. Data b => b -> b) -> Curve_P256R1 -> Curve_P256R1
$cgmapT :: (forall b. Data b => b -> b) -> Curve_P256R1 -> Curve_P256R1
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P256R1)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P256R1)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P256R1)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P256R1)
dataTypeOf :: Curve_P256R1 -> DataType
$cdataTypeOf :: Curve_P256R1 -> DataType
toConstr :: Curve_P256R1 -> Constr
$ctoConstr :: Curve_P256R1 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P256R1
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P256R1
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P256R1 -> c Curve_P256R1
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P256R1 -> c Curve_P256R1
Data)

instance EllipticCurve Curve_P256R1 where
    type Point Curve_P256R1 = P256.Point
    type Scalar Curve_P256R1 = P256.Scalar
    curveSizeBits :: forall (proxy :: * -> *). proxy Curve_P256R1 -> Int
curveSizeBits proxy Curve_P256R1
_ = Int
256
    curveGenerateScalar :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_P256R1 -> randomly (Scalar Curve_P256R1)
curveGenerateScalar proxy Curve_P256R1
_ = forall (randomly :: * -> *).
MonadRandom randomly =>
randomly Scalar
P256.scalarGenerate
    curveGenerateKeyPair :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_P256R1 -> randomly (KeyPair Curve_P256R1)
curveGenerateKeyPair proxy Curve_P256R1
_ = forall {curve}.
(Point curve ~ Point, Scalar curve ~ Scalar) =>
Scalar -> KeyPair curve
toKeyPair forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (randomly :: * -> *).
MonadRandom randomly =>
randomly Scalar
P256.scalarGenerate
      where toKeyPair :: Scalar -> KeyPair curve
toKeyPair Scalar
scalar = forall curve. Point curve -> Scalar curve -> KeyPair curve
KeyPair (Scalar -> Point
P256.toPoint Scalar
scalar) Scalar
scalar
    encodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P256R1 -> Point Curve_P256R1 -> bs
encodePoint proxy Curve_P256R1
_ Point Curve_P256R1
p = forall bs. ByteArray bs => bs
mxy
      where
        mxy :: forall bs. ByteArray bs => bs
        mxy :: forall bs. ByteArray bs => bs
mxy = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
B.concat [bs
uncompressed, bs
xy]
          where
            uncompressed, xy :: bs
            uncompressed :: bs
uncompressed = forall a. ByteArray a => Word8 -> a
B.singleton Word8
4
            xy :: bs
xy = forall ba. ByteArray ba => Point -> ba
P256.pointToBinary Point Curve_P256R1
p
    decodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P256R1 -> bs -> CryptoFailable (Point Curve_P256R1)
decodePoint proxy Curve_P256R1
_ bs
mxy = case forall a. ByteArray a => a -> Maybe (Word8, a)
B.uncons bs
mxy of
        Maybe (Word8, bs)
Nothing -> forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_PointSizeInvalid
        Just (Word8
m,bs
xy)
            -- uncompressed
            | Word8
m forall a. Eq a => a -> a -> Bool
== Word8
4 -> forall ba. ByteArrayAccess ba => ba -> CryptoFailable Point
P256.pointFromBinary bs
xy
            | Bool
otherwise -> forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_PointFormatInvalid

instance EllipticCurveArith Curve_P256R1 where
    pointAdd :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Point Curve_P256R1 -> Point Curve_P256R1 -> Point Curve_P256R1
pointAdd  proxy Curve_P256R1
_ Point Curve_P256R1
a Point Curve_P256R1
b = Point -> Point -> Point
P256.pointAdd Point Curve_P256R1
a Point Curve_P256R1
b
    pointNegate :: forall (proxy :: * -> *).
proxy Curve_P256R1 -> Point Curve_P256R1 -> Point Curve_P256R1
pointNegate proxy Curve_P256R1
_ Point Curve_P256R1
p = Point -> Point
P256.pointNegate Point Curve_P256R1
p
    pointSmul :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Scalar Curve_P256R1 -> Point Curve_P256R1 -> Point Curve_P256R1
pointSmul proxy Curve_P256R1
_ Scalar Curve_P256R1
s Point Curve_P256R1
p = Scalar -> Point -> Point
P256.pointMul Scalar Curve_P256R1
s Point Curve_P256R1
p

instance EllipticCurveDH Curve_P256R1 where
    ecdhRaw :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Scalar Curve_P256R1 -> Point Curve_P256R1 -> SharedSecret
ecdhRaw proxy Curve_P256R1
_ Scalar Curve_P256R1
s Point Curve_P256R1
p = ScrubbedBytes -> SharedSecret
SharedSecret forall a b. (a -> b) -> a -> b
$ forall binary. ByteArray binary => Scalar -> Point -> binary
P256.pointDh Scalar Curve_P256R1
s Point Curve_P256R1
p
    ecdh :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Scalar Curve_P256R1
-> Point Curve_P256R1
-> CryptoFailable SharedSecret
ecdh  proxy Curve_P256R1
prx Scalar Curve_P256R1
s Point Curve_P256R1
p = SharedSecret -> CryptoFailable SharedSecret
checkNonZeroDH (forall curve (proxy :: * -> *).
EllipticCurveDH curve =>
proxy curve -> Scalar curve -> Point curve -> SharedSecret
ecdhRaw proxy Curve_P256R1
prx Scalar Curve_P256R1
s Point Curve_P256R1
p)

instance EllipticCurveBasepointArith Curve_P256R1 where
    curveOrderBits :: forall (proxy :: * -> *). proxy Curve_P256R1 -> Int
curveOrderBits proxy Curve_P256R1
_ = Int
256
    pointBaseSmul :: forall (proxy :: * -> *).
proxy Curve_P256R1 -> Scalar Curve_P256R1 -> Point Curve_P256R1
pointBaseSmul proxy Curve_P256R1
_ = Scalar -> Point
P256.toPoint
    pointsSmulVarTime :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Scalar Curve_P256R1
-> Scalar Curve_P256R1
-> Point Curve_P256R1
-> Point Curve_P256R1
pointsSmulVarTime proxy Curve_P256R1
_ = Scalar -> Scalar -> Point -> Point
P256.pointsMulVarTime
    encodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P256R1 -> Scalar Curve_P256R1 -> bs
encodeScalar proxy Curve_P256R1
_ = forall ba. ByteArray ba => Scalar -> ba
P256.scalarToBinary
    decodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P256R1 -> bs -> CryptoFailable (Scalar Curve_P256R1)
decodeScalar proxy Curve_P256R1
_ = forall ba. ByteArrayAccess ba => ba -> CryptoFailable Scalar
P256.scalarFromBinary
    scalarToInteger :: forall (proxy :: * -> *).
proxy Curve_P256R1 -> Scalar Curve_P256R1 -> Integer
scalarToInteger proxy Curve_P256R1
_ = Scalar -> Integer
P256.scalarToInteger
    scalarFromInteger :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Integer -> CryptoFailable (Scalar Curve_P256R1)
scalarFromInteger proxy Curve_P256R1
_ = Integer -> CryptoFailable Scalar
P256.scalarFromInteger
    scalarAdd :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Scalar Curve_P256R1
-> Scalar Curve_P256R1
-> Scalar Curve_P256R1
scalarAdd proxy Curve_P256R1
_ = Scalar -> Scalar -> Scalar
P256.scalarAdd
    scalarMul :: forall (proxy :: * -> *).
proxy Curve_P256R1
-> Scalar Curve_P256R1
-> Scalar Curve_P256R1
-> Scalar Curve_P256R1
scalarMul proxy Curve_P256R1
_ = Scalar -> Scalar -> Scalar
P256.scalarMul

data Curve_P384R1 = Curve_P384R1
    deriving (Int -> Curve_P384R1 -> ShowS
[Curve_P384R1] -> ShowS
Curve_P384R1 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Curve_P384R1] -> ShowS
$cshowList :: [Curve_P384R1] -> ShowS
show :: Curve_P384R1 -> String
$cshow :: Curve_P384R1 -> String
showsPrec :: Int -> Curve_P384R1 -> ShowS
$cshowsPrec :: Int -> Curve_P384R1 -> ShowS
Show,Typeable Curve_P384R1
Curve_P384R1 -> DataType
Curve_P384R1 -> Constr
(forall b. Data b => b -> b) -> Curve_P384R1 -> Curve_P384R1
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Curve_P384R1 -> u
forall u. (forall d. Data d => d -> u) -> Curve_P384R1 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P384R1 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P384R1 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P384R1
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P384R1 -> c Curve_P384R1
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P384R1)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P384R1)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P384R1 -> m Curve_P384R1
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_P384R1 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_P384R1 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_P384R1 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_P384R1 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P384R1 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P384R1 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P384R1 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P384R1 -> r
gmapT :: (forall b. Data b => b -> b) -> Curve_P384R1 -> Curve_P384R1
$cgmapT :: (forall b. Data b => b -> b) -> Curve_P384R1 -> Curve_P384R1
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P384R1)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P384R1)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P384R1)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P384R1)
dataTypeOf :: Curve_P384R1 -> DataType
$cdataTypeOf :: Curve_P384R1 -> DataType
toConstr :: Curve_P384R1 -> Constr
$ctoConstr :: Curve_P384R1 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P384R1
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P384R1
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P384R1 -> c Curve_P384R1
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P384R1 -> c Curve_P384R1
Data)

instance EllipticCurve Curve_P384R1 where
    type Point Curve_P384R1 = Simple.Point Simple.SEC_p384r1
    type Scalar Curve_P384R1 = Simple.Scalar Simple.SEC_p384r1
    curveSizeBits :: forall (proxy :: * -> *). proxy Curve_P384R1 -> Int
curveSizeBits proxy Curve_P384R1
_ = Int
384
    curveGenerateScalar :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_P384R1 -> randomly (Scalar Curve_P384R1)
curveGenerateScalar proxy Curve_P384R1
_ = forall (randomly :: * -> *) curve.
(MonadRandom randomly, Curve curve) =>
randomly (Scalar curve)
Simple.scalarGenerate
    curveGenerateKeyPair :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_P384R1 -> randomly (KeyPair Curve_P384R1)
curveGenerateKeyPair proxy Curve_P384R1
_ = forall {curve} {curve}.
(Point curve ~ Point curve, Scalar curve ~ Scalar curve,
 Curve curve) =>
Scalar curve -> KeyPair curve
toKeyPair forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (randomly :: * -> *) curve.
(MonadRandom randomly, Curve curve) =>
randomly (Scalar curve)
Simple.scalarGenerate
      where toKeyPair :: Scalar curve -> KeyPair curve
toKeyPair Scalar curve
scalar = forall curve. Point curve -> Scalar curve -> KeyPair curve
KeyPair (forall curve. Curve curve => Scalar curve -> Point curve
Simple.pointBaseMul Scalar curve
scalar) Scalar curve
scalar
    encodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P384R1 -> Point Curve_P384R1 -> bs
encodePoint proxy Curve_P384R1
_ Point Curve_P384R1
point = forall curve bs. (Curve curve, ByteArray bs) => Point curve -> bs
encodeECPoint Point Curve_P384R1
point
    decodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P384R1 -> bs -> CryptoFailable (Point Curve_P384R1)
decodePoint proxy Curve_P384R1
_ bs
bs = forall curve bs.
(Curve curve, ByteArray bs) =>
bs -> CryptoFailable (Point curve)
decodeECPoint bs
bs

instance EllipticCurveArith Curve_P384R1 where
    pointAdd :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Point Curve_P384R1 -> Point Curve_P384R1 -> Point Curve_P384R1
pointAdd proxy Curve_P384R1
_ Point Curve_P384R1
a Point Curve_P384R1
b = forall curve.
Curve curve =>
Point curve -> Point curve -> Point curve
Simple.pointAdd Point Curve_P384R1
a Point Curve_P384R1
b
    pointNegate :: forall (proxy :: * -> *).
proxy Curve_P384R1 -> Point Curve_P384R1 -> Point Curve_P384R1
pointNegate proxy Curve_P384R1
_ Point Curve_P384R1
p = forall curve. Curve curve => Point curve -> Point curve
Simple.pointNegate Point Curve_P384R1
p
    pointSmul :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Scalar Curve_P384R1 -> Point Curve_P384R1 -> Point Curve_P384R1
pointSmul proxy Curve_P384R1
_ Scalar Curve_P384R1
s Point Curve_P384R1
p = forall curve.
Curve curve =>
Scalar curve -> Point curve -> Point curve
Simple.pointMul Scalar Curve_P384R1
s Point Curve_P384R1
p

instance EllipticCurveDH Curve_P384R1 where
    ecdh :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Scalar Curve_P384R1
-> Point Curve_P384R1
-> CryptoFailable SharedSecret
ecdh proxy Curve_P384R1
_ Scalar Curve_P384R1
s Point Curve_P384R1
p = forall curve.
Curve curve =>
Proxy curve -> Point curve -> CryptoFailable SharedSecret
encodeECShared Proxy SEC_p384r1
prx (forall curve.
Curve curve =>
Scalar curve -> Point curve -> Point curve
Simple.pointMul Scalar Curve_P384R1
s Point Curve_P384R1
p)
      where
        prx :: Proxy SEC_p384r1
prx = forall {k} (t :: k). Proxy t
Proxy :: Proxy Simple.SEC_p384r1

instance EllipticCurveBasepointArith Curve_P384R1 where
    curveOrderBits :: forall (proxy :: * -> *). proxy Curve_P384R1 -> Int
curveOrderBits proxy Curve_P384R1
_ = Int
384
    pointBaseSmul :: forall (proxy :: * -> *).
proxy Curve_P384R1 -> Scalar Curve_P384R1 -> Point Curve_P384R1
pointBaseSmul proxy Curve_P384R1
_ = forall curve. Curve curve => Scalar curve -> Point curve
Simple.pointBaseMul
    pointsSmulVarTime :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Scalar Curve_P384R1
-> Scalar Curve_P384R1
-> Point Curve_P384R1
-> Point Curve_P384R1
pointsSmulVarTime proxy Curve_P384R1
_ = forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Point curve -> Point curve
ecPointsMulVarTime
    encodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P384R1 -> Scalar Curve_P384R1 -> bs
encodeScalar proxy Curve_P384R1
_ = forall curve bs. (Curve curve, ByteArray bs) => Scalar curve -> bs
ecScalarToBinary
    decodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P384R1 -> bs -> CryptoFailable (Scalar Curve_P384R1)
decodeScalar proxy Curve_P384R1
_ = forall curve bs.
(Curve curve, ByteArrayAccess bs) =>
bs -> CryptoFailable (Scalar curve)
ecScalarFromBinary
    scalarToInteger :: forall (proxy :: * -> *).
proxy Curve_P384R1 -> Scalar Curve_P384R1 -> Integer
scalarToInteger proxy Curve_P384R1
_ = forall curve. Scalar curve -> Integer
ecScalarToInteger
    scalarFromInteger :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Integer -> CryptoFailable (Scalar Curve_P384R1)
scalarFromInteger proxy Curve_P384R1
_ = forall curve.
Curve curve =>
Integer -> CryptoFailable (Scalar curve)
ecScalarFromInteger
    scalarAdd :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Scalar Curve_P384R1
-> Scalar Curve_P384R1
-> Scalar Curve_P384R1
scalarAdd proxy Curve_P384R1
_ = forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Scalar curve
ecScalarAdd
    scalarMul :: forall (proxy :: * -> *).
proxy Curve_P384R1
-> Scalar Curve_P384R1
-> Scalar Curve_P384R1
-> Scalar Curve_P384R1
scalarMul proxy Curve_P384R1
_ = forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Scalar curve
ecScalarMul

data Curve_P521R1 = Curve_P521R1
    deriving (Int -> Curve_P521R1 -> ShowS
[Curve_P521R1] -> ShowS
Curve_P521R1 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Curve_P521R1] -> ShowS
$cshowList :: [Curve_P521R1] -> ShowS
show :: Curve_P521R1 -> String
$cshow :: Curve_P521R1 -> String
showsPrec :: Int -> Curve_P521R1 -> ShowS
$cshowsPrec :: Int -> Curve_P521R1 -> ShowS
Show,Typeable Curve_P521R1
Curve_P521R1 -> DataType
Curve_P521R1 -> Constr
(forall b. Data b => b -> b) -> Curve_P521R1 -> Curve_P521R1
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Curve_P521R1 -> u
forall u. (forall d. Data d => d -> u) -> Curve_P521R1 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P521R1 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P521R1 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P521R1
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P521R1 -> c Curve_P521R1
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P521R1)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P521R1)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_P521R1 -> m Curve_P521R1
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_P521R1 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_P521R1 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_P521R1 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_P521R1 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P521R1 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P521R1 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P521R1 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_P521R1 -> r
gmapT :: (forall b. Data b => b -> b) -> Curve_P521R1 -> Curve_P521R1
$cgmapT :: (forall b. Data b => b -> b) -> Curve_P521R1 -> Curve_P521R1
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P521R1)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_P521R1)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P521R1)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_P521R1)
dataTypeOf :: Curve_P521R1 -> DataType
$cdataTypeOf :: Curve_P521R1 -> DataType
toConstr :: Curve_P521R1 -> Constr
$ctoConstr :: Curve_P521R1 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P521R1
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_P521R1
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P521R1 -> c Curve_P521R1
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_P521R1 -> c Curve_P521R1
Data)

instance EllipticCurve Curve_P521R1 where
    type Point Curve_P521R1 = Simple.Point Simple.SEC_p521r1
    type Scalar Curve_P521R1 = Simple.Scalar Simple.SEC_p521r1
    curveSizeBits :: forall (proxy :: * -> *). proxy Curve_P521R1 -> Int
curveSizeBits proxy Curve_P521R1
_ = Int
521
    curveGenerateScalar :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_P521R1 -> randomly (Scalar Curve_P521R1)
curveGenerateScalar proxy Curve_P521R1
_ = forall (randomly :: * -> *) curve.
(MonadRandom randomly, Curve curve) =>
randomly (Scalar curve)
Simple.scalarGenerate
    curveGenerateKeyPair :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_P521R1 -> randomly (KeyPair Curve_P521R1)
curveGenerateKeyPair proxy Curve_P521R1
_ = forall {curve} {curve}.
(Point curve ~ Point curve, Scalar curve ~ Scalar curve,
 Curve curve) =>
Scalar curve -> KeyPair curve
toKeyPair forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (randomly :: * -> *) curve.
(MonadRandom randomly, Curve curve) =>
randomly (Scalar curve)
Simple.scalarGenerate
      where toKeyPair :: Scalar curve -> KeyPair curve
toKeyPair Scalar curve
scalar = forall curve. Point curve -> Scalar curve -> KeyPair curve
KeyPair (forall curve. Curve curve => Scalar curve -> Point curve
Simple.pointBaseMul Scalar curve
scalar) Scalar curve
scalar
    encodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P521R1 -> Point Curve_P521R1 -> bs
encodePoint proxy Curve_P521R1
_ Point Curve_P521R1
point = forall curve bs. (Curve curve, ByteArray bs) => Point curve -> bs
encodeECPoint Point Curve_P521R1
point
    decodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P521R1 -> bs -> CryptoFailable (Point Curve_P521R1)
decodePoint proxy Curve_P521R1
_ bs
bs = forall curve bs.
(Curve curve, ByteArray bs) =>
bs -> CryptoFailable (Point curve)
decodeECPoint bs
bs

instance EllipticCurveArith Curve_P521R1 where
    pointAdd :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Point Curve_P521R1 -> Point Curve_P521R1 -> Point Curve_P521R1
pointAdd proxy Curve_P521R1
_ Point Curve_P521R1
a Point Curve_P521R1
b = forall curve.
Curve curve =>
Point curve -> Point curve -> Point curve
Simple.pointAdd Point Curve_P521R1
a Point Curve_P521R1
b
    pointNegate :: forall (proxy :: * -> *).
proxy Curve_P521R1 -> Point Curve_P521R1 -> Point Curve_P521R1
pointNegate proxy Curve_P521R1
_ Point Curve_P521R1
p = forall curve. Curve curve => Point curve -> Point curve
Simple.pointNegate Point Curve_P521R1
p
    pointSmul :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Scalar Curve_P521R1 -> Point Curve_P521R1 -> Point Curve_P521R1
pointSmul proxy Curve_P521R1
_ Scalar Curve_P521R1
s Point Curve_P521R1
p = forall curve.
Curve curve =>
Scalar curve -> Point curve -> Point curve
Simple.pointMul Scalar Curve_P521R1
s Point Curve_P521R1
p

instance EllipticCurveDH Curve_P521R1 where
    ecdh :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Scalar Curve_P521R1
-> Point Curve_P521R1
-> CryptoFailable SharedSecret
ecdh proxy Curve_P521R1
_ Scalar Curve_P521R1
s Point Curve_P521R1
p = forall curve.
Curve curve =>
Proxy curve -> Point curve -> CryptoFailable SharedSecret
encodeECShared Proxy SEC_p521r1
prx (forall curve.
Curve curve =>
Scalar curve -> Point curve -> Point curve
Simple.pointMul Scalar Curve_P521R1
s Point Curve_P521R1
p)
      where
        prx :: Proxy SEC_p521r1
prx = forall {k} (t :: k). Proxy t
Proxy :: Proxy Simple.SEC_p521r1

instance EllipticCurveBasepointArith Curve_P521R1 where
    curveOrderBits :: forall (proxy :: * -> *). proxy Curve_P521R1 -> Int
curveOrderBits proxy Curve_P521R1
_ = Int
521
    pointBaseSmul :: forall (proxy :: * -> *).
proxy Curve_P521R1 -> Scalar Curve_P521R1 -> Point Curve_P521R1
pointBaseSmul proxy Curve_P521R1
_ = forall curve. Curve curve => Scalar curve -> Point curve
Simple.pointBaseMul
    pointsSmulVarTime :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Scalar Curve_P521R1
-> Scalar Curve_P521R1
-> Point Curve_P521R1
-> Point Curve_P521R1
pointsSmulVarTime proxy Curve_P521R1
_ = forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Point curve -> Point curve
ecPointsMulVarTime
    encodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P521R1 -> Scalar Curve_P521R1 -> bs
encodeScalar proxy Curve_P521R1
_ = forall curve bs. (Curve curve, ByteArray bs) => Scalar curve -> bs
ecScalarToBinary
    decodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_P521R1 -> bs -> CryptoFailable (Scalar Curve_P521R1)
decodeScalar proxy Curve_P521R1
_ = forall curve bs.
(Curve curve, ByteArrayAccess bs) =>
bs -> CryptoFailable (Scalar curve)
ecScalarFromBinary
    scalarToInteger :: forall (proxy :: * -> *).
proxy Curve_P521R1 -> Scalar Curve_P521R1 -> Integer
scalarToInteger proxy Curve_P521R1
_ = forall curve. Scalar curve -> Integer
ecScalarToInteger
    scalarFromInteger :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Integer -> CryptoFailable (Scalar Curve_P521R1)
scalarFromInteger proxy Curve_P521R1
_ = forall curve.
Curve curve =>
Integer -> CryptoFailable (Scalar curve)
ecScalarFromInteger
    scalarAdd :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Scalar Curve_P521R1
-> Scalar Curve_P521R1
-> Scalar Curve_P521R1
scalarAdd proxy Curve_P521R1
_ = forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Scalar curve
ecScalarAdd
    scalarMul :: forall (proxy :: * -> *).
proxy Curve_P521R1
-> Scalar Curve_P521R1
-> Scalar Curve_P521R1
-> Scalar Curve_P521R1
scalarMul proxy Curve_P521R1
_ = forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Scalar curve
ecScalarMul

data Curve_X25519 = Curve_X25519
    deriving (Int -> Curve_X25519 -> ShowS
[Curve_X25519] -> ShowS
Curve_X25519 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Curve_X25519] -> ShowS
$cshowList :: [Curve_X25519] -> ShowS
show :: Curve_X25519 -> String
$cshow :: Curve_X25519 -> String
showsPrec :: Int -> Curve_X25519 -> ShowS
$cshowsPrec :: Int -> Curve_X25519 -> ShowS
Show,Typeable Curve_X25519
Curve_X25519 -> DataType
Curve_X25519 -> Constr
(forall b. Data b => b -> b) -> Curve_X25519 -> Curve_X25519
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Curve_X25519 -> u
forall u. (forall d. Data d => d -> u) -> Curve_X25519 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X25519 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X25519 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_X25519
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_X25519 -> c Curve_X25519
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_X25519)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_X25519)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_X25519 -> m Curve_X25519
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_X25519 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_X25519 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_X25519 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_X25519 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X25519 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X25519 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X25519 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X25519 -> r
gmapT :: (forall b. Data b => b -> b) -> Curve_X25519 -> Curve_X25519
$cgmapT :: (forall b. Data b => b -> b) -> Curve_X25519 -> Curve_X25519
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_X25519)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_X25519)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_X25519)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_X25519)
dataTypeOf :: Curve_X25519 -> DataType
$cdataTypeOf :: Curve_X25519 -> DataType
toConstr :: Curve_X25519 -> Constr
$ctoConstr :: Curve_X25519 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_X25519
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_X25519
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_X25519 -> c Curve_X25519
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_X25519 -> c Curve_X25519
Data)

instance EllipticCurve Curve_X25519 where
    type Point Curve_X25519 = X25519.PublicKey
    type Scalar Curve_X25519 = X25519.SecretKey
    curveSizeBits :: forall (proxy :: * -> *). proxy Curve_X25519 -> Int
curveSizeBits proxy Curve_X25519
_ = Int
255
    curveGenerateScalar :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_X25519 -> randomly (Scalar Curve_X25519)
curveGenerateScalar proxy Curve_X25519
_ = forall (m :: * -> *). MonadRandom m => m SecretKey
X25519.generateSecretKey
    curveGenerateKeyPair :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_X25519 -> randomly (KeyPair Curve_X25519)
curveGenerateKeyPair proxy Curve_X25519
_ = do
        SecretKey
s <- forall (m :: * -> *). MonadRandom m => m SecretKey
X25519.generateSecretKey
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall curve. Point curve -> Scalar curve -> KeyPair curve
KeyPair (SecretKey -> PublicKey
X25519.toPublic SecretKey
s) SecretKey
s
    encodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_X25519 -> Point Curve_X25519 -> bs
encodePoint proxy Curve_X25519
_ Point Curve_X25519
p = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert Point Curve_X25519
p
    decodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_X25519 -> bs -> CryptoFailable (Point Curve_X25519)
decodePoint proxy Curve_X25519
_ bs
bs = forall bs. ByteArrayAccess bs => bs -> CryptoFailable PublicKey
X25519.publicKey bs
bs

instance EllipticCurveDH Curve_X25519 where
    ecdhRaw :: forall (proxy :: * -> *).
proxy Curve_X25519
-> Scalar Curve_X25519 -> Point Curve_X25519 -> SharedSecret
ecdhRaw proxy Curve_X25519
_ Scalar Curve_X25519
s Point Curve_X25519
p = ScrubbedBytes -> SharedSecret
SharedSecret forall a b. (a -> b) -> a -> b
$ forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert DhSecret
secret
      where secret :: DhSecret
secret = PublicKey -> SecretKey -> DhSecret
X25519.dh Point Curve_X25519
p Scalar Curve_X25519
s
    ecdh :: forall (proxy :: * -> *).
proxy Curve_X25519
-> Scalar Curve_X25519
-> Point Curve_X25519
-> CryptoFailable SharedSecret
ecdh proxy Curve_X25519
prx Scalar Curve_X25519
s Point Curve_X25519
p = SharedSecret -> CryptoFailable SharedSecret
checkNonZeroDH (forall curve (proxy :: * -> *).
EllipticCurveDH curve =>
proxy curve -> Scalar curve -> Point curve -> SharedSecret
ecdhRaw proxy Curve_X25519
prx Scalar Curve_X25519
s Point Curve_X25519
p)

data Curve_X448 = Curve_X448
    deriving (Int -> Curve_X448 -> ShowS
[Curve_X448] -> ShowS
Curve_X448 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Curve_X448] -> ShowS
$cshowList :: [Curve_X448] -> ShowS
show :: Curve_X448 -> String
$cshow :: Curve_X448 -> String
showsPrec :: Int -> Curve_X448 -> ShowS
$cshowsPrec :: Int -> Curve_X448 -> ShowS
Show,Typeable Curve_X448
Curve_X448 -> DataType
Curve_X448 -> Constr
(forall b. Data b => b -> b) -> Curve_X448 -> Curve_X448
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Curve_X448 -> u
forall u. (forall d. Data d => d -> u) -> Curve_X448 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X448 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X448 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_X448
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_X448 -> c Curve_X448
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_X448)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Curve_X448)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Curve_X448 -> m Curve_X448
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_X448 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Curve_X448 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_X448 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_X448 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X448 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X448 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X448 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_X448 -> r
gmapT :: (forall b. Data b => b -> b) -> Curve_X448 -> Curve_X448
$cgmapT :: (forall b. Data b => b -> b) -> Curve_X448 -> Curve_X448
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Curve_X448)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Curve_X448)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_X448)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_X448)
dataTypeOf :: Curve_X448 -> DataType
$cdataTypeOf :: Curve_X448 -> DataType
toConstr :: Curve_X448 -> Constr
$ctoConstr :: Curve_X448 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_X448
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_X448
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_X448 -> c Curve_X448
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Curve_X448 -> c Curve_X448
Data)

instance EllipticCurve Curve_X448 where
    type Point Curve_X448 = X448.PublicKey
    type Scalar Curve_X448 = X448.SecretKey
    curveSizeBits :: forall (proxy :: * -> *). proxy Curve_X448 -> Int
curveSizeBits proxy Curve_X448
_ = Int
448
    curveGenerateScalar :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_X448 -> randomly (Scalar Curve_X448)
curveGenerateScalar proxy Curve_X448
_ = forall (m :: * -> *). MonadRandom m => m SecretKey
X448.generateSecretKey
    curveGenerateKeyPair :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_X448 -> randomly (KeyPair Curve_X448)
curveGenerateKeyPair proxy Curve_X448
_ = do
        SecretKey
s <- forall (m :: * -> *). MonadRandom m => m SecretKey
X448.generateSecretKey
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall curve. Point curve -> Scalar curve -> KeyPair curve
KeyPair (SecretKey -> PublicKey
X448.toPublic SecretKey
s) SecretKey
s
    encodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_X448 -> Point Curve_X448 -> bs
encodePoint proxy Curve_X448
_ Point Curve_X448
p = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert Point Curve_X448
p
    decodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_X448 -> bs -> CryptoFailable (Point Curve_X448)
decodePoint proxy Curve_X448
_ bs
bs = forall bs. ByteArrayAccess bs => bs -> CryptoFailable PublicKey
X448.publicKey bs
bs

instance EllipticCurveDH Curve_X448 where
    ecdhRaw :: forall (proxy :: * -> *).
proxy Curve_X448
-> Scalar Curve_X448 -> Point Curve_X448 -> SharedSecret
ecdhRaw proxy Curve_X448
_ Scalar Curve_X448
s Point Curve_X448
p = ScrubbedBytes -> SharedSecret
SharedSecret forall a b. (a -> b) -> a -> b
$ forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert DhSecret
secret
      where secret :: DhSecret
secret = PublicKey -> SecretKey -> DhSecret
X448.dh Point Curve_X448
p Scalar Curve_X448
s
    ecdh :: forall (proxy :: * -> *).
proxy Curve_X448
-> Scalar Curve_X448
-> Point Curve_X448
-> CryptoFailable SharedSecret
ecdh proxy Curve_X448
prx Scalar Curve_X448
s Point Curve_X448
p = SharedSecret -> CryptoFailable SharedSecret
checkNonZeroDH (forall curve (proxy :: * -> *).
EllipticCurveDH curve =>
proxy curve -> Scalar curve -> Point curve -> SharedSecret
ecdhRaw proxy Curve_X448
prx Scalar Curve_X448
s Point Curve_X448
p)

data Curve_Edwards25519 = Curve_Edwards25519
    deriving (Int -> Curve_Edwards25519 -> ShowS
[Curve_Edwards25519] -> ShowS
Curve_Edwards25519 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Curve_Edwards25519] -> ShowS
$cshowList :: [Curve_Edwards25519] -> ShowS
show :: Curve_Edwards25519 -> String
$cshow :: Curve_Edwards25519 -> String
showsPrec :: Int -> Curve_Edwards25519 -> ShowS
$cshowsPrec :: Int -> Curve_Edwards25519 -> ShowS
Show,Typeable Curve_Edwards25519
Curve_Edwards25519 -> DataType
Curve_Edwards25519 -> Constr
(forall b. Data b => b -> b)
-> Curve_Edwards25519 -> Curve_Edwards25519
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> Curve_Edwards25519 -> u
forall u. (forall d. Data d => d -> u) -> Curve_Edwards25519 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_Edwards25519 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_Edwards25519 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_Edwards25519
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Curve_Edwards25519
-> c Curve_Edwards25519
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_Edwards25519)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_Edwards25519)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Curve_Edwards25519 -> m Curve_Edwards25519
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> Curve_Edwards25519 -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> Curve_Edwards25519 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_Edwards25519 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Curve_Edwards25519 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_Edwards25519 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_Edwards25519 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_Edwards25519 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Curve_Edwards25519 -> r
gmapT :: (forall b. Data b => b -> b)
-> Curve_Edwards25519 -> Curve_Edwards25519
$cgmapT :: (forall b. Data b => b -> b)
-> Curve_Edwards25519 -> Curve_Edwards25519
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_Edwards25519)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Curve_Edwards25519)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_Edwards25519)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Curve_Edwards25519)
dataTypeOf :: Curve_Edwards25519 -> DataType
$cdataTypeOf :: Curve_Edwards25519 -> DataType
toConstr :: Curve_Edwards25519 -> Constr
$ctoConstr :: Curve_Edwards25519 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_Edwards25519
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Curve_Edwards25519
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Curve_Edwards25519
-> c Curve_Edwards25519
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Curve_Edwards25519
-> c Curve_Edwards25519
Data)

instance EllipticCurve Curve_Edwards25519 where
    type Point Curve_Edwards25519 = Edwards25519.Point
    type Scalar Curve_Edwards25519 = Edwards25519.Scalar
    curveSizeBits :: forall (proxy :: * -> *). proxy Curve_Edwards25519 -> Int
curveSizeBits proxy Curve_Edwards25519
_ = Int
255
    curveGenerateScalar :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_Edwards25519 -> randomly (Scalar Curve_Edwards25519)
curveGenerateScalar proxy Curve_Edwards25519
_ = forall (randomly :: * -> *).
MonadRandom randomly =>
randomly Scalar
Edwards25519.scalarGenerate
    curveGenerateKeyPair :: forall (randomly :: * -> *) (proxy :: * -> *).
MonadRandom randomly =>
proxy Curve_Edwards25519 -> randomly (KeyPair Curve_Edwards25519)
curveGenerateKeyPair proxy Curve_Edwards25519
_ = forall {curve}.
(Point curve ~ Point, Scalar curve ~ Scalar) =>
Scalar -> KeyPair curve
toKeyPair forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (randomly :: * -> *).
MonadRandom randomly =>
randomly Scalar
Edwards25519.scalarGenerate
      where toKeyPair :: Scalar -> KeyPair curve
toKeyPair Scalar
scalar = forall curve. Point curve -> Scalar curve -> KeyPair curve
KeyPair (Scalar -> Point
Edwards25519.toPoint Scalar
scalar) Scalar
scalar
    encodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_Edwards25519 -> Point Curve_Edwards25519 -> bs
encodePoint proxy Curve_Edwards25519
_ Point Curve_Edwards25519
point = forall bs. ByteArray bs => Point -> bs
Edwards25519.pointEncode Point Curve_Edwards25519
point
    decodePoint :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_Edwards25519
-> bs -> CryptoFailable (Point Curve_Edwards25519)
decodePoint proxy Curve_Edwards25519
_ bs
bs = forall bs. ByteArrayAccess bs => bs -> CryptoFailable Point
Edwards25519.pointDecode bs
bs

instance EllipticCurveArith Curve_Edwards25519 where
    pointAdd :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Point Curve_Edwards25519
-> Point Curve_Edwards25519
-> Point Curve_Edwards25519
pointAdd proxy Curve_Edwards25519
_ Point Curve_Edwards25519
a Point Curve_Edwards25519
b = Point -> Point -> Point
Edwards25519.pointAdd Point Curve_Edwards25519
a Point Curve_Edwards25519
b
    pointNegate :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Point Curve_Edwards25519 -> Point Curve_Edwards25519
pointNegate proxy Curve_Edwards25519
_ Point Curve_Edwards25519
p = Point -> Point
Edwards25519.pointNegate Point Curve_Edwards25519
p
    pointSmul :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Point Curve_Edwards25519
-> Point Curve_Edwards25519
pointSmul proxy Curve_Edwards25519
_ Scalar Curve_Edwards25519
s Point Curve_Edwards25519
p = Scalar -> Point -> Point
Edwards25519.pointMul Scalar Curve_Edwards25519
s Point Curve_Edwards25519
p

instance EllipticCurveBasepointArith Curve_Edwards25519 where
    curveOrderBits :: forall (proxy :: * -> *). proxy Curve_Edwards25519 -> Int
curveOrderBits proxy Curve_Edwards25519
_ = Int
253
    pointBaseSmul :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Scalar Curve_Edwards25519 -> Point Curve_Edwards25519
pointBaseSmul proxy Curve_Edwards25519
_ = Scalar -> Point
Edwards25519.toPoint
    pointsSmulVarTime :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Point Curve_Edwards25519
-> Point Curve_Edwards25519
pointsSmulVarTime proxy Curve_Edwards25519
_ = Scalar -> Scalar -> Point -> Point
Edwards25519.pointsMulVarTime
    encodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_Edwards25519 -> Scalar Curve_Edwards25519 -> bs
encodeScalar proxy Curve_Edwards25519
_ = forall bs. ByteArray bs => bs -> bs
B.reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bs. ByteArray bs => Scalar -> bs
Edwards25519.scalarEncode
    decodeScalar :: forall bs (proxy :: * -> *).
ByteArray bs =>
proxy Curve_Edwards25519
-> bs -> CryptoFailable (Scalar Curve_Edwards25519)
decodeScalar proxy Curve_Edwards25519
_ bs
bs
        | forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
bs forall a. Eq a => a -> a -> Bool
== Int
32 = forall bs. ByteArrayAccess bs => bs -> CryptoFailable Scalar
Edwards25519.scalarDecodeLong (forall bs. ByteArray bs => bs -> bs
B.reverse bs
bs)
        | Bool
otherwise         = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeySizeInvalid
    scalarToInteger :: forall (proxy :: * -> *).
proxy Curve_Edwards25519 -> Scalar Curve_Edwards25519 -> Integer
scalarToInteger proxy Curve_Edwards25519
_ Scalar Curve_Edwards25519
s = forall ba. ByteArrayAccess ba => ba -> Integer
LE.os2ip (forall bs. ByteArray bs => Scalar -> bs
Edwards25519.scalarEncode Scalar Curve_Edwards25519
s :: B.Bytes)
    scalarFromInteger :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Integer -> CryptoFailable (Scalar Curve_Edwards25519)
scalarFromInteger proxy Curve_Edwards25519
_ Integer
i =
        case forall ba. ByteArray ba => Int -> Integer -> Maybe ba
LE.i2ospOf Int
32 Integer
i of
            Maybe Bytes
Nothing -> forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeySizeInvalid
            Just Bytes
bs -> forall bs. ByteArrayAccess bs => bs -> CryptoFailable Scalar
Edwards25519.scalarDecodeLong (Bytes
bs :: B.Bytes)
    scalarAdd :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Scalar Curve_Edwards25519
scalarAdd proxy Curve_Edwards25519
_ = Scalar -> Scalar -> Scalar
Edwards25519.scalarAdd
    scalarMul :: forall (proxy :: * -> *).
proxy Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Scalar Curve_Edwards25519
-> Scalar Curve_Edwards25519
scalarMul proxy Curve_Edwards25519
_ = Scalar -> Scalar -> Scalar
Edwards25519.scalarMul

checkNonZeroDH :: SharedSecret -> CryptoFailable SharedSecret
checkNonZeroDH :: SharedSecret -> CryptoFailable SharedSecret
checkNonZeroDH s :: SharedSecret
s@(SharedSecret ScrubbedBytes
b)
    | forall ba. ByteArrayAccess ba => ba -> Bool
B.constAllZero ScrubbedBytes
b = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_ScalarMultiplicationInvalid
    | Bool
otherwise        = forall a. a -> CryptoFailable a
CryptoPassed SharedSecret
s

encodeECShared :: Simple.Curve curve => Proxy curve -> Simple.Point curve -> CryptoFailable SharedSecret
encodeECShared :: forall curve.
Curve curve =>
Proxy curve -> Point curve -> CryptoFailable SharedSecret
encodeECShared Proxy curve
_   Point curve
Simple.PointO      = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_ScalarMultiplicationInvalid
encodeECShared Proxy curve
prx (Simple.Point Integer
x Integer
_) = forall a. a -> CryptoFailable a
CryptoPassed forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScrubbedBytes -> SharedSecret
SharedSecret forall a b. (a -> b) -> a -> b
$ forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ (forall curve (proxy :: * -> *). Curve curve => proxy curve -> Int
Simple.curveSizeBytes Proxy curve
prx) Integer
x

encodeECPoint :: forall curve bs . (Simple.Curve curve, ByteArray bs) => Simple.Point curve -> bs
encodeECPoint :: forall curve bs. (Curve curve, ByteArray bs) => Point curve -> bs
encodeECPoint Point curve
Simple.PointO      = forall a. HasCallStack => String -> a
error String
"encodeECPoint: cannot serialize point at infinity"
encodeECPoint (Simple.Point Integer
x Integer
y) = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
B.concat [bs
uncompressed,bs
xb,bs
yb]
  where
    size :: Int
size = forall curve (proxy :: * -> *). Curve curve => proxy curve -> Int
Simple.curveSizeBytes (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)
    uncompressed, xb, yb :: bs
    uncompressed :: bs
uncompressed = forall a. ByteArray a => Word8 -> a
B.singleton Word8
4
    xb :: bs
xb = forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
size Integer
x
    yb :: bs
yb = forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
size Integer
y

decodeECPoint :: (Simple.Curve curve, ByteArray bs) => bs -> CryptoFailable (Simple.Point curve)
decodeECPoint :: forall curve bs.
(Curve curve, ByteArray bs) =>
bs -> CryptoFailable (Point curve)
decodeECPoint bs
mxy = case forall a. ByteArray a => a -> Maybe (Word8, a)
B.uncons bs
mxy of
    Maybe (Word8, bs)
Nothing     -> forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_PointSizeInvalid
    Just (Word8
m,bs
xy)
        -- uncompressed
        | Word8
m forall a. Eq a => a -> a -> Bool
== Word8
4 ->
            let siz :: Int
siz = forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
xy forall a. Integral a => a -> a -> a
`div` Int
2
                (bs
xb,bs
yb) = forall bs. ByteArray bs => Int -> bs -> (bs, bs)
B.splitAt Int
siz bs
xy
                x :: Integer
x = forall ba. ByteArrayAccess ba => ba -> Integer
os2ip bs
xb
                y :: Integer
y = forall ba. ByteArrayAccess ba => ba -> Integer
os2ip bs
yb
             in forall curve.
Curve curve =>
(Integer, Integer) -> CryptoFailable (Point curve)
Simple.pointFromIntegers (Integer
x,Integer
y)
        | Bool
otherwise -> forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_PointFormatInvalid

ecPointsMulVarTime :: forall curve . Simple.Curve curve
                   => Simple.Scalar curve
                   -> Simple.Scalar curve -> Simple.Point curve
                   -> Simple.Point curve
ecPointsMulVarTime :: forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Point curve -> Point curve
ecPointsMulVarTime Scalar curve
n1 = forall curve.
Curve curve =>
Scalar curve
-> Point curve -> Scalar curve -> Point curve -> Point curve
Simple.pointAddTwoMuls Scalar curve
n1 Point curve
g
  where g :: Point curve
g = forall curve. CurveParameters curve -> Point curve
Simple.curveEccG forall a b. (a -> b) -> a -> b
$ forall curve (proxy :: * -> *).
Curve curve =>
proxy curve -> CurveParameters curve
Simple.curveParameters (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)

ecScalarFromBinary :: forall curve bs . (Simple.Curve curve, ByteArrayAccess bs)
                   => bs -> CryptoFailable (Simple.Scalar curve)
ecScalarFromBinary :: forall curve bs.
(Curve curve, ByteArrayAccess bs) =>
bs -> CryptoFailable (Scalar curve)
ecScalarFromBinary bs
ba
    | forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
ba forall a. Eq a => a -> a -> Bool
/= Int
size = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeySizeInvalid
    | Bool
otherwise           = forall a. a -> CryptoFailable a
CryptoPassed (forall curve. Integer -> Scalar curve
Simple.Scalar forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Integer
os2ip bs
ba)
  where size :: Int
size = forall curve (proxy :: * -> *). Curve curve => proxy curve -> Int
ecCurveOrderBytes (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)

ecScalarToBinary :: forall curve bs . (Simple.Curve curve, ByteArray bs)
                 => Simple.Scalar curve -> bs
ecScalarToBinary :: forall curve bs. (Curve curve, ByteArray bs) => Scalar curve -> bs
ecScalarToBinary (Simple.Scalar Integer
s) = forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
size Integer
s
  where size :: Int
size = forall curve (proxy :: * -> *). Curve curve => proxy curve -> Int
ecCurveOrderBytes (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)

ecScalarFromInteger :: forall curve . Simple.Curve curve
                    => Integer -> CryptoFailable (Simple.Scalar curve)
ecScalarFromInteger :: forall curve.
Curve curve =>
Integer -> CryptoFailable (Scalar curve)
ecScalarFromInteger Integer
s
    | Integer -> Int
numBits Integer
s forall a. Ord a => a -> a -> Bool
> Int
nb = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeySizeInvalid
    | Bool
otherwise      = forall a. a -> CryptoFailable a
CryptoPassed (forall curve. Integer -> Scalar curve
Simple.Scalar Integer
s)
  where nb :: Int
nb = Int
8 forall a. Num a => a -> a -> a
* forall curve (proxy :: * -> *). Curve curve => proxy curve -> Int
ecCurveOrderBytes (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)

ecScalarToInteger :: Simple.Scalar curve -> Integer
ecScalarToInteger :: forall curve. Scalar curve -> Integer
ecScalarToInteger (Simple.Scalar Integer
s) = Integer
s

ecCurveOrderBytes :: Simple.Curve c => proxy c -> Int
ecCurveOrderBytes :: forall curve (proxy :: * -> *). Curve curve => proxy curve -> Int
ecCurveOrderBytes proxy c
prx = (Integer -> Int
numBits Integer
n forall a. Num a => a -> a -> a
+ Int
7) forall a. Integral a => a -> a -> a
`div` Int
8
  where n :: Integer
n = forall curve. CurveParameters curve -> Integer
Simple.curveEccN forall a b. (a -> b) -> a -> b
$ forall curve (proxy :: * -> *).
Curve curve =>
proxy curve -> CurveParameters curve
Simple.curveParameters proxy c
prx

ecScalarAdd :: forall curve . Simple.Curve curve
            => Simple.Scalar curve -> Simple.Scalar curve -> Simple.Scalar curve
ecScalarAdd :: forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Scalar curve
ecScalarAdd (Simple.Scalar Integer
a) (Simple.Scalar Integer
b) = forall curve. Integer -> Scalar curve
Simple.Scalar ((Integer
a forall a. Num a => a -> a -> a
+ Integer
b) forall a. Integral a => a -> a -> a
`mod` Integer
n)
  where n :: Integer
n = forall curve. CurveParameters curve -> Integer
Simple.curveEccN forall a b. (a -> b) -> a -> b
$ forall curve (proxy :: * -> *).
Curve curve =>
proxy curve -> CurveParameters curve
Simple.curveParameters (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)

ecScalarMul :: forall curve . Simple.Curve curve
            => Simple.Scalar curve -> Simple.Scalar curve -> Simple.Scalar curve
ecScalarMul :: forall curve.
Curve curve =>
Scalar curve -> Scalar curve -> Scalar curve
ecScalarMul (Simple.Scalar Integer
a) (Simple.Scalar Integer
b) = forall curve. Integer -> Scalar curve
Simple.Scalar ((Integer
a forall a. Num a => a -> a -> a
* Integer
b) forall a. Integral a => a -> a -> a
`mod` Integer
n)
  where n :: Integer
n = forall curve. CurveParameters curve -> Integer
Simple.curveEccN forall a b. (a -> b) -> a -> b
$ forall curve (proxy :: * -> *).
Curve curve =>
proxy curve -> CurveParameters curve
Simple.curveParameters (forall {k} (t :: k). Proxy t
Proxy :: Proxy curve)