Module packing

Module packing 

Source
Expand description

Shared utilities for packing and unpacking values in EVM storage slots.

This module provides helper functions for bit-level manipulation of storage slots, enabling efficient packing of multiple small values into single 32-byte slots.

Packing only applies to primitive types where LAYOUT::Bytes(count) && count < 32. Non-primitives (structs, fixed-size arrays, dynamic types) have LAYOUT = Layout::Slot.

§Solidity Compatibility

This implementation matches Solidity’s value packing convention:

  • Values are right-aligned within their byte range
  • Types smaller than 32 bytes can pack multiple per slot when dimensions align

Structs§

FieldLocation
Location information for a packed field within a storage slot.
PackedSlot
A helper struct to support packing elements into a single slot. Represents an in-memory storage slot value.

Functions§

calc_element_loc
Calculate the element location within a slot for an array element at index idx.
calc_element_offset
Calculate the byte offset within a slot for an array element at index idx.
calc_element_slot
Calculate which slot an array element at index idx starts in.
calc_packed_slot_count
Calculate the total number of slots needed for an array.
create_element_mask
Create a bit mask for a value of the given byte size.
extract_packed_value
Extract a packed value from a storage slot at a given byte offset.
gen_word_fromtest-utils
Test helper function for constructing EVM words from hex string literals.
insert_packed_value
Insert a packed value into a storage slot at a given byte offset.
zero_packed_value
Zero out a packed value in a storage slot at a given byte offset.