def test_num_recent_block_hashes(expected): recent_block_hashes = [blake(i.to_bytes(32, 'big')) for i in range(expected)] active_state = ActiveState( recent_block_hashes=recent_block_hashes, ) assert active_state.num_recent_block_hashes == expected
def hash_to_G2(m: bytes) -> Tuple[FQ2, FQ2, FQ2]: """ WARNING: this function has not been standardized yet. """ if m in CACHE: return CACHE[m] k2 = m while 1: k1 = blake(k2) k2 = blake(k1) x1 = int.from_bytes(k1, 'big') % field_modulus x2 = int.from_bytes(k2, 'big') % field_modulus x = FQ2([x1, x2]) xcb = x**3 + b2 if xcb**((field_modulus**2 - 1) // 2) == FQ2([1, 0]): break y = sqrt_fq2(xcb) o = multiply((x, y, FQ2([1, 0])), 2 * field_modulus - curve_order) CACHE[m] = o return o
def create_signing_message(slot: int, parent_hashes: Iterable[Hash32], shard_id: int, shard_block_hash: Hash32, justified_slot: int) -> bytes: """ Return the signining message for attesting. """ # TODO: Will be updated with SSZ encoded attestation. return blake( slot.to_bytes(8, byteorder='big') + b''.join(parent_hashes) + shard_id.to_bytes(2, byteorder='big') + shard_block_hash + justified_slot.to_bytes(8, 'big'))
def genesis_validators(init_validator_keys, init_randao, deposit_size, default_end_dynasty): current_dynasty = 1 return [ ValidatorRecord(pubkey=pub, withdrawal_shard=0, withdrawal_address=blake(pub.to_bytes(32, 'big'))[-20:], randao_commitment=init_randao, balance=deposit_size, start_dynasty=current_dynasty, end_dynasty=default_end_dynasty) for pub in init_validator_keys ]
def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_chain_tip: Hash32, index: int, pubkey: int, flag: int) -> Hash32: """ Compute the next hash in the validator registry delta hash chain. """ return blake( current_validator_registry_delta_chain_tip + flag.to_bytes(1, 'big') + index.to_bytes(3, 'big') + # TODO: currently, we use 256-bit pubkey which is different form the spec pubkey.to_bytes(32, 'big') )
def get_hashes_to_sign(recent_block_hashes: Sequence[Hash32], block: 'Block', cycle_length: int) -> Iterable[Hash32]: """ Given the head block to attest to, collect the list of hashes to be signed in the attestation. """ yield from get_hashes_from_recent_block_hashes( recent_block_hashes, block.slot_number, from_slot=block.slot_number - cycle_length + 1, to_slot=block.slot_number - 1, cycle_length=cycle_length, ) yield blake(block.hash)
def shuffle(values: Sequence[Any], seed: Hash32) -> Iterable[Any]: """ Returns the shuffled ``values`` with seed as entropy. Mainly for shuffling active validators in-protocol. Spec: https://github.com/ethereum/eth2.0-specs/blob/0941d592de7546a428066c0473fd1000a7e3e3af/specs/beacon-chain.md#helper-functions # noqa: E501 """ values_count = len(values) if values_count > SAMPLE_RANGE: raise ValueError( "values_count (%s) should less than or equal to SAMPLE_RANGE (%s)." % (values_count, SAMPLE_RANGE) ) output = [x for x in values] source = seed index = 0 while index < values_count: # Re-hash the source source = blake(source) for position in range(0, 30, 3): # gets indices 3 bytes at a time # Select a 3-byte sampled int sample_from_source = int.from_bytes(source[position:position + 3], 'big') # `remaining` is the size of remaining indices of this round remaining = values_count - index if remaining == 0: break # Set a random maximum bound of sample_from_source rand_max = SAMPLE_RANGE - SAMPLE_RANGE % remaining # Select `replacement_position` with the given `sample_from_source` and `remaining` if sample_from_source < rand_max: # Use random number to get `replacement_position`, where it's not `index` replacement_position = (sample_from_source % remaining) + index # Swap the index-th and replacement_position-th elements (output[index], output[replacement_position]) = ( output[replacement_position], output[index] ) index += 1 else: pass return output
def test_get_hashes_to_sign(genesis_block, beacon_config): cycle_length = beacon_config.cycle_length current_block_slot_number = 1 blocks, recent_block_hashes = generate_mock_recent_block_hashes( genesis_block, current_block_slot_number, cycle_length, ) block = blocks[current_block_slot_number] result = get_hashes_to_sign( recent_block_hashes, block, cycle_length, ) assert len(result) == cycle_length assert result[-1] == blake(block.hash)
def hash(self) -> Hash32: if self._hash is None: self._hash = blake(rlp.encode(self)) return self._hash
def test_hash(sample_block_params): block = BaseBeaconBlock(**sample_block_params) assert block.hash == blake(rlp.encode(block))
def test_blake(): output = blake(b'helloworld') assert len(output) == 32 assert output == b'\xf2@\xa8\x02\x04\x1b_\xaf\x89E\x02\xd42I\xe0\x80\xd5\xd3\xf7\xe2\xd4Q\xf2\xcf\xc9;#|\xb5\xd2\xeeo' # noqa: E501
def test_hash(sample_beacon_state_params): state = BeaconState(**sample_beacon_state_params) assert state.hash == blake(rlp.encode(state))
def test_chaindb_persist_block_and_unknown_parent(chaindb, block, seed): n_block = block.copy(ancestor_hashes=(blake(seed), )) with pytest.raises(ParentNotFound): chaindb.persist_block(n_block)
def privkeys(): return [int.from_bytes(blake(str(i).encode('utf-8'))[:4], 'big') for i in range(1000)]
def test_hash(sample_active_state_params): active_state = ActiveState(**sample_active_state_params) assert active_state.hash == blake(rlp.encode(active_state))
def test_hash(sample_crystallized_state_params): crystallized_state = CrystallizedState(**sample_crystallized_state_params) assert crystallized_state.hash == blake(rlp.encode(crystallized_state))