Skip to content

Commit

Permalink
Merge pull request #386 from ralexstokes/clean-up-sig-fn
Browse files Browse the repository at this point in the history
drop unnecessary `verify_signature`
  • Loading branch information
ralexstokes authored Apr 22, 2024
2 parents 6f26186 + 7524d33 commit 72800f5
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 100 deletions.
14 changes: 6 additions & 8 deletions ethereum-consensus/src/altair/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ pub fn process_proposer_slashing<
let epoch = compute_epoch_at_slot(header_1.slot, context);
let domain = get_domain(state, DomainType::BeaconProposer, Some(epoch), context)?;
for signed_header in [&proposer_slashing.signed_header_1, &proposer_slashing.signed_header_2] {
let signing_root = compute_signing_root(&signed_header.message, domain)?;
let public_key = &proposer.public_key;
if verify_signature(public_key, signing_root.as_ref(), &signed_header.signature).is_err() {
if verify_signed_data(&signed_header.message, &signed_header.signature, public_key, domain)
.is_err()
{
return Err(invalid_operation_error(InvalidOperation::ProposerSlashing(
InvalidProposerSlashing::InvalidSignature(signed_header.signature.clone()),
)));
Expand Down Expand Up @@ -230,8 +231,7 @@ pub fn apply_deposit<
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&deposit_message, domain)?;
if verify_signature(public_key, signing_root.as_ref(), signature).is_err() {
if verify_signed_data(&deposit_message, signature, public_key, domain).is_err() {
return Ok(());
}
add_validator_to_registry(
Expand Down Expand Up @@ -466,8 +466,7 @@ pub fn process_randao<
let proposer_index = get_beacon_proposer_index(state, context)?;
let proposer = &state.validators[proposer_index];
let domain = get_domain(state, DomainType::Randao, Some(epoch), context)?;
let signing_root = compute_signing_root(&epoch, domain)?;
if verify_signature(&proposer.public_key, signing_root.as_ref(), &body.randao_reveal).is_err() {
if verify_signed_data(&epoch, &body.randao_reveal, &proposer.public_key, domain).is_err() {
return Err(invalid_operation_error(InvalidOperation::Randao(body.randao_reveal.clone())));
}
let mix = xor(get_randao_mix(state, epoch), &hash(body.randao_reveal.as_ref()));
Expand Down Expand Up @@ -1120,9 +1119,8 @@ pub fn verify_block_signature<
.get(proposer_index)
.ok_or(Error::OutOfBounds { requested: proposer_index, bound: state.validators.len() })?;
let domain = get_domain(state, DomainType::BeaconProposer, None, context)?;
let signing_root = compute_signing_root(&signed_block.message, domain)?;
let public_key = &proposer.public_key;
verify_signature(public_key, signing_root.as_ref(), &signed_block.signature).map_err(Into::into)
verify_signed_data(&signed_block.message, &signed_block.signature, public_key, domain)
}
pub fn get_domain<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down
14 changes: 6 additions & 8 deletions ethereum-consensus/src/bellatrix/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@ pub fn process_proposer_slashing<
let epoch = compute_epoch_at_slot(header_1.slot, context);
let domain = get_domain(state, DomainType::BeaconProposer, Some(epoch), context)?;
for signed_header in [&proposer_slashing.signed_header_1, &proposer_slashing.signed_header_2] {
let signing_root = compute_signing_root(&signed_header.message, domain)?;
let public_key = &proposer.public_key;
if verify_signature(public_key, signing_root.as_ref(), &signed_header.signature).is_err() {
if verify_signed_data(&signed_header.message, &signed_header.signature, public_key, domain)
.is_err()
{
return Err(invalid_operation_error(InvalidOperation::ProposerSlashing(
InvalidProposerSlashing::InvalidSignature(signed_header.signature.clone()),
)));
Expand Down Expand Up @@ -489,8 +490,7 @@ pub fn apply_deposit<
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&deposit_message, domain)?;
if verify_signature(public_key, signing_root.as_ref(), signature).is_err() {
if verify_signed_data(&deposit_message, signature, public_key, domain).is_err() {
return Ok(());
}
add_validator_to_registry(
Expand Down Expand Up @@ -753,8 +753,7 @@ pub fn process_randao<
let proposer_index = get_beacon_proposer_index(state, context)?;
let proposer = &state.validators[proposer_index];
let domain = get_domain(state, DomainType::Randao, Some(epoch), context)?;
let signing_root = compute_signing_root(&epoch, domain)?;
if verify_signature(&proposer.public_key, signing_root.as_ref(), &body.randao_reveal).is_err() {
if verify_signed_data(&epoch, &body.randao_reveal, &proposer.public_key, domain).is_err() {
return Err(invalid_operation_error(InvalidOperation::Randao(body.randao_reveal.clone())));
}
let mix = xor(get_randao_mix(state, epoch), &hash(body.randao_reveal.as_ref()));
Expand Down Expand Up @@ -2028,9 +2027,8 @@ pub fn verify_block_signature<
.get(proposer_index)
.ok_or(Error::OutOfBounds { requested: proposer_index, bound: state.validators.len() })?;
let domain = get_domain(state, DomainType::BeaconProposer, None, context)?;
let signing_root = compute_signing_root(&signed_block.message, domain)?;
let public_key = &proposer.public_key;
verify_signature(public_key, signing_root.as_ref(), &signed_block.signature).map_err(Into::into)
verify_signed_data(&signed_block.message, &signed_block.signature, public_key, domain)
}
pub fn get_domain<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down
34 changes: 16 additions & 18 deletions ethereum-consensus/src/capella/block_processing.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use crate::{
capella::{
compute_domain, compute_signing_root, compute_timestamp_at_slot, decrease_balance,
get_current_epoch, get_randao_mix, is_fully_withdrawable_validator,
is_partially_withdrawable_validator, process_attestation, process_attester_slashing,
process_block_header, process_deposit, process_eth1_data, process_proposer_slashing,
process_randao, process_sync_aggregate, process_voluntary_exit, BeaconBlock,
BeaconBlockBody, BeaconState, DomainType, ExecutionAddress, ExecutionPayload,
ExecutionPayloadHeader, SignedBlsToExecutionChange, Withdrawal,
compute_domain, compute_timestamp_at_slot, decrease_balance, get_current_epoch,
get_randao_mix, is_fully_withdrawable_validator, is_partially_withdrawable_validator,
process_attestation, process_attester_slashing, process_block_header, process_deposit,
process_eth1_data, process_proposer_slashing, process_randao, process_sync_aggregate,
process_voluntary_exit, BeaconBlock, BeaconBlockBody, BeaconState, DomainType,
ExecutionAddress, ExecutionPayload, ExecutionPayloadHeader, SignedBlsToExecutionChange,
Withdrawal,
},
crypto::{hash, verify_signature},
crypto::hash,
error::{
invalid_operation_error, InvalidBlsToExecutionChange, InvalidDeposit,
InvalidExecutionPayload, InvalidOperation, InvalidWithdrawals,
},
execution_engine::ExecutionEngine,
primitives::{BLS_WITHDRAWAL_PREFIX, ETH1_ADDRESS_WITHDRAWAL_PREFIX},
signing::verify_signed_data,
ssz::prelude::*,
state_transition::{Context, Result},
};
Expand Down Expand Up @@ -63,23 +64,20 @@ pub fn process_bls_to_execution_change<
)))
}

// NOTE: compute `signing_root` ahead of the public key check to satisfy borrow check
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
let signing_root = compute_signing_root(address_change, domain)?;

let public_key = &address_change.from_bls_public_key;
if withdrawal_credentials[1..] != hash(public_key.as_ref())[1..] {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::PublicKeyMismatch(public_key.clone()),
)))
}

verify_signature(public_key, signing_root.as_ref(), signature)?;
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
verify_signed_data(address_change, signature, public_key, domain)?;

withdrawal_credentials[0] = ETH1_ADDRESS_WITHDRAWAL_PREFIX;
withdrawal_credentials[1..12].fill(0);
Expand Down
14 changes: 6 additions & 8 deletions ethereum-consensus/src/capella/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ pub fn process_proposer_slashing<
let epoch = compute_epoch_at_slot(header_1.slot, context);
let domain = get_domain(state, DomainType::BeaconProposer, Some(epoch), context)?;
for signed_header in [&proposer_slashing.signed_header_1, &proposer_slashing.signed_header_2] {
let signing_root = compute_signing_root(&signed_header.message, domain)?;
let public_key = &proposer.public_key;
if verify_signature(public_key, signing_root.as_ref(), &signed_header.signature).is_err() {
if verify_signed_data(&signed_header.message, &signed_header.signature, public_key, domain)
.is_err()
{
return Err(invalid_operation_error(InvalidOperation::ProposerSlashing(
InvalidProposerSlashing::InvalidSignature(signed_header.signature.clone()),
)));
Expand Down Expand Up @@ -496,8 +497,7 @@ pub fn apply_deposit<
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&deposit_message, domain)?;
if verify_signature(public_key, signing_root.as_ref(), signature).is_err() {
if verify_signed_data(&deposit_message, signature, public_key, domain).is_err() {
return Ok(());
}
add_validator_to_registry(
Expand Down Expand Up @@ -768,8 +768,7 @@ pub fn process_randao<
let proposer_index = get_beacon_proposer_index(state, context)?;
let proposer = &state.validators[proposer_index];
let domain = get_domain(state, DomainType::Randao, Some(epoch), context)?;
let signing_root = compute_signing_root(&epoch, domain)?;
if verify_signature(&proposer.public_key, signing_root.as_ref(), &body.randao_reveal).is_err() {
if verify_signed_data(&epoch, &body.randao_reveal, &proposer.public_key, domain).is_err() {
return Err(invalid_operation_error(InvalidOperation::Randao(body.randao_reveal.clone())));
}
let mix = xor(get_randao_mix(state, epoch), &hash(body.randao_reveal.as_ref()));
Expand Down Expand Up @@ -2296,9 +2295,8 @@ pub fn verify_block_signature<
.get(proposer_index)
.ok_or(Error::OutOfBounds { requested: proposer_index, bound: state.validators.len() })?;
let domain = get_domain(state, DomainType::BeaconProposer, None, context)?;
let signing_root = compute_signing_root(&signed_block.message, domain)?;
let public_key = &proposer.public_key;
verify_signature(public_key, signing_root.as_ref(), &signed_block.signature).map_err(Into::into)
verify_signed_data(&signed_block.message, &signed_block.signature, public_key, domain)
}
pub fn get_domain<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down
29 changes: 13 additions & 16 deletions ethereum-consensus/src/deneb/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,19 @@ pub fn process_bls_to_execution_change<
InvalidBlsToExecutionChange::WithdrawalCredentialsPrefix(withdrawal_credentials[0]),
)));
}
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
let signing_root = compute_signing_root(address_change, domain)?;
let public_key = &address_change.from_bls_public_key;
if withdrawal_credentials[1..] != hash(public_key.as_ref())[1..] {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::PublicKeyMismatch(public_key.clone()),
)));
}
verify_signature(public_key, signing_root.as_ref(), signature)?;
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
verify_signed_data(address_change, signature, public_key, domain)?;
withdrawal_credentials[0] = ETH1_ADDRESS_WITHDRAWAL_PREFIX;
withdrawal_credentials[1..12].fill(0);
withdrawal_credentials[12..].copy_from_slice(address_change.to_execution_address.as_ref());
Expand Down Expand Up @@ -545,9 +544,10 @@ pub fn process_proposer_slashing<
let epoch = compute_epoch_at_slot(header_1.slot, context);
let domain = get_domain(state, DomainType::BeaconProposer, Some(epoch), context)?;
for signed_header in [&proposer_slashing.signed_header_1, &proposer_slashing.signed_header_2] {
let signing_root = compute_signing_root(&signed_header.message, domain)?;
let public_key = &proposer.public_key;
if verify_signature(public_key, signing_root.as_ref(), &signed_header.signature).is_err() {
if verify_signed_data(&signed_header.message, &signed_header.signature, public_key, domain)
.is_err()
{
return Err(invalid_operation_error(InvalidOperation::ProposerSlashing(
InvalidProposerSlashing::InvalidSignature(signed_header.signature.clone()),
)));
Expand Down Expand Up @@ -656,8 +656,7 @@ pub fn apply_deposit<
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&deposit_message, domain)?;
if verify_signature(public_key, signing_root.as_ref(), signature).is_err() {
if verify_signed_data(&deposit_message, signature, public_key, domain).is_err() {
return Ok(());
}
add_validator_to_registry(
Expand Down Expand Up @@ -860,8 +859,7 @@ pub fn process_randao<
let proposer_index = get_beacon_proposer_index(state, context)?;
let proposer = &state.validators[proposer_index];
let domain = get_domain(state, DomainType::Randao, Some(epoch), context)?;
let signing_root = compute_signing_root(&epoch, domain)?;
if verify_signature(&proposer.public_key, signing_root.as_ref(), &body.randao_reveal).is_err() {
if verify_signed_data(&epoch, &body.randao_reveal, &proposer.public_key, domain).is_err() {
return Err(invalid_operation_error(InvalidOperation::Randao(body.randao_reveal.clone())));
}
let mix = xor(get_randao_mix(state, epoch), &hash(body.randao_reveal.as_ref()));
Expand Down Expand Up @@ -2352,9 +2350,8 @@ pub fn verify_block_signature<
.get(proposer_index)
.ok_or(Error::OutOfBounds { requested: proposer_index, bound: state.validators.len() })?;
let domain = get_domain(state, DomainType::BeaconProposer, None, context)?;
let signing_root = compute_signing_root(&signed_block.message, domain)?;
let public_key = &proposer.public_key;
verify_signature(public_key, signing_root.as_ref(), &signed_block.signature).map_err(Into::into)
verify_signed_data(&signed_block.message, &signed_block.signature, public_key, domain)
}
pub fn get_domain<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down
29 changes: 13 additions & 16 deletions ethereum-consensus/src/electra/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,19 @@ pub fn process_bls_to_execution_change<
InvalidBlsToExecutionChange::WithdrawalCredentialsPrefix(withdrawal_credentials[0]),
)));
}
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
let signing_root = compute_signing_root(address_change, domain)?;
let public_key = &address_change.from_bls_public_key;
if withdrawal_credentials[1..] != hash(public_key.as_ref())[1..] {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::PublicKeyMismatch(public_key.clone()),
)));
}
verify_signature(public_key, signing_root.as_ref(), signature)?;
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
verify_signed_data(address_change, signature, public_key, domain)?;
withdrawal_credentials[0] = ETH1_ADDRESS_WITHDRAWAL_PREFIX;
withdrawal_credentials[1..12].fill(0);
withdrawal_credentials[12..].copy_from_slice(address_change.to_execution_address.as_ref());
Expand Down Expand Up @@ -770,9 +769,10 @@ pub fn process_proposer_slashing<
let epoch = compute_epoch_at_slot(header_1.slot, context);
let domain = get_domain(state, DomainType::BeaconProposer, Some(epoch), context)?;
for signed_header in [&proposer_slashing.signed_header_1, &proposer_slashing.signed_header_2] {
let signing_root = compute_signing_root(&signed_header.message, domain)?;
let public_key = &proposer.public_key;
if verify_signature(public_key, signing_root.as_ref(), &signed_header.signature).is_err() {
if verify_signed_data(&signed_header.message, &signed_header.signature, public_key, domain)
.is_err()
{
return Err(invalid_operation_error(InvalidOperation::ProposerSlashing(
InvalidProposerSlashing::InvalidSignature(signed_header.signature.clone()),
)));
Expand Down Expand Up @@ -894,8 +894,7 @@ pub fn apply_deposit<
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&deposit_message, domain)?;
if verify_signature(public_key, signing_root.as_ref(), signature).is_err() {
if verify_signed_data(&deposit_message, signature, public_key, domain).is_err() {
return Ok(());
}
add_validator_to_registry(
Expand Down Expand Up @@ -1134,8 +1133,7 @@ pub fn process_randao<
let proposer_index = get_beacon_proposer_index(state, context)?;
let proposer = &state.validators[proposer_index];
let domain = get_domain(state, DomainType::Randao, Some(epoch), context)?;
let signing_root = compute_signing_root(&epoch, domain)?;
if verify_signature(&proposer.public_key, signing_root.as_ref(), &body.randao_reveal).is_err() {
if verify_signed_data(&epoch, &body.randao_reveal, &proposer.public_key, domain).is_err() {
return Err(invalid_operation_error(InvalidOperation::Randao(body.randao_reveal.clone())));
}
let mix = xor(get_randao_mix(state, epoch), &hash(body.randao_reveal.as_ref()));
Expand Down Expand Up @@ -2993,9 +2991,8 @@ pub fn verify_block_signature<
.get(proposer_index)
.ok_or(Error::OutOfBounds { requested: proposer_index, bound: state.validators.len() })?;
let domain = get_domain(state, DomainType::BeaconProposer, None, context)?;
let signing_root = compute_signing_root(&signed_block.message, domain)?;
let public_key = &proposer.public_key;
verify_signature(public_key, signing_root.as_ref(), &signed_block.signature).map_err(Into::into)
verify_signed_data(&signed_block.message, &signed_block.signature, public_key, domain)
}
pub fn get_domain<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down
Loading

0 comments on commit 72800f5

Please sign in to comment.