🤖 AI Summary
This paper addresses the efficient computation of multiplicative modular inverses $a^{-1} mod n^k$ for arbitrary integer moduli $n^k$ ($n>1$), overcoming the classical limitation that restricts existing algorithms to prime moduli. We propose the first general-purpose modular inversion algorithm for arbitrary base $n$, combining $p$-adic expansion with exact linear equation solving. The method relies solely on bit shifts, additions, and hardware-friendly built-in arithmetic operations, eliminating all division and modular reduction steps. It is naturally suited to large-word architectures (e.g., $n = 2^{64}$), achieving both theoretical generality and practical efficiency. Experimental evaluation demonstrates substantial speedup over the classic Koc algorithm—particularly pronounced for large word sizes—while maintaining correctness and numerical stability. Additionally, we provide a novel, rigorous proof of correctness for the Koc algorithm.
📝 Abstract
Recently, Koc proposed a neat and efficient algorithm for computing $x = a^{-1} pmod {p^k}$ for a prime $p$ based on the exact solution of linear equations using $p$-adic expansions. The algorithm requires only addition and right shift per step. In this paper, we design an algorithm that computes $x = a^{-1} pmod {n^k}$ for any integer $n>1$. The algorithm has a motivation from the schoolbook multiplication and achieves both efficiency and generality. The greater flexibility of our algorithm is explored by utilizing the build-in arithmetic of computer architecture, e.g., $n=2^{64}$, and experimental results show significant improvements. This paper also contains some results on modular inverse based on an alternative proof of correctness of Koc algorithm.