FHERMA
IMPLEMENTATION04 SEPT 2024 · 3 MINKERNEL · encrypted-lookup

Encrypted Lookup Table

The article details the solution provided by the winner of the FHERMA Lookup table challenge.

JDJules DumezyContributor

Encrypted Lookup Table

The article details the solution provided by the winner of the Lookup Table challenge

Author: Jules Dumezy, MSc Student at the Ecole Centrale de Lille.

Introduction

A lookup table, specifically A[i] for a list A and an index i, is a fundamental concept in computer science and plays a vital role in efficient algorithm design and implementation. At their core, LUTs provide a mechanism for efficiently retrieving precomputed values based on an index, thereby allowing for constant-time access to data, which is crucial for performance-critical applications.

Aside from the obvious use of LUT to retrieve data and use it in calculation, it can also be used to evaluate an arbitrary function f. By precomputing its outputs and storing them in an encrypted vector, f's computation can be reduced to a simple lookup operation, which can be far less resource-intensive. This capability can be essential in applications such as encrypted neural networks, and allows to easily evaluate non-smooth functions.

The challenge

We are working with BFV using the plaintext modulus 65537. Given an encrypted vector A=[x0,,xn1]A = [x_0,\dots,x_{n-1}] of size 2048 and an encrypted index i[0..n1]i\in [0..n-1], we want to extract xix_i. Specifically, we want to obtain an encrypted vector with xix_i in the first slot.

Although the challenge specifies that the values xix_i are bounded between 0 and 255, the proposed solution does not rely on this restriction. Additionally, the solution can be easily adapted for larger vectors (with more than 2048 values) or for larger plaintext moduli.

The algorithm

The algorithm can be divided into two main steps :

  1. Creating a mask vector with a 11 at the ii-th element and zeros everywhere else
  2. Extracting the value from AA and placing it in the first slot

Step 1

Since we use packing, the encrypted index vector has the following form: [i,0,0,0,,0][i, 0, 0, 0,\dots, 0]. We start by rotating this vector by -2047, resulting in a vector with 2047 zeros and the index ii at the 2048th position (index 2047).

This approach is used because rotations are generally faster with positive values, and it also minimizes the number of rotation keys required—we only need 12 rotation keys in total by reusing them, instead of 22.

By applying 11 rotations and additions (using rotation keys 1, 2, 4, 8, ..., 1024), we obtain a new encrypted vector c1c_1 with ii in the first 20482048 slots [i,i,i,,i,0,,0][i, i, i,\dots, i, 0, \dots, 0].

Next we create a packed plaintext for the vector [0,1,2,3,,2045,2046,2047][0, -1, -2, -3, \dots, -2045, -2046, -2047]. We then add this plaintext to the previous ciphertext c1c_1 to produce a new ciphertext c2c_2 of the form [i,i1,i2,,i2047][i, i-1, i-2, \dots, i-2047]. This new ciphertext c2c_2 has exactly one zero at the ii-th slot, with non-zero values in every other slot.

By Fermat's Little Theorem, and because operations are performed modulo 65537=216+165537 = 2^{16}+1 (a prime number), we know that for all x[1,65536]x\in[1, 65536], x655371=x216=1x^{65537-1} = x^{2^{16}} = 1.

Using fast exponentiation, we only need 16 multiplications to exponentiate each value of c2c_2 to 2162^{16}, creating c3c_3, a ciphertext containing ones in all 20482048 slots except for ii-th slot, since 065536=00^{65536} = 0.

Finally, we create a packed plaintext filled with ones, and subtract c3c_3 from it, giving us the desired mask vector mm with zeros everywhere except for the ii-th slot.

Step 2

Given the mask vector mm and the original encrypted vector AA, we use a seventeenth multiplication to multiply mm by AA, resulting in the partial result ciphertext c4c_4: [0,,0,xi,0,,0][0, \dots, 0, x_i, 0,\dots, 0].

Knowing that xix_i is in the first 20482048 slots, we apply the same strategy of 1111 rotations and additions (with the same rotation keys) to bring xix_i to the first slot.

References

[1] Ilia Iliashenko and Vincent Zucca, "Faster homomorphic comparison operations for BGV and BFV". Cryptology ePrint Archive, Paper 2021/315, 2021. Available online at https://eprint.iacr.org/2021/315

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.

  1. 01
    FHERMA 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
  2. 02
    FHERMA: Building the Open-Source FHE Components Library for Practical UseGurgen Arakelov, Nikita Kaskov, Daria Pianykh, Yuriy PolyakovCryptology ePrint Archive, Paper 2024/612