Correct some code and fix Debian 9

This commit is contained in:
Jean-Pierre Rupp 2020-04-14 17:39:38 +01:00
parent 3307b6d5dd
commit 4fcaca3a20
5 changed files with 39 additions and 9 deletions

View File

@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## 0.2.2
### Removed
- Hide tweak negation behind a flag for compatibilidy with Debian 9.
### Fixed
- Correct code that was not compiling with some flags enabled.
## 0.2.1
### Changed
- Do not depend on hardcoded DER signatures in tests.

View File

@ -1,5 +1,5 @@
name: secp256k1-haskell
version: 0.2.1
version: 0.2.2
synopsis: Bindings for secp256k1 library from Bitcoin Core
description: Sign and verify signatures using the very fast C secp256k1 library from Pieter Wuille. Has Haskell types and abstractions for keys and signatures.
category: Crypto
@ -34,6 +34,8 @@ library:
cpp-options: -DSCHNORR
- condition: flag(recovery)
cpp-options: -DRECOVERY
- condition: flag(negate)
cpp-options: -DNEGATE
tests:
spec:
main: Spec.hs
@ -47,6 +49,10 @@ tests:
- mtl
- HUnit
flags:
negate:
description: "Enable tweak negate"
manual: true
default: false
ecdh:
description: "Enable (experimental) ECDH APIs"
manual: true

View File

@ -59,7 +59,9 @@ module Crypto.Secp256k1
, tweakAddPubKey
, tweakMulPubKey
, combinePubKeys
#ifdef NEGATE
, tweakNegate
#endif
#ifdef ECDH
-- * Diffie Hellman
@ -89,12 +91,15 @@ import qualified Data.ByteString.Base16 as B16
import Data.ByteString.Short (fromShort, toShort)
import Data.Hashable (Hashable (..))
import Data.Maybe (fromJust, fromMaybe, isJust)
import Data.Serialize (decode, encode)
import Data.String (IsString (..))
import Data.String.Conversions (ConvertibleStrings, cs)
import Foreign (ForeignPtr, alloca, allocaArray,
allocaBytes, mallocForeignPtr,
import Foreign (ForeignPtr, alloca,
allocaArray, allocaBytes,
mallocForeignPtr,
nullPtr, peek, poke, pokeArray,
withForeignPtr)
import Foreign.C.Types (CInt)
import System.IO.Unsafe (unsafePerformIO)
import Test.QuickCheck (Arbitrary (..),
arbitraryBoundedRandom, suchThat)
@ -542,7 +547,7 @@ signRecMsg (SecKey fk) (Msg fm) = withContext $ \ctx ->
withForeignPtr fk $ \k -> withForeignPtr fm $ \m -> do
fg <- mallocForeignPtr
ret <- withForeignPtr fg $ \g ->
ecdsaSignRecoverable ctx g m k nullFunPtr nullPtr
ecdsaSignRecoverable ctx g m k nullPtr nullPtr
unless (isSuccess ret) $ error "could not sign message"
return $ RecSig fg
@ -555,6 +560,7 @@ recover (RecSig frg) (Msg fm) = withContext $ \ctx ->
if isSuccess ret then return $ Just $ PubKey fp else return Nothing
#endif
#ifdef NEGATE
tweakNegate :: Tweak -> Maybe Tweak
tweakNegate (Tweak fk) = withContext $ \ctx -> do
fnew <- mallocForeignPtr
@ -566,6 +572,7 @@ tweakNegate (Tweak fk) = withContext $ \ctx -> do
if isSuccess ret
then Just (Tweak fnew)
else Nothing
#endif
#ifdef ECDH
-- | Compute Diffie-Hellman secret.
@ -611,10 +618,14 @@ schnorrTweakAddSecKey (SecKey fk) (Tweak ft) = withContext $ \ctx ->
if isSuccess ret then return $ Just $ SecKey fk' else return Nothing
signMsgSchnorr :: SecKey -> Msg -> SchnorrSig
signMsgSchnorr (SecKey fk) (Msg fm) = withContext $ \ctx ->
withForeignPtr fk $ \k -> withForeignPtr fm $ \m -> do
signMsgSchnorr (SecKey fk) (Msg fm) =
withContext $ \ctx ->
withForeignPtr fk $ \k ->
withForeignPtr fm $ \m -> do
fg <- mallocForeignPtr
ret <- withForeignPtr fg $ \g -> schnorrSign ctx g m k nullFunPtr nullPtr
ret <-
withForeignPtr fg $ \g ->
schnorrSign ctx g m k nullPtr nullPtr
unless (isSuccess ret) $ error "could not schnorr-sign message"
return $ SchnorrSig fg

View File

@ -23,6 +23,7 @@ import Data.Serialize (Serialize (..))
import qualified Data.Serialize.Get as Get
import qualified Data.Serialize.Put as Put
import Data.Void (Void)
import Data.Word (Word8)
import Foreign (ForeignPtr, FunPtr, Ptr, Storable (..),
alloca, castPtr, copyArray,
newForeignPtr, withForeignPtr)
@ -413,12 +414,14 @@ foreign import ccall
-> Ptr Tweak32
-> IO Ret
#ifdef NEGATE
foreign import ccall
"secp256k1.h secp256k1_ec_privkey_negate"
ecTweakNegate
:: Ptr Ctx
-> Ptr Tweak32
-> IO Ret
#endif
foreign import ccall
"secp256k1.h secp256k1_ec_pubkey_tweak_add"

View File

@ -6,12 +6,11 @@ import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Char8 as B8
import Data.Maybe (fromMaybe)
import Data.Serialize
import Data.String (fromString)
import Data.String.Conversions (cs)
import Test.Hspec
import Test.HUnit (Assertion, assertEqual)
import Test.QuickCheck (Property, property, (==>))
import Test.QuickCheck (property)
spec :: Spec
spec = do
@ -58,7 +57,9 @@ spec = do
it "multiply public key" $ property $ tweakMulPubKeyTest
it "combine public keys" $ property $ combinePubKeyTest
it "can't combine 0 public keys" $ property $ combinePubKeyEmptyListTest
#ifdef NEGATE
it "negates tweak" $ property $ negateTweakTest
#endif
#ifdef ECDH
describe "ecdh" $ do
it "computes dh secret" $ property $ computeDhSecret
@ -285,6 +286,7 @@ combinePubKeyEmptyListTest =
expected = Nothing
combined = combinePubKeys []
#ifdef NEGATE
negateTweakTest :: Assertion
negateTweakTest =
assertEqual "can recover secret key 1 after adding tweak 1" oneKey subtracted
@ -296,6 +298,7 @@ negateTweakTest =
Just minusOneTwk = tweakNegate oneTwk
Just twoKey = tweakAddSecKey oneKey oneTwk
Just subtracted = tweakAddSecKey twoKey minusOneTwk
#endif
#ifdef ECDH
computeDhSecret :: Assertion