  In this work we obtain a geometric characterization of the measures $\mu$ in
$\mathbb{R}^{n+1}$ with polynomial upper growth of degree $n$ such that the
$n$-dimensional Riesz transform $\mathcal{R}\mu (x) = \int
\frac{x-y}{|x-y|^{n+1}}\,d\mu(y)$ belongs to $L^2(\mu)$, under the assumption
that $\mu$ satisfies the following Wolff energy estimate, for any ball
$B\subset\mathbb{R}^{n+1}$: $$\int_B \int_0^\infty
\left(\frac{\mu(B(x,r))}{r^{n-\frac38}}\right)^2\,\frac{dr}r\,d\mu(x)\leq
M\,\bigg(\frac{\mu(2B)}{r(B)^{n-\frac38}}\bigg)^2\,\mu(2B).$$ More precisely,
we show that $\mu$ satisfies the following estimate:
$$\|\mathcal{R}\mu\|_{L^2(\mu)}^2 + \|\mu\|\approx \int\!\!\int_0^\infty
\beta_{\mu,2}(x,r)^2\,\frac{\mu(B(x,r))}{r^n}\,\frac{dr}r\,d\mu(x) + \|\mu\|,$$
where $\beta_{\mu,2}(x,r)^2 = \inf_L \frac1{r^n}\int_{B(x,r)}
\left(\frac{\mathrm{dist}(y,L)}r\right)^2\,d\mu(y),$ with the infimum taken
over all affine $n$-planes $L\subset\mathbb{R}^{n+1}$. In a companion paper
which relies on the results obtained in this work it is shown that the same
result holds without the above assumption regarding the Wolff energy of $\mu$.
This result has important consequences for the Painlev\'e problem for Lipschitz
harmonic functions.
