Binary GCD Like Algorithms for Some Complex Quadratic Rings
Saurabh Agarwal and Gudmund Skovbjerg Frandsen
BRICS , Department of Computer Science, University of Aarhus IT-Parken, Abogade˚ 34, DK-8200, Aarhus N, Denmark. {saurabh, gudmund}@daimi.au.dk
Abstract. On the lines of the binary gcd algorithm for rational integers, algorithms√ for computing the gcd are presented for the ring of integers in Q( d) where d ∈{−2, −7, −11, −19}. Thus a binary gcd like algorithm is presented for a unique factorization domain which is not Euclidean (case d = −19). Together with the√ earlier known√ binary gcd like algorithms for the ring of integers in Q( −1) and Q( −3), one now has binary gcd like algorithms for all complex quadratic Euclidean domains. The running time of our algorithms is O(n2) in each ring. While there exists an O(n2) algorithm for computing the gcd in quadratic number rings by Erich Kaltofen and Heinrich Rolletschek, it has large constants hidden under the big-oh notation and it is not practical for medium sized inputs. On the other hand our algorithms are quite fast and very simple to implement.
1 Introduction
Greatest common divisor is one of the most fundamental concepts of number the- ory. Elementary number theory texts introduce gcd very early and also present an algorithm to compute it, the Euclid’s algorithm. However, it is not possible to extend Euclid’s algorithm to all number rings. The rings in which one can extend Euclid’s algorithm are called Euclidean rings. A large amount of effort has been put in identifying Euclidean number rings. Franz Lemmermeyer’s pa- per on Euclidean number rings [12] contains an almost complete list of all known Euclidean number rings. In 1965 a different algorithm to compute gcd was presented by J. Stein [19]. Apart from being very simple to understand, this algorithm has the virtue of being efficiently implementable on a computer as the only operations used by the algorithm are addition, subtraction and division by 2. Since divisions by 2 can be performed by right shifts (on a computer), this algorithm essentially has no divisions at all. This algorithm is popularly known as the binary gcd algorithm. In this paper we present extensions of this algorithm to four complex quadratic rings. Basic Research in Computer Science (www.brics.dk), funded by the Danish National Research Foundation.
D.A. Buell (Ed.): ANTS 2004, LNCS 3076, pp. 57–71, 2004. c Springer-Verlag Berlin Heidelberg 2004 58 S. Agarwal and G.S. Frandsen
1.1 Overview of Results
We successfully generalize√ the binary gcd algorithm to compute the gcd in the ring of integers in Q( d) where d ∈{−2, −7, −11, −19}. In each case the time complexity of the algorithm is O(n2) with small constants hidden under the big-oh notation. The only operations used in our algorithms are addition, sub- traction and division by a small fixed number (2 or 3 or 5). One of the main re- sult is an extension of the binary gcd algorithm to a unique factorization domain (ufd) which is not Euclidean (case d = −19). Our extension clearly indicates that the binary gcd like algorithms are not restricted to Euclidean rings.
1.2 Road Map Section 2 contains some preliminaries. In Sect. 3, we review some other algo- rithms for computing the gcd. The main idea of our algorithm is presented in Sect.√ 4. In Sect. 5, the algorithms for computing the gcd in the ring of integers in Q( d) are presented where d ∈{−2, −7, −11, −19}.
2 Preliminaries
The definitions/facts in this section are found in most books on algebra and/or algebraic number theory (for example√ see [10,6,7]). Complex quadratic number fields are of the form Q = Q( d) where√ d is a negative square-free rational√ integer. Any α ∈Qis of the form a + b d where a, b ∈ Q.√ For any α = a + b d, the norm of α is defined as N(α)=αα¯ whereα ¯ = a − b d is the conjugate of α.IfZ denotes the ring of algebraic integers in Q, then √ Z + Z d if d ≡ 2, 3 (mod 4) Z = √ Z Z 1 − d d ≡ + 2 ( 1+ ) if 1 (mod 4)
There are nine complex√ quadratic rings which are also ufd. These are the ring of integers in Q( d) where d ∈{−1, −2, −3, −7, −11, −19, −43, −67, −163} [18]. From now on we will assume that Z is one of these nine rings. For all α ∈Z, N(α) is a non-negative rational integer and N(α)=0iff α = 0. An element u ∈Z is a unit iff N(u) = 1. Any two elements α, β ∈Z are called associates if α|β and β|α. A non-zero non-unit element ρ ∈Zis a prime if (ρ|αβ) ⇒ (ρ|α or ρ|β). If ρ ∈Z is a prime, then there exists a rational prime p such that N(ρ)=p or p2. In the former case ρ is not associate to any rational prime and in the latter case ρ is an associate to p. The quotient ring Z/ρZ is a finite field with N(ρ) elements. If N(ρ)=p for some odd rational prime p, then − p−1 ,... , ,... , p−1 Z/ρZ 2 0 2 forms a complete set of coset representatives for and if N(ρ) = 2, then {0, 1} forms a complete set of coset representatives for Z/ρZ. Let α, β ∈Zand αβ = 0. Then a non-zero element g ∈Zis said to be greatest common divisor (gcd) of α and β if Binary GCD Like Algorithms for Some Complex Quadratic Rings 59
a. g|α and g|β, and b. for any γ ∈Z\{0},ifγ|α and γ|β, then γ|g. For any α = 0, gcd of α and 0 is defined to be α. In the literature, gcd of α and β is denoted by (α, β) and we also use this notation. In general (α, β) is not unique. However if g1 =(α, β) and g2 =(α, β), then g1 and g2 are associates. It is customary to overload the ’=’ operator for gcd. Thus a statement like (α, β)=(γ,η) means that gcd of α, β and gcd of γ,η are associates. The following facts about gcd are easily shown.
Lemma 1. Let α, β ∈Z be arbitrary integers and ρ ∈Z be any prime.
a. If ρ|α and ρ|β, then (α, β)=ρ (α/ρ, β/ρ). b. If ρ|α and ρ β, then (α, β)=(α/ρ, β). c. (α, β)=(α + λβ, β) for all λ ∈Z.
3 Related Work
The main aim of this section is to present some of the algorithms which can possibly be used or extended to compute the gcd in different number rings. There are many different known ways of computing the gcd and the list of algorithms discussed in this section is not exhaustive. In the rest of this section the term ’ring’ will always mean number ring.
The problem of computing the gcd is as old as number theory itself. Euclid gave an algorithm to compute the gcd of rational integers in 300 B.C. [9]. This algo- rithm is called the Euclidean Algorithm (ea). However ea cannot be extended to all rings. The rings in which one can extend ea are the Euclidean rings. A fairly complete list of all known Euclidean rings can be found in [12]. Let R be any Euclidean ring. Then by definition of Euclidean ring, there exists ϕ: R → N ∪{0} such that for any a, b ∈ R there exits q, r ∈ R such that a = bq + r and ϕ(r)
3.2 Non-euclidean Algorithms
Erich Kaltofen and Heinrich Rolletschek [8] gave an O(n3) algorithm for com- puting the gcd in all complex quadratic ufds. They transform the problem of computing the gcd to that of computing a short vector in a suitable four dimen- sional integer lattice. They have also given an O(n2) algorithm to compute the gcd in any quadratic ring. This algorithm is based on a result which states that, given α and β in some quadratic ring, one can always divide lα by β and have a remainder with norm smaller than N(β) where l is a small rational integer. However the algorithm has large constants under the big-oh notation [8,21]. Henri Cohen has given a general algorithm for computing the extended gcd by reducing the problem to that of computing Hermite normal form of a suitable rational integer matrix [3]. However, he has not given the exact complexity of this algorithm in different rings. The simplicity of the operations used in the binary gcd algorithm makes it the method of choice on real computers [2]. This algorithm√ has been generalized by Andr´e Weilert [21] to the ring of integers in Q( −1)√ and by Ivan Damg˚ard and Gudmund Frandsen [5] to the ring of integers in Q( −3). In both the cases the running time of the algorithm is O(n2) with small constants hidden under the big-oh notation. There have been several variations and enhancements of the original binary gcd algorithm (see the notes at the end of chapter 4 in [2]). Our approach is quite similar to that of Jonathan Sorenson’s [17] k-ary algorithm. The main reason we look at the possibility of extending the binary gcd algorithm is its simplicity and its speed. While the approaches like controlled Euclidean descent are expected to be asymptotically faster, they are impractical for smaller inputs.
4 Binary GCD Like Algorithms in Number Rings
The binary gcd algorithm for Z is shown in Alg. 1. The algorithm is slightly modified to include negative integers. The algorithm is based on the following three facts: a. |a |b a, b a , b If 2 and 2 , then ( )=2 2 2 . b. |a b a, b a ,b If 2 and 2 , then ( )= 2 . c. a b a, b a−b ,b a+b ,b If 2 and 2 , then ( )= 2 = 2 . Thus given two non-zero odd a, b ∈ Z with |a|≥|b|, one can find c ∈ Z in O |a| |c|≤ |a| a, b c, b (log ) time such that 2 and ( )=( ). This fact forms the basis of Binary GCD Like Algorithms for Some Complex Quadratic Rings 61
Algorithm 1 Binary Gcd algorithm for Z (a, b are inputs) 1. Find i, j ≥ 0 such that 2i|a, 2i+1 a, 2j |b and 2j+1 b. 2. a = a/2i, b = b/2j 3. Assert that |a|≥|b|.Swapa and b if needed. 4. while true 5. if |a − b|≤|a + b| then c = a − b 6. else c = a + b 7. if c =0then break 8. Find h ≥ 1 such that 2h|c and 2h+1 c. 9. c = c/2h 10. if |c|≥|b| then a = c 11. else a = b, b = c 12. return 2min{i,j}a
the while loop in Alg. 1 and guarantees that the algorithm will terminate in at most (log |a||b| + 1) iterations of the while loop. This algorithm may not work for algebraic integers. This is because if α and β are algebraic integers such that 2 α and 2 β then 2 need not divide α + β or α − β. Thus the argument for termination of the above algorithm fails for algebraic integers. Our aim is to create an algorithm similar to Alg. 1 for complex quadratic rings. As a first step we will generalize the binary gcd algorithm to use ratio- nal primes other than 2. This generalization can be seen as a special case of Sorenson’s k-ary algorithm [17]. Suppose we have a, b ∈ Z such that a and b are co-prime to an odd prime p, then p|(a + lb) for some l ∈ Zp where Zp = Z/pZ p − p−1 ,... , ,... , p−1 is the finite field of residues modulo . The set 2 0 2 forms a complete set of coset representatives for Zp and hence we can always choose l |l|≤ p−1 p p such that 2 . Therefore by using in place of 2 in Alg. 1, we have a -ary algorithm for calculation of gcd in Z as shown in Alg. 2. In Alg. 2 assuming |a|≥|b|, it takes O(log |a|) time to compute a suitable c in steps 5 and 6. Thus a c a, b c, b |c|≤ 1 1 |a| we can replace with such that ( )=( ) and 2 + 2p and the complexity of Alg. 2 is the same as the complexity of the binary gcd algorithm. Let Z be an imaginary quadratic number ring. If ρ is a prime in Z, then F = Z/ρZ is a field. Thus if α and β are any two integers co-prime to ρ, then there exists a λ ∈Fsuch that α+λβ is divisible by ρ. By Lemma 1 we know that α, β α λβ, β − p−1 ,... , ,... , p−1 F ( )=( + ). Thus by replacing 2 0 2 with in step 5 of Alg. 2 one can construct an algorithm similar to Alg. 2 for computing the gcd Z.IfN(α + λβ) ≤ fN(ρα) for some f<1, then one can show that such an algorithm will terminate. However for a fixed choice of F, it is not guaranteed that there will exist a λ ∈Fsatisfying the√ above termination condition.√ For example consider the ring of integers in Q( −2). In this ring ζ = −2isa prime of norm 2. If α and β are any two integers in this ring and co-prime to ζ, then α ± β is divisible by ζ.Ifγ is the norm-wise smaller of {α + β,α − β}, then N(γ) ≤ 2N(α) (it follows using Lemma 2 which is mentioned later). Thus N(γ/ζ) ≤ N(α) and the above argument for termination breaks down. However 62 S. Agarwal and G.S. Frandsen
Algorithm 2 p-ary Gcd Algorithm for Z (p ∈ Z is a fixed odd prime and inputs are a, b ∈ Z) 1. Find i, j ≥ 0 such that pi|a, pi+1 a, pj |b and pj+1 b. 2. a = a/pi, b = b/pj 3. Assert that |a|≥|b|.Swapa and b if needed. 4. while true 5. l ∈ p−1 ,... ,0,... , p−1 (a + lb) ≡ 0 (mod p) Find 2 2 such that 6. c = a + lb 7. if c =0then break 8. Find h ≥ 1 such that ph|c and ph+1 c. 9. c = c/ph 10. if |c|≥|b| then a = c 11. else a = b, b = c 12. return pmin{i,j}a
one can get around this problem in some imaginary quadratic rings as follows (one can get around the termination problem in this particular ring in another way which is mentioned in conclusion). Suppose p is a rational prime. Then either p splits or ramifies or remains inert in a quadratic ring Z. Suppose p splits into ρ andρ ¯. In this situation ρ and ρ¯ are not associates and are co-prime to each other. Since N(ρ)=N(¯ρ)=p, − p−1 ,... , ,... , p−1 { , } 2 0 2 or 0 1 forms a complete set of coset representatives for both Z/ρZ and Z/ρ¯Z depending on if p is odd or even. Now if α and β are any two integers co-prime to both ρ andρ ¯, then we have two choices of λ (λ1 and λ2 such that ρ|(α + λ1β) andρ ¯|(α + λ2β)). The idea is now to use both primes and then choose λ which makes α+λβ small. However this trick is useful only when there is at most one choice of λ ∈Fwhich can result in α + λβ having a large norm. One can verify that this favorable situation occurs only when N(ρ) ≤ 5.
5 GCD Algorithms for Complex Quadratic Rings
In this section we materialize the ideas presented in the last section and√ construct algorithms for computing the gcd in the ring of integers in Q = Q( d) where d ∈{−2, −7, −11, −19}. These algorithms can be seen as an instance of the abstract algorithm shown in Alg. 3. The main difference from Alg. 2 is that we use a pair of conjugate primes instead of one fixed prime. Let ρ andρ ¯ be the primes used in Alg. 3. The equivalent of c from Alg. 2 is C(α, β) and is denoted by γ. For different rings, ρ and C are different. In the rest of this section we will show how to choose ρ and C in different rings and prove the termination of the algorithm in each case. Note that if Alg. 3 terminates, it will terminate with correct answer if (α, β)=(C(α, β),β). In all the rings, our choice of C(α, β)isα + lβ for some rational integer l. Thus the Binary GCD Like Algorithms for Some Complex Quadratic Rings 63
Algorithm 3 Gcd algorithm for Z (inputs are α, β and primes ρ, ρ¯ are fixed) 1. Let p1 = ρ, p2 =¯ρ it it+1 jt jt+1 2. Find i1, i2, j1 and j2 such that pt |α, pt α, pt |β and pt β for t =1, 2. i1 i2 j1 j2 3. α = α/p1 p2 , β = β/p1 p2 4. Assert that N(α) ≥ N(β).Swapα and β if needed. 5. While true 6. γ = C(α, β) 7. if γ =0then break ht ht+1 8. Find (h1,h2) such that pt |γ and pt γ for t =1, 2. h1 h2 9. η = γ/p1 p2 10. if N(η) ≥ N(β) then α = η 11. else α = β, β = η min{i1,j1} min{i2,j2} 12. return p1 p2 α
correctness follows from Lemma 1. The following lemma will be the major tool in proving the termination results. Lemma 2. Let Z be an imaginary quadratic ring. Let α, β ∈Z and l, m ∈ Z. If N(α) ≥ N(β), then a. N(lα + mβ) ≤ (|l| + |m|)2N(α), b. min{N(lα + mβ),N(lα − mβ)}≤(l2 + m2)N(α). Proof. We note that for any integer α, N(α) is the same as the complex norm of α when α is viewed as a complex number. Proof of above statements now follows by elementary properties of complex norm. For d ∈{−2, −7, −11}, we show that N(η) ≤ fN(α) for some f<1 and the termination of the algorithm is trivial. For d = −19, we show that in at most two iterations of the while loop, the product N(α)N(β) will decrease by a factor f>1 and hence the algorithm will terminate. The time complexity of Alg. 3 is the same in all rings and we discuss this in Sect. 5.5. In the rest of this section we will compare algebraic integers with respect to the norm. Thus whenever we say that α<β, it means that N(α)