EncapsulationCiphertext

Enum EncapsulationCiphertext 

Source
pub enum EncapsulationCiphertext {
    X25519(X25519PublicKey),
    MLKEM(MLKEMCiphertext),
}
Expand description

A ciphertext produced by a key encapsulation mechanism (KEM).

EncapsulationCiphertext represents the output of a key encapsulation operation where a shared secret has been encapsulated for secure transmission. The ciphertext can only be used to recover the shared secret by the holder of the corresponding private key.

This enum has two variants:

  • X25519: For X25519 key agreement, this is the ephemeral public key generated during encapsulation
  • MLKEM: For ML-KEM post-quantum key encapsulation, this is the ML-KEM ciphertext

Variants§

§

X25519(X25519PublicKey)

X25519 key agreement ciphertext (ephemeral public key)

§

MLKEM(MLKEMCiphertext)

ML-KEM post-quantum ciphertext

Implementations§

Source§

impl EncapsulationCiphertext

Source

pub fn x25519_public_key(&self) -> Result<&X25519PublicKey>

Returns the X25519 public key if this is an X25519 ciphertext.

§Returns

A Result containing a reference to the X25519 public key if this is an X25519 ciphertext, or an error if it’s not.

§Errors

Returns an error if this is not an X25519 ciphertext.

§Example
use bc_components::{
    EncapsulationScheme, X25519PrivateKey, X25519PublicKey,
};

// Generate a keypair
let (private_key, public_key) = EncapsulationScheme::X25519.keypair();

// Encapsulate a shared secret (creates an ephemeral keypair internally)
let (_, ciphertext) = public_key.encapsulate_new_shared_secret();

// Get the X25519 public key from the ciphertext
if let Ok(ephemeral_public_key) = ciphertext.x25519_public_key() {
    // This is an X25519 ephemeral public key
    assert_eq!(ephemeral_public_key.data().len(), 32);
}
Source

pub fn mlkem_ciphertext(&self) -> Result<&MLKEMCiphertext>

Returns the ML-KEM ciphertext if this is an ML-KEM ciphertext.

§Returns

A Result containing a reference to the ML-KEM ciphertext if this is an ML-KEM ciphertext, or an error if it’s not.

§Errors

Returns an error if this is not an ML-KEM ciphertext.

§Example
use bc_components::EncapsulationScheme;

// Generate an ML-KEM keypair
let (private_key, public_key) = EncapsulationScheme::MLKEM768.keypair();

// Encapsulate a shared secret
let (_, ciphertext) = public_key.encapsulate_new_shared_secret();

// Check if it's an ML-KEM ciphertext
assert!(ciphertext.is_mlkem());

// Get the ML-KEM ciphertext
if let Ok(mlkem_ciphertext) = ciphertext.mlkem_ciphertext() {
    // This is an ML-KEM ciphertext
    assert_eq!(mlkem_ciphertext.level(), bc_components::MLKEM::MLKEM768);
}
Source

pub fn is_x25519(&self) -> bool

Returns true if this is an X25519 ciphertext.

§Returns

true if this is an X25519 ciphertext, false otherwise.

§Example
use bc_components::EncapsulationScheme;

// Generate an X25519 keypair
let (_, public_key) = EncapsulationScheme::X25519.keypair();

// Encapsulate a shared secret
let (_, ciphertext) = public_key.encapsulate_new_shared_secret();

// Check if it's an X25519 ciphertext
assert!(ciphertext.is_x25519());
assert!(!ciphertext.is_mlkem());
Source

pub fn is_mlkem(&self) -> bool

Returns true if this is an ML-KEM ciphertext.

§Returns

true if this is an ML-KEM ciphertext, false otherwise.

§Example
use bc_components::EncapsulationScheme;

// Generate an ML-KEM keypair
let (_, public_key) = EncapsulationScheme::MLKEM768.keypair();

// Encapsulate a shared secret
let (_, ciphertext) = public_key.encapsulate_new_shared_secret();

// Check if it's an ML-KEM ciphertext
assert!(ciphertext.is_mlkem());
assert!(!ciphertext.is_x25519());
Source

pub fn encapsulation_scheme(&self) -> EncapsulationScheme

Returns the encapsulation scheme associated with this ciphertext.

§Returns

The encapsulation scheme (X25519, MLKEM512, MLKEM768, or MLKEM1024) that corresponds to this ciphertext.

§Example
use bc_components::EncapsulationScheme;

// Generate a key pair using ML-KEM768
let (_, public_key) = EncapsulationScheme::MLKEM768.keypair();

// Encapsulate a shared secret
let (_, ciphertext) = public_key.encapsulate_new_shared_secret();

// Check the scheme
assert_eq!(
    ciphertext.encapsulation_scheme(),
    EncapsulationScheme::MLKEM768
);

Trait Implementations§

Source§

impl Clone for EncapsulationCiphertext

Source§

fn clone(&self) -> EncapsulationCiphertext

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EncapsulationCiphertext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<EncapsulationCiphertext> for CBOR

Conversion from EncapsulationCiphertext to CBOR for serialization.

Source§

fn from(ciphertext: EncapsulationCiphertext) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for EncapsulationCiphertext

Source§

fn eq(&self, other: &EncapsulationCiphertext) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<CBOR> for EncapsulationCiphertext

Conversion from CBOR to EncapsulationCiphertext for deserialization.

Source§

type Error = Error

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

fn try_from(cbor: CBOR) -> Result<Self, Error>

Performs the conversion.
Source§

impl StructuralPartialEq for EncapsulationCiphertext

Auto Trait Implementations§

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> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

Source§

impl<T> ErasedDestructor for T
where T: 'static,