Binary quadratic forms and class groups
- Cost
- Reduction and NUCOMP cost or less per operation; listing the reduced forms costs ; the order of an element whose order is -smooth apart from one prime below costs compositions.
A binary quadratic form is the polynomial with integer coefficients; its discriminant is , which is congruent to 0 or 1 modulo 4. The group 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 (, ) are supported by qfb and treated here. The standard references are [Buell1989], [Coh1996, Chapter 5] and [Cox2013].
Reduction#
Definition 1. A positive definite form is reduced if , and whenever or .
Theorem 2. Every positive definite form is equivalent to exactly one reduced form. A reduced form of discriminant has .
The bound follows from . The existence and uniqueness are classical [Coh1996, §5.3]. The reduction algorithm alternates two transformations:
Input: with and discriminant .
Output: the equivalent reduced form.
- If : replace by its residue modulo in and set (a substitution ).
- If : exchange and and negate (the substitution ), and go to step 1.
- If or , replace by . Return .
Each exchange in step 2 decreases , and after the first step the algorithm behaves like a Euclidean algorithm on the coefficients, with 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 (those with ) modulo equivalence form a finite abelian group under composition, the form class group , of order the class number . By Theorem 2, is the number of primitive reduced forms, and these can be listed by searching with and , . FLINT does this in two ways, both in operations:
- by (
qfb_reduced_forms): sieve the factorisations of for all at once, using the square roots of modulo each small prime (the for which divides form arithmetic progressions), and read off the divisors ; - by (
qfb_reduced_forms_large): factor for all by sieving, and for each enumerate the square roots of modulo withn_sqrtmodn. This needs only the primes up to and is used for large .
Asymptotically faster methods for , of complexity about (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 and of discriminant is as follows (Dirichlet's composition, see [Cox2013, §3]). Let and find with
Then is the class of . The coefficients of this form are of the size of , and a reduction brings them back to .
Shanks' NUCOMP [Sha1989] avoids the large intermediate form. It computes the same quantities, but stops the extended Euclidean algorithm that determines halfway, when the remainders reach , 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 such that is a square modulo , there is a form of discriminant with , obtained from a square root of modulo (qfb_prime_form). These prime forms correspond to the prime ideals of norm , and the prime forms with below a bound of order generate the class group under the generalised Riemann hypothesis: Bach [Bach1990] proved that suffices; see also [BD1992].
Orders and the exponent#
The order of an element of a finite group can be found by an analogue of Pollard's method when it is smooth [Sut2007].
Stage 1. Let . If , then the order of divides , and it is found by removing the prime factors of one at a time: for each prime , raise to successive powers of until the identity appears. FLINT accumulates 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 , the order of may still be a single prime . With , store the baby steps for odd in a hash table and compute the giant steps . Since the table is searched for a form or its inverse (equal and ), a match gives , so the baby steps need only cover half of the residues, and odd suffice because is odd. The candidate is accepted if it is prime.
Exponent. The exponent of is the least common multiple of the orders of its elements, a multiple of the order of every element and a divisor of . Sutherland's Algorithm 8.1 [Sut2007] computes it from random elements, here prime forms: each new form is first raised to the exponent found so far, so that only the missing part of its order has to be found, and . 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 is not fundamental, a prime form may be imprimitive; it is then divided by its content, which changes the discriminant to .
Use in FLINT#
| Function | Method |
|---|---|
qfb_reduce, qfb_is_reduced | Algorithm 1 |
qfb_reduced_forms, qfb_reduced_forms_large | enumeration by or by with sieving |
qfb_nucomp, qfb_nudupl | NUCOMP and NUDUPL [JvdP2002] |
qfb_pow_ui, qfb_pow, qfb_pow_with_root | binary powering with reduction |
qfb_prime_form | square root of modulo |
qfb_exponent_element | stage 1 and stage 2 with qfb_hash_t |
qfb_exponent, qfb_exponent_grh | Sutherland's Algorithm 8.1 on prime forms |
void qfb_reduce(qfb_t r, qfb_t f, fmpz_t D)slong qfb_reduced_forms(qfb ** forms, slong d)int qfb_exponent_grh(fmpz_t exponent, fmpz_t n, ulong B1, ulong B2_sqrt)Referenced from#
- qfb.h (module)
qfb_exponent_element
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 : 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.
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).
@misc{flint-doc-algorithms-number-theory-binary-quadratic-forms,
author = {{The FLINT team}},
title = {{Binary quadratic forms and class groups}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/binary-quadratic-forms.html},
note = {Generated 2026-11-02}
}