Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Numerical methods for finding multiplicative inverses of a modulo N Journal: Songklanakarin Journal of Science and Technology Manuscript ID SJST-0-0.R Manuscript Type: Original Article Date Submitted by the Author: -Jul-0 Complete List of Authors: Prempreesuk, Bencharat; Silpakorn University - Sanam Chandra Palace Campus, Mathematics Pongsriiam, Prapanpong ; Silpakorn University - Sanam Chandra Palace Campus, Mathematics KANYAMEE, NAIRAT; Silpakorn University - Sanam Chandra Palace Campus, Mathematics Keyword: modular multiplicative inverse, Newton s method, secant method, binary representation, Zeckendorf representation
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 Numerical methods for finding multiplicative inverses of modulo Bencharat Prempreesuk, Prapanpong Pongsriiam, and Nairat Kanyamee * Department of Mathematics, Faculty of Science, Silpakorn University, Nakhon Pathom, Thailand. * Corresponding author, Email address: kanyamee_n@su.ac.th Abstract In this work, we propose new methods which are based on the root-finding methods in numerical analysis to calculate the inverses of an integer modulo for any positive integer. We apply Newton's and secant methods with a power-reduction process and Newton's and secant methods together with the binary representation and Zeckendorf representation to determine the inverse of an integer modulo. The numerical results confirm an accuracy compared to the analytical results. In addition, the two methods with the binary and Zeckendorf representations give better CPU times than Newton s and secant methods with power-reduction for large. Keyword: modular multiplicative inverse, Newton s method, secant method, binary representation, Zeckendorf representation
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0. Introduction Modular multiplicative inverse is often seen in number theory and it is desirable to be able to calculate it quickly. This motivates us to search for an efficient method in finding the modular multiplicative inverse. On the other hand, numerical analysis involves algorithms that are used in approximating the solutions to a particular problem. So, there should be some connections between the two fields of number theory and numerical analysis. Let Z and, Z. We write mod if. Assume further that,=. Then it is a well-known result (Rosen, 00) in number theory that there exists an integer such that mod. The integer is called the modular multiplicative inverse (or simply the inverse) of modulo and it is denoted by or. For example, mod, so the inverse of modulo is, that is, mod. By the fundamental theorem of arithmetic (Rosen, 00), we can write =, where,,, are distinct primes and,,, are positive integers. We can first calculate the inverse of modulo,,,, respectively, and then apply Chinese remainder theorem (Rosen, 00) to obtain the inverse of modulo. Apart from the Euclidean algorithm to calculate the inverse, some researchers have introduced an interesting application of numerical analysis to number theory. In numerical analysis, we have the classical root-finding methods such as Newton's method and secant method to find a zero of a function or to find a solution of = 0. The concept of iterative root-finding methods is to construct a sequence
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0,,, that will converge to a zero of in [,]. In 0, Knapp and Xenophontos calculated the inverses of an integer modulo using the rootfinding methods such as Newton s method, secant method, fixed-point iteration and higherorder convergent method (Knapp & Xenophontos, 0). Newton s method is suitable for calculating the inverse of an integer modulo when = for =,,, and some positive integer. The secant method is used to find the inverse of modulo, when the inverses of mod and mod are already known. The higher-order convergent method of order is suitable to calculate an inverse of modulo when = for =,,, and some positive integer. In 0, Dumas studied fast algorithms for the computation of modular inverses by using Newton-Raphson iteration (Dumas, 0). He designed four algorithms with a few operations used. He claimed that his hybrid algorithm is % faster than any direct method. In this paper, we extend the work from Knapp et al. by using Newton s and secant methods with a power-reduction calculation and Newton's and secant methods together with the binary representation and Zeckendorf representation to determine the inverse of an integer modulo for any positive integer by splitting into its prime factor form and calculate the inverse in terms of modulo. To do this, we consider into two cases. First case, we represent as the sum of distinct powers of. For the second case, we represent as the Zeckendorf representation (Griffiths, 0). Then we use the root-finding methods to calculate inverses modulo. Furthermore, we show the algorithms in MATLAB to calculate the inverse of modulo and compare the CPU times of each method.
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0. Rootfinding methods for the inverse of modulo In this section, we establish root-finding methods with a power-reduction calculation to calculate the inverse of an integer modulo. The methods are based on the root-finding methods in Knapp et al. (Knapp & Xenophontos, 0). For our problem, we are looking for an integer for which mod or mod. Following the methods in Knapp et al., a suitable choice for the function is =. The goal is to solve the equation =0.. Newton s method with a power-reduction calculation Newton s method is one of the most powerful and well-known numerical method for root-finding problems. We combine this method with a process of modular calculation to reduce power of to find the inverse mod. Newton s method relies on the continuity of,. According to (Burden & Faires, ), the iteration for Newton s method to locate a zero of is given by =, =,,, with a suitable initial value. This iteration generates a sequence { } which converges to a zero of. To apply the method, we first find =. The iteration for Newton s method becomes = =, =,,, (.) Here, we obtain the sequence { } which converges to a solution of =0.
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 Theorem... Let >0 and suppose that is an inverse of modulo. Then given by (.) is an inverse of modulo. Proof. See Knapp and Xenophontos (0). Theorem.. implies that if we know the inverse of modulo, then we can use Newton s method to calculate the inverse of modulo, modulo, modulo and so on. For simplicity, we choose the initial guess to be the inverse of modulo and apply Newton s method in (.) to determine the inverse of modulo, modulo, modulo and so on. As a result, this method is suitable for calculating the inverse of an integer modulo when =, =,,,. It is known that the method converges at a quadratic rate for a simple root (Burden & Faires, ). In addition to Newton s method in Knapp et al., we construct the algorithm in order to find the inverse of an integer modulo for any integer, other than the form of. We first apply Newton s method in (.) to calculate the inverse of an integer modulo where << for some positive integer. Then, we begin to reduce the power of from down to. Since mod, it follows that mod. The reduction process applies this idea together with the congruence property to search for the smallest positive number in the same congruent class as modulo (shown in line 0 (referred to line ) in Algorithm ). By continuing this process, we finally arrive with the inverse of an integer modulo. The algorithm (Algorithm ) to determine such an inverse is shown in Table below. (Table ) Table : Algorithm for Newton s method with a power-reduction calculation
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 Example : Let =,= and =. We want to calculate the inverse of modulo. We choose the initial value = since mod. Observe that <<. We apply Newton s method to find the inverse of modulo and so on until as follows: mod mod mod. After we obtain, we use the power-reduction calculation and finally get the inverse of modulo as.. The secant method with a power-reduction calculation Secant method is designed to converge almost as fast as Newton s method, but involves only not (Atkinson, ). In a similar way to Newton s method, we also combine this method with a modular calculation to reduce the power of. The iteration for secant method is given by =, =,,, The method requires two initial values and to generate a sequence { } which converges to a zero of. By setting =, the iteration for secant method is given by = = +, =,,, (.) Equation (.) provides a sequence { } which converges to a root of =0.
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 Theorem... Suppose that mod and that mod. Then, with given by (.), we have mod. Proof. See Knapp and Xenophontos (0). From this theorem, we can use secant method to find the inverse of modulo if we know the inverse of modulo and modulo. We first choose and such that mod, and mod. Then, we iterate from secant method which will be the inverse of modulo, and continue the process. Since it is simpler to calculate the inverse of modulo, both initial values and for secant method are chosen to be the inverse of modulo. Then, we apply secant method to calculate which is the inverse of modulo. By continuing the process, we obtain the inverse of modulo, modulo, modulo and so on. In fact, the iterated values,, are the inverses in which the powers of are the Fibonacci numbers. This method is known to provide a convergence of order. (Atkinson, ). In addition to the secant method in Knapp et al., we construct the algorithm in order to find the inverse of an integer modulo for any positive integer, other than the form of or where is a Fibonacci number. Recall that the Fibonacci sequence is defined by the recurrence relation = + for and the initial values = =. We first apply the secant method in (.) to calculate the inverse of an integer modulo where << for some positive integer. Then, we use the reduction process (mentioned in section.) to reduce the power of from down to in order to obtain the inverse of an integer modulo. The algorithm (Algorithm ) to determine such an inverse is shown in Table. (Table )
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 Table : Algorithm for the secant method with a power-reduction calculation Example : Let =,= and =. We want to calculate the inverse of modulo. We choose the initial value =, = because mod. Notice that <<. We apply the secant method to find the inverse of modulo, modulo and so on until modulo as follows: mod mod mod mod mod. Once we obtain, we use the power-reduction calculation and finally get the inverse of modulo as.. Calculating the inverse of mod and mod In this section, we use the methods in section. and section. to calculate the inverse of modulo for any positive integer. We consider into two cases. For the first case, we represent as the sum of distinct powers of. For the second case, we represent in the form of Zeckendorf representation.. Using binary representation Since we know that every positive integer can be represented as the sum of distinct powers of (Rosen, 00), we first express in a binary system. If is a positive integer and the digit,,, exist, the binary expansion of is given by (Mathew, ) = + + + +,
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 where each digit is either a 0 or. This can also be written in a binary notation as =. Therefore = (.) or =. (.) The form in (.) consists of a prime number with the power of. We can apply Newton s method in Algorithm to calculate the inverse of an integer modulo for each in which in (.) is not zero. The secant method in Theorem.. and Algorithm are then applied to determine the inverse of each pair of modulo until the largest power term. The algorithm (Algorithm ) to calculate the inverse using the binary representation is presented in Table. (Table ) Table : Algorithm for calculating the inverse using binary representation Example : Let =,= and =. We want to calculate the inverse of modulo. We choose the initial value =, since mod. The binary representation of in this problem is == +=. Observe that the representation contains the terms and. We apply Newton s method and Theorem... to find the inverses of modulo, as follows: mod or mod mod or mod mod or mod.
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 Using the secant method and Theorem... with and as the initial conditions to calculate the inverse of modulo yields = mod Therefore, is the inverse of modulo.. Using the Zeckendorf representation In view of the fact that any positive integer can be expressed as the sum of Fibonacci numbers (Rosen, 00). In this section, we represent the integer in the form of the Zeckendorf representation. Theorem... (Zeckendorf s Theorem (Rosen, 00; Griffiths, 0)) Every positive integer can be represented uniquely as the sum of one or more distinct Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers. More precisely, if is any positive integer, there exist positive integers, with > + for every {0,,,,} such that = where is the Fibonacci number. Such a sum is called the Zeckendorf representation of. In fact, the greedy algorithm always leads to Zeckendorf representation. Proof. See Rosen (00) and Griffiths (0). Example : Consider the number =. There are several ways to express this number as a sum of Fibonacci numbers. Notice that =+++= + + + and =+++= + + + are not Zeckendorf representations because,, and, are consecutive Fibonacci numbers.
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 However, =++= + + is a Zeckendorf representation which is uniquely determined. From the greedy algorithm and Zeckendorf s theorem, we can construct an algorithm to write in form of the Zeckendorf representation. We first construct a Fibonacci sequence starting from and contains numbers closest to. The binary search algorithm is applied to find the largest Fibonacci numbers,, that is less than or equal to. Then repeat the process to find the largest Fibonacci numbers,, that is less than or equal to. By continuing this process, we arrive with the Zeckendorf representation = + + + for some N. Algorithm in Table provides the process of finding such a representation. (Table ) Table : Algorithm for representing with the Zeckendorf representation Once we have the Zeckendorf representation from Algorithm. The secant method in section. is applied to find the inverse of an integer modulo for each Fibonacci number in Algorithm. Then, we apply the secant method again to find the inverse of an integer modulo. The algorithm (Algorithm ) that includes the secant method to find the inverse of an integer modulo is shown in Table. (Table ) Table : Algorithm for calculating the inverse using Zeckendorf representation Example : Let =,= and =. We want to calculate the inverse of modulo. We choose the initial value = and = because mod. We have that =++. Then, we use the secant method and Theorem... to
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 find the inverses of modulo, where is a Fibonacci number less than or equal as follows: mod mod mod mod. Next, we apply the secant method and Theorem... again to find the inverse of modulo and modulo. This yields the inverse = mod. Therefore, is the inverse of modulo. In the next example, we compare the CPU times of each method used to find the inverse of modulo by using MATLAB. The calculations are carried out with an Intel Core i CPU@ GHz RAM GB computer. The results are represented in Example. Example : Let =, =. We choose the initial value = and =, since mod. The CPU times are recorded in seconds. (Table ) Table : CPU times in finding the inverse of modulo of the four methods. From the Table, Newton s method is the fastest method to calculate the inverses when is the power of and is not too large due to less number of operations to execute than the other methods. In this example, we can see that when = and, Newton s method gives the smallest time to calculate the results. If we consider the secant method, it has the best time when is a Fibonacci number and not too large. We can see that this method gives us the smallest time to find the inverses
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 when =, and. The binary and Zeckendorf representation methods can calculate the inverses for every and the CPU time is increased a bit at a time when is larger. These last two methods are the best way to calculate the inverses of modulo, especially when is not in a power of or a Fibonacci number or when is large. The figures below also confirm our discussion above. Figure (a) shows the graphs of the CPU times for the four methods versus when =,,,0. The CPU time in the graph of Newton s method drops when =,,,, and it jumps up due to the power-reduction process. For example, the method uses low CPU time when =, but it takes much longer time when = since it first calculates the inverse of modulo then it reduces the power down to =. Similarly, the CPU time in the graph of the secant method drops when =,,,,, and it jumps up in a similar manner. If we consider the graphs of the CPU times for the binary and Zeckendorf representation methods, the graphs are quite stable and do not increase much. It takes less CPU times than the first two methods as increases. Figure (b) illustrates the behavior of CPU times of these methods when =,,,0. We can see from the graphs that the times taken do not rise up when gets larger. They are time efficient and very stable to calculate the inverse of modulo for any number. (Figure (a)) (Figure (b)) (a) (b) Figure : (a) The graphs of CPU times of the four methods versus when =,,,0. (b) The graphs of CPU times of the binary and Zeckendorf representations versus when =,,,0.
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 We end the section with an example to calculate the inverse of an integer modulo where =. Example : Let = and =0. The prime factorization of is =. We would like to find an integer such that mod. We break it down into finding mod, mod, mod. By using our algorithms, we can find the inverses of each case as mod, mod, mod. Then we combine them by using the Chinese remainder theorem. Let =, =, =, =, =, =. From the Chinese remainder theorem (Rosen, 00), we define = = = = = = = = = = = =. To determine the inverse of, denoted by, we solve mod, which is equivalent to mod. So mod. Next, we find the inverse of, denoted by, by solving mod, which is equivalent
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 to mod. So mod. The inverse of, denoted by, is calculated by solving mod, or equivalently, mod. Then 0 mod. Therefore, according to the Chinese remainder theorem, the inverse of modulo 0 is + + mod ++0 mod mod mod.. Conclusion and Discussion This work introduces new methods to calculate inverses modulo for any positive integer. We express in its prime factorization as =, where,,, are distinct prime numbers and,,, are positive integers. Then, we calculate the inverse by breaking it down to find the inverse of mod, mod,, and mod and combine them using the Chinese remainder theorem. The four methods proposed to calculate the inverse of mod are designed based on the Newton's and secant methods. We also construct the algorithms for each method to help calculating the inverses of modulo based on the MATLAB program and present them in the example of each method. In addition, we compare the CPU times of each method in Example. Newton s method with a power-reduction calculation is good for the case when = for =,,,. The secant method with a power-reduction calculation is used to find the inverse of modulo when the inverses of mod and
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page 0 of 0 0 0 0 0 mod are already obtained. In fact, it is good for the case when is a Fibonacci number. However, these two methods may take time when is not in such a form because of the power-reduction process. The method that expresses as a binary representation followed by Newton s and secant methods is suitable for any numbers because every number can be represented in base. The method does not include power-reduction calculation. As a result, it is time efficient and very stable to calculate the inverse for any number, especially when is large or is not in a power of or a Fibonacci number. Similarly, the method that expresses as a Zeckendorf representation followed by the secant method is suitable for any numbers because every number has its Zeckendorf representation. This method is also as good as the method that expresses in a binary representation. Acknowledgement We would like to thank Dr. Pinyo Taeprasartsit for his helpful suggestions in programming throughout this work. References Atkinson, K. E. (). An Introduction To Numerical Analysis ( nd ed). USA: John Wiley&Sons. Burden, R. L.,& Faires, J. D. (). Numerical Analysis. Pacific Grove, USA: Brooks/Cole. Dumas, J. G. (0). On Newton-Raphson Iteration for Multiplicative Inverses Modulo Prime Powers. IEEE Transations on Computers, (), 0-. doi:./tc.0.
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 Griffiths, M. (0). The Zeckendorf Representation of a Beatty-Related Finonacci Sum. Fibonacci Quart. (), 0-. MSC0: B, B. Knapp, M. P.& Xenophontos, C. (0). Numerical analysis meets number theory: Using rootfinding methods to calculate inverses mod. Appl. Anal. Discrete Math. (), -. doi:./aadm00k Mathew, J. H. (). Numerical Methods For Mathematics, Science, And Engineering. New Jersey, USA: Prentice-Hall. Rosen, K. H. (00). Elementary Number Theory And Its Application. USA: Addison- Wesley.
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 SJST MANUSCRIPT TEMPLATE FOR A FIGURE FILE Figure (a) Figure (b) Figure : (a) The graphs of CPU times of the four methods versus when,,,0. (b) The graphs of CPU times of the binary and Zeckendorf representations versus when,,,0.
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 SJST MANUSCRIPT TEMPLATE FOR A TABLE FILE Algorithm : Newton s method with a power-reduction calculation : Input:,, Z ; N ; intitial value (inverse of mod ); : for k = to M : compute : = ( ); = ; : if <0 : set =; = + ; : while <0 : =+; = +( ) ; : end ; display : else if >0 and < : display ; stop the process : else if >0 and > : set =; = ; : while > : =+; = ( ) ; : end ; display : end : if = : stop the process : else if > 0: repeat line - by replacing with : end : end Table : Algorithm for Newton s method with a power-reduction calculation
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 Algorithm : The secant method with a power-reduction calculation : Input:,, Z ; N ; intitial value, (inverse of mod ); : =; =; : for k = to M : compute : = + ; = + ;= ; : if <0 : set =; = + ; : while <0 : =+; = +( ) ; : end ; display : else if >0 and < : display ; stop the process : else if >0 and > : set =; = ; : while > : =+; = ( ) ; : end ; display : end : if = : stop the process 0: else if > : repeat line - by replacing with : end : end Table : Algorithm for the secant method with a power-reduction calculation
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 Algorithm : Inverse of mod using binary representation : Input:,, Z ; N ; intitial value (inverse of mod ); : = vector representing n in base two ; =size of ; : = vector containing indices of nonzero elements in ; =size of ; : Apply Algorithm from line - : Input:,, Z ; N ; intitial value (inverse of mod ) : Output: : if = : display () ; stop the process : else for = to : =( ) ; : end : = ( ) + ( ) ; : Apply Algorithm from line - : Input:,, Z ; N ; intitial value, ; : Output: : for = to : = + ( ) ; : Apply Algorithm from line - : Input:,, Z ; N ; intitial value, ; 0: end : display () ; stop the process : end Table : Algorithm for calculating the inverse using binary representation
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 Algorithm : Representing with the Zeckendorf representation : Input: N ; : set = ; = ;=;= ; : while : = + ; = ; =+ ; (constructing Fibonacci sequence) : end : while 0 : Find =, the closet value to the number, using binary search algorithm : Input:, : Output: and (index of value in ) : if > : = ; =, ; =, ; : else : = ; =, ; =,; : end : = ; : end : = flip left/right of ; = flip left/right of ; display and (Return, the Zeckendorf representation of and corresponding indices ) Table : Algorithm for representing with the Zeckendorf representation
Page of Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE 0 0 0 0 0 Algorithm : Inverse of mod using Zeckendorf representation : Input:, Z ;, N ; intitial value (inverse of mod ) : Apply Algorithm : Input : : Output :,, : Find the closet value to (),, and its index,, using binary search algorithm : Input:,() : Output :, : Apply Algorithm from line -, Change in line to (inv of mod,,, ) : Input:,,() Z ;, N ; intitial value, ; : Output: : = : : if length of = : display () : else = + ; : Apply Algorithm from line - : Input:,, Z ; N ; intitial value, ; : Output: : for = to (length of ) : = + ; 0: Apply Algorithm from line - : Input:,, Z ; N ; intitial value, ; : Output: : end : display () : end Table : Algorithm for calculating the inverse using Zeckendorf representation
Songklanakarin Journal of Science and Technology SJST-0-0.R KANYAMEE Page of 0 0 0 0 0 Methods Newton Secant Base Zeckendorf (mod ) 0.00 0.00 0.0 0.0 (mod ) 0.00. 0.0 0.0 (mod ) 0.00 0.00000 0.0 0.0 (mod ) 0.00 0.00 0.0 0.0 (mod ) >00 0.00 0.0 0.0 (mod )..00 0.0 0.0 (mod ).0 >00 0.0 0.0 (mod ) 0. >00 0.0 0.00 Table : CPU times in finding the inverse of modulo of the four methods.