← Back to section · Tutorials & Labs

PQClean: Reading Reference Implementations as a Textbook

PQClean collects clean C implementations written for readability: its inclusion criteria, unified API and directory layout, a method for understanding an algorithm line by line, and how it divides labour with production libraries and benchmarking frameworks.

Published 22 November 2026 · 5 min read · PQ Frontier

PQClean: Reading Reference Implementations as a Textbook
AI-generated site artwork
Video explainer · Using PQC libraries: install and go

To really understand a post-quantum algorithm, the pseudocode in a standards document is often not enough: how parameters are laid out, where exactly sampling happens, how rejection sampling shapes the control flow — these details only become clear in real code. But going straight to a production library’s source buries you under vectorization tricks, platform branches and macro machinery. PQClean exists to fill that gap: it is a project that collects “clean” implementations — for each scheme, a C implementation whose primary goal is readability, with a unified interface and tests. This tutorial explains its criteria and layout, and demonstrates a method for using it to truly read an algorithm.

The inclusion criteria: what “clean” means

PQClean imposes a set of hard requirements on every implementation it accepts. First, a clean coding style: a consistent subset of C with a uniform naming discipline, no clever tricks, and identifiers that map directly onto the symbols used in the algorithm’s specification. Second, no dynamic memory allocation: an implementation may not request heap memory at runtime; all workspace is provided by the caller as arrays, which makes behaviour easier to audit and naturally suits embedded environments. Third, no reliance on undefined behaviour or non-standard compiler extensions, so results are the same under any conforming C compiler. Fourth, a unified API, covered in the next section. In addition, each implementation must ship a metadata file declaring the scheme, parameter set, provenance and licence, so downstream tools can discover and integrate implementations automatically. Together these rules send one signal: this code is written first for humans to read, and only second for machines to run.

Directory layout and the unified API

The PQClean repository is organized by scheme, then parameter set, then implementation; each implementation directory is self-contained, holding source files, a header and the metadata file. The part most worth studying is the API convention: every key encapsulation mechanism exposes crypto_kem_keypair, crypto_kem_enc and crypto_kem_dec, every signature scheme exposes the corresponding crypto_sign_keypair, crypto_sign_signature and crypto_sign_verify, and key and ciphertext lengths are given as constants in the header. This convention pays off twice. For learners, the cost of reading a second algorithm collapses — you already know what each function is supposed to do, so you can focus on the differences in the algorithm logic itself. For tooling, a uniform interface lets automated tests, correctness comparisons and benchmarking frameworks drive every scheme identically: the well-known embedded benchmarking framework pqm4 is built on exactly this PQClean-style interface, and clean implementations of many schemes flow from here into other evaluation and teaching projects.

Two easily missed details: symbol namespacing and consistency checks

Two further design details of PQClean deserve mention. The first is symbol namespacing: every exported symbol of an implementation must carry a prefix built from the scheme and parameter set, so that linking many schemes into one program cannot produce symbol collisions — precisely the precondition that lets benchmarking frameworks collect dozens of schemes onto one stage. The second is that the project ships a set of consistency-check scripts that automatically verify style, metadata completeness and test-vector passage for new submissions, turning “clean” from a verbal expectation into an executable gate. The lesson for learners: when you maintain experimental code of your own, borrow this approach — a uniform interface, uniform naming, and a metadata file plus self-test script per implementation will extend the useful life of your codebase considerably.

A method for reading an algorithm with PQClean

Here is a concrete reading route. Step one: from the standard, or from this site’s introductory article on the algorithm, write out the skeleton — the major steps and key parameters of key generation, encapsulation and decapsulation. Step two: locate those steps function by function in the implementation, paying attention to three things: where randomness is consumed, how the sampling loops are written, and how failure or rejection branches are handled — the three places where prose descriptions are most often vague and code must give definite answers. Step three: run the implementation’s own test vectors, adding prints or breakpoints at key function entries, and check that the sizes and shapes of intermediate values match the documentation. Step four: compare horizontally — find the same algorithm’s implementation in liboqs and skim it, noting which parts the optimization rewrote and which structures survived unchanged; this comparison builds intuition for the boundary between “the algorithm itself” and “engineering optimization”. You need not write a line of production code in the whole process, yet what you gain is the scarcest asset in debugging: a map of the implementation’s internals.

Its division of labour with production libraries

It must be said plainly: PQClean implementations deliberately forgo performance optimization and usually lack systematic hardening against side-channel attacks. Their role is reference and teaching, not deployment. The sensible division of labour is: use PQClean to understand an algorithm and to generate expected test values; use liboqs or oqs-provider for application prototypes and interoperability experiments; look at frameworks such as pqm4, which optimize clean implementations for specific platforms, when microcontrollers are the target; and choose a maintained, audited, certification-track production library for anything that actually ships. Keeping these four roles straight avoids both failure modes — burying a security risk by treating a textbook as a product, and giving up on understanding because optimized code was your first encounter with an algorithm. Reading source is the slowest and most solid road into cryptographic engineering, and PQClean makes its starting slope gentle.

Tags: PQCleansource readingreference implementations