CACS459 Information Security

Information SecurityUnit 510 min read

Number Theory & Math Foundations for Cryptography

Unit 5 of Information Security covers modular arithmetic, prime numbers, Fermat’s Little Theorem, Euler’s Totient, RSA’s mathematical underpinnings, and primality testing (Miller-Rabin) with worked examples and real-world cryptographic applications.

TAKEAWAYS:

  • Modular arithmetic is the “clock math” behind RSA encryption and digital signatures, where means divides .
  • Prime numbers are the “building blocks” of cryptography: RSA’s security relies on the hardness of factoring large primes like .
  • Euler’s Totient and Fermat’s Little Theorem () unlock symmetric and asymmetric encryption schemes.
  • The Miller-Rabin test efficiently checks if a number is probably prime—critical for generating RSA keys.
  • Chinese Remainder Theorem (CRT) speeds up RSA decryption by splitting work across coprime factors.
  • Real-world: WhatsApp uses modular arithmetic for end-to-end encryption keys; Ncell’s SIM cards rely on primes for authentication.

Core Concepts: The Math Behind Cryptography

1. Modular Arithmetic: The Clock Math of Cryptography

Modular arithmetic is the foundation of all modern cryptosystems. It defines operations “modulo ” (remainder after division by ), turning addition/subtraction/multiplication into cyclic operations—like a clock wrapping around at 12.

08162431a8 bitsb8 bitsc8 bitsd8 bits
Modular Arithmetic Example: (a + b) mod 256 = c (overflow ignored)

How it works: For integers and , and modulus : Example: because and .

Why it matters:

  • Encryption: In RSA, plaintext is encrypted as , where is a large composite.
  • Key exchange: Diffie-Hellman uses modular exponentiation to share secrets.

Real-world use:

  • WhatsApp’s Signal Protocol: Uses modular arithmetic to compute shared keys between users. When Alice and Bob exchange public keys and , their shared secret is , where is a large prime.
  • Ncell SIM Authentication: Your SIM card’s unique identifier (IMSI) is hashed using modular operations to generate a response for network authentication.

2. Prime Numbers: The Backbone of RSA

Prime numbers are integers >1 with no divisors other than 1 and themselves. In cryptography, they are used to:

  • Generate large keys (e.g., 2048-bit RSA keys use two 1024-bit primes).
  • Ensure the security of asymmetric encryption (hardness of factoring ).

Key properties:

  • Uniqueness: Every integer >1 has a unique prime factorization (Fundamental Theorem of Arithmetic).
  • Density: Primes become rarer as numbers grow, but checking primality for large numbers is computationally hard.

Example: Factorize 55: Both 5 and 11 are primes.

Real-world use:

  • Nepal Rastra Bank (NRB) Digital Transactions: When you transfer money via eSewa or Khalti, the system uses RSA keys generated from two large primes to encrypt your transaction details. Breaking this would require factoring , which is infeasible for 2048-bit primes.
  • NEPSE Stock Exchange: Encrypted communications between brokers and the exchange rely on prime-based cryptography to prevent fraud.

3. Fermat’s Little Theorem and Euler’s Totient

These theorems provide shortcuts for working with modular arithmetic in cryptography.

1111111123456
Euler’s Totient Function φ(n) for n=6: Multiplicative structure of coprimes
Fermat’s Little Theorem:

If is prime and is not divisible by : Example: Let , :

Euler’s Totient Function :

Counts the integers up to that are coprime with (i.e., ). For a prime : For (two distinct primes):

Why it matters:

  • RSA decryption uses Euler’s theorem: , where is the private exponent derived from .

Real-world use:

  • Khalti’s Secure Payments: When you pay via Khalti, the system uses Euler’s Totient to compute the private key for decrypting your transaction. For example, if (from primes 5 and 7), . The private exponent is the modular inverse of the public exponent modulo 24.

4. Primality Testing: Is It Prime?

Testing if a number is prime is critical for cryptography. The Miller-Rabin test is a probabilistic method to check primality efficiently.

Miller-Rabin Algorithm Steps:
  1. Write as (where is odd).
  2. Pick a random in .
  3. Compute .
  4. If or , is probably prime.
  5. Otherwise, square up to times. If none yield , is composite.

Example: Test if 561 is prime (it’s a Carmichael number, so it fools naive tests).

  • Let :
    • → , .
    • → Fails (561 is composite).
stateDiagram-v2
  [*] --> Is n-1 = d*2^s?
  Is n-1 = d*2^s? --> Yes: Pick random a in [2, n-2]
  Pick random a in [2, n-2] --> Compute x = a^d mod n
  Compute x = a^d mod n --> Is x ≡ 1 or x ≡ n-1?
  Is x ≡ 1 or x ≡ n-1? --> Yes: n is probably prime
  Is x ≡ 1 or x ≡ n-1? --> No: Square x up to s-1 times
  Square x up to s-1 times --> Any x ≡ n-1? Yes: n is probably prime
  Any x ≡ n-1? No: n is composite

Real-world use:

  • Pathao’s Driver Verification: When you register as a driver on Pathao, the app uses primality testing to generate a unique cryptographic key pair. The server checks if the public key’s modulus is probably prime using Miller-Rabin before accepting it.
  • NTC’s Online Bill Payments: The NTC website uses primality tests to validate the large primes used in its SSL/TLS certificates, ensuring secure connections for online payments.

5. Chinese Remainder Theorem (CRT): Speeding Up RSA

CRT allows breaking a problem into smaller coprime parts and combining results. In RSA, it speeds up decryption by:

  1. Splitting the ciphertext into parts using and .
  2. Decrypting each part separately: , .
  3. Combining results to get .

Example: Let (, ), , (private exponent).

  • Decrypt (since ).
  • Decrypt (since ).
  • Combine using CRT: .
  • Original message .

Real-world use:

  • eSewa’s Fast Transactions: When you pay via eSewa, the system uses CRT to speed up decryption of your transaction details. Instead of computing (where is large), it splits the work into and , then combines results—saving time and computational power.

## In the Real World

  1. WhatsApp/Signal Protocol:

    • Idea: Modular arithmetic and primes.
    • How: When Alice and Bob exchange keys, they compute a shared secret using , where is a large prime (e.g., 2048-bit). This ensures only they can decrypt messages.
  2. Ncell SIM Authentication:

    • Idea: Primality testing and modular exponentiation.
    • How: Your SIM’s unique identifier is hashed using primes to generate a response for the network. If the response matches, your SIM is authenticated.
  3. Khalti/eSewa Payments:

    • Idea: RSA encryption (primes + Euler’s Totient).
    • How: When you transfer money, your transaction details are encrypted as , where . The merchant’s server decrypts using (derived from ).
  4. NEPSE Stock Exchange:

    • Idea: Secure communication via primes.
    • How: Brokers and the exchange use RSA keys (based on large primes) to encrypt order details, preventing insider trading or fraud.
  5. Pathao/Daraz Driver Verification:

    • Idea: Miller-Rabin primality test.
    • How: When you register, the app checks if your cryptographic key’s modulus is probably prime before accepting it. This ensures only valid keys are used for secure communications.

## Exam Tip

  1. Modular Arithmetic:

    • Always reduce results to the smallest positive remainder (e.g., ).
    • Practice computing efficiently (use exponentiation by squaring).
  2. Primes and Factorization:

    • Memorize: Primes are the “atoms” of cryptography. RSA’s security relies on the difficulty of factoring .
    • For exams: Given , factorize it into primes if possible (e.g., ).
  3. Fermat’s Little Theorem vs. Euler’s Totient:

    • Fermat’s theorem applies only to primes: .
    • Euler’s theorem generalizes: for any coprime with .
  4. Miller-Rabin Test:

    • Key steps: Write , pick , compute , check squares.
    • Exam trick: If passes for a few random , it’s probably prime. For full marks, show all steps for one example.
  5. CRT in RSA:

    • Explain how splitting decryption into and reduces computation time.
    • Example: Given , , and , show how to compute using CRT.
  6. Common Pitfalls:

    • Off-by-one errors: (not ).
    • Non-coprime cases: Euler’s theorem requires .
    • Miller-Rabin: Always check all squarings, not just the first.

Past Exam Question Breakdown: For the question:

“What do you mean by ‘Feistel Structure for Block Ciphers’? Explain with block diagram. How can a number be tested for primality using Miller-Rabin algorithm? Explain with suitable example.”

Expected Answer Structure:

  1. Feistel Structure (5 marks):

    • Definition: A block cipher design where plaintext is split into two halves, processed in rounds with a function and a subkey.
    • Diagram: Draw a 4-round Feistel network with inputs , rounds , .
    • Example: DES uses Feistel structure.
  2. Miller-Rabin (5 marks):

    • Steps: Write , pick , compute , square up to times.
    • Example: Test with (show it fails).
    • Conclusion: 561 is composite (Carmichael number).

Visual for Feistel Structure:

Based on the TU BCA syllabus for Information Security (CACS459), unit 5.

Discussion

Loading…