Skip to main content

privacy/
error.rs

1//! Unified error types for the PQCPrivacy crate.
2//!
3//! All modules surface errors through [`PrivacyError`], enabling
4//! composable error handling across the full framework.
5
6extern crate alloc;
7use alloc::string::{String, ToString};
8
9use thiserror::Error;
10
11/// Top-level error type for the PQCPrivacy framework.
12#[derive(Debug, Error)]
13pub enum PrivacyError {
14    // ── Hypergraph ─────────────────────────────────────────
15    #[error("Hypergraph vertex not found: {0}")]
16    VertexNotFound(String),
17    #[error("Hyperedge formation failed: CHSH bound not satisfied (got {got:.4}, need >{threshold:.4})")]
18    ChshBoundViolation { got: f64, threshold: f64 },
19    #[error("Manifold dimension out of range: {0}")]
20    InvalidDimension(usize),
21
22    // ── ZK Primitives ────────────────────────────
23    #[error("ZK proof generation failed: {0}")]
24    ProofGenerationFailed(String),
25    #[error("ZK proof verification failed")]
26    ProofVerificationFailed,
27    #[error("Public inputs do not match proof")]
28    PublicInputMismatch,
29    #[error("Invalid proof encoding")]
30    InvalidProofEncoding,
31    #[error("Recursive aggregation depth exceeded: {0}")]
32    AggregationDepthExceeded(usize),
33    #[error("Entanglement CHSH violation not achieved: {0:.4}")]
34    EntanglementFailed(f64),
35
36    // ── Chaos ────────────────────────────────────
37    #[error("Chaos attractor stalled (Lyapunov exponent {0:.4} < 4.5)")]
38    AttractorStalled(f64),
39    #[error("Chaos oracle entropy quality check failed: H_min={0:.4}")]
40    EntropyQualityFailed(f64),
41    #[error("Both Chua and Rössler attractors stalled")]
42    AllAttractorsStalled,
43
44    // ── Differential Privacy ─────────────────────────────
45    #[error("Privacy budget exhausted: ε={epsilon:.6} > limit={limit:.6}")]
46    PrivacyBudgetExhausted { epsilon: f64, limit: f64 },
47    #[error("Rényi composition overflow at α={alpha}")]
48    RenyiCompositionOverflow { alpha: u32 },
49    #[error("Invalid sensitivity value: {0}")]
50    InvalidSensitivity(f64),
51
52    // ── TupleChain Ledger ─────────────────────────────────
53    #[error("Tuple expired at timestamp {0}")]
54    TupleExpired(u64),
55    #[error("Tuple not found: {0}")]
56    TupleNotFound(String),
57    #[error("Homomorphic operation failed: {0}")]
58    HomomorphicOpFailed(String),
59    #[error("Ledger anchor failed: {0}")]
60    AnchorFailed(String),
61
62    // ── QFKH Key Hopping ──────────────────────────────────
63    #[error("Key hop interval exceeded: {elapsed_ms}ms > {limit_ms}ms")]
64    KeyHopIntervalExceeded { elapsed_ms: u64, limit_ms: u64 },
65    #[error("Ratchet state corrupted")]
66    RatchetCorrupted,
67    #[error("Key zeroization failed")]
68    ZeroizationFailed,
69
70    // ── Genomic / QTAID ──────────────────────────────
71    #[error("SNP match below threshold: {matched}/{total} < {threshold}")]
72    SnpMatchFailed { matched: u32, total: u32, threshold: u32 },
73    #[error("Genomic token minting failed: {0}")]
74    TokenMintFailed(String),
75    #[error("Biometric revocation failed: {0}")]
76    RevocationFailed(String),
77    #[error("Invalid allele encoding: {0}")]
78    InvalidAllele(String),
79
80    // ── Mesh / DW3B ──────────────────────────────────
81    #[error("Mesh node unreachable: {0}")]
82    NodeUnreachable(String),
83    #[error("Sphinx packet construction failed: {0}")]
84    SphinxFailed(String),
85    #[error("Bloom filter false positive rate exceeded: {0:.4}")]
86    BloomFpExceeded(f64),
87    #[error("Sybil attack detected: stake below threshold {0}")]
88    SybilDetected(u64),
89    #[error("Governance vote failed: {0}")]
90    GovernanceFailed(String),
91    #[error("Key distribution threshold not met: {got}/{needed}")]
92    ThresholdNotMet { got: usize, needed: usize },
93    #[error("Entropy elasticity scaling failed: {0}")]
94    EntropyScalingFailed(String),
95
96    // ── Sanctuary Vault ──────────────────────────────────
97    #[error("FHE encryption failed: {0}")]
98    FheEncryptionFailed(String),
99    #[error("Shard reconstruction failed: {got}/{needed} shards")]
100    ShardReconstructionFailed { got: usize, needed: usize },
101    #[error("Vault access denied: {0}")]
102    VaultAccessDenied(String),
103
104    // ── Messenger ─────────────────────────────────────────
105    #[error("P2P connection failed: {0}")]
106    P2pConnectionFailed(String),
107    #[error("Metadata obliteration failed: {0}")]
108    MetadataObliterationFailed(String),
109
110    // ── SCIF Viewer ───────────────────────────────────────
111    #[error("Clearance proof failed: tier {required} required, got {got}")]
112    ClearanceFailed { required: u8, got: u8 },
113    #[error("FHE redaction failed: {0}")]
114    RedactionFailed(String),
115
116    // ── Interfaces UNI/UVI ────────────────────────────
117    #[error("IBC routing failed: {0}")]
118    IbcRoutingFailed(String),
119    #[error("WASM circuit execution failed: {0}")]
120    WasmExecutionFailed(String),
121    #[error("Capability not found: {0}")]
122    CapabilityNotFound(String),
123
124    // ── Enclave / WAVEN ───────────────────────────────────
125    #[error("Memory page access denied: key={key}, page={page}")]
126    PageAccessDenied { key: u8, page: usize },
127    #[error("Enclave attestation failed: {0}")]
128    AttestationFailed(String),
129
130    // ── Compression ───────────────────────────────────────
131    #[error("Fractal IFS compression failed: {0}")]
132    CompressionFailed(String),
133    #[error("Decompression failed: {0}")]
134    DecompressionFailed(String),
135
136    // ── Serialization ────────────────────────────────────
137    #[error("Icosuple serialization failed: size {size} > limit {limit}")]
138    IcosupleTooBig { size: usize, limit: usize },
139    #[error("Icosuple deserialization failed: {0}")]
140    IcosupleDeserializeFailed(String),
141
142    // ── Chronosync ────────────────────────────────────────
143    #[error("Poset resolution conflict: {0}")]
144    PosetConflict(String),
145    #[error("Causal consistency violation: {0}")]
146    CausalViolation(String),
147
148    // ── Generic ───────────────────────────────────────────────────────────
149    #[error("Encoding error: {0}")]
150    EncodingError(String),
151    #[error("Serialization error: {0}")]
152    SerializationError(String),
153    #[error("Internal error: {0}")]
154    Internal(String),
155}
156
157impl From<serde_json::Error> for PrivacyError {
158    fn from(e: serde_json::Error) -> Self {
159        PrivacyError::SerializationError(e.to_string())
160    }
161}