@@ -52,13 +52,14 @@ module Crypto.Secp256k1 (
52
52
recSigToSig ,
53
53
derivePubKey ,
54
54
keyPairCreate ,
55
+ keyPairSecKey ,
55
56
keyPairPubKeyXY ,
56
57
keyPairPubKeyXO ,
57
58
xyToXO ,
58
59
59
60
-- * Tweaks
60
- ecSecKeyTweakAdd ,
61
- ecSecKeyTweakMul ,
61
+ secKeyTweakAdd ,
62
+ secKeyTweakMul ,
62
63
keyPairPubKeyXOTweakAdd ,
63
64
pubKeyCombine ,
64
65
pubKeyNegate ,
@@ -70,8 +71,6 @@ module Crypto.Secp256k1 (
70
71
71
72
-- * Schnorr Operations
72
73
schnorrSign ,
73
- SchnorrExtra (.. ),
74
- schnorrSignCustom ,
75
74
schnorrVerify ,
76
75
77
76
-- * Other
@@ -109,6 +108,8 @@ import Foreign (
109
108
FunPtr ,
110
109
Ptr ,
111
110
Storable ,
111
+ Word32 ,
112
+ Word64 ,
112
113
Word8 ,
113
114
alloca ,
114
115
allocaArray ,
@@ -136,6 +137,7 @@ import Foreign (
136
137
withForeignPtr ,
137
138
)
138
139
import Foreign.C (CInt (.. ), CSize (.. ))
140
+ import Foreign.Storable (Storable (.. ))
139
141
import GHC.Generics (Generic )
140
142
import GHC.IO.Handle.Text (memcpy )
141
143
import System.IO.Unsafe (unsafePerformIO )
@@ -230,7 +232,7 @@ newtype Signature = Signature {signatureFPtr :: ForeignPtr Prim.Sig64}
230
232
231
233
232
234
instance Show Signature where
233
- show sig = " 0x" <> B8. unpack ( BA. convertToBase BA. Base16 (exportSignatureCompact sig) )
235
+ show sig = " 0x" <> ( B8. unpack . encodeBase16) (exportSignatureCompact sig)
234
236
instance Eq Signature where
235
237
sig == sig' = unsafePerformIO . evalContT $ do
236
238
sigp <- ContT $ withForeignPtr (signatureFPtr sig)
@@ -242,6 +244,10 @@ instance Eq Signature where
242
244
newtype RecoverableSignature = RecoverableSignature { recoverableSignatureFPtr :: ForeignPtr Prim. RecSig65}
243
245
244
246
247
+ instance Show RecoverableSignature where
248
+ show recSig = " 0x" <> (B8. unpack . encodeBase16) (exportRecoverableSignature recSig)
249
+
250
+
245
251
instance Eq RecoverableSignature where
246
252
rs == rs' = unsafePerformIO . evalContT $ do
247
253
rsp <- ContT $ withForeignPtr (recoverableSignatureFPtr rs)
@@ -253,6 +259,10 @@ instance Eq RecoverableSignature where
253
259
newtype Tweak = Tweak { tweakFPtr :: ForeignPtr Prim. Tweak32}
254
260
255
261
262
+ instance Show Tweak where
263
+ show (Tweak fptr) = show (SecKey $ castForeignPtr fptr)
264
+
265
+
256
266
instance Eq Tweak where
257
267
sk == sk' = unsafePerformIO . evalContT $ do
258
268
skp <- ContT $ withForeignPtr (tweakFPtr sk)
@@ -410,6 +420,7 @@ exportRecoverableSignature RecoverableSignature{..} = unsafePerformIO . evalCont
410
420
recIdPtr <- malloc
411
421
_ret <- Prim. ecdsaRecoverableSignatureSerializeCompact ctx outBuf recIdPtr recSigPtr
412
422
recId <- peek recIdPtr
423
+ pokeByteOff outBuf 64 recId
413
424
unsafePackByteString (outBuf, 65 )
414
425
415
426
@@ -437,8 +448,7 @@ ecdsaSign (SecKey skFPtr) msgHash
437
448
(msgHashPtr, _) <- ContT (unsafeUseByteString msgHash)
438
449
lift $ do
439
450
sigBuf <- mallocBytes 64
440
- entropy <- mallocBytes 32
441
- ret <- Prim. ecdsaSign ctx sigBuf msgHashPtr skPtr Prim. nonceFunctionRfc6979 entropy
451
+ ret <- Prim. ecdsaSign ctx sigBuf msgHashPtr skPtr Prim. nonceFunctionDefault nullPtr
442
452
if isSuccess ret
443
453
then Just . Signature <$> newForeignPtr finalizerFree sigBuf
444
454
else free sigBuf $> Nothing
@@ -513,8 +523,8 @@ ecdh SecKey{..} PubKeyXY{..} = unsafePerformIO . evalContT $ do
513
523
514
524
515
525
-- -- | Add 'Tweak' to 'SecKey'.
516
- ecSecKeyTweakAdd :: SecKey -> Tweak -> Maybe SecKey
517
- ecSecKeyTweakAdd SecKey {.. } Tweak {.. } = unsafePerformIO . evalContT $ do
526
+ secKeyTweakAdd :: SecKey -> Tweak -> Maybe SecKey
527
+ secKeyTweakAdd SecKey {.. } Tweak {.. } = unsafePerformIO . evalContT $ do
518
528
skPtr <- ContT (withForeignPtr secKeyFPtr)
519
529
skOut <- lift (mallocBytes 32 )
520
530
lift (memcpy skOut skPtr 32 )
@@ -527,8 +537,8 @@ ecSecKeyTweakAdd SecKey{..} Tweak{..} = unsafePerformIO . evalContT $ do
527
537
528
538
529
539
-- | Multiply 'SecKey' by 'Tweak'.
530
- ecSecKeyTweakMul :: SecKey -> Tweak -> Maybe SecKey
531
- ecSecKeyTweakMul SecKey {.. } Tweak {.. } = unsafePerformIO . evalContT $ do
540
+ secKeyTweakMul :: SecKey -> Tweak -> Maybe SecKey
541
+ secKeyTweakMul SecKey {.. } Tweak {.. } = unsafePerformIO . evalContT $ do
532
542
skPtr <- ContT (withForeignPtr secKeyFPtr)
533
543
skOut <- lift (mallocBytes 32 )
534
544
lift (memcpy skOut skPtr 32 )
@@ -627,62 +637,6 @@ schnorrSign KeyPair{..} bs
627
637
else free sigBuf $> Nothing
628
638
629
639
630
- -- | Extra parameters object for alternative nonce generation
631
- data SchnorrExtra a = Storable a =>
632
- SchnorrExtra
633
- { schnorrExtraNonceFunHardened :: ByteString -> SecKey -> PubKeyXO -> ByteString -> a -> Maybe (SizedByteArray 32 ByteString )
634
- , schnorrExtraData :: a
635
- }
636
-
637
-
638
- -- | Compute a schnorr signature with an alternative scheme for generating nonces, it is not recommended you use this
639
- -- unless you know what you are doing. Instead, favor the usage of 'schnorrSign'
640
- schnorrSignCustom :: forall a . KeyPair -> ByteString -> SchnorrExtra a -> Maybe Signature
641
- schnorrSignCustom KeyPair {.. } msg SchnorrExtra {.. } = unsafePerformIO . evalContT $ do
642
- (msgPtr, msgLen) <- ContT (unsafeUseByteString msg)
643
- keyPairPtr <- ContT (withForeignPtr keyPairFPtr)
644
- lift $ do
645
- sigBuf <- mallocBytes 64
646
- -- convert fn into funptr
647
- funptr <- mkNonceFunHardened primFn
648
- -- allocate memory for extra data ptr
649
- dataptr <- malloc
650
- -- copy data to new pointer
651
- poke dataptr schnorrExtraData
652
- -- allocate extraparams structure
653
- extraPtr <- mallocBytes (4 + sizeOf funptr + sizeOf dataptr)
654
- -- fill magic
655
- pokeByteOff extraPtr 0 (0xDA :: Word8 )
656
- pokeByteOff extraPtr 1 (0x6F :: Word8 )
657
- pokeByteOff extraPtr 2 (0xB3 :: Word8 )
658
- pokeByteOff extraPtr 3 (0x8C :: Word8 )
659
- -- fill funptr
660
- pokeByteOff extraPtr 4 funptr
661
- -- fill dataptr
662
- pokeByteOff extraPtr (4 + sizeOf funptr) dataptr
663
- ret <- Prim. schnorrsigSignCustom ctx sigBuf msgPtr msgLen keyPairPtr extraPtr
664
- freeHaskellFunPtr funptr
665
- free dataptr
666
- free extraPtr
667
- if isSuccess ret
668
- then Just . Signature <$> newForeignPtr finalizerFree sigBuf
669
- else free sigBuf $> Nothing
670
- where
671
- primFn :: Storable a => Prim. NonceFunHardened a
672
- primFn outBuf msgPtr msgLen sk xopk algo algolen dataPtr = do
673
- msg <- unsafePackByteString (msgPtr, msgLen)
674
- sk <- SecKey <$> newForeignPtr_ (castPtr sk)
675
- xopk <- PubKeyXO <$> newForeignPtr_ (castPtr xopk)
676
- algo <- unsafePackByteString (algo, algolen)
677
- extra <- peek dataPtr
678
- case schnorrExtraNonceFunHardened msg sk xopk algo extra of
679
- Nothing -> pure 0
680
- Just bs -> evalContT $ do
681
- (hashPtr, _) <- ContT (unsafeUseByteString (unSizedByteArray bs))
682
- lift (memcpy outBuf hashPtr 32 )
683
- pure 1
684
-
685
-
686
640
-- | Verify the authenticity of a schnorr signature. @True@ means the 'Signature' is correct.
687
641
schnorrVerify :: PubKeyXO -> ByteString -> Signature -> Bool
688
642
schnorrVerify PubKeyXO {.. } bs Signature {.. } = unsafePerformIO . evalContT $ do
@@ -711,7 +665,7 @@ taggedSha256 tag msg = unsafePerformIO . evalContT $ do
711
665
-- | Combine a list of 'PubKeyXY's into a single 'PubKeyXY'. This will result in @Nothing@ if the group operation results
712
666
-- in the Point at Infinity
713
667
pubKeyCombine :: [PubKeyXY ] -> Maybe PubKeyXY
714
- pubKeyCombine keys = unsafePerformIO $ do
668
+ pubKeyCombine keys@ (_ : _) = unsafePerformIO $ do
715
669
let n = length keys
716
670
keysBuf <- mallocBytes (64 * n)
717
671
for_ (zip [0 .. ] keys) $ \ (i, PubKeyXY {.. }) ->
@@ -721,6 +675,7 @@ pubKeyCombine keys = unsafePerformIO $ do
721
675
if isSuccess ret
722
676
then Just . PubKeyXY <$> newForeignPtr finalizerFree outBuf
723
677
else free outBuf $> Nothing
678
+ pubKeyCombine [] = Nothing
724
679
725
680
726
681
-- | Negate a 'PubKeyXY'
0 commit comments