This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
fuzz.go
60 lines (53 loc) · 1.56 KB
/
fuzz.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
package fuzz
import (
"github.com/cespare/xxhash"
"github.com/protolambda/zrnt/eth2/phase0"
"helper"
)
// TODO(gnattishness) allow configurable at compile time
const VALIDATE_STATE_ROOT bool = true
func xxhash256(input []byte) [32]byte {
var ret [32]byte
hash := xxhash.Sum64(input)
ret[0] = byte(hash & 0xFF)
ret[1] = byte(hash>>8) & 0xFF
ret[2] = byte(hash>>16) & 0xFF
ret[3] = byte(hash>>24) & 0xFF
ret[4] = byte(hash>>32) & 0xFF
ret[5] = byte(hash>>40) & 0xFF
ret[6] = byte(hash>>48) & 0xFF
ret[7] = byte(hash>>56) & 0xFF
return ret
}
func init() {
helper.SetInputType(helper.INPUT_TYPE_BLOCK)
/* Uncomment once PySpec can use xxhash */
/*
ssz.InitZeroHashes(xxhash256)
hashing.Hash = xxhash256
hashing.GetHashFn = func() hashing.HashFn {
return xxhash256
}
*/
}
func Fuzz(data []byte) []byte {
// TODO set fuzz to true here? or no, to keep consistent decoding
input, err := helper.DecodeBlock(data, false)
if err != nil {
// A sanity check to ensure preprocessing works
// Assumes preprocessing ensures data is decodable
panic("Decoding failed - bug in preprocessing.")
}
ffstate := phase0.NewFullFeaturedState(&input.Pre)
ffstate.LoadPrecomputedData()
blockProc := new(phase0.BlockProcessFeature)
blockProc.Meta = ffstate
blockProc.Block = &input.Block
if err := ffstate.StateTransition(blockProc, VALIDATE_STATE_ROOT); err != nil {
return []byte{}
}
// NOTE this will panic if the invariants aren't correct
return helper.EncodePoststate(ffstate.BeaconState)
// equiv to
// return helper.EncodePoststate(&input.Pre)
}