CSC316 Cryptography

CryptographyUnit 27 min read

Number Theory & Math Foundations: Primes, GCD, RSA, ElGamal

Unit 2 of Cryptography covers the mathematical backbone of modern cryptosystems: prime numbers, modular arithmetic, Euler’s totient function, GCD/LCM algorithms, Fermat’s Little Theorem, and primality tests—all essential for RSA, ElGamal, and Diffie-Hellman protocols.

Core Concepts & Definitions

1. Prime Numbers & Primality Testing

Definition: A prime number is a natural number >1 divisible only by 1 and itself. Primes are the building blocks of RSA and ElGamal.

Why primes?

  • RSA: Relies on the hardness of factoring large primes (e.g., ).
  • ElGamal: Uses a prime and primitive root for secure key exchange.
  • Diffie-Hellman: Requires a prime and generator to prevent MITM attacks.

Primality Tests

Test Time Complexity Use Case
Trial Division Small numbers (e.g., )
Fermat’s Test Probabilistic check (fast but flawed)
Miller-Rabin Gold standard for large primes (used in Bitcoin, TLS)

Example: Miller-Rabin Test for

  1. Factor .
  2. Pick a witness .
  3. Compute where : (passes). But fails → 561 is composite (Carmichael number).
stateDiagram-v2
    [*] --> IsPrime: Check if n > 1
    IsPrime --> TrialDivision: For small n
    IsPrime --> FermatTest: Fast but unreliable
    IsPrime --> MillerRabin: Reliable (k rounds)
    MillerRabin --> Pass: If all tests pass
    MillerRabin --> Fail: If any witness fails

2. Modular Arithmetic & Congruences

Definition: means divides . Key Properties:

Example: RSA Encryption

  • Public key: (where ).
  • Encrypt : . Compute step-by-step: , , , , .

3. Euler’s Totient Function

Definition: Counts integers up to coprime with . Formula:

  • If , then .

Example: .

Role in RSA:

  • Private key is the modular inverse of modulo : .

4. Greatest Common Divisor (GCD) & Euclidean Algorithm

Definition: Largest integer dividing two numbers without remainder. Euclidean Algorithm:

  1. Divide by , get remainder .
  2. Replace with , with .
  3. Repeat until . GCD is the last non-zero remainder.

Example:

16 = 1 × 12 + 4
12 = 3 × 4 + 0 → GCD = 4

Extended Euclidean Algorithm: Finds integers such that . Use in RSA: Computes the private key .


In the Real World

  1. eSewa & Khalti (Nepal)

    • Use RSA for secure payment authentication. When you log in, your device generates a session key encrypted with eSewa’s public key (derived from large primes). The private key (kept secret) decrypts it to verify your identity.
    • Example: If eSewa’s modulus (where are 2048-bit primes), breaking RSA would require factoring , which is computationally infeasible.
  2. Ncell & NTC (Nepal)

    • Diffie-Hellman (DH) secures your 4G/5G connection. When your phone connects to a tower, it exchanges and to derive a shared secret key for encryption. An eavesdropper (MITM) would need to solve the Discrete Logarithm Problem (DLP), which is hard for large primes.
  3. YouTube (Global)

    • Digital Signatures (DSA/ECDSA) verify video uploads. When you upload a video, YouTube signs it with its private key. Viewers use the public key to verify the signature, ensuring the video hasn’t been tampered with. This relies on Euler’s theorem and primitive roots in finite fields.

Worked Example: ElGamal Encryption (Nepal Context)

Scenario: A farmer in Pokhara wants to send a secret message to a buyer via Pathao’s secure chat (hypothetical). The system uses ElGamal.

  • Public params: (prime), (primitive root), (buyer’s public key).
  • Farmer’s private key: , random .
  • Message: "CSIT" → (C=2, S=18, I=8, T=19; here, we encrypt "C" as 2).

Steps:

  1. Compute shared secret: .
  2. Encrypt :
    • .
    • . Compute : , , , . So, .
  3. Ciphertext: .

Decryption by Buyer:

  • Buyer computes . , , , .
  • Then, . . is the inverse of 41, which is 77 (since ). .

Comparison: Symmetric vs. Asymmetric Cryptography

Feature Symmetric (AES) Asymmetric (RSA/ElGamal)
Key Size 128–256 bits 2048–4096 bits
Speed Fast Slow
Key Distribution Shared secret Public/private keys
Use Case Encrypting data Key exchange, signatures
Math Foundation Block ciphers Primes, GCD, Euler’s

Exam Tip

  1. Memorize Definitions:

    • Prime, totient function , congruence, GCD, and their formulas.
    • Fermat’s Little Theorem: if is prime and .
  2. Algorithms:

    • Euclidean Algorithm: Practice on .
    • Miller-Rabin: Show steps for (Carmichael number).
  3. Worked Examples:

    • RSA: Given , compute and encrypt/decrypt.
    • ElGamal: Given , encrypt/decrypt a message.
    • Primality Test: Apply Miller-Rabin to (prime) and (composite).
  4. Real-World Links:

    • eSewa/Khalti: RSA for authentication.
    • Ncell/NTC: DH for secure key exchange.
    • YouTube: DSA for digital signatures.
  5. Common Pitfalls:

    • Forgetting to check in Fermat’s theorem.
    • Misapplying the Euclidean algorithm (e.g., not updating and correctly).
    • Confusing primitive roots (used in ElGamal/DH) with just any generator.

Diffie-Hellman key exchange**Diagram of Alice and Bob exchanging and to derive . (Image: de:Benutzer:DaMutz, CC BY-SA 4.0, via Wikimedia Commons)

Based on the TU BSc CSIT syllabus for Cryptography (CSC316), unit 2.

Discussion

Loading…