ML-DSA
Header: #include <cryptopp/mldsa.h> | Namespace: CryptoPP
Since: cryptopp-modern 2026.3.0
Thread Safety: Signer/Verifier instances are not thread-safe; use one per thread. If sharing key bytes, load them into separate instances per thread.
ML-DSA (Module-Lattice-Based Digital Signature Algorithm) is the NIST post-quantum signature standard, FIPS 204, derived from CRYSTALS-Dilithium. It is designed to resist attack by quantum computers as well as conventional ones.
Key Features
- Post-quantum - Security rests on module lattice problems (Module-LWE and Module-SIS)
- SUF-CMA - Designed to be strongly unforgeable under chosen-message attack
- Randomised signing - Signing takes randomness from the RNG, so two signatures of one message differ
- 3 parameter sets - NIST security categories 2, 3 and 5
Parameter Sets
| Parameter Set | Security Category | Public Key | Secret Key | Signature |
|---|---|---|---|---|
| ML-DSA-44 | 2 | 1312 bytes | 2560 bytes | 2420 bytes |
| ML-DSA-65 | 3 | 1952 bytes | 4032 bytes | 3309 bytes |
| ML-DSA-87 | 5 | 2592 bytes | 4896 bytes | 4627 bytes |
A NIST security category compares the cost of breaking the scheme with a generic attack on a symmetric primitive: category 2 with a collision search on SHA-256, category 3 with a key search on AES-192 and category 5 with a key search on AES-256. ML-DSA-65 is a common general-purpose choice, with signatures of about 3.3 KB. ML-DSA-44 has smaller keys and signatures at category 2; ML-DSA-87 is the category 5 set.
Quick Example
#include <cryptopp/mldsa.h>
#include <cryptopp/osrng.h>
#include <cryptopp/secblock.h>
#include <string>
#include <iostream>
int main() {
using namespace CryptoPP;
AutoSeededRandomPool rng;
// === Generate key pair and create signer ===
MLDSASigner<MLDSA_65> signer(rng);
// === Create verifier from signer's public key ===
MLDSAVerifier<MLDSA_65> verifier(signer);
// === Sign a message ===
std::string message = "Hello, post-quantum world!";
SecByteBlock signature(signer.SignatureLength());
size_t sigLen = signer.SignMessage(
rng,
(const byte*)message.data(),
message.size(),
signature.begin()
);
// === Verify the signature ===
bool valid = verifier.VerifyMessage(
(const byte*)message.data(),
message.size(),
signature.begin(),
sigLen
);
std::cout << "Signature valid: " << (valid ? "YES" : "NO") << std::endl;
return valid ? 0 : 1;
}Usage Guidelines
Do:
- Choose the parameter set by the security category you need; ML-DSA-65 suits most applications
- Keep private keys in
SecByteBlock - Use a context string to separate signatures made for different purposes
- Verify a signature before acting on the signed data
Avoid:
- Writing private keys to logs or error messages
- Using one key pair for unrelated purposes
When to Use ML-DSA
Use ML-DSA when:
- You need post-quantum digital signatures
- Signatures must stay unforgeable for a long time, for example on firmware, certificates or archived documents
- A protocol or policy specifies FIPS 204
SLH-DSA is a better fit if you want security that rests only on hash functions, with no lattice assumptions, and can accept larger signatures (7,856 bytes at the smallest parameter set, against 3,309 for ML-DSA-65).
Stay with Ed25519 when quantum attackers are not in your threat model. Ed25519 signatures are 64 bytes against ML-DSA-65’s 3,309.
Class: MLDSASigner
Sign messages using a private key.
Template Parameter
template <class PARAMS>
struct MLDSASigner;PARAMS is one of: MLDSA_44, MLDSA_65, MLDSA_87
Constants
CRYPTOPP_CONSTANT(SECRET_KEYLENGTH = PARAMS::SECRET_KEY_SIZE);
CRYPTOPP_CONSTANT(PUBLIC_KEYLENGTH = PARAMS::PUBLIC_KEY_SIZE);
CRYPTOPP_CONSTANT(SIGNATURE_LENGTH = PARAMS::SIGNATURE_SIZE);Constructors
Default Constructor
MLDSASigner();Create an empty signer. Use AccessKey().GenerateRandom(rng, params) to generate a key.
Constructor with RNG (Generate New Key Pair)
MLDSASigner(RandomNumberGenerator &rng);Generate a new key pair.
Example:
AutoSeededRandomPool rng;
MLDSASigner<MLDSA_65> signer(rng);
// Ready to sign messages
Constructor with Private Key
MLDSASigner(const byte *privateKey, size_t privateKeyLen);Load an existing private key.
Example:
// Load stored private key
SecByteBlock storedKey = /* loaded from storage */;
MLDSASigner<MLDSA_65> signer(storedKey.begin(), storedKey.size());Methods
SignMessage (Convenience Method)
size_t SignMessage(RandomNumberGenerator &rng,
const byte *message, size_t messageLen,
byte *signature) const;Sign a message in one call (inherited from PK_Signer).
Parameters:
rng- Random number generatormessage- Message to signmessageLen- Length of messagesignature- Output buffer for signature (SignatureLength() bytes)
Returns: Actual signature length
Example:
std::string message = "Document to sign";
SecByteBlock signature(signer.SignatureLength());
size_t sigLen = signer.SignMessage(rng,
(const byte*)message.data(), message.size(),
signature.begin());SignatureLength
size_t SignatureLength() const;Get the signature size in bytes.
GetKey / AccessKey
const MLDSAPrivateKey<PARAMS>& GetKey() const;
MLDSAPrivateKey<PARAMS>& AccessKey();Access the private key.
AlgorithmName
std::string AlgorithmName() const;Get the algorithm name (e.g., “ML-DSA-65”). Useful for logging and runtime identification.
Class: MLDSAVerifier
Verify signatures using a public key.
Template Parameter
template <class PARAMS>
struct MLDSAVerifier;PARAMS is one of: MLDSA_44, MLDSA_65, MLDSA_87
Constants
CRYPTOPP_CONSTANT(PUBLIC_KEYLENGTH = PARAMS::PUBLIC_KEY_SIZE);
CRYPTOPP_CONSTANT(SIGNATURE_LENGTH = PARAMS::SIGNATURE_SIZE);Constructors
Default Constructor
MLDSAVerifier();Create an empty verifier. Use AccessKey().SetPublicKey() to set the key.
Constructor from Signer
MLDSAVerifier(const MLDSASigner<PARAMS> &signer);Create a verifier from a signer’s public key.
Example:
MLDSASigner<MLDSA_65> signer(rng);
MLDSAVerifier<MLDSA_65> verifier(signer);Constructor with Public Key
MLDSAVerifier(const byte *publicKey, size_t publicKeyLen);Load a public key.
Example:
SecByteBlock publicKey = /* received from signer */;
MLDSAVerifier<MLDSA_65> verifier(publicKey.begin(), publicKey.size());Methods
VerifyMessage (Convenience Method)
bool VerifyMessage(const byte *message, size_t messageLen,
const byte *signature, size_t signatureLen) const;Verify a signature in one call (inherited from PK_Verifier).
Parameters:
message- Original messagemessageLen- Length of messagesignature- Signature to verifysignatureLen- Signature length. Any length other thanSignatureLength()throwsInvalidArgument.
Returns: true if signature is valid, false otherwise
Example:
bool valid = verifier.VerifyMessage(
(const byte*)message.data(), message.size(),
signature.begin(), signature.size());
if (valid) {
std::cout << "Signature verified!" << std::endl;
}SignatureLength
size_t SignatureLength() const;Get the expected signature size in bytes.
GetKey / AccessKey
const MLDSAPublicKey<PARAMS>& GetKey() const;
MLDSAPublicKey<PARAMS>& AccessKey();Access the public key.
AlgorithmName
std::string AlgorithmName() const;Get the algorithm name (e.g., “ML-DSA-65”).
Class: MLDSAPrivateKey
Holds the secret key material for signing.
Methods
GetAlgorithmID
OID GetAlgorithmID() const;Get the algorithm OID for this key type. Used in ASN.1/DER encoding.
GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms);Generate a new random key pair.
Example:
MLDSAPrivateKey<MLDSA_65> privateKey;
privateKey.GenerateRandom(rng, g_nullNameValuePairs);GetPublicKeyBytePtr / GetPublicKeySize
const byte* GetPublicKeyBytePtr() const;
size_t GetPublicKeySize() const;Get the embedded public key.
GetPrivateKeyBytePtr / GetPrivateKeySize
const byte* GetPrivateKeyBytePtr() const;
size_t GetPrivateKeySize() const;Get the private key bytes.
Save / Load
void Save(BufferedTransformation &bt) const;
void Load(BufferedTransformation &bt);Serialize/deserialize in ASN.1 DER format (PKCS#8 OneAsymmetricKey).
Class: MLDSAPublicKey
Holds the public key material for verification.
Methods
SetPublicKey
void SetPublicKey(const byte *key, size_t len);Set the public key bytes.
GetPublicKeyBytePtr / GetPublicKeySize
const byte* GetPublicKeyBytePtr() const;
size_t GetPublicKeySize() const;Get the public key bytes.
Save / Load
void Save(BufferedTransformation &bt) const;
void Load(BufferedTransformation &bt);Serialize/deserialize in ASN.1 DER format (X.509 SubjectPublicKeyInfo).
Key Serialization
Requires: #include <cryptopp/files.h> for file I/O, #include <cstring> for std::memcpy.
Note: This implementation buffers the entire message before signing/verification. For very large messages, consider hashing first and signing the digest.
Saving Keys (Raw Bytes)
// Generate key pair
MLDSASigner<MLDSA_65> signer(rng);
const auto& privKey = signer.GetKey();
// Save private key (includes secret and public parts)
SecByteBlock privateKeyBytes(privKey.GetPrivateKeySize());
std::memcpy(privateKeyBytes.begin(),
privKey.GetPrivateKeyBytePtr(),
privateKeyBytes.size());
// Save public key (for distribution)
SecByteBlock publicKeyBytes(privKey.GetPublicKeySize());
std::memcpy(publicKeyBytes.begin(),
privKey.GetPublicKeyBytePtr(),
publicKeyBytes.size());Loading Keys (Raw Bytes)
// Load private key (for signing)
MLDSASigner<MLDSA_65> signer(
privateKeyBytes.begin(), privateKeyBytes.size());
// Load public key only (for verification)
MLDSAVerifier<MLDSA_65> verifier(
publicKeyBytes.begin(), publicKeyBytes.size());Saving Keys (ASN.1 DER)
// Save private key to file
FileSink privFile("mldsa_private.der");
signer.GetKey().Save(privFile);
// Save public key to file
FileSink pubFile("mldsa_public.der");
MLDSAPublicKey<MLDSA_65> pubKey;
pubKey.SetPublicKey(signer.GetKey().GetPublicKeyBytePtr(),
signer.GetKey().GetPublicKeySize());
pubKey.Save(pubFile);Loading Keys (ASN.1 DER)
// Load private key from file
FileSource privFile("mldsa_private.der", true);
MLDSASigner<MLDSA_65> signer;
signer.AccessKey().Load(privFile);
// Load public key from file
FileSource pubFile("mldsa_public.der", true);
MLDSAVerifier<MLDSA_65> verifier;
verifier.AccessKey().Load(pubFile);Loading Keys from a std::string (in-memory)
The StringSource overload trap that affects ML-KEM Save/Load applies here too. See the ML-KEM Loading Keys section for the safe forms. Substitute MLDSAPublicKey or MLDSAPrivateKey for the ML-KEM class names.
Convenience Type Aliases
// ML-DSA-44 (category 2)
typedef MLDSASigner<MLDSA_44> MLDSA_44_Signer;
typedef MLDSAVerifier<MLDSA_44> MLDSA_44_Verifier;
// ML-DSA-65 (category 3)
typedef MLDSASigner<MLDSA_65> MLDSA_65_Signer;
typedef MLDSAVerifier<MLDSA_65> MLDSA_65_Verifier;
// ML-DSA-87 (category 5)
typedef MLDSASigner<MLDSA_87> MLDSA_87_Signer;
typedef MLDSAVerifier<MLDSA_87> MLDSA_87_Verifier;Example using convenience types:
MLDSA_65_Signer signer(rng);
MLDSA_65_Verifier verifier(signer);Context Strings
FIPS 204 supports an optional context string (up to 255 bytes) that binds a signature to a specific application or protocol, so a signature made for one purpose does not verify for another.
Context strings are set on the message accumulator before signing or verifying:
#include <cryptopp/mldsa.h>
#include <cryptopp/osrng.h>
#include <cryptopp/secblock.h>
#include <cryptopp/smartptr.h>
#include <iostream>
#include <string>
int main() {
using namespace CryptoPP;
AutoSeededRandomPool rng;
MLDSASigner<MLDSA_65> signer(rng);
MLDSAVerifier<MLDSA_65> verifier(signer);
std::string message = "Important document";
const byte ctx[] = "myapp-v1";
// Sign with context
member_ptr<PK_MessageAccumulator> signAcc(signer.NewSignatureAccumulator(rng));
dynamic_cast<MLDSA_MessageAccumulator<MLDSA_65>&>(*signAcc).SetContext(ctx, sizeof(ctx) - 1);
signAcc->Update((const byte*)message.data(), message.size());
SecByteBlock signature(signer.SignatureLength());
size_t sigLen = signer.SignAndRestart(rng, *signAcc, signature.begin());
// Verify with the same context
member_ptr<PK_MessageAccumulator> verAcc(verifier.NewVerificationAccumulator());
dynamic_cast<MLDSA_MessageAccumulator<MLDSA_65>&>(*verAcc).SetContext(ctx, sizeof(ctx) - 1);
verifier.InputSignature(*verAcc, signature.begin(), sigLen);
verAcc->Update((const byte*)message.data(), message.size());
bool valid = verifier.VerifyAndRestart(*verAcc);
// The plain call uses an empty context, so it must reject this signature
bool plain = verifier.VerifyMessage(
(const byte*)message.data(), message.size(), signature.begin(), sigLen);
std::cout << "With context: " << (valid ? "valid" : "invalid") << std::endl;
std::cout << "Without context: " << (plain ? "valid" : "invalid") << std::endl;
return (valid && !plain) ? 0 : 1;
}Notes:
- Context must be identical for signing and verification; a mismatch causes verification to fail
SignMessage()andVerifyMessage()use the empty context, which is the default in FIPS 204- Maximum context length is 255 bytes;
SetContextthrowsInvalidArgumentif exceeded
Hybrid Signatures
For defence in depth, a system can sign a message with both ML-DSA and a classical algorithm such as Ed25519 and require both signatures to verify. A forger then has to break both. How the two signatures and keys are bound together is a protocol design question; where you need interoperability, follow a published composite-signature format instead of inventing one.
cryptest b measures ML-DSA on your own hardware. Signing uses rejection sampling, so its time varies from call to call.
Security Considerations
Private Key Protection - Keep private key bytes in
SecByteBlock, which wipes its memory on destruction. Never log or expose private key bytes.Key Lifetime - Consider key rotation policies. Long-lived signing keys have increased exposure risk.
Signature Verification - Always verify signatures before trusting signed data.
Quantum Timeline - A signature only has to resist forgery while it is relied on, so recorded traffic is not the concern it is for encryption. The case for moving early is long-lived signatures and keys: firmware roots of trust, certificate authorities and archived documents that must still verify safely once quantum computers exist.
Hybrid Mode - For critical applications, combine ML-DSA with Ed25519 or ECDSA and require both signatures. Forgery then requires breaking both algorithms.
Message Size - The implementation buffers the whole message before signing or verifying. For very large inputs, sign a hash of the data and have the verifier do the same.
Object Identifiers (OIDs)
Library-provided OIDs for ASN.1/DER encoding:
| Parameter Set | OID | Accessor |
|---|---|---|
| ML-DSA-44 | 2.16.840.1.101.3.4.3.17 | ASN1::id_ml_dsa_44() |
| ML-DSA-65 | 2.16.840.1.101.3.4.3.18 | ASN1::id_ml_dsa_65() |
| ML-DSA-87 | 2.16.840.1.101.3.4.3.19 | ASN1::id_ml_dsa_87() |
Usage:
// Get OID from key object
OID oid = privateKey.GetAlgorithmID();
// Get OID statically from parameter set
OID staticOid = MLDSA_65::StaticAlgorithmOID();
// Compare OIDs
if (oid == ASN1::id_ml_dsa_65()) {
// ML-DSA-65 key
}Exceptions
InvalidArgument- A key has the wrong length or is invalid; a context string is longer than 255 bytes; or a signature passed to the verifier is not exactlySignatureLength()bytesBERDecodeErr-Load()was given a malformed encoding
A signature of the right length that does not verify is reported by the return value (false), not by an exception. Check the length of a signature from an outside source before passing it to the verifier, or catch InvalidArgument.
Specification Compliance
This implementation follows NIST FIPS 204 (August 2024):
- Interface: The external, pure ML-DSA.Sign and ML-DSA.Verify functions, with an optional context string. The pre-hash variant (HashML-DSA) is not provided.
- Primitives: SHAKE128 to expand the public matrix, SHAKE256 for the other expansion and hashing steps
- Parameters: All three parameter sets (44, 65, 87)
The library’s test suite runs NIST ACVP test vectors for key generation, signing and verification.
FIPS 204 Internal Parameters:
| Parameter Set | k | l | eta | gamma1 | gamma2 | omega |
|---|---|---|---|---|---|---|
| ML-DSA-44 | 4 | 4 | 2 | 2^17 | (q-1)/88 | 80 |
| ML-DSA-65 | 6 | 5 | 4 | 2^19 | (q-1)/32 | 55 |
| ML-DSA-87 | 8 | 7 | 2 | 2^19 | (q-1)/32 | 75 |
See Also
- ML-KEM - Post-quantum key encapsulation
- SLH-DSA - Hash-based digital signatures
- X-Wing - Hybrid KEM (X25519 + ML-KEM-768)
- PQC Overview - Algorithm comparison and guidance