diff --git a/merkletree/hasher/hasher_test.go b/merkletree/hasher/hasher_test.go new file mode 100644 index 0000000..bf84cb2 --- /dev/null +++ b/merkletree/hasher/hasher_test.go @@ -0,0 +1,55 @@ +package hasher + +import ( + "encoding/hex" + "testing" +) + +// h2h converts a hex string into its Hash object. +func h2h(h string) Hash { + b, err := hex.DecodeString(h) + if err != nil { + panic("invalid hex string") + } + var ret Hash + copy(ret[:], b) + return ret +} + +// s2h converts a byte slice into its Hash object. +func s2h(s []byte) Hash { + var ret Hash + copy(ret[:], s) + return ret +} + +func TestHashLeafVectors(t *testing.T) { + for _, tc := range []struct { + treeNonce []byte // treeNonce is treeID in KT, it should be a 8-byte slice + index []byte + depth uint32 + leaf []byte + want Hash + }{ + {treeNonce: make([]byte, 8), index: []byte("foo"), depth: 128, leaf: []byte("leaf"), want: h2h("d7e63cd03f40c88dcddd11cd33d060ba555adf5a792846fb47fea12d95d3501c")}, + } { + if got, want := s2h(Default.HashLeaf(tc.treeNonce, tc.index, tc.depth, tc.leaf)), tc.want; got != want { + t.Errorf("HashLeaf(%v, %s, %v, %s): %x, want %x", tc.treeNonce, tc.index, tc.depth, tc.leaf, got, want) + } + } +} + +func TestHashEmptyVectors(t *testing.T) { + for _, tc := range []struct { + treeNonce []byte // treeNonce is treeID in KT, it should be a 8-byte slice + index []byte + depth uint32 + want Hash + }{ + {treeNonce: make([]byte, 8), index: []byte("foo"), depth: 128, want: h2h("ee50dbada0aeb067724550e453101b802034a277bc0565d751c22ee1dce4998f")}, + } { + if got, want := s2h(Default.HashEmpty(tc.treeNonce, tc.index, tc.depth)), tc.want; got != want { + t.Errorf("HashLeaf(%v, %s, %v): %x, want %x", tc.treeNonce, tc.index, tc.depth, got, want) + } + } +}