Modular Arithmetic and prime numbers With respect to the maths behind the Diffie Hellman Key exchange algorithm.  
Why does:
(ga mod p)b mod p = gab mod p
It might be fairly obvious, but what basic maths guarantees this? Why does the first modulo p expression disappear from the LHS in the expression on the RHS.
Apologies if it's not the right forum to ask it in. Maybe I should try the Information security stack Exchange forum instead.

We can actually use the binomial theorem:
Observe that if we write $g^a=m+pk$, where $m\equiv g^a\mod p$, then $g^{ab}=(m+pk)^b=\sum_{n=0}^b \binom{b}{n} m^n(pk)^{n-b}\equiv m^b\mod p$, as every term except for the $n=b$ term will we divisible by $p$. Therefore, $(g^a\mod p)^b\mod p\equiv m^b\mod p$ and on the other hand $g^{ab}\mod p\equiv m^b$.
