github.com/aquanetwork/aquachain@v1.7.8/opt/bmt/bmt_r.go (about) 1 // Copyright 2017 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 // 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 // see testBMTHasherCorrectness function in bmt_test.go 24 package bmt 25 26 import ( 27 "hash" 28 ) 29 30 // RefHasher is the non-optimized easy to read reference implementation of BMT 31 type RefHasher struct { 32 span int 33 section int 34 cap int 35 h hash.Hash 36 } 37 38 // NewRefHasher returns a new RefHasher 39 func NewRefHasher(hasher BaseHasher, count int) *RefHasher { 40 h := hasher() 41 hashsize := h.Size() 42 maxsize := hashsize * count 43 c := 2 44 for ; c < count; c *= 2 { 45 } 46 if c > 2 { 47 c /= 2 48 } 49 return &RefHasher{ 50 section: 2 * hashsize, 51 span: c * hashsize, 52 cap: maxsize, 53 h: h, 54 } 55 } 56 57 // Hash returns the BMT hash of the byte slice 58 // implements the SwarmHash interface 59 func (rh *RefHasher) Hash(d []byte) []byte { 60 if len(d) > rh.cap { 61 d = d[:rh.cap] 62 } 63 64 return rh.hash(d, rh.span) 65 } 66 67 func (rh *RefHasher) hash(d []byte, s int) []byte { 68 l := len(d) 69 left := d 70 var right []byte 71 if l > rh.section { 72 for ; s >= l; s /= 2 { 73 } 74 left = rh.hash(d[:s], s) 75 right = d[s:] 76 if l-s > rh.section/2 { 77 right = rh.hash(right, s) 78 } 79 } 80 defer rh.h.Reset() 81 rh.h.Write(left) 82 rh.h.Write(right) 83 h := rh.h.Sum(nil) 84 return h 85 }