A norm_num
extension for Jacobi and Legendre symbols #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
We extend the tactic.interactive.norm_num
tactic so that it can be used to provably compute
the value of the Jacobi symbol J(a | b)
or the Legendre symbol legendre_sym p a
when
the arguments are numerals.
Implementation notes #
We use the Law of Quadratic Reciprocity for the Jacobi symbol to compute the value of J(a | b)
efficiently, roughly comparable in effort with the euclidean algorithm for the computation
of the gcd of a
and b
. More precisely, the computation is done in the following steps.
-
Use
J(a | 0) = 1
(an artifact of the definition) andJ(a | 1) = 1
to deal with corner cases. -
Use
J(a | b) = J(a % b | b)
to reduce to the case thata
is a natural number. We define a version of the Jacobi symbol restricted to natural numbers for use in the following steps; seenorm_num.jacobi_sym_nat
. (But we'll continue to writeJ(a | b)
in this description.) -
Remove powers of two from
b
. This is done viaJ(2a | 2b) = 0
andJ(2a+1 | 2b) = J(2a+1 | b)
(another artifact of the definition). -
Now
0 ≤ a < b
andb
is odd. Ifb = 1
, then the value is1
. Ifa = 0
(andb > 1
), then the value is0
. Otherwise, we remove powers of two froma
viaJ(4a | b) = J(a | b)
andJ(2a | b) = ±J(a | b)
, where the sign is determined by the residue class ofb
mod 8, to reduce toa
odd. -
Once
a
is odd, we use Quadratic Reciprocity (QR) in the formJ(a | b) = ±J(b % a | a)
, where the sign is determined by the residue classes ofa
andb
mod 4. We are then back in the previous case.
We provide customized versions of these results for the various reduction steps,
where we encode the residue classes mod 2, mod 4, or mod 8 by using terms like
bit1 (bit0 a)
. In this way, the only divisions we have to compute and prove
are the ones occurring in the use of QR above.
The Jacobi symbol restricted to natural numbers in both arguments.
Equations
- norm_num.jacobi_sym_nat a b = jacobi_sym ↑a b
API Lemmas #
We repeat part of the API for jacobi_sym
with norm_num.jacobi_sym_nat
and without implicit
arguments, in a form that is suitable for constructing proofs in norm_num
.
Base cases: b = 0
, b = 1
, a = 0
, a = 1
.
Turn a Legendre symbol into a Jacobi symbol.
The value depends only on the residue class of a
mod b
.
The symbol vanishes when both entries are even (and b ≠ 0
).
When a
is odd and b
is even, we can replace b
by b / 2
.
If a
is divisible by 4
and b
is odd, then we can remove the factor 4
from a
.
If a
is even and b
is odd, then we can remove a factor 2
from a
,
but we may have to change the sign, depending on b % 8
.
We give one version for each of the four odd residue classes mod 8
.
Use quadratic reciproity to reduce to smaller b
.
Certified evaluation of the Jacobi symbol #
The following functions recursively evaluate a Jacobi symbol and construct the corresponding proof term.