github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/crypto/merkle/hash.go (about)

     1  package merkle
     2  
     3  import (
     4  	"hash"
     5  
     6  	"github.com/ari-anchor/sei-tendermint/crypto"
     7  )
     8  
     9  // TODO: make these have a large predefined capacity
    10  var (
    11  	leafPrefix  = []byte{0}
    12  	innerPrefix = []byte{1}
    13  )
    14  
    15  // returns tmhash(<empty>)
    16  func emptyHash() []byte {
    17  	return crypto.Checksum([]byte{})
    18  }
    19  
    20  // returns tmhash(0x00 || leaf)
    21  func leafHash(leaf []byte) []byte {
    22  	return crypto.Checksum(append(leafPrefix, leaf...))
    23  }
    24  
    25  // returns tmhash(0x00 || leaf)
    26  func leafHashOpt(s hash.Hash, leaf []byte) []byte {
    27  	s.Reset()
    28  	s.Write(leafPrefix)
    29  	s.Write(leaf)
    30  	return s.Sum(nil)
    31  }
    32  
    33  // returns tmhash(0x01 || left || right)
    34  func innerHash(left []byte, right []byte) []byte {
    35  	data := make([]byte, len(innerPrefix)+len(left)+len(right))
    36  	n := copy(data, innerPrefix)
    37  	n += copy(data[n:], left)
    38  	copy(data[n:], right)
    39  	return crypto.Checksum(data)[:]
    40  }
    41  
    42  func innerHashOpt(s hash.Hash, left []byte, right []byte) []byte {
    43  	s.Reset()
    44  	s.Write(innerPrefix)
    45  	s.Write(left)
    46  	s.Write(right)
    47  	return s.Sum(nil)
    48  }