2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-10-31 06:37:43 -05:00
|
|
|
package crypto
|
2014-09-07 03:07:51 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-29 12:35:33 -05:00
|
|
|
"encoding/hex"
|
2015-01-12 03:19:27 -06:00
|
|
|
"fmt"
|
2014-09-07 03:07:51 -05:00
|
|
|
"testing"
|
2015-01-12 03:19:27 -06:00
|
|
|
"time"
|
2015-01-19 04:22:56 -06:00
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-16 19:32:35 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2014-09-07 03:07:51 -05:00
|
|
|
)
|
|
|
|
|
2014-10-29 12:35:33 -05:00
|
|
|
// These tests are sanity checks.
|
|
|
|
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
|
|
|
|
// and that the sha3 library uses keccak-f permutation.
|
|
|
|
|
2014-09-07 03:07:51 -05:00
|
|
|
func TestSha3(t *testing.T) {
|
2014-10-29 12:35:33 -05:00
|
|
|
msg := []byte("abc")
|
|
|
|
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
2015-01-27 07:29:33 -06:00
|
|
|
checkhash(t, "Sha3-256", func(in []byte) []byte { return Sha3(in) }, msg, exp)
|
2014-10-29 12:35:33 -05:00
|
|
|
}
|
|
|
|
|
2015-03-16 19:32:35 -05:00
|
|
|
func TestSha3Hash(t *testing.T) {
|
|
|
|
msg := []byte("abc")
|
|
|
|
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
|
|
|
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Sha3Hash(in); return h[:] }, msg, exp)
|
|
|
|
}
|
|
|
|
|
2014-10-29 12:35:33 -05:00
|
|
|
func TestSha256(t *testing.T) {
|
|
|
|
msg := []byte("abc")
|
|
|
|
exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
|
|
|
|
checkhash(t, "Sha256", Sha256, msg, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRipemd160(t *testing.T) {
|
|
|
|
msg := []byte("abc")
|
|
|
|
exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")
|
|
|
|
checkhash(t, "Ripemd160", Ripemd160, msg, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
|
|
|
|
sum := f(msg)
|
|
|
|
if bytes.Compare(exp, sum) != 0 {
|
|
|
|
t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp)
|
2014-09-07 03:07:51 -05:00
|
|
|
}
|
|
|
|
}
|
2015-01-12 03:19:27 -06:00
|
|
|
|
|
|
|
func BenchmarkSha3(b *testing.B) {
|
|
|
|
a := []byte("hello world")
|
|
|
|
amount := 1000000
|
|
|
|
start := time.Now()
|
|
|
|
for i := 0; i < amount; i++ {
|
|
|
|
Sha3(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(amount, ":", time.Since(start))
|
|
|
|
}
|
2015-01-19 04:22:56 -06:00
|
|
|
|
|
|
|
func Test0Key(t *testing.T) {
|
2015-01-22 11:12:05 -06:00
|
|
|
t.Skip()
|
2015-03-16 05:27:38 -05:00
|
|
|
key := common.Hex2Bytes("1111111111111111111111111111111111111111111111111111111111111111")
|
2015-01-19 04:22:56 -06:00
|
|
|
|
|
|
|
p, err := secp256k1.GeneratePubKey(key)
|
|
|
|
addr := Sha3(p[1:])[12:]
|
|
|
|
fmt.Printf("%x\n", p)
|
|
|
|
fmt.Printf("%v %x\n", err, addr)
|
|
|
|
}
|
2015-01-22 11:12:05 -06:00
|
|
|
|
|
|
|
func TestInvalidSign(t *testing.T) {
|
|
|
|
_, err := Sign(make([]byte, 1), nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected sign with hash 1 byte to error")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = Sign(make([]byte, 33), nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected sign with hash 33 byte to error")
|
|
|
|
}
|
|
|
|
}
|