github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/crypto/blake2b/blake2b_f_fuzz_test.go (about)

     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  }