@@ -47,6 +47,7 @@ module Crypto.Secp256k1 (
47
47
ecdsaSign ,
48
48
ecdsaSignRecoverable ,
49
49
ecdsaRecover ,
50
+ ecdsaNormalizeSignature ,
50
51
51
52
-- * Conversions
52
53
recSigToSig ,
@@ -64,6 +65,7 @@ module Crypto.Secp256k1 (
64
65
pubKeyCombine ,
65
66
pubKeyNegate ,
66
67
secKeyNegate ,
68
+ tweakNegate ,
67
69
pubKeyTweakAdd ,
68
70
pubKeyTweakMul ,
69
71
pubKeyXOTweakAdd ,
@@ -362,12 +364,12 @@ importSignature bs = unsafePerformIO $
362
364
outBuf <- mallocBytes 64
363
365
ret <-
364
366
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
371
373
if isSuccess ret
372
374
then Just . Signature <$> newForeignPtr finalizerFree outBuf
373
375
else free outBuf $> Nothing
@@ -485,6 +487,17 @@ ecdsaRecover RecoverableSignature{..} msgHash
485
487
else free pubKeyBuf $> Nothing
486
488
487
489
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
+
488
501
-- | Forgets the recovery id of a signature
489
502
recSigToSig :: RecoverableSignature -> Signature
490
503
recSigToSig RecoverableSignature {.. } = unsafePerformIO . evalContT $ do
@@ -724,6 +737,16 @@ secKeyNegate SecKey{..} = unsafePerformIO $ do
724
737
SecKey <$> newForeignPtr finalizerFree outBuf
725
738
726
739
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
+
727
750
-- | Convert 'PubKeyXY' to 'PubKeyXO'. See 'keyPairPubKeyXO' for more information on how to interpret the parity bit.
728
751
xyToXO :: PubKeyXY -> (PubKeyXO , Bool )
729
752
xyToXO PubKeyXY {.. } = unsafePerformIO $ do
0 commit comments