2
2
3
3
module Bitcoin.Crypto.SignatureSpec (spec ) where
4
4
5
- import Bitcoin (getCompactSig )
5
+ import Bitcoin (exportSignatureCompact )
6
6
import Bitcoin.Address (
7
7
Address (WitnessPubKeyAddress ),
8
8
pubKeyWitnessAddr ,
9
9
)
10
10
import Bitcoin.Constants (btc )
11
11
import Bitcoin.Crypto (
12
12
SecKey ,
13
- Sig ,
13
+ Signature ,
14
14
decodeStrictSig ,
15
15
derivePubKey ,
16
- exportCompactSig ,
17
- exportSig ,
16
+ ecdsaSign ,
17
+ exportSignatureCompact ,
18
+ exportSignatureDer ,
18
19
getSig ,
19
- importSig ,
20
+ importSecKey ,
21
+ importSignatureDer ,
20
22
isCanonicalHalfOrder ,
21
23
putSig ,
22
- secKey ,
23
24
sha256 ,
24
25
signHash ,
25
- signMsg ,
26
26
verifyHashSig ,
27
27
)
28
28
import Bitcoin.Keys (PubKeyI , derivePubKeyI , wrapSecKey )
@@ -53,7 +53,7 @@ import Data.ByteString (ByteString)
53
53
import qualified Data.ByteString as BS
54
54
import Data.Map.Strict (Map )
55
55
import qualified Data.Map.Strict as Map
56
- import Data.Maybe (fromJust )
56
+ import Data.Maybe (fromJust , fromMaybe )
57
57
import Data.String.Conversions (cs )
58
58
import Data.Text (Text )
59
59
import Test.HUnit (
@@ -81,10 +81,10 @@ spec = do
81
81
testIsCanonical . lst3
82
82
prop " decodeStrictSig . exportSig identity" $
83
83
forAll arbitrarySignature $
84
- (\ s -> decodeStrictSig (exportSig s) == Just s) . lst3
84
+ (\ s -> decodeStrictSig (exportSignatureDer s) == Just s) . lst3
85
85
prop " importSig . exportSig identity" $
86
86
forAll arbitrarySignature $
87
- (\ s -> importSig (exportSig s) == Just s) . lst3
87
+ (\ s -> importSignatureDer (exportSignatureDer s) == Just s) . lst3
88
88
prop " getSig . putSig identity" $
89
89
forAll arbitrarySignature $ \ (_, _, s) ->
90
90
(U. runGet getSig . runPut . putSig) s == Right s
@@ -105,7 +105,7 @@ spec = do
105
105
106
106
-- github.com/bitcoin/bitcoin/blob/master/src/script.cpp
107
107
-- from function IsCanonicalSignature
108
- testIsCanonical :: Sig -> Bool
108
+ testIsCanonical :: Signature -> Bool
109
109
testIsCanonical sig =
110
110
not $
111
111
-- Non-canonical signature: too short
@@ -156,7 +156,7 @@ testIsCanonical sig =
156
156
&& not (testBit (BS. index s (fromIntegral rlen + 7 )) 7 )
157
157
)
158
158
where
159
- s = exportSig sig
159
+ s = exportSignatureDer sig
160
160
len = fromIntegral $ BS. length s
161
161
rlen = BS. index s 3
162
162
slen = BS. index s (fromIntegral rlen + 5 )
@@ -175,10 +175,13 @@ data ValidImpl
175
175
implSig :: Text
176
176
implSig =
177
177
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"
182
185
183
186
184
187
-- We have test vectors for these cases
@@ -201,7 +204,7 @@ validImplMap =
201
204
202
205
203
206
getImpl :: Maybe ValidImpl
204
- getImpl = implSig `Map.lookup` validImplMap
207
+ getImpl = pure ImplCore
205
208
206
209
207
210
rfc6979files :: ValidImpl -> (FilePath , FilePath )
@@ -223,32 +226,32 @@ checkDistSig go =
223
226
-- github.com/trezor/python-ecdsa/blob/master/ecdsa/test_pyecdsa.py
224
227
225
228
toVector :: (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)
227
230
228
231
229
232
testRFC6979Vector :: (SecKey , ByteString , Text ) -> Assertion
230
233
testRFC6979Vector (prv, m, res) = do
231
- assertEqual " RFC 6979 Vector" res $ encodeHex . getCompactSig $ exportCompactSig s
234
+ assertEqual " RFC 6979 Vector" res $ encodeHex . exportSignatureCompact $ s
232
235
assertBool " Signature is valid" $ verifyHashSig h s (derivePubKey prv)
233
236
assertBool " Signature is canonical" $ testIsCanonical s
234
237
assertBool " Signature is normalized" $ isCanonicalHalfOrder s
235
238
where
236
239
h = sha256 m
237
- s = signHash prv h
240
+ s = fromMaybe ( error " Signing Failed " ) $ signHash prv h
238
241
239
242
240
243
-- Test vectors from:
241
244
-- https://crypto.stackexchange.com/questions/20838/request-for-data-to-test-deterministic-ecdsa-signature-algorithm-for-secp256k1
242
245
243
246
testRFC6979DERVector :: (SecKey , ByteString , Text ) -> Assertion
244
247
testRFC6979DERVector (prv, m, res) = do
245
- assertEqual " RFC 6979 DER Vector" res (encodeHex $ exportSig s)
248
+ assertEqual " RFC 6979 DER Vector" res (encodeHex $ exportSignatureDer s)
246
249
assertBool " DER Signature is valid" $ verifyHashSig h s (derivePubKey prv)
247
250
assertBool " DER Signature is canonical" $ testIsCanonical s
248
251
assertBool " DER Signature is normalized" $ isCanonicalHalfOrder s
249
252
where
250
253
h = sha256 m
251
- s = signHash prv h
254
+ s = fromMaybe ( error " Signing Failed " ) $ signHash prv h
252
255
253
256
254
257
-- Reproduce the P2WPKH example from BIP 143
@@ -497,7 +500,7 @@ testBip143p2shp2wpkhMulsig =
497
500
498
501
499
502
secHexKey :: Text -> Maybe SecKey
500
- secHexKey = decodeHex >=> secKey
503
+ secHexKey = decodeHex >=> importSecKey
501
504
502
505
503
506
toPubKey :: SecKey -> PubKeyI
0 commit comments