Skip to main content

MlKem

Struct MlKem 

Source
pub struct MlKem { /* private fields */ }
Expand description

ML-KEM-1024 (NIST FIPS 203, security level 5).

ML-KEM (formerly Kyber) is a lattice-based KEM standardised by NIST. It is the recommended default KEM for most applications due to its balance of performance and key/ciphertext size.

§Sizes

ObjectBytes
Public key1,568
Secret key3,168
Ciphertext1,568
Shared secret32

§Pros

  • Small, symmetric public key and ciphertext sizes — well-suited to protocols like TLS and SSH where both are transmitted in a handshake.
  • Fast key generation, encapsulation, and decapsulation.
  • NIST-standardised (FIPS 203); broad library and hardware support.

§Cons

  • Lattice-based: security depends on the hardness of Module-LWE. While considered very strong, this is a newer assumption (~10 years of widespread cryptanalysis) compared to code-based alternatives.

§Reuse

Construct once with MlKem::new and reuse across operations — each instance owns the underlying liboqs algorithm object, so reusing it avoids re-allocating that object on every encapsulation/decapsulation.

Implementations§

Source§

impl MlKem

Source

pub const PUBLIC_KEY_LEN: usize = 1_568

Length in bytes of a serialized public key.

Source

pub const SECRET_KEY_LEN: usize = 3_168

Length in bytes of a serialized secret key.

Source

pub const CIPHERTEXT_LEN: usize = 1_568

Length in bytes of a ciphertext.

Source

pub const SHARED_SECRET_LEN: usize = 32

Length in bytes of the encapsulated shared secret.

Source

pub fn new() -> Self

Construct a reusable ML-KEM-1024 instance.

Infallible: ML-KEM-1024 is always compiled in via the crate’s oqs feature set, so the underlying algorithm object can always be created.

Source

pub fn keypair(&self) -> Result<(PublicKey, SecretKey)>

Source

pub fn encapsulate( &self, public_key: &PublicKey, ) -> Result<(Ciphertext, SharedSecret)>

Source

pub fn decapsulate( &self, secret_key: &SecretKey, ciphertext: &Ciphertext, ) -> Result<SharedSecret>

Source

pub fn ciphertext_from_bytes(&self, bytes: &[u8]) -> Result<Ciphertext>

Reconstruct a ciphertext from its serialized bytes.

Returns Error::InvalidLength if bytes is not exactly CIPHERTEXT_LEN bytes long.

Source

pub fn public_key_from_bytes(&self, bytes: &[u8]) -> Result<PublicKey>

Reconstruct a public key from its serialized bytes.

Returns Error::InvalidLength if bytes is not exactly PUBLIC_KEY_LEN bytes long.

Source

pub fn secret_key_from_bytes(&self, bytes: &[u8]) -> Result<SecretKey>

Reconstruct a secret key from its serialized bytes.

Returns Error::InvalidLength if bytes is not exactly SECRET_KEY_LEN bytes long.

Trait Implementations§

Source§

impl Default for MlKem

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for MlKem

§

impl RefUnwindSafe for MlKem

§

impl Send for MlKem

§

impl Sync for MlKem

§

impl Unpin for MlKem

§

impl UnsafeUnpin for MlKem

§

impl UnwindSafe for MlKem

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.