In
arithmetic
Arithmetic is an elementary branch of mathematics that deals with numerical operations like addition, subtraction, multiplication, and division. In a wider sense, it also includes exponentiation, extraction of roots, and taking logarithms.
...
and
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, the extended Euclidean algorithm is an extension to the
Euclidean algorithm
In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's ''Topics in Algebra'' and Serge Lang's ''Algebra'', use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is a ...
, and computes, in addition to the
greatest common divisor
In mathematics, the greatest common divisor (GCD), also known as greatest common factor (GCF), of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For two integers , , the greatest co ...
(gcd) of integers ''a'' and ''b'', also the coefficients of
Bézout's identity, which are integers ''x'' and ''y'' such that
:
This is a
certifying algorithm, because the gcd is the only number that can simultaneously satisfy this equation and divide the inputs.
It allows one to compute also, with almost no extra cost, the quotients of ''a'' and ''b'' by their greatest common divisor.
also refers to a
very similar algorithm for computing the
polynomial greatest common divisor and the coefficients of Bézout's identity of two
univariate polynomials.
The extended Euclidean algorithm is particularly useful when ''a'' and ''b'' are
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
. With that provision, ''x'' is the
modular multiplicative inverse
In mathematics, particularly in the area of arithmetic, a modular multiplicative inverse of an integer is an integer such that the product is congruent to 1 with respect to the modulus .. In the standard notation of modular arithmetic this cong ...
of ''a''
modulo
In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the '' modulus'' of the operation.
Given two positive numbers and , mo ...
''b'', and ''y'' is the modular multiplicative inverse of ''b'' modulo ''a''. Similarly, the polynomial extended Euclidean algorithm allows one to compute the
multiplicative inverse
In mathematics, a multiplicative inverse or reciprocal for a number ''x'', denoted by 1/''x'' or ''x''−1, is a number which when Multiplication, multiplied by ''x'' yields the multiplicative identity, 1. The multiplicative inverse of a ra ...
in
algebraic field extensions and, in particular in
finite field
In mathematics, a finite field or Galois field (so-named in honor of Évariste Galois) is a field (mathematics), field that contains a finite number of Element (mathematics), elements. As with any field, a finite field is a Set (mathematics), s ...
s of non prime order. It follows that both extended Euclidean algorithms are widely used in
cryptography
Cryptography, or cryptology (from "hidden, secret"; and ''graphein'', "to write", or ''-logy, -logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of Adversary (cryptography), ...
. In particular, the computation of the
modular multiplicative inverse
In mathematics, particularly in the area of arithmetic, a modular multiplicative inverse of an integer is an integer such that the product is congruent to 1 with respect to the modulus .. In the standard notation of modular arithmetic this cong ...
is an essential step in the derivation of key-pairs in the
RSA public-key encryption method.
Description
The standard Euclidean algorithm proceeds by a succession of
Euclidean divisions whose quotients are not used. Only the ''remainders'' are kept. For the extended algorithm, the successive quotients are used. More precisely, the standard Euclidean algorithm with ''a'' and ''b'' as input, consists of computing a sequence
of quotients and a sequence
of remainders such that
:
It is the main property of
Euclidean division that the inequalities on the right define uniquely
and
from
and
The computation stops when one reaches a remainder
which is zero; the greatest common divisor is then the last non zero remainder
The extended Euclidean algorithm proceeds similarly, but adds two other sequences, as follows
:
The computation also stops when
and gives
*
is the greatest common divisor of the input
and
* The Bézout coefficients are
and
that is
* The quotients of ''a'' and ''b'' by their greatest common divisor are given by
and
Moreover, if ''a'' and ''b'' are both positive and
, then
:
for
where
denotes the
integral part
In mathematics, an integral is the continuous analog of a sum, which is used to calculate areas, volumes, and their generalizations. Integration, the process of computing an integral, is one of the two fundamental operations of calculus,Int ...
of , that is the greatest integer not greater than .
This implies that the pair of Bézout's coefficients provided by the extended Euclidean algorithm is the ''minimal pair'' of Bézout coefficients, as being the unique pair satisfying both above inequalities.
It also means that the algorithm can be done without
integer overflow by a
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
using integers of a fixed size that is larger than that of ''a'' and ''b''.
Example
The following table shows how the extended Euclidean algorithm proceeds with input and . The greatest common divisor is the last non zero entry, in the column "remainder". The computation stops at row 6, because the remainder in it is . Bézout coefficients appear in the last two columns of the second-to-last row. In fact, it is easy to verify that . Finally the last two entries and of the last row are, up to the sign, the quotients of the input and by the greatest common divisor .
Proof
As
the sequence of the
is a decreasing sequence of nonnegative integers (from ''i'' = 2 on). Thus it must stop with some
This proves that the algorithm stops eventually.
As
the greatest common divisor is the same for
and
This shows that the greatest common divisor of the input
is the same as that of
This proves that
is the greatest common divisor of ''a'' and ''b''. (Until this point, the proof is the same as that of the classical Euclidean algorithm.)
As
and
we have
for ''i'' = 0 and 1. The relation follows by induction for all
:
Thus
and
are Bézout coefficients.
Consider the matrix
The recurrence relation may be rewritten in matrix form
The matrix
is the identity matrix and its determinant is one. The determinant of the rightmost matrix in the preceding formula is −1. It follows that the determinant of
is
In particular, for
we have
Viewing this as a Bézout's identity, this shows that
and
are
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
. The relation
that has been proved above and
Euclid's lemma
In algebra and number theory, Euclid's lemma is a lemma that captures a fundamental property of prime numbers:
For example, if , , , then , and since this is divisible by 19, the lemma implies that one or both of 133 or 143 must be as well. In ...
show that
divides , that is that
for some integer . Dividing by
the relation
gives
So,
and
are coprime integers that are the quotients of and by a common factor, which is thus their greatest common divisor or its
opposite.
To prove the last assertion, assume that ''a'' and ''b'' are both positive and
. Then,
, and if
, it can be seen that the ''s'' and ''t'' sequences for (''a'',''b'') under the EEA are, up to initial 0s and 1s, the ''t'' and ''s'' sequences for (''b'',''a''). The definitions then show that the (''a'',''b'') case reduces to the (''b'',''a'') case. So assume that
without loss of generality.
It can be seen that
is 1 and
(which exists by
) is a negative integer. Thereafter, the
alternate in sign and strictly increase in magnitude, which follows inductively from the definitions and the fact that
for
, the case
holds because
. The same is true for the
after the first few terms, for the same reason. Furthermore, it is easy to see that
(when ''a'' and ''b'' are both positive and
). Thus, noticing that
, we obtain
This, accompanied by the fact that
are larger than or equal to in absolute value than any previous
or
respectively completed the proof.
Polynomial extended Euclidean algorithm
For
univariate polynomials with coefficients in a
field, everything works similarly, Euclidean division, Bézout's identity and extended Euclidean algorithm. The first difference is that, in the Euclidean division and the algorithm, the inequality
has to be replaced by an inequality on the degrees
Otherwise, everything which precedes in this article remains the same, simply by replacing integers by polynomials.
A second difference lies in the bound on the size of the Bézout coefficients provided by the extended Euclidean algorithm, which is more accurate in the polynomial case, leading to the following theorem.
''If a and b are two nonzero polynomials, then the extended Euclidean algorithm produces the unique pair of polynomials'' (''s'', ''t'') ''such that''
:
''and''
:
A third difference is that, in the polynomial case, the greatest common divisor is defined only up to the multiplication by a non zero constant. There are several ways to define unambiguously a greatest common divisor.
In mathematics, it is common to require that the greatest common divisor be a
monic polynomial
In algebra, a monic polynomial is a non-zero univariate polynomial (that is, a polynomial in a single variable) in which the leading coefficient (the nonzero coefficient of highest degree) is equal to 1. That is to say, a monic polynomial is one ...
. To get this, it suffices to divide every element of the output by the
leading coefficient of
This allows that, if ''a'' and ''b'' are coprime, one gets 1 in the right-hand side of Bézout's inequality. Otherwise, one may get any non-zero constant. In
computer algebra
In mathematics and computer science, computer algebra, also called symbolic computation or algebraic computation, is a scientific area that refers to the study and development of algorithms and software for manipulating expression (mathematics), ...
, the polynomials commonly have integer coefficients, and this way of normalizing the greatest common divisor introduces too many fractions to be convenient.
The second way to normalize the greatest common divisor in the case of polynomials with integer coefficients is to divide every output by the
content of
to get a
primitive greatest common divisor. If the input polynomials are coprime, this normalisation also provides a greatest common divisor equal to 1. The drawback of this approach is that a lot of fractions should be computed and simplified during the computation.
A third approach consists in extending the algorithm of
subresultant pseudo-remainder sequences in a way that is similar to the extension of the Euclidean algorithm to the extended Euclidean algorithm. This allows that, when starting with polynomials with integer coefficients, all polynomials that are computed have integer coefficients. Moreover, every computed remainder
is a
subresultant polynomial. In particular, if the input polynomials are coprime, then the Bézout's identity becomes
:
where
denotes the
resultant of ''a'' and ''b''. In this form of Bézout's identity, there is no denominator in the formula. If one divides everything by the resultant one gets the classical Bézout's identity, with an explicit common denominator for the rational numbers that appear in it.
Pseudocode
To implement the algorithm that is described above, one should first remark that only the two last values of the indexed variables are needed at each step. Thus, for saving memory, each indexed variable must be replaced by just two variables.
For simplicity, the following algorithm (and the other algorithms in this article) uses
parallel assignments. In a programming language which does not have this feature, the parallel assignments need to be simulated with an auxiliary variable. For example, the first one,
(old_r, r) := (r, old_r - quotient * r)
is equivalent to
prov := r;
r := old_r - quotient × prov;
old_r := prov;
and similarly for the other parallel assignments.
This leads to the following code:
function extended_gcd(a, b)
(old_r, r) := (a, b)
(old_s, s) := (1, 0)
(old_t, t) := (0, 1)
while r ≠ 0 do
quotient := old_r div r
(old_r, r) := (r, old_r − quotient × r)
(old_s, s) := (s, old_s − quotient × s)
(old_t, t) := (t, old_t − quotient × t)
output "Bézout coefficients:", (old_s, old_t)
output "greatest common divisor:", old_r
output "quotients by the gcd:", (t, s)
The quotients of ''a'' and ''b'' by their greatest common divisor, which is output, may have an incorrect sign. This is easy to correct at the end of the computation but has not been done here for simplifying the code. Similarly, if either ''a'' or ''b'' is zero and the other is negative, the greatest common divisor that is output is negative, and all the signs of the output must be changed.
Finally, notice that in Bézout's identity,
, one can solve for
given
. Thus, an optimization to the above algorithm is to compute only the
sequence (which yields the Bézout coefficient
), and then compute
at the end:
function extended_gcd(a, b)
s := 0; old_s := 1
r := b; old_r := a
while r ≠ 0 do
quotient := old_r div r
(old_r, r) := (r, old_r − quotient × r)
(old_s, s) := (s, old_s − quotient × s)
if b ≠ 0 then
bezout_t := (old_r − old_s × a) div b
else
bezout_t := 0
output "Bézout coefficients:", (old_s, bezout_t)
output "greatest common divisor:", old_r
However, in many cases this is not really an optimization: whereas the former algorithm is not susceptible to overflow when used with machine integers (that is, integers with a fixed upper bound of digits), the multiplication of ''old_s * a'' in computation of ''bezout_t'' can overflow, limiting this optimization to inputs which can be represented in less than half the maximal size. When using integers of unbounded size, the time needed for multiplication and division grows quadratically with the size of the integers. This implies that the "optimisation" replaces a sequence of multiplications/divisions of small integers by a single multiplication/division, which requires more computing time than the operations that it replaces, taken together.
Simplification of fractions
A fraction is in canonical simplified form if and are
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
and is positive. This canonical simplified form can be obtained by replacing the three output lines of the preceding pseudo code by
if then output "Division by zero"
if then ; (''for avoiding negative denominators'')
if then output (''for avoiding denominators equal to'' 1)
output
The proof of this algorithm relies on the fact that and are two coprime integers such that , and thus
. To get the canonical simplified form, it suffices to move the minus sign for having a positive denominator.
If divides evenly, the algorithm executes only one iteration, and we have at the end of the algorithm. It is the only case where the output is an integer.
Computing multiplicative inverses in modular structures
The extended Euclidean algorithm is the essential tool for computing
multiplicative inverse
In mathematics, a multiplicative inverse or reciprocal for a number ''x'', denoted by 1/''x'' or ''x''−1, is a number which when Multiplication, multiplied by ''x'' yields the multiplicative identity, 1. The multiplicative inverse of a ra ...
s in modular structures, typically the
modular integers and the
algebraic field extensions. A notable instance of the latter case are the finite fields of non-prime order.
Modular integers
If is a positive integer, the
ring may be identified with the set of the remainders of
Euclidean division by , the addition and the multiplication consisting in taking the remainder by of the result of the addition and the multiplication of integers. An element of has a multiplicative inverse (that is, it is a
unit) if it is
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
to . In particular, if is
prime, has a multiplicative inverse if it is not zero (modulo ). Thus is a field if and only if is prime.
Bézout's identity asserts that and are coprime if and only if there exist integers and such that
:
Reducing this identity modulo gives
:
Thus , or, more exactly, the remainder of the division of by , is the multiplicative inverse of modulo .
To adapt the extended Euclidean algorithm to this problem, one should remark that the Bézout coefficient of is not needed, and thus does not need to be computed. Also, for getting a result which is positive and lower than ''n'', one may use the fact that the integer provided by the algorithm satisfies . That is, if , one must add to it at the end. This results in the
pseudocode, in which the input ''n'' is an integer larger than 1.
function inverse(a, n)
t := 0; newt := 1
r := n; newr := a
while newr ≠ 0 do
quotient := r div newr
(t, newt) := (newt, t − quotient × newt)
(r, newr) := (newr, r − quotient × newr)
if r > 1 then
return "a is not invertible"
if t < 0 then
t := t + n
return t
Simple algebraic field extensions
The extended Euclidean algorithm is also the main tool for computing
multiplicative inverse
In mathematics, a multiplicative inverse or reciprocal for a number ''x'', denoted by 1/''x'' or ''x''−1, is a number which when Multiplication, multiplied by ''x'' yields the multiplicative identity, 1. The multiplicative inverse of a ra ...
s in
simple algebraic field extensions. An important case, widely used in
cryptography
Cryptography, or cryptology (from "hidden, secret"; and ''graphein'', "to write", or ''-logy, -logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of Adversary (cryptography), ...
and
coding theory
Coding theory is the study of the properties of codes and their respective fitness for specific applications. Codes are used for data compression, cryptography, error detection and correction, data transmission and computer data storage, data sto ...
, is that of
finite field
In mathematics, a finite field or Galois field (so-named in honor of Évariste Galois) is a field (mathematics), field that contains a finite number of Element (mathematics), elements. As with any field, a finite field is a Set (mathematics), s ...
s of non-prime order. In fact, if is a prime number, and , the field of order is a simple algebraic extension of the
prime field of elements, generated by a root of an
irreducible polynomial of degree .
A simple algebraic extension of a field , generated by the root of an irreducible polynomial of degree may be identified to the
quotient ring , and its elements are in
bijective correspondence with the polynomials of degree less than . The addition in is the addition of polynomials. The multiplication in is the remainder of the
Euclidean division by of the product of polynomials. Thus, to complete the arithmetic in , it remains only to define how to compute multiplicative inverses. This is done by the extended Euclidean algorithm.
The algorithm is very similar to that provided above for computing the modular multiplicative inverse. There are two main differences: firstly the last but one line is not needed, because the Bézout coefficient that is provided always has a degree less than . Secondly, the greatest common divisor which is provided, when the input polynomials are coprime, may be any non zero elements of ; this Bézout coefficient (a polynomial generally of positive degree) has thus to be multiplied by the inverse of this element of . In the pseudocode which follows, is a polynomial of degree greater than one, and is a polynomial.
function inverse(a, p)
t := 0; newt := 1
r := p; newr := a
while newr ≠ 0 do
quotient := r div newr
(r, newr) := (newr, r − quotient × newr)
(t, newt) := (newt, t − quotient × newt)
if degree(r) > 0 then
return "Either p is not irreducible or a is a multiple of p"
return (1/r) × t
Example
For example, if the polynomial used to define the finite field GF(2
8) is , and is the element whose inverse is desired, then performing the algorithm results in the computation described in the following table. Let us recall that in fields of order 2
''n'', one has −''z'' = ''z'' and ''z'' + ''z'' = 0 for every element ''z'' in the field). Since 1 is the only nonzero element of GF(2), the adjustment in the last line of the pseudocode is not needed.
Thus, the inverse is , as can be confirmed by
multiplying the two elements together, and taking the remainder by of the result.
The case of more than two numbers
One can handle the case of more than two numbers iteratively. First we show that
. To prove this let
. By definition of gcd
is a divisor of
and
. Thus
for some
. Similarly
is a divisor of
so
for some
. Let
. By our construction of
,
but since
is the greatest divisor
is a
unit. And since
the result is proven.
So if
then there are
and
such that
so the final equation will be
:
So then to apply to ''n'' numbers we use induction
:
with the equations following directly.
See also
*
Euclidean domain
*
Linear congruence theorem
*
Kuṭṭaka
References
* Volume 2, Chapter 4.
*
Thomas H. Cormen,
Charles E. Leiserson,
Ronald L. Rivest, and
Clifford Stein. ''
Introduction to Algorithms
''Introduction to Algorithms'' is a book on computer programming by Thomas H. Cormen, Charles E. Leiserson, Ron Rivest, Ronald L. Rivest, and Clifford Stein. The book is described by its publisher as "the leading algorithms text in universities w ...
'', Second Edition. MIT Press and McGraw-Hill, 2001. . Pages 859–861 of section 31.2: Greatest common divisor.
External links
Source for the form of the algorithm used to determine the multiplicative inverse in GF(2^8)
{{number theoretic algorithms
Number theoretic algorithms
Articles with example pseudocode
Euclid