# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-capability-grant-whose-issuer-chain-is-valid-but-whose-trust-root-attestation-has-silently-r.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-capability-grant-whose-issuer-chain-is-valid-but-whose-trust-root-attestation-has-silently-r.warrant.json
# Markdown SHA-256:   9687a81f82cc6854f2e7dee6c25fdb2880d48a3d50754847f6b50937ea8e911f
# Code AST SHA-256:   d4de8474c778d48f14ad1dec7277b9205abdbb8fca131f93d3d800454b5c6830
# Verified Classes:   TrustEpochError, TrustEpochMismatch, TrustEpochRevoked, PinnedAnchor, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional

class TrustEpochError(RuntimeError):
    pass

class TrustEpochMismatch(TrustEpochError):
    """Warrant epoch unknown to verifier, or signature fails against pinned material."""
    pass

class TrustEpochRevoked(TrustEpochError):
    """Warrant epoch is revoked; signature validity is irrelevant."""
    pass

@dataclass(frozen=True)
class PinnedAnchor:
    root_id: str
    key_epoch: int
    pubkey: bytes
    not_after: float          # epoch validity end
    revoked: bool = False

@dataclass(frozen=True)
class Warrant:
    issuer_root_id: str
    iss_epoch: int
    signature: bytes
    signed_payload: bytes

def verify_warrant(
    warrant: Warrant,
    anchors: dict[tuple[str, int], PinnedAnchor],
    now: float,
    verify_sig,
) -> None:
    """
    Enforce epoch-pinned trust bootstrap. `anchors` is keyed by (root_id, epoch)
    and is populated ONLY at bootstrap or via attested rotation records — never
    by lazy discovery refresh. Raises on any boundary violation.
    """
    key = (warrant.issuer_root_id, warrant.iss_epoch)
    anchor: Optional[PinnedAnchor] = anchors.get(key)

    if anchor is None:
        # Do NOT fall back to "current key" from discovery. That is the
        # unpinned-reference substitution surface.
        raise TrustEpochMismatch(
            f"no pinned anchor for root={warrant.issuer_root_id} "
            f"epoch={warrant.iss_epoch}; re-bootstrap required"
        )

    if anchor.revoked:
        raise TrustEpochRevoked(
            f"epoch {warrant.iss_epoch} of {warrant.issuer_root_id} is revoked"
        )

    if now >= anchor.not_after:
        raise TrustEpochMismatch(
            f"epoch {warrant.iss_epoch} expired at {anchor.not_after}, now={now}"
        )

    if not verify_sig(anchor.pubkey, warrant.signed_payload, warrant.signature):
        # Signature failure against the *pinned* key for this exact epoch is a
        # trust-epoch event, not a generic crypto failure: the issuer may have
        # signed with a different epoch's key.
        raise TrustEpochMismatch(
            f"signature does not verify against pinned key for "
            f"{warrant.issuer_root_id}@{warrant.iss_epoch}"
        )
    # Accepted: warrant is bound to the exact root key material the verifier
    # independently validated, within its validity window, and not revoked.
