eth2.0-specs/test_libs/pyspec/eth2spec/test/helpers/custody.py

44 lines
1.4 KiB
Python
Raw Normal View History

2019-05-31 09:32:53 +00:00
from eth2spec.test.helpers.keys import privkeys
2019-06-22 14:56:16 +00:00
from eth2spec.utils.bls import bls_sign, bls_aggregate_signatures
2019-05-31 09:32:53 +00:00
def get_valid_early_derived_secret_reveal(spec, state, epoch=None):
current_epoch = spec.get_current_epoch(state)
revealed_index = spec.get_active_validator_indices(state, current_epoch)[-1]
masker_index = spec.get_active_validator_indices(state, current_epoch)[0]
if epoch is None:
epoch = current_epoch + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING
2019-06-22 14:56:16 +00:00
# Generate the secret that is being revealed
2019-05-31 09:32:53 +00:00
reveal = bls_sign(
message_hash=spec.hash_tree_root(epoch),
privkey=privkeys[revealed_index],
domain=spec.get_domain(
state=state,
domain_type=spec.DOMAIN_RANDAO,
message_epoch=epoch,
),
)
2019-06-22 14:56:16 +00:00
# Generate the mask (any random 32 bytes will do)
mask = reveal[:32]
# Generate masker's signature on the mask
masker_signature = bls_sign(
message_hash=mask,
2019-05-31 09:32:53 +00:00
privkey=privkeys[masker_index],
domain=spec.get_domain(
state=state,
domain_type=spec.DOMAIN_RANDAO,
message_epoch=epoch,
),
)
2019-06-22 14:56:16 +00:00
masked_reveal = bls_aggregate_signatures([reveal, masker_signature])
2019-05-31 09:32:53 +00:00
return spec.EarlyDerivedSecretReveal(
revealed_index=revealed_index,
epoch=epoch,
2019-06-22 14:56:16 +00:00
reveal=masked_reveal,
2019-05-31 09:32:53 +00:00
masker_index=masker_index,
mask=mask,
)