bitfields

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: MIT Imports: 3 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BitIndex

func BitIndex(v byte) (out uint64)

Get index of left-most 1 bit. 0 (incl.) to 8 (excl.)

func BitlistCheck

func BitlistCheck(b []byte, limit uint64) error

Helper function to implement Bitlist with. It checks if:

  1. the raw bitlist is not empty, there must be a 1 bit to determine the length.
  2. the bitlist has a leading 1 bit in the last byte to determine the length with.
  3. if b has no more than given limit in bits.

func BitlistCheckByteLen

func BitlistCheckByteLen(byteLen uint64, bitLimit uint64) error

func BitlistCheckLastByte

func BitlistCheckLastByte(last byte, limit uint64) error

func BitlistLen

func BitlistLen(b []byte) uint64

Returns the length of the bitlist. And although strictly speaking invalid. a sane default is returned for:

  • an empty raw bitlist: a default 0 bitlist will be of length 0 too.
  • a bitlist with a leading 0 byte: return the bitlist raw bit length, excluding the last byte (As if it was full 0 padding).

func BitlistOnesCount added in v0.1.3

func BitlistOnesCount(v []byte) uint64

Counts the bits set to 1, excluding the delimiter bit.

func BitvectorCheck

func BitvectorCheck(b []byte, n uint64) error

Helper function to implement Bitvector with. It checks if:

  1. b has the same amount of bytes as necessary for n bits.
  2. unused bits in b are 0

func BitvectorCheckByteLen

func BitvectorCheckByteLen(byteLen uint64, bitLength uint64) error

func BitvectorCheckLastByte

func BitvectorCheckLastByte(last byte, n uint64) error

func BitvectorOnesCount added in v0.1.3

func BitvectorOnesCount(v []byte) uint64

Counts the bits set to 1. Assumes the bitvector with length not a multiple of 8 has trailing zero bits.

func Covers added in v0.1.3

func Covers(af []byte, bf []byte) (bool, error)

Returns true if bf only has bits set to 1 that bitfield af also has set to 1 For bitlists and bitvectors, the trailing part is always the same if the bitlength is the same. So the result is independent of having a delimiting bit or not.

func GetBit

func GetBit(b []byte, i uint64) bool

Helper function to implement Bitfields with. Assumes i is a valid bit-index to retrieve a bit from bytes b.

func IsZeroBitlist

func IsZeroBitlist(b []byte) bool

Checks if the bitList is fully zeroed (except the leading bit)

func SetBit

func SetBit(b []byte, i uint64, v bool)

Helper function to implement Bitfields with. Assumes i is a valid bit-index to set a bit within bytes b.

Types

type Bitfield

type Bitfield interface {
	Get(i uint64) bool
	Set(i uint64, v bool)
}

General base interface for Bitlists and Bitvectors Note for Bitfields to work with the SSZ functionality:

  • Bitlists need to be of kind []byte (packed bits, incl delimiter bit)
  • Bitvectors need to be of kind [N]byte (packed bits)

type CheckedBitfield

type CheckedBitfield interface {
	Check() error
}

bitfields implementing this can be checked to be of a valid or not. Useful for untrusted bitfields. See BitlistCheck and BitvectorCheck to easily implement the validity checks.

type SizedBits

type SizedBits interface {
	BitLen() uint64
}

the exact bitlength can be determined for bitfields implementing this method.

Jump to

Keyboard shortcuts

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