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/38] 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 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 02/38] 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 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 03/38] 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 04/38] 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 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 05/38] 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 06/38] 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 07/38] 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 08/38] 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 09/38] 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 10/38] 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 11/38] 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 12/38] 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 13/38] 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 14/38] 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 15/38] 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 16/38] 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 17/38] 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 18/38] 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 19/38] 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 20/38] 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 21/38] 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 22/38] 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 23/38] 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 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 24/38] 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 25/38] 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 26/38] 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 27/38] 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 28/38] 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 29/38] 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 30/38] 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 31/38] 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 32/38] 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 33/38] 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 34/38] 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 35/38] 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 36/38] 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 37/38] 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 38/38] 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;