# Piling-up lemma

In cryptanalysis, the piling-up lemma is a principle used in linear cryptanalysis to construct linear approximations to the action of block ciphers. It was introduced by Mitsuru Matsui (1993) as an analytical tool for linear cryptanalysis.[1] The lemma states that the bias (deviation of the expected value from 1/2) of a linear Boolean function (XOR-clause) of independent binary random variables is related to the product of the input biases:[2]

${\displaystyle \epsilon (X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n})=2^{n-1}\prod _{i=1}^{n}\epsilon (X_{i})}$

or

${\displaystyle I(X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n})=\prod _{i=1}^{n}I(X_{i})}$

where ${\displaystyle \epsilon \in [-{\tfrac {1}{2}},{\tfrac {1}{2}}]}$ is the bias (towards zero[3]) and ${\displaystyle I\in [-1,1]}$ the imbalance:[4][5]

${\displaystyle \epsilon (X)=P(X=0)-{\frac {1}{2}}}$
${\displaystyle I(X)=P(X=0)-P(X=1)=2\epsilon (X)}$.

Conversely, if the lemma does not hold, then the input variables are not independent.[6]

## Interpretation

The lemma implies that XOR-ing independent binary variables always reduces the bias (or at least does not increase it); moreover, the output is unbiased if and only if there is at least one unbiased input variable.

Note that for two variables the quantity ${\displaystyle I(X\oplus Y)}$ is a correlation measure of ${\displaystyle X}$ and ${\displaystyle Y}$, equal to ${\displaystyle P(X=Y)-P(X\neq Y)}$; ${\displaystyle I(X)}$ can be interpreted as the correlation of ${\displaystyle X}$ with ${\displaystyle 0}$.

## Expected value formulation

The piling-up lemma can be expressed more naturally when the random variables take values in ${\displaystyle \{-1,1\}}$. If we introduce variables ${\displaystyle \chi _{i}=1-2X_{i}=(-1)^{X_{i}}}$ (mapping 0 to 1 and 1 to -1) then, by inspection, the XOR-operation transforms to a product:

${\displaystyle \chi _{1}\chi _{2}\cdots \chi _{n}=1-2(X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n})=(-1)^{X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n}}}$

and since the expected values are the imbalances, ${\displaystyle E(\chi _{i})=I(X_{i})}$, the lemma now states:

${\displaystyle E\left(\prod _{i=1}^{n}\chi _{i}\right)=\prod _{i=1}^{n}E(\chi _{i})}$

For dependent variables the above formulation gains a (positive or negative) covariance term, thus the lemma does not hold. In fact, since two Bernoulli variables are independent if and only if they are uncorrelated (i.e. have zero covariance; see uncorrelatedness), we have the converse of the piling up lemma: if it does not hold, the variables are not independent (uncorrelated).

## Boolean derivation

The piling-up lemma allows the cryptanalyst to determine the probability that the equality:

${\displaystyle X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n}=0}$

holds, where the X's are binary variables (that is, bits: either 0 or 1).

Let P(A) denote "the probability that A is true". If it equals one, A is certain to happen, and if it equals zero, A cannot happen. First of all, we consider the piling-up lemma for two binary variables, where ${\displaystyle P(X_{1}=0)=p_{1}}$ and ${\displaystyle P(X_{2}=0)=p_{2}}$.

Now, we consider:

${\displaystyle P(X_{1}\oplus X_{2}=0)}$

Due to the properties of the xor operation, this is equivalent to

${\displaystyle P(X_{1}=X_{2})}$

X1 = X2 = 0 and X1 = X2 = 1 are mutually exclusive events, so we can say

${\displaystyle P(X_{1}=X_{2})=P(X_{1}=X_{2}=0)+P(X_{1}=X_{2}=1)=P(X_{1}=0,X_{2}=0)+P(X_{1}=1,X_{2}=1)}$

Now, we must make the central assumption of the piling-up lemma: the binary variables we are dealing with are independent; that is, the state of one has no effect on the state of any of the others. Thus we can expand the probability function as follows:

 ${\displaystyle P(X_{1}\oplus X_{2}=0)}$ ${\displaystyle =P(X_{1}=0)P(X_{2}=0)+P(X_{1}=1)P(X_{2}=1)}$ ${\displaystyle =p_{1}p_{2}+(1-p_{1})(1-p_{2})}$ ${\displaystyle =p_{1}p_{2}+(1-p_{1}-p_{2}+p_{1}p_{2})}$ ${\displaystyle =2p_{1}p_{2}-p_{1}-p_{2}+1}$

Now we express the probabilities p1 and p2 as ½ + ε1 and ½ + ε2, where the ε's are the probability biases — the amount the probability deviates from ½.

 ${\displaystyle P(X_{1}\oplus X_{2}=0)}$ ${\displaystyle =2(1/2+\epsilon _{1})(1/2+\epsilon _{2})-(1/2+\epsilon _{1})-(1/2+\epsilon _{2})+1}$ ${\displaystyle =1/2+\epsilon _{1}+\epsilon _{2}+2\epsilon _{1}\epsilon _{2}-1/2-\epsilon _{1}-1/2-\epsilon _{2}+1}$ ${\displaystyle =1/2+2\epsilon _{1}\epsilon _{2}}$

Thus the probability bias ε1,2 for the XOR sum above is 2ε1ε2.

This formula can be extended to more X's as follows:

${\displaystyle P(X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n}=0)=1/2+2^{n-1}\prod _{i=1}^{n}\epsilon _{i}}$

Note that if any of the ε's is zero; that is, one of the binary variables is unbiased, the entire probability function will be unbiased — equal to ½.

A related slightly different definition of the bias is ${\displaystyle \epsilon _{i}=P(X_{i}=1)-P(X_{i}=0),}$ in fact minus two times the previous value. The advantage is that now with

${\displaystyle \varepsilon _{total}=P(X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n}=1)-P(X_{1}\oplus X_{2}\oplus \cdots \oplus X_{n}=0)}$

we have

${\displaystyle \varepsilon _{total}=(-1)^{n+1}\prod _{i=1}^{n}\varepsilon _{i},}$

adding random variables amounts to multiplying their (2nd definition) biases.

## Practice

In practice, the Xs are approximations to the S-boxes (substitution components) of block ciphers. Typically, X values are inputs to the S-box and Y values are the corresponding outputs. By simply looking at the S-boxes, the cryptanalyst can tell what the probability biases are. The trick is to find combinations of input and output values that have probabilities of zero or one. The closer the approximation is to zero or one, the more helpful the approximation is in linear cryptanalysis.

However, in practice, the binary variables are not independent, as is assumed in the derivation of the piling-up lemma. This consideration has to be kept in mind when applying the lemma; it is not an automatic cryptanalysis formula.