-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha256x8_test.go
150 lines (131 loc) · 3.26 KB
/
sha256x8_test.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package sha256x8_test
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"hash"
"math/rand"
"testing"
"github.com/bwesterb/go-sha256x8"
)
func TestEmptyDigest(t *testing.T) {
var d sha256x8.Digest
d.Reset()
sums := d.Sums()
for i := 0; i < 8; i++ {
if hex.EncodeToString(sums[i]) != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
t.Fatal()
}
}
}
func TestSingleByte(t *testing.T) {
var d sha256x8.Digest
d.Reset()
d.Write([8][]byte{[]byte{0}, []byte{1}, []byte{2}, []byte{3}, []byte{4},
[]byte{5}, []byte{6}, []byte{7}})
sums := d.Sums()
expected := []string{
"6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
"4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a",
"dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986",
"084fed08b978af4d7d196a7446a86b58009e636b611db16211b65a9aadff29c5",
"e52d9c508c502347344d8c07ad91cbd6068afc75ff6292f062a09ca381c89e71",
"e77b9a9ae9e30b0dbdb6f510a264ef9de781501d7b6b92ae89eb059c5ab743db",
"67586e98fad27da0b9968bc039a1ef34c939b9b8e523a8bef89d478608c5ecf6",
"ca358758f6d27e6cf45272937977a748fd88391db679ceda7dc7bf1f005ee879",
}
for i := 0; i < 8; i++ {
if expected[i] != hex.EncodeToString(sums[i]) {
t.Fatalf("%d: %x != %s", i, sums[i], expected[i])
}
}
}
func TestFuzz(t *testing.T) {
var refs [8]hash.Hash
var d sha256x8.Digest
for n := 0; n < 1000; n++ {
d.Reset()
for i := 0; i < 8; i++ {
refs[i] = sha256.New()
}
for bit := 0; bit < 100; bit++ {
l := rand.Int31n(256)
var bufs [8][]byte
for i := 0; i < 8; i++ {
bufs[i] = make([]byte, l)
rand.Read(bufs[i])
refs[i].Write(bufs[i])
}
d.Write(bufs)
}
sums := d.Sums()
for i := 0; i < 8; i++ {
if !bytes.Equal(refs[i].Sum([]byte{}), sums[i]) {
t.Fatal()
}
}
}
}
func BenchmarkSha256x8On1B(b *testing.B) {
benchmarkSha256x8(1, b)
}
func BenchmarkSha256x8On32B(b *testing.B) {
benchmarkSha256x8(32, b)
}
func BenchmarkSha256x8On64B(b *testing.B) {
benchmarkSha256x8(64, b)
}
func BenchmarkSha256x8On96B(b *testing.B) {
benchmarkSha256x8(96, b)
}
func BenchmarkSha256x8On256B(b *testing.B) {
benchmarkSha256x8(256, b)
}
func BenchmarkSha256x8On10240B(b *testing.B) {
benchmarkSha256x8(10240, b)
}
func BenchmarkSha256On1B(b *testing.B) {
benchmarkSha256(1, b)
}
func BenchmarkSha256On32B(b *testing.B) {
benchmarkSha256(32, b)
}
func BenchmarkSha256On64B(b *testing.B) {
benchmarkSha256(64, b)
}
func BenchmarkSha256On96B(b *testing.B) {
benchmarkSha256(96, b)
}
func BenchmarkSha256On256B(b *testing.B) {
benchmarkSha256(256, b)
}
func BenchmarkSha256On10240B(b *testing.B) {
benchmarkSha256(10240, b)
}
func benchmarkSha256x8(dataLen int, b *testing.B) {
buf := make([]byte, dataLen)
var d sha256x8.Digest
var out [32]byte
bufs := [8][]byte{buf, buf, buf, buf, buf, buf, buf, buf}
outs := [8][]byte{out[:], out[:], out[:], out[:], out[:], out[:], out[:], out[:]}
b.ResetTimer()
for n := 0; n < b.N; n++ {
d.Reset()
d.Write(bufs)
d.SumsInto(outs)
}
}
func benchmarkSha256(dataLen int, b *testing.B) {
d := make([]byte, dataLen)
b.ResetTimer()
for n := 0; n < b.N; n++ {
sha256.Sum256(d)
sha256.Sum256(d)
sha256.Sum256(d)
sha256.Sum256(d)
sha256.Sum256(d)
sha256.Sum256(d)
sha256.Sum256(d)
sha256.Sum256(d)
}
}