---
title: "Class Groups"
canonical: "https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/BinaryQuadraticForms/class-group.html"
source: "https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/BinaryQuadraticForms/class-group.md"
part: "Lattices and Quadratic Forms"
chapter: "Binary Quadratic Forms"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Lattices and Quadratic Forms", "source": "https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/index-lattices-quadratic-forms.md"}
  - {"title": "Binary Quadratic Forms", "source": "https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/BinaryQuadraticForms/index-binary-quadratic-forms.md"}
prev: {"title": "Reduced Forms", "source": "https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/BinaryQuadraticForms/reduced-forms.md"}
next: {"title": "Discrete Logarithms", "source": "https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/BinaryQuadraticForms/log.md"}
intrinsics:
  - {"name": "ClassNumber", "signature": "ClassNumber(Q: parameters): QuadBin -> RngIntElt"}
  - {"name": "ClassNumber", "signature": "ClassNumber(D: parameters): RngIntElt -> RngIntElt"}
  - {"name": "ClassGroup", "signature": "ClassGroup(Q: parameters): QuadBin -> GrpAb, Map"}
  - {"name": "ClassGroupStructure", "signature": "ClassGroupStructure(Q: parameters): QuadBin -> [ RngIntElt ]"}
  - {"name": "AmbiguousForms", "signature": "AmbiguousForms(Q): QuadBin -> SeqEnum"}
  - {"name": "TwoTorsionSubgroup", "signature": "TwoTorsionSubgroup(Q): QuadBin -> GrpAb, Map"}
examples:
  - "ex-d1a38f"
---

# Class Groups

In Magma, the class group of binary quadratic forms of discriminant $D$ is defined by the equivalence relation `IsEquivalent(f1, f2 : Narrow := false)` for forms `f1, f2`. It is the same as the ideal class group (`PicardGroup`) of the quadratic order of discriminant $D$. The class group of binary forms for $D > 0$ is usually defined using standard (“narrow”) equivalence of forms, which agrees with the narrow ideal class group of the order. For $D < 0$, there is no difference. For $D > 0$, the two class groups differ if and only if all units in the quadratic order have norm $1$. When they are different, each class is the union of two narrow classes: forms $\langle a,b,c \rangle$ and $\langle -a,b,-c \rangle$ are equivalent but not narrowly equivalent.

<a id="function-classnumber-quadbin"></a>
## `ClassNumber(Q: parameters): QuadBin -> RngIntElt`

<a id="function-classnumber-rngintelt"></a>
## `ClassNumber(D: parameters): RngIntElt -> RngIntElt`

```magma
Al              : MonStgElt                    Default: "Automatic"
FactorBasisBound: FldReElt                     Default: 0.1
ProofBound      : FldReElt                     Default: 6
ExtraRelations  : RngIntElt                    Default: 1
```

This returns the class number of binary quadratic forms $Q$ of discriminant $D$.

The parameter `Al` may be supplied to select the method used to calculate the class number. The possible values are `"ReducedForms"` (enumerating all reduced forms), `"Shanks"` (using a Shanks-based algorithm in the class group), `"Sieve"`, or `"NoSieve"`. The default is to use reduced form enumeration for small discriminants, the Shanks algorithm for the middle range, a class group index-calculus for large discriminants and the sieve method described in [[Jac99](https://docs.magma-maths.org/v2.30-1/references.md#cite-jacobson)] for very large discriminants.

The parameters `FactorBasisBound`, `ProofBound`, and `ExtraRelations` are relevant only for the index calculus algorithm. They are described (below) under [`ClassGroup`](https://docs.magma-maths.org/v2.30-1/LatticesQuadraticForms/BinaryQuadraticForms/class-group.md#function-quadbin-classgroup).

<a id="function-quadbin-classgroup"></a>
## `ClassGroup(Q: parameters): QuadBin -> GrpAb, Map`

```magma
Al              : MonStgElt                    Default: "Automatic"
FactorBasisBound: FldReElt                     Default: 0.1
ProofBound      : FldReElt                     Default: 6
ExtraRelations  : RngIntElt                    Default: 1
```

This computes the class group of binary quadratic forms of discriminant $D$. The function returns the class group as an abelian group, together with a map from the group to quadratic forms.

**Important:** the class group is defined using non-strict equivalence (see the introduction to this subsection).

Depending on the size of $D$, either a Shanks-based method is used ([[BJT97](https://docs.magma-maths.org/v2.30-1/references.md#cite-teske-bsgs), [Tes98a](https://docs.magma-maths.org/v2.30-1/references.md#cite-teske-prho)]), an index calculus variant ([[CDO93](https://docs.magma-maths.org/v2.30-1/references.md#cite-cohdiol), [Coh93](https://docs.magma-maths.org/v2.30-1/references.md#cite-cohen1), [HM89](https://docs.magma-maths.org/v2.30-1/references.md#cite-hafnermccurley)]) or a sieving method ([[Jac99](https://docs.magma-maths.org/v2.30-1/references.md#cite-jacobson)]). If the parameter `Al` is set to `"Sieve"` or $D$ is larger than $10^{20},$ then the sieving method is used. The results are only proven under GRH. If the parameter `Al` is set to `"NoSieve"` then the sieving method will not be used.

The parameters `FactorBasisBound`, `ProofBound`, and `ExtraRelations` apply to the index calculus method only. This method performs two steps. In the first step a factor basis containing prime forms of norm $< B_1$ is built. Next, one looks for generators for the full lattice of relations between the forms in the factor basis. The determinant of this lattice will be the class number and the Smith-form of the relation matrix gives the structure of the class group. Once the matrix is of full rank, the algorithm will look for `ExtraRelations` more relations to potentially decrease the discriminant. In the second step, for all prime forms of norm $< B_2$ it is verified that they are in the class group generated by the forms of the first step. The bounds are chosen to be $B_1 :=$ `FactorBasisBound`$\cdot \log^2 |D|$ and $B_2 :=$ `ProofBound`$\cdot \log^2 |D|$, so the result is correct under the assumption of GRH. The final result is then checked against the Euler product over the first $30000$ primes. If the quotient becomes too large, a warning is issued. In this case one should increase the `ExtraRelations` parameter.

<a id="function-classgroupstructure-quadbin"></a>
## `ClassGroupStructure(Q: parameters): QuadBin -> [ RngIntElt ]`

The structure of the class group of the binary quadratic forms $Q$ of discriminant $D$ returned as a sequence of integers giving the abelian invariants.

The parameters are the same as for `ClassGroup`.

<a id="function-ambiguousforms-quadbin"></a>
## `AmbiguousForms(Q): QuadBin -> SeqEnum`

Enumerates the ambiguous forms of negative discriminant $D$, where $D$ is the discriminant of the magma of binary quadratic forms $Q$.

<a id="function-twotorsionsubgroup-quadbin"></a>
## `TwoTorsionSubgroup(Q): QuadBin -> GrpAb, Map`

The subgroup of 2–torsion elements of in the class group of $Q$.

<a id="example-ex-d1a38f"></a>
## `Example: Computing in a Form Class Group (ex-d1a38f)`

We give an example of some computations in the class group of a magma of quadratic forms. Elements in the class group are not really represented by single reduced forms but by cycles of equivalent reduced forms.

```magma
> Q<z> := QuadraticField(7537543);                        // arbitrary choice
> Q := QuadraticForms(Discriminant(Q));
> C, m := ClassGroup(Q);
> C;
Abelian Group isomorphic to Z/2 + Z/76
Defined on 2 generators
Relations:
    76*C.1 = 0
    2*C.2 = 0
>                                    // get the generators as quadratic forms:
> f := m(C.1);
> g := m(C.2);
> h := g^2;
> g, h;
<-1038,4894,1493> <-887,4340,3189>
> c := [];                     // create the cycle of forms equivalent to g^2:
> repeat
>     h := ReductionStep(h);
>     Append(~c, h);
> until h eq g^2;
> P := Parent(g);
> Identity(P) in c;
true                 // this proves that the second class has order dividing 2
> for d in Divisors(76) do
>     c := [];
>     h := f^d;
>     repeat h := ReductionStep(h); Append(~c, h);
>     until h eq f^d;
>     d, Identity(P) in c, #c;
> end for;
1 false 16
2 false 14
4 false 12                           // the cycle lengths vary
19 false 18
38 false 16
76 true 14                           // so the true order of this class is 76
```
