7.7 KiB
SimpleSerialiZe (SSZ)
This is a work in progress describing typing, serialization and Merkleization of Ethereum 2.0 objects.
Table of contents
Constants
| Name | Value | Description |
|---|---|---|
BYTES_PER_CHUNK |
32 |
Number of bytes per chunk. |
BYTES_PER_LENGTH_PREFIX |
4 |
Number of bytes per serialized length prefix. |
Typing
Basic types
"uintN":N-bit unsigned integer (whereN in [8, 16, 32, 64, 128, 256])"bool":TrueorFalse
Composite types
- container: ordered heterogenous collection of values
- key-pair curly bracket notation
{}, e.g.{"foo": "uint64", "bar": "bool"}
- key-pair curly bracket notation
- vector: ordered fixed-length homogeneous collection of values
- angle bracket notation
[type, N], e.g.["uint64", N]
- angle bracket notation
- list: ordered variable-length homogenous collection of values
- angle bracket notation
[type], e.g.["uint64"]
- angle bracket notation
We recursively define "variable-size" types to be lists and all types that contains a variable-size type. All other types are said to be "fixed-size".
Aliases
For convenience we alias:
"byte"to"uint8"(this is a basic type)"bytes"to["byte"](this is not a basic type)"bytesN"to["byte", N](this is not a basic type)
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,signed_root, etc.) objects implicitly carry their type.
Basic Types
For basic types the serialize function is defined as follows.
"uintN"
A byte string of width N // 8 containing the little-endian encode integer.
assert N in [8, 16, 32, 64, 128, 256]
return value.to_bytes(N // 8, "little")
"bool"
- The byte
\x00if the value isFalse - The byte
\x01if the value isTrue
assert value in (True, False)
return b"\x01" if value is True else b"\x00"
Composite Types (Vectors, Containers and Lists)
The serialized representation of composite types is comprised of two binary sections.
- The first section is fixed size for all types, containing the concatenation of either
- The serialized representation for each of the fixed size elements of value
- The
"uint32"serialized offset where the serialized representation of the variable sized type is located in the second section relative to the beginning of the first section.
- The second section contains the concatenation of the serialized representations of only the variable size types.
- This section is empty in the case of a purely fixed size type.
"vector", "container" and "list"
An implementation of the serialize function for "Vector", "Container" and
"List" types would take the following form.
# The second section is just the concatenation of the serialized *variable size* elements
section_2_parts = [
serialize(element) if is_variable_size(element)
else ''
for element in value
]
section_2_lengths = [len(part) for part in section_2_parts]
section_2 = ''.join(section_2_parts)
# Compute the length of the first section (can also be extracted from the type directly)
section_1_length = sum(
len(serialize(element)) if is_fixed_size(element)
else 4
for element in value
)
# Compute the offset values for each part of the second section
section_1_offsets = [
section_1_length + sum(section_2_lengths[:element_index]) if is_variable_size(element)
else None
for element_index, element in enumerate(value)
]
assert all(offset is None or offset < 2**32 for offset in section_1_offsets)
# The first section is the concatenation of the serialized static size elements and offsets
section_1_parts = [
serialize(element) if is_fixed_size(element)
else serialize(section_1_offsets[element_index])
for element_index, element in enumerate(value)
]
section_1 = ''.join(section_1_parts)
return ''.join([section_1, section_2])
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. Efficient algorithms for computing this object can be found in the implementations.
Merkleization
We first define helper functions:
pack: Given ordered objects of the same basic type, serialize them, pack them intoBYTES_PER_CHUNK-byte chunks, right-pad the last chunk with zero bytes, and return the chunks.merkleize: Given orderedBYTES_PER_CHUNK-byte chunks, if necessary append zero chunks so that the number of chunks is a power of two, Merkleize the chunks, and return the root.mix_in_length: Given a Merkle rootrootand a lengthlength("uint256"little-endian serialization) returnhash(root + length).
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 objectsmix_in_length(merkleize(pack(value)), len(value))ifvalueis a list of basic objectsmerkleize([hash_tree_root(element) for element in value])ifvalueis a vector of composite objects or a containermix_in_length(merkleize([hash_tree_root(element) for element in value]), len(value))ifvalueis a list of composite objects
Self-signed containers
Let value be a self-signed container object. The convention is that the signature (e.g. a "bytes96" BLS12-381 signature) be the last field of value. Further, the signed message for value is signed_root(value) = hash_tree_root(truncate_last(value)) where truncate_last truncates the last element of value.
Implementations
| Language | Project | Maintainer | Implementation |
|---|---|---|---|
| Python | Ethereum 2.0 | Ethereum Foundation | https://github.com/ethereum/py-ssz |
| Rust | Lighthouse | Sigma Prime | https://github.com/sigp/lighthouse/tree/master/beacon_chain/utils/ssz |
| Nim | Nimbus | Status | https://github.com/status-im/nim-beacon-chain/blob/master/beacon_chain/ssz.nim |
| Rust | Shasper | ParityTech | https://github.com/paritytech/shasper/tree/master/util/ssz |
| Javascript | Lodestart | Chain Safe Systems | https://github.com/ChainSafeSystems/ssz-js/blob/master/src/index.js |
| Java | Cava | ConsenSys | https://www.github.com/ConsenSys/cava/tree/master/ssz |
| Go | Prysm | Prysmatic Labs | https://github.com/prysmaticlabs/prysm/tree/master/shared/ssz |
| Swift | Yeeth | Dean Eigenmann | https://github.com/yeeth/SimpleSerialize.swift |
| C# | Jordan Andrews | https://github.com/codingupastorm/csharp-ssz | |
| C++ | https://github.com/NAKsir-melody/cpp_ssz |