Techniqueadvanced
Test vector leakage assessment
Also called fixed versus random t-test.
Tests a device for leakage by a Welch t-test between traces of fixed and random inputs (the non-specific variant), with 4.5 as the conventional threshold.
Test vector leakage assessment (TVLA) asks whether a device leaks, without trying to find a key. In its common, non-specific form, the evaluator records one group of power traces with a fixed input and another with random inputs, in random order, and computes a Welch t-test between the two groups at every time sample. If the device leaks any information about the data it processes, the mean power at some sample differs between the fixed and the random group. Goodwill, Jun, Jaffe and Rohatgi (2011) proposed the method with a threshold and a repetition on a second, independent set of traces. The threshold is a convention, not a theorem. Schneider and Moradi (2015) extended the test to higher orders, which is needed to test masked implementations.
In plain words
Run the chip many times on the same input and many times on random inputs, and record its power each time. If the chip hides its data well, the two piles of recordings look the same on average at every moment. If it does not, at some moment the average of one pile is a little higher than the other. A t-test measures whether such a difference is larger than what random noise would make. A big value at some moment means the chip shows something about its data there. The test does not say whether an attacker can use the leak, or how. A small value does not prove that the chip is safe, only that this test with this many traces found nothing.
When it applies
- A leakage question, not an attack. Use it to decide whether an implementation passes or fails, or to find the samples that leak during development. It needs no model of the leakage and no knowledge of the key.
- Control of the inputs. The evaluator must choose the input of each run, so that a fixed group and a random group can be made. The fixed input is often chosen so that some intermediate value is special, but any input works.
- Comparable runs. The traces must be aligned in time, and the two groups must be recorded interleaved in random order, so that a drift of the temperature or of the supply affects both the same way.
- First order by default. The plain test compares means. A first-order masked implementation has the same mean for every input at each sample, so the test must be repeated on combinations of samples, or on higher moments, to find a higher-order leak.
The template
TVLA(device, n traces for each group, threshold C, order o)- 1
for each of two independent sets s = 1, 2 do - 2
record n traces with a FIXED input and n with RANDOM inputs, interleaved at random - 3
for each sample j do t_s[j] ← WELCH-T(PREPROCESS_o(fixed group, j), PREPROCESS_o(random group, j)) - 4
leaky ← the samples j with |t_1[j]| > C and |t_2[j]| > C - 5
return fail if leaky is not empty, else pass
The holes are the inputs FIXED and RANDOM, the preprocessing for the order and the threshold . Line L3 is the Welch statistic
with the sample means and the unbiased sample variances of the two groups at sample . For , PREPROCESS is the identity. For a second-order test on one pair of samples , it replaces each trace by the centred product , with the means of its own group; for a univariate test of order 2 on one sample, by (Schneider and Moradi).
Line L4 is the decision rule of Goodwill and others: a sample fails only if both sets pass the threshold there. For a normal variable, . If there is no leak and is large, is close to normal at each sample (a theorem, by the central limit theorem). So with samples in a trace, one set gives about 0.68 false alarms on average. The two-set rule makes a false alarm at the same sample about likely for each sample, if the sets are independent. These numbers are approximations for large and independent samples; they are heuristics for real traces.
The cost is arithmetic operations for each set with samples; a second-order test on all pairs costs .
Classic examples
The examples use a synthetic device: the 4-bit PRESENT S-box, key , and traces of 4 samples with Gaussian noise . Sample 0 leaks; the others are noise. The fixed input is , so the fixed value is , with Hamming weight 4. The code of this page draws these traces from the stated model with a seeded random.Random; it never reads a device, a file or a clock, and its tests reproduce the numbers below.
An unprotected device. Sample 0 is . The fixed group has mean 4 and variance 1; the random group has mean 2 and variance . So : this is the expected value for large . With the two sets gave and at sample 0 and elsewhere, so the device fails at sample 0 only. Over 20 runs with , the mean of was 16.2, against . The same leak is what DPA and CPA use to find the key.
A first-order masked device. Sample 0 is and sample 1 is for a fresh uniform mask , as in Boolean masking. Each sample has mean 2 for every input, so the first-order test passes: with , at all samples. The centred product of samples 0 and 1 has expectation for the value , which is for the fixed and 0 on average over random . The second-order test on the same traces gave and , so the device fails at order 2, as a first-order masked device must.
A threshold implementation in hardware. For hardware, the test is run on the masked design, for example a threshold implementation, to check that glitches do not combine shares. A first-order pass with many traces, together with a second-order failure, is the expected result for a design with three shares and first-order security.
A specific test. Instead of fixed against random, split the random traces by one bit of an intermediate value, such as a bit of with the known key. This specific test finds a leak of that bit, and it is close to DPA with the right key. It needs a model of the intermediate values; the non-specific test does not.
Variations
- Fixed versus fixed. Two different fixed inputs give two groups with smaller variance than a random group, so the test can be more sensitive to a leak that the two inputs separate. It misses leaks that both inputs share.
- Higher-order and multivariate tests. Schneider and Moradi (2015) define tests on centred moments of order for one sample, and on centred products of samples, with one-pass formulas that update the moments as traces arrive.
- Other statistics. A test on histograms of the samples, or an estimate of the mutual information, can detect differences in distribution that the mean misses, at a cost in traces.
- Corrections for many samples. With very many samples, a Bonferroni correction or a control of the false discovery rate gives a threshold with a stated error probability, instead of the fixed 4.5.
Pitfalls
- A pass is not a proof. The test finds only differences that its statistic and its can see. A device can pass with traces and fail with , and a first-order pass says nothing about second-order leaks.
- A fail is not an attack. A leak at one sample of the input loading, or of a value that is public, is a fail, but it may not help an attacker. The evaluator must find out what leaks before claiming an attack.
- Groups recorded apart. If the fixed group is recorded first and the random group second, a slow drift of the device appears as a difference of means at every sample. Interleave the two groups in random order.
- Small groups. With 30 traces in each group the Welch statistic has heavier tails than the normal law: the synthetic test of this page measured for 110 of 2000 samples of pure noise (5.5 percent), against 4.55 percent for the normal law. The threshold 4.5 assumes large groups.
- The wrong means in second order. The centring of the second-order preprocessing must use the mean of each group, or of all traces, the same way in both groups. Centring with a constant that is not the mean adds a first-order term to the product.
- Many samples. Without the second set, a trace with millions of samples has false alarms by chance. Use the two-set rule or a correction for multiple tests.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/test-vector-leakage-assessment/. The authors are listed in prov/89GDF4.jsonl.
"""The fixed versus random Welch t-test of TVLA (Goodwill, Jun, Jaffe, Rohatgi, 2011).
The traces come from a stated synthetic model of a device, drawn with a
seeded ``random.Random``: one sample leaks HW(S(p xor k)) + N(0, sigma^2)
with the 4-bit PRESENT S-box, or, for the masked model, two samples leak
HW(S(p xor k) xor m) and HW(m) for a fresh uniform mask m. No device, file
or clock is used.
"""
import math
import random
PRESENT_SBOX: tuple[int, ...] = (
0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD,
0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2,
)
Trace = list[float]
def hamming_weight(v: int) -> int:
"""Return the number of 1 bits of a non-negative integer."""
return bin(v).count("1")
def device_trace(rng: random.Random, p: int, key: int, samples: int, sigma: float, masked: bool) -> Trace:
"""One synthetic trace of the given number of samples (at least 2).
Unmasked: sample 0 is HW(S(p xor key)) + noise. Masked: sample 0 is
HW(S(p xor key) xor m) + noise and sample 1 is HW(m) + noise for a
fresh uniform m. Every other sample is noise with mean 2.
"""
v = PRESENT_SBOX[p ^ key]
tr = [2.0 + rng.gauss(0.0, sigma) for _ in range(samples)]
if masked:
m = rng.randrange(16)
tr[0] = hamming_weight(v ^ m) + rng.gauss(0.0, sigma)
tr[1] = hamming_weight(m) + rng.gauss(0.0, sigma)
else:
tr[0] = hamming_weight(v) + rng.gauss(0.0, sigma)
return tr
def collect(
rng: random.Random, n: int, fixed_input: int, key: int, samples: int, sigma: float, masked: bool
) -> tuple[list[Trace], list[Trace]]:
"""n traces with the fixed input and n with uniform inputs, in random order."""
fixed: list[Trace] = []
rand: list[Trace] = []
while len(fixed) < n or len(rand) < n:
if len(rand) >= n or (len(fixed) < n and rng.randrange(2) == 0):
fixed.append(device_trace(rng, fixed_input, key, samples, sigma, masked))
else:
rand.append(device_trace(rng, rng.randrange(16), key, samples, sigma, masked))
return fixed, rand
def welch_t(x: list[float], y: list[float]) -> float:
"""Welch's t statistic of two samples, each of size at least 2.
t = (mean(x) - mean(y)) / sqrt(var(x)/|x| + var(y)/|y|) with unbiased
variances. Returns 0.0 if both variances are 0 and the means are equal,
and +-inf if only the variances are 0. Cost: Theta(|x| + |y|).
"""
nx, ny = len(x), len(y)
mx, my = sum(x) / nx, sum(y) / ny
vx = sum((a - mx) ** 2 for a in x) / (nx - 1)
vy = sum((b - my) ** 2 for b in y) / (ny - 1)
se = math.sqrt(vx / nx + vy / ny)
if se == 0.0:
return 0.0 if mx == my else math.copysign(math.inf, mx - my)
return (mx - my) / se
def centred_products(traces: list[Trace], i: int, j: int) -> list[float]:
"""Second-order preprocessing: (l[i] - mean_i) * (l[j] - mean_j) for each trace.
The means are those of the given group. Cost: Theta(len(traces)).
"""
mi = sum(t[i] for t in traces) / len(traces)
mj = sum(t[j] for t in traces) / len(traces)
return [(t[i] - mi) * (t[j] - mj) for t in traces]
def tvla(sets: list[tuple[list[Trace], list[Trace]]], threshold: float = 4.5) -> tuple[list[list[float]], list[int]]:
"""The fixed versus random test on independent sets of traces.
Each set is (fixed traces, random traces), all with T samples and at
least 2 traces in each group. Returns the t-values of each set at each
sample and the samples where |t| > threshold in every set; the device
fails the test if that list is not empty.
Cost: Theta(s * n * T) for s sets of n traces in each group.
"""
tvals: list[list[float]] = []
for fixed, rand in sets: # @line L1
width = len(fixed[0]) # @line L2
row = []
for j in range(width): # @line L3
row.append(welch_t([t[j] for t in fixed], [t[j] for t in rand]))
tvals.append(row)
leaky = [j for j in range(len(tvals[0])) if all(abs(r[j]) > threshold for r in tvals)] # @line L4
return tvals, leaky # @line L5
test_test_vector_leakage_assessment.py 92 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/test-vector-leakage-assessment/. The authors are listed in prov/89GDF4.jsonl.
import math
import random
import statistics
import pytest
from leakage_assessment import (
PRESENT_SBOX,
centred_products,
collect,
hamming_weight,
tvla,
welch_t,
)
KEY = 0xB
FIXED = 0x1 # S(1 xor B) = F, Hamming weight 4
def test_welch_t_against_statistics() -> None:
rng = random.Random(20240507)
for _ in range(500):
x = [rng.gauss(rng.uniform(-1, 1), rng.uniform(0.1, 3)) for _ in range(rng.randrange(2, 40))]
y = [rng.gauss(rng.uniform(-1, 1), rng.uniform(0.1, 3)) for _ in range(rng.randrange(2, 40))]
se = math.sqrt(statistics.variance(x) / len(x) + statistics.variance(y) / len(y))
want = (statistics.mean(x) - statistics.mean(y)) / se
assert welch_t(x, y) == pytest.approx(want, rel=1e-9, abs=1e-12)
assert welch_t(y, x) == pytest.approx(-want, rel=1e-9, abs=1e-12)
def test_welch_t_edges() -> None:
assert welch_t([1.0, 1.0], [1.0, 1.0]) == 0.0
assert welch_t([2.0, 2.0], [1.0, 1.0]) == math.inf
assert welch_t([0.0, 0.0], [1.0, 1.0]) == -math.inf
def test_fixed_input_has_weight_four() -> None:
assert hamming_weight(PRESENT_SBOX[FIXED ^ KEY]) == 4
def test_unprotected_device_fails() -> None:
rng = random.Random(1)
sets = [collect(rng, 50, FIXED, KEY, 4, 1.0, False) for _ in range(2)]
tvals, leaky = tvla(sets)
assert leaky == [0]
assert all(abs(v) < 4.5 for row in tvals for v in row[1:])
def test_t_grows_as_the_root_of_n() -> None:
# Expected t is 2 / sqrt(1/n + 2/n): fixed variance 1, random variance 1 + Var(HW) = 2.
rng = random.Random(2)
n = 200
ts = [tvla([collect(rng, n, FIXED, KEY, 2, 1.0, False)])[0][0][0] for _ in range(20)]
assert statistics.mean(ts) == pytest.approx(2 * math.sqrt(n / 3), rel=0.1)
def test_masked_device_first_and_second_order() -> None:
rng = random.Random(3)
sets = [collect(rng, 1000, FIXED, KEY, 4, 1.0, True) for _ in range(2)]
assert tvla(sets)[1] == []
second = [
([[v] for v in centred_products(f, 0, 1)], [[v] for v in centred_products(r, 0, 1)])
for f, r in sets
]
tvals, leaky = tvla(second)
assert leaky == [0]
# E[(HW(x xor m) - 2)(HW(m) - 2)] = 1 - HW(x)/2 = -1 for the fixed x = F, and 0 for random x.
assert all(row[0] < 0 for row in tvals)
def test_second_order_expectation_exact() -> None:
for x in range(16):
e = sum((hamming_weight(x ^ m) - 2) * (hamming_weight(m) - 2) for m in range(16)) / 16
assert e == 1 - hamming_weight(x) / 2
assert sum(1 - hamming_weight(x) / 2 for x in range(16)) == 0
def test_no_false_alarms_on_noise() -> None:
rng = random.Random(4)
over2 = over45 = total = 0
for _ in range(100):
f = [[rng.gauss(0, 1) for _ in range(20)] for _ in range(30)]
r = [[rng.gauss(0, 1) for _ in range(20)] for _ in range(30)]
row = tvla([(f, r)])[0][0]
total += len(row)
over2 += sum(abs(v) > 2 for v in row)
over45 += sum(abs(v) > 4.5 for v in row)
assert over45 == 0
# With 30 traces per group the t law has heavier tails than the normal law (0.0455).
assert 0.03 < over2 / total < 0.08
Exercises
- In the unprotected example, compute the expected at sample 0 for , and the at which it reaches 4.5.
- Show that for a uniform 4-bit mask , for every , so the first-order test of a masked device has the same mean in both groups.
- Show that . Which fixed values make the second-order test blind?
- A trace has samples, and one set is tested at . How many false alarms do you expect with no leak? What threshold gives an expected 0.01 false alarms for one set?
Proof: The fixed versus random t-test has the stated false alarm rate for large groups and detects every difference of meansfull
Fix one sample and one set of traces. The fixed group is independent and identically distributed with mean and variance ; the random group is independent of it and identically distributed with mean and variance . Line L3 computes , where and are the sample mean and the unbiased sample variance, and the same for . The code computes exactly these sums, so we study .
Lemma 1. and with probability 1.
Proof. . By the strong law of large numbers, and , and .
Theorem 1 (false alarms). If , then converges in distribution to the standard normal law, so for every ,
which is about for . If the two sets of line L1 are independent, the probability that line L4 flags the sample tends to , about .
Proof. The differences are independent with mean 0 and variance , so by the central limit theorem converges in distribution to . By Lemma 1, with probability 1. By Slutsky's lemma the product, which is , converges to in distribution. The normal law has no atom at , so the probabilities converge. The values of the two sets are independent, so the probability that both exceed is the product.
Theorem 2 (detection). If , then for every , and so the flag of line L4 has probability tending to 1.
Proof. . The ratio converges with probability 1 to , so with probability 1.
So the test with large groups has the claimed rate of false alarms at each sample that does not leak at first order, and it finds every sample whose mean depends on the input class. The rate is a limit, not a bound for finite ; for finite and Gaussian data, the law of is close to a Student law with heavier tails.
Proposition 3 (first-order masking). In the masked model, and with , a uniform 4-bit mask independent of , and independent centred noise. Then the mean of each sample is 2 for every input , so by Theorem 1 the first-order test has the false alarm rate of a device with no leak. The centred product has mean given .
Proof. For a fixed , the map is a bijection of , so is uniform, and . Also . Write for the bits of , and , so that , because a bit of equal to 1 flips the bit of . Then
because the are independent with mean 0 and . The noise terms are independent of everything and centred, so they add nothing to the mean of the product.
With the population means in place of the group means, the second-order test is the first-order test of Theorems 1 and 2 applied to the product. For the fixed value the mean is , and over a uniform it is 0, because the mean of is 2. So by Theorem 2 the second-order test detects the masked device when grows. The code centres with the means of each group; those converge to the population means with probability 1, and the difference changes the product by terms that tend to 0, so the limits are the same.
References
- Gilbert Goodwill, Benjamin Jun, Josh Jaffe and Pankaj Rohatgi, A testing methodology for side-channel resistance validation, NIST Non-Invasive Attack Testing Workshop, 2011, Sections 2 and 3 (the fixed versus random and the specific tests, the Welch t statistic, the threshold 4.5, and the rule of two independent sets of traces)
- Tobias Schneider and Amir Moradi, Leakage assessment methodology, a clear roadmap for side-channel evaluations, CHES 2015, LNCS 9293, pages 495 to 513, the higher-order univariate and multivariate t-tests on centred and standardized moments, and the one-pass formulas for the moments
- Stefan Mangard, Elisabeth Oswald and Thomas Popp, Power Analysis Attacks, Springer, 2007, ISBN 978-0-387-30857-9, Chapter 4 (the statistics of power traces and the signal to noise ratio)
- Andrey Bogdanov and others, PRESENT, an ultra-lightweight block cipher, CHES 2007, LNCS 4727, the description of the substitution layer (the table of the 4-bit S-box used in the synthetic device of this page)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 89GDF4 Edit this page History Report an error Markdown