Skip to content

Commit eb189d7

Browse files
authored
ml-kem: define parameter set modules (#162)
Defines the following modules: - `ml_kem_512` - `ml_kem_768` - `ml_kem_1024` Each contains the `ParameterSet` definition for the given security level (re-exported at the toplevel) along with type aliases for `DecapsulationKey` and `EncapsulationKey`. The documentation has been updated to use such a type alias for `DecapsulationKey` as originally proposed in #161
1 parent 321554a commit eb189d7

File tree

2 files changed

+85
-36
lines changed

2 files changed

+85
-36
lines changed

ml-kem/src/kem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{Encoded, EncodedSizeUser, Seed};
1414
use zeroize::{Zeroize, ZeroizeOnDrop};
1515

1616
// Re-export traits from the `kem` crate
17-
pub use ::kem::{Decapsulate, Encapsulate};
17+
pub use ::kem::{Decapsulate, Encapsulate, KeyInit, KeySizeUser};
1818

1919
/// A shared key resulting from an ML-KEM transaction
2020
pub(crate) type SharedKey = B32;

ml-kem/src/lib.rs

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
//! computers.
2222
//!
2323
//! ```
24-
//! # use ml_kem::*;
25-
//! # use ::kem::{Decapsulate, Encapsulate};
26-
//! let mut rng = rand::rng();
24+
//! use ml_kem::{
25+
//! ml_kem_768::DecapsulationKey,
26+
//! kem::{Decapsulate, Encapsulate, KeyInit}
27+
//! };
2728
//!
28-
//! // Generate a (decapsulation key, encapsulation key) pair
29-
//! let (dk, ek) = MlKem768::generate(&mut rng);
29+
//! // Generate a decapsulation/encapsulation keypair
30+
//! let mut rng = rand::rng();
31+
//! let seed = DecapsulationKey::generate_key_with_rng(&mut rng);
32+
//! let dk = DecapsulationKey::new(&seed);
33+
//! let ek = dk.encapsulator();
3034
//!
3135
//! // Encapsulate a shared key to the holder of the decapsulation key, receive the shared
3236
//! // secret `k_send` and the encapsulated form `ct`.
@@ -81,50 +85,95 @@ pub use hybrid_array as array;
8185
#[cfg(feature = "deterministic")]
8286
pub use util::B32;
8387

88+
pub use ml_kem_512::MlKem512Params;
89+
pub use ml_kem_768::MlKem768Params;
90+
pub use ml_kem_1024::MlKem1024Params;
8491
pub use param::{ArraySize, ParameterSet};
8592
pub use traits::*;
8693

8794
/// ML-KEM seeds are decapsulation (private) keys, which are consistently 64-bytes across all
8895
/// security levels, and are the preferred serialization for representing such keys.
8996
pub type Seed = Array<u8, U64>;
9097

91-
/// `MlKem512` is the parameter set for security category 1, corresponding to key search on a block
98+
/// ML-KEM-512 is the parameter set for security category 1, corresponding to key search on a block
9299
/// cipher with a 128-bit key.
93-
#[derive(Default, Clone, Debug, PartialEq)]
94-
pub struct MlKem512Params;
95-
96-
impl ParameterSet for MlKem512Params {
97-
type K = U2;
98-
type Eta1 = U3;
99-
type Eta2 = U2;
100-
type Du = U10;
101-
type Dv = U4;
100+
pub mod ml_kem_512 {
101+
use super::{Debug, ParameterSet, U2, U3, U4, U10, kem};
102+
103+
/// `MlKem512` is the parameter set for security category 1, corresponding to key search on a
104+
/// block cipher with a 128-bit key.
105+
#[derive(Default, Clone, Debug, PartialEq)]
106+
pub struct MlKem512Params;
107+
108+
impl ParameterSet for MlKem512Params {
109+
type K = U2;
110+
type Eta1 = U3;
111+
type Eta2 = U2;
112+
type Du = U10;
113+
type Dv = U4;
114+
}
115+
116+
/// An ML-KEM-512 `DecapsulationKey` which provides the ability to generate a new key pair, and
117+
/// decapsulate an encapsulated shared key.
118+
pub type DecapsulationKey = kem::DecapsulationKey<MlKem512Params>;
119+
120+
/// An ML-KEM-512 `EncapsulationKey` provides the ability to encapsulate a shared key so that it
121+
/// can only be decapsulated by the holder of the corresponding decapsulation key.
122+
pub type EncapsulationKey = kem::EncapsulationKey<MlKem512Params>;
102123
}
103124

104-
/// `MlKem768` is the parameter set for security category 3, corresponding to key search on a block
125+
/// ML-KEM-768 is the parameter set for security category 3, corresponding to key search on a block
105126
/// cipher with a 192-bit key.
106-
#[derive(Default, Clone, Debug, PartialEq)]
107-
pub struct MlKem768Params;
108-
109-
impl ParameterSet for MlKem768Params {
110-
type K = U3;
111-
type Eta1 = U2;
112-
type Eta2 = U2;
113-
type Du = U10;
114-
type Dv = U4;
127+
pub mod ml_kem_768 {
128+
use super::{Debug, ParameterSet, U2, U3, U4, U10, kem};
129+
130+
/// `MlKem768` is the parameter set for security category 3, corresponding to key search on a
131+
/// block cipher with a 192-bit key.
132+
#[derive(Default, Clone, Debug, PartialEq)]
133+
pub struct MlKem768Params;
134+
135+
impl ParameterSet for MlKem768Params {
136+
type K = U3;
137+
type Eta1 = U2;
138+
type Eta2 = U2;
139+
type Du = U10;
140+
type Dv = U4;
141+
}
142+
143+
/// An ML-KEM-768 `DecapsulationKey` which provides the ability to generate a new key pair, and
144+
/// decapsulate an encapsulated shared key.
145+
pub type DecapsulationKey = kem::DecapsulationKey<MlKem768Params>;
146+
147+
/// An ML-KEM-768 `EncapsulationKey` provides the ability to encapsulate a shared key so that it
148+
/// can only be decapsulated by the holder of the corresponding decapsulation key.
149+
pub type EncapsulationKey = kem::EncapsulationKey<MlKem768Params>;
115150
}
116151

117-
/// `MlKem1024` is the parameter set for security category 5, corresponding to key search on a block
152+
/// ML-KEM-1024 is the parameter set for security category 5, corresponding to key search on a block
118153
/// cipher with a 256-bit key.
119-
#[derive(Default, Clone, Debug, PartialEq)]
120-
pub struct MlKem1024Params;
121-
122-
impl ParameterSet for MlKem1024Params {
123-
type K = U4;
124-
type Eta1 = U2;
125-
type Eta2 = U2;
126-
type Du = U11;
127-
type Dv = U5;
154+
pub mod ml_kem_1024 {
155+
use super::{Debug, ParameterSet, U2, U4, U5, U11, kem};
156+
157+
/// `MlKem1024` is the parameter set for security category 5, corresponding to key search on a
158+
/// block cipher with a 256-bit key.
159+
#[derive(Default, Clone, Debug, PartialEq)]
160+
pub struct MlKem1024Params;
161+
162+
impl ParameterSet for MlKem1024Params {
163+
type K = U4;
164+
type Eta1 = U2;
165+
type Eta2 = U2;
166+
type Du = U11;
167+
type Dv = U5;
168+
}
169+
170+
/// An ML-KEM-1024 `DecapsulationKey` which provides the ability to generate a new key pair, and
171+
/// decapsulate an encapsulated shared key.
172+
pub type DecapsulationKey = kem::DecapsulationKey<MlKem1024Params>;
173+
174+
/// An ML-KEM-1024 `EncapsulationKey` provides the ability to encapsulate a shared key so that
175+
/// it can only be decapsulated by the holder of the corresponding decapsulation key.
176+
pub type EncapsulationKey = kem::EncapsulationKey<MlKem1024Params>;
128177
}
129178

130179
/// A shared key produced by the KEM `K`

0 commit comments

Comments
 (0)