diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 2e87707..41ecd61 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -6,7 +6,6 @@ use poulpy_hal::{ VecZnxSwitchRing, }, layouts::{Backend, DataMut, Module, Scratch}, - layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; @@ -17,8 +16,6 @@ use crate::{ }, }; -impl AutomorphismKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize impl AutomorphismKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where @@ -31,10 +28,8 @@ impl AutomorphismKey> { "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) } - pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, @@ -45,7 +40,6 @@ impl AutomorphismKey> { "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) - GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) } } @@ -63,25 +57,6 @@ pub trait GGLWEAutomorphismKeyEncryptSk { B: GLWESecretToRef; } -impl AutomorphismKey -where - Self: AutomorphismKeyToMut, -{ - pub fn encrypt_sk( -pub trait GGLWEAutomorphismKeyEncryptSk { - fn gglwe_automorphism_key_encrypt_sk( - &self, - res: &mut A, - p: i64, - sk: &B, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef; -} - impl AutomorphismKey where Self: AutomorphismKeyToMut, @@ -89,14 +64,11 @@ where pub fn encrypt_sk( &mut self, module: &Module, - module: &Module, p: i64, sk: &S, - sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - scratch: &mut Scratch, ) where S: GLWESecretToRef, Module: GGLWEAutomorphismKeyEncryptSk, @@ -149,29 +121,20 @@ where { use crate::layouts::{GLWEInfos, LWEInfos}; - assert_eq!(res.n(), sk.n()); - assert_eq!(res.rank_out(), res.rank_in()); - assert_eq!(sk.rank(), res.rank_out()); assert_eq!(res.n(), sk.n()); assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available(), AutomorphismKey::encrypt_sk_tmp_bytes(self, res) - AutomorphismKey::encrypt_sk_tmp_bytes(self, res) ) } let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { - (0..res.rank_out().into()).for_each(|i| { - self.vec_znx_automorphism( - self.galois_element_inv(p), (0..res.rank_out().into()).for_each(|i| { self.vec_znx_automorphism( self.galois_element_inv(p), @@ -183,12 +146,9 @@ where }); } - res.key - .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); res.key .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); res.p = p; - res.p = p; } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index d0ab3a0..1f7deec 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,5 +1,6 @@ use poulpy_hal::{ api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + // oep::SvpPPolAllocBytesImpl, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; @@ -13,24 +14,34 @@ use crate::{ }; impl GGLWE> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: GGLWEEncryptSk, { - GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) - + (GLWEPlaintext::bytes_of_from_infos(module, &infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) + module.gglwe_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(_module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GGLWEEncryptSk, { - unimplemented!() + module.gglwe_encrypt_sk_tmp_bytes(infos) } } pub trait GGLWEEncryptSk { + + fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn gglwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + fn gglwe_encrypt_sk( &self, res: &mut R, @@ -47,9 +58,34 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where - Module: ModuleN + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Module: ModuleN + + GLWEEncryptSk + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAddScalarInplace + + VecZnxNormalizeInplace, + // + SvpPPolAllocBytesImpl, Scratch: ScratchAvailable + ScratchTakeCore, { + fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout()) + + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes()) + } + + fn gglwe_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize + where + A: GGLWEInfos, + { + unimplemented!() + // TODO: Is this correct? + // GLWE::encrypt_pk_tmp_bytes(self, &infos.glwe_layout()) + // + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes()) + + } + fn gglwe_encrypt_sk( &self, res: &mut R, diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index c825c73..0ba6047 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -12,27 +12,45 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, }, + encryption::gglwe_ct::GGLWEEncryptSk, }; impl GLWESwitchingKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + M: GLWESwitchingKeyEncryptSk, { - (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) - + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) + module.glwe_switching_key_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize + + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GLWESwitchingKeyEncryptSk, { - GGLWE::encrypt_pk_tmp_bytes(module, _infos) + module.glwe_switching_key_encrypt_pk_tmp_bytes(infos) } + + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + // { + // (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + // + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + // + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) + // } + + // pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize + // where + // A: GGLWEInfos, + // { + // GGLWE::encrypt_pk_tmp_bytes(module, _infos) + // } } impl GLWESwitchingKey { @@ -112,3 +130,50 @@ impl GLWESwitchingKey { self.sk_out_n = sk_out.n().into(); } } + + +pub trait GLWESwitchingKeyEncryptSk +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc + + GGLWEEncryptSk, +{ + fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyEncryptSk for Module where + Self: ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc + + GGLWEEncryptSk, +{ + + fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + (GGLWE::encrypt_sk_tmp_bytes(self, infos) | ScalarZnx::bytes_of(self.n(), 1)) + + ScalarZnx::bytes_of(self.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout()) + } + + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + GGLWE::encrypt_pk_tmp_bytes(self, infos) + } + +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index fa314d5..a23d7c6 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -19,25 +19,21 @@ use crate::{ }; impl GLWE> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GLWEEncryptSk, { - let size: usize = infos.size(); - assert_eq!(module.n() as u32, infos.n()); - module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft(1, size) + module.glwe_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, + M: GLWEEncryptPk { - let size: usize = infos.size(); - assert_eq!(module.n() as u32, infos.n()); - ((module.bytes_of_vec_znx_dft(1, size) + module.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) - + module.bytes_of_svp_ppol(1) - + module.vec_znx_normalize_tmp_bytes() + module.glwe_encrypt_pk_tmp_bytes(infos) } } @@ -67,7 +63,7 @@ impl GLWE { scratch: &mut Scratch, ) where S: GLWESecretPreparedToRef, - Module: GLWEEncryptZeroSk, + Module: GLWEEncryptSk, { module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); } @@ -97,13 +93,20 @@ impl GLWE { scratch: &mut Scratch, ) where K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptZeroPk, + Module: GLWEEncryptPk, { module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); } } pub trait GLWEEncryptSk { + fn glwe_encrypt_sk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos; + fn glwe_encrypt_sk( &self, res: &mut R, @@ -116,13 +119,40 @@ pub trait GLWEEncryptSk { R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; + + fn glwe_encrypt_zero_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + S: GLWESecretPreparedToRef; } impl GLWEEncryptSk for Module where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module: Sized + + ModuleN + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + GLWEEncryptSkInternal, Scratch: ScratchAvailable, { + fn glwe_encrypt_sk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos, + { + let size: usize = infos.size(); + assert_eq!(self.n() as u32, infos.n()); + self.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(self.n(), 1, size) + self.bytes_of_vec_znx_dft(1, size) + } + fn glwe_encrypt_sk( &self, res: &mut R, @@ -169,26 +199,7 @@ where scratch, ); } -} -pub trait GLWEEncryptZeroSk { - fn glwe_encrypt_zero_sk( - &self, - res: &mut R, - sk: &S, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - R: GLWEToMut, - S: GLWESecretPreparedToRef; -} - -impl GLWEEncryptZeroSk for Module -where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - Scratch: ScratchAvailable, -{ fn glwe_encrypt_zero_sk( &self, res: &mut R, @@ -209,10 +220,10 @@ where assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); assert!( - scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), + scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(&res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GLWE::encrypt_sk_tmp_bytes(self, &res) + self.glwe_encrypt_sk_tmp_bytes(&res) ) } @@ -230,10 +241,17 @@ where SIGMA, scratch, ); - } + } } pub trait GLWEEncryptPk { + fn glwe_encrypt_pk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos; + fn glwe_encrypt_pk( &self, res: &mut R, @@ -246,12 +264,43 @@ pub trait GLWEEncryptPk { R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef; + + fn glwe_encrypt_zero_pk( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + K: GLWEPublicKeyPreparedToRef; + + } impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal, + Module: GLWEEncryptPkInternal + + VecZnxDftBytesOf + + SvpPPolBytesOf + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes, { + fn glwe_encrypt_pk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos, + { + let size: usize = infos.size(); + assert_eq!(self.n() as u32, infos.n()); + ((self.bytes_of_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(self.n(), 1)) + + self.bytes_of_svp_ppol(1) + + self.vec_znx_normalize_tmp_bytes() + } + fn glwe_encrypt_pk( &self, res: &mut R, @@ -267,25 +316,7 @@ where { self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); } -} -pub trait GLWEEncryptZeroPk { - fn glwe_encrypt_zero_pk( - &self, - res: &mut R, - pk: &K, - source_xu: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef; -} - -impl GLWEEncryptZeroPk for Module -where - Module: GLWEEncryptPkInternal, -{ fn glwe_encrypt_zero_pk( &self, res: &mut R, diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index 9073cfe..b28f2fd 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -5,13 +5,27 @@ use poulpy_hal::{ }; use crate::{ - encryption::glwe_ct::GLWEEncryptZeroSk, + encryption::glwe_ct::{GLWEEncryptSk}, layouts::{ GLWE, GLWEPublicKey, GLWEPublicKeyToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; +impl GLWEPublicKey { + pub fn generate( + &mut self, + module: &Module, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + ) where + Module: GLWEPublicKeyGenerate, + { + module.glwe_public_key_generate(self, sk, source_xa, source_xe); + } +} + pub trait GLWEPublicKeyGenerate { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where @@ -21,7 +35,7 @@ pub trait GLWEPublicKeyGenerate { impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptZeroSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) @@ -45,25 +59,11 @@ where } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(self, res)); + 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; } -} - -impl GLWEPublicKey { - pub fn generate( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module: GLWEPublicKeyGenerate, - { - module.glwe_public_key_generate(self, sk, source_xa, source_xe); - } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 5a743eb..a91b2b7 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -23,10 +23,20 @@ impl LWE { } } - pub trait LWEEncryptSk where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, +{ + fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + R: LWEToMut, + P: LWEPlaintextToRef, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl; +} + +impl LWEEncryptSk for Module where + Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, { fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where @@ -93,9 +103,4 @@ where res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; }); } -} - -impl LWEEncryptSk for Module where - Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, -{ } \ No newline at end of file diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index ed1c5d6..063240e 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -10,38 +10,47 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, - prepared::GLWESecretPrepared, + GLWESecretAlloc, + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, + prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, + ScratchTakeCore, }; impl LWESwitchingKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: LWESwitchingKeyEncrypt, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - GLWESecret::bytes_of(module, Rank(1)) - + GLWESecretPrepared::bytes_of(module, Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + module.lwe_switching_key_encrypt_sk_tmp_bytes(infos) } + + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + // { + // debug_assert_eq!( + // infos.dsize().0, + // 1, + // "dsize > 1 is not supported for LWESwitchingKey" + // ); + // debug_assert_eq!( + // infos.rank_in().0, + // 1, + // "rank_in > 1 is not supported for LWESwitchingKey" + // ); + // debug_assert_eq!( + // infos.rank_out().0, + // 1, + // "rank_out > 1 is not supported for LWESwitchingKey" + // ); + // GLWESecret::bytes_of(module, Rank(1)) + // + GLWESecretPrepared::bytes_of(module, Rank(1)) + // + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + // } } impl LWESwitchingKey { @@ -107,3 +116,83 @@ impl LWESwitchingKey { ); } } + +pub trait LWESwitchingKeyEncrypt +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPrepare + + VecZnxSwitchRing + + GLWESecretAlloc + + GLWESecretPreparedAlloc +{ + fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl LWESwitchingKeyEncrypt for Module where + Self: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPrepare + + VecZnxSwitchRing + + GLWESecretAlloc + + GLWESecretPreparedAlloc +{ + fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos + { + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + GLWESecret::bytes_of(self, Rank(1)) + + GLWESecretPrepared::bytes_of(self, Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + } +} \ No newline at end of file