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

Crypto From First Principles

CI License: MIT Launch Binder GitHub Discussions

Learn cryptography by building it. 123 interactive notebooks, 57 Rust exercises, 12 modules, from modular arithmetic to zero-knowledge proofs.

Learning flow: Explore, Implement, Break, Connect

Why This Exists

Traditional crypto education either drowns you in pure math or hands you a library. We bridge the gap:

  • Explore the math in SageMath. Visualize groups, curves, and lattices

  • Implement every primitive from scratch in Rust. No magic black boxes

  • Break weakened versions. See exactly why parameters matter

  • Connect to real protocols. Find your math in TLS, Bitcoin, Signal, and Zcash

How does this compare?

This RepoCryptopalsCryptoHackMoonMathBoneh (Stanford)
Interactive notebooksyes (Jupyter)noneweb-onlyPDF onlynone
Math foundationsalgebra to ZKnonesomeexcellentexcellent
Build from scratchRustPythonPythonnonenone
Visualizationsplots + graphsnonesomenonenone
Attack labs27 notebooks48 challenges100+ challengesnonenone
Protocol connectionsTLS, Bitcoin, SignalnonenoneEthereum/ZKnone
Zero-install (Binder)yesnoyes (web)nono
Scopegroups to FHE/MPCsymmetric + PKvariedalgebra to ZKbroad theory

Elliptic curve point addition    Lattice basis reduction

Try It Now

No install needed. Launch in your browser:

Launch Binder    Open in CoCalc

Binder is free and launches instantly. CoCalc has native SageMath support and is better for longer sessions.

Or clone locally:

git clone https://github.com/duyuefeng0708/Cryptography-From-First-Principle.git cd Cryptography-From-First-Principle conda activate sage jupyter notebook foundations/01-modular-arithmetic-groups/sage/01a-integers-and-division.ipynb

Modules

Foundations (BSc)

#ModuleWhat You'll BuildNotebooks
01Modular Arithmetic & GroupsCyclic groups, generators, Lagrange's theorem6 explore · 2 break · 2 connect
02Rings, Fields & PolynomialsRing axioms, polynomial rings, irreducibility6 explore · 2 break · 2 connect
03Galois Fields & AESGF(2^8), AES S-box, MixColumns6 explore · 2 break · 2 connect
04Number Theory & RSAEuler/Fermat, CRT, RSA from scratch6 explore · 3 break · 2 connect
05Discrete Log & Diffie-HellmanDLP, CDH, DDH, key exchange6 explore · 3 break · 2 connect
06Elliptic CurvesPoint addition, ECDH, ECDSA6 explore · 3 break · 3 connect

Frontier (Postgraduate)

#ModuleWhat You'll BuildNotebooks
07Bilinear PairingsWeil/Tate pairing, BLS signatures, IBE5 explore · 2 break · 2 connect
08Lattices & Post-QuantumLWE, Ring-LWE, Kyber overview6 explore · 2 break · 2 connect
09Commitments & Sigma ProtocolsPedersen, Schnorr, Fiat-Shamir5 explore · 2 break · 2 connect
10SNARKs & STARKsR1CS, QAP, Groth16, FRI5 explore · 2 break · 3 connect
11Homomorphic EncryptionBGV, BFV, CKKS5 explore · 2 break · 3 connect
12Multi-Party ComputationSecret sharing, Yao's GC, SPDZ5 explore · 2 break · 3 connect

How to Work Through a Module

Each module has its own README.md with prerequisites, learning objectives, and a detailed roadmap.

1. Explore. Work through the SageMath notebooks in order (a, b, c, ...). Run every cell, modify examples, answer the exercises.

2. Implement. Open rust/src/lib.rs and replace todo!() with your code. Early modules give loop skeletons; later modules give only function signatures.

cargo test -- --ignored # Run all exercises in this module

3. Break. Attack weakened versions of what you just built. See why small keys, nonce reuse, and bad parameters are fatal.

4. Connect. Trace your math to real protocols. Find your group theory in TLS, your curves in Bitcoin, your pairings in Ethereum.

Installation (local setup)

Prerequisites

  • Rust >= 1.75

  • SageMath >= 10.0 (via conda)

  • Jupyter with SageMath kernel

Linux (Ubuntu/Debian)

# Rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source ~/.cargo/env # SageMath + Jupyter (via Miniforge) curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh" bash Miniforge3-Linux-x86_64.sh source ~/miniforge3/etc/profile.d/conda.sh conda create -n sage sage jupyter -c conda-forge conda activate sage # Register SageMath kernel (visible to VS Code / JupyterLab outside the env) python -m sage.repl.ipython_kernel.install --user # Verify rustc --version jupyter kernelspec list # should show "sagemath"

macOS

# Rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source ~/.cargo/env # SageMath + Jupyter (via Miniforge) curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-$(uname -m).sh" bash Miniforge3-MacOSX-$(uname -m).sh source ~/miniforge3/etc/profile.d/conda.sh conda create -n sage sage jupyter -c conda-forge conda activate sage # Register SageMath kernel python -m sage.repl.ipython_kernel.install --user # Verify rustc --version jupyter kernelspec list

Windows

# Rust: download and run the installer from https://rustup.rs # SageMath + Jupyter (via Miniforge) # Download Miniforge3-Windows-x86_64.exe from: # https://github.com/conda-forge/miniforge/releases/latest # Run the installer, then open Miniforge Prompt: conda create -n sage sage jupyter -c conda-forge conda activate sage # Register SageMath kernel python -m sage.repl.ipython_kernel.install --user # Verify rustc --version jupyter kernelspec list

VS Code Setup

Install the Jupyter extension, then connect to a SageMath Jupyter server:

  1. Start the server (inside the conda env):

    conda activate sage sage -n jupyter
  2. Copy the URL with token from the terminal output (e.g. http://localhost:8888/?token=abc123...).

  3. In VS Code, open any .ipynb, click Select Kernel, then Existing Jupyter Server, and paste the URL.

  4. Select the SageMath kernel from the list.

The server stays running. You only need to do steps 2 through 4 once per session.

Roadmap

  • SageMath exploration notebooks (72 notebooks across 12 modules)

  • Scaffolded Rust exercises (57 functions with progressive difficulty)

  • Break/connect notebooks (55 notebooks across 12 modules)

  • Binder integration for zero-install browser experience

  • Community-contributed exercises and visualizations

Contributing

See CONTRIBUTING.md for guidelines on reporting issues, adding content, and submitting PRs.

License

MIT