Quadratic Residue
- •
What it is
- •
An integer is a quadratic residue (QR) modulo if some satisfies . Modulo a prime, the QRs form a subgroup, which gives the set a useful multiplicative structure.
- •
- •
When to apply
- •
A challenge gives a prime modulus and asks you to identify or find the root of a QR among candidates, or — more subtly — leaks information through whether ciphertext values are QRs or not (a residuosity oracle).
- •
- •
Symbols and assumptions
- •
is an odd prime. For the multiplicative subgroup, a quadratic residue is a nonzero value with . Zero is a square too, but is excluded from that group.
- •
- •
Math — step by step
- •
- Squaring identifies and : their squares agree. Over a field these are the only two roots of a nonzero square, because implies or .
- •
- The nonzero inputs therefore produce exactly nonzero squares. Their products and inverses are squares: and .
- •
- These squares form an index-two subgroup. Multiplying by a square preserves whether a nonzero element is a square. This is the property exploited by ElGamal (Subgroup and Quadratic-Residue Leakage).
- •
- •
- •
- •
Python
- •
def is_qr(a, p): return pow(a, (p - 1) // 2, p) == 1
- •
- •
SageMath
- •
Mod(6, 29).is_square() # True Mod(6, 29).sqrt() # 8 (or 21, the other root)
- •
- •
- •