Why am I allowed to do this? Do you have a reference? $$a^{-1}\mod p \equiv a^{p-2}\mod p$$ Where do I get this? From here
m = (Q_y - N_y) * pow((Q_x-N_x), p-2, p)
It is point addition in EC (calculating the slope).
Why am I allowed to do this? Do you have a reference? $$a^{-1}\mod p \equiv a^{p-2}\mod p$$ Where do I get this? From here
m = (Q_y - N_y) * pow((Q_x-N_x), p-2, p)
It is point addition in EC (calculating the slope).
On
Notice $a^{p-1} = a*a^{p-2}$ (assuming $p-1 > 0$)
So if $a^{-1}$ exists then
$a^{-1}*a^{p-1} \equiv a^{-1}(a*a^{p-2})\equiv (a^{-1}*a)a^{p-2}\equiv 1*a^{p-2}\equiv a^{p-2}\pmod p$.
But $a^{-1}$ can only exist if $\gcd(a,p) = 1$.
Now Fermat's little Theorem states. If $p$ is prime and if $\gcd(a,p) = 1$ then
$a^{p-1} \equiv 1 \pmod p$>
So.......
$a^{-1} \equiv a^{-1}*1 \equiv a^{-1}*a^{p-1}\equiv a^{-1}*a*a^{p-2}\equiv 1*a^{p-2}\equiv a^{p-2}\pmod p$.
That's all.
According to Fermat's little theorem, if $p$ does not divide $a$, then $a^{p-1}\equiv 1\bmod p$.
Again, if $p$ does not divide $a$, we can multiply both sides by $a^{-1}$ to get the desired result.