← Back to section · Software & Developer Tools
Known-Answer Tests in Depth: What KAT Catches and What It Cannot
KAT is the oldest and most basic testing tool for cryptographic code. This article goes deeper into how it works: deterministic randomness injection, intermediate-value comparison, layered vector design, and the error classes it cannot see.
The toolbox article in this collection already introduced the basics of known-answer testing (KAT): fixed inputs, byte-for-byte output comparison. This article goes one level deeper and answers three more specific questions. How does KAT actually run inside an implementation? Why can some vector sets localize an error to a particular line of code while others only tell you “wrong”? And most importantly — once KAT passes, which errors can still be hiding in the implementation? Understanding these three points turns KAT from “run it once and see green” into a genuinely effective engineering tool.
Freezing Randomness: The Core Mechanism of KAT
Most cryptographic algorithms involve randomness: key generation draws random numbers, signing draws random masks, encapsulation draws a random message. If the randomness source differs on every run, the output naturally differs too, and there is nothing stable to compare against. KAT solves this by freezing randomness: in a test build, the random number generator is replaced with a deterministic version driven by a seed supplied in the vector file, so the whole computation degenerates into a pure function — the same input necessarily produces the same output, and any deviation shows up in the byte comparison.
This matters especially for post-quantum algorithms. ML-KEM key generation expands a seed by hashing into all key material, ML-DSA signing randomness can be derived deterministically from the private key and message, and the SLH-DSA tree structure is generated layer by layer from a seed. Standards and reference implementations exploit exactly this determinizability when publishing vectors: given the seed, the expected public key, ciphertext, and signature are all pinned down in the file. What the implementer must do is ensure their code, in test mode, accepts an externally injected deterministic random stream — which is itself an architectural requirement: the randomness source must be replaceable and injectable, never hard-coded deep inside the algorithm. An implementation that cannot do this cannot even run KAT, which is why experienced teams reserve this interface before writing the first line of algorithm code.
Intermediate Values: Localizing Errors to a Specific Stage
KAT that compares only the final output has an obvious weakness: it tells you the result is wrong, not where it went wrong. The computation chain in lattice cryptography is long — seed expansion, polynomial sampling, the number theoretic transform, multiplication, compression and encoding — and an error in any link makes the final bytes unrecognizable, leaving debugging a needle-in-a-haystack exercise. Test vectors with intermediate values exist precisely for this: besides the final output, the vector file records internal state at key stages, such as sampled polynomial coefficients, the frequency-domain representation after the transform, or the integer array before encoding. The implementation, in test mode, exports the corresponding stage’s internal state for item-by-item comparison, and the first stage that disagrees is where the error lives.
Such vectors are especially common during standardization, because a candidate algorithm’s reference and optimized implementations must cross-check each other, and editors publish quite fine-grained intermediate data. For implementers, intermediate-value vectors have a second value: distinguishing “misunderstood the algorithm” from “engineering bug.” If the sampling stage already disagrees, the cause is usually a misread specification detail — byte order, the acceptance condition of rejection sampling, or a domain separation string. If everything matches until the encoding stage, the cause is more likely a pure engineering issue such as bit packing. The fixes for these two error classes are completely different, so triaging them quickly saves substantial debugging time. Naturally, intermediate-state export must exist only in test builds and be removed from production builds — the exposure of internal state is itself a risk.
What KAT Cannot Catch: Negative Testing Fills the Gap
It must be clearly understood that KAT is a purely positive test: it uses legitimate inputs to verify that an implementation produces correct answers. Several classes of problems are entirely invisible to it. First, error handling: whether decapsulation, given a tampered ciphertext, follows the specification’s implicit rejection and returns the substitute value derived from key and ciphertext, and whether verification flatly rejects out-of-range encodings — these paths are never triggered by positive vectors. Second, robustness against malformed input: wrong-length public keys, encodings whose polynomial coefficients exceed the modulus range, signatures with missing components — whether the implementation errors out, silently computes on, or even reads out of bounds can only be answered by specially constructed negative vectors. Third, the quality of the randomness source itself: KAT replaces that source by design, so production failures such as entropy starvation are completely outside its view; those belong to the random generator’s own testing and health checks.
A mature test regime therefore treats KAT as only the first layer: above it sit negative test sets (malformed inputs and the rejection behavior expected of them), pairwise self-checks such as decapsulation consistency tests, fuzzing of parsers, and constant-time behavior checks — each covered by other articles in this collection. Seen within that regime, KAT’s position is clear: it is the cheapest layer, the first to run, and the one that must never fail. If it goes red, the implementation has not reached even basic specification conformance, and no other testing is worth discussing.
Where Vectors Come From: Trusted Sources and Version Alignment
The strength of a KAT conclusion depends entirely on the trustworthiness of the vectors. Testing against wrong vectors will only “fix” a correct implementation into a wrong one. Reliable sources, in rough priority order, are: vector sets published alongside the standard text or by the standards body, reference vectors published by the algorithm design team, and community vector sets cross-validated across multiple implementations. Version alignment deserves vigilance: parameters and details often change during standardization, and vectors from different periods may be mutually incompatible — vectors from before and after the finalization of ML-KEM, for example, cannot be mixed. Before using a vector set, confirm that the specification version it targets matches the version your implementation aims at; trivial as it sounds, this is the most common source of “false failures” in practice. Vector files themselves should also be under version control with pinned checksums, so that tests never run on a corrupted or accidentally modified foundation.
Summary
The value of KAT lies not in sophistication but in turning the fuzzy question “does the implementation conform to the specification” into a mechanically decidable byte comparison. Three practices make it work: the test build must support randomness injection and intermediate-state export; vectors must come from trusted sources aligned to the right specification version; and its boundary must be understood — beyond positive conformance, error handling, malformed inputs, and randomness quality need other testing methods to cover them. With those three in place, KAT becomes a real line of defense rather than a formality that merely “passed once.”
Tags: KATTest VectorsCryptographic Validation