{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeApplications #-}
-- According to the documentation for unsafePerformIO:
-- 
-- > Make sure that the either you switch off let-floating 
-- > (-fno-full-laziness), or that the call to unsafePerformIO cannot float 
-- > outside a lambda.
--
-- If we do not switch off let-floating, our calls to unsafeDupablePerformIO for
-- FFI functions become nondeterministic in their behaviour when run with
-- parallelism enabled (such as -with-rtsopts=-N), possibly yielding wrong
-- answers on a range of tasks, including serialization.
{-# OPTIONS_GHC -fno-full-laziness #-}

module Cardano.Crypto.DSIGN.SchnorrSecp256k1 (
  SchnorrSecp256k1DSIGN,
  VerKeyDSIGN,
  SignKeyDSIGN,
  SigDSIGN
  ) where

import GHC.TypeNats (natVal)
import Foreign.ForeignPtr (withForeignPtr)
import Data.Proxy (Proxy (Proxy))
import Data.ByteString (useAsCStringLen)
import GHC.Generics (Generic)
import Control.DeepSeq (NFData)
import Data.Primitive.Ptr (copyPtr)
import Cardano.Crypto.Seed (getBytesFromSeedT)
import Cardano.Crypto.SECP256K1.Constants (
  SECP256K1_SCHNORR_PRIVKEY_BYTES,
  SECP256K1_SCHNORR_SIGNATURE_BYTES,
  SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL,
  SECP256K1_SCHNORR_PUBKEY_BYTES,
  )
import Cardano.Crypto.SECP256K1.C (
  secpKeyPairCreate,
  secpXOnlyPubkeySerialize,
  secpKeyPairXOnlyPub,
  secpXOnlyPubkeyParse,
  secpSchnorrSigVerify,
  secpSchnorrSigSignCustom,
  secpCtxPtr,
  )
import Cardano.Foreign (allocaSized)
import Control.Monad (when)
import System.IO.Unsafe (unsafeDupablePerformIO)
import Cardano.Binary (FromCBOR (fromCBOR), ToCBOR (toCBOR, encodedSizeExpr))
import Foreign.Ptr (castPtr, nullPtr)
import NoThunks.Class (NoThunks)
import Cardano.Crypto.DSIGN.Class (
  DSIGNAlgorithm (VerKeyDSIGN,
                  SignKeyDSIGN,
                  SigDSIGN,
                  SeedSizeDSIGN,
                  SizeSigDSIGN,
                  SizeSignKeyDSIGN,
                  SizeVerKeyDSIGN,
                  algorithmNameDSIGN,
                  deriveVerKeyDSIGN,
                  signDSIGN,
                  verifyDSIGN,
                  genKeyDSIGN,
                  rawSerialiseSigDSIGN,
                  Signable,
                  rawSerialiseVerKeyDSIGN,
                  rawSerialiseSignKeyDSIGN,
                  rawDeserialiseVerKeyDSIGN,
                  rawDeserialiseSignKeyDSIGN,
                  rawDeserialiseSigDSIGN),
  encodeVerKeyDSIGN,
  encodedVerKeyDSIGNSizeExpr,
  decodeVerKeyDSIGN,
  encodeSignKeyDSIGN,
  encodedSignKeyDESIGNSizeExpr,
  decodeSignKeyDSIGN,
  encodeSigDSIGN,
  encodedSigDSIGNSizeExpr,
  decodeSigDSIGN,
  seedSizeDSIGN
  )
import Cardano.Crypto.Util (SignableRepresentation (getSignableRepresentation))
import Cardano.Crypto.PinnedSizedBytes (
  PinnedSizedBytes,
  psbUseAsSizedPtr,
  psbCreateSizedResult,
  psbCreate,
  psbCreateSized,
  psbToByteString,
  psbFromByteStringCheck,
  )
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) 

data SchnorrSecp256k1DSIGN

instance DSIGNAlgorithm SchnorrSecp256k1DSIGN where
  type SeedSizeDSIGN SchnorrSecp256k1DSIGN = SECP256K1_SCHNORR_PRIVKEY_BYTES
  type SizeSigDSIGN SchnorrSecp256k1DSIGN = SECP256K1_SCHNORR_SIGNATURE_BYTES
  type SizeSignKeyDSIGN SchnorrSecp256k1DSIGN = SECP256K1_SCHNORR_PRIVKEY_BYTES
  type SizeVerKeyDSIGN SchnorrSecp256k1DSIGN = SECP256K1_SCHNORR_PUBKEY_BYTES
  type Signable SchnorrSecp256k1DSIGN = SignableRepresentation
  newtype VerKeyDSIGN SchnorrSecp256k1DSIGN =
    VerKeySchnorrSecp256k1 (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
    deriving newtype (VerKeyDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
(VerKeyDSIGN SchnorrSecp256k1DSIGN
 -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool)
-> (VerKeyDSIGN SchnorrSecp256k1DSIGN
    -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool)
-> Eq (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VerKeyDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
$c/= :: VerKeyDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
== :: VerKeyDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
$c== :: VerKeyDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
Eq, VerKeyDSIGN SchnorrSecp256k1DSIGN -> ()
(VerKeyDSIGN SchnorrSecp256k1DSIGN -> ())
-> NFData (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. (a -> ()) -> NFData a
rnf :: VerKeyDSIGN SchnorrSecp256k1DSIGN -> ()
$crnf :: VerKeyDSIGN SchnorrSecp256k1DSIGN -> ()
NFData)
    deriving stock (Int -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS
[VerKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS
VerKeyDSIGN SchnorrSecp256k1DSIGN -> String
(Int -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS)
-> (VerKeyDSIGN SchnorrSecp256k1DSIGN -> String)
-> ([VerKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS)
-> Show (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VerKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS
$cshowList :: [VerKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS
show :: VerKeyDSIGN SchnorrSecp256k1DSIGN -> String
$cshow :: VerKeyDSIGN SchnorrSecp256k1DSIGN -> String
showsPrec :: Int -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS
$cshowsPrec :: Int -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS
Show, (forall x.
 VerKeyDSIGN SchnorrSecp256k1DSIGN
 -> Rep (VerKeyDSIGN SchnorrSecp256k1DSIGN) x)
-> (forall x.
    Rep (VerKeyDSIGN SchnorrSecp256k1DSIGN) x
    -> VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> Generic (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall x.
Rep (VerKeyDSIGN SchnorrSecp256k1DSIGN) x
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
forall x.
VerKeyDSIGN SchnorrSecp256k1DSIGN
-> Rep (VerKeyDSIGN SchnorrSecp256k1DSIGN) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (VerKeyDSIGN SchnorrSecp256k1DSIGN) x
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
$cfrom :: forall x.
VerKeyDSIGN SchnorrSecp256k1DSIGN
-> Rep (VerKeyDSIGN SchnorrSecp256k1DSIGN) x
Generic)
    deriving anyclass (Context
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
Proxy (VerKeyDSIGN SchnorrSecp256k1DSIGN) -> String
(Context
 -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo))
-> (Context
    -> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo))
-> (Proxy (VerKeyDSIGN SchnorrSecp256k1DSIGN) -> String)
-> NoThunks (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (VerKeyDSIGN SchnorrSecp256k1DSIGN) -> String
$cshowTypeOf :: Proxy (VerKeyDSIGN SchnorrSecp256k1DSIGN) -> String
wNoThunks :: Context
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
noThunks :: Context
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
$cnoThunks :: Context
-> VerKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
NoThunks)
  newtype SignKeyDSIGN SchnorrSecp256k1DSIGN =
    SignKeySchnorrSecp256k1 (PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN))
    deriving newtype (SignKeyDSIGN SchnorrSecp256k1DSIGN
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
(SignKeyDSIGN SchnorrSecp256k1DSIGN
 -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool)
-> (SignKeyDSIGN SchnorrSecp256k1DSIGN
    -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool)
-> Eq (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SignKeyDSIGN SchnorrSecp256k1DSIGN
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
$c/= :: SignKeyDSIGN SchnorrSecp256k1DSIGN
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
== :: SignKeyDSIGN SchnorrSecp256k1DSIGN
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
$c== :: SignKeyDSIGN SchnorrSecp256k1DSIGN
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> Bool
Eq, SignKeyDSIGN SchnorrSecp256k1DSIGN -> ()
(SignKeyDSIGN SchnorrSecp256k1DSIGN -> ())
-> NFData (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. (a -> ()) -> NFData a
rnf :: SignKeyDSIGN SchnorrSecp256k1DSIGN -> ()
$crnf :: SignKeyDSIGN SchnorrSecp256k1DSIGN -> ()
NFData)
    deriving stock (Int -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS
[SignKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS
SignKeyDSIGN SchnorrSecp256k1DSIGN -> String
(Int -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS)
-> (SignKeyDSIGN SchnorrSecp256k1DSIGN -> String)
-> ([SignKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS)
-> Show (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SignKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS
$cshowList :: [SignKeyDSIGN SchnorrSecp256k1DSIGN] -> ShowS
show :: SignKeyDSIGN SchnorrSecp256k1DSIGN -> String
$cshow :: SignKeyDSIGN SchnorrSecp256k1DSIGN -> String
showsPrec :: Int -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS
$cshowsPrec :: Int -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> ShowS
Show, (forall x.
 SignKeyDSIGN SchnorrSecp256k1DSIGN
 -> Rep (SignKeyDSIGN SchnorrSecp256k1DSIGN) x)
-> (forall x.
    Rep (SignKeyDSIGN SchnorrSecp256k1DSIGN) x
    -> SignKeyDSIGN SchnorrSecp256k1DSIGN)
-> Generic (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall x.
Rep (SignKeyDSIGN SchnorrSecp256k1DSIGN) x
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
forall x.
SignKeyDSIGN SchnorrSecp256k1DSIGN
-> Rep (SignKeyDSIGN SchnorrSecp256k1DSIGN) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (SignKeyDSIGN SchnorrSecp256k1DSIGN) x
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
$cfrom :: forall x.
SignKeyDSIGN SchnorrSecp256k1DSIGN
-> Rep (SignKeyDSIGN SchnorrSecp256k1DSIGN) x
Generic)
    deriving anyclass (Context
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
Proxy (SignKeyDSIGN SchnorrSecp256k1DSIGN) -> String
(Context
 -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo))
-> (Context
    -> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo))
-> (Proxy (SignKeyDSIGN SchnorrSecp256k1DSIGN) -> String)
-> NoThunks (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (SignKeyDSIGN SchnorrSecp256k1DSIGN) -> String
$cshowTypeOf :: Proxy (SignKeyDSIGN SchnorrSecp256k1DSIGN) -> String
wNoThunks :: Context
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
noThunks :: Context
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
$cnoThunks :: Context
-> SignKeyDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
NoThunks)
  newtype SigDSIGN SchnorrSecp256k1DSIGN =
    SigSchnorrSecp256k1 (PinnedSizedBytes (SizeSigDSIGN SchnorrSecp256k1DSIGN))
    deriving newtype (SigDSIGN SchnorrSecp256k1DSIGN
-> SigDSIGN SchnorrSecp256k1DSIGN -> Bool
(SigDSIGN SchnorrSecp256k1DSIGN
 -> SigDSIGN SchnorrSecp256k1DSIGN -> Bool)
-> (SigDSIGN SchnorrSecp256k1DSIGN
    -> SigDSIGN SchnorrSecp256k1DSIGN -> Bool)
-> Eq (SigDSIGN SchnorrSecp256k1DSIGN)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SigDSIGN SchnorrSecp256k1DSIGN
-> SigDSIGN SchnorrSecp256k1DSIGN -> Bool
$c/= :: SigDSIGN SchnorrSecp256k1DSIGN
-> SigDSIGN SchnorrSecp256k1DSIGN -> Bool
== :: SigDSIGN SchnorrSecp256k1DSIGN
-> SigDSIGN SchnorrSecp256k1DSIGN -> Bool
$c== :: SigDSIGN SchnorrSecp256k1DSIGN
-> SigDSIGN SchnorrSecp256k1DSIGN -> Bool
Eq, SigDSIGN SchnorrSecp256k1DSIGN -> ()
(SigDSIGN SchnorrSecp256k1DSIGN -> ())
-> NFData (SigDSIGN SchnorrSecp256k1DSIGN)
forall a. (a -> ()) -> NFData a
rnf :: SigDSIGN SchnorrSecp256k1DSIGN -> ()
$crnf :: SigDSIGN SchnorrSecp256k1DSIGN -> ()
NFData)
    deriving stock (Int -> SigDSIGN SchnorrSecp256k1DSIGN -> ShowS
[SigDSIGN SchnorrSecp256k1DSIGN] -> ShowS
SigDSIGN SchnorrSecp256k1DSIGN -> String
(Int -> SigDSIGN SchnorrSecp256k1DSIGN -> ShowS)
-> (SigDSIGN SchnorrSecp256k1DSIGN -> String)
-> ([SigDSIGN SchnorrSecp256k1DSIGN] -> ShowS)
-> Show (SigDSIGN SchnorrSecp256k1DSIGN)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SigDSIGN SchnorrSecp256k1DSIGN] -> ShowS
$cshowList :: [SigDSIGN SchnorrSecp256k1DSIGN] -> ShowS
show :: SigDSIGN SchnorrSecp256k1DSIGN -> String
$cshow :: SigDSIGN SchnorrSecp256k1DSIGN -> String
showsPrec :: Int -> SigDSIGN SchnorrSecp256k1DSIGN -> ShowS
$cshowsPrec :: Int -> SigDSIGN SchnorrSecp256k1DSIGN -> ShowS
Show, (forall x.
 SigDSIGN SchnorrSecp256k1DSIGN
 -> Rep (SigDSIGN SchnorrSecp256k1DSIGN) x)
-> (forall x.
    Rep (SigDSIGN SchnorrSecp256k1DSIGN) x
    -> SigDSIGN SchnorrSecp256k1DSIGN)
-> Generic (SigDSIGN SchnorrSecp256k1DSIGN)
forall x.
Rep (SigDSIGN SchnorrSecp256k1DSIGN) x
-> SigDSIGN SchnorrSecp256k1DSIGN
forall x.
SigDSIGN SchnorrSecp256k1DSIGN
-> Rep (SigDSIGN SchnorrSecp256k1DSIGN) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (SigDSIGN SchnorrSecp256k1DSIGN) x
-> SigDSIGN SchnorrSecp256k1DSIGN
$cfrom :: forall x.
SigDSIGN SchnorrSecp256k1DSIGN
-> Rep (SigDSIGN SchnorrSecp256k1DSIGN) x
Generic)
    deriving anyclass (Context -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
Proxy (SigDSIGN SchnorrSecp256k1DSIGN) -> String
(Context -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo))
-> (Context
    -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo))
-> (Proxy (SigDSIGN SchnorrSecp256k1DSIGN) -> String)
-> NoThunks (SigDSIGN SchnorrSecp256k1DSIGN)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (SigDSIGN SchnorrSecp256k1DSIGN) -> String
$cshowTypeOf :: Proxy (SigDSIGN SchnorrSecp256k1DSIGN) -> String
wNoThunks :: Context -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
noThunks :: Context -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SigDSIGN SchnorrSecp256k1DSIGN -> IO (Maybe ThunkInfo)
NoThunks)
  algorithmNameDSIGN :: proxy SchnorrSecp256k1DSIGN -> String
algorithmNameDSIGN proxy SchnorrSecp256k1DSIGN
_ = String
"schnorr-secp256k1"
  {-# NOINLINE deriveVerKeyDSIGN #-}
  deriveVerKeyDSIGN :: SignKeyDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
deriveVerKeyDSIGN (SignKeySchnorrSecp256k1 psb) = 
    IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
forall a. IO a -> a
unsafeDupablePerformIO (IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
 -> VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> ((SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
     -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
    -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
    -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
    -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
psb ((SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
  -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
 -> VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
    -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
skp ->
      (SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
 -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall (n :: Nat) b. KnownNat n => (SizedPtr n -> IO b) -> IO b
allocaSized ((SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
  -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
 -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> (SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
    -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
kpp -> 
        ForeignPtr SECP256k1Context
-> (Ptr SECP256k1Context -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SECP256k1Context
secpCtxPtr ((Ptr SECP256k1Context -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
 -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> (Ptr SECP256k1Context -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ \Ptr SECP256k1Context
ctx -> do
          CInt
res <- Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
-> SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
-> IO CInt
secpKeyPairCreate Ptr SECP256k1Context
ctx SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
kpp SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
skp
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
res CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
1) 
               (String -> IO ()
forall a. HasCallStack => String -> a
error String
"deriveVerKeyDSIGN: Failed to create keypair for SchnorrSecp256k1DSIGN")
          PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
xonlyPSB <- (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO ())
 -> IO (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL))
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
xonlyp -> do
                        CInt
res' <- Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> Ptr CInt
-> SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
-> IO CInt
secpKeyPairXOnlyPub Ptr SECP256k1Context
ctx SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
xonlyp Ptr CInt
forall a. Ptr a
nullPtr SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
kpp
                        Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
res' CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
1) 
                             (String -> IO ()
forall a. HasCallStack => String -> a
error String
"deriveVerKeyDSIGN: could not extract xonly pubkey for SchnorrSecp256k1DSIGN")
          VerKeyDSIGN SchnorrSecp256k1DSIGN
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VerKeyDSIGN SchnorrSecp256k1DSIGN
 -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
VerKeySchnorrSecp256k1 (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
 -> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> IO (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
xonlyPSB
  {-# NOINLINE signDSIGN #-}
  signDSIGN :: ContextDSIGN SchnorrSecp256k1DSIGN
-> a
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
-> SigDSIGN SchnorrSecp256k1DSIGN
signDSIGN () a
msg (SignKeySchnorrSecp256k1 skpsb) = 
    IO (SigDSIGN SchnorrSecp256k1DSIGN)
-> SigDSIGN SchnorrSecp256k1DSIGN
forall a. IO a -> a
unsafeDupablePerformIO (IO (SigDSIGN SchnorrSecp256k1DSIGN)
 -> SigDSIGN SchnorrSecp256k1DSIGN)
-> ((SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
     -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
    -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
    -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> SigDSIGN SchnorrSecp256k1DSIGN
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
    -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
skpsb ((SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
  -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
 -> SigDSIGN SchnorrSecp256k1DSIGN)
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
    -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> SigDSIGN SchnorrSecp256k1DSIGN
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
skp -> do
      let bs :: ByteString
bs = a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
msg
      (SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
 -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall (n :: Nat) b. KnownNat n => (SizedPtr n -> IO b) -> IO b
allocaSized ((SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
  -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
 -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> (SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
    -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
kpp -> 
        ForeignPtr SECP256k1Context
-> (Ptr SECP256k1Context -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SECP256k1Context
secpCtxPtr ((Ptr SECP256k1Context -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
 -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> (Ptr SECP256k1Context -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ \Ptr SECP256k1Context
ctx -> do
          CInt
res <- Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
-> SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
-> IO CInt
secpKeyPairCreate Ptr SECP256k1Context
ctx SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
kpp SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
skp
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
res CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
1) (String -> IO ()
forall a. HasCallStack => String -> a
error String
"signDSIGN: Failed to create keypair for SchnorrSecp256k1DSIGN")
          PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
sigPSB <- (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO ())
 -> IO (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL))
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
sigp -> ByteString -> (CStringLen -> IO ()) -> IO ()
forall a. ByteString -> (CStringLen -> IO a) -> IO a
useAsCStringLen ByteString
bs ((CStringLen -> IO ()) -> IO ()) -> (CStringLen -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
msgp, Int
msgLen) -> do
            CInt
res' <- Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> Ptr CUChar
-> CSize
-> SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
-> Ptr SECP256k1SchnorrExtraParams
-> IO CInt
secpSchnorrSigSignCustom Ptr SECP256k1Context
ctx
                                             SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
sigp
                                             (Ptr CChar -> Ptr CUChar
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
msgp)
                                             (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
msgLen)
                                             SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES
kpp
                                             Ptr SECP256k1SchnorrExtraParams
forall a. Ptr a
nullPtr
            Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
res' CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
1) (String -> IO ()
forall a. HasCallStack => String -> a
error String
"signDSIGN: Failed to sign SchnorrSecp256k1DSIGN message")
          SigDSIGN SchnorrSecp256k1DSIGN
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SigDSIGN SchnorrSecp256k1DSIGN
 -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> SigDSIGN SchnorrSecp256k1DSIGN)
-> PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> SigDSIGN SchnorrSecp256k1DSIGN
PinnedSizedBytes (SizeSigDSIGN SchnorrSecp256k1DSIGN)
-> SigDSIGN SchnorrSecp256k1DSIGN
SigSchnorrSecp256k1 (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
 -> IO (SigDSIGN SchnorrSecp256k1DSIGN))
-> PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> IO (SigDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
sigPSB
  {-# NOINLINE verifyDSIGN #-}
  verifyDSIGN :: ContextDSIGN SchnorrSecp256k1DSIGN
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
-> a
-> SigDSIGN SchnorrSecp256k1DSIGN
-> Either String ()
verifyDSIGN () (VerKeySchnorrSecp256k1 pubkeyPSB) a
msg (SigSchnorrSecp256k1 sigPSB) =
    IO (Either String ()) -> Either String ()
forall a. IO a -> a
unsafeDupablePerformIO (IO (Either String ()) -> Either String ())
-> ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
     -> IO (Either String ()))
    -> IO (Either String ()))
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO (Either String ()))
-> Either String ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO (Either String ()))
-> IO (Either String ())
forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
pubkeyPSB ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
  -> IO (Either String ()))
 -> Either String ())
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO (Either String ()))
-> Either String ()
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
pkp ->
      PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO (Either String ()))
-> IO (Either String ())
forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
PinnedSizedBytes (SizeSigDSIGN SchnorrSecp256k1DSIGN)
sigPSB ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
  -> IO (Either String ()))
 -> IO (Either String ()))
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO (Either String ()))
-> IO (Either String ())
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
sigp -> do
        let bs :: ByteString
bs = a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
msg
        CInt
res <- ByteString -> (CStringLen -> IO CInt) -> IO CInt
forall a. ByteString -> (CStringLen -> IO a) -> IO a
useAsCStringLen ByteString
bs ((CStringLen -> IO CInt) -> IO CInt)
-> (CStringLen -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
msgp, Int
msgLen) ->
          ForeignPtr SECP256k1Context
-> (Ptr SECP256k1Context -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SECP256k1Context
secpCtxPtr ((Ptr SECP256k1Context -> IO CInt) -> IO CInt)
-> (Ptr SECP256k1Context -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr SECP256k1Context
ctx -> 
            CInt -> IO CInt
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CInt -> IO CInt) -> CInt -> IO CInt
forall a b. (a -> b) -> a -> b
$ Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> Ptr CUChar
-> CSize
-> SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> CInt
secpSchnorrSigVerify Ptr SECP256k1Context
ctx
                                        SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
sigp
                                        (Ptr CChar -> Ptr CUChar
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
msgp) 
                                        (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
msgLen)
                                        SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
pkp
        Either String () -> IO (Either String ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either String () -> IO (Either String ()))
-> Either String () -> IO (Either String ())
forall a b. (a -> b) -> a -> b
$ if CInt
res CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0
          then String -> Either String ()
forall a b. a -> Either a b
Left String
"SigDSIGN SchnorrSecp256k1DSIGN failed to verify."
          else () -> Either String ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  {-# NOINLINE genKeyDSIGN #-}
  genKeyDSIGN :: Seed -> SignKeyDSIGN SchnorrSecp256k1DSIGN
genKeyDSIGN Seed
seed = PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
SignKeySchnorrSecp256k1 (PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
 -> SignKeyDSIGN SchnorrSecp256k1DSIGN)
-> PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
forall a b. (a -> b) -> a -> b
$
    let (ByteString
bs, Seed
_) = Word -> Seed -> (ByteString, Seed)
getBytesFromSeedT (Proxy SchnorrSecp256k1DSIGN -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
seedSizeDSIGN (Proxy SchnorrSecp256k1DSIGN
forall k (t :: k). Proxy t
Proxy @SchnorrSecp256k1DSIGN)) Seed
seed
    in IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
-> PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
forall a. IO a -> a
unsafeDupablePerformIO (IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
 -> PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
-> PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
forall a b. (a -> b) -> a -> b
$
         (Ptr Word8 -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
forall (n :: Nat).
KnownNat n =>
(Ptr Word8 -> IO ()) -> IO (PinnedSizedBytes n)
psbCreate ((Ptr Word8 -> IO ())
 -> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES))
-> (Ptr Word8 -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
skp ->
           ByteString -> (CStringLen -> IO ()) -> IO ()
forall a. ByteString -> (CStringLen -> IO a) -> IO a
useAsCStringLen ByteString
bs ((CStringLen -> IO ()) -> IO ()) -> (CStringLen -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
bsp, Int
sz) ->
             Ptr Word8 -> Ptr Word8 -> Int -> IO ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Ptr a -> Ptr a -> Int -> m ()
copyPtr Ptr Word8
skp (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
bsp) Int
sz
  rawSerialiseSigDSIGN :: SigDSIGN SchnorrSecp256k1DSIGN -> ByteString
rawSerialiseSigDSIGN (SigSchnorrSecp256k1 sigPSB) = PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> ByteString
forall (n :: Nat). PinnedSizedBytes n -> ByteString
psbToByteString PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
PinnedSizedBytes (SizeSigDSIGN SchnorrSecp256k1DSIGN)
sigPSB
  {-# NOINLINE rawSerialiseVerKeyDSIGN #-}
  rawSerialiseVerKeyDSIGN :: VerKeyDSIGN SchnorrSecp256k1DSIGN -> ByteString
rawSerialiseVerKeyDSIGN (VerKeySchnorrSecp256k1 vkPSB) = 
    IO ByteString -> ByteString
forall a. IO a -> a
unsafeDupablePerformIO (IO ByteString -> ByteString)
-> ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
     -> IO ByteString)
    -> IO ByteString)
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO ByteString)
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO ByteString)
-> IO ByteString
forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
vkPSB ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
  -> IO ByteString)
 -> ByteString)
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> IO ByteString)
-> ByteString
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
pkbPtr -> do
      PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
res <- (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized ((SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES -> IO ())
 -> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES))
-> (SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES -> IO ())
-> IO (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
bsPtr -> 
        ForeignPtr SECP256k1Context
-> (Ptr SECP256k1Context -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SECP256k1Context
secpCtxPtr ((Ptr SECP256k1Context -> IO ()) -> IO ())
-> (Ptr SECP256k1Context -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr SECP256k1Context
ctx -> do
          CInt
res' <- Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
-> SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> IO CInt
secpXOnlyPubkeySerialize Ptr SECP256k1Context
ctx SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES
bsPtr SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
pkbPtr
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
res' CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
1) 
               (String -> IO ()
forall a. HasCallStack => String -> a
error String
"rawSerialiseVerKeyDSIGN: Failed to serialise VerKeyDSIGN SchnorrSecp256k1DSIGN")
      ByteString -> IO ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> IO ByteString)
-> (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES -> ByteString)
-> PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
-> IO ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES -> ByteString
forall (n :: Nat). PinnedSizedBytes n -> ByteString
psbToByteString (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES -> IO ByteString)
-> PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
-> IO ByteString
forall a b. (a -> b) -> a -> b
$ PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
res
  rawSerialiseSignKeyDSIGN :: SignKeyDSIGN SchnorrSecp256k1DSIGN -> ByteString
rawSerialiseSignKeyDSIGN (SignKeySchnorrSecp256k1 skPSB) = PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES -> ByteString
forall (n :: Nat). PinnedSizedBytes n -> ByteString
psbToByteString PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
skPSB
  {-# NOINLINE rawDeserialiseVerKeyDSIGN #-}
  rawDeserialiseVerKeyDSIGN :: ByteString -> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
rawDeserialiseVerKeyDSIGN ByteString
bs = 
    IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. IO a -> a
unsafeDupablePerformIO (IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
 -> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> ((CStringLen -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
    -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
-> (CStringLen -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString
-> (CStringLen -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
-> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
forall a. ByteString -> (CStringLen -> IO a) -> IO a
unsafeUseAsCStringLen ByteString
bs ((CStringLen -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
 -> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> (CStringLen -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ptr, Int
len) ->
      if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= (Natural -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Natural -> Int)
-> (Proxy SECP256K1_SCHNORR_PRIVKEY_BYTES -> Natural)
-> Proxy SECP256K1_SCHNORR_PRIVKEY_BYTES
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy SECP256K1_SCHNORR_PRIVKEY_BYTES -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natVal (Proxy SECP256K1_SCHNORR_PRIVKEY_BYTES -> Int)
-> Proxy SECP256K1_SCHNORR_PRIVKEY_BYTES -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (SizeVerKeyDSIGN SchnorrSecp256k1DSIGN)
forall k (t :: k). Proxy t
Proxy @(SizeVerKeyDSIGN SchnorrSecp256k1DSIGN))
      then Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. Maybe a
Nothing
      else do
        let dataPtr :: Ptr CUChar
dataPtr = Ptr CChar -> Ptr CUChar
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr
        (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
vkPsb, CInt
res) <- (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO CInt)
-> IO
     (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL, CInt)
forall (n :: Nat) r.
KnownNat n =>
(SizedPtr n -> IO r) -> IO (PinnedSizedBytes n, r)
psbCreateSizedResult ((SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO CInt)
 -> IO
      (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL, CInt))
-> (SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL -> IO CInt)
-> IO
     (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL, CInt)
forall a b. (a -> b) -> a -> b
$ \SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
outPtr ->
          ForeignPtr SECP256k1Context
-> (Ptr SECP256k1Context -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SECP256k1Context
secpCtxPtr ((Ptr SECP256k1Context -> IO CInt) -> IO CInt)
-> (Ptr SECP256k1Context -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr SECP256k1Context
ctx -> 
            Ptr SECP256k1Context
-> SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> Ptr CUChar
-> IO CInt
secpXOnlyPubkeyParse Ptr SECP256k1Context
ctx SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
outPtr Ptr CUChar
dataPtr
        Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
 -> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)))
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> IO (Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
forall a b. (a -> b) -> a -> b
$ case CInt
res of 
          CInt
1 -> VerKeyDSIGN SchnorrSecp256k1DSIGN
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VerKeyDSIGN SchnorrSecp256k1DSIGN
 -> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
    -> VerKeyDSIGN SchnorrSecp256k1DSIGN)
-> PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> VerKeyDSIGN SchnorrSecp256k1DSIGN
VerKeySchnorrSecp256k1 (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
 -> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN))
-> PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a b. (a -> b) -> a -> b
$ PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
vkPsb
          CInt
_ -> Maybe (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall a. Maybe a
Nothing
  rawDeserialiseSignKeyDSIGN :: ByteString -> Maybe (SignKeyDSIGN SchnorrSecp256k1DSIGN)
rawDeserialiseSignKeyDSIGN ByteString
bs = 
    PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
PinnedSizedBytes (SizeSignKeyDSIGN SchnorrSecp256k1DSIGN)
-> SignKeyDSIGN SchnorrSecp256k1DSIGN
SignKeySchnorrSecp256k1 (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES
 -> SignKeyDSIGN SchnorrSecp256k1DSIGN)
-> Maybe (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
-> Maybe (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Maybe (PinnedSizedBytes SECP256K1_SCHNORR_PRIVKEY_BYTES)
forall (n :: Nat).
KnownNat n =>
ByteString -> Maybe (PinnedSizedBytes n)
psbFromByteStringCheck ByteString
bs
  rawDeserialiseSigDSIGN :: ByteString -> Maybe (SigDSIGN SchnorrSecp256k1DSIGN)
rawDeserialiseSigDSIGN ByteString
bs = 
    PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
-> SigDSIGN SchnorrSecp256k1DSIGN
PinnedSizedBytes (SizeSigDSIGN SchnorrSecp256k1DSIGN)
-> SigDSIGN SchnorrSecp256k1DSIGN
SigSchnorrSecp256k1 (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL
 -> SigDSIGN SchnorrSecp256k1DSIGN)
-> Maybe (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
-> Maybe (SigDSIGN SchnorrSecp256k1DSIGN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Maybe (PinnedSizedBytes SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL)
forall (n :: Nat).
KnownNat n =>
ByteString -> Maybe (PinnedSizedBytes n)
psbFromByteStringCheck ByteString
bs

instance ToCBOR (VerKeyDSIGN SchnorrSecp256k1DSIGN) where
  toCBOR :: VerKeyDSIGN SchnorrSecp256k1DSIGN -> Encoding
toCBOR = VerKeyDSIGN SchnorrSecp256k1DSIGN -> Encoding
forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> Encoding
encodeVerKeyDSIGN
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyDSIGN SchnorrSecp256k1DSIGN) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ = Proxy (VerKeyDSIGN SchnorrSecp256k1DSIGN) -> Size
forall v. DSIGNAlgorithm v => Proxy (VerKeyDSIGN v) -> Size
encodedVerKeyDSIGNSizeExpr

instance FromCBOR (VerKeyDSIGN SchnorrSecp256k1DSIGN) where
  fromCBOR :: Decoder s (VerKeyDSIGN SchnorrSecp256k1DSIGN)
fromCBOR = Decoder s (VerKeyDSIGN SchnorrSecp256k1DSIGN)
forall v s. DSIGNAlgorithm v => Decoder s (VerKeyDSIGN v)
decodeVerKeyDSIGN

instance ToCBOR (SignKeyDSIGN SchnorrSecp256k1DSIGN) where
  toCBOR :: SignKeyDSIGN SchnorrSecp256k1DSIGN -> Encoding
toCBOR = SignKeyDSIGN SchnorrSecp256k1DSIGN -> Encoding
forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> Encoding
encodeSignKeyDSIGN
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyDSIGN SchnorrSecp256k1DSIGN) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ = Proxy (SignKeyDSIGN SchnorrSecp256k1DSIGN) -> Size
forall v. DSIGNAlgorithm v => Proxy (SignKeyDSIGN v) -> Size
encodedSignKeyDESIGNSizeExpr

instance FromCBOR (SignKeyDSIGN SchnorrSecp256k1DSIGN) where
  fromCBOR :: Decoder s (SignKeyDSIGN SchnorrSecp256k1DSIGN)
fromCBOR = Decoder s (SignKeyDSIGN SchnorrSecp256k1DSIGN)
forall v s. DSIGNAlgorithm v => Decoder s (SignKeyDSIGN v)
decodeSignKeyDSIGN

instance ToCBOR (SigDSIGN SchnorrSecp256k1DSIGN) where
  toCBOR :: SigDSIGN SchnorrSecp256k1DSIGN -> Encoding
toCBOR = SigDSIGN SchnorrSecp256k1DSIGN -> Encoding
forall v. DSIGNAlgorithm v => SigDSIGN v -> Encoding
encodeSigDSIGN
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SigDSIGN SchnorrSecp256k1DSIGN) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ = Proxy (SigDSIGN SchnorrSecp256k1DSIGN) -> Size
forall v. DSIGNAlgorithm v => Proxy (SigDSIGN v) -> Size
encodedSigDSIGNSizeExpr

instance FromCBOR (SigDSIGN SchnorrSecp256k1DSIGN) where
  fromCBOR :: Decoder s (SigDSIGN SchnorrSecp256k1DSIGN)
fromCBOR = Decoder s (SigDSIGN SchnorrSecp256k1DSIGN)
forall v s. DSIGNAlgorithm v => Decoder s (SigDSIGN v)
decodeSigDSIGN