← Back to section · Software & Developer Tools

Testing Constant-Time Behavior: Leakage Is Invisible — It Has to Be Measured

Constant-time discipline is the basic defense against timing side channels, but it cannot be confirmed by reading code. This article covers the sources of timing leakage, statistical measurement methods, instrumentation-based detection, and the inherent limits of such testing.

Published 22 November 2026 · 6 min read · PQ Frontier

Testing Constant-Time Behavior: Leakage Is Invisible — It Has to Be Measured
AI-generated site artwork
Video explainer · PQC testing: the gates an implementation must pass

Computing correctly is not enough for a cryptographic implementation; it must also compute “evenly.” If execution time varies systematically with the private key or plaintext, an attacker who can merely trigger the operation and measure its duration from afar can statistically estimate the secret bit by bit. Such timing side-channel attacks need no physical access to the device, which is why constant-time discipline — execution path, memory access pattern, and running time all independent of secret data — is a basic craft of cryptographic coding. But the discipline is written into code, and whether the code truly obeys it cannot be confirmed by eyeballing: compiler optimizations, processor caches, and pipeline behavior can all make source that “looks constant-time” run non-constant-time on the machine. This article explains how testing can check constant-time behavior, and what such testing can and cannot establish.

Where Leakage Comes From: Three Classes of Secret-Dependent Behavior

To test for leakage, first know what it looks like. The first class is secret-dependent branches: the code takes one path when a bit of secret data is one and another when it is zero, and if the paths differ in length, the time difference directly encodes the secret. The second class is secret-dependent memory access: when a table is indexed by a secret value, the cache line hit varies with the secret, and the timing difference between hits and misses leaks the index to anyone who can measure — the classic leakage in table-based implementations, and the main motivation for replacing lookups with full-table scans plus masked selection. The third class is subtler: variable-length operations and early exits. Big-integer routines that loop over the effective length, comparison functions that return at the first unequal byte, rejection sampling that stops early on acceptance — each “optimization” looks harmless alone, but if the iteration count or exit timing correlates with the secret, the time distribution carries the secret’s fingerprint.

In post-quantum algorithms, sampling and rejection operations deserve special attention: rejection sampling inherently carries information about “how many tries until success,” and if what is being sampled correlates with secret material, the distribution of tries can leak. Likewise, if the comparison and selection steps in decapsulation use ordinary equality comparison, early exit tells a timer “at which byte it went wrong.” Constant-time testing coverage must therefore extend beyond textbook table lookups to sampling, comparison, and conditional selection as well.

The Mainstream Method: Statistical Comparison of Two Distributions

The most common testing method is statistical measurement. Construct two classes of input: one with a fixed secret (all zeros, say, or some fixed value) and random public inputs, the other with random secrets and random public inputs. Sample a large number of execution times for the operation under test in each class, then use a statistical test to compare whether the two distributions are distinguishable. If they differ significantly, timing leakage is declared and localized to the operation under test. To reduce environmental noise, measurements are usually taken on a quiet machine with a high-resolution timer, with samples from the two classes interleaved so that drift in machine state is not misread as a difference between classes.

The method’s strength is that it is black-box and universal: it requires no understanding of the code’s internals and runs for any language and platform. Its weaknesses are equally clear: it measures distinguishability “on this machine, under this load, for these inputs,” and the conclusion transfers only so far; with too few samples, weak leakage drowns in noise and goes undetected; with very many samples, slight environmental correlations manufacture false positives that send the team chasing nonexistent leaks. The correct use of statistical measurement is therefore as a screening and regression tool: screen new code once, use it after a fix to verify the leakage signal is gone, then keep it in continuous integration as a regression guard — if some commit makes a previously clean distribution distinguishable again, that is an unambiguous signal of regression.

The Other Route: Instrumentation and Execution Tracing

Complementing black-box measurement is white-box instrumentation-based detection. The idea is to tag secret data (taint it), run the implementation on a real processor or in an emulated execution environment, record every secret-dependent branch and memory address along the way, and finally check whether those records vary with the secret. Because it observes the actual executed instruction stream rather than external timing, it is insensitive to factors beyond the cache and immune to environmental noise, and it can localize leakage to a specific instruction, making fixes precisely targeted.

The cost of instrumentation is that it needs a dedicated toolchain and runtime environment, executes slowly, and struggles to cover large input volumes; moreover, it only observes the execution paths actually taken, leaving uncovered paths to review and measurement. In practice the two routes are combined: instrumentation for deep investigation and localization during development, statistical measurement for black-box screening and regression during integration — confidence is only solid when the two agree. There is also a research direction that expresses constant-time properties as verifiable assertions and provides guarantees at the compilation or proof level, but that lies beyond testing, in another branch of design and verification methodology.

Limitations: Not Measuring It Does Not Mean It Is Absent

The inherent limits of this kind of testing must be faced honestly. First, microarchitectural complexity: caches, prefetching, and speculative execution in modern processors make “time” a composite of many factors, and indistinguishability measured on today’s machine does not guarantee the same result on the next processor generation. Second, compiler uncertainty: masked selection and constant-time comparisons at source level can be “optimized” back into branching form by the compiler, so the object of testing must be the actual behavior of the final binary, not the source’s intent — and repeating the check under different build options is a necessary discipline. Third, the coverage problem: test inputs cover only a small part of the secret space, and leakage paths that are never triggered will not reveal themselves.

These limits define the role of constant-time testing: it is a powerful screening and regression instrument that catches many real leaks and defends fixes once made, but it never provides a mathematical proof of “no leakage.” The right engineering attitude is to stack testing, code review, and coding standards: standards keep new code from introducing leakage, review catches design-level hazards, and testing performs the final check at the level of real execution. No single layer suffices alone; together, the three come close to cryptographic engineering’s complete answer on side channels.

Summary

Constant-time testing has a single core question: does behavior vary with the secret? Statistical measurement answers with the distinguishability of two timing distributions — cheap and universal, suited to screening and regression. Instrumentation-based tracing answers with instruction-level actual execution — precisely localizing, suited to deep investigation. Both must target the final binary on the target platform, and both require honestly acknowledging their boundary: failing to measure leakage is not the same as its absence. Keeping that honesty is what prevents constant-time testing from degenerating into a formality that issues certificates to insecure implementations.

Tags: Constant TimeSide ChannelsTiming Analysis