[PR #6437] [MERGED] Connection cipher: hardware AES, CFB8 decryption many bytes at once #6871

Closed
opened 2026-11-03 06:28:25 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6437
Author: @ludovic111
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @ludovic111

Base: main ← Head: perf/cfb8-hw-aes


📝 Commits (2)

  • 77cb31c Connection cipher: hardware AES, CFB8 decryption many bytes at once
  • 9c55432 launcher/Cargo.lock: horizon-protocol depends on aes

📊 Changes

6 files changed (+309 additions, -122 deletions)

View changed files

📝 Cargo.lock (+1 -0)
📝 crates/horizon-protocol/Cargo.toml (+2 -0)
📝 crates/horizon-protocol/src/crypto.rs (+96 -121)
➕ crates/horizon-protocol/src/crypto/blocks.rs (+207 -0)
📝 crates/horizon-protocol/src/lib.rs (+2 -1)
📝 launcher/Cargo.lock (+1 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

What

Online-mode servers encrypt every byte they send (AES/CFB8). The client decrypted it with a byte-wise software AES: one full AES block per byte, S-box lookups and xtime in loops, about 460 ns per byte, so ~2 MB/s for a whole core. A render distance 32 join (30 MB on the wire from a Horizon server) cost ~8-9.5 s of CPU on the network thread just to decrypt, and the network thread could not receive faster than that whatever the server did.

  • CFB8 decryption knows each byte's register up front (the 16 ciphertext bytes before it), so runs of bytes are encrypted together (crypto/blocks.rs): 8 at a time on ARM, 32 through the aes crate.
  • Apple Silicon (aarch64 with the AES extension at compile time, which aarch64-apple-darwin always has): ARMv8 AESE/AESMC directly. This needs unsafe to call the intrinsics, so horizon-protocol goes from forbid(unsafe_code) to deny, with one #[allow(unsafe_code)] module (three unsafe calls, each gated by cfg(target_feature = "aes")).
  • Why the unsafe exception is narrow and worth it: without the intrinsics, Apple Silicon has only software AES (the aes crate's constant-time fallback measured no faster than the old code there), so the ×240 needs them. The exception is one module: crate root deny(unsafe_code) instead of forbid, #[allow(unsafe_code)] on crypto/blocks.rs's aarch64 module only, every unsafe block with a SAFETY comment (the module is compiled only when cfg(target_feature = "aes") guarantees the instructions), and tests against the software path (lanes_match_single_blocks_and_the_aes_crate: each lane against single blocks and RustCrypto's AES, plus the FIPS-197/SP 800-38A vectors).
  • Everywhere else: RustCrypto's aes 0.8.4 (MIT OR Apache-2.0, already in Cargo.lock through aes-gcm). It picks AES-NI at run time on x86/x86_64, and a constant-time software AES elsewhere (no faster than the old code, no slower).
  • Encryption (what the client sends) stays one block per byte, as CFB8 requires. On ARM the register stays in a vector register.

Protocol is unchanged: same cipher, same bytes. Tests: FIPS-197 and NIST SP 800-38A vectors, the 8/32-block path against single blocks and the aes crate, and decryption against the byte-by-byte definition however reads cut the stream.

Numbers

Micro-bench, 4 MB stream, best of 5 (old code timed on 256 KB):

before after
Mac mini M4 Pro, decrypt 455 ns/byte (2.2 MB/s) 1.6-1.9 ns/byte (530-630 MB/s) ×240-280
Mac mini M4 Pro, encrypt 665 ns/byte 25-55 ns/byte (load 40-100) ×12-25
AMD EPYC 7662 (playhorizon), decrypt 460-495 ns/byte 6.4-11 ns/byte ×45-75

A real join replayed: horizon-bots --capture of a Horizon dedicated server, RD 32, 3725 chunks, 116k packets, 30.2 MB on the wire, replayed through ConnReader::recv on one EPYC core (bench tool in #6436):

recv for every packet main this PR
online-mode (compressed + encrypted) 8.0-9.5 s 0.51-0.61 s
offline (compressed) 0.42-0.62 s 0.41-0.47 s (unchanged)

A vanilla 26.3 server's capture (1262 chunks, 170k packets) decodes with 0 errors before and after.

Vanilla's Java cipher gets the CPU's AES instructions from HotSpot's AES intrinsics; Horizon now uses them too.

Self-check

pr_ready (tools/pr_ready.py --quick on playhorizon, AMD EPYC, against origin/main at 455b6916b, 130 min, head 77cb31ce1; 9c554326e only adds the launcher lockfile line)

Check Result
Scope ✅ 5 files, +308 −122
Cargo.lock ✅ changes with a Cargo.toml
Public tree ✅
Python tests ✅ 586 passed (3 min 32 s)
rustfmt ✅ (38 s)
clippy ✅ (30 min 45 s)
tests: workspace ⚠️ 1259 passed, 2 failed (77 min 24 s): title::tests::backup_toast_slides_in_and_out_with_its_sounds (timing, passes alone) and entities::armor_stand_motion_tests::a_tick_keeps_the_previous_position_for_interpolation, which needs .local prepared data (CI skips it) and doesn't reach horizon-protocol; not rerun on main
doc tests ✅ (17 s)
content root tests ✅ 21 passed (17 min 48 s)
launcher rustfmt ✅
launcher clippy / tests ❌ locally on 77cb31ce1 (launcher/Cargo.lock lacked aes): fixed in 9c554326e, CI launcher ✅
Parity ➖ 375 affected, to run in CI

The ARMv8 path (CI is Linux only) ran on the Mac mini M4 Pro: the same blocks.rs and Cfb8 in a standalone crate, the four crypto tests pass (FIPS-197, SP 800-38A, lanes vs single blocks vs the aes crate, decryption vs the byte-by-byte definition).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/6437 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/cfb8-hw-aes` --- ### 📝 Commits (2) - [`77cb31c`](https://github.com/horizon-rust/horizon/commit/77cb31ce19fa4b03aef5f3b1c4f586211d2ba15c) Connection cipher: hardware AES, CFB8 decryption many bytes at once - [`9c55432`](https://github.com/horizon-rust/horizon/commit/9c554326ee49beb460b3f7efb01cdc3d470e4ed2) launcher/Cargo.lock: horizon-protocol depends on aes ### 📊 Changes **6 files changed** (+309 additions, -122 deletions) <details> <summary>View changed files</summary> 📝 `Cargo.lock` (+1 -0) 📝 `crates/horizon-protocol/Cargo.toml` (+2 -0) 📝 `crates/horizon-protocol/src/crypto.rs` (+96 -121) ➕ `crates/horizon-protocol/src/crypto/blocks.rs` (+207 -0) 📝 `crates/horizon-protocol/src/lib.rs` (+2 -1) 📝 `launcher/Cargo.lock` (+1 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Online-mode servers encrypt every byte they send (AES/CFB8). The client decrypted it with a byte-wise software AES: one full AES block per byte, S-box lookups and `xtime` in loops, about **460 ns per byte**, so **~2 MB/s for a whole core**. A render distance 32 join (30 MB on the wire from a Horizon server) cost **~8-9.5 s of CPU on the network thread** just to decrypt, and the network thread could not receive faster than that whatever the server did. - CFB8 decryption knows each byte's register up front (the 16 ciphertext bytes before it), so runs of bytes are encrypted together (`crypto/blocks.rs`): 8 at a time on ARM, 32 through the `aes` crate. - **Apple Silicon** (`aarch64` with the AES extension at compile time, which `aarch64-apple-darwin` always has): ARMv8 `AESE`/`AESMC` directly. This needs `unsafe` to call the intrinsics, so `horizon-protocol` goes from `forbid(unsafe_code)` to `deny`, with one `#[allow(unsafe_code)]` module (three `unsafe` calls, each gated by `cfg(target_feature = "aes")`). - Why the `unsafe` exception is narrow and worth it: without the intrinsics, Apple Silicon has only software AES (the `aes` crate's constant-time fallback measured no faster than the old code there), so the ×240 needs them. The exception is one module: crate root `deny(unsafe_code)` instead of `forbid`, `#[allow(unsafe_code)]` on `crypto/blocks.rs`'s aarch64 module only, every `unsafe` block with a `SAFETY` comment (the module is compiled only when `cfg(target_feature = "aes")` guarantees the instructions), and tests against the software path (`lanes_match_single_blocks_and_the_aes_crate`: each lane against single blocks and RustCrypto's AES, plus the FIPS-197/SP 800-38A vectors). - **Everywhere else**: RustCrypto's `aes` 0.8.4 (MIT OR Apache-2.0, already in `Cargo.lock` through `aes-gcm`). It picks AES-NI at run time on x86/x86_64, and a constant-time software AES elsewhere (no faster than the old code, no slower). - Encryption (what the client sends) stays one block per byte, as CFB8 requires. On ARM the register stays in a vector register. Protocol is unchanged: same cipher, same bytes. Tests: FIPS-197 and NIST SP 800-38A vectors, the 8/32-block path against single blocks and the `aes` crate, and decryption against the byte-by-byte definition however reads cut the stream. ## Numbers Micro-bench, 4 MB stream, best of 5 (old code timed on 256 KB): | | before | after | | |---|---|---|---| | Mac mini M4 Pro, decrypt | 455 ns/byte (2.2 MB/s) | 1.6-1.9 ns/byte (530-630 MB/s) | ×240-280 | | Mac mini M4 Pro, encrypt | 665 ns/byte | 25-55 ns/byte (load 40-100) | ×12-25 | | AMD EPYC 7662 (playhorizon), decrypt | 460-495 ns/byte | 6.4-11 ns/byte | ×45-75 | A real join replayed: `horizon-bots --capture` of a Horizon dedicated server, RD 32, 3725 chunks, 116k packets, 30.2 MB on the wire, replayed through `ConnReader::recv` on one EPYC core (bench tool in #6436): | `recv` for every packet | main | this PR | |---|---|---| | online-mode (compressed + encrypted) | 8.0-9.5 s | 0.51-0.61 s | | offline (compressed) | 0.42-0.62 s | 0.41-0.47 s (unchanged) | A vanilla 26.3 server's capture (1262 chunks, 170k packets) decodes with 0 errors before and after. Vanilla's Java cipher gets the CPU's AES instructions from HotSpot's AES intrinsics; Horizon now uses them too. ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on playhorizon, AMD EPYC, against origin/main at 455b6916b, 130 min, head 77cb31ce1; 9c554326e only adds the launcher lockfile line) | Check | Result | | --- | --- | | Scope | ✅ 5 files, +308 −122 | | Cargo.lock | ✅ changes with a Cargo.toml | | Public tree | ✅ | | Python tests | ✅ 586 passed (3 min 32 s) | | rustfmt | ✅ (38 s) | | clippy | ✅ (30 min 45 s) | | tests: workspace | ⚠️ 1259 passed, 2 failed (77 min 24 s): `title::tests::backup_toast_slides_in_and_out_with_its_sounds` (timing, passes alone) and `entities::armor_stand_motion_tests::a_tick_keeps_the_previous_position_for_interpolation`, which needs `.local` prepared data (CI skips it) and doesn't reach horizon-protocol; not rerun on main | | doc tests | ✅ (17 s) | | content root tests | ✅ 21 passed (17 min 48 s) | | launcher rustfmt | ✅ | | launcher clippy / tests | ❌ locally on 77cb31ce1 (launcher/Cargo.lock lacked `aes`): fixed in 9c554326e, CI `launcher` ✅ | | Parity | ➖ 375 affected, to run in CI | The ARMv8 path (CI is Linux only) ran on the Mac mini M4 Pro: the same `blocks.rs` and `Cfb8` in a standalone crate, the four crypto tests pass (FIPS-197, SP 800-38A, lanes vs single blocks vs the `aes` crate, decryption vs the byte-by-byte definition). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:28:25 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#6871
No description provided.