Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions adr/0039-datagram-fec-erasure-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion crates/vot-fec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
175 changes: 165 additions & 10 deletions crates/vot-fec/src/gf.rs
Original file line number Diff line number Diff line change
@@ -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<Level> = 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
Expand Down Expand Up @@ -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]
}
Expand All @@ -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::*;
Expand Down Expand Up @@ -159,15 +293,24 @@ mod tests {

#[test]
fn mul_add_matches_the_scalar_kernel() {
let symbol: Vec<u8> = (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<u8> = (0..length)
.map(|i| u8::try_from(i % 256).expect("under 256"))
.collect();
for coefficient in 0..=u8::MAX {
let expected: Vec<u8> = 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}"
);
}
}
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions fuzz/frame_codec/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/check_sdk_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ed25519",
"ed25519-dalek",
"fiat-crypto",
"fearless_simd",
"generic-array",
"hybrid-array",
"libc",
Expand Down
Loading