Max Element Search over an Encrypted Vector
The article details the solution provided by the winner of the Max Element challenge.
Author: Vivian Maloney, cryptography researcher at the Johns Hopkins University Applied Physics Laboratory
Introduction
We are tasked with finding the maximum value in an encrypted array of length , where each entry is an integer within [0, 256). This solution is implemented using BFV homomorphic encryption, taking advantage of the discrete nature of the values and FHE’s SIMD parallelism.
The Path Not Taken
In traditional, unencrypted computation, a binary tree of pairwise comparisons can find the maximum element with comparisons and a circuit depth of . However, this approach does not map well to FHE, given its depth constraints and the benefits of parallelism in SIMD operations.
Algorithm Overview
Step Polynomial Evaluation
We evaluate whether each element of the encrypted array is greater than or equal to a threshold , using a step polynomial:
This step is computed for every threshold between 1 and .
Algorithm Pseudocode
The algorithm we implemented corresponds to the following pseudocode:
def max(x, m=256):
sum = 0
for i in range(1, m):
y = (x >= i)
if np.any(y):
sum += 1
return sum
For each threshold , we perform two key operations:
- Elementwise Comparison: Compare each encrypted value with to get a binary result (1 if , 0 otherwise).
- Boolean Aggregation: Use a homomorphic NAND operation to check if any element in the array exceeds .
Boolean Aggregation via NAND
The NAND operation checks whether any element of the array exceeds the threshold . The homomorphic NAND function is implemented as follows:
def NAND(y, n):
z = 1 - y
r = 1
while r < n:
z = z * (z << r)
r *= 2
z = 1 - z
return z
This approach reduces the multiplicative depth during iterations, making later steps faster.
FHE Comparison
In FHE, non-linear operations like comparisons are evaluated using polynomials. For each threshold , we use Lagrange interpolation to construct a polynomial that acts as a step function:
An alternative approach involves subtracting the threshold from each element and applying a unary Heaviside step function, defined as:
This results in a polynomial evaluation over the expanded range instead of , which simplifies the step function's definition but incurs additional multiplicative depth. The expanded range allows for more straightforward evaluation but trades off efficiency due to the need for deeper circuits.
Parallelization with SIMD
Since is smaller than half the ring dimension (16384), we can duplicate the encrypted array across the available slots and process different thresholds in parallel. The polynomial coefficients for each threshold are encoded as plaintext vectors, and ciphertext-plaintext multiplications are used to evaluate the different polynomials.
CKKS Alternative
While our current approach uses BFV for discrete integers, an alternative implementation using CKKS could leverage approximate comparisons. A well-known method for comparing encrypted numbers in CKKS relies on Chebyshev polynomials to approximate the absolute value function, allowing us to compute:
Since the array values are discrete, the Chebyshev approximation of the absolute value function only needs to be accurate within the small range of noise.
This approach can yield more efficient results than the method described in the paper Numerical Method for Comparison on Homomorphically Encrypted Numbers by Jung Hee Cheon et al. (Cheon et al., 2019).
Conclusion
This approach efficiently computes the maximum in an encrypted array by combining polynomial step functions, NAND-based Boolean aggregation, and SIMD parallelism. The result is an optimized homomorphic computation suitable for BFV.
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
