Part II Chapter 7
The neural network
Contents
Chapter 6 ended on a question: can a model trained on traces and S-box labels, never shown the masks, learn to combine the two samples where the shares leak? The paper this guide follows answers it with a neural network. This chapter builds that network from its smallest part: what one neuron computes, what a single neuron can find in a trace, how many numbers the full network holds, and where in the trace the trained one looks.
One neuron
A neuron takes some numbers in, multiplies each by its own weight, adds the results plus one more number, the bias, and passes the total through a small bend. Weights and bias are the knobs training adjusts. With three samples of a trace as inputs:
The bend is what makes depth useful. Without it, a chain of neurons feeding neurons would collapse into one weighted sum, however many layers it had. The paper’s networks use ELU, which passes positive sums through unchanged and squashes negative ones toward −1.
A neuron on the simulated chip
What can one neuron do with a trace? Take the simulated chip from Chapter 6, without a mask: 60 samples, six current pulses, and in the fourth pulse the height follows the Hamming weight of Sbox[p ⊕ k]. Connect one neuron to all 60 samples, one weight per sample.
The weights decide which samples the neuron listens to. Below the traces, the bars are those weights: drag on them to paint your own. The lower plot shows the neuron’s output for 500 traces, grouped by label, and the red line joins the group averages. If the output climbs with the label, the neuron has become a detector for the leak.
The presets mark the landmarks:
- Random weights are how every neuron starts before training. They listen a little everywhere, and the output barely follows the label (correlation 0.12).
- Sample 12 sits in a cycle of unrelated data: correlation near zero.
- Sample 32 is the SNR peak from Chapter 6: correlation 0.86.
- The whole 4th pulse, with weights shaped like the pulse, beats it: 0.94. Ten noisy readings of the same leak, added up, cancel part of each other’s noise, something no single sample can do.
Spreading weights over cycles 3 to 5 instead drops the correlation to about 0.5: the neighbouring cycles carry unrelated data, and the neuron adds it in as noise. The bias shifts the sum before the bend; pull it far down and ELU flattens the low groups.
Training is the search for weights like these, done automatically for every neuron at once. On the masked chip of Chapter 6 one neuron’s weighted sum isn’t enough: each share alone is independent of the label, so any weighted sum of the two samples has the same average for every label. Combining the shares takes the bends, and neurons that feed other neurons.
From neurons to a network
A layer is a bank of neurons working side by side on the same inputs; a network chains layers, the outputs of one feeding the inputs of the next.
The last layer has one neuron per class, one per value the label can take. Chapter 6 grouped traces by the Hamming weight of the S-box output, 9 possible values; the ASCADr network predicts the full value of Sbox[p ⊕ k] instead, so it has 256 classes, one per byte value. Each output neuron gives its class a raw score, and the last step, the softmax, turns the scores into probabilities: positive, between 0 and 1, totaling 1. With three classes:
A probability of 0.79 for one class means “given this trace, four chances in five that the S-box output has this value”. The whole network, then, is one function: from a trace to a probability for each of the 256 values. It never outputs a key byte.
How big is such a function? The ASCADr model has six layers of 100 neurons and the 256-neuron output layer:
276,456 numbers, and training is the process of setting them. Most sit in the first layer, which holds one weight for every pair of trace sample and neuron: each of its 100 neurons is a version of the neuron in the widget above, listening to all 2,000 samples of an ASCADr trace.
What the trained network outputs
A trained network is stored as a checkpoint, a file holding its 276,456 numbers. Load the ASCADr checkpoint after its full training, 100 epochs (passes over the profiling set; Chapter 8 covers how they go), and feed it one trace from the attack set:
On a single trace the network is usually wrong: its favourite for attack trace 0 is 0x9a, and the true value 0x91 ranks 81st of 256. The edge only shows on average. Over 10,000 attack traces the true value gets 0.0107, 2.7 times what a blind guess gives it; the same network after one epoch got 0.0039, no better than the blind guess. Small, but consistent, and consistency is what an attack needs: Chapter 3 scored every key guess across many traces, and the same accumulation turns a small, steady edge into the key byte.
Where the network looks
The first layer multiplies every sample by its own weight, once for each of its 100 neurons. Every sample is scaled to the same spread before entering the network, so the sizes of those weights can be compared directly: adding up the size of a sample’s 100 weights says how strongly the first layer listens to it. The figure divides that sum by the median over all samples, so 1 means a typical sample:
After one epoch the profile is flat: the weights are still close to their random start. After 100 epochs it rises to 9.8 times the median, and the 20 heaviest samples all fall between 857 and 1,538, the stretch that contains both SNR peaks from Chapter 6. The peaks themselves aren’t the heaviest samples: 970 ranks 84th and 1,526 ranks 31st. A heavy weight shows where the network listens, not what it does with what it hears. Whether it combines the two shares is a question for a later chapter.
The paper’s five models
The paper trains five models, one or two per dataset. This guide follows mlp_ascadr; the MLPs for ESHARD and CHES_CTF come back when a later chapter compares the three datasets, and the two convolutional networks (CNNs) are listed for completeness:
| Model family | Dataset | Leakage model | Input samples | Output classes |
|---|---|---|---|---|
mlp_ascadr | ASCADr | identity | 2,000 | 256 |
cnn_ascadr | ASCADr | identity | 2,000 | 256 |
mlp_eshard | ESHARD | Hamming weight | 1,400 | 9 |
cnn_eshard | ESHARD | Hamming weight | 1,400 | 9 |
mlp_ches_ctf | CHES_CTF | Hamming weight | 15,000 | 9 |
None of these models is ever shown a mask. During training each trace comes with its label, computed from the plaintext and the key, and on ASCADr that label is the unmasked S-box output. The chip never handled that value directly, only its two shares, so whatever the network learns about the shares, it has to find in the traces. The exact layer stacks live in the package (src/feature_emergence/profiling_and_attack.py) and are printed layer by layer in the companion notebook (opens in a new tab).
A whole network, small enough to see
The ASCADr network is too big to draw: its first layer alone has 200,000 connections. Here is the same kind of network shrunk to fit the simulated chip: 60 inputs, two layers of 8 neurons, and 9 outputs, one per Hamming weight. It was trained on 1,500 of the chip’s traces and is tested here on 500 it never saw. It names the right label for 69% of them; always guessing the most common label would get 27%.
Each line is a weight, blue where it adds and red where it subtracts. When a trace comes in, the lines light up layer by layer with what each weight carries for that trace, and the outputs grow with their probability. Training was asked to keep as few connections as it could, which keeps the picture readable, but nobody told it which ones to keep. Press play to send test traces through it one after another, drag to look from another angle, and notice where its input lines start.
Before training, those 276,456 numbers are random. How does showing the network labelled traces turn them into the profile above? Chapter 8 follows that process.