Skip to content

Commit

Permalink
Rename "vector" to "tuple" in SSZ spec (#794)
Browse files Browse the repository at this point in the history
To be done in combination with #777.

Also:

* Define "fixed-size" and "variable-size" more rigorously
* Use `"` vs `'` consistently
* Add missing `"`
  • Loading branch information
JustinDrake authored Mar 17, 2019
1 parent 506fdf4 commit 390ece7
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions specs/simple-serialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
- [Serialization](#serialization)
- [`"uintN"`](#uintn)
- [`"bool"`](#bool)
- [Tuples, containers, lists](#tuples-containers-lists)
- [Vectors, containers, lists](#vectors-containers-lists)
- [Deserialization](#deserialization)
- [Merkleization](#merkleization)
- [Self-signed containers](#self-signed-containers)
Expand All @@ -34,12 +34,14 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
### Composite types

* **container**: ordered heterogenous collection of values
* key-pair curly bracket notation `{}`, e.g. `{'foo': "uint64", 'bar': "bool"}`
* **tuple**: ordered fixed-length homogeneous collection of values
* key-pair curly bracket notation `{}`, e.g. `{"foo": "uint64", "bar": "bool"}`
* **vector**: ordered fixed-length homogeneous collection of values
* angle bracket notation `[type, N]`, e.g. `["uint64", N]`
* **list**: ordered variable-length homogenous collection of values
* angle bracket notation `[type]`, e.g. `["uint64"]`

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:
Expand All @@ -54,34 +56,34 @@ We recursively define the `serialize` function which consumes an object `value`

*Note*: In the function definitions below (`serialize`, `hash_tree_root`, `signed_root`, etc.) objects implicitly carry their type.

### `uintN`
### `"uintN"`

```python
assert N in [8, 16, 32, 64, 128, 256]
return value.to_bytes(N // 8, 'little')
return value.to_bytes(N // 8, "little")
```

### `bool`
### `"bool"`

```python
assert value in (True, False)
return b'\x01' if value is True else b'\x00'
return b"\x01" if value is True else b"\x00"
```

### Tuples, containers, lists
### Vectors, containers, lists

If `value` is fixed-length (i.e. does not embed a list):
If `value` is fixed-size:

```python
return ''.join([serialize(element) for element in value])
return "".join([serialize(element) for element in value])
```

If `value` is variable-length (i.e. embeds a list):
If `value` is variable-size:

```python
serialized_bytes = ''.join([serialize(element) for element in value])
serialized_bytes = "".join([serialize(element) for element in value])
assert len(serialized_bytes) < 2**(8 * BYTES_PER_LENGTH_PREFIX)
serialized_length = len(serialized_bytes).to_bytes(BYTES_PER_LENGTH_PREFIX, 'little')
serialized_length = len(serialized_bytes).to_bytes(BYTES_PER_LENGTH_PREFIX, "little")
return serialized_length + serialized_bytes
```

Expand All @@ -99,9 +101,9 @@ We first define helper functions:

We now define Merkleization `hash_tree_root(value)` of an object `value` recursively:

* `merkleize(pack(value))` if `value` is a basic object or a tuple of basic objects
* `merkleize(pack(value))` if `value` is a basic object or a vector of basic objects
* `mix_in_length(merkleize(pack(value)), len(value))` if `value` is a list of basic objects
* `merkleize([hash_tree_root(element) for element in value])` if `value` is a tuple of composite objects or a container
* `merkleize([hash_tree_root(element) for element in value])` if `value` is a vector of composite objects or a container
* `mix_in_length(merkleize([hash_tree_root(element) for element in value]), len(value))` if `value` is a list of composite objects

## Self-signed containers
Expand Down

1 comment on commit 390ece7

@NAKsir-melody
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow it's more clear,

fixed = no length field
variable = with length field.

and vector look good than tuple.

Thanks!

Please sign in to comment.