github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/blake2b/blake2b_f_fuzz.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    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  }