github.com/braveheart12/insolar-09-08-19@v0.8.7/network/merkle/helpers.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package merkle
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/insolar/insolar/core"
    24  	"github.com/insolar/insolar/core/utils"
    25  )
    26  
    27  const reserved = 0xDEADBEEF
    28  
    29  type merkleHelper struct {
    30  	scheme     core.PlatformCryptographyScheme
    31  	leafHasher core.Hasher
    32  }
    33  
    34  func newMerkleHelper(scheme core.PlatformCryptographyScheme) *merkleHelper {
    35  	return &merkleHelper{
    36  		scheme:     scheme,
    37  		leafHasher: scheme.IntegrityHasher(),
    38  	}
    39  }
    40  
    41  func (mh *merkleHelper) doubleSliceHash(slice1, slice2 []byte) []byte {
    42  	hasher := mh.scheme.IntegrityHasher()
    43  	var err error
    44  
    45  	_, err = hasher.Write(slice1)
    46  	if err != nil {
    47  		panic(fmt.Sprintf("[ doubleSliceHash ] Hash write error: %s", err.Error()))
    48  	}
    49  	_, err = hasher.Write(slice2)
    50  	if err != nil {
    51  		panic(fmt.Sprintf("[ doubleSliceHash ] Hash write error: %s", err.Error()))
    52  	}
    53  
    54  	return hasher.Sum(nil)
    55  }
    56  
    57  func (mh *merkleHelper) pulseHash(pulse *core.Pulse) []byte {
    58  	pulseNumberHash := mh.leafHasher.Hash(pulse.PulseNumber.Bytes())
    59  	entropyHash := mh.leafHasher.Hash(pulse.Entropy[:])
    60  
    61  	return mh.doubleSliceHash(pulseNumberHash, entropyHash)
    62  }
    63  
    64  func (mh *merkleHelper) nodeInfoHash(pulseHash, stateHash []byte) []byte {
    65  	return mh.doubleSliceHash(pulseHash, stateHash)
    66  }
    67  
    68  func (mh *merkleHelper) nodeHash(nodeSignature, nodeInfoHash []byte) []byte {
    69  	nodeSignatureHash := mh.leafHasher.Hash(nodeSignature)
    70  	return mh.doubleSliceHash(nodeSignatureHash, nodeInfoHash)
    71  }
    72  
    73  func (mh *merkleHelper) bucketEntryHash(entryIndex uint32, nodeHash []byte) []byte {
    74  	entryIndexHash := mh.leafHasher.Hash(utils.UInt32ToBytes(entryIndex))
    75  	return mh.doubleSliceHash(entryIndexHash, nodeHash)
    76  }
    77  
    78  func (mh *merkleHelper) bucketInfoHash(role core.StaticRole, nodeCount uint32) []byte {
    79  	roleHash := mh.leafHasher.Hash(utils.UInt32ToBytes(uint32(role)))
    80  	nodeCountHash := mh.leafHasher.Hash(utils.UInt32ToBytes(nodeCount))
    81  	return mh.doubleSliceHash(roleHash, nodeCountHash)
    82  }
    83  
    84  func (mh *merkleHelper) bucketHash(bucketInfoHash, bucketEntryHash []byte) []byte {
    85  	return mh.doubleSliceHash(bucketInfoHash, bucketEntryHash)
    86  }
    87  
    88  func (mh *merkleHelper) globuleInfoHash(prevCloudHash []byte, globuleID, nodeCount uint32) []byte {
    89  	reservedHash := mh.leafHasher.Hash(utils.UInt32ToBytes(reserved))
    90  	globuleIDHash := mh.leafHasher.Hash(utils.UInt32ToBytes(globuleID))
    91  	nodeCountHash := mh.leafHasher.Hash(utils.UInt32ToBytes(nodeCount))
    92  
    93  	return mh.doubleSliceHash(
    94  		mh.doubleSliceHash(reservedHash, prevCloudHash),
    95  		mh.doubleSliceHash(globuleIDHash, nodeCountHash),
    96  	)
    97  }
    98  
    99  func (mh *merkleHelper) globuleHash(globuleInfoHash, globuleNodeRoot []byte) []byte {
   100  	return mh.doubleSliceHash(globuleInfoHash, globuleNodeRoot)
   101  }