I am not sure if I should ask this question here or somewhere else. In fact, I initially asked my question here at mathoverflow.net but it was marked as off-topic
Background: I was searching through random mathematics paper that are related to cryptography and I came across this paper (page 3). I just read the abstract and algorithm itself, I don't understand Chinese. It offers new method to find a Modular inverses. It has some interesting properties that I observed:
- during each step iteration of the loop: $x_{11} * x_{22} + x_{12} * x_{21} = m$ which is good to validate the result during each iteration
- algorithm terminates in even number of steps for some unknown reason
In abstract section, author says this method was invented by this mathematicians.
My question is, why does this algorithm always terminate in aeven number of steps (or number of iterations of the loop is always even)?
Algorithm to calculate: $a^{-1} (\bmod m)$:
$\text{xgcd}(a, m):$
$\quad x_{11} \leftarrow 1, x_{21} \leftarrow 0, x_{12} \leftarrow a, x_{22} \leftarrow m$
$\quad \text{While }(x_{12} > 1) \text{ do}$
$\quad \quad \text{If }(x_{22} > x_{12}) \text{ then}$
$\quad\quad\quad\quad q \leftarrow \Big\lfloor\frac{x_{22} - 1}{x_{12}}\Big\rfloor$
$\quad\quad\quad\quad r \leftarrow x_{22} - q ~x_{12}$
$\quad\quad\quad\quad \begin{pmatrix}x_{11} & x_{12}\\x_{21} & x_{22}\end{pmatrix} \leftarrow \begin{pmatrix}x_{11} & x_{12}\\q~x_{11} + x_{21} & r\end{pmatrix} $
$\quad\quad \text{If }(x_{12} > x_{22}) \text{ then}$
$\quad\quad\quad\quad q \leftarrow \Big\lfloor\frac{x_{12} - 1}{x_{22}}\Big\rfloor$
$\quad\quad\quad\quad r \leftarrow x_{12} - q~x_{22}$
$\quad\quad\quad\quad \begin{pmatrix}x_{11} & x_{12}\\x_{21} & x_{22}\end{pmatrix} \leftarrow \begin{pmatrix}q~x_{21} + x_{11} & r\\x_{21} & x_{22}\end{pmatrix}$
$\quad \text{Return } x_{11}$
- Screenshot of the algorithm
- Python and SageMath implementations