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 encapsulationMLKEM
: 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
impl EncapsulationCiphertext
Sourcepub fn x25519_public_key(&self) -> Result<&X25519PublicKey>
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);
}
Sourcepub fn mlkem_ciphertext(&self) -> Result<&MLKEMCiphertext>
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);
}
Sourcepub fn is_x25519(&self) -> bool
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());
Sourcepub fn is_mlkem(&self) -> bool
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());
Sourcepub fn encapsulation_scheme(&self) -> EncapsulationScheme
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
impl Clone for EncapsulationCiphertext
Source§fn clone(&self) -> EncapsulationCiphertext
fn clone(&self) -> EncapsulationCiphertext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for EncapsulationCiphertext
impl Debug for EncapsulationCiphertext
Source§impl From<EncapsulationCiphertext> for CBOR
Conversion from EncapsulationCiphertext
to CBOR for serialization.
impl From<EncapsulationCiphertext> for CBOR
Conversion from EncapsulationCiphertext
to CBOR for serialization.
Source§fn from(ciphertext: EncapsulationCiphertext) -> Self
fn from(ciphertext: EncapsulationCiphertext) -> Self
Source§impl PartialEq for EncapsulationCiphertext
impl PartialEq for EncapsulationCiphertext
Source§impl TryFrom<CBOR> for EncapsulationCiphertext
Conversion from CBOR to EncapsulationCiphertext
for deserialization.
impl TryFrom<CBOR> for EncapsulationCiphertext
Conversion from CBOR to EncapsulationCiphertext
for deserialization.