Skip to content

perf(beacon): verify a block's signatures in one random-weight batch - #738

Merged
pablodeymo merged 58 commits into
mainfrom
perf/block-signature-batch
Nov 20, 2026
Merged

pablodeymo merged 58 commits into
mainfrom
perf/block-signature-batch

Conversation

@MegaRedHand

@MegaRedHand MegaRedHand commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Stack 2/4 of the BLS verification work: (a) batched key sums -> (b) per-block signature batch -> (c) committee key sums -> (d) index-keyed pubkey cache. Based on (a), which has merged (#737), so this now targets main.

What changed and why

Block import checked every signature with its own pairing: the proposer's, the RANDAO reveal, each attestation, the sync aggregate and any slashings, exits and BLS changes. A pairing check is dominated by its final exponentiation, which a batch pays once for all sets.

  • ethlambda-crypto: new bls/batch.rs with SignatureSet (single, fast_aggregate, eth_fast_aggregate, verify) and verify_signature_sets. A set is the resolved inputs of a check (validated and summed key, message, validated signature); a batch is verify_multiple_aggregate_signatures with 64-bit non-zero weights from rand::rng(), a ChaCha12 generator seeded from the operating system. Random weights are required: without them two invalid signatures can be crafted to cancel; with unpredictable 64-bit weights a forger succeeds with probability about 2^-64. A single set skips the weights. An identity key (keys summing to it) fails alone and in a batch because blst refuses infinite public keys on both paths.
  • bls::verify, bls::fast_aggregate_verify and bls::eth_fast_aggregate_verify are built on SignatureSet, so gossip, fork choice and block import decide a signature with one piece of code. SignatureSet::eth_fast_aggregate holds the empty-participants rule (valid exactly with G2_POINT_AT_INFINITY) once, answering Valid, Invalid, or the set whose pairing decides.
  • ethlambda-state-transition: new stf/signatures.rs with SignatureBatch. Operations build their set where the specification verifies it and push it with the same label the immediate check used. Outside the crate there are only two ways to get a batch, and both check every set: SignatureBatch::deferred(|batch| ...) verifies what is pending once the closure returns, and immediate() checks at push time (see below).
  • A failed batch is bisected to its first failing set in push order, and that set's label is the rejection: each step checks the first half of the range holding it, so the search checks fewer sets than the batch had, at one final exponentiation per step, rather than a pairing per set.
  • stf::block::process_block keeps its signature and still means "the block is valid": it runs the body inside SignatureBatch::deferred. apply_block checks the proposer's block signature on its own first, as before this PR: a block fetched by request reaches the STF without gossip's precheck_block, so a block its proposer never signed is still rejected after one pairing rather than after the whole body.
  • The indexed-attestation rules live once, in helpers::attestation::attesters_signature_set (non-empty, at most MAX_VALIDATORS_PER_SLOT, sorted and unique, in the registry). Phase0, electra and gloas each keep is_valid_indexed_attestation and indexed_attestation_signature_set over it; gossip's aggregate validation calls it with the schedule's domain.

Deferred, and checked before writing

A deferred signature lets its operation write before the signature is known to be good, so deferral is confined to votes, and to one write nothing reads before its check:

  • Deferred to the end of the block: attestations (phase0, altair, deneb, electra, gloas: participation flags and the proposer's inclusion reward), the sync aggregate (altair.rs, the last step of a block; no participants is decided without a pairing), gloas payload attestations (no writes).
  • Gloas execution payload bid: records the builder's payment before its check, and is checked with the RANDAO reveal right after, before any operation runs (whenever the reveal is verified, which import always does).
  • Checked with everything pending before the operation writes (SignatureBatch::verify_pending): the RANDAO reveal, before the mix; proposer slashing headers (operations.rs, shared by gloas); attester slashings (phase0, electra, gloas); voluntary exits (phase0, deneb, electra); BLS to execution changes (capella.rs).

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 of ordinary votes still checks all of its attestations and its sync aggregate in one batch.

Immediate, unchanged (failure changes state but does not reject): every deposit signature (operations.rs, electra.rs, epoch/electra.rs) and the gloas builder deposit signature. The gloas envelope signature is not reachable from process_block. Immediate, as on main: the proposer's block signature (apply_block).

Behavior notes

  • Every accepted input is still accepted and every rejected one rejected. A rejected block reports the first failing signature's label, but another defect later in the block may be reported first or instead.
  • An Err from process_block leaves the state partly applied, as any mid-block failure did, now including an attestation or sync aggregate whose signature turns out bad at the end. transition_block (fork_choice.rs) returns Result<BeaconState>, so no caller can read a failed transition's state.
  • The operation packer in block production applies candidates in place and relies on a rejected candidate writing nothing. It therefore passes SignatureBatch::immediate(), which checks at push time.
  • gloas::process_block_and_verify is the helper for the direct gloas drivers in block production.

Cost of a forged block

Rejecting a block whose body carries a bad signature costs the processing up to the check that catches it, that batch, and the bisection.

With immediate checks a forger already got a whole body processed, by putting the bad signature on the sync aggregate, the last one checked. That cost every operation plus n standalone checks, each two Miller loops and a final exponentiation.

Now a forged slashing, exit or credential change stops at its own operation. A forged vote costs every operation, the batch (n+1 Miller loops, one final exponentiation), and the bisection (sets totalling fewer than n, about log2 n final exponentiations). Both pay the weights' 64-bit scalar multiplications. For n = 130 that is about the same Miller loops as before this PR, about 10 final exponentiations instead of 130, and up to twice the hash-to-curve. Not measured in wall time.

Measured

On a loaded M3, the pairing per block took 2.7-4x less wall time with one batch. That ratio mixes two effects: fewer final exponentiations, and blst's verify_multiple_aggregate_signatures spreading the Miller loops over its own pool where the old per-set check used one worker. CPU time drops too: profiling a Hoodi block replay on a Ryzen 9700X (the whole stack, warm caches) puts pairing and hash-to-curve at 7.9 ms CPU per block before and 2.4 ms after. That run predates the review fixes that take the proposer's signature and the RANDAO reveal out of the body's batch, so a block now pays two more final exponentiations than profiled: three for a block with no slashing, exit or credential change (proposer; RANDAO; body), against one per signature set before this PR.

Tests

  • ethlambda-crypto: the batch tests cover a valid batch of ten, one bad among ten, swapped signatures, empty batch, single set, repeated messages, mixed single and aggregate, an invalid aggregate inside a batch, agreement with bls::verify and bls::fast_aggregate_verify, None for invalid inputs, weights never zero, an infinity signature among valid sets, keys that cancel ([P, -P] never verifies, alone or in a batch; [P, P, -P] verifies under P's signature), and eth_fast_aggregate with and without participants (agreeing with bls::eth_fast_aggregate_verify).
  • ethlambda-state-transition --lib: bisection lands on the first bad set at every position for several batch sizes; verify_pending clears what it checked; a bad RANDAO reveal is rejected before the mix changes, and so is a bad set pending before it; a valid reveal is checked, not deferred; a forged BLS-to-execution change is refused by the operation itself and a genuine one while a forged set is pending, credentials untouched; the shared attester-list rules; deferred and immediate failures carry the same label.
  • beacon_spec_tests: the operation runner builds a batch around each operation and treats an Err from the operation or from the batch as the rejection.

Before the second review round: crypto --lib 32 passed; state-transition --lib 655 passed; beacon_spec_tests unfiltered (mainnet preset) 7493 passed, 977 ignored, 0 failed; on the stack's tip, storage 213, blockchain 291, p2p 362, rpc 335 passed.

After the second review round: make fmt, make lint and cargo clippy -p ethlambda-state-transition --all-targets --features beacon-spec-tests -D warnings are clean; the test suites were not rerun locally and are left to CI.

… 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.
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.
SignatureSet::fast_aggregate now sums its keys with part a's batched
sum_validated_points instead of a sequential AggregatePublicKey::aggregate,
and the identity-key check in verify_signature_sets documents that blst
rejects the same input.
@MegaRedHand
MegaRedHand added this pull request to stack #744 October 30, 2026 16:08
MegaRedHand added a commit that referenced this pull request Oct 30, 2026
…738)

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.

Cherry-picked from perf/block-signature-batch at 7736808.
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.
…tch 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.
@github-actions

Copy link
Copy Markdown

🤖 Codex Code Review

Looks good overall. The PR is a sensible refactor toward reusable signature batching, and I did not spot a consensus-breaking issue in the changed paths.

  • crates/blockchain/state_transition/src/beacon/block_production.rs:457 is the most important correctness point, and it looks right: using SignatureBatch::immediate() preserves the old “reject before mutating scratch state” behavior for pool packing. That avoids a subtle block-production bug where invalid attestations/exits/slashings could otherwise leak mutations into scratch.

  • crates/blockchain/state_transition/src/beacon/gloas_block_production.rs:406 and crates/blockchain/state_transition/src/beacon/gloas_block_production.rs:532 are also correct in spirit: switching block production from process_block to process_block_and_verify closes a real validation gap. Without that, produced gloas blocks could compute a state root from a post-state that includes deferred-but-never-verified signatures.

  • crates/blockchain/state_transition/src/beacon/helpers/attestation.rs:94, crates/blockchain/state_transition/src/beacon/helpers/electra.rs:294, and crates/blockchain/state_transition/src/beacon/helpers/gloas.rs:138 preserve the old attestation validity checks while exposing an Option<SignatureSet>. Returning None for malformed index sets or out-of-range validator indices is the right shape for callers that batch signatures.

  • Performance is improved or neutral in the hot path: deferring attestation pairings into SignatureBatch remains the same model, and the new helpers avoid redoing structural checks. The extra allocation of Vec<Pubkey> was already present before in the old fast_aggregate_verify path.

  • Security/memory-safety wise, I don’t see unsafe behavior or error-swallowing regressions. The Option<SignatureSet> API is also safer than a boolean helper for callers that need to distinguish “cannot even build a valid signature set” from “built, but pairing failed”.

  • Readability is good. The doc comments around immediate vs deferred verification in crates/blockchain/state_transition/src/beacon/stf/signatures.rs:78 make the invariant explicit, which is important for consensus code.

No requested changes from me. If you want, I can also do a second pass specifically on whether PR 738 needs new regression tests around gloas block production and attestation batching.


Automated review by OpenAI Codex · gpt-5.4 · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 Claude Code Review

Review: PR 738, per-block BLS signature batch

I read the new bls/batch.rs and stf/signatures.rs, and the changes to apply_block, process_block*, the operations and the attestation helpers. I did not build or run the tests.

Overall: the design is sound and I found no blocking issues.

Soundness of the batch

  • bls/batch.rs weights each set with a non-zero 64-bit scalar from the OS-seeded rand::rng(). That is the standard random-linear-combination construction, and the doc comment explains why the weights must be random.
  • Keys and signatures are validated when each set is built, so passing false for the group-check flags in verify_multiple_aggregate_signatures is correct.
  • The explicit identity-key check (0x40 infinity flag on byte 0 of the compressed key) is correct. A single set skips it and goes through verify(), which is also fine.
  • sync_committee_bits with no participants is still decided up front by comparing the signature to G2_POINT_AT_INFINITY. It never builds a set, which avoids an empty fast_aggregate.

Deferral semantics

  • Deferring is safe where the block is rejected anyway. The signatures.rs module doc argues this well.
  • Deposits correctly stay immediate, because a bad deposit signature skips the deposit instead of rejecting the block.
  • apply_block pushes the proposer's signature first, so a block whose only defect is that signature keeps its error label.
  • signatures.verify() runs before apply_pending_mutations and the state-root computation, so a bad block does not pay for hashing.
  • Making new() and verify() crate-internal, with only deferred/immediate public, rules out dropping an unverified batch. The test-only checked helper covers the unit tests.
  • Block production's pack_each uses SignatureBatch::immediate(), which keeps its "rejected candidate leaves scratch untouched" invariant.

Minor points (non-blocking)

  1. Cost of a failing batch (signatures.rs, SignatureBatch::verify). On failure it re-checks every set serially. A block whose last signature is bad now costs one batch plus n single pairings, where the old path cost about n. A bad block still costs bounded work, since the proposer signature is checked before the body, so this is probably acceptable. A comment saying so would help. Bisecting the batch would cut the cost if it ever matters.
  2. Block-level error order changed. A block with several defects can now be rejected for a later structural error before the bad signature is reported, or the reverse. The module doc already says this. The spec fixtures only assert that the block is rejected, so this should be fine, but it is worth confirming make test-beacon passes on both presets.
  3. Partly mutated state after Err (block.rs, process_block doc). Callers must discard state after an error. That holds today (transition_block clones the parent state), but nothing enforces it. A one-line pointer in process_block_with_signatures would help future callers.
  4. Duplicated set builders. The indexed_attestation_signature_set(_with_domain) code is copied across phase0, electra and gloas, and each copy builds its pubkey vector with the same loop. Part (c) of the stack, committee key sums, may remove this. If not, a shared generic helper would cut the duplication.
  5. SignatureSet is Copy (batch.rs). verify() copies every set into a fresh Vec (self.sets.iter().map(|(set, _)| *set)). This is cheap, but it could take a &[SignatureSet] view if the label were stored in a parallel vector.

LGTM, pending CI on the spec fixtures for both presets.


Automated review by Claude (Anthropic) · sonnet · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 Kimi Code Review

I'll review this PR which implements BLS signature batching for block processing. This is a significant optimization and correctness change for consensus-layer signature verification.

High-Level Assessment

The PR introduces deferred signature batching across all beacon state transition forks, replacing individual pairing checks with a single random-linear-combination batch verification. This is a well-known optimization (2-3x faster for typical blocks), but requires careful handling because:

  1. Some signatures must remain immediate (deposits with bad signatures are skipped, not rejected)
  2. Deferred verification means state mutations happen before signature checks
  3. The batch failure must identify which signature failed for debugging/spec compliance

Detailed Review

1. Core Batching Logic (crates/common/crypto/src/bls/batch.rs)

Line 78-82: RAND_BITS and security analysis

const RAND_BITS: usize = 64;

The 64-bit random weights provide ~2^-64 soundness, which is acceptable for consensus. However, I note that blst's verify_multiple_aggregate_signatures with RAND_BITS uses a specific multi-scalar multiplication path. The documentation correctly cites the ethresear.ch post.

Line 183-195: key_is_identity check

fn key_is_identity(&self) -> bool {
    self.pubkey.to_bytes()[0] & INFINITY_FLAG != 0
}

This is correct but fragile—it assumes specific encoding details of blst's compressed G1 format. The INFINITY_FLAG constant is well-defined in the BLS12-381 serialization spec, but this should have a comment referencing the spec (Zcash BLS12-381 §1.2 or similar).

Line 206-217: random_weight

fn random_weight(rng: &mut impl RngExt) -> blst_scalar {
    let weight = loop {
        let candidate: u64 = rng.random();
        if candidate != 0 {
            break candidate;
        }
    };

Good: zero weights are explicitly rejected. The RngExt trait usage is correct for rand 0.10.

Line 237-250: verify_signature_sets

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;
    }

The single-set optimization is correct and important. The identity key check is necessary because blst's verify_multiple_aggregate_signatures may not reject identity pubkeys in all cases (the behavior is aggregation-dependent).

Potential issue: verify_multiple_aggregate_signatures parameters

Looking at line 253-261:

let result = Signature::verify_multiple_aggregate_signatures(
    &messages,
    DST,
    &pubkeys,
    false,
    &signatures,
    false,
    &weights,
    RAND_BITS,
);

The false parameters are sig_groupcheck and pks_validate. Since keys and signatures were validated at SignatureSet construction time, skipping re-validation is correct. However, I want to verify: does verify_multiple_aggregate_signatures perform the final exponentiation once? Yes, this is blst's documented behavior for this API.

2. SignatureBatch State Management (crates/blockchain/state_transition/src/beacon/stf/signatures.rs)

Line 88-96: SignatureBatch::deferred

pub fn deferred<T>(f: impl FnOnce(&mut SignatureBatch) -> Result<T>) -> Result<T> {
    let mut batch = Self::new();
    let out = f(&mut batch)?;
    batch.verify()?;
    Ok(out)
}

This is the critical API. The closure receives &mut SignatureBatch, and verification happens after f returns successfully. Important correctness property: if f returns Err, the batch is dropped unverified, which is correct because the block is already rejected.

Line 104-111: SignatureBatch::immediate

pub fn immediate() -> Self {
    Self {
        immediate: true,
        ..Self::default()
    }
}

Used in block production's pack_each for scratch-state candidate testing. This preserves the "reject before write" invariant.

Line 113-123: push

pub fn push(&mut self, set: Option<SignatureSet>, what: &'static str) -> Result<()> {
    let Some(set) = set else {
        return verify(false, what);
    };
    if self.immediate {
        return verify(set.verify(), what);
    }
    self.sets.push(set);
    self.labels.push(what);
    Ok(())
}

Correct: None from SignatureSet::single/fast_aggregate means invalid key/signature/empty-signer-list, which fails immediately with the same label.

Line 128-142: verify with fallback

pub(crate) fn verify(self) -> Result<()> {
    if verify_signature_sets(&self.sets) {
        return Ok(());
    }
    // ... fallback to individual verification
    let mut last = "block signatures";
    for (set, what) in self.sets.iter().zip(self.labels) {
        last = what;
        verify(set.verify(), what)?;
    }
    verify(false, last)
}

The fallback is necessary for error attribution. However, there's a subtle issue: if the batch fails but no individual set fails (shouldn't happen with correct crypto, but defense in depth), the final verify(false, last) catches it.

Line 142: Potential panic source

verify(false, last)

This will panic via verify if somehow reached. Actually, looking at the loop: if all individual set.verify() pass, we reach verify(false, last) which will panic. This is unreachable with correct BLS math, but consider making this a hard error rather than panic, or document the invariant.

3. Fork-Specific Integration

Phase0/Altair (crates/blockchain/state_transition/src/beacon/stf/altair.rs)

Line 89-97: process_attestation now takes signatures: &mut SignatureBatch

Line 137-145: The attestation signature is deferred:

signatures.push(
    indexed_attestation_signature_set(state, &indexed_attestation),
    "is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))",
)?;

Line 310-345: process_sync_aggregate — critical special case handled correctly

The eth_fast_aggregate_verify with zero participants requires the signature to be the point at infinity. This is checked immediately (not deferred) because there's no pairing to batch:

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(...),
        ...
    )?;
}

This is correct per the spec and preserves the "no participants = infinity signature" rule.

Bellatrix through Fulu: #[allow(clippy::too_many_arguments)]

Multiple functions now have 8+ parameters due to the signatures parameter. The allow attributes are pragmatic but suggest these functions should perhaps be refactored into struct-based configuration. Not blocking for this PR.

4. Gloas-Specific Changes

Line 106-140: process_block_and_verify

New public function for gloas block production that wraps process_block with SignatureBatch::deferred. This is necessary because gloas block production calls process_block directly rather than through the generic process_block dispatch.

Line 786-808: execution_payload_bid_signature_set

Returns Result<Option<SignatureSet>> — the Result is for state access errors (builder index out of range), Option is for invalid key/signature. This is correctly propagated:

signatures.push(
    execution_payload_bid_signature_set(state, signed_bid)?,
    "process_execution_payload_bid: verify_execution_payload_bid_signature(state, signed_bid)",
)?;

Note: the ? propagates Result::Err (state access failure) immediately, while None from Ok(None) would be caught by push and converted to a verification failure. This is correct: a missing builder is a state error, not a signature failure.

5. Test Infrastructure

crates/blockchain/state_transition/tests/beacon_spec/operations.rs

The test wrapper apply now uses SignatureBatch::deferred:

let mut unhandled = None;
let outcome = SignatureBatch::deferred(|signatures| {
    run(handler, case, state, config, signatures).unwrap_or_else(|err| {
        unhandled = Some(err);
        Ok(())
    })
});

This is slightly awkward. The unhandled captures non-handler errors (unknown operation names). The unwrap_or_else converts the inner Result to always succeed for the closure, letting SignatureBatch::deferred verify signatures even for unknown operations... wait, no: if run returns Err(String) (unknown handler), the closure returns Ok(()) and signatures are verified on an empty batch, then unhandled is checked.

Actually, re-reading: run returns Result<Result<(), Error>, String> — outer Err is "no handler", inner Err is state transition failure. The closure must return Result<()> to match SignatureBatch::deferred's bound.

The pattern:

let outcome = SignatureBatch::deferred(|signatures| {
    run(...).unwrap_or_else(|err| { unhandled = Some(err); Ok(()) })
});

If run returns Err("unknown handler"), we set unhandled and return Ok(()). The deferred batch verifies (empty, so succeeds), then we check unhandled and return that error.

If run returns Ok(Err(state_transition_error)), the closure returns Err(state_transition_error), SignatureBatch::deferred short-circuits without verifying, and we return that error.

If run returns Ok(Ok(())), signatures are verified, and we return Ok(()).

This is correct but the control flow is subtle. A comment explaining this would help.

6. rand Dependency Addition

Cargo.lock and crates/common/crypto/Cargo.toml

rand 0.10.1 added to ethlambda-state-transition (lockfile) and rand.workspace = true in ethlambda-crypto. The workspace already has rand elsewhere; this is consistent.

7. Block Production (pack_each)

crates/blockchain/state_transition/src/beacon/block_production.rs

Line 443-462:

fn pack_each<T>(
    candidates: Vec<T>,
    max: usize,
    config: &Config,
    apply: impl Fn(&mut BeaconState, &T, &Config, &mut SignatureBatch) -> Result<()>,
) -> Vec<T> {
    // ...
    let mut signatures = SignatureBatch::immediate();
    if apply(scratch, &candidate, config, &mut signatures).is_ok() {
        packed.push(candidate);
    }
}

The comment explains why immediate is used: "a bad signature rejects the candidate before it writes, which is what lets a rejected candidate leave scratch untouched."

This is a critical invariant for block production. The immediate batch verifies on each push, so apply returns Err before any state mutation from a later step. Correct.

Issues Found

Issue 1: Potential for Unbatched Block Signature in apply_block

File: crates/blockchain/state_transition/src/beacon/stf/mod.rs
Lines: 177-192

if validate_result {
    verify(
        verify_block_signature(state, signed_block),
        "block signature",
    )?;
}
// ...
process_block(state, signed_block, ...)?;

The block proposer signature is verified before process_block, not batched with body signatures. The comment explains this is intentional (early rejection for gossip-unchecked blocks). However, this means:

  1. A block with an invalid proposer signature costs 1 pairing (early)
  2. A block with valid proposer signature but invalid body signature costs 1 + batch pairings

This is optimal for the common case (invalid blocks rejected early), but the comment should explicitly note that the proposer signature is intentionally not in the batch.

Severity: Documentation only. The logic is correct.

Issue 2: verify_signature_sets with Single Element vs. key_is_identity

File: crates/common/crypto/src/bls/batch.rs
Lines: 222-226

match sets {
    [] => return true,
    [only] => return only.verify(),
    _ => {}
}
if sets.iter().any(SignatureSet::key_is_identity) {
    return false;
}

The single-element optimization skips key_is_identity. But only.verify() will call self.signature.verify(...) or fast_aggregate_verify_pre_aggregated, which does reject identity pubkeys (BLST_PK_IS_INFINITY). So behavior is consistent.

However, for a single aggregated set with identity key (e.g., [P, -P] summing to identity), only.verify() returns false, same as the batch would. Correct.

Issue 3: Missing #[must_use] on SignatureBatch::deferred Return Value

File: crates/blockchain/state_transition/src/beacon/stf/signatures.rs
Line: 91

SignatureBatch::deferred returns Result<T>. If a caller ignores this, they might not notice batch verification failure. However, Result is already must-use-like in Rust. Not a real issue.

Issue 4: Test Helper checked Could Panic on Nested Use

File: crates/blockchain/state_transition/src/beacon/stf/signatures.rs
Lines: 155-158

#[cfg(test)]
pub(crate) fn checked<T>(f: impl FnOnce(&mut SignatureBatch) -> Result<T>) -> Result<T> {
    SignatureBatch::deferred(f)
}

If f internally calls SignatureBatch::deferred, we'd have nested deferred batches. This would work (each gets its own batch), but it's inefficient. No actual nested use in the PR.

Issue 5: process_block Name Collision in gloas

File: crates/blockchain/state_transition/src/beacon/stf/gloas.rs

There are now two process_block functions with different signatures:

  • gloas::process_block (takes &mut SignatureBatch, doesn't verify)
  • super::block::process_block (generic dispatch, verifies)

And gloas::process_block_and_verify wraps the former. The naming is clear in context but could confuse. The gloas block production calls use process_block_and_verify correctly.

Issue 6: Comment Typo

File: crates/blockchain/state_transition/src/beacon/stf/signatures.rs
Line: 0 (module doc)

"valid except for its signatures" — good warning. But also check line 85: "A deferring batch that could be dropped unverified would turn every function taking one into 'valid except for its signatures'." This is why SignatureBatch is not Clone and verify takes self. Good design.

Consensus Correctness Verification

Let me verify the critical property: deferred signature verification must not accept any block that immediate verification would reject.

For each signature type:

Operation Deferred? Immediate fallback? Correct?
Block proposer No N/A Yes (early reject)
RANDAO Yes process_randao Yes
Attestation (all forks) Yes indexed_attestation_signature_set → verify Yes
Sync aggregate (non-empty) Yes SignatureSet::verify Yes
Sync aggregate (empty) No (infinity check) N/A Yes
Proposer slashing Yes SignatureSet::verify Yes
Attester slashing Yes indexed_attestation_signature_set → verify Yes
Voluntary exit Yes SignatureSet::verify Yes
BLS to execution change Yes SignatureSet::verify Yes
Deposit No N/A Yes (bad deposit skipped, not rejected)
Execution payload bid (gloas) Yes SignatureSet::verify Yes
Payload attestation (gloas) Yes SignatureSet::verify Yes

All correct. Deposits remain immediate because a bad signature deposit is skipped (not rejected), which changes state transition behavior.

Performance Assessment

The batch verification uses blst's verify_multiple_aggregate_signatures with:

  • One Miller loop per signature set
  • One final exponentiation total
  • Random 64-bit weights

For a typical block with ~16 attestations + RANDAO + sync aggregate + exits:

  • Before: ~19 full pairings = ~19 final exponentiations
  • After: ~19 Miller loops + 1 final exponentiation ≈ 3-4x faster

The SignatureSet construction validates keys/signatures once, avoiding re-validation in the batch. Good.

Final Verdict

Approve with minor suggestions.

The PR is correct, well-tested, and implements a significant optimization safely. The deferred batch pattern with immediate fallback for block production is sound.

Suggested changes:

  1. Document INFINITY_FLAG source (crates/common/crypto/src/bls/batch.rs:75):

    /// The bit of a compressed G1 point's first byte that marks the point at
    /// infinity (Zcash BLS12-381 §1.2: "The most significant three bits of
    /// u[0] are used to unambiguously represent the underlying element").
    const INFINITY_FLAG: u8 = 0b0100_0000;
  2. Add safety comment (crates/blockchain/state_transition/src/beacon/stf/signatures.rs:142):

    // Unreachable with correct BLS batch verification: if the batch failed,
    // at least one individual set must fail. This is a defense-in-depth panic.
    verify(false, last)
  3. Clarify test wrapper (crates/blockchain/state_transition/tests/beacon_spec/operations.rs:174-192):
    Add comment explaining the unhandled pattern for operation-name errors vs. state-transition errors.

  4. Consider whether process_block_and_verify should be the pattern for all forks, not just gloas


Automated review by Kimi (Moonshot AI) · kimi-k2.6 · custom prompt

MegaRedHand added a commit that referenced this pull request Oct 30, 2026
…738): review fixes

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.

Updates nightly's #738 from perf/block-signature-batch at 7736808 to e6eeb5b.
MegaRedHand added a commit that referenced this pull request Oct 30, 2026
…and resources (#756)

The import replay reported `stf` as one number, which is most of a mainnet
block's import but does not say which step of the transition to attack, nor
whether an epoch-crossing block, a block after skipped slots and an ordinary
block differ. The beacon STF had no internal timing at all.

lean_beacon_stf_phase_seconds{context,phase} is observed around the steps of
the transition: process_slots (excluding epoch work), process_epoch, the block
signature, header (including the body hash it commits to), randao, eth1 data,
execution payload (gloas: parent payload, withdrawals, bid), each operation
list, the sync aggregate and the post-state root check. One Instant pair per
step, none per attestation; an empty list observes nothing. The phase list is
generated with the enum that the code observes through, so the two cannot
disagree.

The epoch precompute worker runs process_epoch on another thread while an
import runs, and the replay reads the histogram before and after each block, so
one series would charge the worker to whichever block was importing. The series
is split by a context label instead: only fork_choice's transition_block sets
`import` (a thread-local guard, since the steps are observed deep inside the
per-fork block processing), and every other caller writes `other`. The replay
reads only `import`.

The replay report (schema_version 2) gains per block: the STF steps, the stf
time outside any step, a class (epoch_boundary, after_skip, ordinary), process
CPU seconds and RSS; and per run: p95 and p99, a per-class summary and the
deltas of the cache lookup counters, so precompute hit rates appear in it.
Consensus behavior is unchanged.

Adapted onto nightly: block processing now defers every signature but the
proposer's into one `SignatureBatch` (#738), so the old inline checks the
timers wrapped no longer exist. Every call site keeps nightly's `signatures`
argument and order, with the timers wrapped around the same steps as before;
the proposer signature stays immediate and is still timed as
`block_signature`. A new phase `batch_signatures` times `SignatureBatch::verify`,
the one random-weight check of everything the steps deferred, so the verify
cost that used to sit inside `randao`, the operation lists and `sync_aggregate`
is now its own series. Those phases now cover building the signature sets and
the state changes, not verification, and docs/metrics.md and
docs/benchmarking.md say so. The phase is declared in the `StfPhase` enum, so
the label list stays generated from it. replay.rs keeps both
`--warm-committee-sums` (warm-up before the timers start) and the report
changes; the report test for warm_committee_sums gets the new `Caches`
argument. Verdicts, order and errors are nightly's. docs/benchmarking.md keeps
#755's paragraph on the overlapped transition ahead of this section.

Cherry-picked from perf/beacon-stf-phase-timing at c891134, adapted in
5c385e084.
@MegaRedHand

Copy link
Copy Markdown
Collaborator Author

Block import benchmark (offline replay of mainnet blocks, steady state)

leg STF per block vs main
main 012dc4f 157.7 ms
main + #737 150.0 ms -4.9%
main + #737 + #738 145.9 ms -7.5%
Method

Base automatically changed from perf/bls-batched-key-sums to main November 19, 2026 19:41

@pablodeymo pablodeymo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review notes on the deferred batch signature verification. The first two inline comments (DoS cost of forged blocks, state mutated by unverified operations) are the ones worth resolving before merge.

One more that falls outside the diff: crates/common/crypto/src/bls.rs (~L329), bls::verify and bls::fast_aggregate_verify still implement the full validate/sum/pair sequence themselves instead of SignatureSet::single(..).is_some_and(|s| s.verify()). Only tests keep the two copies in sync, and a drift would make gossip/fork choice (bls::*) disagree with block import (SignatureSet).

///
/// 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forged blocks now cost ~100x more CPU on import. Deferring every body signature to the end means a block with forged attestation/slashing/exit signatures runs every operation, then a ~130-set multi-pairing, then a par_iter single re-check of every set, where before it was rejected after one or two pairings at the first attestation. Only one valid proposer signature is needed, and one proposer key can sign any number of equivocating blocks for its slot; by-root/by-range sync skips gossip's per-proposer-slot dedup. The doc's "bounded because it takes the slot proposer's key" doesn't hold. Suggestion: verify the attestation sets first, or bail to per-set checks as soon as the first operation's signature fails.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two changes in 477ca69, and one place where I'd push back on the framing.

A failed batch no longer re-checks every set on its own. first_failing in stf/signatures.rs bisects: it checks the first half of the range that holds the first failing set and keeps the half that answers. The halves shrink by half each step, so it checks fewer sets than the batch had, at one final exponentiation per step: about 8 for a ~130-set block, instead of 130 pairings. The label is still the first failing set in push order (bisection_finds_the_first_bad_set_at_every_position).

Slashings, exits and BLS-to-execution changes now call SignatureBatch::verify_pending before they write, which checks their sets together with everything pending. A forged one stops the block at that operation, as before. (That is also the fix for the next thread.)

On "before it was rejected after one or two pairings": that was the cheapest forged block, not the costliest. With immediate checks a forger who wants us to do the work puts the bad signature on the sync aggregate, the last signature a block carries (process_operations then process_sync_aggregate, stf/electra.rs:1884-1885 on main). main then runs every operation and n standalone checks before rejecting. So the worst case was already a whole body.

In blst units, a standalone check is two Miller loops (key and message, then the signature in PAIRING_FinalVerify) and one final exponentiation. A batch of n is n+1 Miller loops and one final exponentiation, plus a 64-bit scalar multiplication in G1 and in G2 per set for the weights.

  • main's worst case, for n = 130: ~260 Miller loops, 130 final exponentiations, 130 hash-to-curves.
  • 63ba4aa added the batch and a full re-check on top of that, so you're right that it was strictly worse.
  • Now it is the batch plus the bisection: ~270 Miller loops, ~10 final exponentiations, up to ~260 hash-to-curves, and the weights' scalar multiplications.

So about main's Miller loops, far fewer final exponentiations, more hashing. I haven't measured the net in wall time. The doc's "bounded because it takes the slot proposer's key" is gone; the module doc has a "What a forged block costs" section saying the above.

I didn't add a separate up-front pass over the attestations. Their sets come out of get_indexed_attestation and the key sums, which are most of what processing an attestation costs. A second pass would do that work twice for honest blocks and still not lower the worst case, which sits in the sync aggregate. The many-equivocating-blocks-through-sync point holds on main too; capping how many blocks per (proposer, slot) sync imports would be a separate change.

//! 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.
//!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With signatures deferred, operations whose signatures are forged mutate the state before later operations run. "Reaches no state a valid block could not" holds only if every key holder colludes; one attacker can now combine forged slashings + exits + BLS changes into a state no honest set of signers produces, making any unwrap/unreachable!/overflow that assumes those combinations impossible reachable. Also, transition_block returns the partly mutated state alongside the Err, so correctness now relies on an undocumented "never read the state after Err" rule.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the "reaches no state a valid block could not" argument assumed every key holder signs, which a forger doesn't need. Fixed in 477ca69: every operation that changes a validator's record checks its own sets, and everything pending, through SignatureBatch::verify_pending before it writes. That covers proposer and attester slashings, voluntary exits and BLS-to-execution changes. The RANDAO reveal does the same before the mix (next thread). So none of them writes on a forged signature, and none runs on a state an earlier forged signature wrote to. A block can no longer stack forged slashings, exits and credential changes.

What stays deferred is vote accounting: attestations (participation flags and the proposer's inclusion reward, what honest votes for the same data write), the sync aggregate (the last step of a block, so nothing runs after it), and gloas payload attestations (no writes). One write still precedes its check. The gloas bid records the builder's payment and the bid before its signature is verified. It is verified right after, together with the RANDAO reveal, before any operation runs, so nothing reads that record unverified. That holds whenever the reveal is verified, which import always does. I didn't give the bid a check of its own, which would cost another final exponentiation per gloas block for a write nothing reads in between. The module doc's "What may be deferred" section lists all four (af1671d). a_credential_change_checks_its_signature_and_every_pending_one_before_it_writes (147d418) pins it: through a deferring batch, a forged 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. Before the change, the first call returned Ok and rewrote them.

transition_block now returns Result<BeaconState> instead of (BeaconState, Result<()>), and SpeculativeTransition holds that Result (93b4566). A failed transition has no state to read, so the rule is in the type, not in a doc.

@@ -295,8 +354,8 @@ pub fn process_randao(

let proposer = state.validator(proposer_index)?;
if verification == RandaoVerification::Verify {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RANDAO reveal is XORed into randao_mixes before its signature is verified, so anything later in the same block reading the current mix (gloas get_seed/get_randao_mix) sees an attacker-chosen value until the batch fails at the end. This inverts the spec's verify-then-mix order.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 477ca69. process_randao pushes the reveal's set and calls verify_pending before it touches randao_mixes, so it is verify-then-mix again. In gloas, the builder's bid is pushed just before and is checked in the same batch, so nothing reads the bid's write unverified either. Tests in stf/block.rs: a_bad_randao_reveal_is_rejected_before_it_is_mixed_in checks the mix is unchanged after the rejection. a_bad_set_pending_before_the_reveal_is_rejected_with_it is the bid's case.

Cost: one more final exponentiation per block than 63ba4aa, since the reveal no longer shares the body's batch. The alternative was to fold it into apply_block's up-front proposer check as a 2-set batch. I didn't, because that would make an unsigned block cost more than the one pairing you asked to keep in the next thread.

/// 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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block_signature_set is new public API whose only caller is verify_block_signature. It's left over from when the proposer signature was batched, and keeping it public invites someone to batch it again, undoing the "reject an unsigned block after one pairing" property apply_block relies on. Suggest inlining it or making it private.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in fa8c70f. verify_block_signature calls verify_proposer_signature directly, and its doc says it is checked on its own, never batched, so an unsigned block costs one pairing.


/// [`indexed_attestation_signature_set`] under a signing domain the caller
/// chose.
pub fn indexed_attestation_signature_set_with_domain(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of helpers/{attestation,electra,gloas}.rs now has four near-identical functions (is_valid_*, is_valid_*_with_domain, *_signature_set, *_signature_set_with_domain). The bool versions are just .is_some_and(|s| s.verify()), so 12 functions could be 6, and *_signature_set_with_domain is pub with only same-module callers. Fixes to the structural checks still have to be made in three places.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b22918d. The rules live once, in helpers::attestation::attesters_signature_set: non-empty, at most MAX_VALIDATORS_PER_SLOT, sorted and unique, every index in the registry, then the set. It takes the attester list, data and signature of any fork's container and an optional domain. Phase0, electra and gloas each keep is_valid_indexed_attestation and indexed_attestation_signature_set, one line each over it. Both _with_domain variants are gone; gossip's aggregate validation, their only caller, calls the shared builder with the schedule's domain. The length bound is gloas's, and the earlier forks' list types are bounded at or below it, so it changes none of their answers. New test: only_a_sorted_unique_list_of_known_validators_builds_a_set.

&sync_aggregate.sync_committee_signature,
),
eth_fast_aggregate_verify,
)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-implements bls::eth_fast_aggregate_verify's empty-participants rule (must be G2_POINT_AT_INFINITY) inline, while block production's sync-aggregate precheck (block_production.rs:153) still uses the helper. If the two drift, the node packs aggregates its own STF rejects. A SignatureSet constructor returning trivially-valid / set / invalid would keep one definition.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 01e0024. SignatureSet::eth_fast_aggregate holds the rule and answers EthFastAggregate::{Valid, Invalid, Pairing(set)}. bls::eth_fast_aggregate_verify is SignatureSet::eth_fast_aggregate(..).verify(), which is what block production's verified_sync_aggregate calls. process_sync_aggregate matches on the same value: Valid passes, Invalid rejects, Pairing is pushed. Tests: no_participants_is_decided_without_a_pairing, participants_are_checked_by_their_pairing (the second compares against bls::eth_fast_aggregate_verify).

}

/// Whether every set verifies, in one random-linear-combination check:
/// `verify_multiple_aggregate_signatures` with 64-bit scalars from the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key_is_identity pre-check is redundant by its own doc (blst already rejects identity keys, "changes no behavior"), and the [only] => only.verify() arm returns before it, so it doesn't apply to single-set batches. Either drop it or apply it uniformly as a real guard, including in SignatureSet::verify.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped in 619e202. blst refuses an infinite public key wherever it adds one to a pairing (BLST_PK_IS_INFINITY, blst/src/aggregate.c in the min_pk path), and both the single check and verify_multiple_aggregate_signatures go through it. verify_signature_sets' doc now says that instead of claiming a check. keys_that_cancel_never_verify_alone_or_in_a_batch stays: [P, -P] fails alone and inside a batch, with the identity signature and with a real one, now with nothing of ours in the way.

Comment thread crates/common/crypto/src/bls/batch.rs Outdated
//! 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the soundness argument says rand::rng() is "reseeded periodically", but rand 0.10's ThreadRng dropped the periodic-reseeding wrapper. It's still an OS-seeded CSPRNG, so this isn't exploitable, but the documented guarantee is inaccurate.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked, and the locked version still reseeds. rand 0.10.0 removed the public ReseedingRng type, but ThreadRng in rand 0.10.1 (what Cargo.lock pins) wraps its ChaCha12 core in an internal ReseedingCore that reseeds from SysRng every 64 KiB (rand-0.10.1/src/rngs/thread.rs, RESEED_BLOCK_THRESHOLD; its doc says "Automatic seeding via SysRng and after every 64 kB of output"). Your underlying point stands, though: the argument shouldn't lean on that. The doc now says soundness rests only on the OS-seeded CSPRNG being unpredictable, and mentions the reseeding as something nothing depends on (619e202).

/// 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() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CLAUDE.md prefers module-scoped LazyLock statics for state_transition metrics; this one is function-scoped, unlike LEAN_BEACON_COMMITTEE_CACHE_LOOKUPS_TOTAL just below.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to a module-scoped LazyLock like LEAN_BEACON_COMMITTEE_CACHE_LOOKUPS_TOTAL (0521d9d). The help text and the docs/metrics.md row now describe what it counts with the bisection: a failed batch whose narrowed-to set verifies on its own.

…e 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.
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<BeaconState>, and
SpeculativeTransition holds that Result, so a failed transition has no
state to read.
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.
…ecks

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.
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.
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.
…t 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.
… 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.
…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.
@MegaRedHand

Copy link
Copy Markdown
Collaborator Author

bls::verify and bls::fast_aggregate_verify duplicating SignatureSet

Done in 01e0024. bls::verify is SignatureSet::single(..).is_some_and(|set| set.verify()), bls::fast_aggregate_verify is the same over SignatureSet::fast_aggregate, and bls::eth_fast_aggregate_verify goes through SignatureSet::eth_fast_aggregate. Gossip, fork choice and block import now decide a signature with one piece of code. The existing agreement tests (verify_agrees_with_bls_verify, verify_agrees_with_fast_aggregate_verify) stay as a guard against someone splitting them again. bls::aggregate_verify (distinct messages, no SignatureSet counterpart) is unchanged.

I also replied on each inline thread. In short: slashings, exits, BLS changes and the RANDAO reveal are now checked, with everything pending, before they write, and only vote accounting is deferred. A failed batch is bisected instead of re-checked set by set. transition_block returns no state with an error. The cost is one more final exponentiation per valid block than the reviewed head (the RANDAO reveal). The PR description is updated to match.

@MegaRedHand

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

Copy link
Copy Markdown

🤖 Codex Code Review

No actionable findings. The BLS batching preserves input validation, checks sensitive operations before mutation, and discards failed transition states.

Tests could not run because rustup attempted to write to a read-only directory.


Automated review by OpenAI Codex · gpt-6.1-sol · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 Gemini Code Review

This PR looks great. The batch verification design and safety boundaries are well thought out:

  • Cryptographic soundness: Using 64-bit non-zero random scalars from rand::rng() for blst's verify_multiple_aggregate_signatures, while validating pubkeys and signature group membership up front when constructing SignatureSet, matches consensus-layer BLS batching requirements and properly rejects identity aggregate pubkeys ([P, -P]).
  • State mutation boundaries: Flushing pending signatures via verify_pending() before mutating the RANDAO mix or validator registry state (proposer/attester slashings, voluntary exits, BLS-to-execution credential changes) ensures subsequent operations in the same block never execute against state derived from unverified signatures, while still batching all attestations, payload attestations, and the sync aggregate together (as well as batching the Gloas builder bid with the RANDAO reveal).
  • Encapsulation of partial state: Changing transition_block (and SpeculativeTransition) to return Result<BeaconState> instead of (BeaconState, Result<()>) prevents callers from accidentally inspecting a partially mutated state when a deferred signature check fails, and SignatureBatch::immediate() preserves pack_each's invariant on its scratch state during block production.
  • Error attribution: The bisection in first_failing efficiently pinpoints the first invalid signature in push order without redundant single-set pairings when a 1-element left slice has already been checked.

No issues found — LGTM.


Automated review by Gemini · gemini-4-argon · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 Claude Code Review

Review: batched BLS signature verification (PR 738)

Verdict: the design is sound and the PR is mergeable. Items 1–3 are worth addressing; the rest are nits.

What checks out

Soundness of the batch

  • Weights are random, non-zero, 64-bit, and drawn from rand::rng() (ChaCha12, seeded from the OS).
  • Keys are validated through validated_pubkey(s) and signatures through sig_validate (subgroup checked) when a set is built. So passing false for the validate and groupcheck flags in verify_multiple_aggregate_signatures is correct.
  • An identity aggregate key fails in blst on both paths, and the [P, -P] test proves it.

Deferral boundaries

  • These operations all call verify_pending() before writing: RANDAO, proposer and attester slashings, exits, BLS-to-execution changes, and gloas proposer slashing (before it removes the BuilderPendingPayment).
  • Only writes equivalent to honest votes are deferred: participation flags, the proposer's inclusion reward, and sync rewards.
  • Deposit signatures remain immediate, which is correct because a bad one skips the deposit rather than rejecting the block.

first_failing bisection (stf/signatures.rs)

  • The invariant holds. A range of one that failed as a batch is exactly that set's own check, because verify_signature_sets([x]) delegates to x.verify().
  • The failed flag correctly avoids re-verifying that last set.

transition_block returning Result<BeaconState>

  • This is the right way to enforce "no partially applied state escapes." It is also what makes deferral safe at import.

pack_each

  • Using SignatureBatch::immediate() preserves the "rejected candidate leaves scratch untouched" property.
  • verify_pending() costs nothing for an immediate batch, so packing behavior is unchanged.

Issues and suggestions

  1. Disagreement fallback rejects possibly valid blocks (stf/signatures.rs, check()).

    • When the batch fails but the bisected set verifies alone, the block is rejected. The doc itself says this "can fork this node off."
    • A valid batch cannot fail mathematically, so this path only fires on a bug. When it does, a node that rejects a valid block has a liveness and consensus problem. A node that falls back to the spec's definition does not.
    • Suggested fix: run a linear sets.iter().position(|s| !s.verify()).
      • If it finds a set, reject with that set's label.
      • If every set passes, accept and keep the error! log and metric.
    • The cost only appears on this already-pathological path.
  2. The partial-mutation contract is only documented, not enforced (stf/block.rs, process_block; stf/gloas.rs, process_block_and_verify).

    • Both are pub and take &mut BeaconState. Any caller other than transition_block that inspects or keeps the state after Err now sees attestation and sync writes from a block with a forged signature. Before this PR it only saw writes from valid operations.
    • Please audit the remaining callers that do not go through transition_block: block production's assemble_gloas_block*, the spec-test sanity/blocks harness, and any state_transition users.
    • Longer term, consider a by-value signature (fn(BeaconState, ..) -> Result<BeaconState>) so the type system enforces the contract the way transition_block now does.
  3. No end-to-end test of a forged signature at the block level.

    • Unit coverage of SignatureBatch and the per-operation flushes is good.
    • There is no full-block test (transition_block or apply_block) that combines:
      • a block whose only defect is one forged attestation among several valid ones;
      • an assertion that the block is rejected with that attestation's label;
      • an assertion that no state is returned.
    • Adding one would pin the import-path guarantee that the whole design rests on.
  4. Gossip aggregate path (gossip/aggregate.rs, around lines 444–501).

    • Unifying phase0, electra and gloas through attesters_signature_set adds the MAX_VALIDATORS_PER_SLOT bound to phase0 and electra. That is harmless because their SSZ bounds are already at or below it, as the doc notes.
    • Nit: the three match arms are now identical apart from the getter; a small macro or helper would cut the repetition.
  5. The "only deferred can make a deferring batch" guarantee can be bypassed.

    • Inside a deferred closure, std::mem::replace(signatures, SignatureBatch::immediate()) extracts the deferring batch so it can be dropped unverified.
    • This is not a practical risk. If you want the guarantee airtight, add a Drop impl that debug_assert!s sets.is_empty() for non-immediate batches. That would also catch future internal misuse in tests.
  6. Inconsistent import path for SignatureSet (stf/signatures.rs).

    • This file imports ethlambda_crypto::bls::{SignatureSet, verify_signature_sets} directly, while every other module goes through crate::beacon::bls::SignatureSet.
    • Pick one path for consistency.
  7. Error naming changed for multi-defect blocks.

    • The module doc acknowledges this. If any metrics, logs or tests key on the specific SpecAssert label (for example, invalid-block reason counters), check that they tolerate a deferred signature label appearing after a later structural failure, or the reverse.
  8. Gloas bid with RandaoVerification::Skip.

    • This is correct today because only production skips, and the first verify_pending (any slashing or exit) or the end-of-block check flushes it.
    • Suggest a debug_assert! or comment at the options.randao construction site in import, stating that import must use Verify. The bid-before-operations guarantee depends on it.
  9. Doc verbosity (nit).

    • Several comments run long, e.g. SignatureBatch's type doc and transition_block's. The reasoning is valuable, but trimming would help maintainability.
    • The #[allow(clippy::too_many_arguments)] additions are fine. A BlockContext { config, committees, active_balances, engine } struct would remove them across all drivers.

Performance

  • One batch per block for attestations plus the sync aggregate is a real win.
  • blst's verify_multiple_aggregate_signatures already parallelizes Miller loops on its internal pool, so no rayon chunking is needed.
  • Per-block allocation of the four Vecs in verify_signature_sets is negligible.

Automated review by Claude · claude-opus-5-5 · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 DeepSeek Code Review

A few concerns before merge:

  • crates/common/crypto/src/bls/batch.rs, in verify_signature_sets: weights is Vec<blst_scalar>, but the call passes &weights to Signature::verify_multiple_aggregate_signatures as rand. Upstream blst 0.3.16 expects rand: &[u8]; Vec<blst_scalar> does not coerce to &[u8]. This likely does not compile against stock blst. If a local fork accepts &[blst_scalar], fine, but otherwise concatenate each blst_scalar.b into a Vec<u8>.

  • crates/blockchain/state_transition/src/beacon/stf/block.rs / signatures.rs: deferring all attestation signatures to the final batch means a block with a bad attestation signature early in the body is processed all the way through the remaining operations and sync aggregate before rejection. The old immediate checks would reject at that attestation. Critical operations (process_randao, slashings, exits, BLS-to-execution changes) call verify_pending before their writes, but ordinary attestations do not. This is a meaningful invalid-block DoS increase, even if bounded by full block processing.

  • crates/blockchain/state_transition/src/beacon/stf/signatures.rs, SignatureBatch::deferred: if the closure returns Err, pending signatures are never verified. The current callers drop the state on error, so it is safe, but this invariant is only documentation. A future caller that inspects the post-state on error would get a state with unverified writes. Consider making this impossible by construction or adding a debug assertion.

  • crates/blockchain/state_transition/src/beacon/stf/signatures.rs, check: when first_failing returns None after a failed batch, the code rejects with the last label. That is fail-closed, which is good, but it can hide the real error and makes the metric path easy to hit only under a batch-check bug. The probability of bisection missing a failing set is 2^-64, so this is acceptable; just be aware the error message may be misleading.

  • crates/blockchain/state_transition/src/beacon/helpers/attestation.rs, attesters_signature_set: the new unconditional attesting_indices.len() > preset::MAX_VALIDATORS_PER_SLOT check is correct for Gloas and matches Electra, but verify the preset invariant in your configurations: if any earlier fork has MAX_VALIDATORS_PER_COMMITTEE > MAX_VALIDATORS_PER_SLOT, phase0/altair/bellatrix valid attestations could be rejected. This is probably an invariant, but worth asserting.

The Result<BeaconState> refactor in fork_choice.rs and the immediate batch in pack_each look correct and improve safety.


Automated review by DeepSeek · deepseek-v4-pro · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 Kimi Code Review

No blocking correctness or security issues found. The deferred-verification design remains fail-closed: transition_block discards partial states, while RANDAO, slashings, exits, and credential changes verify pending signatures before their sensitive writes. The bisection and batch-verification tests provide good coverage.

  • Minor performance hardening — beacon/gossip/aggregate.rs:447-501: each fork arm calls indexed.attesting_indices.to_vec() before the shared structural/signature validation. For gloas’s unbounded AttestingIndices, an oversized invalid aggregate is therefore copied before being rejected; previously the length check happened first. Validate/build the SignatureSet before cloning the indices, or at least precheck the gloas length, so invalid gossip avoids the extra allocation and copy.

Automated review by Kimi (Moonshot AI) · kimi-k3.1-pro · custom prompt

@github-actions

Copy link
Copy Markdown

🤖 Grok Code Review

The batching design is careful: invalid blocks still fail closed, state-changing operations call verify_pending before writing, and transition_block no longer hands back a half-applied state. A few consensus hazards are still only comments, not enforced.

  1. Gloas bid signature is not flushed unless RANDAO verification runs. process_execution_payload_bid records the payment and only pushes the set (gloas.rs around the new push near the old verify_execution_payload_bid_signature call). The flush is inside process_randao, and only in the Verify arm (block.rs ~362–370). Skip mixes the reveal and lets every later operation observe an unchecked bid. Block production uses Skip and relies on process_block_and_verify failing at the end. That is safe only while nothing between the bid write and the final batch reads the bid to decide later writes or the state root. Call verify_pending unconditionally at the end of bid processing, or at the start of process_randao even when the reveal itself is skipped. An empty batch is cheap; the coupling is not.

  2. Fork process_block no longer means "signatures checked". stf::process_block and gloas::process_block_and_verify wrap a deferred batch, but gloas::process_block and the other fork drivers return Ok with attestations, the sync aggregate, and payload attestations still pending. A missed outer deferred accepts those signatures. verify_pending is pub(crate), so out-of-crate callers cannot even flush a batch they were handed. Make the signature-taking drivers pub(crate) and keep the verifying wrappers as the public API. A Drop bomb on a non-immediate batch that still has sets would catch an internal miss.

  3. Attestation and sync-aggregate effects are applied before the pairing. That matches the spec only if every Err drops the state. Fork choice does (Result<BeaconState>). In-place callers do not: process_attestation / process_sync_aggregate set participation and pay the proposer, then the batch fails in SignatureBatch::deferred. The spec-operation harness now mutates state on a pure signature failure and only then returns the batch error (operations.rs apply). Confirm that runner ignores post-state on failure. Add a fork-choice test that a block whose only defect is a bad attestation or sync signature does not move head, balances, or participation.

  4. Batch/individual disagreement fails closed by blaming the last label (signatures.rs check). That can be a real batch bug or a 2^-64 false accept on a bisection subset; either way this node rejects a block peers may accept. The log and lean_beacon_signature_batch_disagreements_total are right, but the error looks like an ordinary invalid signature, so the node keeps running on a different chain. Use a distinct error and halt in debug (and ideally in production) when first_failing returns None.

  5. Smaller notes.

    • attesters_signature_set now applies MAX_VALIDATORS_PER_SLOT on every fork, including phase0 gossip (attestation.rs). Safe for SSZ-bounded containers; worth one test that a max-size phase0/electra committee still builds a set.
    • pack_each's immediate batch preserves the scratch invariant only if every apply pushes before it writes. Worth an assertion in the packer tests that a bad signature leaves scratch unchanged.
    • 64-bit OS-seeded weights are the right soundness tradeoff. Do not let a test RNG replace rand::rng() on this path.

I did not see a signature whose failure must not reject the block (deposits) get deferred, and the payload-attestation duplicate rule (<=, not unique) was preserved. The RANDAO, slashing, exit, and BLS-change "verify before write" tests are the right ones.


Automated review by Grok (xAI) · grok-4.7 · custom prompt

@pablodeymo
pablodeymo added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit 98e543c Nov 20, 2026
15 checks passed
@pablodeymo
pablodeymo deleted the perf/block-signature-batch branch November 20, 2026 01:29
pablodeymo added a commit that referenced this pull request Nov 20, 2026
Main's one-batch signature verification (#738) and this branch both
appended a parameter to the same block-processing functions. Each keeps
both, `target_delta` before `signatures`; the three `process_operations`
that reach eight arguments allow clippy's too_many_arguments, like their
siblings.

`transition_block` takes main's `Result<BeaconState>` shape and calls the
`*_with_options` entry points. `SpeculativeTransition` holds main's
`transition` beside this branch's `target_delta`, and `assert_agrees_with`
compares both. The delta test wraps `process_attestation` in `checked`,
as main's single-operation tests do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beacon Ethereum Beacon Chain client performance Performance improvements or possible performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants