diff --git a/Cargo.lock b/Cargo.lock index 3f0e0c87..2400f778 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -989,6 +989,12 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "fearless_simd" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a847f35d37ef14f89ccd8028d7d98a43a59d3c9bfae690ca7a8a95a5937ff9b5" + [[package]] name = "fiat-crypto" version = "0.2.9" @@ -2649,6 +2655,7 @@ version = "0.0.1" name = "vot-fec" version = "0.0.1" dependencies = [ + "fearless_simd", "sha2 0.11.0", ] diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index f827f5bd..0eee8420 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -29,6 +29,7 @@ Minimum Rust version: 1.88. | `rustix` | 1.1.4 | Safe Linux `O_DIRECT` | | `fs4` | 1.1.0 | Cross-platform file locking for the resume store and journal writer leases | | `libc` | 0.2.189 | Don't-fragment socket options, process metrics | +| `fearless_simd` | 0.5.0 | Runtime-dispatched AVX2/NEON FEC kernels with scalar fallback | ### Optional (`s3-live` feature) @@ -39,11 +40,18 @@ Minimum Rust version: 1.88. ### Unsafe in dependencies -`curve2551919-dalek` (via `ed25519-dalek`) contains unsafe in its backends. +`curve25519-dalek` (via `ed25519-dalek`) contains unsafe in its backends. This does not relax the workspace lint (which governs VOT's own crates only) but is part of the audited surface. Accepted because a receipt that cannot be verified without the ability to forge it is not evidence (ADR-0017). +`fearless_simd` is maintained by the Linebender project and dual-licensed +MIT/Apache-2.0. Version 0.5.0 supports the workspace's Rust 1.88 minimum, has +no transitive runtime dependencies, and is exact-pinned in the lockfile. It +contains narrowly scoped unsafe target-feature dispatch and representation +transmutes behind safe CPU-feature tokens. VOT uses that surface only for +AVX2 and NEON GF(2^8) kernels; unsupported targets retain the scalar path. + ## Test-only dependencies | Crate | Version | Purpose | diff --git a/adr/0039-datagram-fec-erasure-code.md b/adr/0039-datagram-fec-erasure-code.md index b1d39330..b8862a06 100644 --- a/adr/0039-datagram-fec-erasure-code.md +++ b/adr/0039-datagram-fec-erasure-code.md @@ -58,5 +58,5 @@ its crate exist. a receiver accepted. - Changing the field, polynomial, or matrix is a new code and needs a new extension identifier; version zero of the vectors is frozen. -- The extension stays negotiated and disabled by default; nothing on the - reliable path changes. +- The extension is offered by default. Local path-loss policy enables coding; + clean traffic stays on the reliable path. diff --git a/crates/vot-fec/Cargo.toml b/crates/vot-fec/Cargo.toml index 126e4602..4054d8be 100644 --- a/crates/vot-fec/Cargo.toml +++ b/crates/vot-fec/Cargo.toml @@ -6,7 +6,8 @@ license.workspace = true rust-version.workspace = true publish = false -[dependencies] +[target.'cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))'.dependencies] +fearless_simd = "=0.5.0" [dev-dependencies] sha2 = { workspace = true } diff --git a/crates/vot-fec/src/gf.rs b/crates/vot-fec/src/gf.rs index 9df32032..3a6eae2d 100644 --- a/crates/vot-fec/src/gf.rs +++ b/crates/vot-fec/src/gf.rs @@ -1,5 +1,27 @@ //! GF(2^8) with the 0x11D polynomial and generator 2 (`spec/fec.md` section 1). +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))] +use std::sync::LazyLock; + +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))] +use fearless_simd::Level; + +#[cfg(target_arch = "aarch64")] +use core::arch::aarch64::{uint8x16_t, vandq_u8, vdupq_n_u8, veorq_u8, vqtbl1q_u8, vshrq_n_u8}; +#[cfg(target_arch = "x86")] +use core::arch::x86::{ + __m256i, _mm256_and_si256, _mm256_set1_epi8, _mm256_shuffle_epi8, _mm256_srli_epi16, + _mm256_xor_si256, +}; +#[cfg(target_arch = "x86_64")] +use core::arch::x86_64::{ + __m256i, _mm256_and_si256, _mm256_set1_epi8, _mm256_shuffle_epi8, _mm256_srli_epi16, + _mm256_xor_si256, +}; + +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))] +static SIMD_LEVEL: LazyLock = LazyLock::new(Level::new); + const POLYNOMIAL: u16 = 0x11D; /// `EXP[i] = 2^i` for `i` in `0..255`, then repeated so a sum of two logs @@ -76,6 +98,25 @@ const fn product_tables() -> [[u8; 256]; 256] { static PRODUCTS: [[u8; 256]; 256] = product_tables(); +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))] +const fn nibble_tables() -> [[[u8; 16]; 2]; 256] { + let mut tables = [[[0_u8; 16]; 2]; 256]; + let mut coefficient = 0; + while coefficient < 256 { + let mut nibble = 0; + while nibble < 16 { + tables[coefficient][0][nibble] = PRODUCTS[coefficient][nibble]; + tables[coefficient][1][nibble] = PRODUCTS[coefficient][nibble << 4]; + nibble += 1; + } + coefficient += 1; + } + tables +} + +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))] +static NIBBLES: [[[u8; 16]; 2]; 256] = nibble_tables(); + fn products_of(coefficient: u8) -> &'static [u8; 256] { &PRODUCTS[coefficient as usize] } @@ -92,12 +133,105 @@ pub(crate) fn mul_add(out: &mut [u8], coefficient: u8, symbol: &[u8]) { } return; } + + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + if let Some(avx2) = SIMD_LEVEL.as_avx2() { + mul_add_avx2(avx2, out, coefficient, symbol); + return; + } + + #[cfg(target_arch = "aarch64")] + if let Some(neon) = SIMD_LEVEL.as_neon() { + mul_add_neon(neon, out, coefficient, symbol); + return; + } + + mul_add_scalar(out, coefficient, symbol); +} + +fn mul_add_scalar(out: &mut [u8], coefficient: u8, symbol: &[u8]) { let products = products_of(coefficient); - for (o, s) in out.iter_mut().zip(symbol) { + let mut out_chunks = out.chunks_exact_mut(8); + let mut symbol_chunks = symbol.chunks_exact(8); + for (o, s) in out_chunks.by_ref().zip(symbol_chunks.by_ref()) { + o[0] ^= products[s[0] as usize]; + o[1] ^= products[s[1] as usize]; + o[2] ^= products[s[2] as usize]; + o[3] ^= products[s[3] as usize]; + o[4] ^= products[s[4] as usize]; + o[5] ^= products[s[5] as usize]; + o[6] ^= products[s[6] as usize]; + o[7] ^= products[s[7] as usize]; + } + for (o, s) in out_chunks + .into_remainder() + .iter_mut() + .zip(symbol_chunks.remainder()) + { *o ^= products[*s as usize]; } } +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +fearless_simd::kernel!( + #[inline] + fn mul_add_avx2(avx2: Avx2, out: &mut [u8], coefficient: u8, symbol: &[u8]) { + use fearless_simd::{SimdBase, SimdInto, u8x16, u8x32}; + + let tables = &NIBBLES[coefficient as usize]; + let low_table: __m256i = u8x32::block_splat(u8x16::from_slice(avx2, &tables[0])).into(); + let high_table: __m256i = u8x32::block_splat(u8x16::from_slice(avx2, &tables[1])).into(); + let mask = _mm256_set1_epi8(0x0f); + let mut out_chunks = out.chunks_exact_mut(32); + let mut symbol_chunks = symbol.chunks_exact(32); + for (o, s) in out_chunks.by_ref().zip(symbol_chunks.by_ref()) { + let output: __m256i = u8x32::from_slice(avx2, o).into(); + let input: __m256i = u8x32::from_slice(avx2, s).into(); + let low = _mm256_and_si256(input, mask); + let high = _mm256_and_si256(_mm256_srli_epi16::<4>(input), mask); + let product = _mm256_xor_si256( + _mm256_shuffle_epi8(low_table, low), + _mm256_shuffle_epi8(high_table, high), + ); + let result: u8x32<_> = _mm256_xor_si256(output, product).simd_into(avx2); + result.store_slice(o); + } + mul_add_scalar( + out_chunks.into_remainder(), + coefficient, + symbol_chunks.remainder(), + ); + } +); + +#[cfg(target_arch = "aarch64")] +fearless_simd::kernel!( + #[inline] + fn mul_add_neon(neon: Neon, out: &mut [u8], coefficient: u8, symbol: &[u8]) { + use fearless_simd::{SimdBase, SimdInto, u8x16}; + + let tables = &NIBBLES[coefficient as usize]; + let low_table: uint8x16_t = u8x16::from_slice(neon, &tables[0]).into(); + let high_table: uint8x16_t = u8x16::from_slice(neon, &tables[1]).into(); + let mut out_chunks = out.chunks_exact_mut(16); + let mut symbol_chunks = symbol.chunks_exact(16); + for (o, s) in out_chunks.by_ref().zip(symbol_chunks.by_ref()) { + let output: uint8x16_t = u8x16::from_slice(neon, o).into(); + let input: uint8x16_t = u8x16::from_slice(neon, s).into(); + let low = vandq_u8(input, vdupq_n_u8(0x0f)); + let high = vshrq_n_u8::<4>(input); + let product = veorq_u8(vqtbl1q_u8(low_table, low), vqtbl1q_u8(high_table, high)); + let result: u8x16<_> = veorq_u8(output, product).simd_into(neon); + result.store_slice(o); + } + mul_add_scalar( + out_chunks.into_remainder(), + coefficient, + symbol_chunks.remainder(), + ); + } +); + #[cfg(test)] mod tests { use super::*; @@ -159,15 +293,24 @@ mod tests { #[test] fn mul_add_matches_the_scalar_kernel() { - let symbol: Vec = (0..=255).collect(); - for coefficient in [0_u8, 1, 2, 0x1D, 0x80, 0xFF] { - let mut out = vec![0xA5_u8; 256]; - mul_add(&mut out, coefficient, &symbol); - for (i, byte) in out.iter().enumerate() { + for length in [0, 1, 15, 16, 31, 32, 33, 255, 256, 1_023, 1_024, 1_025] { + let symbol: Vec = (0..length) + .map(|i| u8::try_from(i % 256).expect("under 256")) + .collect(); + for coefficient in 0..=u8::MAX { + let expected: Vec = symbol + .iter() + .map(|value| 0xA5 ^ mul(coefficient, *value)) + .collect(); + let mut scalar = vec![0xA5_u8; length]; + mul_add_scalar(&mut scalar, coefficient, &symbol); + assert_eq!(scalar, expected, "scalar {coefficient} at length {length}"); + + let mut dispatched = vec![0xA5_u8; length]; + mul_add(&mut dispatched, coefficient, &symbol); assert_eq!( - *byte, - 0xA5 ^ mul(coefficient, symbol[i]), - "{coefficient} at {i}" + dispatched, expected, + "dispatched {coefficient} at length {length}" ); } } @@ -195,7 +338,19 @@ mod cost { .map(|i| u8::try_from(i % 251).expect("under 251")) .collect(); let mut out = vec![0_u8; symbol.len()]; - let rounds: u32 = 400_000; + let rounds: u32 = 4_000_000; + + let began = std::time::Instant::now(); + for _ in 0..rounds { + mul_add(&mut out, 1, &symbol); + } + let spent = began.elapsed(); + eprintln!( + "mul_add xor: {rounds} calls of {LENGTH} bytes in {spent:?}, {:.2} GB/s, sink={}", + (f64::from(rounds) * f64::from(LENGTH)) / spent.as_secs_f64() / 1e9, + out[0] + ); + let began = std::time::Instant::now(); for round in 0..rounds { let coefficient = u8::try_from(round % 254).expect("under 254") + 2; diff --git a/fuzz/frame_codec/Cargo.lock b/fuzz/frame_codec/Cargo.lock index b7197ebd..b2366db9 100644 --- a/fuzz/frame_codec/Cargo.lock +++ b/fuzz/frame_codec/Cargo.lock @@ -211,6 +211,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "fearless_simd" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a847f35d37ef14f89ccd8028d7d98a43a59d3c9bfae690ca7a8a95a5937ff9b5" + [[package]] name = "fiat-crypto" version = "0.2.9" @@ -478,6 +484,9 @@ version = "0.0.1" [[package]] name = "vot-fec" version = "0.0.1" +dependencies = [ + "fearless_simd", +] [[package]] name = "vot-frame-fuzz-driver" diff --git a/tools/check_sdk_dependencies.py b/tools/check_sdk_dependencies.py index 0b144aa2..aa8db8ab 100644 --- a/tools/check_sdk_dependencies.py +++ b/tools/check_sdk_dependencies.py @@ -29,6 +29,7 @@ "ed25519", "ed25519-dalek", "fiat-crypto", + "fearless_simd", "generic-array", "hybrid-array", "libc",