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:
- Primality testing: How to check if a number is prime efficiently (critical for RSA key generation).
- Euler’s Totient function: Counts numbers coprime to , used in RSA encryption/decryption.
- Primitive roots: Simplify modular exponentiation in cryptographic protocols.
- 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:
- Check divisibility by primes ≤ : 2, 3, 5.
- 37 ÷ 2 = 18.5 → not divisible.
- 37 ÷ 3 ≈ 12.33 → not divisible.
- 37 ÷ 5 = 7.4 → not divisible. Conclusion: 37 is prime.
Miller-Rabin Primality Test (Probabilistic)
Algorithm:
- Write as (e.g., for , ).
- Pick a random (1 < < ).
- Compute .
- If or , might be prime.
- Square up to times. If never , is composite.
Worked Example: Test if 341 is prime (using ):
- → , .
- .
- 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 :
- Factorize: .
- 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:
- .
- .
- Euler’s Theorem: If , then .
Example: RSA Encryption
- Public key: , where is coprime to .
- Private key: , where .
- Encryption: .
- Decryption: .
In the Real World
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.
Bitcoin (Blockchain):
- Elliptic Curve Cryptography (ECC) depends on hard number-theoretic problems (e.g., discrete logarithms).
- Miners solve primality tests to validate transactions.
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.
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
endVisual: 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
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.
Euler’s Totient:
- Memorize the formula for prime powers: .
- Shortcut: For , .
Modular Arithmetic:
- Practice computing using exponentiation by squaring (e.g., ).
- Exam trick: If , Euler’s theorem doesn’t apply.
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."
Primality tests validate blockchain transactions (Image: Xiangfu, CC BY-SA 4.0, via Wikimedia Commons)
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…