def test_validate_multiple(backend): bls.use(backend) msg = b"\x32" * 32 privkey_0 = 42 privkey_1 = 4242 pubkey_0 = bls.sk_to_pk(privkey_0) pubkey_1 = bls.sk_to_pk(privkey_1) aggregate_sig = bls.aggregate(bls.sign(privkey_0, msg), bls.sign(privkey_1, msg)) bls.validate(msg, aggregate_sig, pubkey_0, pubkey_1)
def test_validate_multiple_one_invalid(backend): bls.use(backend) msg = b"\x32" * 32 privkey_0 = 42 privkey_1 = 4242 privkey_other = 424242 pubkey_0 = bls.sk_to_pk(privkey_0) pubkey_1 = bls.sk_to_pk(privkey_1) aggregate_sig = bls.aggregate( bls.sign(privkey_0, msg), bls.sign(privkey_other, msg) ) with pytest.raises(SignatureError): bls.validate(msg, aggregate_sig, pubkey_0, pubkey_1)
def test_validate(backend): bls.use(backend) msg = b"\x32" * 32 privkey = 42 pubkey = bls.sk_to_pk(privkey) sig = bls.sign(privkey, msg) bls.validate(msg, sig, pubkey)
def test_validate_invalid(backend): bls.use(backend) msg = b"\x32" * 32 privkey = 42 pubkey = bls.sk_to_pk(privkey) sig = b"\x42" * 96 with pytest.raises(SignatureError): bls.validate(msg, sig, pubkey)
def create_key_pairs_for( validator_count: int) -> Iterable[Tuple[BLSPubkey, int]]: """ Generates ``validator_count`` key pairs derived in a deterministic manner based on the validator index in the ``range(validator_count)``. Returns a second map associating a public key with the validator's index in the set. """ for i in range(validator_count): private_key = generate_privkey_from_index(i) public_key = bls.sk_to_pk(private_key) yield public_key, private_key
def extract_privkeys_from_dir(dir_path: Path) -> Dict[BLSPubkey, int]: validator_keymap: Dict[BLSPubkey, int] = {} # pub -> priv try: key_files = os.listdir(dir_path) except FileNotFoundError: logger.debug("Could not find key directory: %s", str(dir_path)) return validator_keymap for key_file_name in key_files: key_file_path = dir_path / key_file_name privkey = _read_privkey(key_file_path) pubkey = bls.sk_to_pk(privkey) validator_keymap[pubkey] = privkey logger.debug("imported public key: %s", humanize_hash(Hash32(pubkey))) if len(validator_keymap) == 0: pass return validator_keymap
def _generate_pubkey(self, privkey): """ NOTE: this is currently our expensive function """ return bls.sk_to_pk(privkey)
state = BeaconState.create(**sample_beacon_state_params).set( "slot", state_slot) block = BeaconBlock.create(**sample_beacon_block_params).set( "slot", block_slot) if isinstance(expected, Exception): with pytest.raises(ValidationError): validate_block_slot(state, block) else: validate_block_slot(state, block) @pytest.mark.parametrize( "slots_per_epoch, max_committees_per_slot," "proposer_privkey, proposer_pubkey, is_valid_signature", ( (5, 5, 56, bls.sk_to_pk(56), True), (5, 5, 56, bls.sk_to_pk(56)[1:] + b"\x01", False), (5, 5, 123, bls.sk_to_pk(123), True), (5, 5, 123, bls.sk_to_pk(123)[1:] + b"\x01", False), ), ) def test_validate_proposer_signature( slots_per_epoch, max_committees_per_slot, proposer_privkey, proposer_pubkey, is_valid_signature, sample_beacon_block_params, sample_beacon_state_params, target_committee_size, max_effective_balance,
def sample_bls_public_key(sample_bls_private_key): return bls.sk_to_pk(sample_bls_private_key)
def mk_key_pair_from_seed_index(seed_index: int) -> Tuple[BLSPubkey, int]: privkey = int.from_bytes( hash_eth2(str(seed_index).encode("utf-8"))[:4], "big") pubkey = bls.sk_to_pk(privkey) return (pubkey, privkey)
def _compute_key_pair_from_private_key_bytes( private_key_bytes: bytes ) -> Tuple[BLSPubkey, BLSPrivateKey]: private_key = _deserialize_private_key(private_key_bytes) return (bls.sk_to_pk(private_key), private_key)