csecp256k1

Haskell FFI bindings to bitcoin-core/secp256k1.
git clone git://git.ppad.tech/csecp256k1.git
Log | Files | Refs | README | LICENSE

commit a71ad772508b5983607eca5fa2de6c98064fa0cf
parent 75d24657209d68b3c9eb5c17a7c38b2d71cd5a05
Author: Jared Tobin <jared@jtobin.io>
Date:   Mon, 26 Aug 2024 13:18:23 -0230

secp256k1-sys: add internal wcontext

Eases benchmarking while making an illegal state unrepresentable to the
library user.

Diffstat:
Mbench/Main.hs | 4++--
Mlib/Crypto/Secp256k1.hs | 2+-
Msecp256k1-sys/lib/Crypto/Secp256k1/Internal.hs | 10++++++++++
Msecp256k1-sys/test/Main.hs | 8+-------
4 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/bench/Main.hs b/bench/Main.hs @@ -37,8 +37,8 @@ suite = envWithCleanup setup destroy $ \ ~(tex, fen, pub, sig) -> where setup = do ptr <- SI.secp256k1_context_create SI._SECP256K1_CONTEXT_NONE - pub <- S.wcontext $ \tex -> S.parse_pub tex _PUB_COMPRESSED - sig <- S.wcontext $ \tex -> S.parse_der tex _DER + pub <- SI.wcontext $ \tex -> S.parse_pub (S.Context tex) _PUB_COMPRESSED + sig <- SI.wcontext $ \tex -> S.parse_der (S.Context tex) _DER pure (S.Context ptr, BS.replicate 32 0, pub, sig) destroy (S.Context tex, _, _, _) = SI.secp256k1_context_destroy tex diff --git a/lib/Crypto/Secp256k1.hs b/lib/Crypto/Secp256k1.hs @@ -61,7 +61,7 @@ module Crypto.Secp256k1 ( import Control.Exception (Exception, bracket, throwIO) import Control.Monad (when) -import Crypto.Secp256k1.Internal hiding (Context) +import Crypto.Secp256k1.Internal hiding (Context, wcontext) import qualified Crypto.Secp256k1.Internal as I (Context) import GHC.Generics import qualified Data.ByteString as BS diff --git a/secp256k1-sys/lib/Crypto/Secp256k1/Internal.hs b/secp256k1-sys/lib/Crypto/Secp256k1/Internal.hs @@ -20,6 +20,7 @@ module Crypto.Secp256k1.Internal ( , secp256k1_context_create , secp256k1_context_destroy , secp256k1_context_randomize + , wcontext -- ec , PubKey64 @@ -61,6 +62,7 @@ module Crypto.Secp256k1.Internal ( , secp256k1_schnorrsig_verify ) where +import Control.Exception (bracket) import Foreign.Ptr (Ptr) import Foreign.C.Types (CUChar(..), CInt(..), CUInt(..), CSize(..)) @@ -146,6 +148,14 @@ foreign import capi -> Ptr Seed32 -> IO CInt +-- returning the context itself and attempting to use it outside of a +-- 'wcontext' block will produce segfaults +wcontext :: (Ptr Context -> IO a) -> IO a +wcontext = + bracket + (secp256k1_context_create _SECP256K1_CONTEXT_NONE) + secp256k1_context_destroy + -- ec -- 64-byte public key diff --git a/secp256k1-sys/test/Main.hs b/secp256k1-sys/test/Main.hs @@ -4,7 +4,7 @@ module Main where import Control.Monad (when) -import Control.Exception (Exception, bracket, throwIO) +import Control.Exception (Exception, throwIO) import Crypto.Secp256k1.Internal import qualified Data.ByteString as BS import Foreign.Ptr (Ptr) @@ -51,12 +51,6 @@ units = testGroup "unit tests" [ -- context -wcontext :: (Ptr Context -> IO a) -> IO a -wcontext = - bracket - (secp256k1_context_create _SECP256K1_CONTEXT_NONE) - secp256k1_context_destroy - wentropy :: (Ptr Seed32 -> IO a) -> IO a wentropy c = do bs <- E.getEntropy 32