The sum of quadratic residues mod $p$ is $0$

elementary-number-theoryprime numbersquadratic-residuessolution-verification

I'm trying to solve this problem:

If $p$ is congruent to $1$ mod $4$, then the sum of the quadratic residues mod $p$ is $0$. (Where $p$ is an integer prime)

But I seem to have proven this holds for all prime $p$. Presumably my proof is wrong, but I can't see it; can anyone spot where?

Proof: As $p$ is a prime, there $\exists a $ primitive root. The quadratic residues are then $a^2, a^4, \dots, a^{p-1}$, as we know that there are $\phi(p)/2$ quadratic residues. Adding these gives $a^2(1+ (a^2)^1 + \dots + (a^2)^{\frac{p-3}{2}})$. As $(1-a^2, p) = 1$ (as $a$ is a primitive root it is not $\pm 1$ modulo $p$) we calculate $a^2(1+ (a^2)^1 + \dots + (a^2)^{\frac{p-3}{2}}) \times (1-a^2) = a^2\times (1-(a^2)^{\frac{p-1}{2}})$, which is divisible by $p$ due to Euler's theorem.

I haven't used that $p$ is congruent to $1$ mod $4$; have I gone wrong somewhere in my proof?

Best Answer

According to https://oeis.org/A076409 (sum of quadratic residues), for primes $p\ge 5$, the sum of quadratic residues of $p$ is divisible by $p$. The comments points to Wolstenholme's theorem, but your proof is perfectly fine except for $p=2,3$, for which the primitive root $\equiv -1 \pmod p$, and $1$ is the only quadratic residue for these primes.

Remark: if you are to use $p \equiv 1 \pmod 4$, we must have $p\ge 5$, then your claim that $a \ne \pm 1\pmod p$ is true, and the proof can proceed.