SimpleSerialize (SSZ)¶
Table of contents¶
- Constants
- Typing
- Basic types
- Composite types
- Variable-size and fixed-size
- Byte
- Aliases
- Default values
- Illegal types
- Serialization
uintN
boolean
Bitvector[N]
Bitlist[N]
- Vectors, containers, lists
- Union
- Deserialization
- Merkleization
- Merkleization helper functions
- Chunking
- merkle_root
- merkle_proof and verify_merkle_proof
- Weber-optimized Merkleization
- Summaries and expansions
- Summary functions
- Expansion functions
- Implementations
- JSON mapping
- Convention
- Example
- Well-formedness validation
Constants¶
Name | Value | Description |
---|---|---|
BYTES_PER_CHUNK |
32 |
Number of bytes per chunk. |
BYTES_PER_LENGTH_OFFSET |
4 |
Number of bytes per serialized length offset. |
BITS_PER_BYTE |
8 |
Number of bits per byte. |
Typing¶
Basic types¶
uintN
:N
-bit unsigned integer (whereN in [8, 16, 32, 64, 128, 256]
)byte
: 8-bit opaque data container, equivalent in serialization and hashing touint8
boolean
:True
orFalse
Composite types¶
- container: ordered heterogeneous collection of values
- python dataclass notation with key-type pairs, e.g.
- vector: ordered fixed-length homogeneous collection, with
N
values- notation
Vector[type, N]
, e.g.Vector[uint64, N]
- notation
- list: ordered variable-length homogeneous collection, limited to
N
values- notation
List[type, N]
, e.g.List[uint64, N]
- notation
- bitvector: ordered fixed-length collection of
boolean
values, withN
bits- notation
Bitvector[N]
- notation
- bitlist: ordered variable-length collection of
boolean
values, limited toN
bits- notation
Bitlist[N]
- notation
- union: union type containing one of the given subtypes
- notation
Union[type_0, type_1, ...]
, e.g.union[None, uint64, uint32]
- notation
Note: Both Vector[boolean, N]
and Bitvector[N]
are valid, yet distinct due to their different serialization requirements. Similarly, both List[boolean, N]
and Bitlist[N]
are valid, yet distinct. Generally Bitvector[N]
/Bitlist[N]
are preferred because of their serialization efficiencies.
Variable-size and fixed-size¶
We recursively define "variable-size" types to be lists, unions, Bitlist
and all types that contain a variable-size type. All other types are said to be "fixed-size".
Byte¶
Although the SSZ serialization of byte
is equivalent to that of uint8
, the former is used for opaque data while the latter is intended as a number.
Aliases¶
For convenience we alias:
bit
toboolean
BytesN
andByteVector[N]
toVector[byte, N]
(this is not a basic type)ByteList[N]
toList[byte, N]
Aliases are semantically equivalent to their underlying type and therefore share canonical representations both in SSZ and in related formats.
Default values¶
Assuming a helper function default(type)
which returns the default value for type
, we can recursively define the default value for all types.
Type | Default Value |
---|---|
uintN |
0 |
boolean |
False |
Container |
[default(type) for type in container] |
Vector[type, N] |
[default(type)] * N |
Bitvector[N] |
[False] * N |
List[type, N] |
[] |
Bitlist[N] |
[] |
Union[type_0, type_1, ...] |
default(type_0) |
is_zero
¶
An SSZ object is called zeroed (and thus, is_zero(object)
returns true) if it is equal to the default value for that type.
Illegal types¶
- Empty vector types (
Vector[type, 0]
,Bitvector[0]
) are illegal. - Containers with no fields are illegal.
- The
None
type option in aUnion
type is only legal as the first option (i.e. with index zero).
Serialization¶
We recursively define the serialize
function which consumes an object value
(of the type specified) and returns a bytestring of type bytes
.
Note: In the function definitions below (serialize
, hash_tree_root
, is_variable_size
, etc.) objects implicitly carry their type.
uintN
¶
boolean
¶
Bitvector[N]
¶
Bitlist[N]
¶
Note that from the offset coding, the length (in bytes) of the bitlist is known. An additional 1
bit is added to the end, at index e
where e
is the length of the bitlist (not the limit), so that the length in bits will also be known.
Vectors, containers, lists¶
Union¶
A value
as Union[T...]
type has properties value.value
with the contained value, and value.selector
which indexes the selected Union
type option T
.
A Union
:
- May have multiple selectors with the same type.
- Should not use selectors above 127 (i.e. highest bit is set), these are reserved for backwards compatible extensions.
- Must have at least 1 type option.
- May have None
as first type option, i.e. selector == 0
- Must have at least 2 type options if the first is None
- Is always considered a variable-length type, even if all type options have an equal fixed-length.
Deserialization¶
Because serialization is an injective function (i.e. two distinct objects of the same type will serialize to different values) any bytestring has at most one object it could deserialize to.
Deserialization can be implemented using a recursive algorithm. The deserialization of basic objects is easy, and from there we can find a simple recursive algorithm for all fixed-size objects. For variable-size objects we have to do one of the following depending on what kind of object it is:
- Vector/list of a variable-size object: The serialized data will start with offsets of all the serialized objects (
BYTES_PER_LENGTH_OFFSET
bytes each). - Using the first offset, we can compute the length of the list (divide by
BYTES_PER_LENGTH_OFFSET
), as it gives us the total number of bytes in the offset data. - The size of each object in the vector/list can be inferred from the difference of two offsets. To get the size of the last object, the total number of bytes has to be known (it is not generally possible to deserialize an SSZ object of unknown length)
- Containers follow the same principles as vectors, with the difference that there may be fixed-size objects in a container as well. This means the
fixed_parts
data will contain offsets as well as fixed-size objects. - In the case of bitlists, the length in bits cannot be uniquely inferred from the number of bytes in the object. Because of this, they have a bit at the end that is always set. This bit has to be used to infer the size of the bitlist in bits.
- In the case of unions, the first byte of the deserialization scope is deserialized as type selector, the remainder of the scope is deserialized as the selected type.
Note that deserialization requires hardening against invalid inputs. A non-exhaustive list:
- Offsets: out of order, out of range, mismatching minimum element size.
- Scope: Extra unused bytes, not aligned with element size.
- More elements than a list limit allows. Part of enforcing consensus.
- An out-of-bounds selected index in an
Union
Efficient algorithms for computing this object can be found in the implementations.
Merkleization¶
We first define helper functions:
Merkleization helper functions¶
Chunking¶
merkle_root¶
merkle_proof and verify_merkle_proof¶
Weber-optimized Merkleization¶
Weber introduces several optimizations to the standard Merkleization process:
Summaries and expansions¶
SSZ defines functions for producing "summaries" (compact representations sufficient for verifying Merkle proofs against) and "expansions" (full reconstructions of objects from minimal information).
Summary functions¶
Expansion functions¶
Implementations¶
Reference implementations of SSZ exist in multiple languages:
- Python: py-ssz
- Go: go-ssz
- Rust: ethereum_ssz
- Java: tech.pegasys.teku.ssz
- JavaScript: ssz
- Nim: nim-ssz
- C#: ssz.net
Web-based tools: * SSZ Playground
JSON mapping¶
For debugging and interoperability with other systems like JSON-RPC, SSZ defines a convention for mapping SSZ objects to and from JSON.
Convention¶
The mapping follows these rules:
boolean
values map to the JSON boolean typeuintN
values map to JSON strings with decimal representation (to support large integers)Bytes32
,Vector[byte, N]
map to JSON strings with 0x-prefixed hexadecimal encodingList
andVector
values map to JSON arraysContainer
values map to JSON objects with fields as propertiesUnion
values map to JSON objects with a selectedtype
key and avalue
key
Example¶
JSON representation:
Well-formedness validation¶
When parsing JSON into SSZ objects, implementations should:
- Validate types match the expected SSZ schema
- Validate that numeric values are within range for their types
- Validate list lengths against maximum sizes
- Apply strict parsing for hexadecimal strings (proper format and length)