Update simple-serialize.md
This commit is contained in:
parent
8176cc4cf0
commit
74349eacd2
|
@ -4,6 +4,7 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
|
||||||
|
|
||||||
## Table of contents
|
## Table of contents
|
||||||
|
|
||||||
|
- [Constants](#constants)
|
||||||
- [Typing](#typing)
|
- [Typing](#typing)
|
||||||
- [Basic types](#basic-types)
|
- [Basic types](#basic-types)
|
||||||
- [Composite types](#composite-types)
|
- [Composite types](#composite-types)
|
||||||
|
@ -17,8 +18,14 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
|
||||||
- [Self-signed containers](#self-signed-containers)
|
- [Self-signed containers](#self-signed-containers)
|
||||||
- [Implementations](#implementations)
|
- [Implementations](#implementations)
|
||||||
|
|
||||||
## Typing
|
## 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
|
### Basic types
|
||||||
|
|
||||||
* `uintN`: `N`-bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]`)
|
* `uintN`: `N`-bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]`)
|
||||||
|
@ -27,7 +34,7 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
|
||||||
### Composite types
|
### Composite types
|
||||||
|
|
||||||
* **container**: ordered heterogenous collection of values
|
* **container**: ordered heterogenous collection of values
|
||||||
* key-pair curly braket notation `{}`, e.g. `{'foo': uint64, 'bar': bool}`
|
* key-pair curly braket notation `{}`, e.g. `{'foo': "uint64", 'bar': "bool"}`
|
||||||
* **tuple**: ordered fixed-length homogeneous collection of values
|
* **tuple**: ordered fixed-length homogeneous collection of values
|
||||||
* angle braket notation `[N]`, e.g. `uint64[N]`
|
* angle braket notation `[N]`, e.g. `uint64[N]`
|
||||||
* **list**: ordered variable-length homogenous collection of values
|
* **list**: ordered variable-length homogenous collection of values
|
||||||
|
@ -63,9 +70,8 @@ return b'\x01' if value is True else b'\x00'
|
||||||
|
|
||||||
```python
|
```python
|
||||||
serialized_bytes = ''.join([serialize(element) for element in value])
|
serialized_bytes = ''.join([serialize(element) for element in value])
|
||||||
LENGTH_BYTES = 4
|
assert len(serialized_bytes) < 2**(8 * BYTES_PER_LENGTH_PREFIX)
|
||||||
assert len(serialized_bytes) < 2**(8 * LENGTH_BYTES)
|
serialized_length = len(serialized_bytes).to_bytes(BYTES_PER_LENGTH_PREFIX, 'little')
|
||||||
serialized_length = len(serialized_bytes).to_bytes(LENGTH_BYTES, 'little')
|
|
||||||
return serialized_length + serialized_bytes
|
return serialized_length + serialized_bytes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -77,8 +83,8 @@ Given a type, serialization is an injective function from objects of that type t
|
||||||
|
|
||||||
We first define helper functions:
|
We first define helper functions:
|
||||||
|
|
||||||
* `pack`: Given ordered objects of the same basic type, serialize them, pack them into 32-byte chunks, right-pad the last chunk with zero bytes, and return the chunks.
|
* `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.
|
||||||
* `merkleize`: Given ordered 32-byte chunks, right-pad them with zero chunks to the next power of two, Merkleize the chunks, and return the root.
|
* `merkleize`: Given ordered BYTES_PER_CHUNK-byte chunks, right-pad them with zero chunks to the next power of two, Merkleize the chunks, and return the root.
|
||||||
* `mix_in_length`: Given a Merkle root `root` and a length `length` (`uint256` little-endian serialization) return `hash(root + length)`.
|
* `mix_in_length`: Given a Merkle root `root` and a length `length` (`uint256` little-endian serialization) return `hash(root + length)`.
|
||||||
|
|
||||||
We now define Merkleization `hash_tree_root(value)` of an object `value` recursively:
|
We now define Merkleization `hash_tree_root(value)` of an object `value` recursively:
|
||||||
|
|
Loading…
Reference in New Issue