← Back to section · Tutorials & Labs

Hands-On: Your First ML-DSA Signature with liboqs

In the liboqs environment from the previous lab, run the full ML-DSA flow: key generation, signing, verification, a tamper test that must fail, and a size comparison across all three parameter sets.

Published 21 November 2026 · 7 min read · PQ Frontier

Hands-On: Your First ML-DSA Signature with liboqs
AI-generated artwork created for PQ Frontier
Video explainer · Using PQC libraries: install and go

The previous lab (“Your First ML-KEM Key Exchange with liboqs”) set up the environment; this one runs the full digital-signature flow in the same liboqs-python setup. The goal: generate ML-DSA keys, sign a message, verify it — then change one byte of the message by hand and watch verification fail. That last step is the entire point of a signature scheme.

Setup

Reuse the previous lab’s environment (liboqs and the oqs bindings installed). First confirm ML-DSA is enabled in your build:

python3 -c "import oqs; print([a for a in oqs.get_enabled_sig_mechanisms() if 'ML-DSA' in a])"

Expect ['ML-DSA-44', 'ML-DSA-65', 'ML-DSA-87', ...] (draft variants with IPD suffixes may also appear; this lab uses only the suffix-free FIPS 204 final versions).

The full script

import oqs

message = b"PQ Frontier lab: my first post-quantum signature"

for alg in ["ML-DSA-44", "ML-DSA-65", "ML-DSA-87"]:
    with oqs.Signature(alg) as signer, oqs.Signature(alg) as verifier:
        public_key = signer.generate_keypair()
        secret_key = signer.export_secret_key()
        signature = signer.sign(message)

        ok = verifier.verify(message, signature, public_key)
        tampered = verifier.verify(message + b"!", signature, public_key)

        print(f"{alg}: pk={len(public_key)}B sk={len(secret_key)}B "
              f"sig={len(signature)}B verify={ok} tampered={tampered}")
        assert ok and not tampered

print("ALL OK")

Expected output (sizes match the FIPS 204 table line for line):

ML-DSA-44: pk=1312B sk=2560B sig=2420B verify=True tampered=False
ML-DSA-65: pk=1952B sk=4032B sig=3309B verify=True tampered=False
ML-DSA-87: pk=2592B sk=4896B sig=4627B verify=True tampered=False
ALL OK

Reading the code

  • generate_keypair() creates the key pair inside the object and returns the public key; export_secret_key() exports the private key as bytes — in a real system the key stays in an HSM or protected store; exporting belongs to the lab only;
  • sign() uses FIPS 204’s hedged randomized signing by default: sign the same message twice and the signatures differ, yet both verify. Try it;
  • verify()’s tamper sensitivity comes from hash binding: one changed byte produces a completely different Fiat–Shamir challenge and the signature collapses. The message + b"!" check must return False — if it ever returns True, something is deeply wrong with the library or its use; stop and investigate;
  • Compare the printed sizes with the parameter table in our ML-DSA explainer — half the lab’s value is the flow, the other half is turning “2,420 bytes” from a table entry into output on your own screen.

Experiment 2: Swap the Public Key — Verification Must Fail Too

The main script only tampered with the message. Add the symmetric experiment: leave message and signature untouched, and change only the verification key.

import oqs

message = b"PQ Frontier lab: my first post-quantum signature"

with oqs.Signature("ML-DSA-65") as signer, \
     oqs.Signature("ML-DSA-65") as other, \
     oqs.Signature("ML-DSA-65") as verifier:
    public_key = signer.generate_keypair()
    other_public_key = other.generate_keypair()   # a completely independent key pair
    signature = signer.sign(message)

    print("Correct key:", verifier.verify(message, signature, public_key))
    print("Wrong key:  ", verifier.verify(message, signature, other_public_key))
    # Expect: True / False

A signature mathematically binds three things together: private key, message, and public key. Signing folds a hash of the public key into the challenge value, so verifying under a different key cannot reproduce that challenge — the result must be False. This property is the foundation of all of PKI: a certificate is, at bottom, a verifiable claim about whose key this is; if a signature verified under the wrong key, the certificate system would collapse on the spot.

Complete the set with a third tampering: flip one byte of the signature itself. Change the message, change the signature, change the key — you now own all three failure modes of verification. If any of them ever returns True, your environment or library has a serious problem; stop and investigate before learning anything further on top of it.

Experiment 3: Why Signing Twice Gives Two Different Signatures

Sign the same message twice and compare byte for byte:

import oqs

message = b"sign me twice"

with oqs.Signature("ML-DSA-65") as signer:
    public_key = signer.generate_keypair()
    sig1 = signer.sign(message)
    sig2 = signer.sign(message)
    print("Are the two signatures identical?", sig1 == sig2)   # False
    with oqs.Signature("ML-DSA-65") as verifier:
        print("Do both verify?",
              verifier.verify(message, sig1, public_key),
              verifier.verify(message, sig2, public_key))   # True True

Different, yet both valid — this is FIPS 204’s default hedged signing: signing hashes the private key, the message, and fresh randomness together, so each run takes a different internal sampling path. Why not sign deterministically? Because deterministic signing has a fatal implementation weakness: if a fault injection (a voltage glitch, say) corrupts one step mid-signature, the faulty signature can leak the private key, and the attack is repeatable at will. Randomization makes every signature’s internal state different, so the same trick cannot be replayed. This is also why the labs keep stressing randomness quality — for signature schemes it bears directly on private-key safety.

One more distinction worth filing away: ML-DSA signature lengths are fixed (one exact value per parameter set, as the main script’s output shows), unlike Falcon’s variable-length signatures. Fixed length is a real convenience for protocol engineering: buffers can be statically allocated, and the wire format needs no length field describing the signature itself.

Experiment 4: A Quick Speed Test Across the Parameter Sets

Add timing to the main loop and feel ML-DSA’s performance scale firsthand:

import timeit
import oqs

message = b"benchmark" * 16

for alg in ["ML-DSA-44", "ML-DSA-65", "ML-DSA-87"]:
    with oqs.Signature(alg) as signer, oqs.Signature(alg) as verifier:
        public_key = signer.generate_keypair()
        signature = signer.sign(message)
        t_sign = timeit.timeit(lambda: signer.sign(message), number=200) / 200
        t_verify = timeit.timeit(
            lambda: verifier.verify(message, signature, public_key),
            number=200) / 200
        print(f"{alg}: sign {t_sign*1000:.3f} ms, verify {t_verify*1000:.3f} ms")

On a typical desktop CPU, signing and verification both land in the sub-millisecond to few-millisecond range, with verification usually clearly faster — signing contains a rejection-sampling loop whose iteration count varies, so sign times jitter; take a median over several rounds. This sense of scale matters: it explains why ML-DSA fits high-frequency verification settings like TLS certificates, and it throws SLH-DSA’s one-to-two-orders-of-magnitude slower signing into relief (the signatures section has a dedicated comparison). These numbers describe your machine only — production selection needs measurements on the target hardware.

A Note on Context Strings

Read FIPS 204 closely and you will find an optional input to signing: the context string (up to 255 bytes). Its job is domain separation — the same key pair signs under a different context in each application, so a signature made for application A cannot be replayed and verified in application B, which defuses cross-protocol attacks. TLS, certificates, and code signing each define their own context or equivalent mechanism when adopting ML-DSA. How liboqs-python exposes this parameter varies by version, so check the documentation of the version you installed; fix the concept here first: a signature is never just “over a message” — it is “over a message, in an explicit context”.

Troubleshooting

  • oqs.Signature reports the mechanism unavailable — your liboqs build did not enable it; rebuild with the previous lab’s CMake flags or use a newer distribution build;
  • Trying Falcon — swap the name to Falcon-512 (some builds enable it separately). Falcon signature lengths are variable; expect roughly 666 bytes, fluctuating — that is normal;
  • Performance feel — timeit 100 sign/verify runs per parameter set; on a desktop CPU both operations sit in the sub-millisecond to millisecond range.

Next step

Signatures answer “who sent it”; what remains is running them inside a real protocol. The next lab uses system OpenSSL 3.5 to stand up a local TLS handshake with ML-KEM hybrid key exchange and an ML-DSA certificate — KEM and signature meeting in one handshake.

Security note, repeated on purpose: liboqs positions itself as a research and prototyping library. Never carry this lab’s key handling (plaintext byte export) into any real system.

Sources and further reading

  • Open Quantum Safe, liboqs-python documentation and examples. https://github.com/open-quantum-safe/liboqs-python
  • NIST, FIPS 204 (parameter and size tables), 2024.
  • On this site: “Your First ML-KEM Key Exchange with liboqs” and “ML-DSA Explained”

Tags: liboqsML-DSAPythonlab