An Efﬁcient Method for Comparing Numbers and Determining the Sign of a Number in RNS for Even Ranges

: Fully Homomorphic Encryption (FHE) permits processing information in the form of ciphertexts without decryption. It can ensure the security of information in common technologies used today, such as cloud computing, the Internet of Things, and machine learning, among others. A primary disadvantage for its practical application is the low efﬁciency of sign and comparison operations. Several FHE schemes use the Residue Number System (RNS) to decrease the time complexity of these operations. Converting from the RNS to the positional number system and calculating the positional characteristic of a number are standard approaches for both operations in the RNS domain. In this paper, we propose a new method for comparing numbers and determining the sign of a number in RNS. We focus on the even ranges that are computationally simple due to their peculiarities. We compare the performance of several state-of-art algorithms based on an implementation in C++ and relatively simple moduli with a bit depth from 24 to 64 bits. The experimental analysis shows a better performance of our approach for all the test cases; it improves the sign detection between 1.93 and 15.3 times and the number comparison within 1.55–11.35 times with respect to all the methods and conﬁgurations.


Introduction
Number comparison and sign detection are simple and basic operations for calculation in computing systems. Both operations are necessary for almost any application area in computing technology: mathematical calculations, rendering, data storage, data transfer, data retrieval, encryption, machine learning, etc. Unfortunately, their application for encrypted data exhibits several limitations.
Sign detection and comparison of numbers in the Residue Number System (RNS) are based on the calculation of the positional characteristic. The algorithms for both operations in RNS are computationally complex because RNS is not a Weighted Number System (WNS). Various methods are used to reduce their computational complexity, such as the Pirlo and Impedovo function [1], Diagonal Function (DF) [2,3], Modified Diagonal Function (MDF) [4], Approximate Method (AM) [5,6], and Core Function (CF) [7].

Chinese Remainder Theorem
The following formula is used to convert numbers from RNS to WNS with Chinese Remainder Theorem (CRT) where P i = P p i and |P −1 i | p i is a multiplicative inverse of P i modulo p i . The restored number must be compared with P/2. If X < P/2, then the number is greater than zero. Two's complement is the most common method of representing signed integers. Its half of the range (the upper half) denotes a negative number and vice versa. To estimate the time complexity, we assume that all RNS moduli are composed of l-bits bit words, so the time complexity is defined by O n 2 ·l 2 .
Let us consider an example based on this method.
Algorithm 1 presents the pseudocode of the CRT method. However, this method has a significant drawback due to the divisions to obtain X. The division of large numbers by a large P increases the time required for calculations.

Mixed Radix Conversion Method
The Mixed Radix Conversion (MRC) method consists of a consecutive translation of a number from RNS to WNS. In addition, a pattern in the number in a generalized form is traced by y i = (U i ·V i ) ρ i , where U i is known as the conversion coefficient and defines an increasing series U 1 = x 1 , Yassin and Moore [24] found that Example 2. The initial data are a set of moduli (127, 63, 50, 13), a set of residues (78, 41, 47, 7), V 1 = 1, V 2 = 1, V 3 = 1, and V 4 = 1.
Algorithm 2 describes the pseudocode of the MRC method.
. sum+ = U i mod p i 4. for i = 1 to n do 4.1 P * = p i 4.2 sum+ = P * (U i ) mod p i 5 X = sum 6 if X < P/2: 6.1 S = 1 7. else: Effective implementation of this method allows avoiding division by a large P. However, the method uses many modular operations related to the vector (p 1 , p 2 , . . . , p n ).

Approximate Method
The algorithm for finding the Euclidean division by the range RNS is computationally complex. To reduce the computational complexity of the Approximate Method (AM), it is proposed to use weighted coefficients, which allow replacing the operation of Euclidean division with the operation of taking a fractional part.
AM is based on the mapping from [0, P − 1] to [0, 2) to avoid the division by a large P. The mapping allows calculating the positional characteristic of the number, hence, to determine the sign of the number or compare numbers with each other. The positional characteristic is originally calculated as: However, the use of (3) as a positional characteristic requires the use of rational fractions or their approximation using numbers with a fixed precision, which increases the computational complexity of the algorithm. In [25], a modification of the methods from [5] was proposed, which allows retaining the advantages of the method from [5] but at the same time avoiding the use of rational numbers.

Diagonal Function
The positional characteristic of a number in RNS can be also determined by the socalled Diagonal Function (DF), which is defined by D(X) = ∑ n i=1 X p i [2]. To calculate D(X) using moduli RNS, use the following formula: where k * i = |−P −1 i | SQ , SQ = P 1 + P 2 + . . . + P n . The sign of the number depends on the comparison of D(X) and SQ/2. Let us look at an example of comparing numbers. The time complexity of the algorithm is Example 4. We consider the numbers of previous examples, X = (2, 2, 3) and moduli (3,5,7). First, we calculate the values SQ = 35 + 21 + 15 = 71 and k * i . Then, we find the value of the DF: Since 10 < 71/2, then X is positive. Algorithm 4 describes the pseudocode of the DF method.

Core Function
Burgess [26] proposed the minimal Akushsky Core Function (CF) based on the result obtained in [27,28]. This approach is similar to the DF method, and it does not need to calculate the critical cores.
The analytical form of the Pirlo function is calculated as: where k * * i = If Pi(X) < P n /2, then the number is positive, since the function under study is monotonically increasing. Time complexity is O((n − 1) 2 l 2 ).
Let us consider an example of comparing numbers.

Determining the Sign of a Number
Knowing the sign of a number can increase the performance of number comparison in RNS; the difference between numbers X and Y provides information about their comparison. Determining the Sign of a Number (DSN) in RNS is based on the fact that the residues x i of the negative number X are the complement of x i to the modulus p i . Hence, the following relationship holds: The formula based on CRT and an AM allows determining the sign of the number based on the following equation [29].
X > 0 if the equation holds; otherwise, X < 0. Let us consider the algorithm for comparing numbers in RNS based on DSN.
Example 6. Let the number in RNS be (1, 0, 4) with the basis (3,5,7). We determine the sign of the number by calculating Formula (8): So, the number is positive. Algorithm 6 shows the pseudocode of the DSN method.

Modified Diagonal Function
The Modified Diagonal Function (MDF) method reduces the computational complexity of comparing numbers in the RNS [5]. It proposes a new positional characteristic based on the DF and an AM. The essence of the MDF method is to calculate the relative value of the Computation 2022, 10, 17 9 of 21 DF to SQ, which allows replacing the computationally complex operation of finding the residue of division by SQ by taking the fractional part of the number, and the coefficients k * i are replaced by the relative value k * i of SQ in Formula (5). wherek Let us look at an example of comparing numbers. Time complexity is O ((n − 1)l + log 2 n) log 2 ((n − 1)l + log 2 n) .

Example 7.
We use the previously numbers X = (2, 2, 3) and moduli (3,5,7). Then, we take 2 N M = 8.755 and value k * i from the diagonal function:  Since the RNS range is an Even Number (EN), one of the RNS moduli is an EN. Without loss of generality, we assume that the module p n is even; then, using the property X a b = X a·b , the determination of a sign is reduced to a two-stage algorithm. The first stage is dividing by an integer P n = P p n . The second stage is dividing by an integer p n

.
Formally, it is determined by the following formula: The correctness of (10) follows from the proof of the following Theorem 1.
Theorem 1. If X, a, and b are integer numbers with a, b ≥ 2, then the equality holds: Proof. Due to X a = X−|X| a a , Equality (11) takes the form: We represent X in the form X = a·b· X a·b + |X| a·b . Substituting it in (12), we have: Let X = ω·a + γ, where 0 ≤ γ ≤ a − 1, we obtain: Considering that 0 ≤ |ω| b ≤ b − 1, then Substituting this result in (13), we have: The theorem is proved.

Corollary 1. Equation (10) is correct.
Proof. Let a = P n and b = p n 2 . Since p n is an even number, then p n 2 is an integer number. Therefore, the conditions of Theorem 1 are satisfied and the corollary is proved. The time complexity of the algorithm is O n 2 l . Example 8. Let the RNS be given by the moduli p 1 = 17, p 2 = 19, p 3 = 23, and p 4 = 32. We determine the sign of a number X = (16,18,22,15) and Y = (0, 0, 0, 16). Let us calculate the synoptic weights: The number X is positive since and Y is negative because Algorithm 8 shows the pseudocode of the DSN with the EN method.

Performance Evaluation
The proposed algorithm described in Section 3.8 has the advantage among the methods described in Section 3.
To confirm the properties, we implement all the algorithms using the high-level language C++ and compare their performance. The experiments are carried out on operating systems Windows 10 Home Edition on a computer with Intel Core i5-8250U 1.60 GHz, RAM DDR4 8 GB 1196 MHz, and SSD 512 GB.
For the analysis, we use a 128-bit number transferred to RNS, a set of six RNS moduli, and a width of 24 to 64 bits. We vary a vector length of 3 to 8 moduli with a 32-bit width.
The experimental results report the maximum, minimum, and average of 10,000 runs of each method. Additionally, we incorporate the average calculated based on the maximum and minimum values.
The information of the execution is presented in Appendix A. For simplicity, we only analyze the significant discoveries of the results. The average values have stable linear growth. The minimum values are prevailing because the average values of each method are very close to them. Hence, the maximum values are not common causes.
To avoid time measuring biases, the mean values are calculated as the average of 10,000 measuring of the maximum values and 10,000 measuring of the minimum values. We also consider the medium values of all methods to determine the most efficient ones.

Comparative Analysis
The performance of all methods for the operations of determining the sign of a number and number comparison is presented in Figure 1. DSN-EN has the highest performance for both operations with different numbers of modules p and dynamic bit width. AM, DF, and MDF are in the group of the second most efficient methods for all operations.

Performance Evaluation
The proposed algorithm described in Section 3.8 has the advantage among the methods described in Section 3.
To confirm the properties, we implement all the algorithms using the high-level language C++ and compare their performance. The experiments are carried out on operating systems Windows 10 Home Edition on a computer with Intel Core i5-8250U 1.60 GHz, RAM DDR4 8 GB 1196 MHz, and SSD 512 GB.
For the analysis, we use a 128-bit number transferred to RNS, a set of six RNS moduli, and a width of 24 to 64 bits. We vary a vector length of 3 to 8 moduli with a 32-bit width.
The experimental results report the maximum, minimum, and average of 10,000 runs of each method. Additionally, we incorporate the average calculated based on the maximum and minimum values.
The information of the execution is presented in Appendix A. For simplicity, we only analyze the significant discoveries of the results. The average values have stable linear growth. The minimum values are prevailing because the average values of each method are very close to them. Hence, the maximum values are not common causes.
To avoid time measuring biases, the mean values are calculated as the average of 10,000 measuring of the maximum values and 10,000 measuring of the minimum values. We also consider the medium values of all methods to determine the most efficient ones.

Comparative Analysis
The performance of all methods for the operations of determining the sign of a number and number comparison is presented in Figure 1. DSN-EN has the highest performance for both operations with different numbers of modules and dynamic bit width. AM, DF, and MDF are in the group of the second most efficient methods for all operations.      Figure 2 present a comparison between DSN-EN and AM, both methods with the best overall performance. From Figure 2a, we observe that the time difference is increased with respect to the number of modules for both operations. Figure 2b shows the execution time varying a dynamic bit width.
tion with respect to DSN-EN, the efficiency of AM is between 1.93 and 5.64 times worst. Likewise, DF and MDF are from 2.44 to 6.1 and from 2.11 to 5.64 times worst, respectively. Similar results present the comparison of numbers, DSN-EN improves AM within 1.55-4.6 times, DF among 1.75-4.47 times, and MDF within 1.93-4.57 times.
The results of comparison with different dynamic bit widths exhibit that DSN-EN is more efficient than MDF from 2.68 to 3.61 times, AM within 2.93-3.46 times, and MD between 2.97 and 3.44 times. For comparison of numbers, the differences are in 2.45-3.47, 2.48-3.47, and 2.56-3.43 for AM, DF, and MDF, respectively. Figure 2 present a comparison between DSN-EN and AM, both methods with the best overall performance. From Figure 2a, we observe that the time difference is increased with respect to the number of modules for both operations. Figure 2b shows the execution time varying a dynamic bit width.
In general, the proposed method is several times more productive than AM, which was determined as the most efficient of the existing state-of-the-art ones. We consider that the DSN-EN can be applied efficiently in systems that use RNS.

Conclusions
Determining the sign of a number is fundamental for the implementation of several techniques with privacy preserving in untrusted environments. Current homomorphic encryption approaches try to make the implementation of this operation more efficient by using a residual number system. Unfortunately, the methods based on converting a number to a weight number system and determining the sign are inefficient and slow. The calculation of positional characteristics is better in performance but not efficient. In general, the proposed method is several times more productive than AM, which was determined as the most efficient of the existing state-of-the-art ones. We consider that the DSN-EN can be applied efficiently in systems that use RNS.

Conclusions
Determining the sign of a number is fundamental for the implementation of several techniques with privacy preserving in untrusted environments. Current homomorphic encryption approaches try to make the implementation of this operation more efficient by using a residual number system. Unfortunately, the methods based on converting a number to a weight number system and determining the sign are inefficient and slow. The calculation of positional characteristics is better in performance but not efficient.
We propose a new method to determine the sign of a number in RNS and provide a theoretical foundation for when one modulo is an even number. We provide its performance evaluation compared with other determining the sign methods. The results show the advantages of our approach for different modules numbers and dynamic bit widths. In the future, we will study determining the sign of a number and comparing numbers in RNS for odd ranges.

Conflicts of Interest:
The authors declare no conflict of interest. Table A1 presents the time in milliseconds (ms) needed to determine the number's sign and comparison of numbers for considered methods with a dynamic number of modules p based on the 10,000 measurements.  Table A2 shows the time (ms) needed to determine the number's sing and comparison of numbers for with a dynamic bit width based on the 10,000 measurements.                           Figure A8. Performance of the DSN-EN method to (a) determine the number's sign with a dynamic number of modules p, (b) compare dynamic numbers of modules p, (c) determine the number's sign with a dynamic bit width, and (d) compare numbers with dynamic bit width.