6 . "github.com/tendermint/tmlibs/common"
7 . "github.com/tendermint/tmlibs/test"
14 func (tI testItem) Hash() []byte {
18 func TestSimpleProof(t *testing.T) {
22 items := make([]Hashable, total)
23 for i := 0; i < total; i++ {
24 items[i] = testItem(RandBytes(32))
27 rootHash := SimpleHashFromHashables(items)
29 rootHash2, proofs := SimpleProofsFromHashables(items)
31 if !bytes.Equal(rootHash, rootHash2) {
32 t.Errorf("Unmatched root hashes: %X vs %X", rootHash, rootHash2)
35 // For each item, check the trail.
36 for i, item := range items {
37 itemHash := item.Hash()
41 ok := proof.Verify(i, total, itemHash, rootHash)
43 t.Errorf("Verification failed for index %v.", i)
46 // Wrong item index should make it fail
48 ok = proof.Verify((i+1)%total, total, itemHash, rootHash)
50 t.Errorf("Expected verification to fail for wrong index %v.", i)
54 // Trail too long should make it fail
55 origAunts := proof.Aunts
56 proof.Aunts = append(proof.Aunts, RandBytes(32))
58 ok = proof.Verify(i, total, itemHash, rootHash)
60 t.Errorf("Expected verification to fail for wrong trail length.")
63 proof.Aunts = origAunts
65 // Trail too short should make it fail
66 proof.Aunts = proof.Aunts[0 : len(proof.Aunts)-1]
68 ok = proof.Verify(i, total, itemHash, rootHash)
70 t.Errorf("Expected verification to fail for wrong trail length.")
73 proof.Aunts = origAunts
75 // Mutating the itemHash should make it fail.
76 ok = proof.Verify(i, total, MutateByteSlice(itemHash), rootHash)
78 t.Errorf("Expected verification to fail for mutated leaf hash")
81 // Mutating the rootHash should make it fail.
82 ok = proof.Verify(i, total, itemHash, MutateByteSlice(rootHash))
84 t.Errorf("Expected verification to fail for mutated root hash")