This repository contains a Rust implementation of the exercises and concepts from the book "Programming Bitcoin" by Jimmy Song. As a Rust developer, I am using the book to learn Bitcoin, but doing the same exercises in Rust, not by copying the python code, but understanding the logic and rebuilding.
"Programming Bitcoin" teaches the mathematical foundations of Bitcoin, including finite fields, elliptic curves, and the secp256k1 curve used in Bitcoin. This implementation follows the book's structure, chapter by chapter, with each chapter containing exercises that build upon the previous ones.
The code is organized by chapters, mirroring the book's progression:
-
src/ch01/- Finite Fields- Basic field arithmetic operations
- FieldElement struct with addition, subtraction, multiplication, division, and exponentiation
-
src/ch02/- Elliptic Curves over Finite Fields- Point operations on elliptic curves
- Point addition and scalar multiplication
-
src/ch03/- Elliptic Curves over secp256k1- Implementation of the secp256k1 curve used in Bitcoin
- Large number handling with BigUint/BigInt for cryptographic operations
-
src/ch04/- Serialization- Serialization of secp256k1 points and signatures
- SEC (Standards for Efficient Cryptography) format implementation
-
src/ch05/- Transactions- Bitcoin transaction parsing and structure
- Variable-length integer encoding/decoding
- Transaction input/output handling
-
src/lib.rs- Library entry point -
src/main.rs- Executable entry point -
tests/- Integration tests for each chapter
This project uses the following Rust crates:
num-bigintandnum-traits- Arbitrary-precision arithmetic for large numberssecp256k1- secp256k1 elliptic curve cryptography libraryhex- Hexadecimal encoding/decodingserdeandserde_json- Serialization/deserializationsha2- SHA-256 hashinghmac- HMAC (Hash-based Message Authentication Code)rand- Random number generationbs58- Base58 encoding (used in Bitcoin addresses)ripemd- RIPEMD-160 hashingreqwest- HTTP client for API interactionstokio- Asynchronous runtimedotenvy- Environment variable loading
- Rust 1.70+ (install via rustup)
cargo buildcargo testcargo runThis project uses GitHub Actions for continuous integration and deployment. The CI pipeline includes:
- Automated Testing: Runs
cargo teston multiple Rust versions and operating systems - Code Formatting: Ensures code follows Rust formatting standards with
cargo fmt - Linting: Checks for common issues with
cargo clippy - Security Auditing: Scans dependencies for vulnerabilities
The CI workflow is defined in .github/workflows/ci.yml and includes jobs for:
- Building and testing on Linux, macOS, and Windows
- Code quality checks
- Dependency auditing
To set up CI/CD locally or contribute:
- Ensure your code passes
cargo fmtandcargo clippy - Run the full test suite with
cargo test - Push to a branch to trigger CI checks
- Finite Fields: Custom FieldElement struct supporting modular arithmetic
- Elliptic Curves: Point addition, doubling, and scalar multiplication
- secp256k1: Bitcoin's elliptic curve with proper large number handling
- Serialization: SEC format for public key compression and uncompressed formats
- Digital Signatures: ECDSA signature creation and verification
- Transactions: Bitcoin transaction structure, parsing, and validation
- Variable-Length Integers: CompactSize encoding for Bitcoin protocol
- Cryptographic Primitives: Modular inverse, exponentiation, and reduction
This implementation serves as a learning tool to:
- Understand Bitcoin's mathematical foundations from finite fields to transaction validation
- Practice Rust programming with complex mathematical concepts and cryptography
- Implement cryptographic algorithms and protocols from scratch
- Compare Python and Rust approaches to the same problems
- Learn Bitcoin's serialization formats and transaction structure
- Gain experience with real-world cryptographic libraries and best practices
- Programming Bitcoin by Jimmy Song
- Bitcoin Wiki - Elliptic Curve Digital Signature Algorithm
- secp256k1 specification
This project is for educational purposes. The original book's code and concepts belong to Jimmy Song and O'Reilly Media. /home/wilfrid_k/projects/boss_challenge/programming_bitcoin/README.md