22
33module Bitcoin.Crypto.SignatureSpec (spec ) where
44
5- import Bitcoin (getCompactSig )
5+ import Bitcoin (exportSignatureCompact )
66import Bitcoin.Address (
77 Address (WitnessPubKeyAddress ),
88 pubKeyWitnessAddr ,
99 )
1010import Bitcoin.Constants (btc )
1111import Bitcoin.Crypto (
1212 SecKey ,
13- Sig ,
13+ Signature ,
1414 decodeStrictSig ,
1515 derivePubKey ,
16- exportCompactSig ,
17- exportSig ,
16+ ecdsaSign ,
17+ exportSignatureCompact ,
18+ exportSignatureDer ,
1819 getSig ,
19- importSig ,
20+ importSecKey ,
21+ importSignatureDer ,
2022 isCanonicalHalfOrder ,
2123 putSig ,
22- secKey ,
2324 sha256 ,
2425 signHash ,
25- signMsg ,
2626 verifyHashSig ,
2727 )
2828import Bitcoin.Keys (PubKeyI , derivePubKeyI , wrapSecKey )
@@ -53,7 +53,7 @@ import Data.ByteString (ByteString)
5353import qualified Data.ByteString as BS
5454import Data.Map.Strict (Map )
5555import qualified Data.Map.Strict as Map
56- import Data.Maybe (fromJust )
56+ import Data.Maybe (fromJust , fromMaybe )
5757import Data.String.Conversions (cs )
5858import Data.Text (Text )
5959import Test.HUnit (
@@ -81,10 +81,10 @@ spec = do
8181 testIsCanonical . lst3
8282 prop " decodeStrictSig . exportSig identity" $
8383 forAll arbitrarySignature $
84- (\ s -> decodeStrictSig (exportSig s) == Just s) . lst3
84+ (\ s -> decodeStrictSig (exportSignatureDer s) == Just s) . lst3
8585 prop " importSig . exportSig identity" $
8686 forAll arbitrarySignature $
87- (\ s -> importSig (exportSig s) == Just s) . lst3
87+ (\ s -> importSignatureDer (exportSignatureDer s) == Just s) . lst3
8888 prop " getSig . putSig identity" $
8989 forAll arbitrarySignature $ \ (_, _, s) ->
9090 (U. runGet getSig . runPut . putSig) s == Right s
@@ -105,7 +105,7 @@ spec = do
105105
106106-- github.com/bitcoin/bitcoin/blob/master/src/script.cpp
107107-- from function IsCanonicalSignature
108- testIsCanonical :: Sig -> Bool
108+ testIsCanonical :: Signature -> Bool
109109testIsCanonical sig =
110110 not $
111111 -- Non-canonical signature: too short
@@ -156,7 +156,7 @@ testIsCanonical sig =
156156 && not (testBit (BS. index s (fromIntegral rlen + 7 )) 7 )
157157 )
158158 where
159- s = exportSig sig
159+ s = exportSignatureDer sig
160160 len = fromIntegral $ BS. length s
161161 rlen = BS. index s 3
162162 slen = BS. index s (fromIntegral rlen + 5 )
@@ -175,10 +175,13 @@ data ValidImpl
175175implSig :: Text
176176implSig =
177177 encodeHex $
178- exportSig $
179- signMsg
180- " 0000000000000000000000000000000000000000000000000000000000000001"
181- " 0000000000000000000000000000000000000000000000000000000000000000"
178+ exportSignatureDer $
179+ fromMaybe (error " Signing Failed" ) $
180+ ecdsaSign key " 0000000000000000000000000000000000000000000000000000000000000000"
181+ where
182+ key =
183+ fromMaybe (error " Invalid SecKey" ) . (importSecKey <=< decodeHex) $
184+ " 0000000000000000000000000000000000000000000000000000000000000001"
182185
183186
184187-- We have test vectors for these cases
@@ -201,7 +204,7 @@ validImplMap =
201204
202205
203206getImpl :: Maybe ValidImpl
204- getImpl = implSig `Map.lookup` validImplMap
207+ getImpl = pure ImplCore
205208
206209
207210rfc6979files :: ValidImpl -> (FilePath , FilePath )
@@ -223,32 +226,32 @@ checkDistSig go =
223226-- github.com/trezor/python-ecdsa/blob/master/ecdsa/test_pyecdsa.py
224227
225228toVector :: (Text , Text , Text ) -> (SecKey , ByteString , Text )
226- toVector (prv, m, res) = (fromJust $ (secKey <=< decodeHex) prv, cs m, res)
229+ toVector (prv, m, res) = (fromJust $ (importSecKey <=< decodeHex) prv, cs m, res)
227230
228231
229232testRFC6979Vector :: (SecKey , ByteString , Text ) -> Assertion
230233testRFC6979Vector (prv, m, res) = do
231- assertEqual " RFC 6979 Vector" res $ encodeHex . getCompactSig $ exportCompactSig s
234+ assertEqual " RFC 6979 Vector" res $ encodeHex . exportSignatureCompact $ s
232235 assertBool " Signature is valid" $ verifyHashSig h s (derivePubKey prv)
233236 assertBool " Signature is canonical" $ testIsCanonical s
234237 assertBool " Signature is normalized" $ isCanonicalHalfOrder s
235238 where
236239 h = sha256 m
237- s = signHash prv h
240+ s = fromMaybe ( error " Signing Failed " ) $ signHash prv h
238241
239242
240243-- Test vectors from:
241244-- https://crypto.stackexchange.com/questions/20838/request-for-data-to-test-deterministic-ecdsa-signature-algorithm-for-secp256k1
242245
243246testRFC6979DERVector :: (SecKey , ByteString , Text ) -> Assertion
244247testRFC6979DERVector (prv, m, res) = do
245- assertEqual " RFC 6979 DER Vector" res (encodeHex $ exportSig s)
248+ assertEqual " RFC 6979 DER Vector" res (encodeHex $ exportSignatureDer s)
246249 assertBool " DER Signature is valid" $ verifyHashSig h s (derivePubKey prv)
247250 assertBool " DER Signature is canonical" $ testIsCanonical s
248251 assertBool " DER Signature is normalized" $ isCanonicalHalfOrder s
249252 where
250253 h = sha256 m
251- s = signHash prv h
254+ s = fromMaybe ( error " Signing Failed " ) $ signHash prv h
252255
253256
254257-- Reproduce the P2WPKH example from BIP 143
@@ -497,7 +500,7 @@ testBip143p2shp2wpkhMulsig =
497500
498501
499502secHexKey :: Text -> Maybe SecKey
500- secHexKey = decodeHex >=> secKey
503+ secHexKey = decodeHex >=> importSecKey
501504
502505
503506toPubKey :: SecKey -> PubKeyI
0 commit comments