Browse Source

Merge pull request #210 from tendermint/bucky/tmhash

tmhash
pull/1780/head
Ethan Buchman 7 years ago
committed by GitHub
parent
commit
c3b72ea708
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 11 deletions
  1. +2
    -2
      merkle/simple_map.go
  2. +6
    -6
      merkle/simple_map_test.go
  3. +3
    -3
      merkle/simple_tree.go
  4. +41
    -0
      merkle/tmhash/hash.go
  5. +23
    -0
      merkle/tmhash/hash_test.go

+ 2
- 2
merkle/simple_map.go View File

@ -2,7 +2,7 @@ package merkle
import (
cmn "github.com/tendermint/tmlibs/common"
"golang.org/x/crypto/ripemd160"
"github.com/tendermint/tmlibs/merkle/tmhash"
)
type SimpleMap struct {
@ -63,7 +63,7 @@ func (sm *SimpleMap) KVPairs() cmn.KVPairs {
type KVPair cmn.KVPair
func (kv KVPair) Hash() []byte {
hasher := ripemd160.New()
hasher := tmhash.New()
err := encodeByteSlice(hasher, kv.Key)
if err != nil {
panic(err)


+ 6
- 6
merkle/simple_map_test.go View File

@ -17,37 +17,37 @@ func TestSimpleMap(t *testing.T) {
{
db := NewSimpleMap()
db.Set("key1", strHasher("value1"))
assert.Equal(t, "acdb4f121bc6f25041eb263ab463f1cd79236a32", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
assert.Equal(t, "3dafc06a52039d029be57c75c9d16356a4256ef4", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
}
{
db := NewSimpleMap()
db.Set("key1", strHasher("value2"))
assert.Equal(t, "b8cbf5adee8c524e14f531da9b49adbbbd66fffa", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
assert.Equal(t, "03eb5cfdff646bc4e80fec844e72fd248a1c6b2c", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
}
{
db := NewSimpleMap()
db.Set("key1", strHasher("value1"))
db.Set("key2", strHasher("value2"))
assert.Equal(t, "1708aabc85bbe00242d3db8c299516aa54e48c38", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
assert.Equal(t, "acc3971eab8513171cc90ce8b74f368c38f9657d", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
}
{
db := NewSimpleMap()
db.Set("key2", strHasher("value2")) // NOTE: out of order
db.Set("key1", strHasher("value1"))
assert.Equal(t, "1708aabc85bbe00242d3db8c299516aa54e48c38", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
assert.Equal(t, "acc3971eab8513171cc90ce8b74f368c38f9657d", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
}
{
db := NewSimpleMap()
db.Set("key1", strHasher("value1"))
db.Set("key2", strHasher("value2"))
db.Set("key3", strHasher("value3"))
assert.Equal(t, "e728afe72ce351eed6aca65c5f78da19b9a6e214", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
assert.Equal(t, "0cd117ad14e6cd22edcd9aa0d84d7063b54b862f", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
}
{
db := NewSimpleMap()
db.Set("key2", strHasher("value2")) // NOTE: out of order
db.Set("key1", strHasher("value1"))
db.Set("key3", strHasher("value3"))
assert.Equal(t, "e728afe72ce351eed6aca65c5f78da19b9a6e214", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
assert.Equal(t, "0cd117ad14e6cd22edcd9aa0d84d7063b54b862f", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
}
}

+ 3
- 3
merkle/simple_tree.go View File

@ -25,11 +25,11 @@ For larger datasets, use IAVLTree.
package merkle
import (
"golang.org/x/crypto/ripemd160"
"github.com/tendermint/tmlibs/merkle/tmhash"
)
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
var hasher = ripemd160.New()
var hasher = tmhash.New()
err := encodeByteSlice(hasher, left)
if err != nil {
panic(err)
@ -68,7 +68,7 @@ func SimpleHashFromByteslices(bzs [][]byte) []byte {
}
func SimpleHashFromBytes(bz []byte) []byte {
hasher := ripemd160.New()
hasher := tmhash.New()
hasher.Write(bz)
return hasher.Sum(nil)
}


+ 41
- 0
merkle/tmhash/hash.go View File

@ -0,0 +1,41 @@
package tmhash
import (
"crypto/sha256"
"hash"
)
var (
Size = 20
BlockSize = sha256.BlockSize
)
type sha256trunc struct {
sha256 hash.Hash
}
func (h sha256trunc) Write(p []byte) (n int, err error) {
return h.sha256.Write(p)
}
func (h sha256trunc) Sum(b []byte) []byte {
shasum := h.sha256.Sum(b)
return shasum[:Size]
}
func (h sha256trunc) Reset() {
h.sha256.Reset()
}
func (h sha256trunc) Size() int {
return Size
}
func (h sha256trunc) BlockSize() int {
return h.sha256.BlockSize()
}
func New() hash.Hash {
return sha256trunc{
sha256: sha256.New(),
}
}

+ 23
- 0
merkle/tmhash/hash_test.go View File

@ -0,0 +1,23 @@
package tmhash_test
import (
"crypto/sha256"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tmlibs/merkle/tmhash"
)
func TestHash(t *testing.T) {
testVector := []byte("abc")
hasher := tmhash.New()
hasher.Write(testVector)
bz := hasher.Sum(nil)
hasher = sha256.New()
hasher.Write(testVector)
bz2 := hasher.Sum(nil)
bz2 = bz2[:20]
assert.Equal(t, bz, bz2)
}

Loading…
Cancel
Save