Skip to content

perf(crypto): sum validated public keys through blst's batched affine addition - #737

Merged
MegaRedHand merged 5 commits into
mainfrom
perf/bls-batched-key-sums
Nov 19, 2026
Merged

MegaRedHand merged 5 commits into
mainfrom
perf/bls-batched-key-sums

Conversation

@MegaRedHand

@MegaRedHand MegaRedHand commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Stack 1/4 of the BLS verification work: (a) batched key sums -> (b) per-block signature batch -> (c) committee key sums -> (d) index-keyed pubkey cache.

What changed

crates/common/crypto/src/bls.rs only. A private helper, sum_validated_points, sums already-validated public keys through blst's safe MultiPoint::add for [PublicKey], which calls blst_p1s_add: a batched affine addition with one shared field inversion. fast_aggregate_verify (now ending in Signature::fast_aggregate_verify_pre_aggregated) and eth_aggregate_pubkeys use it instead of AggregatePublicKey::aggregate. No unsafe, no signature changes. aggregate_verify (distinct messages) never sums keys, so it is untouched.

Notes on the helper:

  • MultiPoint::add indexes the first element, so the empty slice is guarded (None). Both callers already reject empty input before.
  • From 384 points up, on a host with two or more CPUs, blst splits the work over its own thread pool (separate from rayon; the same pool its pairing already ran on); below that, or on one CPU, it runs on the calling thread. The result is the same either way.
  • Equal points (sync committees are drawn with replacement, so P + P occurs) give the same element as the sequential sum.

Why

Measured on blst 0.3.16, block shape of 11 signature sets and 33k signers, single-threaded, on a heavily loaded M3 (ratios only; absolutes to be taken on a quiet x86 host):

Phase per block Ratio
key aggregation, sequential (before) 1.4-2.5x the pairing
key aggregation, blst_p1s_add (this PR) 1.6-2.3x faster than sequential

Behavior

No change: every input accepted before is accepted, every input rejected before is rejected, including the identity aggregate: P and -P are both valid keys (flip the sort flag in the compressed encoding; whoever holds sk can sign -P's deposit with -sk), so a registry can hold both and an aggregate can name both. eth_aggregate_pubkeys([P, -P]) returns the identity's bytes on both paths, and fast_aggregate_verify stays false because blst refuses an infinite key at pairing time.

Tests

New unit tests compare sum_validated_points with AggregatePublicKey::aggregate: empty input, sizes 1, 2, 3, 17, 383, 384, 385, 767 and 2000, P + P, and runs with scattered repeats at 10, 400 and 1000 keys; fast_aggregate_verify with a repeated signer verifies (and fails with the key listed once); eth_aggregate_pubkeys equals the sequential sum with repeats; keys that cancel (adjacent P, -P pairs and two halves that cancel each other, at 8, 200, 400 and 1100 pairs) match the sequential sum, and [P, -P] aggregates to the identity and never verifies, not even against the identity signature.

Run:

  • cargo test -p ethlambda-crypto --profile release-fast --lib: 17 passed, 14 ignored
  • cargo test -p ethlambda-state-transition --profile release-fast --lib --features beacon-spec-tests (BLS fixture vectors): 645 passed
  • ... --test beacon_spec_tests --features beacon-spec-tests -- /sync_aggregate/: 182 passed
  • ... -- /attestation/: 356 passed
  • ... -- sanity/: 595 passed
  • cargo fmt --all, cargo clippy -p ethlambda-crypto --all-targets -- -D warnings: clean

Rerun after the review fixes, on the stack's tip (all four PRs, --profile release-fast): ethlambda-crypto --lib 47 passed, 14 ignored; ethlambda-state-transition --lib --features beacon-spec-tests (with the BLS fixture vectors) 658 passed; storage 213, blockchain 291, p2p 362, rpc 335 passed; full beacon_spec_tests 7493 passed, 977 ignored, 0 failed; make lint clean, also on a trial merge of current main.

@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
… addition (#737)

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.

Cherry-picked from perf/bls-batched-key-sums at 54be2b2.
@MegaRedHand
MegaRedHand marked this pull request as ready for review October 30, 2026 21:43
@github-actions

Copy link
Copy Markdown

🤖 Claude Code Review

Review of PR 737: batched public-key sum in crates/common/crypto/src/bls.rs

I read the diff only. I did not build it or run the tests.

The change looks correct and is well tested. I found no blocking issues.

  • Correctness:
    • sum_validated_points guards the empty slice, which MultiPoint::add would otherwise panic on, and returns None for it.
    • eth_aggregate_pubkeys keeps its len(pubkeys) > 0 SpecAssert through that None.
    • fast_aggregate_verify can only reach the None branch with empty input, and validated_pubkeys already rejects that case.
  • Safety of skipping the group check: every point comes from validated_pubkeys, which runs key_validate, so passing false for the group check is sound. The signature is validated earlier in the function.
  • Edge cases:
    • Repeated keys: the doubling case is covered, and sync committees drawn with replacement hit it.
    • Cancelling keys (P and -P): the sum is the identity, and verification fails. blst's pre-aggregated verify rejects an infinity public key.
    • eth_aggregate_pubkeys returns the identity encoding for that cancelling case, matching the spec pseudocode.
    • These cases are covered by fast_aggregate_verify_with_a_repeated_signer_still_verifies, a_key_and_its_negation_sum_to_the_identity_and_never_verify and the_batched_sum_matches_the_sequential_one_when_keys_cancel.
  • Tests: the batched sum is compared with the sequential sum at sizes on both sides of blst's 384-point threading threshold, which is the right boundary to test.
  • Performance: one shared field inversion across the whole slice replaces a projective addition per key. The doc comment also notes that blst's own thread pool is separate from rayon's, so no new threads are added.

Nits, optional

  • The comment "pubkeys is non-empty, so there is always a sum" in fast_aggregate_verify could say the non-empty guarantee comes from validated_pubkeys. As written, a reader has to check that.
  • The the_batched_sum_* tests build up to 2000 keys, which is fine for the speed of blst. If fresh_pubkey is slow, the 2000 and 1100 cases could be trimmed.

Approve.


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

@github-actions

Copy link
Copy Markdown

🤖 Codex Code Review

Looks good overall. The change is narrowly scoped to BLS pubkey aggregation, improves the hot path for attestation verification, and the added tests cover the main edge cases that matter for consensus safety: repeated keys, cancellation, and identity results.

  • No correctness issue jumped out in fast_aggregate_verify at crates/common/crypto/src/bls.rs:399. Replacing per-call AggregatePublicKey::aggregate with MultiPoint::add preserves the same group sum for validated keys, and the empty-slice guard in crates/common/crypto/src/bls.rs:289 avoids the only obvious panic hazard.
  • Security-wise, this does not appear to weaken subgroup or non-identity checks. Inputs still flow through Signature::sig_validate and cached PublicKey::key_validate before verification at crates/common/crypto/src/bls.rs:407 and crates/common/crypto/src/bls.rs:410.
  • Performance impact should be positive on the attestation hot path. Avoiding Vec<&PublicKey> allocation plus using batched addition in crates/common/crypto/src/bls.rs:418 is a sensible optimization.
  • The new tests in crates/common/crypto/src/bls.rs:653, crates/common/crypto/src/bls.rs:682, and crates/common/crypto/src/bls.rs:702 are strong and directly target consensus-relevant corner cases.

One minor maintainability note:

  • sum_validated_points at crates/common/crypto/src/bls.rs:289 returns Option<PublicKey>, but all call sites already enforce non-empty input. Returning PublicKey and keeping the emptiness check at the boundary callers might make the invariant clearer; not a blocker, just a readability tradeoff.

I don’t see fork-choice, justification/finalization, state transition, XMSS, or SSZ concerns in this PR since the diff is isolated to BLS aggregation logic.


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

@github-actions

Copy link
Copy Markdown

🤖 Kimi Code Review

I'll review this PR which optimizes BLS public key aggregation using blst's batched MultiPoint::add instead of sequential AggregatePublicKey::aggregate.

Overall Assessment

This is a well-crafted performance optimization with excellent test coverage. The core mathematical reasoning is sound, and the safety considerations around point validation and edge cases are properly handled.

Issues Found

1. Critical: fast_aggregate_verify spec compliance for empty pubkey list

File: crates/common/crypto/src/bls.rs, Lines 408-415

let Some(points) = validated_pubkeys(pubkeys) else {
    return false;
};
// `pubkeys` is non-empty, so there is always a sum.
let Some(aggregated) = sum_validated_points(&points) else {
    return false;
};

The comment claims pubkeys is non-empty, but this isn't guaranteed. validated_pubkeys returns Some(empty_vec) for an empty input, not None:

fn validated_pubkeys(pubkeys: &[BlsPubkey]) -> Option<Vec<PublicKey>> {
    // ...
    let mut points = Vec::with_capacity(pubkeys.len());
    // ...
    points.into_iter().collect()  // Returns Some([]) for empty input
}

Impact: fast_aggregate_verify(&[], message, &signature) will hit the sum_validated_points empty guard and return false, which is correct per spec (should fail). However, the code path is misleading—the comment is wrong, and we're relying on the downstream guard rather than explicit validation.

Fix: Either add an explicit is_empty() check with a clear comment, or fix the comment to accurately describe the behavior. The current code happens to be correct but for the wrong reason.

2. Minor: sum_validated_points naming inconsistency with behavior

File: crates/common/crypto/src/bls.rs, Lines 287-288

The function returns Option<PublicKey> where None means "empty input." But in eth_aggregate_pubkeys (line 437), the error message says "len(pubkeys) > 0"—this conflates "empty input" with "validation failed."

Suggestion: Consider making sum_validated_points return Result<PublicKey, &'static str> or similar to distinguish:

  • Empty input (caller decides if this is an error)
  • Other potential future failures

For now, this is acceptable since the only failure mode is empty input, but it's slightly fragile.

3. Performance: Unnecessary allocation in fast_aggregate_verify

File: crates/common/crypto/src/bls.rs, Lines 408-415

The old code collected point_refs: Vec<&PublicKey>; the new code still allocates points: Vec<PublicKey> in validated_pubkeys. The optimization is in the aggregation step, but we're still allocating the full vector of public keys.

Note: This is pre-existing and not introduced by this PR. The PR actually removes the point_refs allocation, which is good. No action needed, but worth noting the validated_pubkeys allocation remains.

4. Test coverage gap: fast_aggregate_verify with empty pubkeys

File: crates/common/crypto/src/bls.rs, Tests

No test verifies fast_aggregate_verify(&[], message, &signature) returns false. Given the subtlety in Point 1, add:

#[test]
fn fast_aggregate_verify_empty_pubkeys_fails() {
    let message = H256([1u8; 32]);
    let signature = BlsSignature(G2_POINT_AT_INFINITY);
    assert!(!fast_aggregate_verify(&[], message, &signature));
}

5. Documentation: MultiPoint trait import could be more specific

File: crates/common/crypto/src/bls.rs, Line 100

use blst::{BLST_ERROR, MultiPoint};

The MultiPoint trait is used only for .add() on &[PublicKey]. This is fine, but consider importing it closer to usage or using a more explicit call like <[PublicKey] as MultiPoint>::add(&points) if there's any ambiguity risk. Currently acceptable.

Positive Observations

  1. Excellent test coverage: The tests for doubling, cancellation, threshold boundaries (383/384/385), and repeated signers are thorough and mathematically meaningful.

  2. Correct handling of identity element: The negated test confirms that P + (-P) = identity is handled consistently, which is critical for consensus safety.

  3. Proper validation gating: The sum_validated_points function correctly requires pre-validated points and skips re-checking, with clear documentation of this safety invariant.

  4. Thread pool awareness: The documentation accurately describes blst's internal threading behavior and its interaction with the existing thread pool.

Summary

Severity Count Description
Critical 1 Misleading comment in fast_aggregate_verify masks empty-input handling; needs explicit check or corrected comment
Minor 1 sum_validated_points error semantics slightly conflate empty input with other failures
Test gap 1 Missing empty-pubkeys test for fast_aggregate_verify

The core optimization is correct and safe. Address Point 1 before merge—it's a latent maintainability issue that could lead to bugs if validated_pubkeys behavior changes.


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

MegaRedHand added a commit that referenced this pull request Oct 30, 2026
… addition (#737): review fixes

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.

Updates nightly's #737 from perf/bls-batched-key-sums at 54be2b2 to bac0aa3.
@MegaRedHand

Copy link
Copy Markdown
Collaborator Author

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

leg STF per block difference
main 012dc4f 157.7 ms
main + #737 150.0 ms -7.7 ms (-4.9%), 95% CI [-8.3, -7.2]
Method

@MegaRedHand

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

Copy link
Copy Markdown

🤖 Grok Code Review

The change is a sound, well-tested swap of sequential AggregatePublicKey::aggregate for blst's batched MultiPoint::add on already-validated keys. A few correctness and API nits are worth fixing before this ships.

  1. eth_aggregate_pubkeys error text is now wrong. In eth_aggregate_pubkeys (bls.rs around the new sum_validated_points call), None is mapped to Error::SpecAssert("len(pubkeys) > 0"). That used to be the only failure after key_validate. After this change, sum_validated_points also returns None when the sum is the identity (cancelling keys: P + (-P)). Your own test a_key_and_its_negation_sum_to_the_identity expects eth_aggregate_pubkeys to return the identity, so this only fires if to_public_key() is None for infinity — which is exactly the case PublicKey cannot represent. If that is what happens, the error string is a lie and callers cannot distinguish an empty list from a cancelling committee. If to_public_key() actually yields a point at infinity, the comment on sum_validated_points should say so and the identity test should assert the compressed bytes, not only == identity_pubkey(). Either way, do not reuse the empty-list assert for a different failure.

  2. Confirm identity handling against blst, not just the sequential path. points.add().to_public_key() returns Option in current blst. The sequential path goes through AggregatePublicKey, which can hold infinity, then to_public_key(). Those two APIs are not the same. A sum of validated points that cancels must not panic, must not return a non-identity key, and must not be reported as len(pubkeys) > 0. Sync committees are drawn with replacement, so repeats are real; cancelling pairs are rarer but still valid registry keys (your negated helper is the right case). Add an assertion that sum_validated_points on [P, -P] is either None or identity_pubkey(), and that fast_aggregate_verify / eth_fast_aggregate_verify both reject it (the verify test already covers the latter).

  3. fast_aggregate_verify empty path is fine; the None arm is not only "unreachable". The early return on empty pubkeys makes the empty-slice panic guard redundant there, which the comment states. It is still reachable if a future caller drops that check, and it is also the identity-sum path if to_public_key() is None. Returning false is the safe choice for verify (do not accept). Keep that. Do not treat the same None as "empty input" in eth_aggregate_pubkeys.

  4. Thread-pool interaction is a performance risk, not a correctness one. The comment is accurate that blst may fan out at 384 points on its own pool, separate from rayon. fast_aggregate_verify already parallelizes key validation with rayon, then this sum may spawn blst workers, then pairing may use the same pool. On a multi-threaded host that is oversubscription, not a wrong sum. If attestation verification is already rayon-parallel at the call site, consider keeping sums on the calling thread (blst's single-thread path below the threshold, or an explicit single-thread add if the crate exposes one) unless you have a benchmark showing the 384+ split wins under load. The unit tests compare bytes, not latency; they do not catch this.

  5. PublicKey copy in the repeat test. let one = validated_run(...)[0]; and points.push(*point) require PublicKey: Copy. That is true for blst's affine key, but it is a 96-byte copy per push. Fine for tests. Do not copy keys like that on the hot path; sum_validated_points already takes &[PublicKey], which is the right shape.

  6. What looks correct.

  • Empty slice is guarded before MultiPoint::add (that API panics on an empty slice). Good.
  • Group check stays off only after key_validate / signature validation. Matches the old aggregate(..., false) and fast_aggregate_verify(false, ...).
  • Switching fast_aggregate_verify to fast_aggregate_verify_pre_aggregated is the right API once the sum is one key: it checks the signature against the aggregate, which is what the spec's FastAggregateVerify does after summing keys.
  • DST and message slice are unchanged.
  • Tests cover sizes on both sides of 384, duplicates, and cancelling pairs versus AggregatePublicKey::aggregate. That is the right oracle.
  • fast_aggregate_verify still rejects an empty list while eth_fast_aggregate_verify accepts the infinity signature. Spec split is preserved.

No fork-choice, justification, or state-transition logic in this diff. The consensus risk is only "wrong aggregate pubkey ⇒ attestation accepted or rejected incorrectly." Item 1 and Item 2 are the ones that can do that if infinity is mishandled. The distinct-key and repeat tests already show the common path matches blst's sequential sum.


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

@github-actions

Copy link
Copy Markdown

🤖 Gemini Code Review

The changes look solid. Replacing sequential AggregatePublicKey::aggregate calls with batched affine addition via blst::MultiPoint::add (and pairing it with fast_aggregate_verify_pre_aggregated) is a clean optimization that also eliminates the intermediate Vec<&PublicKey> allocations in both fast_aggregate_verify and eth_aggregate_pubkeys.

Edge cases around empty slices, point doubling (duplicate signers in sync committees), and point cancellation (P + (-P)) are properly handled and well-covered by the new tests.


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

@github-actions

Copy link
Copy Markdown

🤖 Codex Code Review

No actionable findings. The change preserves public-key validation and empty-input behavior while replacing sequential aggregation with blst’s batched addition. Added tests cover repeated keys, cancelling keys, identity sums, and sizes around the threading threshold.

I couldn’t run the tests: Rust toolchain initialization failed because /home/runner/.rustup/tmp is read-only.


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

@github-actions

Copy link
Copy Markdown

🤖 Claude Code Review

Review: batched public-key sum via blst::MultiPoint::add

I found no blocking correctness bugs. The change preserves semantics. Details below.

Correctness

  • Semantic equivalence holds. blst's fast_aggregate_verify(false, …) internally does AggregatePublicKey::aggregate(pks, false), then to_public_key(), then aggregate_verify(…, &[&pk], false). fast_aggregate_verify_pre_aggregated enters that same aggregate_verify call. Two things carry over unchanged:

    • The infinity-PK rejection (BLST_PK_IS_INFINITY in PAIRING_Aggregate_PK_in_G1).
    • The thread-pool usage.

    The doc comment's claim that the pool is "the one blst's pairing already ran on" is accurate.

  • Identity handling matches the spec. In a_key_and_its_negation_sum_to_the_identity_and_never_verify, eth_aggregate_pubkeys returns the identity. fast_aggregate_verify rejects it, consistent with CoreVerify's KeyValidate.

  • Empty-slice guard is correct. In sum_validated_points (bls.rs ~L289), the guard is needed and correctly placed. The None arm in fast_aggregate_verify is indeed unreachable, and returning false there is the safe default.

Testing concerns

  1. The threaded path may never run in CI (the_batched_sum_matches_the_sequential_one_at_every_size, ~L617). On a single-CPU CI runner, the ≥384 multi-threaded branch never runs. The test comment acknowledges this, but nothing enforces coverage of that path.
    • Consider logging or asserting std::thread::available_parallelism() >= 2 in at least one CI job.
    • Alternatively, document that coverage of the pooled path depends on the runner.
  2. Possible interference with the global cache test. The new tests push several thousand fresh keys through validated_pubkeys, which feeds the global cache. Tests run in parallel by default. If the_cache_stops_growing_at_its_capacity makes assertions about occupancy or eviction of specific entries, these concurrent inserts could make it flaky. Please confirm that test only asserts an upper bound.
  3. Confusing test comment. The remark "every pool worker's chunk stays below blst_p1s_add's scratch stride, so that inner loop is never reached through the safe API" (~L622) is hard to follow and unverifiable from this crate. Either expand it with a reference to the blst source or drop it.
  4. The tests act as a correctness pin on blst. Correctness of the doubling and cancellation cases relies on blst's ptype##s_accumulate internals. The new tests serve well as regression guards. It is still worth confirming blst is pinned (exact or ~) in Cargo.toml, so an upgrade that changes p1s_add behavior gets noticed.

Performance

  1. No benchmark numbers are given. The batched affine path pays a batch inversion per chunk and then a final to_affine in to_public_key(). For tiny inputs (1–4 keys) this may be no faster, or slightly slower, than sequential projective adds. Please include before/after numbers at representative sizes, for example 1, 2, 16, 128, 512, and 2048 (the sync committee aggregate).
  2. Cheap win for n == 1. Single-signer aggregates are common. A fast path avoids the round trip entirely:
    match points {
        [] => None,
        [single] => Some(*single),
        _ => Some(points.add().to_public_key()),
    }
  3. Blocking inside rayon workers. If fast_aggregate_verify is called from rayon par_iter (rayon is imported here), each rayon worker blocks on blst's pool channel for large sums. This isn't new, since the pairing already did it, but sums of 384 or more keys now add another blocking hop. It is acceptable; just keep it in mind if latency profiling shows oversubscription.

Style

  1. points.add() reads like std::ops::Add. At ~L293, MultiPoint::add(points) makes the trait dispatch explicit and avoids reader confusion.
  2. Doc comment length. The doc comment on sum_validated_points is thorough but long. The threading paragraph could be condensed to one or two sentences.

Overall this looks good to merge once benchmark numbers are added (Point 5). Point 2 should also be confirmed as not an issue.


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

@github-actions

Copy link
Copy Markdown

🤖 Kimi Code Review

No blocking correctness or security issues found.

  • fast_aggregate_verify preserves empty-key rejection, and pre-aggregated verification remains safe because every public key has already passed key_validate and the signature was validated earlier.
  • eth_aggregate_pubkeys preserves the existing len(pubkeys) > 0 error behavior.
  • The tests provide strong coverage for duplicates/doubling, cancellation to the identity, empty inputs, repeated signers, and sizes around blst’s threading threshold.

Non-blocking: crates/common/crypto/src/bls.rs:264-290 documents highly version-specific blst internals, including the exact 384-point threshold and thread-pool behavior. Consider shortening this or explicitly noting the blst version so the documentation does not become misleading after a dependency upgrade.


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

@github-actions

Copy link
Copy Markdown

🤖 DeepSeek Code Review

Overall the change is sound: batched public-key aggregation via MultiPoint::add is appropriate here, the empty-slice guard is necessary, and the tests cover important edge cases such as repeated keys and cancellation.

One issue in the touched function:

  • In crates/common/crypto/src/bls.rs, eth_aggregate_pubkeys still returns Error::SpecAssert("len(pubkeys) > 0") for an empty pubkeys slice, via sum_validated_points(&points).ok_or(...). The Ethereum BLS wrapper eth_aggregate_pubkeys is specified to accept empty input and return G1_POINT_AT_INFINITY, mirroring how eth_fast_aggregate_verify accepts the empty pubkey list with the infinity signature. Since this PR touches that path and introduces the empty-sum guard, it’s worth adding an early if pubkeys.is_empty() { return Ok(identity_pubkey()); } before validation.

Minor nit:

  • In fast_aggregate_verify, the new comment says the signature was validated above, which is why the group check stays off. The false argument to fast_aggregate_verify_pre_aggregated controls public-key group validation, so the relevant reason is that the individual pubkeys have already been validated by validated_pubkeys. The comment could be clarified.

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

@pablodeymo
pablodeymo force-pushed the perf/bls-batched-key-sums branch from 79a4c89 to 8bf0171 Compare November 18, 2026 20:54
@MegaRedHand

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

Copy link
Copy Markdown

🤖 Gemini Code Review

This PR looks great. Switching to blst::MultiPoint::add (blst_p1s_add) with fast_aggregate_verify_pre_aggregated is a clean optimization that avoids the intermediate Vec<&PublicKey> allocation and uses batched affine addition while preserving exact consensus behavior.

A few highlights:

  • sum_validated_points properly guards the empty slice (avoiding the slice-indexing panic in MultiPoint::add) and short-circuits the single-key case to skip unnecessary projective-to-affine conversion.
  • All points entering sum_validated_points are guaranteed to have passed key_validate via validated_pubkeys.
  • Edge cases around point doubling (repeated signers in sync committees), point negation/cancellation to infinity, and blst's internal batching/thread-pool thresholds are thoroughly covered by the new tests.

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

@github-actions

Copy link
Copy Markdown

🤖 Grok Code Review

I'll review the aggregation change against the surrounding BLS verify path and blst's sum API so the equivalence claim can be checked, not just the comments.


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

@github-actions

Copy link
Copy Markdown

🤖 Claude Code Review

Review: batched public-key summation via MultiPoint::add

Verdict: Correct, and well-tested for the edge cases that matter. A few small items below; none block merging.

Correctness

The semantics are preserved.

  • What the old path did. Signature::fast_aggregate_verify(false, ...) sums the keys with AggregatePublicKey::aggregate(_, false) and then calls fast_aggregate_verify_pre_aggregated. Replacing only the summation step is equivalent, as long as the sum is the same group element.
  • Edge cases in the batched routine. blst_p1s_add's head step branches on X1 == X2.
    • If Y1 == Y2, it doubles.
    • If Y1 == -Y2, it produces infinity.
    • Infinity inputs are also handled.
    • The new tests cover all of these at sizes on both sides of the 384-point threshold: repeats, adjacent cancelling pairs, and cancelling halves.
  • Identity aggregate key is still rejected. PAIRING_Aggregate_PK_in_G1 returns BLST_PK_IS_INFINITY at the C level, whatever the Rust-side pks_validate flag says. The a_key_and_its_negation_sum_to_the_identity_and_never_verify test pins this down, which is the security-relevant property.
  • Empty-input handling is unchanged.
    • validated_pubkeys(&[]) returns Some(vec![]).
    • sum_validated_points then returns None.
    • eth_aggregate_pubkeys still raises SpecAssert("len(pubkeys) > 0").
    • The [] => None guard correctly avoids the points[0] index panic inside MultiPoint::add.
  • Single-signer fast path. [single] => Some(*single) is sound. PublicKey is Copy, and the old path produced the same affine point after a projective round-trip.

Points to address

  1. blst version pin (Cargo.toml, not in the diff). The doc comment on sum_validated_points (bls.rs ~L280) relies on blst 0.3.16 behavior: the 384-point threshold and the use of blst's own thread pool. Two things follow:

    • Make sure the manifest requires at least the version that introduced MultiPoint for [PublicKey] with these semantics, e.g. blst = "0.3.16", not just "0.3" plus the lockfile.
    • Older blst releases had fixes in the bulk-addition paths, so a downgrade through lockfile churn should be impossible.
  2. Nested parallelism. At 384 or more keys, the sum now fans out to blst's pool. If callers already run fast_aggregate_verify inside rayon's par_iter, as batch attestation verification likely does, every rayon worker will block while it dispatches into a second pool of roughly num_cpus threads.

    • This cannot deadlock, because blst's pool workers never call back into rayon.
    • It can oversubscribe the CPUs under load.
    • The pairing already behaves the same way, so this is not a regression in kind. Still, it is worth confirming with a benchmark under concurrent verification, not only single-call microbenchmarks.
  3. Performance claim for small sets.

    • For 2 or 3 keys, blst_p1s_add spends one batched inversion and to_public_key spends another. The old path made a few projective additions and then one inversion. Small sums may therefore be flat or slightly slower.
    • This is acceptable, but please put benchmark numbers in the PR description (e.g. n = 2, 16, 128, 512, 2048) so the doc comment's claim that "this is where the saving is" is backed by evidence.
  4. Shared cache in tests. validated_run and the cancellation tests push several thousand keys through validated_pubkeys. If that function writes to the global LazyLock cache, these tests share state with the_cache_stops_growing_at_its_capacity when tests run in parallel. Please check that the capacity test uses an isolated cache instance or measures something that cannot be disturbed by concurrent inserts.

  5. Doc and comment verbosity.

    • The doc comment on sum_validated_points (~30 lines) hard-codes version-specific internals: the 384 threshold, the 1024-point scratch buffer, and the per-worker split. These will drift silently when blst changes. Consider keeping only:
      • the precondition: inputs are already key-validated;
      • the empty and single-point behavior;
      • the note that equal and cancelling points are handled.
    • The comment in fast_aggregate_verify (~L414–418) explaining why the None arm is unreachable could be one line.

Minor

  • negated() in the tests flips the compressed sort flag. This is a nice way to construct -P without the secret key.
  • Unifying fast_aggregate_verify and eth_aggregate_pubkeys behind one helper removes the duplicated point_refs allocation. Good.

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

@github-actions

Copy link
Copy Markdown

🤖 Codex Code Review

No actionable findings in the diff. The change preserves public-key and signature validation, guards empty inputs, and adds coverage for repeated keys, cancelling keys, and batching thresholds. No changes affect fork choice, state transitions, XMSS, or SSZ.

Tests could not run because Rustup requires writes outside the permitted workspace. Dependency source verification was also blocked by network access.


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

@github-actions

Copy link
Copy Markdown

🤖 Kimi Code Review

No blocking correctness or security issues found.

  • sum_validated_points preserves the existing validation invariant and correctly guards the empty slice before calling MultiPoint::add.
  • fast_aggregate_verify retains its empty-key rejection and uses the same DST and verification mode; individually validated keys make skipping another group check on their sum appropriate.
  • eth_aggregate_pubkeys preserves the non-empty specification assertion.
  • The tests cover important edge cases: repeated keys, threshold sizes, identity sums from cancelling keys, and equivalence with the previous sequential aggregation.

Non-blocking performance consideration: MultiPoint::add can use blst’s internal thread pool for inputs of 384 points or more. Since callers may already parallelize verification with Rayon, it would be useful to benchmark large committees under concurrent load to rule out thread-pool oversubscription. Otherwise, this looks good.


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

@github-actions

Copy link
Copy Markdown

🤖 DeepSeek Code Review

The change is correct and well-tested. Moving to MultiPoint::add + fast_aggregate_verify_pre_aggregated is a sound optimization for the already-validated key path, and the added tests cover empty input, single keys, duplicates, cancellation, and the thread-pool threshold.

Minor comment only:

  • In crates/common/crypto/src/bls.rs around the new fast_aggregate_verify comment (signature was validated above, which is why the group check stays off here): the false flag is pk_validate, and the reason it is safe is that validated_pubkeys already ran key_validate on each point. Signature validation is unrelated to that flag. Consider rewording to something like “the points were validated above, so the aggregate key does not need re-checking” to avoid misleading future readers.

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

@MegaRedHand
MegaRedHand force-pushed the perf/bls-batched-key-sums branch from 4426ef0 to be952d1 Compare November 19, 2026 18:40
… 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.
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.
…mes 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`.
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.
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.
@MegaRedHand
MegaRedHand force-pushed the perf/bls-batched-key-sums branch from be952d1 to 94b92bb Compare November 19, 2026 19:21
@MegaRedHand
MegaRedHand enabled stacked auto-merge November 19, 2026 19:29
@MegaRedHand
MegaRedHand added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 921a438 Nov 19, 2026
11 checks passed
@MegaRedHand
MegaRedHand deleted the perf/bls-batched-key-sums branch November 19, 2026 19:41
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