I am trying to understand modular exponentiation and its implementation in C from here
On my way of understanding it I came across the following equation: $$(x^{n/2}\bmod M)^2\equiv(x^2\bmod M)^{n/2}$$ I am not able to understand how are we arriving from LHS to RHS.
It is not true. Take $n=2$, $M=5$, $x=2$. Then $x^{n/2}=x$, your equality now would be $$ (2\mod 5)^2=(4\mod 5)^2, $$ which is not true (the left-hand-side is $4$, while the right -hand-side is $1$).