It was a bit unfortunate that I could only solve one chal this time. There were two chals that I looked into (prove it! and budget-bag) but I could not solve them on time. I'll maybe get back to them, once I have more free time. The competition website is here.
crypto/hOlyT
Author: freed
God is trying to talk to you through a noisy wire
Use nc chall.lac.tf 31171 to talk to him.
The code that the server runs server.py is as follows:
Let's first connect to the server and have it print out the ciphertext, modulus, and public key.
In server.py, the first four helper functions legendre(a, p), tonelli(n, p), xgcd(a, b), and crt(a, b, m, n) are pretty typical algorithms in number theory, and so there isn't anything much that we need to take a deep look into.
The advice function advice(x, p, q) however is interesting. Given an input $x$, it computes its square root $y$ (i.e., $x = y^2$) in $\textrm{mod } p$ and $\textrm{mod } q$. Then, it sometimes multiplies $-1$ to $y \textrm{ mod } p$ and/or $y \textrm{ mod } q$.
Let $m_1$ and $n_1$ be the output of xgcd(p,q), i.e., $pm_1 + q n_1 = 1$. Then crt(x1, x2, p, q) would return $x_2 p m_1 + x_1 q n_1 \textrm{ mod } N$.
If we feed $x = 1$ into advice(x, p, q), then $x_1 = 1$ or $p-1$, and $x_2 = 1$ or $x_2 = q-1$ similarly. So, there are four possibilities:
After "asking for the advice" multiple times, we can confirm that there are four different possible values for "advices" and two of them are $1$ and $N-1$ indeed.
This means that the remaining two must be $(q-1)p m_1 + q n_1$ and $p m_1 + (p-1)q n_1$. It is unclear which one is which, but that is not crucial because, adding $p m_1 + q n_1$ to both of them gives a multiple of $p$ and $q$ respectively.
\[
\begin{align}
& p m_1 + q n_1 + p m_1 + (p-1)q n_1 = 2 p m_1 + pq n_1 = 2 p m_1 \mod N \\
& p m_1 + q n_1 + (q-1)p m_1 + q n_1 = 2 q n_1 + pq m_1 = 2 q n_1 \;\mod N
\end{align}
\]
We can compute $2^{-1} \textrm{ mod } N$ and multiply it to both to get $p m_1$ and $q n_1$. Then, we can take $\gcd(p m_1, N)$ and $\gcd(q n_1, N)$ to extract $p$ and $q$. We are done now; now that we have $p$ and $q$, we can just compute the private key $d$ and decrypt the ciphertext.