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)
def attest_proposed_block(cls, post_crystallized_state: CrystallizedState, post_active_state: ActiveState, block_proposal: 'BlockProposal', chaindb: BaseBeaconChainDB, private_key: int) -> 'AttestationRecord': """ Return the initial attestation by the block proposer. The proposer broadcasts their attestation with the proposed block. """ block_committees_info = get_block_committees_info( block_proposal.block, post_crystallized_state, cls.config.CYCLE_LENGTH, ) # Vote attester_bitfield = set_voted( get_empty_bitfield(block_committees_info.proposer_committee_size), block_committees_info.proposer_index_in_committee, ) # Get justified_slot and justified_block_hash justified_slot = post_crystallized_state.last_justified_slot justified_block_hash = chaindb.get_canonical_block_hash_by_slot( justified_slot) # Get signing message and sign it parent_hashes = get_hashes_to_sign( post_active_state.recent_block_hashes, block_proposal.block, cls.config.CYCLE_LENGTH, ) message = create_signing_message( block_proposal.block.slot_number, parent_hashes, block_proposal.shard_id, block_proposal.shard_block_hash, justified_slot, ) sig = bls.sign( message, private_key, ) return cls.get_attestation_record_class()( slot=block_proposal.block.slot_number, shard_id=block_proposal.shard_id, oblique_parent_hashes=(), shard_block_hash=block_proposal.shard_block_hash, attester_bitfield=attester_bitfield, justified_slot=justified_slot, justified_block_hash=justified_block_hash, aggregate_sig=sig, )
def test_bls_core(privkey): p1 = multiply(G1, privkey) p2 = multiply(G2, privkey) msg = str(privkey).encode('utf-8') msghash = hash_to_G2(msg) assert normalize(decompress_G1(compress_G1(p1))) == normalize(p1) assert normalize(decompress_G2(compress_G2(p2))) == normalize(p2) assert normalize(decompress_G2(compress_G2(msghash))) == normalize(msghash) sig = sign(msg, privkey) pub = privtopub(privkey) assert verify(msg, pub, sig)
def test_signature_aggregation(msg, privkeys): sigs = [sign(msg, k) for k in privkeys] pubs = [privtopub(k) for k in privkeys] aggsig = aggregate_sigs(sigs) aggpub = aggregate_pubs(pubs) assert verify(msg, aggpub, aggsig)