Skip to content

JIT AES, PMULL and SHA-1 to speed up AArch64 crypto - #5

Merged
losfair merged 2 commits into
mainfrom
perf/crypto-jit
Oct 25, 2026
Merged

losfair merged 2 commits into
mainfrom
perf/crypto-jit

Conversation

@losfair

@losfair losfair commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

AArch64 crypto libraries select AES, PMULL and SHA-1 using kemu’s advertised capabilities, but those instructions currently invoke the interpreter individually. This translates them directly and removes the scalar lane moves/shifts that otherwise keep OpenSSL’s GHASH loop entering the kernel.

  • AESE/AESD/AESMC/AESIMC use AES-NI; 64-bit PMULL/PMULL2 use PCLMULQDQ. Byte-wise PMULL/PMULL2 and all six SHA-1 instructions use AVX2.
  • Scalar SIMD lane copies, scalar SHL and byte-vector SHL also stay in translated code. Sampling AES-GCM after lowering only the crypto instructions found five scalar lane moves and a scalar shift dominating its remaining ~11 million helper calls in two seconds.
  • Kernel and userspace wrappers detect optional host features. Hosts without AES/PCLMUL retain interpreter fallback, with no increase to the x86-64-v3 minimum. The core stays safe Rust. Lowerings read aliased inputs before a single destination commit and preserve NZCV/FPCR/FPSR for interruption recovery.
  • Add mode 7 differential tests, host-feature masking, a reproducible crypto instruction benchmark, and a KEMU_CPU VM override for testing absent CPU features.

Performance was measured against main 9ee0d5d on an i7-13700H shared development host, Linux 6.18 under QEMU/KVM, one vCPU and 2 GiB RAM pinned to host CPU 2. Our builds and stress tests were stopped during timing. Native x86_64 and emulated arm64 OpenSSL are both version 3.5.7 with default hardware feature selection.

OpenSSL medians from three alternating baseline/patched VM runs, using speed -elapsed -seconds 2 -bytes 16384 -evp <algorithm>. Native measurements run in the same patched VM as the arm64 measurements. Throughput is decimal MB/s:

OpenSSL workload Before After Native x86_64 Improvement
AES-128-GCM 3.12 506.27 4739.78 162.2×
SHA-1 4.95 184.39 1569.38 37.3×

AES-GCM remains 9.4× below native, and SHA-1 8.5× below native. Guest vectors still reside in memory between translated instructions, and SHA-1 uses the portable AVX2 lowering.

Instruction microbenchmarks (tests/bench/crypto.c + crypto.S): median of three runs, 2,000,000 iterations per case after warming translations. Times include loop and operand-handling overhead and are not full cipher/hash throughput:

Instruction mix Before (ns/instruction) After (ns/instruction) Improvement
AESE + AESMC 243.97 3.81 64.1×
AESD + AESIMC 202.67 3.92 51.7×
PMULL/PMULL2, 64-bit 383.33 3.51 109.1×
PMULL/PMULL2, 8-bit 327.43 5.85 56.0×
SHA-1 instructions 248.88 7.49 33.2×

Validation on the final implementation:

  • 60,000 crypto/GHASH differential cases: normal, asynchronous interruption + relocation, and AES/PCLMUL disabled. Exact helper counts catch unexpected interpreter fallback; cases include aliases, V31, all scalar lane sizes, shift boundaries, live flags and nonzero FPSR.
  • 150,000 general JIT differential cases, including 50,000 with asynchronous interruption + relocation; 28 core tests on Rust 1.88.0.
  • 15,200 independent QEMU comparisons for the reference interpreter (7,000 crypto + 8,200 GHASH move/shift cases). All benchmark checksums match QEMU before and after, including the userspace runner.
  • Native kernel build, all 34 ABI and 8 tagged-pointer checks, threading stress and integer benchmarks pass. A separate VM with AES, PCLMUL, SHA-NI, VAES and VPCLMUL hidden passes the crypto benchmark with matching checksums.
  • Userspace CI passes on the final revision. Kernel CI is running.

@losfair losfair changed the title JIT AES, PMULL and SHA-1 crypto instructions JIT AES, PMULL and SHA-1 to speed up AArch64 crypto Oct 25, 2026
@losfair
losfair marked this pull request as ready for review October 25, 2026 07:39
@losfair
losfair merged commit c771695 into main Oct 25, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant