I was wondering how to prove the following sum of two squares algorithm works. Let $p$ be a prime congruent to $1$ mod $4$.
- Find a nonzero quadratic nonresidue $a$ modulo $p$.
- Find $r\in \{0,\cdots, p-1\}$ so that $r\equiv a^{(p-1)/4}\mod p.$
- Write the GCD of $p$ and $r-i$ in the form $x +iy$ (where the GCD is in $\mathbb{Z}[i]$).
- Output $x,y$.
The algorithm's output satisfies $x^2 + y^2 = p$, which is what I want to prove.
To prove why the algorithm works, I was thinking of using some properties of the gcd in the Gaussian integers, but I can't seem to make much progress. For instance, I know $x+iy = ap+b(r-i)$ for some $a,b\in \mathbb{Z}[i]$. Also, $\gcd(r-i, p) = \gcd(a^{(p-1)/4} - i, p)$. Perhaps it might be useful to show that $p$ is a nonzero nonunit? To show this, one would just need to find Gaussian integers $a,b$ so that $p | ab$ but $p\nmid a, b$.