# Gauge freedom in Lagrangian corresponds to canonical transformation of Hamiltonian

I want to show that the gauge transformation

$$L(q,\dot{q},t)\mapsto L^\prime(q,\dot{q},t):=L(q,\dot{q},t)+\frac{d}{dt}f(q, t)$$

corresponds to a canonical transformation of the Hamiltonian $$H(p, q, t)=p^i\dot{q}_i-L$$.

First, I calculate the new momenta: $$p^\prime_i=\frac{\partial L^\prime}{\partial\dot{q}^i}=\frac{\partial L}{\partial\dot{q}^i}+\frac{\partial}{\partial\dot{q}^i}\frac{df}{dt}=p_i+\frac{\partial}{\partial\dot{q}^i}\left(\frac{\partial f}{\partial q^i}\dot{q}^i+\frac{\partial f}{\partial t}\right)=p_i+\frac{\partial f}{\partial q^i}.$$

Then, I calculate the transformed Hamiltonian: $$H^\prime=\frac{\partial L}{\partial\dot{q}^i}-L^\prime=\left(p_i+\frac{\partial f}{\partial q^i}\right)\dot{q}^i-L-\frac{df}{dt}\\ =p_i\dot{q}^i-L+\frac{\partial f}{\partial q^i}\dot{q}^i-\frac{\partial f}{\partial q^i}\dot{q}^i-\frac{\partial f}{\partial t}=H-\frac{\partial f}{\partial t}.$$

Now I want to show that this transformation is indeed canonical, i.e. the Hamilton equations are covariant under such transformations.

For the position components this works out because they are left unchanged by the gauge transformation. However, for the generalised momenta, what I come up with is obviously not covariant: $$\dot{p}^\prime_i=\frac{\partial H^\prime}{\partial q^i}=\frac{\partial H}{\partial q^i}-\frac{\partial}{\partial q^i}\frac{\partial f}{\partial t}=\frac{\partial H}{\partial q^i}-\frac{\partial}{\partial t}\frac{\partial f}{\partial q^i}=\dot{p}_i-\frac{\partial}{\partial t}\frac{\partial f}{\partial q^i}.$$

Where lies my mistake?