{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Crypto.SECP256K1.C (
SECP256k1Context,
secpContextSignVerify,
SECP256k1SchnorrExtraParams,
secpContextCreate,
secpKeyPairCreate,
secpSchnorrSigSignCustom,
secpKeyPairXOnlyPub,
secpSchnorrSigVerify,
secpXOnlyPubkeySerialize,
secpXOnlyPubkeyParse,
secpCtxPtr,
secpEcPubkeyCreate,
secpEcdsaSign,
secpEcdsaVerify,
secpEcCompressed,
secpEcPubkeySerialize,
secpEcdsaSignatureSerializeCompact,
secpEcdsaSignatureParseCompact,
secpEcPubkeyParse,
) where
import Cardano.Crypto.SECP256K1.Constants (
SECP256K1_ECDSA_MESSAGE_BYTES,
SECP256K1_ECDSA_PRIVKEY_BYTES,
SECP256K1_ECDSA_PUBKEY_BYTES_INTERNAL,
SECP256K1_ECDSA_SIGNATURE_BYTES,
SECP256K1_ECDSA_SIGNATURE_BYTES_INTERNAL,
SECP256K1_SCHNORR_KEYPAIR_BYTES,
SECP256K1_SCHNORR_PRIVKEY_BYTES,
SECP256K1_SCHNORR_PUBKEY_BYTES,
SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL,
SECP256K1_SCHNORR_SIGNATURE_BYTES,
)
import Cardano.Foreign (SizedPtr (SizedPtr))
import Control.Exception (mask_)
import Data.Bits ((.|.))
import Foreign.C.Types (CInt (CInt), CSize (CSize), CUChar, CUInt (CUInt))
import Foreign.ForeignPtr (FinalizerPtr, ForeignPtr, newForeignPtr)
import Foreign.Ptr (Ptr)
import System.IO.Unsafe (unsafePerformIO)
data SECP256k1Context
data
{-# NOINLINE secpCtxPtr #-}
secpCtxPtr :: ForeignPtr SECP256k1Context
secpCtxPtr :: ForeignPtr SECP256k1Context
secpCtxPtr = forall a. IO a -> a
unsafePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ do
Ptr SECP256k1Context
ctx <- CUInt -> IO (Ptr SECP256k1Context)
secpContextCreate CUInt
secpContextSignVerify
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr SECP256k1Context
secpContextDestroy Ptr SECP256k1Context
ctx
foreign import ccall unsafe "secp256k1.h &secp256k1_context_destroy"
secpContextDestroy :: FinalizerPtr SECP256k1Context
foreign import ccall unsafe "secp256k1.h secp256k1_context_create"
secpContextCreate ::
CUInt ->
IO (Ptr SECP256k1Context)
foreign import capi "secp256k1.h value SECP256K1_CONTEXT_SIGN"
secpContextSign :: CUInt
foreign import capi "secp256k1.h value SECP256K1_CONTEXT_VERIFY"
secpContextVerify :: CUInt
secpContextSignVerify :: CUInt
secpContextSignVerify :: CUInt
secpContextSignVerify = CUInt
secpContextSign forall a. Bits a => a -> a -> a
.|. CUInt
secpContextVerify
foreign import capi "secp256k1.h value SECP256K1_EC_COMPRESSED"
secpEcCompressed :: CUInt
foreign import ccall unsafe "secp256k1_extrakeys.h secp256k1_keypair_create"
secpKeyPairCreate ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES ->
SizedPtr SECP256K1_SCHNORR_PRIVKEY_BYTES ->
IO CInt
foreign import ccall unsafe "secp256k1_schnorrsig.h secp256k1_schnorrsig_sign_custom"
secpSchnorrSigSignCustom ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_SCHNORR_SIGNATURE_BYTES ->
Ptr CUChar ->
CSize ->
SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES ->
Ptr SECP256k1SchnorrExtraParams ->
IO CInt
foreign import ccall unsafe "secp256k1_extrakeys.h secp256k1_keypair_xonly_pub"
secpKeyPairXOnlyPub ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL ->
Ptr CInt ->
SizedPtr SECP256K1_SCHNORR_KEYPAIR_BYTES ->
IO CInt
foreign import ccall unsafe "secp256k1_schnorrsig.h secp256k1_schnorrsig_verify"
secpSchnorrSigVerify ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_SCHNORR_SIGNATURE_BYTES ->
Ptr CUChar ->
CSize ->
SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL ->
CInt
foreign import ccall unsafe "secp256k1_extrakeys.h secp256k1_xonly_pubkey_serialize"
secpXOnlyPubkeySerialize ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES ->
SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL ->
IO CInt
foreign import ccall unsafe "secp256k1_extrakeys.h secp256k1_xonly_pubkey_parse"
secpXOnlyPubkeyParse ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_SCHNORR_PUBKEY_BYTES_INTERNAL ->
Ptr CUChar ->
IO CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ec_pubkey_create"
secpEcPubkeyCreate ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_ECDSA_PUBKEY_BYTES_INTERNAL ->
SizedPtr SECP256K1_ECDSA_PRIVKEY_BYTES ->
IO CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ecdsa_sign"
secpEcdsaSign ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_ECDSA_SIGNATURE_BYTES_INTERNAL ->
SizedPtr SECP256K1_ECDSA_MESSAGE_BYTES ->
SizedPtr SECP256K1_ECDSA_PRIVKEY_BYTES ->
Ptr CUChar ->
Ptr CUChar ->
IO CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ecdsa_verify"
secpEcdsaVerify ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_ECDSA_SIGNATURE_BYTES_INTERNAL ->
SizedPtr SECP256K1_ECDSA_MESSAGE_BYTES ->
SizedPtr SECP256K1_ECDSA_PUBKEY_BYTES_INTERNAL ->
CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ec_pubkey_serialize"
secpEcPubkeySerialize ::
Ptr SECP256k1Context ->
Ptr CUChar ->
Ptr CSize ->
SizedPtr SECP256K1_ECDSA_PUBKEY_BYTES_INTERNAL ->
CUInt ->
IO CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ecdsa_signature_serialize_compact"
secpEcdsaSignatureSerializeCompact ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_ECDSA_SIGNATURE_BYTES ->
SizedPtr SECP256K1_ECDSA_SIGNATURE_BYTES_INTERNAL ->
IO CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ecdsa_signature_parse_compact"
secpEcdsaSignatureParseCompact ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_ECDSA_SIGNATURE_BYTES_INTERNAL ->
SizedPtr SECP256K1_ECDSA_SIGNATURE_BYTES ->
IO CInt
foreign import ccall unsafe "secp256k1.h secp256k1_ec_pubkey_parse"
secpEcPubkeyParse ::
Ptr SECP256k1Context ->
SizedPtr SECP256K1_ECDSA_PUBKEY_BYTES_INTERNAL ->
Ptr CUChar ->
CSize ->
IO CInt