SimpleSerialize (SSZ)¶
- Constants
 - Typing
 - Basic types
 - Composite types
 - Variable-size and fixed-size
 - Byte
 - Aliases
 - Default values
 - Illegal types
 - Serialization
 uintNbooleanBitvector[N]Bitlist[N],ProgressiveBitlist- Vectors, containers, progressive containers, lists, progressive lists
 - Union
 - Compatible unions
 - Deserialization
 - Merkleization
 - Summaries and expansions
 - Implementations
 - JSON mapping
 
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 touint8boolean:TrueorFalse
Composite types¶
- container: ordered heterogeneous collection of values
 - python dataclass notation with key-type pairs, e.g.
 - progressive container [EIP-7495, currently unused]: ordered heterogeneous collection of values with stable Merkleization
 - python dataclass notation with key-type pairs, e.g.
  
 - vector: ordered fixed-length homogeneous collection, with 
Nvalues - notation 
Vector[type, N], e.g.Vector[uint64, N] - list: ordered variable-length homogeneous collection, limited to 
Nvalues - notation 
List[type, N], e.g.List[uint64, N] - progressive list [EIP-7916, currently unused]: ordered variable-length homogeneous collection, without limit
 - notation 
ProgressiveList[type], e.g.ProgressiveList[uint64] - bitvector: ordered fixed-length collection of 
booleanvalues, withNbits - notation 
Bitvector[N] - bitlist: ordered variable-length collection of 
booleanvalues, limited toNbits - notation 
Bitlist[N] - progressive bitlist [EIP-7916, currently unused]: ordered
  variable-length collection of 
booleanvalues, without limit - notation 
ProgressiveBitlist - union: union type containing one of the given subtypes
 - notation 
Union[type_0, type_1, ...], e.g.union[None, uint64, uint32] - compatible union [EIP-8016, currently unused]: union type containing one of the given subtypes with compatible Merkleization
 - notation 
CompatibleUnion({selector: type}), e.g.CompatibleUnion({1: Square, 2: Circle}) 
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, progressive lists, unions, compatible unions, bitlists, progressive bitlists, and all composite 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:
bittobooleanBytesNandByteVector[N]toVector[byte, N](this is not a basic type)ByteList[N]toList[byte, N]ProgressiveByteListtoProgressiveList[byte]
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] | 
ProgressiveContainer(active_fields) | 
[default(type) for type in progressive_container] | 
Vector[type, N] | 
[default(type)] * N | 
Bitvector[N] | 
[False] * N | 
List[type, N] | 
[] | 
ProgressiveList[type] | 
[] | 
Bitlist[N] | 
[] | 
ProgressiveBitlist | 
[] | 
Union[type_0, type_1, ...] | 
default(type_0) | 
CompatibleUnion({selector: type}) | 
n/a (error) | 
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.
 ProgressiveContainerwith no fields are illegal.ProgressiveContainerwith anactive_fieldsconfiguration of more than 256 entries are illegal.ProgressiveContainerwith anactive_fieldsconfiguration ending in0are illegal.ProgressiveContainerwith anactive_fieldsconfiguration with a different count of1than fields are illegal.- The 
Nonetype option in aUniontype is only legal as the first option (i.e. with index zero). CompatibleUnion({})without any type options are illegal.CompatibleUnion({selector: type})with a selector outsideuint8(1)throughuint8(127)are illegal.CompatibleUnion({selector: type})with a type option that has incompatible Merkleization with another type option are illegal.
Compatible Merkleization¶
- Types are compatible with themselves.
 byteis compatible withuint8and vice versa.Bitlist[N]are compatible if they share the same capacityN.Bitvector[N]are compatible if they share the same capacityN.List[type, N]are compatible iftypeis compatible and they share the same capacityN.Vector[type, N]are compatible iftypeis compatible and they share the same capacityN.ProgressiveList[type]are compatible iftypeis compatible.Containerare compatible if they share the same field names in the same order, and all field types are compatible.ProgressiveContainer(active_fields)are compatible if all1entries in both type'sactive_fieldscorrespond to fields with shared names and compatible types, and no other field name is shared across both types.CompatibleUnionare compatible with each other if all type options across bothCompatibleUnionare compatible.- All other types are incompatible.
 
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], ProgressiveBitlist¶
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, progressive containers, lists, progressive 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 
Noneas 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.
 
Compatible unions¶
A value as CompatibleUnion({selector: type}) has properties value.data
with the contained value, and value.selector which indexes the selected type
option.
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/progressive list of a variable-size object: The serialized data
  will start with offsets of all the serialized objects
  (
BYTES_PER_LENGTH_OFFSETbytes 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/progressive 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/progressive containers follow the same principles as vectors, with
  the difference that there may be fixed-size objects in a container/progressive
  container as well. This means the 
fixed_partsdata will contain offsets as well as fixed-size objects. - In the case of bitlists/progressive 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/compatible 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. - An out-of-bounds type selector in a 
CompatibleUnion. - Incomplete data in a 
CompatibleUnionwhere the input is shorter than required for the selected type. - Corrupted input in a 
CompatibleUnionwhere the data contains invalid values or malformed content. - Inner type validation failures in a 
CompatibleUnionwhere the deserialized data fails validation for the selected type. 
Efficient algorithms for computing this object can be found in the implementations.
Merkleization¶
We first define helper functions:
size_of(B), whereBis a basic type: the length, in bytes, of the serialized form of the basic type.chunk_count(type): calculate the amount of leaves for merkleization of the type.- all basic types: 
1 Bitlist[N]andBitvector[N]:(N + 255) // 256(dividing by chunk size, rounding up)List[B, N]andVector[B, N], whereBis a basic type:(N * size_of(B) + 31) // 32(dividing by chunk size, rounding up)List[C, N]andVector[C, N], whereCis a composite type:N- containers: 
len(fields) get_active_fields(value), wherevalueis of typeProgressiveContainer(active_fields): returnactive_fields.pack(values): Given ordered objects of the same basic type: 1. Serializevaluesinto bytes. 2. If not aligned to a multiple ofBYTES_PER_CHUNKbytes, right-pad with zeroes to the next multiple. 3. Partition the bytes intoBYTES_PER_CHUNK-byte chunks. 4. Return the chunks.pack_bits(bits): Given the bits of bitlist or bitvector, getbitfield_bytesby packing them in bytes and aligning to the start. The length-delimiting bit for bitlists is excluded. Then returnpack(bitfield_bytes).next_pow_of_two(i): get the next power of 2 ofi, if not already a power of 2, with 0 mapping to 1. Examples:0->1, 1->1, 2->2, 3->4, 4->4, 6->8, 9->16merkleize(chunks, limit=None): Given orderedBYTES_PER_CHUNK-byte chunks, merkleize the chunks, and return the root:- The merkleization depends on the effective input, which must be
    padded/limited:
- if no limit: pad the 
chunkswith zeroed chunks tonext_pow_of_two(len(chunks))(virtually for memory efficiency). - if 
limit >= len(chunks), pad thechunkswith zeroed chunks tonext_pow_of_two(limit)(virtually for memory efficiency). - if 
limit < len(chunks): do not merkleize, input exceeds limit. Raise an error instead. 
 - if no limit: pad the 
 - Then, merkleize the chunks (empty input is padded to 1 zero chunk):
- If 
1chunk: the root is the chunk itself. - If 
> 1chunks: merkleize as binary tree. 
 - If 
 merkleize_progressive(chunks, num_leaves=1): Given orderedBYTES_PER_CHUNK-byte chunks:- The merkleization depends on the number of input chunks and is defined
    recursively:
- If 
len(chunks) == 0: the root is a zero value,Bytes32(). - Otherwise: compute the root using 
hash(a, b) a: Recursively merkleize chunks beyondnum_leavesusingmerkleize_progressive(chunks[num_leaves:], num_leaves * 4).b: Merkleize the first up tonum_leaveschunks as a binary tree usingmerkleize(chunks[:num_leaves], num_leaves).
 - If 
 mix_in_active_fields: Given a Merkle rootrootand anactive_fieldsconfiguration returnhash(root, pack_bits(active_fields)). Note thatactive_fieldsis restricted to ≤ 256 bits.mix_in_length: Given a Merkle rootrootand a lengthlength("uint256"little-endian serialization) returnhash(root, length).mix_in_selector: Given a Merkle rootrootand a type selectorselector("uint8"serialization) returnhash(root, selector).
We now define Merkleization hash_tree_root(value) of an object value
recursively:
merkleize(pack(value))ifvalueis a basic object or a vector of basic objects.merkleize(pack_bits(value), limit=chunk_count(type))ifvalueis a bitvector.mix_in_length(merkleize(pack(value), limit=chunk_count(type)), len(value))ifvalueis a list of basic objects.mix_in_length(merkleize_progressive(pack(value)), len(value))ifvalueis a progressive list of basic objects.mix_in_length(merkleize(pack_bits(value), limit=chunk_count(type)), len(value))ifvalueis a bitlist.mix_in_length(merkleize_progressive(pack_bits(value)), len(value))ifvalueis a progressive bitlist.merkleize([hash_tree_root(element) for element in value])ifvalueis a vector of composite objects or a container.mix_in_active_fields(merkleize_progressive([hash_tree_root(element) for element in value]), get_active_fields(value))ifvalueis a progressive container.mix_in_length(merkleize([hash_tree_root(element) for element in value], limit=chunk_count(type)), len(value))ifvalueis a list of composite objects.mix_in_length(merkleize_progressive([hash_tree_root(element) for element in value]), len(value))ifvalueis a progressive list of composite objects.mix_in_selector(hash_tree_root(value.value), value.selector)ifvalueis of union type, andvalue.valueis notNonemix_in_selector(Bytes32(), 0)ifvalueis of union type, andvalue.valueisNonemix_in_selector(hash_tree_root(value.data), value.selector)ifvalueis of compatible union type.
Summaries and expansions¶
Let A be an object derived from another object B by replacing some of the
(possibly nested) values of B by their hash_tree_root. We say A is a
"summary" of B, and that B is an "expansion" of A. Notice
hash_tree_root(A) == hash_tree_root(B).
We similarly define "summary types" and "expansion types". For example,
BeaconBlock is an expansion
type of
BeaconBlockHeader. Notice
that objects expand to at most one object of a given expansion type. For
example, BeaconBlockHeader objects uniquely expand to BeaconBlock objects.
Implementations¶
See https://github.com/ethereum/consensus-specs/issues/2138 for a list of current known implementations.
JSON mapping¶
The canonical JSON mapping assigns to each SSZ type a corresponding JSON encoding, enabling an SSZ schema to also define the JSON encoding.
When decoding JSON data, all fields in the SSZ schema must be present with a value. Parsers may ignore additional JSON fields.
| SSZ | JSON | Example | 
|---|---|---|
uintN | 
string | "0" | 
byte | 
hex-byte-string | "0x00" | 
boolean | 
bool | false | 
Container | 
object | { "field": ... } | 
ProgressiveContainer(active_fields) | 
object | { "field": ... } | 
Vector[type, N] | 
array | [element, ...] | 
Vector[byte, N] | 
hex-byte-string | "0x1122" | 
Bitvector[N] | 
hex-byte-string | "0x1122" | 
List[type, N] | 
array | [element, ...] | 
List[byte, N] | 
hex-byte-string | "0x1122" | 
ProgressiveList[type] | 
array | [element, ...] | 
ProgressiveList[byte] | 
hex-byte-string | "0x1122" | 
Bitlist[N] | 
hex-byte-string | "0x1122" | 
ProgressiveBitlist | 
hex-byte-string | "0x1122" | 
Union[type_0, type_1, ...] | 
selector-object | { "selector": number, "data": type_N } | 
CompatibleUnion({selector: type}) | 
selector-object | { "selector": string, "data": type } | 
Integers are encoded as strings to avoid loss of precision in 64-bit values.
Aliases are encoded as their underlying type.
hex-byte-string is a 0x-prefixed hex encoding of byte data, as it would
appear in an SSZ stream.
List, ProgressiveList, and Vector of byte (and aliases thereof) are
encoded as hex-byte-string. Bitlist, ProgressiveBitlist, and Bitvector
similarly map their SSZ-byte encodings to a hex-byte-string.
Union and CompatibleUnion are encoded as an object with a selector and
data field, where the contents of data change according to the selector.