use std::collections::HashMap;
use poulpy_hal::{
api::{ModuleLogN, ScratchAvailable},
layouts::{Backend, GaloisElement, Module, Scratch},
};
use crate::{
GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, GLWETrace, ScratchTakeCore,
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GetGaloisElement},
};
pub trait GLWEPacking<BE: Backend> {
fn glwe_pack_galois_elements(&self) -> Vec<i64>;
fn glwe_pack_tmp_bytes<R, K>(&self, res: &R, key: &K) -> usize
where
R: GLWEInfos,
K: GGLWEInfos;
fn glwe_pack<R, A, K, H>(
&self,
res: &mut R,
a: HashMap<usize, &mut A>,
log_gap_out: usize,
keys: &H,
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut + GLWEInfos,
A: GLWEToMut + GLWEInfos,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
H: GLWEAutomorphismKeyHelper<K, BE>;
}
impl<BE: Backend> GLWEPacking<BE> for Module<BE>
where
Self: GLWEAutomorphism<BE>
+ GaloisElement
+ ModuleLogN
+ GLWERotate<BE>
+ GLWESub
+ GLWEShift<BE>
+ GLWEAdd
+ GLWENormalize<BE>
+ GLWECopy
+ GLWETrace<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn glwe_pack_galois_elements(&self) -> Vec<i64> {
self.glwe_trace_galois_elements()
}
fn glwe_pack_tmp_bytes<R, K>(&self, res: &R, key: &K) -> usize
where
R: GLWEInfos,
K: GGLWEInfos,
{
assert_eq!(self.n() as u32, res.n());
assert_eq!(self.n() as u32, key.n());
let lvl_0: usize = GLWE::bytes_of_from_infos(res);
let lvl_1: usize = self
.glwe_rotate_tmp_bytes()
.max(self.glwe_rsh_tmp_byte())
.max(self.glwe_normalize_tmp_bytes())
.max(self.glwe_automorphism_tmp_bytes(res, res, key));
(lvl_0 + lvl_1).max(self.glwe_trace_tmp_bytes(res, res, key))
}
fn glwe_pack<R, A, K, H>(
&self,
res: &mut R,
mut a: HashMap<usize, &mut A>,
log_gap_out: usize,
keys: &H,
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut + GLWEInfos,
A: GLWEToMut + GLWEInfos,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
H: GLWEAutomorphismKeyHelper<K, BE>,
{
assert!(*a.keys().max().unwrap() < self.n());
let key_infos = keys.automorphism_key_infos();
assert!(
scratch.available() >= self.glwe_pack_tmp_bytes(res, &key_infos),
"scratch.available(): {} < GLWEPacking::glwe_pack_tmp_bytes: {}",
scratch.available(),
self.glwe_pack_tmp_bytes(res, &key_infos)
);
let log_n: usize = self.log_n();
for i in 0..(log_n - log_gap_out) {
let t: usize = (1 << log_n).min(1 << (log_n - 1 - i));
let key: &K = if i == 0 {
keys.get_automorphism_key(-1).unwrap()
} else {
keys.get_automorphism_key(self.galois_element(1 << (i - 1))).unwrap()
};
for j in 0..t {
let mut lo: Option<&mut A> = a.remove(&j);
let mut hi: Option<&mut A> = a.remove(&(j + t));
pack_internal(self, &mut lo, &mut hi, i, key, scratch);
if let Some(lo) = lo {
a.insert(j, lo);
} else if let Some(hi) = hi {
a.insert(j, hi);
}
}
}
self.glwe_trace(res, log_n - log_gap_out, *a.get(&0).unwrap(), keys, scratch);
}
}
#[allow(clippy::too_many_arguments)]
fn pack_internal<M, A, B, K, BE: Backend>(
module: &M,
a: &mut Option<&mut A>,
b: &mut Option<&mut B>,
i: usize,
auto_key: &K,
scratch: &mut Scratch<BE>,
) where
M: GLWEAutomorphism<BE> + GLWERotate<BE> + GLWESub + GLWEShift<BE> + GLWEAdd + GLWENormalize<BE>,
A: GLWEToMut + GLWEInfos,
B: GLWEToMut + GLWEInfos,
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
if let Some(a) = a.as_deref_mut() {
let t: i64 = 1 << (a.n().log2() - i - 1);
if let Some(b) = b.as_deref_mut() {
let (mut tmp_b, scratch_1) = scratch.take_glwe(a);
module.glwe_rotate_inplace(-t, a, scratch_1);
module.glwe_sub(&mut tmp_b, a, b);
module.glwe_rsh(1, &mut tmp_b, scratch_1);
module.glwe_add_inplace(a, b);
module.glwe_rsh(1, a, scratch_1);
module.glwe_normalize_inplace(&mut tmp_b, scratch_1);
module.glwe_automorphism_inplace(&mut tmp_b, auto_key, scratch_1);
module.glwe_sub_inplace(a, &tmp_b);
module.glwe_normalize_inplace(a, scratch_1);
module.glwe_rotate_inplace(t, a, scratch_1);
} else {
module.glwe_rsh(1, a, scratch);
module.glwe_automorphism_add_inplace(a, auto_key, scratch);
}
} else if let Some(b) = b.as_deref_mut() {
let t: i64 = 1 << (b.n().log2() - i - 1);
let (mut tmp_b, scratch_1) = scratch.take_glwe(b);
module.glwe_rotate(t, &mut tmp_b, b);
module.glwe_rsh(1, &mut tmp_b, scratch_1);
module.glwe_automorphism_sub_negate(b, &tmp_b, auto_key, scratch_1);
}
}