📖 Introduction
Exploring Cryptography with Rust is a hands‑on companion to modern cryptography textbooks, built entirely in Rust. Instead of just reading about algorithms, you can explore self‑contained Rust crates that implement and demonstrate each concept, from error‑correcting codes to probabilistic proofs.
🚀 Project Goals
-
Bridge Theory and Practice
Turn textbook pseudocode into real, tested Rust libraries. -
Learn by Example
Run and modify examples to see how Reed–Solomon encoding, Freivalds’ algorithm, zero‑knowledge proofs, and more really work. -
Organized by Book
Each crate lives under a folder named for the source text (e.g. Proofs, Arguments, and Zero‑Knowledge), letting you browse code chapter by chapter.
📚 What’s Inside
-
fingerprint
Reed–Solomon encoding & Lagrange interpolation examples (from Proofs, Arguments, and Zero‑Knowledge). -
freivalds
Freivalds’ probabilistic matrix multiplication verifier. -
(…and more crates to come, each tied to a classic crypto book.)
Each book defines features that includes:
- A minimal
Cargo.toml
- A
src/lib.rs
implementing the core routines - An
examples/
directory showing how to generate codewords, test proofs, or verify matrices
🛠️ Getting Started
🛠️ Prerequisites
- Rust (via rustup)
This project uses mdBook v0.4.45 to ensure compatibility with mdbook-katex
.
- mdBook and KaTeX support:
cargo install mdbook --version 0.4.45 cargo install mdbook-katex or chmod +x ./setup.sh ./setup.sh
🚀 Quick Start
git clone https://github.com/carougen/exploring-cryptography-with-rust.git
cd exploring-cryptography-with-rust
cargo build --all
mdbook serve book
📦 Build & Tests
# Compile all crates
cargo build --all
# Run test for a specific package
cargo test -p freivalds freivalds_test
# Run all tests
cargo test --all
# Generate the HTML for the book
cd book
mdbook build
# Run benchmark for a specific package
cargo bench --package freivalds --bench freivalds_bench
# Run benchmark for all packages
cargo bench --workspace --all-targets
▶️ Running Examples
From "Proofs, Arguments, and Zero-Knowledge"
fingerprint
cargo run -p fingerprint --example rs_lagrange
cargo run -p fingerprint --example rs_polynomial
freivalds
cargo run -p freivalds --example freivalds
📖 Documentation & mdBook
To serve the docs locally with live reload:
cd book
mdbook serve
Then open: http://localhost:3000
🤝 Contributing
Thank you for your interest in contributing! Before you begin, please note that this project is licensed under MIT and adheres to the Rust Community Code of Conduct. By participating, you agree to abide by these terms.
➕ Adding a New Book or Chapter
- Fork the repository and create a new branch
git checkout -b feature/<book>-short-description
- Under
crates/your_book/your_feature
, add your crate following the existing structure:Cargo.toml
src/lib.rs
examples/
- In
book/src/<your_language>/your_book>/
, add:index.md
courses/
exercices/
examples/
images/
- Update
book/src/SUMMARY.md
to include your new chapter links
🛠️ Code Quality Checks
- Formatting:
cargo fmt --all -- --check
- Linting:
cargo clippy --all -- -D warnings
- Tests:
Add at least one unit test undersrc/
.
Example: seecrates/proofs_arguments_zeroknowledge/fingerprint/src/lib.rs
for a sample test.
🚀 Pull Request Process
- Ensure all CI checks are passing
- Fill out the PR
- Submit your pull request
- Wait for review and address any feedback
🌿 Branch Naming
- Features:
feature/<book>-short-description
- Bugfixes:
fix/<crate>-short-description
📜 Code of Conduct
Please follow the Rust Community Code of Conduct.
Please follow the Project Code of Conduct.
📘 Serious Cryptography (2nd Edition)
Serious Cryptography by Jean-Philippe Aumasson is a modern, practical introduction to applied cryptography. Thoroughly revised and updated, this second edition walks readers through the core ideas, mathematics, and algorithms that make encryption and secure communication work in the real world.
Rather than avoiding the math, this book provides just enough to help you truly understand how algorithms like AES, RSA, ECDSA, and SHA-3 work—and how they can break. It’s a hands-on guide to cryptography that balances accessibility with rigor.
🎯 What You’ll Learn
- 🔐 The basics of computational security, attacker models, and forward secrecy
- 🌐 The strengths and limitations of TLS, the protocol behind HTTPS
- ⚛️ Quantum computation and post-quantum cryptography
- ⚙️ Deep dives into AES, ECDSA, Ed25519, Salsa20, and SHA-3
- 🧩 Advanced techniques like multisignatures, threshold signing, and zero-knowledge proofs
📦 Structure in This Repository
This section of the repository contains:
courses/
: Notes and breakdowns of each chapterexamples/
: Rust code implementing algorithms and constructionsexercices/
: Walkthroughs and solutions to problems from the bookimages/
: Diagrams and visuals to aid understanding
🆕 Status
This module is still under active development. Contributions are welcome as we build out more chapters, examples, and implementations!
📘 An Introduction to Mathematical Cryptography (2nd Edition)
An Introduction to Mathematical Cryptography by Jeffrey Hoffstein, Jill Pipher and Joseph H. Silverman provides a rigorous yet accessible entry point into the mathematical foundations of public-key cryptosystems and digital signatures. Written for students in mathematics and computer science, this textbook emphasizes the theoretical underpinnings and algebraic structures behind modern cryptography.
This self-contained book introduces all necessary mathematical tools, from number theory to probability, as they are needed, making it suitable for readers with only basic linear algebra knowledge.
🎯 What You’ll Learn
- 🔑 Core cryptographic constructions like Diffie–Hellman, RSA, and ElGamal
- ✍️ Digital signature schemes including DSA, RSA, and lattice-based signatures
- 🧮 Mathematical foundations: primality testing, factorization, modular arithmetic
- 🔢 Concepts from probability theory, information theory, and collision analysis
- 🧱 Deep dives into elliptic curve cryptography, pairing-based systems, and lattice cryptography
- 🧠 Specialized constructions like NTRU, digital cash, and homomorphic encryption
📦 Structure in This Repository
This section of the repository contains:
courses/
: Notes and guided walkthroughs per chapterexamples/
: Rust implementations of core algorithmsexercices/
: Worked solutions and explanationsimages/
: Diagrams supporting cryptographic concepts
🆕 Status
This module is still under active development. Contributions are welcome as we build out more chapters, examples, and implementations!
🏛️ Chapter 1 — An Introduction to Cryptography
Source: An Introduction to Mathematical Cryptography : Hoffstein, Pipher, Silverman (2nd ed.)
Last updated 2025-04-27
🚩 Navigation
§ | Title | Emoji |
---|---|---|
1.1 | Simple Substitution Ciphers | 🔤 |
1.2 | Divisibility & GCD | ➗ |
1.3 | Modular Arithmetic | 🔄 |
1.4 | Primes & Finite Fields | 🔺 |
1.5 | Powers & Primitive Roots | ⚡ |
1.6 | Historical Milestones | 📜 |
1.7 | Symmetric vs Asymmetric | 🔐 |
🔤 1.1 Simple Substitution Ciphers
👑 1.1.1 Caesar / shift cipher
Assign letters .
To encrypt a letter of value with key :
To decrypt:
Example (). Number the alphabet . With key , encryption sends
so in letters:
🔀 1.1.2 Mono-alphabetic cipher
A Mono-alphabetic cipher fixes a permutation of the 26 letters. For instance, one choice of is
Since there are possible permutations, the key-space has size
📊 1.1.3 Why statistics leak
English single‑letter frequencies (approx.):
E | T | A | O | N | I | S | R | H | L |
---|---|---|---|---|---|---|---|---|---|
13 % | 9 % | 8 % | 7 % | 7 % | 6 % | 6 % | 6 % | 5 % | 4 % |
Bigrams th 17 %, he 13 % dominate. A fixed substitution preserves these peaks, so histograms expose the key.
🛡️ 1.1.4 Defensive evolution
Era | Technique | Defence idea | Ultimate weakness |
---|---|---|---|
16ᵗʰ c. | Vigenère | Changing shift | Key length leaks |
1920s | Enigma | Rotor permutation stream | Operator mistakes |
Today | AES / ChaCha | Ciphertext indistinguishable from random | Only brute force |
➗ 1.2 Divisibility & GCD
🔢 1.2.1 Toolkit
-
Divisibility
We say an integer divides an integer , written
Here, is the quotient, and the absence of remainder is crucial.
For example, since , but . -
Greatest Common Divisor (GCD)
For nonzero integers , the greatest common divisor is the largest positive integer satisfying Equivalently, -
Bézout’s Identity
There exist integers (the Bézout coefficients) such that This identity shows is the smallest positive linear combination of and . -
Modular Inverse
If , Bézout’s identity yields so is the multiplicative inverse of modulo .
➗ 1.2.2 Euclidean Algorithm (Worked)
The Euclidean algorithm computes via repeated division with remainder.
Example: Compute
Once the remainder is , the last nonzero remainder is
Complexity: Lamé’s theorem (1844) shows the number of division steps is , in fact fewer than times the decimal digits of .
🧮 1.2.3 Extended Euclidean Algorithm
To find Bézout coefficients such that we maintain sequences with
2024 | 1 | 0 | |
748 | 2 | 0 | 1 |
528 | 1 | 1 | −2 |
220 | 2 | −1 | 3 |
88 | 2 | 3 | −8 |
44 | −7 | 19 |
Thus,
🔄 1.3 Modular Arithmetic
📦 1.3.1 Ring Structure of
-
Residue Classes.
We denote by the equivalence classes of integers modulo : Addition and multiplication are defined with wrap‑around: where sums and products are reduced modulo to lie in . -
Examples ().
The set forms a ring, commonly denoted , with:- Additive identity: , since .
- Multiplicative identity: , since .
- Additive inverses: .
🧮 1.3.2 Euler’s Theorem and Fermat’s Little Theorem
-
Euler’s Totient Function equals the number of integers in that are coprime to .
-
Euler’s Theorem.
If , then -
Fermat’s Little Theorem.
If is prime and , then and
⚡️ 1.3.3 Efficient Exponentiation
-
Binary Expansion of the Exponent.
Any exponent admits a binary representation -
Square‑and‑Multiply Concept.
- Precompute successive squares: modulo .
- Multiply together those squares corresponding to the 1‑bits of .
- Total modular multiplications: about .
-
Illustration.
To compute : so each obtained via repeated squaring and selected multiplies. This reduces the number of multiplications from (naïve) to roughly squares + multiplies = total.
🔺 1.4 Primes & Finite Fields
🔢 1.4.1 Prime Number
- Definition.
An integer is called prime if its only positive divisors are and . Equivalently,
🔍 1.4.2 Generating Primes with the Sieve of Eratosthenes
-
Conceptual Overview.
- Start with a list of integers from up to .
- Repeatedly mark (“sieve out”) multiples of each prime found, beginning with .
- The unmarked numbers that remain are the primes .
-
Example ().
- Begin: .
- Mark multiples of : .
- Next unmarked is , mark its multiples: .
- Continue up to .
- Primes found: .
-
Efficiency.
- Time complexity: .
- Space: .
- Generates all primes up to efficiently, and for , runs in well under a second in optimized implementations.
⚗️ 1.4.3 The Finite Field
-
Definition.
For a prime , the set with addition and multiplication modulo forms a field, denoted . -
Field Axioms.
- Additive group: is cyclic of order .
- Multiplicative group: is cyclic of order .
- Every nonzero element has a unique inverse satisfying .
-
Computing Inverses (Fermat’s Little Theorem).
If , then -
Cryptographic Choice.
- Modern cryptosystems (e.g. Diffie–Hellman, elliptic curves) pick to achieve roughly 128‑bit security against brute‑force attacks.
⚡ 1.5 Powers & Primitive Roots
🔢 1.5.1 Multiplicative Order
-
Definition.
For a prime and an integer with , the order of mod , denoted , is the smallest positive integer such that -
Properties.
- divides , since by Fermat’s little theorem .
- The powers cycle through a subgroup of of size .
🌱 1.5.2 Primitive Roots (Generators)
-
Primitive Root.
A primitive root modulo is an element whose order is exactly : Equivalently, the powers produce all nonzero residues in some order. -
Existence.
For every prime , there exists at least one primitive root modulo .- To find a primitive root , one factors and tests candidates until
📜 1.6 Historical Milestones
A brief overview of landmark ciphers throughout history and the key techniques that led to their cryptanalysis.
Year | Cipher | Break Highlight |
---|---|---|
50 BCE | Caesar Shift | Brute‑force search of 26 shifts |
830 | Arab Cryptanalysis | Frequency analysis of letters |
1553 | Vigenère Cipher | Index of coincidence |
1917 | Zimmermann Telegram | Exploitation of linguistic patterns |
1939 | Enigma Machine | Mechanical Bombe‑based search |
1944 | Lorenz Cipher | Colossus computer and statistical methods |
🏛️ 50 BCE — Caesar Shift
- Algorithm: Shift each letter by a fixed offset (typically 3).
- Cryptanalysis: A brute‑force attempt of all 26 possible shifts quickly reveals the plaintext.
- Significance: One of the earliest recorded uses of substitution ciphers.
🔍 830 — Arab Cryptanalysis
- Cryptographer: Al-Kindi (Arab mathematician).
- Technique: Developed frequency analysis, observing that certain letters appear more often in a language.
- Impact: Demonstrated that simple substitution ciphers are insecure.
🔒 1553 — Vigenère Cipher
- Cipher: Polyalphabetic substitution using a repeating keyword.
- Cryptanalysis: The index of coincidence method by Babbage and later Kasiski reveals the keyword length, reducing it to multiple Caesar ciphers.
- Legacy: Once called “le chiffre indéchiffrable,” it was eventually broken and paved the way for modern cryptanalysis.
🕵️ 1917 — Zimmermann Telegram
- Context: World War I diplomatic communication.
- Attack Method: Analysts used linguistic context and known-plaintext guesses to reconstruct parts of the message.
- Historical Outcome: Its decryption helped bring the United States into the war.
⚙️ 1939 — Enigma Machine
- Device: Electro‑mechanical rotor cipher machine used by Germany.
- Decryption: The Bombe machine, designed by Alan Turing and colleagues at Bletchley Park, automated key search using cribs and known patterns.
- Effect: Allied forces gained critical intelligence during WWII.
🖥️ 1944 — Lorenz Cipher
- System: High‑level teleprinter cipher (called “Tunny” by British codebreakers).
- Breakthrough: The Colossus computer exploited statistical weaknesses and wheel‑pattern analysis.
- Importance: One of the first large‑scale electronic computers, marking the birth of modern computing.
🔐 1.7 Symmetric vs Asymmetric
🔑 1.7.1 Symmetric Ciphers
- Definition: Both sender and receiver share the same secret key for encryption and decryption.
- Characteristics:
- Very efficient: suitable for encrypting large volumes of data.
- Key distribution challenge: secure key exchange is required before use.
- Examples:
- Block ciphers like AES (Advanced Encryption Standard) operate on fixed-length blocks (e.g. 128 bits).
- Stream ciphers generate a pseudorandom keystream to XOR with the plaintext.
🌍 1.7.2 Asymmetric Ciphers
- Definition: Uses a public key for encryption and a private key for decryption.
- Characteristics:
- Simplifies key distribution: public key can be openly shared.
- Computationally heavier: used typically to secure small messages or keys.
- Examples:
- RSA (Rivest–Shamir–Adleman).
- Diffie–Hellman key exchange for establishing shared secrets.
- Elliptic curve variants (e.g.\ ECDH, ECDSA).
🔄 1.7.3 Hybrid Encryption
- Concept: Combine strengths of both systems:
- Generate a random session key for a fast symmetric cipher.
- Encrypt the session key using the recipient’s public key.
- Encrypt the bulk data with the symmetric cipher using the session key.
- Benefit: Achieves both performance and secure key distribution.
📘 Introduction to Modern Cryptography (3rd Edition)
Introduction to Modern Cryptography by Jonathan Katz and Yehuda Lindell is one of the most widely used textbooks for undergraduate and graduate courses in cryptography. This third edition builds on its predecessors by adding updated material, exercises, and clearer pedagogical structure, making it a top-tier introduction to the formal study of cryptographic security.
The book presents cryptography as a rigorous discipline, with an emphasis on formal definitions and provable security. It systematically develops the theoretical foundations behind modern cryptographic constructions while also highlighting their practical relevance.
🎯 What You’ll Learn
- 🔑 Foundations of modern cryptography: computational security, indistinguishability, and reductionist proofs
- 🧪 Private-key encryption and message authentication codes
- 🔐 Public-key encryption and digital signatures
- 💡 Hash functions and their role in collision resistance and integrity
- 🔄 Pseudorandom generators, functions, and permutations
- 🧱 Construction of secure protocols and hybrid encryption
- ⚖️ Formal security notions like semantic security and chosen-ciphertext attacks
- 🚨 Real-world implications of idealized models
- 🔬 Exercises with strong conceptual focus and formal proof structure
📦 Structure in This Repository
This section of the repository contains:
courses/
: Notes and summaries of each chapterexamples/
: Rust implementations of cryptographic primitivesexercices/
: Exercises with explanations and codeimages/
: Visualizations of cryptographic concepts and constructions
🆕 Status
This module is still under active development. Contributions are welcome as we build out more chapters, examples, and implementations!
📘 Rational Points on Elliptic Curves (2nd Edition)
Rational Points on Elliptic Curves by Joseph H. Silverman and John T. Tate offers a beautiful blend of algebra, geometry, analysis, and number theory. This textbook serves as both an elegant introduction to elliptic curves and an accessible doorway into arithmetic geometry and Diophantine equations. Written in a friendly and informal style, it invites readers of all levels to appreciate the unity of modern mathematics.
The book is designed for advanced undergraduates and assumes only foundational mathematical knowledge. It is rich in exercises and aims to make advanced topics approachable using tools commonly taught in undergraduate courses.
🎯 What You’ll Learn
- 🧮 The geometry and group structure of elliptic curves
- 🔢 The Nagell–Lutz Theorem for classifying torsion points
- 📐 The Mordell–Weil Theorem on the finite generation of rational points
- 🔍 The Thue–Siegel Theorem on integer points
- 📊 Methods for counting points over finite fields
- 🔐 Lenstra’s Elliptic Curve Factorization Algorithm
- 🧠 A glimpse into Complex Multiplication and Galois Representations
- 🔐 Introduction to Elliptic Curve Cryptography (ECC)
- 🧩 A brief discussion on Fermat’s Last Theorem and its connection to elliptic curves
📦 Structure in This Repository
This section of the repository contains:
courses/
: Guided chapter notes and explanationsexamples/
: Demonstrations and Rust implementations of algorithmsexercices/
: Exercise solutions and walkthroughsimages/
: Visual diagrams and mathematical illustrations
🆕 Status
This module is still under active development. Contributions are welcome as we build out more chapters, examples, and implementations!
📘 Proofs, Arguments, and Zero-Knowledge
Proofs, Arguments, and Zero-Knowledge by Justin Thaler offers a comprehensive and modern introduction to the theory of probabilistic proofs and cryptographic arguments. This textbook provides a unified treatment of foundational techniques used in interactive proofs, SNARKs, and zero-knowledge systems. It is ideal for students and researchers interested in complexity theory, cryptography, and secure computation.
The book is self-contained and pedagogical, blending formal rigor with clear intuition. It features numerous exercises and a progressive structure, starting from classical proof systems to advanced modern protocols.
🎯 What You’ll Learn
- 🔁 The distinction between proofs, arguments, and zero-knowledge proofs
- 🎲 Probabilistically checkable proofs (PCPs) and their significance
- 🔐 Interactive proofs, multi-prover systems, and non-interactive arguments
- 🧠 Key constructions like Freivalds algorithm and sum-check protocols
- 🧩 SNARKs, zk-SNARKs, and succinct non-interactive arguments
- 🚧 The role of cryptographic assumptions (e.g., collision resistance, knowledge soundness)
- 📦 Applications to verifiable computation, blockchain, and privacy-preserving systems
📦 Structure in This Repository
This section of the repository contains:
courses/
: Chapter notes and walkthroughsexamples/
: Rust implementations of protocolsexercices/
: Solutions and explanations for key problemsimages/
: Supporting visuals and protocol diagrams
🆕 Status
This module is under active development. Contributions are welcome to expand code implementations and deepen documentation for each chapter.