github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/bmt/bmt_r.go (about) 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 17 // Package bmt is a simple nonconcurrent reference implementation for hashsize segment based 18 // Binary Merkle tree hash on arbitrary but fixed maximum chunksize 19 // 20 // This implementation does not take advantage of any paralellisms and uses 21 // far more memory than necessary, but it is easy to see that it is correct. 22 // It can be used for generating test cases for optimized implementations. 23 // There is extra check on reference hasher correctness in bmt_test.go 24 // * TestRefHasher 25 // * testBMTHasherCorrectness function 26 package bmt 27 28 import ( 29 "hash" 30 ) 31 32 // RefHasher is the non-optimized easy-to-read reference implementation of BMT 33 type RefHasher struct { 34 maxDataLength int // c * hashSize, where c = 2 ^ ceil(log2(count)), where count = ceil(length / hashSize) 35 sectionLength int // 2 * hashSize 36 hasher hash.Hash // base hash func (Keccak256 SHA3) 37 } 38 39 // NewRefHasher returns a new RefHasher 40 func NewRefHasher(hasher BaseHasherFunc, count int) *RefHasher { 41 h := hasher() 42 hashsize := h.Size() 43 c := 2 44 for ; c < count; c *= 2 { 45 } 46 return &RefHasher{ 47 sectionLength: 2 * hashsize, 48 maxDataLength: c * hashsize, 49 hasher: h, 50 } 51 } 52 53 // Hash returns the BMT hash of the byte slice 54 // implements the SwarmHash interface 55 func (rh *RefHasher) Hash(data []byte) []byte { 56 // if data is shorter than the base length (maxDataLength), we provide padding with zeros 57 d := make([]byte, rh.maxDataLength) 58 length := len(data) 59 if length > rh.maxDataLength { 60 length = rh.maxDataLength 61 } 62 copy(d, data[:length]) 63 return rh.hash(d, rh.maxDataLength) 64 } 65 66 // data has length maxDataLength = segmentSize * 2^k 67 // hash calls itself recursively on both halves of the given slice 68 // concatenates the results, and returns the hash of that 69 // if the length of d is 2 * segmentSize then just returns the hash of that section 70 func (rh *RefHasher) hash(data []byte, length int) []byte { 71 var section []byte 72 if length == rh.sectionLength { 73 // section contains two data segments (d) 74 section = data 75 } else { 76 // section contains hashes of left and right BMT subtreea 77 // to be calculated by calling hash recursively on left and right half of d 78 length /= 2 79 section = append(rh.hash(data[:length], length), rh.hash(data[length:], length)...) 80 } 81 rh.hasher.Reset() 82 rh.hasher.Write(section) 83 return rh.hasher.Sum(nil) 84 }