Files
evm/utils/bytes.go
T
Ceyhun OnurandGitHub 19a0e5bf99 move predicate package and merge with predicate utils (#907)
* add get predicate result bytes helpers

* move predicate package and merge with predicate utils

* update readme

* refactor imports & utils

* typed errs

* add comment

* fix import name

* fix stutter & same var names as pkg name

* rename functions

* fix reviews

* more rename
2023-10-02 16:55:55 -04:00

45 lines
1.0 KiB
Go

// (c) 2021-2022, Ava Labs, 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
}