From 54be2b29e78676b58b09605de5518e205cfb2cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:21:22 -0300 Subject: [PATCH 01/47] perf(crypto): sum validated public keys through blst's batched affine addition A committee's key sum is the largest cost left in verifying an attestation once its keys are cached. Folding the keys one by one through AggregatePublicKey::aggregate pays a projective addition per key; blst_p1s_add (reached through the safe MultiPoint::add) shares one field inversion across the batch and runs 1.6-2.3x faster in a single-threaded measurement. From 384 points up blst also spreads the work over its own thread pool. fast_aggregate_verify and eth_aggregate_pubkeys now sum through one private helper, sum_validated_points, which guards the empty slice (MultiPoint::add indexes the first element). The helper returns the same group element as the sequential sum, including for repeated keys (sync committees are drawn with replacement, so P + P occurs), which the new tests check at sizes around the 384-point threshold. Accepted and rejected inputs are unchanged. --- crates/common/crypto/src/bls.rs | 146 ++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 8 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index fb9dd2005..edc304417 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -96,8 +96,8 @@ use std::collections::HashMap; use std::sync::{LazyLock, RwLock}; -use blst::BLST_ERROR; -use blst::min_pk::{AggregatePublicKey, AggregateSignature, PublicKey, Signature}; +use blst::min_pk::{AggregateSignature, PublicKey, Signature}; +use blst::{BLST_ERROR, MultiPoint}; use rayon::prelude::*; use ethlambda_types::beacon::error::{Error, Result}; @@ -261,6 +261,35 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { points.into_iter().collect() } +/// The sum of `points` as one public key, 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 +/// through blst's `MultiPoint::add`, which hands the whole slice to +/// `blst_p1s_add`: a batched affine addition that shares one field inversion +/// across many additions, instead of paying a projective addition per key the +/// way folding the keys one by one through blst's `AggregatePublicKey::aggregate` +/// 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 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 +/// order of additions differs, and the group is commutative. +/// +/// From 384 points up, blst splits the work over its own thread pool, which is +/// separate from rayon's; below that it runs on the calling thread. Either way +/// the answer is the same, 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. +fn sum_validated_points(points: &[PublicKey]) -> Option { + if points.is_empty() { + return None; + } + Some(points.add().to_public_key()) +} + /// Builds `specs/altair/bls.md`'s `G2_POINT_AT_INFINITY` constant: the /// compressed encoding of the identity element of G2, which is the /// specification's sentinel value for "no one signed anything". @@ -378,8 +407,13 @@ pub fn fast_aggregate_verify( let Some(points) = validated_pubkeys(pubkeys) else { return false; }; - let point_refs: Vec<&PublicKey> = points.iter().collect(); - let result = signature.fast_aggregate_verify(false, message.as_slice(), DST, &point_refs); + // `pubkeys` is non-empty, so there is always a sum. 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 } @@ -397,12 +431,10 @@ pub fn eth_aggregate_pubkeys(pubkeys: &[BlsPubkey]) -> Result { let points = validated_pubkeys(pubkeys).ok_or(Error::SpecAssert( "all(bls.KeyValidate(pubkey) for pubkey in pubkeys)", ))?; - let point_refs: Vec<&PublicKey> = points.iter().collect(); // Every point above already passed `key_validate`, so the sum skips // re-checking them. - let aggregated = AggregatePublicKey::aggregate(&point_refs, false) - .map_err(|_| Error::SpecAssert("len(pubkeys) > 0"))?; - Ok(BlsPubkey(aggregated.to_public_key().to_bytes())) + let aggregated = sum_validated_points(&points).ok_or(Error::SpecAssert("len(pubkeys) > 0"))?; + Ok(BlsPubkey(aggregated.to_bytes())) } /// `specs/altair/bls.md`'s @@ -442,6 +474,7 @@ pub fn key_validate(pubkey: &BlsPubkey) -> bool { #[cfg(test)] mod tests { use super::*; + use blst::min_pk::AggregatePublicKey; #[test] fn key_validate_rejects_the_all_zero_pubkey() { @@ -544,6 +577,103 @@ mod tests { } } + /// The sequential sum `sum_validated_points` must equal, through blst's + /// own `AggregatePublicKey::aggregate`. + fn sequential_sum(points: &[PublicKey]) -> [u8; BLS_PUBKEY_SIZE] { + let refs: Vec<&PublicKey> = points.iter().collect(); + AggregatePublicKey::aggregate(&refs, false) + .expect("a non-empty run of valid points sums") + .to_public_key() + .to_bytes() + } + + fn validated_run(seeds: impl Iterator) -> Vec { + let pubkeys: Vec = seeds.map(fresh_pubkey).collect(); + validated_pubkeys(&pubkeys).expect("every key above is valid") + } + + #[test] + fn the_batched_sum_of_nothing_is_none() { + assert!(sum_validated_points(&[]).is_none()); + } + + #[test] + fn the_batched_sum_matches_the_sequential_one_at_every_size() { + // One point, a pair, then both sides of blst's 384-point thread-pool + // threshold (and the first multi-chunk size), then a large run. + for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { + let points = validated_run(1000..1000 + count); + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} distinct points" + ); + } + } + + #[test] + fn the_batched_sum_doubles_equal_points() { + let one = validated_run(5000..5001)[0]; + let pair = [one, one]; + assert_eq!( + sum_validated_points(&pair).map(|sum| sum.to_bytes()), + Some(sequential_sum(&pair)), + "P + P is a doubling" + ); + + // Several repeats scattered through a run, the shape of a committee + // drawn with replacement, at sizes either side of the threshold. + for count in [10u64, 400, 1000] { + let distinct = validated_run(6000..6000 + count); + let mut points = Vec::new(); + for (index, point) in distinct.iter().enumerate() { + points.push(*point); + if index % 3 == 0 { + points.push(*point); + } + if index % 7 == 0 { + points.push(distinct[0]); + } + } + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} keys with repeats" + ); + } + } + + #[test] + fn fast_aggregate_verify_with_a_repeated_signer_still_verifies() { + // Two signatures from the same key over one message aggregate to twice + // its signature, which only verifies if the doubled key sum is right. + let message = ethlambda_types::beacon::primitives::H256([9u8; 32]); + let mut ikm = [0x33u8; 32]; + ikm[..8].copy_from_slice(&7u64.to_le_bytes()); + let secret = blst::min_pk::SecretKey::key_gen(&ikm, &[]).unwrap(); + let pubkey = BlsPubkey(secret.sk_to_pk().to_bytes()); + let one = BlsSignature(secret.sign(message.as_slice(), DST, &[]).to_bytes()); + let signature = aggregate(&[one, one]).unwrap(); + assert!(fast_aggregate_verify( + &[pubkey, pubkey], + message, + &signature + )); + assert!(!fast_aggregate_verify(&[pubkey], message, &signature)); + } + + #[test] + fn eth_aggregate_pubkeys_matches_the_sequential_sum_with_repeats() { + let mut pubkeys: Vec = (30..40).map(fresh_pubkey).collect(); + pubkeys.push(pubkeys[2]); + pubkeys.push(pubkeys[2]); + let points = validated_pubkeys(&pubkeys).unwrap(); + assert_eq!( + eth_aggregate_pubkeys(&pubkeys).unwrap().0, + sequential_sum(&points) + ); + } + #[test] fn the_cache_stops_growing_at_its_capacity() { // One entry per shard. From 5c787c6cc7490be0f43a92e30f1a9e469c64bd3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:54:04 -0300 Subject: [PATCH 02/47] perf(beacon): committee key sums, warmed off the import path An attestation aggregate usually covers most of its committee, so its verification key is cheaper to derive as the committee's key sum minus the non-attesters than as the sum of the attesters. The result is the same group element, so the pairing check is unchanged; only the number of point additions drops. Each resident shuffling in the committee cache gets a second slot for its committee key sums, so eviction and head pinning stay in one place. Block import only looks the sums up: building them costs a pass over the whole active set, which is more than they save for committees attested once. The epoch precompute worker builds them instead, after it has handed the advanced state to the actor, so the epoch's first block never waits. Sums are keyed by the shuffling key. That is sound because the registry is append-only, a pubkey is never reassigned, and a validator active at epoch E was activated at least MAX_SEED_LOOKAHEAD + 1 epochs earlier, so it is already in the history the decision root commits to. Nothing in block processing calls the new key builder yet. --- crates/blockchain/src/epoch_precompute.rs | 9 + .../src/beacon/helpers/accessors.rs | 113 ++++++- .../src/beacon/helpers/electra.rs | 262 +++++++++++++++- .../state_transition/src/metrics.rs | 15 + crates/common/crypto/src/bls.rs | 3 + .../common/crypto/src/bls/committee_sums.rs | 291 ++++++++++++++++++ crates/storage/src/committee_cache.rs | 146 ++++++++- docs/metrics.md | 1 + 8 files changed, 823 insertions(+), 17 deletions(-) create mode 100644 crates/common/crypto/src/bls/committee_sums.rs diff --git a/crates/blockchain/src/epoch_precompute.rs b/crates/blockchain/src/epoch_precompute.rs index 9c77e137a..78ac11e2e 100644 --- a/crates/blockchain/src/epoch_precompute.rs +++ b/crates/blockchain/src/epoch_precompute.rs @@ -25,6 +25,7 @@ use std::sync::Arc; 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}; @@ -220,13 +221,21 @@ 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 { + let _timing = stf_metrics::time_key_sums_warm(); + committee_cache.warm_key_sums(&state, key.epoch); + } }); } } diff --git a/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs index 60cdcc7a1..cfb5413ab 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, ShufflingKey, }; +use crate::beacon::bls::{AggregatePubkey, CommitteeKeySums}; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::BeaconState; @@ -24,7 +27,7 @@ use crate::beacon::hash::hash; use crate::beacon::lean_state_unreachable; use crate::beacon::preset; use crate::beacon::primitives::{ - Bytes32, CommitteeIndex, Domain, DomainType, Epoch, Gwei, Root, Slot, ValidatorIndex, + BlsPubkey, Bytes32, CommitteeIndex, Domain, DomainType, Epoch, Gwei, Root, Slot, ValidatorIndex, }; use super::misc::{ @@ -278,6 +281,59 @@ 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. + fn warm_key_sums(&self, state: &BeaconState, epoch: Epoch); } impl CommitteeCacheExt for CommitteeCache { @@ -303,6 +359,61 @@ 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) { + 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, || 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. +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/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index 94fa7d20b..668e21365 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -123,7 +123,7 @@ //! single easiest mistake to make copying that shape without also copying //! the reasoning behind it. -use crate::beacon::bls; +use crate::beacon::bls::{self, AggregatePubkey}; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::shared::Validator; @@ -139,8 +139,8 @@ use crate::beacon::primitives::{ }; use super::accessors::{ - CommitteeCache, CommitteeCacheExt, get_active_validator_indices, get_current_epoch, get_domain, - get_seed, get_total_active_balance, + CommitteeCache, CommitteeCacheExt, committee_number, get_active_validator_indices, + get_current_epoch, get_domain, get_seed, get_total_active_balance, }; use super::math::bytes_to_uint64; use super::misc::{compute_activation_exit_epoch, compute_epoch_at_slot, compute_signing_root}; @@ -513,6 +513,93 @@ 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 it is the sum of the +/// attesters. A committee whose sum is `None` (a member's key fails +/// validation) takes the second branch, so only the attesters' own keys decide +/// it, exactly as with cold sums. The parts are then added. +/// 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'. +/// +/// Phase0 through deneb attestations stay on the direct path: each names one +/// committee, and the live chain is past electra. +#[allow( + dead_code, + reason = "wired into process_attestation by the next change" +)] +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); + + let mut total: Option = None; + 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)); + let part = match sum { + Some(sum) if attester_count * 2 > committee.len() => { + let absent: Option> = committee + .iter() + .zip(&attested) + .filter(|(_, set)| !**set) + .map(|(index, _)| pubkey_of(*index)) + .collect(); + absent.and_then(|absent| sum.minus(&absent)) + } + _ => { + let present: Option> = committee + .iter() + .zip(&attested) + .filter(|(_, set)| **set) + .map(|(index, _)| pubkey_of(*index)) + .collect(); + present.and_then(|present| AggregatePubkey::sum(&present)) + } + }; + let Some(part) = part else { + return Ok(None); + }; + total = Some(match total { + Some(total) => total.plus(&part), + None => part, + }); + } + Ok(total) +} + /// 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 @@ -1990,4 +2077,173 @@ 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; mainnet's preset is the only one these run under. + 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 = 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, 31] { + 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[5]; + 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/metrics.rs b/crates/blockchain/state_transition/src/metrics.rs index 63c029166..1cd9eb560 100644 --- a/crates/blockchain/state_transition/src/metrics.rs +++ b/crates/blockchain/state_transition/src/metrics.rs @@ -145,6 +145,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] + ) + .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/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index fb9dd2005..1267c1e91 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -439,6 +439,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/committee_sums.rs b/crates/common/crypto/src/bls/committee_sums.rs new file mode 100644 index 000000000..f7d3e6b9c --- /dev/null +++ b/crates/common/crypto/src/bls/committee_sums.rs @@ -0,0 +1,291 @@ +//! 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::BLST_ERROR; +use blst::MultiPoint; +use blst::min_pk::{AggregatePublicKey, PublicKey, Signature}; + +use ethlambda_types::beacon::primitives::{BlsPubkey, BlsSignature, Root}; + +use super::{DST, validated_pubkeys}; + +/// The sum of `points`, or `None` for an empty slice. +/// +/// `blst`'s batched addition indexes its first element, so an empty input has +/// to be refused here rather than reaching it. Every point passed already +/// cleared `key_validate`, so nothing is re-checked. Equal points are fine: the +/// addition doubles where it must, which matters because committees drawn with +/// replacement (sync and payload timeliness committees) can hold one key twice. +fn sum_points(points: &[PublicKey]) -> Option { + if points.is_empty() { + return None; + } + Some(points.add()) +} + +/// 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_points(&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_points(&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 { + let Ok(signature) = Signature::sig_validate(signature.as_ref(), false) else { + return false; + }; + let key = self.0.to_public_key(); + signature.fast_aggregate_verify_pre_aggregated(false, message.as_slice(), DST, &key) + == BLST_ERROR::BLST_SUCCESS + } + + /// 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::*; + 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()); + } + + #[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/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/metrics.md b/docs/metrics.md index 1ea07953f..9178a702f 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -544,6 +544,7 @@ leanMetrics spec. |------|------|-------|-------------------------|--------|---------| | `lean_beacon_epoch_precompute_lookups_total` | Counter | Epoch-crossing imports, by whether a precomputed boundary state was cached | On each import whose parent is before the block's epoch start and whose block is at or after it | 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 every committee's keys for one epoch, after its precomputed state was handed to the actor | When a warm-up finishes | | 0.05, 0.1, 0.25, 0.5, 1, 2, 4, 8 | | `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 From bece6dddd925e3595f625ac0a5c07e4ba116ab14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:54:37 -0300 Subject: [PATCH 03/47] perf(beacon): verify a block's signatures in one random-weight batch Block import paid one pairing per signature: the proposer's, the RANDAO reveal, every attestation, the sync aggregate and each slashing, exit and BLS change. A pairing is dominated by its final exponentiation, which a batch pays once, so a block with a dozen signature sets checks in a fraction of the time. ethlambda-crypto gains SignatureSet and verify_signature_sets: resolved checks combined with unpredictable non-zero 64-bit weights from the OS-seeded CSPRNG (without them two invalid signatures can cancel). The state transition builds each set where the specification verifies it and pushes it into a SignatureBatch that the block driver verifies once processing ends. A failing batch re-checks sets one by one so the error still names the first bad signature with its usual label. Signatures whose failure rejects the block are deferred; deposit signatures, whose failure only skips the deposit, stay immediate, as does epoch processing. process_block keeps its meaning and signature, apply_block adds the proposer's signature to the same batch. The operation packer in block production uses an immediate batch so a rejected candidate still writes nothing. The attestation validity helpers are split into a set builder plus the existing bool function, so gossip and fork choice are untouched. --- Cargo.lock | 1 + .../src/beacon/block_production.rs | 9 +- .../src/beacon/gloas_block_production.rs | 8 +- .../src/beacon/helpers/attestation.rs | 37 +- .../src/beacon/helpers/electra.rs | 36 +- .../src/beacon/helpers/gloas.rs | 53 ++- .../state_transition/src/beacon/stf/altair.rs | 99 +++-- .../src/beacon/stf/bellatrix.rs | 13 +- .../state_transition/src/beacon/stf/block.rs | 145 ++++++- .../src/beacon/stf/capella.rs | 43 ++- .../state_transition/src/beacon/stf/deneb.rs | 40 +- .../src/beacon/stf/electra.rs | 60 ++- .../state_transition/src/beacon/stf/fulu.rs | 38 +- .../state_transition/src/beacon/stf/gloas.rs | 156 ++++++-- .../state_transition/src/beacon/stf/mod.rs | 32 +- .../src/beacon/stf/operations.rs | 62 ++- .../src/beacon/stf/signatures.rs | 205 ++++++++++ .../tests/beacon_spec/operations.rs | 85 ++++- crates/common/crypto/Cargo.toml | 1 + crates/common/crypto/src/bls.rs | 3 + crates/common/crypto/src/bls/batch.rs | 361 ++++++++++++++++++ .../rpc/src/beacon/validator_client_tests.rs | 7 +- 22 files changed, 1296 insertions(+), 198 deletions(-) create mode 100644 crates/blockchain/state_transition/src/beacon/stf/signatures.rs create mode 100644 crates/common/crypto/src/bls/batch.rs diff --git a/Cargo.lock b/Cargo.lock index fcda3ecb2..c83dbf4b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2011,6 +2011,7 @@ dependencies = [ "hex", "leanvm", "postcard", + "rand 0.10.1", "rayon", "thiserror 2.0.18", "tracing", diff --git a/crates/blockchain/state_transition/src/beacon/block_production.rs b/crates/blockchain/state_transition/src/beacon/block_production.rs index 9c3d0f8ce..cf8519a9b 100644 --- a/crates/blockchain/state_transition/src/beacon/block_production.rs +++ b/crates/blockchain/state_transition/src/beacon/block_production.rs @@ -47,6 +47,7 @@ use super::helpers::electra::{ 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, ExecutionEngine}; use ethlambda_storage::pools::single_committee; @@ -442,7 +443,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. @@ -450,7 +451,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/gloas_block_production.rs b/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs index b4621d542..eeb0ee7c2 100644 --- a/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs +++ b/crates/blockchain/state_transition/src/beacon/gloas_block_production.rs @@ -403,7 +403,7 @@ pub fn assemble_gloas_block_on_bid( body, }; let mut post = state.clone(); - stf::gloas::process_block( + stf::gloas::process_block_and_verify( &mut post, &block, config, @@ -529,7 +529,7 @@ pub fn assemble_gloas_block( let BeaconState::Gloas(_) = &post else { unreachable!("checked above") }; - stf::gloas::process_block( + stf::gloas::process_block_and_verify( &mut post, &block, config, @@ -745,7 +745,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(), @@ -1042,7 +1042,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/helpers/attestation.rs b/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs index b225195c9..d52da7017 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs @@ -9,6 +9,8 @@ //! 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::error::Result; @@ -16,7 +18,6 @@ use crate::beacon::helpers::accessors::{CommitteeCache, CommitteeCacheExt, get_d 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}; /// The committee members whose bit is set in `attestation`, in ascending order. /// @@ -79,12 +80,27 @@ pub fn is_valid_indexed_attestation( state: &BeaconState, indexed_attestation: &IndexedAttestation, ) -> bool { + 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. +/// +/// Every structural check [`is_valid_indexed_attestation`] makes (the +/// attester set's shape and range) happens here, at once: `None` means the +/// attestation is invalid without needing a pairing. Only the pairing is left +/// to the caller. +pub fn indexed_attestation_signature_set( + state: &BeaconState, + indexed_attestation: &IndexedAttestation, +) -> Option { 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_with_domain(state, indexed_attestation, domain) } /// [`is_valid_indexed_attestation`] under a signing domain the caller chose. @@ -97,19 +113,30 @@ pub fn is_valid_indexed_attestation_with_domain( indexed_attestation: &IndexedAttestation, domain: Domain, ) -> bool { + indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) + .is_some_and(|set| set.verify()) +} + +/// [`indexed_attestation_signature_set`] under a signing domain the caller +/// chose. +pub fn indexed_attestation_signature_set_with_domain( + state: &BeaconState, + indexed_attestation: &IndexedAttestation, + domain: Domain, +) -> Option { let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; if indices.is_empty() || !are_indices_sorted_and_unique(indices) { - 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, } } let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &indexed_attestation.signature) + SignatureSet::fast_aggregate(&pubkeys, signing_root, &indexed_attestation.signature) } diff --git a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index 94fa7d20b..6e6daf9c2 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -123,7 +123,7 @@ //! single easiest mistake to make copying that shape without also copying //! the reasoning behind it. -use crate::beacon::bls; +use crate::beacon::bls::SignatureSet; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::shared::Validator; @@ -280,12 +280,27 @@ pub fn is_valid_indexed_attestation( state: &BeaconState, indexed_attestation: &electra::IndexedAttestation, ) -> bool { + 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. +/// +/// Every structural check [`is_valid_indexed_attestation`] makes (the +/// attester set's shape and range) happens here, at once: `None` means the +/// attestation is invalid without needing a pairing. Only the pairing is left +/// to the caller. +pub fn indexed_attestation_signature_set( + state: &BeaconState, + indexed_attestation: &electra::IndexedAttestation, +) -> Option { 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_with_domain(state, indexed_attestation, domain) } /// [`is_valid_indexed_attestation`] under a signing domain the caller chose. @@ -297,21 +312,32 @@ pub fn is_valid_indexed_attestation_with_domain( indexed_attestation: &electra::IndexedAttestation, domain: Domain, ) -> bool { + indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) + .is_some_and(|set| set.verify()) +} + +/// [`indexed_attestation_signature_set`] under a signing domain the caller +/// chose. +pub fn indexed_attestation_signature_set_with_domain( + state: &BeaconState, + indexed_attestation: &electra::IndexedAttestation, + domain: Domain, +) -> Option { let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; if indices.is_empty() || !are_indices_sorted_and_unique(indices) { - 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, } } let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &indexed_attestation.signature) + SignatureSet::fast_aggregate(&pubkeys, signing_root, &indexed_attestation.signature) } // --------------------------------------------------------------------------- diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index 3bb3d0094..a95b66f06 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; @@ -124,12 +124,27 @@ pub fn is_valid_indexed_attestation( state: &BeaconState, indexed_attestation: &gloas::IndexedAttestation, ) -> bool { + 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. +/// +/// Every structural check [`is_valid_indexed_attestation`] makes (the +/// attester set's shape and range) happens here, at once: `None` means the +/// attestation is invalid without needing a pairing. Only the pairing is left +/// to the caller. +pub fn indexed_attestation_signature_set( + state: &BeaconState, + indexed_attestation: &gloas::IndexedAttestation, +) -> Option { 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_with_domain(state, indexed_attestation, domain) } /// [`is_valid_indexed_attestation`] under a signing domain the caller chose. @@ -141,24 +156,35 @@ pub fn is_valid_indexed_attestation_with_domain( indexed_attestation: &gloas::IndexedAttestation, domain: Domain, ) -> bool { + indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) + .is_some_and(|set| set.verify()) +} + +/// [`indexed_attestation_signature_set`] under a signing domain the caller +/// chose. +pub fn indexed_attestation_signature_set_with_domain( + state: &BeaconState, + indexed_attestation: &gloas::IndexedAttestation, + domain: Domain, +) -> Option { 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; + 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, } } let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - bls::fast_aggregate_verify(&pubkeys, signing_root, &indexed_attestation.signature) + SignatureSet::fast_aggregate(&pubkeys, signing_root, &indexed_attestation.signature) } // --------------------------------------------------------------------------- @@ -284,16 +310,27 @@ 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; 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 +340,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 2b33e79be..ba926d667 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::{self, 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; // --------------------------------------------------------------------------- @@ -84,6 +89,7 @@ pub fn process_attestation( attestation: &phase0::Attestation, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -137,8 +143,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))", )?; @@ -304,6 +310,7 @@ pub fn process_sync_aggregate( state: &mut BeaconState, sync_aggregate: &altair::SyncAggregate, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let committee_bits = &sync_aggregate.sync_committee_bits; @@ -327,14 +334,25 @@ 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`'s one carve-out: with no participants the + // signature must be the point at infinity, and there is nothing to + // pair, so that case is decided here and defers nothing. + let eth_fast_aggregate_verify = "eth_fast_aggregate_verify(participant_pubkeys, signing_root, sync_aggregate.sync_committee_signature)"; + if participant_pubkeys.is_empty() { + verify( + sync_aggregate.sync_committee_signature.as_ref() == bls::G2_POINT_AT_INFINITY, + eth_fast_aggregate_verify, + )?; + } else { + signatures.push( + SignatureSet::fast_aggregate( + &participant_pubkeys, + signing_root, + &sync_aggregate.sync_committee_signature, + ), + 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 @@ -593,12 +611,15 @@ mod tests { let proposer_index = get_beacon_proposer_index(&state).unwrap(); let balance_before = state.balance(proposer_index).unwrap(); - process_attestation( - &mut state, - &attestation, - &CommitteeCache::default(), - &ActiveBalanceCache::default(), - ) + checked(|s| { + process_attestation( + &mut state, + &attestation, + &CommitteeCache::default(), + &ActiveBalanceCache::default(), + s, + ) + }) .unwrap(); let (previous_epoch_participation, _, _) = state.altair_validator_lists().unwrap(); @@ -629,12 +650,15 @@ 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, - &CommitteeCache::default(), - &ActiveBalanceCache::default(), - ) + checked(|s| { + process_attestation( + &mut state, + &attestation, + &CommitteeCache::default(), + &ActiveBalanceCache::default(), + s, + ) + }) .unwrap(); let balance_after_replay = state.balance(proposer_index).unwrap(); assert_eq!( @@ -711,8 +735,15 @@ mod tests { .map(|index| state.balance(index).unwrap()) .collect(); - process_sync_aggregate(&mut state, &sync_aggregate, &ActiveBalanceCache::default()) - .unwrap(); + checked(|s| { + process_sync_aggregate( + &mut state, + &sync_aggregate, + &ActiveBalanceCache::default(), + s, + ) + }) + .unwrap(); // Independently derived expected rewards, sharing only the // already-tested building blocks (`get_total_active_balance`, @@ -777,8 +808,15 @@ mod tests { sync_committee_signature: BlsSignature::default(), }; assert!( - process_sync_aggregate(&mut state.clone(), &wrong, &ActiveBalanceCache::default()) - .is_err(), + checked(|s| { + process_sync_aggregate( + &mut state.clone(), + &wrong, + &ActiveBalanceCache::default(), + s, + ) + }) + .is_err(), "the all-zero signature is not the point at infinity, so this must be rejected" ); @@ -787,7 +825,10 @@ mod tests { sync_committee_signature: g2_point_at_infinity(), }; let balance_before = state.balance(0).unwrap(); - process_sync_aggregate(&mut state, &correct, &ActiveBalanceCache::default()).unwrap(); + checked(|s| { + process_sync_aggregate(&mut state, &correct, &ActiveBalanceCache::default(), s) + }) + .unwrap(); // The sole validator holds every seat and none of them participated, // so it is penalized once per seat and the proposer (itself, the only diff --git a/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs b/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs index e9287513e..e5fa269d4 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/bellatrix.rs @@ -40,6 +40,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::ExecutionEngine; @@ -63,6 +64,7 @@ use super::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, @@ -71,6 +73,7 @@ pub fn process_block( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -82,7 +85,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, @@ -94,8 +97,14 @@ pub fn process_block( config, committees, active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, )?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; Ok(()) } diff --git a/crates/blockchain/state_transition/src/beacon/stf/block.rs b/crates/blockchain/state_transition/src/beacon/stf/block.rs index dcaa32684..2ac017dcc 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}; @@ -31,16 +31,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::{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 it before returning +/// `Ok`, so a caller that has nothing to add to the batch (block production +/// re-running its own block) needs no knowledge of it. A caller with a +/// signature of its own to check alongside, such as the proposer's, calls +/// [`process_block_with_signatures`] and verifies the batch itself. +/// +/// An `Err` leaves `state` partly mutated, as it always could: an operation +/// whose signature is deferred has already written when a bad signature is +/// found at the end. Every caller discards the state after an error. pub fn process_block( state: &mut BeaconState, signed_block: &containers::SignedBeaconBlock, @@ -49,6 +62,38 @@ pub fn process_block( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: BlockProcessingOptions, +) -> Result<()> { + let mut signatures = SignatureBatch::new(); + process_block_with_signatures( + state, + signed_block, + config, + engine, + committees, + active_balances, + options, + &mut signatures, + )?; + signatures.verify() +} + +/// [`process_block`] without the final signature check: every signature the +/// block's processing needs is pushed into `signatures`, and the caller +/// verifies them. +/// +/// For [`super::apply_block`], which adds the proposer's block signature to the +/// same batch so the whole block costs one pairing check. The block is only +/// valid once `signatures.verify()` has returned `Ok`. +#[allow(clippy::too_many_arguments)] +pub fn process_block_with_signatures( + state: &mut BeaconState, + signed_block: &containers::SignedBeaconBlock, + config: &Config, + engine: &ExecutionEngine, + committees: &CommitteeCache, + active_balances: &ActiveBalanceCache, + options: BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { match signed_block { containers::SignedBeaconBlock::Phase0(signed) => process_block_phase0( @@ -58,6 +103,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Altair(signed) => process_block_altair( state, @@ -66,6 +112,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Bellatrix(signed) => bellatrix::process_block( state, @@ -75,6 +122,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Capella(signed) => capella::process_block( state, @@ -84,6 +132,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Deneb(signed) => deneb::process_block( state, @@ -93,6 +142,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Electra(signed) => electra::process_block( state, @@ -102,6 +152,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Fulu(signed) => fulu::process_block( state, @@ -111,6 +162,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), // ePBS (EIP-7732) restructures block processing around a builder's // bid rather than an embedded payload: a gloas block carries no @@ -122,6 +174,7 @@ pub fn process_block( committees, active_balances, options, + signatures, ), containers::SignedBeaconBlock::Lean(_) => { crate::beacon::lean_block_unreachable("process_block") @@ -138,6 +191,7 @@ pub fn process_block_phase0( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { process_block_header( state, @@ -146,7 +200,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, @@ -158,6 +212,7 @@ pub fn process_block_phase0( config, committees, active_balances, + signatures, )?; Ok(()) } @@ -175,6 +230,7 @@ pub fn process_block_altair( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { process_block_header( state, @@ -183,7 +239,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, @@ -195,8 +251,14 @@ pub fn process_block_altair( config, committees, active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, )?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; Ok(()) } @@ -276,8 +338,9 @@ pub enum RandaoVerification { Skip, } -/// Verifies the proposer's RANDAO reveal (unless `verification` skips it) and -/// mixes it into the current epoch's randao mix. +/// Defers the check of the proposer's RANDAO reveal (unless `verification` +/// skips it) to `signatures`, and mixes the reveal into the current epoch's +/// randao mix. /// /// 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 @@ -287,6 +350,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)?; @@ -295,8 +359,8 @@ 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", )?; } @@ -333,7 +397,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`. @@ -410,16 +476,73 @@ 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) + } + + #[test] + fn a_bad_randao_reveal_is_deferred_and_rejected_by_the_batch() { + let mut state = crate::beacon::helpers::test_state::with_validators(8); + let reveal = reveal_over(&state, Root::repeat_byte(7)); + let mut signatures = SignatureBatch::new(); + + // Nothing is checked while processing; the batch is what rejects. + process_randao( + &mut state, + &reveal, + RandaoVerification::Verify, + &mut signatures, + ) + .unwrap(); + let err = signatures.verify().unwrap_err(); + assert!( + format!("{err:?}").contains("RANDAO reveal signature"), + "{err:?}" + ); + } + + #[test] + fn a_valid_randao_reveal_passes_the_batch() { + let mut state = crate::beacon::helpers::test_state::with_validators(8); + let epoch = get_current_epoch(&state); + let domain = get_domain(&state, constants::DOMAIN_RANDAO, None); + let signing_root = compute_signing_root(epoch.hash_tree_root(), domain); + let reveal = reveal_over(&state, signing_root); + + checked(|s| process_randao(&mut state, &reveal, RandaoVerification::Verify, s)).unwrap(); + } + + #[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::immediate(); + + process_randao( + &mut state, + &reveal, + RandaoVerification::Skip, + &mut signatures, + ) + .unwrap(); + signatures.verify().unwrap(); + } + #[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 f773d6da7..90958a744 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::ExecutionEngine; @@ -72,6 +73,7 @@ use super::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, @@ -80,6 +82,7 @@ pub fn process_block( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -90,7 +93,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, @@ -103,8 +106,14 @@ pub fn process_block( config, committees, active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, )?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; Ok(()) } @@ -369,6 +378,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { super::operations::process_operations( state, @@ -380,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(()) } @@ -414,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; @@ -434,8 +446,8 @@ 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, @@ -748,7 +760,10 @@ mod tests { ); let signed_change = capella::SignedBLSToExecutionChange { message, signature }; - process_bls_to_execution_change(&mut state, &signed_change, &config).unwrap(); + crate::beacon::stf::signatures::checked(|s| { + process_bls_to_execution_change(&mut state, &signed_change, &config, s) + }) + .unwrap(); let validator = state.validator(0).unwrap(); assert_eq!( @@ -797,7 +812,12 @@ mod tests { ); let signed_change = capella::SignedBLSToExecutionChange { message, signature }; - assert!(process_bls_to_execution_change(&mut state, &signed_change, &config).is_err()); + assert!( + crate::beacon::stf::signatures::checked(|s| { + process_bls_to_execution_change(&mut state, &signed_change, &config, s) + }) + .is_err() + ); } #[test] @@ -818,6 +838,11 @@ mod tests { signature: BlsSignature::default(), }; - assert!(process_bls_to_execution_change(&mut state, &signed_change, &config).is_err()); + assert!( + crate::beacon::stf::signatures::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 72b5ac946..920f5d3d0 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::ExecutionEngine; @@ -72,6 +75,7 @@ use super::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, @@ -80,6 +84,7 @@ pub fn process_block( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -96,7 +101,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, @@ -109,8 +114,14 @@ pub fn process_block( config, committees, active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, )?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; Ok(()) } @@ -152,6 +163,7 @@ fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let outstanding = state .eth1_data() @@ -166,22 +178,22 @@ fn process_operations( )?; for proposer_slashing in proposer_slashings { - super::operations::process_proposer_slashing(state, proposer_slashing, config)?; + super::operations::process_proposer_slashing(state, proposer_slashing, config, signatures)?; } for attester_slashing in attester_slashings { - super::operations::process_attester_slashing(state, attester_slashing, config)?; + super::operations::process_attester_slashing(state, attester_slashing, config, signatures)?; } for attestation in attestations { - process_attestation(state, attestation, committees, active_balances)?; + process_attestation(state, attestation, committees, active_balances, signatures)?; } for deposit in deposits { 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(()) @@ -287,6 +299,7 @@ pub fn process_attestation( attestation: &phase0::Attestation, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -336,8 +349,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))", )?; @@ -660,6 +673,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); @@ -696,8 +710,8 @@ 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, diff --git a/crates/blockchain/state_transition/src/beacon/stf/electra.rs b/crates/blockchain/state_transition/src/beacon/stf/electra.rs index 9b3519caf..53becb7bb 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/electra.rs @@ -44,7 +44,7 @@ use libssz::SszEncode as _; -use crate::beacon::bls; +use crate::beacon::bls::{self, SignatureSet}; use crate::beacon::config::Config; use crate::beacon::constants::{self, FAR_FUTURE_EPOCH}; use crate::beacon::containers::shared::{ @@ -65,8 +65,8 @@ use crate::beacon::helpers::electra::{ get_consolidation_churn_limit_for_fork, get_indexed_attestation, get_max_effective_balance, get_pending_balance_to_withdraw, has_compounding_withdrawal_credential, has_eth1_withdrawal_credential, has_execution_withdrawal_credential, - initiate_validator_exit as electra_initiate_validator_exit, is_fully_withdrawable_validator, - is_partially_withdrawable_validator, is_valid_indexed_attestation, pending_queue_fields, + indexed_attestation_signature_set, initiate_validator_exit as electra_initiate_validator_exit, + is_fully_withdrawable_validator, is_partially_withdrawable_validator, pending_queue_fields, pending_queue_fields_ref, }; use crate::beacon::helpers::math::integer_squareroot; @@ -81,6 +81,7 @@ use crate::beacon::primitives::{ BlsPubkey, BlsSignature, Bytes32, Gwei, HashTreeRoot as _, ParticipationFlags, ValidatorIndex, WithdrawalIndex, }; +use crate::beacon::stf::signatures::SignatureBatch; use super::ExecutionEngine; @@ -489,6 +490,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); @@ -528,8 +530,8 @@ 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, @@ -560,8 +562,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) } // --------------------------------------------------------------------------- @@ -586,6 +589,7 @@ pub fn process_attester_slashing( state: &mut BeaconState, attester_slashing: &electra::AttesterSlashing, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let attestation_1 = &attester_slashing.attestation_1; let attestation_2 = &attester_slashing.attestation_2; @@ -594,12 +598,12 @@ 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)", )?; @@ -641,6 +645,7 @@ pub fn process_attestation( attestation: &electra::Attestation, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -710,8 +715,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))", )?; @@ -1785,6 +1790,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let deposit_requests_start_index = block_ref(state, "process_operations")?.deposit_requests_start_index(); @@ -1803,24 +1809,24 @@ pub fn process_operations( } for proposer_slashing in body.proposer_slashings.iter() { - super::operations::process_proposer_slashing(state, proposer_slashing, config)?; + super::operations::process_proposer_slashing(state, proposer_slashing, config, signatures)?; } for attester_slashing in body.attester_slashings.iter() { - process_attester_slashing(state, attester_slashing, config)?; + process_attester_slashing(state, attester_slashing, config, signatures)?; } // [Modified in Electra:EIP7549] for attestation in body.attestations.iter() { - process_attestation(state, attestation, committees, active_balances)?; + process_attestation(state, attestation, committees, active_balances, signatures)?; } for deposit in body.deposits.iter() { process_deposit(state, deposit, config)?; } // [Modified in Electra:EIP7251] for voluntary_exit in body.voluntary_exits.iter() { - process_voluntary_exit(state, voluntary_exit, config)?; + process_voluntary_exit(state, voluntary_exit, config, 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(state, &body.execution_requests, config)?; @@ -1845,6 +1851,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, @@ -1853,6 +1860,7 @@ pub fn process_block( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -1863,10 +1871,22 @@ 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, &block.body, config, committees, active_balances)?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; + process_operations( + state, + &block.body, + config, + committees, + active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, + )?; Ok(()) } diff --git a/crates/blockchain/state_transition/src/beacon/stf/fulu.rs b/crates/blockchain/state_transition/src/beacon/stf/fulu.rs index 1a8bc3b58..9f04facf6 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/fulu.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/fulu.rs @@ -49,6 +49,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::ExecutionEngine; @@ -61,6 +62,7 @@ use super::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( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { super::block::process_block_header( state, @@ -79,10 +82,22 @@ 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, &block.body, config, committees, active_balances)?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; + process_operations( + state, + &block.body, + config, + committees, + active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, + )?; Ok(()) } @@ -224,26 +239,33 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { // [Modified in Fulu] verify(body.deposits.is_empty(), "len(body.deposits) == 0")?; for proposer_slashing in body.proposer_slashings.iter() { - super::operations::process_proposer_slashing(state, proposer_slashing, config)?; + super::operations::process_proposer_slashing(state, proposer_slashing, config, signatures)?; } for attester_slashing in body.attester_slashings.iter() { - super::electra::process_attester_slashing(state, attester_slashing, config)?; + super::electra::process_attester_slashing(state, attester_slashing, config, signatures)?; } for attestation in body.attestations.iter() { - super::electra::process_attestation(state, attestation, committees, active_balances)?; + super::electra::process_attestation( + state, + attestation, + 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)?; + super::electra::process_voluntary_exit(state, voluntary_exit, config, 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, &body.execution_requests, config)?; diff --git a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs index 0ef4c1c8c..26835b970 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs @@ -47,7 +47,7 @@ use libssz::SszEncode as _; -use crate::beacon::bls; +use crate::beacon::bls::{self, SignatureSet}; use crate::beacon::config::Config; use crate::beacon::constants; use crate::beacon::containers::shared::{DepositMessage, ProposerSlashing}; @@ -64,9 +64,9 @@ use crate::beacon::helpers::gloas::{ add_builder_to_registry, can_builder_cover_bid, convert_builder_index_to_validator_index, convert_validator_index_to_builder_index, get_attestation_participation_flag_indices, get_indexed_attestation, get_indexed_payload_attestation, gloas_state, gloas_state_ref, + indexed_attestation_signature_set, indexed_payload_attestation_signature_set, initiate_builder_exit, is_active_builder, is_attestation_same_slot, is_builder_index, - is_builder_withdrawal_credential, is_valid_indexed_attestation, - is_valid_indexed_payload_attestation, settle_builder_payment, + is_builder_withdrawal_credential, settle_builder_payment, }; use crate::beacon::helpers::misc::{compute_domain, compute_epoch_at_slot, compute_signing_root}; use crate::beacon::helpers::mutators::{decrease_balance, increase_balance, slash_validator}; @@ -76,6 +76,7 @@ use crate::beacon::primitives::{ Bytes32, ExecutionAddress, Gwei, HashTreeRoot as _, ParticipationFlags, Root, Slot, ValidatorIndex, WithdrawalIndex, }; +use crate::beacon::stf::signatures::SignatureBatch; use super::ExecutionEngine; @@ -105,6 +106,7 @@ pub fn process_block( committees: &CommitteeCache, active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, + signatures: &mut SignatureBatch, ) -> Result<()> { // [New in Gloas:EIP7732] let parent_slot = state.latest_block_header().slot; @@ -122,8 +124,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( @@ -133,11 +140,45 @@ pub fn process_block( config, committees, active_balances, + signatures, + )?; + super::altair::process_sync_aggregate( + state, + &block.body.sync_aggregate, + active_balances, + signatures, )?; - super::altair::process_sync_aggregate(state, &block.body.sync_aggregate, active_balances)?; 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, + committees: &CommitteeCache, + active_balances: &ActiveBalanceCache, + options: super::block::BlockProcessingOptions, +) -> Result<()> { + let mut signatures = SignatureBatch::new(); + process_block( + state, + block, + config, + committees, + active_balances, + options, + &mut signatures, + )?; + signatures.verify() +} + // --------------------------------------------------------------------------- // Withdrawals // --------------------------------------------------------------------------- @@ -745,6 +786,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 @@ -756,7 +808,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, @@ -768,6 +820,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; @@ -816,8 +869,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)", )?; } @@ -1186,9 +1239,13 @@ pub fn process_proposer_slashing( state: &mut BeaconState, proposer_slashing: &ProposerSlashing, config: &Config, + 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. @@ -1249,6 +1306,7 @@ pub fn process_attester_slashing( state: &mut BeaconState, attester_slashing: &gloas::AttesterSlashing, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let attestation_1 = &attester_slashing.attestation_1; let attestation_2 = &attester_slashing.attestation_2; @@ -1257,12 +1315,12 @@ 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)", )?; @@ -1303,6 +1361,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); @@ -1363,8 +1422,8 @@ pub fn process_attestation( get_attestation_participation_flag_indices(state, &data, inclusion_delay, parent_slot)?; let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; - verify( - is_valid_indexed_attestation(state, &indexed_attestation), + signatures.push( + indexed_attestation_signature_set(state, &indexed_attestation), "process_attestation: is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -1541,6 +1600,7 @@ pub fn process_payload_attestation( state: &mut BeaconState, payload_attestation: &gloas::PayloadAttestation, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = payload_attestation.data; @@ -1561,8 +1621,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(()) @@ -1588,6 +1648,7 @@ pub fn process_operations( config: &Config, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, + signatures: &mut SignatureBatch, ) -> Result<()> { verify( body.deposits.is_empty(), @@ -1622,26 +1683,43 @@ pub fn process_operations( // [Modified in Gloas:EIP7732] for proposer_slashing in body.proposer_slashings.iter() { - process_proposer_slashing(state, proposer_slashing, config)?; + process_proposer_slashing(state, proposer_slashing, config, signatures)?; } for attester_slashing in body.attester_slashings.iter() { - process_attester_slashing(state, attester_slashing, config)?; + process_attester_slashing(state, attester_slashing, config, 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(state, voluntary_exit, config)?; + crate::beacon::stf::electra::process_voluntary_exit( + state, + voluntary_exit, + config, + 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(()) @@ -1677,7 +1755,10 @@ mod tests { signature: g2_point_at_infinity(), }; - process_execution_payload_bid(&mut state, &signed_bid, &config).unwrap(); + crate::beacon::stf::signatures::checked(|s| { + process_execution_payload_bid(&mut state, &signed_bid, &config, s) + }) + .unwrap(); assert_eq!( gloas_state_ref(&state, "test") .unwrap() @@ -1706,7 +1787,9 @@ mod tests { signature: g2_point_at_infinity(), }; - let result = process_execution_payload_bid(&mut state, &signed_bid, &config); + let result = crate::beacon::stf::signatures::checked(|s| { + process_execution_payload_bid(&mut state, &signed_bid, &config, s) + }); assert!( matches!( result, @@ -1922,14 +2005,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 = crate::beacon::stf::signatures::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 3dba6eef8..0e6070d88 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}; @@ -177,14 +178,14 @@ pub fn apply_block( "the block's fork matches the state's", )?; + // One batch for the whole block, the proposer's signature first so a block + // whose only defect is that signature is still rejected with its label. + let mut signatures = signatures::SignatureBatch::new(); if validate_result { - verify( - verify_block_signature(state, signed_block), - "block signature", - )?; + signatures.push(block_signature_set(state, signed_block), "block signature")?; } - block::process_block( + block::process_block_with_signatures( state, signed_block, config, @@ -192,7 +193,11 @@ pub fn apply_block( committees, active_balances, block::BlockProcessingOptions::default(), + &mut signatures, )?; + // Before the state root is computed: a block rejected here would otherwise + // pay for hashing a state it is about to discard. + signatures.verify()?; // As in `process_slot`: the post-state's root, checked below and needed // again in the next slot, is then computed on the state's own nodes. state.apply_pending_mutations(); @@ -212,12 +217,21 @@ pub fn verify_block_signature( state: &BeaconState, signed_block: &containers::SignedBeaconBlock, ) -> bool { - let Ok(proposer) = state.validator(signed_block.proposer_index()) else { - return false; - }; + block_signature_set(state, signed_block).is_some_and(|set| set.verify()) +} + +/// The proposer's signature check [`verify_block_signature`] makes, resolved +/// but not yet run, so [`apply_block`] can verify it with the block's other +/// signatures in one batch. `None` is a proposer index outside the registry, +/// or a key or signature that does not validate. +pub fn block_signature_set( + state: &BeaconState, + signed_block: &containers::SignedBeaconBlock, +) -> Option { + let proposer = state.validator(signed_block.proposer_index()).ok()?; let domain = get_domain(state, constants::DOMAIN_BEACON_PROPOSER, None); let signing_root = compute_signing_root(signed_block.message_hash_tree_root(), domain); - bls::verify(&proposer.pubkey, signing_root, &signed_block.signature()) + bls::SignatureSet::single(&proposer.pubkey, signing_root, &signed_block.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 5f9add7cc..86d15dcb8 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,10 @@ pub fn process_operations( )?; for proposer_slashing in proposer_slashings { - process_proposer_slashing(state, proposer_slashing, config)?; + process_proposer_slashing(state, proposer_slashing, config, signatures)?; } for attester_slashing in attester_slashings { - process_attester_slashing(state, attester_slashing, config)?; + process_attester_slashing(state, attester_slashing, config, signatures)?; } for attestation in attestations { // Phase0 defers an attestation's reward to the epoch boundary, so it @@ -139,16 +143,17 @@ pub fn process_operations( attestation, committees, active_balances, + signatures, )?; } else { - process_attestation(state, attestation, config, committees)?; + process_attestation(state, attestation, config, committees, signatures)?; } } for deposit in deposits { 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(()) @@ -160,7 +165,8 @@ 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. Both signatures are pushed to `signatures`, which +/// the block's driver checks once processing ends. /// /// Shared between phase0's own [`process_proposer_slashing`] below and /// `crate::beacon::stf::gloas::process_proposer_slashing`: gloas's own @@ -177,6 +183,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; @@ -210,8 +217,8 @@ 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)", )?; } @@ -226,8 +233,10 @@ pub fn process_proposer_slashing( state: &mut BeaconState, proposer_slashing: &ProposerSlashing, config: &Config, + 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)?; Ok(()) } @@ -295,6 +304,7 @@ pub fn process_attester_slashing( state: &mut BeaconState, attester_slashing: &phase0::AttesterSlashing, config: &Config, + signatures: &mut SignatureBatch, ) -> Result<()> { let attestation_1 = &attester_slashing.attestation_1; let attestation_2 = &attester_slashing.attestation_2; @@ -303,12 +313,12 @@ 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)", )?; @@ -345,6 +355,7 @@ pub fn process_attestation( attestation: &phase0::Attestation, _config: &Config, committees: &CommitteeCache, + signatures: &mut SignatureBatch, ) -> Result<()> { let data = attestation.data; let current_epoch = get_current_epoch(state); @@ -419,8 +430,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))", )?; @@ -621,6 +632,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); @@ -659,8 +671,8 @@ 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, @@ -705,7 +717,12 @@ mod tests { signed_header_2: signed_header, }; - assert!(process_proposer_slashing(&mut state, &slashing, &config).is_err()); + assert!( + crate::beacon::stf::signatures::checked(|s| { + process_proposer_slashing(&mut state, &slashing, &config, s) + }) + .is_err() + ); } #[test] @@ -790,6 +807,11 @@ mod tests { attestation_2, }; - assert!(process_attester_slashing(&mut state, &slashing, &config).is_err()); + assert!( + crate::beacon::stf::signatures::checked(|s| { + process_attester_slashing(&mut state, &slashing, &config, 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..d39843166 --- /dev/null +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -0,0 +1,205 @@ +//! 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 a dozen or so +//! signature sets (the RANDAO reveal, every attestation, 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, pushes it here, and the block +//! driver verifies the whole [`SignatureBatch`] once processing ends. +//! +//! # What may be deferred +//! +//! A signature may be deferred exactly 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 (`transition_block` in +//! `fork_choice.rs`) and never reads that clone after an error, so a state +//! left partly mutated by a block that is rejected a few steps later is never +//! observed. 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. +//! +//! # 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 +//! re-checks the sets one at a time in push order and reports the first that +//! fails, 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 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. A batch +/// built with [`Self::new`] verifies nothing until [`Self::verify`]; one built +/// with [`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. +#[derive(Default)] +pub struct SignatureBatch { + sets: Vec<(SignatureSet, &'static str)>, + immediate: bool, +} + +impl SignatureBatch { + /// An empty batch that defers every set pushed to it. + pub fn new() -> Self { + Self::default() + } + + /// 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 { + sets: Vec::new(), + immediate: 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 today's 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, what)); + Ok(()) + } + + /// One batch check. On failure, re-checks the sets one by one and rejects with + /// the first failing set's `what`, so the error names the same check it does + /// today. + pub fn verify(self) -> Result<()> { + let sets: Vec = self.sets.iter().map(|(set, _)| *set).collect(); + if verify_signature_sets(&sets) { + return Ok(()); + } + // A batch of valid sets always passes, so one of these fails. Should + // none do (which would be a bug in the batch check), the block is + // still rejected, with the last label, rather than accepted. + let mut last = "block signatures"; + for (set, what) in &self.sets { + last = what; + verify(set.verify(), what)?; + } + verify(false, last) + } +} + +/// Runs `f` against a fresh batch and verifies the batch, the way a block's +/// driver does: for tests of a single operation, which only mean "accepted" +/// once their deferred signatures have been checked. +#[cfg(test)] +pub(crate) fn checked(f: impl FnOnce(&mut SignatureBatch) -> Result) -> Result { + let mut batch = SignatureBatch::new(); + let out = f(&mut batch)?; + batch.verify()?; + Ok(out) +} + +#[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:?}"); + } + + #[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/tests/beacon_spec/operations.rs b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs index e627daeba..835380139 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs @@ -140,6 +140,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}; @@ -178,6 +179,10 @@ fn apply( // is nothing for the cache to share between calls. let committees = CommitteeCache::default(); let active_balances = ActiveBalanceCache::default(); + // Every signature an operation defers lands here and 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. + let mut signatures = SignatureBatch::new(); let outcome = match handler { // Every fork's own `BeaconBlock` carries a different concrete body // (altair's adds a sync aggregate, bellatrix's an execution payload, @@ -274,27 +279,51 @@ fn apply( // Phase0 defers every attestation's reward to the epoch boundary. ForkName::Phase0 => { let attestation: phase0::Attestation = case.ssz("attestation"); - operations::process_attestation(state, &attestation, config, &committees) + operations::process_attestation( + state, + &attestation, + config, + &committees, + &mut signatures, + ) } // Altair scores an attestation immediately instead, and neither // bellatrix's nor capella's specification lists a further change, // so altair's own function serves both. ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => { let attestation: phase0::Attestation = case.ssz("attestation"); - altair_stf::process_attestation(state, &attestation, &committees, &active_balances) + altair_stf::process_attestation( + state, + &attestation, + &committees, + &active_balances, + &mut signatures, + ) } // Deneb widens the inclusion window and the timely-target // condition (EIP-7045); the container is still phase0's. ForkName::Deneb => { let attestation: phase0::Attestation = case.ssz("attestation"); - deneb_stf::process_attestation(state, &attestation, &committees, &active_balances) + deneb_stf::process_attestation( + state, + &attestation, + &committees, + &active_balances, + &mut signatures, + ) } // Electra reshapes the container itself (EIP-7549's // `committee_bits`), and fulu's specification makes no further // change to either the container or the function. ForkName::Electra | ForkName::Fulu => { let attestation: electra::Attestation = case.ssz("attestation"); - electra_stf::process_attestation(state, &attestation, &committees, &active_balances) + electra_stf::process_attestation( + state, + &attestation, + &committees, + &active_balances, + &mut signatures, + ) } // Gloas reshapes the container a third time (EIP-7688's unbounded // `AttestingIndices`) and modifies the function again (the new @@ -311,6 +340,7 @@ fn apply( meta.parent_slot, &committees, &active_balances, + &mut signatures, ) } ForkName::Lean => lean_is_not_a_fixture_fork("attestation"), @@ -323,7 +353,7 @@ fn apply( | ForkName::Capella | ForkName::Deneb => { let slashing: phase0::AttesterSlashing = case.ssz("attester_slashing"); - operations::process_attester_slashing(state, &slashing, config) + operations::process_attester_slashing(state, &slashing, config, &mut signatures) } // Electra's container widens the same way `Attestation`'s does // (see the `attestation` arm above); the function itself is not @@ -331,14 +361,14 @@ 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) + electra_stf::process_attester_slashing(state, &slashing, config, &mut 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) + gloas_stf::process_attester_slashing(state, &slashing, config, &mut signatures) } ForkName::Lean => lean_is_not_a_fixture_fork("attester_slashing"), }, @@ -357,8 +387,12 @@ fn apply( | ForkName::Capella | ForkName::Deneb | ForkName::Electra - | ForkName::Fulu => operations::process_proposer_slashing(state, &slashing, config), - ForkName::Gloas => gloas_stf::process_proposer_slashing(state, &slashing, config), + | ForkName::Fulu => { + operations::process_proposer_slashing(state, &slashing, config, &mut signatures) + } + ForkName::Gloas => { + gloas_stf::process_proposer_slashing(state, &slashing, config, &mut signatures) + } ForkName::Lean => lean_is_not_a_fixture_fork("proposer_slashing"), } } @@ -405,14 +439,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, &mut 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, &mut signatures) } // Electra adds a pending-partial-withdrawal check and its own // balance-churn exit-queue accounting (EIP-7251); neither fulu's @@ -422,7 +456,7 @@ 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) + electra_stf::process_voluntary_exit(state, &exit, config, &mut signatures) } ForkName::Lean => lean_is_not_a_fixture_fork("voluntary_exit"), }, @@ -430,7 +464,12 @@ fn apply( // further fork match. "sync_aggregate" => { let sync_aggregate: altair::SyncAggregate = case.ssz("sync_aggregate"); - altair_stf::process_sync_aggregate(state, &sync_aggregate, &active_balances) + altair_stf::process_sync_aggregate( + state, + &sync_aggregate, + &active_balances, + &mut signatures, + ) } // The fixture's operation file is a whole `BeaconBlockBody`, not a // bare payload: checking one needs fields that live on the body @@ -516,7 +555,12 @@ 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, + &mut signatures, + ) } "withdrawals" => match case.fork { ForkName::Capella => { @@ -556,7 +600,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, &mut 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 @@ -627,12 +671,19 @@ 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, + &mut signatures, + ) } other => return Err(format!("unhandled operation `{other}`")), }; - outcome.map_err(|err| format!("{err:?}")) + outcome + .and_then(|()| signatures.verify()) + .map_err(|err| format!("{err:?}")) } pub fn trials() -> Vec { diff --git a/crates/common/crypto/Cargo.toml b/crates/common/crypto/Cargo.toml index bce0ef64f..c324d55e1 100644 --- a/crates/common/crypto/Cargo.toml +++ b/crates/common/crypto/Cargo.toml @@ -13,6 +13,7 @@ version.workspace = true blst = "0.3.16" ethlambda-metrics.workspace = true ethlambda-types.workspace = true +rand.workspace = true rayon.workspace = true leanvm.workspace = true diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index fb9dd2005..6b4dc691d 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -105,6 +105,9 @@ use ethlambda_types::beacon::primitives::{ BLS_PUBKEY_SIZE, BLS_SIGNATURE_SIZE, BlsPubkey, BlsSignature, Root, }; +mod batch; +pub use batch::{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. diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs new file mode 100644 index 000000000..2d0619956 --- /dev/null +++ b/crates/common/crypto/src/bls/batch.rs @@ -0,0 +1,361 @@ +//! 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`'s thread-local CSPRNG, which is seeded from the +//! operating system's entropy and reseeded periodically, and 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 the sets individually afterwards +//! ([`SignatureSet::verify`]), which is also the answer a standalone +//! [`super::verify`] or [`super::fast_aggregate_verify`] gives for the same +//! inputs. + +use blst::BLST_ERROR; +use blst::blst_scalar; +use blst::min_pk::{AggregatePublicKey, PublicKey, Signature}; +use rand::RngExt; + +use ethlambda_types::beacon::primitives::{BlsPubkey, BlsSignature, Root}; + +use super::{DST, validated_pubkey, validated_pubkeys}; + +/// How many bits of each random weight the batch check uses. +const RAND_BITS: usize = 64; + +/// The bit of a compressed G1 point's first byte that marks the point at +/// infinity. +const INFINITY_FLAG: u8 = 0b0100_0000; + +/// 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`]). +#[derive(Clone, Copy)] +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)?; + let point_refs: Vec<&PublicKey> = points.iter().collect(); + // Every point above already passed `key_validate`, so the sum skips + // re-checking them. + let pubkey = AggregatePublicKey::aggregate(&point_refs, false) + .ok()? + .to_public_key(); + Some(Self { + pubkey, + message, + signature, + aggregated: true, + }) + } + + /// The pairing check on its own: the same answer [`super::verify`] or + /// [`super::fast_aggregate_verify`] gives for these inputs. + 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 + } + + /// Whether the key is the identity, which no signature can validly be + /// made under. Only a sum of keys can be: a single registry key is + /// rejected by `key_validate` if it is. + fn key_is_identity(&self) -> bool { + self.pubkey.to_bytes()[0] & INFINITY_FLAG != 0 + } +} + +/// 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 fails the batch, as it fails +/// [`SignatureSet::verify`]: the weighted equation would otherwise let an +/// identity key over an identity signature through. +pub fn verify_signature_sets(sets: &[SignatureSet]) -> bool { + match sets { + [] => return true, + [only] => return only.verify(), + _ => {} + } + if sets.iter().any(SignatureSet::key_is_identity) { + return false; + } + 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 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 random_weights_are_never_zero() { + let mut rng = rand::rng(); + for _ in 0..10_000 { + let weight = random_weight(&mut rng); + assert!(weight.b[..8].iter().any(|byte| *byte != 0)); + assert!(weight.b[8..].iter().all(|byte| *byte == 0)); + } + } +} diff --git a/crates/net/rpc/src/beacon/validator_client_tests.rs b/crates/net/rpc/src/beacon/validator_client_tests.rs index 693450ebf..04f7816b1 100644 --- a/crates/net/rpc/src/beacon/validator_client_tests.rs +++ b/crates/net/rpc/src/beacon/validator_client_tests.rs @@ -1006,12 +1006,17 @@ fn assert_full_sync_aggregate( "every member signed the parent" ); let mut state = advanced.clone(); + let mut signatures = ethlambda_state_transition::beacon::stf::signatures::SignatureBatch::new(); ethlambda_state_transition::beacon::stf::altair::process_sync_aggregate( &mut state, aggregate, ðlambda_state_transition::beacon::helpers::accessors::ActiveBalanceCache::default(), + &mut signatures, ) - .expect("the packed aggregate verifies as the state transition checks it"); + .expect("the packed aggregate is well formed"); + signatures + .verify() + .expect("the packed aggregate verifies as the state transition checks it"); } /// A sync committee member's slot through this node: the duties and From dd3c975208dcd82b9cf15d0891cf78bdd5ea4631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 12:11:03 -0300 Subject: [PATCH 04/47] perf(beacon): verify attestations against committee key sums Electra, fulu and gloas process_attestation now build the attestation's signature set from attesters_aggregate_pubkey, which derives the key as the warmed committee sum minus the non-attesters when most of the committee attested, and sums the attesters otherwise. The key is the same group element either way, so the verdict is unchanged; only the point additions differ. Import still never builds the sums: with them cold, the builder sums the attesters directly. The indexed attestation is still built for the participation flags, and the structural checks on it (non-empty, sorted and unique, gloas's length bound) and the push label are the ones the indexed path made. The spec runners use a fresh committee cache, which only ever runs the cold branch, so electra-and-later attestation cases and every block run now run a second time with the sums warmed. Both runs must reach the same verdict and post-state. --- .../src/beacon/helpers/electra.rs | 64 ++++++++++++- .../src/beacon/helpers/gloas.rs | 24 +++++ .../src/beacon/stf/electra.rs | 96 ++++++++++++++++++- .../state_transition/src/beacon/stf/gloas.rs | 11 ++- .../state_transition/tests/beacon_spec/mod.rs | 22 +++++ .../tests/beacon_spec/operations.rs | 39 ++++++-- .../tests/beacon_spec/sanity.rs | 45 +++++++-- .../common/crypto/src/bls/committee_sums.rs | 2 +- 8 files changed, 274 insertions(+), 29 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index 51f516a55..bb880f1f1 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -126,7 +126,7 @@ 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; @@ -560,10 +560,6 @@ pub(crate) fn attesting_indices_from_committee_bits( /// /// Phase0 through deneb attestations stay on the direct path: each names one /// committee, and the live chain is past electra. -#[allow( - dead_code, - reason = "wired into process_attestation by the next change" -)] pub(crate) fn attesters_aggregate_pubkey( state: &BeaconState, slot: Slot, @@ -626,6 +622,64 @@ pub(crate) fn attesters_aggregate_pubkey( 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 structural checks [`indexed_attestation_signature_set_with_domain`] +/// makes on `attesting_indices` (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 diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index a95b66f06..f3c15394d 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs @@ -187,6 +187,30 @@ pub fn indexed_attestation_signature_set_with_domain( SignatureSet::fast_aggregate(&pubkeys, signing_root, &indexed_attestation.signature) } +/// [`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 +/// [`indexed_attestation_signature_set_with_domain`] 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; + } + 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, + ) +} + // --------------------------------------------------------------------------- // Attestations // --------------------------------------------------------------------------- diff --git a/crates/blockchain/state_transition/src/beacon/stf/electra.rs b/crates/blockchain/state_transition/src/beacon/stf/electra.rs index 53becb7bb..c0e138775 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/electra.rs @@ -61,7 +61,7 @@ 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, @@ -716,7 +716,7 @@ pub fn process_attestation( let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; signatures.push( - indexed_attestation_signature_set(state, &indexed_attestation), + electra_attestation_signature_set(state, attestation, &indexed_attestation, committees), "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; @@ -2431,4 +2431,96 @@ 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(); + process_attestation( + &mut state, + attestation, + 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/gloas.rs b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs index 26835b970..2c0bf772c 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs @@ -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, - 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, + 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}; @@ -1423,7 +1424,7 @@ pub fn process_attestation( let indexed_attestation = get_indexed_attestation(state, attestation, committees)?; signatures.push( - indexed_attestation_signature_set(state, &indexed_attestation), + gloas_attestation_signature_set(state, attestation, &indexed_attestation, committees), "process_attestation: is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))", )?; 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 835380139..a47cf02f7 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs @@ -173,11 +173,11 @@ 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(); + // `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(); // Every signature an operation defers lands here and is checked once the // operation has run, which is how a block's own driver treats them: a @@ -283,7 +283,7 @@ fn apply( state, &attestation, config, - &committees, + committees, &mut signatures, ) } @@ -295,7 +295,7 @@ fn apply( altair_stf::process_attestation( state, &attestation, - &committees, + committees, &active_balances, &mut signatures, ) @@ -307,7 +307,7 @@ fn apply( deneb_stf::process_attestation( state, &attestation, - &committees, + committees, &active_balances, &mut signatures, ) @@ -320,7 +320,7 @@ fn apply( electra_stf::process_attestation( state, &attestation, - &committees, + committees, &active_balances, &mut signatures, ) @@ -338,7 +338,7 @@ fn apply( state, &attestation, meta.parent_slot, - &committees, + committees, &active_balances, &mut signatures, ) @@ -696,7 +696,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 7d85ee935..a3f456130 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::helpers::accessors::{ActiveBalanceCache, CommitteeCache}; @@ -27,7 +28,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` @@ -59,6 +70,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(&committees, state); + } + stf::state_transition( state, &block, @@ -96,11 +111,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/src/bls/committee_sums.rs b/crates/common/crypto/src/bls/committee_sums.rs index 3d5b6371f..df30eee9b 100644 --- a/crates/common/crypto/src/bls/committee_sums.rs +++ b/crates/common/crypto/src/bls/committee_sums.rs @@ -131,8 +131,8 @@ impl std::fmt::Debug for CommitteeKeySums { #[cfg(test)] mod tests { - use super::*; use super::super::DST; + use super::*; use blst::min_pk::SecretKey; fn secret(seed: u64) -> SecretKey { From 508c43ea03a5c4afdb94003616262c245e6efd70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 13:06:22 -0300 Subject: [PATCH 05/47] feat(benchmark): add --warm-committee-sums to the import replay The replay drives import_block directly with no tick loop, so the epoch precompute worker never runs and committee key sums stay cold; the committee-sum saving is invisible in a replay although a live node gets it. The flag warms each epoch's sums before its first block, outside every measured span, so the warm-up is in no phase and no wall_seconds. It is recorded in the report params and the human header. --- bin/ethlambda/src/benchmark/import/mod.rs | 12 ++++ bin/ethlambda/src/benchmark/import/replay.rs | 71 +++++++++++++++++++- bin/ethlambda/src/benchmark/report/import.rs | 20 +++++- docs/benchmarking.md | 13 ++++ 4 files changed, 112 insertions(+), 4 deletions(-) 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..1915e59ee 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,48 @@ 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 { + None => epoch.checked_sub(1).into_iter().chain([epoch]).collect(), + Some(previous) if epoch > previous => vec![epoch], + Some(_) => return Ok(()), + }; + 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 +301,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 +318,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/docs/benchmarking.md b/docs/benchmarking.md index ae50db389..648bfc4b6 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 From bac0aa3745aeea0d4eaaa4edb5874bcce8b38758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:17:32 -0300 Subject: [PATCH 06/47] test(crypto): cover keys that cancel in the batched sum A key and its negation are both valid, so a registry can hold both and an aggregate can name both. Pin that the batched sum still matches the sequential one when keys cancel, that such a pair aggregates to the identity, and that the identity never verifies. Also state the CPU-count condition on blst's pool. --- crates/common/crypto/src/bls.rs | 79 ++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index edc304417..066ace760 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -277,9 +277,12 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// twice) and returns the same group element as the sequential sum; only the /// order of additions differs, and the group is commutative. /// -/// From 384 points up, blst splits the work over its own thread pool, which is -/// separate from rayon's; below that it runs on the calling thread. Either way -/// the answer is the same, so the caller needs no size check. +/// From 384 points up, on a host with two or more CPUs, blst splits the work +/// over its own thread pool, which is separate from rayon's; below that, or on +/// one CPU, it runs on the calling thread. The pool is the one blst's pairing +/// already ran on before this helper existed, so the sums join the same queue +/// rather than adding threads. Either way the answer is the same, 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. @@ -577,8 +580,8 @@ mod tests { } } - /// The sequential sum `sum_validated_points` must equal, through blst's - /// own `AggregatePublicKey::aggregate`. + /// The sequential sum, through blst's own `AggregatePublicKey::aggregate`: + /// what `sum_validated_points` must equal. fn sequential_sum(points: &[PublicKey]) -> [u8; BLS_PUBKEY_SIZE] { let refs: Vec<&PublicKey> = points.iter().collect(); AggregatePublicKey::aggregate(&refs, false) @@ -600,7 +603,11 @@ mod tests { #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool - // threshold (and the first multi-chunk size), then a large run. + // threshold, then larger runs. Which side of the threshold runs also + // depends on the host: on one CPU everything stays on the calling + // thread. On more, every pool worker's chunk stays below + // `blst_p1s_add`'s scratch stride, so that inner loop is never + // reached through the safe API. for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { let points = validated_run(1000..1000 + count); assert_eq!( @@ -662,6 +669,66 @@ mod tests { assert!(!fast_aggregate_verify(&[pubkey], message, &signature)); } + /// `-P`, from `P`'s compressed encoding: flipping the sort flag (bit 5 of + /// the first byte) picks the other `y` for the same `x`. Both are valid + /// keys, so a registry can hold both: whoever holds `P`'s secret signs + /// `-P`'s deposit with its negation. + fn negated(pubkey: BlsPubkey) -> BlsPubkey { + let mut bytes = pubkey.0; + bytes[0] ^= 0b0010_0000; + BlsPubkey(bytes) + } + + #[test] + fn a_key_and_its_negation_sum_to_the_identity_and_never_verify() { + let pubkey = fresh_pubkey(7000); + let pair = [pubkey, negated(pubkey)]; + assert!(key_validate(&pair[1]), "-P is a valid key"); + + // Both paths sum to the identity, and `eth_aggregate_pubkeys` hands it + // back as the spec's own pseudocode would. + let points = validated_pubkeys(&pair).unwrap(); + assert_eq!(sequential_sum(&points), identity_pubkey().0); + assert_eq!(eth_aggregate_pubkeys(&pair).unwrap(), identity_pubkey()); + + // The identity's signature is G2's identity, which blst refuses to pair + // against an identity key. + let message = ethlambda_types::beacon::primitives::H256([5u8; 32]); + let signature = BlsSignature(G2_POINT_AT_INFINITY); + assert!(!fast_aggregate_verify(&pair, message, &signature)); + assert!(!eth_fast_aggregate_verify(&pair, message, &signature)); + } + + #[test] + fn the_batched_sum_matches_the_sequential_one_when_keys_cancel() { + // Cancelling pairs next to each other, and two halves that cancel each + // other, either side of the thread-pool threshold. + for count in [8u64, 200, 400, 1100] { + let pubkeys: Vec = (8000..8000 + count).map(fresh_pubkey).collect(); + + let adjacent: Vec = pubkeys + .iter() + .flat_map(|&pubkey| [pubkey, negated(pubkey)]) + .chain([pubkeys[0]]) + .collect(); + let halves: Vec = pubkeys + .iter() + .copied() + .chain(pubkeys.iter().map(|&pubkey| negated(pubkey))) + .chain([pubkeys[1]]) + .collect(); + + for (shape, run) in [("adjacent", adjacent), ("halves", halves)] { + let points = validated_pubkeys(&run).unwrap(); + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} cancelling pairs, {shape}" + ); + } + } + } + #[test] fn eth_aggregate_pubkeys_matches_the_sequential_sum_with_repeats() { let mut pubkeys: Vec = (30..40).map(fresh_pubkey).collect(); From e6eeb5b2a399ac7df44a993e020e411b81868c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:30:21 -0300 Subject: [PATCH 07/47] fix(beacon): check the proposer's signature first and never drop a batch unverified A block fetched by request reaches the state transition without gossip's precheck, and with the proposer's signature in the body's batch a block its proposer never signed was only rejected after the whole body ran. Check that one signature on its own again, before the body, as before the batch existed. A deferring SignatureBatch could also be built and dropped without its sets ever being checked, turning any function that takes one into "valid except for its signatures". Outside the crate, a deferring batch now only comes from SignatureBatch::deferred, which verifies once its closure returns. Also cover an infinity signature inside a batch and keys that cancel. --- .../state_transition/src/beacon/stf/block.rs | 44 ++++------ .../state_transition/src/beacon/stf/gloas.rs | 22 ++--- .../state_transition/src/beacon/stf/mod.rs | 25 +++--- .../src/beacon/stf/signatures.rs | 61 +++++++++----- .../tests/beacon_spec/operations.rs | 83 ++++++++++--------- crates/common/crypto/src/bls/batch.rs | 50 +++++++++++ .../rpc/src/beacon/validator_client_tests.rs | 20 ++--- 7 files changed, 186 insertions(+), 119 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/block.rs b/crates/blockchain/state_transition/src/beacon/stf/block.rs index 2ac017dcc..af6057f3f 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/block.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/block.rs @@ -46,10 +46,7 @@ use super::{ExecutionEngine, bellatrix, capella, deneb, electra, fulu, gloas}; /// /// Means what it always did, that the block is valid: it builds the /// [`SignatureBatch`] the drivers push into and checks it before returning -/// `Ok`, so a caller that has nothing to add to the batch (block production -/// re-running its own block) needs no knowledge of it. A caller with a -/// signature of its own to check alongside, such as the proposer's, calls -/// [`process_block_with_signatures`] and verifies the batch itself. +/// `Ok`, so a caller needs no knowledge of it. /// /// An `Err` leaves `state` partly mutated, as it always could: an operation /// whose signature is deferred has already written when a bad signature is @@ -63,29 +60,24 @@ pub fn process_block( active_balances: &ActiveBalanceCache, options: BlockProcessingOptions, ) -> Result<()> { - let mut signatures = SignatureBatch::new(); - process_block_with_signatures( - state, - signed_block, - config, - engine, - committees, - active_balances, - options, - &mut signatures, - )?; - signatures.verify() + SignatureBatch::deferred(|signatures| { + process_block_with_signatures( + state, + signed_block, + config, + engine, + committees, + active_balances, + options, + signatures, + ) + }) } -/// [`process_block`] without the final signature check: every signature the -/// block's processing needs is pushed into `signatures`, and the caller -/// verifies them. -/// -/// For [`super::apply_block`], which adds the proposer's block signature to the -/// same batch so the whole block costs one pairing check. The block is only -/// valid once `signatures.verify()` has returned `Ok`. +/// [`process_block`]'s dispatch: every signature the block's processing needs +/// is pushed into `signatures`, which [`process_block`] then verifies. #[allow(clippy::too_many_arguments)] -pub fn process_block_with_signatures( +fn process_block_with_signatures( state: &mut BeaconState, signed_block: &containers::SignedBeaconBlock, config: &Config, @@ -523,7 +515,7 @@ mod tests { 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::immediate(); + let mut signatures = SignatureBatch::new(); process_randao( &mut state, @@ -532,7 +524,7 @@ mod tests { &mut signatures, ) .unwrap(); - signatures.verify().unwrap(); + assert!(signatures.is_empty()); } #[test] diff --git a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs index 26835b970..45de2f56b 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs @@ -166,17 +166,17 @@ pub fn process_block_and_verify( active_balances: &ActiveBalanceCache, options: super::block::BlockProcessingOptions, ) -> Result<()> { - let mut signatures = SignatureBatch::new(); - process_block( - state, - block, - config, - committees, - active_balances, - options, - &mut signatures, - )?; - signatures.verify() + SignatureBatch::deferred(|signatures| { + process_block( + state, + block, + config, + committees, + active_balances, + options, + signatures, + ) + }) } // --------------------------------------------------------------------------- diff --git a/crates/blockchain/state_transition/src/beacon/stf/mod.rs b/crates/blockchain/state_transition/src/beacon/stf/mod.rs index 0e6070d88..890c670bd 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/mod.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/mod.rs @@ -178,14 +178,20 @@ pub fn apply_block( "the block's fork matches the state's", )?; - // One batch for the whole block, the proposer's signature first so a block - // whose only defect is that signature is still rejected with its label. - let mut signatures = signatures::SignatureBatch::new(); + // 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. if validate_result { - signatures.push(block_signature_set(state, signed_block), "block signature")?; + verify( + verify_block_signature(state, signed_block), + "block signature", + )?; } - block::process_block_with_signatures( + block::process_block( state, signed_block, config, @@ -193,11 +199,7 @@ pub fn apply_block( committees, active_balances, block::BlockProcessingOptions::default(), - &mut signatures, )?; - // Before the state root is computed: a block rejected here would otherwise - // pay for hashing a state it is about to discard. - signatures.verify()?; // As in `process_slot`: the post-state's root, checked below and needed // again in the next slot, is then computed on the state's own nodes. state.apply_pending_mutations(); @@ -221,9 +223,8 @@ pub fn verify_block_signature( } /// The proposer's signature check [`verify_block_signature`] makes, resolved -/// but not yet run, so [`apply_block`] can verify it with the block's other -/// signatures in one batch. `None` is a proposer index outside the registry, -/// or a key or signature that does not validate. +/// but not yet run. `None` is a proposer index outside the registry, or a key +/// or signature that does not validate. pub fn block_signature_set( state: &BeaconState, signed_block: &containers::SignedBeaconBlock, diff --git a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs index d39843166..7717c944d 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -40,23 +40,41 @@ 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. A batch -/// built with [`Self::new`] verifies nothing until [`Self::verify`]; one built -/// with [`Self::immediate`] checks each set as it is pushed, which is for the +/// 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. +/// whether it was accepted. A deferring batch that could be dropped +/// unverified would turn every function taking one into "valid except for +/// its signatures". #[derive(Default)] pub struct SignatureBatch { - sets: Vec<(SignatureSet, &'static str)>, + 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 { - /// An empty batch that defers every set pushed to it. - pub fn new() -> Self { + /// An empty batch that defers every set pushed to it. Crate-internal: the + /// caller must [`Self::verify`] it, which [`Self::deferred`] guarantees. + pub(crate) fn new() -> Self { Self::default() } + /// Runs `f` with a batch that defers every set pushed to it, then verifies + /// them all: what a block's driver does, and the only way outside this + /// crate 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::new(); + let out = f(&mut batch)?; + batch.verify()?; + Ok(out) + } + /// An empty batch that checks each set the moment it is pushed. /// /// Block production's operation packer applies candidates one after the @@ -67,8 +85,8 @@ impl SignatureBatch { /// immediate checks did. pub fn immediate() -> Self { Self { - sets: Vec::new(), immediate: true, + ..Self::default() } } @@ -82,39 +100,42 @@ impl SignatureBatch { if self.immediate { return verify(set.verify(), what); } - self.sets.push((set, what)); + self.sets.push(set); + self.labels.push(what); Ok(()) } /// One batch check. On failure, re-checks the sets one by one and rejects with /// the first failing set's `what`, so the error names the same check it does /// today. - pub fn verify(self) -> Result<()> { - let sets: Vec = self.sets.iter().map(|(set, _)| *set).collect(); - if verify_signature_sets(&sets) { + pub(crate) fn verify(self) -> Result<()> { + if verify_signature_sets(&self.sets) { return Ok(()); } // A batch of valid sets always passes, so one of these fails. Should // none do (which would be a bug in the batch check), the block is // still rejected, with the last label, rather than accepted. let mut last = "block signatures"; - for (set, what) in &self.sets { + for (set, what) in self.sets.iter().zip(self.labels) { last = what; verify(set.verify(), what)?; } verify(false, last) } + + /// Whether no set has been deferred. + #[cfg(test)] + pub(crate) fn is_empty(&self) -> bool { + self.sets.is_empty() + } } -/// Runs `f` against a fresh batch and verifies the batch, the way a block's -/// driver does: for tests of a single operation, which only mean "accepted" -/// once their deferred signatures have been checked. +/// [`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 { - let mut batch = SignatureBatch::new(); - let out = f(&mut batch)?; - batch.verify()?; - Ok(out) + SignatureBatch::deferred(f) } #[cfg(test)] diff --git a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs index 835380139..2c2023283 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs @@ -174,15 +174,37 @@ fn apply( state: &mut BeaconState, config: &Config, ) -> Result<(), String> { + // Every signature an operation defers is checked once the operation has + // run, which is how a block's own driver treats them: a case whose only + // defect is a bad signature is still rejected, only later. + let mut unhandled = None; + let outcome = SignatureBatch::deferred(|signatures| { + run(handler, case, state, config, signatures).unwrap_or_else(|err| { + unhandled = Some(err); + Ok(()) + }) + }); + if let Some(err) = unhandled { + return Err(err); + } + outcome.map_err(|err| format!("{err:?}")) +} + +/// [`apply`]'s dispatch, pushing every deferred signature into `signatures`. +/// The outer `Err` is a case no handler here covers; the inner result is the +/// crate's own verdict. +fn run( + handler: &str, + case: &Case, + state: &mut BeaconState, + config: &Config, + signatures: &mut SignatureBatch, +) -> Result, String> { // Only the `attestation` handler reads this. Bound up here so each of its // calls fits on one line; a case processes a single attestation, so there // is nothing for the cache to share between calls. let committees = CommitteeCache::default(); let active_balances = ActiveBalanceCache::default(); - // Every signature an operation defers lands here and 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. - let mut signatures = SignatureBatch::new(); let outcome = match handler { // Every fork's own `BeaconBlock` carries a different concrete body // (altair's adds a sync aggregate, bellatrix's an execution payload, @@ -284,7 +306,7 @@ fn apply( &attestation, config, &committees, - &mut signatures, + signatures, ) } // Altair scores an attestation immediately instead, and neither @@ -297,7 +319,7 @@ fn apply( &attestation, &committees, &active_balances, - &mut signatures, + signatures, ) } // Deneb widens the inclusion window and the timely-target @@ -309,7 +331,7 @@ fn apply( &attestation, &committees, &active_balances, - &mut signatures, + signatures, ) } // Electra reshapes the container itself (EIP-7549's @@ -322,7 +344,7 @@ fn apply( &attestation, &committees, &active_balances, - &mut signatures, + signatures, ) } // Gloas reshapes the container a third time (EIP-7688's unbounded @@ -340,7 +362,7 @@ fn apply( meta.parent_slot, &committees, &active_balances, - &mut signatures, + signatures, ) } ForkName::Lean => lean_is_not_a_fixture_fork("attestation"), @@ -353,7 +375,7 @@ fn apply( | ForkName::Capella | ForkName::Deneb => { let slashing: phase0::AttesterSlashing = case.ssz("attester_slashing"); - operations::process_attester_slashing(state, &slashing, config, &mut signatures) + operations::process_attester_slashing(state, &slashing, config, signatures) } // Electra's container widens the same way `Attestation`'s does // (see the `attestation` arm above); the function itself is not @@ -361,14 +383,14 @@ 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, &mut signatures) + electra_stf::process_attester_slashing(state, &slashing, config, 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, &mut signatures) + gloas_stf::process_attester_slashing(state, &slashing, config, signatures) } ForkName::Lean => lean_is_not_a_fixture_fork("attester_slashing"), }, @@ -388,10 +410,10 @@ fn apply( | ForkName::Deneb | ForkName::Electra | ForkName::Fulu => { - operations::process_proposer_slashing(state, &slashing, config, &mut signatures) + operations::process_proposer_slashing(state, &slashing, config, signatures) } ForkName::Gloas => { - gloas_stf::process_proposer_slashing(state, &slashing, config, &mut signatures) + gloas_stf::process_proposer_slashing(state, &slashing, config, signatures) } ForkName::Lean => lean_is_not_a_fixture_fork("proposer_slashing"), } @@ -439,14 +461,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, &mut signatures) + 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, &mut signatures) + 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 @@ -456,7 +478,7 @@ 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, &mut signatures) + electra_stf::process_voluntary_exit(state, &exit, config, signatures) } ForkName::Lean => lean_is_not_a_fixture_fork("voluntary_exit"), }, @@ -464,12 +486,7 @@ fn apply( // further fork match. "sync_aggregate" => { let sync_aggregate: altair::SyncAggregate = case.ssz("sync_aggregate"); - altair_stf::process_sync_aggregate( - state, - &sync_aggregate, - &active_balances, - &mut signatures, - ) + altair_stf::process_sync_aggregate(state, &sync_aggregate, &active_balances, signatures) } // The fixture's operation file is a whole `BeaconBlockBody`, not a // bare payload: checking one needs fields that live on the body @@ -555,12 +572,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, - &mut signatures, - ) + capella_stf::process_bls_to_execution_change(state, &signed_change, config, signatures) } "withdrawals" => match case.fork { ForkName::Capella => { @@ -600,7 +612,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, &mut signatures) + 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 @@ -671,19 +683,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, - &mut signatures, - ) + gloas_stf::process_payload_attestation(state, &payload_attestation, config, signatures) } other => return Err(format!("unhandled operation `{other}`")), }; - outcome - .and_then(|()| signatures.verify()) - .map_err(|err| format!("{err:?}")) + Ok(outcome) } pub fn trials() -> Vec { diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs index bbb8acdf0..f9ac8bea0 100644 --- a/crates/common/crypto/src/bls/batch.rs +++ b/crates/common/crypto/src/bls/batch.rs @@ -350,6 +350,56 @@ mod tests { 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.key_is_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.key_is_identity()); + let mut sets = valid_sets(3); + sets.push(set); + assert!(verify_signature_sets(&sets)); + } + #[test] fn random_weights_are_never_zero() { let mut rng = rand::rng(); diff --git a/crates/net/rpc/src/beacon/validator_client_tests.rs b/crates/net/rpc/src/beacon/validator_client_tests.rs index 04f7816b1..35a296a16 100644 --- a/crates/net/rpc/src/beacon/validator_client_tests.rs +++ b/crates/net/rpc/src/beacon/validator_client_tests.rs @@ -1006,17 +1006,15 @@ fn assert_full_sync_aggregate( "every member signed the parent" ); let mut state = advanced.clone(); - let mut signatures = ethlambda_state_transition::beacon::stf::signatures::SignatureBatch::new(); - ethlambda_state_transition::beacon::stf::altair::process_sync_aggregate( - &mut state, - aggregate, - ðlambda_state_transition::beacon::helpers::accessors::ActiveBalanceCache::default(), - &mut signatures, - ) - .expect("the packed aggregate is well formed"); - signatures - .verify() - .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(), + 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 From 562b3691b148bfddfc50074b32d635728f92d786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:35:14 -0300 Subject: [PATCH 08/47] perf(beacon): keep cold committee sums at the plain path's cost and warm missed epochs With the sums cold, every committee was summed on its own, and each sum of a few hundred keys paid blst's pool dispatch separately where the plain path made one sum over all attesters: a cold multi-committee aggregate got slower than before this change. Every directly summed committee now goes into one sum, so the cold path is that plain sum again. Warming only ran for the epoch a precompute worker advanced into. The worker now also warms the previous epoch (after a restart mid-epoch, or a boundary whose worker did not run), and a boundary state someone else already cached (a gloas bid for the epoch's first slot) gets a warm-only task instead of no warm-up at all. Pre-electra states skip it, since nothing reads the sums there, and the warm-up histogram now times the sums alone, only when they are built. --- crates/blockchain/src/epoch_precompute.rs | 45 ++++++++++++---- .../src/beacon/helpers/accessors.rs | 14 ++++- .../src/beacon/helpers/electra.rs | 51 ++++++++++++------- .../common/crypto/src/bls/committee_sums.rs | 27 ++++++++++ docs/metrics.md | 2 +- 5 files changed, 108 insertions(+), 31 deletions(-) diff --git a/crates/blockchain/src/epoch_precompute.rs b/crates/blockchain/src/epoch_precompute.rs index 78ac11e2e..30f5f69d0 100644 --- a/crates/blockchain/src/epoch_precompute.rs +++ b/crates/blockchain/src/epoch_precompute.rs @@ -183,13 +183,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(), @@ -197,9 +194,24 @@ 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 { + // Someone else advanced the state already (a gloas bid for the + // epoch's first slot does), so no worker runs: warm the sums from + // that state instead, or the epoch would import without them. + if let Some(state) = cached + && decide(&DecisionInputs { + already_cached: false, + ..inputs + }) + .is_some() + { + let committee_cache = self.store.committee_cache(); + tokio::task::spawn_blocking(move || { + warm_epoch_key_sums(&committee_cache, &state, candidate_epoch) + }); + } return; }; // The head's post-state is resident (it was just imported); a read that @@ -233,13 +245,26 @@ impl BlockChainServer { // not wait for key sums, which only make its attestations cheaper. let _ = actor.send(EpochPrecomputed { key, result }); if let Some(state) = advanced { - let _timing = stf_metrics::time_key_sums_warm(); - committee_cache.warm_key_sums(&state, key.epoch); + 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)); +} + 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/state_transition/src/beacon/helpers/accessors.rs b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs index cfb5413ab..ef4f2aec4 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs @@ -332,7 +332,9 @@ pub trait CommitteeCacheExt { /// 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. + /// `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. fn warm_key_sums(&self, state: &BeaconState, epoch: Epoch); } @@ -365,6 +367,9 @@ impl CommitteeCacheExt for CommitteeCache { } 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; }; @@ -374,7 +379,12 @@ impl CommitteeCacheExt for CommitteeCache { // 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, || build_committee_key_sums(state, &committees, 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) + }); } } diff --git a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index bb880f1f1..471d538d6 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -550,13 +550,18 @@ pub(crate) fn attesting_indices_from_committee_bits( /// 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 it is the sum of the -/// attesters. A committee whose sum is `None` (a member's key fails -/// validation) takes the second branch, so only the attesters' own keys decide -/// it, exactly as with cold sums. The parts are then added. -/// 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'. +/// 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. @@ -574,7 +579,10 @@ pub(crate) fn attesters_aggregate_pubkey( 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)?; @@ -591,7 +599,7 @@ pub(crate) fn attesters_aggregate_pubkey( let number = committee_number(slot, committees_per_slot, committee_index)?; let sum = key_sums.as_ref().and_then(|sums| sums.get(number as usize)); - let part = match sum { + match sum { Some(sum) if attester_count * 2 > committee.len() => { let absent: Option> = committee .iter() @@ -599,19 +607,26 @@ pub(crate) fn attesters_aggregate_pubkey( .filter(|(_, set)| !**set) .map(|(index, _)| pubkey_of(*index)) .collect(); - absent.and_then(|absent| sum.minus(&absent)) + 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, + }); } _ => { - let present: Option> = committee - .iter() - .zip(&attested) - .filter(|(_, set)| **set) - .map(|(index, _)| pubkey_of(*index)) - .collect(); - present.and_then(|present| AggregatePubkey::sum(&present)) + for (index, _) in committee.iter().zip(&attested).filter(|(_, set)| **set) { + let Some(pubkey) = pubkey_of(*index) else { + return Ok(None); + }; + direct.push(pubkey); + } } - }; - let Some(part) = part else { + } + } + if !direct.is_empty() { + let Some(part) = AggregatePubkey::sum(&direct) else { return Ok(None); }; total = Some(match total { diff --git a/crates/common/crypto/src/bls/committee_sums.rs b/crates/common/crypto/src/bls/committee_sums.rs index df30eee9b..a59000a66 100644 --- a/crates/common/crypto/src/bls/committee_sums.rs +++ b/crates/common/crypto/src/bls/committee_sums.rs @@ -201,6 +201,33 @@ mod tests { 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); diff --git a/docs/metrics.md b/docs/metrics.md index 9178a702f..eeb9393e6 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -544,7 +544,7 @@ leanMetrics spec. |------|------|-------|-------------------------|--------|---------| | `lean_beacon_epoch_precompute_lookups_total` | Counter | Epoch-crossing imports, by whether a precomputed boundary state was cached | On each import whose parent is before the block's epoch start and whose block is at or after it | 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 every committee's keys for one epoch, after its precomputed state was handed to the actor | When a warm-up finishes | | 0.05, 0.1, 0.25, 0.5, 1, 2, 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 | | `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 From 944e1512d6c32771c8842feea616452dc325c290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:39:40 -0300 Subject: [PATCH 09/47] test(beacon): make the committee key-sum tests preset-agnostic The two attesters_aggregate_pubkey tests added in #742 assumed the mainnet preset, but `make test-beacon-minimal` (and the minimal-preset test job) runs the crate's `--lib` target with `preset-minimal` too, so both failed there: - the_builder_key_is_the_attesters_sum_with_cold_and_warm_sums used slot offset 31, past minimal's 8-slot epoch (SpecAssert "compute_epoch_at_slot(slot) == epoch"). - a_committee_with_an_invalid_member_key_falls_back took members[5] of a committee that holds 4 members under minimal (index out of bounds). Take the last offset from preset::SLOTS_PER_EPOCH and the bad member from the committee's middle, so both run under either preset. --- .../state_transition/src/beacon/helpers/electra.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index 471d538d6..51a649895 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -2181,7 +2181,10 @@ mod tests { use crate::beacon::primitives::Root; /// Enough validators for two committees per slot, so one attestation can - /// name two committees; mainnet's preset is the only one these run under. + /// 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) @@ -2232,7 +2235,7 @@ mod tests { assert!(warm.key_sums(&state, epoch).is_some()); assert!(cold.key_sums(&state, epoch).is_none()); - for slot_offset in [0u64, 7, 31] { + 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 { @@ -2315,7 +2318,7 @@ mod tests { .committee(slot, 0) .unwrap() .to_vec(); - let bad = members[5]; + let bad = members[members.len() / 2]; state.validator_mut(bad).unwrap().pubkey = Default::default(); let committees = CommitteeCache::default(); From f46b185d4509ea74461fc7b6752e98fe0cc715ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:37:03 -0300 Subject: [PATCH 10/47] docs(crypto): say where fast_aggregate_verify's non-empty key list comes from The comment credited the sum's existence to `pubkeys` being non-empty without saying what guarantees it. It is the early return at the top of the function, not `validated_pubkeys`, which maps an empty input to an empty `Some`. --- crates/common/crypto/src/bls.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 066ace760..a06690cec 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -410,8 +410,11 @@ pub fn fast_aggregate_verify( let Some(points) = validated_pubkeys(pubkeys) else { return false; }; - // `pubkeys` is non-empty, so there is always a sum. The signature was - // validated above, which is why the group check stays off here. + // `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; }; From f42da8263d4e311d110b434b6b0b1fa1ce1e65b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:37:07 -0300 Subject: [PATCH 11/47] test(crypto): fast_aggregate_verify rejects an empty key list Nothing pinned the early return that keeps an empty list away from the batched sum. The identity signature is used because it is the one input `eth_fast_aggregate_verify` accepts for an empty list, so the test also shows the two functions part ways there. --- crates/common/crypto/src/bls.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index a06690cec..6d265a3f4 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -603,6 +603,16 @@ mod tests { assert!(sum_validated_points(&[]).is_none()); } + #[test] + fn fast_aggregate_verify_rejects_an_empty_key_list() { + // Even with the identity signature, which is the one input the eth2 + // wrapper accepts for an empty list. + let message = ethlambda_types::beacon::primitives::H256([3u8; 32]); + let signature = BlsSignature(G2_POINT_AT_INFINITY); + assert!(!fast_aggregate_verify(&[], message, &signature)); + assert!(eth_fast_aggregate_verify(&[], message, &signature)); + } + #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool From d9f9381a4c1ea7001c9d34a1085920fd75aa5e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:41:58 -0300 Subject: [PATCH 12/47] docs(crypto): spell out the compressed-point flag bits behind INFINITY_FLAG The constant named the bit without saying where the layout comes from. It is the ZCash compressed encoding that 48-byte keys and blst's `to_bytes` use: bit 7 compressed, bit 6 identity, bit 5 the choice of y. --- crates/common/crypto/src/bls/batch.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs index f9ac8bea0..f888fd4bd 100644 --- a/crates/common/crypto/src/bls/batch.rs +++ b/crates/common/crypto/src/bls/batch.rs @@ -48,6 +48,13 @@ const RAND_BITS: usize = 64; /// The bit of a compressed G1 point's first byte that marks the point at /// infinity. +/// +/// A compressed BLS12-381 point (the ZCash serialization the consensus specs' +/// 48-byte keys use, and what blst's `to_bytes` writes) keeps three flags in +/// the top bits of its first byte: bit 7 says the encoding is compressed, bit +/// 6 that the point is the identity, and bit 5 which of the two `y` values +/// goes with `x`. The identity is therefore `0xc0` followed by zeros, and +/// bit 6 is set on no other point. const INFINITY_FLAG: u8 = 0b0100_0000; /// One signature check, resolved and ready, that may be verified alone or in a From 5c2d988f03acec1a20058b988abc323316cacb35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:42:02 -0300 Subject: [PATCH 13/47] docs(beacon): say what a failing signature batch costs and why it stays bounded A failing batch re-checks every set on its own to name the bad one, so it costs the batch plus up to one pairing per set. Only a rejected block pays that, its operation limits cap the sets, and on import the proposer's signature and index are checked before the batch runs. --- .../state_transition/src/beacon/stf/signatures.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs index 7717c944d..ac5f5c8f1 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -108,6 +108,15 @@ impl SignatureBatch { /// One batch check. On failure, re-checks the sets one by one and rejects with /// the first failing set's `what`, so the error names the same check it does /// today. + /// + /// A failing batch therefore costs the batch plus up to one single check + /// per set, roughly twice what checking each set on push would. That stays + /// bounded: only a block that is rejected pays it, the block's operation + /// limits cap how many sets it pushes, and on import `apply_block` checks + /// the proposer's signature (and the header its proposer index) before + /// this runs, so making such a block takes the slot proposer's key. + /// Bisecting the failed batch would cut the re-checks if this ever shows + /// up in a profile. pub(crate) fn verify(self) -> Result<()> { if verify_signature_sets(&self.sets) { return Ok(()); From 1bd8f80fa34d9658be1140f06e7d0cad0092887b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:42:06 -0300 Subject: [PATCH 14/47] docs(beacon): point process_block_with_signatures at the partial-state rule The note that an error leaves the state partly mutated lived only on `process_block`. The dispatch under it now points there, for whoever calls it next. --- crates/blockchain/state_transition/src/beacon/stf/block.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/blockchain/state_transition/src/beacon/stf/block.rs b/crates/blockchain/state_transition/src/beacon/stf/block.rs index af6057f3f..d04a23f08 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/block.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/block.rs @@ -76,6 +76,9 @@ pub fn process_block( /// [`process_block`]'s dispatch: every signature the block's processing needs /// is pushed into `signatures`, which [`process_block`] then verifies. +/// +/// 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, From 9344f78f0e65be3651739e192774346dc7f2f161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:42:10 -0300 Subject: [PATCH 15/47] test(beacon): explain how the operations wrapper keeps an unhandled case apart `run`'s outer error (no handler) is parked so the closure can return Ok and let the empty batch pass, while the crate's own error skips the batch the way a block's driver does. The control flow said none of that. --- .../state_transition/tests/beacon_spec/operations.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs index 2c2023283..c626c6c3b 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs @@ -177,6 +177,12 @@ fn apply( // 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. + // + // `run`'s outer `Err` (no handler for the case) is not a verdict, so the + // closure parks it in `unhandled` and returns `Ok`: the batch, empty since + // nothing ran, then passes, and the parked error is what is reported. The + // crate's own `Err` is returned from the closure as is, which skips the + // batch, as a block's driver does. let mut unhandled = None; let outcome = SignatureBatch::deferred(|signatures| { run(handler, case, state, config, signatures).unwrap_or_else(|err| { From 79a4c89752faad90da70a083bc4b969b75eb69a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:16:35 -0300 Subject: [PATCH 16/47] perf(crypto): take a single validated key as its own sum From the second automated review round: - sum_validated_points returns a one-key slice's key as is, skipping blst_p1s_add and its conversion back to affine coordinates for an aggregate with one signer. - MultiPoint::add is called by its trait name, so it does not read like std::ops::Add. - The thread-pool paragraph is tied to blst 0.3.16 and shortened, since its 384-point threshold is that version's. - The size test's comment says plainly which paths a host exercises: one CPU stays on the calling thread and takes two 1024-point scratch passes at 2000 points; two or more split the work into chunks of about 256. --- crates/common/crypto/src/bls.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 6d265a3f4..58b335dc0 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -277,20 +277,21 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// twice) and returns the same group element as the sequential sum; only the /// order of additions differs, and the group is commutative. /// -/// From 384 points up, on a host with two or more CPUs, blst splits the work -/// over its own thread pool, which is separate from rayon's; below that, or on -/// one CPU, it runs on the calling thread. The pool is the one blst's pairing -/// already ran on before this helper existed, so the sums join the same queue -/// rather than adding threads. Either way the answer is the same, so the -/// caller needs no size check. +/// In blst 0.3.16, from 384 points up on a host with two or more CPUs, the sum +/// is split over blst's own thread pool (not rayon's; the one its pairing +/// already runs on), and below that it runs on the calling thread. The answer +/// 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. +/// 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 +/// conversion back to affine coordinates. fn sum_validated_points(points: &[PublicKey]) -> Option { - if points.is_empty() { - return None; + match points { + [] => None, + [single] => Some(*single), + _ => Some(MultiPoint::add(points).to_public_key()), } - Some(points.add().to_public_key()) } /// Builds `specs/altair/bls.md`'s `G2_POINT_AT_INFINITY` constant: the @@ -616,11 +617,11 @@ mod tests { #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool - // threshold, then larger runs. Which side of the threshold runs also - // depends on the host: on one CPU everything stays on the calling - // thread. On more, every pool worker's chunk stays below - // `blst_p1s_add`'s scratch stride, so that inner loop is never - // reached through the safe API. + // threshold, then larger runs. Which paths run depends on the host. On + // one CPU everything stays on the calling thread, and the 2000-point + // run takes two passes through `blst_p1s_add`'s 1024-point scratch + // buffer. On two or more, the pool hands each worker about 256 points, + // so the threaded split runs instead and no sum takes a second pass. for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { let points = validated_run(1000..1000 + count); assert_eq!( From aa65405cd210f334b06086d164ae52305b158aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:20:40 -0300 Subject: [PATCH 17/47] fix(beacon): give SignatureBatch no public Default Four reviewers in the second automated round found the same gap: #[derive(Default)] on the public SignatureBatch made SignatureBatch::default() a public way to get a deferring batch, which outside the crate can never be verified, since verify is crate-internal. Passed to any public operation processor and dropped, it accepts a well-formed but wrong signature. new() and immediate() now build the struct themselves, and the doc says why there is no Default. --- .../state_transition/src/beacon/stf/signatures.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs index ac5f5c8f1..b5c07a377 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -47,8 +47,8 @@ use crate::beacon::error::{Result, verify}; /// 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". -#[derive(Default)] +/// its signatures", which is also why it does not implement `Default`: a +/// public `default()` would be exactly such a batch. pub struct SignatureBatch { sets: Vec, /// `sets[i]`'s label, apart from the sets so the batch check reads them @@ -61,7 +61,11 @@ impl SignatureBatch { /// An empty batch that defers every set pushed to it. Crate-internal: the /// caller must [`Self::verify`] it, which [`Self::deferred`] guarantees. pub(crate) fn new() -> Self { - Self::default() + Self { + sets: Vec::new(), + labels: Vec::new(), + immediate: false, + } } /// Runs `f` with a batch that defers every set pushed to it, then verifies @@ -86,7 +90,7 @@ impl SignatureBatch { pub fn immediate() -> Self { Self { immediate: true, - ..Self::default() + ..Self::new() } } From 62c4cbcf0ecbd888f0810cfe73d0951d2e8e1443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:20:40 -0300 Subject: [PATCH 18/47] docs(beacon): say at the check why payload attestation indices may repeat Two reviewers read the <= in indexed_payload_attestation_signature_set as a missed uniqueness check. The reason (the PTC is drawn with replacement) is on is_valid_indexed_payload_attestation above; the check now points there. --- crates/blockchain/state_transition/src/beacon/helpers/gloas.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index a95b66f06..ac8385161 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs @@ -322,6 +322,8 @@ pub fn indexed_payload_attestation_signature_set( attestation: &gloas::IndexedPayloadAttestation, ) -> Option { let indices: &[ValidatorIndex] = &attestation.attesting_indices; + // `<=`, not the `<` of the other attestation kinds: a PTC member can repeat + // (see `is_valid_indexed_payload_attestation`), so duplicates are valid. if indices.is_empty() || !indices.windows(2).all(|pair| pair[0] <= pair[1]) { return None; } From b41192b6b15503046987140e06664a18abc27f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:20:41 -0300 Subject: [PATCH 19/47] test(crypto): an invalid aggregate inside a batch fails it The batch check goes through blst's multi-signature routine and the single check through fast_aggregate_verify_pre_aggregated, and no test put a bad aggregate (a missing signer's share, another message, a claimed key that never signed) among valid sets, where a false accept would pass a block. --- crates/common/crypto/src/bls/batch.rs | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs index f888fd4bd..fa5bb7fa6 100644 --- a/crates/common/crypto/src/bls/batch.rs +++ b/crates/common/crypto/src/bls/batch.rs @@ -306,6 +306,51 @@ mod tests { 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); From f8b9cd31676590ee5abd5fb7b89465375a7e847e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:21:22 -0300 Subject: [PATCH 20/47] perf(crypto): sum validated public keys through blst's batched affine addition A committee's key sum is the largest cost left in verifying an attestation once its keys are cached. Folding the keys one by one through AggregatePublicKey::aggregate pays a projective addition per key; blst_p1s_add (reached through the safe MultiPoint::add) shares one field inversion across the batch and runs 1.6-2.3x faster in a single-threaded measurement. From 384 points up blst also spreads the work over its own thread pool. fast_aggregate_verify and eth_aggregate_pubkeys now sum through one private helper, sum_validated_points, which guards the empty slice (MultiPoint::add indexes the first element). The helper returns the same group element as the sequential sum, including for repeated keys (sync committees are drawn with replacement, so P + P occurs), which the new tests check at sizes around the 384-point threshold. Accepted and rejected inputs are unchanged. --- crates/common/crypto/src/bls.rs | 146 ++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 8 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index fb9dd2005..edc304417 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -96,8 +96,8 @@ use std::collections::HashMap; use std::sync::{LazyLock, RwLock}; -use blst::BLST_ERROR; -use blst::min_pk::{AggregatePublicKey, AggregateSignature, PublicKey, Signature}; +use blst::min_pk::{AggregateSignature, PublicKey, Signature}; +use blst::{BLST_ERROR, MultiPoint}; use rayon::prelude::*; use ethlambda_types::beacon::error::{Error, Result}; @@ -261,6 +261,35 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { points.into_iter().collect() } +/// The sum of `points` as one public key, 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 +/// through blst's `MultiPoint::add`, which hands the whole slice to +/// `blst_p1s_add`: a batched affine addition that shares one field inversion +/// across many additions, instead of paying a projective addition per key the +/// way folding the keys one by one through blst's `AggregatePublicKey::aggregate` +/// 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 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 +/// order of additions differs, and the group is commutative. +/// +/// From 384 points up, blst splits the work over its own thread pool, which is +/// separate from rayon's; below that it runs on the calling thread. Either way +/// the answer is the same, 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. +fn sum_validated_points(points: &[PublicKey]) -> Option { + if points.is_empty() { + return None; + } + Some(points.add().to_public_key()) +} + /// Builds `specs/altair/bls.md`'s `G2_POINT_AT_INFINITY` constant: the /// compressed encoding of the identity element of G2, which is the /// specification's sentinel value for "no one signed anything". @@ -378,8 +407,13 @@ pub fn fast_aggregate_verify( let Some(points) = validated_pubkeys(pubkeys) else { return false; }; - let point_refs: Vec<&PublicKey> = points.iter().collect(); - let result = signature.fast_aggregate_verify(false, message.as_slice(), DST, &point_refs); + // `pubkeys` is non-empty, so there is always a sum. 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 } @@ -397,12 +431,10 @@ pub fn eth_aggregate_pubkeys(pubkeys: &[BlsPubkey]) -> Result { let points = validated_pubkeys(pubkeys).ok_or(Error::SpecAssert( "all(bls.KeyValidate(pubkey) for pubkey in pubkeys)", ))?; - let point_refs: Vec<&PublicKey> = points.iter().collect(); // Every point above already passed `key_validate`, so the sum skips // re-checking them. - let aggregated = AggregatePublicKey::aggregate(&point_refs, false) - .map_err(|_| Error::SpecAssert("len(pubkeys) > 0"))?; - Ok(BlsPubkey(aggregated.to_public_key().to_bytes())) + let aggregated = sum_validated_points(&points).ok_or(Error::SpecAssert("len(pubkeys) > 0"))?; + Ok(BlsPubkey(aggregated.to_bytes())) } /// `specs/altair/bls.md`'s @@ -442,6 +474,7 @@ pub fn key_validate(pubkey: &BlsPubkey) -> bool { #[cfg(test)] mod tests { use super::*; + use blst::min_pk::AggregatePublicKey; #[test] fn key_validate_rejects_the_all_zero_pubkey() { @@ -544,6 +577,103 @@ mod tests { } } + /// The sequential sum `sum_validated_points` must equal, through blst's + /// own `AggregatePublicKey::aggregate`. + fn sequential_sum(points: &[PublicKey]) -> [u8; BLS_PUBKEY_SIZE] { + let refs: Vec<&PublicKey> = points.iter().collect(); + AggregatePublicKey::aggregate(&refs, false) + .expect("a non-empty run of valid points sums") + .to_public_key() + .to_bytes() + } + + fn validated_run(seeds: impl Iterator) -> Vec { + let pubkeys: Vec = seeds.map(fresh_pubkey).collect(); + validated_pubkeys(&pubkeys).expect("every key above is valid") + } + + #[test] + fn the_batched_sum_of_nothing_is_none() { + assert!(sum_validated_points(&[]).is_none()); + } + + #[test] + fn the_batched_sum_matches_the_sequential_one_at_every_size() { + // One point, a pair, then both sides of blst's 384-point thread-pool + // threshold (and the first multi-chunk size), then a large run. + for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { + let points = validated_run(1000..1000 + count); + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} distinct points" + ); + } + } + + #[test] + fn the_batched_sum_doubles_equal_points() { + let one = validated_run(5000..5001)[0]; + let pair = [one, one]; + assert_eq!( + sum_validated_points(&pair).map(|sum| sum.to_bytes()), + Some(sequential_sum(&pair)), + "P + P is a doubling" + ); + + // Several repeats scattered through a run, the shape of a committee + // drawn with replacement, at sizes either side of the threshold. + for count in [10u64, 400, 1000] { + let distinct = validated_run(6000..6000 + count); + let mut points = Vec::new(); + for (index, point) in distinct.iter().enumerate() { + points.push(*point); + if index % 3 == 0 { + points.push(*point); + } + if index % 7 == 0 { + points.push(distinct[0]); + } + } + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} keys with repeats" + ); + } + } + + #[test] + fn fast_aggregate_verify_with_a_repeated_signer_still_verifies() { + // Two signatures from the same key over one message aggregate to twice + // its signature, which only verifies if the doubled key sum is right. + let message = ethlambda_types::beacon::primitives::H256([9u8; 32]); + let mut ikm = [0x33u8; 32]; + ikm[..8].copy_from_slice(&7u64.to_le_bytes()); + let secret = blst::min_pk::SecretKey::key_gen(&ikm, &[]).unwrap(); + let pubkey = BlsPubkey(secret.sk_to_pk().to_bytes()); + let one = BlsSignature(secret.sign(message.as_slice(), DST, &[]).to_bytes()); + let signature = aggregate(&[one, one]).unwrap(); + assert!(fast_aggregate_verify( + &[pubkey, pubkey], + message, + &signature + )); + assert!(!fast_aggregate_verify(&[pubkey], message, &signature)); + } + + #[test] + fn eth_aggregate_pubkeys_matches_the_sequential_sum_with_repeats() { + let mut pubkeys: Vec = (30..40).map(fresh_pubkey).collect(); + pubkeys.push(pubkeys[2]); + pubkeys.push(pubkeys[2]); + let points = validated_pubkeys(&pubkeys).unwrap(); + assert_eq!( + eth_aggregate_pubkeys(&pubkeys).unwrap().0, + sequential_sum(&points) + ); + } + #[test] fn the_cache_stops_growing_at_its_capacity() { // One entry per shard. From b19da8f8c2c2aa10a9f98060a4b5367c2e77530b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:17:32 -0300 Subject: [PATCH 21/47] test(crypto): cover keys that cancel in the batched sum A key and its negation are both valid, so a registry can hold both and an aggregate can name both. Pin that the batched sum still matches the sequential one when keys cancel, that such a pair aggregates to the identity, and that the identity never verifies. Also state the CPU-count condition on blst's pool. --- crates/common/crypto/src/bls.rs | 79 ++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index edc304417..066ace760 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -277,9 +277,12 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// twice) and returns the same group element as the sequential sum; only the /// order of additions differs, and the group is commutative. /// -/// From 384 points up, blst splits the work over its own thread pool, which is -/// separate from rayon's; below that it runs on the calling thread. Either way -/// the answer is the same, so the caller needs no size check. +/// From 384 points up, on a host with two or more CPUs, blst splits the work +/// over its own thread pool, which is separate from rayon's; below that, or on +/// one CPU, it runs on the calling thread. The pool is the one blst's pairing +/// already ran on before this helper existed, so the sums join the same queue +/// rather than adding threads. Either way the answer is the same, 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. @@ -577,8 +580,8 @@ mod tests { } } - /// The sequential sum `sum_validated_points` must equal, through blst's - /// own `AggregatePublicKey::aggregate`. + /// The sequential sum, through blst's own `AggregatePublicKey::aggregate`: + /// what `sum_validated_points` must equal. fn sequential_sum(points: &[PublicKey]) -> [u8; BLS_PUBKEY_SIZE] { let refs: Vec<&PublicKey> = points.iter().collect(); AggregatePublicKey::aggregate(&refs, false) @@ -600,7 +603,11 @@ mod tests { #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool - // threshold (and the first multi-chunk size), then a large run. + // threshold, then larger runs. Which side of the threshold runs also + // depends on the host: on one CPU everything stays on the calling + // thread. On more, every pool worker's chunk stays below + // `blst_p1s_add`'s scratch stride, so that inner loop is never + // reached through the safe API. for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { let points = validated_run(1000..1000 + count); assert_eq!( @@ -662,6 +669,66 @@ mod tests { assert!(!fast_aggregate_verify(&[pubkey], message, &signature)); } + /// `-P`, from `P`'s compressed encoding: flipping the sort flag (bit 5 of + /// the first byte) picks the other `y` for the same `x`. Both are valid + /// keys, so a registry can hold both: whoever holds `P`'s secret signs + /// `-P`'s deposit with its negation. + fn negated(pubkey: BlsPubkey) -> BlsPubkey { + let mut bytes = pubkey.0; + bytes[0] ^= 0b0010_0000; + BlsPubkey(bytes) + } + + #[test] + fn a_key_and_its_negation_sum_to_the_identity_and_never_verify() { + let pubkey = fresh_pubkey(7000); + let pair = [pubkey, negated(pubkey)]; + assert!(key_validate(&pair[1]), "-P is a valid key"); + + // Both paths sum to the identity, and `eth_aggregate_pubkeys` hands it + // back as the spec's own pseudocode would. + let points = validated_pubkeys(&pair).unwrap(); + assert_eq!(sequential_sum(&points), identity_pubkey().0); + assert_eq!(eth_aggregate_pubkeys(&pair).unwrap(), identity_pubkey()); + + // The identity's signature is G2's identity, which blst refuses to pair + // against an identity key. + let message = ethlambda_types::beacon::primitives::H256([5u8; 32]); + let signature = BlsSignature(G2_POINT_AT_INFINITY); + assert!(!fast_aggregate_verify(&pair, message, &signature)); + assert!(!eth_fast_aggregate_verify(&pair, message, &signature)); + } + + #[test] + fn the_batched_sum_matches_the_sequential_one_when_keys_cancel() { + // Cancelling pairs next to each other, and two halves that cancel each + // other, either side of the thread-pool threshold. + for count in [8u64, 200, 400, 1100] { + let pubkeys: Vec = (8000..8000 + count).map(fresh_pubkey).collect(); + + let adjacent: Vec = pubkeys + .iter() + .flat_map(|&pubkey| [pubkey, negated(pubkey)]) + .chain([pubkeys[0]]) + .collect(); + let halves: Vec = pubkeys + .iter() + .copied() + .chain(pubkeys.iter().map(|&pubkey| negated(pubkey))) + .chain([pubkeys[1]]) + .collect(); + + for (shape, run) in [("adjacent", adjacent), ("halves", halves)] { + let points = validated_pubkeys(&run).unwrap(); + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} cancelling pairs, {shape}" + ); + } + } + } + #[test] fn eth_aggregate_pubkeys_matches_the_sequential_sum_with_repeats() { let mut pubkeys: Vec = (30..40).map(fresh_pubkey).collect(); From 5bb3dda9ef86fbab4f10ab6d610a71cf601f49fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:37:03 -0300 Subject: [PATCH 22/47] docs(crypto): say where fast_aggregate_verify's non-empty key list comes from The comment credited the sum's existence to `pubkeys` being non-empty without saying what guarantees it. It is the early return at the top of the function, not `validated_pubkeys`, which maps an empty input to an empty `Some`. --- crates/common/crypto/src/bls.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 066ace760..a06690cec 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -410,8 +410,11 @@ pub fn fast_aggregate_verify( let Some(points) = validated_pubkeys(pubkeys) else { return false; }; - // `pubkeys` is non-empty, so there is always a sum. The signature was - // validated above, which is why the group check stays off here. + // `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; }; From be26d6bf0efde12a6ad491ccd167d65803405b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:37:07 -0300 Subject: [PATCH 23/47] test(crypto): fast_aggregate_verify rejects an empty key list Nothing pinned the early return that keeps an empty list away from the batched sum. The identity signature is used because it is the one input `eth_fast_aggregate_verify` accepts for an empty list, so the test also shows the two functions part ways there. --- crates/common/crypto/src/bls.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index a06690cec..6d265a3f4 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -603,6 +603,16 @@ mod tests { assert!(sum_validated_points(&[]).is_none()); } + #[test] + fn fast_aggregate_verify_rejects_an_empty_key_list() { + // Even with the identity signature, which is the one input the eth2 + // wrapper accepts for an empty list. + let message = ethlambda_types::beacon::primitives::H256([3u8; 32]); + let signature = BlsSignature(G2_POINT_AT_INFINITY); + assert!(!fast_aggregate_verify(&[], message, &signature)); + assert!(eth_fast_aggregate_verify(&[], message, &signature)); + } + #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool From 8bf017188acf4f54547d2bcd1dd3200594a14d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:16:35 -0300 Subject: [PATCH 24/47] perf(crypto): take a single validated key as its own sum From the second automated review round: - sum_validated_points returns a one-key slice's key as is, skipping blst_p1s_add and its conversion back to affine coordinates for an aggregate with one signer. - MultiPoint::add is called by its trait name, so it does not read like std::ops::Add. - The thread-pool paragraph is tied to blst 0.3.16 and shortened, since its 384-point threshold is that version's. - The size test's comment says plainly which paths a host exercises: one CPU stays on the calling thread and takes two 1024-point scratch passes at 2000 points; two or more split the work into chunks of about 256. --- crates/common/crypto/src/bls.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 6d265a3f4..58b335dc0 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -277,20 +277,21 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// twice) and returns the same group element as the sequential sum; only the /// order of additions differs, and the group is commutative. /// -/// From 384 points up, on a host with two or more CPUs, blst splits the work -/// over its own thread pool, which is separate from rayon's; below that, or on -/// one CPU, it runs on the calling thread. The pool is the one blst's pairing -/// already ran on before this helper existed, so the sums join the same queue -/// rather than adding threads. Either way the answer is the same, so the -/// caller needs no size check. +/// In blst 0.3.16, from 384 points up on a host with two or more CPUs, the sum +/// is split over blst's own thread pool (not rayon's; the one its pairing +/// already runs on), and below that it runs on the calling thread. The answer +/// 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. +/// 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 +/// conversion back to affine coordinates. fn sum_validated_points(points: &[PublicKey]) -> Option { - if points.is_empty() { - return None; + match points { + [] => None, + [single] => Some(*single), + _ => Some(MultiPoint::add(points).to_public_key()), } - Some(points.add().to_public_key()) } /// Builds `specs/altair/bls.md`'s `G2_POINT_AT_INFINITY` constant: the @@ -616,11 +617,11 @@ mod tests { #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool - // threshold, then larger runs. Which side of the threshold runs also - // depends on the host: on one CPU everything stays on the calling - // thread. On more, every pool worker's chunk stays below - // `blst_p1s_add`'s scratch stride, so that inner loop is never - // reached through the safe API. + // threshold, then larger runs. Which paths run depends on the host. On + // one CPU everything stays on the calling thread, and the 2000-point + // run takes two passes through `blst_p1s_add`'s 1024-point scratch + // buffer. On two or more, the pool hands each worker about 256 points, + // so the threaded split runs instead and no sum takes a second pass. for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { let points = validated_run(1000..1000 + count); assert_eq!( From 170aee8bae6818747d9603328ee9d3b0d794a14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:57:13 -0300 Subject: [PATCH 25/47] fix(beacon): keep the unverified signature batch test-only and make a disagreement loud From the second and third automated review rounds on SignatureBatch: - SignatureBatch::new, a deferring batch nothing verifies, exists only under cfg(test). Production code can hold a deferring batch only through deferred, which always verifies it, so the in-crate footgun is closed by the compiler. A drop guard would also fire on correct code: a batch dropped because its block was already rejected. - A failed batch finds its first bad set with rayon's position_first, keeping push order but spreading the single checks over the cores. - A batch that fails while every set verifies alone is a batch-check bug. The block is still rejected, but now with an error log and lean_beacon_signature_batch_disagreements_total, since rejecting a block every single check accepts can fork the node off. - verify debug-asserts one label per set. - The module doc gives the real range of sets per block, and argues why deferring adds no reachable state: an operation's effects never depend on its signature, so a forged one leads only where a genuine one could, and every path applying outside input validates signatures (import and era replay pass true). --- .../src/beacon/stf/signatures.rs | 85 ++++++++++++------- .../state_transition/src/metrics.rs | 15 ++++ docs/metrics.md | 1 + 3 files changed, 70 insertions(+), 31 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs index b5c07a377..38fecfb4e 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -1,9 +1,10 @@ //! 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 a dozen or so -//! signature sets (the RANDAO reveal, every attestation, the sync aggregate, -//! any exits and slashings), and checking them in one random-linear-combination +//! 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, pushes it here, and the block @@ -21,6 +22,16 @@ //! 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 lets an operation with a forged signature change the state the +//! rest of the block runs on (a forged slashing slashes before the sync +//! aggregate's rewards are paid), but it reaches no state a valid block could +//! not: what an operation does never depends on its signature, so the same +//! effects follow from the same operation genuinely signed (a validator that +//! did equivocate, an exit that was signed). Any panic a later operation hits +//! there is one a valid block can reach too. Every path that applies outside +//! input checks signatures (`validate_result` is true on import and in era +//! replay), and the proposer's signature is checked before the body runs. +//! //! # What a late failure names //! //! Every error is the check's own label (the same `&'static str` the immediate @@ -35,6 +46,8 @@ //! specification, only that the block is invalid. use ethlambda_crypto::bls::{SignatureSet, verify_signature_sets}; +use rayon::prelude::*; +use tracing::error; use crate::beacon::error::{Result, verify}; @@ -48,7 +61,10 @@ use crate::beacon::error::{Result, verify}; /// 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. +/// 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 @@ -58,22 +74,28 @@ pub struct SignatureBatch { } impl SignatureBatch { - /// An empty batch that defers every set pushed to it. Crate-internal: the - /// caller must [`Self::verify`] it, which [`Self::deferred`] guarantees. - pub(crate) fn new() -> Self { + fn empty(immediate: bool) -> Self { Self { sets: Vec::new(), labels: Vec::new(), - immediate: false, + 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 - /// them all: what a block's driver does, and the only way outside this - /// crate to defer signatures. An error from `f` is returned as is, since - /// it rejects whatever `f` was processing whatever the signatures say. + /// them all: 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::new(); + let mut batch = Self::empty(false); let out = f(&mut batch)?; batch.verify()?; Ok(out) @@ -88,15 +110,12 @@ impl SignatureBatch { /// happening, so its calls check as they go and behave exactly as the /// immediate checks did. pub fn immediate() -> Self { - Self { - immediate: true, - ..Self::new() - } + 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 today's immediate check fails with. + /// 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); @@ -109,30 +128,34 @@ impl SignatureBatch { Ok(()) } - /// One batch check. On failure, re-checks the sets one by one and rejects with - /// the first failing set's `what`, so the error names the same check it does - /// today. + /// One batch check. On failure, re-checks the sets one by one and rejects + /// with the first failing set's `what` in push order, so the error names + /// the check the immediate path would have stopped at. /// - /// A failing batch therefore costs the batch plus up to one single check - /// per set, roughly twice what checking each set on push would. That stays + /// The re-checks run on rayon's pool, so a failing batch costs the batch + /// plus one round of single checks spread over the cores. That stays /// bounded: only a block that is rejected pays it, the block's operation /// limits cap how many sets it pushes, and on import `apply_block` checks /// the proposer's signature (and the header its proposer index) before /// this runs, so making such a block takes the slot proposer's key. - /// Bisecting the failed batch would cut the re-checks if this ever shows - /// up in a profile. pub(crate) fn verify(self) -> Result<()> { + debug_assert_eq!(self.sets.len(), self.labels.len(), "one label per set"); if verify_signature_sets(&self.sets) { return Ok(()); } - // A batch of valid sets always passes, so one of these fails. Should - // none do (which would be a bug in the batch check), the block is - // still rejected, with the last label, rather than accepted. - let mut last = "block signatures"; - for (set, what) in self.sets.iter().zip(self.labels) { - last = what; - verify(set.verify(), what)?; + if let Some(first_bad) = self.sets.par_iter().position_first(|set| !set.verify()) { + return verify(false, self.labels[first_bad]); } + // A batch of valid sets always passes, so a set should have failed + // above. None did, 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 = self.sets.len(), + "Signature batch failed but every set verifies on its own; rejecting the block" + ); + crate::metrics::inc_signature_batch_disagreements(); + let last = self.labels.last().copied().unwrap_or("block signatures"); verify(false, last) } diff --git a/crates/blockchain/state_transition/src/metrics.rs b/crates/blockchain/state_transition/src/metrics.rs index 63c029166..bf89ea5a0 100644 --- a/crates/blockchain/state_transition/src/metrics.rs +++ b/crates/blockchain/state_transition/src/metrics.rs @@ -45,6 +45,21 @@ pub fn inc_finalizations(result: &str) { LEAN_FINALIZATIONS_TOTAL.with_label_values(&[result]).inc(); } +/// A block's signature batch failed while every set in it verified 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() { + static LEAN_BEACON_SIGNATURE_BATCH_DISAGREEMENTS_TOTAL: LazyLock = + LazyLock::new(|| { + register_int_counter!( + "lean_beacon_signature_batch_disagreements_total", + "Signature batches that failed while every set verified on its own" + ) + .unwrap() + }); + LEAN_BEACON_SIGNATURE_BATCH_DISAGREEMENTS_TOTAL.inc(); +} + static LEAN_BEACON_COMMITTEE_CACHE_LOOKUPS_TOTAL: LazyLock = LazyLock::new(|| { register_int_counter_vec!( "lean_beacon_committee_cache_lookups_total", diff --git a/docs/metrics.md b/docs/metrics.md index 20f353660..a3e261be4 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -499,6 +499,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 while every set in them verified 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 no single set does | | **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 From f7805d235da44401277ff9e82c55a6c4eff4bdd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:57:13 -0300 Subject: [PATCH 26/47] perf(crypto): tell an identity key from its affine point, and stop copying signature sets implicitly key_is_identity compressed every key to read one flag. blst stores the identity as the all-zero affine point and compares PublicKey by those bytes, so comparing against PublicKey::default() is its blst_p1_affine_is_inf without the unsafe call. It looks at aggregated sets only, since a single validated key is never the identity; the existing cancellation tests assert both answers. SignatureSet is about 300 bytes, so it is Clone but no longer Copy. --- crates/common/crypto/src/bls/batch.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs index fa5bb7fa6..3fad3583b 100644 --- a/crates/common/crypto/src/bls/batch.rs +++ b/crates/common/crypto/src/bls/batch.rs @@ -46,17 +46,6 @@ use super::{DST, validated_pubkey, validated_pubkeys}; /// How many bits of each random weight the batch check uses. const RAND_BITS: usize = 64; -/// The bit of a compressed G1 point's first byte that marks the point at -/// infinity. -/// -/// A compressed BLS12-381 point (the ZCash serialization the consensus specs' -/// 48-byte keys use, and what blst's `to_bytes` writes) keeps three flags in -/// the top bits of its first byte: bit 7 says the encoding is compressed, bit -/// 6 that the point is the identity, and bit 5 which of the two `y` values -/// goes with `x`. The identity is therefore `0xc0` followed by zeros, and -/// bit 6 is set on no other point. -const INFINITY_FLAG: u8 = 0b0100_0000; - /// One signature check, resolved and ready, that may be verified alone or in a /// batch. /// @@ -65,7 +54,10 @@ const INFINITY_FLAG: u8 = 0b0100_0000; /// 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`]). -#[derive(Clone, Copy)] +/// +/// `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, @@ -147,8 +139,13 @@ impl SignatureSet { /// blst rejects the same input itself (`BLST_PK_IS_INFINITY`, from the /// pairing aggregation both the single and the batch check go through), /// so this makes the existing rule explicit and changes no behavior. + /// + /// Read off the affine point, with no serialization: blst stores the + /// identity as the all-zero point, which is `PublicKey::default()`, and its + /// `PartialEq` compares those bytes (the same test as its + /// `blst_p1_affine_is_inf`, without the `unsafe` call). fn key_is_identity(&self) -> bool { - self.pubkey.to_bytes()[0] & INFINITY_FLAG != 0 + self.aggregated && self.pubkey == PublicKey::default() } } From bb658bc977bc5a315b17eff8cc1ec967c4d31813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:57:13 -0300 Subject: [PATCH 27/47] docs(beacon): cite the spec's sorted-only check for payload attestation indices --- .../blockchain/state_transition/src/beacon/helpers/gloas.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index ac8385161..7b3702f1b 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs @@ -322,8 +322,9 @@ pub fn indexed_payload_attestation_signature_set( attestation: &gloas::IndexedPayloadAttestation, ) -> Option { let indices: &[ValidatorIndex] = &attestation.attesting_indices; - // `<=`, not the `<` of the other attestation kinds: a PTC member can repeat - // (see `is_valid_indexed_payload_attestation`), so duplicates are valid. + // `<=`, 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 None; } From 1f5554206bc43451a356a305b7eefd508c2ec7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:57:14 -0300 Subject: [PATCH 28/47] test(beacon): import checked everywhere, and drop the operations harness's side channel Eight tests called crate::beacon::stf::signatures::checked by its full path; they import it now, like the rest. The operations harness parked an unhandled case in a mutable variable outside SignatureBatch::deferred. The closure now turns run's nesting inside out instead: the crate's error skips the batch, and an unhandled case passes through the empty batch as Ok(Err(..)) and comes back out as itself. --- .../src/beacon/stf/capella.rs | 11 ++++--- .../state_transition/src/beacon/stf/gloas.rs | 13 ++++----- .../src/beacon/stf/operations.rs | 11 ++----- .../tests/beacon_spec/operations.rs | 29 +++++++++---------- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/capella.rs b/crates/blockchain/state_transition/src/beacon/stf/capella.rs index 90958a744..1f86775f3 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/capella.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/capella.rs @@ -520,6 +520,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 @@ -760,10 +761,8 @@ mod tests { ); let signed_change = capella::SignedBLSToExecutionChange { message, signature }; - crate::beacon::stf::signatures::checked(|s| { - process_bls_to_execution_change(&mut state, &signed_change, &config, s) - }) - .unwrap(); + checked(|s| process_bls_to_execution_change(&mut state, &signed_change, &config, s)) + .unwrap(); let validator = state.validator(0).unwrap(); assert_eq!( @@ -813,7 +812,7 @@ mod tests { let signed_change = capella::SignedBLSToExecutionChange { message, signature }; assert!( - crate::beacon::stf::signatures::checked(|s| { + checked(|s| { process_bls_to_execution_change(&mut state, &signed_change, &config, s) }) .is_err() @@ -839,7 +838,7 @@ mod tests { }; assert!( - crate::beacon::stf::signatures::checked(|s| { + 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/gloas.rs b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs index 45de2f56b..f11bdb0e5 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs @@ -1731,6 +1731,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) @@ -1755,10 +1756,7 @@ mod tests { signature: g2_point_at_infinity(), }; - crate::beacon::stf::signatures::checked(|s| { - process_execution_payload_bid(&mut state, &signed_bid, &config, s) - }) - .unwrap(); + checked(|s| process_execution_payload_bid(&mut state, &signed_bid, &config, s)).unwrap(); assert_eq!( gloas_state_ref(&state, "test") .unwrap() @@ -1787,9 +1785,8 @@ mod tests { signature: g2_point_at_infinity(), }; - let result = crate::beacon::stf::signatures::checked(|s| { - process_execution_payload_bid(&mut state, &signed_bid, &config, s) - }); + let result = + checked(|s| process_execution_payload_bid(&mut state, &signed_bid, &config, s)); assert!( matches!( result, @@ -2005,7 +2002,7 @@ mod tests { for (expected_message, body) in cases { let mut state = state.clone(); let committees = CommitteeCache::default(); - let result = crate::beacon::stf::signatures::checked(|s| { + let result = checked(|s| { process_operations( &mut state, &body, diff --git a/crates/blockchain/state_transition/src/beacon/stf/operations.rs b/crates/blockchain/state_transition/src/beacon/stf/operations.rs index 86d15dcb8..33da443a2 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/operations.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/operations.rs @@ -694,6 +694,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() { @@ -718,10 +719,7 @@ mod tests { }; assert!( - crate::beacon::stf::signatures::checked(|s| { - process_proposer_slashing(&mut state, &slashing, &config, s) - }) - .is_err() + checked(|s| { process_proposer_slashing(&mut state, &slashing, &config, s) }).is_err() ); } @@ -808,10 +806,7 @@ mod tests { }; assert!( - crate::beacon::stf::signatures::checked(|s| { - process_attester_slashing(&mut state, &slashing, &config, s) - }) - .is_err() + checked(|s| { process_attester_slashing(&mut state, &slashing, &config, s) }).is_err() ); } } diff --git a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs index c626c6c3b..50137ba7c 100644 --- a/crates/blockchain/state_transition/tests/beacon_spec/operations.rs +++ b/crates/blockchain/state_transition/tests/beacon_spec/operations.rs @@ -178,22 +178,19 @@ fn apply( // 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. // - // `run`'s outer `Err` (no handler for the case) is not a verdict, so the - // closure parks it in `unhandled` and returns `Ok`: the batch, empty since - // nothing ran, then passes, and the parked error is what is reported. The - // crate's own `Err` is returned from the closure as is, which skips the - // batch, as a block's driver does. - let mut unhandled = None; - let outcome = SignatureBatch::deferred(|signatures| { - run(handler, case, state, config, signatures).unwrap_or_else(|err| { - unhandled = Some(err); - Ok(()) - }) - }); - if let Some(err) = unhandled { - return Err(err); - } - outcome.map_err(|err| format!("{err:?}")) + // The closure turns `run`'s nesting inside out. The crate's own `Err` is + // the closure's error, which skips the batch as a block's driver does. A + // case no handler covers ran nothing, so it passes through the batch + // (empty, so it verifies) as the closure's `Ok(Err(..))`, and the last + // step hands that error back. + SignatureBatch::deferred( + |signatures| match run(handler, case, state, config, signatures) { + Ok(verdict) => verdict.map(Ok), + Err(unhandled) => Ok(Err(unhandled)), + }, + ) + .map_err(|err| format!("{err:?}")) + .and_then(|handled| handled) } /// [`apply`]'s dispatch, pushing every deferred signature into `signatures`. From 407074c6b89f8e506e10a70187fc431f819cc136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:18:16 -0300 Subject: [PATCH 29/47] perf(blockchain): run one warm-only key-sum task at a time, and name why one runs From the automated review (Claude, Codex, Kimi): - The warm-only path spawned a blocking task on every trigger while the epoch's state stayed cached. The sums sit behind a OnceLock, so a second task never rebuilt them, but it held a blocking thread waiting on the first. An AtomicBool in the actor now lets one run at a time, cleared by a drop guard however the task ends. - The re-asked decide() is bound to a named boolean, only_the_cache_stopped_the_worker, and the comment says what advances the state early (validating a gloas bid for the epoch's first slot). --- crates/blockchain/src/epoch_precompute.rs | 37 ++++++++++++++++++----- crates/blockchain/src/lib.rs | 8 +++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/blockchain/src/epoch_precompute.rs b/crates/blockchain/src/epoch_precompute.rs index 30f5f69d0..31b396636 100644 --- a/crates/blockchain/src/epoch_precompute.rs +++ b/crates/blockchain/src/epoch_precompute.rs @@ -22,6 +22,7 @@ //! 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; @@ -197,18 +198,28 @@ impl BlockChainServer { already_cached: cached.is_some(), }; let Some(key) = decide(&inputs) else { - // Someone else advanced the state already (a gloas bid for the - // epoch's first slot does), so no worker runs: warm the sums from - // that state instead, or the epoch would import without them. + // 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 - && decide(&DecisionInputs { - already_cached: false, - ..inputs - }) - .is_some() + && 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) }); } @@ -265,6 +276,16 @@ fn warm_epoch_key_sums( 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 251048b7a..5f7f4d316 100644 --- a/crates/blockchain/src/lib.rs +++ b/crates/blockchain/src/lib.rs @@ -425,6 +425,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, @@ -555,6 +556,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 @@ -2018,6 +2024,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), @@ -4490,6 +4497,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, From 5af189d4ca0ee274cf7daadf98cad628a066e2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:18:16 -0300 Subject: [PATCH 30/47] fix(benchmark): warm the previous epoch's key sums after a gap in the corpus The replay warmed only the new epoch whenever a previous block existed, so a corpus that skipped an epoch left epoch - 1 cold where the live worker warms both. It now warms both after a gap, as it already did for the first block. --- bin/ethlambda/src/benchmark/import/replay.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bin/ethlambda/src/benchmark/import/replay.rs b/bin/ethlambda/src/benchmark/import/replay.rs index 1915e59ee..a96fb7923 100644 --- a/bin/ethlambda/src/benchmark/import/replay.rs +++ b/bin/ethlambda/src/benchmark/import/replay.rs @@ -234,9 +234,14 @@ fn warm_committee_sums( ) -> eyre::Result<()> { let epoch = block.slot() / preset::SLOTS_PER_EPOCH; let epochs: Vec = match previous_epoch { - None => epoch.checked_sub(1).into_iter().chain([epoch]).collect(), - Some(previous) if epoch > previous => vec![epoch], - Some(_) => return Ok(()), + 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(|| { From 678cf9c97b7aaf8a916c77c45aaf9a5a1cfefd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:18:17 -0300 Subject: [PATCH 31/47] docs(beacon): spell out the key-sum majority rule and what a warm-up costs - The strict-majority gate reads attester_count > committee.len() / 2, the same rule as attester_count * 2 > len without a multiplication that could overflow, with a comment saying why subtraction wins there. - warm_key_sums says committee derivation is part of a warm-up's cost though the warm metric times the sums alone, and build_committee_key_sums states its transient 48 MB key list and that it shares rayon's pool with import's key validation for about 70 ms an epoch. --- .../state_transition/src/beacon/helpers/accessors.rs | 12 ++++++++++++ .../state_transition/src/beacon/helpers/electra.rs | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs index ef4f2aec4..b386c8959 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/accessors.rs @@ -335,6 +335,11 @@ pub trait CommitteeCacheExt { /// `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); } @@ -395,6 +400,13 @@ impl CommitteeCacheExt for CommitteeCache { /// 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, diff --git a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs index 51a649895..4cf875e02 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -599,8 +599,11 @@ pub(crate) fn attesters_aggregate_pubkey( 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 * 2 > committee.len() => { + Some(sum) if attester_count > committee.len() / 2 => { let absent: Option> = committee .iter() .zip(&attested) From efa46694c6cd83302fa1d29d58c66a55095850e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:18:17 -0300 Subject: [PATCH 32/47] chore(metrics): give the key-sum warm histogram a 16 s bucket --- crates/blockchain/state_transition/src/metrics.rs | 2 +- docs/metrics.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/blockchain/state_transition/src/metrics.rs b/crates/blockchain/state_transition/src/metrics.rs index af4000625..4b537d0fa 100644 --- a/crates/blockchain/state_transition/src/metrics.rs +++ b/crates/blockchain/state_transition/src/metrics.rs @@ -164,7 +164,7 @@ static LEAN_BEACON_COMMITTEE_KEY_SUMS_WARM_SECONDS: LazyLock = LazyLo 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] + vec![0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0] ) .unwrap() }); diff --git a/docs/metrics.md b/docs/metrics.md index 210b5532b..a63c14561 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -558,7 +558,7 @@ leanMetrics spec. |------|------|-------|-------------------------|--------|---------| | `lean_beacon_epoch_precompute_lookups_total` | Counter | Epoch-crossing imports, by whether a precomputed boundary state was cached | On each import whose parent is before the block's epoch start and whose block is at or after it | 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 | +| `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 From 7ecb3659bf680d0ce5d366ed9a3700fb901da02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:21:22 -0300 Subject: [PATCH 33/47] perf(crypto): sum validated public keys through blst's batched affine addition A committee's key sum is the largest cost left in verifying an attestation once its keys are cached. Folding the keys one by one through AggregatePublicKey::aggregate pays a projective addition per key; blst_p1s_add (reached through the safe MultiPoint::add) shares one field inversion across the batch and runs 1.6-2.3x faster in a single-threaded measurement. From 384 points up blst also spreads the work over its own thread pool. fast_aggregate_verify and eth_aggregate_pubkeys now sum through one private helper, sum_validated_points, which guards the empty slice (MultiPoint::add indexes the first element). The helper returns the same group element as the sequential sum, including for repeated keys (sync committees are drawn with replacement, so P + P occurs), which the new tests check at sizes around the 384-point threshold. Accepted and rejected inputs are unchanged. --- crates/common/crypto/src/bls.rs | 146 ++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 8 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index fb9dd2005..edc304417 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -96,8 +96,8 @@ use std::collections::HashMap; use std::sync::{LazyLock, RwLock}; -use blst::BLST_ERROR; -use blst::min_pk::{AggregatePublicKey, AggregateSignature, PublicKey, Signature}; +use blst::min_pk::{AggregateSignature, PublicKey, Signature}; +use blst::{BLST_ERROR, MultiPoint}; use rayon::prelude::*; use ethlambda_types::beacon::error::{Error, Result}; @@ -261,6 +261,35 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { points.into_iter().collect() } +/// The sum of `points` as one public key, 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 +/// through blst's `MultiPoint::add`, which hands the whole slice to +/// `blst_p1s_add`: a batched affine addition that shares one field inversion +/// across many additions, instead of paying a projective addition per key the +/// way folding the keys one by one through blst's `AggregatePublicKey::aggregate` +/// 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 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 +/// order of additions differs, and the group is commutative. +/// +/// From 384 points up, blst splits the work over its own thread pool, which is +/// separate from rayon's; below that it runs on the calling thread. Either way +/// the answer is the same, 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. +fn sum_validated_points(points: &[PublicKey]) -> Option { + if points.is_empty() { + return None; + } + Some(points.add().to_public_key()) +} + /// Builds `specs/altair/bls.md`'s `G2_POINT_AT_INFINITY` constant: the /// compressed encoding of the identity element of G2, which is the /// specification's sentinel value for "no one signed anything". @@ -378,8 +407,13 @@ pub fn fast_aggregate_verify( let Some(points) = validated_pubkeys(pubkeys) else { return false; }; - let point_refs: Vec<&PublicKey> = points.iter().collect(); - let result = signature.fast_aggregate_verify(false, message.as_slice(), DST, &point_refs); + // `pubkeys` is non-empty, so there is always a sum. 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 } @@ -397,12 +431,10 @@ pub fn eth_aggregate_pubkeys(pubkeys: &[BlsPubkey]) -> Result { let points = validated_pubkeys(pubkeys).ok_or(Error::SpecAssert( "all(bls.KeyValidate(pubkey) for pubkey in pubkeys)", ))?; - let point_refs: Vec<&PublicKey> = points.iter().collect(); // Every point above already passed `key_validate`, so the sum skips // re-checking them. - let aggregated = AggregatePublicKey::aggregate(&point_refs, false) - .map_err(|_| Error::SpecAssert("len(pubkeys) > 0"))?; - Ok(BlsPubkey(aggregated.to_public_key().to_bytes())) + let aggregated = sum_validated_points(&points).ok_or(Error::SpecAssert("len(pubkeys) > 0"))?; + Ok(BlsPubkey(aggregated.to_bytes())) } /// `specs/altair/bls.md`'s @@ -442,6 +474,7 @@ pub fn key_validate(pubkey: &BlsPubkey) -> bool { #[cfg(test)] mod tests { use super::*; + use blst::min_pk::AggregatePublicKey; #[test] fn key_validate_rejects_the_all_zero_pubkey() { @@ -544,6 +577,103 @@ mod tests { } } + /// The sequential sum `sum_validated_points` must equal, through blst's + /// own `AggregatePublicKey::aggregate`. + fn sequential_sum(points: &[PublicKey]) -> [u8; BLS_PUBKEY_SIZE] { + let refs: Vec<&PublicKey> = points.iter().collect(); + AggregatePublicKey::aggregate(&refs, false) + .expect("a non-empty run of valid points sums") + .to_public_key() + .to_bytes() + } + + fn validated_run(seeds: impl Iterator) -> Vec { + let pubkeys: Vec = seeds.map(fresh_pubkey).collect(); + validated_pubkeys(&pubkeys).expect("every key above is valid") + } + + #[test] + fn the_batched_sum_of_nothing_is_none() { + assert!(sum_validated_points(&[]).is_none()); + } + + #[test] + fn the_batched_sum_matches_the_sequential_one_at_every_size() { + // One point, a pair, then both sides of blst's 384-point thread-pool + // threshold (and the first multi-chunk size), then a large run. + for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { + let points = validated_run(1000..1000 + count); + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} distinct points" + ); + } + } + + #[test] + fn the_batched_sum_doubles_equal_points() { + let one = validated_run(5000..5001)[0]; + let pair = [one, one]; + assert_eq!( + sum_validated_points(&pair).map(|sum| sum.to_bytes()), + Some(sequential_sum(&pair)), + "P + P is a doubling" + ); + + // Several repeats scattered through a run, the shape of a committee + // drawn with replacement, at sizes either side of the threshold. + for count in [10u64, 400, 1000] { + let distinct = validated_run(6000..6000 + count); + let mut points = Vec::new(); + for (index, point) in distinct.iter().enumerate() { + points.push(*point); + if index % 3 == 0 { + points.push(*point); + } + if index % 7 == 0 { + points.push(distinct[0]); + } + } + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} keys with repeats" + ); + } + } + + #[test] + fn fast_aggregate_verify_with_a_repeated_signer_still_verifies() { + // Two signatures from the same key over one message aggregate to twice + // its signature, which only verifies if the doubled key sum is right. + let message = ethlambda_types::beacon::primitives::H256([9u8; 32]); + let mut ikm = [0x33u8; 32]; + ikm[..8].copy_from_slice(&7u64.to_le_bytes()); + let secret = blst::min_pk::SecretKey::key_gen(&ikm, &[]).unwrap(); + let pubkey = BlsPubkey(secret.sk_to_pk().to_bytes()); + let one = BlsSignature(secret.sign(message.as_slice(), DST, &[]).to_bytes()); + let signature = aggregate(&[one, one]).unwrap(); + assert!(fast_aggregate_verify( + &[pubkey, pubkey], + message, + &signature + )); + assert!(!fast_aggregate_verify(&[pubkey], message, &signature)); + } + + #[test] + fn eth_aggregate_pubkeys_matches_the_sequential_sum_with_repeats() { + let mut pubkeys: Vec = (30..40).map(fresh_pubkey).collect(); + pubkeys.push(pubkeys[2]); + pubkeys.push(pubkeys[2]); + let points = validated_pubkeys(&pubkeys).unwrap(); + assert_eq!( + eth_aggregate_pubkeys(&pubkeys).unwrap().0, + sequential_sum(&points) + ); + } + #[test] fn the_cache_stops_growing_at_its_capacity() { // One entry per shard. From ed8f09c7da1c2804352edc611f0dace745020666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:17:32 -0300 Subject: [PATCH 34/47] test(crypto): cover keys that cancel in the batched sum A key and its negation are both valid, so a registry can hold both and an aggregate can name both. Pin that the batched sum still matches the sequential one when keys cancel, that such a pair aggregates to the identity, and that the identity never verifies. Also state the CPU-count condition on blst's pool. --- crates/common/crypto/src/bls.rs | 79 ++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index edc304417..066ace760 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -277,9 +277,12 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// twice) and returns the same group element as the sequential sum; only the /// order of additions differs, and the group is commutative. /// -/// From 384 points up, blst splits the work over its own thread pool, which is -/// separate from rayon's; below that it runs on the calling thread. Either way -/// the answer is the same, so the caller needs no size check. +/// From 384 points up, on a host with two or more CPUs, blst splits the work +/// over its own thread pool, which is separate from rayon's; below that, or on +/// one CPU, it runs on the calling thread. The pool is the one blst's pairing +/// already ran on before this helper existed, so the sums join the same queue +/// rather than adding threads. Either way the answer is the same, 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. @@ -577,8 +580,8 @@ mod tests { } } - /// The sequential sum `sum_validated_points` must equal, through blst's - /// own `AggregatePublicKey::aggregate`. + /// The sequential sum, through blst's own `AggregatePublicKey::aggregate`: + /// what `sum_validated_points` must equal. fn sequential_sum(points: &[PublicKey]) -> [u8; BLS_PUBKEY_SIZE] { let refs: Vec<&PublicKey> = points.iter().collect(); AggregatePublicKey::aggregate(&refs, false) @@ -600,7 +603,11 @@ mod tests { #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool - // threshold (and the first multi-chunk size), then a large run. + // threshold, then larger runs. Which side of the threshold runs also + // depends on the host: on one CPU everything stays on the calling + // thread. On more, every pool worker's chunk stays below + // `blst_p1s_add`'s scratch stride, so that inner loop is never + // reached through the safe API. for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { let points = validated_run(1000..1000 + count); assert_eq!( @@ -662,6 +669,66 @@ mod tests { assert!(!fast_aggregate_verify(&[pubkey], message, &signature)); } + /// `-P`, from `P`'s compressed encoding: flipping the sort flag (bit 5 of + /// the first byte) picks the other `y` for the same `x`. Both are valid + /// keys, so a registry can hold both: whoever holds `P`'s secret signs + /// `-P`'s deposit with its negation. + fn negated(pubkey: BlsPubkey) -> BlsPubkey { + let mut bytes = pubkey.0; + bytes[0] ^= 0b0010_0000; + BlsPubkey(bytes) + } + + #[test] + fn a_key_and_its_negation_sum_to_the_identity_and_never_verify() { + let pubkey = fresh_pubkey(7000); + let pair = [pubkey, negated(pubkey)]; + assert!(key_validate(&pair[1]), "-P is a valid key"); + + // Both paths sum to the identity, and `eth_aggregate_pubkeys` hands it + // back as the spec's own pseudocode would. + let points = validated_pubkeys(&pair).unwrap(); + assert_eq!(sequential_sum(&points), identity_pubkey().0); + assert_eq!(eth_aggregate_pubkeys(&pair).unwrap(), identity_pubkey()); + + // The identity's signature is G2's identity, which blst refuses to pair + // against an identity key. + let message = ethlambda_types::beacon::primitives::H256([5u8; 32]); + let signature = BlsSignature(G2_POINT_AT_INFINITY); + assert!(!fast_aggregate_verify(&pair, message, &signature)); + assert!(!eth_fast_aggregate_verify(&pair, message, &signature)); + } + + #[test] + fn the_batched_sum_matches_the_sequential_one_when_keys_cancel() { + // Cancelling pairs next to each other, and two halves that cancel each + // other, either side of the thread-pool threshold. + for count in [8u64, 200, 400, 1100] { + let pubkeys: Vec = (8000..8000 + count).map(fresh_pubkey).collect(); + + let adjacent: Vec = pubkeys + .iter() + .flat_map(|&pubkey| [pubkey, negated(pubkey)]) + .chain([pubkeys[0]]) + .collect(); + let halves: Vec = pubkeys + .iter() + .copied() + .chain(pubkeys.iter().map(|&pubkey| negated(pubkey))) + .chain([pubkeys[1]]) + .collect(); + + for (shape, run) in [("adjacent", adjacent), ("halves", halves)] { + let points = validated_pubkeys(&run).unwrap(); + assert_eq!( + sum_validated_points(&points).map(|sum| sum.to_bytes()), + Some(sequential_sum(&points)), + "{count} cancelling pairs, {shape}" + ); + } + } + } + #[test] fn eth_aggregate_pubkeys_matches_the_sequential_sum_with_repeats() { let mut pubkeys: Vec = (30..40).map(fresh_pubkey).collect(); From 1eaf1942999b151e28c9d1a8e77c821fe78b5fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:37:03 -0300 Subject: [PATCH 35/47] docs(crypto): say where fast_aggregate_verify's non-empty key list comes from The comment credited the sum's existence to `pubkeys` being non-empty without saying what guarantees it. It is the early return at the top of the function, not `validated_pubkeys`, which maps an empty input to an empty `Some`. --- crates/common/crypto/src/bls.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 066ace760..a06690cec 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -410,8 +410,11 @@ pub fn fast_aggregate_verify( let Some(points) = validated_pubkeys(pubkeys) else { return false; }; - // `pubkeys` is non-empty, so there is always a sum. The signature was - // validated above, which is why the group check stays off here. + // `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; }; From 4e4fe13b9cd4aa185d570c6e26edfa05cfe069bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:37:07 -0300 Subject: [PATCH 36/47] test(crypto): fast_aggregate_verify rejects an empty key list Nothing pinned the early return that keeps an empty list away from the batched sum. The identity signature is used because it is the one input `eth_fast_aggregate_verify` accepts for an empty list, so the test also shows the two functions part ways there. --- crates/common/crypto/src/bls.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index a06690cec..6d265a3f4 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -603,6 +603,16 @@ mod tests { assert!(sum_validated_points(&[]).is_none()); } + #[test] + fn fast_aggregate_verify_rejects_an_empty_key_list() { + // Even with the identity signature, which is the one input the eth2 + // wrapper accepts for an empty list. + let message = ethlambda_types::beacon::primitives::H256([3u8; 32]); + let signature = BlsSignature(G2_POINT_AT_INFINITY); + assert!(!fast_aggregate_verify(&[], message, &signature)); + assert!(eth_fast_aggregate_verify(&[], message, &signature)); + } + #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool From be952d1b5369d964c5e069648c452f48907bc5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:16:35 -0300 Subject: [PATCH 37/47] perf(crypto): take a single validated key as its own sum From the second automated review round: - sum_validated_points returns a one-key slice's key as is, skipping blst_p1s_add and its conversion back to affine coordinates for an aggregate with one signer. - MultiPoint::add is called by its trait name, so it does not read like std::ops::Add. - The thread-pool paragraph is tied to blst 0.3.16 and shortened, since its 384-point threshold is that version's. - The size test's comment says plainly which paths a host exercises: one CPU stays on the calling thread and takes two 1024-point scratch passes at 2000 points; two or more split the work into chunks of about 256. --- crates/common/crypto/src/bls.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index 6d265a3f4..58b335dc0 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -277,20 +277,21 @@ fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option> { /// twice) and returns the same group element as the sequential sum; only the /// order of additions differs, and the group is commutative. /// -/// From 384 points up, on a host with two or more CPUs, blst splits the work -/// over its own thread pool, which is separate from rayon's; below that, or on -/// one CPU, it runs on the calling thread. The pool is the one blst's pairing -/// already ran on before this helper existed, so the sums join the same queue -/// rather than adding threads. Either way the answer is the same, so the -/// caller needs no size check. +/// In blst 0.3.16, from 384 points up on a host with two or more CPUs, the sum +/// is split over blst's own thread pool (not rayon's; the one its pairing +/// already runs on), and below that it runs on the calling thread. The answer +/// 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. +/// 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 +/// conversion back to affine coordinates. fn sum_validated_points(points: &[PublicKey]) -> Option { - if points.is_empty() { - return None; + match points { + [] => None, + [single] => Some(*single), + _ => Some(MultiPoint::add(points).to_public_key()), } - Some(points.add().to_public_key()) } /// Builds `specs/altair/bls.md`'s `G2_POINT_AT_INFINITY` constant: the @@ -616,11 +617,11 @@ mod tests { #[test] fn the_batched_sum_matches_the_sequential_one_at_every_size() { // One point, a pair, then both sides of blst's 384-point thread-pool - // threshold, then larger runs. Which side of the threshold runs also - // depends on the host: on one CPU everything stays on the calling - // thread. On more, every pool worker's chunk stays below - // `blst_p1s_add`'s scratch stride, so that inner loop is never - // reached through the safe API. + // threshold, then larger runs. Which paths run depends on the host. On + // one CPU everything stays on the calling thread, and the 2000-point + // run takes two passes through `blst_p1s_add`'s 1024-point scratch + // buffer. On two or more, the pool hands each worker about 256 points, + // so the threaded split runs instead and no sum takes a second pass. for count in [1u64, 2, 3, 17, 383, 384, 385, 767, 2000] { let points = validated_run(1000..1000 + count); assert_eq!( From 477ca69d398811bdfc6e1043e29f4bac74ff791f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:44:32 -0300 Subject: [PATCH 38/47] fix(beacon): check pending signatures before a validator record or the RANDAO mix changes A deferred signature let its operation write before the signature was known to be good, and every later operation ran on that write. That is now confined to votes: 1. Proposer and attester slashings, voluntary exits and BLS-to-execution changes call SignatureBatch::verify_pending before they write, so their own sets and every set still pending are checked first. 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 forged one is refused at its own operation. 2. The RANDAO reveal is checked the same way before it is mixed in, the specification's order. In gloas the builder's bid, pushed just before it, is checked in the same batch. 3. A failed batch is bisected to its first failing set instead of re-checking every set on its own: fewer set checks than the batch had, at one final exponentiation per halving, rather than a full pairing per set on top of the batch. Attestations, the sync aggregate and gloas's payload attestations still go into one batch, checked at the next of those points or at the end. --- .../state_transition/src/beacon/stf/block.rs | 101 ++++++-- .../src/beacon/stf/capella.rs | 2 + .../state_transition/src/beacon/stf/deneb.rs | 2 + .../src/beacon/stf/electra.rs | 4 + .../state_transition/src/beacon/stf/gloas.rs | 2 + .../src/beacon/stf/operations.rs | 11 +- .../src/beacon/stf/signatures.rs | 228 +++++++++++++----- 7 files changed, 271 insertions(+), 79 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/block.rs b/crates/blockchain/state_transition/src/beacon/stf/block.rs index d04a23f08..2777d5536 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/block.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/block.rs @@ -45,12 +45,15 @@ use super::{ExecutionEngine, bellatrix, capella, deneb, electra, fulu, gloas}; /// `signed_block`'s slot. /// /// Means what it always did, that the block is valid: it builds the -/// [`SignatureBatch`] the drivers push into and checks it before returning -/// `Ok`, so a caller needs no knowledge of it. +/// [`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, as it always could: an operation -/// whose signature is deferred has already written when a bad signature is -/// found at the end. Every caller discards the state after an error. +/// 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, @@ -75,7 +78,8 @@ pub fn process_block( } /// [`process_block`]'s dispatch: every signature the block's processing needs -/// is pushed into `signatures`, which [`process_block`] then verifies. +/// 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. @@ -333,9 +337,13 @@ pub enum RandaoVerification { Skip, } -/// Defers the check of the proposer's RANDAO reveal (unless `verification` -/// skips it) to `signatures`, and mixes the reveal 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 @@ -358,6 +366,7 @@ pub fn process_randao( 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)); @@ -482,36 +491,90 @@ mod tests { 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_deferred_and_rejected_by_the_batch() { + 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(); - // Nothing is checked while processing; the batch is what rejects. - process_randao( + let err = process_randao( &mut state, &reveal, RandaoVerification::Verify, &mut signatures, ) - .unwrap(); - let err = signatures.verify().unwrap_err(); + .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_passes_the_batch() { + 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 domain = get_domain(&state, constants::DOMAIN_RANDAO, None); - let signing_root = compute_signing_root(epoch.hash_tree_root(), domain); - let reveal = reveal_over(&state, signing_root); + let reveal = valid_reveal(&state); + let expected = xor(get_randao_mix(&state, epoch), hash(&reveal.0)); + let mut signatures = SignatureBatch::new(); - checked(|s| process_randao(&mut state, &reveal, RandaoVerification::Verify, s)).unwrap(); + 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] diff --git a/crates/blockchain/state_transition/src/beacon/stf/capella.rs b/crates/blockchain/state_transition/src/beacon/stf/capella.rs index 1f86775f3..97a3d20c4 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/capella.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/capella.rs @@ -454,6 +454,8 @@ pub fn process_bls_to_execution_change( ), "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; diff --git a/crates/blockchain/state_transition/src/beacon/stf/deneb.rs b/crates/blockchain/state_transition/src/beacon/stf/deneb.rs index 8ad65d3c8..efaf0d370 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/deneb.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/deneb.rs @@ -734,6 +734,8 @@ pub fn process_voluntary_exit( ), "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 de9c2933e..3e1587872 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/electra.rs @@ -538,6 +538,8 @@ pub fn process_voluntary_exit( ), "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(state, voluntary_exit.validator_index, config)?; @@ -606,6 +608,8 @@ pub fn process_attester_slashing( 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, diff --git a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs index f11bdb0e5..922dae67e 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/gloas.rs @@ -1323,6 +1323,8 @@ pub fn process_attester_slashing( 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, diff --git a/crates/blockchain/state_transition/src/beacon/stf/operations.rs b/crates/blockchain/state_transition/src/beacon/stf/operations.rs index 8abe48f2f..de3b6b6c4 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/operations.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/operations.rs @@ -192,8 +192,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. Both signatures are pushed to `signatures`, which -/// the block's driver checks once processing ends. +/// 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 @@ -249,6 +251,7 @@ pub(crate) fn verify_proposer_slashing( "bls.Verify(proposer.pubkey, signing_root, signed_header.signature)", )?; } + signatures.verify_pending()?; Ok((proposer_index, current_epoch)) } @@ -348,6 +351,8 @@ pub fn process_attester_slashing( 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, @@ -712,6 +717,8 @@ pub fn process_voluntary_exit( ), "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/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs index 38fecfb4e..37bc35700 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -7,46 +7,67 @@ //! 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, pushes it here, and the block -//! driver verifies the whole [`SignatureBatch`] once processing ends. +//! 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 exactly when its failure rejects the block, since +//! 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 (`transition_block` in -//! `fork_choice.rs`) and never reads that clone after an error, so a state -//! left partly mutated by a block that is rejected a few steps later is never -//! observed. 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. +//! 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 lets an operation with a forged signature change the state the -//! rest of the block runs on (a forged slashing slashes before the sync -//! aggregate's rewards are paid), but it reaches no state a valid block could -//! not: what an operation does never depends on its signature, so the same -//! effects follow from the same operation genuinely signed (a validator that -//! did equivocate, an exit that was signed). Any panic a later operation hits -//! there is one a valid block can reach too. Every path that applies outside -//! input checks signatures (`validate_result` is true on import and in era -//! replay), and the proposer's signature is checked before the body runs. +//! 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: 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; and gloas's payload attestations, +//! which write nothing. 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 (in +//! gloas, the builder's bid, pushed just before it, is checked with it); +//! - 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`]), so it costs less than the batch did. //! //! # 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 -//! re-checks the sets one at a time in push order and reports the first that -//! fails, 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. +//! 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 rayon::prelude::*; use tracing::error; use crate::beacon::error::{Result, verify}; @@ -91,13 +112,14 @@ impl SignatureBatch { } /// Runs `f` with a batch that defers every set pushed to it, then verifies - /// them all: 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. + /// 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()?; + batch.verify_pending()?; Ok(out) } @@ -128,44 +150,99 @@ impl SignatureBatch { Ok(()) } - /// One batch check. On failure, re-checks the sets one by one and rejects - /// with the first failing set's `what` in push order, so the error names - /// the check the immediate path would have stopped at. + /// 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. /// - /// The re-checks run on rayon's pool, so a failing batch costs the batch - /// plus one round of single checks spread over the cores. That stays - /// bounded: only a block that is rejected pays it, the block's operation - /// limits cap how many sets it pushes, and on import `apply_block` checks - /// the proposer's signature (and the header its proposer index) before - /// this runs, so making such a block takes the slot proposer's key. - pub(crate) fn verify(self) -> Result<()> { + /// 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 less than 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"); - if verify_signature_sets(&self.sets) { - return Ok(()); - } - if let Some(first_bad) = self.sets.par_iter().position_first(|set| !set.verify()) { - return verify(false, self.labels[first_bad]); - } - // A batch of valid sets always passes, so a set should have failed - // above. None did, 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 = self.sets.len(), - "Signature batch failed but every set verifies on its own; rejecting the block" - ); - crate::metrics::inc_signature_batch_disagreements(); - let last = self.labels.last().copied().unwrap_or("block signatures"); - verify(false, last) + 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 has been deferred. + /// 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 a set should have failed + // above. None did, 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: for the ~130 sets of a full pre-electra block, +/// fewer than 130 Miller loops and 8 final exponentiations, where checking +/// every set on its own costs 130 of each. +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. @@ -227,6 +304,41 @@ mod tests { 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(); From 93b4566acb6d3b6f2ffdd3139f035aa484912e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:44:40 -0300 Subject: [PATCH 39/47] refactor(beacon): return no state from a failed transition_block transition_block returned the partly applied copy alongside the error, so nothing but convention kept a caller from reading a state that a rejected block had written to. It now returns Result, and SpeculativeTransition holds that Result, so a failed transition has no state to read. --- .../src/beacon/fork_choice.rs | 133 +++++++++--------- 1 file changed, 65 insertions(+), 68 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/fork_choice.rs b/crates/blockchain/state_transition/src/beacon/fork_choice.rs index f1b3b2c32..16808d029 100644 --- a/crates/blockchain/state_transition/src/beacon/fork_choice.rs +++ b/crates/blockchain/state_transition/src/beacon/fork_choice.rs @@ -1053,11 +1053,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( engine: &stf::ExecutionEngine, committees: &CommitteeCache, active_balances: &ActiveBalanceCache, -) -> (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,7 +1101,7 @@ 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, @@ -1106,26 +1109,24 @@ fn transition_block( engine, committees, active_balances, - ); - 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, - committees, - active_balances, - ) - }); - 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, + committees, + active_balances, + )?; + return Ok(state); }; if crosses_with_empty_slots { @@ -1134,23 +1135,19 @@ 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, - committees, - active_balances, - ) - }); - (state, outcome) + stf::apply_block( + &mut state, + signed_block, + validate_result, + config, + engine, + committees, + active_balances, + )?; + Ok(state) } /// The precomputed epoch-boundary state an import can resume from, if any. @@ -4934,8 +4931,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 { @@ -4968,20 +4965,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" ); } @@ -5086,7 +5083,7 @@ pub fn speculate_transition( .ok()?; let active_balances = store.active_balance_cache(); let engine = stf::ExecutionEngine::valid(); - let (state, outcome) = transition_block( + let transition = transition_block( store, &parent_state, parent_root, @@ -5101,8 +5098,7 @@ pub fn speculate_transition( block_root, parent_root, engine, - state, - outcome, + transition, }) } @@ -5287,11 +5283,11 @@ pub fn on_block_speculative( &active_balances, ) }; - 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(), }; @@ -5336,7 +5332,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 @@ -6631,7 +6627,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, @@ -6641,8 +6637,8 @@ mod tests { &stf::ExecutionEngine::valid(), &CommitteeCache::default(), &ActiveBalanceCache::default(), - ); - outcome.expect("applies"); + ) + .expect("applies"); post.apply_pending_mutations(); assert!( store @@ -7698,7 +7694,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) } @@ -7745,10 +7741,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"); @@ -11071,7 +11068,7 @@ mod tests { parent_root: Root, block: &SignedBeaconBlock, ) -> Root { - let (mut state, outcome) = transition_block( + let mut state = transition_block( store, parent, parent_root, @@ -11081,8 +11078,8 @@ mod tests { &stf::ExecutionEngine::valid(), &CommitteeCache::default(), &ActiveBalanceCache::default(), - ); - outcome.expect("the block applies"); + ) + .expect("the block applies"); state.apply_pending_mutations(); state.hash_tree_root() } From fa8c70ff7f7ad09c9b3922b4bf64f32a25860c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:44:52 -0300 Subject: [PATCH 40/47] refactor(beacon): drop block_signature_set Its one caller was verify_block_signature, and as public API it invited batching the proposer's signature again, which would undo the one-pairing rejection of an unsigned block apply_block relies on. verify_block_signature now calls verify_proposer_signature directly. --- .../state_transition/src/beacon/stf/mod.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/mod.rs b/crates/blockchain/state_transition/src/beacon/stf/mod.rs index 6685c2d60..f423b9407 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/mod.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/mod.rs @@ -215,21 +215,14 @@ 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, ) -> bool { - block_signature_set(state, signed_block).is_some_and(|set| set.verify()) -} - -/// The proposer's signature check [`verify_block_signature`] makes, resolved -/// but not yet run. `None` is a proposer index outside the registry, or a key -/// or signature that does not validate. -pub fn block_signature_set( - state: &BeaconState, - signed_block: &containers::SignedBeaconBlock, -) -> Option { - proposer_signature_set( + verify_proposer_signature( state, signed_block.proposer_index(), signed_block.message_hash_tree_root(), From b22918db8c3eb8968ebb4ac8f9c87a4f591b0feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:47:45 -0300 Subject: [PATCH 41/47] refactor(beacon): one definition of is_valid_indexed_attestation's checks Phase0, electra and gloas each had four functions for one check: is_valid_indexed_attestation, its _with_domain variant, and the two matching signature-set builders, with the structural rules copied into each fork's file. The rules now live once, in attestation::attesters_signature_set, which takes the attester list, data and signature of any fork's container and an optional domain. Each fork keeps is_valid_indexed_attestation and indexed_attestation_signature_set, both thin over it: six functions where there were twelve. The _with_domain pair is gone; gossip's aggregate validation, its only caller, calls attesters_signature_set with the schedule's domain. The shared check includes gloas's MAX_VALIDATORS_PER_SLOT bound. Every earlier fork's attester list is bounded at or below it by its own type, so the check changes no earlier fork's answer. --- .../src/beacon/gossip/aggregate.rs | 52 ++++----- .../src/beacon/helpers/attestation.rs | 104 ++++++++++++------ .../src/beacon/helpers/electra.rs | 77 +++---------- .../src/beacon/helpers/gloas.rs | 67 ++--------- 4 files changed, 126 insertions(+), 174 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs b/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs index d95914a61..b73b8e025 100644 --- a/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs +++ b/crates/blockchain/state_transition/src/beacon/gossip/aggregate.rs @@ -444,7 +444,7 @@ pub fn stateful_checks( // (cached) committees, so this costs no further shuffle. let attester_domain = get_domain_from_schedule(&config, &state, DOMAIN_BEACON_ATTESTER, target_epoch); - let attesting_indices = match aggregate { + let (attesting_indices, indexed_data, aggregate_signature) = match aggregate { SignedAggregateAndProof::Phase0(signed) => { let phase0_attestation = &signed.message.aggregate; let indexed = crate::beacon::helpers::attestation::get_indexed_attestation( @@ -453,14 +453,11 @@ pub fn stateful_checks( &committees, ) .map_err(|_| Outcome::Ignore(IgnoreReason::Internal))?; - if !crate::beacon::helpers::attestation::is_valid_indexed_attestation_with_domain( - &state, - &indexed, - attester_domain, - ) { - return Err(Outcome::Reject(RejectReason::AggregateSignature)); - } - indexed.attesting_indices.to_vec() + ( + indexed.attesting_indices.to_vec(), + indexed.data, + indexed.signature, + ) } SignedAggregateAndProof::Electra(signed) => { let electra_attestation = &signed.message.aggregate; @@ -470,14 +467,11 @@ pub fn stateful_checks( &committees, ) .map_err(|_| Outcome::Ignore(IgnoreReason::Internal))?; - if !crate::beacon::helpers::electra::is_valid_indexed_attestation_with_domain( - &state, - &indexed, - attester_domain, - ) { - return Err(Outcome::Reject(RejectReason::AggregateSignature)); - } - indexed.attesting_indices.to_vec() + ( + indexed.attesting_indices.to_vec(), + indexed.data, + indexed.signature, + ) } SignedAggregateAndProof::Gloas(signed) => { let gloas_attestation = &signed.message.aggregate; @@ -487,16 +481,24 @@ pub fn stateful_checks( &committees, ) .map_err(|_| Outcome::Ignore(IgnoreReason::Internal))?; - if !crate::beacon::helpers::gloas::is_valid_indexed_attestation_with_domain( - &state, - &indexed, - attester_domain, - ) { - return Err(Outcome::Reject(RejectReason::AggregateSignature)); - } - indexed.attesting_indices.to_vec() + ( + indexed.attesting_indices.to_vec(), + indexed.data, + indexed.signature, + ) } }; + // Every fork's `is_valid_indexed_attestation`, under the schedule's domain. + let signature_set = crate::beacon::helpers::attestation::attesters_signature_set( + &state, + &attesting_indices, + &indexed_data, + &aggregate_signature, + Some(attester_domain), + ); + if !signature_set.is_some_and(|set| set.verify()) { + return Err(Outcome::Reject(RejectReason::AggregateSignature)); + } // Ancestry, via the vote state's own history; see this function's // documentation for why this state can always answer both questions. diff --git a/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs b/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs index d52da7017..1f1b62ae5 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/attestation.rs @@ -13,11 +13,13 @@ 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::preset; +use crate::beacon::primitives::{BlsSignature, Domain, HashTreeRoot as _, ValidatorIndex}; /// The committee members whose bit is set in `attestation`, in ascending order. /// @@ -85,58 +87,92 @@ pub fn is_valid_indexed_attestation( /// 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. -/// -/// Every structural check [`is_valid_indexed_attestation`] makes (the -/// attester set's shape and range) happens here, at once: `None` means the -/// attestation is invalid without needing a pairing. Only the pairing is left -/// to the caller. +/// batch. See [`attesters_signature_set`] for the checks. pub fn indexed_attestation_signature_set( state: &BeaconState, indexed_attestation: &IndexedAttestation, ) -> Option { - let domain = get_domain( + attesters_signature_set( state, - constants::DOMAIN_BEACON_ATTESTER, - Some(indexed_attestation.data.target.epoch), - ); - indexed_attestation_signature_set_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( - state: &BeaconState, - indexed_attestation: &IndexedAttestation, - domain: Domain, -) -> bool { - indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) - .is_some_and(|set| set.verify()) -} - -/// [`indexed_attestation_signature_set`] under a signing domain the caller -/// chose. -pub fn indexed_attestation_signature_set_with_domain( +pub(crate) fn attesters_signature_set( state: &BeaconState, - indexed_attestation: &IndexedAttestation, - domain: Domain, + attesting_indices: &[ValidatorIndex], + data: &AttestationData, + signature: &BlsSignature, + domain: Option, ) -> Option { - let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; - if indices.is_empty() || !are_indices_sorted_and_unique(indices) { + 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 None, } } - let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - SignatureSet::fast_aggregate(&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 6e6daf9c2..872f91fa0 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/electra.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/electra.rs @@ -134,17 +134,15 @@ use crate::beacon::hash::hash; use crate::beacon::lean_state_unreachable; use crate::beacon::preset; use crate::beacon::primitives::{ - BLS_SIGNATURE_SIZE, BlsSignature, Bytes32, CommitteeIndex, Domain, Epoch, Gwei, - HashTreeRoot as _, Slot, ValidatorIndex, + BLS_SIGNATURE_SIZE, BlsSignature, Bytes32, CommitteeIndex, Epoch, Gwei, Slot, ValidatorIndex, }; use super::accessors::{ - CommitteeCache, CommitteeCacheExt, get_active_validator_indices, get_current_epoch, get_domain, - get_seed, get_total_active_balance, + CommitteeCache, CommitteeCacheExt, get_active_validator_indices, get_current_epoch, get_seed, + get_total_active_balance, }; use super::math::bytes_to_uint64; -use super::misc::{compute_activation_exit_epoch, compute_epoch_at_slot, compute_signing_root}; -use super::predicates::are_indices_sorted_and_unique; +use super::misc::{compute_activation_exit_epoch, compute_epoch_at_slot}; use super::shuffling::compute_shuffled_index; // --------------------------------------------------------------------------- @@ -271,11 +269,12 @@ 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, @@ -285,59 +284,19 @@ pub fn is_valid_indexed_attestation( /// 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. -/// -/// Every structural check [`is_valid_indexed_attestation`] makes (the -/// attester set's shape and range) happens here, at once: `None` means the -/// attestation is invalid without needing a pairing. Only the pairing is left -/// to the caller. +/// 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, ) -> Option { - let domain = get_domain( + super::attestation::attesters_signature_set( state, - constants::DOMAIN_BEACON_ATTESTER, - Some(indexed_attestation.data.target.epoch), - ); - indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) -} - -/// [`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( - state: &BeaconState, - indexed_attestation: &electra::IndexedAttestation, - domain: Domain, -) -> bool { - indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) - .is_some_and(|set| set.verify()) -} - -/// [`indexed_attestation_signature_set`] under a signing domain the caller -/// chose. -pub fn indexed_attestation_signature_set_with_domain( - state: &BeaconState, - indexed_attestation: &electra::IndexedAttestation, - domain: Domain, -) -> Option { - let indices: &[ValidatorIndex] = &indexed_attestation.attesting_indices; - if indices.is_empty() || !are_indices_sorted_and_unique(indices) { - 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 None, - } - } - - let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - SignatureSet::fast_aggregate(&pubkeys, signing_root, &indexed_attestation.signature) + &indexed_attestation.attesting_indices, + &indexed_attestation.data, + &indexed_attestation.signature, + None, + ) } // --------------------------------------------------------------------------- diff --git a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs index 7b3702f1b..7beb0516f 100644 --- a/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs +++ b/crates/blockchain/state_transition/src/beacon/helpers/gloas.rs @@ -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,8 +116,8 @@ 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, @@ -129,62 +127,19 @@ pub fn is_valid_indexed_attestation( /// 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. -/// -/// Every structural check [`is_valid_indexed_attestation`] makes (the -/// attester set's shape and range) happens here, at once: `None` means the -/// attestation is invalid without needing a pairing. Only the pairing is left -/// to the caller. +/// 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, ) -> Option { - let domain = get_domain( + super::attestation::attesters_signature_set( state, - constants::DOMAIN_BEACON_ATTESTER, - Some(indexed_attestation.data.target.epoch), - ); - indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) -} - -/// [`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( - state: &BeaconState, - indexed_attestation: &gloas::IndexedAttestation, - domain: Domain, -) -> bool { - indexed_attestation_signature_set_with_domain(state, indexed_attestation, domain) - .is_some_and(|set| set.verify()) -} - -/// [`indexed_attestation_signature_set`] under a signing domain the caller -/// chose. -pub fn indexed_attestation_signature_set_with_domain( - state: &BeaconState, - indexed_attestation: &gloas::IndexedAttestation, - domain: Domain, -) -> Option { - 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 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 None, - } - } - - let signing_root = compute_signing_root(indexed_attestation.data.hash_tree_root(), domain); - SignatureSet::fast_aggregate(&pubkeys, signing_root, &indexed_attestation.signature) + &indexed_attestation.attesting_indices, + &indexed_attestation.data, + &indexed_attestation.signature, + None, + ) } // --------------------------------------------------------------------------- From 619e202780e5057a91f87e1e7d62eff8d421af6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:51:32 -0300 Subject: [PATCH 42/47] refactor(crypto): drop the batch's redundant identity-key check key_is_identity changed no behavior, by its own documentation: blst refuses an infinite public key wherever it adds one to a pairing (BLST_PK_IS_INFINITY in blst/src/aggregate.c), on the batch path and the single path alike, and the single-set arm returned before the check anyway. The tests that pin it stay: [P, -P] fails alone and inside a batch, with the identity signature and with a real one. Also states what the random weights' soundness rests on. rand::rng() is a ChaCha12 generator seeded from the operating system; rand 0.10.1, the locked version, still reseeds it every 64 KiB, but the argument does not need that. --- crates/common/crypto/src/bls/batch.rs | 45 +++++++++++---------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs index 3fad3583b..9a920336e 100644 --- a/crates/common/crypto/src/bls/batch.rs +++ b/crates/common/crypto/src/bls/batch.rs @@ -22,9 +22,12 @@ //! 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`'s thread-local CSPRNG, which is seeded from the -//! operating system's entropy and reseeded periodically, and a weight of zero -//! is redrawn, since it would drop its set from the equation altogether. +//! 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 //! @@ -131,22 +134,6 @@ impl SignatureSet { }; result == BLST_ERROR::BLST_SUCCESS } - - /// Whether the key is the identity, which no signature can validly be - /// made under. Only a sum of keys can be: a single registry key is - /// rejected by `key_validate` if it is. - /// - /// blst rejects the same input itself (`BLST_PK_IS_INFINITY`, from the - /// pairing aggregation both the single and the batch check go through), - /// so this makes the existing rule explicit and changes no behavior. - /// - /// Read off the affine point, with no serialization: blst stores the - /// identity as the all-zero point, which is `PublicKey::default()`, and its - /// `PartialEq` compares those bytes (the same test as its - /// `blst_p1_affine_is_inf`, without the `unsafe` call). - fn key_is_identity(&self) -> bool { - self.aggregated && self.pubkey == PublicKey::default() - } } /// A random weight in `1..2^64`, as the little-endian scalar `blst` expects. @@ -168,18 +155,19 @@ fn random_weight(rng: &mut impl RngExt) -> blst_scalar { /// /// 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 fails the batch, as it fails -/// [`SignatureSet::verify`]. blst rejects that input itself, so the explicit -/// check only states the rule where a reader of the batch looks for it. +/// 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(), _ => {} } - if sets.iter().any(SignatureSet::key_is_identity) { - return false; - } 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(); @@ -428,7 +416,10 @@ mod tests { 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.key_is_identity()); + assert!( + set.pubkey == PublicKey::default(), + "the keys sum to the identity" + ); assert!(!set.verify()); assert_eq!( set.verify(), @@ -443,7 +434,7 @@ mod tests { // `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.key_is_identity()); + assert!(set.pubkey != PublicKey::default()); let mut sets = valid_sets(3); sets.push(set); assert!(verify_signature_sets(&sets)); From 01e0024757eb350847937de2bb829ddd06c164f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:51:57 -0300 Subject: [PATCH 43/47] refactor(crypto): build the bls predicates on SignatureSet bls::verify and bls::fast_aggregate_verify each repeated the validate, sum and pair sequence SignatureSet holds, so only tests kept gossip and fork choice deciding a signature the way block import does. They are now SignatureSet::single and SignatureSet::fast_aggregate followed by its verify. eth_fast_aggregate_verify's empty-participants rule (valid exactly with G2_POINT_AT_INFINITY) was written twice: in bls, which block production's sync-aggregate check calls, and inline in altair's process_sync_aggregate. It now lives in SignatureSet::eth_fast_aggregate, which answers Valid, Invalid, or the set whose pairing decides. bls::eth_fast_aggregate_verify is that answer checked now, and the sync aggregate pushes the set or rejects from it. --- .../state_transition/src/beacon/stf/altair.rs | 40 +++--- crates/common/crypto/src/bls.rs | 46 ++----- crates/common/crypto/src/bls/batch.rs | 119 +++++++++++++++++- 3 files changed, 144 insertions(+), 61 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/altair.rs b/crates/blockchain/state_transition/src/beacon/stf/altair.rs index d8ebc3759..601365383 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::{self, SignatureSet}; +use crate::beacon::bls::{EthFastAggregate, SignatureSet}; use crate::beacon::constants; use crate::beacon::containers::{BeaconState, altair, phase0}; use crate::beacon::error::{Error, Result, verify}; @@ -285,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 @@ -340,24 +341,18 @@ 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); - // `eth_fast_aggregate_verify`'s one carve-out: with no participants the - // signature must be the point at infinity, and there is nothing to - // pair, so that case is decided here and defers nothing. + // `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)"; - if participant_pubkeys.is_empty() { - verify( - sync_aggregate.sync_committee_signature.as_ref() == bls::G2_POINT_AT_INFINITY, - eth_fast_aggregate_verify, - )?; - } else { - signatures.push( - SignatureSet::fast_aggregate( - &participant_pubkeys, - signing_root, - &sync_aggregate.sync_committee_signature, - ), - eth_fast_aggregate_verify, - )?; + 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 @@ -444,6 +439,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}; diff --git a/crates/common/crypto/src/bls.rs b/crates/common/crypto/src/bls.rs index e80139a4f..131e0fa05 100644 --- a/crates/common/crypto/src/bls.rs +++ b/crates/common/crypto/src/bls.rs @@ -106,7 +106,7 @@ use ethlambda_types::beacon::primitives::{ }; mod batch; -pub use batch::{SignatureSet, verify_signature_sets}; +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 @@ -326,15 +326,11 @@ pub const G2_POINT_AT_INFINITY: [u8; BLS_SIGNATURE_SIZE] = g2_point_at_infinity( /// here rather than panicking or returning an error: an unparseable or /// invalid-point key is treated exactly like a valid key over the wrong /// signature, since the specification never distinguishes the two. +/// +/// [`SignatureSet::single`] and its [`SignatureSet::verify`], so this and a +/// block's batched check of the same signature are one definition. pub fn verify(pubkey: &BlsPubkey, message: Root, signature: &BlsSignature) -> bool { - let Some(pubkey) = validated_pubkey(pubkey) else { - return false; - }; - let Ok(signature) = Signature::sig_validate(signature.as_ref(), false) else { - return false; - }; - let result = signature.verify(false, message.as_slice(), DST, &[], &pubkey, false); - result == BLST_ERROR::BLST_SUCCESS + SignatureSet::single(pubkey, message, signature).is_some_and(|set| set.verify()) } /// The specification's `bls.Aggregate(signatures) -> BLSSignature`. @@ -400,31 +396,15 @@ pub fn aggregate_verify( /// Electra block's attestations, where a single aggregate can carry /// thousands of signers behind one shared message; keys go through /// [`PubkeyCache`], see the module documentation for why that is safe. +/// +/// [`SignatureSet::fast_aggregate`] and its [`SignatureSet::verify`], so this +/// and a block's batched check of the same aggregate are one definition. pub fn fast_aggregate_verify( pubkeys: &[BlsPubkey], message: Root, signature: &BlsSignature, ) -> bool { - if pubkeys.is_empty() { - return false; - } - let Ok(signature) = Signature::sig_validate(signature.as_ref(), false) else { - return false; - }; - let Some(points) = validated_pubkeys(pubkeys) else { - return false; - }; - // `points` is non-empty because of the early return on an empty `pubkeys` - // at the top, not because of `validated_pubkeys` (which maps an empty - // input to an empty `Some`), so the `None` arm is unreachable. The - // signature was validated above, which is why the group check stays off - // here. - let Some(aggregated) = sum_validated_points(&points) else { - return false; - }; - let result = - signature.fast_aggregate_verify_pre_aggregated(false, message.as_slice(), DST, &aggregated); - result == BLST_ERROR::BLST_SUCCESS + SignatureSet::fast_aggregate(pubkeys, message, signature).is_some_and(|set| set.verify()) } /// `specs/altair/bls.md`'s `eth_aggregate_pubkeys(pubkeys) -> BLSPubkey`. @@ -459,15 +439,15 @@ pub fn eth_aggregate_pubkeys(pubkeys: &[BlsPubkey]) -> Result { /// the identity element by convention rather than "no signature was /// provided"; [`fast_aggregate_verify`] itself has no such case, since the /// underlying IETF ciphersuite it wraps was never given one. +/// +/// [`SignatureSet::eth_fast_aggregate`] with its pairing run now, which is +/// where that rule is written down once. pub fn eth_fast_aggregate_verify( pubkeys: &[BlsPubkey], message: Root, signature: &BlsSignature, ) -> bool { - if pubkeys.is_empty() { - return signature.as_ref() == G2_POINT_AT_INFINITY; - } - fast_aggregate_verify(pubkeys, message, signature) + SignatureSet::eth_fast_aggregate(pubkeys, message, signature).verify() } /// The specification's `bls.KeyValidate(pubkey) -> bool`. diff --git a/crates/common/crypto/src/bls/batch.rs b/crates/common/crypto/src/bls/batch.rs index 9a920336e..7dc6cf84d 100644 --- a/crates/common/crypto/src/bls/batch.rs +++ b/crates/common/crypto/src/bls/batch.rs @@ -32,10 +32,12 @@ //! # 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 the sets individually afterwards -//! ([`SignatureSet::verify`]), which is also the answer a standalone -//! [`super::verify`] or [`super::fast_aggregate_verify`] gives for the same -//! inputs. +//! 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; @@ -44,7 +46,7 @@ use rand::RngExt; use ethlambda_types::beacon::primitives::{BlsPubkey, BlsSignature, Root}; -use super::{DST, validated_pubkey, validated_pubkeys}; +use super::{DST, G2_POINT_AT_INFINITY, validated_pubkey, validated_pubkeys}; /// How many bits of each random weight the batch check uses. const RAND_BITS: usize = 64; @@ -112,8 +114,36 @@ impl SignatureSet { }) } + /// `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. + /// [`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( @@ -136,6 +166,34 @@ impl SignatureSet { } } +/// 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 { @@ -440,6 +498,55 @@ mod tests { 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(); From 0521d9d2a454e19b3b7002549d7eadf9e67828f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:52:28 -0300 Subject: [PATCH 44/47] refactor(state-transition): register the batch disagreement counter at module scope The state transition's metrics are module-scoped LazyLock statics, as CLAUDE.md prefers; this was the one function-scoped. Its help text and docs/metrics.md now say what it counts since the failing set is found by bisection: a failed batch whose narrowed-to set verifies alone. --- .../state_transition/src/metrics.rs | 24 ++++++++++--------- docs/metrics.md | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/blockchain/state_transition/src/metrics.rs b/crates/blockchain/state_transition/src/metrics.rs index 1d47490f4..336b9b074 100644 --- a/crates/blockchain/state_transition/src/metrics.rs +++ b/crates/blockchain/state_transition/src/metrics.rs @@ -45,18 +45,20 @@ pub fn inc_finalizations(result: &str) { LEAN_FINALIZATIONS_TOTAL.with_label_values(&[result]).inc(); } -/// A block's signature batch failed while every set in it verified 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. +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() { - static LEAN_BEACON_SIGNATURE_BATCH_DISAGREEMENTS_TOTAL: LazyLock = - LazyLock::new(|| { - register_int_counter!( - "lean_beacon_signature_batch_disagreements_total", - "Signature batches that failed while every set verified on its own" - ) - .unwrap() - }); LEAN_BEACON_SIGNATURE_BATCH_DISAGREEMENTS_TOTAL.inc(); } diff --git a/docs/metrics.md b/docs/metrics.md index 639a3f42b..cb895e610 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -502,7 +502,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 while every set in them verified 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 no single set does | | +| `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 From 147d418a9295702c21fed79fe8bb8fa381d547c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:53:30 -0300 Subject: [PATCH 45/47] test(beacon): a credential change checks pending signatures before it writes Pins the ordering for one of the operations that change a validator's record: through a batch that defers, a forged BLS-to-execution change is refused by the operation itself, and a genuine one is refused under the earlier set's label while a forged set is pending, with the credentials untouched both times. With the signature deferred to the end of the block, the first call returned Ok and rewrote them. --- .../src/beacon/stf/capella.rs | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/crates/blockchain/state_transition/src/beacon/stf/capella.rs b/crates/blockchain/state_transition/src/beacon/stf/capella.rs index 97a3d20c4..964574348 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/capella.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/capella.rs @@ -821,6 +821,79 @@ mod tests { ); } + /// 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] fn a_credential_not_in_the_bls_form_is_rejected() { let config = Config::mainnet(); From 02dd69f29988250974d8304d2beaed0babc4fc47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:53:59 -0300 Subject: [PATCH 46/47] docs(beacon): say in apply_block which body signatures are checked before they write --- crates/blockchain/state_transition/src/beacon/stf/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/mod.rs b/crates/blockchain/state_transition/src/beacon/stf/mod.rs index f423b9407..3b1bafa2e 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/mod.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/mod.rs @@ -183,7 +183,9 @@ pub fn apply_block( // 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. + // 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), From af1671d44450b746739dd90b69712d57e6d7165b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:03:30 -0300 Subject: [PATCH 47/47] docs(beacon): name the gloas bid among the writes before a signature check The signatures module doc said deferral was confined to votes, but process_execution_payload_bid records the builder's payment and the bid before its signature is checked. It is checked right after, with the RANDAO reveal, before any operation runs, whenever the reveal is verified (always on import; block production skips it for its own block). The doc now says so. Also corrects the cost wording: a bisection costs about what the batch did, not less, and a lone check is a pairing with a Miller loop of its own for the signature, which is what a batch saves along with the final exponentiation. --- .../src/beacon/stf/signatures.rs | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs index 37bc35700..04f5be88f 100644 --- a/crates/blockchain/state_transition/src/beacon/stf/signatures.rs +++ b/crates/blockchain/state_transition/src/beacon/stf/signatures.rs @@ -25,16 +25,24 @@ //! //! 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: 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; and gloas's payload attestations, -//! which write nothing. Everything else checks its own set, together with -//! every set still pending, before it writes, through -//! [`SignatureBatch::verify_pending`]: +//! is confined to votes, and to one write nothing reads before its check: //! -//! - the RANDAO reveal, so the mix is never fed an unverified reveal (in -//! gloas, the builder's bid, pushed just before it, is checked with it); +//! - 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. //! @@ -53,7 +61,8 @@ //! 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`]), so it costs less than the batch did. +//! [`SignatureBatch::verify_pending`]): about the batch's cost again, where +//! the re-check cost a full pairing per set. //! //! # What a late failure names //! @@ -161,7 +170,7 @@ impl SignatureBatch { /// for it this costs nothing. /// /// A failed batch is bisected to find its first failing set (see - /// [`first_failing`]), which costs less than the batch did, rather than + /// [`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<()> { @@ -195,10 +204,11 @@ fn check(sets: &[SignatureSet], labels: &[&'static str]) -> Result<()> { if let Some(first_bad) = first_failing(sets) { return verify(false, labels[first_bad]); } - // A batch of valid sets always passes, so a set should have failed - // above. None did, 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. + // 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" @@ -216,9 +226,10 @@ fn check(sets: &[SignatureSet], labels: &[&'static str]) -> Result<()> { /// 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: for the ~130 sets of a full pre-electra block, -/// fewer than 130 Miller loops and 8 final exponentiations, where checking -/// every set on its own costs 130 of each. +/// 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;