Make Ripemd160 the default

This commit is contained in:
Jae Kwon
2015-07-10 12:15:46 -07:00
parent 41845d5b85
commit 2e1d8ba054
10 changed files with 26 additions and 32 deletions

View File

@ -28,7 +28,7 @@ func GenPrivAccount() *PrivAccount {
// Generates a new account with private key from SHA256 hash of a secret // Generates a new account with private key from SHA256 hash of a secret
func GenPrivAccountFromSecret(secret []byte) *PrivAccount { func GenPrivAccountFromSecret(secret []byte) *PrivAccount {
privKey32 := binary.BinarySha256(secret) privKey32 := binary.BinarySha256(secret) // Not Ripemd160 because we want 32 bytes.
privKeyBytes := new([64]byte) privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], privKey32) copy(privKeyBytes[:32], privKey32)
pubKeyBytes := ed25519.MakePublicKey(privKeyBytes) pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)

View File

@ -40,6 +40,7 @@ func BinaryCompare(a, b interface{}) int {
return bytes.Compare(aBytes, bBytes) return bytes.Compare(aBytes, bBytes)
} }
// NOTE: only use this if you need 32 bytes.
func BinarySha256(o interface{}) []byte { func BinarySha256(o interface{}) []byte {
hasher, n, err := sha256.New(), new(int64), new(error) hasher, n, err := sha256.New(), new(int64), new(error)
WriteBinary(o, hasher, n, err) WriteBinary(o, hasher, n, err)
@ -49,6 +50,7 @@ func BinarySha256(o interface{}) []byte {
return hasher.Sum(nil) return hasher.Sum(nil)
} }
// NOTE: The default hash function is Ripemd160.
func BinaryRipemd160(o interface{}) []byte { func BinaryRipemd160(o interface{}) []byte {
hasher, n, err := ripemd160.New(), new(int64), new(error) hasher, n, err := ripemd160.New(), new(int64), new(error)
WriteBinary(o, hasher, n, err) WriteBinary(o, hasher, n, err)

View File

@ -11,7 +11,6 @@ package mempool
import ( import (
"sync" "sync"
"github.com/tendermint/tendermint/binary"
sm "github.com/tendermint/tendermint/state" sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
@ -73,19 +72,18 @@ func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) {
// First, create a lookup map of txns in new block. // First, create a lookup map of txns in new block.
blockTxsMap := make(map[string]struct{}) blockTxsMap := make(map[string]struct{})
for _, tx := range block.Data.Txs { for _, tx := range block.Data.Txs {
txHash := binary.BinarySha256(tx) blockTxsMap[string(types.TxID(state.ChainID, tx))] = struct{}{}
blockTxsMap[string(txHash)] = struct{}{}
} }
// Next, filter all txs from mem.txs that are in blockTxsMap // Next, filter all txs from mem.txs that are in blockTxsMap
txs := []types.Tx{} txs := []types.Tx{}
for _, tx := range mem.txs { for _, tx := range mem.txs {
txHash := binary.BinarySha256(tx) txID := types.TxID(state.ChainID, tx)
if _, ok := blockTxsMap[string(txHash)]; ok { if _, ok := blockTxsMap[string(txID)]; ok {
log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash) log.Debug("Filter out, already committed", "tx", tx, "txID", txID)
continue continue
} else { } else {
log.Debug("Filter in, still new", "tx", tx, "txHash", txHash) log.Debug("Filter in, still new", "tx", tx, "txID", txID)
txs = append(txs, tx) txs = append(txs, tx)
} }
} }

View File

@ -2,7 +2,7 @@ package merkle
import ( import (
"bytes" "bytes"
"crypto/sha256" "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
"io" "io"
"github.com/tendermint/tendermint/binary" "github.com/tendermint/tendermint/binary"
@ -132,7 +132,7 @@ func (node *IAVLNode) hashWithCount(t *IAVLTree) ([]byte, int) {
return node.hash, 0 return node.hash, 0
} }
hasher := sha256.New() hasher := ripemd160.New()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, hashCount, err := node.writeHashBytes(t, buf) _, hashCount, err := node.writeHashBytes(t, buf)
if err != nil { if err != nil {

View File

@ -2,7 +2,9 @@ package merkle
import ( import (
"bytes" "bytes"
"crypto/sha256"
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
"github.com/tendermint/tendermint/binary" "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -41,7 +43,7 @@ type IAVLProofInnerNode struct {
} }
func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte { func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte {
hasher := sha256.New() hasher := ripemd160.New()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
n, err := int64(0), error(nil) n, err := int64(0), error(nil)
binary.WriteInt8(branch.Height, buf, &n, &err) binary.WriteInt8(branch.Height, buf, &n, &err)
@ -67,7 +69,7 @@ type IAVLProofLeafNode struct {
} }
func (leaf IAVLProofLeafNode) Hash() []byte { func (leaf IAVLProofLeafNode) Hash() []byte {
hasher := sha256.New() hasher := ripemd160.New()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
n, err := int64(0), error(nil) n, err := int64(0), error(nil)
binary.WriteInt8(0, buf, &n, &err) binary.WriteInt8(0, buf, &n, &err)

View File

@ -26,16 +26,17 @@ package merkle
import ( import (
"bytes" "bytes"
"crypto/sha256"
"fmt" "fmt"
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
"github.com/tendermint/tendermint/binary" "github.com/tendermint/tendermint/binary"
) )
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte { func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
var n int64 var n int64
var err error var err error
var hasher = sha256.New() var hasher = ripemd160.New()
binary.WriteByteSlice(left, hasher, &n, &err) binary.WriteByteSlice(left, hasher, &n, &err)
binary.WriteByteSlice(right, hasher, &n, &err) binary.WriteByteSlice(right, hasher, &n, &err)
if err != nil { if err != nil {
@ -69,7 +70,7 @@ func SimpleHashFromBinaries(items []interface{}) []byte {
// General Convenience // General Convenience
func SimpleHashFromBinary(item interface{}) []byte { func SimpleHashFromBinary(item interface{}) []byte {
hasher, n, err := sha256.New(), new(int64), new(error) hasher, n, err := ripemd160.New(), new(int64), new(error)
binary.WriteBinary(item, hasher, n, err) binary.WriteBinary(item, hasher, n, err)
if *err != nil { if *err != nil {
panic(err) panic(err)

View File

@ -98,7 +98,7 @@ func (v *Validator) String() string {
} }
func (v *Validator) Hash() []byte { func (v *Validator) Hash() []byte {
return binary.BinarySha256(v) return binary.BinaryRipemd160(v)
} }
//------------------------------------- //-------------------------------------

View File

@ -2,7 +2,6 @@ package types
import ( import (
"bytes" "bytes"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -138,15 +137,7 @@ func (h *Header) Hash() []byte {
return nil return nil
} }
buf := new(bytes.Buffer) return binary.BinaryRipemd160(h)
hasher, n, err := sha256.New(), new(int64), new(error)
binary.WriteBinary(h, buf, n, err)
if *err != nil {
panic(err)
}
hasher.Write(buf.Bytes())
hash := hasher.Sum(nil)
return hash
} }
func (h *Header) StringIndented(indent string) string { func (h *Header) StringIndented(indent string) string {
@ -321,7 +312,7 @@ func (data *Data) Hash() []byte {
for i, tx := range data.Txs { for i, tx := range data.Txs {
bs[i] = account.SignBytes(config.GetString("chain_id"), tx) bs[i] = account.SignBytes(config.GetString("chain_id"), tx)
} }
data.hash = merkle.SimpleHashFromBinaries(bs) data.hash = merkle.SimpleHashFromBinaries(bs) // NOTE: leaves are TxIDs.
} }
return data.hash return data.hash
} }

View File

@ -2,12 +2,13 @@ package types
import ( import (
"bytes" "bytes"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"sync" "sync"
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
"github.com/tendermint/tendermint/binary" "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/merkle"
@ -34,7 +35,7 @@ func (part *Part) Hash() []byte {
if part.hash != nil { if part.hash != nil {
return part.hash return part.hash
} else { } else {
hasher := sha256.New() hasher := ripemd160.New()
_, err := hasher.Write(part.Bytes) _, err := hasher.Write(part.Bytes)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -314,8 +314,7 @@ func (tx *DupeoutTx) String() string {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// NOTE: the tx merkle tree uses sha256, so this TxID is really just for // This should match the leaf hashes of Block.Data.Hash()'s SimpleMerkleTree.
// reference when using the rpc and catching events
func TxID(chainID string, tx Tx) []byte { func TxID(chainID string, tx Tx) []byte {
signBytes := account.SignBytes(chainID, tx) signBytes := account.SignBytes(chainID, tx)
return binary.BinaryRipemd160(signBytes) return binary.BinaryRipemd160(signBytes)