bytesutil

package
v0.0.0-...-0064a47 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package bytesutil defines helper methods for converting integers to byte slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigIntToLittleEndianBytes

func BigIntToLittleEndianBytes(bigInt *big.Int) []byte

BigIntToLittleEndianBytes takes a big integer and returns its bytes stored as little-endian

func Bytes1

func Bytes1(x uint64) []byte

Bytes1 returns integer x to bytes in little-endian format, x.to_bytes(1, 'little').

func Bytes2

func Bytes2(x uint64) []byte

Bytes2 returns integer x to bytes in little-endian format, x.to_bytes(2, 'little').

func Bytes3

func Bytes3(x uint64) []byte

Bytes3 returns integer x to bytes in little-endian format, x.to_bytes(3, 'little').

func Bytes32

func Bytes32(x uint64) []byte

Bytes32 returns integer x to bytes in little-endian format, x.to_bytes(32, 'little').

func Bytes4

func Bytes4(x uint64) []byte

Bytes4 returns integer x to bytes in little-endian format, x.to_bytes(4, 'little').

func Bytes8

func Bytes8(x uint64) []byte

Bytes8 returns integer x to bytes in little-endian format, x.to_bytes(8, 'little').

func BytesToEpochBigEndian

func BytesToEpochBigEndian(b []byte) primitives.Epoch

BytesToEpochBigEndian conversion.

func BytesToSlotBigEndian

func BytesToSlotBigEndian(b []byte) primitives.Slot

BytesToSlotBigEndian conversion.

func BytesToUint64BigEndian

func BytesToUint64BigEndian(b []byte) uint64

BytesToUint64BigEndian conversion. Returns 0 if empty bytes or byte slice with length less than 8.

func ClearBit

func ClearBit(b []byte, i int) []byte

ClearBit clears the index `i` of bitlist `b`. Returns the original bitlist if the index `i` is out of range.

func DecodeHexWithLength

func DecodeHexWithLength(s string, length int) ([]byte, error)

DecodeHexWithLength takes a string and a length in bytes, and validates whether the string is a hex and has the correct length.

func DecodeHexWithMaxLength

func DecodeHexWithMaxLength(s string, maxLength int) ([]byte, error)

DecodeHexWithMaxLength takes a string and a length in bytes, and validates whether the string is a hex and has the correct length.

func EpochToBytesBigEndian

func EpochToBytesBigEndian(i primitives.Epoch) []byte

EpochToBytesBigEndian conversion.

func EpochToBytesLittleEndian

func EpochToBytesLittleEndian(i primitives.Epoch) []byte

EpochToBytesLittleEndian conversion.

func FromBool

func FromBool(x bool) byte

FromBool is a convenience method for converting a bool to a byte. This method will use the first bit to generate the returned value.

func FromBytes2

func FromBytes2(x []byte) uint16

FromBytes2 returns an integer which is stored in the little-endian format(2, 'little') from a byte array.

func FromBytes4

func FromBytes4(x []byte) uint64

FromBytes4 returns an integer which is stored in the little-endian format(4, 'little') from a byte array.

func FromBytes48

func FromBytes48(x [48]byte) []byte

FromBytes48 is a convenience method for converting a fixed-size byte array to a byte slice.

func FromBytes48Array

func FromBytes48Array(x [][48]byte) [][]byte

FromBytes48Array is a convenience method for converting an array of fixed-size byte arrays to an array of byte slices.

func FromBytes8

func FromBytes8(x []byte) uint64

FromBytes8 returns an integer which is stored in the little-endian format(8, 'little') from a byte array.

func HighestBitIndex

func HighestBitIndex(b []byte) (int, error)

HighestBitIndex returns the index of the highest bit set from bitlist `b`.

func HighestBitIndexAt

func HighestBitIndexAt(b []byte, index int) (int, error)

HighestBitIndexAt returns the index of the highest bit set from bitlist `b` that is at `index` (inclusive).

func IsHex

func IsHex(b []byte) bool

IsHex checks whether the byte array is a hex number prefixed with '0x'.

func IsRoot

func IsRoot(root []byte) bool

IsRoot checks whether the byte array is a root.

func IsValidRoot

func IsValidRoot(root []byte) bool

IsValidRoot checks whether the byte array is a valid root.

func LittleEndianBytesToBigInt

func LittleEndianBytesToBigInt(bytes []byte) *big.Int

LittleEndianBytesToBigInt takes bytes of a number stored as little-endian and returns a big integer

func MakeEmptyBitlists

func MakeEmptyBitlists(i int) []byte

MakeEmptyBitlists returns an empty bitlist with input size `i`.

func PadTo

func PadTo(b []byte, size int) []byte

PadTo pads a byte slice to the given size. If the byte slice is larger than the given size, the original slice is returned.

func ReverseByteOrder

func ReverseByteOrder(input []byte) []byte

ReverseByteOrder Switch the endianness of a byte slice by reversing its order. This function does not modify the actual input bytes.

func ReverseBytes32Slice

func ReverseBytes32Slice(arr [][32]byte) [][32]byte

ReverseBytes32Slice will reverse the provided slice's order.

func SafeCopy2d32Bytes

func SafeCopy2d32Bytes(ary [][32]byte) [][32]byte

SafeCopy2d32Bytes will copy and return a non-nil 2d byte slice, otherwise it returns nil.

func SafeCopy2dBytes

func SafeCopy2dBytes(ary [][]byte) [][]byte

SafeCopy2dBytes will copy and return a non-nil 2d byte slice, otherwise it returns nil.

func SafeCopy2dHexUtilBytes

func SafeCopy2dHexUtilBytes(ary []hexutil.Bytes) [][]byte

SafeCopy2dHexUtilBytes will copy and return a non-nil 2d hex util byte slice, otherwise it returns nil.

func SafeCopyBytes

func SafeCopyBytes(cp []byte) []byte

SafeCopyBytes will copy and return a non-nil byte slice, otherwise it returns nil.

func SafeCopyRootAtIndex

func SafeCopyRootAtIndex(input [][]byte, idx uint64) ([]byte, error)

SafeCopyRootAtIndex takes a copy of an 32-byte slice in a slice of byte slices. Returns error if index out of range.

func SetBit

func SetBit(b []byte, i int) []byte

SetBit sets the index `i` of bitlist `b` to 1. It grows and returns a longer bitlist with 1 set if index `i` is out of range.

func SlotToBytesBigEndian

func SlotToBytesBigEndian(i primitives.Slot) []byte

SlotToBytesBigEndian conversion.

func ToBool

func ToBool(x byte) bool

ToBool is a convenience method for converting a byte to a bool. This method will use the first bit of the 0 byte to generate the returned value.

func ToBytes

func ToBytes(x uint64, length int) []byte

ToBytes returns integer x to bytes in little-endian format at the specified length. Spec defines similar method uint_to_bytes(n: uint) -> bytes, which is equivalent to ToBytes(n, 8).

func ToBytes20

func ToBytes20(x []byte) [20]byte

ToBytes20 is a convenience method for converting a byte slice to a fix sized 20 byte array. This method will truncate the input if it is larger than 20 bytes.

func ToBytes32

func ToBytes32(x []byte) [32]byte

ToBytes32 is a convenience method for converting a byte slice to a fix sized 32 byte array. This method will truncate the input if it is larger than 32 bytes.

func ToBytes4

func ToBytes4(x []byte) [4]byte

ToBytes4 is a convenience method for converting a byte slice to a fix sized 4 byte array. This method will truncate the input if it is larger than 4 bytes.

func ToBytes48

func ToBytes48(x []byte) [48]byte

ToBytes48 is a convenience method for converting a byte slice to a fix sized 48 byte array. This method will truncate the input if it is larger than 48 bytes.

func ToBytes48Array

func ToBytes48Array(x [][]byte) [][48]byte

ToBytes48Array is a convenience method for converting an array of byte slices to an array of fixed-sized byte arrays.

func ToBytes64

func ToBytes64(x []byte) [64]byte

ToBytes64 is a convenience method for converting a byte slice to a fix sized 64 byte array. This method will truncate the input if it is larger than 64 bytes.

func ToBytes96

func ToBytes96(x []byte) [96]byte

ToBytes96 is a convenience method for converting a byte slice to a fix sized 96 byte array. This method will truncate the input if it is larger than 96 bytes.

func ToLowInt64

func ToLowInt64(x []byte) int64

ToLowInt64 returns the lowest 8 bytes interpreted as little endian.

func Trunc

func Trunc(x []byte) []byte

Trunc truncates the byte slices to 6 bytes.

func Uint256ToSSZBytes

func Uint256ToSSZBytes(num string) ([]byte, error)

Uint256ToSSZBytes takes a string representation of uint256 and returns its bytes stored as little-endian

func Uint32ToBytes4

func Uint32ToBytes4(i uint32) [4]byte

Uint32ToBytes4 is a convenience method for converting uint32 to a fix sized 4 byte array in big endian order. Returns 4 byte array.

func Uint64ToBytesBigEndian

func Uint64ToBytesBigEndian(i uint64) []byte

Uint64ToBytesBigEndian conversion.

func Uint64ToBytesLittleEndian

func Uint64ToBytesLittleEndian(i uint64) []byte

Uint64ToBytesLittleEndian conversion.

func Uint64ToBytesLittleEndian32

func Uint64ToBytesLittleEndian32(i uint64) []byte

Uint64ToBytesLittleEndian32 conversion of a uint64 to a fix sized 32 byte array in little endian order. Returns 32 byte array.

func ZeroRoot

func ZeroRoot(root []byte) bool

ZeroRoot returns whether or not a root is of proper length and non-zero hash.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL