# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-quorum-equivocation-in-bft-task-commitment-registries-where-attestation-weight-is-decoupled-from-identity-count.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-quorum-equivocation-in-bft-task-commitment-registries-where-attestation-weight-is-decoupled-from-identity-count.warrant.json
# Markdown SHA-256:   bf7282faf95e598c55fafece59067536bb5a7dfa912f71502f2ed23821454831
# Code AST SHA-256:   454631968e567dcbbfe550d04bacf79d47492d7c90e5003d37e1da2c097ac0d4
# Verified Classes:   QuorumEquivocationRisk
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class QuorumEquivocationRisk(RuntimeError):
    """Raised when a quorum certificate is arithmetically sufficient but
    structurally unable to intersect an honest quorum with enough weight
    to detect cross-view equivocation."""
    pass


def validate_weighted_certificate(
    signer_weights: dict[str, float],
    total_weight: float,
    f: int,
    weight_threshold: float = 2.0 / 3.0,
) -> None:
    """
    signer_weights: mapping signer_id -> weight contributed to this certificate
    total_weight:   total registry weight (all agents, honest + byzantine)
    f:              max byzantine faults tolerated
    """
    if total_weight <= 0:
        raise QuorumEquivocationRisk("total_weight must be positive")

    quorum_weight = sum(signer_weights.values())
    if quorum_weight < weight_threshold * total_weight:
        raise QuorumEquivocationRisk(
            f"insufficient weight: {quorum_weight:.4f} < "
            f"{weight_threshold * total_weight:.4f}"
        )

    # Structural safety check: no single signer may dominate the quorum
    # beyond the point where honest intersection weight can reach 1/(3f+1).
    max_share = max(signer_weights.values()) / quorum_weight
    safe_ceiling = weight_threshold - 1.0 / (3 * f + 1)

    if max_share >= safe_ceiling:
        heaviest = max(signer_weights, key=signer_weights.get)
        raise QuorumEquivocationRisk(
            f"signer {heaviest!r} holds {max_share:.4f} of quorum weight, "
            f"at or above structural ceiling {safe_ceiling:.4f}; "
            f"honest intersection weight cannot reach 1/(3f+1)="
            f"{1.0/(3*f+1):.4f}. Certificate is equivocation-vulnerable."
        )

    # Also enforce a distinct-signer floor so a single heavy signer cannot
    # masquerade as a multi-party quorum.
    distinct = len(signer_weights)
    if distinct < f + 1:
        raise QuorumEquivocationRisk(
            f"only {distinct} distinct signers; need at least {f + 1}"
        )
