diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 2586074..ab5fb27 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,66 +1,84 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, - VecZnxNormalizeTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + api::{ModuleN, ScratchAvailable, VecZnxAutomorphism}, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, + encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed}, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretAlloc, GLWESecretToRef, LWEInfos, + compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, }, }; impl AutomorphismKeyCompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: - ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + M: AutomorphismKeyCompressedEncryptSk, { - assert_eq!(module.n() as u32, infos.n()); - GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out()) + module.automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) } } -pub trait GGLWEAutomorphismKeyCompressedEncryptSk { - fn gglwe_automorphism_key_compressed_encrypt_sk( +impl AutomorphismKeyCompressed { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &M, + p: i64, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S: GLWESecretToRef, + M: AutomorphismKeyCompressedEncryptSk, + { + module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); + } +} + +pub trait AutomorphismKeyCompressedEncryptSk { + fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef; } -impl GGLWEAutomorphismKeyCompressedEncryptSk for Module +impl AutomorphismKeyCompressedEncryptSk for Module where - Module: ModuleN - + GGLWEKeyCompressedEncryptSk - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VecZnxAutomorphism - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, + Module: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, + Scratch: ScratchTakeCore, { - fn gglwe_automorphism_key_compressed_encrypt_sk( + fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + self.bytes_of_glwe_secret(infos.rank()) + } + + fn automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef, @@ -68,23 +86,20 @@ where let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - assert_eq!(res.n(), sk.n()); - assert_eq!(res.rank_out(), res.rank_in()); - assert_eq!(sk.rank(), res.rank_out()); - assert!( - scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", - scratch.available(), - AutomorphismKeyCompressed::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() >= AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", + scratch.available(), + AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) + ); let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { - (0..res.rank_out().into()).for_each(|i| { + for i in 0..res.rank_out().into(){ self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), @@ -92,28 +107,11 @@ where &sk.data.as_vec_znx(), i, ); - }); + }; } - self.gglwe_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); + self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); res.p = p; } } - -impl AutomorphismKeyCompressed { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - p: i64, - sk: &GLWESecret, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGLWEAutomorphismKeyCompressedEncryptSk, - { - module.gglwe_automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); - } -} diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 425a0e1..4ba0bcb 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,9 +1,6 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, - VecZnxNormalizeTmpBytes, VecZnxSwitchRing, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing}, + layouts::{Backend, DataMut, Module, ScalarZnx, Scratch}, source::Source, }; @@ -11,119 +8,118 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, RingDegree, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, - prepared::GLWESecretPrepared, + prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; impl GLWESwitchingKeyCompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: - ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + M: GLWESwitchingKeyCompressedEncryptSk, { - (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(module, infos.rank_out()) + module.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) } } -impl GLWESwitchingKeyCompressed { +impl GLWESwitchingKeyCompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + module: &M, + sk_in: &S1, + sk_out: &S2, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: GGLWEKeyCompressedEncryptSk, + S1: GLWESecretToRef, + S2: GLWESecretToRef, + M: GLWESwitchingKeyCompressedEncryptSk, { - module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch); + module.glwe_switching_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch); } } -pub trait GGLWEKeyCompressedEncryptSk { - fn gglwe_key_compressed_encrypt_sk( +pub trait GLWESwitchingKeyCompressedEncryptSk { + fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn glwe_switching_key_compressed_encrypt_sk( &self, res: &mut R, - sk_in: &SI, - sk_out: &SO, + sk_in: &S1, + sk_out: &S2, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWESwitchingKeyCompressedToMut, - SI: GLWESecretToRef, - SO: GLWESecretToRef; + S1: GLWESecretToRef, + S2: GLWESecretToRef; } -impl GGLWEKeyCompressedEncryptSk for Module +impl GLWESwitchingKeyCompressedEncryptSk for Module where - Module: ModuleN - + GGLWECompressedEncryptSk - + SvpPPolBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxSwitchRing - + SvpPrepare - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, + Module: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxSwitchRing, + Scratch: ScratchTakeCore, { - fn gglwe_key_compressed_encrypt_sk( + fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) + .max(ScalarZnx::bytes_of(self.n(), 1)) + + ScalarZnx::bytes_of(self.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of(self, infos.rank_out()) + } + + fn glwe_switching_key_compressed_encrypt_sk( &self, res: &mut R, - sk_in: &SI, - sk_out: &SO, + sk_in: &S1, + sk_out: &S2, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWESwitchingKeyCompressedToMut, - SI: GLWESecretToRef, - SO: GLWESecretToRef, + S1: GLWESecretToRef, + S2: GLWESecretToRef, { let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); - #[cfg(debug_assertions)] - { - use crate::layouts::GLWESwitchingKey; - - 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.gglwe_compressed_encrypt_sk_tmp_bytes(res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", + scratch.available(), + self.gglwe_compressed_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); - }); + } } self.gglwe_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index e165a05..5843030 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, + encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, @@ -49,7 +49,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { impl GGLWETensorKeyCompressedEncryptSk for Module where Module: ModuleN - + GGLWEKeyCompressedEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA @@ -119,7 +119,7 @@ where let (seed_xa_tmp, _) = source_xa.branch(); - self.gglwe_key_compressed_encrypt_sk( + self.glwe_switching_key_compressed_encrypt_sk( res.at_mut(i, j), &sk_ij, sk,