Skip to main content

blockchainkit

Quality License CI Coverage
Documentation Docs
Code style Ruff
Try it online Open in Colab JupyterLite

Cryptography and blockchains, understood through experiments. blockchainkit is a Python toolkit for learning and teaching how blockchains work, from the one-time pad to Ethereum's gas. Each idea is followed from its history and mathematics to a small, typed, inspectable implementation, a figure, and an experiment you can change. Every subpackage's documentation walks through the field's breakthroughs in historical order, 87 of them in all, each linked to the code and the gallery example that reproduce it.

A Merkle proof, Nakamoto's double-spend probabilities, and a Lamport space-time diagram, all drawn with blockchainkit

  • For students: recover a private key from a reused signing nonce, find hash collisions at the birthday bound, watch a fork undo a payment, mine selfishly, eclipse a node, and drain a DAO-style contract, in a few lines each.
  • For instructors: five subpackages, one consistent API, 88 gallery examples (each downloadable as a notebook or runnable in the browser), exercise pages with worked solutions, and cross-cutting tutorials.
  • Honest about its limits: tiny keys, variable-time arithmetic and simplified formats keep the mathematics visible. Every adaptation is marked on its history entry and specified in the model boundaries. This is teaching software, not a wallet or a node.

import blockchainkit loads only the standard library. Matplotlib and NumPy are used only by each subpackage's visualizers.

pip install blockchainkit   # Python 3.10+

Conventionally imported as bk:

import blockchainkit as bk

alice_key = 7  # A fixed teaching key: never use such a key for real money.
alice = bk.structures.address(bk.crypto.public_key(alice_key))
bob = bk.structures.address(bk.crypto.public_key(11))

payment = bk.structures.Transaction(bk.crypto.public_key(alice_key), bob, 25, 0)
payment = payment.signed(alice_key, signing_nonce=17)  # Never reuse a signing nonce.

genesis = bk.consensus.mine(bk.structures.Block(difficulty=5)).block
chain = bk.structures.Blockchain(genesis, bk.structures.Ledger({alice: 100}))
block = bk.structures.Block(genesis.hash, (payment,), height=1, timestamp=1, difficulty=5)
chain.add(bk.consensus.mine(block).block)
assert chain.state.balances[bob] == 25

The quickstart notebook takes one payment from a signature to a mined block in about ten minutes. Open it in Colab using the badge above.

Subpackages

  • blockchainkit.crypto -- the one-time pad, baby-step giant-step and Pohlig-Hellman, Merkle's puzzles, Diffie-Hellman and RSA, Shamir and Feldman secret sharing, Lamport signatures, birthday attacks, commitments (hash and Pedersen), blind signatures, elliptic curves, zero knowledge and Fiat-Shamir, Merkle-Damgård and length extension, HMAC, Schnorr signatures, RFC 6979 nonces, and MuSig. 22 breakthroughs.

    Scalar multiples on an elliptic curve, the SHA-256 avalanche, and the birthday bound

  • blockchainkit.structures -- double-entry ledgers, Bloom filters, Merkle trees and proofs, hash chains, linked and batched timestamps, the UTXO and account models, light clients, Bitcoin's duplicated-leaf bug, Certificate Transparency consistency proofs, transaction malleability, Merkle mountain ranges, replay protection, sparse Merkle trees, and blocks with cumulative-work fork choice. 15 breakthroughs.

    A Merkle proof, a fork in a block tree, and Bloom-filter false-positive rates

  • blockchainkit.consensus -- the gambler's ruin, Byzantine generals, Ben-Or and FLP, partial synchrony, PBFT, pricing functions and Hashcash, Nakamoto consensus and its double-spend calculation, difficulty retargeting, GHOST, selfish mining, proof of stake, nothing at stake, Casper FFG, and cryptographic sortition. 17 breakthroughs.

    Double-spend probabilities, selfish-mining revenue, and difficulty retargeting

  • blockchainkit.network -- discrete-event gossip, random, small-world and scale-free graphs, Lamport and vector clocks, epidemic rumor spreading, Bracha's reliable broadcast, CAP, Kademlia, Sybil and eclipse attacks, inv/getdata relay, propagation and forks, compact blocks, and Dandelion. 17 breakthroughs.

    A small-world graph, push and pull gossip, and Kademlia lookup hops

  • blockchainkit.vm -- a deterministic 256-bit stack machine with gas and atomic failure, reverse Polish notation, Turing machines and the busy beaver, structured programming, Forth, state-machine replication, smart contracts, bytecode verification, Bitcoin Script with P2PKH and hash time-locked contracts, gas repricing, the DAO's reentrancy, and integer overflow. 16 breakthroughs.

    Stack height of two expressions, halting times of two-state Turing machines, and a reentrancy attack

Learn

Development

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest --cov=blockchainkit --cov-branch          # 100% statement and branch coverage, enforced
pytest --doctest-modules blockchainkit --ignore-glob="*/tests/*"
ruff check . && ruff format --check .
mypy                                              # strict
cd docs && MPLBACKEND=Agg make html && MPLBACKEND=Agg make doctest

The documentation build runs every gallery example and every code line in the tutorials and exercise solutions, and treats warnings as errors. The README figures are regenerated with python docs/make_readme_figure.py and python docs/make_readme_subpackage_figures.py. See CONTRIBUTING.md.

blockchainkit belongs to a family of teaching toolkits with the same architecture: mathematicskit, physicskit and chemistrykit.

MIT license; see LICENSE. To cite blockchainkit, see CITATION.cff.

Metadata

Release files for blockchainkit 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blockchainkit 0.2.0
File Size Uploaded
blockchainkit-0.2.0.tar.gz 241.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for blockchainkit 0.2.0
File Interpreter ABI Platform
blockchainkit-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 383.4 kB

Release files / blockchainkit-0.2.0.tar.gz

Download URL blockchainkit-0.2.0.tar.gz
Size 241.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0f6c92bbf8728f81f22a46a5655b375338c49ac4a09dbf93d945031821eb9e1e
BLAKE2b-256 checksum
How to use checksums
351aabc7b72e04fc9799c537d9ebb0fe7f9df1694a83b6277d83026242df82be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 4, 2026.

Transparency log

Release files / blockchainkit-0.2.0-py3-none-any.whl

Download URL blockchainkit-0.2.0-py3-none-any.whl
Size 141.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
259c7d7f0f4a54af1658bc94ffd0bc7e35e0ebf21d7039322073025011414dfd
BLAKE2b-256 checksum
How to use checksums
f956bd828e01fff89ea3d0d15284e4354a918b935f7e93cdf5a824b77a386447
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page