ct, pk, sk, ksk for glwe and lwe
@@ -6,7 +6,6 @@ VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
VecZnxSwitchRing, }, layouts::{Backend, DataMut, Module, Scratch}, - layouts::{Backend, DataMut, Module, Scratch}, source::Source, };@@ -17,8 +16,6 @@ AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos,
}, }; -impl AutomorphismKey<Vec<u8>> { - pub fn encrypt_sk_tmp_bytes<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize impl AutomorphismKey<Vec<u8>> { pub fn encrypt_sk_tmp_bytes<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize where@@ -31,10 +28,8 @@ infos.rank_out(),
"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<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize pub fn encrypt_pk_tmp_bytes<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize where A: GGLWEInfos,@@ -44,7 +39,6 @@ _infos.rank_in(),
_infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) } }@@ -68,34 +62,12 @@ where
Self: AutomorphismKeyToMut, { pub fn encrypt_sk<S, BE: Backend>( -pub trait GGLWEAutomorphismKeyEncryptSk<BE: Backend> { - fn gglwe_automorphism_key_encrypt_sk<A, B>( - &self, - res: &mut A, - p: i64, - sk: &B, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch<BE>, - ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef; -} - -impl<DM: DataMut> AutomorphismKey<DM> -where - Self: AutomorphismKeyToMut, -{ - pub fn encrypt_sk<S, BE: Backend>( &mut self, module: &Module<BE>, - module: &Module<BE>, p: i64, - sk: &S, sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>, ) where S: GLWESecretToRef,@@ -152,16 +124,10 @@
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) ) }@@ -172,9 +138,6 @@ {
(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), &mut sk_out.data.as_vec_znx_mut(), i, &sk.data.as_vec_znx(),@@ -185,10 +148,7 @@ }
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; } -} +}
@@ -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 @@ },
}; impl GGLWE<Vec<u8>> { - pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: GGLWEEncryptSk<BE>, { - 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<B: Backend, A>(_module: &Module<B>, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GGLWEEncryptSk<BE>, { - unimplemented!() + module.gglwe_encrypt_sk_tmp_bytes(infos) } } pub trait GGLWEEncryptSk<B: Backend> { + + fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn gglwe_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos; + fn gglwe_encrypt_sk<R, P, S>( &self, res: &mut R,@@ -47,9 +58,34 @@ }
impl<B: Backend> GGLWEEncryptSk<B> for Module<B> where - Module<B>: ModuleN + GLWEEncryptSk<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace<B>, + Module<B>: ModuleN + + GLWEEncryptSk<B> + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAddScalarInplace + + VecZnxNormalizeInplace<B>, + // + SvpPPolAllocBytesImpl<B>, Scratch<B>: ScratchAvailable + ScratchTakeCore<B>, { + fn gglwe_encrypt_sk_tmp_bytes<A>(&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<A>(&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<R, P, S>( &self, res: &mut R,
@@ -12,27 +12,45 @@
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<Vec<u8>> { - pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module<B>: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc<B>, + M: GLWESwitchingKeyEncryptSk<BE>, { - (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<B: Backend, A>(module: &Module<B>, _infos: &A) -> usize + + pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GLWESwitchingKeyEncryptSk<BE>, { - GGLWE::encrypt_pk_tmp_bytes(module, _infos) + module.glwe_switching_key_encrypt_pk_tmp_bytes(infos) } + + // pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module<B>: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc<B>, + // { + // (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<B: Backend, A>(module: &Module<B>, _infos: &A) -> usize + // where + // A: GGLWEInfos, + // { + // GGLWE::encrypt_pk_tmp_bytes(module, _infos) + // } } impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {@@ -112,3 +130,50 @@ self.sk_in_n = sk_in.n().into();
self.sk_out_n = sk_out.n().into(); } } + + +pub trait GLWESwitchingKeyEncryptSk<BE: Backend> +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc<BE> + + GGLWEEncryptSk<BE>, +{ + fn glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where + Self: ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc<BE> + + GGLWEEncryptSk<BE>, +{ + + fn glwe_switching_key_encrypt_sk_tmp_bytes<A>(&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<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + GGLWE::encrypt_pk_tmp_bytes(self, infos) + } + +}
@@ -19,25 +19,21 @@ },
}; impl GLWE<Vec<u8>> { - pub fn encrypt_sk_tmp_bytes<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module<BE>: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GLWEEncryptSk<BE>, { - 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<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize + + pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module<BE>: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, + M: GLWEEncryptPk<BE> { - 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 @@ source_xe: &mut Source,
scratch: &mut Scratch<BE>, ) where S: GLWESecretPreparedToRef<BE>, - Module<BE>: GLWEEncryptZeroSk<BE>, + Module<BE>: GLWEEncryptSk<BE>, { module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); }@@ -97,13 +93,20 @@ source_xe: &mut Source,
scratch: &mut Scratch<BE>, ) where K: GLWEPublicKeyPreparedToRef<BE>, - Module<BE>: GLWEEncryptZeroPk<BE>, + Module<BE>: GLWEEncryptPk<BE>, { module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); } } pub trait GLWEEncryptSk<BE: Backend> { + fn glwe_encrypt_sk_tmp_bytes<A>( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos; + fn glwe_encrypt_sk<R, P, S>( &self, res: &mut R,@@ -116,13 +119,40 @@ ) where
R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef<BE>; + + fn glwe_encrypt_zero_sk<R, S>( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch<BE>, + ) where + R: GLWEToMut, + S: GLWESecretPreparedToRef<BE>; } impl<BE: Backend> GLWEEncryptSk<BE> for Module<BE> where - Module<BE>: GLWEEncryptSkInternal<BE> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module<BE>: Sized + + ModuleN + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + GLWEEncryptSkInternal<BE>, Scratch<BE>: ScratchAvailable, { + fn glwe_encrypt_sk_tmp_bytes<A>( + &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<R, P, S>( &self, res: &mut R,@@ -169,26 +199,7 @@ SIGMA,
scratch, ); } -} -pub trait GLWEEncryptZeroSk<BE: Backend> { - fn glwe_encrypt_zero_sk<R, S>( - &self, - res: &mut R, - sk: &S, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch<BE>, - ) where - R: GLWEToMut, - S: GLWESecretPreparedToRef<BE>; -} - -impl<BE: Backend> GLWEEncryptZeroSk<BE> for Module<BE> -where - Module<BE>: GLWEEncryptSkInternal<BE> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - Scratch<BE>: ScratchAvailable, -{ fn glwe_encrypt_zero_sk<R, S>( &self, res: &mut R,@@ -209,10 +220,10 @@ assert_eq!(res.rank(), sk.rank());
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 @@ source_xe,
SIGMA, scratch, ); - } + } } pub trait GLWEEncryptPk<BE: Backend> { + fn glwe_encrypt_pk_tmp_bytes<A>( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos; + fn glwe_encrypt_pk<R, P, K>( &self, res: &mut R,@@ -246,12 +264,43 @@ ) where
R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef<BE>; + + fn glwe_encrypt_zero_pk<R, K>( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch<BE>, + ) where + R: GLWEToMut, + K: GLWEPublicKeyPreparedToRef<BE>; + + } impl<BE: Backend> GLWEEncryptPk<BE> for Module<BE> where - Module<BE>: GLWEEncryptPkInternal<BE>, + Module<BE>: GLWEEncryptPkInternal<BE> + + VecZnxDftBytesOf + + SvpPPolBytesOf + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes, { + fn glwe_encrypt_pk_tmp_bytes<A>( + &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<R, P, K>( &self, res: &mut R,@@ -267,25 +316,7 @@ K: GLWEPublicKeyPreparedToRef<BE>,
{ self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); } -} -pub trait GLWEEncryptZeroPk<BE: Backend> { - fn glwe_encrypt_zero_pk<R, K>( - &self, - res: &mut R, - pk: &K, - source_xu: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch<BE>, - ) where - R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef<BE>; -} - -impl<BE: Backend> GLWEEncryptZeroPk<BE> for Module<BE> -where - Module<BE>: GLWEEncryptPkInternal<BE>, -{ fn glwe_encrypt_zero_pk<R, K>( &self, res: &mut R,
@@ -5,13 +5,27 @@ source::Source,
}; use crate::{ - encryption::glwe_ct::GLWEEncryptZeroSk, + encryption::glwe_ct::{GLWEEncryptSk}, layouts::{ GLWE, GLWEPublicKey, GLWEPublicKeyToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; +impl<D: DataMut> GLWEPublicKey<D> { + pub fn generate<S: DataRef, B: Backend>( + &mut self, + module: &Module<B>, + sk: &GLWESecretPrepared<S, B>, + source_xa: &mut Source, + source_xe: &mut Source, + ) where + Module<B>: GLWEPublicKeyGenerate<B>, + { + module.glwe_public_key_generate(self, sk, source_xa, source_xe); + } +} + pub trait GLWEPublicKeyGenerate<B: Backend> { fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where@@ -21,7 +35,7 @@ }
impl<B: Backend> GLWEPublicKeyGenerate<B> for Module<B> where - Module<B>: GLWEEncryptZeroSk<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module<B>: GLWEEncryptSk<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>, { fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)@@ -45,25 +59,11 @@ }
} // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(self, res)); + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(self, res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist; } -} - -impl<D: DataMut> GLWEPublicKey<D> { - pub fn generate<S: DataRef, B: Backend>( - &mut self, - module: &Module<B>, - sk: &GLWESecretPrepared<S, B>, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module<B>: GLWEPublicKeyGenerate<B>, - { - module.glwe_public_key_generate(self, sk, source_xa, source_xe); - } -} +}
@@ -23,9 +23,19 @@ module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe);
} } - pub trait LWEEncryptSk<BE: Backend> where + Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<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 + R: LWEToMut, + P: LWEPlaintextToRef, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>; +} + +impl<BE: Backend> LWEEncryptSk<BE> for Module<BE> where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>, { fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)@@ -93,9 +103,4 @@ (0..res.size()).for_each(|i| {
res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; }); } -} - -impl<BE: Backend> LWEEncryptSk<BE> for Module<BE> where - Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>, -{ }
@@ -10,38 +10,47 @@ source::Source,
}; 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<Vec<u8>> { - pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: LWESwitchingKeyEncrypt<BE>, { - 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<B: Backend, A>(module: &Module<B>, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + 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<D: DataMut> LWESwitchingKey<D> {@@ -107,3 +116,83 @@ scratch_2,
); } } + +pub trait LWESwitchingKeyEncrypt<BE: Backend> +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc<BE> + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace<BE> + + VecZnxAddScalarInplace + + VecZnxBigNormalize<BE> + + VecZnxDftApply<BE> + + SvpApplyDftToDftInplace<BE> + + VecZnxIdftApplyConsume<BE> + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace<BE> + + VecZnxAddNormal + + VecZnxNormalize<BE> + + VecZnxSub + + SvpPrepare<BE> + + VecZnxSwitchRing + + GLWESecretAlloc + + GLWESecretPreparedAlloc<BE> +{ + fn lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl<BE:Backend> LWESwitchingKeyEncrypt<BE> for Module<BE> where + Self: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc<BE> + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace<BE> + + VecZnxAddScalarInplace + + VecZnxBigNormalize<BE> + + VecZnxDftApply<BE> + + SvpApplyDftToDftInplace<BE> + + VecZnxIdftApplyConsume<BE> + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace<BE> + + VecZnxAddNormal + + VecZnxNormalize<BE> + + VecZnxSub + + SvpPrepare<BE> + + VecZnxSwitchRing + + GLWESecretAlloc + + GLWESecretPreparedAlloc<BE> +{ + fn lwe_switching_key_encrypt_sk_tmp_bytes<A>(&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) + } +}