Skip to content

Commit cc6fdaa

Browse files
committed
tree_test
1 parent 3c6e422 commit cc6fdaa

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

testonly/tree_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestTreeHashAt(t *testing.T) {
8484
for size := range len(entries) + 1 {
8585
test(fmt.Sprintf("size:%d", size), entries[:size])
8686
}
87-
test("generated", genEntries(256))
87+
test("generated:256", genEntries(256))
8888
}
8989

9090
func TestTreeInclusionProof(t *testing.T) {
@@ -105,13 +105,47 @@ func TestTreeInclusionProof(t *testing.T) {
105105
})
106106
}
107107

108-
test("generated", genEntries(256))
108+
test("generated:256", genEntries(256))
109109
entries := LeafInputs()
110110
for size := range len(entries) {
111111
test(fmt.Sprintf("golden:%d", size), entries[:size])
112112
}
113113
}
114114

115+
func TestSubtreeInclusionProof(t *testing.T) {
116+
test := func(desc string, entries [][]byte) {
117+
for end := uint64(1); end < uint64(len(entries)); end++ {
118+
for start := range end {
119+
if err := isSubtreeValid(start, end); err != nil {
120+
continue
121+
}
122+
mt := newTree(entries)
123+
t.Run(fmt.Sprintf("%s:%d:%d", desc, start, end), func(t *testing.T) {
124+
t.Parallel()
125+
subtreeEntries := entries[start:end]
126+
for index := start; index < end; index++ {
127+
got, err := mt.SubtreeInclusionProof(index, start, end)
128+
if err != nil {
129+
t.Fatalf("SubtreeInclusionProof(%d, %d, %d): %v", index, start, end, err)
130+
}
131+
want := refInclusionProof(subtreeEntries, index-start, mt.hasher)
132+
if diff := cmp.Diff(got, want, cmpopts.EquateEmpty()); diff != "" {
133+
t.Fatalf("SubtreeInclusionProof(%d, %d, %d): diff (-got +want)\n%s", index, start, end, diff)
134+
}
135+
}
136+
})
137+
}
138+
}
139+
}
140+
141+
// Use smaller trees with subtrees to reduce test duration.
142+
test("generated:128", genEntries(128))
143+
entries := LeafInputs()
144+
for size := 1; size <= len(entries); size++ {
145+
test(fmt.Sprintf("golden:%d", size), entries[:size])
146+
}
147+
}
148+
115149
func TestTreeConsistencyProof(t *testing.T) {
116150
entries := LeafInputs()
117151
mt := newTree(entries)

0 commit comments

Comments
 (0)