← Back to section · Tutorials & Labs
Verify Your PQC Wiring with KATs: Known Answer Tests for liboqs Builds
KATs pin an algorithm's entire output down with fixed inputs and a deterministic random source, producing answers that can be compared byte for byte. This tutorial explains the principle, liboqs's bundled KAT tools and hash lists, and how to validate a self-built library.
In the earlier tutorials we checked our code by round-tripping: encapsulate, decapsulate, and see whether both sides agree on the secret. A round-trip test only proves self-consistency — it cannot prove standards compliance. An implementation with its parameters wired wrong will happily produce matching-but-wrong results on both sides. The tool that pins an implementation to the official reference is the KAT: the Known Answer Test. This tutorial explains how KATs work and how to use the KAT facilities bundled with liboqs to validate your build and your calls into it.
The principle: replace randomness with determinism
KEMs and signature schemes consume randomness internally, so under normal operation every run produces different output and nothing can be compared against a reference. A KAT replaces the random source with a deterministic generator: seeded from a fixed entropy input, every “random” draw inside the algorithm becomes a reproducible, determined value. The entire transcript — key generation, encapsulation, signing — is pinned down byte by byte, and every correct implementation on every machine produces exactly the same transcript.
With a deterministic transcript there are two ways to compare. One is a byte-for-byte comparison of the full transcript file, used in algorithm submissions and standards validation. The other, more common in practice, is to hash the whole transcript with SHA-256 and compare only the hash. liboqs takes the second route: the tests/KATs directory in the repository holds kats.json files, split by kem and sig, recording the expected hash for each algorithm. If the hash of your locally produced transcript matches the list, your algorithm chain — parameters and implementation alike — is identical to upstream.
Validating a build with liboqs’s bundled KAT programs
After building liboqs from source, the tests subdirectory of the build tree contains two programs, kat_kem and kat_sig. Each takes an algorithm name as its argument, runs the full operation sequence under the deterministic random source, and writes the transcript to standard output:
# Inside the liboqs build tree (build steps per the repository README)
./tests/kat_kem ML-KEM-768 | sha256sum
./tests/kat_sig ML-DSA-65 | sha256sum
Compare the resulting hashes, character by character, with the corresponding entries in tests/KATs/kem/kats.json and tests/KATs/sig/kats.json. A match certifies that this algorithm in your build is byte-for-byte equivalent to the upstream release — compiler optimization levels, platform ports and any patches you applied have not changed the algorithm’s behavior. A mismatch means the build is broken; do not run any further experiments against that library until you find out why. liboqs’s own continuous integration pins every algorithm down with exactly this mechanism.
This check matters most for two groups. The first is anyone building liboqs from source: build options, compiler versions and local modifications can all quietly alter behavior, and the KAT is the only low-cost acceptance test. The second is anyone upgrading liboqs: run the full KAT suite after the upgrade, and only swap the new library in once every hash matches — that is how you avoid the “upgrade installed an incompatible implementation” class of accident.
What about the Python layer: a division of labour
The oqs bindings draw randomness from the operating system and expose no deterministic mode, so KAT transcripts cannot be reproduced from Python — do not try to force it by fixing seeds; the binding’s random source is not yours to control. The correct division of labour is:
- Pin the implementation at the C layer. Correctness of the underlying library is established once, by KATs, as described above.
- Check the wiring at the Python layer. Verify with
detailsthat every output has exactly its expected length (public key, ciphertext, signature), then run round-trip consistency checks as a smoke test: encapsulation/decapsulation secrets match, signatures verify, and verification fails after tampering.
The logic behind the split: what can go wrong in a binding layer is wiring — argument order, lengths, encodings — and length checks plus round-trips cover exactly that class. Whether the algorithm implementation itself is right is the C layer’s KAT’s responsibility. Each layer checks its own failure class; together they are a complete verification.
Beyond KATs: ACVP and interoperability testing
KATs are not the only external source of verification, and knowing their neighbours helps you complete the picture. NIST maintains the Automated Cryptographic Validation Protocol (ACVP), through which implementations can submit to testing and obtain validation results — appropriate when a formal compliance credential is required. The difference from KATs is one of shape: ACVP is a submit-and-certify service, while a KAT is a self-check tool that ships with the codebase and runs locally for anyone. A second dimension is interoperability testing: having your implementation encapsulate and decapsulate, sign and verify against an independent implementation (a differently maintained PQC library, say) proves not just “matches the reference” but “works with others” — divergent readings of standard parameters usually surface exactly there. For a local lab the priorities are clear: KATs are mandatory on every build and upgrade, interoperability tests join when a new library or protocol is adopted, and ACVP is reserved for formal projects with compliance needs. Arrange the three as a ladder from cheapest to most expensive, and verification is neither absent nor overdone.
Common mistakes checklist
- Declaring an implementation correct after round-trip tests alone. Self-consistency is not compliance; mis-wired parameters round-trip just fine.
- Treating a KAT hash mismatch as “close enough”. A hash is all-or-nothing by design: one differing byte produces a completely different hash and a non-equivalent implementation. Find the cause.
- Skipping KATs after a library upgrade. Version changes are precisely when implementation behavior can drift; make the KAT suite a mandatory step in the upgrade procedure.
- Trying to reproduce deterministic output from Python. The bindings use the system random source, so outputs necessarily differ between runs; concluding the implementation is broken because outputs differ is a category error.
- Comparing a new build against an old kats.json. The list and the library ship as a set; always compare against the list bundled with the same repository version you built.
Summary
A KAT is the procedure that turns “I believe my implementation is right” into “my implementation is byte-for-byte equivalent to upstream”: a deterministic random source pins down every output, and a hash list does the comparison. Build two habits — run the KATs on every self-build, and re-run them on every upgrade — and implementation correctness becomes evidence rather than faith. As a next step, wrap the whole flow into a script: one command that builds, runs the full KAT suite, compares against the lists and prints a verdict, as the standard acceptance procedure for your local PQC lab.
Tags: liboqsKATTestingProgramming