Back to projects
May 2026

bitbit

BitTorrent Protocol Client & Trackerless Kademlia DHT

Go · BitTorrent Spec (BEP 0003) · Kademlia DHT (BEP 0005) · Bencode · SHA-1 Cryptography · Network Socket Wire Protocol

Highlights

  • Downloaded a 1.24 GB Arch Linux ISO with 0 centralized tracker nodes
  • SHA-1 piece integrity verification across 2,373 pieces
  • Tit-for-tat unchoking with 10s recalibration and 30s optimistic unchoking
  • 160-bit Kademlia DHT with XOR routing and iterative k-closest lookups

System Overview

bitbit is a BitTorrent client and peer wire implementation written in Go from scratch. It features bencode parsing, SHA-1 piece integrity verification, rarest-first scheduling, tit-for-tat choking, and a trackerless Kademlia DHT overlay.

The Problem & Architectural Rationale

Centralized file distribution suffers from single-origin bandwidth exhaustion and single points of failure. Peer swarming solves this but requires distributed piece scheduling, cryptographic piece verification, and game-theoretic incentives to enforce fair peer uploading.

Architecture & Components

1. Bencode Parser & Metainfo

Custom decoder and encoder for bencoded .torrent files, extracting infohashes, piece lengths, and SHA-1 chunk arrays.

2. Piece Selection Policies

Implements Strict Policy for fast piece completion, Rarest First to distribute scarce pieces before seeds leave, Random First bootstrap for new leechers, and Endgame broadcast mode for the final missing blocks.

3. Tit-for-Tat Choker

Calculates recent peer download rates every 10 seconds to unchoke the top 4 fastest peers, and executes an Optimistic Unchoke every 30 seconds to discover new peers.

4. Kademlia DHT Routing

Maintains routing k-buckets sorted by XOR distance over a 160-bit keyspace, executing recursive FIND_NODE / FIND_VALUE RPC lookups for decentralized swarm discovery.

Core Implementation

GO
// Rarest First piece selection across connected peers' bitfields
func RarestFirst(peers []Peer, totalPieces int, myPieces map[int]bool) int {
    count := make([]int, totalPieces)
    for _, p := range peers {
        for piece := range p.Pieces {
            count[piece]++
        }
    }
    rarest, minCount := -1, math.MaxInt
    for piece, c := range count {
        if myPieces[piece] || c == 0 { continue }
        if c < minCount {
            minCount = c
            rarest = piece
        }
    }
    return rarest
}

Rarest-first piece scheduling algorithm

Outcomes & Results

  • Validated end-to-end swarm download of a 1.24 GB Linux ISO with zero active tracker servers.
  • Verified SHA-1 hash integrity across 2,373 individual pieces.