Skip to content

Commit 569e43f

Browse files
authored
ml-kem: impl KeyExport for DecapsulationKey (#228)
With the current way we implement expanded key support, this currently needs to be fallible, and since `KeyExport` is infallible we have to panic. Perhaps we can make a separate type which contains only `dk_pke` and `z` like `ExpandedDecapsulationKey` so we can ensure `DecapsulationKey` always has the seed. For now, this at least lets us have an impl of `KeyExport`, even if having a panicking condition is not ideal.
1 parent 2f96158 commit 569e43f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

ml-kem/src/decapsulation_key.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ where
186186
type KeySize = U64;
187187
}
188188

189+
/// Initialize [`DecapsulationKey`] from a 64-byte uniformly random [`Seed`] value.
189190
impl<P> KeyInit for DecapsulationKey<P>
190191
where
191192
P: KemParams,
@@ -196,6 +197,20 @@ where
196197
}
197198
}
198199

200+
/// Serialize the 64-byte [`Seed`] value used to initialize this [`DecapsulationKey`].
201+
///
202+
/// # Panics
203+
/// If this [`DecapsulationKey`] was initialized using legacy expanded key support
204+
/// (see [`ExpandedKeyEncoding`]).
205+
impl<P> KeyExport for DecapsulationKey<P>
206+
where
207+
P: KemParams,
208+
{
209+
fn to_bytes(&self) -> Seed {
210+
self.to_seed().expect("should be initialized from a seed")
211+
}
212+
}
213+
199214
/// DEPRECATED: support for encoding and decoding [`DecapsulationKey`]s in the legacy expanded form,
200215
/// as opposed to the more widely adopted [`Seed`] form.
201216
///

0 commit comments

Comments
 (0)