Skip to main content

Keypair

Struct Keypair 

Source
pub struct Keypair {
    pub public: PublicKey,
    pub secret: SecretKey,
}
Expand description

A public/secret keypair for use with Kyber.

Byte lengths of the keys are determined by the security level chosen.

§Deprecation

Soft-deprecated since v0.0.7 in favour of the typed MlKem768EncapKey / MlKem768DecapKey split exposed via the KemCore trait. The blob form exposes the secret half through a public field; the typed split prevents that while keeping the legacy byte arrays accessible via .as_bytes() for serialization.

New code should use MlKem768::generate and the typed API. This struct will be removed in a future release.

§Secret handling (legacy)

Keypair does not derive Copy (since v0.0.7) and derives [Zeroize] / [ZeroizeOnDrop] unconditionally — the zeroize Cargo feature is a no-op opt-out retained for one release.

Debug redacts the secret key and prints only the two lengths, matching MlKem768DecapKey: Keypair { public: [1184 bytes; not secret], secret: [REDACTED 2400 bytes] } for ML-KEM-768.

Fields§

§public: PublicKey

The public key.

§secret: SecretKey

The secret key.

Implementations§

Source§

impl Keypair

Source

pub fn generate<R: CryptoRng + RngCore>( rng: &mut R, ) -> Result<Keypair, KyberLibError>

Securely generates a new keypair.

This function generates a new Kyber key pair and returns it as a Keypair struct.

§Arguments
  • rng - The random number generator implementing the RngCore and CryptoRng traits.
§Example
let mut rng = rand::thread_rng();
let keys = Keypair::generate(&mut rng)?;
  public: [0u8; KYBER_PUBLIC_KEY_BYTES], secret: [0u8; KYBER_SECRET_KEY_BYTES]
};
Source

pub fn expose_secret(&self) -> &SecretKey

Explicitly exposes the secret key

 use kyberlib::*;

 let mut rng = rand::thread_rng();
 let keys = Keypair::generate(&mut rng);
 let binding = keys.expect("Exposed secret key");
 let secret = binding.expose_secret();
 assert!(secret.len() == KYBER_SECRET_KEY_BYTES);
 assert!(secret.len() != 0);
Source

pub fn import<R: CryptoRng + RngCore>( public: &mut [u8; 1184], secret: &mut [u8; 2400], rng: &mut R, ) -> Result<Keypair, KyberLibError>

Imports a keypair from existing public and secret key arrays.

This function imports a keypair from existing public and secret key arrays and returns it as a Keypair struct.

§Arguments
  • public - A mutable reference to a [u8; KYBER_PUBLIC_KEY_BYTES] array representing the public key.
  • secret - A mutable reference to a [u8; KYBER_SECRET_KEY_BYTES] array representing the secret key.
  • rng - The random number generator implementing the RngCore and CryptoRng traits.
§Secret hygiene

On success, the caller-supplied secret buffer is zeroized in place (since v0.0.7). Subsequent calls with the same secret buffer will therefore return KyberLibError::InvalidKey; this is intentional — the secret has only one valid home, the returned Keypair. If you genuinely need a copy, use Keypair::clone on the returned value.

§Example
let mut rng = rand::thread_rng();
let keys = keypair(&mut rng)?;
let mut public_key = keys.public;
let mut secret_key = keys.secret;
let imported = Keypair::import(&mut public_key, &mut secret_key, &mut rng)?;
// `secret_key` has been zeroized in place; `imported.secret` is the
// only remaining copy and will be zeroized when `imported` is dropped.

Trait Implementations§

Source§

impl Clone for Keypair

Source§

fn clone(&self) -> Keypair

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Keypair

Source§

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

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

impl Drop for Keypair

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for Keypair

Source§

impl PartialEq for Keypair

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Keypair

Source§

impl Zeroize for Keypair

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

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> 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> 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.