Part II Chapter 7

The neural network

Contents
  1. One neuron
  2. A neuron on the simulated chip
  3. From neurons to a network
  4. What the trained network outputs
  5. Where the network looks
  6. The paper’s five models
  7. A whole network, small enough to see

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:

One neuron: weighted sum, then a bendsample xweight ww · x4.0× 0.8+3.20−1.0× 0.2−0.200.5× −0.1−0.05bias++0.05weighted sum3.00the bend: ELUsum3.00 → 3.00−2 → −0.86
One neuron with three inputs. Left: each input times its weight; the bars are the contributions, which add up with the bias to 3.00. Right: the ELU bend, which leaves positive sums alone and squashes negative ones toward −1.

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.

weights
Traces and the neuron's weightscycle 1cycle 2cycle 3cycle 4cycle 5cycle 6power+1−1weight0102030405059time sample (drag on the weights to paint them) Neuron output, grouped by label02012345678neuron outputlabel: Hamming weight of Sbox[p ⊕ k]
correlation
0.12

output = ELU(w₀·x₀ + w₁·x₁ + … + w₅₉·x₅₉ + b), computed for all 2,000 traces; the dots show 500 of them.

These are random weights, the way a network starts before training. The output barely follows the label (correlation 0.12). Drag on the weights to put them where the chip leaks, or try a preset.

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:

The softmax turns scores into probabilitiesraw scores2.0class A0.5class B−1.0class Csoftmaxe^score, then ÷ totalprobabilities (sum 1)0.79class A0.18class B0.04class C
Softmax on three raw scores. A bigger score gets a bigger share, and the shares always add up to 1.

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:

Where the 276,456 numbers sit2,000samples100100100100100100256scoressoftmax256 probssix layers of 100 neuronsfirst layer: 200,100 = 2,000 × 100 weights + 100 biaseshidden: 50,500 = 5 × (100 × 100 + 100)output: 25,856 = 100 × 256 + 256total: 276,456 numbers, 72% of them in the first layer
The ASCADr MLP from input to output, and where its 276,456 numbers sit. The bar below splits them by 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:

What the network outputsone attack trace: a probability for each of the 256 values00.10.20.30x9a: 0.25, the network’s best guesstrue value 0x91: ≈ 0, rank 81 of 256064128192255average probability of the true value, 10,000 attack tracesafter 1 epoch0.0039after 100 epochs0.0107blind guess: 1/256 = 0.0039
The trained ASCADr network on the attack set. Top: its 256 probabilities for trace 0. Bottom: the probability it gives the true value, averaged over 10,000 attack traces, after 1 and after 100 epochs, next to a blind guess of 1/256.

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:

Where the trained network looksfirst-layer weight per sample (× the median sample)1×5×10×the 20 heaviest samples: 857 to 1,538after 1 epoch: flatafter 100 epochsSNR from Chapter 624mask, sample 970masked share, sample 1,52602505007501,0001,2501,5001,7502,000time sample
Top: how strongly the first layer of the ASCADr network weights each of the 2,000 samples, relative to the median sample, after 1 and after 100 epochs. Bottom: the SNR of the mask and of the masked share from Chapter 6, on the same time axis.

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 familyDatasetLeakage modelInput samplesOutput classes
mlp_ascadrASCADridentity2,000256
cnn_ascadrASCADridentity2,000256
mlp_eshardESHARDHamming weight1,4009
cnn_eshardESHARDHamming weight1,4009
mlp_ches_ctfCHES_CTFHamming weight15,0009

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.

a test trace with label

Test trace 1500, label 4. The network's top guess is 4, with probability 0.53. Press play to send traces through one after another; drag to change the angle.

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.