Modular Math
Quadratic Residues
Suppose modulo p = 29. Take a = 11 and we can calculate that a^2 mod 29 = 121 mod 29 = 5. Here, the square root of 5 is 11.
How about the square root of 18? Let's try brute-forcing a starting from 0 to p-1.
p = 29
possible_a_squared = []
for a in range(p):
res = pow(a, 2, p)
possible_a_squared.append(res)
print(18 in possible_a_squared)
print(sorted(set(possible_a_squared)))
# False
# [0, 1, 4, 5, 6, 7, 9, 13, 16, 20, 22, 23, 24, 25, 28]As we can see, 18 has no square root. In fact, not all elements of modulo 29 have a square root. Roughly, only half of them have one. This actually means that the elements can have more than one root.
In the list printed above, which shows the elements of the modulo which do have square roots, are called the "quadratic residues". Inversely, the elements that do not have square roots are called "quadratic non-residues".
Note:
xis a Quadratic Residue if there exists anasuch thata^2 β‘ mod p. If there is no such solution, then the integera^2is a Quadratic Non-Residue.If
a^2 = xthen(-a)^2 = x. So ifxis a quadratic residue in some finite field, then there are always two solutions fora.
Now onto the challenge. We are given p = 29 and ints = [14,6,11]. One of the integer in ints is a quadratic residue. We have to submit the smaller root of that integer.
To answer this, we can brute-force all possible roots.
Flag: 8
Legendre Symbol
Rather than using a brute-force approach, there is a single operation in which we can decide whether an integer is a quadratic residue, which is called Legendre Symbol.
This is a property of quadratic residues:
Quadratic Residue * Quadratic Residue = Quadratic Residue
Quadratic Residue * Quadratic Non-residue = Quadratic Non-residue
Quadratic Non-residue * Quadratic Non-residue = Quadratic Residue
Legendre Symbol: (a/p) β‘ a^(pβ1)/2 mod p
To check of residue: calculate a^(pβ1)/2 mod p.
To compute the square root, given that the prime p obeys p β‘ 3 mod 4, we can do this:
Here's the solver for the challenge.
Flag: 93291799125366706806545638475797430512104976066103610269938025709952247020061090804870186195285998727680200979853848718589126765742550855954805290253592144209552123062161458584575060939481368210688629862036958857604707468372384278049741369153506182660264876115428251983455344219194133033177700490981696141526
Modular Square Root
Previously, we calculate the square root on the condition that p β‘ 3 mod 4. The generelized algorithm, which also works for p β‘ 1 mod 4, is called Tonelli-Shanks.
Tonelli-Shanks only works for prime moduli. For composite moduli, there is no efficient way to calculate the modular square root.
Main use of Tonelli-Shanks: finding elliptic curve coordinates.
Onto the challenge, here is the solver script.
Flag: 2362339307683048638327773298580489298932137505520500388338271052053734747862351779647314176817953359071871560041125289919247146074907151612762640868199621186559522068338032600991311882224016021222672243139362180461232646732465848840425458257930887856583379600967761738596782877851318489355679822813155123045705285112099448146426755110160002515592418850432103641815811071548456284263507805589445073657565381850521367969675699760755310784623577076440037747681760302434924932113640061738777601194622244192758024180853916244427254065441962557282572849162772740798989647948645207349737457445440405057156897508368531939120
Chinese Remainder Theorem
Given these congruences where the moduli are coprime:
There is a unique solution x β‘ a mod N where N = n1*n2*...*ni.
For the challenge, we have to find a such that x β‘ a mod 935.
Solving by hand (reference):
Flag: 872
Last updated