ore-rs 0.9.0

Order-revealing encryption library used by the CipherStash searchable encryption platform
Documentation
//! Vectorised kernels for the data-parallel inner loops of right-ciphertext
//! encoding, with portable scalar fallbacks (v2 plan §3).
//!
//! Two operations are covered:
//!
//! - **`gt_mask_xor_256`** — XOR the indicator mask `bit j = (table[j] > x)`
//!   into a 32-byte bitvector. This is the bulk form of the per-block
//!   unary encoding (a bytewise compare against a broadcast value, packed
//!   to a bitmask — `cmgt`+pack on NEON, `vpcmpgtb`+`vpmovmskb` on AVX2).
//! - **`lsb_mask_256`** — pack `bit j = blocks[j][0] & 1` (a stride-16
//!   gather over AES outputs) into a 32-byte bitvector.
//!
//! Bit order everywhere is LSB-first within each byte, matching
//! `RightBlock32::set_bit`.
//!
//! # Dispatch
//!
//! - **aarch64:** NEON is baseline — compile-time dispatch, no detection.
//! - **x86_64:** AVX2 via `is_x86_feature_detected!` (cached by `std`) for
//!   the indicator mask; the LSB gather stays scalar (the stride-16 gather
//!   doesn't pay on AVX2 without AVX-512 VBMI, and the scalar form is
//!   already branch-free and fast).
//! - **everything else:** scalar.
//!
//! # Constant-time discipline
//!
//! Every path in this module — scalar and vector — has a fixed trip count,
//! no data-dependent branches, and no data-dependent memory addressing
//! (the NEON `tbl` indices are compile-time constants). Equivalence
//! between scalar and vector paths is pinned by tests below; equivalence
//! against the per-bit reference is pinned in `prp.rs`.

use super::{AesBlock, Packed};
use core::convert::TryFrom;

/// XOR the indicator mask for `x` over `table` into `out`:
/// `out[j/8] ^= ((table[j] > x) as u8) << (j%8)`.
#[inline]
pub(crate) fn gt_mask_xor_256(table: &[u8; 256], x: u8, out: &mut [u8; 32]) {
    #[cfg(target_arch = "aarch64")]
    // SAFETY: NEON is baseline on aarch64; the lengths are in the types.
    unsafe {
        neon::gt_mask_xor_256(table, x, out)
    }

    // Excluded on aarch64, where the NEON path above always handles it, so
    // the fallback is never unreachable code needing an `allow`.
    #[cfg(not(target_arch = "aarch64"))]
    {
        #[cfg(target_arch = "x86_64")]
        if is_x86_feature_detected!("avx2") {
            // SAFETY: AVX2 presence just checked; the lengths are in the types.
            unsafe { avx2::gt_mask_xor_256(table, x, out) };
            return;
        }

        scalar::gt_mask_xor(table, x, out);
    }
}

/// 64-lane variant of [`gt_mask_xor_256`] for the Bit6 block domain.
/// AVX2 gains little at this size (two 32-lane compares vs the scalar
/// loop's ~9 ns); only NEON is dispatched.
#[inline]
pub(crate) fn gt_mask_xor_64(table: &[u8; 64], x: u8, out: &mut [u8; 8]) {
    #[cfg(target_arch = "aarch64")]
    // SAFETY: NEON is baseline on aarch64; the lengths are in the types.
    unsafe {
        neon::gt_mask_xor_64(table, x, out)
    }

    #[cfg(not(target_arch = "aarch64"))]
    scalar::gt_mask_xor(table, x, out);
}

/// Pack the LSB of byte 0 of each block into `out`: bit `j` of `out` is
/// `blocks[j][0] & 1` (bits are assigned, not accumulated). The 256-block
/// case (Bit8's per-block RO output) has a vectorised gather; other sizes
/// use the scalar pack. `D` must be `8 * B`, checked at compile time.
#[inline]
pub(crate) fn lsb_mask<const D: usize, const B: usize>(blocks: &[AesBlock; D], out: &mut [u8; B]) {
    // Decided by `D` and `B` alone, so the untaken arm is compiled out.
    if let (Ok(blocks), Ok(out)) = (
        <&[AesBlock; 256]>::try_from(blocks.as_slice()),
        <&mut [u8; 32]>::try_from(out.as_mut_slice()),
    ) {
        lsb_mask_256(blocks, out);
        return;
    }
    scalar::lsb_mask(blocks, out);
}

/// [`lsb_mask`] for 256 blocks.
#[inline]
pub(crate) fn lsb_mask_256(blocks: &[AesBlock; 256], out: &mut [u8; 32]) {
    #[cfg(target_arch = "aarch64")]
    // SAFETY: NEON is baseline on aarch64; the NEON path gathers `blocks`
    // through raw pointers, which the array type keeps to 256 blocks.
    unsafe {
        neon::lsb_mask_256(blocks, out)
    }

    #[cfg(not(target_arch = "aarch64"))]
    scalar::lsb_mask(blocks, out);
}

pub(crate) mod scalar {
    use super::{AesBlock, Packed};

    /// Scalar indicator pack: `D` must be `8 * B`, checked at compile time.
    ///
    /// Used by the non-aarch64 dispatchers and as the test oracle; on
    /// aarch64 the lib always reaches NEON, so outside `cfg(test)` it is
    /// unreferenced there. Compiling it exactly where it is used keeps it
    /// from being dead code without an `allow`. (`lsb_mask` below stays
    /// always compiled: `hash.rs` calls it directly for non-256 inputs.)
    #[cfg(any(not(target_arch = "aarch64"), test))]
    #[allow(clippy::let_unit_value)]
    pub(crate) fn gt_mask_xor<const D: usize, const B: usize>(
        table: &[u8; D],
        x: u8,
        out: &mut [u8; B],
    ) {
        let () = Packed::<D, B>::OK;
        for (slot, chunk) in out.iter_mut().zip(table.chunks_exact(8)) {
            let mut byte = 0u8;
            for (bit, &p) in chunk.iter().enumerate() {
                byte |= u8::from(p > x) << bit;
            }
            *slot ^= byte;
        }
    }

    /// Scalar LSB pack: `D` must be `8 * B`, checked at compile time.
    #[allow(clippy::let_unit_value)]
    pub(crate) fn lsb_mask<const D: usize, const B: usize>(
        blocks: &[AesBlock; D],
        out: &mut [u8; B],
    ) {
        let () = Packed::<D, B>::OK;
        for (slot, chunk) in out.iter_mut().zip(blocks.chunks_exact(8)) {
            let mut byte = 0u8;
            for (bit, block) in chunk.iter().enumerate() {
                byte |= (block[0] & 1u8) << bit;
            }
            *slot = byte;
        }
    }
}

#[cfg(target_arch = "aarch64")]
mod neon {
    use super::AesBlock;
    use core::arch::aarch64::*;

    /// Lane k holds `1 << (k % 8)`: the movemask bit-position vector.
    const BITPOS: [u8; 16] = [1, 2, 4, 8, 16, 32, 64, 128, 1, 2, 4, 8, 16, 32, 64, 128];

    /// Pack a 16-lane 0x00/0xFF compare mask into 2 bytes, LSB-first. Each
    /// lane contributes a distinct power of two within its 8-lane half, so
    /// the horizontal add is exactly the movemask byte.
    #[inline(always)]
    unsafe fn pack_addv(m: uint8x16_t) -> (u8, u8) {
        let mb = vandq_u8(m, vld1q_u8(BITPOS.as_ptr()));
        (vaddv_u8(vget_low_u8(mb)), vaddv_u8(vget_high_u8(mb)))
    }

    #[target_feature(enable = "neon")]
    pub(super) unsafe fn gt_mask_xor_256(table: &[u8; 256], x: u8, out: &mut [u8; 32]) {
        let xv = vdupq_n_u8(x);
        for i in 0..16 {
            let t = vld1q_u8(table.as_ptr().add(i * 16));
            let (lo, hi) = pack_addv(vcgtq_u8(t, xv));
            out[2 * i] ^= lo;
            out[2 * i + 1] ^= hi;
        }
    }

    #[target_feature(enable = "neon")]
    pub(super) unsafe fn gt_mask_xor_64(table: &[u8; 64], x: u8, out: &mut [u8; 8]) {
        let xv = vdupq_n_u8(x);
        for i in 0..4 {
            let t = vld1q_u8(table.as_ptr().add(i * 16));
            let (lo, hi) = pack_addv(vcgtq_u8(t, xv));
            out[2 * i] ^= lo;
            out[2 * i + 1] ^= hi;
        }
    }

    /// `IDX[g]` places the 4 block-start bytes of 64-byte group `g`
    /// (offsets 0, 16, 32, 48) at lanes 4g..4g+3; all other lanes are 0xFF
    /// (out-of-range, yielding 0 in `tbl`).
    const TBL_IDX: [[u8; 16]; 4] = {
        let mut idx = [[0xFFu8; 16]; 4];
        let mut g = 0;
        while g < 4 {
            let mut k = 0;
            while k < 4 {
                idx[g][4 * g + k] = (16 * k) as u8;
                k += 1;
            }
            g += 1;
        }
        idx
    };

    /// Gather byte 0 of 16 consecutive 16-byte blocks (256 contiguous
    /// bytes) into one vector via constant-index 4-register table lookups.
    #[inline(always)]
    unsafe fn gather_block0_x16(base: *const u8) -> uint8x16_t {
        let r0 = vqtbl4q_u8(vld1q_u8_x4(base), vld1q_u8(TBL_IDX[0].as_ptr()));
        let r1 = vqtbl4q_u8(vld1q_u8_x4(base.add(64)), vld1q_u8(TBL_IDX[1].as_ptr()));
        let r2 = vqtbl4q_u8(vld1q_u8_x4(base.add(128)), vld1q_u8(TBL_IDX[2].as_ptr()));
        let r3 = vqtbl4q_u8(vld1q_u8_x4(base.add(192)), vld1q_u8(TBL_IDX[3].as_ptr()));
        vorrq_u8(vorrq_u8(r0, r1), vorrq_u8(r2, r3))
    }

    #[target_feature(enable = "neon")]
    pub(super) unsafe fn lsb_mask_256(blocks: &[AesBlock; 256], out: &mut [u8; 32]) {
        let p = blocks.as_ptr() as *const u8;
        let one = vdupq_n_u8(1);
        for i in 0..16 {
            let m = vtstq_u8(gather_block0_x16(p.add(i * 256)), one);
            let (lo, hi) = pack_addv(m);
            out[2 * i] = lo;
            out[2 * i + 1] = hi;
        }
    }
}

#[cfg(target_arch = "x86_64")]
mod avx2 {
    use core::arch::x86_64::*;

    /// AVX2 has only *signed* byte compares; XOR-ing 0x80 into both sides
    /// maps unsigned `>` onto signed `>`.
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn gt_mask_xor_256(table: &[u8; 256], x: u8, out: &mut [u8; 32]) {
        let bias = _mm256_set1_epi8(i8::MIN);
        let xv = _mm256_xor_si256(_mm256_set1_epi8(x as i8), bias);
        for i in 0..8 {
            let t = _mm256_loadu_si256(table.as_ptr().add(i * 32) as *const __m256i);
            let gt = _mm256_cmpgt_epi8(_mm256_xor_si256(t, bias), xv);
            let mask = _mm256_movemask_epi8(gt) as u32;
            let m = mask.to_le_bytes();
            out[4 * i] ^= m[0];
            out[4 * i + 1] ^= m[1];
            out[4 * i + 2] ^= m[2];
            out[4 * i + 3] ^= m[3];
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{Rng, SeedableRng};
    use rand_chacha::ChaCha20Rng;

    fn rand_table(rng: &mut ChaCha20Rng) -> [u8; 256] {
        let mut t = [0u8; 256];
        rng.fill(&mut t[..]);
        t
    }

    #[test]
    fn gt_mask_dispatched_matches_scalar() {
        let mut rng = ChaCha20Rng::seed_from_u64(7);
        for _ in 0..5_000 {
            let table = rand_table(&mut rng);
            let x: u8 = rng.gen();
            // Random starting contents so the XOR semantics are exercised.
            let mut a = [0u8; 32];
            rng.fill(&mut a[..]);
            let mut b = a;

            gt_mask_xor_256(&table, x, &mut a);
            scalar::gt_mask_xor(&table, x, &mut b);
            assert_eq!(a, b);
        }
    }

    #[test]
    fn gt_mask_64_dispatched_matches_scalar() {
        let mut rng = ChaCha20Rng::seed_from_u64(13);
        for _ in 0..5_000 {
            let mut table = [0u8; 64];
            rng.fill(&mut table[..]);
            // Block values and table entries are < 64 in real use, but the
            // kernels must agree on all byte values.
            let x: u8 = rng.gen();
            let mut a = [0u8; 8];
            rng.fill(&mut a[..]);
            let mut b = a;

            gt_mask_xor_64(&table, x, &mut a);
            scalar::gt_mask_xor(&table, x, &mut b);
            assert_eq!(a, b);
        }
    }

    #[test]
    fn lsb_mask_dispatched_matches_scalar() {
        let mut rng = ChaCha20Rng::seed_from_u64(11);
        for _ in 0..5_000 {
            let mut blocks = [AesBlock::default(); 256];
            for block in blocks.iter_mut() {
                rng.fill(block.as_mut_slice());
            }
            let mut a = [0u8; 32];
            let mut b = [0u8; 32];

            lsb_mask_256(&blocks, &mut a);
            scalar::lsb_mask(&blocks, &mut b);
            assert_eq!(a, b);
        }
    }
}