github.com/okex/exchain@v1.8.0/libs/tendermint/crypto/merkle/hash.go (about)

     1  package merkle
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/okex/exchain/libs/tendermint/crypto/tmhash"
     6  	"sync"
     7  )
     8  
     9  // TODO: make these have a large predefined capacity
    10  var (
    11  	leafPrefix  = []byte{0}
    12  	innerPrefix = []byte{1}
    13  
    14  	hashBytesPool = &sync.Pool{
    15  		New: func() interface{} {
    16  			return &bytes.Buffer{}
    17  		},
    18  	}
    19  )
    20  
    21  // returns tmhash(0x00 || leaf)
    22  func leafHash(leaf []byte) []byte {
    23  	buf := hashBytesPool.Get().(*bytes.Buffer)
    24  	buf.Reset()
    25  	buf.Grow(len(leafPrefix) + len(leaf))
    26  	buf.Write(leafPrefix)
    27  	buf.Write(leaf)
    28  	h := tmhash.Sum(buf.Bytes())
    29  	hashBytesPool.Put(buf)
    30  	return h
    31  }
    32  
    33  // returns tmhash(0x01 || left || right)
    34  func innerHash(left []byte, right []byte) []byte {
    35  	buf := hashBytesPool.Get().(*bytes.Buffer)
    36  	buf.Reset()
    37  	buf.Grow(len(innerPrefix) + len(left) + len(right))
    38  	buf.Write(innerPrefix)
    39  	buf.Write(left)
    40  	buf.Write(right)
    41  	h := tmhash.Sum(buf.Bytes())
    42  	hashBytesPool.Put(buf)
    43  	return h
    44  }