Skip to content

feat(block): HeaderSigner interface — decouple block signing from scheme#4856

Draft
envestcc wants to merge 11 commits into
iotexproject:masterfrom
envestcc:bls-header-signer
Draft

feat(block): HeaderSigner interface — decouple block signing from scheme#4856
envestcc wants to merge 11 commits into
iotexproject:masterfrom
envestcc:bls-header-signer

Conversation

@envestcc

Copy link
Copy Markdown
Member

Stacks on #4851 (Y1 + Y1.5: `Header.pubkey` is now `Verifier`). The diff includes that PR's changes until it merges; review the last commit (`feat(block): HeaderSigner interface`).

Part of the BLS Producer Identity follow-up to IIP-52, alongside #4850 (system signer), #4851 (header dispatch), #4854 (BLS PoP / rogue-key fix), #4855 (roundCtx.IsProducer).

Why

Block header signing today is hard-coded to `crypto.PrivateKey` (secp256k1). Post-fork, block producers sign with BLS12-381 — but the block package shouldn't have to know which scheme is in use. `Header.pubkey` is already typed as the narrow `Verifier` interface (Y1.5) so verification is scheme-agnostic; this PR closes the symmetric gap on the signing side.

What

`block.HeaderSigner` — the minimal contract: `PubKey() Verifier` + `Sign(msg) ([]byte, error)`. Two thin adapters:

  • `ECDSAHeaderSigner` wraps `crypto.PrivateKey` — the pre-fork path
  • `BLSHeaderSigner` wraps `*crypto.BLS12381PrivateKey` — post-fork

`Builder.SignAndBuild` now takes `HeaderSigner` instead of `crypto.PrivateKey`. Callers wrap their secp256k1 key with `NewECDSAHeaderSigner` — mechanical at ~10 sites (1 production, rest tests). `TestingBuilder.SignAndBuild` is unchanged: it's an explicit test helper where the ECDSA-only signature is fine.

End-to-end tests verify that a block signed by either adapter round-trips through `Header.VerifySignature`, confirming the abstraction integrates with the Verifier path from Y1.

What's not in this PR

  • Mint integration that picks ECDSA vs BLS based on fork + config — depends on Y5 (`blsProducerPrivKey` config field) being available. Today `Mint` continues to pass an `ECDSAHeaderSigner` wrapping the producer's ECDSA key.

Test plan

  • `TestECDSAHeaderSigner`, `TestBLSHeaderSigner` — adapter correctness
  • `TestSignAndBuild_NilSigner` — defensive nil reject
  • `TestSignAndBuild_EndToEnd` (ECDSA + BLS) — signed block VerifySignature round-trip
  • Full `./blockchain/block/ ./state/factory/ ./consensus/scheme/rolldpos/` test suite passing
  • `go build ./...` clean

🤖 Generated with Claude Code

envestcc and others added 11 commits May 27, 2026 10:53
…te (IIP-52)

Scaffolding for the BLS signature aggregation work tracked in IIP-52. No
behavior change yet: EnableBLSAggregation is gated on IsToBeEnabled, and the
BLS keys plumbed into rollDPoSCtx are not yet used to sign or verify
endorsements.

- blockchain/config.go: add Chain.BLSProducerPrivKey (comma-separated hex)
  and BLSProducerPrivateKeys(). Empty value falls back to deriving each BLS
  key from the corresponding ECDSA producer key via
  crypto.GenerateBLS12381PrivateKey.
- consensus/scheme/rolldpos: Builder.SetBLSPriKey; NewRollDPoSCtx accepts
  []*crypto.BLS12381PrivateKey aligned 1:1 with producer ECDSA keys;
  rollDPoSCtx stores them on blsPriKeys for the upcoming signing path.
- consensus/consensus.go: wire SetBLSPriKey(cfg.Chain.BLSProducerPrivateKeys()).
- action/protocol/context.go: FeatureCtx.EnableBLSAggregation gated on
  g.IsToBeEnabled(height); flips to a named hardfork height later.
- go.mod: bump iotex-proto to envestcc/iotex-proto bls-aggregate (52e72a6)
  for the BlockFooter aggregated_signature / signer_bitmap fields and the
  BLSEndorsement message.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Switches consensus vote signing to BLS12-381 post-fork, reusing the
existing Endorsement type and dispatching on signature length (65B
secp256k1 vs 96B BLS). Receiver verification and endorsement-manager
quorum integration land in a follow-up PR; with the feature gate parked
at IsToBeEnabled this commit is dead code in production.

- endorsement.EndorseBLS / VerifyBLSEndorsement: thin helpers that
  produce / verify a regular *Endorsement whose signature field carries
  a BLS sig. Endorser remains the delegate's secp256k1 producer key so
  the existing Endorser().Address() path still resolves the iotex
  address; receivers look up the BLS verifying key from candidate state
  by that address.
- ConsensusConfig.BLSAggregationEnabled(height): feature gate wired off
  Genesis.ToBeEnabledBlockHeight. Will be re-pointed at a named hardfork
  height once the full Phase-2 stack lands.
- rollDPoSCtx.newEndorsement / endorseBlockProposal: post-fork, sign
  PROPOSAL, LOCK and COMMIT votes plus the proposer's wrapping
  endorsement with BLS (skipping delegates without a configured BLS
  key). Block header signing remains on the ECDSA producer key — that
  signature ties the block to chain identity and is unrelated to the
  consensus vote layer.
- The proposer's producerKey (ECDSA + BLS + address) is threaded
  through Proposal / mintNewBlock / endorseBlockProposal so the branch
  can pick the right key without a separate lookup.
- iotex-proto bump to envestcc/iotex-proto@e4439ef (PR iotexproject#169): clarifies
  Endorsement.signature semantics (pre-fork 65B secp256k1, post-fork
  96B BLS, distinguished by length).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Stacks on top of the BLS sender PR. Wires up the receiver side so BLS-
signed consensus endorsements are accepted, verified against pubkeys
resolved from candidate state, and counted toward quorum. With the
feature gate parked at IsToBeEnabled this is dead code in production;
intended for local iteration until the sender PRs land.

- BLSPubKeysByEpochFunc callback type + Builder.SetBLSPubKeysByEpochFunc
  wired through to NewRollDPoSCtx.
- consensus.go provides the implementation: reads the epoch's delegate
  list from candidate state and extracts each candidate's registered
  BLSPubKey, returning a map keyed by operator iotex address.
- roundCalculator caches the BLS pubkey index per round, decoded as
  *crypto.BLS12381PublicKey. UpdateRound carries it across height
  transitions inside an epoch and re-fetches on epoch boundaries.
- roundCtx.BLSPubKey(addr) accessor; roundCtx.verifyEndorsement(doc,
  en) dispatches on signature length (65B secp256k1 vs 96B BLS).
- rollDPoSCtx.VerifyEndorsement(height, doc, en) is the public entry
  point; same length-aware dispatch plus pre/post-fork gating —
  pre-fork rejects 96B sigs, post-fork rejects 65B sigs.
- HandleConsensusMsg replaces the unconditional ECDSA verify with the
  new VerifyEndorsement; CheckBlockProposer's proofOfLock replay path
  flows through round.AddVoteEndorsement which now dispatches on
  signature length internally, so BLS endorsements in proof-of-lock
  are verified transparently.
- endorsement/bls_endorsement_test.go: round-trip EndorseBLS /
  VerifyBLSEndorsement, plus negative cases (wrong pubkey, tampered
  document, tampered signature, nil inputs). Uses deterministic
  in-test keys (no identityset dep from this package).
- consensus/scheme/rolldpos/bls_verify_test.go: covers the two-layer
  dispatch — roundCtx.verifyEndorsement (signature-length branch, BLS
  pubkey lookup miss, mismatched pubkey) and rollDPoSCtx.VerifyEndorsement
  (length gating with the BLS aggregation feature flag in both
  directions). Plus sender tests that newEndorsement emits the
  expected signature scheme based on the feature gate.

11 new tests; everything in the affected packages still passes (51
tests total across endorsement/ and consensus/scheme/rolldpos/).
- Bundle delegate address with BLS pubkey into a single 'delegate'
  struct; roundCtx.delegates becomes []delegate, dropping the parallel
  blsPubKeys map. roundCalc.delegatesAt replaces blsPubKeysFor and
  merges both callbacks into one slice — single source of truth for
  the address/pubkey alignment.
- rollDPoSCtx.VerifyEndorsement takes a single *EndorsedConsensusMessage
  instead of (height, doc, en); the message already carries all three.
- Shrink VerifyEndorsement lock scope: snapshot round + feature flag
  under RLock, release before doing the (potentially slow) signature
  verification. Safe because *roundCtx is replaced, never mutated in
  place.

Test helpers + the two consumers (HandleConsensusMsg, roundCtx_test)
updated. All targeted tests pass.
Per follow-up review on PR iotexproject#4843: remove the separate
BLSPubKeysByEpochFunc callback. NodesSelectionByEpochFunc now returns
[]*Delegate (exported), where each Delegate pairs the operator address
with its decoded BLS12-381 public key. consensus.go builds these from
candidate state in one pass; roundCalculator stores them directly and
no longer needs a parallel lookup/merge step.

- Export delegate -> Delegate{Address, BLSPubKey}.
- NodesSelectionByEpochFunc: ([]string) -> ([]*Delegate).
- Drop BLSPubKeysByEpochFunc type, Builder.SetBLSPubKeysByEpochFunc,
  the NewRollDPoSCtx param, and roundCalculator.delegatesAt /
  blsPubKeysFor.
- roundCalculator.Delegates returns []*Delegate; Proposers extracts
  addresses; IsDelegate scans by address.
- consensus.go decodes each candidate's BLS pubkey once per epoch in
  delegatesByEpochFunc; proposersByEpochFunc reuses it.

Net -68 lines. Build + vet clean; targeted tests pass.
Per PR iotexproject#4843 review: UpdateRound was reaching into round.delegates
directly because Delegates() returned []string while the field is
[]*Delegate. Make the accessor return []*Delegate so UpdateRound (and
any future caller) can use round.Delegates() consistently.

- roundCtx.Delegates() now returns []*Delegate.
- endorsementManager.Log's (unused) delegates param retyped to
  []*Delegate.
- The one genuine []string consumer (ConsensusMetrics.LatestDelegates,
  an external metrics field) extracts addresses at the call site.
Phase 2 deliverable for IIP-52: proposer aggregates the per-block COMMIT
BLS signatures into a single 96-byte sig + a signer bitmap, stored in
BlockFooter.aggregated_signature and BlockFooter.signer_bitmap. Verifiers
reconstruct the signer set from the bitmap, look up each BLS pubkey from
the round's delegate index, and FastAggregateVerify the aggregate against
the shared COMMIT-vote hash.

- blockchain/block/footer.go: new fields aggregatedSignature, signerBitmap;
  proto round-trip; IsAggregated / AggregatedSignature / SignerBitmap
  accessors.
- blockchain/block/block.go: new Block.FinalizeWithAggregate; the one-shot
  contract is preserved via a commitTime witness so either path errors on
  second call.
- consensus/scheme/rolldpos/aggregate.go: aggregateCommitEndorsements
  builds the aggregate sig + bitmap from a slice of BLS COMMIT
  endorsements indexed against the round's delegates;
  bitmapSigners is the inverse for the verifier.
- consensus/scheme/rolldpos/rolldposctx.go: at commit time, branch on
  BLSAggregationEnabled and call FinalizeWithAggregate post-fork.
- consensus/scheme/rolldpos/rolldpos.go: ValidateBlockFooter routes
  aggregated footers through validateAggregatedFooter — bitmap →
  delegates → BLS pubkeys → BLSAggregateSignature.Verify, with a
  separate 2/3 majority check.
- action/protocol/staking/protocol.go: ActiveCandidates filters out
  candidates without a registered BLS pubkey once aggregation is
  enabled, so the aggregate signer set is well-defined.
- endorsement/endorsement.go: expose SigningHash so the verifier can
  reconstruct the COMMIT-vote hash from blk.CommitTime() outside an
  Endorsement struct.

All signers sign the same hash (deterministic ts from round start + TTL
sum), which is what FastAggregateVerify requires. 8 new unit tests cover
the aggregate round-trip, partial signer sets, rejection of non-BLS
endorsements / unknown endorsers, and bitmap edge cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Foundational PR (Y1) for the BLS Producer Identity follow-up to IIP-52.
Decouples Header from the secp256k1-only public key type so that the
existing methods can transparently handle BLS-signed headers post-fork.
No behavior change for pre-fork blocks.

- Header.pubkey (crypto.PublicKey) -> Header.producerPubkey ([]byte) raw
  storage. The signature scheme is implied by len(blockSig): 65 bytes
  secp256k1 (pre-fork), 96 bytes BLS12-381 (post-fork).
- Header.PublicKey() returns nil for non-secp256k1 producer pubkeys; new
  Header.ProducerPubKey() []byte exposes the raw bytes regardless of
  scheme. Callers that need a typed PublicKey for an address derivation
  should switch to ProducerAddress() or do a state lookup post-fork.
- Header.VerifySignature() dispatches on len(blockSig); BLS path uses
  crypto.BLS12381PublicKeyFromBytes + Verify against HashHeaderCore.
- Header.ProducerAddress() dispatches on len(blockSig). Pre-fork returns
  the io1... iotex address (existing behavior). Post-fork returns the
  hex encoding of the 48-byte BLS pubkey, which is the canonical
  post-fork operator identifier (per the IIP draft). Return type stays
  string; the format flips across the fork boundary.

Builder/testing setters write producerPubkey directly. blockindexer.go
is left as-is: its blk.PublicKey().Address() call returns nil for
BLS-signed headers and errors with "failed to get pubkey", which is
the safe fail-loud behavior until the per-block state lookup path
(populating BlockCtx.Producer with the candidate's Operator address)
lands in a later PR.

5 new unit tests cover the BLS dispatch paths: positive round-trip,
proto round-trip, wrong-scheme rejection, and empty-pubkey rejection.
27 existing block-package tests continue to pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
PR iotexproject#4851 review feedback (envestcc): storing the producer pubkey as []byte
sacrifices type information. Pre-Y1 it was crypto.PublicKey, which BLS
can't satisfy because the interface bundles ECDSA-shaped identity methods
(Address, Hash, EcdsaPublicKey) that have no meaningful BLS analogue —
forcing BLS through Address() would invite silent truncation in
hash.BytesToHash160 and common.BytesToAddress consumers.

Compromise: a new narrow interface block.Verifier {Bytes; Verify} that
both crypto.PublicKey (secp256k1) and *crypto.BLS12381PublicKey satisfy
today without modification. Header.pubkey moves from []byte to Verifier.

- VerifySignature: h.pubkey.Verify(digest, sig) — typed, no length switch
- ProducerAddress: type-switch on the stored Verifier instead of dispatch
  on len(blockSig) — intent is explicit ("I'm a BLS key, hex-encode me")
- LoadFromBlockHeaderProto: length-based dispatch lives only here, at the
  wire→typed boundary; downstream code sees the typed Verifier
- PublicKey() accessor: type-assert to crypto.PublicKey; returns nil for
  BLS-signed headers (existing behavior, but no longer re-parses the
  bytes on every call)
- ProducerPubKey(): pubkey.Bytes() with defensive copy
- Identity-derivation (Address/Hash) is deliberately absent from
  Verifier — BLS has no iotex address; the rationale is captured in
  verifier.go and the BLS Producer Identity IIP draft

Identity-shaped accessors that were length-dispatching are now
type-dispatching; tests updated to write the typed key into the field
rather than raw bytes. Added a decode-side rejection test for malformed
pubkey lengths.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Block header signing was hard-coded to crypto.PrivateKey (secp256k1).
Post-fork (BLS Producer Identity follow-up to IIP-52), block producers
sign with BLS12-381; the block package shouldn't have to know which
scheme is in use, only how to sign and how to store the resulting
pubkey on the header (which the Verifier interface from Y1 already
handles uniformly).

Introduces a small HeaderSigner interface:

  type HeaderSigner interface {
      PubKey() Verifier
      Sign(msg []byte) ([]byte, error)
  }

Two thin adapters wrap each scheme's private-key type:

  - ECDSAHeaderSigner: wraps crypto.PrivateKey (pre-fork path)
  - BLSHeaderSigner:   wraps *crypto.BLS12381PrivateKey (post-fork)

Builder.SignAndBuild now takes HeaderSigner instead of crypto.PrivateKey.
Existing callers wrap their secp256k1 key via NewECDSAHeaderSigner —
mechanical change at ~10 call sites (1 production, rest tests).
TestingBuilder.SignAndBuild is unchanged: it's an explicit test helper
where the ECDSA-only signature is fine.

Mint integration (selecting ECDSA vs BLS based on fork + config) lands
later — depends on Y5 (config schema for blsProducerPrivKey).

End-to-end test verifies that a block signed by either adapter
round-trips through Header.VerifySignature, confirming the abstraction
plays nicely with the Verifier-based verify path from Y1.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant