Given a
, b
and m
, all of type u64
(or u128
), I want to calculate (a * b) % m
. However, a
and b
may be large, so an overflow may occur.
While I could cast them to BigUInt
, multiply them and then cast the result back, this seems a bit unelegant to me. (Since the casting to BigUInt
is theoretically unnecessary.)
I also found the modular arithmetic crate, but it seems to be unmaintained, undocumented and the multiplication is very slow in case of an overflow. Also, there is the modular crate, but there an overflow can occur.
So, is there a more elegant way to do modular multiplication in Rust?