This is a post in COSIC’s Guide to Crypto! In this series, researchers from COSIC will be informally writing about different concepts in cryptography. Our aim is to introduce the reader to these concepts with explanations, illustrations and examples; so let’s get started!
Alice’s quest for truly random bits
Let’s assume that Alice would like to perform cryptographic tasks. For a vast majority of them, she would need random bits, for example, to produce nonces, padding, challenges, initialization vectors or secret and public keys. Further, due to Kerckhoff’s principle she must assume that an adversary knows everything about her cryptographic algorithms, and their security solely relies on the uniformity and unpredictability of the random bits. But where would these bits come from? In an ideal world, Alice would have access to an infinite resource of “divine” truly random bits, that are uniform, unpredictable and can be generated as fast as needed – a perfect true random number generator. Unfortunately, Alice doesn’t live in an ideal world – randomness sources that are available in nature are not perfect, and they tend to output bits that are biased, predictable and/or not independent. Thus, she must find a way to transform the output of these non-perfect randomness sources to (almost) uniform unpredictable bits with which she can do crypto. She would also like to use a simple, efficient and fast method to obtain as many uniform bits as possible. In this blog post, we will give an overview of several methods that are suitable for this task.

Fig. 1. A way in which Alice does not get her random bits.
Shannon entropy, min-entropy and distance to the uniform distribution
Before we dive into this topic, let’s present several concepts that we are going to need for the rest of this post.
Mathematically, we can think of a randomness source that produces \(n\)-bit strings as a random variable \(X\) over \(n\)-bits. We also want a tool to measure how unpredictable the randomness source is. That’s why we introduce entropy as a measure of unpredictability. A perfectly uniform and unpredictable \(n\)-bit string would have, what we call, full entropy. There are many different entropies, the most well-known being the Shannon entropy and the min-entropy. Informally, the Shannon entropy \(\require{action} \toggle{\underline{H_{Shannon}}}{H_{Shannon}\left(X\right)=-\sum_{x\ =\ 0}^{2^n-1} P\left[X = x\right]\cdot \log_2 \left(P\left[X = x\right]\right) }\endtoggle\) quantifies the average unpredictability of a random variable. However, in cryptography, a more conservative measure is needed to quantify how likely a potential adversary is able to guess the output of the randomness source on the first try. That is exactly what the min-entropy \(\require{action} \toggle{\underline{H_{min}}}{H_{min}=-\log_2 \left(max_{x=0…2^n-1} \left(P\left[X = x\right]\right)\right)}\endtoggle\) tells us! To illustrate why min-entropy is the preferred entropy in cryptography, let’s consider the following example. Suppose that a flawed randomness source produces \(256\)-bit samples in such a way that 50% of the time it outputs a fixed but random-looking value, and the other 50% of the time any other \(256\)-bit sample. The probability distribution of this source is shown in the figure below.

Fig. 2. The probability distribution of a flawed randomness source.
Let’s also suppose that Alice wants to use this source to derive a \(128\)-bit full entropy key. How many samples from this source does she need? She might first want to calculate how much Shannon entropy is produced by this source. She calculates the following:
$$P_{max}=0.5$$ $$P_{rest}\ = \frac{0.5}{2^{256}-1}$$ $$ H_{Shannon}\left(X\right)=-P_{max}\cdot \log_2 \left(P_{max}\right)-\left(2^{256}-1\right)\cdot P_{rest}\cdot \log_2 \left(P_{rest}\right)$$ $$ H_{Shannon}(X)\approx129\ bits$$
Alice may conclude that she needs only one sample from such a source to get a full entropy \(128\)-bit key. However, on average, every second key generated from this source is completely identical! Even worse, an attacker who knows about the flaw in the randomness source has a 50% chance of guessing the key on the first try. On the other hand, if she calculates the min-entropy instead:
$$H_{min}(X)\ =\ -\log_2 \left(P_{max}\right)\ =\ 1\ bit$$
The min-entropy of only one bit gives her a better picture of the quality of the randomness source and the attacker’s success in guessing the random value. Because of its conservativeness, in this post, we will use min-entropy as a measure of unpredictability.
Another important notion is that of the distance to the uniform distribution. It is a measure of how different the probability distribution associated with the random variable \(X\) over \(n\)-bits is from the uniform one \(U_n\), and it is defined as:
$$d(X,\ U_n)=\frac{1}{2} \cdot\sum_{x\ =\ 0}^{2^n-1}\left|P\left[X\ =\ x\right]-P\left[U_n=x\right]\right| = \sum_{ x \: s.t. \: P\left[X=x\right]\ge P\left[U_n=x\right]} \left(P\left[X\ =\ x\right]-P\left[U_n=x\right]\right)$$
When \(d(X,\ U_n)\le\varepsilon\), it is said that \(X\) is \(\varepsilon\)-close to the uniform distribution. As an illustration, Fig. 3 shows the probability distribution of a \(2\)-bit random variable \(X\) and its distance from the uniform distribution. For the one-bit random variables, the distance from the uniform distribution \(\varepsilon\) is equivalent to the term bias.

Fig. 3. Distance from the uniform distribution for the 2-bit random variable.

Fig. 4. Distance from the uniform distribution (bias) for the 1-bit random variable.
Deterministic extractors
The deterministic extractor is an umbrella term for all the algorithms that transform bits of a non-perfect randomness source to bits that have close to the uniform distribution. A bit more formally, an \(\varepsilon\)-deterministic extractor is a function \(Ext(X)\) that transforms \(n\)-bit strings of all randomness sources \(X\) from some class \(C\) to \(m\)-bit strings that are ε-close to the uniform distribution. A class of randomness sources is a set of all randomness sources that, besides the known amount of entropy, have some common, special structure, as we will see in the following sections.
Von Neumann extractor
Let’s start with a very simple class of randomness sources – IID-bit sources, where IID stands for “independent and identically distributed”. An IID-bit source \(X\) produces \(1\)-bit outputs \(X_i\) such that each output is independent of all other outputs and each output is equally biased, that is \(P\left[X_i=1\right]\ =\ p_1\) for every \(i\) and constant (possibly unknown) value \(0<p_1<1\). How can Alice convert such a randomness source to a source of independent and completely unbiased bits? Luckily for her, already in the 1950s, the famous von Neumann proposed an elegant algorithm for these kinds of sources: break all the bits in non-overlapping pairs, then for each pair whose first and second bit are different output only the first bit and do not output anything if both bits in the pair are the same. This procedure is illustrated in Fig. 5. Because it outputs completely unbiased and independent bits, the von Neumann extractor is a \(0\)-deterministic extractor. The success of this procedure lies in the fact that for the two independent bits \(X_i\) and \(X_{i+1}\) that make up the pair, the following relations hold: \(P\left[X_i=1\ \land\ X_{i+1}=0\right]\ =p_1\cdot\left(1-p_1\right)\ =\ P\left[X_i=0\ \land\ X_{i+1}=1\right]\ =\ \left(1-p_1\right)\cdot p_1\ \)

Fig. 5. Von Neumann extraction.
However, this procedure produces bits with a variable rate where one unbiased random bit is produced on average after \(1/\left(2\cdot p_1\cdot\left(1-p_1\right)\right)\) pairs of the input bit, implying that the output times of the von Neumann extractor follow a geometric probability distribution. The throughput of the processed bit sequence with full entropy cannot exceed \(1/4\) of the throughput of the unprocessed bits. In other words, \(3/4\) of the input entropy will always be lost during the extraction. Further, even if the randomness source produces only slightly biased IID bits, but Alice is unlucky, she could be waiting for very long periods of time between two unbiased output bits, possibly infinitely long!
Let’s now assume that she unwisely applies the von Neumann extractor on a source that produces correlated \(1\)-bit outputs. For example, if the bits exhibit a high negative correlation, then the source is very likely to produce many \(01\) or \(10\) pairs. In that case, the von Neumann extractor applied to this source would produce long sequences of \(0\)s or \(1\)s, and the output sequence would be heavily biased, defeating the purpose of the extractor.
Can Alice do better? Can she increase the throughput of the processed bits? Can the entropy per bit be increased if the randomness source produces bits whose biases are not identical? The answer to all these questions is: yes!
XOR extractor
A slightly more interesting and broader class of randomness sources is the class of independent-bit sources. An independent-bit source \(X\) produces \(1\)-bit outputs \(X_i\) such that each output is independent of all other outputs, but the biases could be different for different bits, that is \(P\left[X_i=1\right]\ =\ p_i\) for every \(i\) and \(0<p_{bound}\le p_i\le1-p_{bound}\) for some constant \(p_{bound}\). To decrease the bias, Alice can apply the XOR extractor. This simple algorithm consists of splitting the input bits into non-overlapping groups of at least two bits and taking the parity of all bits in the group, which is equivalent to applying the XOR function. The resulting output bit will have a smaller bias than any of the input bits in the group. To informally show this, let’s consider a \(2\)-bit XOR of \(X_1\) and \(X_2\) with \(P\left[X_1=1\right]\ =\ p_1\) and \(P\left[X_2=1\right]\ =\ p_2\) :
$$P\left[X_1\ \bigoplus\ X_2=1\right]\ =\ p_1\cdot\left(1-\ p_2\right)+p_2\cdot\left(1-\ p_1\right)\ =\ \frac{1}{2}\ -\ 2\cdot \left(p_1-\frac{1}{2}\right)\cdot \left(p_2-\frac{1}{2}\right)$$ From this equation, it is clear that: \(\left|P\left[X_1\ \bigoplus\ X_2=1\right]\ -\ \frac{1}{2}\right|\le min\left(\left|p_1-\frac{1}{2}\right|,\left|p_2-\frac{1}{2}\right|\right)\), so taking the parity of two independent bits always reduces the bias. Unlike the von Neumann extractor, this algorithm has a constant bit generation rate and it is a \( \left|2\cdot \left(p_1-\frac{1}{2}\right)\cdot \left(p_2-\frac{1}{2}\right)\right|\) – deterministic extractor. It is important to mention that by applying this kind of extractor, Alice can never completely get rid of the bias and obtain bits that have full entropy but can instead increase the entropy per bit as much as necessary by increasing the number of bits whose parity will be taken.

Fig. 6. The 2-bit XOR extraction.
Let’s assume that Alice again unwisely applies the XOR extractor on a source that produces negatively correlated \(1\)-bit outputs. As we saw in the previous section, this implies a higher probability of \(01\) and \(10\) pairs, and a lower probability of \(00\) and \(11\) pairs. The output of the XOR extractor in that case would be biased towards \(1\) and applying the extractor would become pointless.
So, what can Alice do if the randomness source produces bits that are not only biased but also show some degree of mutual dependency? If the randomness source can be modeled by finite state Markov chains, then she could use Blum’s algorithm to obtain completely independent and unbiased bits – bits with full entropy.
Blum extractor
Let’s suppose that the randomness source is an \(n\)-state Markov chain. Each state \(S_i\) has 2 exit paths – labeled as \(p_i\) and \(q_i\), where \(p_i\) and \(q_i\) at the same time represent the probabilities with which each exit is taken (i.e., \(p_i+q_i\ =\ 1\)). Each exit path ends in the state it originated from or in some other state of the Markov chain. Whenever a state is visited, a \(1\) or \(0\) is output. Thus, the probability of each output depends on the states that were previously visited. An example of such a source is depicted in Fig. 7.

Fig. 7. Markov chain as a randomness source.
To extract unbiased and independent bits from this source, Alice can apply Blum’s algorithm: Start in the state \(S_0\) and keep track of the current state as the bits are being output. Whenever the current state is entered for the \((2i+1)^{th}\) time, look at its \((2i-1)^{th}\) and \(2i^{th}\) exits and apply the von Neumann extractor on them. In other words, if \((2i-1)^{th}\) and \(2i^{th}\) exits were different, output \((2i-1)^{th}\) exit bit, and if the two exits were the same, do not output anything. To illustrate this, in the figure below, we can see how this algorithm would work on the source from Fig. 7. Because it completely removes both bias and dependencies, this extractor is a \(0\)-deterministic extractor and its efficiency is the same as von Neumann’s – \(1/4\) of the randomness source’s total entropy is preserved in the output.

Fig. 8. Blum extraction applied on the source in Fig. 7.
Conclusion: General deterministic extractors?
We saw that deterministic extractors exist for some classes of randomness sources that have special structure, so we might ask ourselves if there are deterministic extractors that can be applied to the most general class of sources – the ones that can be described only by the amount of min-entropy that they contain. Unfortunately, the answer is negative. It is even impossible to get one random bit from a deterministic extractor for this most general class of sources. To show this, suppose the opposite: that we have a function \(Ext(X)\) that transforms \(n\)-bit outputs of all randomness sources \(X\) with min-entropy of at least \(n-1\) bits into one uniform bit. This function divides all inputs into one set of all the \(n\)-bit strings that are transformed to a \(0\) – \({Ext(0)}^{-1}\), and another set whose \(n\)-bit strings are transformed to \(1\) – \({Ext(1)}^{-1}\), implying that one of these two sets must have size \(2^{n-1}\) or larger. A randomness source \(X\) that is uniform on the larger set and does not output any value from the smaller set will have min-entropy of at least \(n-1\) bits. However, when the extractor is applied to this randomness source, it will always output the same constant value, which is in contradiction with our initial assumption.
Therefore, there is no “silver bullet” extractor that Alice can use for all possible randomness sources, even if she knows the amount of min-entropy present in the source. To get around the impossibility result, one solution would be to combine multiple independent sources to extract one string of almost uniform bits. Another solution is to add a short public uniform seed as a catalyst to extract the randomness – but that is a topic for another blog post.
Further reading and references
- Robert B. Davies: Exclusive OR (XOR) and hardware random number generators, http://www.robertnz.net/pdf/xor2.pdf, 2002.
- Manuel Blum: Independent Unbiased Coin Flips From a Correlated Biased Source: a Finite State Markov Chain, Combinatorica 6 (1986), no. 2, 97–108.
- David Johnston: Random Number Generators – Principles and Practices, De|G PRESS, 2018.
- Salil Vadhan: Pseudorandomness, Foundations and Trends in Theoretical Computer Science: Vol. 7: No. 1–3, pp 1-336, 2012.


