Skip to content

Commit

Permalink
Add more RFC6962 hasher tests (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
pav-kv authored May 8, 2018
1 parent 1338abf commit 3a68a84
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions merkle/rfc6962/rfc6962_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,36 @@ import (
_ "github.com/golang/glog"
)

func TestRfc6962Hasher(t *testing.T) {
func TestRFC6962Hasher(t *testing.T) {
hasher := DefaultHasher

leafHash, err := hasher.HashLeaf([]byte("L123456"))
if err != nil {
t.Fatalf("HashLeaf(): %v", err)
}
emptyLeafHash, err := hasher.HashLeaf([]byte{})
if err != nil {
t.Fatalf("HashLeaf(empty): %v", err)
}

for _, tc := range []struct {
desc string
got []byte
want string
}{
// echo -n | sha256sum
{
desc: "RFC962 Empty",
desc: "RFC6962 Empty",
want: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
got: hasher.EmptyRoot(),
},
// Check that the empty hash is not the same as the hash of an empty leaf.
// echo -n 00 | xxd -r -p | sha256sum
{
desc: "RFC6962 Empty Leaf",
want: "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
got: emptyLeafHash,
},
// echo -n 004C313233343536 | xxd -r -p | sha256sum
{
desc: "RFC6962 Leaf",
Expand All @@ -52,14 +64,42 @@ func TestRfc6962Hasher(t *testing.T) {
got: hasher.HashChildren([]byte("N123"), []byte("N456")),
},
} {
wantBytes, err := hex.DecodeString(tc.want)
if err != nil {
t.Errorf("hex.DecodeString(%v): %v", tc.want, err)
continue
}
t.Run(tc.desc, func(t *testing.T) {
wantBytes, err := hex.DecodeString(tc.want)
if err != nil {
t.Fatalf("hex.DecodeString(%x): %v", tc.want, err)
}
if got, want := tc.got, wantBytes; !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
})
}
}

// TODO(pavelkalinnikov): Apply this test to all LogHasher implementations.
func TestRFC6962HasherCollisions(t *testing.T) {
hasher := DefaultHasher

// Check that different leaves have different hashes.
leaf1, leaf2 := []byte("Hello"), []byte("World")
hash1, _ := hasher.HashLeaf(leaf1)
hash2, _ := hasher.HashLeaf(leaf2)
if bytes.Equal(hash1, hash2) {
t.Errorf("Leaf hashes should differ, but both are %x", hash1)
}

// Compute an intermediate subtree hash.
subHash1 := hasher.HashChildren(hash1, hash2)
// Check that this is not the same as a leaf hash of their concatenation.
preimage := append(hash1, hash2...)
forgedHash, _ := hasher.HashLeaf(preimage)
if bytes.Equal(subHash1, forgedHash) {
t.Errorf("Hasher is not second-preimage resistant")
}

if got, want := tc.got, wantBytes; !bytes.Equal(got, want) {
t.Fatalf("%v: got %x, want %x", tc.desc, got, want)
}
// Swap the order of nodes and check that the hash is different.
subHash2 := hasher.HashChildren(hash2, hash1)
if bytes.Equal(subHash1, subHash2) {
t.Errorf("Subtree hash does not depend on the order of leaves")
}
}

0 comments on commit 3a68a84

Please sign in to comment.