mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-01-14 12:46:06 +00:00
Add vbuterin's optimization and some formatting
This commit is contained in:
parent
92514716fb
commit
70e482be28
@ -59,7 +59,7 @@
|
|||||||
- [`get_epoch_start_slot`](#get_epoch_start_slot)
|
- [`get_epoch_start_slot`](#get_epoch_start_slot)
|
||||||
- [`is_active_validator`](#is_active_validator)
|
- [`is_active_validator`](#is_active_validator)
|
||||||
- [`get_active_validator_indices`](#get_active_validator_indices)
|
- [`get_active_validator_indices`](#get_active_validator_indices)
|
||||||
- [`get_permuted_index`](#get_permuted_index)
|
- [`shuffle`](#shuffle)
|
||||||
- [`split`](#split)
|
- [`split`](#split)
|
||||||
- [`get_epoch_committee_count`](#get_epoch_committee_count)
|
- [`get_epoch_committee_count`](#get_epoch_committee_count)
|
||||||
- [`get_shuffling`](#get_shuffling)
|
- [`get_shuffling`](#get_shuffling)
|
||||||
@ -693,27 +693,38 @@ def get_active_validator_indices(validators: List[Validator], epoch: EpochNumber
|
|||||||
return [i for i, v in enumerate(validators) if is_active_validator(v, epoch)]
|
return [i for i, v in enumerate(validators) if is_active_validator(v, epoch)]
|
||||||
```
|
```
|
||||||
|
|
||||||
### `get_permuted_index`
|
### `shuffle`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_permuted_index(index: int, list_size: int, seed: Bytes32) -> int:
|
def shuffle(list_size: int, seed: Bytes32) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Return `p(index)` in a pseudorandom permutation `p` of `0...list_size-1` with ``seed`` as entropy.
|
Return shuffled indices in a pseudorandom permutation `0...list_size-1` with ``seed`` as entropy.
|
||||||
|
|
||||||
Utilizes 'swap or not' shuffling found in
|
Utilizes 'swap or not' shuffling found in
|
||||||
https://link.springer.com/content/pdf/10.1007%2F978-3-642-32009-5_1.pdf
|
https://link.springer.com/content/pdf/10.1007%2F978-3-642-32009-5_1.pdf
|
||||||
See the 'generalized domain' algorithm on page 3.
|
See the 'generalized domain' algorithm on page 3.
|
||||||
"""
|
"""
|
||||||
|
indices = list(range(list_size))
|
||||||
|
power_of_two_numbers = [1, 2, 4, 8, 16, 32, 64, 128]
|
||||||
for round in range(90):
|
for round in range(90):
|
||||||
pivot = int.from_bytes(hash(seed + round.to_bytes(1, 'little')), 'little') % list_size
|
hash_bytes = b''.join([
|
||||||
|
hash(seed + int_to_bytes1(round) + int_to_bytes4(i))
|
||||||
flip = (pivot - index) % list_size
|
for i in range((list_size + 255) // 256)
|
||||||
hash_pos = max(index, flip)
|
])
|
||||||
h = hash(seed + round.to_bytes(1, 'little') + (hash_pos // 256).to_bytes(4, 'little'))
|
|
||||||
byte = h[(hash_pos % 256) // 8]
|
pivot = int.from_bytes(hash(seed + int_to_bytes1(round)), 'little') % list_size
|
||||||
bit = (byte >> (hash_pos % 8)) % 2
|
for i in range(list_size):
|
||||||
index = flip if bit else index
|
flip = (pivot - indices[i]) % list_size
|
||||||
return index
|
hash_position = indices[i] if indices[i] > flip else flip
|
||||||
|
byte = hash_bytes[hash_position // 8]
|
||||||
|
mask = power_of_two_numbers[hash_position % 8]
|
||||||
|
if byte & mask:
|
||||||
|
indices[i] = flip
|
||||||
|
else:
|
||||||
|
# not swap
|
||||||
|
pass
|
||||||
|
|
||||||
|
return indices
|
||||||
```
|
```
|
||||||
|
|
||||||
### `split`
|
### `split`
|
||||||
@ -763,9 +774,10 @@ def get_shuffling(seed: Bytes32,
|
|||||||
committees_per_epoch = get_epoch_committee_count(len(active_validator_indices))
|
committees_per_epoch = get_epoch_committee_count(len(active_validator_indices))
|
||||||
|
|
||||||
# Shuffle
|
# Shuffle
|
||||||
|
shuffled_indices = shuffle(len(active_validator_indices), seed)
|
||||||
shuffled_active_validator_indices = [
|
shuffled_active_validator_indices = [
|
||||||
active_validator_indices[get_permuted_index(i, len(active_validator_indices), seed)]
|
active_validator_indices[i]
|
||||||
for i in active_validator_indices
|
for i in shuffle(len(active_validator_indices), seed)
|
||||||
]
|
]
|
||||||
|
|
||||||
# Split the shuffled list into committees_per_epoch pieces
|
# Split the shuffled list into committees_per_epoch pieces
|
||||||
|
Loading…
x
Reference in New Issue
Block a user