  Kloosterman sums play a special role in analytic number theory, for
expressing the integer Fourier coefficients of modular forms as an infinite sum
of Bessel functions, also known as Rademacher formula. The generalization to
vector-valued modular forms is known as generalized Kloosterman sums. In the
paper arxiv:1404.0033, a remarkable connection between these arithmetic sums
and quantum black hole entropy was found. Nevertheless, the computation was
particular for one-eighth BPS black holes in $\mathcal{N}=8$ string theory,
which have a simple counting formula. Here, we review this construction and
extend it to the case of one-quarter BPS black holes in $\mathcal{N}=4$ string
theory, which are counted by (mock) Jacobi forms of arbitrary index. The main
result is an holographic derivation of the Kloosterman sums which includes the
intricate sum over phases and depends exactly on the spectral flow sectors and
the spectrum of polar states. On the microscopic side we derive an analytic
formula for the Kloosterman sums valid for any index, whereas from the
macroscopic side we reproduce the same formula from the M-theory path integral
on $\mathbb{Z}_c$ orbifolds of $AdS_2\times S^1$. A key aspect of the
derivation is the identification of the spectral flow sectors with the
contribution of M2 branes wrapping cycles on the compactification manifold.
After a careful treatment of the measure, the sum over orbifolds results in the
sum over Bessels, in perfect agreement with the Rademacher expansion at any
order in perturbation theory.
