ore-rs 0.11.0

Order-revealing encryption library used by the CipherStash searchable encryption platform
Documentation
//! Block-width abstraction for BlockORE schemes.
//!
//! A [`BlockWidth`] fixes the number of plaintext bits consumed per ORE
//! block and everything that follows from it: the block domain size, the
//! right-ciphertext bitvector type, the PRP over the domain, and the
//! random-oracle key buffer. Widths are sealed: the comparator and wire
//! format are width-specific, so downstream crates must not add widths.
//!
//! Stable Rust cannot express `[u8; 1 << BITS]` for a generic width
//! (`generic_const_exprs`), so the width carries its domain-sized buffers
//! as associated types instead.

use crate::ciphertext::CipherTextBlock;
use crate::primitives::{AesBlock, Hash, Prp, Symbol};
use crate::scheme::bit2::block_types::RightBlock32;
use crate::scheme::bit2_w6::block_types::RightBlock8;
use zeroize::{Zeroize, ZeroizeOnDrop};

mod sealed {
    pub trait Sealed {}
    impl Sealed for super::Bit8 {}
    impl Sealed for super::Bit6 {}
    impl Sealed for [super::AesBlock; 256] {}
    impl Sealed for [super::AesBlock; 64] {}
}

/// A domain-sized buffer of AES blocks used for per-block random-oracle
/// keys. Implemented for `[AesBlock; DOMAIN]` arrays (which have no
/// `Default` for large N on stable Rust).
pub trait AesBlockBuf: sealed::Sealed {
    /// A zeroed buffer.
    fn zeroed() -> Self;
    /// View as a mutable slice for batched PRF passes. (Concrete `[T; N]`
    /// callers resolve to the inherent array method; this exists for
    /// width-generic code, which arrives with the Bit6 scheme.)
    #[allow(dead_code)]
    fn as_mut_slice(&mut self) -> &mut [AesBlock];
    /// Overwrite `self` with `other` (a domain-sized memcpy).
    fn copy_from(&mut self, other: &Self);
}

macro_rules! impl_aes_block_buf {
    ($n:literal) => {
        impl AesBlockBuf for [AesBlock; $n] {
            fn zeroed() -> Self {
                [AesBlock::default(); $n]
            }
            fn as_mut_slice(&mut self) -> &mut [AesBlock] {
                self
            }
            fn copy_from(&mut self, other: &Self) {
                self.clone_from_slice(other);
            }
        }
    };
}

impl_aes_block_buf!(256);
impl_aes_block_buf!(64);

/// Oblivious byte read: returns `block[idx]` while touching **every** byte of
/// `block`, so the memory-access address is independent of `idx`.
///
/// The comparators read the right-ciphertext block at `bit / 8` where `bit` is
/// the secret permuted symbol (`a[l]`, the left ciphertext's permuted index at
/// the first differing block). A direct `block[bit / 8]` index makes the
/// touched address secret-dependent, which is a cache-line timing channel — and
/// because the block fits within a line, even an aligned direct index would
/// still leak at sub-line (4-byte) granularity to a MemJam-class attacker on
/// SMT-enabled Intel. Scanning the whole block removes the data-dependent
/// address entirely, closing both. The block is ≤ 32 bytes, so the scan is
/// cheap relative to the per-comparison AES hash.
///
/// See `docs/reviews/2026-06-14-ore-v2-crypto-review-brief.md` (A4, compare
/// side). The bit within the selected byte is then extracted with [`ct_bit`],
/// which avoids a shift by the secret amount.
#[inline]
pub(crate) fn ct_select_byte(block: &[u8], idx: usize) -> u8 {
    use subtle_ng::{ConditionallySelectable, ConstantTimeEq};
    let mut acc = 0u8;
    for (i, &b) in block.iter().enumerate() {
        // `i` is the public loop counter; `idx` is secret. Both index a block
        // or table of ≤ 256 bytes, so the u8 cast is lossless.
        acc.conditional_assign(&b, (i as u8).ct_eq(&(idx as u8)));
    }
    acc
}

/// Oblivious conditional copy: `dst = src` when `choice` is set, otherwise
/// `dst` is left as it was; every byte of both is touched either way.
///
/// The comparators use this to *latch* the first differing block while the
/// constant-time prefix scan is still running: at each block `n` the scan
/// knows whether `n` is the first difference, and copies that block's left
/// tag, permuted symbol and right bitvector under that choice. Nothing after
/// the scan is then loaded at an address derived from the position of the
/// first difference. A direct `right[l]` after the scan would be the one load
/// whose cache state depends on `l` (the scan never touches the right blocks),
/// which a timing attacker can read; `docs/reviews/` records the measurement.
#[inline]
pub(crate) fn ct_assign_bytes(dst: &mut [u8], src: &[u8], choice: subtle_ng::Choice) {
    use subtle_ng::ConditionallySelectable;
    for (d, s) in dst.iter_mut().zip(src) {
        d.conditional_assign(s, choice);
    }
}

/// Oblivious extraction of bit `pos` (`0..8`) of `byte` — used right after
/// [`ct_select_byte`] to read the target bit of the selected right-block byte.
///
/// `byte >> pos` would be a shift by a *secret* amount; that is constant-time
/// on x86_64/aarch64 (the targets ore.rs ships to) but not guaranteed so on
/// every architecture. Here every candidate shift is a compile-time constant
/// and the result is chosen with a constant-time select, so the timing is
/// data-independent on all targets — defence-in-depth matching the oblivious
/// byte read above.
#[inline]
pub(crate) fn ct_bit(byte: u8, pos: u8) -> u8 {
    use subtle_ng::{ConditionallySelectable, ConstantTimeEq};
    let mut out = 0u8;
    out.conditional_assign(&(byte & 1), pos.ct_eq(&0));
    out.conditional_assign(&((byte >> 1) & 1), pos.ct_eq(&1));
    out.conditional_assign(&((byte >> 2) & 1), pos.ct_eq(&2));
    out.conditional_assign(&((byte >> 3) & 1), pos.ct_eq(&3));
    out.conditional_assign(&((byte >> 4) & 1), pos.ct_eq(&4));
    out.conditional_assign(&((byte >> 5) & 1), pos.ct_eq(&5));
    out.conditional_assign(&((byte >> 6) & 1), pos.ct_eq(&6));
    out.conditional_assign(&((byte >> 7) & 1), pos.ct_eq(&7));
    out
}

/// The first differing block of a comparator's prefix scan: `a`'s permuted
/// symbol `xt` and PRF tag `f`, and `b`'s `R`-byte right bitvector.
///
/// The scan calls [`Self::latch`] for every block, under the choice "this is
/// the first difference", so nothing after the scan is loaded at an address
/// derived from that position (see [`ct_assign_bytes`]). The copies are
/// zeroized on drop, on every return path, so they do not outlive the
/// comparison on the stack.
#[derive(Zeroize, ZeroizeOnDrop)]
pub(crate) struct FirstDiff<const R: usize> {
    xt: u8,
    f: [u8; 16],
    right: [u8; R],
}

impl<const R: usize> FirstDiff<R> {
    pub(crate) fn new() -> Self {
        Self {
            xt: 0,
            f: [0; 16],
            right: [0; R],
        }
    }

    /// Copy one block's values in when `first` is set; every byte of the
    /// latch and the inputs is touched either way.
    #[inline]
    pub(crate) fn latch(&mut self, xt: u8, f: &[u8], right: &[u8], first: subtle_ng::Choice) {
        use subtle_ng::ConditionallySelectable;
        self.xt.conditional_assign(&xt, first);
        ct_assign_bytes(&mut self.f, f, first);
        ct_assign_bytes(&mut self.right, right, first);
    }

    /// The resolution step: bit `xt` of the right bitvector XOR the random
    /// oracle on `f`. `1` means `a > b`. The bit is read with
    /// [`ct_select_byte`] and [`ct_bit`], so its address does not depend on
    /// `xt` either.
    #[inline]
    pub(crate) fn resolve<H: Hash>(&self, hasher: &H) -> u8 {
        // `xt` is a parsed, in-domain symbol. Were it not, the oblivious
        // select below would read no byte and give 0, not go out of bounds.
        let byte = ct_select_byte(&self.right, usize::from(self.xt / 8));
        ct_bit(byte, self.xt % 8) ^ hasher.hash(&self.f)
    }
}

/// Per-block bitvector operations on a Right ciphertext block, one bit per
/// value in the block domain.
pub trait RightBitVec {
    /// The raw bitvector, `[u8; DOMAIN / 8]`.
    type Bits;
    /// The raw bitvector bytes, LSB-first within each byte (the same bit
    /// order as `RightBlock32::set_bit`), for bulk mask construction.
    fn as_mut_bits(&mut self) -> &mut Self::Bits;
}

impl RightBitVec for RightBlock32 {
    type Bits = [u8; 32];

    fn as_mut_bits(&mut self) -> &mut [u8; 32] {
        self.bytes_mut()
    }
}

/// The number of plaintext bits consumed per ORE block, and the types that
/// depend on it. See the module docs.
pub trait BlockWidth: sealed::Sealed + 'static {
    /// Bits of plaintext per block.
    const BITS: usize;
    /// Block domain size. Block values are `0..DOMAIN`. Derived from
    /// [`BITS`](Self::BITS) so the two cannot disagree.
    const DOMAIN: usize = 1 << Self::BITS;
    /// Right-ciphertext block: a `DOMAIN`-bit masked truth-table row.
    type RightBlock: CipherTextBlock + RightBitVec;
    /// A block value: a [`Symbol`] of the `DOMAIN`-element domain.
    type Symbol: Copy;
    /// PRP over the block domain. Its indicator mask is a right block's
    /// bitvector.
    type Prp: Prp<Self::Symbol, Mask = <Self::RightBlock as RightBitVec>::Bits>;
    /// Buffer holding `DOMAIN` random-oracle keys.
    type RoKeyBuf: AesBlockBuf;

    /// Hash the `DOMAIN` random-oracle keys in `ro` (in place, trashing
    /// them) into `out`, one bit per key: [`Hash::hash_all_into`] at this
    /// width's sizes, which the types fix.
    fn hash_into<H: Hash>(
        hasher: &H,
        ro: &mut Self::RoKeyBuf,
        out: &mut <Self::RightBlock as RightBitVec>::Bits,
    );
}

/// The 8-bit block width used by the legacy [`crate::scheme::bit2`] scheme:
/// one plaintext byte per block, domain 256.
#[derive(Debug)]
pub struct Bit8;

impl BlockWidth for Bit8 {
    const BITS: usize = 8;
    type RightBlock = RightBlock32;
    type Symbol = Symbol<256>;
    type Prp = crate::primitives::prp::KnuthShufflePRP<u8, 256>;
    type RoKeyBuf = [AesBlock; 256];

    fn hash_into<H: Hash>(hasher: &H, ro: &mut [AesBlock; 256], out: &mut [u8; 32]) {
        hasher.hash_all_into(ro, out);
    }
}

/// The 6-bit block width used by [`crate::scheme::bit2_w6`]: six plaintext
/// bits per block, domain 64. Right blocks are 8 bytes (vs 32) and each
/// block costs 64 RO evaluations (vs 256).
#[derive(Debug)]
pub struct Bit6;

impl BlockWidth for Bit6 {
    const BITS: usize = 6;
    type RightBlock = RightBlock8;
    // Fixed-draw Fisher–Yates, not the rejection-sampled Knuth shuffle:
    // Bit6's wire format was not yet frozen when it adopted this
    // constant-time, ~9×-faster PRP construction. See `LemireFyPrp`.
    type Prp = crate::primitives::prp::LemireFyPrp<64>;
    type Symbol = Symbol<64>;
    type RoKeyBuf = [AesBlock; 64];

    fn hash_into<H: Hash>(hasher: &H, ro: &mut [AesBlock; 64], out: &mut [u8; 8]) {
        hasher.hash_all_into(ro, out);
    }
}

#[cfg(kani)]
mod kani_proofs {
    use super::*;

    /// `ct_select_byte(block, idx) == block[idx]` for every block of length
    /// 1..=256 (the stated domain) and every in-range `idx`.
    // Every length 1..=256 takes about nine minutes; the shipped lengths are
    // proved by the harness below, so this runs only with `kani-full`.
    #[cfg(feature = "kani-full")]
    #[kani::proof]
    #[kani::unwind(257)]
    fn ct_select_byte_equals_index_up_to_256() {
        let buf: [u8; 256] = kani::any();
        let len: usize = kani::any();
        kani::assume(len >= 1 && len <= 256);
        let idx: usize = kani::any();
        kani::assume(idx < len);
        let block = &buf[..len];
        assert_eq!(ct_select_byte(block, idx), block[idx]);
    }

    /// `ct_select_byte(block, idx) == block[idx]` for the two right-block
    /// sizes the comparators use (8 and 32 bytes), all contents, all
    /// in-range `idx`.
    #[kani::proof]
    #[kani::unwind(33)]
    fn ct_select_byte_equals_index_right_blocks() {
        let b8: [u8; 8] = kani::any();
        let i8: usize = kani::any();
        kani::assume(i8 < 8);
        assert_eq!(ct_select_byte(&b8, i8), b8[i8]);

        let b32: [u8; 32] = kani::any();
        let i32: usize = kani::any();
        kani::assume(i32 < 32);
        assert_eq!(ct_select_byte(&b32, i32), b32[i32]);
    }

    /// `ct_bit(byte, pos) == (byte >> pos) & 1` for every byte and every
    /// `pos` in `0..8`, and is 0 for every `pos >= 8` (exhaustive).
    #[kani::proof]
    fn ct_bit_equals_shift() {
        let byte: u8 = kani::any();
        let pos: u8 = kani::any();
        if pos < 8 {
            assert_eq!(ct_bit(byte, pos), (byte >> pos) & 1);
        } else {
            assert_eq!(ct_bit(byte, pos), 0);
        }
    }
}