Modular Arithmetic (WIP)
It is a mathematical system that deals with integers and their remainders when divided by a specific positive number, known as the modulo.
Instead of traditional arithmetic, where numbers can grow indefinitely, modular arithmetic operates on the principle of congruence, which gives a cyclic behavior to the numerical groups limited by the modulus value.
It enables operations over bounded sets, supports cyclic structures, and ensures that results stay within predictable limits, making it ideal for environments with constraints on data size.
It is implemented in many areas of computer science and cryptography, particularly in:
Hashing algorithms
Checksums and error detection
Cryptographic protocols like RSA and Diffie-Hellman
Finite fields in elliptic curve cryptography
Here we explore some important concepts for this subject:
Operations
Modulo Operator: It is understood as the remainder of the division
Example: because and its remainder is
Addition: Perform subtraction, then apply the modulo
Example:
Subtraction: Perform subtraction, then apply modulo. If the result is negative, we wrap it around by adding to maintain it within the boundaries of the modulo
Example:
Multiplication: Multiply first, then apply modulo
Example:
Exponentiation: Repeated multiplication with modulo applied at each step
Example:
Greatest Common Divisor (GCD)
The GCD is the largest number that divides two positive integers. For example, with , and the divisors of are and the divisors of are . Comparing these two, we see that
If the for any pair of integer numbers and , we say that they are coprime numbers. This works in a particular way for prime numbers, as they only have themselves and as divisors, the everytime, so they will always be coprime.
Also, we note that if is prime and , then a and b are always coprime, because won't have any divisors lower than itself, but this won't always be true the other way ().
Calculating GCD with Code
Iterating from 1 to the smaller of the two numbers and checking which numbers divide both
def gcd(a, b):
gcd = 1
for i in range(1, min(a, b) + 1):
if a % i == 0 and b % i == 0: # Check if the remainder of both is 0
gcd = i
return gcd
print("Enter two numbers to find their GCD:")
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(" The GCD is:", gcd(a, b))
Using Python libraries for optimized versions
import math
print("Enter two numbers to find their GCD:")
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(" The GCD using math is:", math.gcd(a, b))
Using SageMath for optimized versions
from sage.all import *
print("Enter two numbers to find their GCD:")
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(" The GCD using math is:", gcd(a, b))
Congruence
Two integers and are said to be congruent modulo , if they leave the same remainder when divided by . This is written as
This could be understood in different ways:
and have the same remainder when divided by . For example, because and
and are congruent modulo because is divisible by With the valuces, because , and is divisible by
Exits a value that satisfies In the same way, is congruent with modulo because with it satifies
Congruence Equations
To find the value of in an equation we just have to solve
# Example for 8146798528947 ≡ b mod 17
b = 8146798528947 % 17
print(b) # Result is 4
# Or with sage
from sage.all import *
b = mod(8146798528947, 17)
print(b)
This found value works as for any value and the congruence will still be satisfied
For example, is solved because which lets us with
If we use we have and this lets us with which is true
Now, with we have and this lets us with which is also true
And so on with any value
To find the value of in an equation we just have to solve and any of its divisors will work.
def divisors(d):
divs = []
for i in range(1, int(d**0.5) + 1):
if d % i == 0:
divs.append(i)
if i != d // i:
divs.append(d//i)
return sorted(divs)
# Example
a, b = 27, 15
print(divisors(abs(a-b))) # The result is [1, 2, 3, 4, 6, 12]
For example is solved because and the divisors of are
If we use this lets us with which is true
If we use this lets us with which is also true
And so on with any divisor
To find the value of in an equation where we know the coefficient
#
Congruence System of Equations
We
Chinese remainder theorem
We
Modular Inverse
Given:
a⋅a−1≡1(modn)a \cdot a^{-1} \equiv 1 \pmod{n}a⋅a−1≡1(modn)
The modular inverse of
a
modulon
exists iffgcd(a, n) = 1
.It can be found using the Extended Euclidean Algorithm.
Example: 3−1 mod 11=43^{-1} \bmod 11 = 43−1mod11=4 because 3⋅4=12≡1(mod11)3 \cdot 4 = 12 \equiv 1 \pmod{11}3⋅4=12≡1(mod11)
This operation is crucial for decrypting messages in RSA.
def modinv(a, m):
gcd, x, _ = extended_gcd(a, m)
if gcd != 1:
raise Exception("Modular inverse does not exist")
else:
return x % m # Ensure positive inverse
# Example
print(modinv(3, 26)) # Output: 9 → because 3 * 9 ≡ 1 mod 26
Euclidean Algorithm
The GCD is the backbone of many fundamental operations in number theory, algebra, and cryptography. The Euclidean algorithm allows you to calculate it much faster than brute force methods — especially with large numbers.
🔸 Main Uses of the Euclidean Algorithm:
Find GCD(a, b): Quickly determine the largest integer that divides both aaa and bbb without remainder.
Check Coprimality: If gcd(a,b)=1\gcd(a, b) = 1gcd(a,b)=1, then aaa and bbb are coprime — an essential condition in modular arithmetic and cryptographic key generation.
Compute Modular Inverses (Extended Euclidean Algorithm): Solving equations like:
ax≡1(modm)ax \equiv 1 \pmod{m}ax≡1(modm)
requires gcd(a,m)=1\gcd(a, m) = 1gcd(a,m)=1, and the Euclidean algorithm is used to compute xxx.
Support for Cryptography: Algorithms like RSA require:
Computing GCDs
Finding coprime values
Deriving modular inverses
Simplifying Fractions: Reduce ab\frac{a}{b}ba to lowest terms by dividing numerator and denominator by their GCD.
🧠 Intuition Behind the Algorithm
If a number divides two others, it must also divide their difference. So instead of checking all possible divisors, we reduce the problem step by step:
gcd(a,b)=gcd(b,amod b)\gcd(a, b) = \gcd(b, a \mod b)gcd(a,b)=gcd(b,amodb)
This recursive reduction continues until the remainder is 0. The last non-zero number is the GCD
Here we find a coding implementation:
def gcd_euclidean(a, b):
while b != 0:
a, b = b, a % b
return a
def gcd_euclidean_recursive(a, b): # We can also do it using recursion
return a if b == 0 else gcd_recursive(b, a % b)
print("Enter two numbers to find their GCD:")
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(" The GCD is:", gcd_euclidean(a, b))
Extended Euclidean Algorithm
ffff
Here we find a coding implementation:
def extended_gcd(a, b): # Or its extended version with a⋅u+b⋅v=gcd(a,b)
old_r, r = a, b
old_s, s = 1, 0 # Coefficients for a
old_t, t = 0, 1 # Coefficients for b
while r != 0:
quotient = old_r // r
old_r, r = r, old_r - quotient * r
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t
# old_r is gcd, old_s is u, old_t is v
return old_r, old_s, old_t
print("Enter two numbers to find their GCD:")
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
gcd, u, v = extended_gcd(a, b)
print(f"The GCD is:{extended_gcd(a, b)}")
print(f"The Bezout Identity is {a}*{u} + {b}*{v} = {gcd}")
Last updated