bmt_r.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package bmt is a simple nonconcurrent reference implementation for hashsize segment based
  17. // Binary Merkle tree hash on arbitrary but fixed maximum chunksize
  18. //
  19. // This implementation does not take advantage of any paralellisms and uses
  20. // far more memory than necessary, but it is easy to see that it is correct.
  21. // It can be used for generating test cases for optimized implementations.
  22. // There is extra check on reference hasher correctness in bmt_test.go
  23. // * TestRefHasher
  24. // * testBMTHasherCorrectness function
  25. package bmt
  26. import (
  27. "hash"
  28. )
  29. // RefHasher is the non-optimized easy-to-read reference implementation of BMT
  30. type RefHasher struct {
  31. maxDataLength int // c * hashSize, where c = 2 ^ ceil(log2(count)), where count = ceil(length / hashSize)
  32. sectionLength int // 2 * hashSize
  33. hasher hash.Hash // base hash func (Keccak256 SHA3)
  34. }
  35. // NewRefHasher returns a new RefHasher
  36. func NewRefHasher(hasher BaseHasherFunc, count int) *RefHasher {
  37. h := hasher()
  38. hashsize := h.Size()
  39. c := 2
  40. for ; c < count; c *= 2 {
  41. }
  42. return &RefHasher{
  43. sectionLength: 2 * hashsize,
  44. maxDataLength: c * hashsize,
  45. hasher: h,
  46. }
  47. }
  48. // Hash returns the BMT hash of the byte slice
  49. // implements the SwarmHash interface
  50. func (rh *RefHasher) Hash(data []byte) []byte {
  51. // if data is shorter than the base length (maxDataLength), we provide padding with zeros
  52. d := make([]byte, rh.maxDataLength)
  53. length := len(data)
  54. if length > rh.maxDataLength {
  55. length = rh.maxDataLength
  56. }
  57. copy(d, data[:length])
  58. return rh.hash(d, rh.maxDataLength)
  59. }
  60. // data has length maxDataLength = segmentSize * 2^k
  61. // hash calls itself recursively on both halves of the given slice
  62. // concatenates the results, and returns the hash of that
  63. // if the length of d is 2 * segmentSize then just returns the hash of that section
  64. func (rh *RefHasher) hash(data []byte, length int) []byte {
  65. var section []byte
  66. if length == rh.sectionLength {
  67. // section contains two data segments (d)
  68. section = data
  69. } else {
  70. // section contains hashes of left and right BMT subtreea
  71. // to be calculated by calling hash recursively on left and right half of d
  72. length /= 2
  73. section = append(rh.hash(data[:length], length), rh.hash(data[length:], length)...)
  74. }
  75. rh.hasher.Reset()
  76. rh.hasher.Write(section)
  77. return rh.hasher.Sum(nil)
  78. }