mod encrypt;
mod fhe;
mod key;
mod plain;
mod sbox;
#[cfg(test)]
mod test;
pub use fhe::AesFheState;
pub use key::AesFheRoundKeys;
pub use plain::AesPlainState;
use crate::shortint::ciphertext::Degree;
use crate::shortint::{Ciphertext, ClientKey};
use crate::transciphering::ciphers::*;
#[derive(Clone, Copy)]
pub struct AesPlainKey([u8; 16]);
impl AesPlainKey {
pub fn expand(self) -> [bool; 128] {
let mut out = [false; 128];
unpack_bits_lsb_first(&self.0, &mut out);
out
}
pub fn encrypt(&self, client_key: &ClientKey) -> AesFheKey {
AesFheKey {
key: self.expand().map(|b| {
let mut c = client_key.encrypt(b as u64);
c.degree = Degree::new(1);
c
}),
}
}
pub(crate) fn to_csprng_key_u128(self) -> u128 {
u128::from_ne_bytes(self.0)
}
}
impl From<u128> for AesPlainKey {
fn from(value: u128) -> Self {
value.to_be_bytes().into()
}
}
impl From<[u8; 16]> for AesPlainKey {
fn from(value: [u8; 16]) -> Self {
Self(value)
}
}
impl From<[bool; 128]> for AesPlainKey {
fn from(value: [bool; 128]) -> Self {
let mut bits = [0u8; 16];
pack_bits_lsb_first(&value, &mut bits);
bits.into()
}
}
pub struct AesFheKey {
key: [Ciphertext; 128],
}
#[derive(Clone, Copy)]
pub struct AesIv(u128);
impl AesIv {
pub fn to_u128(self) -> u128 {
self.0
}
}
impl From<u128> for AesIv {
fn from(value: u128) -> Self {
Self(value)
}
}
impl From<[u8; 16]> for AesIv {
fn from(value: [u8; 16]) -> Self {
u128::from_be_bytes(value).into()
}
}
impl From<[bool; 128]> for AesIv {
fn from(value: [bool; 128]) -> Self {
let mut bits = [0u8; 16];
pack_bits_lsb_first(&value, &mut bits);
bits.into()
}
}