#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![cfg_attr(
not(test),
deny(
clippy::panic,
clippy::unwrap_used,
clippy::expect_used,
clippy::unreachable,
clippy::todo,
clippy::unimplemented,
clippy::panic_in_result_fn
)
)]
mod block_ore;
mod ciphertext;
mod primitives;
pub mod scheme;
mod term;
#[cfg(test)]
pub(crate) mod testing;
pub use crate::block_ore::{BlockOre, BlockOreSpace, ORE_KEY};
pub(crate) use crate::ciphertext::*;
pub use crate::term::{Query, Term};
#[cfg(feature = "ct-bench")]
#[doc(hidden)]
pub mod ct_bench {
use crate::primitives::prp::{oblivious, LemireFyPrp};
use crate::primitives::{Prp, Symbol};
pub fn lemire_fy_prp_from_stream(stream: &[u8; 512]) -> u8 {
LemireFyPrp::<64>::from_stream(stream)
.permute(Symbol::from_low_bits(0))
.get()
}
fn from_stream_with(
stream: &[u8; 512],
build: fn(&[u8; 512], &mut [u8; 64], &mut [u8; 64]),
) -> u8 {
LemireFyPrp::<64>::from_stream_with(stream, build)
.permute(Symbol::from_low_bits(0))
.get()
}
pub fn lemire_fy_prp_from_stream_reference(stream: &[u8; 512]) -> u8 {
from_stream_with(stream, oblivious::reference::build)
}
pub fn lemire_fy_prp_from_stream_scalar_oblivious(stream: &[u8; 512]) -> u8 {
from_stream_with(stream, oblivious::scalar::build)
}
pub fn lemire_fy_prp_from_stream_swar_oblivious(stream: &[u8; 512]) -> u8 {
from_stream_with(stream, oblivious::swar::build)
}
#[repr(C, align(64))]
pub struct Line(pub [u8; 64]);
#[inline(always)]
fn draw(stream: &[u8; 512], i: usize) -> usize {
usize::from(oblivious::lemire_draw(stream, i))
}
#[inline(never)]
pub fn fy_swaps_only(stream: &[u8; 512], out: &mut Line) {
for (k, p) in out.0.iter_mut().enumerate() {
*p = k as u8;
}
for i in (1..64).rev() {
let j = draw(stream, i);
out.0.swap(i, j);
}
}
#[inline(never)]
pub fn fy_swaps_public(stream: &[u8; 512], out: &mut Line) {
for (k, p) in out.0.iter_mut().enumerate() {
*p = k as u8;
}
for i in (1..64).rev() {
core::hint::black_box(draw(stream, i));
out.0.swap(i, i - 1);
}
}
pub fn permutation_for(stream: &[u8; 512]) -> Line {
let mut l = Line([0u8; 64]);
let mut inverse = [0u8; 64];
oblivious::reference::build(stream, &mut l.0, &mut inverse);
l
}
#[inline(never)]
pub fn inverse_fill_only(perm: &Line, out: &mut Line) {
for (index, val) in perm.0.iter().enumerate() {
out.0[*val as usize & 63] = index as u8;
}
}
#[inline(never)]
pub fn inverse_fill_public(perm: &Line, out: &mut Line) {
for (index, val) in perm.0.iter().enumerate() {
core::hint::black_box(*val);
out.0[index] = index as u8;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn isolation_halves_match_the_reference_builder() {
for seed in 0u8..16 {
let stream: [u8; 512] =
core::array::from_fn(|k| (k as u8).wrapping_mul(167) ^ seed.wrapping_mul(29));
let mut want_perm = [0u8; 64];
let mut want_inverse = [0u8; 64];
oblivious::reference::build(&stream, &mut want_perm, &mut want_inverse);
let mut swaps = Line([0u8; 64]);
fy_swaps_only(&stream, &mut swaps);
assert_eq!(swaps.0, want_perm);
let mut inverse = Line([0u8; 64]);
inverse_fill_only(&permutation_for(&stream), &mut inverse);
assert_eq!(inverse.0, want_inverse);
}
}
}
}
use primitives::PrpError;
use std::cmp::Ordering;
use thiserror::Error;
use vitaminc_ore::OrderPlaintext;
pub(crate) type PlainText<const N: usize> = [u8; N];
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum OreError {
#[error("Failed to initialize cipher")]
InitFailed,
#[error(transparent)]
PrpError(#[from] PrpError),
#[error("Randomness Error")]
Random(#[from] vitaminc_random::RandomError),
#[error("Variable-length plaintext on a fixed-width scheme")]
VariableLength,
#[error("Too many blocks")]
TooManyBlocks,
#[error("String longer than its padding")]
ExceedsPadding,
#[error("String longer than the cipher's maximum")]
TooLong,
#[error("String ends in NUL")]
TrailingNul,
#[error("Plaintext of another target than the space's")]
TargetMismatch,
}
mod sealed {
pub trait Sealed {}
}
pub trait OreScheme: sealed::Sealed {
fn encrypt_plaintext(&self, plaintext: &OrderPlaintext) -> Result<Term, OreError>;
fn encrypt_query_plaintext(&self, plaintext: &OrderPlaintext) -> Result<Query, OreError>;
fn compare_raw_slices(a: &[u8], b: &[u8]) -> Option<Ordering>;
fn compare_left_to_full(left: &[u8], full: &[u8]) -> Option<Ordering>;
}
pub(crate) trait OreCipher: Sized {
type LeftBlockType: CipherTextBlock;
type RightBlockType: CipherTextBlock;
const WIRE_HEADER: Option<WireHeader> = None;
const SYMBOL_DOMAIN: usize = 256;
fn encrypt_left<const N: usize>(&self, input: &PlainText<N>)
-> Result<Left<Self, N>, OreError>;
fn encrypt<const N: usize>(
&self,
input: &PlainText<N>,
) -> Result<CipherText<Self, N>, OreError>;
}
#[cfg(test)]
#[macro_use]
extern crate quickcheck;