EncapsulationPublicKey

Enum EncapsulationPublicKey 

Source
pub enum EncapsulationPublicKey {
    X25519(X25519PublicKey),
    MLKEM(MLKEMPublicKey),
}
Expand description

A public key used for key encapsulation mechanisms (KEM).

EncapsulationPublicKey is an enum representing different types of public keys that can be used for key encapsulation, including:

  • X25519: Curve25519-based key exchange
  • ML-KEM: Module Lattice-based Key Encapsulation Mechanism at various security levels

These public keys are used to encrypt (encapsulate) shared secrets that can only be decrypted (decapsulated) by the corresponding private key holder.

Variants§

§

X25519(X25519PublicKey)

An X25519 public key

§

MLKEM(MLKEMPublicKey)

An ML-KEM public key (post-quantum)

Implementations§

Source§

impl EncapsulationPublicKey

Source

pub fn encapsulation_scheme(&self) -> EncapsulationScheme

Returns the encapsulation scheme associated with this public key.

§Returns

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

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

// Generate a keypair
let private_key = X25519PrivateKey::new();
let public_key = private_key.public_key();

// Convert to encapsulation public key
let encapsulation_public_key =
    bc_components::EncapsulationPublicKey::X25519(public_key);

// Check the scheme
assert_eq!(
    encapsulation_public_key.encapsulation_scheme(),
    EncapsulationScheme::X25519
);
Source

pub fn encapsulate_new_shared_secret( &self, ) -> (SymmetricKey, EncapsulationCiphertext)

Encapsulates a new shared secret using this public key.

This method performs the encapsulation operation for key exchange. It generates a new shared secret and encapsulates it using this public key.

The encapsulation process differs based on the key type:

  • For X25519: Generates an ephemeral private/public key pair, derives a shared secret using Diffie-Hellman, and returns the shared secret along with the ephemeral public key
  • For ML-KEM: Uses the KEM encapsulation algorithm to generate and encapsulate a random shared secret
§Returns

A tuple containing:

  • The generated shared secret as a SymmetricKey
  • The encapsulation ciphertext that can be sent to the private key holder
§Example
use bc_components::EncapsulationScheme;

// Generate a key pair using the default scheme (X25519)
let (private_key, public_key) = EncapsulationScheme::default().keypair();

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

// The private key holder can recover the same shared secret
let recovered_secret =
    private_key.decapsulate_shared_secret(&ciphertext).unwrap();
assert_eq!(shared_secret, recovered_secret);

Trait Implementations§

Source§

impl AsRef<EncapsulationPublicKey> for PublicKeys

Source§

fn as_ref(&self) -> &EncapsulationPublicKey

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for EncapsulationPublicKey

Source§

fn clone(&self) -> EncapsulationPublicKey

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 EncapsulationPublicKey

Source§

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

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

impl Encrypter for EncapsulationPublicKey

Implementation of the Encrypter trait for EncapsulationPublicKey.

This allows EncapsulationPublicKey to be used with the generic encryption interface defined by the Encrypter trait.

Source§

fn encapsulation_public_key(&self) -> EncapsulationPublicKey

Returns the encapsulation public key for this encrypter. Read more
Source§

fn encapsulate_new_shared_secret( &self, ) -> (SymmetricKey, EncapsulationCiphertext)

Encapsulates a new shared secret for the recipient. Read more
Source§

impl From<EncapsulationPublicKey> for CBOR

Conversion from EncapsulationPublicKey to CBOR for serialization.

Source§

fn from(public_key: EncapsulationPublicKey) -> Self

Converts to this type from the input type.
Source§

impl Hash for EncapsulationPublicKey

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for EncapsulationPublicKey

Source§

fn eq(&self, other: &EncapsulationPublicKey) -> 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 EncapsulationPublicKey

Conversion from CBOR to EncapsulationPublicKey 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 Eq for EncapsulationPublicKey

Source§

impl StructuralPartialEq for EncapsulationPublicKey

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,