10.12.2012 Views

Prime Numbers

Prime Numbers

Prime Numbers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9.4 Enhancements for gcd and inverse 463<br />

so the fact of a “stable” value for x really can yield high efficiency, because of<br />

the (lg B) −1 factor. Depending on precise practical setting and requirements,<br />

there exist yet further enhancements, including the use of less extensive<br />

lookup tables (i.e., using only the stored powers such as xBj ), loosening of<br />

the restrictions on the ranges of the for() loops depending on the range of<br />

values of the y digits in base B (in some situations not every possible digit<br />

will occur), and so on. Note that if we do store only the reduced set of powers<br />

xBj , the Step [Loop over digits] will have nested for() loops. There also exist<br />

fixed-y algorithms using so-called addition chains, so that when the exponent<br />

is stable some enhancements are possible. Both fixed-x and fixed-y forms<br />

find applications in cryptography. If public keys are generated as fixed x<br />

values raised to secret y values, for example, the fixed-x enhancements can be<br />

beneficial. Similarly, if a public key (as x = gh ) is to be raised often to a key<br />

power y, then the fixed-y methods may be invoked for extra efficiency.<br />

9.4 Enhancements for gcd and inverse<br />

In Section 2.1.1 we discussed the great classical algorithms for gcd and inverse.<br />

Here we explore more modern methods, especially methods that apply when<br />

the relevant integers are very large, or when some operations (such as shifts)<br />

are relatively efficient.<br />

9.4.1 Binary gcd algorithms<br />

There is a genuine enhancement of the Euclid algorithm worked out by<br />

D. Lehmer in the 1930s. The method exploits the fact that not every implied<br />

division in the Euclid loop requires full precision, and statistically speaking<br />

there will be many single-precision (i.e., small operand) div operations. We<br />

do not lay out the Lehmer method here (for details see [Knuth 1981]), but<br />

observe that Lehmer showed how to enhance an old algorithm to advantage<br />

in such tasks as factorization.<br />

In the 1960s it was observed by R. Silver and J. Terzian [Knuth 1981], and<br />

independently in [Stein 1967], that a gcd algorithm can be effected in a certain<br />

binary fashion. The following relations indeed suggest an elegant algorithm:<br />

Theorem 9.4.1 (Silver, Terzian, and Stein). For integers x, y,<br />

If x, y are both even, then gcd(x, y) =2gcd(x/2,y/2);<br />

If x is even and y is not, then gcd(x, y) =gcd(x/2,y);<br />

(As per Euclid) gcd(x, y) =gcd(x − y, y);<br />

If u, v are both odd, then |u − v| is even and less than max{u, v}.<br />

These observations give rise to the following algorithm:<br />

Algorithm 9.4.2 (Binary gcd). The following algorithm returns the greatest<br />

common divisor of two positive integers x, y. For any positive integer m, let<br />

v2(m) be the number of low-order 0’s in the binary representation of m; that<br />

is, we have 2 v2(m) m. (Note that m/2 v2(m) is the largest odd divisor of m, and

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!