12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package merkle
15
+ package proof
16
16
17
17
import (
18
- "bytes"
19
18
"encoding/hex"
20
19
"fmt"
21
20
"strings"
@@ -212,23 +211,16 @@ func corruptConsistencyProof(size1, size2 uint64, root1, root2 []byte, proof [][
212
211
return ret
213
212
}
214
213
215
- func verifierCheck (v * LogVerifier , leafIndex , treeSize uint64 , proof [][]byte , root , leafHash []byte ) error {
216
- // Verify original inclusion proof.
217
- got , err := v .RootFromInclusionProof (leafIndex , treeSize , leafHash , proof )
218
- if err != nil {
219
- return err
220
- }
221
- if ! bytes .Equal (got , root ) {
222
- return fmt .Errorf ("got root:\n %x\n expected:\n %x" , got , root )
223
- }
224
- if err := v .VerifyInclusion (leafIndex , treeSize , leafHash , proof , root ); err != nil {
214
+ func verifierCheck (nh NodeHasher , leafIndex , treeSize uint64 , proof [][]byte , root , leafHash []byte ) error {
215
+ // FIXME: GetRootFromInclusionProof
216
+ if err := VerifyInclusion (nh , leafIndex , treeSize , leafHash , proof , root ); err != nil {
225
217
return err
226
218
}
227
219
228
220
probes := corruptInclusionProof (leafIndex , treeSize , proof , root , leafHash )
229
221
var wrong []string
230
222
for _ , p := range probes {
231
- if err := v . VerifyInclusion (p .leafIndex , p .treeSize , p .leafHash , p .proof , p .root ); err == nil {
223
+ if err := VerifyInclusion (nh , p .leafIndex , p .treeSize , p .leafHash , p .proof , p .root ); err == nil {
232
224
wrong = append (wrong , p .desc )
233
225
}
234
226
}
@@ -238,9 +230,9 @@ func verifierCheck(v *LogVerifier, leafIndex, treeSize uint64, proof [][]byte, r
238
230
return nil
239
231
}
240
232
241
- func verifierConsistencyCheck (v * LogVerifier , size1 , size2 uint64 , root1 , root2 []byte , proof [][]byte ) error {
233
+ func verifierConsistencyCheck (nh NodeHasher , size1 , size2 uint64 , root1 , root2 []byte , proof [][]byte ) error {
242
234
// Verify original consistency proof.
243
- if err := v . VerifyConsistency (size1 , size2 , root1 , root2 , proof ); err != nil {
235
+ if err := VerifyConsistency (nh , size1 , size2 , proof , root1 , root2 ); err != nil {
244
236
return err
245
237
}
246
238
// For simplicity test only non-trivial proofs that have root1 != root2,
@@ -252,7 +244,7 @@ func verifierConsistencyCheck(v *LogVerifier, size1, size2 uint64, root1, root2
252
244
probes := corruptConsistencyProof (size1 , size2 , root1 , root2 , proof )
253
245
var wrong []string
254
246
for _ , p := range probes {
255
- if err := v . VerifyConsistency (p .size1 , p .size2 , p .root1 , p .root2 , p .proof ); err == nil {
247
+ if err := VerifyConsistency (nh , p .size1 , p .size2 , p .proof , p .root1 , p .root2 ); err == nil {
256
248
wrong = append (wrong , p .desc )
257
249
}
258
250
}
@@ -263,10 +255,10 @@ func verifierConsistencyCheck(v *LogVerifier, size1, size2 uint64, root1, root2
263
255
}
264
256
265
257
func TestVerifyInclusionSingleEntry (t * testing.T ) {
266
- v := NewLogVerifier ( rfc6962 .DefaultHasher )
258
+ hasher := rfc6962 .DefaultHasher
267
259
data := []byte ("data" )
268
260
// Root and leaf hash for 1-entry tree are the same.
269
- hash := v . hasher .HashLeaf (data )
261
+ hash := hasher .HashLeaf (data )
270
262
// The corresponding inclusion proof is empty.
271
263
proof := [][]byte {}
272
264
emptyHash := []byte {}
@@ -282,7 +274,7 @@ func TestVerifyInclusionSingleEntry(t *testing.T) {
282
274
// {emptyHash, emptyHash, true}, // Wrong hash size. FIXME
283
275
} {
284
276
t .Run (fmt .Sprintf ("test:%d" , i ), func (t * testing.T ) {
285
- err := v . VerifyInclusion (0 , 1 , tc .leaf , proof , tc .root )
277
+ err := VerifyInclusion (hasher , 0 , 1 , tc .leaf , proof , tc .root )
286
278
if got , want := err != nil , tc .wantErr ; got != want {
287
279
t .Errorf ("error: %v, want %v" , got , want )
288
280
}
@@ -291,21 +283,21 @@ func TestVerifyInclusionSingleEntry(t *testing.T) {
291
283
}
292
284
293
285
func TestVerifyInclusion (t * testing.T ) {
294
- v := NewLogVerifier ( rfc6962 .DefaultHasher )
286
+ hasher := rfc6962 .DefaultHasher
295
287
proof := [][]byte {}
296
288
297
289
probes := []struct {
298
290
index , size uint64
299
291
}{{0 , 0 }, {0 , 1 }, {1 , 0 }, {2 , 1 }}
300
292
for _ , p := range probes {
301
293
t .Run (fmt .Sprintf ("probe:%d:%d" , p .index , p .size ), func (t * testing.T ) {
302
- if err := v . VerifyInclusion (p .index , p .size , sha256SomeHash , proof , []byte {}); err == nil {
294
+ if err := VerifyInclusion (hasher , p .index , p .size , sha256SomeHash , proof , []byte {}); err == nil {
303
295
t .Error ("Incorrectly verified invalid root/leaf" )
304
296
}
305
- if err := v . VerifyInclusion (p .index , p .size , []byte {}, proof , sha256EmptyTreeHash ); err == nil {
297
+ if err := VerifyInclusion (hasher , p .index , p .size , []byte {}, proof , sha256EmptyTreeHash ); err == nil {
306
298
t .Error ("Incorrectly verified invalid root/leaf" )
307
299
}
308
- if err := v . VerifyInclusion (p .index , p .size , sha256SomeHash , proof , sha256EmptyTreeHash ); err == nil {
300
+ if err := VerifyInclusion (hasher , p .index , p .size , sha256SomeHash , proof , sha256EmptyTreeHash ); err == nil {
309
301
t .Error ("Incorrectly verified invalid root/leaf" )
310
302
}
311
303
})
@@ -316,15 +308,15 @@ func TestVerifyInclusion(t *testing.T) {
316
308
p := inclusionProofs [i ]
317
309
t .Run (fmt .Sprintf ("proof:%d" , i ), func (t * testing.T ) {
318
310
leafHash := rfc6962 .DefaultHasher .HashLeaf (leaves [p .leaf - 1 ])
319
- if err := verifierCheck (& v , p .leaf - 1 , p .size , p .proof , roots [p .size - 1 ], leafHash ); err != nil {
311
+ if err := verifierCheck (hasher , p .leaf - 1 , p .size , p .proof , roots [p .size - 1 ], leafHash ); err != nil {
320
312
t .Errorf ("verifierCheck(): %s" , err )
321
313
}
322
314
})
323
315
}
324
316
}
325
317
326
318
func TestVerifyConsistency (t * testing.T ) {
327
- v := NewLogVerifier ( rfc6962 .DefaultHasher )
319
+ hasher := rfc6962 .DefaultHasher
328
320
329
321
root1 := []byte ("don't care 1" )
330
322
root2 := []byte ("don't care 2" )
@@ -358,7 +350,7 @@ func TestVerifyConsistency(t *testing.T) {
358
350
}
359
351
for i , p := range tests {
360
352
t .Run (fmt .Sprintf ("test:%d:size:%d-%d" , i , p .size1 , p .size2 ), func (t * testing.T ) {
361
- err := verifierConsistencyCheck (& v , p .size1 , p .size2 , p .root1 , p .root2 , p .proof )
353
+ err := verifierConsistencyCheck (hasher , p .size1 , p .size2 , p .root1 , p .root2 , p .proof )
362
354
if p .wantErr && err == nil {
363
355
t .Errorf ("Incorrectly verified" )
364
356
} else if ! p .wantErr && err != nil {
@@ -370,7 +362,7 @@ func TestVerifyConsistency(t *testing.T) {
370
362
for i := 0 ; i < 4 ; i ++ {
371
363
p := consistencyProofs [i ]
372
364
t .Run (fmt .Sprintf ("proof:%d" , i ), func (t * testing.T ) {
373
- err := verifierConsistencyCheck (& v , p .size1 , p .size2 ,
365
+ err := verifierConsistencyCheck (hasher , p .size1 , p .size2 ,
374
366
roots [p .size1 - 1 ], roots [p .size2 - 1 ], p .proof )
375
367
if err != nil {
376
368
t .Fatalf ("Failed to verify known good proof: %s" , err )
0 commit comments