Recent from talks
Nothing was collected or created yet.
Modular multiplicative inverse
View on WikipediaIn mathematics, particularly in the area of arithmetic, a modular multiplicative inverse of an integer a is an integer x such that the product ax is congruent to 1 with respect to the modulus m.[1] In the standard notation of modular arithmetic this congruence is written as
which is the shorthand way of writing the statement that m divides (evenly) the quantity ax − 1, or, put another way, the remainder after dividing ax by the integer m is 1. If a does have an inverse modulo m, then there is an infinite number of solutions of this congruence, which form a congruence class with respect to this modulus. Furthermore, any integer that is congruent to a (i.e., in a's congruence class) has any element of x's congruence class as a modular multiplicative inverse. Using the notation of to indicate the congruence class containing w, this can be expressed by saying that the modulo multiplicative inverse of the congruence class is the congruence class such that:
where the symbol denotes the multiplication of equivalence classes modulo m.[2] Written in this way, the analogy with the usual concept of a multiplicative inverse in the set of rational or real numbers is clearly represented, replacing the numbers by congruence classes and altering the binary operation appropriately.
As with the analogous operation on the real numbers, a fundamental use of this operation is in solving, when possible, linear congruences of the form
Finding modular multiplicative inverses also has practical applications in the field of cryptography, e.g. public-key cryptography and the RSA algorithm.[3][4][5] A benefit for the computer implementation of these applications is that there exists a very fast algorithm (the extended Euclidean algorithm) that can be used for the calculation of modular multiplicative inverses.
Modular arithmetic
[edit]For a given positive integer m, two integers, a and b, are said to be congruent modulo m if m divides their difference. This binary relation is denoted by,
This is an equivalence relation on the set of integers, , and the equivalence classes are called congruence classes modulo m or residue classes modulo m. Let denote the congruence class containing the integer a,[6] then
A linear congruence is a modular congruence of the form
Unlike linear equations over the reals, linear congruences may have zero, one or several solutions. If x is a solution of a linear congruence then every element in is also a solution, so, when speaking of the number of solutions of a linear congruence we are referring to the number of different congruence classes that contain solutions.
If d is the greatest common divisor of a and m then the linear congruence ax ≡ b (mod m) has solutions if and only if d divides b. If d divides b, then there are exactly d solutions.[7]
A modular multiplicative inverse of an integer a with respect to the modulus m is a solution of the linear congruence
The previous result says that a solution exists if and only if gcd(a, m) = 1, that is, a and m must be relatively prime (i.e. coprime). Furthermore, when this condition holds, there is exactly one solution, i.e., when it exists, a modular multiplicative inverse is unique:[8] If b and b' are both modular multiplicative inverses of a respect to the modulus m, then
therefore
If a ≡ 0 (mod m), then gcd(a, m) = m, and a won't even have a modular multiplicative inverse. Therefore, b ≡ b' (mod m).
When ax ≡ 1 (mod m) has a solution it is often denoted in this way −
but this can be considered an abuse of notation since it could be misinterpreted as the reciprocal of (which, contrary to the modular multiplicative inverse, is not an integer except when a is 1 or −1). The notation would be proper if a is interpreted as a token standing for the congruence class , as the multiplicative inverse of a congruence class is a congruence class with the multiplication defined in the next section.
Integers modulo m
[edit]The congruence relation, modulo m, partitions the set of integers into m congruence classes. Operations of addition and multiplication can be defined on these m objects in the following way: To either add or multiply two congruence classes, first pick a representative (in any way) from each class, then perform the usual operation for integers on the two representatives and finally take the congruence class that the result of the integer operation lies in as the result of the operation on the congruence classes. In symbols, with and representing the operations on congruence classes, these definitions are
and
These operations are well-defined, meaning that the end result does not depend on the choices of representatives that were made to obtain the result.
The m congruence classes with these two defined operations form a ring, called the ring of integers modulo m. There are several notations used for these algebraic objects, most often or , but several elementary texts and application areas use a simplified notation when confusion with other algebraic objects is unlikely.
The congruence classes of the integers modulo m were traditionally known as residue classes modulo m, reflecting the fact that all the elements of a congruence class have the same remainder (i.e., "residue") upon being divided by m. Any set of m integers selected so that each comes from a different congruence class modulo m is called a complete system of residues modulo m.[9] The division algorithm shows that the set of integers, {0, 1, 2, ..., m − 1} form a complete system of residues modulo m, known as the least residue system modulo m. In working with arithmetic problems it is sometimes more convenient to work with a complete system of residues and use the language of congruences while at other times the point of view of the congruence classes of the ring is more useful.[10]
Multiplicative group of integers modulo m
[edit]Not every element of a complete residue system modulo m has a modular multiplicative inverse, for instance, zero never does. After removing the elements of a complete residue system that are not relatively prime to m, what is left is called a reduced residue system, all of whose elements have modular multiplicative inverses. The number of elements in a reduced residue system is , where is the Euler totient function, i.e., the number of positive integers less than m that are relatively prime to m.
In a general ring with unity not every element has a multiplicative inverse and those that do are called units. As the product of two units is a unit, the units of a ring form a group, the group of units of the ring and often denoted by R× if R is the name of the ring. The group of units of the ring of integers modulo m is called the multiplicative group of integers modulo m, and it is isomorphic to a reduced residue system. In particular, it has order (size), .
In the case that m is a prime, say p, then and all the non-zero elements of have multiplicative inverses, thus is a finite field. In this case, the multiplicative group of integers modulo p form a cyclic group of order p − 1.
Example
[edit]For any integer , it's always the case that is the modular multiplicative inverse of with respect to the modulus , since . Examples are , , and so on.
The following example uses the modulus 10: Two integers are congruent mod 10 if and only if their difference is divisible by 10, for instance
- since 10 divides 32 − 2 = 30, and
- since 10 divides 111 − 1 = 110.
Some of the ten congruence classes with respect to this modulus are:
- and
The linear congruence 4x ≡ 5 (mod 10) has no solutions since the integers that are congruent to 5 (i.e., those in ) are all odd while 4x is always even. However, the linear congruence 4x ≡ 6 (mod 10) has two solutions, namely, x = 4 and x = 9. The gcd(4, 10) = 2 and 2 does not divide 5, but does divide 6.
Since gcd(3, 10) = 1, the linear congruence 3x ≡ 1 (mod 10) will have solutions, that is, modular multiplicative inverses of 3 modulo 10 will exist. In fact, 7 satisfies this congruence (i.e., 21 − 1 = 20). However, other integers also satisfy the congruence, for instance 17 and −3 (i.e., 3(17) − 1 = 50 and 3(−3) − 1 = −10). In particular, every integer in will satisfy the congruence since these integers have the form 7 + 10r for some integer r and
is divisible by 10. This congruence has only this one congruence class of solutions. The solution in this case could have been obtained by checking all possible cases, but systematic algorithms would be needed for larger moduli and these will be given in the next section.
The product of congruence classes and can be obtained by selecting an element of , say 25, and an element of , say −2, and observing that their product (25)(−2) = −50 is in the congruence class . Thus, . Addition is defined in a similar way. The ten congruence classes together with these operations of addition and multiplication of congruence classes form the ring of integers modulo 10, i.e., .
A complete residue system modulo 10 can be the set {10, −9, 2, 13, 24, −15, 26, 37, 8, 9} where each integer is in a different congruence class modulo 10. The unique least residue system modulo 10 is {0, 1, 2, ..., 9}. A reduced residue system modulo 10 could be {1, 3, 7, 9}. The product of any two congruence classes represented by these numbers is again one of these four congruence classes. This implies that these four congruence classes form a group, in this case the cyclic group of order four, having either 3 or 7 as a (multiplicative) generator. The represented congruence classes form the group of units of the ring . These congruence classes are precisely the ones which have modular multiplicative inverses.
Computation
[edit]Extended Euclidean algorithm
[edit]A modular multiplicative inverse of a modulo m can be found by using the extended Euclidean algorithm.
The Euclidean algorithm determines the greatest common divisor (gcd) of two integers, say a and m. If a has a multiplicative inverse modulo m, this gcd must be 1. The last of several equations produced by the algorithm may be solved for this gcd. Then, using a method called "back substitution", an expression connecting the original parameters and this gcd can be obtained. In other words, integers x and y can be found to satisfy Bézout's identity,
Rewritten, this is
that is,
so, a modular multiplicative inverse of a has been calculated. A more efficient version of the algorithm is the extended Euclidean algorithm, which, by using auxiliary equations, reduces two passes through the algorithm (back substitution can be thought of as passing through the algorithm in reverse) to just one.
In big O notation, this algorithm runs in time O(log2(m)), assuming |a| < m, and is considered to be very fast and generally more efficient than its alternative, exponentiation.
Using Euler's theorem
[edit]As an alternative to the extended Euclidean algorithm, Euler's theorem may be used to compute modular inverses.[11]
According to Euler's theorem, if a is coprime to m, that is, gcd(a, m) = 1, then
where is Euler's totient function. This follows from the fact that a belongs to the multiplicative group × if and only if a is coprime to m. Therefore, a modular multiplicative inverse can be found directly:
In the special case where m is a prime, and a modular inverse is given by
This method is generally slower than the extended Euclidean algorithm, but is sometimes used when an implementation for modular exponentiation is already available. Some disadvantages of this method include:
- The value must be known and the most efficient known computation requires m's factorization. Factorization is widely believed to be a computationally hard problem. However, calculating is straightforward when the prime factorization of m is known.
- The relative cost of exponentiation. Though it can be implemented more efficiently using modular exponentiation, when large values of m are involved this is most efficiently computed with the Montgomery reduction method, that method, itself, requiring a modular inverse mod m, which is what was to be calculated in the first place. Without the Montgomery method, the standard binary exponentiation, which requires division mod m at every step, is a slow operation when m is large.
One notable advantage of this technique is that there are no conditional branches which depend on the value of a, and thus the value of a, which may be an important secret in public-key cryptography, can be protected from side-channel attacks. For this reason, the standard implementation of Curve25519 uses this technique to compute an inverse.
Multiple inverses
[edit]It is possible to compute the inverse of multiple numbers ai, modulo a common m, with a single invocation of the Euclidean algorithm and three multiplications per additional input.[12] The basic idea is to form the product of all the ai, invert that, then multiply by aj for all j ≠ i to leave only the desired a−1
i.
More specifically, the algorithm is (all arithmetic performed modulo m):
- Compute the prefix products for all i ≤ n.
- Compute b−1
n using any available algorithm. - For i from n down to 2, compute
- a−1
i = b−1
ibi−1 and - b−1
i−1 = b−1
iai.
- a−1
- Finally, a−1
1 = b−1
1.
It is possible to perform the multiplications in a tree structure rather than linearly to exploit parallel computing.
Inverses modulo prime powers (including powers of 2)
[edit]In the case where the modulus m is on the form for some prime number p and positive integer m, it is possible to compute modular multiplicative inverses efficiently by using the Newton-Raphson iteration, allowing the inverse to be computed with multiplications. It can be shown that if
(that is, x is a modular multiplicative inverse of a modulo ), then
which means that it is possible to perform a modulo calculation by first computing a modular multiplicative inverse of a modulo the prime number p or a small power thereof, then performing a series of Newton-Raphson iterations to compute the inverse modulo progressively larger prime powers for progressively larger values of n.[13][14]
A practical use of this method is to efficiently compute modular multiplicative inverses modulo powers of 2. For such a calculation, one could start with noting that all odd integers are their own modular multiplicative inverses modulo , as can be shown by inspection:
,
,
,
,
and then iteratively use Newton-Raphson iterations to compute the modular inverse modulo , , and so on.
For example, in the C programming language, where additions, subtractions and multiplications on the uint64_t data type are all done modulo , one could compute the modular multiplicative inverse of an odd integer a modulo using the following function that performs five Newton-Raphson iterations:
#include <stdint.h>
uint64_t modinv64( uint64_t a ) {
uint64_t x = a;
for( int i=0; i<5; i++ ) x *= 2 - a*x;
return x;
}
Depending on application and platform, it may make sense to optimize this routine further — e.g. the first few iterations can be skipped by using a lookup table to provide an inverse modulo a larger power-of-2; also, on some systems, 32-bit multiplications may be faster than 64-bit multiplications, in which case some speedup can be obtained by only using 32-bit multiplications until an inverse modulo has been obtained and then switch to 64-bit multiplications only after that. Applying such optimizations, a C routine that computes a modular multiplicative inverse modulo becomes:
#include <stdint.h>
uint64_t modinv64( uint64_t a ) {
static const uint8_t tbl[256] = {
0,1,0,171,0,205,0,183,0,57,0,163,0,197,0,239,
0,241,0,27,0,61,0,167,0,41,0,19,0,53,0,223,
0,225,0,139,0,173,0,151,0,25,0,131,0,165,0,207,
0,209,0,251,0,29,0,135,0,9,0,243,0,21,0,191,
0,193,0,107,0,141,0,119,0,249,0,99,0,133,0,175,
0,177,0,219,0,253,0,103,0,233,0,211,0,245,0,159,
0,161,0,75,0,109,0,87,0,217,0,67,0,101,0,143,
0,145,0,187,0,221,0,71,0,201,0,179,0,213,0,127,
0,129,0,43,0,77,0,55,0,185,0,35,0,69,0,111,
0,113,0,155,0,189,0,39,0,169,0,147,0,181,0,95,
0,97,0,11,0,45,0,23,0,153,0,3,0,37,0,79,
0,81,0,123,0,157,0,7,0,137,0,115,0,149,0,63,
0,65,0,235,0,13,0,247,0,121,0,227,0,5,0,47,
0,49,0,91,0,125,0,231,0,105,0,83,0,117,0,31,
0,33,0,203,0,237,0,215,0,89,0,195,0,229,0,15,
0,17,0,59,0,93,0,199,0,73,0,51,0,85,0,255 };
uint32_t a32 = (uint32_t)a;
uint32_t x = tbl[a & 0xFF]; // inverse modulo 2^8
x *= 2 - a32*x; // 32-bit multiplications
x *= 2 - a32*x; // 32-bit multiplications
return x * (2 - a*x); // 64-bit multiplications
}
Applications
[edit]Finding a modular multiplicative inverse has many applications in algorithms that rely on the theory of modular arithmetic. For instance, in cryptography the use of modular arithmetic permits some operations to be carried out more quickly and with fewer storage requirements, while other operations become more difficult.[15] Both of these features can be used to advantage. In particular, in the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. One of these numbers is made public and can be used in a rapid encryption procedure, while the other, used in the decryption procedure, is kept hidden. Determining the hidden number from the public number is considered to be computationally infeasible and this is what makes the system work to ensure privacy.[16]
As another example in a different context, consider the exact division problem in computer science where you have a list of odd word-sized numbers each divisible by k and you wish to divide them all by k. One solution is as follows:
- Use the extended Euclidean algorithm to compute k−1, the modular multiplicative inverse of k mod 2w, where w is the number of bits in a word. This inverse will exist since the numbers are odd and the modulus has no odd factors.
- For each number in the list, multiply it by k−1 and take the least significant word of the result.
On many machines, particularly those without hardware support for division, division is a slower operation than multiplication, so this approach can yield a considerable speedup. The first step is relatively slow but only needs to be done once.
Modular multiplicative inverses are used to obtain a solution of a system of linear congruences that is guaranteed by the Chinese Remainder Theorem.
For example, the system
- X ≡ 4 (mod 5)
- X ≡ 4 (mod 7)
- X ≡ 6 (mod 11)
has common solutions since 5,7 and 11 are pairwise coprime. A solution is given by
- X = t1 (7 × 11) × 4 + t2 (5 × 11) × 4 + t3 (5 × 7) × 6
where
- t1 = 3 is the modular multiplicative inverse of 7 × 11 (mod 5),
- t2 = 6 is the modular multiplicative inverse of 5 × 11 (mod 7) and
- t3 = 6 is the modular multiplicative inverse of 5 × 7 (mod 11).
Thus,
- X = 3 × (7 × 11) × 4 + 6 × (5 × 11) × 4 + 6 × (5 × 7) × 6 = 3504
and in its unique reduced form
- X ≡ 3504 ≡ 39 (mod 385)
since 385 is the LCM of 5,7 and 11.
Also, the modular multiplicative inverse figures prominently in the definition of the Kloosterman sum.
See also
[edit]- Inversive congruential generator – a pseudo-random number generator that uses modular multiplicative inverses
- Rational reconstruction (mathematics)
Notes
[edit]- ^ Rosen 1993, p. 132.
- ^ Schumacher 1996, p. 88.
- ^ Stinson, Douglas R. (1995), Cryptography / Theory and Practice, CRC Press, pp. 124–128, ISBN 0-8493-8521-0
- ^ Trappe & Washington 2006, pp. 164−169.
- ^ Moriarty, K.; Kaliski, B.; Jonsson, J.; Rusch, A. (2016). PKCS #1: RSA Cryptography Specifications. sec. 2.2. doi:10.17487/RFC8017. RFC 8017. Retrieved January 21, 2017.
- ^ Other notations are often used, including [a] and [a]m.
- ^ Ireland & Rosen 1990, p. 32
- ^ Shoup, Victor (2005), A Computational Introduction to Number Theory and Algebra, Cambridge University Press, Theorem 2.4, p. 15, ISBN 9780521851541
- ^ Rosen 1993, p. 121
- ^ Ireland & Rosen 1990, p. 31
- ^ Thomas Koshy. Elementary number theory with applications, 2nd edition. ISBN 978-0-12-372487-8. P. 346.
- ^ Brent, Richard P.; Zimmermann, Paul (December 2010). "§2.5.1 Several inversions at once" (PDF). Modern Computer Arithmetic. Cambridge Monographs on Computational and Applied Mathematics. Vol. 18. Cambridge University Press. pp. 67–68. ISBN 978-0-521-19469-3.
- ^ Jean-Guillaume Dumas, On Newton-Raphson iteration for multiplicative inverses modulo prime powers, 22 Apr 2019, p.6
- ^ Cryptography StackExchange, How to determine the multiplicative inverse modulo 64 (or other power of two)?, 17 May 2017.
- ^ Trappe & Washington 2006, p. 167
- ^ Trappe & Washington 2006, p. 165
References
[edit]- Ireland, Kenneth; Rosen, Michael (1990), A Classical Introduction to Modern Number Theory (2nd ed.), Springer-Verlag, ISBN 0-387-97329-X
- Rosen, Kenneth H. (1993), Elementary Number Theory and its Applications (3rd ed.), Addison-Wesley, ISBN 978-0-201-57889-8
- Schumacher, Carol (1996). Chapter Zero: Fundamental Notions of Abstract Mathematics. Addison-Wesley. ISBN 0-201-82653-4.
- Trappe, Wade; Washington, Lawrence C. (2006), Introduction to Cryptography with Coding Theory (2nd ed.), Prentice-Hall, ISBN 978-0-13-186239-5
External links
[edit]- Weisstein, Eric W. "Modular Inverse". MathWorld.
- Guevara Vasquez, Fernando provides a solved example of solving the modulo multiplicative inverse using Euclid's Algorithm
- Integer multiplicative inverse via Newton's method provides fast algorithms to compute multiplicative inverses modulo powers of 2.
Modular multiplicative inverse
View on GrokipediaModular Arithmetic Basics
Congruences and Equivalence Classes
In modular arithmetic, two integers and are said to be congruent modulo , where is a positive integer, denoted , if divides the difference .[9] This relation is an equivalence relation on the set of integers, satisfying reflexivity (), symmetry (if , then ), and transitivity (if and , then ).[9] The equivalence relation partitions the integers into equivalence classes, known as residue classes modulo . The residue class of modulo , denoted , consists of all integers such that , or equivalently, . There are exactly distinct residue classes modulo , typically represented by the integers .[10] Congruences support basic arithmetic operations: if and , then and . These operations are well-defined on the set of residue classes, allowing arithmetic to be performed modulo without altering equivalence.[11] The set of residue classes modulo , denoted , forms a commutative ring under these addition and multiplication operations, with the additive identity and multiplicative identity .[12]Greatest Common Divisor in Modular Contexts
In modular arithmetic, the greatest common divisor (GCD) of two integers and , where , plays a crucial role in determining the structure of solutions to equations within the ring of integers modulo . The Euclidean algorithm provides an efficient method to compute by leveraging the division algorithm repeatedly. Specifically, the algorithm states that , and it proceeds by replacing with and with the remainder until the remainder is zero; the last non-zero remainder is the GCD.[13] This process terminates because the remainders form a strictly decreasing sequence of non-negative integers. If the algorithm terminates with a GCD of 1, then and are coprime.[14] For an illustrative example, consider computing : The last non-zero remainder is 1, so , indicating that 15 and 28 are coprime.[15] Bézout's identity further connects the GCD to linear combinations: if , then there exist integers and such that . Moreover, the set of all integer linear combinations of and consists precisely of the multiples of .[16] A proof sketch relies on the Euclidean algorithm's steps, using back-substitution to express the GCD as such a combination. Starting from the base case where the remainder is (with previous remainder 0), substitute backwards through the division steps. For instance, in the example above for : Thus, and satisfy . This back-substitution works generally because each remainder is a linear combination of the originals, propagating the expression upwards.[16][17] Two integers and are defined as coprime if . In the context of congruences, which equate integers differing by multiples of the modulus, coprimality has significant implications for solvability. Specifically, the linear congruence is solvable for if and only if divides ; when , the congruence is always solvable for any , with exactly one solution modulo .[18] This condition ensures that the coefficient generates all residue classes modulo when coprime, facilitating unique solutions in modular equations.Definition and Conditions
Formal Definition
In modular arithmetic, the modular multiplicative inverse of an integer modulo , where is a positive integer, is an integer satisfying the congruence .[19] This is commonly denoted as .[19] Algebraically, this concept corresponds to the multiplicative inverse in the ring of integers modulo , where and its inverse are elements whose product is the multiplicative identity in the ring.[20] The relation is symmetric: if is the modular multiplicative inverse of modulo , then is the modular multiplicative inverse of modulo .[4]Existence Criteria
The existence of a modular multiplicative inverse for an integer modulo is determined by a fundamental condition in number theory: such an inverse exists if and only if . This criterion ensures that and are coprime, meaning they share no common prime factors other than 1. When this holds, there exists an integer such that .[21][22] To establish this theorem, consider the necessity first. Suppose an inverse exists, so , which implies for some integer . Any common divisor of and must divide the left side , hence it divides 1, forcing . For sufficiency, if , Bézout's identity guarantees integers and such that , or equivalently, . Thus, serves as the inverse.[21][23] If , no inverse exists. In this case, divides , so for any integer , . However, since does not divide 1, , and thus because is a multiple of . This impossibility arises because the equation would require consistency modulo every divisor of , including .[24][25] This existence criterion extends to the broader solvability of linear congruences. The equation has solutions if and only if divides . When , this reduces to the inverse case, but the condition allows for solutions even when , provided the gcd divides , with the number of distinct solutions modulo then equaling the gcd value.[25][26]Properties and Uniqueness
Uniqueness of Inverses
When a modular multiplicative inverse exists for an integer modulo , it is unique modulo . Specifically, if and , then .[27] To prove this, subtract the congruences to obtain , meaning divides . Since the existence of the inverse requires , it follows that divides , so .[27] This uniqueness holds under the existence criterion that . The modular inverses modulo exhibit several basic properties. First, the set of integers coprime to (those possessing inverses) is closed under multiplication modulo : if and , then .[28] Second, the inverse of an inverse recovers the original element: if is the inverse of modulo , then the inverse of is modulo . This follows because and, by commutativity of multiplication, , so satisfies the defining congruence for the inverse of .[27] Finally, the inverse of the negation modulo is the negation of the inverse of : if is the inverse of , then is the inverse of , since .[27]Connection to Units in Rings
In the ring , the units are the residue classes $$ where , as these are precisely the elements that admit multiplicative inverses under the ring's multiplication operation.[29] These units form a multiplicative subgroup known as the group of units, denoted or , consisting of all such invertible elements closed under multiplication modulo .[30] The group is abelian, reflecting the commutative nature of multiplication in .[31] Its order is given by Euler's totient function , which counts the number of integers from 1 to coprime to .[30] Within this group structure, the modular multiplicative inverse of an element coincides with its group inverse , satisfying \cdot [a^{-1}] = {{grok:render&&&type=render_inline_citation&&&citation_id=1&&&citation_type=wikipedia}} in . As a consequence of group theory, this inverse is unique for each unit.[32] A key structural property arises from the Chinese Remainder Theorem: when with , the ring isomorphism induces a group isomorphism .[33] This decomposition allows inverses modulo to be computed via separate inverses modulo and , combined using the theorem's reconstruction mechanism.[33]Computation Techniques
Extended Euclidean Algorithm
The extended Euclidean algorithm provides an efficient method to compute the modular multiplicative inverse of an integer modulo , assuming . This algorithm extends the standard Euclidean algorithm for finding the greatest common divisor by also determining integers and such that , based on Bézout's identity.[34] When , the value serves as the modular inverse, since .[5] The algorithm proceeds in two phases. First, apply the Euclidean algorithm to compute through successive divisions: divide by to get quotient and remainder , then by to get and , and continue until the remainder is 1 (since the gcd is 1). This generates a sequence of equations expressing each remainder as a linear combination of and . In the second phase, back-substitute starting from (the last non-zero remainder) upwards, substituting previous expressions to solve for and . The coefficients are updated iteratively: initialize , , then for each step , , where is the quotient at that step; the final gives .[35][36] Consider the example of finding the inverse of 5 modulo 17. Apply the Euclidean algorithm:,
,
.
Thus, . Now back-substitute:
,
but , so
.
Hence, and , and , confirming 7 as the inverse.[34][5] The following pseudocode implements the recursive version, returning the gcd , , and :
function extended_gcd(a, n):
if n == 0:
return a, 1, 0
d, s1, t1 = extended_gcd(n, a % n)
s = t1
t = s1 - (a // n) * t1
return d, s, t
function extended_gcd(a, n):
if n == 0:
return a, 1, 0
d, s1, t1 = extended_gcd(n, a % n)
s = t1
t = s1 - (a // n) * t1
return d, s, t
extended_gcd(a, n); if , the inverse is .[36]
The time complexity of the extended Euclidean algorithm is , matching the standard Euclidean algorithm due to the logarithmic number of division steps.[37][38]
Euler's Theorem Method
Euler's theorem provides a method for computing the modular multiplicative inverse of an integer modulo when . The theorem states that if and are coprime, then , where is Euler's totient function.[39] From this congruence, it follows that , since multiplying both sides by (which exists under the coprimality condition) yields the identity.[19] To apply this method, first compute , defined as , where the product is over the distinct prime factors of .[40] Then, calculate using efficient modular exponentiation algorithms, such as binary exponentiation, which reduces the computational complexity to multiplications modulo .[19] This approach leverages the group structure of the units modulo , whose order is , but focuses practically on exponentiation rather than group-theoretic operations.[41] For example, consider finding the inverse of 3 modulo 10. Since and , compute . Verifying, , confirming that 7 is the inverse.[40][19] While theoretically elegant due to its connection to the totient function, this method can be inefficient for large without optimized exponentiation, as may still be substantial and require factorization of upfront.[41] It remains valuable for theoretical analysis and cases where exponentiation is preferred over other techniques.[42]Illustrative Examples
Basic Numerical Examples
To illustrate the concept of a modular multiplicative inverse, consider the case of finding the inverse of 7 modulo 26. Since , an inverse exists, and using the extended Euclidean algorithm yields 15 as the solution, as .[43] To verify, compute the product and reduce: remainder 1, confirming . In contrast, the integer 4 has no modular multiplicative inverse modulo 6, because . This violates the existence condition, rendering the congruence unsolvable for any integer , as the greatest common divisor does not divide 1.[4] For small moduli, the inverses of numbers coprime to the modulus can be tabulated systematically. The following table lists the modular multiplicative inverses modulo 5 (where for ):| Verification | ||
|---|---|---|
| 1 | 1 | |
| 2 | 3 | |
| 3 | 2 | |
| 4 | 4 |
