Modular Multiplication: How to Efficiently Calculate (a * b) mod c in C/C++?

  • Context: Undergrad 
  • Thread starter Thread starter chesschi
  • Start date Start date
  • Tags Tags
    Multiplication
Click For Summary
SUMMARY

The discussion focuses on efficiently calculating the expression "(a * b) mod c" in C/C++ using three 64-bit integers. The user outlines a method for breaking down the multiplication of two large numbers into smaller components, specifically using the formula: a = x1 * 2^32 + x0 and b = y1 * 2^32 + y0. The challenge highlighted is the potential for overflow when multiplying two 64-bit numbers, necessitating a careful approach to avoid exceeding the limits of 64-bit arithmetic. The user seeks a well-known algorithm that avoids using classes like BigInt or GMP.

PREREQUISITES
  • Understanding of 64-bit integer arithmetic in C/C++
  • Familiarity with modular arithmetic concepts
  • Knowledge of overflow issues in integer multiplication
  • Basic proficiency in C/C++ programming
NEXT STEPS
  • Research the "Montgomery Reduction" algorithm for modular multiplication
  • Learn about "Karatsuba multiplication" for efficient large number multiplication
  • Explore "Chinese Remainder Theorem" for modular calculations
  • Investigate "Split and Conquer" techniques for handling large integers in C/C++
USEFUL FOR

Software developers, particularly those working with cryptographic algorithms, numerical methods, or anyone needing to perform efficient modular arithmetic in C/C++.

chesschi
Messages
3
Reaction score
0
I want to write a C/C++ program and encounter a problem.If I have three 64 bit numbers and need to manipulate
"a * b mod c"

Is there any well-known, efficient and simple algorithm to implement it?

I only know it can be calculated sth. like this..
__int64 a, b, c
a = x1 * 2^32 + x0
b = y1 * 2^32 + y0

then a * b = x1y1 * 2^64 + (x0y1 + x1y0) * 2^32 + x0y0

but I don't know what to do in the next step

Note that this cannot be calculated using some classes like BigInt, gmp

Thank you very much!
 
Mathematics news on Phys.org
chesschi said:
I want to write a C/C++ program and encounter a problem.If I have three 64 bit numbers and need to manipulate
"a * b mod c"

Is there any well-known, efficient and simple algorithm to implement it?

I only know it can be calculated sth. like this..
__int64 a, b, c
a = x1 * 2^32 + x0
b = y1 * 2^32 + y0
C, C++, C#, and other languages that are based on C have a modulus operator, %

C:
unsigned long long a, b, c;
// Assign values to a, b, and c
x =  a * b % c;
One thing to be concerned about is that if a and b are larger than 32 bits, their product won't fit in 64 bits, and you'll get overflow.
chesschi said:
then a * b = x1y1 * 2^64 + (x0y1 + x1y0) * 2^32 + x0y0

but I don't know what to do in the next step

Note that this cannot be calculated using some classes like BigInt, gmp

Thank you very much!
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
2
Views
4K
  • · Replies 13 ·
Replies
13
Views
4K