use crate::ciphertext::*;
use crate::scheme::bit2::{OreAes128, MAX_BLOCKS};
use crate::PlainText;
use crate::{OreCipher, OreError};
use orderable_bytes::{FixedOrderableBytes, OrderableBytes};
use rand::{Rng, SeedableRng};
pub trait OreEncrypt<T> {
type LeftOutput: OreOutput;
type FullOutput: OreOutput;
fn encrypt_left(&self, cipher: &T) -> Result<Self::LeftOutput, OreError>;
fn encrypt(&self, input: &T) -> Result<Self::FullOutput, OreError>;
}
const BOOL_LEN: usize = <bool as FixedOrderableBytes>::ENCODED_LEN;
const U8_LEN: usize = <u8 as FixedOrderableBytes>::ENCODED_LEN;
const I8_LEN: usize = <i8 as FixedOrderableBytes>::ENCODED_LEN;
const U16_LEN: usize = <u16 as FixedOrderableBytes>::ENCODED_LEN;
const I16_LEN: usize = <i16 as FixedOrderableBytes>::ENCODED_LEN;
const U32_LEN: usize = <u32 as FixedOrderableBytes>::ENCODED_LEN;
const I32_LEN: usize = <i32 as FixedOrderableBytes>::ENCODED_LEN;
const U64_LEN: usize = <u64 as FixedOrderableBytes>::ENCODED_LEN;
const I64_LEN: usize = <i64 as FixedOrderableBytes>::ENCODED_LEN;
const CHAR_LEN: usize = <char as FixedOrderableBytes>::ENCODED_LEN;
const F32_LEN: usize = <f32 as FixedOrderableBytes>::ENCODED_LEN;
const F64_LEN: usize = <f64 as FixedOrderableBytes>::ENCODED_LEN;
macro_rules! impl_ore_encrypt_via_orderable_bytes {
($type:ty, $len_const:ident, $tests:ident, $lo:expr, $hi:expr) => {
const _: () = assert!($len_const <= MAX_BLOCKS);
impl<R: Rng + SeedableRng> OreEncrypt<OreAes128<R>> for $type {
type LeftOutput = Left<OreAes128<R>, $len_const>;
type FullOutput = CipherText<OreAes128<R>, $len_const>;
fn encrypt_left(&self, cipher: &OreAes128<R>) -> Result<Self::LeftOutput, OreError> {
cipher.encrypt_left(&self.to_orderable_bytes())
}
fn encrypt(&self, cipher: &OreAes128<R>) -> Result<Self::FullOutput, OreError> {
cipher.encrypt(&self.to_orderable_bytes())
}
}
#[cfg(test)]
mod $tests {
use super::*;
use crate::scheme::bit2::OreAes128ChaCha20;
use std::cmp::Ordering;
#[test]
fn round_trip_order_and_equality() {
let ore: OreAes128ChaCha20 =
OreAes128ChaCha20::init(&[0x11; 16], &[0x22; 16]).unwrap();
let (lo, hi): ($type, $type) = ($lo, $hi);
assert!(lo < hi);
let lo_ct = lo.encrypt(&ore).unwrap();
let hi_ct = hi.encrypt(&ore).unwrap();
let lo_left = lo.encrypt_left(&ore).unwrap();
let lo_ct2 = lo.encrypt(&ore).unwrap();
let lo_bytes = lo_ct.to_bytes();
let hi_bytes = hi_ct.to_bytes();
assert_eq!(
CipherText::<OreAes128ChaCha20, $len_const>::from_slice(&lo_bytes)
.unwrap()
.to_bytes(),
lo_bytes
);
assert_eq!(
Left::<OreAes128ChaCha20, $len_const>::from_slice(&lo_left.to_bytes())
.unwrap()
.to_bytes(),
lo_left.to_bytes()
);
let cmp = OreAes128ChaCha20::compare_raw_slices;
assert_eq!(cmp(&lo_bytes, &hi_bytes), Some(Ordering::Less));
assert_eq!(cmp(&hi_bytes, &lo_bytes), Some(Ordering::Greater));
assert_ne!(lo_bytes, lo_ct2.to_bytes());
assert_eq!(cmp(&lo_bytes, &lo_ct2.to_bytes()), Some(Ordering::Equal));
assert_eq!(lo_ct.cmp(&hi_ct), Ordering::Less);
}
}
};
}
impl_ore_encrypt_via_orderable_bytes!(bool, BOOL_LEN, bool_tests, false, true);
impl_ore_encrypt_via_orderable_bytes!(u8, U8_LEN, u8_tests, 0u8, u8::MAX);
impl_ore_encrypt_via_orderable_bytes!(i8, I8_LEN, i8_tests, i8::MIN, i8::MAX);
impl_ore_encrypt_via_orderable_bytes!(u16, U16_LEN, u16_tests, 0u16, u16::MAX);
impl_ore_encrypt_via_orderable_bytes!(i16, I16_LEN, i16_tests, i16::MIN, i16::MAX);
impl_ore_encrypt_via_orderable_bytes!(u32, U32_LEN, u32_tests, 0u32, u32::MAX);
impl_ore_encrypt_via_orderable_bytes!(i32, I32_LEN, i32_tests, i32::MIN, i32::MAX);
impl_ore_encrypt_via_orderable_bytes!(u64, U64_LEN, u64_tests, 0u64, u64::MAX);
impl_ore_encrypt_via_orderable_bytes!(i64, I64_LEN, i64_tests, i64::MIN, i64::MAX);
impl_ore_encrypt_via_orderable_bytes!(char, CHAR_LEN, char_tests, '\0', char::MAX);
impl_ore_encrypt_via_orderable_bytes!(f32, F32_LEN, f32_tests, f32::MIN, f32::MAX);
impl_ore_encrypt_via_orderable_bytes!(f64, F64_LEN, f64_tests, f64::MIN, f64::MAX);
impl<R: Rng + SeedableRng, const N: usize> OreEncrypt<OreAes128<R>> for PlainText<N> {
type LeftOutput = Left<OreAes128<R>, N>;
type FullOutput = CipherText<OreAes128<R>, N>;
fn encrypt_left(&self, cipher: &OreAes128<R>) -> Result<Self::LeftOutput, OreError> {
cipher.encrypt_left(self)
}
fn encrypt(&self, cipher: &OreAes128<R>) -> Result<Self::FullOutput, OreError> {
cipher.encrypt(self)
}
}