@@ -18,19 +18,25 @@ package crypto
1818
1919import (
2020 "bytes"
21+ "crypto/ecdsa"
2122 "encoding/hex"
2223 "fmt"
24+ "io/ioutil"
25+ "math/big"
26+ "os"
2327 "testing"
2428 "time"
2529
2630 "github.com/ethereum/go-ethereum/common"
2731 "github.com/ethereum/go-ethereum/crypto/secp256k1"
2832)
2933
34+ var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
35+ var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
36+
3037// These tests are sanity checks.
3138// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
3239// and that the sha3 library uses keccak-f permutation.
33-
3440func TestSha3 (t * testing.T ) {
3541 msg := []byte ("abc" )
3642 exp , _ := hex .DecodeString ("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45" )
@@ -55,13 +61,6 @@ func TestRipemd160(t *testing.T) {
5561 checkhash (t , "Ripemd160" , Ripemd160 , msg , exp )
5662}
5763
58- func checkhash (t * testing.T , name string , f func ([]byte ) []byte , msg , exp []byte ) {
59- sum := f (msg )
60- if bytes .Compare (exp , sum ) != 0 {
61- t .Errorf ("hash %s returned wrong result.\n got: %x\n want: %x" , name , sum , exp )
62- }
63- }
64-
6564func BenchmarkSha3 (b * testing.B ) {
6665 a := []byte ("hello world" )
6766 amount := 1000000
@@ -74,13 +73,41 @@ func BenchmarkSha3(b *testing.B) {
7473}
7574
7675func Test0Key (t * testing.T ) {
77- t .Skip ()
78- key := common .Hex2Bytes ("1111111111111111111111111111111111111111111111111111111111111111" )
76+ key := common .Hex2Bytes ("0000000000000000000000000000000000000000000000000000000000000000" )
77+ _ , err := secp256k1 .GeneratePubKey (key )
78+ if err == nil {
79+ t .Errorf ("expected error due to zero privkey" )
80+ }
81+ }
82+
83+ func TestSign (t * testing.T ) {
84+ key , _ := HexToECDSA (testPrivHex )
85+ addr := common .HexToAddress (testAddrHex )
86+
87+ msg := Sha3 ([]byte ("foo" ))
88+ sig , err := Sign (msg , key )
89+ if err != nil {
90+ t .Errorf ("Sign error: %s" , err )
91+ }
92+ recoveredPub , err := Ecrecover (msg , sig )
93+ if err != nil {
94+ t .Errorf ("ECRecover error: %s" , err )
95+ }
96+ recoveredAddr := PubkeyToAddress (* ToECDSAPub (recoveredPub ))
97+ if addr != recoveredAddr {
98+ t .Errorf ("Address mismatch: want: %x have: %x" , addr , recoveredAddr )
99+ }
100+
101+ // should be equal to SigToPub
102+ recoveredPub2 , err := SigToPub (msg , sig )
103+ if err != nil {
104+ t .Errorf ("ECRecover error: %s" , err )
105+ }
106+ recoveredAddr2 := PubkeyToAddress (* recoveredPub2 )
107+ if addr != recoveredAddr2 {
108+ t .Errorf ("Address mismatch: want: %x have: %x" , addr , recoveredAddr2 )
109+ }
79110
80- p , err := secp256k1 .GeneratePubKey (key )
81- addr := Sha3 (p [1 :])[12 :]
82- fmt .Printf ("%x\n " , p )
83- fmt .Printf ("%v %x\n " , err , addr )
84111}
85112
86113func TestInvalidSign (t * testing.T ) {
@@ -94,3 +121,129 @@ func TestInvalidSign(t *testing.T) {
94121 t .Errorf ("expected sign with hash 33 byte to error" )
95122 }
96123}
124+
125+ func TestNewContractAddress (t * testing.T ) {
126+ key , _ := HexToECDSA (testPrivHex )
127+ addr := common .HexToAddress (testAddrHex )
128+ genAddr := PubkeyToAddress (key .PublicKey )
129+ // sanity check before using addr to create contract address
130+ checkAddr (t , genAddr , addr )
131+
132+ caddr0 := CreateAddress (addr , 0 )
133+ caddr1 := CreateAddress (addr , 1 )
134+ caddr2 := CreateAddress (addr , 2 )
135+ checkAddr (t , common .HexToAddress ("333c3310824b7c685133f2bedb2ca4b8b4df633d" ), caddr0 )
136+ checkAddr (t , common .HexToAddress ("8bda78331c916a08481428e4b07c96d3e916d165" ), caddr1 )
137+ checkAddr (t , common .HexToAddress ("c9ddedf451bc62ce88bf9292afb13df35b670699" ), caddr2 )
138+ }
139+
140+ func TestLoadECDSAFile (t * testing.T ) {
141+ keyBytes := common .FromHex (testPrivHex )
142+ fileName0 := "test_key0"
143+ fileName1 := "test_key1"
144+ checkKey := func (k * ecdsa.PrivateKey ) {
145+ checkAddr (t , PubkeyToAddress (k .PublicKey ), common .HexToAddress (testAddrHex ))
146+ loadedKeyBytes := FromECDSA (k )
147+ if ! bytes .Equal (loadedKeyBytes , keyBytes ) {
148+ t .Fatalf ("private key mismatch: want: %x have: %x" , keyBytes , loadedKeyBytes )
149+ }
150+ }
151+
152+ ioutil .WriteFile (fileName0 , []byte (testPrivHex ), 0600 )
153+ defer os .Remove (fileName0 )
154+
155+ key0 , err := LoadECDSA (fileName0 )
156+ if err != nil {
157+ t .Fatal (err )
158+ }
159+ checkKey (key0 )
160+
161+ // again, this time with SaveECDSA instead of manual save:
162+ err = SaveECDSA (fileName1 , key0 )
163+ if err != nil {
164+ t .Fatal (err )
165+ }
166+ defer os .Remove (fileName1 )
167+
168+ key1 , err := LoadECDSA (fileName1 )
169+ if err != nil {
170+ t .Fatal (err )
171+ }
172+ checkKey (key1 )
173+ }
174+
175+ func TestValidateSignatureValues (t * testing.T ) {
176+ check := func (expected bool , v byte , r , s * big.Int ) {
177+ if ValidateSignatureValues (v , r , s ) != expected {
178+ t .Errorf ("mismatch for v: %d r: %d s: %d want: %v" , v , r , s , expected )
179+ }
180+ }
181+ minusOne := big .NewInt (- 1 )
182+ one := common .Big1
183+ zero := common .Big0
184+ secp256k1nMinus1 := new (big.Int ).Sub (secp256k1n , common .Big1 )
185+
186+ // correct v,r,s
187+ check (true , 27 , one , one )
188+ check (true , 28 , one , one )
189+ // incorrect v, correct r,s,
190+ check (false , 30 , one , one )
191+ check (false , 26 , one , one )
192+
193+ // incorrect v, combinations of incorrect/correct r,s at lower limit
194+ check (false , 0 , zero , zero )
195+ check (false , 0 , zero , one )
196+ check (false , 0 , one , zero )
197+ check (false , 0 , one , one )
198+
199+ // correct v for any combination of incorrect r,s
200+ check (false , 27 , zero , zero )
201+ check (false , 27 , zero , one )
202+ check (false , 27 , one , zero )
203+
204+ check (false , 28 , zero , zero )
205+ check (false , 28 , zero , one )
206+ check (false , 28 , one , zero )
207+
208+ // correct sig with max r,s
209+ check (true , 27 , secp256k1nMinus1 , secp256k1nMinus1 )
210+ // correct v, combinations of incorrect r,s at upper limit
211+ check (false , 27 , secp256k1n , secp256k1nMinus1 )
212+ check (false , 27 , secp256k1nMinus1 , secp256k1n )
213+ check (false , 27 , secp256k1n , secp256k1n )
214+
215+ // current callers ensures r,s cannot be negative, but let's test for that too
216+ // as crypto package could be used stand-alone
217+ check (false , 27 , minusOne , one )
218+ check (false , 27 , one , minusOne )
219+ }
220+
221+ func checkhash (t * testing.T , name string , f func ([]byte ) []byte , msg , exp []byte ) {
222+ sum := f (msg )
223+ if bytes .Compare (exp , sum ) != 0 {
224+ t .Fatalf ("hash %s mismatch: want: %x have: %x" , name , exp , sum )
225+ }
226+ }
227+
228+ func checkAddr (t * testing.T , addr0 , addr1 common.Address ) {
229+ if addr0 != addr1 {
230+ t .Fatalf ("address mismatch: want: %x have: %x" , addr0 , addr1 )
231+ }
232+ }
233+
234+ // test to help Python team with integration of libsecp256k1
235+ // skip but keep it after they are done
236+ func TestPythonIntegration (t * testing.T ) {
237+ kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
238+ k0 , _ := HexToECDSA (kh )
239+ k1 := FromECDSA (k0 )
240+
241+ msg0 := Sha3 ([]byte ("foo" ))
242+ sig0 , _ := secp256k1 .Sign (msg0 , k1 )
243+
244+ msg1 := common .FromHex ("00000000000000000000000000000000" )
245+ sig1 , _ := secp256k1 .Sign (msg0 , k1 )
246+
247+ fmt .Printf ("msg: %x, privkey: %x sig: %x\n " , msg0 , k1 , sig0 )
248+ fmt .Printf ("msg: %x, privkey: %x sig: %x\n " , msg1 , k1 , sig1 )
249+ }
0 commit comments