← Back to section · Tutorials & Labs

Digital Signatures End to End with oqs-python: Generate, Sign, Verify, Export

Use the liboqs Python bindings to run the full ML-DSA-65 flow — key generation, signing, independent third-party verification and secret-key export — with the details dict and correct verifier construction explained.

Published 22 November 2026 · 6 min read · PQ Frontier

Digital Signatures End to End with oqs-python: Generate, Sign, Verify, Export
AI-generated site artwork
Video explainer · PQC programming: calling real libraries

Key encapsulation answers one question — “how do two parties agree on the same secret?” Digital signatures answer a different one: “did the holder of this secret key really produce this message, and was it altered on the way?” This tutorial uses the liboqs Python bindings (liboqs-python, imported as oqs) with ML-DSA-65 to run the complete flow: key generation and signing on the signer’s side, fully independent verification on the verifier’s side, plus secret-key export and recovery. The flow is deliberately written as two separate roles, because in real systems signing and verifying almost always happen in different processes, often on different machines.

Note: liboqs and its Python bindings are research and prototyping tools and are not recommended by their authors for direct production use. This tutorial is about the calling flow and how it maps to the concepts.

Preparation: confirm availability and meet the details dict

As with KEMs, the first step is confirming the algorithm is enabled in your build. The oqs module provides oqs.get_enabled_sig_mechanisms() to list every enabled signature mechanism; if your target algorithm is missing, constructing an oqs.Signature object raises oqs.MechanismNotEnabledError. Doing this check up front eliminates a whole class of confusing failures later.

After constructing an oqs.Signature object, look at its details attribute. It is a dict containing the algorithm name, version, claimed NIST security level, and — most importantly — three lengths: length_public_key, length_secret_key and length_signature. For ML-DSA-65 the public key is 1952 bytes and the signature is 3309 bytes — post-quantum signatures are two orders of magnitude larger than classic ECDSA. Any code that must fit a signature into a database column, a QR code or a protocol packet has to take the real numbers from details first; do not estimate from classical-era experience.

The signer: generate keys and sign

import oqs

ALG = "ML-DSA-65"
message = "Transfer 100 units to account 42".encode("utf-8")  # encode to bytes first

# Signer: construct the object and generate a key pair
with oqs.Signature(ALG) as signer:
    public_key = signer.generate_keypair()
    print("public key length:", len(public_key))   # expect 1952

    # The secret key stays inside the object; export only for backup/migration
    secret_key = signer.export_secret_key()
    print("secret key length:", len(secret_key))   # expect 4032

    # Sign the message; the signature is a byte string detached from it
    signature = signer.sign(message)
    print("signature length:", len(signature))     # expect 3309

Three details deserve attention. First, generate_keypair() returns the public key; the secret key stays inside the object, and sign needs nothing else passed in. Second, the output of export_secret_key() is sensitive material: call it only for explicit backup or migration scenarios, and guard the result like any key material — never write it to logs or casual files. Third, sign accepts and returns byte strings: if your message is text, encode it consistently (for example UTF-8), and the verifier must verify against exactly the same bytes — inconsistent encoding is the most common cause of “verification fails” during integration debugging.

The verifier: public key only, fully independent

The verifier neither needs nor should ever touch the secret key. It needs exactly three things: the original message bytes, the signature bytes, and the signer’s public key. Construct a fresh oqs.Signature object with the same algorithm name and call verify:

import oqs

ALG = "ML-DSA-65"

# Verifier: a clean object; no key pair is generated or imported
with oqs.Signature(ALG) as verifier:
    is_valid = verifier.verify(message, signature, public_key)
    print("verification result:", is_valid)        # expect True

    # Flip one byte and verify again: this must fail
    tampered = bytearray(message)
    tampered[0] ^= 0x01
    print("after tampering:", verifier.verify(bytes(tampered), signature, public_key))
    # expect False

verify returns a boolean: True when message, signature and public key all match, False otherwise — it does not raise an exception on verification failure, so the caller must check the return value explicitly. Treat “forgot to check” as the most dangerous bug in this API: an ignored False is equivalent to not verifying at all. Also note that the verifier object does not need generate_keypair called on it first; construct it and verify directly. (Generating a fresh key pair on the verifier object would not change verify’s result, since verification uses only the public key argument — but it misleads readers and should be avoided.)

Restoring signing capability from an exported secret key

An exported secret key is not only for backup — it can load the signing capability into a fresh object. The oqs.Signature constructor accepts a secret_key parameter: pass in the previously exported secret-key bytes and the new object can sign immediately, with no new key pair generated:

import oqs

ALG = "ML-DSA-65"

# Restore the signer from a previously exported secret key
with oqs.Signature(ALG, secret_key=secret_key) as signer:
    signature = signer.sign(message)

This path is critical across service restarts and process migrations: a key pair’s identity is determined by its secret key, so as long as the secret key survives, a new process on a new machine produces signatures that the original public key still verifies. Conversely, if a restart lazily calls generate_keypair again, the public key changes and every previously published copy of it will fail to verify any new signature — for a signing system, that is an identity change. The correct engineering order is therefore fixed: persist the secret key, load and restore it at start-up, and generate a fresh key pair only at first initialization or inside an explicit key-rotation procedure. After loading, immediately sign a test message with the restored object and verify it against the stored public key before serving traffic; that one step catches wrong-file and wrong-version accidents.

Common mistakes checklist

  1. Mismatched message bytes. Signing with UTF-8 and verifying with a different encoding, or a stray trailing newline, guarantees failure. Fix the encoding by convention and pass messages around as opaque byte strings.
  2. Ignoring the return value of verify. Failure is reported as False, not an exception; not checking it leaves the door wide open.
  3. Losing the association between message and signature. Detached signatures require message, signature and public key to be stored and transported as a set; design your database fields accordingly.
  4. Exporting the secret key casually. The output of export_secret_key is the complete signing capability; leaking it means your identity can be impersonated. Direct file storage is fine for a lab; real systems belong in key-management infrastructure.
  5. Assuming classical signature sizes. An ML-DSA-65 signature is 3309 bytes and SLH-DSA is larger still; protocol fields, column widths and fragmentation logic all need recalculation from the real lengths in details.

Summary

The oqs-python signature flow has only three moves: generate_keypair plus sign on one side, verify on the other. What actually goes wrong in practice is never the API itself but the engineering discipline around byte encodings, return-value checks and key custody. As a next step, combine this tutorial’s signature flow with the KEM flow from the previous article: negotiate a session key with a KEM, then authenticate the KEM public key with a signature — the standard “encrypt plus authenticate” composition used by real protocols.

Tags: liboqs-pythonML-DSADigital SignaturesProgramming