Debian Codegraph @

fuzz.go

raw symbols SCIP
1 // +build gofuzz
2
3 package merkletree
4
5 import (
6 "bytes"
7 "crypto/sha256"
8 "math"
9 )
10
11 // Fuzz is called by go-fuzz to look for inputs to BuildReaderProof that will
12 // not verify correctly.
13 func Fuzz(data []byte) int {
14 // Use the first two bytes to determine the proof index.
15 if len(data) < 2 {
16 return -1
17 }
18 index := 256*uint64(data[0]) + uint64(data[1])
19 data = data[2:]
20
21 // Build a reader proof for index 'index' using the remaining data as input
22 // to the reader. '64' is chosen as the only input size because that is the
23 // size relevant to the Sia project.
24 merkleRoot, proofSet, numLeaves, err := BuildReaderProof(bytes.NewReader(data), sha256.New(), 64, index)
25 if err != nil {
26 return 0
27 }
28 if !VerifyProof(sha256.New(), merkleRoot, proofSet, index, numLeaves) {
29 panic("verification failed!")
30 }
31
32 // Output is more interesting when there is enough data to contain the
33 // index.
34 if uint64(len(data)) > 64*index {
35 return 1
36 }
37 return 0
38 }
39
40 // FuzzReadSubTreesWithProof can be used by go-fuzz to test creating a merkle
41 // tree from cached subTrees and creating/proving a merkle proof on this tree.
42 func FuzzReadSubTreesWithProof(data []byte) int {
43 // We want at least 2 bytes for the index and 1 for a subTree.
44 if len(data) < 3 {
45 return -1
46 }
47 index := 256*uint64(data[0]) + uint64(data[1])
48 data = data[2:]
49 cachedTree, numLeaves := buildAndCompareTreesFromFuzz(data, index)
50
51 // Create and verify the proof.
52 merkleRoot, proofSet, _, numLeaves := cachedTree.Prove()
53 if proofSet == nil {
54 return 0
55 }
56 if !VerifyProof(sha256.New(), merkleRoot, proofSet, index, numLeaves) {
57 panic("verification failed!")
58 }
59
60 // Output is more interesting when there is enough data to contain the
61 // index.
62 if numLeaves > index {
63 return 1
64 }
65 return 0
66 }
67
68 // FuzzReadSubTreesNoProof can be used by go-fuzz to test creating a merkle
69 // tree from cached subTrees.
70 func FuzzReadSubTreesNoProof(data []byte) int {
71 buildAndCompareTreesFromFuzz(data, math.MaxUint64)
72 if len(data) > 2 {
73 return 1
74 }
75 return 0
76 }
77
78 // buildAndCompareTreesFromFuzz will read the input data and create a subTree
79 // or leaf for each byte of the input data. It returns the cached tree.
80 func buildAndCompareTreesFromFuzz(data []byte, proofIndex uint64) (cachedTree *Tree, numLeaves uint64) {
81 hash := sha256.New()
82 tree := New(hash)
83 cachedTree = New(hash)
84 if proofIndex != math.MaxUint64 {
85 if err := cachedTree.SetIndex(proofIndex); err != nil {
86 panic(err)
87 }
88 }
89
90 for _, b := range data {
91 b = b % 6 // should be in range [0;5]
92
93 // if b == 0 we add the data as a leaf.
94 if b == 0 {
95 data := hash.Sum([]byte{byte(numLeaves)})
96 tree.Push(data)
97 cachedTree.Push(data)
98 numLeaves++
99 continue
100 }
101 // else we create a cached subTree of height b-1
102 height := int(b - 1) // should be in range [0:4]
103 subTree := New(hash)
104 for i := uint64(0); i < 1<<uint64(height); i++ {
105 data := hash.Sum([]byte{byte(numLeaves)})
106 tree.Push(data)
107 subTree.Push(data)
108 numLeaves++
109 }
110 if err := cachedTree.PushSubTree(height, subTree.Root()); err != nil {
111 return
112 }
113 }
114 if !bytes.Equal(tree.Root(), cachedTree.Root()) {
115 panic("tree roots don't match")
116 }
117 return
118 }