diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index d0ff213..99c7d0f 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -1,17 +1,14 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, - SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxBigBytesOf, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataViewMut, Module, Scratch}, }; -use crate::{ - layouts::{ - GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, GLWEToMut, GLWEPlaintextToMut, - prepared::{GLWESecretPreparedToRef, GLWESecretPrepared}, - } +use crate::layouts::{ + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToMut, GLWEToMut, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }; impl GLWE> { @@ -25,12 +22,12 @@ impl GLWE> { } impl GLWE { - pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) + pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) where P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, M: GLWEDecryption, - Scratch: ScratchTakeBasic, + Scratch: ScratchTakeBasic, { module.glwe_decrypt(self, pt, sk, scratch); } @@ -48,29 +45,23 @@ where + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace - + VecZnxBigNormalize + + VecZnxBigNormalize, { fn glwe_decrypt_tmp_bytes(&self, infos: &A) -> usize where - A: GLWEInfos + A: GLWEInfos, { let size: usize = infos.size(); (self.vec_znx_normalize_tmp_bytes() | self.bytes_of_vec_znx_dft(1, size)) + self.bytes_of_vec_znx_dft(1, size) } - fn glwe_decrypt( - &self, - res: &mut R, - pt: &mut P, - sk: &S, - scratch: &mut Scratch, - ) where + fn glwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S, scratch: &mut Scratch) + where R: GLWEToMut, P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, Scratch: ScratchTakeBasic, { - let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); @@ -117,10 +108,9 @@ where pt.base2k = res.base2k(); pt.k = pt.k().min(res.k()); } - } -impl GLWEDecryption for Module where +impl GLWEDecryption for Module where Self: ModuleN + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes @@ -132,4 +122,4 @@ impl GLWEDecryption for Module where + VecZnxBigAddSmallInplace + VecZnxBigNormalize { -} \ No newline at end of file +} diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index 1042b72..a847139 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -4,10 +4,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToMut, LWESecretToRef}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut}; -impl LWE -{ +impl LWE { pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: S) where P: LWEPlaintextToMut, @@ -21,16 +20,15 @@ impl LWE pub trait LWEDecrypt where - Self: Sized + ZnNormalizeInplace + Self: Sized + ZnNormalizeInplace, { fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: S) where - R: LWEToMut, - P: LWEPlaintextToMut, - S: LWESecretToRef, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + R: LWEToMut, + P: LWEPlaintextToMut, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { - let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut(); let sk: LWESecret<&[u8]> = sk.to_ref(); @@ -60,8 +58,4 @@ where } } -impl LWEDecrypt for Module where - Self: Sized + ZnNormalizeInplace -{ - -} \ No newline at end of file +impl LWEDecrypt for Module where Self: Sized + ZnNormalizeInplace {} diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 1ee58a6..2586074 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,5 +1,8 @@ use poulpy_hal::{ - api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, + api::{ + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, + VecZnxNormalizeTmpBytes, + }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; @@ -17,7 +20,8 @@ impl AutomorphismKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + Module: + ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { assert_eq!(module.n() as u32, infos.n()); GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out()) diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 3dca15f..425a0e1 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,5 +1,8 @@ use poulpy_hal::{ - api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing}, + api::{ + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, + VecZnxNormalizeTmpBytes, VecZnxSwitchRing, + }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; @@ -18,7 +21,8 @@ impl GLWESwitchingKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + Module: + ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 02393f1..e165a05 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,10 +1,10 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, - oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, + oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, source::Source, }; @@ -12,7 +12,7 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -21,7 +21,13 @@ impl TensorKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, + Module: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxBigBytesOf, { TensorKey::encrypt_sk_tmp_bytes(module, infos) } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index dfd1e97..dc760de 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,37 +1,30 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, - }, + api::{ScratchAvailable, VecZnxAutomorphism}, layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, - }, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos}, }; impl AutomorphismKey> { - pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptSk + M: AutomorphismKeyEncryptSk, { - module.gglwe_automorphism_key_encrypt_sk_tmp_bytes(infos) + module.automorphism_key_encrypt_sk_tmp_bytes(infos) } pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptPk + M: GGLWEAutomorphismKeyEncryptPk, { - module.gglwe_automorphism_key_encrypt_pk_tmp_bytes(infos) + module.automorphism_key_encrypt_pk_tmp_bytes(infos) } } @@ -39,9 +32,9 @@ impl AutomorphismKey where Self: AutomorphismKeyToMut, { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, p: i64, sk: &S, source_xa: &mut Source, @@ -49,57 +42,36 @@ where scratch: &mut Scratch, ) where S: GLWESecretToRef, - Module: GGLWEAutomorphismKeyEncryptSk, + M: AutomorphismKeyEncryptSk, { - module.gglwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); + module.automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); } } -pub trait GGLWEAutomorphismKeyEncryptSk { - fn gglwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait AutomorphismKeyEncryptSk { + fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_automorphism_key_encrypt_sk( + fn automorphism_key_encrypt_sk( &self, - res: &mut A, + res: &mut R, p: i64, - sk: &B, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef; + R: AutomorphismKeyToMut, + S: GLWESecretToRef; } -impl GGLWEAutomorphismKeyEncryptSk for Module +impl AutomorphismKeyEncryptSk for Module where - Module: ModuleN - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + VecZnxAutomorphism - + SvpPPolAlloc - + GaloisElement, - Scratch: ScratchAvailable + ScratchTakeCore, + Module: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, + Scratch: ScratchTakeCore, { - - fn gglwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -108,38 +80,33 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of_from_infos(self, &infos.glwe_layout()) + self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(self, infos) } - fn gglwe_automorphism_key_encrypt_sk( + fn automorphism_key_encrypt_sk( &self, - res: &mut A, + res: &mut R, p: i64, - sk: &B, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef, + R: AutomorphismKeyToMut, + S: GLWESecretToRef, { let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - 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!( - 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) - ) - } + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); + assert!( + scratch.available() >= self.automorphism_key_encrypt_sk_tmp_bytes(res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", + scratch.available(), + self.automorphism_key_encrypt_sk_tmp_bytes(res) + ); let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); @@ -162,45 +129,21 @@ where } } - pub trait GGLWEAutomorphismKeyEncryptPk { - fn gglwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + fn automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - } impl GGLWEAutomorphismKeyEncryptPk for Module where - Module: ModuleN - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeCore, + Module:, + Scratch: ScratchTakeCore, { - - fn gglwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + fn automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GLWESwitchingKey::encrypt_pk_tmp_bytes(self, infos) + unimplemented!() } -} \ 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 e8ae4f8..67d517d 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,20 +1,20 @@ use poulpy_hal::{ api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, - // oep::SvpPPolAllocBytesImpl, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ - encryption::glwe_ct::GLWEEncryptSk, layouts::GLWEInfos, ScratchTakeCore, + ScratchTakeCore, + encryption::glwe_ct::GLWEEncryptSk, + layouts::GLWEInfos, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToMut, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; impl GGLWE> { - pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, @@ -32,16 +32,31 @@ impl GGLWE> { } } -pub trait GGLWEEncryptSk { +impl GGLWE { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &M, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGLWEEncryptSk, + Scratch: ScratchTakeCore, + { + module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); + } +} +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, @@ -49,40 +64,29 @@ pub trait GGLWEEncryptSk { sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGLWEToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GGLWEEncryptSk for Module +impl GGLWEEncryptSk for Module where - Module: ModuleN - + GLWEEncryptSk + Module: ModuleN + + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace - + VecZnxNormalizeInplace, - Scratch: ScratchAvailable + ScratchTakeCore, + + VecZnxNormalizeInplace, + Scratch: 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()) - + self.glwe_encrypt_sk_tmp_bytes(infos) + + GLWEPlaintext::bytes_of_from_infos(self, infos).max(self.vec_znx_normalize_tmp_bytes()) } fn gglwe_encrypt_sk( @@ -92,54 +96,49 @@ where sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGLWEToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - let sk: GLWESecretPrepared<&[u8], B> = 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() >= GGLWE::encrypt_sk_tmp_bytes(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}", - scratch.available(), - res.rank_out(), - res.size(), - GGLWE::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() >= self.gglwe_encrypt_sk_tmp_bytes(res), + "scratch.available: {} < GGLWE::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}", + scratch.available(), + res.rank_out(), + res.size(), + self.gglwe_encrypt_sk_tmp_bytes(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(); @@ -147,7 +146,7 @@ where let rank_in: usize = res.rank_in().into(); let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); - // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns + // For each input column (i.e. rank) produces a GGLWE of rank_out+1 columns // // Example for ksk rank 2 to rank 3: // @@ -177,20 +176,3 @@ where } } } - -impl GGLWE { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGLWEEncryptSk, - { - module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); - } -} diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index bd13c2b..b55e5df 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -1,20 +1,16 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - ScratchTakeBasic, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing}, + layouts::{Backend, DataMut, Module, ScalarZnx, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, prepared::GLWESecretPrepared, - }, encryption::gglwe_ct::GGLWEEncryptSk, + layouts::{ + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, + prepared::GLWESecretPreparedAlloc, + }, }; impl GLWESwitchingKey> { @@ -36,209 +32,102 @@ impl GLWESwitchingKey> { } impl GLWESwitchingKey { - - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + sk_in: &S1, + sk_out: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where + S1: GLWESecretToRef, + S2: GLWESecretToRef, M: GLWESwitchingKeyEncryptSk, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchTakeCore, { module.glwe_switching_key_encrypt_sk(self, sk_in, sk_out, source_xa, source_xe, scratch); } - - - // #[allow(clippy::too_many_arguments)] - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk_in: &GLWESecret, - // sk_out: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // Module: ModuleN - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // assert!(sk_in.n().0 <= module.n() as u32); - // assert!(sk_out.n().0 <= module.n() as u32); - // assert!( - // scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self), - // "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", - // scratch.available(), - // GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self) - // ) - // } - - // // let n: usize = sk_in.n().max(sk_out.n()).into(); - - // let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into()); - // (0..sk_in.rank().into()).for_each(|i| { - // module.vec_znx_switch_ring( - // &mut sk_in_tmp.as_vec_znx_mut(), - // i, - // &sk_in.data.as_vec_znx(), - // i, - // ); - // }); - - // let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank()); - // { - // let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1); - // (0..sk_out.rank().into()).for_each(|i| { - // module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); - // module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); - // }); - // } - - // self.key.encrypt_sk( - // module, - // &sk_in_tmp, - // &sk_out_tmp, - // source_xa, - // source_xe, - // scratch_2, - // ); - // self.sk_in_n = sk_in.n().into(); - // self.sk_out_n = sk_out.n().into(); - // } } - -pub trait GLWESwitchingKeyEncryptSk -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk, -{ +pub trait GLWESwitchingKeyEncryptSk { fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn glwe_switching_key_encrypt_sk( + fn glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + sk_in: &S1, + sk_out: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) - where + ) where R: GLWESwitchingKeyToMut, - DataSkIn: DataRef, - DataSkOut: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore; - + S1: GLWESecretToRef, + S2: GLWESecretToRef, + Scratch: ScratchTakeCore; } -impl GLWESwitchingKeyEncryptSk for Module where - Self: ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk - + VecZnxSwitchRing - + SvpPrepare - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddInplace - + VecZnxAddNormal - + VecZnxSub +impl GLWESwitchingKeyEncryptSk for Module +where + Self: ModuleN + GGLWEEncryptSk + GLWESecretPreparedAlloc + VecZnxSwitchRing + SvpPrepare, + Scratch: ScratchTakeCore, { - 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)) + self.gglwe_encrypt_sk_tmp_bytes(infos) + .max(ScalarZnx::bytes_of(self.n(), 1)) + ScalarZnx::bytes_of(self.n(), infos.rank_in().into()) - + GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout()) + + self.bytes_of_glwe_secret_prepared_from_infos(infos) } - fn glwe_switching_key_encrypt_sk( + fn glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + sk_in: &S1, + sk_out: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) - where + ) where R: GLWESwitchingKeyToMut, - DataSkIn: DataRef, - DataSkOut: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore, + 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(); - #[cfg(debug_assertions)] - { - assert!(sk_in.n().0 <= self.n() as u32); - assert!(sk_out.n().0 <= self.n() as u32); - assert!( - scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", - scratch.available(), - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res) - ) - } - - // let n: usize = sk_in.n().max(sk_out.n()).into(); + assert!(sk_in.n().0 <= self.n() as u32); + assert!(sk_out.n().0 <= self.n() as u32); + assert!( + scratch.available() >= self.glwe_switching_key_encrypt_sk_tmp_bytes(res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", + scratch.available(), + self.glwe_switching_key_encrypt_sk_tmp_bytes(res) + ); let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); - (0..sk_in.rank().into()).for_each(|i| { + for i in 0..sk_in.rank().into() { self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), i, &sk_in.data.as_vec_znx(), i, ); - }); + } let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); - (0..sk_out.rank().into()).for_each(|i| { + for i in 0..sk_out.rank().into() { self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); - }); + } } res.key.encrypt_sk( @@ -251,39 +140,20 @@ impl GLWESwitchingKeyEncryptSk for Module where ); res.sk_in_n = sk_in.n().into(); res.sk_out_n = sk_out.n().into(); - } - + } } -pub trait GLWESwitchingKeyEncryptPk -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk, -{ - +pub trait GLWESwitchingKeyEncryptPk { fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; } -impl GLWESwitchingKeyEncryptPk for Module where - Self: ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk, -{ - - fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize +impl GLWESwitchingKeyEncryptPk for Module { + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize where A: GGLWEInfos, { - GGLWE::encrypt_pk_tmp_bytes(self, infos) + unimplemented!() } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 3f4a31b..f67d730 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,156 +1,54 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyTmpA, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, - oep::{VecZnxAddScalarInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxDftApplyImpl, SvpApplyDftToDftImpl, VecZnxIdftApplyTmpAImpl, VecZnxBigNormalizeImpl}, + layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, - prepared::GLWESecretPrepared, - }, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{ + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GetDist, LWEInfos, Rank, TensorKey, TensorKeyToMut, + prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, + }, }; impl TensorKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWETensorKeyEncryptSk + M: TensorKeyEncryptSk, { - module.gglwe_tensor_key_encrypt_sk_tmp_bytes(infos) + module.tensor_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 + VecZnxBigBytesOf, - // { - // GLWESecretPrepared::bytes_of(module, infos.rank_out()) - // + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) - // + module.bytes_of_vec_znx_big(1, 1) - // + module.bytes_of_vec_znx_dft(1, 1) - // + GLWESecret::bytes_of(module, Rank(1)) - // + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - // } } impl TensorKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk: &GLWESecret, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - M: GGLWETensorKeyEncryptSk, - GLWESecret: GetDist, + M: TensorKeyEncryptSk, + S: GLWESecretToRef + GetDist, Scratch: ScratchAvailable + ScratchTakeCore, { - module.gglwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); + module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); } - - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // GLWESecret: GetDist, - // Module: ModuleN - // + SvpApplyDftToDft - // + VecZnxIdftApplyTmpA - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + VecZnxBigAllocBytesImpl - // + VecZnxBigBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchTakeBasic + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // assert_eq!(self.rank_out(), sk.rank()); - // assert_eq!(self.n(), sk.n()); - // } - - // // let n: RingDegree = sk.n(); - // let rank: Rank = self.rank_out(); - - // let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank); - // sk_dft_prep.prepare(module, sk); - - // let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1); - - // (0..rank.into()).for_each(|i| { - // module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); - // }); - - // let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1); - // let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1)); - // let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1); - - // (0..rank.into()).for_each(|i| { - // (i..rank.into()).for_each(|j| { - // module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); - - // module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); - // module.vec_znx_big_normalize( - // self.base2k().into(), - // &mut sk_ij.data.as_vec_znx_mut(), - // 0, - // self.base2k().into(), - // &sk_ij_big, - // 0, - // scratch_5, - // ); - - // self.at_mut(i, j) - // .encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5); - // }); - // }) - // } } -pub trait GGLWETensorKeyEncryptSk -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxBigBytesOf, -{ - fn gglwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait TensorKeyEncryptSk { + fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -162,41 +60,33 @@ where S: GLWESecretToRef + GetDist; } -impl GGLWETensorKeyEncryptSk for Module where +impl TensorKeyEncryptSk for Module +where Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxBigBytesOf - + VecZnxAddScalarInplaceImpl - + VecZnxDftApply - + VecZnxDftApplyImpl - + SvpApplyDftToDftImpl + GLWESwitchingKeyEncryptSk + + VecZnxDftBytesOf + + VecZnxBigBytesOf + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + VecZnxDftApply + SvpApplyDftToDft - + VecZnxIdftApplyTmpAImpl - + VecZnxBigNormalizeImpl + VecZnxIdftApplyTmpA - + VecZnxBigNormalize - + VecZnxAddScalarInplaceImpl - + SvpPrepare, - Scratch: ScratchTakeBasic + ScratchTakeCore, + + VecZnxBigNormalize, + Scratch: ScratchTakeCore, { - fn gglwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { 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, Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + + 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, Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -217,11 +107,8 @@ impl GGLWETensorKeyEncryptSk for Module where let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - assert_eq!(res.rank_out(), sk.rank()); - assert_eq!(res.n(), sk.n()); - } + assert_eq!(res.rank_out(), sk.rank()); + assert_eq!(res.n(), sk.n()); let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank.into(), 1); @@ -251,6 +138,6 @@ impl GGLWETensorKeyEncryptSk for Module where res.at_mut(i, j) .encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5); }); - }) + }) } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 90bac6b..72b6846 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,14 +1,14 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero}, + api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ SIGMA, ScratchTakeCore, - encryption::glwe_ct::GLWEEncryptSkInternal, + encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -21,31 +21,23 @@ impl GGSW> { { module.ggsw_encrypt_sk_tmp_bytes(infos) } - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGSWInfos, - // Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - // { - // let size = infos.size(); - // GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) - // + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) - // + VecZnx::bytes_of(module.n(), 1, size) - // + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) - // } } -impl GGSW { +impl GGSW { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: GGSWEncryptSk, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGSWEncryptSk, + Scratch: ScratchTakeCore, { module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } @@ -55,7 +47,7 @@ pub trait GGSWEncryptSk { fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos; - + fn ggsw_encrypt_sk( &self, res: &mut R, @@ -74,19 +66,18 @@ impl GGSWEncryptSk for Module where Module: ModuleN + GLWEEncryptSkInternal - + VecZnxAddScalarInplace - + VecZnxNormalizeInplace + + GLWEEncryptSk + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes, + + VecZnxNormalizeInplace + + VecZnxAddScalarInplace, Scratch: ScratchTakeCore, { - fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { let size = infos.size(); - GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout()) + self.glwe_encrypt_sk_tmp_bytes(infos) + VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size) + VecZnx::bytes_of(self.n(), 1, size) + self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) @@ -109,15 +100,10 @@ where let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(pt.n(), self.n()); - assert_eq!(sk.n(), self.n() as u32); - } + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(pt.n(), self.n()); + assert_eq!(sk.n(), self.n() as u32); let k: usize = res.k().into(); let base2k: usize = res.base2k().into(); @@ -149,4 +135,4 @@ where } } } -} \ 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 a23d7c6..082e53f 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -1,15 +1,16 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, - VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, ScratchTakeBasic, + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, + VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ + ScratchTakeCore, dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ @@ -19,7 +20,6 @@ use crate::{ }; impl GLWE> { - pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, @@ -31,16 +31,16 @@ impl GLWE> { pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - M: GLWEEncryptPk + M: GLWEEncryptPk, { module.glwe_encrypt_pk_tmp_bytes(infos) } } impl GLWE { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, pt: &P, sk: &S, source_xa: &mut Source, @@ -49,28 +49,30 @@ impl GLWE { ) where P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, - Module: GLWEEncryptSk, + M: GLWEEncryptSk, + Scratch: ScratchTakeCore, { module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } - pub fn encrypt_zero_sk( + pub fn encrypt_zero_sk( &mut self, - module: &Module, + module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where S: GLWESecretPreparedToRef, - Module: GLWEEncryptSk, + M: GLWEEncryptSk, + Scratch: ScratchTakeCore, { module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); } - pub fn encrypt_pk( + pub fn encrypt_pk( &mut self, - module: &Module, + module: &M, pt: &P, pk: &K, source_xu: &mut Source, @@ -79,31 +81,28 @@ impl GLWE { ) where P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptPk, + M: GLWEEncryptPk, { module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch); } - pub fn encrypt_zero_pk( + pub fn encrypt_zero_pk( &mut self, - module: &Module, + module: &M, pk: &K, source_xu: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptPk, + M: 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 + fn glwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos; @@ -129,22 +128,15 @@ pub trait GLWEEncryptSk { scratch: &mut Scratch, ) where R: GLWEToMut, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } impl GLWEEncryptSk for Module where - Module: Sized - + ModuleN - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + GLWEEncryptSkInternal, + Module: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal, Scratch: ScratchAvailable, { - fn glwe_encrypt_sk_tmp_bytes( - &self, - infos: &A, - ) -> usize + fn glwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos, { @@ -166,23 +158,20 @@ where P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - let mut res: GLWE<&mut [u8]> = res.to_mut(); - let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let pt: &GLWEPlaintext<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(sk.n(), self.n() as u32); - assert_eq!(pt.n(), self.n() as u32); - assert!( - scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - GLWE::encrypt_sk_tmp_bytes(self, &res) - ) - } + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert_eq!(pt.n(), self.n() as u32); + assert!( + scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(res), + "scratch.available(): {} < GLWE::encrypt_sk_tmp_bytes: {}", + scratch.available(), + self.glwe_encrypt_sk_tmp_bytes(res) + ); let cols: usize = (res.rank() + 1).into(); self.glwe_encrypt_sk_internal( @@ -191,7 +180,7 @@ where res.data_mut(), cols, false, - Some((&pt, 0)), + Some((pt, 0)), sk, source_xa, source_xe, @@ -211,21 +200,18 @@ where R: GLWEToMut, S: GLWESecretPreparedToRef, { - let mut res: GLWE<&mut [u8]> = res.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); - 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() >= self.glwe_encrypt_sk_tmp_bytes(&res), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - self.glwe_encrypt_sk_tmp_bytes(&res) - ) - } + 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() >= self.glwe_encrypt_sk_tmp_bytes(res), + "scratch.available(): {} < GLWE::encrypt_sk_tmp_bytes: {}", + scratch.available(), + self.glwe_encrypt_sk_tmp_bytes(res) + ); let cols: usize = (res.rank() + 1).into(); self.glwe_encrypt_sk_internal( @@ -241,14 +227,11 @@ where SIGMA, scratch, ); - } + } } pub trait GLWEEncryptPk { - fn glwe_encrypt_pk_tmp_bytes( - &self, - infos: &A, - ) -> usize + fn glwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos; @@ -274,29 +257,20 @@ pub trait GLWEEncryptPk { scratch: &mut Scratch, ) where R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef; - - + K: GLWEPublicKeyPreparedToRef; } impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes, + Module: GLWEEncryptPkInternal + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { - fn glwe_encrypt_pk_tmp_bytes( - &self, - infos: &A, - ) -> usize + 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_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)).max(ScalarZnx::bytes_of(self.n(), 1))) + self.bytes_of_svp_ppol(1) + self.vec_znx_normalize_tmp_bytes() } @@ -383,15 +357,12 @@ where let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let pk: &GLWEPublicKeyPrepared<&[u8], BE> = &pk.to_ref(); - #[cfg(debug_assertions)] - { - assert_eq!(res.base2k(), pk.base2k()); - 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!(res.base2k(), pk.base2k()); + 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()); } let base2k: usize = pk.base2k().into(); @@ -517,7 +488,7 @@ where assert_eq!( ct.cols(), 1, - "invalid ciphertext: compressed tag=true but #cols={} != 1", + "invalid glwe: compressed tag=true but #cols={} != 1", ct.cols() ) } diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index b28f2fd..e4fad50 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,13 +1,14 @@ use poulpy_hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, - layouts::{Backend, DataMut, DataRef, Module, ScratchOwned}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, DataMut, DataRef, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - encryption::glwe_ct::{GLWEEncryptSk}, + Distribution, ScratchTakeCore, + encryption::glwe_ct::GLWEEncryptSk, layouts::{ - GLWE, GLWEPublicKey, GLWEPublicKeyToMut, + GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -33,37 +34,33 @@ pub trait GLWEPublicKeyGenerate { S: GLWESecretPreparedToRef; } -impl GLWEPublicKeyGenerate for Module +impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Module: GLWEEncryptSk, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where R: GLWEPublicKeyToMut, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use crate::{Distribution, layouts::LWEInfos}; + 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 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; } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 4ade4d9..94d0c62 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,23 +1,17 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, - ScratchTakeBasic - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, - oep::{ScratchAvailableImpl, VecZnxFillUniformImpl}, - source::Source + api::{ModuleN, VecZnxAutomorphismInplace}, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, + source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, GLWEToLWESwitchingKeyToMut, - prepared::GLWESecretPrepared, - }, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{ + GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, + LWESecret, LWESecretToRef, Rank, + prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, + }, }; impl GLWEToLWESwitchingKey> { @@ -28,187 +22,77 @@ impl GLWEToLWESwitchingKey> { { module.glwe_to_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, - // { - // GLWESecretPrepared::bytes_of(module, infos.rank_in()) - // + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in())) - // } } impl GLWEToLWESwitchingKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where M: GLWEToLWESwitchingKeyEncrypt, - DLwe: DataRef, - DGlwe: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore, + S1: LWESecretToRef, + S2: GLWESecretToRef, + Scratch: ScratchTakeCore, { module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } - // #[allow(clippy::too_many_arguments)] - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk_lwe: &LWESecret, - // sk_glwe: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // DLwe: DataRef, - // DGlwe: DataRef, - // Module: ModuleN - // + VecZnxAutomorphismInplace - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchAvailable + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // assert!(sk_lwe.n().0 <= module.n() as u32); - // } - - // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, 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)); - // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - - // self.0.encrypt_sk( - // module, - // sk_glwe, - // &sk_lwe_as_glwe, - // source_xa, - // source_xe, - // scratch_1, - // ); - // } } -pub trait GLWEToLWESwitchingKeyEncrypt -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchAvailableImpl - + VecZnxFillUniform - + VecZnxFillUniformImpl -{ +pub trait GLWEToLWESwitchingKeyEncrypt { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn glwe_to_lwe_switching_key_encrypt_sk( + fn glwe_to_lwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWEToLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore; + S1: LWESecretToRef, + S2: GLWESecretToRef, + R: GLWEToLWESwitchingKeyToMut; } -impl GLWEToLWESwitchingKeyEncrypt for Module where - Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchAvailableImpl - + ScratchTakeBasic - + ScratchTakeCore - + VecZnxFillUniform - + VecZnxFillUniformImpl - + GLWESwitchingKeyEncryptSk +impl GLWEToLWESwitchingKeyEncrypt for Module +where + Module: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Scratch: ScratchTakeCore, { - fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) } - fn glwe_to_lwe_switching_key_encrypt_sk( + fn glwe_to_lwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where + S1: LWESecretToRef, + S2: GLWESecretToRef, R: GLWEToLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore, { - 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(); - #[cfg(debug_assertions)] - { - assert!(sk_lwe.n().0 <= self.n() as u32); - } + assert!(sk_lwe.n().0 <= self.n() as u32); let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); sk_lwe_as_glwe.data.zero(); @@ -222,6 +106,6 @@ impl GLWEToLWESwitchingKeyEncrypt for Module where source_xa, source_xe, scratch_1, - ); + ); } -} \ 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 a91b2b7..4a5d6ac 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -7,11 +7,10 @@ use poulpy_hal::{ use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToRef, LWESecretToRef}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToRef, LWESecret, LWESecretToRef, LWEToMut}, }; impl LWE { - pub fn encrypt_sk(&mut self, module: &M, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where P: LWEPlaintextToRef, @@ -23,27 +22,24 @@ impl LWE { } } -pub trait LWEEncryptSk -where - Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, -{ +pub trait LWEEncryptSk { 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; + S: LWESecretToRef; } -impl LWEEncryptSk for Module where +impl LWEEncryptSk for Module +where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { 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, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let pt: &LWEPlaintext<&[u8]> = &pt.to_ref(); @@ -101,6 +97,6 @@ impl LWEEncryptSk for Module where (0..res.size()).for_each(|i| { res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; - }); + }); } -} \ 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 19c5fc2..348df2c 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -1,22 +1,17 @@ use poulpy_hal::{ - api::{ - ModuleN, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, + api::{ModuleN, VecZnxAutomorphismInplace}, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ + ScratchTakeCore, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{ - GLWESecretAlloc, - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut, + Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, - ScratchTakeCore, }; impl LWESwitchingKey> { @@ -27,145 +22,65 @@ impl LWESwitchingKey> { { module.lwe_switching_key_encrypt_sk_tmp_bytes(infos) } - } impl LWESwitchingKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - sk_lwe_in: &LWESecret, - sk_lwe_out: &LWESecret, + module: &M, + sk_lwe_in: &S1, + sk_lwe_out: &S2, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - DIn: DataRef, - DOut: DataRef, - Module: ModuleN - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchTakeCore, + S1: LWESecretToRef, + S2: LWESecretToRef, + M: LWESwitchingKeyEncrypt, { - #[cfg(debug_assertions)] - { - assert!(sk_lwe_in.n().0 <= self.n().0); - assert!(sk_lwe_out.n().0 <= self.n().0); - assert!(self.n().0 <= module.n() as u32); - } - - let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); - let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(module, Rank(1)); - - sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); - sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); - module.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2); - - sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); - sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); - module.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); - - self.0.encrypt_sk( - module, - &sk_in_glwe, - &sk_out_glwe, - source_xa, - source_xe, - scratch_2, - ); + module.lwe_switching_key_encrypt_sk(self, sk_lwe_in, sk_lwe_out, source_xa, source_xe, scratch); } } -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 -{ +pub trait LWESwitchingKeyEncrypt { fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; + + fn lwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe_in: &S1, + sk_lwe_out: &S2, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: LWESwitchingKeyToMut, + S1: LWESecretToRef, + S2: LWESecretToRef; } -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 - + GLWESwitchingKeyEncryptSk - , +impl LWESwitchingKeyEncrypt for Module +where + Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Scratch: ScratchTakeCore, { fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" @@ -174,4 +89,47 @@ impl LWESwitchingKeyEncrypt for Module where + GLWESecretPrepared::bytes_of(self, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } -} \ No newline at end of file + + #[allow(clippy::too_many_arguments)] + fn lwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe_in: &S1, + sk_lwe_out: &S2, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: LWESwitchingKeyToMut, + 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(); + + assert!(sk_lwe_in.n().0 <= res.n().0); + assert!(sk_lwe_out.n().0 <= res.n().0); + assert!(res.n() <= self.n() as u32); + + let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); + let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1)); + + sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); + sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2); + + sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); + sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); + 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, + &sk_in_glwe, + &sk_out_glwe, + source_xa, + source_xe, + scratch_2, + ); + } +} diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index a554e24..d51faf7 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,20 +1,16 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, - ScratchTakeBasic, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, - oep::{ScratchAvailableImpl}, + api::{ModuleN, VecZnxAutomorphismInplace}, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ - layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank}, ScratchTakeCore, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{ + GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, + LWEToGLWESwitchingKeyToMut, Rank, + }, }; impl LWEToGLWESwitchingKey> { @@ -25,198 +21,81 @@ impl LWEToGLWESwitchingKey> { { module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos) } - - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGLWEInfos, - // Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, - // { - // debug_assert_eq!( - // infos.rank_in(), - // Rank(1), - // "rank_in != 1 is not supported for LWEToGLWESwitchingKey" - // ); - // GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - // + GLWESecret::bytes_of(module, infos.rank_in()) - // } } impl LWEToGLWESwitchingKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where + S1: LWESecretToRef, + S2: GLWESecretToRef, M: LWEToGLWESwitchingKeyEncrypt, - DLwe: DataRef, - DGlwe: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchTakeCore, { module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } - // #[allow(clippy::too_many_arguments)] - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk_lwe: &LWESecret, - // sk_glwe: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // DLwe: DataRef, - // DGlwe: DataRef, - // Module: ModuleN - // + VecZnxAutomorphismInplace - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchAvailable + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // use crate::layouts::LWEInfos; - - // assert!(sk_lwe.n().0 <= module.n() as u32); - // } - - // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); - // sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); - // sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); - // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - - // self.0.encrypt_sk( - // module, - // &sk_lwe_as_glwe, - // sk_glwe, - // source_xa, - // source_xe, - // scratch_1, - // ); - // } } -pub trait LWEToGLWESwitchingKeyEncrypt -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchTakeBasic - + ScratchAvailableImpl -{ +pub trait LWEToGLWESwitchingKeyEncrypt { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn lwe_to_glwe_switching_key_encrypt_sk( + fn lwe_to_glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - R: LWEToGLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore; + S1: LWESecretToRef, + S2: GLWESecretToRef, + R: LWEToGLWESwitchingKeyToMut; } -impl LWEToGLWESwitchingKeyEncrypt for Module where - Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchAvailableImpl - + ScratchTakeBasic - + ScratchTakeCore - + GLWESwitchingKeyEncryptSk +impl LWEToGLWESwitchingKeyEncrypt for Module +where + Self: ModuleN + GLWESwitchingKeyEncryptSk + VecZnxAutomorphismInplace, + Scratch: ScratchTakeCore, { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { debug_assert_eq!( infos.rank_in(), Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) - + GLWESecret::bytes_of(self, infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self, infos.rank_in()) } - fn lwe_to_glwe_switching_key_encrypt_sk( + fn lwe_to_glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) where + ) where + S1: LWESecretToRef, + S2: GLWESecretToRef, R: LWEToGLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore, { let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); - - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; + let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); + let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); - assert!(sk_lwe.n().0 <= self.n() as u32); - } + assert!(sk_lwe.n().0 <= self.n() as u32); let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); @@ -232,4 +111,4 @@ impl LWEToGLWESwitchingKeyEncrypt for Module where scratch_1, ); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index ac7e11f..dd7876c 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -66,7 +66,7 @@ where fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { self.bytes_of_svp_ppol(rank.into()) } - fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_secret_prepared_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { @@ -98,7 +98,7 @@ impl GLWESecretPrepared, B> { A: GLWEInfos, M: GLWESecretPreparedAlloc, { - module.bytes_of_glwe_secret_from_infos(infos) + module.bytes_of_glwe_secret_prepared_from_infos(infos) } pub fn bytes_of(module: &M, rank: Rank) -> usize