I found the following proof for Mantel's theorem in Lecture 1 of David Conlon's "Extremal graph theory" course. I cannot understand how the equality that I have highlighted in the proof quoted below was arrived at. I would appreciate some assistance.
Theorem 1 (Mantel's theorem) *If a graph $G$ on $n$ vertices contains no triangle then it contains at most $\frac {n^2}{4}$ edges.
First proof Suppose that $G$ has $m$ edges. Let $x$ and $y$ be two vertices in $G$ which are joined by an edge. If $d(v)$ is the degree of a vertex $v$, we see that $d(x)+d(y)\leq n$. This is because every vertex in the graph $G$ is connected to at most one of $x$ and $y$. Note now that $$\bbox[5px,border:2px solid red]{ \sum_x d^2(x)=\sum_{x,y\in E} \big( d(x)+d(y)\big) } \leq mn.$$ On the other hand, since $\sum_x d(x)=2m$, the Cauchy-Schwarz inequality implies that $$\sum_x d^2(x)\geq\frac{\big(\sum_x d(x)\big)^2}{n} \geq \frac{4m^2}{n}.$$ Therfore $$\frac{4m^2}{n} \leq mn,$$ and the result follows. $\tag*{$\square$}$
The part I don't understand is: $$\sum_x d^2(x)=\sum_{x,y\in E} \big( d(x)+d(y)\big).$$