[proofplan]
The proof has two parts. First, the Pfaffian Chern-Weil representative of the Euler class, applied to the Levi-Civita connection, identifies the integral of the Euler form with the Euler characteristic. Second, in an oriented orthonormal frame on a four-manifold, the curvature operator decomposes into self-dual and anti-self-dual blocks, and the Pfaffian density is an explicit quadratic expression in those blocks. Substituting the standard four-dimensional curvature decomposition gives the displayed formula.
[/proofplan]
[step:Identify the Euler form with the Euler class]
Let $\nabla^{LC}$ denote the Levi-Civita connection on the oriented Euclidean vector bundle $(TX,g)$, and let $\Omega\in \Omega^2(X;\mathfrak{so}(TX,g))$ denote its curvature form. Let $\operatorname{vol}_g\in\Omega^4(X)$ denote the Riemannian volume form, let $d\operatorname{vol}_g$ denote the associated Riemannian volume measure, and define the smooth Euler density $E_g:X\to\mathbb R$ by the identity $e(\Omega)=E_g\operatorname{vol}_g$. Let $H^4_{\mathrm{dR}}(X)$ denote the fourth de Rham cohomology group of $X$, and let $e(TX)\in H^4_{\mathrm{dR}}(X)$ denote the de Rham Euler class of the oriented tangent bundle. By the Pfaffian Chern-Weil construction of the Euler class for oriented even-rank Euclidean bundles, applied to the rank-$4$ bundle $(TX,g)$, the form $e(\Omega)$ is closed and represents $e(TX)$; this is the normalization used in [citetheorem:9774].
Since $X$ is a closed oriented smooth manifold of dimension $4=2\cdot 2$ and $g$ is a Riemannian metric on $X$, the general [Gauss-Bonnet-Chern theorem](/theorems/9776) [citetheorem:9776] applied with the integer parameter $m=2$ gives
\begin{align*}
\chi(X)=\int_X E_g(x)\,d\operatorname{vol}_g(x).
\end{align*}
This proves the first displayed identity.
[/step]
[step:Write the curvature operator in self-dual and anti-self-dual blocks]
Fix a point $p\in X$. Let $(e_1,e_2,e_3,e_4)$ be a positively oriented $g_p$-[orthonormal basis](/page/Orthonormal%20Basis) of $T_pX$, and let $(e_1^*,e_2^*,e_3^*,e_4^*)$ be the [dual basis](/theorems/414) of $T_p^*X$. The Hodge star operator determined by $g_p$ and the orientation gives the [orthogonal decomposition](/theorems/436)
\begin{align*}
\Lambda^2T_p^*X=\Lambda^2_+T_p^*X\oplus \Lambda^2_-T_p^*X.
\end{align*}
Let $R^g$ denote the Riemann curvature tensor of $\nabla^{LC}$, viewed as the map sending vector fields $Y,Z$ to the endomorphism $R^g(Y,Z)$ of $TX$. Let $\mathcal R_p:\Lambda^2T_p^*X\to \Lambda^2T_p^*X$ denote the curvature operator, defined by
\begin{align*}
(\mathcal R_p(\alpha),\beta)_{\Lambda^2}=\frac{1}{4}\sum_{i,j,k,l=1}^{4}R_{ijkl}(p)\alpha_{ij}\beta_{kl}
\end{align*}
for $\alpha,\beta\in \Lambda^2T_p^*X$, where $R_{ijkl}(p)=g_p(R^g(e_i,e_j)e_k,e_l)$ are the curvature tensor components and $(\cdot,\cdot)_{\Lambda^2}$ is the metric-induced [inner product](/page/Inner%20Product) on $\Lambda^2T_p^*X$.
Let $\pi_+:\Lambda^2T_p^*X\to\Lambda^2_+T_p^*X$ and $\pi_-:\Lambda^2T_p^*X\to\Lambda^2_-T_p^*X$ denote the orthogonal projections. Let $I_+:\Lambda^2_+T_p^*X\to \Lambda^2_+T_p^*X$ and $I_-:\Lambda^2_-T_p^*X\to \Lambda^2_-T_p^*X$ denote the identity maps. The standard four-dimensional curvature decomposition states that there are trace-free self-adjoint endomorphisms $W^+_p:\Lambda^2_+T_p^*X\to\Lambda^2_+T_p^*X$ and $W^-_p:\Lambda^2_-T_p^*X\to\Lambda^2_-T_p^*X$, and a [linear map](/page/Linear%20Map) $B_p:\Lambda^2_+T_p^*X\to\Lambda^2_-T_p^*X$ determined by $\operatorname{Ric}_0(p)$, such that
\begin{align*}
\pi_+\mathcal R_p|_{\Lambda^2_+T_p^*X}=W^+_p+\frac{S(p)}{12}I_+.
\end{align*}
\begin{align*}
\pi_-\mathcal R_p|_{\Lambda^2_+T_p^*X}=B_p.
\end{align*}
\begin{align*}
\pi_+\mathcal R_p|_{\Lambda^2_-T_p^*X}=B_p^*.
\end{align*}
\begin{align*}
\pi_-\mathcal R_p|_{\Lambda^2_-T_p^*X}=W^-_p+\frac{S(p)}{12}I_-.
\end{align*}
Here $B_p^*:\Lambda^2_-T_p^*X\to\Lambda^2_+T_p^*X$ is the adjoint of $B_p$ with respect to the metric-induced inner products. All norms of $W^\pm_p$ and $B_p$ are Hilbert-Schmidt norms on the indicated finite-dimensional inner-product spaces. The trace-free Ricci block normalization is
\begin{align*}
|B_p|^2=\frac{1}{2}|\operatorname{Ric}_0(p)|^2,
\end{align*}
where $|\operatorname{Ric}_0(p)|$ is the tensor norm induced by $g_p$.
[guided]
At a point $p$, the curvature tensor can be treated as a symmetric operator on $2$-forms. The relevant [vector space](/page/Vector%20Space) is $\Lambda^2T_p^*X$, and in dimension $4$ the Hodge star satisfies $*^2=1$ on $2$-forms. Therefore its $+1$ and $-1$ eigenspaces give the orthogonal splitting
\begin{align*}
\Lambda^2T_p^*X=\Lambda^2_+T_p^*X\oplus \Lambda^2_-T_p^*X.
\end{align*}
Let $R^g$ denote the Riemann curvature tensor of $\nabla^{LC}$, viewed as the map sending vector fields $Y,Z$ to the endomorphism $R^g(Y,Z)$ of $TX$. Define its components in the chosen basis by $R_{ijkl}(p)=g_p(R^g(e_i,e_j)e_k,e_l)$. The Riemann curvature tensor defines the curvature operator $\mathcal R_p:\Lambda^2T_p^*X\to \Lambda^2T_p^*X$ by the metric pairing
\begin{align*}
(\mathcal R_p(\alpha),\beta)_{\Lambda^2}=\frac{1}{4}\sum_{i,j,k,l=1}^{4}R_{ijkl}(p)\alpha_{ij}\beta_{kl}.
\end{align*}
This definition fixes the normalization of the operator and hence the coefficients in the final density.
Let $\pi_+:\Lambda^2T_p^*X\to\Lambda^2_+T_p^*X$ and $\pi_-:\Lambda^2T_p^*X\to\Lambda^2_-T_p^*X$ denote the orthogonal projections, and let $I_+$ and $I_-$ denote the identity maps on $\Lambda^2_+T_p^*X$ and $\Lambda^2_-T_p^*X$. With respect to the splitting into self-dual and anti-self-dual forms, the curvature operator is equivalently described by the four block identities
\begin{align*}
\pi_+\mathcal R_p|_{\Lambda^2_+T_p^*X}=W^+_p+\frac{S(p)}{12}I_+.
\end{align*}
\begin{align*}
\pi_-\mathcal R_p|_{\Lambda^2_+T_p^*X}=B_p.
\end{align*}
\begin{align*}
\pi_+\mathcal R_p|_{\Lambda^2_-T_p^*X}=B_p^*.
\end{align*}
\begin{align*}
\pi_-\mathcal R_p|_{\Lambda^2_-T_p^*X}=W^-_p+\frac{S(p)}{12}I_-.
\end{align*}
The diagonal trace-free pieces are the self-dual and anti-self-dual Weyl curvature endomorphisms $W^+_p$ and $W^-_p$, measured with the Hilbert-Schmidt norms on $\Lambda^2_+T_p^*X$ and $\Lambda^2_-T_p^*X$. The scalar curvature contributes the scalar operators $\frac{S(p)}{12}I_+$ and $\frac{S(p)}{12}I_-$ to the diagonal blocks. The off-diagonal block $B_p:\Lambda^2_+T_p^*X\to\Lambda^2_-T_p^*X$ is the trace-free Ricci block, $B_p^*$ is its metric adjoint, and the standard normalization of the curvature operator gives
\begin{align*}
|B_p|^2=\frac{1}{2}|\operatorname{Ric}_0(p)|^2,
\end{align*}
where $|B_p|$ is the Hilbert-Schmidt norm and $|\operatorname{Ric}_0(p)|$ is the tensor norm induced by $g_p$. This is the point where the coefficient $-\frac{1}{2}$ in the final formula enters: the Pfaffian density contains $-|B_p|^2$, which becomes $-\frac{1}{2}|\operatorname{Ric}_0(p)|^2$.
[/guided]
[/step]
[step:Evaluate the Pfaffian density in the curvature decomposition]
For the positively oriented orthonormal basis chosen above, let $\operatorname{vol}_{g,p}\in \Lambda^4T_p^*X$ denote the Riemannian volume form at $p$. Let $\Omega_{ij}(p)\in\Lambda^2T_p^*X$ denote the skew-symmetric curvature-form entries in this oriented orthonormal frame, so that $\Omega_{ij}(p)(e_k,e_l)=R_{klij}(p)$. The Pfaffian normalization from [citetheorem:9774] gives
\begin{align*}
e(\Omega)_p=\frac{1}{32\pi^2}\sum_{i,j,k,l=1}^{4}\varepsilon_{ijkl}\,\Omega_{ij}(p)\wedge\Omega_{kl}(p),
\end{align*}
where $\varepsilon_{ijkl}$ is the sign of the permutation $(i,j,k,l)$ when the four indices are distinct and is $0$ otherwise. Applying the algebraic Pfaffian identities in [citetheorem:9773] to this skew-symmetric curvature matrix, rewritten through the orthogonal splitting $\Lambda^2T_p^*X=\Lambda^2_+T_p^*X\oplus\Lambda^2_-T_p^*X$, gives the pointwise identity
\begin{align*}
e(\Omega)_p=\frac{1}{8\pi^2}\left(|W^+_p|^2+|W^-_p|^2-|B_p|^2+\frac{S(p)^2}{24}\right)\operatorname{vol}_{g,p}.
\end{align*}
The cited Pfaffian identity is used with the curvature operator written in the orthogonal splitting $\Lambda^2T_p^*X=\Lambda^2_+T_p^*X\oplus\Lambda^2_-T_p^*X$. The Hilbert-Schmidt orthogonality of the Weyl, scalar, and trace-free Ricci blocks gives the three contributions $|W^+_p|^2+|W^-_p|^2$, $S(p)^2/24$, and $-|B_p|^2$, respectively. Using the trace-free Ricci block normalization $|B_p|^2=\frac{1}{2}|\operatorname{Ric}_0(p)|^2$, this becomes
\begin{align*}
e(\Omega)_p=\frac{1}{8\pi^2}\left(|W^+_p|^2+|W^-_p|^2-\frac{1}{2}|\operatorname{Ric}_0(p)|^2+\frac{S(p)^2}{24}\right)\operatorname{vol}_{g,p}.
\end{align*}
Since $p\in X$ was arbitrary, the identity holds as an equality of smooth $4$-forms on $X$.
[guided]
At this point the remaining issue is not topological but algebraic: the Euler form is the Pfaffian of the curvature matrix, so the constants are fixed by the Pfaffian convention. In the chosen positively oriented orthonormal frame, let $\Omega_{ij}(p)\in\Lambda^2T_p^*X$ be the skew-symmetric curvature-form entries, characterized by $\Omega_{ij}(p)(e_k,e_l)=R_{klij}(p)$. The normalization in [citetheorem:9774] gives the explicit local formula
\begin{align*}
e(\Omega)_p=\frac{1}{32\pi^2}\sum_{i,j,k,l=1}^{4}\varepsilon_{ijkl}\,\Omega_{ij}(p)\wedge\Omega_{kl}(p),
\end{align*}
where $\varepsilon_{ijkl}$ is the oriented Levi-Civita symbol. The algebraic role of [citetheorem:9773] is to evaluate this Pfaffian expression after the curvature matrix has been rewritten as an operator on $\Lambda^2T_p^*X$ and split into self-dual and anti-self-dual blocks. Applying that normalized Pfaffian identity to the block decomposition of $\mathcal R_p$ gives
\begin{align*}
e(\Omega)_p=\frac{1}{8\pi^2}\left(|W^+_p|^2+|W^-_p|^2-|B_p|^2+\frac{S(p)^2}{24}\right)\operatorname{vol}_{g,p}.
\end{align*}
The reason the expression separates into these terms is that the decomposition of the curvature operator is orthogonal for the Hilbert-Schmidt inner product on endomorphisms of $\Lambda^2T_p^*X$. The trace-free diagonal blocks contribute $|W^+_p|^2+|W^-_p|^2$, the scalar diagonal blocks contribute $S(p)^2/24$, and the off-diagonal trace-free Ricci block contributes with the sign $-|B_p|^2$. Finally, the normalization of the trace-free Ricci block gives
\begin{align*}
|B_p|^2=\frac{1}{2}|\operatorname{Ric}_0(p)|^2.
\end{align*}
Substituting this identity into the Pfaffian density gives
\begin{align*}
e(\Omega)_p=\frac{1}{8\pi^2}\left(|W^+_p|^2+|W^-_p|^2-\frac{1}{2}|\operatorname{Ric}_0(p)|^2+\frac{S(p)^2}{24}\right)\operatorname{vol}_{g,p}.
\end{align*}
Because the point $p\in X$ was arbitrary, this is an equality of smooth $4$-forms on $X$.
[/guided]
[/step]
[step:Integrate the pointwise density]
Let $E_g:X\to\mathbb R$ be the Euler density defined by $e(\Omega)=E_g\operatorname{vol}_g$. Integrating the equality of $4$-forms over the closed oriented manifold $X$ gives
\begin{align*}
\int_X E_g(x)\,d\operatorname{vol}_g(x)=\frac{1}{8\pi^2}\int_X\left(|W^+|^2+|W^-|^2-\frac{1}{2}|\operatorname{Ric}_0|^2+\frac{S^2}{24}\right)\,d\operatorname{vol}_g(x).
\end{align*}
Combining this with $\chi(X)=\int_X E_g(x)\,d\operatorname{vol}_g(x)$ from the first step yields
\begin{align*}
\chi(X)=\frac{1}{8\pi^2}\int_X\left(|W^+|^2+|W^-|^2-\frac{1}{2}|\operatorname{Ric}_0|^2+\frac{S^2}{24}\right)\,d\operatorname{vol}_g(x).
\end{align*}
This is the stated four-dimensional Chern-Gauss-Bonnet formula.
[/step]