Constant-time discrete Gaussian sampling for lattice-based cryptography

Since the inception of Lattice based cryptography, discrete Gaussian sampling has been an integral part of it. Recently there are some efforts to replace discrete Gaussian sampling with centered Binomial distributions. These methods work fine for public-key encryption or key-exchange schemes, but for digital signature and trapdoor constructions, discrete Gaussian distributed noise is necessary to prevent any leakage of the secret (basis of the lattice) to the adversary. There are several ways for generating samples from a known and fixed discrete Gaussian distribution. In this blog we will describe the Knuth-Yao random walk algorithm for this purpose and we will show why it is important to execute the random walk algorithm in constant-time and how this can be achieved.

In 1976 Donald Knuth and Andrew Yao proposed a random walk algorithm that can sample from any distribution given the probabilities of the sample points. From the known sample-probability it is possible to generate a binary tree, known as the discrete distribution generating (DDG) tree. A DDG tree consists of two types of nodes: intermediate nodes and terminal nodes. Every intermediate node has two children in the next level. A terminal node contains a value of a sample point. Here is an example. Consider a small sample space S = {0,1,2} consisting of three sample points with probabilities {0.01110, 0.01101, 0.00101}. The probability values after the decimal points can be written in the form of a matrix Pmat.

For the given probability matrix, the DDG tree is shown here. The column and row indexes of the probability matrix start with 0. The DDG tree consists of two types of nodes: intermediate nodes represented using the letter I, and terminal node containing a sample point.
The i-th level of the DDG tree is associated with the i-th column of the probability matrix: the number of terminal nodes in the i-th level of the DDG tree is equal to the number of 1s in the i-th column of the probability matrix, and the terminal nodes contain the row numbers which have a 1. For e.g., level-1 of the DDG tree in the figure is associated with column number 1 of the probability matrix. Since the column has two 1s, the level contains two terminal nodes. The 1s are present in the row numbers 0 and 1 of the column, so the terminal nodes contain the values 0 and 1.

During a sampling operation, a random walk is performed along the DDG tree starting from its root. For every jump to a next level, a random bit is generated and depending on the bit either left or right branch is selected. When the random walk hits a terminal node, the random walk stops and the value of the node is output as the sample value. An example random walk is shown in the following figure.

To generate n samples from the distribution, the random walk is performed n times. It might appear that one needs to store the DDG tree to perform the random walk. But, in practice, the levels of the DDG tree are generated on-the-fly from the probability matrix. Thus, storing the probability matrix is enough! This also requires less memory. Let us see how the i-th level of the tree is constructed on-the-fly from the i-th column of probability matrix.

Let a random walk has visited the k-th intermediate node of the (i-1)-th level and there are d intermediate nodes in the right side of the k-th node. Each intermediate node in the (i-1)-th level will have two children in the i-th level. Let the random walk visits the right child of the k-th node. As seen from the figure, there will be 2d nodes in the right side of the visited node in the i-th level. But we do not know if these nodes are intermediate nodes or terminal nodes. To discover these nodes, we read the i-th column of the probability matrix from the bottom. Whenever a `1’ is found in the column, a terminal node is discovered from the right side, and the row number for which the `1’ was found, is assigned to the value of the discovered node. Each discovery decrements the value of d.

In this way if there are enough 1s in the column, then at some point the visited node will be discovered as a terminal node. This event happens when d becomes negative for the first time. If there are not enough 1s in the column, then d remains positive after scanning the entire column of the probability matrix. In that case, the random walk will continue to the next level of the DDG tree. Based on this observation, we designed a simple lightweight algorithm to perform the random walk operation.

The algorithm uses small counters for distance d, row-number and column-number. The probability bits are stored in the 2-dimensional matrix P and a bit is accessed using the row and column indexes. Simplicity of the algorithm enables its implementation on resource-constrained platforms. For example, for a standard deviation = 3.3, hardware implementation of the algorithm requires only 44 slices and on average 1.5 cycles per sample in a Xilinx FPGA. Software implementation in a resource-constrained microcontroller Cortex-M4F requires on average 28.5 cycles per sample.

Timing analysis and Knuth-Yao random walk:
As can be seen from the figure of the DDG tree, different terminal nodes are present at different distances from the root of the tree. Thus, the duration of a random walk along the tree varies depending on the input random bit-string. In the following figure, electromagnetic field intensity from a hardware implementation of a Knuth-Yao sampler is shown for two sampling operations.

By observing the EM field intensity, a side-channel attacker can know the number of cycles for a sampling operation and thus can guess the value of the sample. This is dangerous! The sample values are used to hide confidential information. Thus, knowing the sample values, attacker can extract the confidential information. Hence it important to compute the samples in constant-time. In the following, we describe a method to achieve fully constant-time execution.

The random walk operation can be viewed as a mapping from random strings to sample values. Let, a random string r={r0, r1, … , rn-1} of length n gets mapped into a sample value s={s0, s1, … , sm-1} of length m bits. The mapping is many to one. Observing the mapping, it is possible to write down the Boolean expressions for calculating the bits of the sample.

From a random string r, the sample can be computed by evaluating the Boolean expressions on the random bits. To generate the samples in constant time, these evaluations should be performed in constant-time. But, how to evaluate a Boolean equation in constant-time?

Consider a toy Boolean equation

Actual equations for a real sampler would be much longer. Nevertheless, the toy example would be sufficient to explain the idea. If the equation is evaluated on a hypothetical 1-bit machine, then a smart compiler could minimize the computation time by ignoring the computation of (r1 ∨ r2) when ever r0=0. Hence there would be variations in time.

Now consider a bit-sliced implementation on a 64-bit machine. Let three random 64-bit integers be R0, R1, and R2. Using bitwise operations (always work on 64-bit operands) we can compute 64 s0 bits in parallel in a 64-bit integer S0.

Note that the computation of (R1 ∨ R2) becomes unnecessary only when R0 is zero. But, this happens with probability 2-64 on a 64-bit platform. So, a smart compiler will never perform data dependent optimization for bitwise operations. Actually, no real-world compiler would insert data-dependent branches inside bitwise operations as it would be counterproductive. Furthermore, a code developer can even write the code of the sampler in assembly to ensure no data-dependent branching by compiler.

In this way, we used bit-slicing approach to implement a fully constant-time Knuth-Yao Gaussian sampler. Beside constant-time execution, the bit-slicing approach increases throughput by means of parallel computation of 64 bits in a batch.

σ ≈ 6.15543

Precision = 128 bit

Fully constant-time sampling takes 26K cycles to compute 64 samples in a batch

Further readings:

[1] D. E. Knuth and A. C. Yao. The Complexity of Non-Uniform Random Number Generation. Algorithms and Complexity, pages 357–428, 1976.

[2] S. Sinha Roy, F. Vercauteren and I. Verbauwhede. High Precision Discrete Gaussian Sampling on FPGAs, Selected Areas in Cryptography — SAC 2013 pp 383-401.

[3] S. Sinha Roy, O. Reparaz, F. Vercauteren, and I. Verbauwhede. Compact and Side Channel Resistant Discrete Gaussian Sampling, https://eprint.iacr.org/2014/591.pdf

[4] A. Karmakar, S. Sinha Roy, O. Reparaz, F. Vercauteren, and I. Verbauwhede. Constant-time Discrete Gaussian Sampling, (accepted in IEEE Transactions on Computers), https://www.esat.kuleuven.be/cosic/publications/article-2822.pdf

Thanks to Sujoy Sinha Roy for writing this blog post!