Skip to main content

DilithiumKeyPair

Struct DilithiumKeyPair 

Source
pub struct DilithiumKeyPair { /* private fields */ }
Expand description

An ML-DSA key pair (private key + public key).

The private key bytes are automatically zeroized on drop (FIPS 204 §7).

Type aliases: MlDsaKeyPair (FIPS 204 naming) = DilithiumKeyPair (legacy).

Implementations§

Source§

impl DilithiumKeyPair

Source

pub fn generate(mode: DilithiumMode) -> Result<Self, DilithiumError>

Generate a new key pair using OS entropy (FIPS 204 §6.1 KeyGen).

Requires the std or getrandom feature (enabled by default).

Source

pub fn generate_deterministic(mode: DilithiumMode, seed: &[u8; 32]) -> Self

Generate a key pair deterministically from a seed.

Source

pub fn sign( &self, msg: &[u8], ctx: &[u8], ) -> Result<DilithiumSignature, DilithiumError>

Sign a message using pure ML-DSA (FIPS 204 §6.1 ML-DSA.Sign).

Context string ctx is optional (max 255 bytes). Requires the std or getrandom feature for randomized signing.

Source

pub fn sign_prehash( &self, msg: &[u8], ctx: &[u8], ) -> Result<DilithiumSignature, DilithiumError>

Sign a message using HashML-DSA (FIPS 204 §6.2 HashML-DSA.Sign).

The message is internally hashed with SHA-512 before signing. Context string ctx is optional (max 255 bytes). Requires the std or getrandom feature for randomized signing.

Source

pub fn sign_deterministic( &self, msg: &[u8], ctx: &[u8], rnd: &[u8; 32], ) -> Result<DilithiumSignature, DilithiumError>

Sign deterministically (for testing / reproducibility).

Source

pub fn verify( pk: &[u8], sig: &DilithiumSignature, msg: &[u8], ctx: &[u8], mode: DilithiumMode, ) -> bool

Verify a pure ML-DSA signature (FIPS 204 §6.1 ML-DSA.Verify).

Source

pub fn verify_prehash( pk: &[u8], sig: &DilithiumSignature, msg: &[u8], ctx: &[u8], mode: DilithiumMode, ) -> bool

Verify a HashML-DSA signature (FIPS 204 §6.2 HashML-DSA.Verify).

Source

pub fn public_key(&self) -> &[u8]

Get the encoded public key bytes.

Source

pub fn private_key(&self) -> &[u8]

Get the encoded private key bytes.

Source

pub fn mode(&self) -> DilithiumMode

Get the security mode.

Source

pub fn from_keys( privkey: &[u8], pubkey: &[u8], mode: DilithiumMode, ) -> Result<Self, DilithiumError>

Reconstruct from private + public key bytes with validation (FIPS 204 §7.1).

Validates that:

  1. Key sizes match the expected values for the given mode.
  2. The public key embedded in the secret key is consistent.
  3. The secret key’s tr = H(pk) field is consistent.
Source

pub fn to_bytes(&self) -> Vec<u8>

Serialize the full key pair to bytes: [mode_tag(1) | pk | sk].

The mode tag encodes the security level so deserialization can automatically select the correct parameters.

Source

pub fn from_bytes(data: &[u8]) -> Result<Self, DilithiumError>

Deserialize a key pair from the format produced by to_bytes.

Source

pub fn public_key_bytes(&self) -> Vec<u8>

Export only the public key bytes with a mode tag: [mode_tag(1) | pk].

Source

pub fn from_public_key( data: &[u8], ) -> Result<(DilithiumMode, Vec<u8>), DilithiumError>

Create a verify-only handle from tagged public key bytes.

Trait Implementations§

Source§

impl Clone for DilithiumKeyPair

Source§

fn clone(&self) -> DilithiumKeyPair

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 DilithiumKeyPair

Source§

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

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

impl<'de> Deserialize<'de> for DilithiumKeyPair

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for DilithiumKeyPair

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,