Legendre Symbol
- •
What it is
- •
, computed as , tells you whether is a quadratic residue (1), a non-residue (-1, shown as ), or (0). Python/Sage's
kronecker()generalizes it to composite moduli.
- •
- •
When to apply
- •
You need fast QR/non-QR classification on a large prime without brute-forcing the field — including as a side-channel, when a ciphertext scheme leaks a bit per value depending on its residuosity.
- •
- •
Symbols and assumptions
- •
For odd prime , the Legendre symbol is 0 if , +1 for a nonzero square, and −1 for a non-square. The fraction-like notation is a symbol, not division.
- •
- •
Math — step by step
- •
- Euler’s criterion computes it: . In code, −1 appears as the nonnegative residue .
- •
- For nonzero with a primitive root , even means is a square. Since , raising to gives , revealing that parity.
- •
- Exponentiation distributes over multiplication, so . A square mask has symbol +1 and cannot hide the symbol of the value it multiplies.
- •
- Testing for a square and finding a square root are different tasks. Euler’s criterion classifies; Tonelli-Shanks Algorithm finds a root. A Jacobi symbol +1 for composite modulus does not prove the input is a square.
- •
- •
Worked example
- •
Let , so the exponent is 5. For 2: , hence 2 is a non-square.
- •
For 4: , , and , hence 4 is a square.
- •
Mask both with the square 4. The results are and . Their fifth powers are 10 and 1, respectively: the class survives the mask.
- •
For the special input 2, : primes congruent to 1 or 7 modulo 8 give +1; primes congruent to 3 or 5 give −1.
- •
- •
Python
- •
def legendre(a, p): ls = pow(a, (p - 1) // 2, p) return -1 if ls == p - 1 else ls
- •
- •
SageMath
- •
legendre_symbol(6, 29) # 1 kronecker(6, 29) # 1, generalizes to composite moduli too
- •
- •
- •
Cards
- •
What three values can the legendre symbol take, and what does each mean?
- •
1 = quadratic residue, -1 = non-residue, 0 = a ≡ 0 mod p.
- •
- •
What's a real-world CTF use of the Legendre symbol beyond just finding QRs?
- •
As an oracle: testing residuosity of each ciphertext value can leak a bit of hidden information per value.
- •
- •