github.com/aquanetwork/aquachain@v1.7.8/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 29 "gitlab.com/aquachain/aquachain/common" 30 ) 31 32 var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791" 33 var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032" 34 35 // These tests are sanity checks. 36 // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256 37 // and that the sha3 library uses keccak-f permutation. 38 func TestKeccak256Hash(t *testing.T) { 39 msg := []byte("abc") 40 exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") 41 checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp) 42 } 43 44 func TestToECDSAErrors(t *testing.T) { 45 if _, err := HexToECDSA("0000000000000000000000000000000000000000000000000000000000000000"); err == nil { 46 t.Fatal("HexToECDSA should've returned error") 47 } 48 if _, err := HexToECDSA("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); err == nil { 49 t.Fatal("HexToECDSA should've returned error") 50 } 51 } 52 53 func TestArgon2id(t *testing.T) { 54 a := []byte("aquachain") 55 fmt.Printf("\nArgonated: 0x%x\n", Argon2idA(a)) 56 hex2hash := common.HexToHash(fmt.Sprintf("0x%x", Argon2idA(a))) 57 bytes2hash := common.BytesToHash(Argon2idA(a)) 58 if hex2hash.Big().Cmp(bytes2hash.Big()) != 0 { 59 t.Errorf("Unequal") 60 } 61 if !bytes.Equal(hex2hash.Bytes(), bytes2hash.Bytes()) { 62 t.Errorf("Unequal") 63 } 64 fmt.Printf("Argonated: 0x%x\n", Argon2idA(Argon2idA(a))) 65 fmt.Printf("Argonated: 0x%x\n", Argon2idA(Argon2idA(Argon2idA(a)))) 66 fmt.Printf("Argonated: 0x%x\n", Argon2idA(Argon2idA(Argon2idA(Argon2idA(a))))) 67 fmt.Printf("Argonated: 0x%x\n", Argon2idA(Argon2idA(Argon2idA(Argon2idA(Argon2idA(a)))))) 68 fmt.Printf("Argonated: 0x%x\n", Argon2idB(a)) 69 fmt.Printf("Argonated: 0x%x\n", Argon2idC(a)) 70 } 71 func BenchmarkSha3(b *testing.B) { 72 a := []byte("hello world") 73 for i := 0; i < b.N; i++ { 74 Keccak256(a) 75 } 76 } 77 func BenchmarkArgon2idA(b *testing.B) { 78 a := []byte("hello world") 79 for i := 0; i < b.N; i++ { 80 Argon2idA(a) 81 } 82 } 83 func BenchmarkArgon2idB(b *testing.B) { 84 a := []byte("hello world") 85 for i := 0; i < b.N; i++ { 86 Argon2idB(a) 87 } 88 } 89 func BenchmarkArgon2idC(b *testing.B) { 90 a := []byte("hello world") 91 for i := 0; i < b.N; i++ { 92 Argon2idC(a) 93 } 94 } 95 96 func TestSign(t *testing.T) { 97 key, _ := HexToECDSA(testPrivHex) 98 addr := common.HexToAddress(testAddrHex) 99 100 msg := Keccak256([]byte("foo")) 101 sig, err := Sign(msg, key) 102 if err != nil { 103 t.Errorf("Sign error: %s", err) 104 } 105 recoveredPub, err := Ecrecover(msg, sig) 106 if err != nil { 107 t.Errorf("ECRecover error: %s", err) 108 } 109 pubKey := ToECDSAPub(recoveredPub) 110 recoveredAddr := PubkeyToAddress(*pubKey) 111 if addr != recoveredAddr { 112 t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr) 113 } 114 115 // should be equal to SigToPub 116 recoveredPub2, err := SigToPub(msg, sig) 117 if err != nil { 118 t.Errorf("ECRecover error: %s", err) 119 } 120 recoveredAddr2 := PubkeyToAddress(*recoveredPub2) 121 if addr != recoveredAddr2 { 122 t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr2) 123 } 124 } 125 126 func TestInvalidSign(t *testing.T) { 127 if _, err := Sign(make([]byte, 1), nil); err == nil { 128 t.Errorf("expected sign with hash 1 byte to error") 129 } 130 if _, err := Sign(make([]byte, 33), nil); err == nil { 131 t.Errorf("expected sign with hash 33 byte to error") 132 } 133 } 134 135 func TestNewContractAddress(t *testing.T) { 136 key, _ := HexToECDSA(testPrivHex) 137 addr := common.HexToAddress(testAddrHex) 138 genAddr := PubkeyToAddress(key.PublicKey) 139 // sanity check before using addr to create contract address 140 checkAddr(t, genAddr, addr) 141 142 caddr0 := CreateAddress(addr, 0) 143 caddr1 := CreateAddress(addr, 1) 144 caddr2 := CreateAddress(addr, 2) 145 checkAddr(t, common.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0) 146 checkAddr(t, common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1) 147 checkAddr(t, common.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2) 148 } 149 150 func TestLoadECDSAFile(t *testing.T) { 151 keyBytes := common.FromHex(testPrivHex) 152 fileName0 := "test_key0" 153 fileName1 := "test_key1" 154 checkKey := func(k *ecdsa.PrivateKey) { 155 checkAddr(t, PubkeyToAddress(k.PublicKey), common.HexToAddress(testAddrHex)) 156 loadedKeyBytes := FromECDSA(k) 157 if !bytes.Equal(loadedKeyBytes, keyBytes) { 158 t.Fatalf("private key mismatch: want: %x have: %x", keyBytes, loadedKeyBytes) 159 } 160 } 161 162 ioutil.WriteFile(fileName0, []byte(testPrivHex), 0600) 163 defer os.Remove(fileName0) 164 165 key0, err := LoadECDSA(fileName0) 166 if err != nil { 167 t.Fatal(err) 168 } 169 checkKey(key0) 170 171 // again, this time with SaveECDSA instead of manual save: 172 err = SaveECDSA(fileName1, key0) 173 if err != nil { 174 t.Fatal(err) 175 } 176 defer os.Remove(fileName1) 177 178 key1, err := LoadECDSA(fileName1) 179 if err != nil { 180 t.Fatal(err) 181 } 182 checkKey(key1) 183 } 184 185 func TestValidateSignatureValues(t *testing.T) { 186 check := func(expected bool, v byte, r, s *big.Int) { 187 if ValidateSignatureValues(v, r, s, false) != expected { 188 t.Errorf("mismatch for v: %d r: %d s: %d want: %v", v, r, s, expected) 189 } 190 } 191 minusOne := big.NewInt(-1) 192 one := common.Big1 193 zero := common.Big0 194 secp256k1nMinus1 := new(big.Int).Sub(secp256k1_N, common.Big1) 195 196 // correct v,r,s 197 check(true, 0, one, one) 198 check(true, 1, one, one) 199 // incorrect v, correct r,s, 200 check(false, 2, one, one) 201 check(false, 3, one, one) 202 203 // incorrect v, combinations of incorrect/correct r,s at lower limit 204 check(false, 2, zero, zero) 205 check(false, 2, zero, one) 206 check(false, 2, one, zero) 207 check(false, 2, one, one) 208 209 // correct v for any combination of incorrect r,s 210 check(false, 0, zero, zero) 211 check(false, 0, zero, one) 212 check(false, 0, one, zero) 213 214 check(false, 1, zero, zero) 215 check(false, 1, zero, one) 216 check(false, 1, one, zero) 217 218 // correct sig with max r,s 219 check(true, 0, secp256k1nMinus1, secp256k1nMinus1) 220 // correct v, combinations of incorrect r,s at upper limit 221 check(false, 0, secp256k1_N, secp256k1nMinus1) 222 check(false, 0, secp256k1nMinus1, secp256k1_N) 223 check(false, 0, secp256k1_N, secp256k1_N) 224 225 // current callers ensures r,s cannot be negative, but let's test for that too 226 // as crypto package could be used stand-alone 227 check(false, 0, minusOne, one) 228 check(false, 0, one, minusOne) 229 } 230 231 func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) { 232 sum := f(msg) 233 if !bytes.Equal(exp, sum) { 234 t.Fatalf("hash %s mismatch: want: %x have: %x", name, exp, sum) 235 } 236 } 237 238 func checkAddr(t *testing.T, addr0, addr1 common.Address) { 239 if addr0 != addr1 { 240 t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1) 241 } 242 } 243 244 // test to help Python team with integration of libsecp256k1 245 // skip but keep it after they are done 246 func TestPythonIntegration(t *testing.T) { 247 kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032" 248 k0, _ := HexToECDSA(kh) 249 250 msg0 := Keccak256([]byte("foo")) 251 sig0, _ := Sign(msg0, k0) 252 253 msg1 := common.FromHex("00000000000000000000000000000000") 254 sig1, _ := Sign(msg0, k0) 255 256 t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0) 257 t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1) 258 }