Skip to main content

lib_q_types/
lib.rs

1//! Shared algorithm identifiers and categories for lib-Q.
2//!
3//! This crate is the lowest dependency layer: implementation crates can depend on
4//! `lib-q-types` for `Algorithm` / `AlgorithmCategory` without pulling in `lib-q-core`.
5#![no_std]
6#![deny(unsafe_code)]
7#![deny(unused_qualifications)]
8
9pub mod hqc;
10
11#[cfg(feature = "wasm")]
12use wasm_bindgen::prelude::*;
13
14/// Algorithm identifiers for cryptographic operations
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[cfg_attr(feature = "wasm", wasm_bindgen)]
18pub enum Algorithm {
19    // KEM algorithms
20    MlKem512,
21    MlKem768,
22    MlKem1024,
23    CbKem348864,
24    CbKem460896,
25    CbKem6688128,
26    CbKem6960119,
27    CbKem8192128,
28    Hqc128,
29    Hqc192,
30    Hqc256,
31
32    // Signature algorithms
33    MlDsa44,
34    MlDsa65,
35    MlDsa87,
36    FnDsa,
37    FnDsa512,
38    FnDsa1024,
39    SlhDsaSha256128fRobust,
40    SlhDsaSha256192fRobust,
41    SlhDsaSha256256fRobust,
42    SlhDsaShake256128fRobust,
43    SlhDsaShake256192fRobust,
44    SlhDsaShake256256fRobust,
45
46    // Hash algorithms
47    Shake128,
48    Shake256,
49    CShake128,
50    CShake256,
51    Sha3_224,
52    Sha3_256,
53    Sha3_384,
54    Sha3_512,
55    Keccak224,
56    Keccak256,
57    Keccak384,
58    Keccak512,
59    Kt128,
60    Kt256,
61    TurboShake128,
62    TurboShake256,
63    Kmac128,
64    Kmac256,
65    TupleHash128,
66    TupleHash256,
67    ParallelHash128,
68    ParallelHash256,
69
70    // SHA-2 algorithms
71    Sha224,
72    Sha256,
73    Sha384,
74    Sha512,
75    Sha512_224,
76    Sha512_256,
77
78    // AEAD algorithms
79    Saturnin,
80    Shake256Aead,
81    DuplexSpongeAead,
82    TweakAead,
83    RomulusN,
84    RomulusM,
85
86    /// Privacy-protocol identifiers (not standalone KEM/sig/hash providers).
87    LatticeRingSignature,
88    LatticeBlindIssuance,
89    LatticeAnonymousToken,
90    LatticeNullifierRegistry,
91    /// Witness-derived nullifier mode (SHAKE256 over opening witness wire; see `lib-q-lattice-zkp`).
92    LatticeWitnessNullifier,
93    /// DualRing-LB (CCS 2021 Alg. 3 aggregated verify on Ajtai openings, `lib-q-ring-sig`).
94    LatticeDualRingLb,
95    /// ML-KEM-768 layered encapsulation with Saturnin AEAD per hop (mix-layer transport).
96    MixOnionRouting,
97    /// SHAKE256 session token and stateless retry-cookie derivation for resumption handshakes.
98    SessionResumptionBinding,
99}
100
101impl Algorithm {
102    /// Get the security level for this algorithm
103    pub fn security_level(&self) -> u32 {
104        match self {
105            // Level 1 (128-bit security)
106            Algorithm::MlKem512 => 1,
107            Algorithm::CbKem348864 => 1,
108            Algorithm::Hqc128 => 1,
109            Algorithm::MlDsa44 => 1,
110            Algorithm::FnDsa => 1,
111            Algorithm::FnDsa512 => 1,
112            Algorithm::SlhDsaSha256128fRobust => 1,
113            Algorithm::SlhDsaShake256128fRobust => 1,
114
115            // Level 3 (192-bit security)
116            Algorithm::MlKem768 => 3,
117            Algorithm::CbKem460896 => 3,
118            Algorithm::Hqc192 => 3,
119            Algorithm::MlDsa65 => 3,
120            Algorithm::SlhDsaSha256192fRobust => 3,
121            Algorithm::SlhDsaShake256192fRobust => 3,
122
123            // Level 4 (256-bit security)
124            Algorithm::MlKem1024 => 4,
125            Algorithm::CbKem6688128 => 4,
126            Algorithm::CbKem6960119 => 4,
127            Algorithm::Hqc256 => 4,
128            Algorithm::MlDsa87 => 4,
129            Algorithm::SlhDsaSha256256fRobust => 5,
130            Algorithm::SlhDsaShake256256fRobust => 5,
131
132            // Level 5 (256-bit security)
133            Algorithm::FnDsa1024 => 5,
134
135            // Level 5 (256-bit security, higher performance)
136            Algorithm::CbKem8192128 => 5,
137
138            // Hash algorithms don't have security levels
139            Algorithm::Shake128 |
140            Algorithm::Shake256 |
141            Algorithm::CShake128 |
142            Algorithm::CShake256 |
143            Algorithm::Sha3_224 |
144            Algorithm::Sha3_256 |
145            Algorithm::Sha3_384 |
146            Algorithm::Sha3_512 |
147            Algorithm::Keccak224 |
148            Algorithm::Keccak256 |
149            Algorithm::Keccak384 |
150            Algorithm::Keccak512 |
151            Algorithm::Kt128 |
152            Algorithm::Kt256 |
153            Algorithm::TurboShake128 |
154            Algorithm::TurboShake256 |
155            Algorithm::Kmac128 |
156            Algorithm::Kmac256 |
157            Algorithm::TupleHash128 |
158            Algorithm::TupleHash256 |
159            Algorithm::ParallelHash128 |
160            Algorithm::ParallelHash256 |
161            Algorithm::Sha224 |
162            Algorithm::Sha256 |
163            Algorithm::Sha384 |
164            Algorithm::Sha512 |
165            Algorithm::Sha512_224 |
166            Algorithm::Sha512_256 => 0,
167
168            // AEAD algorithms
169            Algorithm::Saturnin => 1,
170            Algorithm::Shake256Aead => 1,
171            Algorithm::DuplexSpongeAead => 4,
172            Algorithm::TweakAead => 4,
173            Algorithm::RomulusN => 1,
174            Algorithm::RomulusM => 1,
175
176            Algorithm::LatticeRingSignature |
177            Algorithm::LatticeBlindIssuance |
178            Algorithm::LatticeAnonymousToken |
179            Algorithm::LatticeNullifierRegistry |
180            Algorithm::LatticeWitnessNullifier |
181            Algorithm::LatticeDualRingLb |
182            Algorithm::MixOnionRouting |
183            Algorithm::SessionResumptionBinding => 3,
184        }
185    }
186
187    /// Get the algorithm category
188    pub fn category(&self) -> AlgorithmCategory {
189        match self {
190            Algorithm::MlKem512 |
191            Algorithm::MlKem768 |
192            Algorithm::MlKem1024 |
193            Algorithm::CbKem348864 |
194            Algorithm::CbKem460896 |
195            Algorithm::CbKem6688128 |
196            Algorithm::CbKem6960119 |
197            Algorithm::CbKem8192128 |
198            Algorithm::Hqc128 |
199            Algorithm::Hqc192 |
200            Algorithm::Hqc256 => AlgorithmCategory::Kem,
201
202            Algorithm::MlDsa44 |
203            Algorithm::MlDsa65 |
204            Algorithm::MlDsa87 |
205            Algorithm::FnDsa |
206            Algorithm::FnDsa512 |
207            Algorithm::FnDsa1024 |
208            Algorithm::SlhDsaSha256128fRobust |
209            Algorithm::SlhDsaSha256192fRobust |
210            Algorithm::SlhDsaSha256256fRobust |
211            Algorithm::SlhDsaShake256128fRobust |
212            Algorithm::SlhDsaShake256192fRobust |
213            Algorithm::SlhDsaShake256256fRobust => AlgorithmCategory::Signature,
214
215            Algorithm::Shake128 |
216            Algorithm::Shake256 |
217            Algorithm::CShake128 |
218            Algorithm::CShake256 |
219            Algorithm::Sha3_224 |
220            Algorithm::Sha3_256 |
221            Algorithm::Sha3_384 |
222            Algorithm::Sha3_512 |
223            Algorithm::Keccak224 |
224            Algorithm::Keccak256 |
225            Algorithm::Keccak384 |
226            Algorithm::Keccak512 |
227            Algorithm::Kt128 |
228            Algorithm::Kt256 |
229            Algorithm::TurboShake128 |
230            Algorithm::TurboShake256 |
231            Algorithm::Kmac128 |
232            Algorithm::Kmac256 |
233            Algorithm::TupleHash128 |
234            Algorithm::TupleHash256 |
235            Algorithm::ParallelHash128 |
236            Algorithm::ParallelHash256 |
237            Algorithm::Sha224 |
238            Algorithm::Sha256 |
239            Algorithm::Sha384 |
240            Algorithm::Sha512 |
241            Algorithm::Sha512_224 |
242            Algorithm::Sha512_256 => AlgorithmCategory::Hash,
243
244            // AEAD algorithms
245            Algorithm::Saturnin |
246            Algorithm::Shake256Aead |
247            Algorithm::DuplexSpongeAead |
248            Algorithm::TweakAead |
249            Algorithm::RomulusN |
250            Algorithm::RomulusM => AlgorithmCategory::Aead,
251
252            Algorithm::LatticeRingSignature |
253            Algorithm::LatticeBlindIssuance |
254            Algorithm::LatticeAnonymousToken |
255            Algorithm::LatticeNullifierRegistry |
256            Algorithm::LatticeWitnessNullifier |
257            Algorithm::LatticeDualRingLb |
258            Algorithm::MixOnionRouting |
259            Algorithm::SessionResumptionBinding => AlgorithmCategory::PrivacyProtocol,
260        }
261    }
262
263    /// Check if an algorithm supports a specific category
264    pub fn supports_category(&self, category: AlgorithmCategory) -> bool {
265        match self {
266            // Pure KEM algorithms
267            Algorithm::MlKem512 |
268            Algorithm::MlKem768 |
269            Algorithm::MlKem1024 |
270            Algorithm::CbKem348864 |
271            Algorithm::CbKem460896 |
272            Algorithm::CbKem6688128 |
273            Algorithm::CbKem6960119 |
274            Algorithm::CbKem8192128 |
275            Algorithm::Hqc128 |
276            Algorithm::Hqc192 |
277            Algorithm::Hqc256 => category == AlgorithmCategory::Kem,
278
279            // Pure signature algorithms
280            Algorithm::MlDsa44 |
281            Algorithm::MlDsa65 |
282            Algorithm::MlDsa87 |
283            Algorithm::FnDsa |
284            Algorithm::FnDsa512 |
285            Algorithm::FnDsa1024 |
286            Algorithm::SlhDsaSha256128fRobust |
287            Algorithm::SlhDsaSha256192fRobust |
288            Algorithm::SlhDsaSha256256fRobust |
289            Algorithm::SlhDsaShake256128fRobust |
290            Algorithm::SlhDsaShake256192fRobust |
291            Algorithm::SlhDsaShake256256fRobust => category == AlgorithmCategory::Signature,
292
293            // Pure hash algorithms
294            Algorithm::Shake128 |
295            Algorithm::Shake256 |
296            Algorithm::CShake128 |
297            Algorithm::CShake256 |
298            Algorithm::Sha3_224 |
299            Algorithm::Sha3_256 |
300            Algorithm::Sha3_384 |
301            Algorithm::Sha3_512 |
302            Algorithm::Keccak224 |
303            Algorithm::Keccak256 |
304            Algorithm::Keccak384 |
305            Algorithm::Keccak512 |
306            Algorithm::Kt128 |
307            Algorithm::Kt256 |
308            Algorithm::TurboShake128 |
309            Algorithm::TurboShake256 |
310            Algorithm::Kmac128 |
311            Algorithm::Kmac256 |
312            Algorithm::TupleHash128 |
313            Algorithm::TupleHash256 |
314            Algorithm::ParallelHash128 |
315            Algorithm::ParallelHash256 |
316            Algorithm::Sha224 |
317            Algorithm::Sha256 |
318            Algorithm::Sha384 |
319            Algorithm::Sha512 |
320            Algorithm::Sha512_224 |
321            Algorithm::Sha512_256 => category == AlgorithmCategory::Hash,
322
323            // Pure AEAD algorithms
324            Algorithm::Saturnin |
325            Algorithm::Shake256Aead |
326            Algorithm::DuplexSpongeAead |
327            Algorithm::TweakAead |
328            Algorithm::RomulusN |
329            Algorithm::RomulusM => category == AlgorithmCategory::Aead,
330
331            Algorithm::LatticeRingSignature |
332            Algorithm::LatticeBlindIssuance |
333            Algorithm::LatticeAnonymousToken |
334            Algorithm::LatticeNullifierRegistry |
335            Algorithm::LatticeWitnessNullifier |
336            Algorithm::LatticeDualRingLb |
337            Algorithm::MixOnionRouting |
338            Algorithm::SessionResumptionBinding => category == AlgorithmCategory::PrivacyProtocol,
339        }
340    }
341}
342
343/// Algorithm categories
344#[derive(Debug, Clone, Copy, PartialEq, Eq)]
345#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
346#[cfg_attr(feature = "wasm", wasm_bindgen)]
347pub enum AlgorithmCategory {
348    Kem,
349    Signature,
350    Hash,
351    Aead,
352    /// Anonymous credentials, mix-layer transport helpers, and related ZKP-adjacent protocols.
353    PrivacyProtocol,
354}
355
356/// Security levels for cryptographic algorithms
357#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
358#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
359#[cfg_attr(feature = "wasm", wasm_bindgen)]
360pub enum SecurityLevel {
361    Level1 = 1, // 128-bit security
362    Level3 = 3, // 192-bit security
363    Level4 = 4, // 256-bit security
364    Level5 = 5, // 256-bit security (higher performance)
365}
366
367impl SecurityLevel {
368    /// Convert from u32 to SecurityLevel
369    pub fn from_u32(level: u32) -> Option<Self> {
370        match level {
371            1 => Some(SecurityLevel::Level1),
372            3 => Some(SecurityLevel::Level3),
373            4 => Some(SecurityLevel::Level4),
374            5 => Some(SecurityLevel::Level5),
375            _ => None,
376        }
377    }
378
379    /// Convert to u32
380    pub fn as_u32(self) -> u32 {
381        self as u32
382    }
383}
384
385impl core::fmt::Display for Algorithm {
386    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
387        match self {
388            // KEM algorithms
389            Algorithm::MlKem512 => write!(f, "ML-KEM-512"),
390            Algorithm::MlKem768 => write!(f, "ML-KEM-768"),
391            Algorithm::MlKem1024 => write!(f, "ML-KEM-1024"),
392            Algorithm::CbKem348864 => write!(f, "CB-KEM-348864"),
393            Algorithm::CbKem460896 => write!(f, "CB-KEM-460896"),
394            Algorithm::CbKem6688128 => write!(f, "CB-KEM-6688128"),
395            Algorithm::CbKem6960119 => write!(f, "CB-KEM-6960119"),
396            Algorithm::CbKem8192128 => write!(f, "CB-KEM-8192128"),
397            Algorithm::Hqc128 => write!(f, "HQC-128"),
398            Algorithm::Hqc192 => write!(f, "HQC-192"),
399            Algorithm::Hqc256 => write!(f, "HQC-256"),
400
401            // Signature algorithms
402            Algorithm::MlDsa44 => write!(f, "ML-DSA-44"),
403            Algorithm::MlDsa65 => write!(f, "ML-DSA-65"),
404            Algorithm::MlDsa87 => write!(f, "ML-DSA-87"),
405            Algorithm::FnDsa => write!(f, "FN-DSA"),
406            Algorithm::FnDsa512 => write!(f, "FN-DSA-512"),
407            Algorithm::FnDsa1024 => write!(f, "FN-DSA-1024"),
408            Algorithm::SlhDsaSha256128fRobust => write!(f, "SLH-DSA-SHA256-128f-Robust"),
409            Algorithm::SlhDsaSha256192fRobust => write!(f, "SLH-DSA-SHA256-192f-Robust"),
410            Algorithm::SlhDsaSha256256fRobust => write!(f, "SLH-DSA-SHA256-256f-Robust"),
411            Algorithm::SlhDsaShake256128fRobust => write!(f, "SLH-DSA-SHAKE256-128f-Robust"),
412            Algorithm::SlhDsaShake256192fRobust => write!(f, "SLH-DSA-SHAKE256-192f-Robust"),
413            Algorithm::SlhDsaShake256256fRobust => write!(f, "SLH-DSA-SHAKE256-256f-Robust"),
414
415            // Hash algorithms
416            Algorithm::Shake128 => write!(f, "SHAKE128"),
417            Algorithm::Shake256 => write!(f, "SHAKE256"),
418            Algorithm::CShake128 => write!(f, "cSHAKE128"),
419            Algorithm::CShake256 => write!(f, "cSHAKE256"),
420            Algorithm::Sha3_224 => write!(f, "SHA3-224"),
421            Algorithm::Sha3_256 => write!(f, "SHA3-256"),
422            Algorithm::Sha3_384 => write!(f, "SHA3-384"),
423            Algorithm::Sha3_512 => write!(f, "SHA3-512"),
424            Algorithm::Keccak224 => write!(f, "Keccak-224"),
425            Algorithm::Keccak256 => write!(f, "Keccak-256"),
426            Algorithm::Keccak384 => write!(f, "Keccak-384"),
427            Algorithm::Keccak512 => write!(f, "Keccak-512"),
428            Algorithm::Sha224 => write!(f, "SHA-224"),
429            Algorithm::Sha256 => write!(f, "SHA-256"),
430            Algorithm::Sha384 => write!(f, "SHA-384"),
431            Algorithm::Sha512 => write!(f, "SHA-512"),
432            Algorithm::Sha512_224 => write!(f, "SHA-512/224"),
433            Algorithm::Sha512_256 => write!(f, "SHA-512/256"),
434
435            // AEAD algorithms
436            Algorithm::Saturnin => write!(f, "Saturnin"),
437            Algorithm::Shake256Aead => write!(f, "SHAKE256-AEAD"),
438            Algorithm::DuplexSpongeAead => write!(f, "Duplex-Sponge-AEAD"),
439            Algorithm::TweakAead => write!(f, "Tweak-AEAD"),
440            Algorithm::RomulusN => write!(f, "Romulus-N"),
441            Algorithm::RomulusM => write!(f, "Romulus-M"),
442
443            // Additional algorithms
444            Algorithm::Kt128 => write!(f, "KT128"),
445            Algorithm::Kt256 => write!(f, "KT256"),
446            Algorithm::TurboShake128 => write!(f, "TurboShake128"),
447            Algorithm::TurboShake256 => write!(f, "TurboShake256"),
448            Algorithm::Kmac128 => write!(f, "KMAC128"),
449            Algorithm::Kmac256 => write!(f, "KMAC256"),
450            Algorithm::TupleHash128 => write!(f, "TupleHash128"),
451            Algorithm::TupleHash256 => write!(f, "TupleHash256"),
452            Algorithm::ParallelHash128 => write!(f, "ParallelHash128"),
453            Algorithm::ParallelHash256 => write!(f, "ParallelHash256"),
454
455            Algorithm::LatticeRingSignature => write!(f, "Lattice-Ring-Signature"),
456            Algorithm::LatticeBlindIssuance => write!(f, "Lattice-Blind-Issuance"),
457            Algorithm::LatticeAnonymousToken => write!(f, "Lattice-Anonymous-Token"),
458            Algorithm::LatticeNullifierRegistry => write!(f, "Lattice-Nullifier-Registry"),
459            Algorithm::LatticeWitnessNullifier => write!(f, "Lattice-Witness-Nullifier"),
460            Algorithm::LatticeDualRingLb => write!(f, "Lattice-DualRing-LB"),
461            Algorithm::MixOnionRouting => write!(f, "Mix-Onion-Routing"),
462            Algorithm::SessionResumptionBinding => write!(f, "Session-Resumption-Binding"),
463        }
464    }
465}
466
467impl core::fmt::Display for AlgorithmCategory {
468    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
469        match self {
470            AlgorithmCategory::Kem => write!(f, "KEM"),
471            AlgorithmCategory::Signature => write!(f, "Signature"),
472            AlgorithmCategory::Hash => write!(f, "Hash"),
473            AlgorithmCategory::Aead => write!(f, "AEAD"),
474            AlgorithmCategory::PrivacyProtocol => write!(f, "Privacy protocol"),
475        }
476    }
477}
478
479#[cfg(test)]
480mod tests {
481    use super::*;
482
483    #[test]
484    fn test_algorithm_categories() {
485        assert_eq!(Algorithm::MlKem512.category(), AlgorithmCategory::Kem);
486        assert_eq!(Algorithm::Shake256Aead.category(), AlgorithmCategory::Aead);
487        assert_eq!(
488            Algorithm::LatticeAnonymousToken.category(),
489            AlgorithmCategory::PrivacyProtocol
490        );
491    }
492}