Skip to content

Commit

Permalink
Array spec to [type] and tuple to [type, N]. Also make notation consi…
Browse files Browse the repository at this point in the history
…stent: use "base" for base types
  • Loading branch information
dankrad committed Mar 6, 2019
1 parent 9d175c9 commit eece029
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions specs/simple-serialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
- [Composite types](#composite-types)
- [Aliases](#aliases)
- [Serialization](#serialization)
- [`uintN`](#uintn)
- [`bool`](#bool)
- [`"uintN"`](#uintn)
- [`"bool"`](#bool)
- [Tuples, containers, lists](#tuples-containers-lists)
- [Deserialization](#deserialization)
- [Merkleization](#merkleization)
Expand All @@ -28,29 +28,29 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
## Typing
### Basic types

* `uintN`: `N`-bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]`)
* `bool`: `True` or `False`
* `"uintN"`: `N`-bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]`)
* `"bool"`: `True` or `False`

### 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
* angle bracket notation `[N]`, e.g. `uint64[N]`
* angle bracket notation `[type, N]`, e.g. `["uint64", N]`
* **list**: ordered variable-length homogenous collection of values
* angle bracket notation `[]`, e.g. `uint64[]`
* angle bracket notation `[type]`, e.g. `["uint64"]`

### Aliases

For convenience we alias:

* `byte` to `uint8`
* `bytes` to `byte[]`
* `bytesN` to `byte[N]`
* `"byte"` to `"uint8"`
* `"bytes"` to `["byte"]`
* `"bytesN"` to `["byte", N]`

## Serialization

We recursively define the `serialize` function which consumes an object `value` (of the type specified) and returns a bytestring of type `bytes`.
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.

Expand Down Expand Up @@ -95,7 +95,7 @@ We first define helper functions:

* `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 `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.
* `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:

Expand All @@ -106,7 +106,7 @@ We now define Merkleization `hash_tree_root(value)` of an object `value` recursi

## 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`.
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

Expand Down

1 comment on commit eece029

@NAKsir-melody
Copy link
Contributor

@NAKsir-melody NAKsir-melody commented on eece029 Mar 18, 2019

Choose a reason for hiding this comment

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

if the byte is basic type,
have we support bytes input in pack function?
because bytes is same basic type of byte.
Am I right?

Please sign in to comment.