mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-02-26 01:05:15 +00:00
Merge pull request #1412 from ethereum/vbuterin-patch-1
Refactor Merkle proof verification
This commit is contained in:
commit
e5f7fafb2c
@ -289,23 +289,27 @@ def get_helper_indices(indices: Sequence[GeneralizedIndex]) -> Sequence[Generali
|
|||||||
Now we provide the Merkle proof verification functions. First, for single item proofs:
|
Now we provide the Merkle proof verification functions. First, for single item proofs:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def verify_merkle_proof(leaf: Hash, proof: Sequence[Hash], index: GeneralizedIndex, root: Hash) -> bool:
|
def calculate_merkle_root(leaf: Hash, proof: Sequence[Hash], index: GeneralizedIndex) -> Hash:
|
||||||
assert len(proof) == get_generalized_index_length(index)
|
assert len(proof) == get_generalized_index_length(index)
|
||||||
for i, h in enumerate(proof):
|
for i, h in enumerate(proof):
|
||||||
if get_generalized_index_bit(index, i):
|
if get_generalized_index_bit(index, i):
|
||||||
leaf = hash(h + leaf)
|
leaf = hash(h + leaf)
|
||||||
else:
|
else:
|
||||||
leaf = hash(leaf + h)
|
leaf = hash(leaf + h)
|
||||||
return leaf == root
|
return leaf
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
def verify_merkle_proof(leaf: Hash, proof: Sequence[Hash], index: GeneralizedIndex, root: Hash) -> bool:
|
||||||
|
return calculate_merkle_root(leaf, proof, index) == root
|
||||||
```
|
```
|
||||||
|
|
||||||
Now for multi-item proofs:
|
Now for multi-item proofs:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def verify_merkle_multiproof(leaves: Sequence[Hash],
|
def calculate_multi_merkle_root(leaves: Sequence[Hash],
|
||||||
proof: Sequence[Hash],
|
proof: Sequence[Hash],
|
||||||
indices: Sequence[GeneralizedIndex],
|
indices: Sequence[GeneralizedIndex]) -> Hash:
|
||||||
root: Hash) -> bool:
|
|
||||||
assert len(leaves) == len(indices)
|
assert len(leaves) == len(indices)
|
||||||
helper_indices = get_helper_indices(indices)
|
helper_indices = get_helper_indices(indices)
|
||||||
assert len(proof) == len(helper_indices)
|
assert len(proof) == len(helper_indices)
|
||||||
@ -324,7 +328,15 @@ def verify_merkle_multiproof(leaves: Sequence[Hash],
|
|||||||
)
|
)
|
||||||
keys.append(GeneralizedIndex(k // 2))
|
keys.append(GeneralizedIndex(k // 2))
|
||||||
pos += 1
|
pos += 1
|
||||||
return objects[GeneralizedIndex(1)] == root
|
return objects[GeneralizedIndex(1)]
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that the single-item proof is a special case of a multi-item proof; a valid single-item proof verifies correctly when put into the multi-item verification function (making the natural trivial changes to input arguments, `index -> [index]` and `leaf -> [leaf]`).
|
```python
|
||||||
|
def verify_merkle_multiproof(leaves: Sequence[Hash],
|
||||||
|
proof: Sequence[Hash],
|
||||||
|
indices: Sequence[GeneralizedIndex],
|
||||||
|
root: Hash) -> bool:
|
||||||
|
return calculate_multi_merkle_root(leaves, proof, indices) == root
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that the single-item proof is a special case of a multi-item proof; a valid single-item proof verifies correctly when put into the multi-item verification function (making the natural trivial changes to input arguments, `index -> [index]` and `leaf -> [leaf]`). Note also that `calculate_merkle_root` and `calculate_multi_merkle_root` can be used independently to compute the new Merkle root of a proof with leaves updated.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user