use poulpy_hal::{
api::{ModuleLogN, ScratchAvailable},
layouts::{Backend, GaloisElement, Module, Scratch},
};
use crate::{
GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore,
glwe_trace::GLWETrace,
layouts::{
GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement,
LWEInfos,
},
};
pub struct GLWEPacker {
accumulators: Vec<Accumulator>,
log_batch: usize,
counter: usize,
}
struct Accumulator {
data: GLWE<Vec<u8>>,
value: bool, control: bool, }
impl Accumulator {
pub fn alloc<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self {
data: GLWE::alloc_from_infos(infos),
value: false,
control: false,
}
}
}
impl GLWEPacker {
pub fn alloc<A>(infos: &A, log_batch: usize) -> Self
where
A: GLWEInfos,
{
let mut accumulators: Vec<Accumulator> = Vec::<Accumulator>::new();
let log_n: usize = infos.n().log2();
(0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(infos)));
GLWEPacker {
accumulators,
log_batch,
counter: 0,
}
}
fn reset(&mut self) {
for i in 0..self.accumulators.len() {
self.accumulators[i].value = false;
self.accumulators[i].control = false;
}
self.counter = 0;
}
pub fn tmp_bytes<R, K, M, BE: Backend>(module: &M, res_infos: &R, key_infos: &K) -> usize
where
R: GLWEInfos,
K: GGLWEInfos,
M: GLWEPackerOps<BE>,
{
GLWE::bytes_of_from_infos(res_infos)
+ module
.glwe_rsh_tmp_byte()
.max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos))
}
pub fn galois_elements<M, BE: Backend>(module: &M) -> Vec<i64>
where
M: GLWETrace<BE>,
{
module.glwe_trace_galois_elements()
}
pub fn add<A, K, H, M, BE: Backend>(&mut self, module: &M, a: Option<&A>, auto_keys: &H, scratch: &mut Scratch<BE>)
where
A: GLWEToRef + GLWEInfos,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
H: GLWEAutomorphismKeyHelper<K, BE>,
M: GLWEPackerOps<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
assert!(
(self.counter as u32) < self.accumulators[0].data.n(),
"Packing limit of {} reached",
self.accumulators[0].data.n().0 as usize >> self.log_batch
);
assert!(
scratch.available() >= Self::tmp_bytes(module, &self.accumulators[0].data, &auto_keys.automorphism_key_infos()),
"scratch.available(): {} < GLWEPacker::tmp_bytes: {}",
scratch.available(),
Self::tmp_bytes(module, &self.accumulators[0].data, &auto_keys.automorphism_key_infos())
);
module.packer_add(self, a, self.log_batch, auto_keys, scratch);
self.counter += 1 << self.log_batch;
}
pub fn flush<R, M, BE: Backend>(&mut self, module: &M, res: &mut R, scratch: &mut Scratch<BE>)
where
R: GLWEToMut + GLWEInfos,
M: GLWEPackerOps<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
assert!(self.counter as u32 == self.accumulators[0].data.n());
let out: &GLWE<Vec<u8>> = &self.accumulators[module.log_n() - self.log_batch - 1].data;
if out.base2k() == res.base2k() {
module.glwe_copy(res, out)
} else {
module.glwe_normalize(res, out, scratch);
}
self.reset();
}
}
impl<BE: Backend> GLWEPackerOps<BE> for Module<BE> where
Self: Sized
+ ModuleLogN
+ GLWEAutomorphism<BE>
+ GaloisElement
+ GLWERotate<BE>
+ GLWESub
+ GLWEShift<BE>
+ GLWEAdd
+ GLWENormalize<BE>
+ GLWECopy
{
}
pub trait GLWEPackerOps<BE: Backend>
where
Self: Sized
+ ModuleLogN
+ GLWEAutomorphism<BE>
+ GaloisElement
+ GLWERotate<BE>
+ GLWESub
+ GLWEShift<BE>
+ GLWEAdd
+ GLWENormalize<BE>
+ GLWECopy,
{
fn packer_add<A, K, H>(&self, packer: &mut GLWEPacker, a: Option<&A>, i: usize, auto_keys: &H, scratch: &mut Scratch<BE>)
where
A: GLWEToRef + GLWEInfos,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
H: GLWEAutomorphismKeyHelper<K, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
pack_core(self, a, &mut packer.accumulators, i, auto_keys, scratch)
}
}
fn pack_core<A, K, H, M, BE: Backend>(
module: &M,
a: Option<&A>,
accumulators: &mut [Accumulator],
i: usize,
auto_keys: &H,
scratch: &mut Scratch<BE>,
) where
A: GLWEToRef + GLWEInfos,
M: ModuleLogN
+ GLWEAutomorphism<BE>
+ GaloisElement
+ GLWERotate<BE>
+ GLWESub
+ GLWEShift<BE>
+ GLWEAdd
+ GLWENormalize<BE>
+ GLWECopy,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
H: GLWEAutomorphismKeyHelper<K, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let log_n: usize = module.log_n();
if i == log_n {
return;
}
let (acc_prev, acc_next) = accumulators.split_at_mut(1);
if !acc_prev[0].control {
let acc_mut_ref: &mut Accumulator = &mut acc_prev[0];
if let Some(a_ref) = a {
if a_ref.base2k() == acc_mut_ref.data.base2k() {
module.glwe_copy(&mut acc_mut_ref.data, a_ref);
} else {
module.glwe_normalize(&mut acc_mut_ref.data, a_ref, scratch);
}
acc_mut_ref.value = true
} else {
acc_mut_ref.value = false
}
acc_mut_ref.control = true; } else {
combine(module, &mut acc_prev[0], a, i, auto_keys, scratch);
acc_prev[0].control = false;
if acc_prev[0].value {
pack_core(module, Some(&acc_prev[0].data), acc_next, i + 1, auto_keys, scratch);
} else {
pack_core(module, None::<&GLWE<Vec<u8>>>, acc_next, i + 1, auto_keys, scratch);
}
}
}
fn combine<B, K, H, M, BE: Backend>(
module: &M,
acc: &mut Accumulator,
b: Option<&B>,
i: usize,
auto_keys: &H,
scratch: &mut Scratch<BE>,
) where
B: GLWEToRef + GLWEInfos,
M: ModuleLogN
+ GLWEAutomorphism<BE>
+ GaloisElement
+ GLWERotate<BE>
+ GLWESub
+ GLWEShift<BE>
+ GLWEAdd
+ GLWENormalize<BE>
+ GLWECopy,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
H: GLWEAutomorphismKeyHelper<K, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let log_n: usize = acc.data.n().log2();
let a: &mut GLWE<Vec<u8>> = &mut acc.data;
let gal_el: i64 = if i == 0 { -1 } else { module.galois_element(1 << (i - 1)) };
let t: i64 = 1 << (log_n - i - 1);
if acc.value {
if let Some(b) = b {
let (mut tmp, scratch_1) = scratch.take_glwe(a);
module.glwe_rotate_inplace(-t, a, scratch_1);
module.glwe_sub(&mut tmp, a, b);
module.glwe_rsh(1, &mut tmp, scratch_1);
module.glwe_add_inplace(a, b);
module.glwe_rsh(1, a, scratch_1);
module.glwe_normalize_inplace(&mut tmp, scratch_1);
if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
module.glwe_automorphism_inplace(&mut tmp, auto_key, scratch_1);
} else {
panic!("auto_key[{gal_el}] not found");
}
module.glwe_sub_inplace(a, &tmp);
module.glwe_normalize_inplace(a, scratch_1);
module.glwe_rotate_inplace(t, a, scratch_1);
} else {
module.glwe_rsh(1, a, scratch);
if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
module.glwe_automorphism_add_inplace(a, auto_key, scratch);
} else {
panic!("auto_key[{gal_el}] not found");
}
}
} else if let Some(b) = b {
let (mut tmp_b, scratch_1) = scratch.take_glwe(a);
module.glwe_rotate(t, &mut tmp_b, b);
module.glwe_rsh(1, &mut tmp_b, scratch_1);
if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
module.glwe_automorphism_sub_negate(a, &tmp_b, auto_key, scratch_1);
} else {
panic!("auto_key[{gal_el}] not found");
}
acc.value = true;
}
}