Initial commit: Naut-Torrent — from-scratch 10 GbE BitTorrent client

A maintainable, extensible BitTorrent client (C11, Linux/io_uring) targeting
10 GbE saturation. All torrent functionality is built from scratch; liburing
is the only linked third-party dependency on the data path.

Implements Phases 1-7 of the roadmap:
- core: page-aligned buffer pool, MPMC/Treiber queues, bitfields, worker pool
- crypto: SHA-1/256 (SHA-NI + scalar), Merkle (BEP-52), RC4 (MSE)
- bencode/metainfo: zero-copy parser, v1/v2/hybrid .torrent + magnet
- peer: sans-IO wire codec, MSE/PE handshake state machine, BEP-10, ut_metadata, PEX
- piece/storage: block-level multi-peer engine, rarest-first + endgame,
  per-file completion events + single-file relocate (move-as-you-finish)
- tracker/dht: HTTP + UDP (BEP-15) trackers, BEP-5 KRPC iterative lookup
- platform: io_uring reactor (SQPOLL, registered buffers, SEND_ZC)
- surface: versioned RPC, native plugin ABI, sandboxed Lua scripting, nautd/nautctl

Verified against libtorrent (single/multi/hybrid, MSE, magnet-via-DHT, swarm);
unit + interop tests green; ASan/UBSan/TSan clean. Scripting reference in
docs/scripting.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ookami125 2026-06-15 12:12:00 -04:00
commit 2178d6a70c
121 changed files with 12644 additions and 0 deletions

50
tests/bench/bench_hash.c Normal file
View file

@ -0,0 +1,50 @@
/* Hash throughput bench — the Phase 2 gate (SHA-256 >= 1.5 GB/s/core). */
#include "naut/hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static double now(void) {
struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec + t.tv_nsec * 1e-9;
}
static double bench(const char *name, void (*h)(const void *, size_t, uint8_t *),
uint8_t *buf, size_t len, int iters, int outlen) {
uint8_t out[32];
/* warm */
h(buf, len, out);
double t0 = now();
for (int i = 0; i < iters; i++) h(buf, len, out);
double dt = now() - t0;
double gb = (double)len * iters / 1e9;
printf(" %-10s %6.2f GB/s (%d-byte digest)\n", name, gb / dt, outlen);
return gb / dt;
}
static void s1(const void *d, size_t n, uint8_t *o) { naut_sha1(d, n, o); }
static void s256(const void *d, size_t n, uint8_t *o) { naut_sha256(d, n, o); }
int main(int argc, char **argv) {
size_t len = (argc > 1) ? (size_t)atoll(argv[1]) * 1024 * 1024 : 256u * 1024 * 1024;
int iters = (argc > 2) ? atoi(argv[2]) : 8;
uint8_t *buf = malloc(len);
if (!buf) { perror("malloc"); return 1; }
memset(buf, 0xa5, len);
printf("buffer %zu MiB x %d iters | sha256 backend: %s\n",
len >> 20, iters, naut_sha256_backend());
double s256_gbs = bench("sha256", s256, buf, len, iters, 32);
bench("sha1", s1, buf, len, iters, 20);
free(buf);
/* gate: a single core must clear the 1.25 GB/s line rate with margin */
if (s256_gbs < 1.5) {
fprintf(stderr, "GATE FAIL: sha256 %.2f GB/s < 1.5 GB/s\n", s256_gbs);
return 1;
}
printf("GATE PASS: sha256 %.2f GB/s >= 1.5 GB/s\n", s256_gbs);
return 0;
}