@@ -38,6 +38,20 @@ type inclusionProbe struct {
3838 WantError bool `json:"wantErr"`
3939}
4040
41+ // subtreeInclusionProbe is a parameter set for subtree inclusion proof
42+ // verification.
43+ type subtreeInclusionProbe struct {
44+ LeafIdx uint64 `json:"leafIdx"`
45+ Start uint64 `json:"start"`
46+ End uint64 `json:"end"`
47+ Root []byte `json:"root"`
48+ LeafHash []byte `json:"leafHash"`
49+ Proof [][]byte `json:"proof"`
50+
51+ Desc string `json:"desc"`
52+ WantError bool `json:"wantErr"`
53+ }
54+
4155// consistencyProbe is a parameter set for consistency proof verification.
4256type consistencyProbe struct {
4357 Size1 uint64 `json:"size1"`
@@ -102,6 +116,58 @@ func TestVerifyInclusionProbes(t *testing.T) {
102116 }
103117}
104118
119+ func TestVerifySubtreeInclusionProbes (t * testing.T ) {
120+ var probes []subtreeInclusionProbe
121+
122+ if err := filepath .WalkDir ("../testdata/subtreeinclusion" , func (path string , d fs.DirEntry , err error ) error {
123+ if err != nil {
124+ return err
125+ }
126+
127+ if d .IsDir () {
128+ return nil
129+ }
130+
131+ if filepath .Ext (d .Name ()) != ".json" {
132+ return nil
133+ }
134+
135+ data , err := os .ReadFile (path )
136+ if err != nil {
137+ return err
138+ }
139+
140+ var probe subtreeInclusionProbe
141+ if err := json .Unmarshal (data , & probe ); err != nil {
142+ return fmt .Errorf ("failed to parse subtree inclusion probe json: %s" , err )
143+ }
144+
145+ probes = append (probes , probe )
146+
147+ return nil
148+ }); err != nil {
149+ t .Errorf ("failed to read subtree inclusion probes: %s" , err )
150+ }
151+
152+ var wrong []string
153+ for _ , p := range probes {
154+ err := VerifySubtreeInclusion (rfc6962 .DefaultHasher , p .LeafIdx , p .Start , p .End , p .LeafHash , p .Proof , p .Root )
155+ if p .WantError && err == nil {
156+ wrong = append (wrong , fmt .Sprintf ("expected error but didn't get one: %s" , p .Desc ))
157+ continue
158+ }
159+
160+ if ! p .WantError && err != nil {
161+ wrong = append (wrong , fmt .Sprintf ("unexpected error: %s, %s" , p .Desc , err ))
162+ continue
163+ }
164+ }
165+
166+ if len (wrong ) > 0 {
167+ t .Errorf ("errors verifying subtree inclusion probes: \n %d out of %d failures \n Error messages: \n %s" , len (wrong ), len (probes ), strings .Join (wrong , "\n " ))
168+ }
169+ }
170+
105171func TestVerifyConsistencyProbes (t * testing.T ) {
106172 var probes []consistencyProbe
107173
0 commit comments