I've written Haskell function, that helps me to compute $x^{-1} \mod m$.
invM x m = invMhelp x 0 m; --modular multiplicative inverse x^-1 (mod m) = inv x m
invMhelp x i m = if (x*i `mod` m == 1) then i else (invMhelp x (i+1) m);
For example:
Main> 13 `invM` 2109
649
That means $13^{-1} = 649 \mod 2109$.
How can I compute $x^{-2} \mod m$? ($x^{-2} \mod m = (x^{-1})^2 \mod m$)
Can I simply use the power of two of the multiplicative inverse and then apply the modulo m again?
Example: $13^{-1} = 649 \mod 2109$, $649^2 \mod 2109 = 1510$.
Is $13^{-2} \mod 2109$ equal to $1510$?
Sorry for the noob question, but I'm not sure about this. Thanks for answers.