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