BIT303 Information Security

Information SecurityUnit 68 min read

Primality Testing & Number Theory: Euler’s Totient, Primitive Roots, Miller-Rabin

Unit 6 of Information Security explores the mathematical foundations of cryptography—primality testing (trial division, Fermat, Miller-Rabin), Euler’s Totient function, primitive roots, and modular arithmetic—with real-world applications in RSA, Diffie-Hellman, and blockchain.

TAKEAWAYS:

  • Primality testing (trial division, Fermat, Miller-Rabin) determines if a number is prime, critical for generating cryptographic keys.
  • Euler’s Totient function counts integers coprime to , used in RSA encryption.
  • Primitive roots enable efficient modular arithmetic in cryptographic protocols like Diffie-Hellman.
  • Modular arithmetic () underpins symmetric/asymmetric encryption and digital signatures.
  • Number theory (GCD, LCM, Euler’s theorem) solves real-world problems like secure key exchange and password hashing.
  • Miller-Rabin test is a probabilistic primality test faster than trial division, used in practice (e.g., Bitcoin).

Core Concepts: Why Number Theory Matters in Security

Cryptography relies on hard mathematical problems to secure data. This unit covers:

  1. Primality testing: How to check if a number is prime efficiently (critical for RSA key generation).
  2. Euler’s Totient function: Counts numbers coprime to , used in RSA encryption/decryption.
  3. Primitive roots: Simplify modular exponentiation in cryptographic protocols.
  4. Modular arithmetic: The "math of clocks" that enables secure key exchange.

1. Primality Testing: Is It Prime?

A prime number has no divisors other than 1 and itself. Primes are the "atoms" of cryptography—used to generate keys in RSA, Diffie-Hellman, and blockchain.

Methods to Test Primality

Method Time Complexity Deterministic? Used In
Trial Division Yes Small numbers, educational use
Fermat Test No (probabilistic) Quick checks, pseudoprimes
Miller-Rabin No (but highly accurate) Bitcoin, TLS, modern crypto

Example: Trial Division

Question: Is 37 prime? Steps:

  1. Check divisibility by primes ≤ : 2, 3, 5.
  2. 37 ÷ 2 = 18.5 → not divisible.
  3. 37 ÷ 3 ≈ 12.33 → not divisible.
  4. 37 ÷ 5 = 7.4 → not divisible. Conclusion: 37 is prime.

Miller-Rabin Primality Test (Probabilistic)

Algorithm:

  1. Write as (e.g., for , ).
  2. Pick a random (1 < < ).
  3. Compute .
  4. If or , might be prime.
  5. Square up to times. If never , is composite.

Worked Example: Test if 341 is prime (using ):

  1. → , .
  2. .
  3. or . Square :
    • .
    • .
    • (now ). Result: 341 is not prime (since it passed, but we know ).

2. Euler’s Totient Function

Counts integers up to that are coprime to (i.e., ).

Formula:

For :

Example:

Compute :

  1. Factorize: .
  2. Apply formula: Verification: Numbers coprime to 30 ≤ 30 are {1, 7, 11, 13, 17, 19, 23, 29} → 8 numbers.

Why It Matters:

  • Used in RSA decryption: relies on .
  • Diffie-Hellman key exchange: Ensures shared secrets are unique.

3. Primitive Roots and Modular Arithmetic

A primitive root modulo is a number whose powers generate all numbers coprime to .

Example:

For , is a primitive root because: All residues {1, 2, 3, 4, 5, 6} are covered.

Application in Diffie-Hellman:

Alice and Bob agree on a prime and primitive root . They exchange:

  • Alice sends .
  • Bob sends . Shared secret: .

4. Modular Arithmetic: The "Clock Math" of Crypto

Modular arithmetic solves (i.e., is divisible by ).

Key Properties:

  1. .
  2. .
  3. Euler’s Theorem: If , then .

Example: RSA Encryption

  • Public key: , where is coprime to .
  • Private key: , where .
  • Encryption: .
  • Decryption: .

In the Real World

  1. eSewa/Khalti (Nepal):

    • Uses RSA (relying on primality testing) to encrypt payment data between your phone and their servers.
    • Example: When you pay a bill, your card number is split into primes, encrypted, and sent securely.
  2. Bitcoin (Blockchain):

    • Elliptic Curve Cryptography (ECC) depends on hard number-theoretic problems (e.g., discrete logarithms).
    • Miners solve primality tests to validate transactions.
  3. Ncell/NTC SIM Activation:

    • When you register a new SIM, the network uses Diffie-Hellman (primitive roots) to exchange a secret key with your phone’s chip, ensuring no eavesdropper can intercept your IMEI.
  4. WhatsApp End-to-End Encryption:

    • Uses Signal Protocol, which relies on Euler’s Totient and modular arithmetic to generate session keys.

Visual: RSA Key Generation Flow

flowchart TD
    A["Choose two large primes\np, q (e.g., 61, 59)"] --> B["Compute n = p × q\n(e.g., 3599)"]
    B --> C["Compute φ(n) = (p-1)(q-1)\n(e.g., 3480)"]
    C --> D["Choose e coprime to φ(n)\n(e.g., 17)"]
    D --> E["Compute d ≡ e⁻¹ mod φ(n)\n(e.g., 2753)"]
    E --> F["Public Key: (e, n)\nPrivate Key: (d, n)"]

Visual: Miller-Rabin Test Steps

sequenceDiagram
    participant Tester
    participant Number as n
    participant Witness as a
    Tester->>Number: Write n-1 = d·2ˢ
    Tester->>Witness: Pick random a (1 < a < n-1)
    Tester->>Number: Compute x = aᵈ mod n
    alt x ≡ 1 or x ≡ n-1
        Tester->>Number: n is probably prime
    else
        loop s-1 times
            Tester->>Number: x = x² mod n
            alt x ≡ n-1
                Tester->>Number: n is probably prime
                break
            else
                Tester->>Number: n is composite
                break
            end
        end
    end

Visual: Euler’s Totient in RSA

classDiagram
    class Totient {
        +φ(n) = n × (1 - 1/p₁) × ... × (1 - 1/pₖ)
        +Used in RSA decryption: d ≡ e⁻¹ mod φ(n)
    }
    class RSA {
        +Public Key: (e, n)
        +Private Key: (d, n)
        +Encryption: c = mᵉ mod n
        +Decryption: m = cᵈ mod n
    }
    Totient --> RSA : "φ(n) enables d calculation"

Exam Tip

  1. Primality Testing:

    • For trial division, always check up to .
    • For Miller-Rabin, remember the steps: decompose , pick , square until or composite.
    • Common pitfall: Forgetting to square up to times in Miller-Rabin.
  2. Euler’s Totient:

    • Memorize the formula for prime powers: .
    • Shortcut: For , .
  3. Modular Arithmetic:

    • Practice computing using exponentiation by squaring (e.g., ).
    • Exam trick: If , Euler’s theorem doesn’t apply.
  4. Real-World Links:

    • Always relate primality tests to RSA key generation or Diffie-Hellman.
    • Example answer starter: "In eSewa’s payment system, primality testing ensures that the public-private key pairs used to encrypt transactions are generated from large primes, preventing brute-force attacks."

Bitcoin mining hardware**Primality tests validate blockchain transactions (Image: Xiangfu, CC BY-SA 4.0, via Wikimedia Commons) Diffie-Hellman key exchange**Primitive roots enable secure key sharing (Image: de:Benutzer:DaMutz, CC BY-SA 4.0, via Wikimedia Commons)

Based on the TU BIT syllabus for Information Security (BIT303), unit 6.

Discussion

Loading…