Specify shuffling algorithm (#37)

This commit is contained in:
Daniel Sanchez 2024-01-31 09:33:44 +01:00 committed by GitHub
parent ff09cf8bb8
commit 7df4f9556f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 2 deletions

View File

@ -13,6 +13,20 @@ def blake2b_hash(committee: Committee) -> bytes:
return hasher.digest()
def fisher_yates_shuffle(nodes: List[Id], entropy: bytes):
"""
Fisher-yates shuffling algorithm
It is the one used by python by default
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
https://softwareengineering.stackexchange.com/a/215780
:param nodes:
:param entropy:
:return:
"""
random.seed(a=entropy, version=2)
random.shuffle(nodes)
class CarnotTree:
"""
This balanced binary tree implementation uses a combination of indexes and keys to easily calculate parenting
@ -134,8 +148,7 @@ class CarnotOverlay(EntropyOverlay):
self.number_of_committees = number_of_committees
self.nodes = nodes.copy()
self.current_leader = current_leader
random.seed(a=self.entropy, version=2)
random.shuffle(self.nodes)
fisher_yates_shuffle(self.nodes, self.entropy)
self.carnot_tree = CarnotTree(nodes, number_of_committees)
def advance(self, entropy: bytes) -> Self: