def validate_aggregate_sig(crystallized_state: 'CrystallizedState', attestation: 'AttestationRecord', attestation_indices: Iterable[int], parent_hashes: Iterable[Hash32]) -> None: """ Validate ``aggregate_sig`` field. Raise ``ValidationError`` if it's invalid. """ pub_keys = [ crystallized_state.validators[validator_index].pubkey for committee_index, validator_index in enumerate(attestation_indices) if has_voted(attestation.attester_bitfield, committee_index) ] message = create_signing_message( attestation.slot, parent_hashes, attestation.shard_id, attestation.shard_block_hash, attestation.justified_slot, ) if not bls.verify(message, bls.aggregate_pubs(pub_keys), attestation.aggregate_sig): raise ValidationError("Attestation aggregate signature fails")
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 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 verify_votes( message: bytes, votes: Iterable[Tuple[int, bytes, int]] ) -> Tuple[Tuple[bytes, ...], Tuple[int, ...]]: """ Verify the given votes. vote: (committee_index, sig, public_key) """ sigs_with_committe_info = tuple( (sig, committee_index) for (committee_index, sig, public_key) in votes if bls.verify(message, public_key, sig)) try: sigs, committee_indices = zip(*sigs_with_committe_info) except ValueError: sigs = tuple() committee_indices = tuple() return sigs, committee_indices
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)