github.com/core-coin/go-core/v2@v2.1.9/crypto/blake2b/blake2b_f_fuzz.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  //go:build gofuzz
    18  // +build gofuzz
    19  
    20  package blake2b
    21  
    22  import (
    23  	"encoding/binary"
    24  )
    25  
    26  func Fuzz(data []byte) int {
    27  	// Make sure the data confirms to the input model
    28  	if len(data) != 211 {
    29  		return 0
    30  	}
    31  	// Parse everything and call all the implementations
    32  	var (
    33  		rounds = binary.BigEndian.Uint16(data[0:2])
    34  
    35  		h [8]uint64
    36  		m [16]uint64
    37  		t [2]uint64
    38  		f uint64
    39  	)
    40  	for i := 0; i < 8; i++ {
    41  		offset := 2 + i*8
    42  		h[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
    43  	}
    44  	for i := 0; i < 16; i++ {
    45  		offset := 66 + i*8
    46  		m[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
    47  	}
    48  	t[0] = binary.LittleEndian.Uint64(data[194:202])
    49  	t[1] = binary.LittleEndian.Uint64(data[202:210])
    50  
    51  	if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1
    52  		f = 0xFFFFFFFFFFFFFFFF
    53  	}
    54  	// Run the blake2b compression on all instruction sets and cross reference
    55  	want := h
    56  	fGeneric(&want, &m, t[0], t[1], f, uint64(rounds))
    57  
    58  	have := h
    59  	fSSE4(&have, &m, t[0], t[1], f, uint64(rounds))
    60  	if have != want {
    61  		panic("SSE4 mismatches generic algo")
    62  	}
    63  	have = h
    64  	fAVX(&have, &m, t[0], t[1], f, uint64(rounds))
    65  	if have != want {
    66  		panic("AVX mismatches generic algo")
    67  	}
    68  	have = h
    69  	fAVX2(&have, &m, t[0], t[1], f, uint64(rounds))
    70  	if have != want {
    71  		panic("AVX2 mismatches generic algo")
    72  	}
    73  	return 1
    74  }