github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/crypto/crypto_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package crypto 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "encoding/hex" 23 "fmt" 24 "io/ioutil" 25 "math/big" 26 "os" 27 "testing" 28 "time" 29 30 "github.com/atheioschain/go-atheios/common" 31 "github.com/atheioschain/go-atheios/crypto/secp256k1" 32 ) 33 34 var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791" 35 var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032" 36 37 // These tests are sanity checks. 38 // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256 39 // and that the sha3 library uses keccak-f permutation. 40 func TestSha3(t *testing.T) { 41 msg := []byte("abc") 42 exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") 43 checkhash(t, "Sha3-256", func(in []byte) []byte { return Keccak256(in) }, msg, exp) 44 } 45 46 func TestSha3Hash(t *testing.T) { 47 msg := []byte("abc") 48 exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") 49 checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp) 50 } 51 52 func TestSha256(t *testing.T) { 53 msg := []byte("abc") 54 exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") 55 checkhash(t, "Sha256", Sha256, msg, exp) 56 } 57 58 func TestRipemd160(t *testing.T) { 59 msg := []byte("abc") 60 exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc") 61 checkhash(t, "Ripemd160", Ripemd160, msg, exp) 62 } 63 64 func BenchmarkSha3(b *testing.B) { 65 a := []byte("hello world") 66 amount := 1000000 67 start := time.Now() 68 for i := 0; i < amount; i++ { 69 Keccak256(a) 70 } 71 72 fmt.Println(amount, ":", time.Since(start)) 73 } 74 75 func TestSign(t *testing.T) { 76 key, _ := HexToECDSA(testPrivHex) 77 addr := common.HexToAddress(testAddrHex) 78 79 msg := Keccak256([]byte("foo")) 80 sig, err := Sign(msg, key) 81 if err != nil { 82 t.Errorf("Sign error: %s", err) 83 } 84 recoveredPub, err := Ecrecover(msg, sig) 85 if err != nil { 86 t.Errorf("ECRecover error: %s", err) 87 } 88 pubKey := ToECDSAPub(recoveredPub) 89 recoveredAddr := PubkeyToAddress(*pubKey) 90 if addr != recoveredAddr { 91 t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr) 92 } 93 94 // should be equal to SigToPub 95 recoveredPub2, err := SigToPub(msg, sig) 96 if err != nil { 97 t.Errorf("ECRecover error: %s", err) 98 } 99 recoveredAddr2 := PubkeyToAddress(*recoveredPub2) 100 if addr != recoveredAddr2 { 101 t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr2) 102 } 103 } 104 105 func TestInvalidSign(t *testing.T) { 106 if _, err := Sign(make([]byte, 1), nil); err == nil { 107 t.Errorf("expected sign with hash 1 byte to error") 108 } 109 if _, err := Sign(make([]byte, 33), nil); err == nil { 110 t.Errorf("expected sign with hash 33 byte to error") 111 } 112 } 113 114 func TestNewContractAddress(t *testing.T) { 115 key, _ := HexToECDSA(testPrivHex) 116 addr := common.HexToAddress(testAddrHex) 117 genAddr := PubkeyToAddress(key.PublicKey) 118 // sanity check before using addr to create contract address 119 checkAddr(t, genAddr, addr) 120 121 caddr0 := CreateAddress(addr, 0) 122 caddr1 := CreateAddress(addr, 1) 123 caddr2 := CreateAddress(addr, 2) 124 checkAddr(t, common.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0) 125 checkAddr(t, common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1) 126 checkAddr(t, common.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2) 127 } 128 129 func TestLoadECDSAFile(t *testing.T) { 130 keyBytes := common.FromHex(testPrivHex) 131 fileName0 := "test_key0" 132 fileName1 := "test_key1" 133 checkKey := func(k *ecdsa.PrivateKey) { 134 checkAddr(t, PubkeyToAddress(k.PublicKey), common.HexToAddress(testAddrHex)) 135 loadedKeyBytes := FromECDSA(k) 136 if !bytes.Equal(loadedKeyBytes, keyBytes) { 137 t.Fatalf("private key mismatch: want: %x have: %x", keyBytes, loadedKeyBytes) 138 } 139 } 140 141 ioutil.WriteFile(fileName0, []byte(testPrivHex), 0600) 142 defer os.Remove(fileName0) 143 144 key0, err := LoadECDSA(fileName0) 145 if err != nil { 146 t.Fatal(err) 147 } 148 checkKey(key0) 149 150 // again, this time with SaveECDSA instead of manual save: 151 err = SaveECDSA(fileName1, key0) 152 if err != nil { 153 t.Fatal(err) 154 } 155 defer os.Remove(fileName1) 156 157 key1, err := LoadECDSA(fileName1) 158 if err != nil { 159 t.Fatal(err) 160 } 161 checkKey(key1) 162 } 163 164 func TestValidateSignatureValues(t *testing.T) { 165 check := func(expected bool, v byte, r, s *big.Int) { 166 if ValidateSignatureValues(v, r, s, false) != expected { 167 t.Errorf("mismatch for v: %d r: %d s: %d want: %v", v, r, s, expected) 168 } 169 } 170 minusOne := big.NewInt(-1) 171 one := common.Big1 172 zero := common.Big0 173 secp256k1nMinus1 := new(big.Int).Sub(secp256k1.N, common.Big1) 174 175 // correct v,r,s 176 check(true, 0, one, one) 177 check(true, 1, one, one) 178 // incorrect v, correct r,s, 179 check(false, 2, one, one) 180 check(false, 3, one, one) 181 182 // incorrect v, combinations of incorrect/correct r,s at lower limit 183 check(false, 2, zero, zero) 184 check(false, 2, zero, one) 185 check(false, 2, one, zero) 186 check(false, 2, one, one) 187 188 // correct v for any combination of incorrect r,s 189 check(false, 0, zero, zero) 190 check(false, 0, zero, one) 191 check(false, 0, one, zero) 192 193 check(false, 1, zero, zero) 194 check(false, 1, zero, one) 195 check(false, 1, one, zero) 196 197 // correct sig with max r,s 198 check(true, 0, secp256k1nMinus1, secp256k1nMinus1) 199 // correct v, combinations of incorrect r,s at upper limit 200 check(false, 0, secp256k1.N, secp256k1nMinus1) 201 check(false, 0, secp256k1nMinus1, secp256k1.N) 202 check(false, 0, secp256k1.N, secp256k1.N) 203 204 // current callers ensures r,s cannot be negative, but let's test for that too 205 // as crypto package could be used stand-alone 206 check(false, 0, minusOne, one) 207 check(false, 0, one, minusOne) 208 } 209 210 func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) { 211 sum := f(msg) 212 if !bytes.Equal(exp, sum) { 213 t.Fatalf("hash %s mismatch: want: %x have: %x", name, exp, sum) 214 } 215 } 216 217 func checkAddr(t *testing.T, addr0, addr1 common.Address) { 218 if addr0 != addr1 { 219 t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1) 220 } 221 } 222 223 // test to help Python team with integration of libsecp256k1 224 // skip but keep it after they are done 225 func TestPythonIntegration(t *testing.T) { 226 kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032" 227 k0, _ := HexToECDSA(kh) 228 k1 := FromECDSA(k0) 229 230 msg0 := Keccak256([]byte("foo")) 231 sig0, _ := secp256k1.Sign(msg0, k1) 232 233 msg1 := common.FromHex("00000000000000000000000000000000") 234 sig1, _ := secp256k1.Sign(msg0, k1) 235 236 fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg0, k1, sig0) 237 fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg1, k1, sig1) 238 }