Introduction to Cryptography Modulo
Modular arithmetic forms the mathematical foundation of modern cryptography. From securing online transactions to protecting government communications, modular operations enable the encryption algorithms that keep our digital world safe.
Why Modular Arithmetic in Cryptography?
- One-way functions: Easy to compute in one direction, hard to reverse
- Trapdoor functions: Easy with secret key, hard without
- Finite fields: Provide mathematical structure for security
- Computational hardness: Based on number theory problems
- Efficiency: Modular operations are computationally efficient
Cryptographic Security Levels
Modern cryptography relies on the computational hardness of modular arithmetic problems:
Factorization (RSA)
2048-bit securityDiscrete Log (DH)
3072-bit securityElliptic Curve (ECC)
256-bit securityIn this comprehensive guide, we'll explore how modular arithmetic enables modern cryptography, with practical examples, interactive tools, and security analysis.
Modular Arithmetic Basics
Modular arithmetic, also known as "clock arithmetic," deals with remainders after division. It's the mathematical foundation for all modern public-key cryptography.
Where:
- a, b are integers
- n is the modulus (n > 0)
- ≡ means "congruent to"
- | means "divides"
Examples:
17 ≡ 5 (mod 12) because 17 - 5 = 12, and 12 divides 12
23 ≡ 2 (mod 7) because 23 - 2 = 21, and 7 divides 21
100 ≡ 1 (mod 11) because 100 - 1 = 99, and 11 divides 99
Modular arithmetic preserves addition, subtraction, and multiplication:
a + c ≡ b + d (mod n)
a - c ≡ b - d (mod n)
a × c ≡ b × d (mod n)
The modular inverse of a modulo n is a number b such that:
This exists only if gcd(a, n) = 1 (a and n are coprime).
Modular Arithmetic Calculator
Enhance your learning by practicing real problems with the modulo-calculator.
RSA Cryptography
RSA (Rivest-Shamir-Adleman) is the most widely used public-key cryptosystem, based on the practical difficulty of factoring large integers.
Key Generation
Step 1: Choose two large primes p and q
Step 2: Compute n = p × q
Step 3: Compute φ(n) = (p-1)(q-1)
Step 4: Choose e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1
Step 5: Compute d = e⁻¹ mod φ(n)
Public key: (n, e), Private key: (n, d)
Encryption
For message m (0 ≤ m < n):
Example: m = 65, e = 17, n = 3233
c = 6517 mod 3233 = 2790
Anyone can encrypt using the public key (n, e)
Decryption
For ciphertext c:
Example: c = 2790, d = 2753, n = 3233
m = 27902753 mod 3233 = 65
Only the private key holder can decrypt
Security
Strength: Based on integer factorization
Key sizes: 2048-bit recommended (617 decimal digits)
Attack resistance: Resists all known classical attacks
Quantum threat: Vulnerable to Shor's algorithm
Factoring n reveals private key d
RSA Demonstration
RSA's correctness relies on Euler's theorem:
For RSA with n = pq and φ(n) = (p-1)(q-1):
Because ed ≡ 1 (mod φ(n)) ⇒ ed = 1 + kφ(n) for some integer k.
Take your understanding further by exploring the modulo-calculator.
Diffie-Hellman Key Exchange
The Diffie-Hellman protocol allows two parties to establish a shared secret over an insecure channel, based on the discrete logarithm problem.
Protocol Setup
Public parameters:
• Large prime p (modulus)
• Generator g (primitive root modulo p)
Example: p = 23, g = 5
These can be reused by many parties
Alice's Steps
1. Choose private key a (random)
2. Compute public key A = ga mod p
3. Send A to Bob
4. Receive B from Bob
5. Compute shared secret s = Ba mod p
Example: a = 6, A = 56 mod 23 = 8
Bob's Steps
1. Choose private key b (random)
2. Compute public key B = gb mod p
3. Send B to Alice
4. Receive A from Alice
5. Compute shared secret s = Ab mod p
Example: b = 15, B = 515 mod 23 = 19
Shared Secret
Alice computes: s = Ba mod p
Bob computes: s = Ab mod p
Both get: s = gab mod p
Example: s = 196 mod 23 = 2
Eavesdroppers cannot compute s without a or b
Diffie-Hellman Simulator
Diffie-Hellman's security relies on the hardness of:
This is the discrete logarithm problem (DLP).
| Prime Size (bits) | Security Level | Example Use |
|---|---|---|
| 1024 | 80-bit (deprecated) | Legacy systems |
| 2048 | 112-bit | Current standard |
| 3072 | 128-bit | High security |
| 15360 | 256-bit | Post-quantum |
Measure your knowledge with hands-on scenarios using the modulo-calculator.
Elliptic Curve Cryptography (ECC)
Elliptic curve cryptography provides stronger security with smaller key sizes compared to RSA and Diffie-Hellman, based on the elliptic curve discrete logarithm problem.
Elliptic Curve Equation
Over finite field Fp (p > 3 prime):
Conditions: 4a³ + 27b² ≢ 0 (mod p)
Example: y² ≡ x³ + 2x + 3 (mod 97)
Points on the curve form an abelian group
Point Addition
Given points P = (x₁, y₁) and Q = (x₂, y₂):
If P ≠ Q:
x₃ = λ² - x₁ - x₂ mod p
y₃ = λ(x₁ - x₃) - y₁ mod p
If P = Q (point doubling): different λ
ECDH Key Exchange
Public: Curve E, base point G
Alice: Private a, public A = aG
Bob: Private b, public B = bG
Shared secret: S = aB = bA = abG
Security: Hard to find a from A = aG
This is the elliptic curve DLP
ECC Advantages
Smaller keys: 256-bit ECC ≈ 3072-bit RSA
Faster computation: Less CPU intensive
Less bandwidth: Smaller signatures
Stronger security: Best known attacks exponential
Widely used: TLS 1.3, Bitcoin, SSH
Elliptic Curve Visualization
ECC provides equivalent security with much smaller key sizes:
| Security Level (bits) | RSA Key Size | ECC Key Size | Ratio |
|---|---|---|---|
| 80 | 1024 | 160 | 6:1 |
| 112 | 2048 | 224 | 9:1 |
| 128 | 3072 | 256 | 12:1 |
| 256 | 15360 | 512 | 30:1 |
This makes ECC ideal for constrained environments like mobile devices and IoT.
Challenge yourself with real-world tasks on the modulo-calculator.
Cryptographic Algorithms
Efficient algorithms are essential for practical cryptography. Here are key algorithms used in modular cryptography.
Modular Exponentiation
Square-and-Multiply Algorithm:
let result = 1;
base = base % mod;
while (exp > 0) {
if (exp & 1) {
result = (result * base) % mod;
}
exp = exp >> 1;
base = (base * base) % mod;
}
return result;
}
Time complexity: O(log exp)
Miller-Rabin Primality Test
Probabilistic test for prime generation:
function isPrime(n, k) {
if (n <= 1) return false;
if (n <= 3) return true;
// Write n-1 = 2^s * d
let d = n - 1;
let s = 0;
while (d % 2 == 0) {
s++;
d = Math.floor(d / 2);
}
// Test k times
for (let i = 0; i < k; i++) {
let a = 2 + Math.floor(Math.random() * (n - 4));
let x = modExp(a, d, n);
if (x == 1 || x == n - 1) continue;
// Continue with squaring...
}
return true;
}
Error probability: ≤ 4-k
Extended Euclidean Algorithm
Finds modular inverses and gcd:
if (b == 0) {
return [a, 1, 0];
}
let [gcd, x1, y1] = extendedGcd(b, a % b);
let x = y1;
let y = x1 - Math.floor(a / b) * y1;
return [gcd, x, y];
}
// Modular inverse: a⁻¹ mod m
function modInverse(a, m) {
let [gcd, x, y] = extendedGcd(a, m);
if (gcd != 1) return null;
return (x % m + m) % m;
}
Time complexity: O(log min(a, b))
Random Number Generation
Cryptographic PRNG:
function blumBlumShub(seed, n, p, q) {
let m = p * q;
let x = seed % m;
let bits = '';
for (let i = 0; i < n; i++) {
x = (x * x) % m;
bits += (x & 1).toString();
}
return parseInt(bits, 2);
}
Security: Based on quadratic residuosity
In practice: Use /dev/urandom or crypto.getRandomValues()
Challenge yourself with real-world tasks on the modulo-calculator.
Security Analysis
Understanding the security of cryptographic systems requires analyzing potential attacks and their computational requirements.
Factorization Attacks (RSA)
General Number Field Sieve (GNFS):
Complexity: exp((64/9)1/3 (log n)1/3 (log log n)2/3)
2048-bit RSA: ~2112 operations
Status: Secure with proper key sizes
Discrete Log Attacks (DH)
Index Calculus:
Complexity: similar to GNFS
3072-bit DH: ~2128 operations
Pohlig-Hellman: Avoid small subgroups
Status: Secure with safe primes
Elliptic Curve Attacks
Best known: Pollard's Rho
Complexity: O(√n) group operations
256-bit ECC: ~2128 operations
MOV attack: Avoid supersingular curves
Status: Very secure
Quantum Attacks
Shor's Algorithm:
Factorization: O((log n)3)
Discrete log: O((log n)3)
Elliptic curve DLP: O((log n)3)
Status: Breaks all current public-key crypto
| Algorithm | Minimum Key Size | Recommended Key Size | Security Until |
|---|---|---|---|
| RSA | 2048 bits | 3072 bits | 2030 |
| Diffie-Hellman | 2048 bits | 3072 bits | 2030 |
| DSA | 2048 bits | 3072 bits | 2030 |
| ECC | 224 bits | 256 bits | 2030+ |
| EdDSA (Ed25519) | 256 bits | 256 bits | 2030+ |
Security Strength Calculator
Explore practical applications and test your knowledge with the modulo-calculator.
Practical Applications
Modular cryptography is everywhere in modern technology. Here are key real-world applications.
TLS/SSL (HTTPS)
Key exchange: ECDHE or DHE
Authentication: RSA or ECDSA signatures
Protocols: TLS 1.2, TLS 1.3
Usage: Secures web traffic
Every HTTPS connection uses modular crypto
Example: Your bank's website
Digital Signatures
Algorithms: RSA-PSS, ECDSA, EdDSA
Applications:
• Software updates (code signing)
• Email (S/MIME, PGP)
• Documents (PDF signing)
• Cryptocurrency transactions
Provides authenticity and non-repudiation
Secure Messaging
Protocols: Signal, WhatsApp, iMessage
Techniques:
• Double Ratchet (ECDH + KDF)
• X3DH key agreement
• Forward secrecy
• Post-compromise security
End-to-end encryption for messages
Cryptocurrencies
Bitcoin: ECDSA with secp256k1
Ethereum: Same curve, different address format
Applications:
• Transaction signing
• Address generation
• Smart contracts
• Zero-knowledge proofs
Public-key crypto enables ownership proof
- Side-channel attacks: Timing, power analysis, cache attacks
- Random number generation: Critical for key generation
- Constant-time implementations: Prevent timing attacks
- Key management: Secure storage and rotation
- Protocol design: Avoid cryptographic pitfalls
- Standards compliance: Follow NIST, FIPS, RFC guidelines
Interactive Tools
Cryptography Workbench
Experiment with modular cryptography operations and algorithms.
Select an operation and provide inputs, then click "Execute"
Solution:
1. Encryption: c = me mod n = 4217 mod 3233
2. Compute using square-and-multiply:
42² mod 3233 = 1764
42⁴ mod 3233 = 1764² mod 3233 = 3111696 mod 3233 = 2916
Continue... final result: c = 2557
3. For decryption, we need d = e⁻¹ mod φ(n)
With p=61, q=53, φ(n)=60×52=3120
d = 17⁻¹ mod 3120 = 2753
4. Decryption: m = cd mod n = 25572753 mod 3233 = 42
Solution:
1. Alice computes A = ga mod p = 56 mod 23
5² mod 23 = 2
5⁴ mod 23 = 2² mod 23 = 4
5⁶ mod 23 = 4 × 5² mod 23 = 4 × 2 mod 23 = 8
So A = 8
2. Bob computes B = gb mod p = 515 mod 23
5⁸ mod 23 = (5⁴)² mod 23 = 4² mod 23 = 16
5¹⁵ mod 23 = 5⁸ × 5⁴ × 5² × 5¹ mod 23 = 16 × 4 × 2 × 5 mod 23
= 640 mod 23 = 19 (since 23×27=621, remainder 19)
So B = 19
3. Shared secret: s = Ba mod p = 196 mod 23 = 2
Or s = Ab mod p = 815 mod 23 = 2
Explore practical applications and test your knowledge with the modulo-calculator.
Advanced Topics
Beyond basic modular cryptography, several advanced topics build on these foundations.
Homomorphic Encryption
Allows computation on encrypted data:
Applications:
• Secure cloud computing
• Private data analysis
• Encrypted search
Schemes: Paillier, BGV, CKKS
Based on modular arithmetic properties
Zero-Knowledge Proofs
Prove knowledge without revealing it:
Applications:
• Anonymous credentials
• zk-SNARKs (Zcash)
• Privacy-preserving authentication
Based on: Discrete log, RSA, or elliptic curves
Post-Quantum Cryptography
Algorithms secure against quantum computers:
Lattice-based: NTRU, Kyber, Dilithium
Code-based: McEliece, BIKE
Hash-based: SPHINCS+
Multivariate: Rainbow
NIST standardization ongoing (2022-2024)
Still uses modular arithmetic extensively
Threshold Cryptography
Distribute cryptographic operations:
Threshold signatures: t-of-n parties needed
Applications:
• Multi-sig wallets
• Distributed key generation
• Secure multi-party computation
Based on: Shamir's secret sharing + modular crypto
Enhances security and availability
Refine your understanding through guided practice with the modulo-calculator.