I'm playing with exponential sums...


If $q$ is an odd prime and $a$ an integer such that $q \nmid a$, then the following formula for the Gaussian sum is known
$$\sum\_{x=0}^{q-1} e\_q(ax^2) = \left(\frac{a}{p}\right) i^{\left(\frac{q-1}{2}\right)^2} \sqrt{q} ,$$
where $e\_q(z) := \exp\left(\frac{2\pi i z}{q}\right)$ and $\left(\frac{a}{q}\right)$ is the Legendre symbol. Using the orthogonality identity
$$\delta\_q(b) := \frac1{q}\sum\_{y = 0}^{q-1} e\_q(by) = \begin{cases}1 & \text{ if } q \mid b; \\ 0 & \text{ if } q \nmid b;\end{cases}$$
for integer $b$, and standard manipulation of exponential sums (I can give more details, but, as I said, it is all standard stuff), I obtained that for all integers $k \geq 1$ and $\lambda$, the number of $x\_1, \ldots, x\_k \in \{0, \ldots, q-1\}$ such that
$$x\_1^2 + \cdots + x\_k^2 \equiv \lambda \bmod q$$
if equal to
$$q^{k-1} + \begin{cases}(-1)^{\frac{k}{2}\left(\frac{q-1}{2}\right)^2} q^{\frac{k}{2}-1} (q \delta\_q(\lambda)-1) & \text{ if } 2 \mid k; \\
\left(\frac{-\lambda}{q}\right)(-1)^{\frac{k+1}{2}\left(\frac{q-1}{2}\right)^2}q^{\frac{k-1}{2}} & \text{ if } 2 \nmid k \text{ and } q \nmid \lambda;\\ 0 & \text{ if } 2 \nmid k \text{ and } q \nmid \lambda .\end{cases} $$
I'm sure that this result should be already known, but I failed to find a reference.


Thank you in advance for any suggestion!



The number of solution of $x_1^2 + \cdots + x_k^2 \equiv \lambda \bmod q$
Yes, this is standard. More generally, we have the following


**Theorem.** Let $p$ be an odd prime, and let $a\_1,\dots,a\_k\in\mathbb{F}\_p^\times$. Then the number of solutions of the equation $a\_1x\_1^2+\cdots +a\_kx\_k^2=1$ in $\mathbb{F}\_p$ equals
\begin{align\*}
p^{k-1}-\left(\frac{a\_1\dots a\_k}{p}\right)p^{\frac{k-2}{2}},&\qquad k\equiv 0\pmod{4};\\
p^{k-1}+\left(\frac{a\_1\dots a\_k}{p}\right)p^{\frac{k-1}{2}},&\qquad k\equiv 1\pmod{4};\\
p^{k-1}-\left(\frac{-a\_1\dots a\_k}{p}\right)p^{\frac{k-2}{2}},&\qquad k\equiv 2\pmod{4};\\
p^{k-1}+\left(\frac{-a\_1\dots a\_k}{p}\right)p^{\frac{k-1}{2}},&\qquad k\equiv 3\pmod{4}.
\end{align\*}


**P.S.** I am sure there is a reference for this, but I found it easier to copy from my notes.


**Added 1.** As Gerry Myerson pointed out (in a comment he deleted), the special case $a\_1=\dots=a\_k=1$ is Proposition 8.6.1 in Ireland-Rosen: A classical introduction to modern number theory (2nd edition). 


**Added 2.** For the sake of completeness, I provide the proof.
Let $e\_p:\mathbb{F}\_p\to\mathbb{C}^\times$ be the standard additive character, $\chi:\mathbb{F}\_p^\times\to\{\pm 1\}$ the nontrivial quadratic character, and
$$\lambda:=\begin{cases}
1,&\qquad p\equiv 1\pmod{4};\\
i,&\qquad p\equiv 3\pmod{4}.\end{cases}$$
Let us remark that $\lambda^2=\chi(-1)$. If $n$ denotes the number of solutions in the theorem, then
\begin{align\*}
pn&=\sum\_{x\_1,\dots,x\_k\in\mathbb{F}\_p}\ \sum\_{m\in\mathbb{F}\_p}\ e\_p(m(a\_1x\_1^2+\cdots +a\_kx\_k^2-1))\\
&=\sum\_{m\in\mathbb{F}\_p}e\_p(-m)\sum\_{x\_1\in\mathbb{F}\_p}e\_p(ma\_1x\_1^2)\ \cdots\sum\_{x\_k\in\mathbb{F}\_p}e\_p(ma\_kx\_k^2)\\
&=p^k + \sum\_{m\in\mathbb{F}\_p^\times}e\_p(-m)\bigl\{\chi(ma\_1)\lambda\sqrt{p}\bigr\}\cdots \bigl\{\chi(ma\_k)\lambda\sqrt{p}\bigr\}\\
&=p^k + \chi(a\_1\dots a\_k)\lambda^k p^{\frac{k}{2}}\sum\_{m\in\mathbb{F}\_p^\times}e\_p(-m)\chi(m)^k.
\end{align\*}
The inner sum equals $-1$ or $\chi(-1)\lambda\sqrt{p}$ depending on whether $k$ is even or odd, therefore
$$n=\begin{cases}
p^{k-1}-\chi((-1)^{\frac{k}{2}}a\_1\dots a\_k)p^{\frac{k-2}{2}},&k\equiv 0\pmod{2};\\
p^{k-1}+\chi((-1)^{\frac{k-1}{2}}a\_1\dots a\_k)p^{\frac{k-1}{2}},&k\equiv 1\pmod{2}.
\end{cases}$$


