diff --git a/poulpy-core/src/dist.rs b/poulpy-core/src/dist.rs index 415b0b5..c754278 100644 --- a/poulpy-core/src/dist.rs +++ b/poulpy-core/src/dist.rs @@ -1,5 +1,13 @@ use std::io::{Read, Result, Write}; +pub trait GetDistribution { + fn dist(&self) -> &Distribution; +} + +pub trait GetDistributionMut { + fn dist_mut(&mut self) -> &mut Distribution; +} + #[derive(Clone, Copy, Debug)] pub enum Distribution { TernaryFixed(usize), // Ternary with fixed Hamming weight diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 638c566..fbac0f6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GGLWEInfos, GLWEPlaintextAlloc, LWEInfos, + GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::GLWESecretPreparedToRef, }, @@ -60,7 +60,7 @@ pub trait GGLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -94,80 +94,85 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); - let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()]; - let sk = &sk.to_ref(); + { + let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - assert_eq!( - res.rank_in(), - pt.cols() as u32, - "res.rank_in(): {} != pt.cols(): {}", - res.rank_in(), - pt.cols() - ); - assert_eq!( - res.rank_out(), - sk.rank(), - "res.rank_out(): {} != sk.rank(): {}", - res.rank_out(), - sk.rank() - ); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - assert!( - scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - GGLWECompressed::encrypt_sk_tmp_bytes(self, res) - ); - assert!( - res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, - "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", - res.dnum(), - res.dsize(), - res.base2k(), - res.dnum().0 * res.dsize().0 * res.base2k().0, - res.k() - ); + assert_eq!( + res.rank_in(), + pt.cols() as u32, + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), + pt.cols() + ); + assert_eq!( + res.rank_out(), + sk.rank(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), + sk.rank() + ); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + assert!( + scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", + scratch.available(), + GGLWECompressed::encrypt_sk_tmp_bytes(self, res) + ); + assert!( + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() + ); - let dnum: usize = res.dnum().into(); - let dsize: usize = res.dsize().into(); - let base2k: usize = res.base2k().into(); - let rank_in: usize = res.rank_in().into(); - let cols: usize = (res.rank_out() + 1).into(); + let dnum: usize = res.dnum().into(); + let dsize: usize = res.dsize().into(); + let base2k: usize = res.base2k().into(); + let rank_in: usize = res.rank_in().into(); + let cols: usize = (res.rank_out() + 1).into(); - let mut source_xa = Source::new(seed); + let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res); - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|d_i| { - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - tmp_pt.data.zero(); // zeroes for next iteration - self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); - self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res); + for col_i in 0..rank_in { + for d_i in 0..dnum { + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + tmp_pt.data.zero(); // zeroes for next iteration + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); - let (seed, mut source_xa_tmp) = source_xa.branch(); - res.seed[col_i * dnum + d_i] = seed; + let (seed, mut source_xa_tmp) = source_xa.branch(); + seeds[col_i * dnum + d_i] = seed; - self.glwe_encrypt_sk_internal( - res.base2k().into(), - res.k().into(), - &mut res.at_mut(d_i, col_i).data, - cols, - true, - Some((&tmp_pt, 0)), - sk, - &mut source_xa_tmp, - source_xe, - SIGMA, - scrach_1, - ); - }); - }); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(d_i, col_i).data, + cols, + true, + Some((&tmp_pt, 0)), + sk, + &mut source_xa_tmp, + source_xe, + SIGMA, + scrach_1, + ); + } + } + } + + res.seed_mut().copy_from_slice(&seeds); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 1184c03..9a37d17 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -9,11 +9,10 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, - encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, - encryption::gglwe_tsk::TensorKeyEncryptSk, + GetDistribution, ScratchTakeCore, + encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk}, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -37,7 +36,7 @@ impl TensorKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, M: GGLWETensorKeyCompressedEncryptSk, { module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); @@ -58,7 +57,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, - S: GLWESecretToRef + GetDist; + S: GLWESecretToRef + GetDistribution; } impl GGLWETensorKeyCompressedEncryptSk for Module @@ -95,7 +94,7 @@ where scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, { let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index ec45398..4526faf 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSWInfos, GLWEInfos, LWEInfos, + GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos, compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -83,62 +83,66 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - 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(); + let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()]; - #[cfg(debug_assertions)] { - use poulpy_hal::layouts::ZnxInfos; + 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(); - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } + #[cfg(debug_assertions)] + { + use poulpy_hal::layouts::ZnxInfos; - let base2k: usize = res.base2k().into(); - let rank: usize = res.rank().into(); - let cols: usize = rank + 1; - let dsize: usize = res.dsize().into(); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + } - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + let dsize: usize = res.dsize().into(); - let mut source = Source::new(seed_xa); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); - res.seed = vec![[0u8; 32]; res.dnum().0 as usize * cols]; + let mut source = Source::new(seed_xa); - for row_i in 0..res.dnum().into() { - tmp_pt.data.zero(); + for row_i in 0..res.dnum().into() { + tmp_pt.data.zero(); - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); - self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); - for col_j in 0..rank + 1 { - // rlwe encrypt of vec_znx_pt into vec_znx_ct + for col_j in 0..rank + 1 { + // rlwe encrypt of vec_znx_pt into vec_znx_ct - let (seed, mut source_xa_tmp) = source.branch(); + let (seed, mut source_xa_tmp) = source.branch(); - res.seed[row_i * cols + col_j] = seed; + seeds[row_i * cols + col_j] = seed; - self.glwe_encrypt_sk_internal( - res.base2k().into(), - res.k().into(), - &mut res.at_mut(row_i, col_j).data, - cols, - true, - Some((&tmp_pt, col_j)), - sk, - &mut source_xa_tmp, - source_xe, - SIGMA, - scratch_1, - ); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(row_i, col_j).data, + cols, + true, + Some((&tmp_pt, col_j)), + sk, + &mut source_xa_tmp, + source_xe, + SIGMA, + scratch_1, + ); + } } } + + res.seed_mut().copy_from_slice(&seeds); } } diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index a30001c..e2b1414 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -9,7 +9,7 @@ use crate::{ glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GLWEInfos, GLWEPlaintextToRef, LWEInfos, + GLWECompressedSeedMut, GLWEInfos, GLWEPlaintextToRef, LWEInfos, compressed::{GLWECompressed, GLWECompressedToMut}, prepared::GLWESecretPreparedToRef, }, @@ -58,7 +58,7 @@ pub trait GLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECompressedToMut, + R: GLWECompressedToMut + GLWECompressedSeedMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; } @@ -83,28 +83,30 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECompressedToMut, + R: GLWECompressedToMut + GLWECompressedSeedMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); - let mut source_xa: Source = Source::new(seed_xa); - let cols: usize = (res.rank() + 1).into(); + { + let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); + let mut source_xa: Source = Source::new(seed_xa); + let cols: usize = (res.rank() + 1).into(); - self.glwe_encrypt_sk_internal( - res.base2k().into(), - res.k().into(), - &mut res.data, - cols, - true, - Some((pt, 0)), - sk, - &mut source_xa, - source_xe, - SIGMA, - scratch, - ); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.data, + cols, + true, + Some((pt, 0)), + sk, + &mut source_xa, + source_xe, + SIGMA, + scratch, + ); + } - res.seed = seed_xa; + res.seed_mut().copy_from_slice(&seed_xa); } } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index d10af38..6867b55 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -8,10 +8,10 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, + GetDistribution, ScratchTakeCore, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GetDist, LWEInfos, Rank, TensorKey, TensorKeyToMut, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -36,7 +36,7 @@ impl TensorKey { scratch: &mut Scratch, ) where M: TensorKeyEncryptSk, - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, Scratch: ScratchTakeCore, { module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); @@ -57,7 +57,7 @@ pub trait TensorKeyEncryptSk { scratch: &mut Scratch, ) where R: TensorKeyToMut, - S: GLWESecretToRef + GetDist; + S: GLWESecretToRef + GetDistribution; } impl TensorKeyEncryptSk for Module @@ -95,7 +95,7 @@ where scratch: &mut Scratch, ) where R: TensorKeyToMut, - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, { let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 5877f37..0e684ac 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -510,6 +510,8 @@ where // ct[i] = uniform (+ pt) self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); + println!("vec_znx_fill_uniform: {}", ct); + let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, size); // ci = ct[i] - pt diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index d30fe62..f4a8c35 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ }; use crate::{ - Distribution, ScratchTakeCore, + Distribution, GetDistribution, GetDistributionMut, ScratchTakeCore, encryption::glwe_ct::GLWEEncryptSk, layouts::{ GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos, @@ -16,7 +16,7 @@ use crate::{ impl GLWEPublicKey { pub fn generate(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef + GetDistribution, M: GLWEPublicKeyGenerate, { module.glwe_public_key_generate(self, sk, source_xa, source_xe); @@ -26,8 +26,8 @@ impl GLWEPublicKey { pub trait GLWEPublicKeyGenerate { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - R: GLWEPublicKeyToMut, - S: GLWESecretPreparedToRef; + R: GLWEPublicKeyToMut + GetDistributionMut, + S: GLWESecretPreparedToRef + GetDistribution; } impl GLWEPublicKeyGenerate for Module @@ -38,25 +38,27 @@ where { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - R: GLWEPublicKeyToMut, - S: GLWESecretPreparedToRef, + R: GLWEPublicKeyToMut + GetDistributionMut, + S: GLWESecretPreparedToRef + GetDistribution, { - let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); + { + let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(sk.n(), self.n() as u32); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); - if sk.dist == Distribution::NONE { - panic!("invalid sk: SecretDistribution::NONE") + if sk.dist == Distribution::NONE { + panic!("invalid sk: SecretDistribution::NONE") + } + + // Its ok to allocate scratch space here since pk is usually generated only once. + let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); + + let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); + + tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); } - - // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); - - let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); - - tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); - res.dist = sk.dist; + *res.dist_mut() = *sk.dist(); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 235cc4d..13d13f9 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -23,6 +23,25 @@ pub struct GGLWECompressed { pub(crate) seed: Vec<[u8; 32]>, } +pub trait GGLWECompressedSeedMut { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]>; +} + +impl GGLWECompressedSeedMut for GGLWECompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.seed + } +} + +pub trait GGLWECompressedSeed { + fn seed(&self) -> &Vec<[u8; 32]>; +} + +impl GGLWECompressedSeed for GGLWECompressed { + fn seed(&self) -> &Vec<[u8; 32]> { + &self.seed + } +} impl LWEInfos for GGLWECompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index 454dae3..d12a85b 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -22,6 +22,26 @@ pub struct GGSWCompressed { pub(crate) seed: Vec<[u8; 32]>, } +pub trait GGSWCompressedSeedMut { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]>; +} + +impl GGSWCompressedSeedMut for GGSWCompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.seed + } +} + +pub trait GGSWCompressedSeed { + fn seed(&self) -> &Vec<[u8; 32]>; +} + +impl GGSWCompressedSeed for GGSWCompressed { + fn seed(&self) -> &Vec<[u8; 32]> { + &self.seed + } +} + impl LWEInfos for GGSWCompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 00f125a..98115b6 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -19,6 +19,26 @@ pub struct GLWECompressed { pub(crate) seed: [u8; 32], } +pub trait GLWECompressedSeedMut { + fn seed_mut(&mut self) -> &mut [u8; 32]; +} + +impl GLWECompressedSeedMut for GLWECompressed { + fn seed_mut(&mut self) -> &mut [u8; 32] { + &mut self.seed + } +} + +pub trait GLWECompressedSeed { + fn seed(&self) -> &[u8; 32]; +} + +impl GLWECompressedSeed for GLWECompressed { + fn seed(&self) -> &[u8; 32] { + &self.seed + } +} + impl LWEInfos for GLWECompressed { fn base2k(&self) -> Base2K { self.base2k diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index 80b0a9d..b601e82 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -3,6 +3,7 @@ use poulpy_hal::layouts::{ }; use crate::{ + GetDistribution, GetDistributionMut, dist::Distribution, layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; @@ -16,6 +17,18 @@ pub struct GLWEPublicKey { pub(crate) dist: Distribution, } +impl GetDistributionMut for GLWEPublicKey { + fn dist_mut(&mut self) -> &mut Distribution { + &mut self.dist + } +} + +impl GetDistribution for GLWEPublicKey { + fn dist(&self) -> &Distribution { + &self.dist + } +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPublicKeyLayout { pub n: Degree, @@ -24,16 +37,6 @@ pub struct GLWEPublicKeyLayout { pub rank: Rank, } -pub trait GetDist { - fn get_dist(&self) -> Distribution; -} - -impl GetDist for GLWEPublicKey { - fn get_dist(&self) -> Distribution { - self.dist - } -} - impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { self.base2k diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 51b2f0d..2d4ade1 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -7,8 +7,9 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -64,9 +65,9 @@ impl LWEInfos for GLWESecret { } } -impl GetDist for GLWESecret { - fn get_dist(&self) -> Distribution { - self.dist +impl GetDistribution for GLWESecret { + fn dist(&self) -> &Distribution { + &self.dist } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 5212a11..f186d22 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -4,8 +4,9 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] @@ -16,13 +17,15 @@ pub struct GLWEPublicKeyPrepared { pub(crate) dist: Distribution, } -pub trait SetDist { - fn set_dist(&mut self, dist: Distribution); +impl GetDistribution for GLWEPublicKeyPrepared { + fn dist(&self) -> &Distribution { + &self.dist + } } -impl SetDist for GLWEPublicKeyPrepared { - fn set_dist(&mut self, dist: Distribution) { - self.dist = dist +impl GetDistributionMut for GLWEPublicKeyPrepared { + fn dist_mut(&mut self) -> &mut Distribution { + &mut self.dist } } @@ -122,8 +125,8 @@ where { fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where - R: GLWEPublicKeyPreparedToMut + SetDist, - O: GLWEPublicKeyToRef + GetDist, + R: GLWEPublicKeyPreparedToMut + GetDistributionMut, + O: GLWEPublicKeyToRef + GetDistribution, { { let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); @@ -140,7 +143,7 @@ where } } - res.set_dist(other.get_dist()); + *res.dist_mut() = *other.dist(); } } @@ -149,7 +152,7 @@ impl GLWEPublicKeyPrepare for Module where Self: GetDegree + V impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) where - O: GLWEPublicKeyToRef + GetDist, + O: GLWEPublicKeyToRef + GetDistribution, M: GLWEPublicKeyPrepare, { module.prepare_glwe_public_key(self, other); diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 5eb9e07..3633e3f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -4,11 +4,9 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision, - prepared::SetDist, - }, + layouts::{Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, LWEInfos, Rank, TorusPrecision}, }; pub struct GLWESecretPrepared { @@ -16,9 +14,15 @@ pub struct GLWESecretPrepared { pub(crate) dist: Distribution, } -impl SetDist for GLWESecretPrepared { - fn set_dist(&mut self, dist: Distribution) { - self.dist = dist +impl GetDistribution for GLWESecretPrepared { + fn dist(&self) -> &Distribution { + &self.dist + } +} + +impl GetDistributionMut for GLWESecretPrepared { + fn dist_mut(&mut self) -> &mut Distribution { + &mut self.dist } } @@ -125,8 +129,8 @@ where { fn prepare_glwe_secret(&self, res: &mut R, other: &O) where - R: GLWESecretPreparedToMut + SetDist, - O: GLWESecretToRef + GetDist, + R: GLWESecretPreparedToMut + GetDistributionMut, + O: GLWESecretToRef + GetDistribution, { { let mut res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); @@ -137,7 +141,7 @@ where } } - res.set_dist(other.get_dist()); + *res.dist_mut() = *other.dist(); } } @@ -147,7 +151,7 @@ impl GLWESecretPrepared { pub fn prepare(&mut self, module: &M, other: &O) where M: GLWESecretPrepare, - O: GLWESecretToRef + GetDist, + O: GLWESecretToRef + GetDistribution, { module.prepare_glwe_secret(self, other); } diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 37d39aa..874a879 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -14,7 +14,7 @@ backend_test_suite!( tests = { //GLWE Encryption glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, - lwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, + glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs index 4d912fe..970002d 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -104,6 +104,7 @@ where let k_pt: usize = 30; for rank in 1_usize..3 { + println!("rank: {}", rank); let n: usize = module.n(); let glwe_infos: GLWELayout = GLWELayout {