← Back to section · Tutorials & Labs
The dudect-Style Lab: Quantifying Your Constant-Time Implementation with Statistical Tests
Borrowing the fixed-versus-random input method of the open-source tool dudect, run statistical timing-leak tests on cryptographic functions you wrote yourself in an isolated environment, and learn to judge leakage with t values instead of eyeballs.
Scope and Goals
The previous timing self-test lab used the “group by matched prefix and look for a staircase” approach, which suits clearly structured objects like comparison functions. When the subject becomes a complete signature, decapsulation, or sampling function, leakage can hide in any secret-dependent branch, and preset groupings may not catch it. The open-source tool dudect offers a more general method, and this lab borrows its approach to run a statistically meaningful leak test on cryptographic functions you wrote yourself.
The boundary is unchanged: the code under test must be your own, the lab runs only on your own machine or in an isolated virtual machine, and no live service or anyone else’s system is tested. The goal is a measurement you can perform and restate independently: construct fixed-class and random-class inputs, collect timings interleaved, reach a quantitative conclusion with a Welch t-test, and phrase the strength of that conclusion correctly.
Method: Two Input Classes and the t-Test
dudect’s approach decomposes into three steps. Step one: prepare two classes of input. One is the fixed class — the same secret and message on every call. The other is the random class — fresh random secrets or messages on every call. If the function’s running time is independent of secret data, the two classes’ timing distributions should be indistinguishable; if some code path triggers only for particular secret values, the distributions separate. Step two: interleaved measurement. Do not measure one class completely before the other; invoke in random interleaved order, recording the timing and the class label each time, so slow drift in machine state acts on both classes equally instead of masquerading as a class difference. Step three: statistical testing. Run a Welch t-test on the two sample sets to obtain a t value; the larger the sample, the stronger the power to detect small differences.
Why anchor on a fixed class? Because under fixed input, data-dependent cache hits, branch prediction, or early exits inside a function often give the timing distribution a shape distinct from random input, and the fixed-versus-random contrast is especially sensitive to exactly that kind of difference. Many published cryptographic library assessments found seemingly harmless leak points with precisely this method.
Procedure
Step one: prepare the function under test and the bench. Extract the target function from your project into a minimal independently callable unit, fix the compiler options, and record them — conclusions at different optimization levels are not comparable. The bench generates the two input classes, invokes interleaved, and records nanosecond timings.
Step two: press the noise down. Disable or lock CPU frequency scaling, pin to a single core where possible, shut down unrelated background work, and run a warm-up period so caches and frequency settle into steady state. The measurement loop does no allocation or I/O; inputs are generated in bulk beforehand.
Step three: collect and test incrementally. Collect in batches, updating the t value after each batch and recording it. The skeleton looks roughly like this:
import random, statistics
def welch_t(a, b):
ma, mb = statistics.mean(a), statistics.mean(b)
va, vb = statistics.variance(a), statistics.variance(b)
return (ma - mb) / ((va / len(a) + vb / len(b)) ** 0.5)
Step four: localize. If the t value crosses the line, do not conclude immediately: first swap the fixed class for a different fixed input and re-test to rule out chance; then progressively narrow the scope under test (bisect by commenting out sub-calls, or time segments separately) and localize the leak to a specific branch, table lookup, or variable-length operation.
Reading the Results
A common empirical threshold is an absolute t value of 4.5 as a rough significance boundary; more conservative engineering assessments use 10 or higher, and require re-verification at sample sizes in the millions. Remember three sentences when interpreting: crossing the line is strong evidence of a data-dependent timing difference and deserves to be treated as a leak; staying below the line is only “not detected under this experiment’s conditions,” with detection power limited by sample size and noise — write the sample size into the conclusion; and a t value that keeps growing with sample size is the most dangerous signal of all — even before it crosses the line, it indicates a real difference exists and individual batches simply do not have enough samples yet.
When wiring this test into continuous integration, mind its temperament: on busy shared machines it false-alarms easily, so it belongs on a dedicated test machine or in long overnight runs. Its positioning complements the staircase method of the previous lab — the staircase checks known structure, dudect checks unknown differences. A constant-time implementation that has passed both can withstand its own lab’s questioning.
What a Complete Experiment Record Contains
So that conclusions are restatable and accountable, every dudect-style test should leave a structurally complete record. At minimum: the name and code version of the function under test, compiler version and optimization flags, the test machine’s hardware and frequency policy, how the two input classes were constructed, total sample size and batch count, the t-value series at each checkpoint, the final conclusion and its strength phrasing. The t-value series matters most: reporting only one final number hides the most diagnostically valuable shape — “monotonically growing with sample size” — while plotting the series makes the presence or absence of a leak obvious at a glance. The record must also state the uncovered scope — for example, that this run tested decapsulation but not key generation, or only a single parameter set. Writing the conclusion’s boundaries clearly does not weaken it; it tells the next person where to test next. Accumulated over time, these records assemble into a historical picture of the implementation’s timing health: which refactor once introduced a leak, which class of function relapses most easily — all leave traces in the picture. That is the mark of side-channel self-testing upgraded from a one-off experiment into an engineering habit.
One practical reminder: when running this test for the first time, take a function with a deliberately data-dependent branch as a positive control, and confirm the whole measurement pipeline can drive a known leak across the line before turning to the target function. A negative result without a positive control deserves a discount — you cannot distinguish “the function is genuinely clean” from “the pipeline cannot detect anything at all.” The step costs only a dozen minutes or so, yet raises the credibility of the entire conclusion by a full grade; it is one of the most concrete differences between professional assessment and amateur self-testing.
Tags: dudectconstant timestatistical testingside-channel self-test