Merge pull request #1423 from wemeetagain/patch-4

merkle proofs: Fix get_helper_indices
This commit is contained in:
Diederik Loerakker 2019-10-23 17:17:34 +08:00 committed by GitHub
commit d8bf5c203a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 10 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,18 +277,13 @@ 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)
)
], reverse=True)
return sorted(all_helper_indices.difference(all_path_indices), reverse=True)
```
Now we provide the Merkle proof verification functions. First, for single item proofs: