github.com/codingfuture/orig-energi3@v0.8.4/swarm/bmt/bmt_r.go (about)

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