From f271d8b35856dcea8db8b3f52eede402e5fd312a Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 3 Oct 2018 14:45:33 +0100 Subject: [PATCH 1/2] Cleanups in get_active_validator_indices and shuffle --- specs/casper_sharding_v2.1.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/specs/casper_sharding_v2.1.md b/specs/casper_sharding_v2.1.md index e19ef1e0c..7c0d7231e 100644 --- a/specs/casper_sharding_v2.1.md +++ b/specs/casper_sharding_v2.1.md @@ -290,29 +290,25 @@ We start off by defining some helper algorithms. First, the function that select ```python def get_active_validator_indices(validators): - o = [] - for i in range(len(validators)): - if validators[i].status == LOGGED_IN: - o.append(i) - return o + return [i for i, v in enumerate(validators) if v.status == LOGGED_IN] ``` Now, a function that shuffles this list: ```python def shuffle(lst, seed): - assert len(lst) <= 16777216 + assert len(lst) <= MAX_VALIDATOR_COUNT o = [x for x in lst] source = seed i = 0 while i < len(lst): - source = blake(source) + source = hash(source) for pos in range(0, 30, 3): m = int.from_bytes(source[pos:pos+3], 'big') remaining = len(lst) - i if remaining == 0: break - rand_max = 16777216 - 16777216 % remaining + rand_max = MAX_VALIDATOR_COUNT - MAX_VALIDATOR_COUNT % remaining if m < rand_max: replacement_pos = (m % remaining) + i o[i], o[replacement_pos] = o[replacement_pos], o[i] From 86d0c209b78f2a02b46cfd7e411c90beee107a04 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Wed, 3 Oct 2018 09:28:42 -0500 Subject: [PATCH 2/2] fix rand_max in shuffle alg. add note about usage --- specs/beacon-chain.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/specs/beacon-chain.md b/specs/beacon-chain.md index 1b8fa4c41..ddea00e70 100644 --- a/specs/beacon-chain.md +++ b/specs/beacon-chain.md @@ -289,7 +289,11 @@ Now, a function that shuffles this list: ```python def shuffle(lst, seed): - assert len(lst) <= MAX_VALIDATOR_COUNT + # entropy is consumed in 3 byte chunks + # rand_max is defined to remove the modulo bias from this entropy source + rand_max = 2**24 + assert len(lst) <= rand_max + o = [x for x in lst] source = seed i = 0 @@ -300,7 +304,7 @@ def shuffle(lst, seed): remaining = len(lst) - i if remaining == 0: break - rand_max = MAX_VALIDATOR_COUNT - MAX_VALIDATOR_COUNT % remaining + rand_max = rand_max - rand_max % remaining if m < rand_max: replacement_pos = (m % remaining) + i o[i], o[replacement_pos] = o[replacement_pos], o[i]