[step:Construct a convex potential from quadratic duality]Define the half-quadratic cost $c_0: \mathbb{R}^n \times \mathbb{R}^n \to [0,\infty)$ by
\begin{align*}
c_0(x,y)=\frac{1}{2}|x-y|^2.
\end{align*}
Apply the following precise form of quadratic Kantorovich duality with attainment to $c_0$: if the source and target are Polish spaces, the cost is continuous and finite-valued, and some admissible coupling has finite cost, then the primal infimum equals the dual supremum, the dual supremum is attained by Borel potentials $a: \mathbb{R}^n \to \mathbb{R}\cup\{-\infty\}$ and $b: \mathbb{R}^n \to \mathbb{R}\cup\{-\infty\}$ satisfying $a(x)+b(y)\leq c_0(x,y)$ for all $x,y$, the marginal integrals $\int_{\mathbb{R}^n} a\,d\mu$ and $\int_{\mathbb{R}^n} b\,d\nu$ are well-defined in the extended-real sense with finite sum equal to the primal value, and every primal optimizer is concentrated on the equality set $a(x)+b(y)=c_0(x,y)$. The spaces $\mathbb{R}^n$ and $\mathbb{R}^n$ are Polish spaces, the cost $c_0$ is continuous and finite-valued, and the product plan $\mu\otimes\nu$ has finite $c_0$-cost because $\mu,\nu\in\mathcal{P}_2(\mathbb{R}^n)$. Hence this theorem applies. We use the standard normalization of the attained potentials, which gives at least one point $x_0\in\mathbb{R}^n$ with $a(x_0)>-\infty$ and at least one point $y_0\in\mathbb{R}^n$ with $b(y_0)>-\infty$. Since $c=2c_0$, the plan $\pi$ is also optimal for $c_0$. Thus
\begin{align*}
a(x)+b(y) \leq \frac{1}{2}|x-y|^2
\end{align*}
for all $x,y \in \mathbb{R}^n$, equality holds for $\pi$-almost every $(x,y) \in \mathbb{R}^n \times \mathbb{R}^n$, and the dual value is finite. This use cites a result not yet in the wiki: Kantorovich duality for the quadratic cost with dual attainment on Polish spaces under finite second moments.
Define $h: \mathbb{R}^n \to \mathbb{R}\cup\{\infty\}$ by
\begin{align*}
h(y)=\frac{1}{2}|y|^2-b(y).
\end{align*}
Define $\varphi: \mathbb{R}^n \to (-\infty,\infty]$ to be the Legendre-Fenchel transform of $h$:
\begin{align*}
\varphi(x)=\sup_{y\in\mathbb{R}^n}\{x\cdot y-h(y)\}.
\end{align*}
As a supremum of affine functions of $x$, the function $\varphi$ is convex and lower semicontinuous. By the finiteness conclusion in the dual attainment theorem, choose points $x_0,y_0\in\mathbb{R}^n$ with $a(x_0)>-\infty$ and $b(y_0)>-\infty$. The inequality above then gives
\begin{align*}
\varphi(x_0) \leq \frac{1}{2}|x_0|^2-a(x_0)<\infty.
\end{align*}
Also $h(y_0)=\frac{1}{2}|y_0|^2-b(y_0)<\infty$, so for every $x\in\mathbb{R}^n$,
\begin{align*}
\varphi(x)\geq x\cdot y_0-h(y_0)>-\infty.
\end{align*}
Thus $\varphi$ is not identically $+\infty$ and never takes the value $-\infty$, so $\varphi$ is proper.
For every $x,y \in \mathbb{R}^n$, the dual inequality gives
\begin{align*}
x\cdot y-h(y) \leq \frac{1}{2}|x|^2-a(x).
\end{align*}
Taking the supremum over $y$ gives
\begin{align*}
\varphi(x) \leq \frac{1}{2}|x|^2-a(x).
\end{align*}
On the set where $a(x)+b(y)=\frac{1}{2}|x-y|^2$, this inequality is an equality at that particular $y$:
\begin{align*}
\varphi(x)=x\cdot y-h(y).
\end{align*}[/step]