Encrypted Array Sorting
The article details the solution provided by the winner of the Array Sorting challenge.
Author: Chi-Hieu Nguyen, University of Technology Sydney, Australia.
Introduction
The objective of the challenge is to sort an encrypted array of real values. Sorting under approximate HE, such as the CKKS scheme, presents unique challenges due to the inherent noise introduced by the scheme. This noise grows with each operation, further amplified by the inaccuracies in the polynomial approximations used for comparisons.
Approach
The proposed solution comprises two main steps:
Step 1: Index Calculation
For each value in the array, we determine its target index by counting the number of values smaller than it. This involves pairwise comparisons of all array elements. To facilitate these comparisons, the current value is subtracted from every other value, and the result is passed through an approximated sign function. These pairwise comparisons are performed simultaneously in a SIMD fashion within a single packed ciphertext containing slots. This process requires several rotations to duplicate values across the ciphertext slots. Figure 1 illustrates the computation of target indices.

Figure 1: A toy example illustrating the computation of sorted indices for an input array of size 4.
To perform the comparison, we approximate the sign function using a composite polynomial approach as described in [1]. Specifically, three polynomials of
degree 63 are employed to approximate the function within the range to satisfy the challenge requirements. Figure 2 depicts the approximation, with a maximum absolute error below . Polynomial evaluations are done using the baby-step giant-step (BSGS) algorithm [2] to optimize level consumption. The comparison circuit utilizes 19 levels in total (), including an additional level for input value scaling. After applying the comparison function, rotation and summation operations are performed to accumulate results and compute the target indices (Figure 1).

Figure 2: Approximation of the sign function using a composition of three polynomials of degree 63.
Step 2: Permutation
A permutation matrix is derived based on the computed indices, as demonstrated in Figure 3. This process requires an approximated equality-checking function, which is constructed as a composition of two polynomials with degrees 59 and 62, as shown in Figure 4. The array is then rearranged into sorted order using vector-matrix multiplication with the permutation matrix. This step consumes a total of 14 levels, obtained by , where the additional two levels account for multiplication with the permutation matrix and a masking operation.

Figure 3: Computation of the permutation matrix from the indices, followed by the rearrangement of array elements into sorted order.

Figure 4: Approximation of the equality checking function using a composition of three polynomials of degree 59 and 62.
References
[1] Lee, Eunsang, et al. "Minimax approximation of sign function by composite polynomial for homomorphic comparison." IEEE Transactions on Dependable and Secure Computing 19.6 (2021): 3711-3727.
[2] J.-P. Bossuat, C. Mouchet, J. Troncoso-Pastoriza and A.-P. Hubaux, "Efficient bootstrapping for approximate homomorphic encryption with non-sparse keys", Proc. EUROCRYPT, pp. 587-617, 2021.
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
