  The problem of characterizing a real polynomial $f$ as a sum of squares of
polynomials on a real algebraic variety $V$ dates back to the pioneering work
of Hilbert in [Mathematische Annalen 32.3 (1888): 342-350]. In this paper, we
investigate this problem with a focus on cases where the real zeros of $f$ on
$V$ are singular points of $V$. By using optimality conditions and irreducible
decomposition, we provide a positive answer to the following essential question
of polynomial optimization: Are there always exact semidefinite programs to
compute the minimum value attained by a given polynomial over a given real
algebraic variety? Our answer implies that Lasserre's hierarchy, which is known
as a bridge between convex and non-convex programs with algebraic structures,
has finite convergence not only in the generic case but also in the general
case. As a result, we constructively prove that each hyperbolic program is
equivalent to a semidefinite program.
