Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
duyuefeng0708
GitHub Repository: duyuefeng0708/Cryptography-From-First-Principle
Path: blob/main/foundations/04-number-theory-rsa/README.md
483 views
unlisted

Module 04: Number Theory and RSA

View on nbviewer

The number theory that makes RSA work, and the attacks that break it when the math is sloppy.

Prerequisites

Learning Objectives

After completing this module you will:

  1. Implement the extended Euclidean algorithm and use it to compute modular inverses

  2. Apply Euler's theorem and the Chinese Remainder Theorem

  3. Generate RSA keys from first principles (prime selection, key pair computation)

  4. Understand textbook RSA's limitations and why padding is essential

Explore (SageMath Notebooks)

Work through these notebooks in order:

#NotebookWhat You'll Learn
aDivisibility, GCD, and EuclidThe Euclidean algorithm and its correctness
bExtended Euclidean AlgorithmComputing Bezout coefficients and modular inverses
cEuler's Totient and Fermat's TheoremWhy a^phi(n) = 1 mod n and how RSA uses this
dChinese Remainder TheoremSolving simultaneous congruences and CRT based RSA speedup
eRSA Key GenerationChoosing primes, computing e and d, the full key generation process
fRSA Encryption and DecryptionTextbook RSA in action and its pitfalls

Implement (Rust)

Build these from scratch in rust/src/lib.rs:

#FunctionDescription
1extended_gcdExtended Euclidean algorithm returning (gcd, x, y)
2mod_inverseModular multiplicative inverse via extended GCD
3euler_totientCompute Euler's totient function for a given n
4rsa_keygenGenerate an RSA key pair (n, e, d) from two primes
5rsa_encrypt_decryptTextbook RSA encryption and decryption

Run: cargo test -p number-theory-rsa

Break

Try these attacks in the break/ folder:

  • Hastad's broadcast attack: recover plaintext from multiple ciphertexts when e is small and no padding is used

  • Wiener's attack on small d: exploit a small private exponent via continued fractions

  • Factor N when |p - q| is small using Fermat's factorization method

Connect

See where this shows up in practice (in the connect/ folder):

  • RSA in TLS certificates and PKCS#1, where RSA signs the certificates that authenticate web servers

  • RSA-OAEP padding in practice, showing why textbook RSA is never used and how OAEP fixes it


Next: Module 05: The Discrete Logarithm and Diffie-Hellman