File: tree_fuzz_test.go

package info (click to toggle)
golang-github-transparency-dev-merkle 0.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: sh: 124; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 3,861 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//go:build go1.18

package testonly

import (
	"bytes"
	"math"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/transparency-dev/merkle/proof"
)

// Compute and verify consistency proofs
func FuzzConsistencyProofAndVerify(f *testing.F) {
	for size := 0; size <= 8; size++ {
		for end := 0; end <= size; end++ {
			for begin := 0; begin <= end; begin++ {
				f.Add(uint64(size), uint64(begin), uint64(end))
			}
		}
	}
	f.Fuzz(func(t *testing.T, size, begin, end uint64) {
		// necessary to restrict size for compile_native_go_fuzzer
		if size >= math.MaxUint16 {
			return
		}
		t.Logf("size=%d, begin=%d, end=%d", size, begin, end)
		if begin > end || end > size {
			return
		}
		tree := newTree(genEntries(size))
		p, err := tree.ConsistencyProof(begin, end)
		t.Logf("proof=%v", p)
		if err != nil {
			t.Error(err)
		}
		err = proof.VerifyConsistency(tree.hasher, begin, end, p, tree.HashAt(begin), tree.HashAt(end))
		if err != nil {
			t.Error(err)
		}
	})
}

// Compute and verify inclusion proofs
func FuzzInclusionProofAndVerify(f *testing.F) {
	for size := 0; size <= 8; size++ {
		for index := 0; index <= size; index++ {
			f.Add(uint64(index), uint64(size))
		}
	}
	f.Fuzz(func(t *testing.T, index, size uint64) {
		if size >= math.MaxUint16 {
			return
		}
		t.Logf("index=%d, size=%d", index, size)
		if index >= size {
			return
		}
		tree := newTree(genEntries(size))
		p, err := tree.InclusionProof(index, size)
		t.Logf("proof=%v", p)
		if err != nil {
			t.Error(err)
		}
		err = proof.VerifyInclusion(tree.hasher, index, size, tree.LeafHash(index), p, tree.Hash())
		if err != nil {
			t.Error(err)
		}
	})
}

func FuzzHashAtAgainstReferenceImplementation(f *testing.F) {
	for size := 0; size <= 8; size++ {
		for index := 0; index <= size; index++ {
			f.Add(uint64(index), uint64(size))
		}
	}
	f.Fuzz(func(t *testing.T, index, size uint64) {
		if size >= math.MaxUint16 {
			return
		}
		t.Logf("index=%d, size=%d", index, size)
		if index >= size {
			return
		}
		entries := genEntries(size)
		mt := newTree(entries)
		got := mt.HashAt(uint64(size))
		want := refRootHash(entries[:size], mt.hasher)
		if !bytes.Equal(got, want) {
			t.Errorf("HashAt(%d): %x, want %x", size, got, want)
		}
	})
}

func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) {
	for size := 0; size <= 8; size++ {
		for index := 0; index <= size; index++ {
			f.Add(uint64(index), uint64(size))
		}
	}
	f.Fuzz(func(t *testing.T, index, size uint64) {
		if size >= math.MaxUint16 {
			return
		}
		t.Logf("index=%d, size=%d", index, size)
		if index >= size {
			return
		}
		entries := genEntries(size)
		tree := newTree(entries)
		got, err := tree.InclusionProof(index, size)
		t.Logf("proof=%v", got)
		if err != nil {
			t.Error(err)
		}
		want := refInclusionProof(entries, index, tree.hasher)
		if diff := cmp.Diff(got, want, cmpopts.EquateEmpty()); diff != "" {
			t.Errorf("InclusionProof(%d, %d): diff (-got +want)\n%s", index, size, diff)
		}
	})
}

func FuzzConsistencyProofAgainstReferenceImplementation(f *testing.F) {
	for size := 0; size <= 8; size++ {
		for end := 0; end <= size; end++ {
			for begin := 0; begin <= end; begin++ {
				f.Add(uint64(size), uint64(begin), uint64(end))
			}
		}
	}
	f.Fuzz(func(t *testing.T, size, begin, end uint64) {
		if size >= math.MaxUint16 {
			return
		}
		t.Logf("size=%d, begin=%d, end=%d", size, begin, end)
		if begin > end || end > size {
			return
		}
		entries := genEntries(size)
		tree := newTree(entries)
		got, err := tree.ConsistencyProof(begin, end)
		if err != nil {
			t.Errorf("ConsistencyProof: %v", err)
		}
		want := refConsistencyProof(entries[:end], end, begin, tree.hasher, true)
		if diff := cmp.Diff(got, want, cmpopts.EquateEmpty()); diff != "" {
			t.Errorf("ConsistencyProof: diff (-got +want)\n%s", diff)
		}
	})
}