2

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.

abejoe
  • 605
  • 2
  • 6
  • 14
  • 4
    https://en.wikipedia.org/wiki/Montgomery_modular_multiplication ​ ​ –  Dec 08 '15 at 04:48
  • @RickyDemer Can u refer me a working algorithm for montgomery modular multiplication?. I read some links on it and couldnt derive a proper algorithm. – abejoe Dec 08 '15 at 05:12
  • No. ​ (I don't know of any.) ​ ​ ​ ​ –  Dec 08 '15 at 05:18
  • 1
    There is no such thing as the one single fastest method. There are several leading methods, and final cost/speed result depends on the hardware architecture. – Vadym Fedyukovych Dec 08 '15 at 11:46
  • 2
    Just use a well-established library such as GMP. – fkraiem Dec 08 '15 at 11:47
  • 1
    Take a look at the HAC's chapter on efficient implementations, it also contains Montgomery arithmetic. If you need a working example, you may want to take a look at Crypto++ which can use Montgomery arithmetic (disclosure: I'm co-working on it) – SEJPM Dec 08 '15 at 19:41
  • @fkraiem I doubt so as gmp lacks support modular multiplication. – user2284570 Aug 23 '22 at 22:05
  • @VadymFedyukovych there are however a current fastest way or implementation for achieving the goal. – user2284570 Aug 23 '22 at 22:06
  • @SEJPM I’m currently interested in the fastest way or implementation in practice which might use avx512 to achieve this (of course there are many naïve Montgomery implementations using simd which turns to be slower than gmp mul followed by mod). – user2284570 Aug 23 '22 at 22:10

0 Answers0