FLINT3.7.0-dev
Algorithm

Binary quadratic forms and class groups

Cost
Reduction and NUCOMP cost O(M(log⁡∣D∣)log⁡log⁡∣D∣)O(M(\log |D|) \log\log |D|) or less per operation; listing the reduced forms costs O(∣D∣1/2+o(1))O(|D|^{1/2+o(1)}); the order of an element whose order is B1B_1-smooth apart from one prime below B2B_2 costs O(π(B1)log⁡B1+B2)O(\pi(B_1) \log B_1 + \sqrt{B_2}) compositions.

A binary quadratic form f=(a,b,c)f = (a, b, c) is the polynomial ax2+bxy+cy2ax^2 + bxy + cy^2 with integer coefficients; its discriminant is D=b2−4acD = b^2 - 4ac, which is congruent to 0 or 1 modulo 4. The group SL2(Z)\mathrm{SL}_2(\mathbb{Z}) acts on forms by linear substitution of the variables, preserving the discriminant, and two forms in the same orbit are equivalent. Only positive definite forms (D<0D < 0, a>0a > 0) are supported by qfb and treated here. The standard references are [Buell1989], [Coh1996, Chapter 5] and [Cox2013].

Reduction#

Definition 1. A positive definite form (a,b,c)(a, b, c) is reduced if ∣b∣≤a≤c|b| \le a \le c, and b≥0b \ge 0 whenever ∣b∣=a|b| = a or a=ca = c.

Theorem 2. Every positive definite form is equivalent to exactly one reduced form. A reduced form of discriminant DD has a≤∣D∣/3a \le \sqrt{|D|/3}.

The bound follows from ∣D∣=4ac−b2≥4a2−a2=3a2|D| = 4ac - b^2 \ge 4a^2 - a^2 = 3a^2. The existence and uniqueness are classical [Coh1996, §5.3]. The reduction algorithm alternates two transformations:

Algorithm 1: Reduction of a positive definite form

Input: (a,b,c)(a, b, c) with a>0a > 0 and discriminant D<0D < 0.

Output: the equivalent reduced form.

  1. If ∣b∣>a|b| > a: replace bb by its residue modulo 2a2a in (−a,a](-a, a] and set c←(b2−D)/(4a)c \gets (b^2 - D)/(4a) (a substitution x↦x+kyx \mapsto x + ky).
  2. If c<ac < a: exchange aa and cc and negate bb (the substitution (x,y)↦(−y,x)(x, y) \mapsto (-y, x)), and go to step 1.
  3. If ∣b∣=a|b| = a or a=ca = c, replace bb by ∣b∣|b|. Return (a,b,c)(a, b, c).

Each exchange in step 2 decreases aa, and after the first step the algorithm behaves like a Euclidean algorithm on the coefficients, with O(log⁡(a/∣D∣)+1)O(\log(a/\sqrt{|D|}) + 1) iterations. qfb_reduce implements it with fmpz arithmetic; the result of a composition is already nearly reduced, so in the class group computations below a reduction costs only a few steps.

Reduced forms and the class number#

The primitive forms of discriminant DD (those with gcd⁡(a,b,c)=1\gcd(a, b, c) = 1) modulo equivalence form a finite abelian group under composition, the form class group Cl(D)\mathrm{Cl}(D), of order the class number h(D)h(D). By Theorem 2, h(D)h(D) is the number of primitive reduced forms, and these can be listed by searching ∣b∣≤a≤∣D∣/3|b| \le a \le \sqrt{|D|/3} with b≡D(mod2)b \equiv D \pmod 2 and a∣(b2−D)/4a \mid (b^2 - D)/4, c=(b2−D)/(4a)≥ac = (b^2 - D)/(4a) \ge a. FLINT does this in two ways, both in O(∣D∣1/2+o(1))O(|D|^{1/2 + o(1)}) operations:

  • by bb (qfb_reduced_forms): sieve the factorisations of (b2−D)/4(b^2 - D)/4 for all bb at once, using the square roots of DD modulo each small prime pp (the bb for which pp divides (b2−D)/4(b^2 - D)/4 form arithmetic progressions), and read off the divisors aa;
  • by aa (qfb_reduced_forms_large): factor 4a4a for all aa by sieving, and for each aa enumerate the square roots bb of DD modulo 4a4a with n_sqrtmodn. This needs only the primes up to ∣D∣1/4|D|^{1/4} and is used for large ∣D∣|D|.

Asymptotically faster methods for h(D)h(D), of complexity about ∣D∣1/4|D|^{1/4} (Shanks' baby-step giant-step on the analytic class number formula) or subexponential (index calculus), are not implemented.

Composition#

Gauss composition of two primitive forms f1=(a1,b1,c1)f_1 = (a_1, b_1, c_1) and f2=(a2,b2,c2)f_2 = (a_2, b_2, c_2) of discriminant DD is as follows (Dirichlet's composition, see [Cox2013, §3]). Let g=gcd⁡(a1,a2,(b1+b2)/2)g = \gcd(a_1, a_2, (b_1 + b_2)/2) and find BB with

B≡b1(mod2a1/g),B≡b2(mod2a2/g),B2≡D(mod4a1a2/g2).B \equiv b_1 \pmod{2a_1/g}, \qquad B \equiv b_2 \pmod{2a_2/g}, \qquad B^2 \equiv D \pmod{4 a_1 a_2/g^2}.

Then f1f2f_1 f_2 is the class of (a1a2/g2,B,(B2−D)g2/(4a1a2))(a_1 a_2/g^2, B, (B^2 - D) g^2/(4 a_1 a_2)). The coefficients of this form are of the size of ∣D∣|D|, and a reduction brings them back to ∣D∣\sqrt{|D|}.

Shanks' NUCOMP [Sha1989] avoids the large intermediate form. It computes the same quantities, but stops the extended Euclidean algorithm that determines BB halfway, when the remainders reach L=⌊∣D∣1/4⌋L = \lfloor |D|^{1/4} \rfloor, and assembles from the partial cofactors a form that is already nearly reduced. All intermediate numbers then have about half the size, which roughly halves the cost of a composition followed by a reduction. FLINT follows the presentation of Jacobson and van der Poorten [JvdP2002] (qfb_nucomp), with the specialisation NUDUPL for squaring (qfb_nudupl), and uses fmpz_xgcd_partial for the partial Euclidean algorithm, a variant of Lehmer's algorithm. Powers are computed by binary powering with NUDUPL, NUCOMP and a reduction after each step (qfb_pow_ui, qfb_pow).

Prime forms#

For a prime pp such that DD is a square modulo 4p4p, there is a form (p,b,c)(p, b, c) of discriminant DD with 0≤b≤p0 \le b \le p, obtained from a square root of DD modulo pp (qfb_prime_form). These prime forms correspond to the prime ideals of norm pp, and the prime forms with pp below a bound of order log⁡2∣D∣\log^2 |D| generate the class group under the generalised Riemann hypothesis: Bach [Bach1990] proved that p≤6log⁡2∣D∣p \le 6 \log^2 |D| suffices; see also [BD1992].

Orders and the exponent#

The order of an element ff of a finite group can be found by an analogue of Pollard's p−1p - 1 method when it is smooth [Sut2007].

Stage 1. Let E=∏p≤B1p⌊log⁡pB1⌋E = \prod_{p \le B_1} p^{\lfloor \log_p B_1 \rfloor}. If fE=1f^E = 1, then the order of ff divides EE, and it is found by removing the prime factors of EE one at a time: for each prime pp, raise fE/pkf^{E/p^{k}} to successive powers of pp until the identity appears. FLINT accumulates EE in products of four primes, doubles the number of primes in each round, and saves intermediate powers so that the exact order can be found without starting again.

Stage 2. If fE≠1f^E \ne 1, the order of h=fEh = f^E may still be a single prime q≤B2q \le B_2. With m≈B2m \approx \sqrt{B_2}, store the baby steps hjh^j for odd j<mj < m in a hash table and compute the giant steps h2imh^{2im}. Since the table is searched for a form or its inverse (equal aa and ∣b∣|b|), a match h2im=h±jh^{2im} = h^{\pm j} gives h2im∓j=1h^{2im \mp j} = 1, so the baby steps need only cover half of the residues, and odd jj suffice because qq is odd. The candidate 2im∓j2im \mp j is accepted if it is prime.

Exponent. The exponent λ\lambda of Cl(D)\mathrm{Cl}(D) is the least common multiple of the orders of its elements, a multiple of the order of every element and a divisor of h(D)h(D). Sutherland's Algorithm 8.1 [Sut2007] computes it from random elements, here prime forms: each new form is first raised to the exponent λ′\lambda' found so far, so that only the missing part of its order has to be found, and λ′←λ′⋅ord⁡(fλ′)\lambda' \gets \lambda' \cdot \operatorname{ord}(f^{\lambda'}). With prime forms for all primes up to Bach's bound, the result is the exponent assuming GRH; with a fixed number of random primes, it is the exponent with high probability. When DD is not fundamental, a prime form may be imprimitive; it is then divided by its content, which changes the discriminant to D/p2D/p^2.

Use in FLINT#

FunctionMethod
qfb_reduce, qfb_is_reducedAlgorithm 1
qfb_reduced_forms, qfb_reduced_forms_largeenumeration by bb or by aa with sieving
qfb_nucomp, qfb_nuduplNUCOMP and NUDUPL [JvdP2002]
qfb_pow_ui, qfb_pow, qfb_pow_with_rootbinary powering with reduction
qfb_prime_formsquare root of DD modulo pp
qfb_exponent_elementstage 1 and stage 2 with qfb_hash_t
qfb_exponent, qfb_exponent_grhSutherland's Algorithm 8.1 on prime forms
void qfb_nucomp(qfb_t r, const qfb_t f, const qfb_t g, fmpz_t D, fmpz_t L)
int qfb_exponent_element(fmpz_t exponent, qfb_t f, fmpz_t n, ulong B1, ulong B2_sqrt)
int qfb_exponent_grh(fmpz_t exponent, fmpz_t n, ulong B1, ulong B2_sqrt)

Referenced from#

References#

  • [Buell1989] D. A. Buell, Binary Quadratic Forms: Classical Theory and Modern Computations, Springer, 1989. https://doi.org/10.1007/978-1-4612-4542-1
  • [Coh1996] Cohen, Henri : A course in computational algebraic number theory, Springer, 1996
  • [Cox2013] D. A. Cox, Primes of the Form x2+ny2x^2 + ny^2: Fermat, Class Field Theory, and Complex Multiplication, second edition, Wiley, 2013.
  • [Sha1989] D. Shanks, "On Gauss and composition I, II", in Number Theory and Applications (R. A. Mollin, ed.), NATO ASI Series C 265, Kluwer, 1989, 163–204.
  • [JvdP2002] M. J. Jacobson Jr. and A. J. van der Poorten. "Computational aspects of NUCOMP." In International Algorithmic Number Theory Symposium, pp. 120-133. Berlin, Heidelberg: Springer Berlin Heidelberg, 2002.
  • [Bach1990] E. Bach, "Explicit bounds for primality testing and related problems", Mathematics of Computation 55 (1990), 355–380. https://doi.org/10.1090/S0025-5718-1990-1023756-8
  • [BD1992] D. Buchmann and S. Düllmann. "Distributed class group computation." Informatik: Festschrift zum 60. Geburtstag von Günter Hotz (1992): 69-79.
  • [Sut2007] A. V. Sutherland. "Order computations in generic groups." PhD diss., Massachusetts Institute of Technology, 2007.
Cite this page

The FLINT team, “Binary quadratic forms and class groups,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/binary-quadratic-forms.html (generated 2026-11-02).