Skip to content

Commit b5cc7e6

Browse files
committed
crypto/blake2b: put architecture-dependent features behind build-tag (ethereum#28381)
This change to fixes a compilation-flaw on master, by putting architecture-specific functions behind corresponding build tags.
1 parent f7b6ad6 commit b5cc7e6

File tree

2 files changed

+75
-58
lines changed

2 files changed

+75
-58
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Only enable fuzzer on platforms with AVX enabled
2+
//go:build go1.7 && amd64 && !gccgo && !appengine
3+
// +build go1.7,amd64,!gccgo,!appengine
4+
5+
package blake2b
6+
7+
import (
8+
"encoding/binary"
9+
"testing"
10+
)
11+
12+
func Fuzz(f *testing.F) {
13+
f.Fuzz(func(t *testing.T, data []byte) {
14+
fuzz(data)
15+
})
16+
}
17+
18+
func fuzz(data []byte) {
19+
// Make sure the data confirms to the input model
20+
if len(data) != 211 {
21+
return
22+
}
23+
// Parse everything and call all the implementations
24+
var (
25+
rounds = binary.BigEndian.Uint16(data[0:2])
26+
27+
h [8]uint64
28+
m [16]uint64
29+
t [2]uint64
30+
f uint64
31+
)
32+
33+
for i := 0; i < 8; i++ {
34+
offset := 2 + i*8
35+
h[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
36+
}
37+
for i := 0; i < 16; i++ {
38+
offset := 66 + i*8
39+
m[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
40+
}
41+
t[0] = binary.LittleEndian.Uint64(data[194:202])
42+
t[1] = binary.LittleEndian.Uint64(data[202:210])
43+
44+
if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1
45+
f = 0xFFFFFFFFFFFFFFFF
46+
}
47+
48+
// Run the blake2b compression on all instruction sets and cross reference
49+
want := h
50+
fGeneric(&want, &m, t[0], t[1], f, uint64(rounds))
51+
52+
have := h
53+
if useSSE4 {
54+
fSSE4(&have, &m, t[0], t[1], f, uint64(rounds))
55+
if have != want {
56+
panic("SSE4 mismatches generic algo")
57+
}
58+
}
59+
60+
if useAVX {
61+
have = h
62+
fAVX(&have, &m, t[0], t[1], f, uint64(rounds))
63+
if have != want {
64+
panic("AVX mismatches generic algo")
65+
}
66+
}
67+
68+
if useAVX2 {
69+
have = h
70+
fAVX2(&have, &m, t[0], t[1], f, uint64(rounds))
71+
if have != want {
72+
panic("AVX2 mismatches generic algo")
73+
}
74+
}
75+
}

crypto/blake2b/blake2b_f_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package blake2b
22

33
import (
4-
"encoding/binary"
54
"fmt"
65
"reflect"
76
"testing"
@@ -58,60 +57,3 @@ var testVectorsF = []testVector{
5857
},
5958
},
6059
}
61-
62-
func Fuzz(f *testing.F) {
63-
f.Fuzz(func(t *testing.T, data []byte) {
64-
fuzz(data)
65-
})
66-
}
67-
68-
func fuzz(data []byte) {
69-
// Make sure the data confirms to the input model
70-
if len(data) != 211 {
71-
return
72-
}
73-
// Parse everything and call all the implementations
74-
var (
75-
rounds = binary.BigEndian.Uint16(data[0:2])
76-
77-
h [8]uint64
78-
m [16]uint64
79-
t [2]uint64
80-
f uint64
81-
)
82-
83-
for i := 0; i < 8; i++ {
84-
offset := 2 + i*8
85-
h[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
86-
}
87-
for i := 0; i < 16; i++ {
88-
offset := 66 + i*8
89-
m[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
90-
}
91-
t[0] = binary.LittleEndian.Uint64(data[194:202])
92-
t[1] = binary.LittleEndian.Uint64(data[202:210])
93-
94-
if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1
95-
f = 0xFFFFFFFFFFFFFFFF
96-
}
97-
98-
// Run the blake2b compression on all instruction sets and cross reference
99-
want := h
100-
fGeneric(&want, &m, t[0], t[1], f, uint64(rounds))
101-
102-
have := h
103-
fSSE4(&have, &m, t[0], t[1], f, uint64(rounds))
104-
if have != want {
105-
panic("SSE4 mismatches generic algo")
106-
}
107-
have = h
108-
fAVX(&have, &m, t[0], t[1], f, uint64(rounds))
109-
if have != want {
110-
panic("AVX mismatches generic algo")
111-
}
112-
have = h
113-
fAVX2(&have, &m, t[0], t[1], f, uint64(rounds))
114-
if have != want {
115-
panic("AVX2 mismatches generic algo")
116-
}
117-
}

0 commit comments

Comments
 (0)