예제 #1
0
    def create_mock_signed_attestation(state, shard_committee,
                                       voting_committee_indices,
                                       attestation_data):
        message = hash_eth2(
            rlp.encode(attestation_data) + (0).to_bytes(1, "big"))
        # participants sign message
        signatures = [
            bls.sign(message,
                     privkeys[shard_committee.committee[committee_index]],
                     domain=get_domain(
                         fork_data=state.fork_data,
                         slot=attestation_data.slot,
                         domain_type=SignatureDomain.DOMAIN_ATTESTATION,
                     )) for committee_index in voting_committee_indices
        ]

        # aggregate signatures and construct participant bitfield
        participation_bitfield, aggregate_signature = aggregate_votes(
            bitfield=get_empty_bitfield(len(shard_committee.committee)),
            sigs=(),
            voting_sigs=signatures,
            voting_committee_indices=voting_committee_indices,
        )

        # create attestation from attestation_data, particpipant_bitfield, and signature
        return Attestation(
            data=attestation_data,
            participation_bitfield=participation_bitfield,
            custody_bitfield=b'',
            aggregate_signature=aggregate_signature,
        )
예제 #2
0
def test_aggregate_votes(votes_count, random, privkeys, pubkeys):
    bit_count = 10
    pre_bitfield = get_empty_bitfield(bit_count)
    pre_sigs = ()

    random_votes = random.sample(range(bit_count), votes_count)
    message = b'hello'

    # Get votes: (committee_index, sig, public_key)
    votes = [(committee_index, bls.sign(message, privkeys[committee_index]),
              pubkeys[committee_index]) for committee_index in random_votes]

    # Verify
    sigs, committee_indices = verify_votes(message, votes)

    # Aggregate the votes
    bitfield, sigs = aggregate_votes(
        bitfield=pre_bitfield,
        sigs=pre_sigs,
        voting_sigs=sigs,
        voting_committee_indices=committee_indices)

    try:
        _, _, pubs = zip(*votes)
    except ValueError:
        pubs = ()

    voted_index = [
        committee_index for committee_index in random_votes
        if has_voted(bitfield, committee_index)
    ]
    assert len(voted_index) == len(votes)

    aggregated_pubs = bls.aggregate_pubs(pubs)
    assert bls.verify(message, aggregated_pubs, sigs)