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_128s … Shake256f) 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>
impl<P: Params> SlhDsa<P>
Sourcepub fn keygen(
rng: &mut dyn CryptoRng,
) -> Result<(SigningKey<P>, VerifyingKey<P>), SlhDsaError>
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 implementingCryptoRng.
§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
SlhDsaError::RngFailureif the RNG cannot provide bytes.
Sourcepub fn sign(
message: &[u8],
ctx: &[u8],
secret_key: &SigningKey<P>,
rng: &mut dyn CryptoRng,
) -> Result<Signature<P>, SlhDsaError>
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 implementingCryptoRng.
§Returns
A Signature<P> of length SlhDsa::signature_size.
§Errors
SlhDsaError::ContextTooLongifctxexceeds 255 bytes.SlhDsaError::RngFailureif the RNG cannot provide bytes.SlhDsaError::FaultDetectedif thesca-fors-redundancyfeature is enabled and the two independent signing runs disagree.
Sourcepub fn sign_prehash(
message: &[u8],
ctx: &[u8],
ph: PreHash,
secret_key: &SigningKey<P>,
rng: &mut dyn CryptoRng,
) -> Result<Signature<P>, SlhDsaError>
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 functionPreHashapplied tomessage.secret_key- The signing key for this parameter set.rng- A cryptographic random number generator implementingCryptoRng.
§Returns
A Signature<P> of length SlhDsa::signature_size.
§Errors
SlhDsaError::ContextTooLongifctxexceeds 255 bytes.SlhDsaError::RngFailureif the RNG cannot provide bytes.SlhDsaError::FaultDetectedif thesca-fors-redundancyfeature is enabled and the two independent signing runs disagree.
Sourcepub fn verify(
message: &[u8],
ctx: &[u8],
signature: &Signature<P>,
public_key: &VerifyingKey<P>,
) -> Result<bool, SlhDsaError>
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
SlhDsaError::ContextTooLongifctxexceeds 255 bytes.SlhDsaError::InvalidKeyifpublic_keyhas the wrong length.SlhDsaError::InvalidSignatureifsignaturehas the wrong length.
Sourcepub fn verify_prehash(
message: &[u8],
ctx: &[u8],
ph: PreHash,
signature: &Signature<P>,
public_key: &VerifyingKey<P>,
) -> Result<bool, SlhDsaError>
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 functionPreHash, 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
SlhDsaError::ContextTooLongifctxexceeds 255 bytes.SlhDsaError::InvalidKeyifpublic_keyhas the wrong length.SlhDsaError::InvalidSignatureifsignaturehas the wrong length.
Sourcepub fn signature_size() -> usize
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.
Sourcepub fn public_key_size() -> usize
pub fn public_key_size() -> usize
Returns the expected public key size in bytes (2*n).
Sourcepub fn secret_key_size() -> usize
pub fn secret_key_size() -> usize
Returns the expected secret key size in bytes (4*n).