-- GENERATED by C->Haskell Compiler, version 0.28.6 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/Crypto/BLST/Internal/Bindings.chs" #-}
-- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
-- SPDX-License-Identifier: MPL-2.0



{-# OPTIONS_HADDOCK not-home #-}

-- | Lower-level bindings. Functions starting with @blst_@ are raw c2hs
-- bindings. Others are slightly higher level wrappers around those bindings.
--
-- See
-- <https://github.com/supranational/blst/tree/f791f7a465cda8ecda74df0a60778331dde40809#introductory-tutorial>
-- for a more comprehensive explanation of the functions declared here.
module Crypto.BLST.Internal.Bindings
  ( module Crypto.BLST.Internal.Bindings
  , module Crypto.BLST.Internal.Bindings.Types
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp



import Prelude hiding (length)

import Control.Exception (Exception, catch, throwIO)
import Data.ByteArray (ByteArrayAccess(..), Bytes, ScrubbedBytes)
import Data.ByteArray qualified as BA
import Data.ByteArray.Sized (SizedByteArray)
import Data.ByteArray.Sized qualified as AS
import Foreign.Marshal.Utils (fromBool, toBool)
import Foreign.Ptr (nullPtr)

import Crypto.BLST.Internal.Bindings.Types

type instance SizeOf (Point 'P1) = 144
{-# LINE 31 "src/Crypto/BLST/Internal/Bindings.chs" #-}

type instance SizeOf (Point 'P2) = 288
{-# LINE 32 "src/Crypto/BLST/Internal/Bindings.chs" #-}


type instance SizeOf (Affine 'P1) = 96
{-# LINE 34 "src/Crypto/BLST/Internal/Bindings.chs" #-}

type instance SizeOf (Affine 'P2) = 192
{-# LINE 35 "src/Crypto/BLST/Internal/Bindings.chs" #-}


type instance SizeOf Scalar = 32
{-# LINE 37 "src/Crypto/BLST/Internal/Bindings.chs" #-}


-- | Possible C return values.
data BlstError = BlstSuccess
               | BlstBadEncoding
               | BlstPointNotOnCurve
               | BlstPointNotInGroup
               | BlstAggrTypeMismatch
               | BlstVerifyFail
               | BlstPkIsInfinity
               | BlstBadScalar
instance Enum BlstError where
  succ BlstSuccess = BlstBadEncoding
  succ BlstBadEncoding = BlstPointNotOnCurve
  succ BlstPointNotOnCurve = BlstPointNotInGroup
  succ BlstPointNotInGroup = BlstAggrTypeMismatch
  succ BlstAggrTypeMismatch = BlstVerifyFail
  succ BlstVerifyFail = BlstPkIsInfinity
  succ BlstPkIsInfinity = BlstBadScalar
  succ BlstBadScalar = error "BlstError.succ: BlstBadScalar has no successor"

  pred BlstBadEncoding = BlstSuccess
  pred BlstPointNotOnCurve = BlstBadEncoding
  pred BlstPointNotInGroup = BlstPointNotOnCurve
  pred BlstAggrTypeMismatch = BlstPointNotInGroup
  pred BlstVerifyFail = BlstAggrTypeMismatch
  pred BlstPkIsInfinity = BlstVerifyFail
  pred BlstBadScalar = BlstPkIsInfinity
  pred BlstSuccess = error "BlstError.pred: BlstSuccess has no predecessor"

  enumFromTo from to = go from
    where
      end = fromEnum to
      go v = case compare (fromEnum v) end of
                 LT -> v : go (succ v)
                 EQ -> [v]
                 GT -> []

  enumFrom from = enumFromTo from BlstBadScalar

  fromEnum BlstSuccess = 0
  fromEnum BlstBadEncoding = 1
  fromEnum BlstPointNotOnCurve = 2
  fromEnum BlstPointNotInGroup = 3
  fromEnum BlstAggrTypeMismatch = 4
  fromEnum BlstVerifyFail = 5
  fromEnum BlstPkIsInfinity = 6
  fromEnum BlstBadScalar = 7

  toEnum 0 = BlstSuccess
  toEnum 1 = BlstBadEncoding
  toEnum 2 = BlstPointNotOnCurve
  toEnum 3 = BlstPointNotInGroup
  toEnum 4 = BlstAggrTypeMismatch
  toEnum 5 = BlstVerifyFail
  toEnum 6 = BlstPkIsInfinity
  toEnum 7 = BlstBadScalar
  toEnum unmatched = error ("BlstError.toEnum: Cannot match " ++ show unmatched)

{-# LINE 40 "src/Crypto/BLST/Internal/Bindings.chs" #-}


deriving stock instance Eq BlstError
deriving stock instance Bounded BlstError
deriving stock instance Show BlstError

instance Exception BlstError

-- | Generate secret key from bytes. Input must be at least 32 bytes long.
keygen :: ByteArrayAccess ba => ba -> IO Scalar
keygen bytes = fmap Scalar $
  AS.create $ \ptr ->
  withByteArray bytes $ \bytes' ->
    -- void blst_keygen(blst_scalar *out_SK, const byte *IKM, size_t IKM_len,
    --                  const byte *info DEFNULL, size_t info_len DEFNULL);
    blst_keygen ptr bytes' (fromIntegral $ length bytes) nullPtr 0

-- | Convert scalar to a point in G1.
skToPkInG1 :: Scalar -> IO (Point 'P1)
skToPkInG1 (Scalar sk) = fmap Point $
  AS.create $ \ptr ->
  withByteArray sk $ \sk' ->
    -- void blst_sk_to_pk_in_g1(blst_p1 *out_pk, const blst_scalar *SK);
    blst_sk_to_pk_in_g1 ptr sk'

-- | Convert scalar to a P2 point in G2.
skToPkInG2 :: Scalar -> IO (Point 'P2)
skToPkInG2 (Scalar sk) = fmap Point $
  AS.create $ \ptr ->
  withByteArray sk $ \sk' ->
    -- void blst_sk_to_pk_in_g2(blst_p2 *out_pk, const blst_scalar *SK);
    blst_sk_to_pk_in_g2 ptr sk'

-- | Sign a message point in G1.
signPkInG1 :: Point 'P2 -> Scalar -> IO (Point 'P2)
signPkInG1 (Point p2) (Scalar sc) = fmap Point $
  AS.create $ \ptr ->
  withByteArray sc $ \sc' ->
  withByteArray p2 $ \p2' ->
    -- void blst_sign_pk_in_g1(blst_p2 *out_sig, const blst_p2 *hash,
    --                                           const blst_scalar *SK);
    blst_sign_pk_in_g1 ptr p2' sc'

-- | Sign a message point in G2.
signPkInG2 :: Point 'P1 -> Scalar -> IO (Point 'P1)
signPkInG2 (Point p1) (Scalar sc) = fmap Point $
  AS.create $ \ptr ->
  withByteArray sc $ \sc' ->
  withByteArray p1 $ \p1' ->
    -- void blst_sign_pk_in_g2(blst_p1 *out_sig, const blst_p1 *hash,
    --                                           const blst_scalar *SK);
    blst_sign_pk_in_g2 ptr p1' sc'

-- | Encode bytes to a point in G1.
encodeToG1 :: (ByteArrayAccess ba, ByteArrayAccess ba2) => ba -> Maybe ba2 -> IO (Point 'P1)
encodeToG1 msg dst = fmap Point $
  AS.create $ \ptr ->
  withByteArray msg $ \msg' ->
  maybe ($ nullPtr) withByteArray dst $ \dst' ->
    -- void blst_encode_to_g1(blst_p1 *out,
    --                        const byte *msg, size_t msg_len,
    --                        const byte *DST DEFNULL, size_t DST_len DEFNULL,
    --                        const byte *aug DEFNULL, size_t aug_len DEFNULL);
    blst_encode_to_g1 ptr msg' (fromIntegral $ length msg)
      dst' (maybe 0 (fromIntegral . length) dst) nullPtr 0

-- | Hash bytes to a point in G1.
hashToG1 :: (ByteArrayAccess ba, ByteArrayAccess ba2) => ba -> Maybe ba2 -> IO (Point 'P1)
hashToG1 :: forall ba ba2.
(ByteArrayAccess ba, ByteArrayAccess ba2) =>
ba -> Maybe ba2 -> IO (Point 'P1)
hashToG1 ba
msg Maybe ba2
dst = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Maybe ba2
dst forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
dst' ->
    -- void blst_hash_to_g1(blst_p1 *out,
    --                      const byte *msg, size_t msg_len,
    --                      const byte *DST DEFNULL, size_t DST_len DEFNULL,
    --                      const byte *aug DEFNULL, size_t aug_len DEFNULL);
    Ptr ()
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO ()
blst_hash_to_g1 Ptr ()
ptr Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg)
      Ptr CUChar
dst' (forall b a. b -> (a -> b) -> Maybe a -> b
maybe CULong
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba. ByteArrayAccess ba => ba -> Int
length) Maybe ba2
dst) forall a. Ptr a
nullPtr CULong
0

-- | Encode bytes to a point in G2.
encodeToG2 :: (ByteArrayAccess ba, ByteArrayAccess ba2) => ba -> Maybe ba2 -> IO (Point 'P2)
encodeToG2 :: forall ba ba2.
(ByteArrayAccess ba, ByteArrayAccess ba2) =>
ba -> Maybe ba2 -> IO (Point 'P2)
encodeToG2 ba
msg Maybe ba2
dst = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Maybe ba2
dst forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
dst' ->
    -- void blst_encode_to_g2(blst_p2 *out,
    --                        const byte *msg, size_t msg_len,
    --                        const byte *DST DEFNULL, size_t DST_len DEFNULL,
    --                        const byte *aug DEFNULL, size_t aug_len DEFNULL);
    Ptr ()
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO ()
blst_encode_to_g2 Ptr ()
ptr Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg)
      Ptr CUChar
dst' (forall b a. b -> (a -> b) -> Maybe a -> b
maybe CULong
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba. ByteArrayAccess ba => ba -> Int
length) Maybe ba2
dst) forall a. Ptr a
nullPtr CULong
0

-- | Hash bytes to a point in G2.
hashToG2 :: (ByteArrayAccess ba, ByteArrayAccess ba2) => ba -> Maybe ba2 -> IO (Point 'P2)
hashToG2 :: forall ba ba2.
(ByteArrayAccess ba, ByteArrayAccess ba2) =>
ba -> Maybe ba2 -> IO (Point 'P2)
hashToG2 ba
msg Maybe ba2
dst = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Maybe ba2
dst forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
dst' ->
    -- void blst_hash_to_g2(blst_p2 *out,
    --                      const byte *msg, size_t msg_len,
    --                      const byte *DST DEFNULL, size_t DST_len DEFNULL,
    --                      const byte *aug DEFNULL, size_t aug_len DEFNULL);
    Ptr ()
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO ()
blst_hash_to_g2 Ptr ()
ptr Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg)
      Ptr CUChar
dst' (forall b a. b -> (a -> b) -> Maybe a -> b
maybe CULong
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba. ByteArrayAccess ba => ba -> Int
length) Maybe ba2
dst) forall a. Ptr a
nullPtr CULong
0

-- | Core signature verification function in G1.
coreVerifyPkInG1
  :: (ByteArrayAccess ba, ByteArrayAccess ba2)
  => Affine 'P1 -- ^ Public key
  -> Affine 'P2 -- ^ Signature
  -> EncodeMethod -- ^ Was message encoded or hashed to the curve
  -> ba -- ^ Message
  -> Maybe ba2 -- ^ Optional domain separation tag
  -> IO BlstError
coreVerifyPkInG1 :: forall ba ba2.
(ByteArrayAccess ba, ByteArrayAccess ba2) =>
Affine 'P1
-> Affine 'P2 -> EncodeMethod -> ba -> Maybe ba2 -> IO BlstError
coreVerifyPkInG1 (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
pk) (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
sig) EncodeMethod
hoe ba
msg Maybe ba2
dst = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall a b. (a -> b) -> a -> b
$
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
pk forall a b. (a -> b) -> a -> b
$ \Ptr ()
pk' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
sig forall a b. (a -> b) -> a -> b
$ \Ptr ()
sig' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Maybe ba2
dst forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
dst' ->
    -- BLST_ERROR blst_core_verify_pk_in_g1(const blst_p1_affine *pk,
    --                                      const blst_p2_affine *signature,
    --                                      bool hash_or_encode,
    --                                      const byte *msg, size_t msg_len,
    --                                      const byte *DST DEFNULL,
    --                                      size_t DST_len DEFNULL,
    --                                      const byte *aug DEFNULL,
    --                                      size_t aug_len DEFNULL);
    Ptr ()
-> Ptr ()
-> CInt
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO CInt
blst_core_verify_pk_in_g1 Ptr ()
pk' Ptr ()
sig' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum EncodeMethod
hoe)
      Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg)
      Ptr CUChar
dst' (forall b a. b -> (a -> b) -> Maybe a -> b
maybe CULong
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba. ByteArrayAccess ba => ba -> Int
length) Maybe ba2
dst)
      forall a. Ptr a
nullPtr CULong
0

-- | Core signature verification function in G2.
coreVerifyPkInG2
  :: (ByteArrayAccess ba, ByteArrayAccess ba2)
  => Affine 'P2 -- ^ Public key
  -> Affine 'P1 -- ^ Signature
  -> EncodeMethod -- ^ Was message encoded or hashed to the curve
  -> ba -- ^ Message
  -> Maybe ba2 -- ^ Optional domain separation tag
  -> IO BlstError
coreVerifyPkInG2 :: forall ba ba2.
(ByteArrayAccess ba, ByteArrayAccess ba2) =>
Affine 'P2
-> Affine 'P1 -> EncodeMethod -> ba -> Maybe ba2 -> IO BlstError
coreVerifyPkInG2 (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
pk) (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
sig) EncodeMethod
hoe ba
msg Maybe ba2
dst = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall a b. (a -> b) -> a -> b
$
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
pk forall a b. (a -> b) -> a -> b
$ \Ptr ()
pk' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
sig forall a b. (a -> b) -> a -> b
$ \Ptr ()
sig' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Maybe ba2
dst forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
dst' ->
    -- BLST_ERROR blst_core_verify_pk_in_g2(const blst_p2_affine *pk,
    --                                      const blst_p1_affine *signature,
    --                                      bool hash_or_encode,
    --                                      const byte *msg, size_t msg_len,
    --                                      const byte *DST DEFNULL,
    --                                      size_t DST_len DEFNULL,
    --                                      const byte *aug DEFNULL,
    --                                      size_t aug_len DEFNULL);
    Ptr ()
-> Ptr ()
-> CInt
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO CInt
blst_core_verify_pk_in_g2 Ptr ()
pk' Ptr ()
sig' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum EncodeMethod
hoe)
      Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg)
      Ptr CUChar
dst' (forall b a. b -> (a -> b) -> Maybe a -> b
maybe CULong
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba. ByteArrayAccess ba => ba -> Int
length) Maybe ba2
dst)
      forall a. Ptr a
nullPtr CULong
0

-- | Convert point to affine point in G1.
p1ToAffine :: Point 'P1 -> IO (Affine 'P1)
p1ToAffine :: Point 'P1 -> IO (Affine 'P1)
p1ToAffine (Point SizedByteArray (SizeOf (Point 'P1)) Bytes
p1) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Affine a)) Bytes -> Affine a
Affine forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Point 'P1)) Bytes
p1 forall a b. (a -> b) -> a -> b
$ \Ptr ()
p1' ->
    -- void blst_p1_to_affine(blst_p1_affine *out, const blst_p1 *in);
    Ptr () -> Ptr () -> IO ()
blst_p1_to_affine Ptr ()
ptr Ptr ()
p1'

-- | Convert point to affine point in G2.
p2ToAffine :: Point 'P2 -> IO (Affine 'P2)
p2ToAffine :: Point 'P2 -> IO (Affine 'P2)
p2ToAffine (Point SizedByteArray (SizeOf (Point 'P2)) Bytes
p2) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Affine a)) Bytes -> Affine a
Affine forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Point 'P2)) Bytes
p2 forall a b. (a -> b) -> a -> b
$ \Ptr ()
p2' ->
    -- void blst_p2_to_affine(blst_p2_affine *out, const blst_p2 *in);
    Ptr () -> Ptr () -> IO ()
blst_p2_to_affine Ptr ()
ptr Ptr ()
p2'

-- | Serialize affine G1 point.
p1AffSerialize :: Affine 'P1 -> IO (SizedByteArray P1SerializeSize Bytes)
p1AffSerialize :: Affine 'P1 -> IO (SizedByteArray 96 Bytes)
p1AffSerialize (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
p1) =
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
p1 forall a b. (a -> b) -> a -> b
$ \Ptr ()
p1' ->
    -- void blst_p1_affine_serialize(byte out[96], const blst_p1_affine *in);
    Ptr CUChar -> Ptr () -> IO ()
blst_p1_affine_serialize Ptr CUChar
ptr Ptr ()
p1'

-- | Serialize and compress affine G1 point.
p1AffCompress :: Affine 'P1 -> IO (SizedByteArray P1CompressSize Bytes)
p1AffCompress :: Affine 'P1 -> IO (SizedByteArray 48 Bytes)
p1AffCompress (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
p1) =
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
p1 forall a b. (a -> b) -> a -> b
$ \Ptr ()
p1' ->
    -- void blst_p1_affine_compress(byte out[48], const blst_p1_affine *in);
    Ptr CUChar -> Ptr () -> IO ()
blst_p1_affine_compress Ptr CUChar
ptr Ptr ()
p1'

-- | Deserialize affine G1 point.
p1Deserialize
  :: ByteArrayAccess ba
  => SizedByteArray P1SerializeSize ba
  -> IO (Either BlstError (Affine 'P1))
p1Deserialize :: forall ba.
ByteArrayAccess ba =>
SizedByteArray 96 ba -> IO (Either BlstError (Affine 'P1))
p1Deserialize SizedByteArray 96 ba
bs = do
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PointKind).
SizedByteArray (SizeOf (Affine a)) Bytes -> Affine a
Affine) forall a b. (a -> b) -> a -> b
$
    forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray 96 ba
bs forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
bs' -> do
      -- BLST_ERROR blst_p1_deserialize(blst_p1_affine *out, const byte in[96]);
      CInt
res <- Ptr () -> Ptr CUChar -> IO CInt
blst_p1_deserialize Ptr ()
ptr Ptr CUChar
bs'
      let res' :: BlstError
res' = forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
res
      case BlstError
res' of
        BlstError
BlstSuccess -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ()
        BlstError
x -> forall e a. Exception e => e -> IO a
throwIO BlstError
x
  forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(BlstError
x :: BlstError) -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left BlstError
x

-- | Deserialize and decompress affine G1 point.
p1Uncompress
  :: ByteArrayAccess ba
  => SizedByteArray P1CompressSize ba
  -> IO (Either BlstError (Affine 'P1))
p1Uncompress :: forall ba.
ByteArrayAccess ba =>
SizedByteArray 48 ba -> IO (Either BlstError (Affine 'P1))
p1Uncompress SizedByteArray 48 ba
bs = do
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PointKind).
SizedByteArray (SizeOf (Affine a)) Bytes -> Affine a
Affine) forall a b. (a -> b) -> a -> b
$
    forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray 48 ba
bs forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
bs' -> do
      -- BLST_ERROR blst_p1_uncompress(blst_p1_affine *out, const byte in[48]);
      CInt
res <- Ptr () -> Ptr CUChar -> IO CInt
blst_p1_uncompress Ptr ()
ptr Ptr CUChar
bs'
      let res' :: BlstError
res' = forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
res
      case BlstError
res' of
        BlstError
BlstSuccess -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ()
        BlstError
x -> forall e a. Exception e => e -> IO a
throwIO BlstError
x
  forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(BlstError
x :: BlstError) -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left BlstError
x

-- | Serialize affine G2 point.
p2AffSerialize :: Affine 'P2 -> IO (SizedByteArray P2SerializeSize Bytes)
p2AffSerialize :: Affine 'P2 -> IO (SizedByteArray 192 Bytes)
p2AffSerialize (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
p2) =
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
p2 forall a b. (a -> b) -> a -> b
$ \Ptr ()
p2' ->
    -- void blst_p2_affine_serialize(byte out[192], const blst_p2_affine *in);
    Ptr CUChar -> Ptr () -> IO ()
blst_p2_affine_serialize Ptr CUChar
ptr Ptr ()
p2'

-- | Serialize and compress affine G2 point.
p2AffCompress :: Affine 'P2 -> IO (SizedByteArray P2CompressSize Bytes)
p2AffCompress :: Affine 'P2 -> IO (SizedByteArray 96 Bytes)
p2AffCompress (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
p2) =
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
ptr ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
p2 forall a b. (a -> b) -> a -> b
$ \Ptr ()
p2' ->
    -- void blst_p2_affine_compress(byte out[96], const blst_p2_affine *in);
    Ptr CUChar -> Ptr () -> IO ()
blst_p2_affine_compress Ptr CUChar
ptr Ptr ()
p2'

-- | Deserialize affine G2 point.
p2Deserialize
  :: ByteArrayAccess ba
  => SizedByteArray P2SerializeSize ba
  -> IO (Either BlstError (Affine 'P2))
p2Deserialize :: forall ba.
ByteArrayAccess ba =>
SizedByteArray 192 ba -> IO (Either BlstError (Affine 'P2))
p2Deserialize SizedByteArray 192 ba
bs = do
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PointKind).
SizedByteArray (SizeOf (Affine a)) Bytes -> Affine a
Affine) forall a b. (a -> b) -> a -> b
$
    forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray 192 ba
bs forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
bs' -> do
      -- BLST_ERROR blst_p2_deserialize(blst_p2_affine *out, const byte in[192]);
      CInt
res <- Ptr () -> Ptr CUChar -> IO CInt
blst_p2_deserialize Ptr ()
ptr Ptr CUChar
bs'
      let res' :: BlstError
res' = forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
res
      case BlstError
res' of
        BlstError
BlstSuccess -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ()
        BlstError
x -> forall e a. Exception e => e -> IO a
throwIO BlstError
x
  forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(BlstError
x :: BlstError) -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left BlstError
x

-- | Deserialize and decompress affine G2 point.
p2Uncompress
  :: ByteArrayAccess ba
  => SizedByteArray P2CompressSize ba
  -> IO (Either BlstError (Affine 'P2))
p2Uncompress :: forall ba.
ByteArrayAccess ba =>
SizedByteArray 96 ba -> IO (Either BlstError (Affine 'P2))
p2Uncompress SizedByteArray 96 ba
bs = do
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PointKind).
SizedByteArray (SizeOf (Affine a)) Bytes -> Affine a
Affine) forall a b. (a -> b) -> a -> b
$
    forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
ptr ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray 96 ba
bs forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
bs' -> do
      -- BLST_ERROR blst_p2_uncompress(blst_p2_affine *out, const byte in[96]);
      CInt
res <- Ptr () -> Ptr CUChar -> IO CInt
blst_p2_uncompress Ptr ()
ptr Ptr CUChar
bs'
      let res' :: BlstError
res' = forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
res
      case BlstError
res' of
        BlstError
BlstSuccess -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ()
        BlstError
x -> forall e a. Exception e => e -> IO a
throwIO BlstError
x
  forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(BlstError
x :: BlstError) -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left BlstError
x

-- | Get scalar bytes in little endian order.
lendianFromScalar :: Scalar -> IO (SizedByteArray SkSerializeSize ScrubbedBytes)
lendianFromScalar :: Scalar -> IO (SizedByteArray 32 ScrubbedBytes)
lendianFromScalar (Scalar SizedByteArray (SizeOf Scalar) ScrubbedBytes
sc) =
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
out ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf Scalar) ScrubbedBytes
sc forall a b. (a -> b) -> a -> b
$ \Ptr ()
sc' ->
  -- void blst_lendian_from_scalar(byte out[32], const blst_scalar *a);
  Ptr CUChar -> Ptr () -> IO ()
blst_lendian_from_scalar Ptr CUChar
out Ptr ()
sc'

-- | Build scalar from bytes in little endian order.
scalarFromLendian :: ByteArrayAccess ba => SizedByteArray SkSerializeSize ba -> IO Scalar
scalarFromLendian :: forall ba. ByteArrayAccess ba => SizedByteArray 32 ba -> IO Scalar
scalarFromLendian SizedByteArray 32 ba
bs = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap SizedByteArray (SizeOf Scalar) ScrubbedBytes -> Scalar
Scalar forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
out ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray 32 ba
bs forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
bs' ->
  -- void blst_scalar_from_lendian(blst_scalar *out, const byte a[32]);
  Ptr () -> Ptr CUChar -> IO ()
blst_scalar_from_lendian Ptr ()
out Ptr CUChar
bs'

-- | Add affine point to point in G1.
p1AddOrDoubleAffine :: Point 'P1 -> Affine 'P1 -> IO (Point 'P1)
p1AddOrDoubleAffine :: Point 'P1 -> Affine 'P1 -> IO (Point 'P1)
p1AddOrDoubleAffine (Point SizedByteArray (SizeOf (Point 'P1)) Bytes
a) (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
b) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
out ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Point 'P1)) Bytes
a forall a b. (a -> b) -> a -> b
$ \Ptr ()
a' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
b forall a b. (a -> b) -> a -> b
$ \Ptr ()
b' ->
  -- void blst_p1_add_or_double_affine(blst_p1 *out, const blst_p1 *a,
  --                                                 const blst_p1_affine *b);
  Ptr () -> Ptr () -> Ptr () -> IO ()
blst_p1_add_or_double_affine Ptr ()
out Ptr ()
a' Ptr ()
b'

-- | Add affine point to point in G2.
p2AddOrDoubleAffine :: Point 'P2 -> Affine 'P2 -> IO (Point 'P2)
p2AddOrDoubleAffine :: Point 'P2 -> Affine 'P2 -> IO (Point 'P2)
p2AddOrDoubleAffine (Point SizedByteArray (SizeOf (Point 'P2)) Bytes
a) (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
b) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
out ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Point 'P2)) Bytes
a forall a b. (a -> b) -> a -> b
$ \Ptr ()
a' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
b forall a b. (a -> b) -> a -> b
$ \Ptr ()
b' ->
  -- void blst_p2_add_or_double_affine(blst_p2 *out, const blst_p2 *a,
  --                                                 const blst_p2_affine *b);
  Ptr () -> Ptr () -> Ptr () -> IO ()
blst_p2_add_or_double_affine Ptr ()
out Ptr ()
a' Ptr ()
b'

-- | Convert affine point to point in G1.
p1FromAffine :: Affine 'P1 -> IO (Point 'P1)
p1FromAffine :: Affine 'P1 -> IO (Point 'P1)
p1FromAffine (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
aff) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
out ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
aff forall a b. (a -> b) -> a -> b
$ \Ptr ()
aff' ->
  -- void blst_p1_from_affine(blst_p1 *out, const blst_p1_affine *in);
  Ptr () -> Ptr () -> IO ()
blst_p1_from_affine Ptr ()
out Ptr ()
aff'

-- | Convert affine point to point in G2.
p2FromAffine :: Affine 'P2 -> IO (Point 'P2)
p2FromAffine :: Affine 'P2 -> IO (Point 'P2)
p2FromAffine (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
aff) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (a :: PointKind).
SizedByteArray (SizeOf (Point a)) Bytes -> Point a
Point forall a b. (a -> b) -> a -> b
$
  forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
AS.create forall a b. (a -> b) -> a -> b
$ \Ptr ()
out ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
aff forall a b. (a -> b) -> a -> b
$ \Ptr ()
aff' ->
  -- void blst_p2_from_affine(blst_p2 *out, const blst_p2_affine *in);
  Ptr () -> Ptr () -> IO ()
blst_p2_from_affine Ptr ()
out Ptr ()
aff'

-- | Check aggregate signature in G1.
pairingChkNAggrPkInG1
  :: ByteArrayAccess ba
  => PairingCtx -- ^ Pairing context. Use 'pairingInit' to create.
  -> Affine 'P1 -- ^ Public key
  -> Bool -- ^ Check public key group?
  -> Maybe (Affine 'P2)
  -- ^ Signature. Only the first call per pairing context specifies the
  -- signature, all consequent calls for the same context should use 'Nothing'
  -- here.
  -> Bool -- ^ Check signature group?
  -> ba -- ^ Message
  -> IO BlstError
pairingChkNAggrPkInG1 :: forall ba.
ByteArrayAccess ba =>
PairingCtx
-> Affine 'P1
-> Bool
-> Maybe (Affine 'P2)
-> Bool
-> ba
-> IO BlstError
pairingChkNAggrPkInG1 (PairingCtx Bytes
ctx) (Affine SizedByteArray (SizeOf (Affine 'P1)) Bytes
pk) Bool
pk_gpck Maybe (Affine 'P2)
sig Bool
sig_gpck ba
msg =
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall a b. (a -> b) -> a -> b
$
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Bytes
ctx forall a b. (a -> b) -> a -> b
$ \Ptr ()
ctx' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P1)) Bytes
pk forall a b. (a -> b) -> a -> b
$ \Ptr ()
pk' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) (forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PointKind).
Affine a -> SizedByteArray (SizeOf (Affine a)) Bytes
unAffine) Maybe (Affine 'P2)
sig forall a b. (a -> b) -> a -> b
$ \Ptr ()
sig' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  -- BLST_ERROR blst_pairing_chk_n_aggr_pk_in_g1(blst_pairing *ctx,
  --                                             const blst_p1_affine *PK,
  --                                             bool pk_grpchk,
  --                                             const blst_p2_affine *signature,
  --                                             bool sig_grpchk,
  --                                             const byte *msg, size_t msg_len,
  --                                             const byte *aug DEFNULL,
  --                                             size_t aug_len DEFNULL);
  Ptr ()
-> Ptr ()
-> CInt
-> Ptr ()
-> CInt
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO CInt
blst_pairing_chk_n_aggr_pk_in_g1 Ptr ()
ctx' Ptr ()
pk' (forall a. Num a => Bool -> a
fromBool Bool
pk_gpck) Ptr ()
sig'
    (forall a. Num a => Bool -> a
fromBool Bool
sig_gpck) Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg) forall a. Ptr a
nullPtr CULong
0

-- | Check aggregate signature in G2.
pairingChkNAggrPkInG2
  :: ByteArrayAccess ba
  => PairingCtx -- ^ Pairing context. Use 'pairingInit' to create.
  -> Affine 'P2 -- ^ Public key
  -> Bool -- ^ Check public key group?
  -> Maybe (Affine 'P1)
  -- ^ Signature. Only the first call per pairing context specifies the
  -- signature, all consequent calls for the same context should use 'Nothing'
  -- here.
  -> Bool -- ^ Check signature group?
  -> ba -- ^ Message
  -> IO BlstError
pairingChkNAggrPkInG2 :: forall ba.
ByteArrayAccess ba =>
PairingCtx
-> Affine 'P2
-> Bool
-> Maybe (Affine 'P1)
-> Bool
-> ba
-> IO BlstError
pairingChkNAggrPkInG2 (PairingCtx Bytes
ctx) (Affine SizedByteArray (SizeOf (Affine 'P2)) Bytes
pk) Bool
pk_gpck Maybe (Affine 'P1)
sig Bool
sig_gpck ba
msg =
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall a b. (a -> b) -> a -> b
$
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Bytes
ctx forall a b. (a -> b) -> a -> b
$ \Ptr ()
ctx' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray SizedByteArray (SizeOf (Affine 'P2)) Bytes
pk forall a b. (a -> b) -> a -> b
$ \Ptr ()
pk' ->
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. (a -> b) -> a -> b
$ forall a. Ptr a
nullPtr) (forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PointKind).
Affine a -> SizedByteArray (SizeOf (Affine a)) Bytes
unAffine) Maybe (Affine 'P1)
sig forall a b. (a -> b) -> a -> b
$ \Ptr ()
sig' ->
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
msg forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
msg' ->
  -- BLST_ERROR blst_pairing_chk_n_aggr_pk_in_g2(blst_pairing *ctx,
  --                                             const blst_p2_affine *PK,
  --                                             bool pk_grpchk,
  --                                             const blst_p1_affine *signature,
  --                                             bool sig_grpchk,
  --                                             const byte *msg, size_t msg_len,
  --                                             const byte *aug DEFNULL,
  --                                             size_t aug_len DEFNULL);
  Ptr ()
-> Ptr ()
-> CInt
-> Ptr ()
-> CInt
-> Ptr CUChar
-> CULong
-> Ptr CUChar
-> CULong
-> IO CInt
blst_pairing_chk_n_aggr_pk_in_g2 Ptr ()
ctx' Ptr ()
pk' (forall a. Num a => Bool -> a
fromBool Bool
pk_gpck) Ptr ()
sig'
    (forall a. Num a => Bool -> a
fromBool Bool
sig_gpck) Ptr CUChar
msg' (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall ba. ByteArrayAccess ba => ba -> Int
length ba
msg) forall a. Ptr a
nullPtr CULong
0

-- | Make new pairing context.
pairingInit :: ByteArrayAccess ba => EncodeMethod -> Maybe ba -> IO PairingCtx
pairingInit :: forall ba.
ByteArrayAccess ba =>
EncodeMethod -> Maybe ba -> IO PairingCtx
pairingInit EncodeMethod
hoe Maybe ba
dst = do
  CULong
sz <- IO CULong
blst_pairing_sizeof
{-# LINE 430 "src/Crypto/BLST/Internal/Bindings.chs" #-}

  fmap PairingCtx $ BA.create (fromIntegral sz) $ \out ->
    maybe ($ nullPtr) withByteArray dst $ \dst' ->
      -- void blst_pairing_init(blst_pairing *new_ctx, bool hash_or_encode,
      --                        const byte *DST DEFNULL, size_t DST_len DEFNULL);
      blst_pairing_init out (fromIntegral $ fromEnum hoe)
        dst' (maybe 0 (fromIntegral . length) dst)

-- | Commit pairing context.
pairingCommit :: PairingCtx -> IO ()
pairingCommit :: PairingCtx -> IO ()
pairingCommit (PairingCtx Bytes
ctx) =
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Bytes
ctx forall a b. (a -> b) -> a -> b
$ \Ptr ()
ctx' ->
    -- void blst_pairing_commit(blst_pairing *ctx);
    Ptr () -> IO ()
blst_pairing_commit Ptr ()
ctx'

-- | Verify pairing context.
pairingFinalVerify :: PairingCtx -> IO Bool
pairingFinalVerify :: PairingCtx -> IO Bool
pairingFinalVerify (PairingCtx Bytes
ctx) = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. (Eq a, Num a) => a -> Bool
toBool forall a b. (a -> b) -> a -> b
$
  forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Bytes
ctx forall a b. (a -> b) -> a -> b
$ \Ptr ()
ctx' ->
    -- bool blst_pairing_finalverify(const blst_pairing *ctx,
    --                               const blst_fp12 *gtsig DEFNULL);
    Ptr () -> Ptr () -> IO CInt
blst_pairing_finalverify Ptr ()
ctx' forall a. Ptr a
nullPtr

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_keygen"
  blst_keygen :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO ()))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_sk_to_pk_in_g1"
  blst_sk_to_pk_in_g1 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_sk_to_pk_in_g2"
  blst_sk_to_pk_in_g2 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_sign_pk_in_g1"
  blst_sign_pk_in_g1 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ()))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_sign_pk_in_g2"
  blst_sign_pk_in_g2 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ()))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_encode_to_g1"
  blst_encode_to_g1 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO ()))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_hash_to_g1"
  blst_hash_to_g1 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO ()))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_encode_to_g2"
  blst_encode_to_g2 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO ()))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_hash_to_g2"
  blst_hash_to_g2 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO ()))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h __c2hs_wrapped__blst_core_verify_pk_in_g1"
  blst_core_verify_pk_in_g1 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO C2HSImp.CInt))))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h __c2hs_wrapped__blst_core_verify_pk_in_g2"
  blst_core_verify_pk_in_g2 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO C2HSImp.CInt))))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_to_affine"
  blst_p1_to_affine :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_to_affine"
  blst_p2_to_affine :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_affine_serialize"
  blst_p1_affine_serialize :: ((C2HSImp.Ptr C2HSImp.CUChar) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_affine_compress"
  blst_p1_affine_compress :: ((C2HSImp.Ptr C2HSImp.CUChar) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_deserialize"
  blst_p1_deserialize :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CInt)))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_uncompress"
  blst_p1_uncompress :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CInt)))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_affine_serialize"
  blst_p2_affine_serialize :: ((C2HSImp.Ptr C2HSImp.CUChar) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_affine_compress"
  blst_p2_affine_compress :: ((C2HSImp.Ptr C2HSImp.CUChar) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_deserialize"
  blst_p2_deserialize :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CInt)))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_uncompress"
  blst_p2_uncompress :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CInt)))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_lendian_from_scalar"
  blst_lendian_from_scalar :: ((C2HSImp.Ptr C2HSImp.CUChar) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_scalar_from_lendian"
  blst_scalar_from_lendian :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_add_or_double_affine"
  blst_p1_add_or_double_affine :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ()))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_add_or_double_affine"
  blst_p2_add_or_double_affine :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ()))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p1_from_affine"
  blst_p1_from_affine :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_p2_from_affine"
  blst_p2_from_affine :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO ())))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h __c2hs_wrapped__blst_pairing_chk_n_aggr_pk_in_g1"
  blst_pairing_chk_n_aggr_pk_in_g1 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO C2HSImp.CInt))))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h __c2hs_wrapped__blst_pairing_chk_n_aggr_pk_in_g2"
  blst_pairing_chk_n_aggr_pk_in_g2 :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO C2HSImp.CInt))))))))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_pairing_sizeof"
  blst_pairing_sizeof :: (IO C2HSImp.CULong)

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h __c2hs_wrapped__blst_pairing_init"
  blst_pairing_init :: ((C2HSImp.Ptr ()) -> (C2HSImp.CInt{-bool-} -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CULong -> (IO ())))))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h blst_pairing_commit"
  blst_pairing_commit :: ((C2HSImp.Ptr ()) -> (IO ()))

foreign import ccall safe "Crypto/BLST/Internal/Bindings.chs.h __c2hs_wrapped__blst_pairing_finalverify"
  blst_pairing_finalverify :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt{-bool-})))