Specific Modular Multiplication Algorithm

I have 3 large 64-bit numbers: A, B and C. I want to calculate:

(A x B) mod C

given that my registers are 64 bits, i.e. the entry a * bactually gives (A x B) mod 2⁢⁴.

What is the best way to do this? I code in C, but I don’t think that in this case the language matters.


After receiving comments in the comments pointing to this decision:

(a * b) % c == ((a % c) * (b % c)) % c

Let me be specific: this is not a solution because ((a% c) * (b% c)) can still be more than 2⁢⁴ and the register will still overflow and give me the wrong answer. I'd:

(((mod mod C) x (B mod C)) mod 2⁢⁴) mod C

+4
source share
1 answer

, . , .

A = (A1 < 32) + A2

B = (B1 < 32) + B2.

:

A * B = ((A1 * B1) < 64) + ((A1 * B2 + A2 * B1) < 32) + A2 * B2.

, 3 , , 2 ^ 64, .

!

, 64 , , . .

, C 2 ^ 63, , .

+8

All Articles