From 63a3d0bafe00fddd106b5e5f703069c1d8ee820a Mon Sep 17 00:00:00 2001 From: prolic Date: Mon, 19 May 2025 15:15:20 -0300 Subject: [PATCH] fix: Add C shim for static context to address Windows MinGW64 runtime failure This change attempts to address a Windows MinGW64 runtime failure that occurs during testing, where we see a "32 bit pseudo relocation out of range" error. The error suggests an issue with how GHC handles pointers in Windows executables. 1. Adds a C shim (hs_secp256k1_shim.c) to handle the static context 2. Updates the Haskell FFI bindings to use the shim instead of direct access 3. Bumps version to 0.3.1 to reflect the FFI interface change --- cbits/hs_secp256k1_shim.c | 7 +++++++ libsecp256k1.cabal | 4 +++- package.yaml | 2 +- src/Crypto/Secp256k1/Prim.hs | 10 +++++++--- 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 cbits/hs_secp256k1_shim.c diff --git a/cbits/hs_secp256k1_shim.c b/cbits/hs_secp256k1_shim.c new file mode 100644 index 0000000..81aa9a4 --- /dev/null +++ b/cbits/hs_secp256k1_shim.c @@ -0,0 +1,7 @@ +#include + +/* Returns the adress of the library's built-in context */ +const secp256k1_context* hs_secp256k1_content_static(void) +{ + return secp256k1_context_static; +} diff --git a/libsecp256k1.cabal b/libsecp256k1.cabal index 06ea1e6..2ce52fc 100644 --- a/libsecp256k1.cabal +++ b/libsecp256k1.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: libsecp256k1 -version: 0.2.1 +version: 0.3.1 synopsis: Bindings for secp256k1 description: Sign and verify signatures using the secp256k1 library. category: Crypto @@ -35,6 +35,8 @@ library Paths_libsecp256k1 hs-source-dirs: src + c-sources: + cbits/hs_secp256k1_shim.c default-extensions: ImportQualifiedPost pkgconfig-depends: diff --git a/package.yaml b/package.yaml index 1ee59f8..0f3caf4 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: libsecp256k1 -version: 0.2.1 +version: 0.3.1 synopsis: Bindings for secp256k1 description: Sign and verify signatures using the secp256k1 library. category: Crypto diff --git a/src/Crypto/Secp256k1/Prim.hs b/src/Crypto/Secp256k1/Prim.hs index 62d44d3..4420f97 100644 --- a/src/Crypto/Secp256k1/Prim.hs +++ b/src/Crypto/Secp256k1/Prim.hs @@ -302,8 +302,12 @@ foreign import capi safe "secp256k1.h secp256k1_context_destroy" -- type serialization/parsing functions which require a context object to maintain -- API consistency, but currently do not require expensive precomputations or dynamic -- allocations. -foreign import ccall unsafe "secp256k1.h secp256k1_context_no_precomp" - contextNoPrecomp :: Ctx +foreign import ccall unsafe "hs_secp256k1_content_static" + c_contextStatic :: IO Ctx + +{-# NOINLINE contextStatic #-} +contextStatic :: Ctx +contextStatic = unsafePerformIO c_contextStatic -- | Copy a secp256k1 context object into caller-provided memory. @@ -445,7 +449,7 @@ foreign import capi safe "secp256k1.h secp256k1_context_set_error_callback" -- undefined. -- -- When this function has not been called (or called with fn==NULL), then the --- default handler will be used. The library provides a default handler which +-- default handler will be used. The library provides a default handler which -- writes the message to stderr and calls abort. This default handler can be -- replaced at link time if the preprocessor macro -- USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build