def test_bls_core_succeed(backend, privkey): bls.use(backend) domain = 0 msg = str(privkey).encode('utf-8') sig = bls.sign(msg, privkey, domain=domain) pub = bls.privtopub(privkey) assert bls.verify(msg, pub, sig, domain=domain)
def test_signature_aggregation(backend, msg, privkeys, domain): bls.use(backend) sigs = [bls.sign(msg, k, domain=domain) for k in privkeys] pubs = [bls.privtopub(k) for k in privkeys] aggsig = bls.aggregate_signatures(sigs) aggpub = bls.aggregate_pubkeys(pubs) assert bls.verify(msg, aggpub, aggsig, domain=domain)
def test_sanity(backend): bls.use(backend) msg_0 = b"\x32" * 32 domain = 123 # Test: Verify the basic sign/verify process privkey_0 = 5566 sig_0 = bls.sign(msg_0, privkey_0, domain) assert_signature(sig_0) pubkey_0 = bls.privtopub(privkey_0) assert_pubkey(pubkey_0) assert bls.verify(msg_0, pubkey_0, sig_0, domain) privkey_1 = 5567 sig_1 = bls.sign(msg_0, privkey_1, domain) pubkey_1 = bls.privtopub(privkey_1) assert bls.verify(msg_0, pubkey_1, sig_1, domain) # Test: Verify signatures are correctly aggregated aggregated_signature = bls.aggregate_signatures([sig_0, sig_1]) assert_signature(aggregated_signature) # Test: Verify pubkeys are correctly aggregated aggregated_pubkey = bls.aggregate_pubkeys([pubkey_0, pubkey_1]) assert_pubkey(aggregated_pubkey) # Test: Verify with `aggregated_signature` and `aggregated_pubkey` assert bls.verify(msg_0, aggregated_pubkey, aggregated_signature, domain) # Test: `verify_multiple` msg_1 = b"x22" * 32 privkey_2 = 55688 sig_2 = bls.sign(msg_1, privkey_2, domain) assert_signature(sig_2) pubkey_2 = bls.privtopub(privkey_2) assert_pubkey(pubkey_2) sig_1_2 = bls.aggregate_signatures([sig_1, sig_2]) assert bls.verify_multiple( pubkeys=[pubkey_1, pubkey_2], message_hashes=[msg_0, msg_1], signature=sig_1_2, domain=domain, )
def process_deposit(state: BeaconState, deposit: Deposit, config: Eth2Config) -> BeaconState: """ Process a deposit from Ethereum 1.0. """ validate_deposit_proof(state, deposit, DEPOSIT_CONTRACT_TREE_DEPTH) # Increment the next deposit index we are expecting. Note that this # needs to be done here because while the deposit contract will never # create an invalid Merkle branch, it may admit an invalid deposit # object, and we need to be able to skip over it state = state.copy( eth1_deposit_index=state.eth1_deposit_index + 1, ) pubkey = deposit.data.pubkey amount = deposit.data.amount validator_pubkeys = tuple(v.pubkey for v in state.validators) if pubkey not in validator_pubkeys: # Verify the deposit signature (proof of possession) for new validators. # Note: The deposit contract does not check signatures. # Note: Deposits are valid across forks, thus the deposit domain # is retrieved directly from `compute_domain`. is_valid_proof_of_possession = bls.verify( message_hash=deposit.data.signing_root, pubkey=pubkey, signature=deposit.data.signature, domain=compute_domain( SignatureDomain.DOMAIN_DEPOSIT, ), ) if not is_valid_proof_of_possession: return state withdrawal_credentials = deposit.data.withdrawal_credentials validator = Validator.create_pending_validator( pubkey, withdrawal_credentials, amount, config, ) return state.copy( validators=state.validators + (validator,), balances=state.balances + (amount, ), ) else: index = ValidatorIndex(validator_pubkeys.index(pubkey)) return increase_balance( state, index, amount, )
def verify_votes( message_hash: Hash32, votes: Iterable[Tuple[ValidatorIndex, BLSSignature, BLSPubkey]], ) -> Tuple[Tuple[BLSSignature, ...], Tuple[ValidatorIndex, ...]]: """ Verify the given votes. """ sigs_with_committee_info = tuple( (sig, committee_index) for (committee_index, sig, pubkey) in votes if bls.verify(message_hash, sig, pubkey)) try: sigs, committee_indices = zip(*sigs_with_committee_info) except ValueError: sigs = tuple() committee_indices = tuple() return sigs, committee_indices
def run_with(_cls, inputs: SignatureData, _config: Optional[Eth2Config]) -> bool: # BLS override bls.use(MilagroBackend) return bls.verify(inputs["message"], inputs["signature"], inputs["pubkey"])