mirror of
https://github.com/luxfi/evm.git
synced 2026-09-24 00:27:44 +00:00
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
// (c) 2021-2022, Hanzo Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package utils
|
|
|
|
import "github.com/ethereum/go-ethereum/common"
|
|
|
|
// IncrOne increments bytes value by one
|
|
func IncrOne(bytes []byte) {
|
|
index := len(bytes) - 1
|
|
for index >= 0 {
|
|
if bytes[index] < 255 {
|
|
bytes[index]++
|
|
break
|
|
} else {
|
|
bytes[index] = 0
|
|
index--
|
|
}
|
|
}
|
|
}
|
|
|
|
// HashSliceToBytes serializes a []common.Hash into a tightly packed byte array.
|
|
func HashSliceToBytes(hashes []common.Hash) []byte {
|
|
bytes := make([]byte, common.HashLength*len(hashes))
|
|
for i, hash := range hashes {
|
|
copy(bytes[i*common.HashLength:], hash[:])
|
|
}
|
|
return bytes
|
|
}
|
|
|
|
// BytesToHashSlice packs [b] into a slice of hash values with zero padding
|
|
// to the right if the length of b is not a multiple of 32.
|
|
func BytesToHashSlice(b []byte) []common.Hash {
|
|
var (
|
|
numHashes = (len(b) + 31) / 32
|
|
hashes = make([]common.Hash, numHashes)
|
|
)
|
|
|
|
for i := range hashes {
|
|
start := i * common.HashLength
|
|
copy(hashes[i][:], b[start:])
|
|
}
|
|
return hashes
|
|
}
|