Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash_tree_root -> hash_tree_root_internal #543

Merged
merged 2 commits into from
Feb 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions specs/simple-serialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,12 @@ return typ(**values), item_index

### Tree Hash

The below `hash_tree_root` 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 the final output only (ie. not intermediate outputs), if the output is less than 32 bytes, right-zero-pad it to 32 bytes. The goal is collision resistance *within* each type, not between types.
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:

```python
def zpad(input: bytes, length: int) -> bytes:
return input + b'\x00' * (length - len(input))
```

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

Expand Down Expand Up @@ -416,20 +421,20 @@ def merkle_hash(lst):
return hash(chunkz[0] + datalen)
```

To `hash_tree_root` a list, we simply do:
To `hash_tree_root_internal` a list, we simply do:

```python
return merkle_hash([hash_tree_root(item) for item in value])
return merkle_hash([hash_tree_root_internal(item) for item in value])
```

Where the inner `hash_tree_root` is a recursive application of the tree-hashing function (returning less than 32 bytes for short single values).
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).

#### Container

Recursively tree hash the values in the container in the same order as the fields, and return the hash of the concatenation of the results.

```python
return hash(b''.join([hash_tree_root(getattr(x, field)) for field in value.fields]))
return hash(b''.join([hash_tree_root_internal(getattr(x, field)) for field in value.fields]))
```

## Implementations
Expand Down