diff --git a/Cargo.lock b/Cargo.lock index 7da4053ce..d9b866e8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2057,6 +2057,7 @@ dependencies = [ "hex", "leanvm", "postcard", + "rand 0.10.1", "rayon", "thiserror 2.0.18", "tracing", diff --git a/crates/blockchain/state_transition/src/beacon/block_production.rs b/crates/blockchain/state_transition/src/beacon/block_production.rs index 0741d466b..22219df0e 100644 --- a/crates/blockchain/state_transition/src/beacon/block_production.rs +++ b/crates/blockchain/state_transition/src/beacon/block_production.rs @@ -51,6 +51,7 @@ use super::helpers::fulu::fulu_state; use super::helpers::misc::compute_signing_root; use super::helpers::predicates::is_slashable_validator; use super::lean_boundary::lean_state_unreachable; +use super::stf::signatures::SignatureBatch; use super::stf::{self, BlockCaches, ExecutionEngine, OwnedBlockCaches}; use ethlambda_storage::pools::single_committee; @@ -390,8 +391,14 @@ pub fn pack_operations(state: &BeaconState, candidates: Operations, config: &Con candidates.proposer_slashings, preset::MAX_PROPOSER_SLASHINGS, config, - |state, slashing, config| { - stf::operations::process_proposer_slashing(state, slashing, config, &active_balances) + |state, slashing, config, signatures| { + stf::operations::process_proposer_slashing( + state, + slashing, + config, + &active_balances, + signatures, + ) }, ); @@ -409,8 +416,14 @@ pub fn pack_operations(state: &BeaconState, candidates: Operations, config: &Con ranked.into_iter().map(|(_, slashing)| slashing).collect(), preset::MAX_ATTESTER_SLASHINGS_ELECTRA, config, - |state, slashing, config| { - stf::electra::process_attester_slashing(state, slashing, config, &active_balances) + |state, slashing, config, signatures| { + stf::electra::process_attester_slashing( + state, + slashing, + config, + &active_balances, + signatures, + ) }, ); @@ -419,8 +432,8 @@ pub fn pack_operations(state: &BeaconState, candidates: Operations, config: &Con candidates.voluntary_exits, preset::MAX_VOLUNTARY_EXITS, config, - |state, exit, config| { - stf::electra::process_voluntary_exit(state, exit, config, &active_balances) + |state, exit, config, signatures| { + stf::electra::process_voluntary_exit(state, exit, config, &active_balances, signatures) }, ); let bls_to_execution_changes = pack_each( @@ -456,7 +469,7 @@ fn pack_each( candidates: Vec, max: usize, config: &Config, - apply: impl Fn(&mut BeaconState, &T, &Config) -> Result<()>, + apply: impl Fn(&mut BeaconState, &T, &Config, &mut SignatureBatch) -> Result<()>, ) -> Vec { let mut packed = Vec::new(); // Bounds the work if the pool is full of stale entries. @@ -464,7 +477,11 @@ fn pack_each( if packed.len() == max { break; } - if apply(scratch, &candidate, config).is_ok() { + // An immediate batch, so a bad signature rejects the candidate before + // it writes, which is what lets a rejected candidate leave `scratch` + // untouched (see above); a deferred batch would only fail at the end. + let mut signatures = SignatureBatch::immediate(); + if apply(scratch, &candidate, config, &mut signatures).is_ok() { packed.push(candidate); } } diff --git a/crates/blockchain/state_transition/src/beacon/fork_choice.rs b/crates/blockchain/state_transition/src/beacon/fork_choice.rs index ad59b54fb..ffe6d99b7 100644 --- a/crates/blockchain/state_transition/src/beacon/fork_choice.rs +++ b/crates/blockchain/state_transition/src/beacon/fork_choice.rs @@ -1054,11 +1054,14 @@ pub fn advance_to_epoch_start( /// the precomputed epoch-boundary state when the block crosses into an epoch /// and one is cached. /// -/// Returns the copy with the transition's outcome, since a failed transition -/// still leaves the caller a state to inspect. Both starting points reach the -/// same post-state: the precomputed one is what `process_slots` would produce -/// from the parent. Either way the copy is independent, so a failing block -/// cannot corrupt a cached entry. +/// Returns the post-state, or the error and no state: a failed transition +/// leaves its copy partly applied (the operations before the failing check, +/// and the attestations and sync aggregate whose signatures were deferred to +/// the end of the block; see `stf::signatures`), and dropping it here is what +/// keeps any caller from reading one. Both starting points reach the same +/// post-state: the precomputed one is what `process_slots` would produce from +/// the parent. Either way the copy is independent, so a failing block cannot +/// corrupt a cached entry. /// /// This is also where the epoch-boundary balances of a crossing block are /// built (see [`cache_boundary_balances`]): a block with empty slots between @@ -1080,7 +1083,7 @@ fn transition_block( config: &Config, engine: &stf::ExecutionEngine, caches: stf::BlockCaches<'_>, -) -> (BeaconState, Result<()>) { +) -> Result { let block_slot = signed_block.slot(); let epoch = compute_epoch_at_slot(block_slot); let epoch_start = compute_start_slot_at_epoch(epoch); @@ -1098,32 +1101,30 @@ fn transition_block( else { let mut state = parent_state.clone(); if !crosses_with_empty_slots { - let outcome = stf::state_transition( + stf::state_transition( &mut state, signed_block, validate_result, config, engine, caches, - ); - return (state, outcome); + )?; + return Ok(state); } // The same `process_slots` steps `state_transition` would run, split // at the epoch start so the boundary state can be read in between. - let outcome = stf::process_slots(&mut state, epoch_start, config) - .inspect(|()| cache_boundary_balances(store, boundary, &state)) - .and_then(|()| stf::process_slots(&mut state, block_slot, config)) - .and_then(|()| { - stf::apply_block( - &mut state, - signed_block, - validate_result, - config, - engine, - caches, - ) - }); - return (state, outcome); + stf::process_slots(&mut state, epoch_start, config)?; + cache_boundary_balances(store, boundary, &state); + stf::process_slots(&mut state, block_slot, config)?; + stf::apply_block( + &mut state, + signed_block, + validate_result, + config, + engine, + caches, + )?; + return Ok(state); }; if crosses_with_empty_slots { @@ -1132,22 +1133,18 @@ fn transition_block( let mut state = (*precomputed).clone(); // Only the slots skipped past the boundary are left to advance; none when // the block sits on the boundary itself. - let outcome = if state.slot() < block_slot { - stf::process_slots(&mut state, block_slot, config) - } else { - Ok(()) + if state.slot() < block_slot { + stf::process_slots(&mut state, block_slot, config)?; } - .and_then(|()| { - stf::apply_block( - &mut state, - signed_block, - validate_result, - config, - engine, - caches, - ) - }); - (state, outcome) + stf::apply_block( + &mut state, + signed_block, + validate_result, + config, + engine, + caches, + )?; + Ok(state) } /// The precomputed epoch-boundary state an import can resume from, if any. @@ -5094,8 +5091,8 @@ pub struct SpeculativeTransition { /// [`Self::stands_in_for`] compares it with the one `on_block` would pick /// rather than restating which verdicts that is. engine: stf::ExecutionEngine, - state: BeaconState, - outcome: Result<()>, + /// What [`transition_block`] returned: the post-state, or the error alone. + transition: Result, } impl SpeculativeTransition { @@ -5128,20 +5125,20 @@ impl SpeculativeTransition { /// /// Outcomes are compared through their `Debug` rendering, since the error /// type carries no `PartialEq`, and roots only on success, since a failed - /// transition's partial state is never used. Debug builds only: it doubles - /// the transition, which is the cost the speculation exists to hide. + /// transition has no state. Debug builds only: it doubles the transition, + /// which is the cost the speculation exists to hide. #[cfg(debug_assertions)] - fn assert_agrees_with(&self, fresh: &(BeaconState, Result<()>)) { - let (fresh_state, fresh_outcome) = fresh; + fn assert_agrees_with(&self, fresh: &Result) { + let outcome = |transition: &Result| format!("{:?}", transition.as_ref().err()); assert_eq!( - format!("{:?}", self.outcome), - format!("{fresh_outcome:?}"), + outcome(&self.transition), + outcome(fresh), "the speculative transition disagrees with on_block's own on the outcome" ); - if self.outcome.is_ok() { + if let (Ok(speculated), Ok(fresh)) = (&self.transition, fresh) { assert_eq!( - self.state.hash_tree_root(), - fresh_state.hash_tree_root(), + speculated.hash_tree_root(), + fresh.hash_tree_root(), "the speculative transition disagrees with on_block's own on the post-state root" ); } @@ -5253,7 +5250,7 @@ pub fn speculate_transition( pubkey_indices: &pubkey_indices, }; let engine = stf::ExecutionEngine::valid(); - let (state, outcome) = transition_block( + let transition = transition_block( store, &parent_state, parent_root, @@ -5267,8 +5264,7 @@ pub fn speculate_transition( block_root, parent_root, engine, - state, - outcome, + transition, }) } @@ -5460,11 +5456,11 @@ pub fn on_block_speculative( caches, ) }; - let (mut state, transition) = match usable { + let transition = match usable { Some(speculation) => { #[cfg(debug_assertions)] speculation.assert_agrees_with(&fresh_transition()); - (speculation.state, speculation.outcome) + speculation.transition } None => fresh_transition(), }; @@ -5509,7 +5505,7 @@ pub fn on_block_speculative( ))); } - transition?; + let mut state = transition?; // A block at its epoch's first slot is its own epoch-boundary block: its // post-state is the checkpoint state. Cached at import so the tick that @@ -6806,7 +6802,7 @@ mod tests { let (signed, _) = block_on(&parent, first_slot); let mut store = empty_store(); - let (mut post, outcome) = transition_block( + let mut post = transition_block( &store, &parent, parent_root, @@ -6815,8 +6811,8 @@ mod tests { &config, &stf::ExecutionEngine::valid(), stf::OwnedBlockCaches::default().caches(), - ); - outcome.expect("applies"); + ) + .expect("applies"); post.apply_pending_mutations(); assert!( store @@ -7870,7 +7866,7 @@ mod tests { config: &Config, ) -> Option { let mut speculation = honest_speculation(store, block, config)?; - speculation.outcome = Err(Error::SpecAssert(POISON)); + speculation.transition = Err(Error::SpecAssert(POISON)); Some(speculation) } @@ -7917,10 +7913,11 @@ mod tests { import_valid_child(&PayloadValidity::Validated, |store, block, config| { let mut speculation = honest_speculation(store, block, config)?; speculation - .outcome - .as_ref() - .expect("the transition succeeds"); - speculation.state.latest_block_header_mut().body_root = Root::repeat_byte(0xab); + .transition + .as_mut() + .expect("the transition succeeds") + .latest_block_header_mut() + .body_root = Root::repeat_byte(0xab); Some(speculation) }); result.expect("the import takes the speculation's outcome"); @@ -11666,7 +11663,7 @@ mod tests { parent_root: Root, block: &SignedBeaconBlock, ) -> Root { - let (mut state, outcome) = transition_block( + let mut state = transition_block( store, parent, parent_root, @@ -11675,8 +11672,8 @@ mod tests { &Config::mainnet(), &stf::ExecutionEngine::valid(), stf::OwnedBlockCaches::default().caches(), - ); - outcome.expect("the block applies"); + ) + .expect("the block applies"); state.apply_pending_mutations(); state.hash_tree_root() } diff --git a/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs b/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs index a1b9dd13f..9097d39e0 100644 --- a/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs +++ b/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs @@ -422,7 +422,7 @@ pub fn assemble_gloas_block_on_bid( pubkey_indices, ..scratch.caches() }; - stf::gloas::process_block(&mut post, &block, config, caches, options)?; + stf::gloas::process_block_and_verify(&mut post, &block, config, caches, options)?; block.state_root = post.hash_tree_root(); Ok(block) } @@ -553,7 +553,7 @@ pub fn assemble_gloas_block( pubkey_indices, ..scratch.caches() }; - stf::gloas::process_block(&mut post, &block, config, caches, options)?; + stf::gloas::process_block_and_verify(&mut post, &block, config, caches, options)?; block.state_root = post.hash_tree_root(); let envelope = ExecutionPayloadEnvelope { @@ -769,7 +769,7 @@ pub mod test_support { /// The state after `block` is applied to `state`, as the chain stores it. pub fn post_state(state: &BeaconState, block: &BeaconBlock) -> BeaconState { let mut post = state.clone(); - stf::gloas::process_block( + stf::gloas::process_block_and_verify( &mut post, block, &config(), @@ -1094,7 +1094,7 @@ mod gloas_block_production_tests { // Importing it with the same option lands on the state root it names. let mut post = state.clone(); - stf::gloas::process_block( + stf::gloas::process_block_and_verify( &mut post, &produced.block, &config(), diff --git a/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs b/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs index d95914a61..b73b8e025 100644 --- a/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs +++ b/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs @@ -444,7 +444,7 @@ pub fn stateful_checks( // (cached) committees, so this costs no further shuffle. let attester_domain = get_domain_from_schedule(&config, &state, DOMAIN_BEACON_ATTESTER, target_epoch); - let attesting_indices = match aggregate { + let (attesting_indices, indexed_data, aggregate_signature) = match aggregate { SignedAggregateAndProof::Phase0(signed) => { let phase0_attestation = &signed.message.aggregate; let indexed = crate::beacon::helpers::attestation::get_indexed_attestation( @@ -453,14 +453,11 @@ pub fn stateful_checks( &committees, ) .map_err(|_| Outcome::Ignore(IgnoreReason::Internal))?; - if !crate::beacon::helpers::attestation::is_valid_indexed_attestation_with_domain( - &state, - &indexed, - attester_domain, - ) { - return Err(Outcome::Reject(RejectReason::AggregateSignature)); - } - indexed.attesting_indices.to_vec() + ( + indexed.attesting_indices.to_vec(), + indexed.data, + indexed.signature, + ) } SignedAggregateAndProof::Electra(signed) => { let electra_attestation = &signed.message.aggregate; @@ -470,14 +467,11 @@ pub fn stateful_checks( &committees, ) .map_err(|_| Outcome::Ignore(IgnoreReason::Internal))?; - if !crate::beacon::helpers::electra::is_valid_indexed_attestation_with_domain( - &state, - &indexed, - attester_domain, - ) { - return Err(Outcome::Reject(RejectReason::AggregateSignature)); - } - indexed.attesting_indices.to_vec() + ( + indexed.attesting_indices.to_vec(), + indexed.data, + indexed.signature, + ) } SignedAggregateAndProof::Gloas(signed) => { let gloas_attestation = &signed.message.aggregate; @@ -487,16 +481,24 @@ pub fn stateful_checks( &committees, ) .map_err(|_| Outcome::Ignore(IgnoreReason::Internal))?; - if !crate::beacon::helpers::gloas::is_valid_indexed_attestation_with_domain( - &state, - &indexed, - attester_domain, - ) { - return Err(Outcome::Reject(RejectReason::AggregateSignature)); - } - indexed.attesting_indices.to_vec() + ( + indexed.attesting_indices.to_vec(), + indexed.data, + indexed.signature, + ) } }; + // Every fork's `is_valid_indexed_attestation`, under the schedule's domain. + let signature_set = crate::beacon::helpers::attestation::attesters_signature_set( + &state, + &attesting_indices, + &indexed_data, + &aggregate_signature, + Some(attester_domain), + ); + if !signature_set.is_some_and(|set| set.verify()) { + return Err(Outcome::Reject(RejectReason::AggregateSignature)); + } // Ancestry, via the vote state's own history; see this function's // documentation for why this state can always answer both questions. diff --git a/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs b/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs index b225195c9..1f1b62ae5 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs @@ -9,14 +9,17 @@ //! the aggregation bits from one committee to a whole slot's worth and adding a //! committee bitfield, so it gets its own versions rather than reusing these. +use crate::beacon::bls::SignatureSet; +use crate::beacon::constants; use crate::beacon::containers::BeaconState; use crate::beacon::containers::phase0::{Attestation, AttestingIndices, IndexedAttestation}; +use crate::beacon::containers::shared::AttestationData; use crate::beacon::error::Result; use crate::beacon::helpers::accessors::{CommitteeCache, CommitteeCacheExt, get_domain}; use crate::beacon::helpers::misc::{compute_epoch_at_slot, compute_signing_root}; use crate::beacon::helpers::predicates::are_indices_sorted_and_unique; -use crate::beacon::primitives::{Domain, HashTreeRoot as _, ValidatorIndex}; -use crate::beacon::{bls, constants}; +use crate::beacon::preset; +use crate::beacon::primitives::{BlsSignature, Domain, HashTreeRoot as _, ValidatorIndex}; /// The committee members whose bit is set in `attestation`, in ascending order. /// @@ -79,37 +82,97 @@ pub fn is_valid_indexed_attestation( state: &BeaconState, indexed_attestation: &IndexedAttestation, ) -> bool { - let domain = get_domain( + indexed_attestation_signature_set(state, indexed_attestation).is_some_and(|set| set.verify()) +} + +/// The signature check [`is_valid_indexed_attestation`] makes, resolved but +/// not yet run, so a block can verify every attestation's signature in one +/// batch. See [`attesters_signature_set`] for the checks. +pub fn indexed_attestation_signature_set( + state: &BeaconState, + indexed_attestation: &IndexedAttestation, +) -> Option { + attesters_signature_set( state, - constants::DOMAIN_BEACON_ATTESTER, - Some(indexed_attestation.data.target.epoch), - ); - is_valid_indexed_attestation_with_domain(state, indexed_attestation, domain) + &indexed_attestation.attesting_indices, + &indexed_attestation.data, + &indexed_attestation.signature, + None, + ) } -/// [`is_valid_indexed_attestation`] under a signing domain the caller chose. +/// `is_valid_indexed_attestation`'s check for any fork's indexed attestation, +/// resolved but not yet run: the one definition of the rules phase0's, +/// electra's and gloas's versions share, which differ only in the container +/// they read `attesting_indices`, `data` and `signature` from. +/// +/// Every structural check happens here, at once, and `None` means the +/// attestation is invalid without needing a pairing: an empty attester list, +/// one longer than `MAX_VALIDATORS_PER_SLOT`, one not sorted and unique, an +/// index outside the registry, or a key or signature that does not validate. +/// The length bound is gloas's (EIP-7688 made its `AttestingIndices` an +/// unbounded progressive list); every earlier fork's list type is bounded at +/// or below it, so the check never changes an earlier fork's answer. /// -/// For gossip, which checks against a state that may not have reached the +/// `domain` is the state's `DOMAIN_BEACON_ATTESTER` at the target epoch when +/// `None`. Gossip passes its own, for a state that may not have reached the /// attestation's fork: see /// [`crate::beacon::helpers::accessors::get_domain_from_schedule`]. -pub fn is_valid_indexed_attestation_with_domain( +pub(crate) fn attesters_signature_set( state: &BeaconState, - indexed_attestation: &IndexedAttestation, - domain: Domain, -) -> bool { - let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; - if indices.is_empty() || !are_indices_sorted_and_unique(indices) { - return false; + attesting_indices: &[ValidatorIndex], + data: &AttestationData, + signature: &BlsSignature, + domain: Option, +) -> Option { + if attesting_indices.is_empty() + || attesting_indices.len() > preset::MAX_VALIDATORS_PER_SLOT + || !are_indices_sorted_and_unique(attesting_indices) + { + return None; } - let mut pubkeys = Vec::with_capacity(indices.len()); - for index in indices { + let mut pubkeys = Vec::with_capacity(attesting_indices.len()); + for index in attesting_indices { match state.validator(*index) { Ok(validator) => pubkeys.push(validator.pubkey), - Err(_) => return false, + Err(_) => return None, } } - let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &indexed_attestation.signature) + let domain = domain.unwrap_or_else(|| { + get_domain( + state, + constants::DOMAIN_BEACON_ATTESTER, + Some(data.target.epoch), + ) + }); + let signing_root = compute_signing_root(data.hash_tree_root(), domain); + SignatureSet::fast_aggregate(&pubkeys, signing_root, signature) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::beacon::helpers::test_state::{sign_for, with_validators}; + use crate::beacon::primitives::Root; + + /// The attester-list rules every fork shares refuse before any pairing, + /// and a well-formed list with a well-formed signature builds a set. + #[test] + fn only_a_sorted_unique_list_of_known_validators_builds_a_set() { + let state = with_validators(8); + let data = AttestationData::default(); + // Any valid point: whether it verifies is the pairing's question. + let signature = sign_for(1, Root::repeat_byte(1)); + let build = |indices: &[ValidatorIndex]| { + attesters_signature_set(&state, indices, &data, &signature, None) + }; + + assert!(build(&[1, 2]).is_some(), "well formed"); + assert!(build(&[]).is_none(), "empty"); + assert!(build(&[2, 1]).is_none(), "unsorted"); + assert!(build(&[1, 1]).is_none(), "repeated"); + assert!(build(&[1, 8]).is_none(), "outside the registry"); + } } diff --git a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index 9ea0cb873..f8711f9dd 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -123,7 +123,7 @@ //! single easiest mistake to make copying that shape without also copying //! the reasoning behind it. -use crate::beacon::bls; +use crate::beacon::bls::SignatureSet; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::shared::Validator; @@ -134,18 +134,15 @@ use crate::beacon::hash::hash; use crate::beacon::lean_state_unreachable; use crate::beacon::preset; use crate::beacon::primitives::{ - BLS_SIGNATURE_SIZE, BlsSignature, Bytes32, CommitteeIndex, Domain, Epoch, Gwei, - HashTreeRoot as _, Slot, ValidatorIndex, + BLS_SIGNATURE_SIZE, BlsSignature, Bytes32, CommitteeIndex, Epoch, Gwei, Slot, ValidatorIndex, }; use super::accessors::{ ActiveBalanceCache, CommitteeCache, CommitteeCacheExt, get_active_validator_indices, - get_current_epoch, get_domain, get_seed, get_total_active_balance, - total_active_balance_or_compute, + get_current_epoch, get_seed, get_total_active_balance, total_active_balance_or_compute, }; use super::math::bytes_to_uint64; -use super::misc::{compute_activation_exit_epoch, compute_epoch_at_slot, compute_signing_root}; -use super::predicates::are_indices_sorted_and_unique; +use super::misc::{compute_activation_exit_epoch, compute_epoch_at_slot}; use super::shuffling::compute_shuffled_index; // --------------------------------------------------------------------------- @@ -272,47 +269,34 @@ pub fn compute_proposer_index( /// Whether an indexed attestation names a valid attester set and carries /// their aggregate signature. /// -/// Logic identical to [`crate::beacon::helpers::attestation::is_valid_indexed_attestation`]; -/// this exists as its own copy only because [`electra::IndexedAttestation`] -/// is a different concrete type from phase0's `IndexedAttestation`, bounded -/// by `MAX_VALIDATORS_PER_SLOT` rather than `MAX_VALIDATORS_PER_COMMITTEE` -/// (EIP-7549: one electra attestation can span every committee in a slot). +/// Logic identical to [`crate::beacon::helpers::attestation::is_valid_indexed_attestation`]: +/// both run [`super::attestation::attesters_signature_set`]. This one exists +/// only because [`electra::IndexedAttestation`] is a different concrete type +/// from phase0's `IndexedAttestation`, bounded by `MAX_VALIDATORS_PER_SLOT` +/// rather than `MAX_VALIDATORS_PER_COMMITTEE` (EIP-7549: one electra +/// attestation can span every committee in a slot). pub fn is_valid_indexed_attestation( state: &BeaconState, indexed_attestation: &electra::IndexedAttestation, ) -> bool { - let domain = get_domain( - state, - constants::DOMAIN_BEACON_ATTESTER, - Some(indexed_attestation.data.target.epoch), - ); - is_valid_indexed_attestation_with_domain(state, indexed_attestation, domain) + indexed_attestation_signature_set(state, indexed_attestation).is_some_and(|set| set.verify()) } -/// [`is_valid_indexed_attestation`] under a signing domain the caller chose. -/// -/// For gossip, which checks against a state that may not have reached the -/// attestation's fork: see [`super::accessors::get_domain_from_schedule`]. -pub fn is_valid_indexed_attestation_with_domain( +/// The signature check [`is_valid_indexed_attestation`] makes, resolved but +/// not yet run, so a block can verify every attestation's signature in one +/// batch. See [`super::attestation::attesters_signature_set`], which holds the +/// checks every fork shares. +pub fn indexed_attestation_signature_set( state: &BeaconState, indexed_attestation: &electra::IndexedAttestation, - domain: Domain, -) -> bool { - let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; - if indices.is_empty() || !are_indices_sorted_and_unique(indices) { - return false; - } - - let mut pubkeys = Vec::with_capacity(indices.len()); - for index in indices { - match state.validator(*index) { - Ok(validator) => pubkeys.push(validator.pubkey), - Err(_) => return false, - } - } - - let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &indexed_attestation.signature) +) -> Option { + super::attestation::attesters_signature_set( + state, + &indexed_attestation.attesting_indices, + &indexed_attestation.data, + &indexed_attestation.signature, + None, + ) } // --------------------------------------------------------------------------- diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index bb00e05bf..3e889e7e0 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs @@ -86,7 +86,7 @@ //! doc), read through [`super::fulu::get_beacon_proposer_index`] rather //! than a copy of it here. -use crate::beacon::bls; +use crate::beacon::bls::SignatureSet; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::shared::AttestationData; @@ -95,8 +95,7 @@ use crate::beacon::error::{Error, Result}; use crate::beacon::hash::hash; use crate::beacon::preset; use crate::beacon::primitives::{ - BlsPubkey, Bytes32, Domain, Epoch, ExecutionAddress, Gwei, HashTreeRoot as _, Slot, - ValidatorIndex, + BlsPubkey, Bytes32, Epoch, ExecutionAddress, Gwei, HashTreeRoot as _, Slot, ValidatorIndex, }; use super::accessors::{ @@ -105,7 +104,6 @@ use super::accessors::{ }; use super::math::{bytes_to_uint64, integer_squareroot}; use super::misc::{compute_epoch_at_slot, compute_signing_root, compute_start_slot_at_epoch}; -use super::predicates::are_indices_sorted_and_unique; use super::shuffling::compute_shuffled_index; // --------------------------------------------------------------------------- @@ -118,47 +116,30 @@ use super::shuffling::compute_shuffled_index; /// Modified from [`super::electra::is_valid_indexed_attestation`] (EIP-7688): /// [`gloas::IndexedAttestation::attesting_indices`] is now the unbounded /// [`gloas::AttestingIndices`], so the length that used to be enforced by the -/// container's own SSZ bound (`MAX_VALIDATORS_PER_SLOT`) is checked here -/// explicitly instead. +/// container's own SSZ bound (`MAX_VALIDATORS_PER_SLOT`) is checked +/// explicitly instead, in [`super::attestation::attesters_signature_set`]. pub fn is_valid_indexed_attestation( state: &BeaconState, indexed_attestation: &gloas::IndexedAttestation, ) -> bool { - let domain = get_domain( - state, - constants::DOMAIN_BEACON_ATTESTER, - Some(indexed_attestation.data.target.epoch), - ); - is_valid_indexed_attestation_with_domain(state, indexed_attestation, domain) + indexed_attestation_signature_set(state, indexed_attestation).is_some_and(|set| set.verify()) } -/// [`is_valid_indexed_attestation`] under a signing domain the caller chose. -/// -/// For gossip, which checks against a state that may not have reached the -/// attestation's fork: see [`super::accessors::get_domain_from_schedule`]. -pub fn is_valid_indexed_attestation_with_domain( +/// The signature check [`is_valid_indexed_attestation`] makes, resolved but +/// not yet run, so a block can verify every attestation's signature in one +/// batch. See [`super::attestation::attesters_signature_set`], which holds the +/// checks every fork shares, the length bound included. +pub fn indexed_attestation_signature_set( state: &BeaconState, indexed_attestation: &gloas::IndexedAttestation, - domain: Domain, -) -> bool { - let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; - if indices.is_empty() - || indices.len() > preset::MAX_VALIDATORS_PER_SLOT - || !are_indices_sorted_and_unique(indices) - { - return false; - } - - let mut pubkeys = Vec::with_capacity(indices.len()); - for index in indices { - match state.validator(*index) { - Ok(validator) => pubkeys.push(validator.pubkey), - Err(_) => return false, - } - } - - let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &indexed_attestation.signature) +) -> Option { + super::attestation::attesters_signature_set( + state, + &indexed_attestation.attesting_indices, + &indexed_attestation.data, + &indexed_attestation.signature, + None, + ) } // --------------------------------------------------------------------------- @@ -284,16 +265,30 @@ pub fn is_valid_indexed_payload_attestation( state: &BeaconState, attestation: &gloas::IndexedPayloadAttestation, ) -> bool { + indexed_payload_attestation_signature_set(state, attestation).is_some_and(|set| set.verify()) +} + +/// The signature check [`is_valid_indexed_payload_attestation`] makes, +/// resolved but not yet run. The structural checks (non-empty, sorted, every +/// index in range) happen here, at once; `None` means the attestation is +/// invalid without needing a pairing. +pub fn indexed_payload_attestation_signature_set( + state: &BeaconState, + attestation: &gloas::IndexedPayloadAttestation, +) -> Option { let indices: &[ValidatorIndex] = &attestation.attesting_indices; + // `<=`, not the `<` of the other attestation kinds: the spec checks only + // `indices == sorted(indices)`, since the PTC is drawn with replacement + // and a member can repeat, so duplicates are valid. if indices.is_empty() || !indices.windows(2).all(|pair| pair[0] <= pair[1]) { - return false; + return None; } let mut pubkeys = Vec::with_capacity(indices.len()); for index in indices { match state.validator(*index) { Ok(validator) => pubkeys.push(validator.pubkey), - Err(_) => return false, + Err(_) => return None, } } @@ -303,7 +298,7 @@ pub fn is_valid_indexed_payload_attestation( Some(compute_epoch_at_slot(attestation.data.slot)), ); let signing_root = compute_signing_root(attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &attestation.signature) + SignatureSet::fast_aggregate(&pubkeys, signing_root, &attestation.signature) } // --------------------------------------------------------------------------- diff --git a/crates/blockchain/state_transition/src/beacon/stf/altair.rs b/crates/blockchain/state_transition/src/beacon/stf/altair.rs index d60b6ffc9..360b65c91 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/altair.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/altair.rs @@ -26,7 +26,7 @@ //! two are the same number applied with opposite sign, not two independently //! derived quantities that happen to match. -use crate::beacon::bls; +use crate::beacon::bls::{EthFastAggregate, SignatureSet}; use crate::beacon::constants; use crate::beacon::containers::{BeaconState, altair, phase0}; use crate::beacon::error::{Error, Result, verify}; @@ -38,11 +38,16 @@ use crate::beacon::helpers::accessors::{ use crate::beacon::helpers::altair::{ add_flag, base_reward_per_increment, get_attestation_participation_flag_indices, has_flag, }; -use crate::beacon::helpers::attestation::{get_indexed_attestation, is_valid_indexed_attestation}; +use crate::beacon::helpers::attestation::{ + get_indexed_attestation, indexed_attestation_signature_set, +}; use crate::beacon::helpers::misc::{compute_epoch_at_slot, compute_signing_root}; use crate::beacon::helpers::mutators::{decrease_balance, increase_balance}; use crate::beacon::preset; use crate::beacon::primitives::{BlsPubkey, Gwei, ParticipationFlags, ValidatorIndex}; +use crate::beacon::stf::signatures::SignatureBatch; +#[cfg(test)] +use crate::beacon::stf::signatures::checked; use std::collections::HashMap; // --------------------------------------------------------------------------- @@ -91,6 +96,7 @@ pub fn process_attestation( proposer_index: ValidatorIndex, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -144,8 +150,8 @@ pub fn process_attestation( get_attestation_participation_flag_indices(state, &data, inclusion_delay)?; let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; - verify( - is_valid_indexed_attestation(state, &indexed_attestation), + signatures.push( + indexed_attestation_signature_set(state, &indexed_attestation), "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -279,10 +285,11 @@ pub fn process_attestation( /// in this module needs one) and its own aggregation already batches every /// point in a single pass, so this collapses the three cases into one direct /// aggregation of the participating members' pubkeys. -/// [`bls::eth_fast_aggregate_verify`]'s own convention for an empty list -/// (accept only when the signature is the point at infinity) covers the case -/// nobody participated exactly the way the specification's own "less than -/// half" branch does when zero bits are set. +/// [`crate::beacon::bls::eth_fast_aggregate_verify`]'s own convention for an empty list +/// (accept only when the signature is the point at infinity), written once in +/// [`SignatureSet::eth_fast_aggregate`] and read here through it, covers the +/// case nobody participated exactly the way the specification's own "less +/// than half" branch does when zero bits are set. /// /// Every reward and penalty here is the same `participant_reward`, applied /// with opposite sign: [`crate::beacon::constants::SYNC_REWARD_WEIGHT`] and @@ -312,6 +319,7 @@ pub fn process_sync_aggregate( sync_aggregate: &altair::SyncAggregate, active_balances: &ActiveBalanceCache, pubkey_indices: &PubkeyIndexCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let committee_bits = &sync_aggregate.sync_committee_bits; @@ -335,14 +343,19 @@ pub fn process_sync_aggregate( Some(compute_epoch_at_slot(previous_slot)), ); let signing_root = compute_signing_root(get_block_root_at_slot(state, previous_slot)?, domain); - verify( - bls::eth_fast_aggregate_verify( - &participant_pubkeys, - signing_root, - &sync_aggregate.sync_committee_signature, - ), - "eth_fast_aggregate_verify(participant_pubkeys, signing_root, sync_aggregate.sync_committee_signature)", - )?; + // `eth_fast_aggregate_verify`, the same constructor block production's + // check of a pooled aggregate goes through. No participants is decided + // there and then, with nothing to pair; otherwise the pairing is deferred. + let eth_fast_aggregate_verify = "eth_fast_aggregate_verify(participant_pubkeys, signing_root, sync_aggregate.sync_committee_signature)"; + match SignatureSet::eth_fast_aggregate( + &participant_pubkeys, + signing_root, + &sync_aggregate.sync_committee_signature, + ) { + EthFastAggregate::Valid => {} + EthFastAggregate::Invalid => verify(false, eth_fast_aggregate_verify)?, + EthFastAggregate::Pairing(set) => signatures.push(Some(set), eth_fast_aggregate_verify)?, + } // Resolve every committee seat's validator index up front; see this // function's documentation for why the mutation loop below cannot do its @@ -465,6 +478,7 @@ mod tests { use blst::min_pk::SecretKey; use super::*; + use crate::beacon::bls; use crate::beacon::containers::altair::SyncCommittee; use crate::beacon::containers::shared::{AttestationData, Checkpoint, Validator}; use crate::beacon::helpers::accessors::{get_beacon_committee, get_total_active_balance}; @@ -638,13 +652,16 @@ mod tests { let proposer_index = get_beacon_proposer_index(&state).unwrap(); let balance_before = state.balance(proposer_index).unwrap(); - process_attestation( - &mut state, - &attestation, - proposer_index, - &CommitteeCache::default(), - &ActiveBalanceCache::default(), - ) + checked(|s| { + process_attestation( + &mut state, + &attestation, + proposer_index, + &CommitteeCache::default(), + &ActiveBalanceCache::default(), + s, + ) + }) .unwrap(); let (previous_epoch_participation, _, _) = state.altair_validator_lists().unwrap(); @@ -675,13 +692,16 @@ mod tests { // so reprocessing the identical attestation must grant nothing new, and // the proposer's balance must not move. let balance_before_replay = state.balance(proposer_index).unwrap(); - process_attestation( - &mut state, - &attestation, - proposer_index, - &CommitteeCache::default(), - &ActiveBalanceCache::default(), - ) + checked(|s| { + process_attestation( + &mut state, + &attestation, + proposer_index, + &CommitteeCache::default(), + &ActiveBalanceCache::default(), + s, + ) + }) .unwrap(); let balance_after_replay = state.balance(proposer_index).unwrap(); assert_eq!( @@ -758,12 +778,15 @@ mod tests { .map(|index| state.balance(index).unwrap()) .collect(); - process_sync_aggregate( - &mut state, - &sync_aggregate, - &ActiveBalanceCache::default(), - &PubkeyIndexCache::default(), - ) + checked(|s| { + process_sync_aggregate( + &mut state, + &sync_aggregate, + &ActiveBalanceCache::default(), + &PubkeyIndexCache::default(), + s, + ) + }) .unwrap(); // Independently derived expected rewards, sharing only the @@ -831,19 +854,25 @@ mod tests { assert_eq!(cache.verified_index(&state, &member), None); let mut with_fresh = state.clone(); - process_sync_aggregate( - &mut with_fresh, - &sync_aggregate, - &ActiveBalanceCache::default(), - &PubkeyIndexCache::default(), - ) + checked(|s| { + process_sync_aggregate( + &mut with_fresh, + &sync_aggregate, + &ActiveBalanceCache::default(), + &PubkeyIndexCache::default(), + s, + ) + }) .unwrap(); - process_sync_aggregate( - &mut state, - &sync_aggregate, - &ActiveBalanceCache::default(), - &cache, - ) + checked(|s| { + process_sync_aggregate( + &mut state, + &sync_aggregate, + &ActiveBalanceCache::default(), + &cache, + s, + ) + }) .unwrap(); assert_eq!(state, with_fresh); @@ -879,12 +908,15 @@ mod tests { sync_committee_signature: BlsSignature::default(), }; assert!( - process_sync_aggregate( - &mut state.clone(), - &wrong, - &ActiveBalanceCache::default(), - &PubkeyIndexCache::default() - ) + checked(|s| { + process_sync_aggregate( + &mut state.clone(), + &wrong, + &ActiveBalanceCache::default(), + &PubkeyIndexCache::default(), + s, + ) + }) .is_err(), "the all-zero signature is not the point at infinity, so this must be rejected" ); @@ -894,12 +926,15 @@ mod tests { sync_committee_signature: g2_point_at_infinity(), }; let balance_before = state.balance(0).unwrap(); - process_sync_aggregate( - &mut state, - &correct, - &ActiveBalanceCache::default(), - &PubkeyIndexCache::default(), - ) + checked(|s| { + process_sync_aggregate( + &mut state, + &correct, + &ActiveBalanceCache::default(), + &PubkeyIndexCache::default(), + s, + ) + }) .unwrap(); // The sole validator holds every seat and none of them participated, diff --git a/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs b/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs index d1143bd26..18d96ba55 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs @@ -38,6 +38,7 @@ use crate::beacon::preset; use crate::beacon::primitives::{ Bytes32, ExecutionAddress, ExecutionBlockHash, HashTreeRoot as _, Root, Slot, Uint256, }; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine}; @@ -61,6 +62,7 @@ use super::{BlockCaches, ExecutionEngine}; /// the one fork where a block might still legitimately carry no real payload, /// if this chain's merge transition has not happened yet. Every later fork /// drops the condition outright, since by then it is always true. +#[allow(clippy::too_many_arguments)] pub fn process_block( state: &mut BeaconState, block: &bellatrix::BeaconBlock, @@ -68,6 +70,7 @@ pub fn process_block( engine: &ExecutionEngine, caches: BlockCaches<'_>, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -79,7 +82,7 @@ pub fn process_block( if is_execution_enabled(state, &block.body.execution_payload)? { process_execution_payload(state, &block.body.execution_payload, config, engine)?; } - super::block::process_randao(state, &block.body.randao_reveal, options.randao)?; + super::block::process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; super::block::process_eth1_data(state, &block.body.eth1_data)?; super::operations::process_operations( state, @@ -91,12 +94,14 @@ pub fn process_block( config, caches.committees, caches.active_balances, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } diff --git a/crates/blockchain/state_transition/src/beacon/stf/block.rs b/crates/blockchain/state_transition/src/beacon/stf/block.rs index 6b5967b7f..fa790d7ec 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/block.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/block.rs @@ -17,7 +17,7 @@ //! every driver, present and future, call the identical function regardless of //! what else that fork's body carries. -use crate::beacon::bls; +use crate::beacon::bls::SignatureSet; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::{self, BeaconBlockHeader, BeaconState, Eth1Data, altair, phase0}; @@ -30,16 +30,29 @@ use crate::beacon::helpers::math::xor; use crate::beacon::helpers::misc::compute_signing_root; use crate::beacon::preset; use crate::beacon::primitives::{BlsSignature, HashTreeRoot as _, Root, Slot, ValidatorIndex}; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine, bellatrix, capella, deneb, electra, fulu, gloas}; -/// Dispatches on the block's fork and runs that fork's own block processing. +/// Dispatches on the block's fork and runs that fork's own block processing, +/// then verifies every signature the processing deferred. /// /// Matches [`containers::SignedBeaconBlock`]'s variant once, then hands the /// unwrapped, fork-specific `BeaconBlock` to the function written for it. Called /// after [`super::process_slots`] has already advanced the state to the block's /// slot, so every arm validates and mutates a state already positioned at /// `signed_block`'s slot. +/// +/// Means what it always did, that the block is valid: it builds the +/// [`SignatureBatch`] the drivers push into and checks all of it before +/// returning `Ok`, so a caller needs no knowledge of it. +/// +/// An `Err` leaves `state` partly mutated: a check that fails midway leaves +/// the operations before it applied, and a bad signature on an attestation or +/// the sync aggregate, which is found only at the end, leaves that operation +/// applied too (see [`super::signatures`] for which writes may precede their +/// signature's check). Every caller discards the state after an error; block +/// import's `transition_block` returns none with one. pub fn process_block( state: &mut BeaconState, signed_block: &containers::SignedBeaconBlock, @@ -47,34 +60,93 @@ pub fn process_block( engine: &ExecutionEngine, caches: BlockCaches<'_>, options: BlockProcessingOptions, +) -> Result<()> { + SignatureBatch::deferred(|signatures| { + process_block_with_signatures( + state, + signed_block, + config, + engine, + caches, + options, + signatures, + ) + }) +} + +/// [`process_block`]'s dispatch: every signature the block's processing needs +/// is pushed into `signatures`, which [`process_block`] then verifies (what the +/// processing has not already checked on its way). +/// +/// An `Err` here, or from the batch after it, leaves `state` partly mutated; +/// see [`process_block`]. A new caller must discard the state after an error. +#[allow(clippy::too_many_arguments)] +fn process_block_with_signatures( + state: &mut BeaconState, + signed_block: &containers::SignedBeaconBlock, + config: &Config, + engine: &ExecutionEngine, + caches: BlockCaches<'_>, + options: BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { match signed_block { containers::SignedBeaconBlock::Phase0(signed) => { - process_block_phase0(state, &signed.message, config, caches, options) + process_block_phase0(state, &signed.message, config, caches, options, signatures) } containers::SignedBeaconBlock::Altair(signed) => { - process_block_altair(state, &signed.message, config, caches, options) - } - containers::SignedBeaconBlock::Bellatrix(signed) => { - bellatrix::process_block(state, &signed.message, config, engine, caches, options) - } - containers::SignedBeaconBlock::Capella(signed) => { - capella::process_block(state, &signed.message, config, engine, caches, options) - } - containers::SignedBeaconBlock::Deneb(signed) => { - deneb::process_block(state, &signed.message, config, engine, caches, options) - } - containers::SignedBeaconBlock::Electra(signed) => { - electra::process_block(state, &signed.message, config, engine, caches, options) - } - containers::SignedBeaconBlock::Fulu(signed) => { - fulu::process_block(state, &signed.message, config, engine, caches, options) + process_block_altair(state, &signed.message, config, caches, options, signatures) } + containers::SignedBeaconBlock::Bellatrix(signed) => bellatrix::process_block( + state, + &signed.message, + config, + engine, + caches, + options, + signatures, + ), + containers::SignedBeaconBlock::Capella(signed) => capella::process_block( + state, + &signed.message, + config, + engine, + caches, + options, + signatures, + ), + containers::SignedBeaconBlock::Deneb(signed) => deneb::process_block( + state, + &signed.message, + config, + engine, + caches, + options, + signatures, + ), + containers::SignedBeaconBlock::Electra(signed) => electra::process_block( + state, + &signed.message, + config, + engine, + caches, + options, + signatures, + ), + containers::SignedBeaconBlock::Fulu(signed) => fulu::process_block( + state, + &signed.message, + config, + engine, + caches, + options, + signatures, + ), // ePBS (EIP-7732) restructures block processing around a builder's // bid rather than an embedded payload: a gloas block carries no // payload for `engine` to answer for, so this arm never reaches it. containers::SignedBeaconBlock::Gloas(signed) => { - gloas::process_block(state, &signed.message, config, caches, options) + gloas::process_block(state, &signed.message, config, caches, options, signatures) } containers::SignedBeaconBlock::Lean(_) => { crate::beacon::lean_block_unreachable("process_block") @@ -90,6 +162,7 @@ pub fn process_block_phase0( config: &Config, caches: BlockCaches<'_>, options: BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { process_block_header( state, @@ -98,7 +171,7 @@ pub fn process_block_phase0( block.parent_root, block.body.hash_tree_root(), )?; - process_randao(state, &block.body.randao_reveal, options.randao)?; + process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; process_eth1_data(state, &block.body.eth1_data)?; crate::beacon::stf::operations::process_operations( state, @@ -110,6 +183,7 @@ pub fn process_block_phase0( config, caches.committees, caches.active_balances, + signatures, )?; Ok(()) } @@ -126,6 +200,7 @@ pub fn process_block_altair( config: &Config, caches: BlockCaches<'_>, options: BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { process_block_header( state, @@ -134,7 +209,7 @@ pub fn process_block_altair( block.parent_root, block.body.hash_tree_root(), )?; - process_randao(state, &block.body.randao_reveal, options.randao)?; + process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; process_eth1_data(state, &block.body.eth1_data)?; crate::beacon::stf::operations::process_operations( state, @@ -146,12 +221,14 @@ pub fn process_block_altair( config, caches.committees, caches.active_balances, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } @@ -232,8 +309,13 @@ pub enum RandaoVerification { Skip, } -/// Verifies the proposer's RANDAO reveal (unless `verification` skips it) and -/// mixes it into the current epoch's randao mix. +/// Verifies the proposer's RANDAO reveal (unless `verification` skips it), then +/// mixes the reveal into the current epoch's randao mix. +/// +/// In the specification's order: the reveal is checked before the mix takes +/// it, so nothing that reads the mix ever sees an unverified reveal. The check +/// goes through `signatures` with everything already pending (in gloas, the +/// builder's bid processed just before this), one batch for both. /// /// The reveal signs the epoch rather than the slot, so the same signature would /// verify for every slot of the epoch: the mix it feeds is per-epoch, not @@ -243,6 +325,7 @@ pub fn process_randao( state: &mut BeaconState, randao_reveal: &BlsSignature, verification: RandaoVerification, + signatures: &mut SignatureBatch, ) -> Result<()> { let epoch = get_current_epoch(state); let proposer_index = get_beacon_proposer_index(state)?; @@ -251,10 +334,11 @@ pub fn process_randao( let proposer = state.validator(proposer_index)?; if verification == RandaoVerification::Verify { - verify( - bls::verify(&proposer.pubkey, signing_root, randao_reveal), + signatures.push( + SignatureSet::single(&proposer.pubkey, signing_root, randao_reveal), "RANDAO reveal signature", )?; + signatures.verify_pending()?; } let mix = xor(get_randao_mix(state, epoch), hash(&randao_reveal.0)); @@ -289,7 +373,9 @@ pub fn process_eth1_data(state: &mut BeaconState, eth1_data: &Eth1Data) -> Resul #[cfg(test)] mod tests { use super::*; + use crate::beacon::bls; use crate::beacon::primitives::Bytes32; + use crate::beacon::stf::signatures::checked; /// An otherwise-empty block body, since the header, RANDAO, and eth1 checks /// below never look past its `hash_tree_root`. @@ -366,16 +452,127 @@ mod tests { let reveal = BlsSignature(bls::G2_POINT_AT_INFINITY); let expected = xor(get_randao_mix(&state, epoch), hash(&reveal.0)); - process_randao(&mut state, &reveal, RandaoVerification::Skip).unwrap(); + checked(|s| process_randao(&mut state, &reveal, RandaoVerification::Skip, s)).unwrap(); assert_eq!(get_randao_mix(&state, epoch), expected); } + /// A reveal signed by the right proposer over the wrong message. + fn reveal_over(state: &BeaconState, signed: Root) -> BlsSignature { + let proposer = get_beacon_proposer_index(state).unwrap(); + crate::beacon::helpers::test_state::sign_for(proposer as usize, signed) + } + + /// The reveal the proposer of `state`'s slot really makes. + fn valid_reveal(state: &BeaconState) -> BlsSignature { + let epoch = get_current_epoch(state); + let domain = get_domain(state, constants::DOMAIN_RANDAO, None); + reveal_over(state, compute_signing_root(epoch.hash_tree_root(), domain)) + } + + /// The spec's order: the reveal is verified, and only then mixed in. A bad + /// one is refused by `process_randao` itself, with the mix untouched, even + /// though the batch it goes through defers. + #[test] + fn a_bad_randao_reveal_is_rejected_before_it_is_mixed_in() { + let mut state = crate::beacon::helpers::test_state::with_validators(8); + let epoch = get_current_epoch(&state); + let mix_before = get_randao_mix(&state, epoch); + let reveal = reveal_over(&state, Root::repeat_byte(7)); + let mut signatures = SignatureBatch::new(); + + let err = process_randao( + &mut state, + &reveal, + RandaoVerification::Verify, + &mut signatures, + ) + .unwrap_err(); + assert!( + format!("{err:?}").contains("RANDAO reveal signature"), + "{err:?}" + ); + assert_eq!(get_randao_mix(&state, epoch), mix_before); + } + + /// What gloas's bid is to the reveal: a set pushed before it is checked + /// with it, before the mix, and a bad one rejects under its own label. + #[test] + fn a_bad_set_pending_before_the_reveal_is_rejected_with_it() { + let mut state = crate::beacon::helpers::test_state::with_validators(8); + let epoch = get_current_epoch(&state); + let mix_before = get_randao_mix(&state, epoch); + let reveal = valid_reveal(&state); + let forged = SignatureSet::single( + &state.validator(0).unwrap().pubkey, + Root::repeat_byte(1), + &crate::beacon::helpers::test_state::sign_for(0, Root::repeat_byte(2)), + ); + let mut signatures = SignatureBatch::new(); + signatures + .push(forged, "the set before the reveal") + .unwrap(); + + let err = process_randao( + &mut state, + &reveal, + RandaoVerification::Verify, + &mut signatures, + ) + .unwrap_err(); + assert!( + format!("{err:?}").contains("the set before the reveal"), + "{err:?}" + ); + assert_eq!(get_randao_mix(&state, epoch), mix_before); + } + + #[test] + fn a_valid_randao_reveal_is_verified_and_mixed_in() { + let mut state = crate::beacon::helpers::test_state::with_validators(8); + let epoch = get_current_epoch(&state); + let reveal = valid_reveal(&state); + let expected = xor(get_randao_mix(&state, epoch), hash(&reveal.0)); + let mut signatures = SignatureBatch::new(); + + process_randao( + &mut state, + &reveal, + RandaoVerification::Verify, + &mut signatures, + ) + .unwrap(); + assert!( + signatures.is_empty(), + "the reveal was checked, not deferred" + ); + assert_eq!(get_randao_mix(&state, epoch), expected); + } + + #[test] + fn skipping_randao_verification_pushes_nothing() { + let mut state = crate::beacon::helpers::test_state::with_validators(8); + let reveal = BlsSignature(bls::G2_POINT_AT_INFINITY); + let mut signatures = SignatureBatch::new(); + + process_randao( + &mut state, + &reveal, + RandaoVerification::Skip, + &mut signatures, + ) + .unwrap(); + assert!(signatures.is_empty()); + } + #[test] fn verifying_randao_rejects_the_infinity_reveal() { let mut state = crate::beacon::helpers::test_state::with_validators(8); let reveal = BlsSignature(bls::G2_POINT_AT_INFINITY); - assert!(process_randao(&mut state, &reveal, RandaoVerification::Verify).is_err()); + assert!( + checked(|s| process_randao(&mut state, &reveal, RandaoVerification::Verify, s)) + .is_err() + ); } } diff --git a/crates/blockchain/state_transition/src/beacon/stf/capella.rs b/crates/blockchain/state_transition/src/beacon/stf/capella.rs index 3de310e1e..0021ba7df 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/capella.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/capella.rs @@ -41,7 +41,7 @@ //! fresh here, since capella's own [`capella::ExecutionPayloadHeader`] adds //! `withdrawals_root` and is a distinct type from bellatrix's. -use crate::beacon::bls; +use crate::beacon::bls::SignatureSet; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::shared::{Deposit, ProposerSlashing, SignedVoluntaryExit}; @@ -58,6 +58,7 @@ use crate::beacon::helpers::misc::{compute_domain, compute_signing_root}; use crate::beacon::helpers::mutators::decrease_balance; use crate::beacon::preset; use crate::beacon::primitives::{H256, HashTreeRoot as _}; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine}; @@ -72,6 +73,7 @@ use super::{BlockCaches, ExecutionEngine}; /// See this module's own documentation for why [`process_withdrawals`] runs /// before [`process_operations`], and so before this same block's own /// `bls_to_execution_changes` are processed. +#[allow(clippy::too_many_arguments)] pub fn process_block( state: &mut BeaconState, block: &capella::BeaconBlock, @@ -79,6 +81,7 @@ pub fn process_block( engine: &ExecutionEngine, caches: BlockCaches<'_>, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -89,7 +92,7 @@ pub fn process_block( )?; process_withdrawals(state, &block.body.execution_payload)?; process_execution_payload(state, &block.body.execution_payload, config, engine)?; - super::block::process_randao(state, &block.body.randao_reveal, options.randao)?; + super::block::process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; super::block::process_eth1_data(state, &block.body.eth1_data)?; process_operations( state, @@ -102,12 +105,14 @@ pub fn process_block( config, caches.committees, caches.active_balances, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } @@ -373,6 +378,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { super::operations::process_operations( state, @@ -384,9 +390,10 @@ pub fn process_operations( config, committees, active_balances, + signatures, )?; for signed_change in bls_to_execution_changes { - process_bls_to_execution_change(state, signed_change, config)?; + process_bls_to_execution_change(state, signed_change, config, signatures)?; } Ok(()) } @@ -418,6 +425,7 @@ pub fn process_bls_to_execution_change( state: &mut BeaconState, signed_change: &capella::SignedBLSToExecutionChange, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let change = &signed_change.message; @@ -438,14 +446,16 @@ pub fn process_bls_to_execution_change( state.genesis_validators_root(), ); let signing_root = compute_signing_root(change.hash_tree_root(), domain); - verify( - bls::verify( + signatures.push( + SignatureSet::single( &change.from_bls_pubkey, signing_root, &signed_change.signature, ), "bls.Verify(address_change.from_bls_pubkey, signing_root, signed_address_change.signature)", )?; + // Before the credentials are rewritten on the strength of it. + signatures.verify_pending()?; let mut credentials = [0u8; 32]; credentials[0] = constants::ETH1_ADDRESS_WITHDRAWAL_PREFIX; @@ -512,6 +522,7 @@ mod tests { BlsPubkey, BlsSignature, ExecutionAddress, ExecutionBlockHash, Gwei, Root, Uint256, ValidatorIndex, }; + use crate::beacon::stf::signatures::checked; use libssz_types::SszVector; /// A capella state with `count` fully active, full-balance validators, one @@ -752,7 +763,8 @@ mod tests { ); let signed_change = capella::SignedBLSToExecutionChange { message, signature }; - process_bls_to_execution_change(&mut state, &signed_change, &config).unwrap(); + checked(|s| process_bls_to_execution_change(&mut state, &signed_change, &config, s)) + .unwrap(); let validator = state.validator(0).unwrap(); assert_eq!( @@ -801,7 +813,85 @@ mod tests { ); let signed_change = capella::SignedBLSToExecutionChange { message, signature }; - assert!(process_bls_to_execution_change(&mut state, &signed_change, &config).is_err()); + assert!( + checked(|s| { + process_bls_to_execution_change(&mut state, &signed_change, &config, s) + }) + .is_err() + ); + } + + /// A credential change rewrites a validator's record, so its signature and + /// every one still pending are checked before it writes, even through a + /// batch that defers: a forged change is refused by the operation itself, + /// and so is a genuine one while a forged set from earlier in the block is + /// pending. Either way the credentials are left as they were. + #[test] + fn a_credential_change_checks_its_signature_and_every_pending_one_before_it_writes() { + let config = Config::mainnet(); + let mut state = capella_state_with_validators(1); + let secret_key = SecretKey::key_gen(&[6u8; 32], &[]).expect("32 bytes of key material"); + let from_pubkey = BlsPubkey(secret_key.sk_to_pk().to_bytes()); + let hashed = hash(&from_pubkey.0); + { + let validator = state.validator_mut(0).unwrap(); + validator.withdrawal_credentials.0[0] = constants::BLS_WITHDRAWAL_PREFIX; + validator.withdrawal_credentials.0[1..].copy_from_slice(&hashed.0[1..]); + } + let credentials_before = state.validator(0).unwrap().withdrawal_credentials; + + let message = capella::BLSToExecutionChange { + validator_index: 0, + from_bls_pubkey: from_pubkey, + to_execution_address: ExecutionAddress::repeat_byte(0xcd), + }; + let domain = compute_domain( + constants::DOMAIN_BLS_TO_EXECUTION_CHANGE, + config.genesis_fork_version, + state.genesis_validators_root(), + ); + let signing_root = compute_signing_root(message.hash_tree_root(), domain); + let sign = |root: Root| BlsSignature(secret_key.sign(root.as_slice(), DST, &[]).to_bytes()); + let genuine = capella::SignedBLSToExecutionChange { + message: message.clone(), + signature: sign(signing_root), + }; + let forged = capella::SignedBLSToExecutionChange { + message, + signature: sign(Root::repeat_byte(1)), + }; + + let mut signatures = SignatureBatch::new(); + let err = process_bls_to_execution_change(&mut state, &forged, &config, &mut signatures) + .unwrap_err(); + assert!( + format!("{err:?}").contains("bls.Verify(address_change.from_bls_pubkey"), + "{err:?}" + ); + assert_eq!( + state.validator(0).unwrap().withdrawal_credentials, + credentials_before + ); + + let mut signatures = SignatureBatch::new(); + let earlier = SignatureSet::single(&from_pubkey, Root::repeat_byte(2), &sign(Root::ZERO)); + signatures.push(earlier, "an earlier operation").unwrap(); + let err = process_bls_to_execution_change(&mut state, &genuine, &config, &mut signatures) + .unwrap_err(); + assert!( + format!("{err:?}").contains("an earlier operation"), + "{err:?}" + ); + assert_eq!( + state.validator(0).unwrap().withdrawal_credentials, + credentials_before + ); + + checked(|s| process_bls_to_execution_change(&mut state, &genuine, &config, s)).unwrap(); + assert_ne!( + state.validator(0).unwrap().withdrawal_credentials, + credentials_before + ); } #[test] @@ -822,6 +912,11 @@ mod tests { signature: BlsSignature::default(), }; - assert!(process_bls_to_execution_change(&mut state, &signed_change, &config).is_err()); + assert!( + checked(|s| { + process_bls_to_execution_change(&mut state, &signed_change, &config, s) + }) + .is_err() + ); } } diff --git a/crates/blockchain/state_transition/src/beacon/stf/deneb.rs b/crates/blockchain/state_transition/src/beacon/stf/deneb.rs index 855d2a7ab..4c3fc8a14 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/deneb.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/deneb.rs @@ -25,7 +25,7 @@ //! caches two new blob-gas fields for EIP-4844, and [`process_voluntary_exit`] //! signs under a fixed fork version for EIP-7044. -use crate::beacon::bls; +use crate::beacon::bls::SignatureSet; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::capella::SignedBLSToExecutionChange; @@ -41,7 +41,9 @@ use crate::beacon::helpers::accessors::{ get_previous_epoch, get_randao_mix, }; use crate::beacon::helpers::altair::{add_flag, base_reward_per_increment, has_flag}; -use crate::beacon::helpers::attestation::{get_indexed_attestation, is_valid_indexed_attestation}; +use crate::beacon::helpers::attestation::{ + get_indexed_attestation, indexed_attestation_signature_set, +}; use crate::beacon::helpers::math::integer_squareroot; use crate::beacon::helpers::misc::{compute_domain, compute_epoch_at_slot, compute_signing_root}; use crate::beacon::helpers::mutators::{ @@ -52,6 +54,7 @@ use crate::beacon::preset; use crate::beacon::primitives::{ Bytes32, Gwei, H256, HashTreeRoot as _, KzgCommitment, ParticipationFlags, ValidatorIndex, }; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine}; @@ -72,6 +75,7 @@ use super::{BlockCaches, ExecutionEngine}; /// [`super::operations::process_operations`] has no fork check of its own to /// hang them on. See [`process_operations`]'s own documentation for why that /// is a full copy of the loop rather than a smaller patch. +#[allow(clippy::too_many_arguments)] pub fn process_block( state: &mut BeaconState, block: &deneb::BeaconBlock, @@ -79,6 +83,7 @@ pub fn process_block( engine: &ExecutionEngine, caches: BlockCaches<'_>, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -95,7 +100,7 @@ pub fn process_block( config, engine, )?; - super::block::process_randao(state, &block.body.randao_reveal, options.randao)?; + super::block::process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; super::block::process_eth1_data(state, &block.body.eth1_data)?; process_operations( state, @@ -108,12 +113,14 @@ pub fn process_block( config, caches.committees, caches.active_balances, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } @@ -156,6 +163,7 @@ fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let outstanding = state .eth1_data() @@ -175,6 +183,7 @@ fn process_operations( proposer_slashing, config, active_balances, + signatures, )?; } for attester_slashing in attester_slashings { @@ -183,6 +192,7 @@ fn process_operations( attester_slashing, config, active_balances, + signatures, )?; } // One proposer lookup for the list, not one per attestation: see the same @@ -196,6 +206,7 @@ fn process_operations( proposer_index, committees, active_balances, + signatures, )?; } } @@ -203,10 +214,10 @@ fn process_operations( super::operations::process_deposit(state, deposit, config)?; } for voluntary_exit in voluntary_exits { - process_voluntary_exit(state, voluntary_exit, config)?; + process_voluntary_exit(state, voluntary_exit, config, signatures)?; } for signed_change in bls_to_execution_changes { - super::capella::process_bls_to_execution_change(state, signed_change, config)?; + super::capella::process_bls_to_execution_change(state, signed_change, config, signatures)?; } Ok(()) @@ -317,6 +328,7 @@ pub fn process_attestation( proposer_index: ValidatorIndex, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -366,8 +378,8 @@ pub fn process_attestation( attestation_participation_flag_indices(state, &data, inclusion_delay)?; let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; - verify( - is_valid_indexed_attestation(state, &indexed_attestation), + signatures.push( + indexed_attestation_signature_set(state, &indexed_attestation), "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -689,6 +701,7 @@ pub fn process_voluntary_exit( state: &mut BeaconState, signed_voluntary_exit: &SignedVoluntaryExit, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let voluntary_exit = signed_voluntary_exit.message; let current_epoch = get_current_epoch(state); @@ -725,14 +738,16 @@ pub fn process_voluntary_exit( state.genesis_validators_root(), ); let signing_root = compute_signing_root(voluntary_exit.hash_tree_root(), domain); - verify( - bls::verify( + signatures.push( + SignatureSet::single( &validator.pubkey, signing_root, &signed_voluntary_exit.signature, ), "bls.Verify(validator.pubkey, signing_root, signed_voluntary_exit.signature)", )?; + // Before the exit is initiated on the strength of it. + signatures.verify_pending()?; initiate_validator_exit(state, voluntary_exit.validator_index, config)?; Ok(()) diff --git a/crates/blockchain/state_transition/src/beacon/stf/electra.rs b/crates/blockchain/state_transition/src/beacon/stf/electra.rs index 1b830f25b..cc80a1c02 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/electra.rs @@ -44,7 +44,7 @@ use libssz::SszEncode as _; -use crate::beacon::bls; +use crate::beacon::bls::{self, SignatureSet}; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::shared::{ @@ -65,8 +65,8 @@ use crate::beacon::helpers::electra::{ get_consolidation_churn_limit_for_fork, get_indexed_attestation, get_max_effective_balance, get_pending_balance_to_withdraw, has_compounding_withdrawal_credential, has_eth1_withdrawal_credential, has_execution_withdrawal_credential, - initiate_validator_exit as electra_initiate_validator_exit, is_fully_withdrawable_validator, - is_partially_withdrawable_validator, is_valid_indexed_attestation, pending_queue_fields, + indexed_attestation_signature_set, initiate_validator_exit as electra_initiate_validator_exit, + is_fully_withdrawable_validator, is_partially_withdrawable_validator, pending_queue_fields, pending_queue_fields_ref, }; use crate::beacon::helpers::math::integer_squareroot; @@ -81,6 +81,7 @@ use crate::beacon::primitives::{ BlsPubkey, BlsSignature, Bytes32, Gwei, HashTreeRoot as _, ParticipationFlags, ValidatorIndex, WithdrawalIndex, }; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine}; @@ -490,6 +491,7 @@ pub fn process_voluntary_exit( signed_voluntary_exit: &SignedVoluntaryExit, config: &Config, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let voluntary_exit = signed_voluntary_exit.message; let current_epoch = get_current_epoch(state); @@ -529,14 +531,16 @@ pub fn process_voluntary_exit( state.genesis_validators_root(), ); let signing_root = compute_signing_root(voluntary_exit.hash_tree_root(), domain); - verify( - bls::verify( + signatures.push( + SignatureSet::single( &validator.pubkey, signing_root, &signed_voluntary_exit.signature, ), "bls.Verify(validator.pubkey, signing_root, signed_voluntary_exit.signature)", )?; + // Before the exit is initiated on the strength of it. + signatures.verify_pending()?; // [Modified in Electra:EIP7251] electra_initiate_validator_exit( @@ -566,8 +570,9 @@ pub fn process_bls_to_execution_change( state: &mut BeaconState, signed_change: &capella::SignedBLSToExecutionChange, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { - super::capella::process_bls_to_execution_change(state, signed_change, config) + super::capella::process_bls_to_execution_change(state, signed_change, config, signatures) } // --------------------------------------------------------------------------- @@ -593,6 +598,7 @@ pub fn process_attester_slashing( attester_slashing: &electra::AttesterSlashing, config: &Config, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let attestation_1 = &attester_slashing.attestation_1; let attestation_2 = &attester_slashing.attestation_2; @@ -601,14 +607,16 @@ pub fn process_attester_slashing( is_slashable_attestation_data(&attestation_1.data, &attestation_2.data), "is_slashable_attestation_data(attestation_1.data, attestation_2.data)", )?; - verify( - is_valid_indexed_attestation(state, attestation_1), + signatures.push( + indexed_attestation_signature_set(state, attestation_1), "is_valid_indexed_attestation(state, attestation_1)", )?; - verify( - is_valid_indexed_attestation(state, attestation_2), + signatures.push( + indexed_attestation_signature_set(state, attestation_2), "is_valid_indexed_attestation(state, attestation_2)", )?; + // Before anyone is slashed on the strength of them. + signatures.verify_pending()?; super::operations::slash_attesting_index_intersection( state, @@ -654,6 +662,7 @@ pub fn process_attestation( proposer_index: ValidatorIndex, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -723,8 +732,8 @@ pub fn process_attestation( attestation_participation_flag_indices(state, &data, inclusion_delay)?; let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; - verify( - is_valid_indexed_attestation(state, &indexed_attestation), + signatures.push( + indexed_attestation_signature_set(state, &indexed_attestation), "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -1795,6 +1804,7 @@ pub fn process_operations( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, pubkey_indices: &PubkeyIndexCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let deposit_requests_start_index = block_ref(state, "process_operations")?.deposit_requests_start_index(); @@ -1818,10 +1828,17 @@ pub fn process_operations( proposer_slashing, config, active_balances, + signatures, )?; } for attester_slashing in body.attester_slashings.iter() { - process_attester_slashing(state, attester_slashing, config, active_balances)?; + process_attester_slashing( + state, + attester_slashing, + config, + active_balances, + signatures, + )?; } // [Modified in Electra:EIP7549] // @@ -1836,6 +1853,7 @@ pub fn process_operations( proposer_index, committees, active_balances, + signatures, )?; } } @@ -1844,10 +1862,10 @@ pub fn process_operations( } // [Modified in Electra:EIP7251] for voluntary_exit in body.voluntary_exits.iter() { - process_voluntary_exit(state, voluntary_exit, config, active_balances)?; + process_voluntary_exit(state, voluntary_exit, config, active_balances, signatures)?; } for signed_change in body.bls_to_execution_changes.iter() { - process_bls_to_execution_change(state, signed_change, config)?; + process_bls_to_execution_change(state, signed_change, config, signatures)?; } // [New in Electra:EIP6110:EIP7002:EIP7251] process_execution_requests( @@ -1878,6 +1896,7 @@ pub fn process_operations( /// them taking those fields directly rather than a whole body (see /// `crate::beacon::stf`'s module documentation). [`super::altair::process_sync_aggregate`] /// is reused the same way, for the same reason. +#[allow(clippy::too_many_arguments)] pub fn process_block( state: &mut BeaconState, block: &electra::BeaconBlock, @@ -1885,6 +1904,7 @@ pub fn process_block( engine: &ExecutionEngine, caches: BlockCaches<'_>, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -1895,7 +1915,7 @@ pub fn process_block( )?; process_withdrawals(state, &block.body.execution_payload)?; process_execution_payload(state, &block.body, config, engine)?; - super::block::process_randao(state, &block.body.randao_reveal, options.randao)?; + super::block::process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; super::block::process_eth1_data(state, &block.body.eth1_data)?; process_operations( state, @@ -1904,12 +1924,14 @@ pub fn process_block( caches.committees, caches.active_balances, caches.pubkey_indices, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } diff --git a/crates/blockchain/state_transition/src/beacon/stf/fulu.rs b/crates/blockchain/state_transition/src/beacon/stf/fulu.rs index ca64680b2..5763c885e 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/fulu.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/fulu.rs @@ -50,6 +50,7 @@ use crate::beacon::helpers::accessors::{ use crate::beacon::helpers::electra::pending_queue_fields; use crate::beacon::helpers::fulu::{fulu_state, fulu_state_ref}; use crate::beacon::primitives::{Bytes32, HashTreeRoot as _}; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine}; @@ -62,6 +63,7 @@ use super::{BlockCaches, ExecutionEngine}; /// for [`super::electra::process_execution_payload`] and /// [`super::electra::process_operations`]. See the module docs for why these /// are the only steps this fork's specification asks to change. +#[allow(clippy::too_many_arguments)] pub fn process_block( state: &mut BeaconState, block: &electra::BeaconBlock, @@ -69,6 +71,7 @@ pub fn process_block( engine: &ExecutionEngine, caches: BlockCaches<'_>, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -79,7 +82,7 @@ pub fn process_block( )?; super::electra::process_withdrawals(state, &block.body.execution_payload)?; process_execution_payload(state, &block.body, config, engine)?; - super::block::process_randao(state, &block.body.randao_reveal, options.randao)?; + super::block::process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; super::block::process_eth1_data(state, &block.body.eth1_data)?; process_operations( state, @@ -88,12 +91,14 @@ pub fn process_block( caches.committees, caches.active_balances, caches.pubkey_indices, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } @@ -237,6 +242,7 @@ pub fn process_operations( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, pubkey_indices: &PubkeyIndexCache, + signatures: &mut SignatureBatch, ) -> Result<()> { // [Modified in Fulu] verify(body.deposits.is_empty(), "len(body.deposits) == 0")?; @@ -247,6 +253,7 @@ pub fn process_operations( proposer_slashing, config, active_balances, + signatures, )?; } for attester_slashing in body.attester_slashings.iter() { @@ -255,6 +262,7 @@ pub fn process_operations( attester_slashing, config, active_balances, + signatures, )?; } // `electra::process_attestation` is handed the proposer rather than looking @@ -271,16 +279,23 @@ pub fn process_operations( proposer_index, committees, active_balances, + signatures, )?; } } // [Modified in Fulu] // Removed `process_deposit` for voluntary_exit in body.voluntary_exits.iter() { - super::electra::process_voluntary_exit(state, voluntary_exit, config, active_balances)?; + super::electra::process_voluntary_exit( + state, + voluntary_exit, + config, + active_balances, + signatures, + )?; } for signed_change in body.bls_to_execution_changes.iter() { - super::electra::process_bls_to_execution_change(state, signed_change, config)?; + super::electra::process_bls_to_execution_change(state, signed_change, config, signatures)?; } process_execution_requests( state, diff --git a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs index e182ee8d2..1af0eb9a7 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs @@ -47,7 +47,7 @@ use libssz::SszEncode as _; -use crate::beacon::bls; +use crate::beacon::bls::{self, SignatureSet}; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::shared::{DepositMessage, ProposerSlashing}; @@ -64,9 +64,9 @@ use crate::beacon::helpers::gloas::{ add_builder_to_registry, can_builder_cover_bid, convert_builder_index_to_validator_index, convert_validator_index_to_builder_index, get_attestation_participation_flag_indices, get_indexed_attestation, get_indexed_payload_attestation, gloas_state, gloas_state_ref, + indexed_attestation_signature_set, indexed_payload_attestation_signature_set, initiate_builder_exit, is_active_builder, is_attestation_same_slot, is_builder_index, - is_builder_withdrawal_credential, is_valid_indexed_attestation, - is_valid_indexed_payload_attestation, settle_builder_payment, + is_builder_withdrawal_credential, settle_builder_payment, }; use crate::beacon::helpers::misc::{compute_domain, compute_epoch_at_slot, compute_signing_root}; use crate::beacon::helpers::mutators::{decrease_balance, increase_balance, slash_validator}; @@ -76,6 +76,7 @@ use crate::beacon::primitives::{ Bytes32, ExecutionAddress, Gwei, HashTreeRoot as _, ParticipationFlags, Root, Slot, ValidatorIndex, WithdrawalIndex, }; +use crate::beacon::stf::signatures::SignatureBatch; use super::{BlockCaches, ExecutionEngine}; @@ -104,6 +105,7 @@ pub fn process_block( config: &Config, caches: BlockCaches<'_>, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { // [New in Gloas:EIP7732] let parent_slot = state.latest_block_header().slot; @@ -127,8 +129,13 @@ pub fn process_block( process_withdrawals(state)?; // [Modified in Gloas:EIP7732] Removed `process_execution_payload`. // [New in Gloas:EIP7732] - process_execution_payload_bid(state, &block.body.signed_execution_payload_bid, config)?; - super::block::process_randao(state, &block.body.randao_reveal, options.randao)?; + process_execution_payload_bid( + state, + &block.body.signed_execution_payload_bid, + config, + signatures, + )?; + super::block::process_randao(state, &block.body.randao_reveal, options.randao, signatures)?; super::block::process_eth1_data(state, &block.body.eth1_data)?; // [Modified in Gloas:EIP7732] process_operations( @@ -138,16 +145,37 @@ pub fn process_block( config, caches.committees, caches.active_balances, + signatures, )?; super::altair::process_sync_aggregate( state, &block.body.sync_aggregate, caches.active_balances, caches.pubkey_indices, + signatures, )?; Ok(()) } +/// [`process_block`] followed by the check of every signature it deferred: +/// the whole of what the specification's `process_block` means. +/// +/// For the callers that run a gloas block through its own driver rather than +/// through [`super::block::process_block`] (block production, which builds a +/// block's state root this way), and have no signature of their own to add to +/// the batch. +pub fn process_block_and_verify( + state: &mut BeaconState, + block: &gloas::BeaconBlock, + config: &Config, + caches: BlockCaches<'_>, + options: super::block::BlockProcessingOptions, +) -> Result<()> { + SignatureBatch::deferred(|signatures| { + process_block(state, block, config, caches, options, signatures) + }) +} + // --------------------------------------------------------------------------- // Withdrawals // --------------------------------------------------------------------------- @@ -755,6 +783,17 @@ pub fn verify_execution_payload_bid_signature( state: &BeaconState, signed_bid: &gloas::SignedExecutionPayloadBid, ) -> Result { + Ok(execution_payload_bid_signature_set(state, signed_bid)?.is_some_and(|set| set.verify())) +} + +/// The signature check [`verify_execution_payload_bid_signature`] makes, +/// resolved but not yet run, so a block can verify it with the rest of its +/// signatures in one batch. `Ok(None)` is a builder key or a signature that +/// does not validate: the bid is invalid without needing a pairing. +pub fn execution_payload_bid_signature_set( + state: &BeaconState, + signed_bid: &gloas::SignedExecutionPayloadBid, +) -> Result> { let builder_index = signed_bid.message.builder_index; let inner = gloas_state_ref(state, "verify_execution_payload_bid_signature")?; let builder = inner @@ -766,7 +805,7 @@ pub fn verify_execution_payload_bid_signature( })?; let domain = get_domain(state, constants::DOMAIN_BEACON_BUILDER, None); let signing_root = compute_signing_root(signed_bid.message.hash_tree_root(), domain); - Ok(bls::verify( + Ok(SignatureSet::single( &builder.pubkey, signing_root, &signed_bid.signature, @@ -778,6 +817,7 @@ pub fn process_execution_payload_bid( state: &mut BeaconState, signed_bid: &gloas::SignedExecutionPayloadBid, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let bid = signed_bid.message.clone(); let builder_index = bid.builder_index; @@ -826,8 +866,8 @@ pub fn process_execution_payload_bid( "process_execution_payload_bid: can_builder_cover_bid(state, builder_index, amount)", )?; // Verify that the bid signature is valid. - verify( - verify_execution_payload_bid_signature(state, signed_bid)?, + signatures.push( + execution_payload_bid_signature_set(state, signed_bid)?, "process_execution_payload_bid: verify_execution_payload_bid_signature(state, signed_bid)", )?; } @@ -1213,9 +1253,13 @@ pub fn process_proposer_slashing( proposer_slashing: &ProposerSlashing, config: &Config, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { - let (proposer_index, current_epoch) = - crate::beacon::stf::operations::verify_proposer_slashing(state, proposer_slashing)?; + let (proposer_index, current_epoch) = crate::beacon::stf::operations::verify_proposer_slashing( + state, + proposer_slashing, + signatures, + )?; // [New in Gloas:EIP7732] Remove the `BuilderPendingPayment` corresponding // to this proposal if it is still in the 2-epoch window. @@ -1277,6 +1321,7 @@ pub fn process_attester_slashing( attester_slashing: &gloas::AttesterSlashing, config: &Config, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let attestation_1 = &attester_slashing.attestation_1; let attestation_2 = &attester_slashing.attestation_2; @@ -1285,14 +1330,16 @@ pub fn process_attester_slashing( is_slashable_attestation_data(&attestation_1.data, &attestation_2.data), "process_attester_slashing: is_slashable_attestation_data(attestation_1.data, attestation_2.data)", )?; - verify( - is_valid_indexed_attestation(state, attestation_1), + signatures.push( + indexed_attestation_signature_set(state, attestation_1), "process_attester_slashing: is_valid_indexed_attestation(state, attestation_1)", )?; - verify( - is_valid_indexed_attestation(state, attestation_2), + signatures.push( + indexed_attestation_signature_set(state, attestation_2), "process_attester_slashing: is_valid_indexed_attestation(state, attestation_2)", )?; + // Before anyone is slashed on the strength of them. + signatures.verify_pending()?; crate::beacon::stf::operations::slash_attesting_index_intersection( state, @@ -1332,6 +1379,7 @@ pub fn process_attestation( parent_slot: Slot, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -1392,8 +1440,8 @@ pub fn process_attestation( get_attestation_participation_flag_indices(state, &data, inclusion_delay, parent_slot)?; let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; - verify( - is_valid_indexed_attestation(state, &indexed_attestation), + signatures.push( + indexed_attestation_signature_set(state, &indexed_attestation), "process_attestation: is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -1570,6 +1618,7 @@ pub fn process_payload_attestation( state: &mut BeaconState, payload_attestation: &gloas::PayloadAttestation, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = payload_attestation.data; @@ -1590,8 +1639,8 @@ pub fn process_payload_attestation( // Verify signature. let indexed_payload_attestation = get_indexed_payload_attestation(state, payload_attestation, config)?; - verify( - is_valid_indexed_payload_attestation(state, &indexed_payload_attestation), + signatures.push( + indexed_payload_attestation_signature_set(state, &indexed_payload_attestation), "process_payload_attestation: is_valid_indexed_payload_attestation(state, indexed_payload_attestation)", )?; Ok(()) @@ -1617,6 +1666,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { verify( body.deposits.is_empty(), @@ -1651,14 +1701,33 @@ pub fn process_operations( // [Modified in Gloas:EIP7732] for proposer_slashing in body.proposer_slashings.iter() { - process_proposer_slashing(state, proposer_slashing, config, active_balances)?; + process_proposer_slashing( + state, + proposer_slashing, + config, + active_balances, + signatures, + )?; } for attester_slashing in body.attester_slashings.iter() { - process_attester_slashing(state, attester_slashing, config, active_balances)?; + process_attester_slashing( + state, + attester_slashing, + config, + active_balances, + signatures, + )?; } // [Modified in Gloas:EIP7732] for attestation in body.attestations.iter() { - process_attestation(state, attestation, parent_slot, committees, active_balances)?; + process_attestation( + state, + attestation, + parent_slot, + committees, + active_balances, + signatures, + )?; } for voluntary_exit in body.voluntary_exits.iter() { crate::beacon::stf::electra::process_voluntary_exit( @@ -1666,16 +1735,22 @@ pub fn process_operations( voluntary_exit, config, active_balances, + signatures, )?; } for signed_change in body.bls_to_execution_changes.iter() { - crate::beacon::stf::capella::process_bls_to_execution_change(state, signed_change, config)?; + crate::beacon::stf::capella::process_bls_to_execution_change( + state, + signed_change, + config, + signatures, + )?; } // [Modified in Gloas:EIP7732] Removed `process_deposit_request`, // `process_withdrawal_request`, and `process_consolidation_request`. // [New in Gloas:EIP7732] for payload_attestation in body.payload_attestations.iter() { - process_payload_attestation(state, payload_attestation, config)?; + process_payload_attestation(state, payload_attestation, config, signatures)?; } Ok(()) @@ -1687,6 +1762,7 @@ mod tests { use crate::beacon::containers::BeaconBlockHeader; use crate::beacon::fork::ForkName; use crate::beacon::primitives::{BlsPubkey, BlsSignature, ExecutionBlockHash, Uint256}; + use crate::beacon::stf::signatures::checked; fn gloas_state_with_validators(count: usize) -> BeaconState { crate::beacon::helpers::test_state::with_validators_at(ForkName::Gloas, count) @@ -1711,7 +1787,7 @@ mod tests { signature: g2_point_at_infinity(), }; - process_execution_payload_bid(&mut state, &signed_bid, &config).unwrap(); + checked(|s| process_execution_payload_bid(&mut state, &signed_bid, &config, s)).unwrap(); assert_eq!( gloas_state_ref(&state, "test") .unwrap() @@ -1740,7 +1816,8 @@ mod tests { signature: g2_point_at_infinity(), }; - let result = process_execution_payload_bid(&mut state, &signed_bid, &config); + let result = + checked(|s| process_execution_payload_bid(&mut state, &signed_bid, &config, s)); assert!( matches!( result, @@ -1956,14 +2033,17 @@ mod tests { for (expected_message, body) in cases { let mut state = state.clone(); let committees = CommitteeCache::default(); - let result = process_operations( - &mut state, - &body, - 0, - &config, - &committees, - &ActiveBalanceCache::default(), - ); + let result = checked(|s| { + process_operations( + &mut state, + &body, + 0, + &config, + &committees, + &ActiveBalanceCache::default(), + s, + ) + }); assert!( matches!(result, Err(Error::SpecAssert(message)) if message == expected_message), "expected {expected_message:?} to fail, got {result:?}" diff --git a/crates/blockchain/state_transition/src/beacon/stf/mod.rs b/crates/blockchain/state_transition/src/beacon/stf/mod.rs index e191c2701..f22e41a78 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/mod.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/mod.rs @@ -63,6 +63,7 @@ pub mod epoch; pub mod fulu; pub mod gloas; pub mod operations; +pub mod signatures; use crate::beacon::containers; use crate::beacon::containers::{BeaconState, phase0}; @@ -208,6 +209,14 @@ pub fn apply_block( "the block's fork matches the state's", )?; + // The proposer's signature on its own, before the body runs, rather than in + // the body's batch: a block nobody but its sender signed then costs one + // pairing to reject instead of a whole body's processing. Gossip checked it + // already (`precheck_block`), but a block fetched by request arrives here + // unchecked. `process_block` batches the body's own signatures and checks + // them before returning, so before the state root below is computed (and + // the ones that slash, exit, redirect or feed the RANDAO mix before they + // write; see `signatures`). if validate_result { verify( verify_block_signature(state, signed_block), @@ -238,6 +247,9 @@ pub fn apply_block( } /// Whether the block carries its proposer's signature. +/// +/// Checked on its own, never batched: [`apply_block`] runs it before the body +/// so that a block its proposer never signed costs one pairing to reject. pub fn verify_block_signature( state: &BeaconState, signed_block: &containers::SignedBeaconBlock, @@ -261,12 +273,23 @@ pub fn verify_proposer_signature( block_root: Root, signature: &BlsSignature, ) -> bool { - let Ok(proposer) = state.validator(proposer_index) else { - return false; - }; + proposer_signature_set(state, proposer_index, block_root, signature) + .is_some_and(|set| set.verify()) +} + +/// [`verify_proposer_signature`]'s check, resolved but not yet run. `None` is +/// a proposer index outside the registry, or a key or signature that does not +/// validate. +fn proposer_signature_set( + state: &BeaconState, + proposer_index: ValidatorIndex, + block_root: Root, + signature: &BlsSignature, +) -> Option { + let proposer = state.validator(proposer_index).ok()?; let domain = get_domain(state, constants::DOMAIN_BEACON_PROPOSER, None); let signing_root = compute_signing_root(block_root, domain); - bls::verify(&proposer.pubkey, signing_root, signature) + bls::SignatureSet::single(&proposer.pubkey, signing_root, signature) } /// Advances the state to `slot`, running epoch processing at each boundary and diff --git a/crates/blockchain/state_transition/src/beacon/stf/operations.rs b/crates/blockchain/state_transition/src/beacon/stf/operations.rs index 4883da86f..9a5fe2cf9 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/operations.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/operations.rs @@ -12,7 +12,7 @@ use std::collections::HashSet; -use crate::beacon::bls; +use crate::beacon::bls::{self, SignatureSet}; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::BeaconState; @@ -26,7 +26,9 @@ use crate::beacon::helpers::accessors::{ ActiveBalanceCache, CommitteeCache, CommitteeCacheExt, get_beacon_proposer_index, get_current_epoch, get_domain, get_previous_epoch, }; -use crate::beacon::helpers::attestation::{get_indexed_attestation, is_valid_indexed_attestation}; +use crate::beacon::helpers::attestation::{ + get_indexed_attestation, indexed_attestation_signature_set, +}; use crate::beacon::helpers::misc::{ compute_deposit_domain, compute_epoch_at_slot, compute_signing_root, is_valid_merkle_branch, }; @@ -39,6 +41,7 @@ use crate::beacon::helpers::predicates::{ use crate::beacon::lean_state_unreachable; use crate::beacon::preset; use crate::beacon::primitives::{Epoch, Gwei, HashTreeRoot as _, ValidatorIndex}; +use crate::beacon::stf::signatures::SignatureBatch; /// Runs every operation in a block, in the specification's order. /// @@ -74,6 +77,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { // Checked before anything below can mutate `state`, and unconditionally // rather than only when `attestations` is non-empty: a refused fork must @@ -118,10 +122,22 @@ pub fn process_operations( )?; for proposer_slashing in proposer_slashings { - process_proposer_slashing(state, proposer_slashing, config, active_balances)?; + process_proposer_slashing( + state, + proposer_slashing, + config, + active_balances, + signatures, + )?; } for attester_slashing in attester_slashings { - process_attester_slashing(state, attester_slashing, config, active_balances)?; + process_attester_slashing( + state, + attester_slashing, + config, + active_balances, + signatures, + )?; } // The proposer every attestation below pays (phase0: records), resolved // once for the list where the specification's `process_attestation` reads @@ -158,9 +174,17 @@ pub fn process_operations( proposer_index, committees, active_balances, + signatures, )?; } else { - process_attestation(state, attestation, proposer_index, config, committees)?; + process_attestation( + state, + attestation, + proposer_index, + config, + committees, + signatures, + )?; } } } @@ -168,7 +192,7 @@ pub fn process_operations( process_deposit(state, deposit, config)?; } for voluntary_exit in voluntary_exits { - process_voluntary_exit(state, voluntary_exit, config)?; + process_voluntary_exit(state, voluntary_exit, config, signatures)?; } Ok(()) @@ -180,7 +204,10 @@ pub fn process_operations( /// Checks a proposer slashing's evidence without slashing anyone: the two /// headers agree on slot and proposer, differ from each other, name a -/// still-slashable proposer, and both signatures check out. +/// still-slashable proposer, and both signatures check out. The signatures go +/// through `signatures` and are verified, with every set still pending, before +/// this returns, since the caller slashes on the strength of them (see +/// [`SignatureBatch::verify_pending`]). /// /// Shared between phase0's own [`process_proposer_slashing`] below and /// `crate::beacon::stf::gloas::process_proposer_slashing`: gloas's own @@ -197,6 +224,7 @@ pub fn process_operations( pub(crate) fn verify_proposer_slashing( state: &BeaconState, proposer_slashing: &ProposerSlashing, + signatures: &mut SignatureBatch, ) -> Result<(ValidatorIndex, Epoch)> { let header_1 = &proposer_slashing.signed_header_1.message; let header_2 = &proposer_slashing.signed_header_2.message; @@ -230,11 +258,12 @@ pub(crate) fn verify_proposer_slashing( let epoch = compute_epoch_at_slot(signed_header.message.slot); let domain = get_domain(state, constants::DOMAIN_BEACON_PROPOSER, Some(epoch)); let signing_root = compute_signing_root(signed_header.message.hash_tree_root(), domain); - verify( - bls::verify(&pubkey, signing_root, &signed_header.signature), + signatures.push( + SignatureSet::single(&pubkey, signing_root, &signed_header.signature), "bls.Verify(proposer.pubkey, signing_root, signed_header.signature)", )?; } + signatures.verify_pending()?; Ok((proposer_index, current_epoch)) } @@ -247,8 +276,10 @@ pub fn process_proposer_slashing( proposer_slashing: &ProposerSlashing, config: &Config, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { - let (proposer_index, _current_epoch) = verify_proposer_slashing(state, proposer_slashing)?; + let (proposer_index, _current_epoch) = + verify_proposer_slashing(state, proposer_slashing, signatures)?; slash_validator(state, proposer_index, None, config, Some(active_balances))?; Ok(()) } @@ -318,6 +349,7 @@ pub fn process_attester_slashing( attester_slashing: &phase0::AttesterSlashing, config: &Config, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let attestation_1 = &attester_slashing.attestation_1; let attestation_2 = &attester_slashing.attestation_2; @@ -326,14 +358,16 @@ pub fn process_attester_slashing( is_slashable_attestation_data(&attestation_1.data, &attestation_2.data), "is_slashable_attestation_data(attestation_1.data, attestation_2.data)", )?; - verify( - is_valid_indexed_attestation(state, attestation_1), + signatures.push( + indexed_attestation_signature_set(state, attestation_1), "is_valid_indexed_attestation(state, attestation_1)", )?; - verify( - is_valid_indexed_attestation(state, attestation_2), + signatures.push( + indexed_attestation_signature_set(state, attestation_2), "is_valid_indexed_attestation(state, attestation_2)", )?; + // Before anyone is slashed on the strength of them. + signatures.verify_pending()?; slash_attesting_index_intersection( state, @@ -375,6 +409,7 @@ pub fn process_attestation( proposer_index: ValidatorIndex, _config: &Config, committees: &CommitteeCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -449,8 +484,8 @@ pub fn process_attestation( } let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; - verify( - is_valid_indexed_attestation(state, &indexed_attestation), + signatures.push( + indexed_attestation_signature_set(state, &indexed_attestation), "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -651,6 +686,7 @@ pub fn process_voluntary_exit( state: &mut BeaconState, signed_voluntary_exit: &SignedVoluntaryExit, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let voluntary_exit = signed_voluntary_exit.message; let current_epoch = get_current_epoch(state); @@ -689,14 +725,16 @@ pub fn process_voluntary_exit( Some(voluntary_exit.epoch), ); let signing_root = compute_signing_root(voluntary_exit.hash_tree_root(), domain); - verify( - bls::verify( + signatures.push( + SignatureSet::single( &validator.pubkey, signing_root, &signed_voluntary_exit.signature, ), "bls.Verify(validator.pubkey, signing_root, signed_voluntary_exit.signature)", )?; + // Before the exit is initiated on the strength of it. + signatures.verify_pending()?; initiate_validator_exit(state, voluntary_exit.validator_index, config)?; Ok(()) @@ -712,6 +750,7 @@ mod tests { AttestationData, BeaconBlockHeader, Checkpoint, SignedBeaconBlockHeader, }; use crate::beacon::primitives::{BlsPubkey, BlsSignature, Root}; + use crate::beacon::stf::signatures::checked; #[test] fn identical_proposer_slashing_headers_are_rejected() { @@ -736,12 +775,15 @@ mod tests { }; assert!( - process_proposer_slashing( - &mut state, - &slashing, - &config, - &ActiveBalanceCache::default() - ) + checked(|s| { + process_proposer_slashing( + &mut state, + &slashing, + &config, + &ActiveBalanceCache::default(), + s, + ) + }) .is_err() ); } @@ -829,12 +871,15 @@ mod tests { }; assert!( - process_attester_slashing( - &mut state, - &slashing, - &config, - &ActiveBalanceCache::default() - ) + checked(|s| { + process_attester_slashing( + &mut state, + &slashing, + &config, + &ActiveBalanceCache::default(), + s, + ) + }) .is_err() ); } diff --git a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs new file mode 100644 index 000000000..04f5be88f --- /dev/null +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -0,0 +1,385 @@ +//! The signatures a block's processing defers, so they can be checked together. +//! +//! The specification checks every signature the moment its operation is +//! processed, and each check is a full pairing. A block carries from a dozen +//! signature sets to well over a hundred (the RANDAO reveal, every attestation, +//! up to 128 of them before electra, the sync aggregate, any exits and +//! slashings), and checking them in one random-linear-combination +//! batch costs a fraction of checking them one by one (see +//! [`ethlambda_crypto::bls::verify_signature_sets`]). So processing *builds* +//! each set where the specification verifies it and pushes it here, and the +//! [`SignatureBatch`] is verified wherever waiting longer would let an +//! unverified signature decide what the block writes, and once more when +//! processing ends. +//! +//! # What may be deferred +//! +//! A signature may be deferred only when its failure rejects the block, since +//! then the late rejection and the early one end in the same place: the state +//! the block was applied to is discarded either way. Block import runs the +//! transition on a clone of the parent's state, and `transition_block` in +//! `fork_choice.rs` hands no state back with an error. What must stay +//! immediate is a signature whose failure does *not* reject the block but +//! changes what happens next: a deposit with a bad signature is skipped and +//! the block carries on, so that check cannot wait. +//! +//! Deferring a signature lets its operation write before the signature is +//! known to be good, and lets the operations after it run on that write. That +//! is confined to votes, and to one write nothing reads before its check: +//! +//! - an attestation's participation flags and the inclusion reward it pays +//! the proposer, which are what honest votes for the same data write; +//! - the sync aggregate's rewards and penalties, the last step of a block, so +//! nothing runs after them; +//! - gloas's payload attestations, which write nothing; +//! - gloas's execution payload bid, which records the builder's payment and +//! the bid itself before its signature is checked, but is checked right +//! after, with the RANDAO reveal, before any operation runs. That holds +//! whenever the reveal is verified, which block import always does; only +//! block production skips the reveal, for a block it is building itself, +//! and then the bid is checked when the block ends. +//! +//! Everything else checks its own set, together with every set still pending, +//! before it writes, through [`SignatureBatch::verify_pending`]: +//! +//! - the RANDAO reveal, so the mix is never fed an unverified reveal; +//! - proposer and attester slashings, voluntary exits and BLS-to-execution +//! changes, the operations that change a validator's record. +//! +//! So no operation that slashes, exits or redirects a validator writes on a +//! forged signature, or runs on a state an earlier forged signature wrote to: +//! a block cannot combine forged slashings, exits and credential changes into +//! a state no set of honest signers produces. A block of ordinary votes still +//! checks all of its attestations and its sync aggregate in one batch. +//! +//! # What a forged block costs +//! +//! Rejecting a block whose body carries a bad signature costs the processing +//! up to the check that catches it, that batch, and finding the bad set in it. +//! The processing is bounded as it was with immediate checks, which also ran +//! every operation before reaching the sync aggregate's signature, the last +//! one a block carries. A forged slashing, exit or credential change is caught +//! at its own operation, before anything after it runs. Finding the bad set +//! bisects the failed batch rather than re-checking each set on its own (see +//! [`SignatureBatch::verify_pending`]): about the batch's cost again, where +//! the re-check cost a full pairing per set. +//! +//! # What a late failure names +//! +//! Every error is the check's own label (the same `&'static str` the immediate +//! check would have failed with), so a rejected block's error still says which +//! signature was bad. A batch that fails does not know which set did, so it +//! looks for the first failing set in push order, which is also the one the +//! immediate checks would have stopped at. The order between a bad signature +//! and some *other* check later in the block can differ from the immediate +//! path: a block with two defects may now be rejected for the later one, or +//! the signature, depending on which the driver reaches first. Which defect is +//! named was never part of the specification, only that the block is invalid. + +use ethlambda_crypto::bls::{SignatureSet, verify_signature_sets}; +use tracing::error; + +use crate::beacon::error::{Result, verify}; + +/// The signatures a block's processing defers, checked together once it ends. +/// +/// See the module documentation for what may be deferred and why. Outside +/// this crate there are two ways to get one, and both check every set pushed: +/// [`Self::deferred`] verifies the batch once its closure returns, and +/// [`Self::immediate`] checks each set as it is pushed, which is for the +/// callers that apply an operation to a scratch state and must learn at once +/// whether it was accepted. A deferring batch that could be dropped +/// unverified would turn every function taking one into "valid except for +/// its signatures", which is also why it does not implement `Default`: a +/// public `default()` would be exactly such a batch. Inside the crate the +/// same holds outside tests: [`Self::new`] exists only under `cfg(test)`, so +/// no production path can hold a deferring batch [`Self::deferred`] did not +/// make. +pub struct SignatureBatch { + sets: Vec, + /// `sets[i]`'s label, apart from the sets so the batch check reads them + /// without a copy. + labels: Vec<&'static str>, + immediate: bool, +} + +impl SignatureBatch { + fn empty(immediate: bool) -> Self { + Self { + sets: Vec::new(), + labels: Vec::new(), + immediate, + } + } + + /// An empty batch that defers every set pushed to it, for tests that run + /// one operation and then [`Self::verify`] (or inspect) what it deferred. + /// Test-only, so production code defers only through [`Self::deferred`]. + #[cfg(test)] + pub(crate) fn new() -> Self { + Self::empty(false) + } + + /// Runs `f` with a batch that defers every set pushed to it, then verifies + /// what is still pending: what a block's driver does, and the only way + /// outside tests to defer signatures. An error from `f` is returned as is, + /// since it rejects whatever `f` was processing whatever the signatures + /// say. + pub fn deferred(f: impl FnOnce(&mut SignatureBatch) -> Result) -> Result { + let mut batch = Self::empty(false); + let out = f(&mut batch)?; + batch.verify_pending()?; + Ok(out) + } + + /// An empty batch that checks each set the moment it is pushed. + /// + /// Block production's operation packer applies candidates one after the + /// other to a scratch state and keeps the ones that are accepted. A + /// candidate rejected only at the end of a deferred batch would already + /// have written to the scratch state, which the packer relies on never + /// happening, so its calls check as they go and behave exactly as the + /// immediate checks did. + pub fn immediate() -> Self { + Self::empty(true) + } + + /// Defers `set`, or rejects now with `what` when the set could not be built + /// (an invalid key or signature, an empty signer list): the same moment and + /// the same label the immediate check fails with. + pub fn push(&mut self, set: Option, what: &'static str) -> Result<()> { + let Some(set) = set else { + return verify(false, what); + }; + if self.immediate { + return verify(set.verify(), what); + } + self.sets.push(set); + self.labels.push(what); + Ok(()) + } + + /// Checks every set pushed since the last check, now and in one batch, + /// and rejects with the first failing set's `what` in push order: the + /// check the immediate path would have stopped at. + /// + /// For an operation about to write something no unverified signature may + /// decide (see the module documentation). It pushes its own set, then + /// calls this before writing, so its set and every one still pending are + /// checked first. A batch that checks at push time has nothing pending, so + /// for it this costs nothing. + /// + /// A failed batch is bisected to find its first failing set (see + /// [`first_failing`]), which costs about what the batch did, rather than + /// re-checking each set on its own, which costs a whole pairing per set on + /// top of the batch. + pub(crate) fn verify_pending(&mut self) -> Result<()> { + debug_assert_eq!(self.sets.len(), self.labels.len(), "one label per set"); + let outcome = check(&self.sets, &self.labels); + self.sets.clear(); + self.labels.clear(); + outcome + } + + /// [`Self::verify_pending`], for a test's batch nothing is pushed to + /// afterwards. + #[cfg(test)] + pub(crate) fn verify(mut self) -> Result<()> { + self.verify_pending() + } + + /// Whether no set is pending. + #[cfg(test)] + pub(crate) fn is_empty(&self) -> bool { + self.sets.is_empty() + } +} + +/// [`SignatureBatch::verify_pending`]'s check of `sets`, `labels[i]` naming +/// `sets[i]`. +fn check(sets: &[SignatureSet], labels: &[&'static str]) -> Result<()> { + if verify_signature_sets(sets) { + return Ok(()); + } + if let Some(first_bad) = first_failing(sets) { + return verify(false, labels[first_bad]); + } + // A batch of valid sets always passes, so the search should have landed + // on a set that fails alone. It did not, which is a bug in the batch + // check: the block is still rejected rather than accepted, but loudly, + // since a block every single check accepts is valid and rejecting it can + // fork this node off. + error!( + sets = sets.len(), + "Signature batch failed but the set it was narrowed to verifies on its own; rejecting the block" + ); + crate::metrics::inc_signature_batch_disagreements(); + let last = labels.last().copied().unwrap_or("block signatures"); + verify(false, last) +} + +/// The position of the first set in `sets` that fails on its own, given that +/// `sets` failed as one batch. `None` when the set that search lands on +/// verifies after all, which only a bug in the batch check can cause. +/// +/// Bisects: each step checks, as one batch, the first half of the range known +/// to hold the first failing set, and keeps that half if it fails or the +/// other if it passes. Each half checked is half the size of the one before, +/// so together they hold fewer sets than `sets` does, at one final +/// exponentiation per step: about 8 for the ~130 sets of a full pre-electra +/// block, where checking every set on its own costs 130 pairings (each its +/// own final exponentiation, and a Miller loop more than the set costs in a +/// batch, for its signature). +fn first_failing(sets: &[SignatureSet]) -> Option { + if sets.is_empty() { + return None; + } + // Every set before `start` verifies, and `sets[start..end]` holds the + // first that does not. `failed` is whether `sets[start..end]` itself was + // the last batch checked, and failed. + let (mut start, mut end) = (0, sets.len()); + let mut failed = true; + while end - start > 1 { + let middle = start + (end - start) / 2; + if verify_signature_sets(&sets[start..middle]) { + start = middle; + failed = false; + } else { + end = middle; + failed = true; + } + } + // A batch of one is that set's own check, so a range of one that failed + // as a batch needs no second look. + (failed || !sets[start].verify()).then_some(start) +} + +/// [`SignatureBatch::deferred`], under the short name the single-operation +/// tests use: they only mean "accepted" once their deferred signatures have +/// been checked. +#[cfg(test)] +pub(crate) fn checked(f: impl FnOnce(&mut SignatureBatch) -> Result) -> Result { + SignatureBatch::deferred(f) +} + +#[cfg(test)] +mod tests { + use blst::min_pk::SecretKey; + + use super::*; + use crate::beacon::bls::DST; + use crate::beacon::primitives::{BlsPubkey, BlsSignature, Root}; + + fn set(seed: u8, valid: bool) -> SignatureSet { + let key = SecretKey::key_gen(&[seed + 1; 32], &[]).unwrap(); + let pubkey = BlsPubkey(key.sk_to_pk().to_bytes()); + let message = Root::repeat_byte(seed); + let signed = if valid { + message + } else { + Root::repeat_byte(seed ^ 0xff) + }; + let signature = BlsSignature(key.sign(signed.as_slice(), DST, &[]).to_bytes()); + SignatureSet::single(&pubkey, message, &signature).unwrap() + } + + #[test] + fn an_empty_batch_verifies() { + assert!(SignatureBatch::new().verify().is_ok()); + } + + #[test] + fn valid_sets_verify_together() { + let mut batch = SignatureBatch::new(); + for seed in 0..5 { + batch.push(Some(set(seed, true)), "valid").unwrap(); + } + assert!(batch.verify().is_ok()); + } + + #[test] + fn a_set_that_could_not_be_built_rejects_at_once_with_its_label() { + let mut batch = SignatureBatch::new(); + let err = batch.push(None, "unbuildable").unwrap_err(); + assert!(format!("{err:?}").contains("unbuildable"), "{err:?}"); + } + + #[test] + fn a_failing_batch_names_the_first_bad_set() { + let mut batch = SignatureBatch::new(); + batch.push(Some(set(0, true)), "first good").unwrap(); + batch.push(Some(set(1, false)), "first bad").unwrap(); + batch.push(Some(set(2, true)), "second good").unwrap(); + batch.push(Some(set(3, false)), "second bad").unwrap(); + let err = batch.verify().unwrap_err(); + assert!(format!("{err:?}").contains("first bad"), "{err:?}"); + } + + /// The bisection lands on the first bad set wherever it sits and however + /// many bad sets follow it, for batch sizes that halve evenly and that do + /// not. + #[test] + fn bisection_finds_the_first_bad_set_at_every_position() { + for len in [1u8, 2, 3, 7, 8, 13] { + for first_bad in 0..len { + for later_bad in [None, Some(len - 1)] { + let sets: Vec = (0..len) + .map(|i| set(i, i != first_bad && Some(i) != later_bad)) + .collect(); + assert_eq!( + first_failing(&sets), + Some(usize::from(first_bad)), + "len {len}, first bad {first_bad}, later bad {later_bad:?}" + ); + } + } + } + } + + #[test] + fn verifying_clears_what_was_pending() { + let mut batch = SignatureBatch::new(); + batch.push(Some(set(0, true)), "good").unwrap(); + batch.verify_pending().unwrap(); + assert!(batch.is_empty()); + // A bad set pushed after a check fails the next one, and only it. + batch.push(Some(set(1, false)), "bad").unwrap(); + let err = batch.verify_pending().unwrap_err(); + assert!(format!("{err:?}").contains("bad"), "{err:?}"); + assert!(batch.is_empty()); + assert!(batch.verify().is_ok()); + } + + #[test] + fn a_deferred_failure_carries_the_label_an_immediate_one_does() { + let mut immediate = SignatureBatch::immediate(); + let early = immediate + .push(Some(set(1, false)), "the label") + .unwrap_err(); + + let mut deferred = SignatureBatch::new(); + deferred.push(Some(set(1, false)), "the label").unwrap(); + let late = deferred.verify().unwrap_err(); + + assert_eq!(format!("{early:?}"), format!("{late:?}")); + } + + #[test] + fn one_bad_signature_among_several_rejects_the_batch() { + let mut batch = SignatureBatch::new(); + for seed in 0..6 { + batch + .push(Some(set(seed, seed != 4)), "attestation") + .unwrap(); + } + assert!(batch.verify().is_err()); + } + + #[test] + fn an_immediate_batch_rejects_on_push() { + let mut batch = SignatureBatch::immediate(); + batch.push(Some(set(0, true)), "good").unwrap(); + let err = batch.push(Some(set(1, false)), "bad").unwrap_err(); + assert!(format!("{err:?}").contains("bad"), "{err:?}"); + } +} diff --git a/crates/blockchain/state_transition/src/metrics.rs b/crates/blockchain/state_transition/src/metrics.rs index 30c3c8bcd..25535fc58 100644 --- a/crates/blockchain/state_transition/src/metrics.rs +++ b/crates/blockchain/state_transition/src/metrics.rs @@ -45,6 +45,23 @@ pub fn inc_finalizations(result: &str) { LEAN_FINALIZATIONS_TOTAL.with_label_values(&[result]).inc(); } +static LEAN_BEACON_SIGNATURE_BATCH_DISAGREEMENTS_TOTAL: LazyLock = + LazyLock::new(|| { + register_int_counter!( + "lean_beacon_signature_batch_disagreements_total", + "Signature batches that failed although the set they narrowed to verifies on its own" + ) + .unwrap() + }); + +/// A block's signature batch failed, but the set its search for the failing +/// one landed on verifies alone, which only a bug in the batch check can +/// cause. The block is rejected all the same, so a non-zero value means this +/// node may be refusing valid blocks. +pub fn inc_signature_batch_disagreements() { + LEAN_BEACON_SIGNATURE_BATCH_DISAGREEMENTS_TOTAL.inc(); +} + static LEAN_BEACON_COMMITTEE_CACHE_LOOKUPS_TOTAL: LazyLock = LazyLock::new(|| { register_int_counter_vec!( "lean_beacon_committee_cache_lookups_total", diff --git a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs index 726932e58..0432a4c1e 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs @@ -142,6 +142,7 @@ use ethlambda_state_transition::beacon::stf::deneb as deneb_stf; use ethlambda_state_transition::beacon::stf::electra as electra_stf; use ethlambda_state_transition::beacon::stf::fulu as fulu_stf; use ethlambda_state_transition::beacon::stf::gloas as gloas_stf; +use ethlambda_state_transition::beacon::stf::signatures::SignatureBatch; use ethlambda_state_transition::beacon::stf::{ExecutionEngine, block, operations}; use libtest_mimic::{Failed, Trial}; @@ -175,6 +176,35 @@ fn apply( state: &mut BeaconState, config: &Config, ) -> Result<(), String> { + // Every signature an operation defers is checked once the operation has + // run, which is how a block's own driver treats them: a case whose only + // defect is a bad signature is still rejected, only later. + // + // The closure turns `run`'s nesting inside out. The crate's own `Err` is + // the closure's error, which skips the batch as a block's driver does. A + // case no handler covers ran nothing, so it passes through the batch + // (empty, so it verifies) as the closure's `Ok(Err(..))`, and the last + // step hands that error back. + SignatureBatch::deferred( + |signatures| match run(handler, case, state, config, signatures) { + Ok(verdict) => verdict.map(Ok), + Err(unhandled) => Ok(Err(unhandled)), + }, + ) + .map_err(|err| format!("{err:?}")) + .and_then(|handled| handled) +} + +/// [`apply`]'s dispatch, pushing every deferred signature into `signatures`. +/// The outer `Err` is a case no handler here covers; the inner result is the +/// crate's own verdict. +fn run( + handler: &str, + case: &Case, + state: &mut BeaconState, + config: &Config, + signatures: &mut SignatureBatch, +) -> Result, String> { // Only the `attestation` handler reads this. Bound up here so each of its // calls fits on one line; a case processes a single attestation, so there // is nothing for the cache to share between calls. @@ -289,6 +319,7 @@ fn apply( proposer, config, &committees, + signatures, ) }) } @@ -304,6 +335,7 @@ fn apply( proposer, &committees, &active_balances, + signatures, ) }) } @@ -318,6 +350,7 @@ fn apply( proposer, &committees, &active_balances, + signatures, ) }) } @@ -333,6 +366,7 @@ fn apply( proposer, &committees, &active_balances, + signatures, ) }) } @@ -351,6 +385,7 @@ fn apply( meta.parent_slot, &committees, &active_balances, + signatures, ) } ForkName::Lean => lean_is_not_a_fixture_fork("attestation"), @@ -363,7 +398,13 @@ fn apply( | ForkName::Capella | ForkName::Deneb => { let slashing: phase0::AttesterSlashing = case.ssz("attester_slashing"); - operations::process_attester_slashing(state, &slashing, config, &active_balances) + operations::process_attester_slashing( + state, + &slashing, + config, + &active_balances, + signatures, + ) } // Electra's container widens the same way `Attestation`'s does // (see the `attestation` arm above); the function itself is not @@ -371,14 +412,26 @@ fn apply( // Fulu's specification changes neither. ForkName::Electra | ForkName::Fulu => { let slashing: electra::AttesterSlashing = case.ssz("attester_slashing"); - electra_stf::process_attester_slashing(state, &slashing, config, &active_balances) + electra_stf::process_attester_slashing( + state, + &slashing, + config, + &active_balances, + signatures, + ) } // Gloas's container widens again (EIP-7688's unbounded // `AttestingIndices`); the function is still not behaviorally // modified, only transcribed against gloas's own type. ForkName::Gloas => { let slashing: gloas::AttesterSlashing = case.ssz("attester_slashing"); - gloas_stf::process_attester_slashing(state, &slashing, config, &active_balances) + gloas_stf::process_attester_slashing( + state, + &slashing, + config, + &active_balances, + signatures, + ) } ForkName::Lean => lean_is_not_a_fixture_fork("attester_slashing"), }, @@ -402,10 +455,15 @@ fn apply( &slashing, config, &active_balances, + signatures, + ), + ForkName::Gloas => gloas_stf::process_proposer_slashing( + state, + &slashing, + config, + &active_balances, + signatures, ), - ForkName::Gloas => { - gloas_stf::process_proposer_slashing(state, &slashing, config, &active_balances) - } ForkName::Lean => lean_is_not_a_fixture_fork("proposer_slashing"), } } @@ -452,14 +510,14 @@ fn apply( // Unchanged through capella. ForkName::Phase0 | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => { let exit: shared::SignedVoluntaryExit = case.ssz("voluntary_exit"); - operations::process_voluntary_exit(state, &exit, config) + operations::process_voluntary_exit(state, &exit, config, signatures) } // Deneb pins the signature to `CAPELLA_FORK_VERSION` (EIP-7044), // so an exit signed long before it takes effect never expires // out from under its own signer at a later fork boundary. ForkName::Deneb => { let exit: shared::SignedVoluntaryExit = case.ssz("voluntary_exit"); - deneb_stf::process_voluntary_exit(state, &exit, config) + deneb_stf::process_voluntary_exit(state, &exit, config, signatures) } // Electra adds a pending-partial-withdrawal check and its own // balance-churn exit-queue accounting (EIP-7251); neither fulu's @@ -469,7 +527,13 @@ fn apply( // `process_voluntary_exit`; see that function's own doc). ForkName::Electra | ForkName::Fulu | ForkName::Gloas => { let exit: shared::SignedVoluntaryExit = case.ssz("voluntary_exit"); - electra_stf::process_voluntary_exit(state, &exit, config, &active_balances) + electra_stf::process_voluntary_exit( + state, + &exit, + config, + &active_balances, + signatures, + ) } ForkName::Lean => lean_is_not_a_fixture_fork("voluntary_exit"), }, @@ -482,6 +546,7 @@ fn apply( &sync_aggregate, &active_balances, &pubkey_indices, + signatures, ) } // The fixture's operation file is a whole `BeaconBlockBody`, not a @@ -568,7 +633,7 @@ fn apply( // from here on. "bls_to_execution_change" => { let signed_change: capella::SignedBLSToExecutionChange = case.ssz("address_change"); - capella_stf::process_bls_to_execution_change(state, &signed_change, config) + capella_stf::process_bls_to_execution_change(state, &signed_change, config, signatures) } "withdrawals" => match case.fork { ForkName::Capella => { @@ -608,7 +673,7 @@ fn apply( // builder's payload. "execution_payload_bid" => { let signed_bid: gloas::SignedExecutionPayloadBid = case.ssz("execution_payload_bid"); - gloas_stf::process_execution_payload_bid(state, &signed_bid, config) + gloas_stf::process_execution_payload_bid(state, &signed_bid, config, signatures) } // New in gloas (EIP-7732): the *next* block's processing of the // previous slot's payload, so the fixture's operation file is a whole @@ -697,12 +762,12 @@ fn apply( // whether the *previous* slot's payload showed up on time. "payload_attestation" => { let payload_attestation: gloas::PayloadAttestation = case.ssz("payload_attestation"); - gloas_stf::process_payload_attestation(state, &payload_attestation, config) + gloas_stf::process_payload_attestation(state, &payload_attestation, config, signatures) } other => return Err(format!("unhandled operation `{other}`")), }; - outcome.map_err(|err| format!("{err:?}")) + Ok(outcome) } pub fn trials() -> Vec { diff --git a/crates/common/crypto/Cargo.toml b/crates/common/crypto/Cargo.toml index bce0ef64f..c324d55e1 100644 --- a/crates/common/crypto/Cargo.toml +++ b/crates/common/crypto/Cargo.toml @@ -13,6 +13,7 @@ version.workspace = true blst = "0.3.16" ethlambda-metrics.workspace = true ethlambda-types.workspace = true +rand.workspace = true rayon.workspace = true leanvm.workspace = true diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 58b335dc0..131e0fa05 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -105,6 +105,9 @@ use ethlambda_types::beacon::primitives::{ BLS_PUBKEY_SIZE, BLS_SIGNATURE_SIZE, BlsPubkey, BlsSignature, Root, }; +mod batch; +pub use batch::{EthFastAggregate, SignatureSet, verify_signature_sets}; + /// The ciphersuite the specification pins BLS signatures to: the IETF BLS /// draft's proof-of-possession scheme over BLS12-381's G2, using SHA-256 in /// the XMD hash-to-curve construction. @@ -323,15 +326,11 @@ pub const G2_POINT_AT_INFINITY: [u8; BLS_SIGNATURE_SIZE] = g2_point_at_infinity( /// here rather than panicking or returning an error: an unparseable or /// invalid-point key is treated exactly like a valid key over the wrong /// signature, since the specification never distinguishes the two. +/// +/// [`SignatureSet::single`] and its [`SignatureSet::verify`], so this and a +/// block's batched check of the same signature are one definition. pub fn verify(pubkey: &BlsPubkey, message: Root, signature: &BlsSignature) -> bool { - let Some(pubkey) = validated_pubkey(pubkey) else { - return false; - }; - let Ok(signature) = Signature::sig_validate(signature.as_ref(), false) else { - return false; - }; - let result = signature.verify(false, message.as_slice(), DST, &[], &pubkey, false); - result == BLST_ERROR::BLST_SUCCESS + SignatureSet::single(pubkey, message, signature).is_some_and(|set| set.verify()) } /// The specification's `bls.Aggregate(signatures) -> BLSSignature`. @@ -397,31 +396,15 @@ pub fn aggregate_verify( /// Electra block's attestations, where a single aggregate can carry /// thousands of signers behind one shared message; keys go through /// [`PubkeyCache`], see the module documentation for why that is safe. +/// +/// [`SignatureSet::fast_aggregate`] and its [`SignatureSet::verify`], so this +/// and a block's batched check of the same aggregate are one definition. pub fn fast_aggregate_verify( pubkeys: &[BlsPubkey], message: Root, signature: &BlsSignature, ) -> bool { - if pubkeys.is_empty() { - return false; - } - let Ok(signature) = Signature::sig_validate(signature.as_ref(), false) else { - return false; - }; - let Some(points) = validated_pubkeys(pubkeys) else { - return false; - }; - // `points` is non-empty because of the early return on an empty `pubkeys` - // at the top, not because of `validated_pubkeys` (which maps an empty - // input to an empty `Some`), so the `None` arm is unreachable. The - // signature was validated above, which is why the group check stays off - // here. - let Some(aggregated) = sum_validated_points(&points) else { - return false; - }; - let result = - signature.fast_aggregate_verify_pre_aggregated(false, message.as_slice(), DST, &aggregated); - result == BLST_ERROR::BLST_SUCCESS + SignatureSet::fast_aggregate(pubkeys, message, signature).is_some_and(|set| set.verify()) } /// `specs/altair/bls.md`'s `eth_aggregate_pubkeys(pubkeys) -> BLSPubkey`. @@ -456,15 +439,15 @@ pub fn eth_aggregate_pubkeys(pubkeys: &[BlsPubkey]) -> Result { /// the identity element by convention rather than "no signature was /// provided"; [`fast_aggregate_verify`] itself has no such case, since the /// underlying IETF ciphersuite it wraps was never given one. +/// +/// [`SignatureSet::eth_fast_aggregate`] with its pairing run now, which is +/// where that rule is written down once. pub fn eth_fast_aggregate_verify( pubkeys: &[BlsPubkey], message: Root, signature: &BlsSignature, ) -> bool { - if pubkeys.is_empty() { - return signature.as_ref() == G2_POINT_AT_INFINITY; - } - fast_aggregate_verify(pubkeys, message, signature) + SignatureSet::eth_fast_aggregate(pubkeys, message, signature).verify() } /// The specification's `bls.KeyValidate(pubkey) -> bool`. diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs new file mode 100644 index 000000000..7dc6cf84d --- /dev/null +++ b/crates/common/crypto/src/bls/batch.rs @@ -0,0 +1,559 @@ +//! Signature checks resolved ahead of time, so a block's worth of them can be +//! verified in one pairing batch instead of one pairing each. +//! +//! # Why a batch is cheaper +//! +//! A signature check is two parts: resolving its inputs (validating the keys, +//! summing them, decoding the signature) and the pairing equation. The pairing +//! is dominated by its final exponentiation, which every standalone check pays +//! in full. Checking `n` equations `e(pk_i, H(m_i)) = e(g1, sig_i)` together +//! needs one Miller loop per set but only one final exponentiation for all of +//! them, so a block with a dozen signature sets spends a fraction of what a +//! dozen separate checks do. +//! +//! # Why the weights are random +//! +//! Multiplying the equations together unweighted is unsound: an attacker who +//! can pick two invalid signatures `s1 * d` and `s2 / d` (for some group +//! element `d`) makes the product of the two checks hold although neither +//! check does. Each set is therefore weighted by its own random scalar +//! before the products are combined (the random linear combination of +//! ). +//! The weights must be unpredictable to whoever chose the signatures: with +//! independent uniform 64-bit weights, a forged batch passes with probability +//! about 2^-64, which is the soundness level every consensus client accepts. +//! They are drawn from `rand::rng()`, a thread-local ChaCha12 generator seeded +//! from the operating system (`SysRng`), and soundness rests on that +//! unpredictability alone: rand 0.10.1, the version locked here, also reseeds +//! it from the operating system after every 64 KiB of output, but nothing +//! here depends on that. A weight of zero is redrawn, since it would drop its +//! set from the equation altogether. +//! +//! # What a batch answers +//! +//! Only whether every set holds. When it fails it cannot say which set did; +//! callers that need to name one check smaller batches or single sets +//! afterwards ([`SignatureSet::verify`]). A single set's check is the +//! standalone one: [`super::verify`], [`super::fast_aggregate_verify`] and +//! [`super::eth_fast_aggregate_verify`] are built on [`SignatureSet`], so a +//! signature gossip accepts and one a block's batch accepts are decided by +//! the same code. + +use blst::BLST_ERROR; +use blst::blst_scalar; +use blst::min_pk::{PublicKey, Signature}; +use rand::RngExt; + +use ethlambda_types::beacon::primitives::{BlsPubkey, BlsSignature, Root}; + +use super::{DST, G2_POINT_AT_INFINITY, validated_pubkey, validated_pubkeys}; + +/// How many bits of each random weight the batch check uses. +const RAND_BITS: usize = 64; + +/// One signature check, resolved and ready, that may be verified alone or in a +/// batch. +/// +/// Holds the signer's key already validated (and, for an aggregate, summed), +/// the signed message and the signature already validated, so building one is +/// everything a check does short of the pairing. That split is what lets a +/// caller decide *when* to pay for the pairing: at once ([`Self::verify`]) or +/// together with the other sets of a block ([`verify_signature_sets`]). +/// +/// `Clone` but not `Copy`: at about 300 bytes, a copy should be one the code +/// asks for. +#[derive(Clone)] +pub struct SignatureSet { + pubkey: PublicKey, + message: Root, + signature: Signature, + /// Whether `pubkey` is a sum of keys, checked the way + /// `bls.FastAggregateVerify` checks one, rather than a single registry key. + aggregated: bool, +} + +impl SignatureSet { + /// `bls.Verify`'s inputs. + /// + /// `None` when the key or the signature fails validation, which is exactly + /// when [`super::verify`] returns false before reaching the pairing. + pub fn single(pubkey: &BlsPubkey, message: Root, signature: &BlsSignature) -> Option { + let pubkey = validated_pubkey(pubkey)?; + let signature = Signature::sig_validate(signature.as_ref(), false).ok()?; + Some(Self { + pubkey, + message, + signature, + aggregated: false, + }) + } + + /// `bls.FastAggregateVerify`'s inputs. + /// + /// `None` for an empty `pubkeys`, any key that fails `key_validate`, or a + /// signature that fails `sig_validate`: the cases + /// [`super::fast_aggregate_verify`] answers false for before the pairing. + pub fn fast_aggregate( + pubkeys: &[BlsPubkey], + message: Root, + signature: &BlsSignature, + ) -> Option { + if pubkeys.is_empty() { + return None; + } + let signature = Signature::sig_validate(signature.as_ref(), false).ok()?; + let points = validated_pubkeys(pubkeys)?; + // Every point above already passed `key_validate`, so the sum does + // not re-check them. + let pubkey = super::sum_validated_points(&points)?; + Some(Self { + pubkey, + message, + signature, + aggregated: true, + }) + } + + /// `eth_fast_aggregate_verify`'s inputs (`specs/altair/bls.md`): what + /// [`Self::fast_aggregate`] builds, except that no participants at all is + /// valid exactly when the signature is [`G2_POINT_AT_INFINITY`], and needs + /// no pairing. + /// + /// The one definition of that rule: [`super::eth_fast_aggregate_verify`] + /// is [`EthFastAggregate::verify`] over this, and a block's sync + /// aggregate goes through it, so block production's own check of an + /// aggregate and the state transition's cannot disagree. + pub fn eth_fast_aggregate( + pubkeys: &[BlsPubkey], + message: Root, + signature: &BlsSignature, + ) -> EthFastAggregate { + if pubkeys.is_empty() { + return if signature.as_ref() == G2_POINT_AT_INFINITY { + EthFastAggregate::Valid + } else { + EthFastAggregate::Invalid + }; + } + match Self::fast_aggregate(pubkeys, message, signature) { + Some(set) => EthFastAggregate::Pairing(set), + None => EthFastAggregate::Invalid, + } + } + + /// The pairing check on its own: the same answer [`super::verify`] or + /// [`super::fast_aggregate_verify`] gives for these inputs, since both are + /// this check. + pub fn verify(&self) -> bool { + let result = if self.aggregated { + self.signature.fast_aggregate_verify_pre_aggregated( + false, + self.message.as_slice(), + DST, + &self.pubkey, + ) + } else { + self.signature.verify( + false, + self.message.as_slice(), + DST, + &[], + &self.pubkey, + false, + ) + }; + result == BLST_ERROR::BLST_SUCCESS + } +} + +/// What [`SignatureSet::eth_fast_aggregate`] makes of its inputs: a check +/// already decided without a pairing, or the set whose pairing decides it. +// `Pairing` is the usual answer and is moved straight on into a batch or +// checked; boxing it would allocate per check to shrink a value that never +// stays around. +#[allow(clippy::large_enum_variant)] +pub enum EthFastAggregate { + /// No participants, and the signature is the point at infinity. + Valid, + /// No participants with any other signature, or a key or the signature + /// that does not validate. + Invalid, + /// The participants' check, decided by its pairing. + Pairing(SignatureSet), +} + +impl EthFastAggregate { + /// The answer with the pairing, if any, run now: + /// `eth_fast_aggregate_verify`'s. + pub fn verify(&self) -> bool { + match self { + Self::Valid => true, + Self::Invalid => false, + Self::Pairing(set) => set.verify(), + } + } +} + +/// A random weight in `1..2^64`, as the little-endian scalar `blst` expects. +fn random_weight(rng: &mut impl RngExt) -> blst_scalar { + let weight = loop { + let candidate: u64 = rng.random(); + if candidate != 0 { + break candidate; + } + }; + let mut scalar = blst_scalar::default(); + scalar.b[..8].copy_from_slice(&weight.to_le_bytes()); + scalar +} + +/// Whether every set verifies, in one random-linear-combination check: +/// `verify_multiple_aggregate_signatures` with 64-bit scalars from the +/// OS-seeded RNG and one final exponentiation. An empty slice verifies. +/// +/// See the module documentation for why the weights are random. A single set +/// skips them and is verified directly, since one equation needs no +/// combining. +/// +/// A set whose key is the identity (keys that sum to it, such as `[P, -P]`) +/// fails here as it fails [`SignatureSet::verify`], with nothing in this crate +/// checking for it: blst refuses an infinite public key wherever it adds a +/// key to a pairing (`BLST_PK_IS_INFINITY` in `blst/src/aggregate.c`), which +/// both this batch and the single check go through. +pub fn verify_signature_sets(sets: &[SignatureSet]) -> bool { + match sets { + [] => return true, + [only] => return only.verify(), + _ => {} + } + let mut rng = rand::rng(); + let weights: Vec = sets.iter().map(|_| random_weight(&mut rng)).collect(); + let messages: Vec<&[u8]> = sets.iter().map(|set| set.message.as_slice()).collect(); + let pubkeys: Vec<&PublicKey> = sets.iter().map(|set| &set.pubkey).collect(); + let signatures: Vec<&Signature> = sets.iter().map(|set| &set.signature).collect(); + // Keys and signatures were validated when the sets were built. + let result = Signature::verify_multiple_aggregate_signatures( + &messages, + DST, + &pubkeys, + false, + &signatures, + false, + &weights, + RAND_BITS, + ); + result == BLST_ERROR::BLST_SUCCESS +} + +#[cfg(test)] +mod tests { + use blst::min_pk::SecretKey; + + use super::*; + use crate::bls; + + fn secret_key(seed: u8) -> SecretKey { + let mut ikm = [seed; 32]; + ikm[0] = ikm[0].wrapping_add(1); + SecretKey::key_gen(&ikm, &[]).unwrap() + } + + fn pubkey_of(key: &SecretKey) -> BlsPubkey { + BlsPubkey(key.sk_to_pk().to_bytes()) + } + + fn sign(key: &SecretKey, message: Root) -> BlsSignature { + BlsSignature(key.sign(message.as_slice(), DST, &[]).to_bytes()) + } + + fn message(byte: u8) -> Root { + Root::from([byte; 32]) + } + + /// `n` single-key sets over distinct messages, all valid. + fn valid_sets(n: u8) -> Vec { + (0..n) + .map(|i| { + let key = secret_key(i); + let msg = message(i); + SignatureSet::single(&pubkey_of(&key), msg, &sign(&key, msg)).unwrap() + }) + .collect() + } + + #[test] + fn a_valid_batch_of_ten_verifies() { + assert!(verify_signature_sets(&valid_sets(10))); + } + + #[test] + fn an_empty_batch_verifies() { + assert!(verify_signature_sets(&[])); + } + + #[test] + fn a_single_set_batch_verifies_or_fails_alone() { + assert!(verify_signature_sets(&valid_sets(1))); + let key = secret_key(1); + let wrong = + SignatureSet::single(&pubkey_of(&key), message(9), &sign(&key, message(8))).unwrap(); + assert!(!verify_signature_sets(&[wrong])); + } + + #[test] + fn one_bad_set_among_ten_fails() { + for bad in [0u8, 4, 9] { + let mut sets = valid_sets(10); + let key = secret_key(bad); + // A well-formed signature over a different message. + sets[usize::from(bad)] = + SignatureSet::single(&pubkey_of(&key), message(bad), &sign(&key, message(200))) + .unwrap(); + assert!(!verify_signature_sets(&sets), "bad set {bad}"); + } + } + + #[test] + fn swapped_signatures_fail() { + let (k0, k1) = (secret_key(0), secret_key(1)); + let (m0, m1) = (message(0), message(1)); + let (s0, s1) = (sign(&k0, m0), sign(&k1, m1)); + let a = SignatureSet::single(&pubkey_of(&k0), m0, &s1).unwrap(); + let b = SignatureSet::single(&pubkey_of(&k1), m1, &s0).unwrap(); + assert!(!a.verify()); + assert!(!b.verify()); + assert!(!verify_signature_sets(&[a, b])); + } + + #[test] + fn repeated_messages_across_sets_verify() { + let msg = message(7); + let sets: Vec = (0..6) + .map(|i| { + let key = secret_key(i); + SignatureSet::single(&pubkey_of(&key), msg, &sign(&key, msg)).unwrap() + }) + .collect(); + assert!(verify_signature_sets(&sets)); + } + + #[test] + fn single_and_aggregate_sets_mix_in_one_batch() { + let mut sets = valid_sets(3); + let keys: Vec = (10..14).map(secret_key).collect(); + let msg = message(99); + let signatures: Vec = keys.iter().map(|key| sign(key, msg)).collect(); + let aggregate = bls::aggregate(&signatures).unwrap(); + let pubkeys: Vec = keys.iter().map(pubkey_of).collect(); + sets.push(SignatureSet::fast_aggregate(&pubkeys, msg, &aggregate).unwrap()); + assert!(verify_signature_sets(&sets)); + } + + #[test] + fn an_invalid_aggregate_among_valid_sets_fails_the_batch() { + // The batch check goes through blst's multi-signature routine and the + // single check through `fast_aggregate_verify_pre_aggregated`, so an + // aggregate both must refuse is checked inside a batch, where a false + // accept would pass a block. + let keys: Vec = (20..24).map(secret_key).collect(); + let pubkeys: Vec = keys.iter().map(pubkey_of).collect(); + let msg = message(77); + let signed_by = |keys: &[SecretKey], msg: Root| { + let signatures: Vec = keys.iter().map(|key| sign(key, msg)).collect(); + bls::aggregate(&signatures).unwrap() + }; + let cases = [ + // One signer's share is missing from the signature. + ( + "missing signer", + pubkeys.clone(), + signed_by(&keys[1..], msg), + ), + // Every signer signed, but another message. + ( + "other message", + pubkeys.clone(), + signed_by(&keys, message(78)), + ), + // A key is claimed that never signed. + ( + "extra key", + [pubkeys.clone(), vec![pubkey_of(&secret_key(30))]].concat(), + signed_by(&keys, msg), + ), + ]; + for (case, claimed, signature) in cases { + let bad = SignatureSet::fast_aggregate(&claimed, msg, &signature).unwrap(); + assert!(!bad.verify(), "{case}: the single check accepted it"); + let mut sets = valid_sets(5); + sets.insert(2, bad); + assert!( + !verify_signature_sets(&sets), + "{case}: the batch accepted it" + ); + } + } + + #[test] + fn verify_agrees_with_bls_verify() { + let key = secret_key(3); + let pubkey = pubkey_of(&key); + let msg = message(3); + let good = sign(&key, msg); + let wrong_message = sign(&key, message(4)); + let other_key = sign(&secret_key(5), msg); + for signature in [good, wrong_message, other_key] { + let set = SignatureSet::single(&pubkey, msg, &signature).unwrap(); + assert_eq!(set.verify(), bls::verify(&pubkey, msg, &signature)); + } + } + + #[test] + fn verify_agrees_with_fast_aggregate_verify() { + let keys: Vec = (0..5).map(secret_key).collect(); + let pubkeys: Vec = keys.iter().map(pubkey_of).collect(); + let msg = message(42); + let signatures: Vec = keys.iter().map(|key| sign(key, msg)).collect(); + let good = bls::aggregate(&signatures).unwrap(); + let short = bls::aggregate(&signatures[1..]).unwrap(); + for (signature, message) in [(good, msg), (short, msg), (good, message(43))] { + let set = SignatureSet::fast_aggregate(&pubkeys, message, &signature).unwrap(); + assert_eq!( + set.verify(), + bls::fast_aggregate_verify(&pubkeys, message, &signature) + ); + } + } + + #[test] + fn invalid_inputs_build_no_set() { + let key = secret_key(1); + let pubkey = pubkey_of(&key); + let msg = message(1); + let signature = sign(&key, msg); + let garbage_key = BlsPubkey([0xff; 48]); + let garbage_signature = BlsSignature([0xff; 96]); + + assert!(SignatureSet::single(&garbage_key, msg, &signature).is_none()); + assert!(SignatureSet::single(&pubkey, msg, &garbage_signature).is_none()); + assert!(SignatureSet::fast_aggregate(&[], msg, &signature).is_none()); + assert!(SignatureSet::fast_aggregate(&[pubkey, garbage_key], msg, &signature).is_none()); + assert!(SignatureSet::fast_aggregate(&[pubkey], msg, &garbage_signature).is_none()); + // The identity signature is a valid encoding: it builds a set that fails. + let infinity = BlsSignature(bls::G2_POINT_AT_INFINITY); + let set = SignatureSet::single(&pubkey, msg, &infinity).unwrap(); + assert!(!set.verify()); + } + + #[test] + fn an_infinity_signature_among_valid_sets_fails_the_batch() { + let mut sets = valid_sets(4); + let key = secret_key(1); + let infinity = BlsSignature(bls::G2_POINT_AT_INFINITY); + sets[1] = SignatureSet::single(&pubkey_of(&key), message(1), &infinity).unwrap(); + assert!(!verify_signature_sets(&sets)); + } + + /// `-P`, from `P`'s compressed encoding (the sort flag picks the other + /// `y`). Valid on its own, so a registry can hold both. + fn negated(pubkey: BlsPubkey) -> BlsPubkey { + let mut bytes = pubkey.0; + bytes[0] ^= 0b0010_0000; + BlsPubkey(bytes) + } + + #[test] + fn keys_that_cancel_never_verify_alone_or_in_a_batch() { + // `[P, -P]` sums to the identity, and with the identity signature that + // is the cancellation the rule against identity keys exists for. A + // real signature over the message must not pass either. + let key = secret_key(20); + let pubkey = pubkey_of(&key); + let pair = [pubkey, negated(pubkey)]; + let msg = message(20); + let infinity = BlsSignature(bls::G2_POINT_AT_INFINITY); + for signature in [infinity, sign(&key, msg)] { + let set = SignatureSet::fast_aggregate(&pair, msg, &signature).unwrap(); + assert!( + set.pubkey == PublicKey::default(), + "the keys sum to the identity" + ); + assert!(!set.verify()); + assert_eq!( + set.verify(), + bls::fast_aggregate_verify(&pair, msg, &signature) + ); + + let mut sets = valid_sets(3); + sets.push(set); + assert!(!verify_signature_sets(&sets)); + } + + // `P + P - P` is `P`, so `P`'s own signature verifies. + let three = [pubkey, pubkey, negated(pubkey)]; + let set = SignatureSet::fast_aggregate(&three, msg, &sign(&key, msg)).unwrap(); + assert!(set.pubkey != PublicKey::default()); + let mut sets = valid_sets(3); + sets.push(set); + assert!(verify_signature_sets(&sets)); + } + + /// `eth_fast_aggregate_verify`'s carve-out: no participants is valid with + /// the point at infinity and nothing else, and needs no pairing either way. + #[test] + fn no_participants_is_decided_without_a_pairing() { + let msg = message(50); + let infinity = BlsSignature(bls::G2_POINT_AT_INFINITY); + let real = sign(&secret_key(50), msg); + assert!(matches!( + SignatureSet::eth_fast_aggregate(&[], msg, &infinity), + EthFastAggregate::Valid + )); + for signature in [real, BlsSignature([0; 96])] { + assert!(matches!( + SignatureSet::eth_fast_aggregate(&[], msg, &signature), + EthFastAggregate::Invalid + )); + } + } + + /// With participants, the answer is `fast_aggregate`'s set and its + /// pairing, and `eth_fast_aggregate_verify` is that answer. + #[test] + fn participants_are_checked_by_their_pairing() { + let keys: Vec = (60..63).map(secret_key).collect(); + let pubkeys: Vec = keys.iter().map(pubkey_of).collect(); + let msg = message(60); + let signatures: Vec = keys.iter().map(|key| sign(key, msg)).collect(); + let good = bls::aggregate(&signatures).unwrap(); + let short = bls::aggregate(&signatures[1..]).unwrap(); + + let check = SignatureSet::eth_fast_aggregate(&pubkeys, msg, &good); + assert!(matches!(check, EthFastAggregate::Pairing(_)) && check.verify()); + let check = SignatureSet::eth_fast_aggregate(&pubkeys, msg, &short); + assert!(matches!(check, EthFastAggregate::Pairing(_)) && !check.verify()); + let infinity = BlsSignature(bls::G2_POINT_AT_INFINITY); + assert!(!SignatureSet::eth_fast_aggregate(&pubkeys, msg, &infinity).verify()); + let garbage = BlsPubkey([0xff; 48]); + assert!(matches!( + SignatureSet::eth_fast_aggregate(&[pubkeys[0], garbage], msg, &good), + EthFastAggregate::Invalid + )); + for signature in [good, short, infinity] { + assert_eq!( + SignatureSet::eth_fast_aggregate(&pubkeys, msg, &signature).verify(), + bls::eth_fast_aggregate_verify(&pubkeys, msg, &signature) + ); + } + } + + #[test] + fn random_weights_are_never_zero() { + let mut rng = rand::rng(); + for _ in 0..10_000 { + let weight = random_weight(&mut rng); + assert!(weight.b[..8].iter().any(|byte| *byte != 0)); + assert!(weight.b[8..].iter().all(|byte| *byte == 0)); + } + } +} diff --git a/crates/net/rpc/src/beacon/validator_client_tests.rs b/crates/net/rpc/src/beacon/validator_client_tests.rs index f371bfef6..c77210a17 100644 --- a/crates/net/rpc/src/beacon/validator_client_tests.rs +++ b/crates/net/rpc/src/beacon/validator_client_tests.rs @@ -1255,13 +1255,16 @@ fn assert_full_sync_aggregate( "every member signed the parent" ); let mut state = advanced.clone(); - ethlambda_state_transition::beacon::stf::altair::process_sync_aggregate( - &mut state, - aggregate, - ðlambda_state_transition::beacon::helpers::accessors::ActiveBalanceCache::default(), - ðlambda_state_transition::beacon::helpers::accessors::PubkeyIndexCache::default(), - ) - .expect("the packed aggregate verifies as the state transition checks it"); + ethlambda_state_transition::beacon::stf::signatures::SignatureBatch::deferred(|signatures| { + ethlambda_state_transition::beacon::stf::altair::process_sync_aggregate( + &mut state, + aggregate, + ðlambda_state_transition::beacon::helpers::accessors::ActiveBalanceCache::default(), + ðlambda_state_transition::beacon::helpers::accessors::PubkeyIndexCache::default(), + signatures, + ) + }) + .expect("the packed aggregate is well formed and verifies as the state transition checks it"); } /// A sync committee member's slot through this node: the duties and diff --git a/docs/metrics.md b/docs/metrics.md index e1a19b128..d99d9cc59 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -529,6 +529,7 @@ leanMetrics spec. | Name | Type | Usage | Sample collection event | Labels | |------|------|-------|-------------------------|--------| | `lean_beacon_committee_cache_lookups_total` | Counter | Committee lookups, by whether the cache served them | On every `CommitteeCacheExt::committees` call: the state transition's, fork choice's and p2p's gossip validation's attestation processing | result=hit,miss,unkeyable | +| `lean_beacon_signature_batch_disagreements_total` | Counter | Signature batches that failed although the set the search for the failing one narrowed to verifies on its own: a bug in the batch check. The block is rejected anyway, so any non-zero value means valid blocks may be refused | When a block's deferred signature batch fails and the set its bisection lands on passes alone | | **Read the miss rate against the epoch rate, not the hit rate.** A follower on one chain misses about once per epoch, when the first block of a new epoch asks