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: PublicKeyThe public key.
secret: SecretKeyThe secret key.
Implementations§
Source§impl Keypair
impl Keypair
Sourcepub fn generate<R: CryptoRng + RngCore>(
rng: &mut R,
) -> Result<Keypair, KyberLibError>
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 theRngCoreandCryptoRngtraits.
§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]
};Sourcepub fn expose_secret(&self) -> &SecretKey
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);Sourcepub fn import<R: CryptoRng + RngCore>(
public: &mut [u8; 1184],
secret: &mut [u8; 2400],
rng: &mut R,
) -> Result<Keypair, KyberLibError>
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 theRngCoreandCryptoRngtraits.
§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.