keyswitch tests

This commit is contained in:
Pro7ech
2025-10-20 15:32:52 +02:00
parent 0c894c19db
commit 252eda36fe
60 changed files with 918 additions and 945 deletions

View File

@@ -7,10 +7,8 @@ use crate::{
ScratchTakeCore, ScratchTakeCore,
automorphism::glwe_ct::GLWEAutomorphism, automorphism::glwe_ct::GLWEAutomorphism,
layouts::{ layouts::{
AutomorphismKey, AutomorphismKeyToMut, AutomorphismKeyToRef, GGLWEInfos, GLWE, GLWEInfos, AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE,
prepared::{ prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement, SetAutomorphismGaloisElement,
},
}, },
}; };
@@ -29,8 +27,8 @@ impl AutomorphismKey<Vec<u8>> {
impl<DataSelf: DataMut> AutomorphismKey<DataSelf> { impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn automorphism<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>) pub fn automorphism<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>)
where where
A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: AutomorphismKeyAutomorphism<BE>, M: AutomorphismKeyAutomorphism<BE>,
{ {
@@ -39,7 +37,7 @@ impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn automorphism_inplace<K, M, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>) pub fn automorphism_inplace<K, M, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>)
where where
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: AutomorphismKeyAutomorphism<BE>, M: AutomorphismKeyAutomorphism<BE>,
{ {
@@ -67,16 +65,11 @@ where
fn automorphism_key_automorphism<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>) fn automorphism_key_automorphism<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>)
where where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
{
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let a: &AutomorphismKey<&[u8]> = &a.to_ref();
let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref();
assert!( assert!(
res.dnum().as_u32() <= a.dnum().as_u32(), res.dnum().as_u32() <= a.dnum().as_u32(),
"res dnum: {} > a dnum: {}", "res dnum: {} > a dnum: {}",
@@ -97,6 +90,10 @@ where
let p: i64 = a.p(); let p: i64 = a.p();
let p_inv: i64 = self.galois_element_inv(p); let p_inv: i64 = self.galois_element_inv(p);
{
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let a: &GGLWE<&[u8]> = &a.to_ref();
for row in 0..res.dnum().as_usize() { for row in 0..res.dnum().as_usize() {
for col in 0..cols_out { for col in 0..cols_out {
let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col);
@@ -104,11 +101,11 @@ where
// Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a)
for i in 0..cols_out { for i in 0..cols_out {
self.vec_znx_automorphism(a.p(), res_tmp.data_mut(), i, &a_ct.data, i); self.vec_znx_automorphism(p, res_tmp.data_mut(), i, &a_ct.data, i);
} }
// Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a)
self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); self.glwe_keyswitch_inplace(&mut res_tmp, key, scratch);
// Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a)
(0..cols_out).for_each(|i| { (0..cols_out).for_each(|i| {
@@ -118,19 +115,15 @@ where
} }
} }
res.set_p((a.p() * key.p()) % (self.cyclotomic_order() as i64)); res.set_p((p * key.p()) % (self.cyclotomic_order() as i64));
} }
fn automorphism_key_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) fn automorphism_key_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement, R: GGLWEToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement + GGLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
{
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref();
assert_eq!( assert_eq!(
res.rank(), res.rank(),
key.rank(), key.rank(),
@@ -140,10 +133,11 @@ where
); );
let cols_out: usize = (key.rank_out() + 1).into(); let cols_out: usize = (key.rank_out() + 1).into();
let p: i64 = res.p(); let p: i64 = res.p();
let p_inv: i64 = self.galois_element_inv(p); let p_inv: i64 = self.galois_element_inv(p);
{
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
for row in 0..res.dnum().as_usize() { for row in 0..res.dnum().as_usize() {
for col in 0..cols_out { for col in 0..cols_out {
let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col);
@@ -154,7 +148,7 @@ where
} }
// Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a)
self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); self.glwe_keyswitch_inplace(&mut res_tmp, key, scratch);
// Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a)
for i in 0..cols_out { for i in 0..cols_out {

View File

@@ -7,8 +7,8 @@ use crate::{
GGSWExpandRows, ScratchTakeCore, GGSWExpandRows, ScratchTakeCore,
automorphism::glwe_ct::GLWEAutomorphism, automorphism::glwe_ct::GLWEAutomorphism,
layouts::{ layouts::{
GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetAutomorphismGaloisElement,
prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, TensorKeyPrepared, TensorKeyPreparedToRef}, prepared::{TensorKeyPrepared, TensorKeyPreparedToRef},
}, },
}; };
@@ -35,7 +35,7 @@ impl<D: DataMut> GGSW<D> {
pub fn automorphism<A, K, T, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>) pub fn automorphism<A, K, T, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
where where
A: GGSWToRef, A: GGSWToRef,
K: AutomorphismKeyPreparedToRef<BE>, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGSWAutomorphism<BE>, M: GGSWAutomorphism<BE>,
@@ -45,7 +45,7 @@ impl<D: DataMut> GGSW<D> {
pub fn automorphism_inplace<K, T, M, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>) pub fn automorphism_inplace<K, T, M, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
where where
K: AutomorphismKeyPreparedToRef<BE>, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGSWAutomorphism<BE>, M: GGSWAutomorphism<BE>,
@@ -78,18 +78,16 @@ where
where where
R: GGSWToMut, R: GGSWToMut,
A: GGSWToRef, A: GGSWToRef,
K: AutomorphismKeyPreparedToRef<BE>, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
let a: &GGSW<&[u8]> = &a.to_ref(); let a: &GGSW<&[u8]> = &a.to_ref();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref();
assert_eq!(res.ggsw_layout(), a.ggsw_layout()); assert_eq!(res.dsize(), a.dsize());
assert_eq!(res.glwe_layout(), a.glwe_layout()); assert!(res.dnum() <= a.dnum());
assert_eq!(res.lwe_layout(), a.lwe_layout());
assert!(scratch.available() >= self.ggsw_automorphism_tmp_bytes(res, a, key, tsk)); assert!(scratch.available() >= self.ggsw_automorphism_tmp_bytes(res, a, key, tsk));
// Keyswitch the j-th row of the col 0 // Keyswitch the j-th row of the col 0
@@ -105,12 +103,11 @@ where
fn ggsw_automorphism_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>) fn ggsw_automorphism_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
where where
R: GGSWToMut, R: GGSWToMut,
K: AutomorphismKeyPreparedToRef<BE>, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref();
// Keyswitch the j-th row of the col 0 // Keyswitch the j-th row of the col 0

View File

@@ -9,8 +9,7 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEKeyswitch, ScratchTakeCore, keyswitch_internal, GLWEKeyswitch, ScratchTakeCore, keyswitch_internal,
layouts::{ layouts::{
GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement,
prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement},
}, },
}; };
@@ -31,7 +30,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism(self, a, key, scratch); module.glwe_automorphism(self, a, key, scratch);
@@ -41,7 +40,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_add(self, a, key, scratch); module.glwe_automorphism_add(self, a, key, scratch);
@@ -51,7 +50,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_sub(self, a, key, scratch); module.glwe_automorphism_sub(self, a, key, scratch);
@@ -61,7 +60,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_sub_negate(self, a, key, scratch); module.glwe_automorphism_sub_negate(self, a, key, scratch);
@@ -70,7 +69,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn automorphism_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>) pub fn automorphism_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>)
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_inplace(self, key, scratch); module.glwe_automorphism_inplace(self, key, scratch);
@@ -79,7 +78,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn automorphism_add_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>) pub fn automorphism_add_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>)
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_add_inplace(self, key, scratch); module.glwe_automorphism_add_inplace(self, key, scratch);
@@ -88,7 +87,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn automorphism_sub_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>) pub fn automorphism_sub_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>)
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_sub_inplace(self, key, scratch); module.glwe_automorphism_sub_inplace(self, key, scratch);
@@ -97,7 +96,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn automorphism_sub_negate_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>) pub fn automorphism_sub_negate_inplace<M, K, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>)
where where
M: GLWEAutomorphism<BE>, M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_automorphism_sub_negate_inplace(self, key, scratch); module.glwe_automorphism_sub_negate_inplace(self, key, scratch);
@@ -125,10 +124,10 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
self.glwe_keyswitch(res, a, &key.to_ref().key, scratch); self.glwe_keyswitch(res, a, key, scratch);
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
@@ -140,10 +139,10 @@ where
fn glwe_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) fn glwe_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where where
R: GLWEToMut, R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
self.glwe_keyswitch_inplace(res, &key.to_ref().key, scratch); self.glwe_keyswitch_inplace(res, key, scratch);
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
@@ -156,15 +155,14 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWE<&[u8]> = &a.to_ref(); let a: &GLWE<&[u8]> = &a.to_ref();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size
let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1);
for i in 0..res.rank().as_usize() + 1 { for i in 0..res.rank().as_usize() + 1 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -184,14 +182,13 @@ where
fn glwe_automorphism_add_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) fn glwe_automorphism_add_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where where
R: GLWEToMut, R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size
let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1);
for i in 0..res.rank().as_usize() + 1 { for i in 0..res.rank().as_usize() + 1 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -212,15 +209,14 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWE<&[u8]> = &a.to_ref(); let a: &GLWE<&[u8]> = &a.to_ref();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size
let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1);
for i in 0..res.rank().as_usize() + 1 { for i in 0..res.rank().as_usize() + 1 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -241,15 +237,14 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWE<&[u8]> = &a.to_ref(); let a: &GLWE<&[u8]> = &a.to_ref();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size
let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1);
for i in 0..res.rank().as_usize() + 1 { for i in 0..res.rank().as_usize() + 1 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -269,14 +264,13 @@ where
fn glwe_automorphism_sub_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) fn glwe_automorphism_sub_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where where
R: GLWEToMut, R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size
let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1);
for i in 0..res.rank().as_usize() + 1 { for i in 0..res.rank().as_usize() + 1 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -296,14 +290,13 @@ where
fn glwe_automorphism_sub_negate_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) fn glwe_automorphism_sub_negate_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where where
R: GLWEToMut, R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref();
let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size
let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1);
for i in 0..res.rank().as_usize() + 1 { for i in 0..res.rank().as_usize() + 1 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);

View File

@@ -119,10 +119,6 @@ where
+ VmpApplyDftToDftTmpBytes + VmpApplyDftToDftTmpBytes
+ VecZnxBigBytesOf + VecZnxBigBytesOf
+ VecZnxNormalizeTmpBytes + VecZnxNormalizeTmpBytes
+ VecZnxDftBytesOf
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigBytesOf
+ VecZnxNormalizeTmpBytes
+ VecZnxDftApply<BE> + VecZnxDftApply<BE>
+ VecZnxDftCopy<BE> + VecZnxDftCopy<BE>
+ VmpApplyDftToDft<BE> + VmpApplyDftToDft<BE>
@@ -236,7 +232,7 @@ where
// = // =
// (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2)
for col_i in 1..cols { for col_i in 1..cols {
let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).data; // Selects Enc(s[i]s[j])
// Extracts a[i] and multipies with Enc(s[i]s[j]) // Extracts a[i] and multipies with Enc(s[i]s[j])
for di in 0..dsize { for di in 0..dsize {

View File

@@ -5,10 +5,7 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEKeyswitch, ScratchTakeCore, GLWEKeyswitch, ScratchTakeCore,
layouts::{ layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank},
GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank,
prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef},
},
}; };
pub trait LWESampleExtract pub trait LWESampleExtract
@@ -71,12 +68,11 @@ where
where where
R: LWEToMut, R: LWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: LWEToGLWESwitchingKeyPreparedToRef<BE> + GGLWEInfos, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWE<&[u8]> = &a.to_ref(); let a: &GLWE<&[u8]> = &a.to_ref();
let key: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref();
assert_eq!(a.n(), self.n() as u32); assert_eq!(a.n(), self.n() as u32);
assert_eq!(key.n(), self.n() as u32); assert_eq!(key.n(), self.n() as u32);
@@ -90,7 +86,7 @@ where
}; };
let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(self, &glwe_layout); let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(self, &glwe_layout);
self.glwe_keyswitch(&mut tmp_glwe, a, &key.0, scratch_1); self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1);
self.lwe_sample_extract(res, &tmp_glwe); self.lwe_sample_extract(res, &tmp_glwe);
} }
} }
@@ -120,7 +116,7 @@ impl<D: DataMut> LWE<D> {
where where
R: LWEToMut, R: LWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: LWEToGLWESwitchingKeyPreparedToRef<BE> + GGLWEInfos, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
M: LWEFromGLWE<BE>, M: LWEFromGLWE<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {

View File

@@ -5,10 +5,7 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEKeyswitch, ScratchTakeCore, GLWEKeyswitch, ScratchTakeCore,
layouts::{ layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef},
GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef,
prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef},
},
}; };
impl<BE: Backend> GLWEFromLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> {} impl<BE: Backend> GLWEFromLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> {}
@@ -43,12 +40,11 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: LWEToRef, A: LWEToRef,
K: LWEToGLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let lwe: &LWE<&[u8]> = &lwe.to_ref(); let lwe: &LWE<&[u8]> = &lwe.to_ref();
let ksk: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref();
assert_eq!(res.n(), self.n() as u32); assert_eq!(res.n(), self.n() as u32);
assert_eq!(ksk.n(), self.n() as u32); assert_eq!(ksk.n(), self.n() as u32);
@@ -108,7 +104,7 @@ where
); );
} }
self.glwe_keyswitch(res, &glwe, &ksk.0, scratch_1); self.glwe_keyswitch(res, &glwe, ksk, scratch_1);
} }
} }
@@ -129,7 +125,7 @@ impl<D: DataMut> GLWE<D> {
where where
M: GLWEFromLWE<BE>, M: GLWEFromLWE<BE>,
A: LWEToRef, A: LWEToRef,
K: LWEToGLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.glwe_from_lwe(self, lwe, ksk, scratch); module.glwe_from_lwe(self, lwe, ksk, scratch);

View File

@@ -1,33 +1,39 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace}, api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace},
layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, ZnxView, ZnxViewMut}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, ZnxView, ZnxViewMut},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
}; };
use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut}; use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut};
impl<DataSelf: DataRef + DataMut> LWE<DataSelf> { impl<DataSelf: DataRef + DataMut> LWE<DataSelf> {
pub fn decrypt<P, S, M, B>(&mut self, module: &M, pt: &mut P, sk: S) pub fn decrypt<P, S, M, B: Backend>(&mut self, module: &M, pt: &mut P, sk: &S)
where where
P: LWEPlaintextToMut, P: LWEPlaintextToMut,
S: LWESecretToRef, S: LWESecretToRef,
M: LWEDecrypt<B>, M: LWEDecrypt<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{ {
module.lwe_decrypt(self, pt, sk); module.lwe_decrypt(self, pt, sk);
} }
} }
pub trait LWEDecrypt<BE: Backend> pub trait LWEDecrypt<BE: Backend> {
fn lwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: &S)
where
R: LWEToMut,
P: LWEPlaintextToMut,
S: LWESecretToRef;
}
impl<BE: Backend> LWEDecrypt<BE> for Module<BE>
where where
Self: Sized + ZnNormalizeInplace<BE>, Self: Sized + ZnNormalizeInplace<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
{ {
fn lwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: S) fn lwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: &S)
where where
R: LWEToMut, R: LWEToMut,
P: LWEPlaintextToMut, P: LWEPlaintextToMut,
S: LWESecretToRef, S: LWESecretToRef,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
{ {
let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut(); let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut();
@@ -57,5 +63,3 @@ where
pt.k = crate::layouts::TorusPrecision(res.k().0.min(pt.size() as u32 * res.base2k().0)); pt.k = crate::layouts::TorusPrecision(res.k().0.min(pt.size() as u32 * res.base2k().0));
} }
} }
impl<BE: Backend> LWEDecrypt<BE> for Module<BE> where Self: Sized + ZnNormalizeInplace<BE> {}

View File

@@ -2,4 +2,4 @@ mod glwe_ct;
mod lwe_ct; mod lwe_ct;
pub use glwe_ct::*; pub use glwe_ct::*;
// pub use lwe_ct::*; pub use lwe_ct::*;

View File

@@ -5,11 +5,10 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
ScratchTakeCore, GGLWECompressedEncryptSk, ScratchTakeCore,
encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared,
compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, compressed::AutomorphismKeyCompressed,
}, },
}; };
@@ -34,7 +33,7 @@ impl<DataSelf: DataMut> AutomorphismKeyCompressed<DataSelf> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
S: GLWESecretToRef, S: GLWESecretToRef + GLWEInfos,
M: AutomorphismKeyCompressedEncryptSk<BE>, M: AutomorphismKeyCompressedEncryptSk<BE>,
{ {
module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch);
@@ -55,20 +54,28 @@ pub trait AutomorphismKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: AutomorphismKeyCompressedToMut, R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef; S: GLWESecretToRef + GLWEInfos;
} }
impl<BE: Backend> AutomorphismKeyCompressedEncryptSk<BE> for Module<BE> impl<BE: Backend> AutomorphismKeyCompressedEncryptSk<BE> for Module<BE>
where where
Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk<BE>, Self: ModuleN
+ GaloisElement
+ VecZnxAutomorphism
+ GGLWECompressedEncryptSk<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of(self.n().into(), infos.rank()) assert_eq!(self.n() as u32, infos.n());
self.gglwe_compressed_encrypt_sk_tmp_bytes(infos)
.max(GLWESecret::bytes_of_from_infos(infos))
+ GLWESecretPrepared::bytes_of_from_infos(self, infos)
} }
fn automorphism_key_compressed_encrypt_sk<R, S>( fn automorphism_key_compressed_encrypt_sk<R, S>(
@@ -80,12 +87,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: AutomorphismKeyCompressedToMut, R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef, S: GLWESecretToRef + GLWEInfos,
{ {
let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref(); let sk: &GLWESecret<&[u8]> = &sk.to_ref();
assert_eq!(res.n(), sk.n()); assert_eq!(res.n(), sk.n());
assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(res.rank_out(), res.rank_in());
assert_eq!(sk.rank(), res.rank_out()); assert_eq!(sk.rank(), res.rank_out());
@@ -96,9 +101,9 @@ where
AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res)
); );
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
{ {
let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank());
for i in 0..res.rank_out().into() { for i in 0..res.rank_out().into() {
self.vec_znx_automorphism( self.vec_znx_automorphism(
self.galois_element_inv(p), self.galois_element_inv(p),
@@ -108,10 +113,18 @@ where
i, i,
); );
} }
sk_out_prepared.prepare(self, &sk_out);
} }
self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); self.gglwe_compressed_encrypt_sk(
res,
&sk.data,
&sk_out_prepared,
seed_xa,
source_xe,
scratch_1,
);
res.p = p; res.set_p(p);
} }
} }

View File

@@ -8,8 +8,9 @@ use crate::{
ScratchTakeCore, ScratchTakeCore,
encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, GLWESwitchingKeyDegreesMut, LWEInfos,
compressed::GLWESwitchingKeyCompressed,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
}, },
}; };
@@ -57,7 +58,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWESwitchingKeyCompressedToMut, R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef, S1: GLWESecretToRef,
S2: GLWESecretToRef; S2: GLWESecretToRef;
} }
@@ -86,11 +87,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWESwitchingKeyCompressedToMut, R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef, S1: GLWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretToRef,
{ {
let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
@@ -122,15 +122,9 @@ where
} }
} }
self.gglwe_compressed_encrypt_sk( self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2);
&mut res.key,
&sk_in_tmp, *res.input_degree() = sk_in.n();
&sk_out_tmp, *res.output_degree() = sk_out.n();
seed_xa,
source_xe,
scratch_2,
);
res.sk_in_n = sk_in.n().into();
res.sk_out_n = sk_out.n().into();
} }
} }

View File

@@ -4,16 +4,15 @@ use poulpy_hal::{
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA,
}, },
layouts::{Backend, DataMut, Module, Scratch}, layouts::{Backend, DataMut, Module, Scratch},
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
source::Source, source::Source,
}; };
use crate::{ use crate::{
GetDistribution, ScratchTakeCore, GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore,
encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk}, encryption::gglwe_tsk::TensorKeyEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, Rank,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, TensorKeyCompressedAtMut, compressed::TensorKeyCompressed,
}, },
}; };
@@ -23,7 +22,7 @@ impl TensorKeyCompressed<Vec<u8>> {
A: GGLWEInfos, A: GGLWEInfos,
M: GGLWETensorKeyCompressedEncryptSk<BE>, M: GGLWETensorKeyCompressedEncryptSk<BE>,
{ {
module.gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos)
} }
} }
@@ -39,16 +38,16 @@ impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
S: GLWESecretToRef + GetDistribution, S: GLWESecretToRef + GetDistribution,
M: GGLWETensorKeyCompressedEncryptSk<BE>, M: GGLWETensorKeyCompressedEncryptSk<BE>,
{ {
module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); module.tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch);
} }
} }
pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> { pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize fn tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where where
A: GGLWEInfos; A: GGLWEInfos;
fn gglwe_tensor_key_encrypt_sk<R, S>( fn tensor_key_compressed_encrypt_sk<R, S, D>(
&self, &self,
res: &mut R, res: &mut R,
sk: &S, sk: &S,
@@ -56,36 +55,40 @@ pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: TensorKeyCompressedToMut, D: DataMut,
R: TensorKeyCompressedAtMut<D> + GGLWEInfos,
S: GLWESecretToRef + GetDistribution; S: GLWESecretToRef + GetDistribution;
} }
impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE> impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE>
where where
Self: ModuleN Self: ModuleN
+ GLWESwitchingKeyCompressedEncryptSk<BE> + GGLWECompressedEncryptSk<BE>
+ TensorKeyEncryptSk<BE> + TensorKeyEncryptSk<BE>
+ VecZnxDftApply<BE> + VecZnxDftApply<BE>
+ SvpApplyDftToDft<BE> + SvpApplyDftToDft<BE>
+ VecZnxIdftApplyTmpA<BE> + VecZnxIdftApplyTmpA<BE>
+ VecZnxBigNormalize<BE> + VecZnxBigNormalize<BE>
+ SvpPrepare<BE> + SvpPrepare<BE>
+ SvpPPolAllocBytesImpl<BE>
+ SvpPPolBytesOf + SvpPPolBytesOf
+ VecZnxDftAllocBytesImpl<BE>
+ VecZnxBigAllocBytesImpl<BE>
+ VecZnxDftBytesOf + VecZnxDftBytesOf
+ VecZnxBigBytesOf, + VecZnxBigBytesOf
+ GLWESecretPreparedAlloc<BE>,
Scratch<BE>: ScratchTakeBasic + ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeBasic + ScratchTakeCore<BE>,
{ {
fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize fn tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.tensor_key_encrypt_sk_tmp_bytes(infos) GLWESecretPrepared::bytes_of(self, infos.rank_out())
+ self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
+ self.bytes_of_vec_znx_big(1, 1)
+ self.bytes_of_vec_znx_dft(1, 1)
+ GLWESecret::bytes_of(self.n().into(), Rank(1))
+ self.gglwe_compressed_encrypt_sk_tmp_bytes(infos)
} }
fn gglwe_tensor_key_encrypt_sk<R, S>( fn tensor_key_compressed_encrypt_sk<R, S, D>(
&self, &self,
res: &mut R, res: &mut R,
sk: &S, sk: &S,
@@ -93,11 +96,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: TensorKeyCompressedToMut, D: DataMut,
R: GGLWEInfos + TensorKeyCompressedAtMut<D>,
S: GLWESecretToRef + GetDistribution, S: GLWESecretToRef + GetDistribution,
{ {
let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out()); let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out());
sk_dft_prep.prepare(self, sk); sk_dft_prep.prepare(self, sk);
@@ -141,10 +143,10 @@ where
let (seed_xa_tmp, _) = source_xa.branch(); let (seed_xa_tmp, _) = source_xa.branch();
self.glwe_switching_key_compressed_encrypt_sk( self.gglwe_compressed_encrypt_sk(
res.at_mut(i, j), res.at_mut(i, j),
&sk_ij, &sk_ij.data,
sk, &sk_dft_prep,
seed_xa_tmp, seed_xa_tmp,
source_xe, source_xe,
scratch_5, scratch_5,

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace}, api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace},
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero},
source::Source, source::Source,
}; };
@@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GGSWCompressedToMut + GGSWCompressedSeedMut, R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>; S: GLWESecretPreparedToRef<BE>;
} }
@@ -83,31 +83,30 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GGSWCompressedToMut + GGSWCompressedSeedMut, R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
{ {
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
{
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
#[cfg(debug_assertions)]
{
use poulpy_hal::layouts::ZnxInfos;
assert_eq!(res.rank(), sk.rank());
assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n());
}
let base2k: usize = res.base2k().into(); let base2k: usize = res.base2k().into();
let rank: usize = res.rank().into(); let rank: usize = res.rank().into();
let cols: usize = rank + 1; let cols: usize = rank + 1;
let dsize: usize = res.dsize().into(); let dsize: usize = res.dsize().into();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
assert_eq!(res.rank(), sk.rank());
assert_eq!(pt.n(), self.n());
assert_eq!(res.n(), self.n() as u32);
assert_eq!(sk.n(), self.n() as u32);
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.dnum().as_usize() * (res.rank().as_usize() + 1)];
{
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
println!("res.seed: {:?}", res.seed);
let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
let mut source = Source::new(seed_xa); let mut source = Source::new(seed_xa);

View File

@@ -1,13 +1,15 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ScratchAvailable, VecZnxAutomorphism}, api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism},
layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, layouts::{Backend, DataMut, GaloisElement, Module, Scratch},
source::Source, source::Source,
}; };
use crate::{ use crate::{
ScratchTakeCore, GGLWEEncryptSk, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{
layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos}, AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared,
GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement,
},
}; };
impl AutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
@@ -30,7 +32,7 @@ impl AutomorphismKey<Vec<u8>> {
impl<DM: DataMut> AutomorphismKey<DM> impl<DM: DataMut> AutomorphismKey<DM>
where where
Self: AutomorphismKeyToMut, Self: GGLWEToRef,
{ {
pub fn encrypt_sk<S, M, BE: Backend>( pub fn encrypt_sk<S, M, BE: Backend>(
&mut self, &mut self,
@@ -62,13 +64,18 @@ pub trait AutomorphismKeyEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: AutomorphismKeyToMut, R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef; S: GLWESecretToRef;
} }
impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE> impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE>
where where
Self: GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphism + GaloisElement, Self: GGLWEEncryptSk<BE>
+ VecZnxAutomorphism
+ GaloisElement
+ SvpPPolBytesOf
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -80,7 +87,10 @@ where
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey" "rank_in != rank_out is not supported for GGLWEAutomorphismKey"
); );
self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(infos) GLWESecretPrepared::bytes_of_from_infos(self, infos)
+ self
.gglwe_encrypt_sk_tmp_bytes(infos)
.max(GLWESecret::bytes_of_from_infos(infos))
} }
fn automorphism_key_encrypt_sk<R, S>( fn automorphism_key_encrypt_sk<R, S>(
@@ -92,10 +102,9 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: AutomorphismKeyToMut, R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef, S: GLWESecretToRef,
{ {
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref(); let sk: &GLWESecret<&[u8]> = &sk.to_ref();
assert_eq!(res.n(), sk.n()); assert_eq!(res.n(), sk.n());
@@ -108,9 +117,10 @@ where
self.automorphism_key_encrypt_sk_tmp_bytes(res) self.automorphism_key_encrypt_sk_tmp_bytes(res)
); );
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
{ {
let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank());
(0..res.rank_out().into()).for_each(|i| { (0..res.rank_out().into()).for_each(|i| {
self.vec_znx_automorphism( self.vec_znx_automorphism(
self.galois_element_inv(p), self.galois_element_inv(p),
@@ -120,12 +130,19 @@ where
i, i,
); );
}); });
sk_out_prepared.prepare(self, &sk_out);
} }
res.key self.gglwe_encrypt_sk(
.encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); res,
&sk.data,
&sk_out_prepared,
source_xa,
source_xe,
scratch_1,
);
res.p = p; res.set_p(p);
} }
} }

View File

@@ -8,7 +8,7 @@ use crate::{
ScratchTakeCore, ScratchTakeCore,
encryption::gglwe_ct::GGLWEEncryptSk, encryption::gglwe_ct::GGLWEEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos,
prepared::GLWESecretPreparedAlloc, prepared::GLWESecretPreparedAlloc,
}, },
}; };
@@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef, S1: GLWESecretToRef,
S2: GLWESecretToRef; S2: GLWESecretToRef;
} }
@@ -93,11 +93,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef, S1: GLWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretToRef,
{ {
let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
@@ -129,16 +128,17 @@ where
} }
} }
res.key.encrypt_sk( self.gglwe_encrypt_sk(
self, res,
&sk_in_tmp, &sk_in_tmp,
&sk_out_tmp, &sk_out_tmp,
source_xa, source_xa,
source_xe, source_xe,
scratch_2, scratch_2,
); );
res.sk_in_n = sk_in.n().into();
res.sk_out_n = sk_out.n().into(); *res.input_degree() = sk_in.n();
*res.output_degree() = sk_out.n();
} }
} }

View File

@@ -8,10 +8,9 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
GetDistribution, ScratchTakeCore, GGLWEEncryptSk, GetDistribution, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
}, },
}; };
@@ -63,7 +62,7 @@ pub trait TensorKeyEncryptSk<BE: Backend> {
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE> impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
where where
Self: ModuleN Self: ModuleN
+ GLWESwitchingKeyEncryptSk<BE> + GGLWEEncryptSk<BE>
+ VecZnxDftBytesOf + VecZnxDftBytesOf
+ VecZnxBigBytesOf + VecZnxBigBytesOf
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
@@ -83,7 +82,7 @@ where
+ self.bytes_of_vec_znx_big(1, 1) + self.bytes_of_vec_znx_big(1, 1)
+ self.bytes_of_vec_znx_dft(1, 1) + self.bytes_of_vec_znx_dft(1, 1)
+ GLWESecret::bytes_of(self.n().into(), Rank(1)) + GLWESecret::bytes_of(self.n().into(), Rank(1))
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GGLWE::encrypt_sk_tmp_bytes(self, infos)
} }
fn tensor_key_encrypt_sk<R, S>( fn tensor_key_encrypt_sk<R, S>(
@@ -102,8 +101,8 @@ where
// let n: RingDegree = sk.n(); // let n: RingDegree = sk.n();
let rank: Rank = res.rank_out(); let rank: Rank = res.rank_out();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
sk_dft_prep.prepare(self, sk); sk_prepared.prepare(self, sk);
let sk: &GLWESecret<&[u8]> = &sk.to_ref(); let sk: &GLWESecret<&[u8]> = &sk.to_ref();
@@ -122,7 +121,7 @@ where
(0..rank.into()).for_each(|i| { (0..rank.into()).for_each(|i| {
(i..rank.into()).for_each(|j| { (i..rank.into()).for_each(|j| {
self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
self.vec_znx_big_normalize( self.vec_znx_big_normalize(
@@ -135,8 +134,14 @@ where
scratch_5, scratch_5,
); );
res.at_mut(i, j) res.at_mut(i, j).encrypt_sk(
.encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5); self,
&sk_ij.data,
&sk_prepared,
source_xa,
source_xe,
scratch_5,
);
}); });
}) })
} }

View File

@@ -79,7 +79,7 @@ impl<D: DataMut> GLWE<D> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>, K: GLWEPublicKeyPreparedToRef<BE>,
M: GLWEEncryptPk<BE>, M: GLWEEncryptPk<BE>,
{ {
@@ -245,7 +245,7 @@ pub trait GLWEEncryptPk<BE: Backend> {
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWEToMut, R: GLWEToMut,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>; K: GLWEPublicKeyPreparedToRef<BE>;
fn glwe_encrypt_zero_pk<R, K>( fn glwe_encrypt_zero_pk<R, K>(
@@ -285,7 +285,7 @@ where
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWEToMut, R: GLWEToMut,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>, K: GLWEPublicKeyPreparedToRef<BE>,
{ {
self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch);
@@ -324,7 +324,7 @@ pub(crate) trait GLWEEncryptPkInternal<BE: Backend> {
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWEToMut, R: GLWEToMut,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>; K: GLWEPublicKeyPreparedToRef<BE>;
} }
@@ -351,7 +351,7 @@ where
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWEToMut, R: GLWEToMut,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>, K: GLWEPublicKeyPreparedToRef<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
@@ -361,8 +361,8 @@ where
assert_eq!(res.n(), pk.n()); assert_eq!(res.n(), pk.n());
assert_eq!(res.rank(), pk.rank()); assert_eq!(res.rank(), pk.rank());
if let Some((pt, _)) = pt { if let Some((pt, _)) = pt {
assert_eq!(pt.to_ref().base2k(), pk.base2k()); assert_eq!(pt.base2k(), pk.base2k());
assert_eq!(pt.to_ref().n(), pk.n()); assert_eq!(pt.n(), pk.n());
} }
let base2k: usize = pk.base2k().into(); let base2k: usize = pk.base2k().into();

View File

@@ -1,14 +1,13 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ModuleN, VecZnxAutomorphismInplace}, api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes},
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
source::Source, source::Source,
}; };
use crate::{ use crate::{
ScratchTakeCore, GGLWEEncryptSk, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPrepare, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos,
LWESecret, LWESecretToRef, Rank, LWESecret, LWESecretToRef, Rank,
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
}, },
@@ -59,12 +58,17 @@ pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend> {
) where ) where
S1: LWESecretToRef, S1: LWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretToRef,
R: GLWEToLWESwitchingKeyToMut; R: GGLWEToMut;
} }
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE> impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE>
where where
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxAutomorphismInplace<BE>, Self: ModuleN
+ GGLWEEncryptSk<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxAutomorphismInplace<BE>
+ VecZnxAutomorphismInplaceTmpBytes
+ GLWESecretPrepare<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -72,7 +76,8 @@ where
A: GGLWEInfos, A: GGLWEInfos,
{ {
GLWESecretPrepared::bytes_of(self, infos.rank_in()) GLWESecretPrepared::bytes_of(self, infos.rank_in())
+ (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self.n().into(), infos.rank_in())) + GGLWE::encrypt_sk_tmp_bytes(self, infos)
.max(GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + self.vec_znx_automorphism_inplace_tmp_bytes())
} }
fn glwe_to_lwe_switching_key_encrypt_sk<R, S1, S2>( fn glwe_to_lwe_switching_key_encrypt_sk<R, S1, S2>(
@@ -86,23 +91,27 @@ where
) where ) where
S1: LWESecretToRef, S1: LWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretToRef,
R: GLWEToLWESwitchingKeyToMut, R: GGLWEToMut,
{ {
let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref();
let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref();
assert!(sk_lwe.n().0 <= self.n() as u32); assert!(sk_lwe.n().0 <= self.n() as u32);
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); let (mut sk_lwe_as_glwe_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, Rank(1));
{
let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1));
sk_lwe_as_glwe.data.zero(); sk_lwe_as_glwe.data.zero();
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2);
sk_lwe_as_glwe_prep.prepare(self, &sk_lwe_as_glwe);
}
res.0.encrypt_sk( self.gglwe_encrypt_sk(
self, res,
sk_glwe, &sk_glwe.data,
&sk_lwe_as_glwe, &sk_lwe_as_glwe_prep,
source_xa, source_xa,
source_xe, source_xe,
scratch_1, scratch_1,

View File

@@ -1,7 +1,6 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace}, api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace},
layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut}, layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
source::Source, source::Source,
}; };
@@ -16,7 +15,6 @@ impl<DataSelf: DataMut> LWE<DataSelf> {
P: LWEPlaintextToRef, P: LWEPlaintextToRef,
S: LWESecretToRef, S: LWESecretToRef,
M: LWEEncryptSk<BE>, M: LWEEncryptSk<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
{ {
module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe); module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe);
} }
@@ -33,7 +31,7 @@ pub trait LWEEncryptSk<BE: Backend> {
impl<BE: Backend> LWEEncryptSk<BE> for Module<BE> impl<BE: Backend> LWEEncryptSk<BE> for Module<BE>
where where
Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>, Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
{ {
fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where where

View File

@@ -8,8 +8,8 @@ use crate::{
ScratchTakeCore, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef,
Rank, LWESwitchingKey, Rank,
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
}, },
}; };
@@ -56,7 +56,7 @@ pub trait LWESwitchingKeyEncrypt<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: LWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: LWESecretToRef, S1: LWESecretToRef,
S2: LWESecretToRef; S2: LWESecretToRef;
} }
@@ -100,13 +100,12 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: LWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: LWESecretToRef, S1: LWESecretToRef,
S2: LWESecretToRef, S2: LWESecretToRef,
{ {
let res: &mut LWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_lwe_in: &LWESecret<&[u8]> = &sk_lwe_in.to_ref(); let sk_lwe_in: &LWESecret<&[u8]> = &sk_lwe_in.to_ref();
let sk_lwe_out = &sk_lwe_out.to_ref(); let sk_lwe_out: &LWESecret<&[u8]> = &sk_lwe_out.to_ref();
assert!(sk_lwe_in.n().0 <= res.n().0); assert!(sk_lwe_in.n().0 <= res.n().0);
assert!(sk_lwe_out.n().0 <= res.n().0); assert!(sk_lwe_out.n().0 <= res.n().0);
@@ -124,7 +123,7 @@ where
self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2);
self.glwe_switching_key_encrypt_sk( self.glwe_switching_key_encrypt_sk(
&mut res.0, res,
&sk_in_glwe, &sk_in_glwe,
&sk_out_glwe, &sk_out_glwe,
source_xa, source_xa,

View File

@@ -1,15 +1,14 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ModuleN, VecZnxAutomorphismInplace}, api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes},
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut},
source::Source, source::Source,
}; };
use crate::{ use crate::{
ScratchTakeCore, GGLWEEncryptSk, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedAlloc, GLWESecretPreparedToRef, LWEInfos, LWESecret,
LWEToGLWESwitchingKeyToMut, Rank, LWESecretToRef, LWEToGLWESwitchingKey, Rank,
}, },
}; };
@@ -34,7 +33,7 @@ impl<D: DataMut> LWEToGLWESwitchingKey<D> {
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
S1: LWESecretToRef, S1: LWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretPreparedToRef<BE>,
M: LWEToGLWESwitchingKeyEncrypt<BE>, M: LWEToGLWESwitchingKeyEncrypt<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
@@ -57,13 +56,17 @@ pub trait LWEToGLWESwitchingKeyEncrypt<BE: Backend> {
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
S1: LWESecretToRef, S1: LWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretPreparedToRef<BE>,
R: LWEToGLWESwitchingKeyToMut; R: GGLWEToMut;
} }
impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE> impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE>
where where
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphismInplace<BE>, Self: ModuleN
+ GGLWEEncryptSk<BE>
+ VecZnxAutomorphismInplace<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxAutomorphismInplaceTmpBytes,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -75,7 +78,8 @@ where
Rank(1), Rank(1),
"rank_in != 1 is not supported for LWEToGLWESwitchingKey" "rank_in != 1 is not supported for LWEToGLWESwitchingKey"
); );
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) GLWESecret::bytes_of(self.n().into(), infos.rank_in())
+ GGLWE::encrypt_sk_tmp_bytes(self, infos).max(self.vec_znx_automorphism_inplace_tmp_bytes())
} }
fn lwe_to_glwe_switching_key_encrypt_sk<R, S1, S2>( fn lwe_to_glwe_switching_key_encrypt_sk<R, S1, S2>(
@@ -88,12 +92,10 @@ where
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
S1: LWESecretToRef, S1: LWESecretToRef,
S2: GLWESecretToRef, S2: GLWESecretPreparedToRef<BE>,
R: LWEToGLWESwitchingKeyToMut, R: GGLWEToMut,
{ {
let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref();
let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref();
assert!(sk_lwe.n().0 <= self.n() as u32); assert!(sk_lwe.n().0 <= self.n() as u32);
@@ -102,9 +104,9 @@ where
sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
res.0.encrypt_sk( self.gglwe_encrypt_sk(
self, res,
&sk_lwe_as_glwe, &sk_lwe_as_glwe.data,
sk_glwe, sk_glwe,
source_xa, source_xa,
source_xe, source_xe,

View File

@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Scratch};
use crate::{ use crate::{
ScratchTakeCore, ScratchTakeCore,
external_product::gglwe_ksk::GGLWEExternalProduct, external_product::gglwe_ksk::GGLWEExternalProduct,
layouts::{AutomorphismKey, AutomorphismKeyToRef, GGLWEInfos, GGSWInfos, prepared::GGSWPreparedToRef}, layouts::{AutomorphismKey, GGLWEInfos, GGLWEToRef, GGSWInfos, prepared::GGSWPreparedToRef},
}; };
impl AutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
@@ -28,11 +28,11 @@ impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn external_product<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) pub fn external_product<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where where
M: GGLWEExternalProduct<BE>, M: GGLWEExternalProduct<BE>,
A: AutomorphismKeyToRef, A: GGLWEToRef,
B: GGSWPreparedToRef<BE>, B: GGSWPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.gglwe_external_product(&mut self.key.key, &a.to_ref().key.key, b, scratch); module.gglwe_external_product(self, a, b, scratch);
} }
pub fn external_product_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) pub fn external_product_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
@@ -41,6 +41,6 @@ impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
A: GGSWPreparedToRef<BE>, A: GGSWPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.gglwe_external_product_inplace(&mut self.key.key, a, scratch); module.gglwe_external_product_inplace(self, a, scratch);
} }
} }

View File

@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero};
use crate::{ use crate::{
GLWEExternalProduct, ScratchTakeCore, GLWEExternalProduct, ScratchTakeCore,
layouts::{ layouts::{
GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey,
prepared::{GGSWPrepared, GGSWPreparedToRef}, prepared::{GGSWPrepared, GGSWPreparedToRef},
}, },
}; };
@@ -116,11 +116,11 @@ impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
pub fn external_product<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) pub fn external_product<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where where
M: GGLWEExternalProduct<BE>, M: GGLWEExternalProduct<BE>,
A: GLWESwitchingKeyToRef, A: GGLWEToRef,
B: GGSWPreparedToRef<BE>, B: GGSWPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.gglwe_external_product(&mut self.key, &a.to_ref().key, b, scratch); module.gglwe_external_product(self, a, b, scratch);
} }
pub fn external_product_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) pub fn external_product_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
@@ -129,6 +129,6 @@ impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
A: GGSWPreparedToRef<BE>, A: GGSWPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.gglwe_external_product_inplace(&mut self.key, a, scratch); module.gglwe_external_product_inplace(self, a, scratch);
} }
} }

View File

@@ -9,8 +9,7 @@ use crate::{
GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore,
glwe_trace::GLWETrace, glwe_trace::GLWETrace,
layouts::{ layouts::{
GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement,
prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement},
}, },
}; };
@@ -118,7 +117,7 @@ impl GLWEPacker {
pub fn add<A, K, M, BE: Backend>(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap<i64, K>, scratch: &mut Scratch<BE>) pub fn add<A, K, M, BE: Backend>(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap<i64, K>, scratch: &mut Scratch<BE>)
where where
A: GLWEToRef + GLWEInfos, A: GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
M: GLWEPacking<BE>, M: GLWEPacking<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
@@ -191,7 +190,7 @@ where
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWEToMut + GLWEToRef + GLWEInfos, R: GLWEToMut + GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@@ -235,7 +234,7 @@ fn pack_core<A, K, M, BE: Backend>(
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
A: GLWEToRef + GLWEInfos, A: GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
M: ModuleLogN M: ModuleLogN
+ GLWEAutomorphism<BE> + GLWEAutomorphism<BE>
+ GaloisElement + GaloisElement
@@ -308,7 +307,7 @@ fn combine<B, M, K, BE: Backend>(
B: GLWEToRef + GLWEInfos, B: GLWEToRef + GLWEInfos,
M: GLWEAutomorphism<BE> + GaloisElement + GLWERotate<BE> + GLWESub + GLWEShift<BE> + GLWEAdd + GLWENormalize<BE>, M: GLWEAutomorphism<BE> + GaloisElement + GLWERotate<BE> + GLWESub + GLWEShift<BE> + GLWEAdd + GLWENormalize<BE>,
B: GLWEToRef + GLWEInfos, B: GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let log_n: usize = acc.data.n().log2(); let log_n: usize = acc.data.n().log2();
@@ -401,7 +400,7 @@ fn pack_internal<M, A, B, K, BE: Backend>(
M: GLWEAutomorphism<BE> + GLWERotate<BE> + GLWESub + GLWEShift<BE> + GLWEAdd + GLWENormalize<BE>, M: GLWEAutomorphism<BE> + GLWERotate<BE> + GLWESub + GLWEShift<BE> + GLWEAdd + GLWENormalize<BE>,
A: GLWEToMut + GLWEToRef + GLWEInfos, A: GLWEToMut + GLWEToRef + GLWEInfos,
B: GLWEToMut + GLWEToRef + GLWEInfos, B: GLWEToMut + GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
// Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t))

View File

@@ -8,8 +8,8 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore, GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore,
layouts::{ layouts::{
Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, prepared::GetAutomorphismGaloisElement,
}, },
}; };
@@ -43,7 +43,7 @@ impl<D: DataMut> GLWE<D> {
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GLWETrace<BE>, M: GLWETrace<BE>,
{ {
@@ -58,7 +58,7 @@ impl<D: DataMut> GLWE<D> {
keys: &HashMap<i64, K>, keys: &HashMap<i64, K>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GLWETrace<BE>, M: GLWETrace<BE>,
{ {
@@ -110,7 +110,7 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
self.glwe_copy(res, a); self.glwe_copy(res, a);
@@ -120,7 +120,7 @@ where
fn glwe_trace_inplace<R, K>(&self, res: &mut R, start: usize, end: usize, keys: &HashMap<i64, K>, scratch: &mut Scratch<BE>) fn glwe_trace_inplace<R, K>(&self, res: &mut R, start: usize, end: usize, keys: &HashMap<i64, K>, scratch: &mut Scratch<BE>)
where where
R: GLWEToMut, R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement, K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();

View File

@@ -3,11 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch};
use crate::{ use crate::{
ScratchTakeCore, ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch, keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{ layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey},
AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey,
GLWESwitchingKeyToRef,
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef},
},
}; };
impl AutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
@@ -25,21 +21,21 @@ impl AutomorphismKey<Vec<u8>> {
impl<DataSelf: DataMut> AutomorphismKey<DataSelf> { impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where where
A: AutomorphismKeyToRef, A: GGLWEToRef + GGLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>, B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>, M: GGLWEKeyswitch<BE>,
{ {
module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); module.gglwe_keyswitch(self, a, b, scratch);
} }
pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
where where
A: GLWESwitchingKeyPreparedToRef<BE>, A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>, M: GGLWEKeyswitch<BE>,
{ {
module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); module.gglwe_keyswitch_inplace(self, a, scratch);
} }
} }
@@ -58,21 +54,21 @@ impl GLWESwitchingKey<Vec<u8>> {
impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> { impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where where
A: GLWESwitchingKeyToRef, A: GGLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>, B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>, M: GGLWEKeyswitch<BE>,
{ {
module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); module.gglwe_keyswitch(self, a, b, scratch);
} }
pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
where where
A: GLWESwitchingKeyPreparedToRef<BE>, A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>, M: GGLWEKeyswitch<BE>,
{ {
module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); module.gglwe_keyswitch_inplace(self, a, scratch);
} }
} }
@@ -92,7 +88,7 @@ impl<DataSelf: DataMut> GGLWE<DataSelf> {
pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where where
A: GGLWEToRef, A: GGLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>, B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>, M: GGLWEKeyswitch<BE>,
{ {
@@ -101,7 +97,7 @@ impl<DataSelf: DataMut> GGLWE<DataSelf> {
pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
where where
A: GLWESwitchingKeyPreparedToRef<BE>, A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>, M: GGLWEKeyswitch<BE>,
{ {
@@ -128,12 +124,11 @@ where
where where
R: GGLWEToMut, R: GGLWEToMut,
A: GGLWEToRef, A: GGLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>, B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let a: &GGLWE<&[u8]> = &a.to_ref(); let a: &GGLWE<&[u8]> = &a.to_ref();
let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref();
assert_eq!( assert_eq!(
res.rank_in(), res.rank_in(),
@@ -180,11 +175,10 @@ where
fn gglwe_keyswitch_inplace<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<BE>) fn gglwe_keyswitch_inplace<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<BE>)
where where
R: GGLWEToMut, R: GGLWEToMut,
A: GLWESwitchingKeyPreparedToRef<BE>, A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref();
assert_eq!( assert_eq!(
res.rank_out(), res.rank_out(),

View File

@@ -3,10 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx};
use crate::{ use crate::{
GGSWExpandRows, ScratchTakeCore, GGSWExpandRows, ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch, keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{ layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef},
GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef,
prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef},
},
}; };
impl GGSW<Vec<u8>> { impl GGSW<Vec<u8>> {
@@ -32,7 +29,7 @@ impl<D: DataMut> GGSW<D> {
pub fn keyswitch<M, A, K, T, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>) pub fn keyswitch<M, A, K, T, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
where where
A: GGSWToRef, A: GGSWToRef,
K: GLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGSWKeyswitch<BE>, M: GGSWKeyswitch<BE>,
@@ -42,7 +39,7 @@ impl<D: DataMut> GGSW<D> {
pub fn keyswitch_inplace<M, K, T, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>) pub fn keyswitch_inplace<M, K, T, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
where where
K: GLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: GGSWKeyswitch<BE>, M: GGSWKeyswitch<BE>,
@@ -93,14 +90,15 @@ where
where where
R: GGSWToMut, R: GGSWToMut,
A: GGSWToRef, A: GGSWToRef,
K: GLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
let a: &GGSW<&[u8]> = &a.to_ref(); let a: &GGSW<&[u8]> = &a.to_ref();
assert_eq!(res.ggsw_layout(), a.ggsw_layout()); assert!(res.dnum() <= a.dnum());
assert_eq!(res.dsize(), a.dsize());
for row in 0..a.dnum().into() { for row in 0..a.dnum().into() {
// Key-switch column 0, i.e. // Key-switch column 0, i.e.
@@ -114,7 +112,7 @@ where
fn ggsw_keyswitch_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>) fn ggsw_keyswitch_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
where where
R: GGSWToMut, R: GGSWToMut,
K: GLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>, T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {

View File

@@ -4,15 +4,12 @@ use poulpy_hal::{
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft,
VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes,
}, },
layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, layouts::{Backend, DataMut, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos},
}; };
use crate::{ use crate::{
ScratchTakeCore, ScratchTakeCore,
layouts::{ layouts::{GGLWEInfos, GGLWEPrepared, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos},
GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef},
},
}; };
impl GLWE<Vec<u8>> { impl GLWE<Vec<u8>> {
@@ -31,7 +28,7 @@ impl<D: DataMut> GLWE<D> {
pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where where
A: GLWEToRef, A: GLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>, B: GGLWEPreparedToRef<BE>,
M: GLWEKeyswitch<BE>, M: GLWEKeyswitch<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
@@ -40,7 +37,7 @@ impl<D: DataMut> GLWE<D> {
pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
where where
A: GLWESwitchingKeyPreparedToRef<BE>, A: GGLWEPreparedToRef<BE>,
M: GLWEKeyswitch<BE>, M: GLWEKeyswitch<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
@@ -129,12 +126,12 @@ where
where where
R: GLWEToMut, R: GLWEToMut,
A: GLWEToRef, A: GLWEToRef,
K: GLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWE<&[u8]> = &a.to_ref(); let a: &GLWE<&[u8]> = &a.to_ref();
let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); let b: &GGLWEPrepared<&[u8], BE> = &key.to_ref();
assert_eq!( assert_eq!(
a.rank(), a.rank(),
@@ -184,11 +181,11 @@ where
fn glwe_keyswitch_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) fn glwe_keyswitch_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where where
R: GLWEToMut, R: GLWEToMut,
K: GLWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); let a: &GGLWEPrepared<&[u8], BE> = &key.to_ref();
assert_eq!( assert_eq!(
res.rank(), res.rank(),
@@ -239,17 +236,17 @@ impl GLWE<Vec<u8>> {}
impl<DataSelf: DataMut> GLWE<DataSelf> {} impl<DataSelf: DataMut> GLWE<DataSelf> {}
pub(crate) fn keyswitch_internal<BE: Backend, M, DR, DA, DB>( pub(crate) fn keyswitch_internal<BE: Backend, M, DR, A, K>(
module: &M, module: &M,
mut res: VecZnxDft<DR, BE>, mut res: VecZnxDft<DR, BE>,
a: &GLWE<DA>, a: &A,
key: &GLWESwitchingKeyPrepared<DB, BE>, key: &K,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) -> VecZnxBig<DR, BE> ) -> VecZnxBig<DR, BE>
where where
DR: DataMut, DR: DataMut,
DA: DataRef, A: GLWEToRef,
DB: DataRef, K: GGLWEPreparedToRef<BE>,
M: ModuleN M: ModuleN
+ VecZnxDftBytesOf + VecZnxDftBytesOf
+ VmpApplyDftToDftTmpBytes + VmpApplyDftToDftTmpBytes
@@ -264,11 +261,14 @@ where
+ VecZnxNormalize<BE>, + VecZnxNormalize<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let a: &GLWE<&[u8]> = &a.to_ref();
let key: &GGLWEPrepared<&[u8], BE> = &key.to_ref();
let base2k_in: usize = a.base2k().into(); let base2k_in: usize = a.base2k().into();
let base2k_out: usize = key.base2k().into(); let base2k_out: usize = key.base2k().into();
let cols: usize = (a.rank() + 1).into(); let cols: usize = (a.rank() + 1).into();
let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out); let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out);
let pmat: &VmpPMat<DB, BE> = &key.key.data; let pmat: &VmpPMat<&[u8], BE> = &key.data;
if key.dsize() == 1 { if key.dsize() == 1 {
let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size()); let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size());

View File

@@ -6,10 +6,7 @@ use poulpy_hal::{
use crate::{ use crate::{
LWESampleExtract, ScratchTakeCore, LWESampleExtract, ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch, keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{ layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision},
GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision,
prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef},
},
}; };
impl LWE<Vec<u8>> { impl LWE<Vec<u8>> {
@@ -28,7 +25,7 @@ impl<D: DataMut> LWE<D> {
pub fn keyswitch<M, A, K, BE: Backend>(&mut self, module: &M, a: &A, ksk: &K, scratch: &mut Scratch<BE>) pub fn keyswitch<M, A, K, BE: Backend>(&mut self, module: &M, a: &A, ksk: &K, scratch: &mut Scratch<BE>)
where where
A: LWEToRef, A: LWEToRef,
K: LWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M: LWEKeySwitch<BE>, M: LWEKeySwitch<BE>,
{ {
@@ -36,7 +33,7 @@ impl<D: DataMut> LWE<D> {
} }
} }
impl<BE: Backend> LWEKeySwitch<BE> for Module<BE> where Self: LWEKeySwitch<BE> {} impl<BE: Backend> LWEKeySwitch<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + LWESampleExtract {}
pub trait LWEKeySwitch<BE: Backend> pub trait LWEKeySwitch<BE: Backend>
where where
@@ -75,12 +72,11 @@ where
where where
R: LWEToMut, R: LWEToMut,
A: LWEToRef, A: LWEToRef,
K: LWESwitchingKeyPreparedToRef<BE>, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
let a: &LWE<&[u8]> = &a.to_ref(); let a: &LWE<&[u8]> = &a.to_ref();
let ksk: &LWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref();
assert!(res.n().as_usize() <= self.n()); assert!(res.n().as_usize() <= self.n());
assert!(a.n().as_usize() <= self.n()); assert!(a.n().as_usize() <= self.n());
@@ -120,7 +116,7 @@ where
glwe_in.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); glwe_in.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]);
} }
self.glwe_keyswitch(&mut glwe_out, &glwe_in, &ksk.0, scratch_1); self.glwe_keyswitch(&mut glwe_out, &glwe_in, ksk, scratch_1);
self.lwe_sample_extract(res, &glwe_out); self.lwe_sample_extract(res, &glwe_out);
} }
} }

View File

@@ -4,10 +4,9 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut,
compressed::{ GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWECompressed, GLWECompressedToMut, GLWECompressedToRef,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, GLWEDecompress, GLWEInfos, LWEInfos, Rank, TorusPrecision,
},
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -15,7 +14,7 @@ use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct AutomorphismKeyCompressed<D: Data> { pub struct AutomorphismKeyCompressed<D: Data> {
pub(crate) key: GLWESwitchingKeyCompressed<D>, pub(crate) key: GGLWECompressed<D>,
pub(crate) p: i64, pub(crate) p: i64,
} }
@@ -102,7 +101,7 @@ impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
AutomorphismKeyCompressed { AutomorphismKeyCompressed {
key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), key: GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
@@ -122,7 +121,7 @@ impl AutomorphismKeyCompressed<Vec<u8>> {
} }
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize) GGLWECompressed::bytes_of(n, base2k, k, rank, dnum, dsize)
} }
} }
@@ -142,19 +141,19 @@ impl<D: DataRef> WriterTo for AutomorphismKeyCompressed<D> {
pub trait AutomorphismKeyDecompress pub trait AutomorphismKeyDecompress
where where
Self: GLWESwitchingKeyDecompress, Self: GGLWEDecompress,
{ {
fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O) fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O)
where where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, R: GGLWEToMut + SetAutomorphismGaloisElement,
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, O: GGLWECompressedToRef + GetAutomorphismGaloisElement,
{ {
self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); self.decompress_gglwe(res, other);
res.set_p(other.p()); res.set_p(other.p());
} }
} }
impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {} impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWEDecompress {}
impl<D: DataMut> AutomorphismKey<D> impl<D: DataMut> AutomorphismKey<D>
where where
@@ -162,20 +161,32 @@ where
{ {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where where
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, O: GGLWECompressedToRef + GetAutomorphismGaloisElement,
M: AutomorphismKeyDecompress, M: AutomorphismKeyDecompress,
{ {
module.decompress_automorphism_key(self, other); module.decompress_automorphism_key(self, other);
} }
} }
impl<D: DataRef> GGLWECompressedToRef for AutomorphismKeyCompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
self.key.to_ref()
}
}
impl<D: DataMut> GGLWECompressedToMut for AutomorphismKeyCompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
self.key.to_mut()
}
}
pub trait AutomorphismKeyCompressedToRef { pub trait AutomorphismKeyCompressedToRef {
fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>;
} }
impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D> impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D>
where where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef, GLWECompressed<D>: GLWECompressedToRef,
{ {
fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> {
AutomorphismKeyCompressed { AutomorphismKeyCompressed {
@@ -191,7 +202,7 @@ pub trait AutomorphismKeyCompressedToMut {
impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D> impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D>
where where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut, GLWECompressed<D>: GLWECompressedToMut,
{ {
fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> {
AutomorphismKeyCompressed { AutomorphismKeyCompressed {
@@ -200,3 +211,15 @@ where
} }
} }
} }
impl<D: DataMut> GGLWECompressedSeedMut for AutomorphismKeyCompressed<D> {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> {
&mut self.key.seed
}
}
impl<D: DataMut> SetAutomorphismGaloisElement for AutomorphismKeyCompressed<D> {
fn set_p(&mut self, p: i64) {
self.p = p
}
}

View File

@@ -258,7 +258,8 @@ where
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let other: &GGLWECompressed<&[u8]> = &other.to_ref(); let other: &GGLWECompressed<&[u8]> = &other.to_ref();
assert_eq!(res.gglwe_layout(), other.gglwe_layout()); assert_eq!(res.dsize(), other.dsize());
assert!(res.dnum() <= other.dnum());
let rank_in: usize = res.rank_in().into(); let rank_in: usize = res.rank_in().into();
let dnum: usize = res.dnum().into(); let dnum: usize = res.dnum().into();

View File

@@ -4,8 +4,8 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWECompressedSeedMut, GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESwitchingKey,
LWEInfos, Rank, TorusPrecision, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -14,8 +14,34 @@ use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GLWESwitchingKeyCompressed<D: Data> { pub struct GLWESwitchingKeyCompressed<D: Data> {
pub(crate) key: GGLWECompressed<D>, pub(crate) key: GGLWECompressed<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) input_degree: Degree, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) output_degree: Degree, // Degree of sk_out
}
impl<D: DataMut> GGLWECompressedSeedMut for GLWESwitchingKeyCompressed<D> {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> {
&mut self.key.seed
}
}
impl<D: DataRef> GLWESwitchingKeyDegrees for GLWESwitchingKeyCompressed<D> {
fn output_degree(&self) -> &Degree {
&self.output_degree
}
fn input_degree(&self) -> &Degree {
&self.input_degree
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWESwitchingKeyCompressed<D> {
fn output_degree(&mut self) -> &mut Degree {
&mut self.output_degree
}
fn input_degree(&mut self) -> &mut Degree {
&mut self.input_degree
}
} }
impl<D: Data> LWEInfos for GLWESwitchingKeyCompressed<D> { impl<D: Data> LWEInfos for GLWESwitchingKeyCompressed<D> {
@@ -76,7 +102,7 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKeyCompressed<D> {
write!( write!(
f, f,
"(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}", "(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}",
self.sk_in_n, self.sk_out_n, self.key.data self.input_degree, self.output_degree, self.key.data
) )
} }
} }
@@ -100,8 +126,8 @@ impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
GLWESwitchingKeyCompressed { GLWESwitchingKeyCompressed {
key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0, input_degree: Degree(0),
sk_out_n: 0, output_degree: Degree(0),
} }
} }
@@ -120,16 +146,16 @@ where {
impl<D: DataMut> ReaderFrom for GLWESwitchingKeyCompressed<D> { impl<D: DataMut> ReaderFrom for GLWESwitchingKeyCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize; self.input_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize; self.output_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.key.read_from(reader) self.key.read_from(reader)
} }
} }
impl<D: DataRef> WriterTo for GLWESwitchingKeyCompressed<D> { impl<D: DataRef> WriterTo for GLWESwitchingKeyCompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?; writer.write_u32::<LittleEndian>(self.input_degree.into())?;
writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?; writer.write_u32::<LittleEndian>(self.output_degree.into())?;
self.key.write_to(writer) self.key.write_to(writer)
} }
} }
@@ -140,13 +166,13 @@ where
{ {
fn decompress_glwe_switching_key<R, O>(&self, res: &mut R, other: &O) fn decompress_glwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where where
R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData, R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: GLWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{ {
let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref(); self.decompress_gglwe(res, other);
self.decompress_gglwe(&mut res.to_mut().key, &other.key);
res.set_sk_in_n(other.sk_in_n); *res.input_degree() = *other.input_degree();
res.set_sk_out_n(other.sk_out_n); *res.output_degree() = *other.output_degree();
} }
} }
@@ -155,43 +181,21 @@ impl<B: Backend> GLWESwitchingKeyDecompress for Module<B> where Self: GGLWEDecom
impl<D: DataMut> GLWESwitchingKey<D> { impl<D: DataMut> GLWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where where
O: GLWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: GLWESwitchingKeyDecompress, M: GLWESwitchingKeyDecompress,
{ {
module.decompress_glwe_switching_key(self, other); module.decompress_glwe_switching_key(self, other);
} }
} }
pub trait GLWESwitchingKeyCompressedToMut { impl<D: DataMut> GGLWECompressedToMut for GLWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
} self.key.to_mut()
impl<D: DataMut> GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed<D>
where
GGLWECompressed<D>: GGLWECompressedToMut,
{
fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> {
GLWESwitchingKeyCompressed {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_mut(),
}
} }
} }
pub trait GLWESwitchingKeyCompressedToRef { impl<D: DataRef> GGLWECompressedToRef for GLWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>; fn to_ref(&self) -> GGLWECompressed<&[u8]> {
} self.key.to_ref()
impl<D: DataRef> GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed<D>
where
GGLWECompressed<D>: GGLWECompressedToRef,
{
fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> {
GLWESwitchingKeyCompressed {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_ref(),
}
} }
} }

View File

@@ -4,17 +4,15 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos,
compressed::{ GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct TensorKeyCompressed<D: Data> { pub struct TensorKeyCompressed<D: Data> {
pub(crate) keys: Vec<GLWESwitchingKeyCompressed<D>>, pub(crate) keys: Vec<GGLWECompressed<D>>,
} }
impl<D: Data> LWEInfos for TensorKeyCompressed<D> { impl<D: Data> LWEInfos for TensorKeyCompressed<D> {
@@ -67,7 +65,7 @@ impl<D: DataMut> FillUniform for TensorKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys self.keys
.iter_mut() .iter_mut()
.for_each(|key: &mut GLWESwitchingKeyCompressed<D>| key.fill_uniform(log_bound, source)) .for_each(|key: &mut GGLWECompressed<D>| key.fill_uniform(log_bound, source))
} }
} }
@@ -100,7 +98,7 @@ impl TensorKeyCompressed<Vec<u8>> {
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyCompressed { TensorKeyCompressed {
keys: (0..pairs) keys: (0..pairs)
.map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .map(|_| GGLWECompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(), .collect(),
} }
} }
@@ -121,7 +119,7 @@ impl TensorKeyCompressed<Vec<u8>> {
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize;
pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) pairs * GGLWECompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize)
} }
} }
@@ -151,8 +149,26 @@ impl<D: DataRef> WriterTo for TensorKeyCompressed<D> {
} }
} }
impl<D: DataMut> TensorKeyCompressed<D> { pub trait TensorKeyCompressedAtRef<D: DataRef> {
pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed<D> { fn at(&self, i: usize, j: usize) -> &GGLWECompressed<D>;
}
impl<D: DataRef> TensorKeyCompressedAtRef<D> for TensorKeyCompressed<D> {
fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
let rank: usize = self.rank_out().into();
&self.keys[i * rank + j - (i * (i + 1) / 2)]
}
}
pub trait TensorKeyCompressedAtMut<D: DataMut> {
fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed<D>;
}
impl<D: DataMut> TensorKeyCompressedAtMut<D> for TensorKeyCompressed<D> {
fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed<D> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -163,7 +179,7 @@ impl<D: DataMut> TensorKeyCompressed<D> {
pub trait TensorKeyDecompress pub trait TensorKeyDecompress
where where
Self: GLWESwitchingKeyDecompress, Self: GGLWEDecompress,
{ {
fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O) fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O)
where where
@@ -182,12 +198,12 @@ where
); );
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.decompress_glwe_switching_key(a, b); self.decompress_gglwe(a, b);
} }
} }
} }
impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {} impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GGLWEDecompress {}
impl<D: DataMut> TensorKey<D> { impl<D: DataMut> TensorKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)
@@ -205,7 +221,7 @@ pub trait TensorKeyCompressedToMut {
impl<D: DataMut> TensorKeyCompressedToMut for TensorKeyCompressed<D> impl<D: DataMut> TensorKeyCompressedToMut for TensorKeyCompressed<D>
where where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut, GGLWECompressed<D>: GGLWECompressedToMut,
{ {
fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> {
TensorKeyCompressed { TensorKeyCompressed {
@@ -220,7 +236,7 @@ pub trait TensorKeyCompressedToRef {
impl<D: DataRef> TensorKeyCompressedToRef for TensorKeyCompressed<D> impl<D: DataRef> TensorKeyCompressedToRef for TensorKeyCompressed<D>
where where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef, GGLWECompressed<D>: GGLWECompressedToRef,
{ {
fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { fn to_ref(&self) -> TensorKeyCompressed<&[u8]> {
TensorKeyCompressed { TensorKeyCompressed {

View File

@@ -138,7 +138,7 @@ impl GGSWCompressed<Vec<u8>> {
base2k, base2k,
dsize, dsize,
rank, rank,
seed: Vec::new(), seed: vec![[0u8; 32]; dnum.as_usize() * (rank.as_usize() + 1)],
} }
} }
@@ -260,7 +260,7 @@ where
} }
} }
impl<B: Backend> GGSWDecompress for Module<B> where Self: GGSWDecompress {} impl<B: Backend> GGSWDecompress for Module<B> where Self: GLWEDecompress {}
impl<D: DataMut> GGSW<D> { impl<D: DataMut> GGSW<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)

View File

@@ -158,7 +158,6 @@ where
self.ring_degree() self.ring_degree()
); );
assert_eq!(res.lwe_layout(), other.lwe_layout());
assert_eq!(res.glwe_layout(), other.glwe_layout()); assert_eq!(res.glwe_layout(), other.glwe_layout());
let mut source: Source = Source::new(other.seed); let mut source: Source = Source::new(other.seed);

View File

@@ -6,11 +6,9 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
TorusPrecision, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision,
compressed::{ compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
}; };
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
@@ -151,10 +149,10 @@ where
{ {
fn decompress_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O) fn decompress_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where where
R: GLWEToLWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: GLWEToLWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{ {
self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); self.decompress_glwe_switching_key(res, other);
} }
} }
@@ -163,35 +161,21 @@ impl<B: Backend> GLWEToLWESwitchingKeyDecompress for Module<B> where Self: GLWES
impl<D: DataMut> GLWEToLWESwitchingKey<D> { impl<D: DataMut> GLWEToLWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where where
O: GLWEToLWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: GLWEToLWESwitchingKeyDecompress, M: GLWEToLWESwitchingKeyDecompress,
{ {
module.decompress_glwe_to_lwe_switching_key(self, other); module.decompress_glwe_to_lwe_switching_key(self, other);
} }
} }
pub trait GLWEToLWESwitchingKeyCompressedToRef { impl<D: DataRef> GGLWECompressedToRef for GLWEToLWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; fn to_ref(&self) -> GGLWECompressed<&[u8]> {
} self.0.to_ref()
impl<D: DataRef> GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{
fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> {
GLWEToLWESwitchingKeyCompressed(self.0.to_ref())
} }
} }
pub trait GLWEToLWESwitchingKeyCompressedToMut { impl<D: DataMut> GGLWECompressedToMut for GLWEToLWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
} self.0.to_mut()
impl<D: DataMut> GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> {
GLWEToLWESwitchingKeyCompressed(self.0.to_mut())
} }
} }

View File

@@ -4,10 +4,9 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
compressed::{ GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWESwitchingKey, Rank, TorusPrecision,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
},
}; };
use std::fmt; use std::fmt;
@@ -152,10 +151,10 @@ where
{ {
fn decompress_lwe_switching_key<R, O>(&self, res: &mut R, other: &O) fn decompress_lwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where where
R: LWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: LWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{ {
self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); self.decompress_glwe_switching_key(res, other);
} }
} }
@@ -164,35 +163,21 @@ impl<B: Backend> LWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchi
impl<D: DataMut> LWESwitchingKey<D> { impl<D: DataMut> LWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where where
O: LWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: LWESwitchingKeyDecompress, M: LWESwitchingKeyDecompress,
{ {
module.decompress_lwe_switching_key(self, other); module.decompress_lwe_switching_key(self, other);
} }
} }
pub trait LWESwitchingKeyCompressedToRef { impl<D: DataRef> GGLWECompressedToRef for LWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; fn to_ref(&self) -> GGLWECompressed<&[u8]> {
} self.0.to_ref()
impl<D: DataRef> LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{
fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> {
LWESwitchingKeyCompressed(self.0.to_ref())
} }
} }
pub trait LWESwitchingKeyCompressedToMut { impl<D: DataMut> GGLWECompressedToMut for LWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
} self.0.to_mut()
impl<D: DataMut> LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{
fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> {
LWESwitchingKeyCompressed(self.0.to_mut())
} }
} }

View File

@@ -4,11 +4,9 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
TorusPrecision, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision,
compressed::{ compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
}; };
use std::fmt; use std::fmt;
@@ -149,10 +147,10 @@ where
{ {
fn decompress_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O) fn decompress_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where where
R: LWEToGLWESwitchingKeyToMut, R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: LWEToGLWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{ {
self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); self.decompress_glwe_switching_key(res, other);
} }
} }
@@ -161,35 +159,21 @@ impl<B: Backend> LWEToGLWESwitchingKeyDecompress for Module<B> where Self: GLWES
impl<D: DataMut> LWEToGLWESwitchingKey<D> { impl<D: DataMut> LWEToGLWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O) pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where where
O: LWEToGLWESwitchingKeyCompressedToRef, O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: LWEToGLWESwitchingKeyDecompress, M: LWEToGLWESwitchingKeyDecompress,
{ {
module.decompress_lwe_to_glwe_switching_key(self, other); module.decompress_lwe_to_glwe_switching_key(self, other);
} }
} }
pub trait LWEToGLWESwitchingKeyCompressedToRef { impl<D: DataRef> GGLWECompressedToRef for LWEToGLWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; fn to_ref(&self) -> GGLWECompressed<&[u8]> {
} self.0.to_ref()
impl<D: DataRef> LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{
fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> {
LWEToGLWESwitchingKeyCompressed(self.0.to_ref())
} }
} }
pub trait LWEToGLWESwitchingKeyCompressedToMut { impl<D: DataMut> GGLWECompressedToMut for LWEToGLWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
} self.0.to_mut()
impl<D: DataMut> LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> {
LWEToGLWESwitchingKeyCompressed(self.0.to_mut())
} }
} }

View File

@@ -4,8 +4,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision,
LWEInfos, Rank, TorusPrecision,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -24,7 +23,7 @@ pub struct AutomorphismKeyLayout {
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct AutomorphismKey<D: Data> { pub struct AutomorphismKey<D: Data> {
pub(crate) key: GLWESwitchingKey<D>, pub(crate) key: GGLWE<D>,
pub(crate) p: i64, pub(crate) p: i64,
} }
@@ -161,7 +160,7 @@ impl AutomorphismKey<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
AutomorphismKey { AutomorphismKey {
key: GLWESwitchingKey::alloc(n, base2k, k, rank, rank, dnum, dsize), key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
@@ -186,39 +185,19 @@ impl AutomorphismKey<Vec<u8>> {
} }
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize) GGLWE::bytes_of(n, base2k, k, rank, rank, dnum, dsize)
} }
} }
pub trait AutomorphismKeyToMut { impl<D: DataMut> GGLWEToMut for AutomorphismKey<D> {
fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>; fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
} self.key.to_mut()
impl<D: DataMut> AutomorphismKeyToMut for AutomorphismKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> {
AutomorphismKey {
key: self.key.to_mut(),
p: self.p,
}
} }
} }
pub trait AutomorphismKeyToRef { impl<D: DataMut> GGLWEToRef for AutomorphismKey<D> {
fn to_ref(&self) -> AutomorphismKey<&[u8]>; fn to_ref(&self) -> GGLWE<&[u8]> {
} self.key.to_ref()
impl<D: DataRef> AutomorphismKeyToRef for AutomorphismKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> AutomorphismKey<&[u8]> {
AutomorphismKey {
p: self.p,
key: self.key.to_ref(),
}
} }
} }

View File

@@ -62,37 +62,37 @@ impl GGLWEInfos for GLWESwitchingKeyLayout {
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GLWESwitchingKey<D: Data> { pub struct GLWESwitchingKey<D: Data> {
pub(crate) key: GGLWE<D>, pub(crate) key: GGLWE<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) input_degree: Degree, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) output_degree: Degree, // Degree of sk_out
} }
pub trait GLWESwitchingKeySetMetaData { pub trait GLWESwitchingKeyDegrees {
fn set_sk_in_n(&mut self, sk_in_n: usize); fn input_degree(&self) -> &Degree;
fn set_sk_out_n(&mut self, sk_out_n: usize); fn output_degree(&self) -> &Degree;
} }
impl<D: DataMut> GLWESwitchingKeySetMetaData for GLWESwitchingKey<D> { impl<D: DataRef> GLWESwitchingKeyDegrees for GLWESwitchingKey<D> {
fn set_sk_in_n(&mut self, sk_in_n: usize) { fn output_degree(&self) -> &Degree {
self.sk_in_n = sk_in_n &self.output_degree
} }
fn set_sk_out_n(&mut self, sk_out_n: usize) { fn input_degree(&self) -> &Degree {
self.sk_out_n = sk_out_n &self.input_degree
} }
} }
pub trait GLWESwtichingKeyGetMetaData { pub trait GLWESwitchingKeyDegreesMut {
fn sk_in_n(&self) -> usize; fn input_degree(&mut self) -> &mut Degree;
fn sk_out_n(&self) -> usize; fn output_degree(&mut self) -> &mut Degree;
} }
impl<D: DataRef> GLWESwtichingKeyGetMetaData for GLWESwitchingKey<D> { impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWESwitchingKey<D> {
fn sk_in_n(&self) -> usize { fn output_degree(&mut self) -> &mut Degree {
self.sk_in_n &mut self.output_degree
} }
fn sk_out_n(&self) -> usize { fn input_degree(&mut self) -> &mut Degree {
self.sk_out_n &mut self.input_degree
} }
} }
@@ -149,8 +149,8 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKey<D> {
write!( write!(
f, f,
"(GLWESwitchingKey: sk_in_n={} sk_out_n={}) {}", "(GLWESwitchingKey: sk_in_n={} sk_out_n={}) {}",
self.sk_in_n, self.input_degree,
self.sk_out_n, self.output_degree,
self.key.data() self.key.data()
) )
} }
@@ -181,8 +181,8 @@ impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
GLWESwitchingKey { GLWESwitchingKey {
key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0, input_degree: Degree(0),
sk_out_n: 0, output_degree: Degree(0),
} }
} }
@@ -214,37 +214,15 @@ impl GLWESwitchingKey<Vec<u8>> {
} }
} }
pub trait GLWESwitchingKeyToMut { impl<D: DataMut> GGLWEToMut for GLWESwitchingKey<D> {
fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
} self.key.to_mut()
impl<D: DataMut> GLWESwitchingKeyToMut for GLWESwitchingKey<D>
where
GGLWE<D>: GGLWEToMut,
{
fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> {
GLWESwitchingKey {
key: self.key.to_mut(),
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
}
} }
} }
pub trait GLWESwitchingKeyToRef { impl<D: DataRef> GGLWEToRef for GLWESwitchingKey<D> {
fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; fn to_ref(&self) -> GGLWE<&[u8]> {
} self.key.to_ref()
impl<D: DataRef> GLWESwitchingKeyToRef for GLWESwitchingKey<D>
where
GGLWE<D>: GGLWEToRef,
{
fn to_ref(&self) -> GLWESwitchingKey<&[u8]> {
GLWESwitchingKey {
key: self.key.to_ref(),
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
}
} }
} }
@@ -262,16 +240,16 @@ impl<D: DataMut> GLWESwitchingKey<D> {
impl<D: DataMut> ReaderFrom for GLWESwitchingKey<D> { impl<D: DataMut> ReaderFrom for GLWESwitchingKey<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize; self.input_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize; self.output_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.key.read_from(reader) self.key.read_from(reader)
} }
} }
impl<D: DataRef> WriterTo for GLWESwitchingKey<D> { impl<D: DataRef> WriterTo for GLWESwitchingKey<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?; writer.write_u32::<LittleEndian>(self.input_degree.into())?;
writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?; writer.write_u32::<LittleEndian>(self.output_degree.into())?;
self.key.write_to(writer) self.key.write_to(writer)
} }
} }

View File

@@ -4,8 +4,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
Rank, TorusPrecision,
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -23,7 +22,7 @@ pub struct TensorKeyLayout {
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct TensorKey<D: Data> { pub struct TensorKey<D: Data> {
pub(crate) keys: Vec<GLWESwitchingKey<D>>, pub(crate) keys: Vec<GGLWE<D>>,
} }
impl<D: Data> LWEInfos for TensorKey<D> { impl<D: Data> LWEInfos for TensorKey<D> {
@@ -116,7 +115,7 @@ impl<D: DataMut> FillUniform for TensorKey<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys self.keys
.iter_mut() .iter_mut()
.for_each(|key: &mut GLWESwitchingKey<D>| key.fill_uniform(log_bound, source)) .for_each(|key: &mut GGLWE<D>| key.fill_uniform(log_bound, source))
} }
} }
@@ -154,7 +153,7 @@ impl TensorKey<Vec<u8>> {
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
TensorKey { TensorKey {
keys: (0..pairs) keys: (0..pairs)
.map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(), .collect(),
} }
} }
@@ -180,13 +179,13 @@ impl TensorKey<Vec<u8>> {
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize;
pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) pairs * GGLWE::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize)
} }
} }
impl<D: DataMut> TensorKey<D> { impl<D: DataMut> TensorKey<D> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey<D> { pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE<D> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -196,8 +195,8 @@ impl<D: DataMut> TensorKey<D> {
} }
impl<D: DataRef> TensorKey<D> { impl<D: DataRef> TensorKey<D> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a reference to GGLWE_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey<D> { pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE<D> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -238,7 +237,7 @@ pub trait TensorKeyToRef {
impl<D: DataRef> TensorKeyToRef for TensorKey<D> impl<D: DataRef> TensorKeyToRef for TensorKey<D>
where where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef, GGLWE<D>: GGLWEToRef,
{ {
fn to_ref(&self) -> TensorKey<&[u8]> { fn to_ref(&self) -> TensorKey<&[u8]> {
TensorKey { TensorKey {
@@ -253,7 +252,7 @@ pub trait TensorKeyToMut {
impl<D: DataMut> TensorKeyToMut for TensorKey<D> impl<D: DataMut> TensorKeyToMut for TensorKey<D>
where where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut, GGLWE<D>: GGLWEToMut,
{ {
fn to_mut(&mut self) -> TensorKey<&mut [u8]> { fn to_mut(&mut self) -> TensorKey<&mut [u8]> {
TensorKey { TensorKey {

View File

@@ -4,8 +4,8 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
Rank, TorusPrecision, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
}; };
use std::fmt; use std::fmt;
@@ -196,28 +196,24 @@ impl GLWEToLWESwitchingKey<Vec<u8>> {
} }
} }
pub trait GLWEToLWESwitchingKeyToRef { impl<D: DataRef> GGLWEToRef for GLWEToLWESwitchingKey<D> {
fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>; fn to_ref(&self) -> GGLWE<&[u8]> {
} self.0.to_ref()
impl<D: DataRef> GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> {
GLWEToLWESwitchingKey(self.0.to_ref())
} }
} }
pub trait GLWEToLWESwitchingKeyToMut { impl<D: DataMut> GGLWEToMut for GLWEToLWESwitchingKey<D> {
fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>; fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
} self.0.to_mut()
}
impl<D: DataMut> GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey<D> }
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut, impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey<D> {
{ fn input_degree(&mut self) -> &mut Degree {
fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> { &mut self.0.input_degree
GLWEToLWESwitchingKey(self.0.to_mut()) }
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
} }
} }

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
Rank, TorusPrecision, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
}; };
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -193,28 +193,34 @@ impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
} }
} }
pub trait LWESwitchingKeyToRef { impl<D: DataRef> GGLWEToRef for LWESwitchingKey<D> {
fn to_ref(&self) -> LWESwitchingKey<&[u8]>; fn to_ref(&self) -> GGLWE<&[u8]> {
} self.0.to_ref()
impl<D: DataRef> LWESwitchingKeyToRef for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWESwitchingKey<&[u8]> {
LWESwitchingKey(self.0.to_ref())
} }
} }
pub trait LWESwitchingKeyToMut { impl<D: DataMut> GGLWEToMut for LWESwitchingKey<D> {
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
} self.0.to_mut()
}
impl<D: DataMut> LWESwitchingKeyToMut for LWESwitchingKey<D> }
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut, impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWESwitchingKey<D> {
{ fn input_degree(&mut self) -> &mut Degree {
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { &mut self.0.input_degree
LWESwitchingKey(self.0.to_mut()) }
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}
impl<D: DataRef> GLWESwitchingKeyDegrees for LWESwitchingKey<D> {
fn input_degree(&self) -> &Degree {
&self.0.input_degree
}
fn output_degree(&self) -> &Degree {
&self.0.output_degree
} }
} }

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
Rank, TorusPrecision, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
}; };
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -196,28 +196,24 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
} }
} }
pub trait LWEToGLWESwitchingKeyToRef { impl<D: DataRef> GGLWEToRef for LWEToGLWESwitchingKey<D> {
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>; fn to_ref(&self) -> GGLWE<&[u8]> {
} self.0.to_ref()
impl<D: DataRef> LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> {
LWEToGLWESwitchingKey(self.0.to_ref())
} }
} }
pub trait LWEToGLWESwitchingKeyToMut { impl<D: DataMut> GGLWEToMut for LWEToGLWESwitchingKey<D> {
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>; fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
} self.0.to_mut()
}
impl<D: DataMut> LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey<D> }
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut, impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey<D> {
{ fn input_degree(&mut self) -> &mut Degree {
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> { &mut self.0.input_degree
LWEToGLWESwitchingKey(self.0.to_mut()) }
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
} }
} }

View File

@@ -1,16 +1,13 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{ use crate::layouts::{
AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
prepared::{ GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct AutomorphismKeyPrepared<D: Data, B: Backend> { pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GLWESwitchingKeyPrepared<D, B>, pub(crate) key: GGLWEPrepared<D, B>,
pub(crate) p: i64, pub(crate) p: i64,
} }
@@ -78,7 +75,7 @@ impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
pub trait AutomorphismKeyPreparedAlloc<B: Backend> pub trait AutomorphismKeyPreparedAlloc<B: Backend>
where where
Self: GLWESwitchingKeyPreparedAlloc<B>, Self: GGLWEPreparedAlloc<B>,
{ {
fn alloc_automorphism_key_prepared( fn alloc_automorphism_key_prepared(
&self, &self,
@@ -89,7 +86,7 @@ where
dsize: Dsize, dsize: Dsize,
) -> AutomorphismKeyPrepared<Vec<u8>, B> { ) -> AutomorphismKeyPrepared<Vec<u8>, B> {
AutomorphismKeyPrepared::<Vec<u8>, B> { AutomorphismKeyPrepared::<Vec<u8>, B> {
key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize), key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
@@ -120,7 +117,7 @@ where
dnum: Dnum, dnum: Dnum,
dsize: Dsize, dsize: Dsize,
) -> usize { ) -> usize {
self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize) self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize)
} }
fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize
@@ -130,7 +127,7 @@ where
assert_eq!( assert_eq!(
infos.rank_in(), infos.rank_in(),
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
); );
self.bytes_of_automorphism_key_prepared( self.bytes_of_automorphism_key_prepared(
infos.base2k(), infos.base2k(),
@@ -142,7 +139,7 @@ where
} }
} }
impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {} impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> { impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
@@ -178,26 +175,26 @@ impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub trait PrepareAutomorphismKey<B: Backend> pub trait PrepareAutomorphismKey<B: Backend>
where where
Self: GLWESwitchingKeyPrepare<B>, Self: GGLWEPrepare<B>,
{ {
fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.prepare_glwe_switching_key_tmp_bytes(infos) self.prepare_gglwe_tmp_bytes(infos)
} }
fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where where
R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement, R: GGLWEPreparedToMut<B> + SetAutomorphismGaloisElement,
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, O: GGLWEToRef + GetAutomorphismGaloisElement,
{ {
self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); self.prepare_gglwe(res, other, scratch);
res.set_p(other.p()); res.set_p(other.p());
} }
} }
impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GLWESwitchingKeyPrepare<B> {} impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GGLWEPrepare<B> {}
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> { impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
@@ -211,35 +208,21 @@ impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> { impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where where
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, O: GGLWEToRef + GetAutomorphismGaloisElement,
M: PrepareAutomorphismKey<B>, M: PrepareAutomorphismKey<B>,
{ {
module.prepare_automorphism_key(self, other, scratch); module.prepare_automorphism_key(self, other, scratch);
} }
} }
pub trait AutomorphismKeyPreparedToMut<B: Backend> { impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
} self.key.to_mut()
impl<D: DataMut, B: Backend> AutomorphismKeyPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> {
AutomorphismKeyPrepared {
p: self.p,
key: self.key.to_mut(),
}
} }
} }
pub trait AutomorphismKeyPreparedToRef<B: Backend> { impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for AutomorphismKeyPrepared<D, BE> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
} self.key.to_ref()
impl<D: DataRef, B: Backend> AutomorphismKeyPreparedToRef<B> for AutomorphismKeyPrepared<D, B> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> {
AutomorphismKeyPrepared {
p: self.p,
key: self.key.to_ref(),
}
} }
} }

View File

@@ -1,35 +1,35 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut,
GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, LWEInfos, Rank, TorusPrecision,
prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GLWESwitchingKeyPrepared<D: Data, B: Backend> { pub struct GLWESwitchingKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWEPrepared<D, B>, pub(crate) key: GGLWEPrepared<D, B>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) input_degree: Degree, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) output_degree: Degree, // Degree of sk_out
} }
impl<D: DataMut, B: Backend> GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared<D, B> { impl<D: DataRef, BE: Backend> GLWESwitchingKeyDegrees for GLWESwitchingKeyPrepared<D, BE> {
fn set_sk_in_n(&mut self, sk_in_n: usize) { fn output_degree(&self) -> &Degree {
self.sk_in_n = sk_in_n &self.output_degree
} }
fn set_sk_out_n(&mut self, sk_out_n: usize) { fn input_degree(&self) -> &Degree {
self.sk_out_n = sk_out_n &self.input_degree
} }
} }
impl<D: DataRef, B: Backend> GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared<D, B> { impl<D: DataMut, BE: Backend> GLWESwitchingKeyDegreesMut for GLWESwitchingKeyPrepared<D, BE> {
fn sk_in_n(&self) -> usize { fn output_degree(&mut self) -> &mut Degree {
self.sk_in_n &mut self.output_degree
} }
fn sk_out_n(&self) -> usize { fn input_degree(&mut self) -> &mut Degree {
self.sk_out_n &mut self.input_degree
} }
} }
@@ -90,8 +90,8 @@ where
) -> GLWESwitchingKeyPrepared<Vec<u8>, B> { ) -> GLWESwitchingKeyPrepared<Vec<u8>, B> {
GLWESwitchingKeyPrepared::<Vec<u8>, B> { GLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0, input_degree: Degree(0),
sk_out_n: 0, output_degree: Degree(0),
} }
} }
@@ -199,12 +199,12 @@ where
fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where where
R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeySetMetaData, R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, O: GGLWEToRef + GLWESwitchingKeyDegrees,
{ {
self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); self.prepare_gglwe(res, other, scratch);
res.set_sk_in_n(other.sk_in_n()); *res.input_degree() = *other.input_degree();
res.set_sk_out_n(other.sk_out_n()); *res.output_degree() = *other.output_degree();
} }
} }
@@ -213,7 +213,7 @@ impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B> where Self: GGLWEPrepa
impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> { impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where where
O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: GLWESwitchingKeyPrepare<B>, M: GLWESwitchingKeyPrepare<B>,
{ {
module.prepare_glwe_switching(self, other, scratch); module.prepare_glwe_switching(self, other, scratch);
@@ -229,30 +229,20 @@ impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
} }
} }
pub trait GLWESwitchingKeyPreparedToMut<B: Backend> { impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for GLWESwitchingKeyPrepared<D, BE>
fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; where
} GGLWEPrepared<D, BE>: GGLWEPreparedToRef<BE>,
{
impl<D: DataMut, B: Backend> GLWESwitchingKeyPreparedToMut<B> for GLWESwitchingKeyPrepared<D, B> { fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { self.key.to_ref()
GLWESwitchingKeyPrepared {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_mut(),
}
} }
} }
pub trait GLWESwitchingKeyPreparedToRef<B: Backend> { impl<D: DataRef, BE: Backend> GGLWEPreparedToMut<BE> for GLWESwitchingKeyPrepared<D, BE>
fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; where
} GGLWEPrepared<D, BE>: GGLWEPreparedToMut<BE>,
{
impl<D: DataRef, B: Backend> GLWESwitchingKeyPreparedToRef<B> for GLWESwitchingKeyPrepared<D, B> { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], BE> {
fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { self.key.to_mut()
GLWESwitchingKeyPrepared {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_ref(),
}
} }
} }

View File

@@ -1,16 +1,13 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
prepared::{ GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct TensorKeyPrepared<D: Data, B: Backend> { pub struct TensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GLWESwitchingKeyPrepared<D, B>>, pub(crate) keys: Vec<GGLWEPrepared<D, B>>,
} }
impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> {
@@ -57,7 +54,7 @@ impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> {
pub trait TensorKeyPreparedAlloc<B: Backend> pub trait TensorKeyPreparedAlloc<B: Backend>
where where
Self: GLWESwitchingKeyPreparedAlloc<B>, Self: GGLWEPreparedAlloc<B>,
{ {
fn alloc_tensor_key_prepared( fn alloc_tensor_key_prepared(
&self, &self,
@@ -70,7 +67,7 @@ where
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyPrepared { TensorKeyPrepared {
keys: (0..pairs) keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize))
.collect(), .collect(),
} }
} }
@@ -82,7 +79,7 @@ where
assert_eq!( assert_eq!(
infos.rank_in(), infos.rank_in(),
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKeyPrepared" "rank_in != rank_out is not supported for TensorKeyPrepared"
); );
self.alloc_tensor_key_prepared( self.alloc_tensor_key_prepared(
infos.base2k(), infos.base2k(),
@@ -95,7 +92,7 @@ where
fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize;
pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) pairs * self.bytes_of_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)
} }
fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize
@@ -112,7 +109,7 @@ where
} }
} }
impl<B: Backend> TensorKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {} impl<B: Backend> TensorKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> { impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
@@ -147,8 +144,8 @@ impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
} }
impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> { impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared<D, B> { pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared<D, B> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -158,8 +155,8 @@ impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
} }
impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> { impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a reference to GGLWE_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared<D, B> { pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared<D, B> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -170,13 +167,13 @@ impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
pub trait TensorKeyPrepare<B: Backend> pub trait TensorKeyPrepare<B: Backend>
where where
Self: GLWESwitchingKeyPrepare<B>, Self: GGLWEPrepare<B>,
{ {
fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.prepare_glwe_switching_key_tmp_bytes(infos) self.prepare_gglwe_tmp_bytes(infos)
} }
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
@@ -190,12 +187,12 @@ where
assert_eq!(res.keys.len(), other.keys.len()); assert_eq!(res.keys.len(), other.keys.len());
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.prepare_glwe_switching(a, b, scratch); self.prepare_gglwe(a, b, scratch);
} }
} }
} }
impl<B: Backend> TensorKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {} impl<B: Backend> TensorKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> { impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
@@ -223,7 +220,7 @@ pub trait TensorKeyPreparedToMut<B: Backend> {
impl<D: DataMut, B: Backend> TensorKeyPreparedToMut<B> for TensorKeyPrepared<D, B> impl<D: DataMut, B: Backend> TensorKeyPreparedToMut<B> for TensorKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>, GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{ {
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> {
TensorKeyPrepared { TensorKeyPrepared {
@@ -238,7 +235,7 @@ pub trait TensorKeyPreparedToRef<B: Backend> {
impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B> impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>, GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{ {
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> {
TensorKeyPrepared { TensorKeyPrepared {

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
prepared::{ GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
GLWESwitchingKeyPreparedToRef,
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
@@ -152,10 +150,10 @@ where
fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where where
R: GLWEToLWESwitchingKeyPreparedToMut<B>, R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GLWEToLWESwitchingKeyToRef, O: GGLWEToRef + GLWESwitchingKeyDegrees,
{ {
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); self.prepare_glwe_switching(res, other, scratch);
} }
} }
@@ -174,35 +172,37 @@ impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> { impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where where
O: GLWEToLWESwitchingKeyToRef, O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: GLWEToLWESwitchingKeyPrepare<B>, M: GLWEToLWESwitchingKeyPrepare<B>,
{ {
module.prepare_glwe_to_lwe_switching_key(self, other, scratch); module.prepare_glwe_to_lwe_switching_key(self, other, scratch);
} }
} }
pub trait GLWEToLWESwitchingKeyPreparedToRef<B: Backend> { impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWEToLWESwitchingKeyPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>, GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{ {
fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> { fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
GLWEToLWESwitchingKeyPrepared(self.0.to_ref()) self.0.to_ref()
} }
} }
pub trait GLWEToLWESwitchingKeyPreparedToMut<B: Backend> { impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>, GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{ {
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
GLWEToLWESwitchingKeyPrepared(self.0.to_mut()) self.0.to_mut()
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
} }
} }

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
prepared::{ GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
GLWESwitchingKeyPreparedToRef,
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
@@ -160,10 +158,10 @@ where
} }
fn prepare_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) fn prepare_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where where
R: LWESwitchingKeyPreparedToMut<B>, R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: LWESwitchingKeyToRef, O: GGLWEToRef + GLWESwitchingKeyDegrees,
{ {
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); self.prepare_glwe_switching(res, other, scratch);
} }
} }
@@ -182,35 +180,37 @@ impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> LWESwitchingKeyPrepared<D, B> { impl<D: DataMut, B: Backend> LWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where where
O: LWESwitchingKeyToRef, O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: LWESwitchingKeyPrepare<B>, M: LWESwitchingKeyPrepare<B>,
{ {
module.prepare_lwe_switching_key(self, other, scratch); module.prepare_lwe_switching_key(self, other, scratch);
} }
} }
pub trait LWESwitchingKeyPreparedToRef<B: Backend> { impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWESwitchingKeyPrepared<D, B>
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> LWESwitchingKeyPreparedToRef<B> for LWESwitchingKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>, GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{ {
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> { fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
LWESwitchingKeyPrepared(self.0.to_ref()) self.0.to_ref()
} }
} }
pub trait LWESwitchingKeyPreparedToMut<B: Backend> { impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWESwitchingKeyPrepared<D, B>
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> LWESwitchingKeyPreparedToMut<B> for LWESwitchingKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>, GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{ {
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
LWESwitchingKeyPrepared(self.0.to_mut()) self.0.to_mut()
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for LWESwitchingKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
} }
} }

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
prepared::{ GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
GLWESwitchingKeyPreparedToRef,
},
}; };
/// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. /// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE].
@@ -159,10 +157,10 @@ where
fn prepare_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) fn prepare_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where where
R: LWEToGLWESwitchingKeyPreparedToMut<B>, R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: LWEToGLWESwitchingKeyToRef, O: GGLWEToRef + GLWESwitchingKeyDegrees,
{ {
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); self.prepare_glwe_switching(res, other, scratch);
} }
} }
@@ -181,35 +179,37 @@ impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> { impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where where
O: LWEToGLWESwitchingKeyToRef, O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: LWEToGLWESwitchingKeyPrepare<B>, M: LWEToGLWESwitchingKeyPrepare<B>,
{ {
module.prepare_lwe_to_glwe_switching_key(self, other, scratch); module.prepare_lwe_to_glwe_switching_key(self, other, scratch);
} }
} }
pub trait LWEToGLWESwitchingKeyPreparedToRef<B: Backend> { impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> LWEToGLWESwitchingKeyPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>, GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{ {
fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> { fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
LWEToGLWESwitchingKeyPrepared(self.0.to_ref()) self.0.to_ref()
} }
} }
pub trait LWEToGLWESwitchingKeyPreparedToMut<B: Backend> { impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
where where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>, GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToMut<B>,
{ {
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
LWEToGLWESwitchingKeyPrepared(self.0.to_mut()) self.0.to_mut()
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
} }
} }

View File

@@ -16,6 +16,7 @@ pub use operations::*;
pub mod layouts; pub mod layouts;
pub use automorphism::*; pub use automorphism::*;
pub use conversion::*; pub use conversion::*;
pub use decryption::*;
pub use dist::*; pub use dist::*;
pub use encryption::*; pub use encryption::*;
pub use external_product::*; pub use external_product::*;

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{ layouts::{
AutomorphismKey, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey,
GLWESwitchingKey, Rank, TensorKey, GLWESecret, GLWESwitchingKey, Rank, TensorKey,
prepared::{ prepared::{
AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
GLWESwitchingKeyPrepared, TensorKeyPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared,
@@ -258,8 +258,8 @@ where
( (
GLWESwitchingKey { GLWESwitchingKey {
key: data, key: data,
sk_in_n: 0, input_degree: Degree(0),
sk_out_n: 0, output_degree: Degree(0),
}, },
scratch, scratch,
) )
@@ -279,8 +279,8 @@ where
( (
GLWESwitchingKeyPrepared { GLWESwitchingKeyPrepared {
key: data, key: data,
sk_in_n: 0, input_degree: Degree(0),
sk_out_n: 0, output_degree: Degree(0),
}, },
scratch, scratch,
) )
@@ -292,7 +292,7 @@ where
M: ModuleN, M: ModuleN,
{ {
assert_eq!(module.n() as u32, infos.n()); assert_eq!(module.n() as u32, infos.n());
let (data, scratch) = self.take_glwe_switching_key(module, infos); let (data, scratch) = self.take_gglwe(module, infos);
(AutomorphismKey { key: data, p: 0 }, scratch) (AutomorphismKey { key: data, p: 0 }, scratch)
} }
@@ -306,7 +306,7 @@ where
M: ModuleN + VmpPMatBytesOf, M: ModuleN + VmpPMatBytesOf,
{ {
assert_eq!(module.n() as u32, infos.n()); assert_eq!(module.n() as u32, infos.n());
let (data, scratch) = self.take_gglwe_switching_key_prepared(module, infos); let (data, scratch) = self.take_gglwe_prepared(module, infos);
(AutomorphismKeyPrepared { key: data, p: 0 }, scratch) (AutomorphismKeyPrepared { key: data, p: 0 }, scratch)
} }
@@ -321,21 +321,21 @@ where
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey" "rank_in != rank_out is not supported for GGLWETensorKey"
); );
let mut keys: Vec<GLWESwitchingKey<&mut [u8]>> = Vec::new(); let mut keys: Vec<GGLWE<&mut [u8]>> = Vec::new();
let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;
let mut scratch: &mut Self = self; let mut scratch: &mut Self = self;
let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); let mut ksk_infos: GGLWELayout = infos.gglwe_layout();
ksk_infos.rank_in = Rank(1); ksk_infos.rank_in = Rank(1);
if pairs != 0 { if pairs != 0 {
let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos);
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
for _ in 1..pairs { for _ in 1..pairs {
let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos);
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
@@ -354,21 +354,21 @@ where
"rank_in != rank_out is not supported for GGLWETensorKeyPrepared" "rank_in != rank_out is not supported for GGLWETensorKeyPrepared"
); );
let mut keys: Vec<GLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new(); let mut keys: Vec<GGLWEPrepared<&mut [u8], B>> = Vec::new();
let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;
let mut scratch: &mut Self = self; let mut scratch: &mut Self = self;
let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); let mut ksk_infos: GGLWELayout = infos.gglwe_layout();
ksk_infos.rank_in = Rank(1); ksk_infos.rank_in = Rank(1);
if pairs != 0 { if pairs != 0 {
let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos);
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
for _ in 1..pairs { for _ in 1..pairs {
let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos);
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }

View File

@@ -18,8 +18,8 @@ backend_test_suite!(
glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk,
glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk,
// GLWE Keyswitch // GLWE Keyswitch
// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch,
// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace,
// GLWE Automorphism // GLWE Automorphism
// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, // glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism,
// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, // glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace,
@@ -35,10 +35,10 @@ gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryptio
gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk,
gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk,
gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk,
// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk,
// GGLWE Keyswitching // GGLWE Keyswitching
// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch,
// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace,
// GGLWE External Product // GGLWE External Product
// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, // gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product,
// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, // gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace,
@@ -46,11 +46,11 @@ gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_
// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, // gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism,
// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, // gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace,
// GGSW Encryption // GGSW Encryption
// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk,
// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk,
// GGSW Keyswitching // GGSW Keyswitching
// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch,
// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace,
// GGSW External Product // GGSW External Product
// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, // ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product,
// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, // ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace,
@@ -58,7 +58,7 @@ gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_
// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, // ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism,
// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace,
// LWE // LWE
// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch,
// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, // glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe,
// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, // lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe,
} }

View File

@@ -5,7 +5,8 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk,
GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare, AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare,
@@ -16,7 +17,7 @@ use crate::{
pub fn test_gglwe_automorphism_key_encrypt_sk<BE: Backend>(module: &Module<BE>) pub fn test_gglwe_automorphism_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: GGLWEEncryptSk<BE> Module<BE>: AutomorphismKeyEncryptSk<BE>
+ GGLWEKeyswitch<BE> + GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
@@ -82,7 +83,6 @@ where
sk_out_prepared.prepare(module, &sk_out); sk_out_prepared.prepare(module, &sk_out);
atk.key atk.key
.key
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA); .assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
} }
} }
@@ -90,7 +90,7 @@ where
pub fn test_gglwe_automorphism_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) pub fn test_gglwe_automorphism_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: GGLWEEncryptSk<BE> Module<BE>: AutomorphismKeyCompressedEncryptSk<BE>
+ GGLWEKeyswitch<BE> + GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
@@ -125,7 +125,9 @@ where
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(
module, &atk_infos,
));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
@@ -153,7 +155,6 @@ where
atk.decompress(module, &atk_compressed); atk.decompress(module, &atk_compressed);
atk.key atk.key
.key
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA); .assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
} }
} }

View File

@@ -31,7 +31,7 @@ where
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
let n = module.n(); let n: usize = module.n();
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 54; let k_ksk: usize = 54;
let dsize: usize = k_ksk / base2k; let dsize: usize = k_ksk / base2k;

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace,
VecZnxSwitchRing, VecZnxSwitchRing,
}, },
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxBig, VecZnxDft},
source::Source, source::Source,
}; };
@@ -73,8 +73,8 @@ where
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); let mut sk_ij_big: VecZnxBig<Vec<u8>, BE> = module.vec_znx_big_alloc(1, 1);
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into());
let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
@@ -126,8 +126,6 @@ where
+ VecZnxFillUniform + VecZnxFillUniform
+ VecZnxCopy + VecZnxCopy
+ VecZnxSwitchRing, + VecZnxSwitchRing,
// + VecZnxNormalizeInplaceImpl<BE>
// + VecZnxNormalizeImpl<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -170,8 +168,8 @@ where
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); let mut sk_ij_big: VecZnxBig<Vec<u8>, BE> = module.vec_znx_big_alloc(1, 1);
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into());
let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);

View File

@@ -9,7 +9,8 @@ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout,
GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare,
TensorKeyPreparedAlloc,
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared},
}, },
noise::noise_ggsw_keyswitch, noise::noise_ggsw_keyswitch,
@@ -26,7 +27,9 @@ where
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ TensorKeyPreparedAlloc<BE> + TensorKeyPreparedAlloc<BE>
+ GLWESwitchingKeyPreparedAlloc<BE> + GLWESwitchingKeyPreparedAlloc<BE>
+ GGSWNoise<BE>, + GGSWNoise<BE>
+ GLWESwitchingKeyPrepare<BE>
+ TensorKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -145,8 +148,12 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
let tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk);
tsk_prepared.prepare(module, &tsk, scratch.borrow());
ggsw_out.keyswitch( ggsw_out.keyswitch(
module, module,
@@ -188,7 +195,9 @@ where
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ TensorKeyPreparedAlloc<BE> + TensorKeyPreparedAlloc<BE>
+ GLWESwitchingKeyPreparedAlloc<BE> + GLWESwitchingKeyPreparedAlloc<BE>
+ GGSWNoise<BE>, + GGSWNoise<BE>
+ GLWESwitchingKeyPrepare<BE>
+ TensorKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -259,6 +268,7 @@ where
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
sk_in.fill_ternary_prob(var_xs, &mut source_xs); sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_in_prepared.prepare(module, &sk_in); sk_in_prepared.prepare(module, &sk_in);
@@ -295,8 +305,12 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
let tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk);
tsk_prepared.prepare(module, &tsk, scratch.borrow());
ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow());

View File

@@ -1,14 +1,15 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform},
layouts::{Backend, Module, Scratch, ScratchOwned}, layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source, source::Source,
}; };
use crate::{ use crate::{
ScratchTakeCore, GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey,
GLWESwitchingKeyLayout, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc,
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
@@ -17,7 +18,15 @@ use crate::{
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn test_glwe_keyswitch<BE: Backend>(module: &Module<BE>) pub fn test_glwe_keyswitch<BE: Backend>(module: &Module<BE>)
where where
Module<BE>:, Module<BE>: VecZnxFillUniform
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWEEncryptSk<BE>
+ GLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ GLWESwitchingKeyPrepare<BE>
+ GLWESwitchingKeyPreparedAlloc<BE>
+ GLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -48,7 +57,7 @@ where
rank: rank_out.into(), rank: rank_out.into(),
}; };
let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { let ksk: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -58,7 +67,7 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&key_apply); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk);
let mut glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_infos); let mut glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_infos);
let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos);
@@ -70,18 +79,22 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply) GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk)
| GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos)
| GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply), | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ksk),
); );
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into());
sk_in.fill_ternary_prob(0.5, &mut source_xs); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_in.into());
sk_in_prepared.prepare(module, &sk_in);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into());
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_out.prepare_alloc(module, scratch.borrow());
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into());
sk_out_prepared.prepare(module, &sk_out);
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,
@@ -101,7 +114,9 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = ksk.prepare_alloc(module, scratch.borrow()); let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow());
@@ -126,7 +141,15 @@ where
pub fn test_glwe_keyswitch_inplace<BE: Backend>(module: &Module<BE>) pub fn test_glwe_keyswitch_inplace<BE: Backend>(module: &Module<BE>)
where where
Module<BE>:, Module<BE>: VecZnxFillUniform
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWEEncryptSk<BE>
+ GLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ GLWESwitchingKeyPrepare<BE>
+ GLWESwitchingKeyPreparedAlloc<BE>
+ GLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -148,7 +171,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { let ksk_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -158,7 +181,7 @@ where
rank_out: rank.into(), rank_out: rank.into(),
}; };
let mut key_apply: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_infos);
let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
@@ -169,20 +192,24 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_infos)
| GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
| GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos), | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ksk_infos),
); );
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
sk_in.fill_ternary_prob(0.5, &mut source_xs); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_in_prepared.prepare(module, &sk_in);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_out.prepare_alloc(module, scratch.borrow());
key_apply.encrypt_sk( let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_out_prepared.prepare(module, &sk_out);
ksk.encrypt_sk(
module, module,
&sk_in, &sk_in,
&sk_out, &sk_out,
@@ -200,7 +227,9 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = key_apply.prepare_alloc(module, scratch.borrow()); let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow());

View File

@@ -5,15 +5,21 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
ScratchTakeCore, LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore,
layouts::{ layouts::{
LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::LWESwitchingKeyPrepared, LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPrepare,
LWESwitchingKeyPreparedAlloc, prepared::LWESwitchingKeyPrepared,
}, },
}; };
pub fn test_lwe_keyswitch<BE: Backend>(module: &Module<BE>) pub fn test_lwe_keyswitch<BE: Backend>(module: &Module<BE>)
where where
Module<BE>:, Module<BE>: LWEKeySwitch<BE>
+ LWESwitchingKeyEncrypt<BE>
+ LWEEncryptSk<BE>
+ LWESwitchingKeyPreparedAlloc<BE>
+ LWEDecrypt<BE>
+ LWESwitchingKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -89,7 +95,8 @@ where
let mut lwe_ct_out: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_out_infos); let mut lwe_ct_out: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_out_infos);
let ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, BE> = ksk.prepare_alloc(module, scratch.borrow()); let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, BE> = LWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow());

View File

@@ -1,4 +1,4 @@
use std::{fmt::Display, marker::PhantomData, ptr::NonNull}; use std::{fmt::{Debug, Display}, marker::PhantomData, ptr::NonNull};
use rand_distr::num_traits::Zero; use rand_distr::num_traits::Zero;
@@ -9,8 +9,8 @@ use crate::{
#[allow(clippy::missing_safety_doc)] #[allow(clippy::missing_safety_doc)]
pub trait Backend: Sized { pub trait Backend: Sized {
type ScalarBig: Copy + Zero + Display; type ScalarBig: Copy + Zero + Display + Debug;
type ScalarPrep: Copy + Zero + Display; type ScalarPrep: Copy + Zero + Display + Debug;
type Handle: 'static; type Handle: 'static;
fn layout_prep_word_count() -> usize; fn layout_prep_word_count() -> usize;
fn layout_big_word_count() -> usize; fn layout_big_word_count() -> usize;

View File

@@ -1,3 +1,5 @@
use std::fmt::{Debug, Display};
use crate::{ use crate::{
layouts::{Backend, Data, DataMut, DataRef}, layouts::{Backend, Data, DataMut, DataRef},
source::Source, source::Source,
@@ -48,7 +50,7 @@ pub trait DataViewMut: DataView {
} }
pub trait ZnxView: ZnxInfos + DataView<D: DataRef> { pub trait ZnxView: ZnxInfos + DataView<D: DataRef> {
type Scalar: Copy + Zero; type Scalar: Copy + Zero + Display + Debug;
/// Returns a non-mutable pointer to the underlying coefficients array. /// Returns a non-mutable pointer to the underlying coefficients array.
fn as_ptr(&self) -> *const Self::Scalar { fn as_ptr(&self) -> *const Self::Scalar {