merkle proofs: Fix get_helper_indices

This commit is contained in:
Cayman 2019-10-01 11:23:03 -05:00 committed by GitHub
parent 61f2a0662e
commit 56fd91b9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 8 deletions

View File

@ -258,6 +258,18 @@ def get_branch_indices(tree_index: GeneralizedIndex) -> Sequence[GeneralizedInde
return o[:-1]
```
```python
def get_path_indices(tree_index: GeneralizedIndex) -> Sequence[GeneralizedIndex]:
"""
Get the generalized indices of the chunks along the path from the chunk with the
given tree index to the root.
"""
o = [tree_index]
while o[-1] > 1:
o.append(generalized_index_parent(o[-1]))
return o[:-1]
```
```python
def get_helper_indices(indices: Sequence[GeneralizedIndex]) -> Sequence[GeneralizedIndex]:
"""
@ -265,17 +277,14 @@ def get_helper_indices(indices: Sequence[GeneralizedIndex]) -> Sequence[Generali
generalized indices. Note that the decreasing order is chosen deliberately to ensure equivalence to the
order of hashes in a regular single-item Merkle proof in the single-item case.
"""
all_indices: Set[GeneralizedIndex] = set()
all_helper_indices: Set[GeneralizedIndex] = set()
all_path_indices: Set[GeneralizedIndex] = set()
for index in indices:
all_indices = all_indices.union(set(list(get_branch_indices(index)) + [index]))
all_helper_indices = all_helper_indices.union(set(get_branch_indices(index)))
all_path_indices = all_path_indices.union(set(get_path_indices(index)))
return sorted([
x for x in all_indices if (
not (
generalized_index_child(x, False) in all_indices and
generalized_index_child(x, True) in all_indices
) and not (x in indices)
)
x for x in all_helper_indices if x not in all_path_indices
], reverse=True)
```