2018-10-02 13:46:22 +00:00
# [WIP] SimpleSerialize (SSZ) Spec
2018-09-23 23:52:38 +00:00
2018-11-27 07:45:04 +00:00
This is the **work in progress** document to describe `SimpleSerialize` , the
2018-09-23 23:52:38 +00:00
current selected serialization method for Ethereum 2.0 using the Beacon Chain.
This document specifies the general information for serializing and
deserializing objects and data types.
## ToC
* [About ](#about )
2019-02-11 14:49:11 +00:00
* [Variables and Functions ](#variables-and-functions )
2018-09-23 23:52:38 +00:00
* [Constants ](#constants )
* [Overview ](#overview )
2018-10-02 13:33:11 +00:00
+ [Serialize/Encode ](#serializeencode )
2019-02-11 14:49:11 +00:00
- [uintN ](#uintn )
- [bool ](#bool )
- [bytesN ](#bytesn )
2018-10-02 13:42:25 +00:00
- [List/Vectors ](#listvectors )
2018-10-26 13:22:28 +00:00
- [Container ](#container )
2018-10-02 13:33:11 +00:00
+ [Deserialize/Decode ](#deserializedecode )
2019-02-11 14:49:11 +00:00
- [uintN ](#uintn-1 )
- [bool ](#bool-1 )
- [bytesN ](#bytesn-1 )
2018-10-02 13:42:25 +00:00
- [List/Vectors ](#listvectors-1 )
2018-10-26 13:22:28 +00:00
- [Container ](#container-1 )
2018-11-27 07:45:04 +00:00
+ [Tree Hash ](#tree-hash )
2019-01-17 11:02:57 +00:00
- [`uint8`..`uint256`, `bool`, `bytes1`..`bytes32` ](#uint8uint256-bool-bytes1bytes32 )
2019-02-11 14:49:11 +00:00
- [`uint264`..`uintN`, `bytes33`..`bytesN` ](#uint264uintn-bytes33bytesn )
2019-01-17 11:02:57 +00:00
- [List/Vectors ](#listvectors-2 )
- [Container ](#container-2 )
2018-09-23 23:52:38 +00:00
* [Implementations ](#implementations )
## About
2018-10-02 02:34:20 +00:00
`SimpleSerialize` was first proposed by Vitalik Buterin as the serialization
2018-09-23 23:52:38 +00:00
protocol for use in the Ethereum 2.0 Beacon Chain.
The core feature of `ssz` is the simplicity of the serialization with low
overhead.
2019-01-24 08:11:45 +00:00
## Variables and Functions
2018-09-23 23:52:38 +00:00
| Term | Definition |
|:-------------|:-----------------------------------------------------------------------------------------------|
2019-01-17 15:34:07 +00:00
| `little` | Little endian. |
2019-01-24 08:12:43 +00:00
| `byteorder` | Specifies [endianness ](https://en.wikipedia.org/wiki/Endianness ): big endian or little endian. |
2019-01-17 15:34:07 +00:00
| `len` | Length/number of bytes. |
2019-01-24 08:12:43 +00:00
| `to_bytes` | Convert to bytes. Should take parameters ``size`` and ``byteorder``. |
| `from_bytes` | Convert from bytes to object. Should take ``bytes`` and ``byteorder``. |
2018-09-23 23:52:38 +00:00
| `value` | The value to serialize. |
| `rawbytes` | Raw serialized bytes. |
2019-01-24 08:08:56 +00:00
| `deserialized_object` | The deserialized data in the data structure of your programming language. |
| `new_index` | An index to keep track the latest position where the `rawbytes` have been deserialized. |
2018-09-23 23:52:38 +00:00
## Constants
2018-11-27 16:12:28 +00:00
| Constant | Value | Definition |
|:------------------|:-----:|:--------------------------------------------------------------------------------------|
| `LENGTH_BYTES` | 4 | Number of bytes used for the length added before a variable-length serialized object. |
2018-12-10 08:28:11 +00:00
| `SSZ_CHUNK_SIZE` | 128 | Number of bytes for the chunk size of the Merkle tree leaf. |
2018-09-23 23:52:38 +00:00
## Overview
### Serialize/Encode
2019-02-11 14:49:11 +00:00
#### uintN
2018-11-27 07:45:04 +00:00
2018-11-27 16:12:28 +00:00
| uint Type | Usage |
|:---------:|:-----------------------------------------------------------|
2019-02-11 14:49:11 +00:00
| `uintN` | Type of `N` bits unsigned integer, where ``N % 8 == 0``. |
2018-11-27 07:45:04 +00:00
2018-09-23 23:52:38 +00:00
Convert directly to bytes the size of the int. (e.g. ``uint16 = 2 bytes``)
2019-01-17 15:34:07 +00:00
All integers are serialized as **little endian** .
2018-09-23 23:52:38 +00:00
2018-10-02 22:17:29 +00:00
| Check to perform | Code |
|:-----------------------|:----------------------|
| Size is a byte integer | ``int_size % 8 == 0`` |
2018-09-23 23:52:38 +00:00
```python
2018-10-02 22:17:29 +00:00
assert(int_size % 8 == 0)
2018-09-23 23:52:38 +00:00
buffer_size = int_size / 8
2019-01-17 15:34:07 +00:00
return value.to_bytes(buffer_size, 'little')
2018-09-23 23:52:38 +00:00
```
2019-02-11 14:49:11 +00:00
#### bool
2018-10-11 13:51:35 +00:00
Convert directly to a single 0x00 or 0x01 byte.
| Check to perform | Code |
|:------------------|:---------------------------|
| Value is boolean | ``value in (True, False)`` |
```python
assert(value in (True, False))
return b'\x01' if value is True else b'\x00'
```
2019-02-11 14:49:11 +00:00
#### bytesN
2018-10-11 13:51:35 +00:00
2019-02-11 14:49:11 +00:00
A fixed-size byte array.
2018-10-02 13:33:11 +00:00
2018-11-09 10:39:03 +00:00
| Checks to perform | Code |
|:---------------------------------------|:---------------------|
2019-02-11 14:49:11 +00:00
| Length in bytes is correct for `bytesN` | ``len(value) == N`` |
2018-10-02 13:33:11 +00:00
```python
2018-11-09 10:39:03 +00:00
assert(len(value) == N)
2018-10-02 13:33:11 +00:00
2018-09-23 23:52:38 +00:00
return value
```
2018-10-02 13:42:25 +00:00
#### List/Vectors
2018-09-23 23:52:38 +00:00
2018-10-27 11:36:10 +00:00
Lists are a collection of elements of the same homogeneous type.
2018-10-02 22:17:29 +00:00
| Check to perform | Code |
|:--------------------------------------------|:----------------------------|
| Length of serialized list fits into 4 bytes | ``len(serialized) < 2 * * 32 ` ` |
2019-01-22 16:14:25 +00:00
1. Serialize all list elements individually and concatenate them.
2. Prefix the concatenation with its length encoded as a `4-byte` **little-endian** unsigned integer.
2018-10-02 13:42:25 +00:00
2019-02-11 14:49:11 +00:00
We define `bytes` to be a synonym of `List[bytes1]` .
2018-10-02 13:42:25 +00:00
**Example in Python**
2018-09-23 23:52:38 +00:00
```python
2018-10-02 22:17:29 +00:00
serialized_list_string = b''
2018-09-23 23:52:38 +00:00
for item in value:
serialized_list_string += serialize(item)
2018-10-02 22:17:29 +00:00
assert(len(serialized_list_string) < 2 * * 32 )
2019-01-17 15:34:07 +00:00
serialized_len = (len(serialized_list_string).to_bytes(LENGTH_BYTES, 'little'))
2018-09-23 23:52:38 +00:00
return serialized_len + serialized_list_string
```
2018-10-03 05:08:20 +00:00
#### Container
2018-10-26 13:22:28 +00:00
A container represents a heterogenous, associative collection of key-value pairs. Each pair is referred to as a `field` . To get the value for a given field, you supply the key which is a symbol unique to the container referred to as the field's `name` . The container data type is analogous to the `struct` type found in many languages like C or Go.
2019-01-17 15:34:07 +00:00
To serialize a container, obtain the list of its field's names in the specified order. For each field name in this list, obtain the corresponding value and serialize it. Tightly pack the complete set of serialized values in the same order as the field names into a buffer. Calculate the size of this buffer of serialized bytes and encode as a `4-byte` **little endian** `uint32` . Prepend the encoded length to the buffer. The result of this concatenation is the final serialized value of the container.
2018-10-26 13:22:28 +00:00
2019-02-11 14:49:11 +00:00
| Check to perform | Code |
|:----------------------------------------------|:----------------------------|
2018-10-26 13:22:28 +00:00
| Length of serialized fields fits into 4 bytes | ``len(serialized) < 2 * * 32 ` ` |
2018-10-03 05:08:20 +00:00
2018-10-26 13:25:35 +00:00
To serialize:
2018-10-26 13:22:28 +00:00
2018-12-19 18:48:04 +00:00
1. Get the list of the container's fields.
2018-10-26 13:22:28 +00:00
2018-12-19 18:48:04 +00:00
2. For each name in the list, obtain the corresponding value from the container and serialize it. Place this serialized value into a buffer. The serialized values should be tightly packed.
2018-10-26 13:22:28 +00:00
2019-01-17 15:34:07 +00:00
3. Get the number of raw bytes in the serialized buffer. Encode that number as a `4-byte` **little endian** `uint32` .
2018-10-26 13:22:28 +00:00
4. Prepend the length to the serialized buffer.
**Example in Python**
```python
def get_field_names(typ):
return typ.fields.keys()
def get_value_for_field_name(value, field_name):
return getattr(value, field_name)
def get_type_for_field_name(typ, field_name):
return typ.fields[field_name]
serialized_buffer = b''
typ = type(value)
2018-12-19 18:48:04 +00:00
for field_name in get_field_names(typ):
2018-10-26 13:22:28 +00:00
field_value = get_value_for_field_name(value, field_name)
field_type = get_type_for_field_name(typ, field_name)
serialized_buffer += serialize(field_value, field_type)
assert(len(serialized_buffer) < 2 * * 32 )
2019-01-17 15:34:07 +00:00
serialized_len = (len(serialized_buffer).to_bytes(LENGTH_BYTES, 'little'))
2018-10-26 13:22:28 +00:00
return serialized_len + serialized_buffer
```
2018-10-03 05:08:20 +00:00
2018-09-23 23:52:38 +00:00
### Deserialize/Decode
The decoding requires knowledge of the type of the item to be decoded. When
performing decoding on an entire serialized string, it also requires knowledge
2018-10-02 22:17:29 +00:00
of the order in which the objects have been serialized.
2018-09-23 23:52:38 +00:00
2019-01-21 17:21:22 +00:00
Note: Each return will provide:
- `deserialized_object`
- `new_index`
2018-09-23 23:52:38 +00:00
At each step, the following checks should be made:
2018-10-02 22:17:29 +00:00
| Check to perform | Check |
|:-------------------------|:-----------------------------------------------------------|
2019-01-21 17:21:22 +00:00
| Ensure sufficient length | ``len(rawbytes) >= current_index + deserialize_length`` |
At the final step, the following checks should be made:
| Check to perform | Check |
|:-------------------------|:-------------------------------------|
| Ensure no extra length | `new_index == len(rawbytes)` |
2018-09-23 23:52:38 +00:00
2019-02-11 14:49:11 +00:00
#### uintN
2018-09-23 23:52:38 +00:00
Convert directly from bytes into integer utilising the number of bytes the same
size as the integer length. (e.g. ``uint16 == 2 bytes``)
2019-01-17 15:34:07 +00:00
All integers are interpreted as **little endian** .
2018-09-23 23:52:38 +00:00
```python
byte_length = int_size / 8
2019-01-04 17:14:27 +00:00
new_index = current_index + byte_length
assert(len(rawbytes) >= new_index)
2019-01-17 15:34:07 +00:00
return int.from_bytes(rawbytes[current_index:current_index+byte_length], 'little'), new_index
2018-09-23 23:52:38 +00:00
```
2019-02-11 14:49:11 +00:00
#### bool
2018-10-11 13:51:35 +00:00
Return True if 0x01, False if 0x00.
```python
assert rawbytes in (b'\x00', b'\x01')
return True if rawbytes == b'\x01' else False
```
2019-02-11 14:49:11 +00:00
#### bytesN
2018-09-23 23:52:38 +00:00
2018-11-09 10:39:03 +00:00
Return the `N` bytes.
2018-09-23 23:52:38 +00:00
```python
2018-11-09 10:39:03 +00:00
assert(len(rawbytes) >= current_index + N)
new_index = current_index + N
return rawbytes[current_index:current_index+N], new_index
2018-09-23 23:52:38 +00:00
```
2018-10-02 13:42:25 +00:00
#### List/Vectors
2018-09-23 23:52:38 +00:00
2018-10-27 11:36:10 +00:00
Deserialize each element in the list.
2018-09-23 23:52:38 +00:00
1. Get the length of the serialized list.
2018-10-01 23:41:18 +00:00
2. Loop through deserializing each item in the list until you reach the
2018-09-23 23:52:38 +00:00
entire length of the list.
2018-10-02 22:17:29 +00:00
| Check to perform | code |
|:------------------------------------------|:----------------------------------------------------------------|
2019-01-24 08:11:04 +00:00
| ``rawbytes`` has enough left for length | ``len(rawbytes) > current_index + LENGTH_BYTES`` |
2018-10-02 22:17:29 +00:00
| list is not greater than serialized bytes | ``len(rawbytes) > current_index + LENGTH_BYTES + total_length`` |
2018-09-23 23:52:38 +00:00
```python
2018-10-02 22:17:29 +00:00
assert(len(rawbytes) > current_index + LENGTH_BYTES)
2019-01-17 15:34:07 +00:00
total_length = int.from_bytes(rawbytes[current_index:current_index + LENGTH_BYTES], 'little')
2018-10-02 02:34:20 +00:00
new_index = current_index + LENGTH_BYTES + total_length
2018-10-02 22:17:29 +00:00
assert(len(rawbytes) >= new_index)
2018-10-02 02:34:20 +00:00
item_index = current_index + LENGTH_BYTES
2018-09-23 23:52:38 +00:00
deserialized_list = []
while item_index < new_index:
object, item_index = deserialize(rawbytes, item_index, item_type)
deserialized_list.append(object)
return deserialized_list, new_index
```
2018-10-03 05:08:20 +00:00
#### Container
2018-10-26 13:22:28 +00:00
Refer to the section on container encoding for some definitions.
To deserialize a container, loop over each field in the container and use the type of that field to know what kind of deserialization to perform. Consume successive elements of the data stream for each successful deserialization.
Instantiate a container with the full set of deserialized data, matching each member with the corresponding field.
| Check to perform | code |
|:------------------------------------------|:----------------------------------------------------------------|
2019-01-24 07:57:50 +00:00
| ``rawbytes`` has enough left for length | ``len(rawbytes) > current_index + LENGTH_BYTES`` |
2018-10-26 13:22:28 +00:00
| list is not greater than serialized bytes | ``len(rawbytes) > current_index + LENGTH_BYTES + total_length`` |
2018-10-26 13:25:35 +00:00
To deserialize:
2018-10-26 13:22:28 +00:00
2018-12-19 18:48:04 +00:00
1. Get the list of the container's fields.
2. For each name in the list, attempt to deserialize a value for that type. Collect these values as they will be used to construct an instance of the container.
2018-10-26 13:22:28 +00:00
3. Construct a container instance after successfully consuming the entire subset of the stream for the serialized container.
**Example in Python**
```python
def get_field_names(typ):
return typ.fields.keys()
def get_value_for_field_name(value, field_name):
return getattr(value, field_name)
def get_type_for_field_name(typ, field_name):
return typ.fields[field_name]
class Container:
# this is the container; here we will define an empty class for demonstration
pass
# get a reference to the type in some way...
container = Container()
typ = type(container)
assert(len(rawbytes) > current_index + LENGTH_BYTES)
2019-01-17 15:34:07 +00:00
total_length = int.from_bytes(rawbytes[current_index:current_index + LENGTH_BYTES], 'little')
2018-10-26 13:22:28 +00:00
new_index = current_index + LENGTH_BYTES + total_length
assert(len(rawbytes) >= new_index)
item_index = current_index + LENGTH_BYTES
values = {}
2018-12-19 18:48:04 +00:00
for field_name in get_field_names(typ):
2018-10-26 13:22:28 +00:00
field_name_type = get_type_for_field_name(typ, field_name)
2018-11-05 13:04:40 +00:00
values[field_name], item_index = deserialize(data, item_index, field_name_type)
2019-01-06 19:05:27 +00:00
assert item_index == new_index
2018-10-26 13:22:28 +00:00
return typ(**values), item_index
2018-10-03 05:08:20 +00:00
```
2018-11-27 07:45:04 +00:00
### Tree Hash
2018-11-15 13:12:34 +00:00
2019-02-01 00:03:23 +00:00
The below `hash_tree_root_internal` algorithm is defined recursively in the case of lists and containers, and it outputs a value equal to or less than 32 bytes in size. For use as a "final output" (eg. for signing), use `hash_tree_root(x) = zpad(hash_tree_root_internal(x), 32)` , where `zpad` is a helper that extends the given `bytes` value to the desired `length` by adding zero bytes on the right:
2019-02-01 14:31:00 +00:00
```python
2019-02-01 00:03:23 +00:00
def zpad(input: bytes, length: int) -> bytes:
2019-02-01 14:31:00 +00:00
return input + b'\x00' * (length - len(input))
2019-02-01 00:03:23 +00:00
```
2018-11-15 13:12:34 +00:00
2019-01-07 06:54:40 +00:00
Refer to [the helper function `hash` ](https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#hash ) of Phase 0 of the [Eth2.0 specs ](https://github.com/ethereum/eth2.0-specs ) for a definition of the hash function used below, `hash(x)` .
2018-11-15 13:12:34 +00:00
2019-01-17 11:02:57 +00:00
#### `uint8`..`uint256`, `bool`, `bytes1`..`bytes32`
2018-11-15 13:12:34 +00:00
Return the serialization of the value.
2019-02-11 14:49:11 +00:00
#### `uint264`..`uintN`, `bytes33`..`bytesN`
2018-11-15 13:12:34 +00:00
Return the hash of the serialization of the value.
#### List/Vectors
2019-02-03 20:26:07 +00:00
First, we define the Merkle tree function.
2018-11-15 13:12:34 +00:00
```python
2018-11-16 23:33:49 +00:00
# Merkle tree hash of a list of homogenous, non-empty items
2018-11-15 13:12:34 +00:00
def merkle_hash(lst):
# Store length of list (to compensate for non-bijectiveness of padding)
2019-01-17 15:34:07 +00:00
datalen = len(lst).to_bytes(32, 'little')
2018-11-16 23:33:49 +00:00
if len(lst) == 0:
# Handle empty list case
2018-11-27 16:12:28 +00:00
chunkz = [b'\x00' * SSZ_CHUNK_SIZE]
elif len(lst[0]) < SSZ_CHUNK_SIZE:
2018-11-16 23:33:49 +00:00
# See how many items fit in a chunk
2018-11-27 16:12:28 +00:00
items_per_chunk = SSZ_CHUNK_SIZE // len(lst[0])
2018-11-16 23:33:49 +00:00
# Build a list of chunks based on the number of items in the chunk
2019-01-31 23:47:09 +00:00
chunkz = [
zpad(b''.join(lst[i:i + items_per_chunk]), SSZ_CHUNK_SIZE)
for i in range(0, len(lst), items_per_chunk)
]
2018-11-16 23:33:49 +00:00
else:
# Leave large items alone
chunkz = lst
2019-02-12 11:12:29 +00:00
# Merkleise
def next_power_of_2(x):
return 1 if x == 0 else 2**(x - 1).bit_length()
for i in range(len(chunkz), next_power_of_2(len(chunkz))):
chunkz.append(b'\x00' * SSZ_CHUNK_SIZE)
while len(chunkz) > 1:
2018-11-15 13:12:34 +00:00
chunkz = [hash(chunkz[i] + chunkz[i+1]) for i in range(0, len(chunkz), 2)]
2018-11-16 23:33:49 +00:00
2019-02-12 11:12:29 +00:00
# Return hash of root and data length
2018-11-20 14:12:56 +00:00
return hash(chunkz[0] + datalen)
2018-11-15 13:12:34 +00:00
```
2019-02-01 00:03:23 +00:00
To `hash_tree_root_internal` a list, we simply do:
2018-11-15 13:12:34 +00:00
```python
2019-02-01 00:03:23 +00:00
return merkle_hash([hash_tree_root_internal(item) for item in value])
2018-11-15 13:12:34 +00:00
```
2019-02-01 00:03:23 +00:00
Where the inner `hash_tree_root_internal` is a recursive application of the tree-hashing function (returning less than 32 bytes for short single values).
2018-11-15 13:12:34 +00:00
#### Container
2019-02-12 21:57:54 +00:00
Recursively tree hash the values in the container in the same order as the fields, and Merkle hash the results.
2018-11-15 13:12:34 +00:00
```python
2019-02-12 21:57:54 +00:00
return merkle_hash([hash_tree_root_internal(getattr(x, field)) for field in value.fields])
2018-11-15 13:12:34 +00:00
```
2018-09-23 23:52:38 +00:00
## Implementations
2018-10-02 00:36:58 +00:00
| Language | Implementation | Description |
|:--------:|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------|
2019-01-17 11:02:57 +00:00
| Python | [ https://github.com/ethereum/py-ssz ](https://github.com/ethereum/py-ssz ) | Python implementation of SSZ |
2018-10-10 07:14:53 +00:00
| Rust | [ https://github.com/sigp/lighthouse/tree/master/beacon_chain/utils/ssz ](https://github.com/sigp/lighthouse/tree/master/beacon_chain/utils/ssz ) | Lighthouse (Rust Ethereum 2.0 Node) maintained SSZ. |
2018-10-02 00:36:58 +00:00
| Nim | [ 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 ) | Nim Implementation maintained SSZ. |
| Rust | [ https://github.com/paritytech/shasper/tree/master/util/ssz ](https://github.com/paritytech/shasper/tree/master/util/ssz ) | Shasper implementation of SSZ maintained by ParityTech. |
2018-10-14 20:29:47 +00:00
| Javascript | [ https://github.com/ChainSafeSystems/ssz-js/blob/master/src/index.js ](https://github.com/ChainSafeSystems/ssz-js/blob/master/src/index.js ) | Javascript Implementation maintained SSZ |
2018-12-03 05:22:51 +00:00
| Java | [ https://www.github.com/ConsenSys/cava/tree/master/ssz ](https://www.github.com/ConsenSys/cava/tree/master/ssz ) | SSZ Java library part of the Cava suite |
2018-12-22 23:13:54 +00:00
| Go | [ https://github.com/prysmaticlabs/prysm/tree/master/shared/ssz ](https://github.com/prysmaticlabs/prysm/tree/master/shared/ssz ) | Go implementation of SSZ mantained by Prysmatic Labs |
2019-01-17 16:51:34 +00:00
| Swift | [ https://github.com/yeeth/SimpleSerialize.swift ](https://github.com/yeeth/SimpleSerialize.swift ) | Swift implementation maintained SSZ |
2019-02-11 11:01:05 +00:00
| C# | [ https://github.com/codingupastorm/csharp-ssz ](https://github.com/codingupastorm/csharp-ssz ) | C# implementation maintained SSZ |
2018-11-22 08:28:24 +00:00
## Copyright
Copyright and related rights waived via [CC0 ](https://creativecommons.org/publicdomain/zero/1.0/ ).