pqc_binary_format/
algorithm.rs

1//! Algorithm identifiers for supported post-quantum cryptographic algorithms.
2
3use serde::{Deserialize, Serialize};
4
5/// Algorithm identifier type
6pub type AlgorithmId = u16;
7
8/// Supported cryptographic algorithms with unique identifiers
9///
10/// Each algorithm has a unique 16-bit identifier used in the binary format.
11/// Identifiers are grouped by algorithm family for easy categorization.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[repr(u16)]
14pub enum Algorithm {
15    // Classical algorithms (0x0050-0x00FF)
16    /// Classical cryptography: X25519 + Ed25519 + AES-256-GCM
17    Classical = 0x0050,
18    /// Password-based classical encryption
19    PasswordClassical = 0x0051,
20
21    // Hybrid algorithms (0x0100-0x01FF)
22    /// Hybrid: ML-KEM-1024 + X25519 + ML-DSA-87 + Ed25519 + AES-256-GCM
23    Hybrid = 0x0100,
24
25    // Pure post-quantum algorithms (0x0200-0x02FF)
26    /// Post-quantum: ML-KEM-1024 + ML-DSA-87 + AES-256-GCM
27    PostQuantum = 0x0200,
28    /// Multi-algorithm runtime selection
29    MultiAlgorithm = 0x0201,
30    /// ML-KEM-1024 with AES-256-GCM
31    MlKem1024 = 0x0202,
32    /// Multi-KEM dual layer
33    MultiKem = 0x0203,
34    /// Multi-KEM triple layer
35    MultiKemTriple = 0x0204,
36    /// Quad-layer redundant security
37    QuadLayer = 0x0205,
38    /// Lattice-Code hybrid stack
39    LatticeCodeHybrid = 0x0206,
40    /// PQ3-Stack with forward secrecy
41    Pq3Stack = 0x0207,
42
43    // Max Secure series (0x0300-0x03FF)
44    /// Max Secure: PQ Lightweight
45    MaxSecureLightweight = 0x0300,
46    /// Max Secure: Pure PQ
47    MaxSecurePurePq = 0x0301,
48    /// Max Secure: Hybrid Transition
49    MaxSecureHybrid = 0x0302,
50    /// Max Secure: Stateless
51    MaxSecureStateless = 0x0303,
52    /// Max Secure: Crypto-Agile
53    MaxSecureCryptoAgile = 0x0304,
54    /// Max Secure: PQC + Zero-Knowledge
55    MaxSecurePqcZk = 0x0305,
56    /// Max Secure: Hybrid Transition
57    MaxSecureHybridTransition = 0x0306,
58
59    // FN-DSA series (Falcon-based signatures) (0x0400-0x04FF)
60    /// FN-DSA 512: Compact
61    FnDsa512Compact = 0x0400,
62    /// FN-DSA 1024: High-Security
63    FnDsa1024Security = 0x0401,
64    /// FN-DSA: Floating-Point Hardened
65    FnDsaFpHardened = 0x0402,
66    /// FN-DSA: Dual Signature
67    FnDsaDualSignature = 0x0403,
68    /// FN-DSA: Transition Stack
69    FnDsaTransition = 0x0404,
70    /// FN-DSA + Zero-Knowledge Stack
71    FnDsaZk = 0x0405,
72    /// FN-DSA + ZK Stack Enhanced
73    FnDsaZkStack = 0x0406,
74    /// FN-DSA: Transition Stack Enhanced
75    FnDsaTransitionStack = 0x0407,
76
77    // Experimental algorithms (0x0500-0x05FF)
78    /// Quantum-Inspired Lattice Fusion
79    QuantumLatticeFusion = 0x0500,
80    /// Post-ZK Homomorphic with LFHE 2023
81    PostZkHomomorphic = 0x0501,
82    /// Quantum-Resistant Consensus
83    QuantumResistantConsensus = 0x0502,
84    /// Entropy-Orchestrated PQ Stack
85    EntropyOrchestrated = 0x0503,
86    /// Lattice-Code Hybrid FN
87    LatticeCodeHybridFn = 0x0504,
88    /// AI-Synthesized Crypto-Agile
89    AiSynthesizedCryptoAgile = 0x0505,
90    /// Experimental Engine (generic)
91    Experimental = 0x0506,
92}
93
94impl Algorithm {
95    /// Convert u16 identifier to Algorithm enum
96    ///
97    /// # Example
98    ///
99    /// ```
100    /// use pqc_binary_format::Algorithm;
101    ///
102    /// let algo = Algorithm::from_id(0x0100).unwrap();
103    /// assert_eq!(algo, Algorithm::Hybrid);
104    /// ```
105    #[must_use]
106    pub fn from_id(id: u16) -> Option<Self> {
107        match id {
108            0x0050 => Some(Self::Classical),
109            0x0051 => Some(Self::PasswordClassical),
110            0x0100 => Some(Self::Hybrid),
111            0x0200 => Some(Self::PostQuantum),
112            0x0201 => Some(Self::MultiAlgorithm),
113            0x0202 => Some(Self::MlKem1024),
114            0x0203 => Some(Self::MultiKem),
115            0x0204 => Some(Self::MultiKemTriple),
116            0x0205 => Some(Self::QuadLayer),
117            0x0206 => Some(Self::LatticeCodeHybrid),
118            0x0207 => Some(Self::Pq3Stack),
119            0x0300 => Some(Self::MaxSecureLightweight),
120            0x0301 => Some(Self::MaxSecurePurePq),
121            0x0302 => Some(Self::MaxSecureHybrid),
122            0x0303 => Some(Self::MaxSecureStateless),
123            0x0304 => Some(Self::MaxSecureCryptoAgile),
124            0x0305 => Some(Self::MaxSecurePqcZk),
125            0x0306 => Some(Self::MaxSecureHybridTransition),
126            0x0400 => Some(Self::FnDsa512Compact),
127            0x0401 => Some(Self::FnDsa1024Security),
128            0x0402 => Some(Self::FnDsaFpHardened),
129            0x0403 => Some(Self::FnDsaDualSignature),
130            0x0404 => Some(Self::FnDsaTransition),
131            0x0405 => Some(Self::FnDsaZk),
132            0x0406 => Some(Self::FnDsaZkStack),
133            0x0407 => Some(Self::FnDsaTransitionStack),
134            0x0500 => Some(Self::QuantumLatticeFusion),
135            0x0501 => Some(Self::PostZkHomomorphic),
136            0x0502 => Some(Self::QuantumResistantConsensus),
137            0x0503 => Some(Self::EntropyOrchestrated),
138            0x0504 => Some(Self::LatticeCodeHybridFn),
139            0x0505 => Some(Self::AiSynthesizedCryptoAgile),
140            0x0506 => Some(Self::Experimental),
141            _ => None,
142        }
143    }
144
145    /// Get u16 identifier for this algorithm
146    ///
147    /// # Example
148    ///
149    /// ```
150    /// use pqc_binary_format::Algorithm;
151    ///
152    /// assert_eq!(Algorithm::Hybrid.as_id(), 0x0100);
153    /// ```
154    #[must_use]
155    pub const fn as_id(self) -> u16 {
156        self as u16
157    }
158
159    /// Get human-readable name for this algorithm
160    ///
161    /// # Example
162    ///
163    /// ```
164    /// use pqc_binary_format::Algorithm;
165    ///
166    /// assert_eq!(Algorithm::Hybrid.name(), "Hybrid");
167    /// ```
168    #[must_use]
169    pub const fn name(self) -> &'static str {
170        match self {
171            Self::Classical => "Classical",
172            Self::PasswordClassical => "Password Classical",
173            Self::Hybrid => "Hybrid",
174            Self::PostQuantum => "Post-Quantum",
175            Self::MultiAlgorithm => "Multi-Algorithm",
176            Self::MlKem1024 => "ML-KEM-1024",
177            Self::MultiKem => "Multi-KEM Dual Layer",
178            Self::MultiKemTriple => "Multi-KEM Triple Layer",
179            Self::QuadLayer => "Quad-Layer",
180            Self::LatticeCodeHybrid => "Lattice-Code Hybrid",
181            Self::Pq3Stack => "PQ3-Stack",
182            Self::MaxSecureLightweight => "Max Secure: PQ Lightweight",
183            Self::MaxSecurePurePq => "Max Secure: Pure PQ",
184            Self::MaxSecureHybrid => "Max Secure: Hybrid",
185            Self::MaxSecureStateless => "Max Secure: Stateless",
186            Self::MaxSecureCryptoAgile => "Max Secure: Crypto-Agile",
187            Self::MaxSecurePqcZk => "Max Secure: PQC + ZK",
188            Self::FnDsa512Compact => "FN-DSA 512: Compact",
189            Self::FnDsa1024Security => "FN-DSA 1024: High-Security",
190            Self::FnDsaFpHardened => "FN-DSA: Floating-Point Hardened",
191            Self::FnDsaDualSignature => "FN-DSA: Dual Signature",
192            Self::FnDsaTransition => "FN-DSA: Transition Stack",
193            Self::FnDsaZk => "FN-DSA + ZK Stack",
194            Self::FnDsaZkStack => "FN-DSA + ZK Stack Enhanced",
195            Self::FnDsaTransitionStack => "FN-DSA: Transition Stack Enhanced",
196            Self::MaxSecureHybridTransition => "Max Secure: Hybrid Transition",
197            Self::QuantumLatticeFusion => "Quantum-Inspired Lattice Fusion",
198            Self::PostZkHomomorphic => "Post-ZK Homomorphic",
199            Self::QuantumResistantConsensus => "Quantum-Resistant Consensus",
200            Self::EntropyOrchestrated => "Entropy-Orchestrated",
201            Self::LatticeCodeHybridFn => "Lattice-Code Hybrid FN",
202            Self::AiSynthesizedCryptoAgile => "AI-Synthesized Crypto-Agile",
203            Self::Experimental => "Experimental Engine",
204        }
205    }
206
207    /// Check if this algorithm is marked as experimental
208    ///
209    /// Experimental algorithms may have reduced security guarantees
210    /// and are intended for research purposes.
211    #[must_use]
212    pub const fn is_experimental(self) -> bool {
213        matches!(
214            self,
215            Self::QuantumLatticeFusion
216                | Self::PostZkHomomorphic
217                | Self::QuantumResistantConsensus
218                | Self::EntropyOrchestrated
219                | Self::LatticeCodeHybridFn
220                | Self::AiSynthesizedCryptoAgile
221                | Self::Experimental
222        )
223    }
224
225    /// Get all defined algorithm identifiers
226    ///
227    /// Useful for iteration and testing.
228    #[must_use]
229    pub fn all() -> Vec<Self> {
230        vec![
231            Self::Classical,
232            Self::PasswordClassical,
233            Self::Hybrid,
234            Self::PostQuantum,
235            Self::MultiAlgorithm,
236            Self::MlKem1024,
237            Self::MultiKem,
238            Self::MultiKemTriple,
239            Self::QuadLayer,
240            Self::LatticeCodeHybrid,
241            Self::Pq3Stack,
242            Self::MaxSecureLightweight,
243            Self::MaxSecurePurePq,
244            Self::MaxSecureHybrid,
245            Self::MaxSecureStateless,
246            Self::MaxSecureCryptoAgile,
247            Self::MaxSecurePqcZk,
248            Self::MaxSecureHybridTransition,
249            Self::FnDsa512Compact,
250            Self::FnDsa1024Security,
251            Self::FnDsaFpHardened,
252            Self::FnDsaDualSignature,
253            Self::FnDsaTransition,
254            Self::FnDsaZk,
255            Self::FnDsaZkStack,
256            Self::FnDsaTransitionStack,
257            Self::QuantumLatticeFusion,
258            Self::PostZkHomomorphic,
259            Self::QuantumResistantConsensus,
260            Self::EntropyOrchestrated,
261            Self::LatticeCodeHybridFn,
262            Self::AiSynthesizedCryptoAgile,
263            Self::Experimental,
264        ]
265    }
266}
267
268#[cfg(test)]
269mod tests {
270    use super::*;
271
272    #[test]
273    fn test_algorithm_roundtrip() {
274        for algo in Algorithm::all() {
275            let id = algo.as_id();
276            let recovered = Algorithm::from_id(id).unwrap();
277            assert_eq!(algo, recovered);
278        }
279    }
280
281    #[test]
282    fn test_invalid_algorithm_id() {
283        assert!(Algorithm::from_id(0xFFFF).is_none());
284    }
285
286    #[test]
287    fn test_experimental_detection() {
288        assert!(Algorithm::QuantumLatticeFusion.is_experimental());
289        assert!(!Algorithm::Hybrid.is_experimental());
290    }
291}