  We present a comprehensive discussion on lattice techniques for the
simulation of scalar and gauge field dynamics in an expanding universe. After
reviewing the continuum formulation of scalar and gauge field interactions in
Minkowski and FLRW backgrounds, we introduce basic tools for the discretization
of field theories, including lattice gauge invariant techniques. Following, we
discuss and classify numerical algorithms, ranging from methods of $O(dt^2)$
accuracy like $staggered~leapfrog$ and $Verlet$ integration, to $Runge-Kutta$
methods up to $O(dt^4)$ accuracy, and the $Yoshida$ and $Gauss-Legendre$
higher-order integrators, accurate up to $O(dt^{10})$. We adapt these methods
for their use in classical lattice simulations of the non-linear dynamics of
scalar and gauge fields in an expanding grid in $3+1$ dimensions, including the
case of `self-consistent' expansion sourced by the volume average of the
fields' energy and pressure densities. We present lattice formulations of
canonical cases of: $i)$ Interacting scalar fields, $ii)$ Abelian $U(1)$ gauge
theories, and $iii)$ Non-Abelian $SU(2)$ gauge theories. In all three cases we
provide symplectic integrators, with accuracy ranging from $O(dt^2)$ up to
$O(dt^{10})$. For each algorithm we provide the form of relevant observables,
such as energy density components, field spectra and the Hubble constraint.
Remarkably, all our algorithms for gauge theories respect the Gauss constraint
to machine precision, including when `self-consistent' expansion is considered.
As a numerical example we analyze the post-inflationary dynamics of an
oscillating inflaton charged under $SU(2)\times U(1)$. The present manuscript
is meant as part of the theoretical basis for $CosmoLattice$, a modern C++
MPI-based package for simulating the non-linear dynamics of scalar-gauge field
theories in an expanding universe, publicly available at www.cosmolattice.net
