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/bin/ethlambda/src/benchmark/import/mod.rs b/bin/ethlambda/src/benchmark/import/mod.rs index f8344b803..eb513e4c5 100644 --- a/bin/ethlambda/src/benchmark/import/mod.rs +++ b/bin/ethlambda/src/benchmark/import/mod.rs @@ -91,6 +91,12 @@ pub(crate) struct ReplayOptions { /// sleep is outside every measured span. 0 feeds blocks back to back. #[arg(long, value_name = "MILLISECONDS", default_value_t = 0)] pub block_delay: u64, + /// Warm each epoch's committee key sums before its first block is + /// imported, outside every measured span. A live node does this on a + /// worker between slots (the epoch precompute); a replay has no tick + /// loop, so without this flag part of the import never sees warm sums. + #[arg(long)] + pub warm_committee_sums: bool, /// Report format printed to stdout. Logs go to stderr, so JSON output can /// be piped directly (e.g. into jq). #[arg(long, value_enum, default_value_t = OutputFormat::Human)] @@ -194,6 +200,12 @@ mod tests { .replay } + #[test] + fn warm_committee_sums_is_off_unless_asked_for() { + assert!(!parse(&[]).warm_committee_sums); + assert!(parse(&["--warm-committee-sums"]).warm_committee_sums); + } + #[test] fn block_delay_defaults_to_zero_and_parses_milliseconds() { assert_eq!(parse(&[]).block_delay, 0); diff --git a/bin/ethlambda/src/benchmark/import/replay.rs b/bin/ethlambda/src/benchmark/import/replay.rs index f0365571f..a96fb7923 100644 --- a/bin/ethlambda/src/benchmark/import/replay.rs +++ b/bin/ethlambda/src/benchmark/import/replay.rs @@ -24,6 +24,7 @@ use ethlambda_blockchain::metrics::{BLOCK_ARRIVAL_PHASES, BLOCK_IMPORT_PHASES}; use ethlambda_blockchain::{BlockChainServer, ImportOutcome}; use ethlambda_p2p::beacon::decode; use ethlambda_state_transition::beacon::fork_choice; +use ethlambda_state_transition::beacon::helpers::accessors::CommitteeCacheExt as _; use ethlambda_storage::Store; use ethlambda_storage::backend::RocksDBBackend; use ethlambda_types::ShortRoot; @@ -123,10 +124,17 @@ pub(crate) async fn replay_corpus(dir: &Path, options: &ReplayOptions) -> eyre:: BlockChainServer::for_replay(store, None, options.safe_slots_to_import_optimistically); let mut samples = Vec::with_capacity(manifest.slots.len()); + // The epoch of the block imported before the one in hand, for + // `--warm-committee-sums`. + let mut previous_epoch: Option = None; let warmup_total = manifest.warmup_slots.len(); for (position, &slot) in manifest.warmup_slots.iter().enumerate() { let (block, block_root) = load_block(dir, &config, &mut clock, slot)?; + if options.warm_committee_sums { + warm_committee_sums(&clock, &block, previous_epoch)?; + } + previous_epoch = Some(slot / preset::SLOTS_PER_EPOCH); let wall_seconds = import(&mut server, block, slot, block_root).await?; eprintln!( "warm-up {}/{warmup_total}: slot {slot} imported in {}", @@ -139,6 +147,12 @@ pub(crate) async fn replay_corpus(dir: &Path, options: &ReplayOptions) -> eyre:: let total = manifest.slots.len(); for (iteration, &slot) in manifest.slots.iter().enumerate() { let (block, block_root) = load_block(dir, &config, &mut clock, slot)?; + // Before the timer starts, so the warm-up is in no phase and no + // `wall_seconds`. + if options.warm_committee_sums { + warm_committee_sums(&clock, &block, previous_epoch)?; + } + previous_epoch = Some(slot / preset::SLOTS_PER_EPOCH); let timer = PhaseTimer::start(IMPORT_PHASE_HISTOGRAM); let wall_seconds = import(&mut server, block, slot, block_root).await?; @@ -164,7 +178,12 @@ pub(crate) async fn replay_corpus(dir: &Path, options: &ReplayOptions) -> eyre:: Ok(Report::new( Environment::collect(), - params_from(&manifest, dir, options.block_delay), + params_from( + &manifest, + dir, + options.block_delay, + options.warm_committee_sums, + ), samples, )) } @@ -198,6 +217,53 @@ fn load_block( Ok((block, block_root)) } +/// Warms the committee key sums a live node's epoch precompute worker would +/// have warmed before `block` arrives: its epoch's, when that epoch is later +/// than `previous_epoch`'s, and, before the first block of the replay +/// (`previous_epoch` is `None`), the epoch before as well. +/// +/// The worker advances the head's post-state to the epoch start and warms +/// from that state; this does the same from the block's parent post-state, so +/// the shuffling key it files the sums under is the one the import looks up. +/// Nothing else the worker does is reproduced: in a replay the import +/// already produces the epoch's state itself, inside the measured span. +fn warm_committee_sums( + store: &Store, + block: &SignedBeaconBlock, + previous_epoch: Option, +) -> eyre::Result<()> { + let epoch = block.slot() / preset::SLOTS_PER_EPOCH; + let epochs: Vec = match previous_epoch { + Some(previous) if epoch <= previous => return Ok(()), + // The epoch right after the last one: its predecessor was warmed when + // it began. + Some(previous) if epoch == previous + 1 => vec![epoch], + // The first block, or the first after a gap in the corpus: the + // previous epoch was never warmed here, so warm it too, as the live + // worker warms both on every boundary. + _ => epoch.checked_sub(1).into_iter().chain([epoch]).collect(), + }; + let parent_root = block.parent_root(); + let parent_state = store.get_state(&parent_root)?.ok_or_else(|| { + eyre::eyre!( + "no post-state for parent {} of the block at slot {}", + ShortRoot(&parent_root.0), + block.slot() + ) + })?; + let config = store.config(); + let committee_cache = store.committee_cache(); + for epoch in epochs { + if parent_state.slot() < epoch * preset::SLOTS_PER_EPOCH { + let advanced = fork_choice::advance_to_epoch_start(&parent_state, epoch, &config)?; + committee_cache.warm_key_sums(&advanced, epoch); + } else { + committee_cache.warm_key_sums(&parent_state, epoch); + } + } + Ok(()) +} + /// Import one block, returning the wall time of the `import_block` call /// alone, and fail unless it imported. async fn import( @@ -240,7 +306,12 @@ fn read_anchor(dir: &Path, config: &Config) -> eyre::Result<(BeaconState, Signed /// The report parameters a manifest already carries, so the loop above never /// has to reconstruct them from samples. -fn params_from(manifest: &Manifest, dir: &Path, block_delay_ms: u64) -> Params { +fn params_from( + manifest: &Manifest, + dir: &Path, + block_delay_ms: u64, + warm_committee_sums: bool, +) -> Params { Params { mode: "import", corpus: dir.display().to_string(), @@ -252,6 +323,7 @@ fn params_from(manifest: &Manifest, dir: &Path, block_delay_ms: u64) -> Params { range_end: manifest.range_end, blocks: manifest.slots.len(), block_delay_ms, + warm_committee_sums, } } diff --git a/bin/ethlambda/src/benchmark/report/import.rs b/bin/ethlambda/src/benchmark/report/import.rs index 74bb20e8e..2a744e869 100644 --- a/bin/ethlambda/src/benchmark/report/import.rs +++ b/bin/ethlambda/src/benchmark/report/import.rs @@ -24,6 +24,9 @@ pub(crate) struct Params { /// Milliseconds slept after each block's import, outside every measured /// span. 0 means blocks were fed back to back. pub block_delay_ms: u64, + /// Whether each epoch's committee key sums were warmed before its first + /// block, outside every measured span (`--warm-committee-sums`). + pub warm_committee_sums: bool, } #[derive(Debug, Serialize)] @@ -107,7 +110,7 @@ impl Report { let _ = writeln!(out, "Block-import benchmark — {} workload", params.mode); let _ = writeln!( out, - " corpus={} network={} anchor_slot={} warmup_blocks={} range=[{}, {}] blocks={} block_delay_ms={}", + " corpus={} network={} anchor_slot={} warmup_blocks={} range=[{}, {}] blocks={} block_delay_ms={} warm_committee_sums={}", params.corpus, params.network, params.anchor_slot, @@ -115,7 +118,8 @@ impl Report { params.range_start, params.range_end, params.blocks, - params.block_delay_ms + params.block_delay_ms, + params.warm_committee_sums ); let _ = writeln!( out, @@ -217,6 +221,7 @@ mod tests { range_end: 3, blocks: 2, block_delay_ms: 0, + warm_committee_sums: false, } } @@ -231,6 +236,17 @@ mod tests { assert!(report.human_table().contains("block_delay_ms=750")); } + #[test] + fn warm_committee_sums_is_recorded_in_the_json_and_the_header() { + let mut params = params(); + params.warm_committee_sums = true; + let report = Report::new(Environment::collect(), params, Vec::new()); + + let json: serde_json::Value = serde_json::from_str(&report.to_json().unwrap()).unwrap(); + assert_eq!(json["params"]["warm_committee_sums"], true); + assert!(report.human_table().contains("warm_committee_sums=true")); + } + #[test] fn the_range_prints_inclusive_as_fetch_takes_it() { let report = Report::new(Environment::collect(), params(), Vec::new()); diff --git a/crates/blockchain/src/epoch_precompute.rs b/crates/blockchain/src/epoch_precompute.rs index 9c77e137a..31b396636 100644 --- a/crates/blockchain/src/epoch_precompute.rs +++ b/crates/blockchain/src/epoch_precompute.rs @@ -22,9 +22,11 @@ //! inline; the late result is stored and simply goes unused by that block. use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use ethlambda_state_transition::beacon::fork_choice; +use ethlambda_state_transition::beacon::helpers::accessors::CommitteeCacheExt as _; use ethlambda_state_transition::beacon::helpers::misc::compute_start_slot_at_epoch; use ethlambda_state_transition::metrics as stf_metrics; use ethlambda_storage::{CacheKey, Chain}; @@ -182,13 +184,10 @@ impl BlockChainServer { Trigger::Head => head_slot / preset::SLOTS_PER_EPOCH + 1, Trigger::Timer { slot } => slot / preset::SLOTS_PER_EPOCH + 1, }; - let already_cached = self - .store - .cached_state(CacheKey::CheckpointState { - epoch: candidate_epoch, - root: head_root, - }) - .is_some(); + let cached = self.store.cached_state(CacheKey::CheckpointState { + epoch: candidate_epoch, + root: head_root, + }); let inputs = DecisionInputs { trigger, syncing: self.sync_status.is_syncing(), @@ -196,9 +195,34 @@ impl BlockChainServer { head_root, current_slot: self.wall_clock_slot(), in_flight: self.epoch_precompute_in_flight, - already_cached, + already_cached: cached.is_some(), }; let Some(key) = decide(&inputs) else { + // Another path already advanced the state to the epoch's start + // (validating a gloas bid for the epoch's first slot does), so no + // worker runs, and the worker is what warms the key sums. Asked + // again as if nothing were cached, `decide` says whether that cache + // hit is the only reason no worker runs; if so, warm the sums from + // the cached state, or the epoch would import without them. + let only_the_cache_stopped_the_worker = decide(&DecisionInputs { + already_cached: false, + ..inputs + }) + .is_some(); + // One warm-only task at a time. A second would not build the sums + // again (they sit behind a `OnceLock`), but it would hold a + // blocking thread waiting on the first. + if let Some(state) = cached + && only_the_cache_stopped_the_worker + && !self.key_sums_warming.swap(true, Ordering::AcqRel) + { + let committee_cache = self.store.committee_cache(); + let warming = ClearOnDrop(Arc::clone(&self.key_sums_warming)); + tokio::task::spawn_blocking(move || { + let _warming = warming; + warm_epoch_key_sums(&committee_cache, &state, candidate_epoch) + }); + } return; }; // The head's post-state is resident (it was just imported); a read that @@ -220,17 +244,48 @@ impl BlockChainServer { let config = self.store.config(); let actor = ctx.actor_ref(); + let committee_cache = self.store.committee_cache(); tokio::task::spawn_blocking(move || { let result = { let _timing = stf_metrics::time_epoch_precompute(); fork_choice::advance_to_epoch_start(&head_state, key.epoch, &config) }; let result = result.map(Arc::new).map_err(|err| err.to_string()); + let advanced = result.as_ref().ok().map(Arc::clone); + // The state goes to the actor first: the epoch's first block must + // not wait for key sums, which only make its attestations cheaper. let _ = actor.send(EpochPrecomputed { key, result }); + if let Some(state) = advanced { + warm_epoch_key_sums(&committee_cache, &state, key.epoch); + } }); } } +/// Warms the key sums `epoch`'s blocks read: `epoch`'s own, and the previous +/// epoch's for the attestations those blocks carry from it. The previous +/// epoch's are normally warm already, from the last boundary; after a restart +/// mid-epoch, or a boundary whose worker did not run, they are not. Both calls +/// return at once when the sums are resident. +fn warm_epoch_key_sums( + committee_cache: ðlambda_storage::CommitteeCache, + state: &BeaconState, + epoch: u64, +) { + committee_cache.warm_key_sums(state, epoch); + committee_cache.warm_key_sums(state, epoch.saturating_sub(1)); +} + +/// Clears the flag it holds when dropped, so a warm-only task releases +/// `key_sums_warming` however it ends, a panic included. +struct ClearOnDrop(Arc); + +impl Drop for ClearOnDrop { + fn drop(&mut self) { + self.0.store(false, Ordering::Release); + } +} + impl Handler for BlockChainServer { async fn handle(&mut self, msg: EpochPrecomputeCheck, ctx: &Context) { self.maybe_start_epoch_precompute(Trigger::Timer { slot: msg.slot }, ctx); diff --git a/crates/blockchain/src/lib.rs b/crates/blockchain/src/lib.rs index 77edf2a0e..a9e0702c7 100644 --- a/crates/blockchain/src/lib.rs +++ b/crates/blockchain/src/lib.rs @@ -430,6 +430,7 @@ impl BlockChain { safe_slots_to_import_optimistically, last_tick_instant: None, epoch_precompute_in_flight: None, + key_sums_warming: Default::default(), sync_status, sync_status_controller, events, @@ -560,6 +561,11 @@ pub struct BlockChainServer { /// most one runs at a time; see [`epoch_precompute`]. epoch_precompute_in_flight: Option, + /// Whether a warm-only key-sum task is running: one started because the + /// epoch's state was already cached, so no precompute worker ran to warm + /// the sums. At most one runs at a time; see [`epoch_precompute`]. + key_sums_warming: std::sync::Arc, + /// Stateful sync heuristic used by `lean_node_sync_status`. Also gates /// validator duties while syncing, unless that gating was disabled at /// startup via `--disable-duty-sync-gate` (then it is metric-only). On a @@ -2153,6 +2159,7 @@ impl BlockChainServer { safe_slots_to_import_optimistically, last_tick_instant: None, epoch_precompute_in_flight: None, + key_sums_warming: Default::default(), sync_status: SyncStatusTracker::new(false), sync_status_controller: SyncStatusController::default(), events: ChainEvents::new(EventBus::default(), &store), @@ -4625,6 +4632,7 @@ mod tests { safe_slots_to_import_optimistically: constants::SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY, last_tick_instant: None, epoch_precompute_in_flight: None, + key_sums_warming: Default::default(), sync_status: SyncStatusTracker::new(false), sync_status_controller: SyncStatusController::default(), duties: ChainDuties::Beacon, 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/accessors.rs b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs index e3ebce7d3..f2d4fe7d3 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs @@ -7,6 +7,8 @@ use std::sync::Arc; +use rayon::prelude::*; + // Re-exported at this old path (`crate::beacon::helpers::accessors::CommitteeCache`, // and so on), so every existing import of the type this module used to define // is unaffected by its move to `ethlambda-storage`; see [`CommitteeCacheExt`] @@ -15,6 +17,7 @@ pub use ethlambda_storage::{ ActiveBalanceCache, ActiveBalanceKey, CommitteeCache, Lookup, PubkeyIndexCache, ShufflingKey, }; +use crate::beacon::bls::{AggregatePubkey, CommitteeKeySums}; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::BeaconState; @@ -278,6 +281,66 @@ pub trait CommitteeCacheExt { /// the previous head pinned stays resident until an insertion evicts it /// on epoch like any other entry. fn update_head(&self, head_root: Root, head_state: &BeaconState); + + /// `epoch`'s committee key sums as `state` names them, if they have been + /// warmed. + /// + /// Lookup only, and it never builds: building costs a pass over every + /// active validator's key, which is more than the sums save for + /// committees attested once, so block import must never be the one to pay + /// it. `None` (cold, evicted, or an epoch `state` cannot key) means the + /// caller sums its attesters directly. Warmed by [`Self::warm_key_sums`]. + /// + /// # Why the sums can be shared on the shuffling's key + /// + /// The sums are filed under the same [`ShufflingKey`] as the committees, + /// so two states with one key must agree on every member's *public key* as + /// well as on who the members are. They do, for three reasons, each + /// checked against this crate's spec code: + /// + /// - A validator's index and public key are fixed when it is appended to + /// the registry and never change after: the registry only grows + /// (`process_deposit`, and electra's pending-deposit path through + /// `add_validator_to_registry`, push; nothing removes or reorders), and + /// no code assigns `validator.pubkey` after construction. + /// - A new validator is appended with `activation_epoch` at + /// `FAR_FUTURE_EPOCH`. The only writer that gives it a real one is + /// `process_registry_updates`, running in epoch `N`'s epoch processing, + /// and it writes `compute_activation_exit_epoch(N)`, which is + /// `N + 1 + MAX_SEED_LOOKAHEAD`. (Genesis validators are active from + /// `GENESIS_EPOCH` and are in every state's history.) + /// - A validator in `epoch`'s committees is active there, so its + /// `activation_epoch <= epoch`, so it was activated by epoch processing + /// at some `N <= epoch - 1 - MAX_SEED_LOOKAHEAD`, and appended no later + /// than that. That is before the end of `epoch - 2`, so the block the + /// [`ShufflingKey`] names as the decision root already has it in its + /// registry, at that index, with that key. Every state descending from + /// that block inherits it. + /// + /// So the member list and each member's key are functions of the key, + /// and so is their sum. This is a stronger claim than the committees' + /// own (which need only the activation and exit epochs to be fixed), but + /// it follows from the same lookahead and from the registry being + /// append-only. + fn key_sums(&self, state: &BeaconState, epoch: Epoch) -> Option>; + + /// Builds `epoch`'s committee key sums as `state` names them, unless they + /// are already there. Meant for a background task: it derives the + /// committees if they are not resident, then spends one pass over the + /// registry and one key sum per committee, parallel across committees. + /// + /// A committee with a member whose key fails validation gets no sum + /// (`None` in its slot), so lookups of it fall back to the direct path + /// rather than to a sum that omits the member. Does nothing for an epoch + /// `state` cannot key, since there is nowhere to file the result, nor + /// before electra, whose attestations never read the sums. Idempotent, so + /// a caller unsure whether an epoch is warm can just ask again. + /// + /// Deriving the committees, when they are not resident yet, is part of a + /// warm-up's cost, but `lean_beacon_committee_key_sums_warm_seconds` + /// times the sums alone: the committees are the shuffling every + /// attestation of the epoch needs anyway, warm or not. + fn warm_key_sums(&self, state: &BeaconState, epoch: Epoch); } impl CommitteeCacheExt for CommitteeCache { @@ -303,6 +366,76 @@ impl CommitteeCacheExt for CommitteeCache { let keys = epochs.map(|epoch| shuffling_key(head_state, epoch)); self.pin_head(head_root, keys); } + + fn key_sums(&self, state: &BeaconState, epoch: Epoch) -> Option> { + self.resident_key_sums(shuffling_key(state, epoch)?) + } + + fn warm_key_sums(&self, state: &BeaconState, epoch: Epoch) { + if state.fork_name() < ForkName::Electra { + return; + } + let Some(key) = shuffling_key(state, epoch) else { + return; + }; + if self.resident_key_sums(key).is_some() { + return; + } + // Resident before the sums are filed into it; a no-op when the import + // path already built them. + let committees = self.committees(state, epoch); + self.get_or_init_key_sums(key, || { + // The sums alone: not the committees above, nor a lookup that + // found the work already done. + let _timing = crate::metrics::time_key_sums_warm(); + build_committee_key_sums(state, &committees, epoch) + }); + } +} + +/// Every committee of `committees`' epoch summed, indexed by committee number. +/// +/// One `state.iter_validators()` pass copies each validator's key bytes into a +/// flat list (a per-index `state.validator(i)` is a tree descent, and the +/// whole active set is read), then committees are summed in parallel. An entry +/// is `None` for a committee with a member whose key fails validation or whose +/// index is outside the registry, and for an empty committee. +/// +/// The flat key list is transient, about 48 MB on a mainnet-sized registry. +/// The parallel sums run on rayon's global pool, which block import's key +/// validation also uses (blst's pairings and leanVM's prover have pools of +/// their own). A warm-up is short, about 70 ms per epoch measured on a mainnet +/// node, and runs once or twice an epoch, so it delays import's key work only +/// briefly when the two coincide. +fn build_committee_key_sums( + state: &BeaconState, + committees: &EpochCommittees, + epoch: Epoch, +) -> CommitteeKeySums { + let pubkeys: Vec = state.iter_validators().map(|v| v.pubkey).collect(); + let per_slot = committees.committees_per_slot(); + let first_slot = compute_start_slot_at_epoch(epoch); + + let members: Vec<&[ValidatorIndex]> = (0..preset::SLOTS_PER_EPOCH) + .flat_map(|offset| (0..per_slot).map(move |index| (first_slot + offset, index))) + .map(|(slot, index)| { + committees + .committee(slot, index) + .expect("slot is in the epoch and index is below the committee count") + }) + .collect(); + + let sums = members + .par_iter() + .map(|committee| { + let keys: Option> = committee + .iter() + .map(|index| pubkeys.get(*index as usize).copied()) + .collect(); + AggregatePubkey::sum(&keys?) + }) + .collect(); + CommitteeKeySums::new(sums) } /// The key `state`'s total active balance is cached under, or `None` if 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..afb5f00d5 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -123,10 +123,10 @@ //! single easiest mistake to make copying that shape without also copying //! the reasoning behind it. -use crate::beacon::bls; +use crate::beacon::bls::{AggregatePubkey, SignatureSet}; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; -use crate::beacon::containers::shared::Validator; +use crate::beacon::containers::shared::{AttestationData, Validator}; use crate::beacon::containers::{BeaconState, electra, fulu, gloas}; use crate::beacon::error::{Error, Result}; use crate::beacon::fork::ForkName; @@ -134,14 +134,14 @@ 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, HashTreeRoot as _, + 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, + ActiveBalanceCache, CommitteeCache, CommitteeCacheExt, committee_number, + get_active_validator_indices, get_current_epoch, get_domain, 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}; @@ -272,47 +272,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, + ) } // --------------------------------------------------------------------------- @@ -520,6 +507,166 @@ pub(crate) fn attesting_indices_from_committee_bits( Ok(indices) } +/// The aggregate key of the attesters an electra-shaped attestation names: +/// the sum of their public keys, which is what `bls.FastAggregateVerify` +/// checks the signature against. `Ok(None)` when a key fails validation, a +/// named attester is outside the registry, or no attester is set. +/// +/// Walks the committees the way [`attesting_indices_from_committee_bits`] +/// does, `is_set` being the same closure over the aggregation bits (gloas +/// passes its progressive bitlist through it, so one builder serves both). +/// Per committee, when the shuffling's key sums are warm +/// ([`CommitteeCacheExt::key_sums`], lookup only: import never builds them) +/// and more than half of the committee attested, the committee's part is its +/// sum minus the keys of the non-attesters; otherwise its attesters' keys are +/// summed directly. A committee whose sum is `None` (a member's key fails +/// validation) is summed directly, so only the attesters' own keys decide it, +/// exactly as with cold sums. Subtracting is the same group element as summing +/// the attesters, so the pairing check cannot tell the two apart; only the +/// point additions differ, absentees' instead of attesters'. +/// +/// Every directly summed attester, across all committees, goes into one sum +/// rather than one per committee. With cold sums that is the single sum of +/// every attester the plain path makes, at its cost: blst splits a large sum +/// over its pool, and one sum per committee would pay that pool's dispatch +/// once per committee for a few hundred keys each. +/// +/// Phase0 through deneb attestations stay on the direct path: each names one +/// committee, and the live chain is past electra. +pub(crate) fn attesters_aggregate_pubkey( + state: &BeaconState, + slot: Slot, + committee_bits: &electra::CommitteeBits, + is_set: impl Fn(usize) -> bool, + committees: &CommitteeCache, +) -> Result> { + let epoch = compute_epoch_at_slot(slot); + let epoch_committees = committees.committees(state, epoch); + let key_sums = committees.key_sums(state, epoch); + let committees_per_slot = epoch_committees.committees_per_slot(); + + let pubkey_of = |index: ValidatorIndex| state.validator(index).ok().map(|v| v.pubkey); + + // Committees derived from their sums, added as they come, and the keys of + // every committee summed directly, summed once at the end. + let mut total: Option = None; + let mut direct = Vec::new(); + let mut committee_offset = 0usize; + for committee_index in get_committee_indices(committee_bits) { + let committee = epoch_committees.committee(slot, committee_index)?; + let first_bit = committee_offset; + committee_offset += committee.len(); + + let attested: Vec = (0..committee.len()) + .map(|position| is_set(first_bit + position)) + .collect(); + let attester_count = attested.iter().filter(|set| **set).count(); + if attester_count == 0 { + continue; + } + + let number = committee_number(slot, committees_per_slot, committee_index)?; + let sum = key_sums.as_ref().and_then(|sums| sums.get(number as usize)); + // A strict majority attested (`attester_count * 2 > len`, written + // without the multiplication): subtracting the absent minority from + // the sum then takes fewer additions than summing the attesters. + match sum { + Some(sum) if attester_count > committee.len() / 2 => { + let absent: Option> = committee + .iter() + .zip(&attested) + .filter(|(_, set)| !**set) + .map(|(index, _)| pubkey_of(*index)) + .collect(); + let Some(part) = absent.and_then(|absent| sum.minus(&absent)) else { + return Ok(None); + }; + total = Some(match total { + Some(total) => total.plus(&part), + None => part, + }); + } + _ => { + for (index, _) in committee.iter().zip(&attested).filter(|(_, set)| **set) { + let Some(pubkey) = pubkey_of(*index) else { + return Ok(None); + }; + direct.push(pubkey); + } + } + } + } + if !direct.is_empty() { + let Some(part) = AggregatePubkey::sum(&direct) else { + return Ok(None); + }; + total = Some(match total { + Some(total) => total.plus(&part), + None => part, + }); + } + Ok(total) +} + +/// The signature check for an electra-shaped attestation, with the key taken +/// from the shuffling's committee key sums when they are warm: the counterpart +/// of [`indexed_attestation_signature_set`] for a caller that still holds the +/// bit-encoded attestation next to the indexed one it built. +/// +/// Makes the attester-list checks [`super::attestation::attesters_signature_set`] +/// makes that the construction below does not already guarantee (non-empty, +/// sorted and unique) and signs over the same root under the same domain +/// (`DOMAIN_BEACON_ATTESTER` at the target epoch). The key comes from +/// [`attesters_aggregate_pubkey`], the same group element as the sum of +/// `attesting_indices`' keys: the indices are read off the same bits, and a +/// committee member is drawn from the state's own active set, so every index is +/// in the registry by construction. +/// +/// `None` means the attestation is invalid without needing a pairing, as for +/// [`indexed_attestation_signature_set`]. A bound on the number of indices +/// (gloas's `MAX_VALIDATORS_PER_SLOT`) is the caller's to check first. +pub(crate) fn attestation_signature_set( + state: &BeaconState, + data: &AttestationData, + signature: &BlsSignature, + attesting_indices: &[ValidatorIndex], + committee_bits: &electra::CommitteeBits, + is_set: impl Fn(usize) -> bool, + committees: &CommitteeCache, +) -> Option { + if attesting_indices.is_empty() || !are_indices_sorted_and_unique(attesting_indices) { + return None; + } + let key = + attesters_aggregate_pubkey(state, data.slot, committee_bits, is_set, committees).ok()??; + let domain = get_domain( + state, + constants::DOMAIN_BEACON_ATTESTER, + Some(data.target.epoch), + ); + let signing_root = compute_signing_root(data.hash_tree_root(), domain); + SignatureSet::from_aggregate(key, signing_root, signature) +} + +/// [`attestation_signature_set`] for an electra or fulu attestation and the +/// indexed attestation built from it. +pub fn electra_attestation_signature_set( + state: &BeaconState, + attestation: &electra::Attestation, + indexed_attestation: &electra::IndexedAttestation, + committees: &CommitteeCache, +) -> Option { + attestation_signature_set( + state, + &indexed_attestation.data, + &indexed_attestation.signature, + &indexed_attestation.attesting_indices, + &attestation.committee_bits, + |bit| attestation.aggregation_bits.get(bit).unwrap_or(false), + committees, + ) +} + /// The committee members whose bit is set in `attestation`, in ascending /// order. See [`attesting_indices_from_committee_bits`] for the walk itself; /// this is a thin wrapper over it, closing over `attestation`'s own @@ -2030,4 +2177,176 @@ mod tests { ); assert_ne!(electra_limit, gloas_limit); } + + // -- attesters_aggregate_pubkey ------------------------------------------ + + use crate::beacon::helpers::accessors::get_current_epoch; + use crate::beacon::helpers::misc::compute_start_slot_at_epoch; + use crate::beacon::helpers::test_state::{sign_for, with_signing_validators_at}; + use crate::beacon::primitives::Root; + + /// Enough validators for two committees per slot, so one attestation can + /// name two committees. These tests run under both presets (`make + /// test-beacon-minimal` runs `--lib` with `preset-minimal`), so slots and + /// member positions are derived from the preset rather than hardcoded: + /// minimal's epoch is 8 slots and its committees here hold 4 members. + fn two_committees_per_slot_state() -> BeaconState { + let count = (preset::SLOTS_PER_EPOCH * preset::TARGET_COMMITTEE_SIZE * 2) as usize; + with_signing_validators_at(ForkName::Electra, count) + } + + /// A reproducible xorshift stream, so a failing mask can be replayed. + fn next(seed: &mut u64) -> u64 { + *seed ^= *seed << 13; + *seed ^= *seed >> 7; + *seed ^= *seed << 17; + *seed + } + + fn bits_naming(indices: &[usize]) -> electra::CommitteeBits { + let mut bits = electra::CommitteeBits::default(); + for index in indices { + bits.set(*index, true).unwrap(); + } + bits + } + + /// Whether `key` is exactly the sum of the validators `attesters` names, by + /// the only check available from outside the crypto crate: it verifies the + /// aggregate of those validators' signatures over one message, which a + /// different group element would not. + fn is_sum_of(key: &AggregatePubkey, attesters: &[ValidatorIndex], message: Root) -> bool { + let signatures: Vec<_> = attesters + .iter() + .map(|index| sign_for(*index as usize, message)) + .collect(); + let aggregate = crate::beacon::bls::aggregate(&signatures).unwrap(); + key.verify(message, &aggregate) + } + + /// For random committee sizes and masks of every shape the majority rule + /// distinguishes, the builder's key is the attesters' sum, with the sums + /// cold and with them warm. + #[test] + fn the_builder_key_is_the_attesters_sum_with_cold_and_warm_sums() { + let state = two_committees_per_slot_state(); + let epoch = get_current_epoch(&state); + let message = Root::repeat_byte(0x42); + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + + let cold = CommitteeCache::default(); + let warm = CommitteeCache::default(); + warm.warm_key_sums(&state, epoch); + assert!(warm.key_sums(&state, epoch).is_some()); + assert!(cold.key_sums(&state, epoch).is_none()); + + for slot_offset in [0u64, 7, preset::SLOTS_PER_EPOCH - 1] { + let slot = compute_start_slot_at_epoch(epoch) + slot_offset; + let committee_sets: [&[usize]; 3] = [&[0], &[1], &[0, 1]]; + for named in committee_sets { + let bits = bits_naming(named); + let epoch_committees = warm.committees(&state, epoch); + let members: Vec = named + .iter() + .flat_map(|c| { + epoch_committees + .committee(slot, *c as u64) + .unwrap() + .to_vec() + }) + .collect(); + let len = members.len(); + + let mut masks: Vec> = vec![ + vec![false; len], + vec![true; len], + (0..len).map(|p| p == len / 3).collect(), + (0..len).map(|p| p < len / 2).collect(), + (0..len).map(|p| p <= len / 2).collect(), + ]; + for _ in 0..4 { + masks.push( + (0..len) + .map(|_| !next(&mut seed).is_multiple_of(5)) + .collect(), + ); + masks.push( + (0..len) + .map(|_| next(&mut seed).is_multiple_of(2)) + .collect(), + ); + } + + for mask in masks { + let attesters: Vec = members + .iter() + .zip(&mask) + .filter(|(_, set)| **set) + .map(|(index, _)| *index) + .collect(); + for cache in [&cold, &warm] { + let key = attesters_aggregate_pubkey( + &state, + slot, + &bits, + |bit| mask.get(bit).copied().unwrap_or(false), + cache, + ) + .unwrap(); + match key { + None => assert!(attesters.is_empty(), "slot {slot} {named:?}"), + Some(key) => assert!( + is_sum_of(&key, &attesters, message), + "slot {slot} committees {named:?} with {} of {len} set", + attesters.len() + ), + } + } + } + } + } + } + + /// A committee with a member whose key fails validation gets no sum, its + /// neighbours keep theirs, and the builder falls back to the attesters: + /// valid when the bad member did not attest, refused when it did. + #[test] + fn a_committee_with_an_invalid_member_key_falls_back() { + let mut state = two_committees_per_slot_state(); + let epoch = get_current_epoch(&state); + let slot = compute_start_slot_at_epoch(epoch) + 3; + let message = Root::repeat_byte(0x17); + + let probe = CommitteeCache::default(); + let members = probe + .committees(&state, epoch) + .committee(slot, 0) + .unwrap() + .to_vec(); + let bad = members[members.len() / 2]; + state.validator_mut(bad).unwrap().pubkey = Default::default(); + + let committees = CommitteeCache::default(); + committees.warm_key_sums(&state, epoch); + let sums = committees.key_sums(&state, epoch).expect("warmed"); + let per_slot = committees.committees(&state, epoch).committees_per_slot(); + let number = |index| committee_number(slot, per_slot, index).unwrap() as usize; + assert!( + sums.get(number(0)).is_none(), + "the bad committee has no sum" + ); + assert!(sums.get(number(1)).is_some(), "its neighbour keeps one"); + + let bits = bits_naming(&[0]); + let without_bad: Vec = members.iter().map(|m| *m != bad).collect(); + let key = attesters_aggregate_pubkey(&state, slot, &bits, |b| without_bad[b], &committees) + .unwrap() + .expect("the bad member did not attest"); + let attesters: Vec = + members.iter().copied().filter(|m| *m != bad).collect(); + assert!(is_sum_of(&key, &attesters, message)); + + let everyone = attesters_aggregate_pubkey(&state, slot, &bits, |_| true, &committees); + assert!(everyone.unwrap().is_none(), "the bad member attested"); + } } diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index bb00e05bf..776c047d4 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,54 @@ 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; - } +) -> Option { + super::attestation::attesters_signature_set( + state, + &indexed_attestation.attesting_indices, + &indexed_attestation.data, + &indexed_attestation.signature, + 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, - } +/// [`super::electra::electra_attestation_signature_set`] for a gloas +/// attestation: the same checks, plus the length bound the container no longer +/// enforces (`MAX_VALIDATORS_PER_SLOT`), checked first as +/// [`super::attestation::attesters_signature_set`] does. +pub fn gloas_attestation_signature_set( + state: &BeaconState, + attestation: &gloas::Attestation, + indexed_attestation: &gloas::IndexedAttestation, + committees: &CommitteeCache, +) -> Option { + if indexed_attestation.attesting_indices.len() > preset::MAX_VALIDATORS_PER_SLOT { + 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) + super::electra::attestation_signature_set( + state, + &indexed_attestation.data, + &indexed_attestation.signature, + &indexed_attestation.attesting_indices, + &attestation.committee_bits, + |bit| attestation.aggregation_bits.get(bit).unwrap_or(false), + committees, + ) } // --------------------------------------------------------------------------- @@ -284,16 +289,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 +322,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..5cd3f76b1 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::{ @@ -61,12 +61,12 @@ use crate::beacon::helpers::accessors::{ use crate::beacon::helpers::altair::{add_flag, base_reward_per_increment, has_flag}; use crate::beacon::helpers::capella::withdrawal_address; use crate::beacon::helpers::electra::{ - compute_exit_epoch_and_update_churn, get_committee_indices, + compute_exit_epoch_and_update_churn, electra_attestation_signature_set, get_committee_indices, 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( + electra_attestation_signature_set(state, attestation, &indexed_attestation, committees), "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(()) } @@ -2534,4 +2556,100 @@ mod tests { assert!(list[0].len() > 1); assert!(list[1].len() > 1); } + + // -- process_attestation with warmed committee key sums ------------------- + + const ATTESTATION_LABEL: &str = + "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))"; + + /// An attestation for slot `SLOTS_PER_EPOCH - 1` of a state one epoch in, + /// signed by every member of its committee but the first two, so that the + /// majority rule subtracts when the sums are warm. `signed_message` is the + /// root the committee actually signs; a root other than the attestation's + /// own makes the signature bad. + fn attestation_for_the_last_slot_of_epoch_zero( + state: &BeaconState, + signed_message: Option, + ) -> electra::Attestation { + use crate::beacon::helpers::accessors::{get_beacon_committee, get_domain}; + use crate::beacon::helpers::misc::compute_signing_root; + use crate::beacon::helpers::test_state::sign_for; + + let slot = preset::SLOTS_PER_EPOCH - 1; + let committee = get_beacon_committee(state, slot, 0).unwrap(); + let data = AttestationData { + slot, + ..Default::default() + }; + let domain = get_domain(state, constants::DOMAIN_BEACON_ATTESTER, Some(0)); + let root = + signed_message.unwrap_or_else(|| compute_signing_root(data.hash_tree_root(), domain)); + + let mut aggregation_bits = electra::AggregationBits::with_length(committee.len()).unwrap(); + let mut signatures = Vec::new(); + for (position, member) in committee.iter().enumerate().skip(2) { + aggregation_bits.set(position, true).unwrap(); + signatures.push(sign_for(*member as usize, root)); + } + let mut committee_bits = electra::CommitteeBits::default(); + committee_bits.set(0, true).unwrap(); + electra::Attestation { + aggregation_bits, + data, + signature: bls::aggregate(&signatures).unwrap(), + committee_bits, + } + } + + fn run_attestation( + mut state: BeaconState, + attestation: &electra::Attestation, + committees: &CommitteeCache, + ) -> Result { + let mut signatures = SignatureBatch::new(); + // Looked up here as a block's `process_operations` does once for all + // its attestations. + let proposer_index = crate::beacon::helpers::accessors::get_beacon_proposer_index(&state)?; + process_attestation( + &mut state, + attestation, + proposer_index, + committees, + &ActiveBalanceCache::default(), + &mut signatures, + )?; + signatures.verify()?; + state.apply_pending_mutations(); + Ok(state) + } + + /// A good attestation reaches the same post-state whether the key sums are + /// cold or warm, and a bad signature is refused under the attestation's + /// own label either way, so the warm path rejects what the cold one does. + #[test] + fn warm_key_sums_change_no_verdict_and_no_post_state() { + let state = + crate::beacon::helpers::test_state::with_signing_validators_at(ForkName::Electra, 1024); + let epoch = get_previous_epoch(&state); + let warm = CommitteeCache::default(); + warm.warm_key_sums(&state, epoch); + assert!(warm.key_sums(&state, epoch).is_some(), "sums are warm"); + let cold = CommitteeCache::default(); + + let good = attestation_for_the_last_slot_of_epoch_zero(&state, None); + let cold_state = run_attestation(state.clone(), &good, &cold).unwrap(); + let warm_state = run_attestation(state.clone(), &good, &warm).unwrap(); + assert_eq!(cold_state.hash_tree_root(), warm_state.hash_tree_root()); + assert_ne!(cold_state.hash_tree_root(), state.hash_tree_root()); + + let wrong_root = crate::beacon::primitives::Root::repeat_byte(0x5a); + let bad = attestation_for_the_last_slot_of_epoch_zero(&state, Some(wrong_root)); + for committees in [&cold, &warm] { + let err = run_attestation(state.clone(), &bad, committees).unwrap_err(); + assert!( + format!("{err:?}").contains(ATTESTATION_LABEL), + "unexpected rejection {err:?}" + ); + } + } } 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..06b587292 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}; @@ -63,10 +63,11 @@ use crate::beacon::helpers::electra::{g2_point_at_infinity, get_committee_indice 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, - 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, + get_indexed_attestation, get_indexed_payload_attestation, gloas_attestation_signature_set, + 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, + 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 +77,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 +106,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 +130,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 +146,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 +784,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 +806,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 +818,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 +867,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 +1254,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 +1322,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 +1331,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 +1380,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 +1441,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( + gloas_attestation_signature_set(state, attestation, &indexed_attestation, committees), "process_attestation: is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -1570,6 +1619,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 +1640,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 +1667,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { verify( body.deposits.is_empty(), @@ -1651,14 +1702,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 +1736,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 +1763,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 +1788,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 +1817,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 +2034,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..8ccc1ecb3 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", @@ -167,6 +184,21 @@ pub fn time_epoch_precompute() -> TimingGuard { TimingGuard::new(&LEAN_BEACON_EPOCH_PRECOMPUTE_SECONDS) } +static LEAN_BEACON_COMMITTEE_KEY_SUMS_WARM_SECONDS: LazyLock = LazyLock::new(|| { + register_histogram!( + "lean_beacon_committee_key_sums_warm_seconds", + "Duration of warming one epoch's committee key sums, off the import path", + vec![0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0] + ) + .unwrap() +}); + +/// Start timing one committee key-sum warm-up. Records duration when the +/// guard is dropped. +pub fn time_key_sums_warm() -> TimingGuard { + TimingGuard::new(&LEAN_BEACON_COMMITTEE_KEY_SUMS_WARM_SECONDS) +} + static LEAN_BEACON_EPOCH_PRECOMPUTE_STARTED_TOTAL: LazyLock = LazyLock::new(|| { register_int_counter_vec!( "lean_beacon_epoch_precompute_started_total", diff --git a/crates/blockchain/state_transition/tests/beacon_spec/mod.rs b/crates/blockchain/state_transition/tests/beacon_spec/mod.rs index f84ae0d5b..878e028a3 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/mod.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/mod.rs @@ -595,3 +595,25 @@ pub fn check_transition( (false, Err(_)) => Ok(()), } } + +/// Warms `committees`' key sums for the epochs a block applied to `state` can +/// reach: its previous, current and next epochs, the same three the node pins +/// for its head. A case that crosses an epoch boundary asks the next epoch's +/// shuffling after `process_slots`, whose key `state` already names. +/// +/// The fixture runners run an electra-or-later attestation twice, once with a +/// fresh cache and once after this, because a fresh cache only ever exercises +/// the cold branch of the attestation key builder. Both runs must reach the +/// same verdict and the same post-state. +pub fn warm_key_sums_around( + committees: ðlambda_state_transition::beacon::helpers::accessors::CommitteeCache, + state: &BeaconState, +) { + use ethlambda_state_transition::beacon::helpers::accessors::{ + CommitteeCacheExt as _, get_current_epoch, get_previous_epoch, + }; + let current = get_current_epoch(state); + for epoch in [get_previous_epoch(state), current, current + 1] { + committees.warm_key_sums(state, epoch); + } +} diff --git a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs index 726932e58..6e822f0b9 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}; @@ -174,11 +175,41 @@ fn apply( case: &Case, state: &mut BeaconState, config: &Config, + committees: &CommitteeCache, ) -> 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. - let committees = CommitteeCache::default(); + // 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, committees, 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, + committees: &CommitteeCache, + signatures: &mut SignatureBatch, +) -> Result, String> { + // `committees` is only read by the `attestation` handler. The caller owns + // it so that an electra-or-later attestation can be run once with a cold + // cache and once with a warmed one; see [`trials`]. let active_balances = ActiveBalanceCache::default(); let pubkey_indices = PubkeyIndexCache::default(); let outcome = match handler { @@ -288,7 +319,8 @@ fn apply( &attestation, proposer, config, - &committees, + committees, + signatures, ) }) } @@ -302,8 +334,9 @@ fn apply( state, &attestation, proposer, - &committees, + committees, &active_balances, + signatures, ) }) } @@ -316,8 +349,9 @@ fn apply( state, &attestation, proposer, - &committees, + committees, &active_balances, + signatures, ) }) } @@ -331,8 +365,9 @@ fn apply( state, &attestation, proposer, - &committees, + committees, &active_balances, + signatures, ) }) } @@ -349,8 +384,9 @@ fn apply( state, &attestation, meta.parent_slot, - &committees, + committees, &active_balances, + signatures, ) } ForkName::Lean => lean_is_not_a_fixture_fork("attestation"), @@ -363,7 +399,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 +413,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 +456,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 +511,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 +528,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 +547,7 @@ fn apply( &sync_aggregate, &active_balances, &pubkey_indices, + signatures, ) } // The fixture's operation file is a whole `BeaconBlockBody`, not a @@ -568,7 +634,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 +674,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 +763,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 { @@ -715,7 +781,26 @@ pub fn trials() -> Vec { .map_err(|err| format!("the fixture's pre-state does not decode: {err:?}"))?; let config = super::case_config(case); - let outcome = apply(&handler, case, &mut state, &config); + // An electra-or-later attestation picks its aggregate key from the + // shuffling's committee key sums when they are warm, and a fresh + // cache is never warm, so the case runs a second time on a copy + // of the pre-state with the sums built. Both runs must agree. + let warm_run = (handler == "attestation" && case.fork >= ForkName::Electra) + .then(|| state.clone()); + + let outcome = apply(&handler, case, &mut state, &config, &CommitteeCache::default()); + if let Some(mut warm_state) = warm_run { + let committees = CommitteeCache::default(); + super::warm_key_sums_around(&committees, &warm_state); + let warm = apply(&handler, case, &mut warm_state, &config, &committees); + if warm != outcome { + return Err(format!( + "the warm-sums run answered {warm:?} where the cold run answered {outcome:?}" + )); + } + super::check_transition(case, warm, &warm_state) + .map_err(|err| format!("warm sums: {err}"))?; + } super::check_transition(case, outcome, &state) })); } diff --git a/crates/blockchain/state_transition/tests/beacon_spec/sanity.rs b/crates/blockchain/state_transition/tests/beacon_spec/sanity.rs index c94ffd03d..eaf2728a6 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/sanity.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/sanity.rs @@ -13,6 +13,7 @@ //! Unlike the `operations` suite, these run the full [`state_transition`], so //! they check the proposer signature and the committed `state_root` too. +use ethlambda_state_transition::beacon::ForkName; use ethlambda_state_transition::beacon::config::Config; use ethlambda_state_transition::beacon::containers::{BeaconState, SignedBeaconBlock}; use ethlambda_state_transition::beacon::stf::{self, ExecutionEngine, OwnedBlockCaches}; @@ -26,7 +27,17 @@ struct BlocksMeta { } /// Applies every block in the case, stopping at the first one rejected. -fn apply_blocks(case: &Case, state: &mut BeaconState, config: &Config) -> Result<(), String> { +/// +/// With `warm`, the shuffling's committee key sums are built before each block, +/// from the state it applies to, the way the node's epoch precompute worker +/// builds them ahead of the epoch's first block. A fresh cache would otherwise +/// only ever run the attestation key builder's cold branch. +fn apply_blocks( + case: &Case, + state: &mut BeaconState, + config: &Config, + warm: bool, +) -> Result<(), String> { let meta: BlocksMeta = case.yaml("meta"); // `sanity/blocks`, `finality`, and `random` ship no `execution.yaml` @@ -57,6 +68,10 @@ fn apply_blocks(case: &Case, state: &mut BeaconState, config: &Config) -> Result state.latest_block_header_mut().state_root = root; } + if warm { + super::warm_key_sums_around(&caches.committees, state); + } + stf::state_transition(state, &block, true, config, &engine, caches.caches()) .map_err(|err| format!("block {index} rejected: {err:?}"))?; @@ -86,11 +101,29 @@ fn run(runner: &str, handler: &str, slots: bool) -> Vec { .map_err(|err| format!("the fixture's pre-state does not decode: {err:?}"))?; let config = super::case_config(case); - let outcome = if slots { - apply_slots(case, &mut state, &config) - } else { - apply_blocks(case, &mut state, &config) - }; + if slots { + let outcome = apply_slots(case, &mut state, &config); + return super::check_transition(case, outcome, &state); + } + + // Electra and later pick an attestation's aggregate key from + // warmed committee key sums when there are any, so those cases run + // a second time with the sums built. Both runs must reach the same + // verdict and the same post-state. + if case.fork >= ForkName::Electra { + let mut warm_state = state.clone(); + let warm = apply_blocks(case, &mut warm_state, &config, true); + let cold = apply_blocks(case, &mut state, &config, false); + if warm != cold { + return Err(format!( + "the warm-sums run answered {warm:?} where the cold run answered {cold:?}" + )); + } + super::check_transition(case, warm, &warm_state) + .map_err(|err| format!("warm sums: {err}"))?; + return super::check_transition(case, cold, &state); + } + let outcome = apply_blocks(case, &mut state, &config, false); super::check_transition(case, outcome, &state) })); } 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..90a35ecf3 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -96,7 +96,7 @@ use std::collections::HashMap; use std::sync::{LazyLock, RwLock}; -use blst::min_pk::{AggregateSignature, PublicKey, Signature}; +use blst::min_pk::{AggregatePublicKey, AggregateSignature, PublicKey, Signature}; use blst::{BLST_ERROR, MultiPoint}; use rayon::prelude::*; @@ -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. @@ -261,7 +264,7 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { points.into_iter().collect() } -/// The sum of `points` as one public key, or `None` for an empty slice. +/// The sum of `points` as a projective point, or `None` for an empty slice. /// /// Every point must already have passed [`PublicKey::key_validate`] (they come /// from [`validated_pubkeys`]), so the sum skips re-checking them. It goes @@ -272,6 +275,11 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// does. A committee's key sum is the largest cost left in verifying an /// attestation once the keys are cached, so this is where the saving is. /// +/// The result stays projective, which is the form `add_aggregate` and +/// `sub_aggregate` take: [`AggregatePubkey`] adds and subtracts such sums and +/// would pay a field inversion per conversion to affine and back. Callers that +/// need an affine key convert once, as [`sum_validated_points`] does. +/// /// The batched routine handles equal points (`P + P` is a doubling, and sync /// committees are drawn with replacement, so one sum can name the same key /// twice) and returns the same group element as the sequential sum; only the @@ -283,14 +291,22 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// is the same either way, so the caller needs no size check. /// /// The empty slice is guarded here because `MultiPoint::add` indexes the -/// slice's first element and would panic on it. A single point is its own -/// sum, so an aggregate with one signer skips the batched routine and its +/// slice's first element and would panic on it. +fn sum_validated_projective(points: &[PublicKey]) -> Option { + if points.is_empty() { + return None; + } + Some(MultiPoint::add(points)) +} + +/// The sum of `points` as one affine public key, or `None` for an empty +/// slice: [`sum_validated_projective`] converted once. A single point is its +/// own sum, so an aggregate with one signer skips the batched routine and its /// conversion back to affine coordinates. fn sum_validated_points(points: &[PublicKey]) -> Option { match points { - [] => None, [single] => Some(*single), - _ => Some(MultiPoint::add(points).to_public_key()), + _ => sum_validated_projective(points).map(|sum| sum.to_public_key()), } } @@ -323,15 +339,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 +409,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 +452,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`. @@ -478,6 +474,9 @@ pub fn key_validate(pubkey: &BlsPubkey) -> bool { validated_pubkey(pubkey).is_some() } +mod committee_sums; +pub use committee_sums::{AggregatePubkey, CommitteeKeySums}; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs new file mode 100644 index 000000000..c77f47711 --- /dev/null +++ b/crates/common/crypto/src/bls/batch.rs @@ -0,0 +1,580 @@ +//! 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::{AggregatePubkey, 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, + }) + } + + /// `bls.FastAggregateVerify`'s inputs with the key already summed, as + /// committee key sums produce it. + /// + /// `None` when `signature` fails `sig_validate`. The key needs no check: a + /// sum of validated points is in the subgroup, and the identity, the one + /// sum that is not a valid key, is refused by [`Self::verify`] and by the + /// batch at pairing time, as it is for a key summed here from scratch. + pub fn from_aggregate( + key: AggregatePubkey, + message: Root, + signature: &BlsSignature, + ) -> Option { + let signature = Signature::sig_validate(signature.as_ref(), false).ok()?; + Some(Self { + pubkey: key.to_public_key(), + 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/common/crypto/src/bls/committee_sums.rs b/crates/common/crypto/src/bls/committee_sums.rs new file mode 100644 index 000000000..a59000a66 --- /dev/null +++ b/crates/common/crypto/src/bls/committee_sums.rs @@ -0,0 +1,304 @@ +//! Committee key sums: a committee's aggregate public key, kept as a curve +//! point so an attestation's key can be derived from it instead of re-summed. +//! +//! An attestation aggregate usually covers most of its committee, and its +//! verification key is the sum of its attesters' keys. Summing every attester +//! costs one point addition per signer; summing the *non*-attesters and +//! subtracting from the committee's own sum costs one per absentee, which on a +//! healthy network is a small fraction. The two are the same group element, so +//! the pairing check is unchanged and only the number of additions moves. +//! +//! This module holds the crypto half: [`AggregatePubkey`], a sum that can be +//! added to, subtracted from and verified against without ever being +//! serialized, and [`CommitteeKeySums`], one shuffling's committee sums +//! indexed by committee number. Which committees there are, who is in them and +//! when a sum is still valid belongs to `ethlambda-storage`'s `CommitteeCache` +//! and `ethlambda-state-transition`'s accessors; this module knows only keys. +//! +//! Every key reaches a sum through `validated_pubkeys`, so the guarantees of +//! the module documentation in [`super`] hold here too: a key that fails +//! `key_validate` is never summed, and a failure is reported as `None` rather +//! than as a sum that silently omits the key. + +use blst::min_pk::{AggregatePublicKey, PublicKey}; + +use ethlambda_types::beacon::primitives::{BlsPubkey, BlsSignature, Root}; + +use super::{SignatureSet, sum_validated_projective, validated_pubkeys}; + +/// A sum of validated public keys, kept as a curve point. Never serialized. +/// +/// Not a [`BlsPubkey`] on purpose: compressing a point costs a field inversion, +/// and the only consumer is a pairing check in this process, so the point stays +/// as it is. The sum may be the identity (every key subtracted away), which is +/// representable here and refused by [`Self::verify`], exactly as `blst` +/// refuses an infinite key at pairing time. +#[derive(Clone, Copy)] +pub struct AggregatePubkey(AggregatePublicKey); + +impl AggregatePubkey { + /// The sum of `pubkeys`, each validated through the pubkey cache. `None` if + /// `pubkeys` is empty or any key fails validation. + pub fn sum(pubkeys: &[BlsPubkey]) -> Option { + let points = validated_pubkeys(pubkeys)?; + sum_validated_projective(&points).map(Self) + } + + /// `self - sum(pubkeys)`, with each key validated through the pubkey + /// cache. `None` if any key fails validation; an empty `pubkeys` is `self`. + /// + /// Negation goes through `blst`'s own `sub_aggregate`, so no `unsafe` is + /// needed here. Subtracting every key `self` was built from yields the + /// identity, not an error: see [`Self::verify`]. + pub fn minus(&self, pubkeys: &[BlsPubkey]) -> Option { + if pubkeys.is_empty() { + return Some(*self); + } + let points = validated_pubkeys(pubkeys)?; + let removed = sum_validated_projective(&points)?; + let mut result = self.0; + result.sub_aggregate(&removed); + Some(Self(result)) + } + + /// `self + other`. + pub fn plus(&self, other: &Self) -> Self { + let mut result = self.0; + result.add_aggregate(&other.0); + Self(result) + } + + /// `bls.FastAggregateVerify` with this already-summed key: false for an + /// invalid signature, and false for the identity key, as the path that sums + /// the keys itself is. + /// + /// The signature is validated here for the same reason + /// [`super::fast_aggregate_verify`] validates it: nothing upstream + /// guarantees a subgroup-correct point. The key needs no check, since a sum + /// of validated points is in the subgroup; the identity is the one value + /// that is not a valid key, and `blst` rejects it at pairing time. + pub fn verify(&self, message: Root, signature: &BlsSignature) -> bool { + SignatureSet::from_aggregate(*self, message, signature).is_some_and(|set| set.verify()) + } + + /// The sum as an affine key, the form a [`SignatureSet`] holds. One field + /// inversion, which is why the sums stay projective until they are checked. + pub(super) fn to_public_key(self) -> PublicKey { + self.0.to_public_key() + } + + /// The sum compressed, for comparing two sums in tests. + #[cfg(test)] + fn to_bytes(self) -> [u8; 48] { + self.0.to_public_key().to_bytes() + } +} + +impl std::fmt::Debug for AggregatePubkey { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("AggregatePubkey(..)") + } +} + +/// One shuffling's committee key sums, by committee number within the epoch +/// (`slot_offset * committees_per_slot + committee_index`, the position +/// `committee_number` computes). An entry is `None` when some member's key +/// fails validation, so that committee falls back to summing its attesters. +pub struct CommitteeKeySums { + sums: Vec>, +} + +impl CommitteeKeySums { + /// Wraps `sums`, one entry per committee of the epoch in committee-number order. + pub fn new(sums: Vec>) -> Self { + Self { sums } + } + + /// The sum of committee `committee_number`'s members' keys, or `None` when + /// the number is out of range or that committee has no usable sum. + pub fn get(&self, committee_number: usize) -> Option<&AggregatePubkey> { + self.sums.get(committee_number)?.as_ref() + } +} + +impl std::fmt::Debug for CommitteeKeySums { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CommitteeKeySums") + .field("committees", &self.sums.len()) + .finish() + } +} + +#[cfg(test)] +mod tests { + use super::super::DST; + use super::*; + use blst::min_pk::SecretKey; + + fn secret(seed: u64) -> SecretKey { + let mut ikm = [0x3cu8; 32]; + ikm[..8].copy_from_slice(&seed.to_le_bytes()); + SecretKey::key_gen(&ikm, &[]).expect("32 bytes of input material") + } + + fn pubkeys(count: usize) -> Vec { + (0..count) + .map(|i| BlsPubkey(secret(i as u64 + 1000).sk_to_pk().to_bytes())) + .collect() + } + + /// The sum straight through `blst`, no cache and no helper of this module. + fn direct(keys: &[BlsPubkey]) -> [u8; 48] { + let points: Vec = keys + .iter() + .map(|k| PublicKey::key_validate(k.as_ref()).unwrap()) + .collect(); + let refs: Vec<&PublicKey> = points.iter().collect(); + AggregatePublicKey::aggregate(&refs, false) + .unwrap() + .to_public_key() + .to_bytes() + } + + #[test] + fn sum_matches_the_direct_sum_including_duplicates() { + let keys = pubkeys(8); + let mut with_duplicates = keys.clone(); + with_duplicates.push(keys[0]); + with_duplicates.push(keys[0]); + for set in [&keys[..1], &keys[..2], &keys[..], &with_duplicates[..]] { + assert_eq!(AggregatePubkey::sum(set).unwrap().to_bytes(), direct(set)); + } + let twice = [keys[0], keys[0]]; + assert_eq!( + AggregatePubkey::sum(&twice).unwrap().to_bytes(), + direct(&twice) + ); + } + + #[test] + fn sum_of_nothing_or_of_an_invalid_key_is_none() { + assert!(AggregatePubkey::sum(&[]).is_none()); + let mut keys = pubkeys(3); + keys.push(BlsPubkey::default()); + assert!(AggregatePubkey::sum(&keys).is_none()); + } + + /// `committee - absentees` is the attesters' sum for every mask shape. + #[test] + fn minus_and_plus_agree_with_the_direct_sum() { + let keys = pubkeys(16); + let total = AggregatePubkey::sum(&keys).unwrap(); + for absent in [0usize, 1, 8, 9, 15] { + let attesters = &keys[absent..]; + let absentees = &keys[..absent]; + let derived = total.minus(absentees).unwrap(); + assert_eq!(derived.to_bytes(), direct(attesters), "absent {absent}"); + } + let left = AggregatePubkey::sum(&keys[..5]).unwrap(); + let right = AggregatePubkey::sum(&keys[5..]).unwrap(); + assert_eq!(left.plus(&right).to_bytes(), total.to_bytes()); + assert_eq!(total.minus(&[]).unwrap().to_bytes(), total.to_bytes()); + } + + /// The same over random masks of a committee-sized run, with the attesters + /// scattered rather than a prefix: `sum - absentees` and the direct sum of + /// the attesters are the same bytes. + #[test] + fn minus_agrees_with_the_direct_sum_over_random_masks() { + let keys = pubkeys(64); + let total = AggregatePubkey::sum(&keys).unwrap(); + // xorshift64, fixed seed: reproducible without a seeded-RNG dependency. + let mut state = 0x9e37_79b9_7f4a_7c15u64; + for round in 0..40 { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + let (attesters, absentees): (Vec<_>, Vec<_>) = keys + .iter() + .enumerate() + .partition(|(position, _)| state >> position & 1 == 1); + let attesters: Vec = attesters.into_iter().map(|(_, key)| *key).collect(); + let absentees: Vec = absentees.into_iter().map(|(_, key)| *key).collect(); + if attesters.is_empty() { + continue; + } + let derived = total.minus(&absentees).unwrap(); + assert_eq!(derived.to_bytes(), direct(&attesters), "round {round}"); + } + } + + #[test] + fn minus_with_an_invalid_key_is_none() { + let keys = pubkeys(4); + let total = AggregatePubkey::sum(&keys).unwrap(); + assert!(total.minus(&[BlsPubkey::default()]).is_none()); + } + + fn signed(count: usize, seed: u64, message: Root) -> (Vec, Vec) { + let secrets: Vec = (0..count as u64).map(|i| secret(i + seed)).collect(); + let keys = secrets + .iter() + .map(|s| BlsPubkey(s.sk_to_pk().to_bytes())) + .collect(); + let sigs = secrets + .iter() + .map(|s| BlsSignature(s.sign(message.as_slice(), DST, &[]).to_bytes())) + .collect(); + (keys, sigs) + } + + #[test] + fn verify_agrees_with_fast_aggregate_verify_on_real_signatures() { + let message = Root::repeat_byte(9); + let (keys, sigs) = signed(6, 50, message); + let good = crate::bls::aggregate(&sigs).unwrap(); + let partial = crate::bls::aggregate(&sigs[1..]).unwrap(); + + let total = AggregatePubkey::sum(&keys).unwrap(); + assert!(total.verify(message, &good)); + assert!(crate::bls::fast_aggregate_verify(&keys, message, &good)); + + // The same key from `total - absentee`, against the matching signature. + let derived = total.minus(&keys[..1]).unwrap(); + assert!(derived.verify(message, &partial)); + assert!(crate::bls::fast_aggregate_verify( + &keys[1..], + message, + &partial + )); + + // A wrong signature, a wrong message and a missing signature fail on both paths. + assert!(!derived.verify(message, &good)); + assert!(!crate::bls::fast_aggregate_verify( + &keys[1..], + message, + &good + )); + assert!(!total.verify(Root::repeat_byte(10), &good)); + assert!(!total.verify(message, &BlsSignature::default())); + } + + #[test] + fn the_identity_aggregate_never_verifies() { + let message = Root::repeat_byte(1); + let (keys, sigs) = signed(3, 90, message); + let signature = crate::bls::aggregate(&sigs).unwrap(); + let identity = AggregatePubkey::sum(&keys).unwrap().minus(&keys).unwrap(); + assert!(!identity.verify(message, &signature)); + } + + #[test] + fn committee_key_sums_index_by_committee_number() { + let keys = pubkeys(2); + let sums = CommitteeKeySums::new(vec![ + AggregatePubkey::sum(&keys[..1]), + None, + AggregatePubkey::sum(&keys), + ]); + assert!(sums.get(0).is_some()); + assert!(sums.get(1).is_none()); + assert!(sums.get(2).is_some()); + assert!(sums.get(3).is_none()); + } +} 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/crates/storage/src/committee_cache.rs b/crates/storage/src/committee_cache.rs index 35c079d9c..a2361b9c8 100644 --- a/crates/storage/src/committee_cache.rs +++ b/crates/storage/src/committee_cache.rs @@ -13,6 +13,11 @@ //! runner, a one-off lookup, a unit test) holds a fresh //! [`CommitteeCache::default`] for as long as that work lasts instead. //! +//! Each resident shuffling also carries a second, independently filled slot +//! for its [`CommitteeKeySums`] (the sum of every committee's public keys), +//! built off the import path and only ever looked up on it; see +//! [`CommitteeCache::get_or_init_key_sums`]. +//! //! This module knows nothing about a `BeaconState`: it is handed a //! [`ShufflingKey`] and, on a miss, runs a builder closure that hands back //! the finished [`EpochCommittees`]. The consensus logic that derives both @@ -25,6 +30,7 @@ use std::sync::{Arc, Mutex, OnceLock}; +use ethlambda_crypto::bls::CommitteeKeySums; use ethlambda_types::beacon::committees::EpochCommittees; use ethlambda_types::beacon::primitives::{Epoch, Root}; @@ -107,16 +113,29 @@ pub enum Lookup { Miss, } -/// One resident shuffling's slot: empty until some call's builder fills it. +/// One resident shuffling's slot: its committees and their key sums, each +/// empty until some call's builder fills it. /// -/// An `Arc` around the `OnceLock` (rather than the `OnceLock` sitting -/// directly in the entry list) is what lets [`CommitteeCache::evict_one`] -/// drop the entry list's own reference to a slot while a waiter elsewhere -/// still holds a clone of the same `Arc`, taken before eviction ran: the -/// waiter's `get_or_init` call keeps running against a slot that is no -/// longer reachable from the cache at all, exactly as safely as if it had -/// never been evicted. -type Slot = Arc>>; +/// The key sums sit beside the committees, rather than in a second cache, so +/// that eviction and head pinning stay in one place and a shuffling's sums die +/// with it: nothing can hold sums for a shuffling the cache no longer holds. +/// The two fill independently. The committees are built on the import path as +/// soon as any block needs them; the sums are built off it by +/// [`CommitteeCache::get_or_init_key_sums`] and only ever read on it, since +/// building them costs a pass over the whole active set. +#[derive(Debug, Default)] +struct Resident { + committees: OnceLock>, + key_sums: OnceLock>, +} + +/// A [`Resident`] behind an `Arc`, which is what lets +/// [`CommitteeCache::evict_one`] drop the entry list's own reference to a slot +/// while a waiter elsewhere still holds a clone of the same `Arc`, taken +/// before eviction ran: the waiter's `get_or_init` call keeps running against +/// a slot that is no longer reachable from the cache at all, exactly as safely +/// as if it had never been evicted. +type Slot = Arc; /// The state one [`Mutex`] in [`CommitteeCache`] guards: the resident entries /// and the current head's pins. One lock over both, rather than two, because @@ -215,7 +234,7 @@ impl CommitteeCache { { Arc::clone(slot) } else { - let slot: Slot = Arc::new(OnceLock::new()); + let slot: Slot = Arc::new(Resident::default()); if state.entries.len() >= COMMITTEE_CACHE_CAPACITY { state.evict_one(); } @@ -230,12 +249,12 @@ impl CommitteeCache { // just inserted, or one a concurrent call inserted a moment ago and // has not finished building yet, is a `Miss` regardless of which of // the racing calls ends up actually running `build`. - let lookup = if slot.get().is_some() { + let lookup = if slot.committees.get().is_some() { Lookup::Hit } else { Lookup::Miss }; - let committees = Arc::clone(slot.get_or_init(|| Arc::new(build()))); + let committees = Arc::clone(slot.committees.get_or_init(|| Arc::new(build()))); (committees, lookup) } @@ -285,7 +304,55 @@ impl CommitteeCache { let (_, keys) = state.head.as_ref()?; let current = keys[HEAD_CURRENT_EPOCH]?; let (_, slot) = state.entries.iter().find(|(key, _)| *key == current)?; - slot.get().cloned() + slot.committees.get().cloned() + } + + /// `key`'s committee key sums, if that shuffling is resident and its sums + /// have been built. + /// + /// Never builds, and never inserts: block import must not pay for a pass + /// over the active set that costs more than it saves for committees + /// attested once. A cold lookup is an answer, and the caller falls back to + /// summing the attesters directly. Named apart from + /// `CommitteeCacheExt::key_sums` for the reason [`Self::pin_head`] gives. + pub fn resident_key_sums(&self, key: ShufflingKey) -> Option> { + let slot = self.resident_slot(key)?; + slot.key_sums.get().cloned() + } + + /// `key`'s committee key sums, running `build` if they are not there yet + /// and sharing the result with every call naming the same `key`. + /// + /// `None`, without running `build`, when the shuffling is not resident: + /// the sums belong to a resident entry and this never creates one, so a + /// caller warms the committees first (the entry's own `get_or_init`) and + /// the sums second. An entry evicted between the two simply loses the + /// warm-up, which costs a later import the direct sum and nothing else. + /// + /// The same concurrency rule as [`Self::get_or_init`]: the entry-list lock + /// is held only to find the slot, never while `build` runs, since `build` + /// is a pass over every active validator's key. Concurrent callers race + /// [`OnceLock::get_or_init`] on the slot: one runs `build`, the rest wait + /// on it. A slot evicted while `build` runs is built into regardless and + /// dropped with its last holder. + pub fn get_or_init_key_sums( + &self, + key: ShufflingKey, + build: impl FnOnce() -> CommitteeKeySums, + ) -> Option> { + let slot = self.resident_slot(key)?; + Some(Arc::clone(slot.key_sums.get_or_init(|| Arc::new(build())))) + } + + /// `key`'s slot, if resident, found under the entry-list lock and handed + /// back as an owned `Arc` so the lock is released before it is used. + fn resident_slot(&self, key: ShufflingKey) -> Option { + let state = self.state.lock().unwrap(); + state + .entries + .iter() + .find(|(entry_key, _)| *entry_key == key) + .map(|(_, slot)| Arc::clone(slot)) } } @@ -469,6 +536,59 @@ mod tests { } } + /// `resident_key_sums` is a lookup: it never creates an entry and never + /// builds sums, however often it is asked. + #[test] + fn key_sums_lookup_never_builds_or_inserts() { + let cache = CommitteeCache::default(); + let k = key(3, 3); + assert!(cache.resident_key_sums(k).is_none(), "not resident"); + assert!(cache.state.lock().unwrap().entries.is_empty()); + + cache.get_or_init(k, || dummy_committees(3)); + assert!(cache.resident_key_sums(k).is_none(), "resident but cold"); + } + + /// Sums are built into a resident shuffling only, once, and then served. + #[test] + fn key_sums_are_built_once_into_a_resident_shuffling() { + let cache = CommitteeCache::default(); + let k = key(3, 3); + let refused = cache.get_or_init_key_sums(k, || panic!("no entry to build into")); + assert!(refused.is_none()); + assert!(cache.state.lock().unwrap().entries.is_empty()); + + cache.get_or_init(k, || dummy_committees(3)); + let first = cache + .get_or_init_key_sums(k, || CommitteeKeySums::new(vec![None])) + .expect("resident"); + let second = cache + .get_or_init_key_sums(k, || panic!("should not rebuild")) + .expect("resident"); + assert!(Arc::ptr_eq(&first, &second)); + assert!(Arc::ptr_eq(&first, &cache.resident_key_sums(k).unwrap())); + } + + /// The sums leave with their shuffling: once the entry is evicted, a + /// lookup finds nothing, and a fresh entry for the key starts cold. + #[test] + fn key_sums_are_evicted_with_their_shuffling() { + let cache = CommitteeCache::default(); + let oldest = key(0, 0); + cache.get_or_init(oldest, || dummy_committees(0)); + cache.get_or_init_key_sums(oldest, || CommitteeKeySums::new(Vec::new())); + assert!(cache.resident_key_sums(oldest).is_some()); + + for n in 1..=COMMITTEE_CACHE_CAPACITY { + let k = key(n as Epoch, n as u8); + cache.get_or_init(k, move || dummy_committees(k.epoch)); + } + assert!(cache.resident_key_sums(oldest).is_none()); + + cache.get_or_init(oldest, || dummy_committees(0)); + assert!(cache.resident_key_sums(oldest).is_none(), "starts cold"); + } + /// Every clone of a `CommitteeCache` shares one underlying cache: a /// lookup through one clone is a hit through another. `CommitteeCache` /// does not itself derive `Clone` (its only owner, `Store`, holds it diff --git a/docs/benchmarking.md b/docs/benchmarking.md index 9a7c3fcb3..e4977d935 100644 --- a/docs/benchmarking.md +++ b/docs/benchmarking.md @@ -249,6 +249,19 @@ has no end-to-end figure to adjust. The value is recorded as `params.block_delay_ms` in the JSON report and printed in the human header, so compare only reports made with the same delay. +**`--warm-committee-sums`** (default off) warms each epoch's committee key +sums before the epoch's first block is imported. A live node does this on the +epoch precompute worker between slots; a replay drives `import_block` +directly with no tick loop, so that worker never runs and the sums stay cold, +which hides the committee-sum saving. This flag reproduces only the worker's +committee-sum warm-up (not its state advance, which the import still does +inside the measured span). It runs before a block whose epoch is later than the +previous block's, and for both the first block's epoch and the one before it +ahead of the first block. The warm-up runs outside every measured span, so it +is in no phase and no `wall_seconds`. The flag is recorded as +`params.warm_committee_sums` in the JSON report and printed in the human +header; compare only reports made with the same setting. + ### What is measured The measured span is one `BlockChainServer::import_block` call per block, the diff --git a/docs/metrics.md b/docs/metrics.md index e1a19b128..29bffe506 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 @@ -587,6 +588,7 @@ leanMetrics spec. |------|------|-------|-------------------------|--------|---------| | `lean_beacon_epoch_precompute_lookups_total` | Counter | Epoch-crossing block transitions, by whether a precomputed boundary state was cached | On each state transition of a block whose parent is before the block's epoch start and whose block is at or after it. Usually once per import; twice when the import runs the transition again (a speculation computed during the `engine_newPayload` round trip and then discarded for an `INVALIDATED` verdict, or re-checked in a debug build), and once for a block held after its speculation ran | result=hit,miss | | | `lean_beacon_epoch_precompute_seconds` | Histogram | Worker time to advance, flush and hash one state | When a precompute worker finishes | | 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 8 | +| `lean_beacon_committee_key_sums_warm_seconds` | Histogram | Worker time to sum one epoch's committee keys, off the import path: the sums alone, not the committees they are built from | When a warm-up that built sums finishes; a warm-up that finds them resident records nothing | | 0.05, 0.1, 0.25, 0.5, 1, 2, 4, 8, 16 | | `lean_beacon_epoch_precompute_started_total` | Counter | Workers started, by trigger | When a worker is spawned | trigger=head,timer | | **Read the hit ratio against epochs.** On a synced follower nearly every