Mathematics - Modular Math
Quadratic Residues
"We say that an integer x
is a Quadratic Residue if there exists an a
such that a^2 = x mod p
. If there is no such solution, then the integer is a Quadratic Non-Residue.
If a^2 = x
then (-a)^2 = x
. So if x
is a quadratic residue in some finite field, then there are always two solutions for a
.
p = 29
ints = [14, 6, 11]
"
We can simply loop through all value for a
from 0
to p-1
and see if a solution exists
Legendre Symbol
"Before looking at Legendre's symbol, let's take a brief detour to see an interesting property of quadratic (non-)residues.
Quadratic Residue * Quadratic Residue = Quadratic Residue
Quadratic Residue * Quadratic Non-residue = Quadratic Non-residue
Quadratic Non-residue * Quadratic Non-residue = Quadratic Residue
Legendre's Symbol: (a / p) ≡ a(p-1)/2 mod p
obeys:
(a / p) = 1 if a is a quadratic residue and a ≢ 0 mod p
(a / p) = -1 if a is a quadratic non-residue mod p
(a / p) = 0 if a ≡ 0 mod p
Which means given any integer a
, calculating pow(a,(p-1)//2,p)
is enough to determine if a
is a quadratic residue.
Now for the flag. Given the following 1024 bit prime and 10 integers, find the quadratic residue and then calculate its square root; the square root is your flag. Of the two possible roots, submit the larger one as your answer.
So Legendre's symbol tells us which integer is a quadratic residue, but how do we find the square root?! The prime supplied obeys p = 3 mod 4
, which allows us easily compute the square root. The answer is online, but you can figure it out yourself if you think about Fermat's little theorem."
When p = 3 mod 4
then a^((p+1)/4) mod p
is a square root of a
So, first we find the quadratic residue and then the square root.
Modular Square Root
The following implementation does the trick.
The function legendre_symbol(a, p)
computes the Legendre symbol of a
modulo p
, using Euler's criterion. The function tonelli_shanks(n, p)
finds a quadratic residue of n
modulo p
, assuming that p
is prime. If n
is not a quadratic residue modulo p
, the function returns None
.
Chinese Remainder Theorem
"The Chinese Remainder Theorem gives a unique solution to a set of linear congruences if their moduli are coprime.
This means, that given a set of arbitrary integers ai
, and pairwise coprime integers ni
, such that the following linear congruences hold:
Note "pairwise coprime integers" means that if we have a set of integers {n1, n2, ..., ni}
, all pairs of integers selected from the set are coprime: gcd(ni, nj) = 1
.
x ≡ a1 mod n1
x ≡ a2 mod n2
...
x ≡ an mod nn
There is a unique solution x ≡ a mod N
where N = n1 * n2 * ... * nn
.
In cryptography, we commonly use the Chinese Remainder Theorem to help us reduce a problem of very large integers into a set of several, easier problems.
Given the following set of linear congruences:
x ≡ 2 mod 5
x ≡ 3 mod 11
x ≡ 5 mod 17
Find the integer a
such that x ≡ a mod 935
"
This script will output x given a list of a's and n's.
n = [5, 11, 17]
a = [2, 3, 5]
The script will output a number smaller than 935, which means that x
is a solution for a
Also, I've created a solution that explains the steps a bit more clearly.
Last updated