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,
automorphism::glwe_ct::GLWEAutomorphism,
layouts::{
AutomorphismKey, AutomorphismKeyToMut, AutomorphismKeyToRef, GGLWEInfos, GLWE, GLWEInfos,
prepared::{
AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement, SetAutomorphismGaloisElement,
},
AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
},
};
@@ -29,8 +27,8 @@ impl AutomorphismKey<Vec<u8>> {
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>)
where
A: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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>)
where
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
M: AutomorphismKeyAutomorphism<BE>,
{
@@ -67,35 +65,34 @@ where
fn automorphism_key_automorphism<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>)
where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement,
A: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
assert!(
res.dnum().as_u32() <= a.dnum().as_u32(),
"res dnum: {} > a dnum: {}",
res.dnum(),
a.dnum()
);
assert_eq!(
res.dsize(),
a.dsize(),
"res dnum: {} != a dnum: {}",
res.dsize(),
a.dsize()
);
let cols_out: usize = (key.rank_out() + 1).into();
let p: i64 = a.p();
let p_inv: i64 = self.galois_element_inv(p);
{
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!(
res.dnum().as_u32() <= a.dnum().as_u32(),
"res dnum: {} > a dnum: {}",
res.dnum(),
a.dnum()
);
assert_eq!(
res.dsize(),
a.dsize(),
"res dnum: {} != a dnum: {}",
res.dsize(),
a.dsize()
);
let cols_out: usize = (key.rank_out() + 1).into();
let p: i64 = a.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 col in 0..cols_out {
@@ -104,11 +101,11 @@ where
// Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a)
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)
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)
(0..cols_out).for_each(|i| {
@@ -118,32 +115,29 @@ 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>)
where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
R: GGLWEToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement + GGLWEInfos,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
assert_eq!(
res.rank(),
key.rank(),
"key rank: {} != key rank: {}",
res.rank(),
key.rank()
);
let cols_out: usize = (key.rank_out() + 1).into();
let p: i64 = res.p();
let p_inv: i64 = self.galois_element_inv(p);
{
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref();
assert_eq!(
res.rank(),
key.rank(),
"key rank: {} != key rank: {}",
res.rank(),
key.rank()
);
let cols_out: usize = (key.rank_out() + 1).into();
let p: i64 = res.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 col in 0..cols_out {
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)
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)
for i in 0..cols_out {

View File

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

View File

@@ -9,8 +9,7 @@ use poulpy_hal::{
use crate::{
GLWEKeyswitch, ScratchTakeCore, keyswitch_internal,
layouts::{
GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement},
GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement,
},
};
@@ -31,7 +30,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where
M: GLWEAutomorphism<BE>,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.glwe_automorphism(self, a, key, scratch);
@@ -41,7 +40,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where
M: GLWEAutomorphism<BE>,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.glwe_automorphism_add(self, a, key, scratch);
@@ -51,7 +50,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where
M: GLWEAutomorphism<BE>,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.glwe_automorphism_sub(self, a, key, scratch);
@@ -61,7 +60,7 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where
M: GLWEAutomorphism<BE>,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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>)
where
M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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>)
where
M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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>)
where
M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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>)
where
M: GLWEAutomorphism<BE>,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.glwe_automorphism_sub_negate_inplace(self, key, scratch);
@@ -125,10 +124,10 @@ where
where
R: GLWEToMut,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
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();
@@ -140,10 +139,10 @@ where
fn glwe_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where
R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
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();
@@ -156,15 +155,14 @@ where
where
R: GLWEToMut,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
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 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 {
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>)
where
R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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 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 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -212,15 +209,14 @@ where
where
R: GLWEToMut,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
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 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 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);
@@ -241,15 +237,14 @@ where
where
R: GLWEToMut,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
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 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 {
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>)
where
R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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 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 {
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>)
where
R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GetAutomorphismGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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 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 {
self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1);

View File

@@ -119,10 +119,6 @@ where
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigBytesOf
+ VecZnxNormalizeTmpBytes
+ VecZnxDftBytesOf
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigBytesOf
+ VecZnxNormalizeTmpBytes
+ VecZnxDftApply<BE>
+ VecZnxDftCopy<BE>
+ VmpApplyDftToDft<BE>
@@ -236,7 +232,7 @@ where
// =
// (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2)
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])
for di in 0..dsize {

View File

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

View File

@@ -5,10 +5,7 @@ use poulpy_hal::{
use crate::{
GLWEKeyswitch, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef,
prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef},
},
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef},
};
impl<BE: Backend> GLWEFromLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> {}
@@ -43,12 +40,11 @@ where
where
R: GLWEToMut,
A: LWEToRef,
K: LWEToGLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
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!(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
M: GLWEFromLWE<BE>,
A: LWEToRef,
K: LWEToGLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.glwe_from_lwe(self, lwe, ksk, scratch);

View File

@@ -1,33 +1,39 @@
use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace},
layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, ZnxView, ZnxViewMut},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
};
use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut};
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
P: LWEPlaintextToMut,
S: LWESecretToRef,
M: LWEDecrypt<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
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
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
R: LWEToMut,
P: LWEPlaintextToMut,
S: LWESecretToRef,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
{
let res: &mut LWE<&mut [u8]> = &mut res.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));
}
}
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;
pub use glwe_ct::*;
// pub use lwe_ct::*;
pub use lwe_ct::*;

View File

@@ -5,11 +5,10 @@ use poulpy_hal::{
};
use crate::{
ScratchTakeCore,
encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk,
GGLWECompressedEncryptSk, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut},
GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared,
GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, compressed::AutomorphismKeyCompressed,
},
};
@@ -34,7 +33,7 @@ impl<DataSelf: DataMut> AutomorphismKeyCompressed<DataSelf> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: GLWESecretToRef,
S: GLWESecretToRef + GLWEInfos,
M: AutomorphismKeyCompressedEncryptSk<BE>,
{
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,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef;
R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef + GLWEInfos;
}
impl<BE: Backend> AutomorphismKeyCompressedEncryptSk<BE> for Module<BE>
where
Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk<BE>,
Self: ModuleN
+ GaloisElement
+ VecZnxAutomorphism
+ GGLWECompressedEncryptSk<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
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>(
@@ -80,12 +87,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef,
R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef + GLWEInfos,
{
let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
assert_eq!(res.n(), sk.n());
assert_eq!(res.rank_out(), res.rank_in());
assert_eq!(sk.rank(), res.rank_out());
@@ -96,9 +101,9 @@ where
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() {
self.vec_znx_automorphism(
self.galois_element_inv(p),
@@ -108,10 +113,18 @@ where
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,
encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef,
GLWESwitchingKeyDegreesMut, LWEInfos,
compressed::GLWESwitchingKeyCompressed,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -57,7 +58,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyCompressedToMut,
R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef;
}
@@ -86,11 +87,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyCompressedToMut,
R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef,
{
let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
@@ -122,15 +122,9 @@ where
}
}
self.gglwe_compressed_encrypt_sk(
&mut res.key,
&sk_in_tmp,
&sk_out_tmp,
seed_xa,
source_xe,
scratch_2,
);
res.sk_in_n = sk_in.n().into();
res.sk_out_n = sk_out.n().into();
self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2);
*res.input_degree() = sk_in.n();
*res.output_degree() = sk_out.n();
}
}

View File

@@ -4,16 +4,15 @@ use poulpy_hal::{
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA,
},
layouts::{Backend, DataMut, Module, Scratch},
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
source::Source,
};
use crate::{
GetDistribution, ScratchTakeCore,
encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk},
GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore,
encryption::gglwe_tsk::TensorKeyEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, Rank,
TensorKeyCompressedAtMut, compressed::TensorKeyCompressed,
},
};
@@ -23,7 +22,7 @@ impl TensorKeyCompressed<Vec<u8>> {
A: GGLWEInfos,
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,
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> {
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
A: GGLWEInfos;
fn gglwe_tensor_key_encrypt_sk<R, S>(
fn tensor_key_compressed_encrypt_sk<R, S, D>(
&self,
res: &mut R,
sk: &S,
@@ -56,36 +55,40 @@ pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: TensorKeyCompressedToMut,
D: DataMut,
R: TensorKeyCompressedAtMut<D> + GGLWEInfos,
S: GLWESecretToRef + GetDistribution;
}
impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE>
where
Self: ModuleN
+ GLWESwitchingKeyCompressedEncryptSk<BE>
+ GGLWECompressedEncryptSk<BE>
+ TensorKeyEncryptSk<BE>
+ VecZnxDftApply<BE>
+ SvpApplyDftToDft<BE>
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxBigNormalize<BE>
+ SvpPrepare<BE>
+ SvpPPolAllocBytesImpl<BE>
+ SvpPPolBytesOf
+ VecZnxDftAllocBytesImpl<BE>
+ VecZnxBigAllocBytesImpl<BE>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf,
+ VecZnxBigBytesOf
+ GLWESecretPreparedAlloc<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
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,
res: &mut R,
sk: &S,
@@ -93,11 +96,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: TensorKeyCompressedToMut,
D: DataMut,
R: GGLWEInfos + TensorKeyCompressedAtMut<D>,
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());
sk_dft_prep.prepare(self, sk);
@@ -141,10 +143,10 @@ where
let (seed_xa_tmp, _) = source_xa.branch();
self.glwe_switching_key_compressed_encrypt_sk(
self.gglwe_compressed_encrypt_sk(
res.at_mut(i, j),
&sk_ij,
sk,
&sk_ij.data,
&sk_dft_prep,
seed_xa_tmp,
source_xe,
scratch_5,

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace},
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero},
source::Source,
};
@@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>;
}
@@ -83,30 +83,29 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>,
{
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
let base2k: usize = res.base2k().into();
let rank: usize = res.rank().into();
let cols: usize = rank + 1;
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();
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 rank: usize = res.rank().into();
let cols: usize = rank + 1;
let dsize: usize = res.dsize().into();
println!("res.seed: {:?}", res.seed);
let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());

View File

@@ -1,13 +1,15 @@
use poulpy_hal::{
api::{ScratchAvailable, VecZnxAutomorphism},
api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism},
layouts::{Backend, DataMut, GaloisElement, Module, Scratch},
source::Source,
};
use crate::{
ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos},
GGLWEEncryptSk, ScratchTakeCore,
layouts::{
AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared,
GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement,
},
};
impl AutomorphismKey<Vec<u8>> {
@@ -30,7 +32,7 @@ impl AutomorphismKey<Vec<u8>> {
impl<DM: DataMut> AutomorphismKey<DM>
where
Self: AutomorphismKeyToMut,
Self: GGLWEToRef,
{
pub fn encrypt_sk<S, M, BE: Backend>(
&mut self,
@@ -62,13 +64,18 @@ pub trait AutomorphismKeyEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyToMut,
R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef;
}
impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE>
where
Self: GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphism + GaloisElement,
Self: GGLWEEncryptSk<BE>
+ VecZnxAutomorphism
+ GaloisElement
+ SvpPPolBytesOf
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -80,7 +87,10 @@ where
infos.rank_out(),
"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>(
@@ -92,10 +102,9 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyToMut,
R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef,
{
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
assert_eq!(res.n(), sk.n());
@@ -108,9 +117,10 @@ where
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| {
self.vec_znx_automorphism(
self.galois_element_inv(p),
@@ -120,12 +130,19 @@ where
i,
);
});
sk_out_prepared.prepare(self, &sk_out);
}
res.key
.encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1);
self.gglwe_encrypt_sk(
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,
encryption::gglwe_ct::GGLWEEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos,
GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos,
prepared::GLWESecretPreparedAlloc,
},
};
@@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef;
}
@@ -93,11 +93,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef,
{
let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
@@ -129,16 +128,17 @@ where
}
}
res.key.encrypt_sk(
self,
self.gglwe_encrypt_sk(
res,
&sk_in_tmp,
&sk_out_tmp,
source_xa,
source_xe,
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::{
GetDistribution, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
GGLWEEncryptSk, GetDistribution, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut,
GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -63,7 +62,7 @@ pub trait TensorKeyEncryptSk<BE: Backend> {
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
where
Self: ModuleN
+ GLWESwitchingKeyEncryptSk<BE>
+ GGLWEEncryptSk<BE>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ GLWESecretPreparedAlloc<BE>
@@ -83,7 +82,7 @@ where
+ self.bytes_of_vec_znx_big(1, 1)
+ self.bytes_of_vec_znx_dft(1, 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>(
@@ -102,8 +101,8 @@ where
// let n: RingDegree = sk.n();
let rank: Rank = res.rank_out();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
sk_dft_prep.prepare(self, sk);
let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
sk_prepared.prepare(self, sk);
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
@@ -122,7 +121,7 @@ where
(0..rank.into()).for_each(|i| {
(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_big_normalize(
@@ -135,8 +134,14 @@ where
scratch_5,
);
res.at_mut(i, j)
.encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5);
res.at_mut(i, j).encrypt_sk(
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,
scratch: &mut Scratch<BE>,
) where
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>,
M: GLWEEncryptPk<BE>,
{
@@ -245,7 +245,7 @@ pub trait GLWEEncryptPk<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>;
fn glwe_encrypt_zero_pk<R, K>(
@@ -285,7 +285,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>,
{
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>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>;
}
@@ -351,7 +351,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
@@ -361,8 +361,8 @@ where
assert_eq!(res.n(), pk.n());
assert_eq!(res.rank(), pk.rank());
if let Some((pt, _)) = pt {
assert_eq!(pt.to_ref().base2k(), pk.base2k());
assert_eq!(pt.to_ref().n(), pk.n());
assert_eq!(pt.base2k(), pk.base2k());
assert_eq!(pt.n(), pk.n());
}
let base2k: usize = pk.base2k().into();

View File

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

View File

@@ -1,7 +1,6 @@
use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace},
layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
source::Source,
};
@@ -16,7 +15,6 @@ impl<DataSelf: DataMut> LWE<DataSelf> {
P: LWEPlaintextToRef,
S: LWESecretToRef,
M: LWEEncryptSk<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
{
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>
where
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)
where

View File

@@ -8,8 +8,8 @@ use crate::{
ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut,
Rank,
GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef,
LWESwitchingKey, Rank,
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -56,7 +56,7 @@ pub trait LWESwitchingKeyEncrypt<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: LWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: LWESecretToRef,
S2: LWESecretToRef;
}
@@ -100,13 +100,12 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: LWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: 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_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_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.glwe_switching_key_encrypt_sk(
&mut res.0,
res,
&sk_in_glwe,
&sk_out_glwe,
source_xa,

View File

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

View File

@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Scratch};
use crate::{
ScratchTakeCore,
external_product::gglwe_ksk::GGLWEExternalProduct,
layouts::{AutomorphismKey, AutomorphismKeyToRef, GGLWEInfos, GGSWInfos, prepared::GGSWPreparedToRef},
layouts::{AutomorphismKey, GGLWEInfos, GGLWEToRef, GGSWInfos, prepared::GGSWPreparedToRef},
};
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>)
where
M: GGLWEExternalProduct<BE>,
A: AutomorphismKeyToRef,
A: GGLWEToRef,
B: GGSWPreparedToRef<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>)
@@ -41,6 +41,6 @@ impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
A: GGSWPreparedToRef<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::{
GLWEExternalProduct, ScratchTakeCore,
layouts::{
GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef,
GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey,
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>)
where
M: GGLWEExternalProduct<BE>,
A: GLWESwitchingKeyToRef,
A: GGLWEToRef,
B: GGSWPreparedToRef<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>)
@@ -129,6 +129,6 @@ impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
A: GGSWPreparedToRef<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,
glwe_trace::GLWETrace,
layouts::{
GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement},
GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::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>)
where
A: GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
M: GLWEPacking<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
@@ -191,7 +190,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut + GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
@@ -235,7 +234,7 @@ fn pack_core<A, K, M, BE: Backend>(
scratch: &mut Scratch<BE>,
) where
A: GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
M: ModuleLogN
+ GLWEAutomorphism<BE>
+ GaloisElement
@@ -308,7 +307,7 @@ fn combine<B, M, K, BE: Backend>(
B: GLWEToRef + GLWEInfos,
M: GLWEAutomorphism<BE> + GaloisElement + GLWERotate<BE> + GLWESub + GLWEShift<BE> + GLWEAdd + GLWENormalize<BE>,
B: GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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>,
A: GLWEToMut + GLWEToRef + GLWEInfos,
B: GLWEToMut + GLWEToRef + GLWEInfos,
K: AutomorphismKeyPreparedToRef<BE> + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
// 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::{
GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore,
layouts::{
Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement},
Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos,
prepared::GetAutomorphismGaloisElement,
},
};
@@ -43,7 +43,7 @@ impl<D: DataMut> GLWE<D> {
scratch: &mut Scratch<BE>,
) where
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
M: GLWETrace<BE>,
{
@@ -58,7 +58,7 @@ impl<D: DataMut> GLWE<D> {
keys: &HashMap<i64, K>,
scratch: &mut Scratch<BE>,
) where
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
M: GLWETrace<BE>,
{
@@ -110,7 +110,7 @@ where
where
R: GLWEToMut,
A: GLWEToRef,
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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>)
where
R: GLWEToMut,
K: AutomorphismKeyPreparedToRef<BE> + GGLWEInfos + GetAutomorphismGaloisElement,
K: GGLWEPreparedToRef<BE> + GetAutomorphismGaloisElement + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
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::{
ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{
AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey,
GLWESwitchingKeyToRef,
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef},
},
layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey},
};
impl AutomorphismKey<Vec<u8>> {
@@ -25,21 +21,21 @@ impl AutomorphismKey<Vec<u8>> {
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>)
where
A: AutomorphismKeyToRef,
B: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEToRef + GGLWEToRef,
B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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>)
where
A: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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> {
pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
where
A: GLWESwitchingKeyToRef,
B: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEToRef,
B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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>)
where
A: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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>)
where
A: GGLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>,
B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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>)
where
A: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
M: GGLWEKeyswitch<BE>,
{
@@ -128,12 +124,11 @@ where
where
R: GGLWEToMut,
A: GGLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>,
B: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let a: &GGLWE<&[u8]> = &a.to_ref();
let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref();
assert_eq!(
res.rank_in(),
@@ -180,11 +175,10 @@ where
fn gglwe_keyswitch_inplace<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<BE>)
where
R: GGLWEToMut,
A: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref();
assert_eq!(
res.rank_out(),

View File

@@ -3,10 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx};
use crate::{
GGSWExpandRows, ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{
GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef,
prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef},
},
layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef},
};
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>)
where
A: GGSWToRef,
K: GLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<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>)
where
K: GLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
M: GGSWKeyswitch<BE>,
@@ -93,14 +90,15 @@ where
where
R: GGSWToMut,
A: GGSWToRef,
K: GLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
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() {
// 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>)
where
R: GGSWToMut,
K: GLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE>,
T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{

View File

@@ -4,15 +4,12 @@ use poulpy_hal::{
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft,
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::{
ScratchTakeCore,
layouts::{
GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef},
},
layouts::{GGLWEInfos, GGLWEPrepared, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos},
};
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>)
where
A: GLWEToRef,
B: GLWESwitchingKeyPreparedToRef<BE>,
B: GGLWEPreparedToRef<BE>,
M: GLWEKeyswitch<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>)
where
A: GLWESwitchingKeyPreparedToRef<BE>,
A: GGLWEPreparedToRef<BE>,
M: GLWEKeyswitch<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
@@ -129,12 +126,12 @@ where
where
R: GLWEToMut,
A: GLWEToRef,
K: GLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
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!(
a.rank(),
@@ -184,11 +181,11 @@ where
fn glwe_keyswitch_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>)
where
R: GLWEToMut,
K: GLWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
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!(
res.rank(),
@@ -239,17 +236,17 @@ impl GLWE<Vec<u8>> {}
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,
mut res: VecZnxDft<DR, BE>,
a: &GLWE<DA>,
key: &GLWESwitchingKeyPrepared<DB, BE>,
a: &A,
key: &K,
scratch: &mut Scratch<BE>,
) -> VecZnxBig<DR, BE>
where
DR: DataMut,
DA: DataRef,
DB: DataRef,
A: GLWEToRef,
K: GGLWEPreparedToRef<BE>,
M: ModuleN
+ VecZnxDftBytesOf
+ VmpApplyDftToDftTmpBytes
@@ -264,11 +261,14 @@ where
+ VecZnxNormalize<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_out: usize = key.base2k().into();
let cols: usize = (a.rank() + 1).into();
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 {
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::{
LWESampleExtract, ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{
GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision,
prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef},
},
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision},
};
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>)
where
A: LWEToRef,
K: LWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<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>
where
@@ -75,12 +72,11 @@ where
where
R: LWEToMut,
A: LWEToRef,
K: LWESwitchingKeyPreparedToRef<BE>,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
let a: &LWE<&[u8]> = &a.to_ref();
let ksk: &LWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref();
assert!(res.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..]);
}
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);
}
}

View File

@@ -4,10 +4,9 @@ use poulpy_hal::{
};
use crate::layouts::{
AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut,
GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWECompressed, GLWECompressedToMut, GLWECompressedToRef,
GLWEDecompress, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -15,7 +14,7 @@ use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct AutomorphismKeyCompressed<D: Data> {
pub(crate) key: GLWESwitchingKeyCompressed<D>,
pub(crate) key: GGLWECompressed<D>,
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 {
AutomorphismKeyCompressed {
key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
key: GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
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 {
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
where
Self: GLWESwitchingKeyDecompress,
Self: GGLWEDecompress,
{
fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O)
where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement,
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
R: GGLWEToMut + SetAutomorphismGaloisElement,
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());
}
}
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>
where
@@ -162,20 +161,32 @@ where
{
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
O: GGLWECompressedToRef + GetAutomorphismGaloisElement,
M: AutomorphismKeyDecompress,
{
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 {
fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>;
}
impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
GLWECompressed<D>: GLWECompressedToRef,
{
fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> {
AutomorphismKeyCompressed {
@@ -191,7 +202,7 @@ pub trait AutomorphismKeyCompressedToMut {
impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
GLWECompressed<D>: GLWECompressedToMut,
{
fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> {
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 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 dnum: usize = res.dnum().into();

View File

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

View File

@@ -4,17 +4,15 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos,
GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct TensorKeyCompressed<D: Data> {
pub(crate) keys: Vec<GLWESwitchingKeyCompressed<D>>,
pub(crate) keys: Vec<GGLWECompressed<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) {
self.keys
.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);
TensorKeyCompressed {
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(),
}
}
@@ -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 {
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(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed<D> {
pub trait TensorKeyCompressedAtRef<D: DataRef> {
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 {
std::mem::swap(&mut i, &mut j);
};
@@ -163,7 +179,7 @@ impl<D: DataMut> TensorKeyCompressed<D> {
pub trait TensorKeyDecompress
where
Self: GLWESwitchingKeyDecompress,
Self: GGLWEDecompress,
{
fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O)
where
@@ -182,12 +198,12 @@ where
);
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> {
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>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
GGLWECompressed<D>: GGLWECompressedToMut,
{
fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> {
TensorKeyCompressed {
@@ -220,7 +236,7 @@ pub trait TensorKeyCompressedToRef {
impl<D: DataRef> TensorKeyCompressedToRef for TensorKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
GGLWECompressed<D>: GGLWECompressedToRef,
{
fn to_ref(&self) -> TensorKeyCompressed<&[u8]> {
TensorKeyCompressed {

View File

@@ -138,7 +138,7 @@ impl GGSWCompressed<Vec<u8>> {
base2k,
dsize,
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> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,8 +4,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -23,7 +22,7 @@ pub struct TensorKeyLayout {
#[derive(PartialEq, Eq, Clone)]
pub struct TensorKey<D: Data> {
pub(crate) keys: Vec<GLWESwitchingKey<D>>,
pub(crate) keys: Vec<GGLWE<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) {
self.keys
.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);
TensorKey {
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(),
}
}
@@ -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 {
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> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey<D> {
// Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -196,8 +195,8 @@ impl<D: DataMut> TensorKey<D> {
}
impl<D: DataRef> TensorKey<D> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey<D> {
// Returns a reference to GGLWE_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -238,7 +237,7 @@ pub trait TensorKeyToRef {
impl<D: DataRef> TensorKeyToRef for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
GGLWE<D>: GGLWEToRef,
{
fn to_ref(&self) -> TensorKey<&[u8]> {
TensorKey {
@@ -253,7 +252,7 @@ pub trait TensorKeyToMut {
impl<D: DataMut> TensorKeyToMut for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
GGLWE<D>: GGLWEToMut,
{
fn to_mut(&mut self) -> TensorKey<&mut [u8]> {
TensorKey {

View File

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

@@ -6,8 +6,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -193,28 +193,34 @@ impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
}
}
pub trait LWESwitchingKeyToRef {
fn to_ref(&self) -> LWESwitchingKey<&[u8]>;
}
impl<D: DataRef> LWESwitchingKeyToRef for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWESwitchingKey<&[u8]> {
LWESwitchingKey(self.0.to_ref())
impl<D: DataRef> GGLWEToRef for LWESwitchingKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.0.to_ref()
}
}
pub trait LWESwitchingKeyToMut {
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> LWESwitchingKeyToMut for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> {
LWESwitchingKey(self.0.to_mut())
impl<D: DataMut> GGLWEToMut for LWESwitchingKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.0.to_mut()
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWESwitchingKey<D> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
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::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -196,28 +196,24 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
}
}
pub trait LWEToGLWESwitchingKeyToRef {
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> {
LWEToGLWESwitchingKey(self.0.to_ref())
impl<D: DataRef> GGLWEToRef for LWEToGLWESwitchingKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.0.to_ref()
}
}
pub trait LWEToGLWESwitchingKeyToMut {
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> {
LWEToGLWESwitchingKey(self.0.to_mut())
impl<D: DataMut> GGLWEToMut for LWEToGLWESwitchingKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.0.to_mut()
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey<D> {
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,16 +1,13 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq)]
pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GLWESwitchingKeyPrepared<D, B>,
pub(crate) key: GGLWEPrepared<D, B>,
pub(crate) p: i64,
}
@@ -78,7 +75,7 @@ impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
pub trait AutomorphismKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
Self: GGLWEPreparedAlloc<B>,
{
fn alloc_automorphism_key_prepared(
&self,
@@ -89,7 +86,7 @@ where
dsize: Dsize,
) -> 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,
}
}
@@ -120,7 +117,7 @@ where
dnum: Dnum,
dsize: Dsize,
) -> 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
@@ -130,7 +127,7 @@ where
assert_eq!(
infos.rank_in(),
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(
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> {
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>
where
Self: GLWESwitchingKeyPrepare<B>,
Self: GGLWEPrepare<B>,
{
fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
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>)
where
R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
R: GGLWEPreparedToMut<B> + SetAutomorphismGaloisElement,
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());
}
}
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> {
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> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
O: GGLWEToRef + GetAutomorphismGaloisElement,
M: PrepareAutomorphismKey<B>,
{
module.prepare_automorphism_key(self, other, scratch);
}
}
pub trait AutomorphismKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>;
}
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(),
}
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
self.key.to_mut()
}
}
pub trait AutomorphismKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>;
}
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(),
}
impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for AutomorphismKeyPrepared<D, BE> {
fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
self.key.to_ref()
}
}

View File

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

View File

@@ -1,16 +1,13 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
};
#[derive(PartialEq, Eq)]
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> {
@@ -57,7 +54,7 @@ impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> {
pub trait TensorKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
Self: GGLWEPreparedAlloc<B>,
{
fn alloc_tensor_key_prepared(
&self,
@@ -70,7 +67,7 @@ where
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyPrepared {
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(),
}
}
@@ -82,7 +79,7 @@ where
assert_eq!(
infos.rank_in(),
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(
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 {
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
@@ -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> {
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> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared<D, B> {
// Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared<D, B> {
if i > 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> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared<D, B> {
// Returns a reference to GGLWE_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared<D, B> {
if i > 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>
where
Self: GLWESwitchingKeyPrepare<B>,
Self: GGLWEPrepare<B>,
{
fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
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>)
@@ -190,12 +187,12 @@ where
assert_eq!(res.keys.len(), other.keys.len());
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> {
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>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> {
TensorKeyPrepared {
@@ -238,7 +235,7 @@ pub trait TensorKeyPreparedToRef<B: Backend> {
impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> {
TensorKeyPrepared {

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
};
#[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>)
where
R: GLWEToLWESwitchingKeyPreparedToMut<B>,
O: GLWEToLWESwitchingKeyToRef,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
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> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GLWEToLWESwitchingKeyToRef,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: GLWEToLWESwitchingKeyPrepare<B>,
{
module.prepare_glwe_to_lwe_switching_key(self, other, scratch);
}
}
pub trait GLWEToLWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWEToLWESwitchingKeyPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> {
GLWEToLWESwitchingKeyPrepared(self.0.to_ref())
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
self.0.to_ref()
}
}
pub trait GLWEToLWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> {
GLWEToLWESwitchingKeyPrepared(self.0.to_mut())
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
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 crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
};
#[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>)
where
R: LWESwitchingKeyPreparedToMut<B>,
O: LWESwitchingKeyToRef,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
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> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: LWESwitchingKeyToRef,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: LWESwitchingKeyPrepare<B>,
{
module.prepare_lwe_switching_key(self, other, scratch);
}
}
pub trait LWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> LWESwitchingKeyPreparedToRef<B> for LWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> {
LWESwitchingKeyPrepared(self.0.to_ref())
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
self.0.to_ref()
}
}
pub trait LWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> LWESwitchingKeyPreparedToMut<B> for LWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> {
LWESwitchingKeyPrepared(self.0.to_mut())
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
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 crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
};
/// 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>)
where
R: LWEToGLWESwitchingKeyPreparedToMut<B>,
O: LWEToGLWESwitchingKeyToRef,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
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> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: LWEToGLWESwitchingKeyToRef,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: LWEToGLWESwitchingKeyPrepare<B>,
{
module.prepare_lwe_to_glwe_switching_key(self, other, scratch);
}
}
pub trait LWEToGLWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> LWEToGLWESwitchingKeyPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> {
LWEToGLWESwitchingKeyPrepared(self.0.to_ref())
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
self.0.to_ref()
}
}
pub trait LWEToGLWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> {
LWEToGLWESwitchingKeyPrepared(self.0.to_mut())
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
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 use automorphism::*;
pub use conversion::*;
pub use decryption::*;
pub use dist::*;
pub use encryption::*;
pub use external_product::*;

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
use crate::{
dist::Distribution,
layouts::{
AutomorphismKey, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret,
GLWESwitchingKey, Rank, TensorKey,
AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey,
GLWESecret, GLWESwitchingKey, Rank, TensorKey,
prepared::{
AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
GLWESwitchingKeyPrepared, TensorKeyPrepared,
@@ -258,8 +258,8 @@ where
(
GLWESwitchingKey {
key: data,
sk_in_n: 0,
sk_out_n: 0,
input_degree: Degree(0),
output_degree: Degree(0),
},
scratch,
)
@@ -279,8 +279,8 @@ where
(
GLWESwitchingKeyPrepared {
key: data,
sk_in_n: 0,
sk_out_n: 0,
input_degree: Degree(0),
output_degree: Degree(0),
},
scratch,
)
@@ -292,7 +292,7 @@ where
M: ModuleN,
{
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)
}
@@ -306,7 +306,7 @@ where
M: ModuleN + VmpPMatBytesOf,
{
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)
}
@@ -321,21 +321,21 @@ where
infos.rank_out(),
"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 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);
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;
keys.push(gglwe);
}
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;
keys.push(gglwe);
}
@@ -354,21 +354,21 @@ where
"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 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);
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;
keys.push(gglwe);
}
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;
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_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk,
// 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 => crate::tests::test_suite::keyswitch::test_glwe_keyswitch,
glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace,
// GLWE Automorphism
// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism,
// 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_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_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_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 => 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 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,
@@ -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_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace,
// GGSW Encryption
// 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_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 Keyswitching
// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch,
// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace,
ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch,
ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace,
// 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,
@@ -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_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace,
// 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,
// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe,
}

View File

@@ -5,7 +5,8 @@ use poulpy_hal::{
};
use crate::{
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk,
GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA,
layouts::{
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>)
where
Module<BE>: GGLWEEncryptSk<BE>
Module<BE>: AutomorphismKeyEncryptSk<BE>
+ GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
@@ -82,7 +83,6 @@ where
sk_out_prepared.prepare(module, &sk_out);
atk.key
.key
.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>)
where
Module<BE>: GGLWEEncryptSk<BE>
Module<BE>: AutomorphismKeyCompressedEncryptSk<BE>
+ GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
@@ -125,7 +125,9 @@ where
let mut source_xs: 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);
sk.fill_ternary_prob(0.5, &mut source_xs);
@@ -153,7 +155,6 @@ where
atk.decompress(module, &atk_compressed);
atk.key
.key
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
}
}

View File

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

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace,
VecZnxSwitchRing,
},
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft},
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxBig, VecZnxDft},
source::Source,
};
@@ -73,8 +73,8 @@ where
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_big = module.vec_znx_big_alloc(1, 1);
let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_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_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
@@ -126,8 +126,6 @@ where
+ VecZnxFillUniform
+ VecZnxCopy
+ VecZnxSwitchRing,
// + VecZnxNormalizeInplaceImpl<BE>
// + VecZnxNormalizeImpl<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<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 sk_ij_dft = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_alloc(1, 1);
let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_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_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);

View File

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

View File

@@ -1,14 +1,15 @@
use poulpy_hal::{
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform},
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
use crate::{
ScratchTakeCore,
GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA,
layouts::{
GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout,
GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey,
GLWESwitchingKeyLayout, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc,
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
},
noise::log2_std_noise_gglwe_product,
@@ -17,7 +18,15 @@ use crate::{
#[allow(clippy::too_many_arguments)]
pub fn test_glwe_keyswitch<BE: Backend>(module: &Module<BE>)
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>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -48,7 +57,7 @@ where
rank: rank_out.into(),
};
let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
let ksk: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
@@ -58,7 +67,7 @@ where
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_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);
@@ -70,18 +79,22 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
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::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());
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());
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(
module,
@@ -101,7 +114,9 @@ where
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());
@@ -126,7 +141,15 @@ where
pub fn test_glwe_keyswitch_inplace<BE: Backend>(module: &Module<BE>)
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>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -148,7 +171,7 @@ where
rank: rank.into(),
};
let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
let ksk_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
@@ -158,7 +181,7 @@ where
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 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);
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::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());
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());
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,
&sk_in,
&sk_out,
@@ -200,7 +227,9 @@ where
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());

View File

@@ -5,15 +5,21 @@ use poulpy_hal::{
};
use crate::{
ScratchTakeCore,
LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore,
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>)
where
Module<BE>:,
Module<BE>: LWEKeySwitch<BE>
+ LWESwitchingKeyEncrypt<BE>
+ LWEEncryptSk<BE>
+ LWESwitchingKeyPreparedAlloc<BE>
+ LWEDecrypt<BE>
+ LWESwitchingKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<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 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());