Fix the definition of `GeneralizedIndex`

This commit is contained in:
Hsiao-Wei Wang 2019-08-23 20:16:46 +08:00
parent 7409b5ae82
commit bbaa238742
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
2 changed files with 7 additions and 12 deletions

View File

@ -37,7 +37,7 @@ from eth2spec.utils.bls import (
from eth2spec.utils.hash_function import hash from eth2spec.utils.hash_function import hash
''' '''
PHASE1_IMPORTS = '''from typing import ( PHASE1_IMPORTS = '''from typing import (
Any, Dict, Optional, Set, Sequence, MutableSequence, Tuple, Union, Any, Dict, Optional, Set, Sequence, MutableSequence, NewType, Tuple, Union,
) )
from math import ( from math import (
log2, log2,
@ -70,6 +70,7 @@ from eth2spec.utils.hash_function import hash
SSZVariableName = str SSZVariableName = str
GeneralizedIndex = NewType('GeneralizedIndex', int)
''' '''
SUNDRY_CONSTANTS_FUNCTIONS = ''' SUNDRY_CONSTANTS_FUNCTIONS = '''
def ceillog2(x: uint64) -> int: def ceillog2(x: uint64) -> int:

View File

@ -7,7 +7,6 @@
- [Merkle proof formats](#merkle-proof-formats) - [Merkle proof formats](#merkle-proof-formats)
- [Table of contents](#table-of-contents) - [Table of contents](#table-of-contents)
- [Custom types](#custom-types)
- [Helper functions](#helper-functions) - [Helper functions](#helper-functions)
- [Generalized Merkle tree index](#generalized-merkle-tree-index) - [Generalized Merkle tree index](#generalized-merkle-tree-index)
- [SSZ object to index](#ssz-object-to-index) - [SSZ object to index](#ssz-object-to-index)
@ -22,13 +21,6 @@
<!-- /TOC --> <!-- /TOC -->
## Custom types
We define the following Python custom types for type hinting and readability:
| - | - | - |
| `GeneralizedIndex` | `uint64` | the index of a node in a binary Merkle tree |
## Helper functions ## Helper functions
```python ```python
@ -75,6 +67,8 @@ def merkle_tree(leaves: Sequence[Hash]) -> Sequence[Hash]:
return o return o
``` ```
We define a custom type `GeneralizedIndex` as a Python integer type in this document. It can be represented as a Bitvector/Bitlist object as well.
We will define Merkle proofs in terms of generalized indices. We will define Merkle proofs in terms of generalized indices.
## SSZ object to index ## SSZ object to index
@ -175,7 +169,7 @@ def get_generalized_index(typ: SSZType, path: Sequence[Union[int, SSZVariableNam
else: else:
pos, _, _ = get_item_position(typ, p) pos, _, _ = get_item_position(typ, p)
base_index = (GeneralizedIndex(2) if issubclass(typ, (List, Bytes)) else GeneralizedIndex(1)) base_index = (GeneralizedIndex(2) if issubclass(typ, (List, Bytes)) else GeneralizedIndex(1))
root = root * base_index * get_next_power_of_two(chunk_count(typ)) + pos root = GeneralizedIndex(root * base_index * get_next_power_of_two(chunk_count(typ)) + pos)
typ = get_elem_type(typ, p) typ = get_elem_type(typ, p)
return root return root
``` ```
@ -280,8 +274,8 @@ def get_helper_indices(indices: Sequence[GeneralizedIndex]) -> Sequence[Generali
return sorted([ return sorted([
x for x in all_indices if ( x for x in all_indices if (
not ( not (
generalized_index_child(x, GeneralizedIndex(0)) in all_indices and generalized_index_child(x, False) in all_indices and
generalized_index_child(x, GeneralizedIndex(1)) in all_indices generalized_index_child(x, True) in all_indices
) and not (x in indices) ) and not (x in indices)
) )
], reverse=True) ], reverse=True)