feat: add native MoonBit I2P router core and embedded SAMv3 bridge
check / check (macos-latest) (push) Failing after 31s
check / check (windows-latest) (push) Failing after 31s
check / check (ubuntu-latest) (push) Failing after 34s

This commit is contained in:
nandi committed 2026-11-27 19:49:34 +00:00
1 parent 4866f3c46d
commit cadb8cd898
6 files changed
+913 -42

No files matched your search

+42 -42
View File
@@ -1,81 +1,81 @@
# moon-i2p
A clean, zero-dependency, pure-[MoonBit](https://www.moonbitlang.com/) port of the modern **I2P (Invisible Internet Project)** protocol suite targeting `wasm`, `wasm-gc`, `js`, and `native`.
A clean, zero-dependency, pure-[MoonBit](https://www.moonbitlang.com/) implementation of the modern **I2P (Invisible Internet Project)** protocol suite and native router core, with pure protocol packages targeting `wasm`, `wasm-gc`, `js`, and `native`.
## Architecture & Implemented Modules
```
moon-i2p/
├── encoding/ # I2P Base64 (-/~), Base32 (.b32.i2p), big-endian Reader/Writer, UTF-8, Hex
├── crypto/ # Pure-MoonBit SHA-256, HMAC-SHA256, HKDF-SHA256, ChaCha20-Poly1305, X25519, SipHash-2-4
├── common/ # Hash, KeyTypes, Certificate/KeyCertificate, Mapping, Destination, RouterIdentity, RouterInfo, Lease2, LeaseSet2
├── crypto/ # Pure-MoonBit SHA-256, SHA-512, HMAC/HKDF-SHA256, ChaCha20-Poly1305, AES-256-CBC, X25519, Ed25519, SipHash-2-4
├── common/ # Hash, KeyTypes, Certificate/KeyCertificate, Mapping, Destination, RouterIdentity, signed RouterInfo & LeaseSet2
├── i2np/ # 16-byte standard & 9-byte NTCP2 short headers, SHA-256 checksums, core I2NP messages
├── noise/ # Noise_XK_25519_ChaChaPoly_SHA256 handshake/transport + NTCP2 SipHash-2-4 length masking
├── ntcp2/ # Pure-MoonBit NTCP2 (Noise_XKaesobfse+hs2+hs3, AES-256-CBC obfuscation, SipHash KDF, data-phase blocks)
├── ntcp2_net/ # Native POSIX TCP transport, OS CSPRNG/clock, and NTCP2 dial/accept session manager
├── netdb/ # Pure-MoonBit NetDB & Floodfill Kademlia DHT (XOR distance, daily routing keys, Ed25519-verified store)
├── tunnel/ # Pure-MoonBit Proposal 157 ShortTunnelBuild (218B records), 1024B TunnelData AES-256-CBC layer crypto, fragmentation & relay
├── sam/ # SAMv3 text protocol parser, serializer, and command builders
├── sam_client/ # Native POSIX TCP SAMv3 client with sane modern I2P defaults
├── router/ # Native MoonBit I2P Router Core with transient identity, NetDB, TunnelManager, NTCP2 listener & embedded SAMv3 bridge
├── cmd/main/ # Pure protocol demo CLI entrypoint
├── cmd/router/ # Native MoonBit I2P router daemon (`--sam`, `--ntcp2-port`, `--peer`)
├── cmd/client/ # Native SAMv3 CLI (`keygen`, `lookup`, `http-get`, `dial`, `listen`)
└── scripts/ # Loopback SAMv3 bridge (`mock_sam.py`) and end-to-end test (`e2e_test.py`)
```
### Verified Test Vectors
### Verified Test Vectors & Interop
Every cryptographic primitive in `crypto/` is verified against official standards:
- **SHA-256**: FIPS 180-4 / NIST test vectors
- **SHA-256 & SHA-512**: FIPS 180-4 / NIST test vectors
- **HMAC-SHA256**: RFC 4231 test cases 1 & 2
- **HKDF-SHA256**: RFC 5869 test case 1
- **ChaCha20-Poly1305 AEAD**: RFC 8439 Section 2.8.2
- **AES-256 & AES-256-CBC**: NIST FIPS-197 Appendix C.3 & SP 800-38A F.2.5/F.2.6
- **X25519**: RFC 7748 Sections 5.2 & 6.1
- **Ed25519**: RFC 8032 Section 7.1 test vectors 1–3 (+ non-canonical `S >= L` & tamper rejection)
- **SipHash-2-4**: Official SipHash reference test vector
- **Live I2P Interop**: Verified real `i2pd` `router.info` parsing + Ed25519 signature verification, and full live NTCP2 handshake (Msg 1 → Msg 2 → Msg 3 → Data Phase SipHash length-masked AEAD frames) against a live public I2P floodfill router.
## Running the SAMv3 Client (Native)
## Running the Native MoonBit Router & SAMv3 Client
The native client (`cmd/client`) connects to an I2P SAMv3 bridge with sane modern defaults:
```bash
# Build the native router and client binaries
moon build --target native cmd/router
moon build --target native cmd/client
# Start the MoonBit router (transient keys, NTCP2 listener + embedded SAMv3 bridge on 127.0.0.1:7656)
./_build/native/debug/build/cmd/router/router.exe --sam 127.0.0.1:7656 --ntcp2-port 14568
# In another terminal, talk to our MoonBit router with cmd/client:
./_build/native/debug/build/cmd/client/client.exe keygen
./_build/native/debug/build/cmd/client/client.exe listen "hello back"
./_build/native/debug/build/cmd/client/client.exe lookup <listener.b32.i2p>
./_build/native/debug/build/cmd/client/client.exe dial <listener.b32.i2p> "hello over i2p"
```
Default SAMv3 client settings:
- **Bridge:** `127.0.0.1:7656` (override with `--sam <host:port>` or `I2P_SAM_HOST` / `I2P_SAM_PORT`)
- **Protocol:** SAM `3.1`–`3.3`
- **Keys:** `TRANSIENT` destination, `SIGNATURE_TYPE=7` (`EdDSA_SHA512_Ed25519`)
- **I2CP Tunnel Defaults:** `i2cp.leaseSetEncType=4,0` (ECIES-X25519 preferred), 3-hop / 3-tunnel inbound & outbound pools
```bash
# Build the native client binary
moon build --target native cmd/client
# Start an I2P router with SAMv3 enabled (e.g. i2pd --sam.enabled=true)
# or use the bundled local loopback SAMv3 bridge for testing:
# python3 scripts/mock_sam.py
# Generate a new Ed25519 I2P destination
./_build/native/debug/build/cmd/client/client.exe keygen
# Resolve an I2P hostname
./_build/native/debug/build/cmd/client/client.exe lookup stats.i2p
# Fetch an eepsite over a transient STREAM session
./_build/native/debug/build/cmd/client/client.exe http-get stats.i2p /
# Peer-to-peer STREAM listener and dialer
./_build/native/debug/build/cmd/client/client.exe listen "hello back"
./_build/native/debug/build/cmd/client/client.exe dial <listener.b32.i2p> "hello over i2p"
```
## Running Checks & Tests
```bash
moon check --target wasm
moon test --target wasm
moon check --target native
moon test --target js
moon test --target native
moon build --target native cmd/client && python3 scripts/e2e_test.py
moon build --target native cmd/router && moon build --target native cmd/client
python3 scripts/e2e_test.py
```
## Roadmap
1. **Phase 1–2 (Completed):** Core encoding, pure-MoonBit modern crypto, I2P Common Structures, I2NP messages, `Noise_XK_25519_ChaChaPoly_SHA256` state machine, NTCP2 SipHash length masking, SAMv3 protocol codec, and working native SAMv3 TCP client + CLI (`sam_client/`, `cmd/client/`).
2. **Phase 3 (Signatures & Ratchet):**
- Pure-MoonBit **Ed25519** signing & verification (`RouterInfo` & `LeaseSet2` signatures).
- **ECIES-X25519-AEAD-Ratchet** (Proposal 144) for Garlic encryption & end-to-end sessions.
3. **Phase 4 (Direct Transports):**
- **NTCP2** full TCP transport (AES-256-CBC ephemeral key obfuscation, options/padding blocks, session manager).
4. **Phase 5 (Router Core):**
- **NetDB** & Floodfill DHT (Kademlia XOR routing with daily routing key derivation).
- **Tunnel** builder (`ShortTunnelBuild` Proposal 157), layered tunnel encryption, and tunnel pool management.
- **Streaming** & **Repliable Datagrams** libraries.
- [x] **Phase 1–2:** Core encoding, pure-MoonBit modern crypto, I2P Common Structures, I2NP messages, `Noise_XK_25519_ChaChaPoly_SHA256`, SAMv3 codec, and native SAMv3 TCP client + CLI (`sam_client/`, `cmd/client/`).
- [x] **Phase 3 (Signatures & Block Crypto):** Pure-MoonBit **SHA-512**, **Ed25519** (RFC 8032), **AES-256-CBC** (FIPS-197 / SP 800-38A), and Ed25519-signed `RouterInfo` & `LeaseSet2` (Proposal 123).
- [x] **Phase 4 (Direct Transport):** Full **NTCP2** protocol (`ntcp2/`) + native POSIX TCP transport (`ntcp2_net/`), verified live against public I2P floodfill routers.
- [x] **Phase 5 (Router Core):** Pure-MoonBit **NetDB** & Floodfill Kademlia DHT (`netdb/`), Proposal 157 **ShortTunnelBuild** & 1024-byte `TunnelData` layer crypto/relay (`tunnel/`), and native **Router Core** + embedded SAMv3 bridge (`router/`, `cmd/router/`).
- [ ] **Phase 6 (Full End-to-End I2P Network Tunneling):**
- **ECIES-X25519-AEAD-Ratchet** (Proposal 144) for Garlic encryption & end-to-end remote peer sessions.
- Active remote floodfill NetDB lookup/publish over NTCP2 + gzip decompression for `DatabaseStore` type 0.
- **I2P Streaming** (SYN/ACK/windowing) & **Repliable Datagrams** over multi-hop remote tunnels.