← Back to section · Tutorials & Labs

Project: A Batch Verifier for KAT Test Vectors

Parse NIST-style KAT vector files and run three layers of batch checks — field lengths against the parameter table, shared-secret recomputation by decapsulation, and signature verification — to learn what standards conformance testing actually tests.

Published 22 November 2026 · 5 min read · PQ Frontier

Project: A Batch Verifier for KAT Test Vectors
AI-generated site artwork
Video explainer · Code examples and hands-on projects

On what basis may a cryptographic implementation claim it “conforms to FIPS 203”? Not on its own say-so, but by feeding it the officially published test vectors and checking that the outputs match byte for byte — the KAT, or known answer test. This project builds a batch verifier for them: it reads NIST-style KAT vector files (.rsp text format, each vector carrying a count number, a seed, and hex fields for the public key, secret key, ciphertext, and shared secret), recomputes each vector with liboqs, sweeps through hundreds or thousands of them, and reports a pass rate plus the location of the first failure. It is both a practical tool and the best teaching aid for feeling the weight of the word “conformance”. The boundary: this is a teaching tool practised against public vector files. Passing it shows only that the implementation agrees with this batch of vectors; it is not an official certification. Formal cryptographic module validation runs through the full FIPS 140-3 and CMVP process, of which vector checking is only one small step.

Goal and Architecture

The verifier has three layers, ordered by cost. Layer one, format and length checks: parse each vector’s fields and verify that every hex length equals exactly twice the published parameter value — one check each for the public key, the ciphertext, and the shared secret. This layer performs no cryptography at all, yet it catches most low-level errors: the wrong parameter set downloaded, a truncated field, a character lost in copying all show up here. Layer two, KEM recomputation: construct an oqs.KeyEncapsulation object with the vector’s secret key, run decap_secret on the vector’s ciphertext, and compare the resulting shared secret byte for byte against the vector’s ss field. Note that the verifier can only recompute in the decapsulation direction: encapsulation involves randomness and cannot be replayed to the same ciphertext, which makes KAT verification inherently asymmetric. Layer three, signature verification: for signature algorithm vectors, take the message, signature, and public key and call verify. Signatures are deterministically verifiable; what this layer checks is whether the implementation’s signing behaviour on the same input lands inside the legal set the vector defines. For randomized signature schemes the verifier checks that “the vector’s signature verifies under the public key”, not that its own signature equals the vector’s byte for byte — a distinction the report must state explicitly.

Architecturally, the parser and the verifier are strictly separate: the parser only turns .rsp text into a list of dictionaries and reports format errors; the verifier only does cryptographic checking. The benefit is that vector file formats have several historical variants with differing field names and layouts; when the format changes, only the parser moves and the verification logic stays untouched.

Walking Through the Core Code

The core of parsing and of the layer-two check:

import oqs

def parse_rsp(text: str) -> list[dict]:
    records, current = [], {}
    for line in text.splitlines():
        line = line.strip()
        if not line or line.startswith("#"):
            continue
        if " = " in line:
            k, v = line.split(" = ", 1)
            current[k.strip()] = v.strip()
        if line.startswith("count") and current.get("pk"):
            records.append(current); current = {}
    if current:
        records.append(current)
    return records

def check_kem_record(alg: str, rec: dict) -> bool:
    sk = bytes.fromhex(rec["sk"])
    ct = bytes.fromhex(rec["ct"])
    ss_expected = bytes.fromhex(rec["ss"])
    with oqs.KeyEncapsulation(alg, secret_key=sk) as kem:
        return kem.decap_secret(ct) == ss_expected

Three details. First, the secret_key constructor argument of KeyEncapsulation is the entry point for injecting a vector’s secret key — business code never needs it, but KAT verification cannot move a step without it, which is why you read the interface documentation before writing code. Second, length checks must run on the hex string before bytes.fromhex: an odd-length hex string makes fromhex raise, mixing parse errors into cryptographic failures and destroying the report’s ability to locate problems. The verifier’s report must distinguish three failure kinds — “the file is malformed”, “a length is wrong”, “the recomputation disagrees”. Third, when sweeping thousands of vectors, do not exit at the first failure; collect every failing count number and summarize. Conformance testing cares about the failure pattern — total wipeout versus a scattered few, and which field they cluster in — not about the position of the first failure.

Running and Verifying

The verifier itself needs verifying, with three control experiments. First, the positive control: run the full official vector set matching liboqs and the pass rate must be 100%; any failure should make you suspect the verifier, or a version mismatch between the vector set and the parameter set, before anything else. Second, the negative control: hand-edit one hex character of some vector’s ss, and the verifier must pinpoint that exact count number as failing at layer two — if it cannot, the comparison logic is decoration. Third, the cross control: feed ML-KEM-512 vectors into the ML-KEM-768 checking path, and layer one’s length check must stop them on the spot rather than letting decapsulation fail later. The three controls guard the verifier’s three virtues respectively: never convict a good implementation, never let bad data through, and catch errors at the cheapest layer.

Exercises

One: complete the signature side — parse ML-DSA KAT files, verify every vector, and count how many failures each of the three layers intercepts. Two: add a vector-set fingerprint — hash the entire vector file with SHA-256 and write it into the report, so the report can answer “exactly which revision of the vectors did I test”; vector sets are themselves revised, and version mismatch is a common source of conformance disputes. Three: turn the verifier into a continuous-integration step with the exit code tied to the failure count, and discuss why CI should pin a fixed version of the vector files instead of pulling the latest each time. Four, the advanced one: research why interactive online testing such as ACVP (the Automated Cryptographic Validation Protocol) is needed alongside KATs, and write up how the two divide coverage and trust assumptions between them.

Summary

This verifier reduces “conforms to the standard” from a slogan to executable actions: check lengths, recompute, compare, report. It also teaches the right posture toward conformance — suspect your own tool first, the implementation second, and the vectors last. The final boundary: a teaching tool’s self-test pass is only a self-test; formal certification has its own complete process, and the two should never be conflated.

Tags: KATConformance TestingML-KEMTest Vectors