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 security

Discrete Log (DH)

3072-bit security

Elliptic Curve (ECC)

256-bit security

In 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.

a ≡ b (mod n) ⇔ n | (a - b)

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

1
Basic Operations

Modular arithmetic preserves addition, subtraction, and multiplication:

If a ≡ b (mod n) and c ≡ d (mod n), then:
a + c ≡ b + d (mod n)
a - c ≡ b - d (mod n)
a × c ≡ b × d (mod n)
2
Modular Inverse

The modular inverse of a modulo n is a number b such that:

a × b ≡ 1 (mod n)

This exists only if gcd(a, n) = 1 (a and n are coprime).

Modular Arithmetic Calculator

Enter values and click "Calculate"

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):

c ≡ me (mod 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:

m ≡ cd (mod n)

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

Enter values and click "Run RSA Demo"
Why RSA Works: Euler's Theorem

RSA's correctness relies on Euler's theorem:

If gcd(a, n) = 1, then aφ(n) ≡ 1 (mod n)

For RSA with n = pq and φ(n) = (p-1)(q-1):

cd ≡ (me)d ≡ med ≡ m1 + kφ(n) ≡ m × (mφ(n))k ≡ m (mod n)

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

Click "Simulate Key Exchange" to see the protocol in action
Discrete Logarithm Problem

Diffie-Hellman's security relies on the hardness of:

Given g, p, and gx mod p, find x

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):

y² ≡ x³ + ax + b (mod p)

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:

λ = (y₂ - y₁)(x₂ - x₁)⁻¹ mod p
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

Enter curve parameters and click "Visualize Curve"
Key Size Comparison

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:

function modExp(base, exp, mod) {
  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:

// Test if n is prime (k iterations)
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:

function extendedGcd(a, b) {
  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:

// Blum Blum Shub (concept)
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

Security Recommendations
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

Select algorithm and key size, then click "Calculate Security"

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

Implementation Considerations
  • 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"

Challenge: Encrypt the message m = 42 using RSA with n = 3233, e = 17. Then decrypt the ciphertext to verify.

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

Challenge: In Diffie-Hellman with p=23, g=5, if Alice chooses a=6 and Bob chooses b=15, what is their shared secret?

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:

Enc(m₁) ⊙ Enc(m₂) = Enc(m₁ ⊕ m₂)

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:

Prove: "I know x such that gx ≡ y (mod p)"

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.