2019-04-15 18:02:23 +00:00
# SimpleSerialize (SSZ)
2019-02-27 10:54:23 +00:00
2019-05-06 15:30:32 +00:00
**Notice**: This document is a work-in-progress describing typing, serialization, and Merkleization of Eth 2.0 objects.
2019-02-27 10:54:23 +00:00
## Table of contents
2019-05-06 15:30:32 +00:00
<!-- TOC -->
- [SimpleSerialize (SSZ) ](#simpleserialize-ssz )
- [Table of contents ](#table-of-contents )
- [Constants ](#constants )
- [Typing ](#typing )
- [Basic types ](#basic-types )
- [Composite types ](#composite-types )
- [Aliases ](#aliases )
- [Default values ](#default-values )
2019-06-06 13:45:20 +00:00
- [Illegal types ](#illegal-types )
2019-05-06 15:30:32 +00:00
- [Serialization ](#serialization )
2019-06-27 08:51:06 +00:00
- [`uintN` ](#uintn )
- [`boolean` ](#boolean )
- [`null` ](#null )
2019-05-07 17:10:18 +00:00
- [Vectors, containers, lists, unions ](#vectors-containers-lists-unions )
2019-05-06 15:30:32 +00:00
- [Deserialization ](#deserialization )
- [Merkleization ](#merkleization )
- [Self-signed containers ](#self-signed-containers )
- [Implementations ](#implementations )
<!-- /TOC -->
2018-09-23 23:52:38 +00:00
2019-02-27 21:33:36 +00:00
## Constants
| Name | Value | Description |
|-|-|-|
2019-04-13 13:44:14 +00:00
| `BYTES_PER_CHUNK` | `32` | Number of bytes per chunk. |
2019-04-13 13:56:06 +00:00
| `BYTES_PER_LENGTH_OFFSET` | `4` | Number of bytes per serialized length offset. |
2019-04-13 13:44:14 +00:00
| `BITS_PER_BYTE` | `8` | Number of bits per byte. |
2018-09-23 23:52:38 +00:00
2019-02-27 21:33:36 +00:00
## Typing
2019-02-27 16:54:19 +00:00
### Basic types
2018-09-23 23:52:38 +00:00
2019-06-27 08:51:06 +00:00
* `uintN` : `N` -bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]` )
* `boolean` : `True` or `False`
2018-11-27 07:45:04 +00:00
2019-02-27 16:54:19 +00:00
### Composite types
2018-09-23 23:52:38 +00:00
2019-04-20 05:18:14 +00:00
* **container**: ordered heterogeneous collection of values
2019-06-27 10:30:23 +00:00
* python dataclass notation with key-type pairs, e.g.
2019-06-27 08:51:06 +00:00
```python
class ContainerExample(Container):
foo: uint64
bar: boolean
```
2019-06-28 15:07:36 +00:00
* **vector**: ordered fixed-length homogeneous collection, with `N` values
2019-06-27 08:51:06 +00:00
* notation `Vector[type, N]` , e.g. `Vector[uint64, N]`
2019-06-28 15:07:36 +00:00
* **list**: ordered variable-length homogeneous collection, limited to `N` values
2019-06-27 08:51:06 +00:00
* notation `List[type, N]` , e.g. `List[uint64, N]`
2019-06-28 15:07:36 +00:00
* **bitvector**: ordered fixed-length collection of `boolean` values, with `N` bits
2019-06-27 08:51:06 +00:00
* notation `Bitvector[N]`
2019-06-28 15:07:36 +00:00
* **bitlist**: ordered variable-length collection of `boolean` values, limited to `N` bits
2019-06-27 08:51:06 +00:00
* notation `Bitlist[N]`
2019-06-27 10:30:23 +00:00
* **union**: union type containing one of the given subtypes
* notation `Union[type_1, type_2, ...]` , e.g. `union[null, uint64]`
2018-09-23 23:52:38 +00:00
2019-05-01 12:52:37 +00:00
### Variable-size and fixed-size
2019-05-01 10:39:44 +00:00
2019-06-27 08:51:06 +00:00
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".
2019-03-17 11:33:29 +00:00
2019-02-27 17:00:49 +00:00
### Aliases
2018-10-02 13:33:11 +00:00
2019-02-27 10:54:23 +00:00
For convenience we alias:
2018-10-02 13:33:11 +00:00
2019-06-27 08:51:06 +00:00
* `bit` to `boolean`
* `byte` to `uint8` (this is a basic type)
* `BytesN` to `Vector[byte, N]` (this is *not* a basic type)
* `null` : `{}` , i.e. the empty container
2018-10-02 22:17:29 +00:00
2019-04-19 08:26:54 +00:00
### Default values
2019-06-27 08:51:06 +00:00
The default value of a type upon initialization is recursively defined using `0` for `uintN` , `False` for `boolean` and the elements of `Bitvector` , and `[]` for lists and `Bitlist` . Unions default to the first type in the union (with type index zero), which is `null` if present in the union.
2019-04-19 08:26:54 +00:00
2019-06-05 13:29:26 +00:00
#### `is_empty`
2019-06-25 13:32:56 +00:00
An SSZ object is called empty (and thus, `is_empty(object)` returns true) if it is equal to the default value for that type.
2019-06-05 13:29:26 +00:00
2019-05-01 12:52:37 +00:00
### Illegal types
2019-06-27 08:51:06 +00:00
Empty vector types (i.e. `[subtype, 0]` for some `subtype` ) are not legal. The `null` type is only legal as the first type in a union subtype (i.e. with type index zero).
2019-04-19 08:26:54 +00:00
2019-02-27 10:54:23 +00:00
## Serialization
2018-10-02 13:42:25 +00:00
2019-06-27 08:51:06 +00:00
We recursively define the `serialize` function which consumes an object `value` (of the type specified) and returns a bytestring of type `bytes` .
2019-02-28 10:32:54 +00:00
2019-06-25 13:32:56 +00:00
*Note*: In the function definitions below (`serialize`, `hash_tree_root` , `signing_root` , `is_variable_size` , etc.) objects implicitly carry their type.
2019-03-18 14:57:02 +00:00
2019-06-27 08:51:06 +00:00
### `uintN`
2019-03-18 22:28:33 +00:00
2018-09-23 23:52:38 +00:00
```python
2019-02-27 10:54:23 +00:00
assert N in [8, 16, 32, 64, 128, 256]
2019-03-17 11:33:29 +00:00
return value.to_bytes(N // 8, "little")
2018-09-23 23:52:38 +00:00
```
2019-06-27 08:51:06 +00:00
### `boolean`
2019-03-18 22:28:33 +00:00
2018-10-26 13:22:28 +00:00
```python
2019-02-27 16:35:26 +00:00
assert value in (True, False)
2019-03-17 11:33:29 +00:00
return b"\x01" if value is True else b"\x00"
2018-09-23 23:52:38 +00:00
```
2019-06-27 08:51:06 +00:00
### `null`
2019-04-10 14:09:53 +00:00
```python
return b""
```
2019-06-27 08:51:06 +00:00
### `Bitvector[N]`
```python
as_integer = sum([value[i] < < i for i in range ( len ( value ) ) ] )
return as_integer.to_bytes((N + 7) // 8, "little")
```
### `Bitlist[N]`
Note that from the offset coding, the length (in bytes) of the bitlist is known. An additional leading `1` bit is added so that the length in bits will also be known.
```python
as_integer = (1 < < len ( value ) ) + sum ( [ value [ i ] < < i for i in range ( len ( value ) ) ] )
return as_integer.to_bytes((as_integer.bit_length() + 7) // 8, "little")
```
2019-04-11 15:05:16 +00:00
### Vectors, containers, lists, unions
2019-03-18 14:57:02 +00:00
2019-04-13 13:44:14 +00:00
```python
2019-05-07 00:36:55 +00:00
# Recursively serialize
2019-04-24 05:53:28 +00:00
fixed_parts = [serialize(element) if not is_variable_size(element) else None for element in value]
variable_parts = [serialize(element) if is_variable_size(element) else b"" for element in value]
2019-03-18 14:57:02 +00:00
2019-04-13 13:50:23 +00:00
# Compute and check lengths
2019-04-13 13:55:08 +00:00
fixed_lengths = [len(part) if part != None else BYTES_PER_LENGTH_OFFSET for part in fixed_parts]
2019-04-13 14:10:02 +00:00
variable_lengths = [len(part) for part in variable_parts]
2019-04-13 13:55:08 +00:00
assert sum(fixed_lengths + variable_lengths) < 2 * * ( BYTES_PER_LENGTH_OFFSET * BITS_PER_BYTE )
2019-03-18 14:57:02 +00:00
2019-04-13 14:22:41 +00:00
# Interleave offsets of variable-size parts with fixed-size parts
2019-04-13 14:26:44 +00:00
variable_offsets = [serialize(sum(fixed_lengths + variable_lengths[:i])) for i in range(len(value))]
fixed_parts = [part if part != None else variable_offsets[i] for i, part in enumerate(fixed_parts)]
2019-03-18 14:57:02 +00:00
2019-04-13 14:22:41 +00:00
# Return the concatenation of the fixed-size parts (offsets interleaved) with the variable-size parts
2019-04-24 05:53:28 +00:00
return b"".join(fixed_parts + variable_parts)
2018-10-03 05:08:20 +00:00
```
2019-05-01 10:39:07 +00:00
If `value` is a union type:
2019-04-10 14:09:53 +00:00
Define value as an object that has properties `value.value` with the contained value, and `value.type_index` which indexes the type.
```python
serialized_bytes = serialize(value.value)
2019-05-01 10:39:24 +00:00
serialized_type_index = value.type_index.to_bytes(BYTES_PER_LENGTH_OFFSET, "little")
2019-04-11 15:00:53 +00:00
return serialized_type_index + serialized_bytes
2019-04-10 14:09:53 +00:00
```
2019-02-27 10:54:23 +00:00
## Deserialization
2018-11-15 13:12:34 +00:00
2019-02-28 10:32:54 +00:00
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 ](#implementations ).
2018-11-15 13:12:34 +00:00
2019-06-28 15:07:36 +00:00
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.
2019-02-27 10:54:23 +00:00
## Merkleization
2018-11-15 13:12:34 +00:00
2019-02-27 10:54:23 +00:00
We first define helper functions:
2018-11-15 13:12:34 +00:00
2019-02-27 21:44:20 +00:00
* `pack` : Given ordered objects of the same basic type, serialize them, pack them into `BYTES_PER_CHUNK` -byte chunks, right-pad the last chunk with zero bytes, and return the chunks.
2019-06-28 20:45:20 +00:00
* `next_pow_of_two(i)` : get the next power of 2 of `i` , 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->16`
* `merkleize(data, pad_for)` : Given ordered `BYTES_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.
The merkleization depends on the effective input, which can be padded: if `pad_for=L` , then pad the `data` with zeroed chunks to `next_pow_of_two(L)` (virtually for memory efficiency).
Then, merkleize the chunks (empty input is padded to 1 zero chunk):
- If `1` chunk: A single chunk is simply that chunk, i.e. the identity when the number of chunks is one.
- If `> 1` chunks: pad to `next_pow_of_two(len(chunks))` , merkleize as binary tree.
2019-03-06 16:46:40 +00:00
* `mix_in_length` : Given a Merkle root `root` and a length `length` (`"uint256"` little-endian serialization) return `hash(root + length)` .
2019-04-10 14:09:53 +00:00
* `mix_in_type` : Given a Merkle root `root` and a type_index `type_index` (`"uint256"` little-endian serialization) return `hash(root + type_index)` .
2018-11-15 13:12:34 +00:00
2019-02-27 16:56:51 +00:00
We now define Merkleization `hash_tree_root(value)` of an object `value` recursively:
2019-02-16 21:44:27 +00:00
2019-03-17 11:33:29 +00:00
* `merkleize(pack(value))` if `value` is a basic object or a vector of basic objects
2019-06-28 20:45:20 +00:00
* `mix_in_length(merkleize(pack(value), pad_for=(N * elem_size / BYTES_PER_CHUNK)), len(value))` if `value` is a list of basic objects.
2019-03-17 11:33:29 +00:00
* `merkleize([hash_tree_root(element) for element in value])` if `value` is a vector of composite objects or a container
2019-06-28 20:45:20 +00:00
* `mix_in_length(merkleize([hash_tree_root(element) for element in value], pad_for=N), len(value))` if `value` is a list of composite objects.
2019-04-11 15:05:16 +00:00
* `mix_in_type(merkleize(value.value), value.type_index)` if `value` is of union type
2019-02-16 21:44:27 +00:00
2019-06-27 08:51:06 +00:00
### Merkleization of `Bitvector[N]`
```python
as_integer = sum([value[i] < < i for i in range ( len ( value ) ) ] )
return merkleize(as_integer.to_bytes((N + 7) // 8, "little"))
```
### `Bitlist[N]`
```python
as_integer = sum([value[i] < < i for i in range ( len ( value ) ) ] )
return mix_in_length(merkleize(as_integer.to_bytes((N + 7) // 8, "little")), len(value))
```
2019-02-27 11:40:08 +00:00
## Self-signed containers
2019-02-16 21:44:27 +00:00
2019-04-24 05:53:28 +00:00
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 `signing_root(value) = hash_tree_root(truncate_last(value))` where `truncate_last` truncates the last element of `value` .
2019-02-16 21:44:27 +00:00
2018-09-23 23:52:38 +00:00
## Implementations
2019-02-27 11:40:08 +00:00
| Language | Project | Maintainer | Implementation |
2019-02-27 11:54:56 +00:00
|-|-|-|-|
2019-02-27 11:40:08 +00:00
| Python | Ethereum 2.0 | Ethereum Foundation | [https://github.com/ethereum/py-ssz ](https://github.com/ethereum/py-ssz ) |
2019-04-10 18:28:24 +00:00
| Rust | Lighthouse | Sigma Prime | [https://github.com/sigp/lighthouse/tree/master/eth2/utils/ssz ](https://github.com/sigp/lighthouse/tree/master/eth2/utils/ssz ) |
2019-02-27 11:40:08 +00:00
| Nim | Nimbus | Status | [https://github.com/status-im/nim-beacon-chain/blob/master/beacon_chain/ssz.nim ](https://github.com/status-im/nim-beacon-chain/blob/master/beacon_chain/ssz.nim ) |
2019-06-25 13:32:56 +00:00
| Rust | Shasper | ParityTech | [https://github.com/paritytech/shasper/tree/master/utils/ssz ](https://github.com/paritytech/shasper/tree/master/util/ssz ) |
2019-04-10 18:28:24 +00:00
| TypeScript | Lodestar | ChainSafe Systems | [https://github.com/ChainSafe/ssz-js ](https://github.com/ChainSafe/ssz-js ) |
2019-02-27 11:40:08 +00:00
| Java | Cava | ConsenSys | [https://www.github.com/ConsenSys/cava/tree/master/ssz ](https://www.github.com/ConsenSys/cava/tree/master/ssz ) |
2019-06-03 05:34:00 +00:00
| Go | Prysm | Prysmatic Labs | [https://github.com/prysmaticlabs/go-ssz ](https://github.com/prysmaticlabs/go-ssz ) |
2019-02-27 11:40:08 +00:00
| Swift | Yeeth | Dean Eigenmann | [https://github.com/yeeth/SimpleSerialize.swift ](https://github.com/yeeth/SimpleSerialize.swift ) |
| C# | | Jordan Andrews | [https://github.com/codingupastorm/csharp-ssz ](https://github.com/codingupastorm/csharp-ssz ) |
2019-05-06 15:30:32 +00:00
| C++ | | Jiyun Kim | [https://github.com/NAKsir-melody/cpp_ssz ](https://github.com/NAKsir-melody/cpp_ssz ) |