SHL Challenge Solution
The article details the solution provided by the winner of the SHL challenge.
Author: Chi-Hieu Nguyen, University of Technology Sydney, Australia.
Introduction
The objective of the challenge is to evaluate a bitwise shift left operation on an encrypted integer within the range . This operation can be defined as , where and are ciphertexts encrypting the input integer and the number of shifted bits , respectively, in their first slots. The value of is constrained between 0 and 16. The output ciphertext should encrypt the logical shifted value in its first slot, which is also a value in the range .
Approach
The proposed solution employs a two-way lookup table approach, using and as keys and as the lookup value. In this way, the computation in the encrypted domain is expressed as:
where
\begin{equation} EQ(a,b) =\begin{array}{rl} 1 & \text{if } a = b \ 0 & \text{if } a \neq b \end{array}
\end{equation}is the equality function. Notably, the term for is omitted since the computation naturally returns when , which is the correct result.
To optimize the number of comparisons, we leverage the SIMD property of the underlying BFV/BGV system by transforming the above equation to:
Here the function function replicates the value in the first slot of a ciphertext across its first slots, while the function sums the first slots of a ciphertext, placing the result in the first slot. These functions can be implemented using sequencial rotation and addition operations. Additionally, is the plaintext that encodes the vector containing all possible input values, and encodes the vector for all possible output values given the shifted amount .
By precompute the plaintexts for all possible values, the remaining task is to implement the equality comparison function between a ciphertext and a plaintext value. Due to different ranges of and , two distinct equality comparison methods are employed for optimal performance. Specifically, Fermat's Little Theorem (FLT) is used for comparing values in and (referred to as the outer comparison), while the Lagrange polynomial method is used for comparing and (referred to as the inner comparison).
Outer Equality Comparison
The FLT method is utilized to evaluate . This function returns a ciphertext where the -th slot is one if equals and zero otherwise. This approach is similar to the FHERMA's Lookup Table solution [1]. However, given the input range , the plaintext modulus must be set higher, specifically , to encode the values correctly. To begin with, we subtract from . The resulting ciphertext contains exactly one zero at the -th slot and non-zero values in other slots. We then compute the exponentiation , which consumes 20 multiplication levels in total. The final comparison results is obtained by evaluating .
Inner Equality Comparison
For a fixed value of (), the function can be represented by a Lagrange polynomial with integer coefficients , calculated over the interpolation nodes and the corresponding values at and elsewhere. The coefficients of are computed by using modular multiplication and division in the plaintext modulus . As the degree of is 17, its evaluation consumes 5 multiplication levels using the Paterson-Stockmeyer algorithm. We modified the EvalPolyPS from the OpenFHE library to accommodate integer coefficient polynomials.
However, the above approach requires 16 executions of the Paterson-Stockmeyer algorithm, which can be time-consuming. This can be reduced to a single execution in a SIMD fashion. Similar to the outer comparison, we calculate the subtraction , where encodes the vector of possible values. We then compute a Lagrange polynomial at the interpolation nodes , where the polynomial value is at zero and elsewhere. In this way, the transformed ciphertext is equivalent to , having a at the -th slot and elsewhere. Finally, can be derived from for different values by by applying a mask to extract the -th slot and duplicating it across the first slots. This requires only one multiplication and some rotations, without further polynomial evaluation.
Extra Level Reduction
After evaluating and the summation of inner comparisons
, these results must be multiplied together, which incurs an additional level of computation. To avoid this, the multiplication order can be rearranged as
, thus maintaining the total computation depth at 20.
References
[1] Lookup Table Challenge https://fherma.io/content/66d9c84af6ea18c58bf5e97a
CITING THIS WORK
This write-up documents a component of the FHERMA library. If it informs your work, cite the two papers below rather than the article URL.
- 01FHERMA Cookbook: FHE Components for Privacy-Preserving ApplicationsJanis Adamek, Aikata Aikata, Ahmad Al Badawi, Andreea Alexandru, Armen Arakelov, Philipp Binfet, Victor Correa, Jules Dumezy, Sergey Gomenyuk, Valentina Kononova, Dmitrii Lekomtsev, Vivian Maloney, Chi-Hieu Nguyen, Yuriy Polyakov, Daria Pianykh, Hayim Shaul, Moritz Schulze Darup, Dieter Teichrib, Dmitry Tronin, Gurgen ArakelovCryptology ePrint Archive, Paper 2025/1302 · doi:10.1145/3733811.3767313
- 02FHERMA: Building the Open-Source FHE Components Library for Practical UseGurgen Arakelov, Nikita Kaskov, Daria Pianykh, Yuriy PolyakovCryptology ePrint Archive, Paper 2024/612
