Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzz ConsistencyProof function against reference implementation #36

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 45 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,10 @@ func FuzzConsistencyProof(f *testing.F) {
}
}
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
Expand All @@ -39,13 +44,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 +78,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 +102,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 +122,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)
}
})
}