How pq-crystals/dilithium works
Purpose and variants
This is the official C implementation of the FIPS 204 Dilithium post-quantum signature scheme. It ships two parallel implementations: portable reference code in ref/ and x86-64 AVX2-optimized code in avx2/, each supporting parameter sets Dilithium2, 3, and 5.
Public signing boundary
The public API exposes key generation, detached/attached signing, and verification with parameter-set-specific sizes and namespacing. Core entry points and scheme orchestration are in:
• ref/api.h, ref/sign.c, ref/sign.h
• avx2/api.h, avx2/sign.c, avx2/sign.h
Compile-time parameter selection is encoded in each implementation’s params.h; config.h controls hedged versus deterministic signing.
Signing and verification pipeline
sign.c coordinates the protocol: derive seeds and secrets during key generation; expand the public matrix; sample masking vectors; compute matrix-vector products; decompose/round coefficients; derive a Fiat–Shamir challenge; and pack outputs. Verification reverses the public relation, reconstructs the challenge input, and compares it to the signature challenge.
The principal data boundary is serialized byte arrays versus internal polynomial/vector structures:
• Serialization: ref/packing.c, ref/packing.h; avx2/packing.c, avx2/packing.h
• Polynomial vectors: ref/polyvec.c, ref/polyvec.h; avx2/polyvec.c, avx2/polyvec.h
• Polynomial primitives: ref/poly.c, ref/poly.h; avx2/poly.c, avx2/poly.h
Lattice arithmetic layer
Polynomial operations operate modulo Dilithium’s ring and modulus. The reference implementation expresses transforms and reduction in portable C:
• ref/ntt.c, ref/reduce.c, ref/rounding.c
The AVX2 path retains compatible high-level C interfaces but implements performance-critical transforms and pointwise arithmetic in assembly:
• avx2/ntt.S, avx2/invntt.S, avx2/pointwise.S
• avx2/rounding.c, avx2/rejsample.c
This is the primary portability/performance boundary; callers should use the shared polynomial and signing interfaces rather than architecture-specific assembly.
Symmetric cryptography and entropy
SHAKE/FIPS 202 provides hashing, matrix expansion, challenge derivation, and pseudorandom sampling:
• ref/fips202.c, ref/symmetric-shake.c, ref/symmetric.h
• avx2/fips202.c, avx2/fips202x4.c, avx2/symmetric-shake.c
AVX2 adds four-lane Keccak processing, including assembly permutation support in avx2/f1600x4.S. Randomness enters through randombytes for key generation and, by default, hedged signing:
• ref/randombytes.c, ref/config.h
• avx2/randombytes.c, avx2/config.h
Build, artifacts, and runtime assumptions
ref/Makefile and avx2/Makefile build per-parameter-set tests, benchmarks, and shared libraries. The AVX2 build requires an x86 CPU supporting AVX2; the reference build is the portable baseline. OpenSSL is an external dependency for some test/KAT-related programs, not the Dilithium cryptographic core.
Validation and interoperability
Functional tests exercise repeated API keygen/sign/verify and malformed signatures; deterministic-vector programs expose intermediate algorithm values for cross-implementation compatibility:
• ref/test/test_dilithium.c, ref/test/test_vectors.c
• avx2/test/test_dilithium.c, avx2/test/test_vectors.c
NIST KAT generation is isolated under ref/nistkat/, including its RNG adapter and PQCgenKAT_sign.c.
Main entry points: ref/sign.c Reference signing protocol · avx2/sign.c AVX2 signing protocol · api.h Reference API · Makefile Per-variant builds
Components 20
Public interfaces
- Application callers
Portable reference
- Reference API
ref/api.h - Reference signing protocol · Keygen, signing, and verification flows.
ref/sign.c - Reference parameters
ref/params.h - Reference serialization · Converts keys and signatures to and from byte arrays.
ref/packing.c - Reference polynomial layer · Polynomial and vector operations over the Dilithium ring.
ref/poly.c - Portable NTT and reduction
ref/ntt.c - Reference SHAKE · Hashing, expansion, challenge derivation, and sampling.
ref/symmetric-shake.c - Reference randomness · Used by key generation and hedged signing.
ref/randombytes.c
AVX2 optimized
- AVX2 API
avx2/api.h - AVX2 signing protocol · Compatible optimized keygen, signing, and verification.
avx2/sign.c - AVX2 parameters
avx2/params.h - AVX2 serialization
avx2/packing.c - AVX2 polynomial layer · Shared high-level polynomial interface for optimized arithmetic.
avx2/poly.c - AVX2 arithmetic assembly · NTT, inverse NTT, and pointwise operations.
avx2/ntt.S - AVX2 SHAKE x4 · Four-lane Keccak acceleration.
avx2/fips202x4.c - AVX2 randomness · Used by key generation and hedged signing.
avx2/randombytes.c
Build and validation
- Per-variant builds · Build parameter-set tests, benchmarks, and shared libraries.
ref/Makefile - API and vector validation · Checks signing behavior, malformed signatures, and interoperability vectors.
ref/test/test_dilithium.c - NIST KAT generation · Isolated deterministic KAT generation path.
ref/nistkat/PQCgenKAT_sign.c
Connections 20
- Application callers → Reference API: portable API
- Application callers → AVX2 API: AVX2 API
- Reference API → Reference signing protocol: keygen / sign / verify
- AVX2 API → AVX2 signing protocol: keygen / sign / verify
- Reference parameters → Reference signing protocol: selects Dilithium 2/3/5
- AVX2 parameters → AVX2 signing protocol: selects Dilithium 2/3/5
- Reference signing protocol → Reference serialization: pack and unpack
- AVX2 signing protocol → AVX2 serialization: pack and unpack
- Reference signing protocol → Reference polynomial layer: matrix and vector operations
- AVX2 signing protocol → AVX2 polynomial layer: matrix and vector operations
- Reference polynomial layer → Portable NTT and reduction: transforms and reduction
- AVX2 polynomial layer → AVX2 arithmetic assembly: optimized transforms
- Reference signing protocol → Reference SHAKE: expand and derive challenge
- AVX2 signing protocol → AVX2 SHAKE x4: expand and derive challenge
- Reference randomness → Reference signing protocol: keygen and hedged signing
- AVX2 randomness → AVX2 signing protocol: keygen and hedged signing
- Per-variant builds → API and vector validation: builds
- Per-variant builds → NIST KAT generation: builds
- API and vector validation → Reference API: exercises
- NIST KAT generation → Reference signing protocol: generates deterministic vectors