hasher

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: Apache-2.0 Imports: 8 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetZeroHash added in v1.1.1

func GetZeroHash(depth int) []byte

GetZeroHash returns the precomputed zero hash at the given merkle tree depth.

func GetZeroHashLevel added in v1.1.1

func GetZeroHashLevel(hash string) (int, bool)

GetZeroHashLevel returns the merkle tree depth level for a known zero hash. Returns the level and true if the hash is a recognized zero hash, or 0 and false otherwise.

func GetZeroHashLevelBytes added in v1.3.0

func GetZeroHashLevelBytes(hash []byte) (int, bool)

GetZeroHashLevelBytes returns the merkle tree depth level for a known zero hash. Returns the level and true if the hash is a recognized zero hash, or 0 and false otherwise.

func GetZeroHashes added in v1.1.1

func GetZeroHashes() [65][32]byte

GetZeroHashes returns the full array of precomputed zero hashes for all 65 merkle tree depth levels.

func ParseBitlist

func ParseBitlist(dst, buf []byte) ([]byte, uint64)

ParseBitlist decodes an SSZ-encoded bitlist into its raw bit representation.

SSZ bitlists include a mandatory termination bit: a single `1` bit appended immediately after the final data bit, then padded to a full byte. The position of this termination bit defines the logical length of the bitlist.

This function performs the inverse transformation:

  1. Identify the termination bit in the final byte and compute the logical bitlist length (`size`).
  2. Clear the termination bit, leaving only the actual data bits.
  3. Trim any trailing zero bytes introduced by SSZ padding.
  4. Return the compact raw bitlist (no termination bit, no padding) together with its logical size.

The returned `[]byte` contains the data bits packed little-endian in each byte, and `size` is the exact number of meaningful bits in that raw bitlist.

func ParseBitlistWithHasher added in v1.2.0

func ParseBitlistWithHasher(hw sszutils.HashWalker, buf []byte) ([]byte, uint64)

ParseBitlistWithHasher decodes an SSZ-encoded bitlist using the hasher's internal buffer to avoid allocations. It returns the raw bit data and the logical bit count, same as ParseBitlist.

IMPORTANT: The returned bitlist slice references the hasher's internal buffer spare capacity. It must be consumed (e.g. passed to AppendBytes32) before any operation that may grow the buffer, as a reallocation would invalidate it.

func WithDefaultHasher added in v1.1.1

func WithDefaultHasher(fn func(hh sszutils.HashWalker) error) error

WithDefaultHasher acquires a Hasher from the FastHasherPool, passes it to fn, and returns it to the pool when done. This is a convenience wrapper for one-off hashing operations.

Types

type HashFn

type HashFn func(dst []byte, input []byte) error

HashFn is a function that hashes pairs of 32-byte chunks from input into dst. It processes the input as consecutive 64-byte pairs and writes each 32-byte hash result into dst.

func NativeHashWrapper

func NativeHashWrapper(hashFn hash.Hash) HashFn

NativeHashWrapper wraps a hash.Hash function into a HashFn

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

Hasher is a utility tool to hash SSZ structs

func NewHasher

func NewHasher() *Hasher

NewHasher creates a new Hasher with the default sha256 hash function.

func NewHasherWithHash

func NewHasherWithHash(hh hash.Hash) *Hasher

NewHasherWithHash creates a new Hasher with a custom hash.Hash function.

func NewHasherWithHashFn

func NewHasherWithHashFn(hh HashFn) *Hasher

NewHasherWithHashFn creates a new Hasher with a custom HashFn function.

func (*Hasher) Append

func (h *Hasher) Append(i []byte)

Append appends raw bytes to the buffer without padding.

func (*Hasher) AppendBool

func (h *Hasher) AppendBool(b bool)

AppendBool appends a single byte (0 or 1) to the buffer without padding.

func (*Hasher) AppendBytes32

func (h *Hasher) AppendBytes32(b []byte)

AppendBytes32 appends b to the buffer, right-padding with zeros to align to 32 bytes.

func (*Hasher) AppendUint8

func (h *Hasher) AppendUint8(i uint8)

AppendUint8 appends a uint8 to the buffer without padding.

func (*Hasher) AppendUint16

func (h *Hasher) AppendUint16(i uint16)

AppendUint16 appends a little-endian uint16 to the buffer without padding.

func (*Hasher) AppendUint32

func (h *Hasher) AppendUint32(i uint32)

AppendUint32 appends a little-endian uint32 to the buffer without padding.

func (*Hasher) AppendUint64

func (h *Hasher) AppendUint64(i uint64)

AppendUint64 appends a little-endian uint64 to the buffer without padding.

func (*Hasher) Collapse added in v1.3.0

func (h *Hasher) Collapse()

Collapse hints the hasher to collapse accumulated chunks in the current layer if the batch threshold is reached. This is a no-op for non-incremental layers or when no layer is active.

func (*Hasher) CurrentIndex added in v1.3.0

func (h *Hasher) CurrentIndex() int

CurrentIndex returns the current buffer position without pushing a layer.

func (*Hasher) FillUpTo32

func (h *Hasher) FillUpTo32()

FillUpTo32 pads the buffer with zero bytes to align to a 32-byte boundary.

func (*Hasher) Hash

func (h *Hasher) Hash() []byte

Hash returns the last 32 bytes of the buffer.

func (*Hasher) HashRoot

func (h *Hasher) HashRoot() (res [32]byte, err error)

HashRoot returns the final 32-byte hash root, or an error if the buffer is not exactly 32 bytes.

func (*Hasher) Index

func (h *Hasher) Index() int

Index returns the current buffer position and pushes a non-incremental layer. This is for legacy/external code that doesn't use StartTree. The non-incremental layer blocks Collapse on this scope but is properly popped by Merkleize. Collapse on the parent layer is unaffected — it only sees completed child roots after the child's Merkleize pops this layer.

func (*Hasher) Merkleize

func (h *Hasher) Merkleize(indx int)

Merkleize computes the binary merkle root of the buffer from indx onwards and replaces that region with the 32-byte root. Pops the matching layer if one exists.

func (*Hasher) MerkleizeProgressive

func (h *Hasher) MerkleizeProgressive(indx int)

MerkleizeProgressive computes the progressive merkle root of the buffer from indx onwards. If incremental progressive data was accumulated via Collapse, it is finalized; otherwise the standard recursive algorithm is used.

func (*Hasher) MerkleizeProgressiveWithActiveFields

func (h *Hasher) MerkleizeProgressiveWithActiveFields(indx int, activeFields []byte)

MerkleizeProgressiveWithActiveFields computes the progressive merkle root from indx and mixes in the active fields bitvector.

func (*Hasher) MerkleizeProgressiveWithMixin

func (h *Hasher) MerkleizeProgressiveWithMixin(indx int, num uint64)

MerkleizeProgressiveWithMixin computes the progressive merkle root from indx and mixes in num as the list length.

func (*Hasher) MerkleizeWithMixin

func (h *Hasher) MerkleizeWithMixin(indx int, num, limit uint64)

MerkleizeWithMixin computes the binary merkle root from indx with the given limit, then mixes in num as the list length. Pops the matching layer if one exists.

func (*Hasher) PutBitlist

func (h *Hasher) PutBitlist(bb []byte, maxSize uint64)

PutBitlist appends an SSZ-encoded bitlist, merkleizes its content, and mixes in the bit count with the given maxSize limit.

func (*Hasher) PutBool

func (h *Hasher) PutBool(b bool)

PutBool appends a boolean as a 32-byte zero-padded chunk.

func (*Hasher) PutBytes

func (h *Hasher) PutBytes(b []byte)

PutBytes appends b as a 32-byte chunk. If b exceeds 32 bytes, the content is merkleized in-place to a single root without interacting with the layer stack.

func (*Hasher) PutProgressiveBitlist

func (h *Hasher) PutProgressiveBitlist(bb []byte)

PutProgressiveBitlist appends an SSZ-encoded bitlist and merkleizes it using the progressive algorithm with a length mixin.

func (*Hasher) PutRootVector

func (h *Hasher) PutRootVector(b [][]byte, maxCapacity ...uint64) error

PutRootVector appends an array of 32-byte roots and merkleizes them. If maxCapacity is provided, the result includes a length mixin.

func (*Hasher) PutUint8

func (h *Hasher) PutUint8(i uint8)

PutUint8 appends a uint8 as a 32-byte zero-padded chunk.

func (*Hasher) PutUint16

func (h *Hasher) PutUint16(i uint16)

PutUint16 appends a little-endian uint16 as a 32-byte zero-padded chunk.

func (*Hasher) PutUint32

func (h *Hasher) PutUint32(i uint32)

PutUint32 appends a little-endian uint32 as a 32-byte zero-padded chunk.

func (*Hasher) PutUint64

func (h *Hasher) PutUint64(i uint64)

PutUint64 appends a little-endian uint64 as a 32-byte zero-padded chunk.

func (*Hasher) PutUint64Array

func (h *Hasher) PutUint64Array(b []uint64, maxCapacity ...uint64)

PutUint64Array appends an array of uint64 values and merkleizes them. If maxCapacity is provided, the result includes a length mixin.

func (*Hasher) Reset

func (h *Hasher) Reset()

Reset clears the buffer and layer stack for reuse.

func (*Hasher) StartTree added in v1.3.0

func (h *Hasher) StartTree(treeType sszutils.TreeType) int

StartTree opens a new SSZ object scope and returns the buffer index. TreeTypeBinary/Progressive: pushes an incremental layer (supports Collapse). TreeTypeNone: pushes a non-incremental layer (Collapse is a no-op on this scope).

func (*Hasher) WithTemp

func (h *Hasher) WithTemp(fn func(tmp []byte) []byte)

WithTemp provides access to the hasher's temporary buffer via a callback. The callback receives the tmp buffer and must return a (possibly reallocated) replacement.

type HasherPool

type HasherPool struct {
	HashFn HashFn
	// contains filtered or unexported fields
}

HasherPool may be used for pooling Hashers for similarly typed SSZs.

var DefaultHasherPool HasherPool

DefaultHasherPool is a default hasher pool

var FastHasherPool HasherPool = HasherPool{
	HashFn: hashtree.HashByteSlice,
}

FastHasherPool is the fast hasher pool that uses the hashtree library if cgo is enabled

func (*HasherPool) Get

func (hh *HasherPool) Get() *Hasher

Get acquires a Hasher from the pool.

func (*HasherPool) Put

func (hh *HasherPool) Put(h *Hasher)

Put releases the Hasher to the pool.

Jump to

Keyboard shortcuts

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