Skip to main content

SlhDsa

Struct SlhDsa 

Source
pub struct SlhDsa<P: Params> { /* private fields */ }
Expand description

Generic SLH-DSA interface parameterized by parameter set (FIPS 205).

This struct provides the high-level API for SLH-DSA key generation, signing, and verification. The type parameter P selects one of the twelve FIPS 205 parameter sets (Sha2_128sShake256f) defined in the params module.

All methods are stateless; SlhDsa carries no runtime data and exists only to bind the parameter set at the type level.

Implementations§

Source§

impl<P: Params> SlhDsa<P>

Source

pub fn keygen( rng: &mut dyn CryptoRng, ) -> Result<(SigningKey<P>, VerifyingKey<P>), SlhDsaError>

Generate a new SLH-DSA key pair using the provided RNG.

Implements Algorithm 21 of FIPS 205.

§Arguments
  • rng - A cryptographic random number generator implementing CryptoRng.
§Returns

A tuple (secret_key, public_key) of typed wrappers. The secret key is 4*n bytes and the public key is 2*n bytes, where n = P::N.

§Errors
Source

pub fn sign( message: &[u8], ctx: &[u8], secret_key: &SigningKey<P>, rng: &mut dyn CryptoRng, ) -> Result<Signature<P>, SlhDsaError>

Sign a message with an optional context string (hedged mode).

Implements Algorithm 22 of FIPS 205 (external SLH-DSA.Sign): wraps the message as M' = 0x00 || len(ctx) || ctx || message for domain separation, then hedged-signs it. Use an empty ctx (b"") when no context is needed — the wrapping still applies, keeping signatures interoperable with conformant verifiers.

§Arguments
  • message - The message to sign (arbitrary length).
  • ctx - Optional context string, at most 255 bytes (b"" for none).
  • secret_key - The signing key for this parameter set.
  • rng - A cryptographic random number generator implementing CryptoRng.
§Returns

A Signature<P> of length SlhDsa::signature_size.

§Errors
Source

pub fn sign_prehash( message: &[u8], ctx: &[u8], ph: PreHash, secret_key: &SigningKey<P>, rng: &mut dyn CryptoRng, ) -> Result<Signature<P>, SlhDsaError>

Sign with pre-hashing — HashSLH-DSA (Algorithm 23 of FIPS 205).

The message is replaced by its digest under ph; the library computes ph(message) and embeds the matching OID, forming M' = 0x01 || len(ctx) || ctx || OID(ph) || ph(message).

§Arguments
  • message - The message to pre-hash and sign (arbitrary length).
  • ctx - Optional context string, at most 255 bytes (b"" for none).
  • ph - The pre-hash function PreHash applied to message.
  • secret_key - The signing key for this parameter set.
  • rng - A cryptographic random number generator implementing CryptoRng.
§Returns

A Signature<P> of length SlhDsa::signature_size.

§Errors
Source

pub fn verify( message: &[u8], ctx: &[u8], signature: &Signature<P>, public_key: &VerifyingKey<P>, ) -> Result<bool, SlhDsaError>

Verify a signature on a message with an optional context string.

Implements Algorithm 24 of FIPS 205 (external SLH-DSA.Verify): reconstructs M' = 0x00 || len(ctx) || ctx || message and checks it. ctx must match the one used at signing time.

§Arguments
  • message - The signed message.
  • ctx - The context string used during signing, at most 255 bytes.
  • signature - The signature to check.
  • public_key - The verifying key for this parameter set.
§Returns

Ok(true) if the signature is valid, Ok(false) if it does not verify.

§Errors
Source

pub fn verify_prehash( message: &[u8], ctx: &[u8], ph: PreHash, signature: &Signature<P>, public_key: &VerifyingKey<P>, ) -> Result<bool, SlhDsaError>

Verify a pre-hash HashSLH-DSA signature (Algorithm 25 of FIPS 205).

ph must match the PreHash used at signing time.

§Arguments
  • message - The signed message.
  • ctx - The context string used during signing, at most 255 bytes.
  • ph - The pre-hash function PreHash, matching the one used at signing.
  • signature - The signature to check.
  • public_key - The verifying key for this parameter set.
§Returns

Ok(true) if the signature is valid, Ok(false) if it does not verify.

§Errors
Source

pub fn signature_size() -> usize

Returns the expected signature size in bytes for this parameter set.

The signature consists of a randomizer R (n bytes), a FORS signature, and a hypertree signature: n + k*(1+a)*n + (h + d*len)*n.

Source

pub fn public_key_size() -> usize

Returns the expected public key size in bytes (2*n).

Source

pub fn secret_key_size() -> usize

Returns the expected secret key size in bytes (4*n).

Auto Trait Implementations§

§

impl<P> Freeze for SlhDsa<P>

§

impl<P> RefUnwindSafe for SlhDsa<P>
where P: RefUnwindSafe,

§

impl<P> Send for SlhDsa<P>
where P: Send,

§

impl<P> Sync for SlhDsa<P>
where P: Sync,

§

impl<P> Unpin for SlhDsa<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for SlhDsa<P>

§

impl<P> UnwindSafe for SlhDsa<P>
where P: UnwindSafe,

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