Here is an implementation with the double addition of a * b % m multiplication, without overflow, regardless of the size of a, b and m.
(Note that the lines res -= m and temp_b -= m rely on a 64-bit unsigned integer overflow to produce the expected results. This should be good, because an unrecognized integer overflow is correctly defined in C and C ++. The reason is important for use unsigned integers .)
uint64_t mulmod(uint64_t a, uint64_t b, uint64_t m) { uint64_t res = 0; uint64_t temp_b; if (b >= m) { if (m > UINT64_MAX / 2u) b -= m; else b %= m; } while (a != 0) { if (a & 1) { if (b >= m - res) res -= m; res += b; } a >>= 1; temp_b = b; if (b >= m - b) temp_b -= m; b += temp_b; } return res; }
This is my modification of another answer to another similar question .
Craig McQueen
source share