Skip to content

Commit

Permalink
Fuzz ConsistencyProof function against reference implementation
Browse files Browse the repository at this point in the history
Add remaining fuzz tests to ClusterFuzzLite
  • Loading branch information
hickford committed Aug 4, 2022
1 parent 8eafa9d commit 3dada97
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .clusterfuzzlite/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@
# undocumented dependency
go install github.com/AdamKorcz/go-118-fuzz-build@latest
go get github.com/AdamKorcz/go-118-fuzz-build/utils

# workaround https://github.com/AdamKorcz/go-118-fuzz-build/issues/2
mv testonly/constants.go testonly/constants_fuzz.go
mv testonly/reference_test.go testonly/reference_test_fuzz.go
mv testonly/tree_test.go testonly/tree_test_fuzz.go
mv testonly/tree.go testonly/tree_fuzz.go

# necessary to list each fuzz test explicitly
compile_native_go_fuzzer github.com/transparency-dev/merkle/compact FuzzRangeNodes FuzzRangeNodes
compile_native_go_fuzzer github.com/transparency-dev/merkle/testonly FuzzConsistencyProofAndVerify FuzzConsistencyProofAndVerify
compile_native_go_fuzzer github.com/transparency-dev/merkle/testonly FuzzInclusionProofAndVerify FuzzInclusionProofAndVerify
compile_native_go_fuzzer github.com/transparency-dev/merkle/testonly FuzzHashAtAgainstReferenceImplementation FuzzHashAtAgainstReferenceImplementation
compile_native_go_fuzzer github.com/transparency-dev/merkle/testonly FuzzInclusionProofAgainstReferenceImplementation FuzzInclusionProofAgainstReferenceImplementation
compile_native_go_fuzzer github.com/transparency-dev/merkle/testonly FuzzConsistencyProofAgainstReferenceImplementation FuzzConsistencyProofAgainstReferenceImplementation
46 changes: 44 additions & 2 deletions testonly/tree_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package testonly

import (
"bytes"
"math"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -12,7 +13,7 @@ import (
)

// Compute and verify consistency proofs
func FuzzConsistencyProof(f *testing.F) {
func FuzzConsistencyProofAndVerify(f *testing.F) {
for size := 0; size <= 8; size++ {
for end := 0; end <= size; end++ {
for begin := 0; begin <= end; begin++ {
Expand All @@ -21,6 +22,9 @@ func FuzzConsistencyProof(f *testing.F) {
}
}
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
Expand All @@ -39,13 +43,16 @@ func FuzzConsistencyProof(f *testing.F) {
}

// Compute and verify inclusion proofs
func FuzzInclusionProof(f *testing.F) {
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
Expand All @@ -70,6 +77,9 @@ func FuzzHashAtAgainstReferenceImplementation(f *testing.F) {
}
}
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
Expand All @@ -91,6 +101,9 @@ func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) {
}
}
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
Expand All @@ -108,3 +121,32 @@ func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) {
}
})
}

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)
}
})
}

0 comments on commit 3dada97

Please sign in to comment.