Skip to content

Commit 0c9002b

Browse files
committed
uncomment legacy tests and adapt them to new api
1 parent 2f68e8a commit 0c9002b

File tree

6 files changed

+709
-645
lines changed

6 files changed

+709
-645
lines changed

libsecp256k1.cabal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.34.4.
3+
-- This file has been generated from package.yaml by hpack version 0.36.0.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -62,11 +62,14 @@ test-suite spec
6262
build-depends:
6363
HUnit
6464
, base >=4.9 && <5
65+
, base16
6566
, bytestring >=0.10.8 && <0.12
67+
, either
6668
, entropy >=0.3.8 && <0.5
6769
, hedgehog
6870
, hspec
6971
, libsecp256k1
7072
, memory >=0.14.15 && <1.0
73+
, monad-par
7174
, transformers >=0.4.0.0 && <1.0
7275
default-language: Haskell2010

package.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ tests:
3333
- -rtsopts
3434
- -with-rtsopts=-N
3535
dependencies:
36+
- base16
37+
- either
3638
- hspec
3739
- libsecp256k1
40+
- monad-par
3841
- HUnit

src/Crypto/Secp256k1.hs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module Crypto.Secp256k1 (
4747
ecdsaSign,
4848
ecdsaSignRecoverable,
4949
ecdsaRecover,
50+
ecdsaNormalizeSignature,
5051

5152
-- * Conversions
5253
recSigToSig,
@@ -64,6 +65,7 @@ module Crypto.Secp256k1 (
6465
pubKeyCombine,
6566
pubKeyNegate,
6667
secKeyNegate,
68+
tweakNegate,
6769
pubKeyTweakAdd,
6870
pubKeyTweakMul,
6971
pubKeyXOTweakAdd,
@@ -362,12 +364,12 @@ importSignature bs = unsafePerformIO $
362364
outBuf <- mallocBytes 64
363365
ret <-
364366
if
365-
-- compact
366-
| len == 64 -> Prim.ecdsaSignatureParseCompact ctx outBuf inBuf
367-
-- der
368-
| len >= 69 && len <= 73 -> Prim.ecdsaSignatureParseDer ctx outBuf inBuf len
369-
-- invalid
370-
| otherwise -> pure 0
367+
-- compact
368+
| len == 64 -> Prim.ecdsaSignatureParseCompact ctx outBuf inBuf
369+
-- der
370+
| len >= 69 && len <= 73 -> Prim.ecdsaSignatureParseDer ctx outBuf inBuf len
371+
-- invalid
372+
| otherwise -> pure 0
371373
if isSuccess ret
372374
then Just . Signature <$> newForeignPtr finalizerFree outBuf
373375
else free outBuf $> Nothing
@@ -485,6 +487,17 @@ ecdsaRecover RecoverableSignature{..} msgHash
485487
else free pubKeyBuf $> Nothing
486488

487489

490+
-- | Convert a 'Signature' to a normalized lower-S form. If the 'Signature' was already in its lower-S form it will
491+
-- be equal to the input.
492+
ecdsaNormalizeSignature :: Signature -> Signature
493+
ecdsaNormalizeSignature Signature{..} = unsafePerformIO . evalContT $ do
494+
sigPtr <- ContT (withForeignPtr signatureFPtr)
495+
lift $ do
496+
outBuf <- mallocBytes 64
497+
_ret <- Prim.ecdsaSignatureNormalize ctx outBuf sigPtr
498+
Signature <$> newForeignPtr finalizerFree outBuf
499+
500+
488501
-- | Forgets the recovery id of a signature
489502
recSigToSig :: RecoverableSignature -> Signature
490503
recSigToSig RecoverableSignature{..} = unsafePerformIO . evalContT $ do
@@ -724,6 +737,16 @@ secKeyNegate SecKey{..} = unsafePerformIO $ do
724737
SecKey <$> newForeignPtr finalizerFree outBuf
725738

726739

740+
-- | Negate a 'Tweak'
741+
tweakNegate :: Tweak -> Tweak
742+
tweakNegate Tweak{..} = unsafePerformIO $ do
743+
outBuf <- mallocBytes 32
744+
let asKey = castForeignPtr tweakFPtr
745+
withForeignPtr asKey $ flip (memcpy outBuf) 32
746+
_ret <- Prim.ecSeckeyNegate ctx outBuf
747+
Tweak <$> newForeignPtr finalizerFree (castPtr outBuf)
748+
749+
727750
-- | Convert 'PubKeyXY' to 'PubKeyXO'. See 'keyPairPubKeyXO' for more information on how to interpret the parity bit.
728751
xyToXO :: PubKeyXY -> (PubKeyXO, Bool)
729752
xyToXO PubKeyXY{..} = unsafePerformIO $ do

0 commit comments

Comments
 (0)