I'm working on a code that needs to perform modular multiplication of big numbers several times. Since the operation takes place several times, using division to find the remainder is very expensive. Repeated subtraction with the mod value also takes a lot of time because big numbers are involved.
The following commonly used modular multiplication algorithm is also slow
n = i
r = 0
for (bit = 0; bit < bitlength; bit++) {
if (bitset(j, bit)) {
r = (r + n) mod p
}
n = (n + n) mod p
}
So is there any other method to perform this quickly and efficiently?. I need expert advice on any alternative formula or shortcuts that can be used.