aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/crypto_test.go
blob: 63a9c3f5e969adb14a7aeb53f59472a2e3d2ac86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package crypto

import (
    "bytes"
    "encoding/hex"
    "fmt"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto/secp256k1"
)

// 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.

func TestSha3(t *testing.T) {
    msg := []byte("abc")
    exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
    checkhash(t, "Sha3-256", func(in []byte) []byte { return Sha3(in) }, msg, exp)
}

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)
}

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)
    }
}

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))
}

func Test0Key(t *testing.T) {
    t.Skip()
    key := common.Hex2Bytes("1111111111111111111111111111111111111111111111111111111111111111")

    p, err := secp256k1.GeneratePubKey(key)
    addr := Sha3(p[1:])[12:]
    fmt.Printf("%x\n", p)
    fmt.Printf("%v %x\n", err, addr)
}

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")
    }
}