You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
969 B

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