diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 66fb932..2833bee 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, TorusPrecision, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }; use std::{hint::black_box, time::Duration}; @@ -104,7 +104,8 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let mut ksk_prepared: AutomorphismKeyPrepared, _> = AutomorphismKeyPrepared::alloc_from_infos(&module, &ksk); + let mut ksk_prepared: GLWEAutomorphismKeyPrepared, _> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(&module, &ksk); ksk_prepared.prepare(&module, &ksk, scratch.borrow()); move || { diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 3c9c9f3..6918ee1 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,8 +7,7 @@ use crate::{ ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, - prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, + AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, GetGaloisElement, SetGaloisElement, }, }; @@ -18,7 +17,7 @@ impl AutomorphismKey> { R: GGLWEInfos, A: GGLWEInfos, K: GGLWEInfos, - M: AutomorphismKeyAutomorphism, + M: GLWEAutomorphismKeyAutomorphism, { module.automorphism_key_automorphism_tmp_bytes(res_infos, a_infos, key_infos) } @@ -27,30 +26,30 @@ impl AutomorphismKey> { impl AutomorphismKey { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where - A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + A: GGLWEToRef + GetGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, - M: AutomorphismKeyAutomorphism, + M: GLWEAutomorphismKeyAutomorphism, { module.automorphism_key_automorphism(self, a, key, scratch); } pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, - M: AutomorphismKeyAutomorphism, + M: GLWEAutomorphismKeyAutomorphism, { module.automorphism_key_automorphism_inplace(self, key, scratch); } } -impl AutomorphismKeyAutomorphism for Module where +impl GLWEAutomorphismKeyAutomorphism for Module where Self: GaloisElement + GLWEAutomorphism + VecZnxAutomorphism { } -pub trait AutomorphismKeyAutomorphism +pub trait GLWEAutomorphismKeyAutomorphism where Self: GaloisElement + GLWEAutomorphism + VecZnxAutomorphism, { @@ -65,9 +64,9 @@ where fn automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) where - R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, - A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + R: GGLWEToMut + SetGaloisElement + GGLWEInfos, + A: GGLWEToRef + GetGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { assert!( @@ -121,8 +120,8 @@ where fn automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where - R: GGLWEToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement + GGLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + R: GGLWEToMut + SetGaloisElement + GetGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { assert_eq!( diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index 58b8b53..cf77758 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -7,7 +7,7 @@ use crate::{ GGSWExpandRows, ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetAutomorphismGaloisElement, + GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetGaloisElement, prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, }, }; @@ -35,7 +35,7 @@ impl GGSW { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where A: GGSWToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, @@ -45,7 +45,7 @@ impl GGSW { pub fn automorphism_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, @@ -78,7 +78,7 @@ where where R: GGSWToMut, A: GGSWToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { @@ -103,7 +103,7 @@ where fn ggsw_automorphism_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 5d973b6..2c05af5 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -8,9 +8,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, keyswitch_internal, - layouts::{ - GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos}, }; impl GLWE> { @@ -30,7 +28,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism(self, a, key, scratch); @@ -40,7 +38,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_add(self, a, key, scratch); @@ -50,7 +48,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub(self, a, key, scratch); @@ -60,7 +58,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_negate(self, a, key, scratch); @@ -69,7 +67,7 @@ impl GLWE { pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_inplace(self, key, scratch); @@ -78,7 +76,7 @@ impl GLWE { pub fn automorphism_add_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_add_inplace(self, key, scratch); @@ -87,7 +85,7 @@ impl GLWE { pub fn automorphism_sub_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_inplace(self, key, scratch); @@ -96,7 +94,7 @@ impl GLWE { pub fn automorphism_sub_negate_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_negate_inplace(self, key, scratch); @@ -124,7 +122,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { self.glwe_keyswitch(res, a, key, scratch); @@ -139,7 +137,7 @@ where fn glwe_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { self.glwe_keyswitch_inplace(res, key, scratch); @@ -155,7 +153,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -182,7 +180,7 @@ where fn glwe_automorphism_add_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -209,7 +207,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -237,7 +235,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -264,7 +262,7 @@ where fn glwe_automorphism_sub_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -290,7 +288,7 @@ where fn glwe_automorphism_sub_negate_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, + K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 072f3d5..461d07d 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -85,7 +85,7 @@ where rank: Rank(1), }; - let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(self, &glwe_layout); + let (mut tmp_glwe, scratch_1) = scratch.take_glwe(self, &glwe_layout); self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1); self.lwe_sample_extract(res, &tmp_glwe); } diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index 7d72ef5..90c71a2 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -50,7 +50,7 @@ where assert_eq!(ksk.n(), self.n() as u32); assert!(lwe.n() <= self.n() as u32); - let (mut glwe, scratch_1) = scratch.take_glwe_ct( + let (mut glwe, scratch_1) = scratch.take_glwe( self, &GLWELayout { n: ksk.n(), diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe.rs similarity index 100% rename from poulpy-core/src/decryption/glwe_ct.rs rename to poulpy-core/src/decryption/glwe.rs diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe.rs similarity index 100% rename from poulpy-core/src/decryption/lwe_ct.rs rename to poulpy-core/src/decryption/lwe.rs diff --git a/poulpy-core/src/decryption/mod.rs b/poulpy-core/src/decryption/mod.rs index 03e5fd4..4266117 100644 --- a/poulpy-core/src/decryption/mod.rs +++ b/poulpy-core/src/decryption/mod.rs @@ -1,5 +1,5 @@ -mod glwe_ct; -mod lwe_ct; +mod glwe; +mod lwe; -pub use glwe_ct::*; -pub use lwe_ct::*; +pub use glwe::*; +pub use lwe::*; diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe.rs similarity index 97% rename from poulpy-core/src/encryption/compressed/gglwe_ct.rs rename to poulpy-core/src/encryption/compressed/gglwe.rs index 1081525..1f502c5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe.rs @@ -6,10 +6,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - encryption::{ - SIGMA, - glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, - }, + encryption::{GLWEEncryptSk, GLWEEncryptSkInternal, SIGMA}, layouts::{ GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintext, GLWESecretPrepared, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, @@ -145,7 +142,7 @@ where let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(self, res); for col_i in 0..rank_in { for d_i in 0..dnum { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw.rs similarity index 96% rename from poulpy-core/src/encryption/compressed/ggsw_ct.rs rename to poulpy-core/src/encryption/compressed/ggsw.rs index 9d242c8..e1695b8 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal}, + encryption::{GGSWEncryptSk, GLWEEncryptSkInternal, SIGMA}, layouts::{ GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos, compressed::{GGSWCompressed, GGSWCompressedToMut}, @@ -107,7 +107,7 @@ where println!("res.seed: {:?}", res.seed); - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(self, &res.glwe_layout()); let mut source = Source::new(seed_xa); diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs similarity index 79% rename from poulpy-core/src/encryption/compressed/gglwe_atk.rs rename to poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index d6aa85f..713dd31 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -7,12 +7,12 @@ use poulpy_hal::{ use crate::{ GGLWECompressedEncryptSk, ScratchTakeCore, layouts::{ - GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared, - GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, compressed::AutomorphismKeyCompressed, + GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, + GLWESecretPreparedApi, GLWESecretToRef, LWEInfos, SetGaloisElement, compressed::GLWEAutomorphismKeyCompressed, }, }; -impl AutomorphismKeyCompressed> { +impl GLWEAutomorphismKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, @@ -22,7 +22,7 @@ impl AutomorphismKeyCompressed> { } } -impl AutomorphismKeyCompressed { +impl GLWEAutomorphismKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -54,18 +54,13 @@ pub trait AutomorphismKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetGaloisElement + GGLWEInfos, S: GLWESecretToRef + GLWEInfos; } impl AutomorphismKeyCompressedEncryptSk for Module where - Self: ModuleN - + GaloisElement - + VecZnxAutomorphism - + GGLWECompressedEncryptSk - + GLWESecretPreparedAlloc - + GLWESecretPrepare, + Self: ModuleN + GaloisElement + VecZnxAutomorphism + GGLWECompressedEncryptSk + GLWESecretPreparedApi, Scratch: ScratchTakeCore, { fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -87,7 +82,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetGaloisElement + GGLWEInfos, S: GLWESecretToRef + GLWEInfos, { let sk: &GLWESecret<&[u8]> = &sk.to_ref(); @@ -95,10 +90,10 @@ where 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() >= GLWEAutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", scratch.available(), - AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) + GLWEAutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) ); let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index e2b1414..47b8565 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -4,10 +4,7 @@ use poulpy_hal::{ }; use crate::{ - encryption::{ - SIGMA, - glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, - }, + encryption::{GLWEEncryptSk, GLWEEncryptSkInternal, SIGMA}, layouts::{ GLWECompressedSeedMut, GLWEInfos, GLWEPlaintextToRef, LWEInfos, compressed::{GLWECompressed, GLWECompressedToMut}, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs similarity index 94% rename from poulpy-core/src/encryption/compressed/gglwe_ksk.rs rename to poulpy-core/src/encryption/compressed/glwe_switching_key.rs index 7067250..cdc65b1 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs @@ -5,13 +5,12 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, - encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, + GGLWECompressedEncryptSk, ScratchTakeCore, layouts::{ GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKeyDegreesMut, LWEInfos, compressed::GLWESwitchingKeyCompressed, - prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, + prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, }, }; @@ -65,7 +64,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk { impl GLWESwitchingKeyCompressedEncryptSk for Module where - Self: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxSwitchRing, + Self: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedApi + VecZnxSwitchRing, Scratch: ScratchTakeCore, { fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs similarity index 92% rename from poulpy-core/src/encryption/compressed/gglwe_tsk.rs rename to poulpy-core/src/encryption/compressed/glwe_tensor_key.rs index dbbbcb5..76050b5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs @@ -8,15 +8,14 @@ use poulpy_hal::{ }; use crate::{ - GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, - encryption::gglwe_tsk::TensorKeyEncryptSk, + GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, TensorKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, Rank, - TensorKeyCompressedAtMut, compressed::TensorKeyCompressed, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedApi, GLWESecretToRef, LWEInfos, Rank, + TensorKeyCompressedAtMut, compressed::GLWETensorKeyCompressed, }, }; -impl TensorKeyCompressed> { +impl GLWETensorKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, @@ -26,7 +25,7 @@ impl TensorKeyCompressed> { } } -impl TensorKeyCompressed { +impl GLWETensorKeyCompressed { pub fn encrypt_sk( &mut self, module: &M, @@ -73,7 +72,7 @@ where + SvpPPolBytesOf + VecZnxDftBytesOf + VecZnxBigBytesOf - + GLWESecretPreparedAlloc, + + GLWESecretPreparedApi, Scratch: ScratchTakeBasic + ScratchTakeCore, { fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/compressed/mod.rs b/poulpy-core/src/encryption/compressed/mod.rs index a2f7b9a..e96eeb5 100644 --- a/poulpy-core/src/encryption/compressed/mod.rs +++ b/poulpy-core/src/encryption/compressed/mod.rs @@ -1,13 +1,13 @@ -mod gglwe_atk; -mod gglwe_ct; -mod gglwe_ksk; -mod gglwe_tsk; -mod ggsw_ct; +mod gglwe; +mod ggsw; +mod glwe_automorphism_key; mod glwe_ct; +mod glwe_switching_key; +mod glwe_tensor_key; -pub use gglwe_atk::*; -pub use gglwe_ct::*; -pub use gglwe_ksk::*; -pub use gglwe_tsk::*; -pub use ggsw_ct::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe_automorphism_key::*; pub use glwe_ct::*; +pub use glwe_switching_key::*; +pub use glwe_tensor_key::*; diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe.rs similarity index 97% rename from poulpy-core/src/encryption/gglwe_ct.rs rename to poulpy-core/src/encryption/gglwe.rs index fa80541..01f1bc3 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe.rs @@ -5,8 +5,7 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, - encryption::glwe_ct::GLWEEncryptSk, + GLWEEncryptSk, ScratchTakeCore, layouts::GLWEInfos, layouts::{ GGLWE, GGLWEInfos, GGLWEToMut, GLWEPlaintext, LWEInfos, @@ -144,7 +143,7 @@ where let base2k: usize = res.base2k().into(); let rank_in: usize = res.rank_in().into(); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(self, &res.glwe_layout()); // For each input column (i.e. rank) produces a GGLWE of rank_out+1 columns // // Example for ksk rank 2 to rank 3: diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw.rs similarity index 95% rename from poulpy-core/src/encryption/ggsw_ct.rs rename to poulpy-core/src/encryption/ggsw.rs index bc67822..c3bef71 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw.rs @@ -5,8 +5,7 @@ use poulpy_hal::{ }; use crate::{ - SIGMA, ScratchTakeCore, - encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, + GLWEEncryptSk, GLWEEncryptSkInternal, SIGMA, ScratchTakeCore, layouts::{ GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, @@ -110,7 +109,7 @@ where let dsize: usize = res.dsize().into(); let cols: usize = (rank + 1).into(); - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, res); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(self, res); for row_i in 0..res.dnum().into() { tmp_pt.data.zero(); diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe.rs similarity index 88% rename from poulpy-core/src/encryption/glwe_ct.rs rename to poulpy-core/src/encryption/glwe.rs index 85aa6e1..d4aca7d 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe.rs @@ -10,12 +10,12 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, + GetDistribution, ScratchTakeCore, dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ - GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, GLWEToMut, LWEInfos, - prepared::{GLWEPublicKeyPrepared, GLWEPublicKeyPreparedToRef, GLWESecretPrepared, GLWESecretPreparedToRef}, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, GLWEPrepared, GLWEPreparedToRef, GLWEToMut, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -80,7 +80,7 @@ impl GLWE { scratch: &mut Scratch, ) where P: GLWEPlaintextToRef + GLWEInfos, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPreparedToRef + GetDistribution + GLWEInfos, M: GLWEEncryptPk, { module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch); @@ -94,7 +94,7 @@ impl GLWE { source_xe: &mut Source, scratch: &mut Scratch, ) where - K: GLWEPublicKeyPreparedToRef, + K: GLWEPreparedToRef + GetDistribution + GLWEInfos, M: GLWEEncryptPk, { module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); @@ -246,7 +246,7 @@ pub trait GLWEEncryptPk { ) where R: GLWEToMut, P: GLWEPlaintextToRef + GLWEInfos, - K: GLWEPublicKeyPreparedToRef; + K: GLWEPreparedToRef + GetDistribution + GLWEInfos; fn glwe_encrypt_zero_pk( &self, @@ -257,7 +257,7 @@ pub trait GLWEEncryptPk { scratch: &mut Scratch, ) where R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef; + K: GLWEPreparedToRef + GetDistribution + GLWEInfos; } impl GLWEEncryptPk for Module @@ -286,7 +286,7 @@ where ) where R: GLWEToMut, P: GLWEPlaintextToRef + GLWEInfos, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPreparedToRef + GetDistribution + GLWEInfos, { self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); } @@ -300,7 +300,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPreparedToRef + GetDistribution + GLWEInfos, { self.glwe_encrypt_pk_internal( res, @@ -325,7 +325,7 @@ pub(crate) trait GLWEEncryptPkInternal { ) where R: GLWEToMut, P: GLWEPlaintextToRef + GLWEInfos, - K: GLWEPublicKeyPreparedToRef; + K: GLWEPreparedToRef + GetDistribution + GLWEInfos; } impl GLWEEncryptPkInternal for Module @@ -352,10 +352,9 @@ where ) where R: GLWEToMut, P: GLWEPlaintextToRef + GLWEInfos, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPreparedToRef + GetDistribution + GLWEInfos, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let pk: &GLWEPublicKeyPrepared<&[u8], BE> = &pk.to_ref(); assert_eq!(res.base2k(), pk.base2k()); assert_eq!(res.n(), pk.n()); @@ -374,52 +373,56 @@ where { let (mut u, _) = scratch_1.take_scalar_znx(self, 1); - match pk.dist { + match pk.dist() { Distribution::NONE => panic!( "invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \ Self::generate" ), - Distribution::TernaryFixed(hw) => u.fill_ternary_hw(0, hw, source_xu), - Distribution::TernaryProb(prob) => u.fill_ternary_prob(0, prob, source_xu), - Distribution::BinaryFixed(hw) => u.fill_binary_hw(0, hw, source_xu), - Distribution::BinaryProb(prob) => u.fill_binary_prob(0, prob, source_xu), - Distribution::BinaryBlock(block_size) => u.fill_binary_block(0, block_size, source_xu), + Distribution::TernaryFixed(hw) => u.fill_ternary_hw(0, *hw, source_xu), + Distribution::TernaryProb(prob) => u.fill_ternary_prob(0, *prob, source_xu), + Distribution::BinaryFixed(hw) => u.fill_binary_hw(0, *hw, source_xu), + Distribution::BinaryProb(prob) => u.fill_binary_prob(0, *prob, source_xu), + Distribution::BinaryBlock(block_size) => u.fill_binary_block(0, *block_size, source_xu), Distribution::ZERO => {} } self.svp_prepare(&mut u_dft, 0, &u, 0); } - // ct[i] = pk[i] * u + ei (+ m if col = i) - (0..cols).for_each(|i| { - let (mut ci_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, 1, size_pk); - // ci_dft = DFT(u) * DFT(pk[i]) - self.svp_apply_dft_to_dft(&mut ci_dft, 0, &u_dft, 0, &pk.data, i); + { + let pk: &GLWEPrepared<&[u8], BE> = &pk.to_ref(); - // ci_big = u * p[i] - let mut ci_big = self.vec_znx_idft_apply_consume(ci_dft); + // ct[i] = pk[i] * u + ei (+ m if col = i) + for i in 0..cols { + let (mut ci_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, 1, size_pk); + // ci_dft = DFT(u) * DFT(pk[i]) + self.svp_apply_dft_to_dft(&mut ci_dft, 0, &u_dft, 0, &pk.data, i); - // ci_big = u * pk[i] + e - self.vec_znx_big_add_normal( - base2k, - &mut ci_big, - 0, - pk.k().into(), - source_xe, - SIGMA, - SIGMA_BOUND, - ); + // ci_big = u * p[i] + let mut ci_big = self.vec_znx_idft_apply_consume(ci_dft); - // ci_big = u * pk[i] + e + m (if col = i) - if let Some((pt, col)) = pt - && col == i - { - self.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.to_ref().data, 0); + // ci_big = u * pk[i] + e + self.vec_znx_big_add_normal( + base2k, + &mut ci_big, + 0, + pk.k().into(), + source_xe, + SIGMA, + SIGMA_BOUND, + ); + + // ci_big = u * pk[i] + e + m (if col = i) + if let Some((pt, col)) = pt + && col == i + { + self.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.to_ref().data, 0); + } + + // ct[i] = norm(ci_big) + self.vec_znx_big_normalize(base2k, &mut res.data, i, base2k, &ci_big, 0, scratch_2); } - - // ct[i] = norm(ci_big) - self.vec_znx_big_normalize(base2k, &mut res.data, i, base2k, &ci_big, 0, scratch_2); - }); + } } } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs similarity index 90% rename from poulpy-core/src/encryption/gglwe_atk.rs rename to poulpy-core/src/encryption/glwe_automorphism_key.rs index b7469a2..bcd01c7 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/glwe_automorphism_key.rs @@ -7,8 +7,8 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, ScratchTakeCore, layouts::{ - AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared, - GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, + AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedApi, + GLWESecretToRef, LWEInfos, SetGaloisElement, }, }; @@ -64,18 +64,13 @@ pub trait AutomorphismKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, + R: GGLWEToMut + SetGaloisElement + GGLWEInfos, S: GLWESecretToRef; } impl AutomorphismKeyEncryptSk for Module where - Self: GGLWEEncryptSk - + VecZnxAutomorphism - + GaloisElement - + SvpPPolBytesOf - + GLWESecretPrepare - + GLWESecretPreparedAlloc, + Self: GGLWEEncryptSk + VecZnxAutomorphism + GaloisElement + SvpPPolBytesOf + GLWESecretPreparedApi, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -102,7 +97,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, + R: GGLWEToMut + SetGaloisElement + GGLWEInfos, S: GLWESecretToRef, { let sk: &GLWESecret<&[u8]> = &sk.to_ref(); diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_public_key.rs similarity index 84% rename from poulpy-core/src/encryption/glwe_pk.rs rename to poulpy-core/src/encryption/glwe_public_key.rs index 58b5b8a..460b037 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_public_key.rs @@ -5,10 +5,9 @@ use poulpy_hal::{ }; use crate::{ - Distribution, GetDistribution, GetDistributionMut, ScratchTakeCore, - encryption::glwe_ct::GLWEEncryptSk, + Distribution, GLWEEncryptSk, GetDistribution, GetDistributionMut, ScratchTakeCore, layouts::{ - GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos, + GLWE, GLWEInfos, GLWEPublicKey, GLWEToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -26,7 +25,7 @@ impl GLWEPublicKey { pub trait GLWEPublicKeyGenerate { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - R: GLWEPublicKeyToMut + GetDistributionMut, + R: GLWEToMut + GetDistributionMut + GLWEInfos, S: GLWESecretPreparedToRef + GetDistribution; } @@ -38,11 +37,10 @@ where { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - R: GLWEPublicKeyToMut + GetDistributionMut, + R: GLWEToMut + GetDistributionMut + GLWEInfos, S: GLWESecretPreparedToRef + GetDistribution, { { - let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); assert_eq!(res.n(), self.n() as u32); diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/glwe_switching_key.rs similarity index 96% rename from poulpy-core/src/encryption/gglwe_ksk.rs rename to poulpy-core/src/encryption/glwe_switching_key.rs index 2699e4a..cf6cf8e 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_switching_key.rs @@ -6,10 +6,10 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - encryption::gglwe_ct::GGLWEEncryptSk, + encryption::gglwe::GGLWEEncryptSk, layouts::{ GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, - prepared::GLWESecretPreparedAlloc, + prepared::GLWESecretPreparedApi, }, }; @@ -71,7 +71,7 @@ pub trait GLWESwitchingKeyEncryptSk { impl GLWESwitchingKeyEncryptSk for Module where - Self: ModuleN + GGLWEEncryptSk + GLWESecretPreparedAlloc + VecZnxSwitchRing + SvpPrepare, + Self: ModuleN + GGLWEEncryptSk + GLWESecretPreparedApi + VecZnxSwitchRing + SvpPrepare, Scratch: ScratchTakeCore, { fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/glwe_tensor_key.rs similarity index 96% rename from poulpy-core/src/encryption/gglwe_tsk.rs rename to poulpy-core/src/encryption/glwe_tensor_key.rs index 10f404a..21bf474 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/glwe_tensor_key.rs @@ -11,7 +11,7 @@ use crate::{ GGLWEEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut, - prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, + prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, }, }; @@ -65,8 +65,7 @@ where + GGLWEEncryptSk + VecZnxDftBytesOf + VecZnxBigBytesOf - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs similarity index 90% rename from poulpy-core/src/encryption/glwe_to_lwe_ksk.rs rename to poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs index 28a9778..d77eb60 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPrepare, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos, - LWESecret, LWESecretToRef, Rank, - prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, + Rank, + prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, }, }; @@ -65,10 +65,9 @@ impl GLWEToLWESwitchingKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxAutomorphismInplace - + VecZnxAutomorphismInplaceTmpBytes - + GLWESecretPrepare, + + VecZnxAutomorphismInplaceTmpBytes, Scratch: ScratchTakeCore, { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe.rs similarity index 100% rename from poulpy-core/src/encryption/lwe_ct.rs rename to poulpy-core/src/encryption/lwe.rs diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_switching_key.rs similarity index 96% rename from poulpy-core/src/encryption/lwe_ksk.rs rename to poulpy-core/src/encryption/lwe_switching_key.rs index 50e08a1..e06c910 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_switching_key.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + encryption::glwe_switching_key::GLWESwitchingKeyEncryptSk, layouts::{ GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, Rank, - prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, + prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, }, }; @@ -63,7 +63,7 @@ pub trait LWESwitchingKeyEncrypt { impl LWESwitchingKeyEncrypt for Module where - Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedApi + VecZnxAutomorphismInplace, Scratch: ScratchTakeCore, { fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs similarity index 97% rename from poulpy-core/src/encryption/lwe_to_glwe_ksk.rs rename to poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs index ddf6264..24153b6 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedAlloc, GLWESecretPreparedToRef, LWEInfos, LWESecret, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedApi, GLWESecretPreparedToRef, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, Rank, }, }; @@ -65,7 +65,7 @@ where Self: ModuleN + GGLWEEncryptSk + VecZnxAutomorphismInplace - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxAutomorphismInplaceTmpBytes, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/encryption/mod.rs b/poulpy-core/src/encryption/mod.rs index 9cc3a4b..7a391a6 100644 --- a/poulpy-core/src/encryption/mod.rs +++ b/poulpy-core/src/encryption/mod.rs @@ -1,28 +1,28 @@ mod compressed; -mod gglwe_atk; -mod gglwe_ct; -mod gglwe_ksk; -mod gglwe_tsk; -mod ggsw_ct; -mod glwe_ct; -mod glwe_pk; -mod glwe_to_lwe_ksk; -mod lwe_ct; -mod lwe_ksk; -mod lwe_to_glwe_ksk; +mod gglwe; +mod ggsw; +mod glwe; +mod glwe_automorphism_key; +mod glwe_public_key; +mod glwe_switching_key; +mod glwe_tensor_key; +mod glwe_to_lwe_switching_key; +mod lwe; +mod lwe_switching_key; +mod lwe_to_glwe_switching_key; pub use compressed::*; -pub use gglwe_atk::*; -pub use gglwe_ct::*; -pub use gglwe_ksk::*; -pub use gglwe_tsk::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; -pub use glwe_pk::*; -pub use glwe_to_lwe_ksk::*; -pub use lwe_ct::*; -pub use lwe_ksk::*; -pub use lwe_to_glwe_ksk::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; +pub use glwe_automorphism_key::*; +pub use glwe_public_key::*; +pub use glwe_switching_key::*; +pub use glwe_tensor_key::*; +pub use glwe_to_lwe_switching_key::*; +pub use lwe::*; +pub use lwe_switching_key::*; +pub use lwe_to_glwe_switching_key::*; pub const SIGMA: f64 = 3.2; pub(crate) const SIGMA_BOUND: f64 = 6.0 * SIGMA; diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe.rs similarity index 74% rename from poulpy-core/src/external_product/gglwe_ksk.rs rename to poulpy-core/src/external_product/gglwe.rs index f79ff6e..a9537c1 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe.rs @@ -3,11 +3,50 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; use crate::{ GLWEExternalProduct, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, - prepared::{GGSWPrepared, GGSWPreparedToRef}, + AutomorphismKey, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GGSWPrepared, GLWEInfos, GLWESwitchingKey, + prepared::GGSWPreparedToRef, }, }; +impl AutomorphismKey> { + pub fn external_product_tmp_bytes( + &self, + module: &M, + res_infos: &R, + a_infos: &A, + b_infos: &B, + ) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + B: GGSWInfos, + M: GGLWEExternalProduct, + { + module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) + } +} + +impl AutomorphismKey { + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GGLWEToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.gglwe_external_product(self, a, b, scratch); + } + + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.gglwe_external_product_inplace(self, a, scratch); + } +} + pub trait GGLWEExternalProduct where Self: GLWEExternalProduct, diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs deleted file mode 100644 index 2943ce3..0000000 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ /dev/null @@ -1,46 +0,0 @@ -use poulpy_hal::layouts::{Backend, DataMut, Scratch}; - -use crate::{ - ScratchTakeCore, - external_product::gglwe_ksk::GGLWEExternalProduct, - layouts::{AutomorphismKey, GGLWEInfos, GGLWEToRef, GGSWInfos, prepared::GGSWPreparedToRef}, -}; - -impl AutomorphismKey> { - pub fn external_product_tmp_bytes( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize - where - R: GGLWEInfos, - A: GGLWEInfos, - B: GGSWInfos, - M: GGLWEExternalProduct, - { - module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) - } -} - -impl AutomorphismKey { - pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) - where - M: GGLWEExternalProduct, - A: GGLWEToRef, - B: GGSWPreparedToRef, - Scratch: ScratchTakeCore, - { - module.gglwe_external_product(self, a, b, scratch); - } - - pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) - where - M: GGLWEExternalProduct, - A: GGSWPreparedToRef, - Scratch: ScratchTakeCore, - { - module.gglwe_external_product_inplace(self, a, scratch); - } -} diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw.rs similarity index 100% rename from poulpy-core/src/external_product/ggsw_ct.rs rename to poulpy-core/src/external_product/ggsw.rs diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe.rs similarity index 100% rename from poulpy-core/src/external_product/glwe_ct.rs rename to poulpy-core/src/external_product/glwe.rs diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index 532406b..cf5e6c0 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/poulpy-core/src/external_product/mod.rs @@ -1,8 +1,7 @@ -mod gglwe_atk; -mod gglwe_ksk; -mod ggsw_ct; -mod glwe_ct; +mod gglwe; +mod ggsw; +mod glwe; -pub use gglwe_ksk::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 548f4d4..1982269 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -8,9 +8,7 @@ use poulpy_hal::{ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, glwe_trace::GLWETrace, - layouts::{ - GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -117,7 +115,7 @@ impl GLWEPacker { pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) where A: GLWEToRef + GLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, M: GLWEPacking, Scratch: ScratchTakeCore, { @@ -190,7 +188,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut + GLWEToRef + GLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -234,7 +232,7 @@ fn pack_core( scratch: &mut Scratch, ) where A: GLWEToRef + GLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, M: ModuleLogN + GLWEAutomorphism + GaloisElement @@ -307,7 +305,7 @@ fn combine( B: GLWEToRef + GLWEInfos, M: GLWEAutomorphism + GaloisElement + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, B: GLWEToRef + GLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { let log_n: usize = acc.data.n().log2(); @@ -333,7 +331,7 @@ fn combine( // since 2*(I(X) * Q/2) = I(X) * Q = 0 mod Q. if acc.value { if let Some(b) = b { - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); + let (mut tmp_b, scratch_1) = scratch.take_glwe(module, a); // a = a * X^-t module.glwe_rotate_inplace(-t, a, scratch_1); @@ -373,7 +371,7 @@ fn combine( } } } else if let Some(b) = b { - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); + let (mut tmp_b, scratch_1) = scratch.take_glwe(module, a); module.glwe_rotate(t, &mut tmp_b, b); module.glwe_rsh(1, &mut tmp_b, scratch_1); @@ -400,7 +398,7 @@ fn pack_internal( M: GLWEAutomorphism + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, A: GLWEToMut + GLWEToRef + GLWEInfos, B: GLWEToMut + GLWEToRef + GLWEInfos, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) @@ -417,7 +415,7 @@ fn pack_internal( let t: i64 = 1 << (a.n().log2() - i - 1); if let Some(b) = b.as_deref_mut() { - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); + let (mut tmp_b, scratch_1) = scratch.take_glwe(module, a); // a = a * X^-t module.glwe_rotate_inplace(-t, a, scratch_1); @@ -451,7 +449,7 @@ fn pack_internal( } else if let Some(b) = b.as_deref_mut() { let t: i64 = 1 << (b.n().log2() - i - 1); - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, b); + let (mut tmp_b, scratch_1) = scratch.take_glwe(module, b); module.glwe_rotate(t, &mut tmp_b, b); module.glwe_rsh(1, &mut tmp_b, scratch_1); diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index a1c2573..421692f 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -8,8 +8,7 @@ use poulpy_hal::{ use crate::{ GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore, layouts::{ - Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, - prepared::GetAutomorphismGaloisElement, + Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos, }, }; @@ -43,7 +42,7 @@ impl GLWE { scratch: &mut Scratch, ) where A: GLWEToRef, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -58,7 +57,7 @@ impl GLWE { keys: &HashMap, scratch: &mut Scratch, ) where - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -110,7 +109,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { self.glwe_copy(res, a); @@ -120,7 +119,7 @@ where fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &HashMap, scratch: &mut Scratch) where R: GLWEToMut, - K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -141,7 +140,7 @@ where } if res.base2k() != basek_ksk { - let (mut self_conv, scratch_1) = scratch.take_glwe_ct( + let (mut self_conv, scratch_1) = scratch.take_glwe( self, &GLWELayout { n: self.n().into(), diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe.rs similarity index 99% rename from poulpy-core/src/keyswitching/gglwe_ct.rs rename to poulpy-core/src/keyswitching/gglwe.rs index 3b6495a..1f9232f 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeyswitch, + keyswitching::GLWEKeyswitch, layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey}, }; diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw.rs similarity index 99% rename from poulpy-core/src/keyswitching/ggsw_ct.rs rename to poulpy-core/src/keyswitching/ggsw.rs index 7682315..334572d 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeyswitch, + keyswitching::GLWEKeyswitch, layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef}, }; diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe.rs similarity index 100% rename from poulpy-core/src/keyswitching/glwe_ct.rs rename to poulpy-core/src/keyswitching/glwe.rs diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe.rs similarity index 95% rename from poulpy-core/src/keyswitching/lwe_ct.rs rename to poulpy-core/src/keyswitching/lwe.rs index e73747a..249ccca 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ use crate::{ LWESampleExtract, ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeyswitch, + keyswitching::GLWEKeyswitch, layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision}, }; @@ -87,7 +87,7 @@ where let a_size: usize = a.k().div_ceil(ksk.base2k()) as usize; - let (mut glwe_in, scratch_1) = scratch.take_glwe_ct( + let (mut glwe_in, scratch_1) = scratch.take_glwe( self, &GLWELayout { n: ksk.n(), @@ -98,7 +98,7 @@ where ); glwe_in.data.zero(); - let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct( + let (mut glwe_out, scratch_1) = scratch_1.take_glwe( self, &GLWELayout { n: ksk.n(), diff --git a/poulpy-core/src/keyswitching/mod.rs b/poulpy-core/src/keyswitching/mod.rs index d7621db..fab0f5c 100644 --- a/poulpy-core/src/keyswitching/mod.rs +++ b/poulpy-core/src/keyswitching/mod.rs @@ -1,9 +1,9 @@ -mod gglwe_ct; -mod ggsw_ct; -mod glwe_ct; -mod lwe_ct; +mod gglwe; +mod ggsw; +mod glwe; +mod lwe; -pub use gglwe_ct::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; -pub use lwe_ct::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; +pub use lwe::*; diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/gglwe_ct.rs rename to poulpy-core/src/layouts/compressed/gglwe.rs diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/ggsw_ct.rs rename to poulpy-core/src/layouts/compressed/ggsw.rs diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/glwe_ct.rs rename to poulpy-core/src/layouts/compressed/glwe.rs diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs similarity index 62% rename from poulpy-core/src/layouts/compressed/gglwe_atk.rs rename to poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs index 1f15bfc..6760d98 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs @@ -5,26 +5,25 @@ use poulpy_hal::{ use crate::layouts::{ AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, - GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWECompressed, GLWECompressedToMut, GLWECompressedToRef, - GLWEDecompress, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, + GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank, + SetGaloisElement, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct AutomorphismKeyCompressed { +pub struct GLWEAutomorphismKeyCompressed { pub(crate) key: GGLWECompressed, pub(crate) p: i64, } -impl GetAutomorphismGaloisElement for AutomorphismKeyCompressed { +impl GetGaloisElement for GLWEAutomorphismKeyCompressed { fn p(&self) -> i64 { self.p } } -impl LWEInfos for AutomorphismKeyCompressed { +impl LWEInfos for GLWEAutomorphismKeyCompressed { fn n(&self) -> Degree { self.key.n() } @@ -42,13 +41,13 @@ impl LWEInfos for AutomorphismKeyCompressed { } } -impl GLWEInfos for AutomorphismKeyCompressed { +impl GLWEInfos for GLWEAutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for AutomorphismKeyCompressed { +impl GGLWEInfos for GLWEAutomorphismKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -66,25 +65,25 @@ impl GGLWEInfos for AutomorphismKeyCompressed { } } -impl fmt::Debug for AutomorphismKeyCompressed { +impl fmt::Debug for GLWEAutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for AutomorphismKeyCompressed { +impl FillUniform for GLWEAutomorphismKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for AutomorphismKeyCompressed { +impl fmt::Display for GLWEAutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) } } -impl AutomorphismKeyCompressed> { +impl GLWEAutomorphismKeyCompressed> { pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, @@ -100,7 +99,7 @@ impl AutomorphismKeyCompressed> { } pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - AutomorphismKeyCompressed { + GLWEAutomorphismKeyCompressed { key: GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } @@ -125,14 +124,14 @@ impl AutomorphismKeyCompressed> { } } -impl ReaderFrom for AutomorphismKeyCompressed { +impl ReaderFrom for GLWEAutomorphismKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.p = reader.read_u64::()? as i64; self.key.read_from(reader) } } -impl WriterTo for AutomorphismKeyCompressed { +impl WriterTo for GLWEAutomorphismKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) @@ -145,8 +144,8 @@ where { fn decompress_automorphism_key(&self, res: &mut R, other: &O) where - R: GGLWEToMut + SetAutomorphismGaloisElement, - O: GGLWECompressedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetGaloisElement, + O: GGLWECompressedToRef + GetGaloisElement, { self.decompress_gglwe(res, other); res.set_p(other.p()); @@ -157,68 +156,36 @@ impl AutomorphismKeyDecompress for Module where Self: GLWEDecompr impl AutomorphismKey where - Self: SetAutomorphismGaloisElement, + Self: SetGaloisElement, { pub fn decompress(&mut self, module: &M, other: &O) where - O: GGLWECompressedToRef + GetAutomorphismGaloisElement, + O: GGLWECompressedToRef + GetGaloisElement, M: AutomorphismKeyDecompress, { module.decompress_automorphism_key(self, other); } } -impl GGLWECompressedToRef for AutomorphismKeyCompressed { +impl GGLWECompressedToRef for GLWEAutomorphismKeyCompressed { fn to_ref(&self) -> GGLWECompressed<&[u8]> { self.key.to_ref() } } -impl GGLWECompressedToMut for AutomorphismKeyCompressed { +impl GGLWECompressedToMut for GLWEAutomorphismKeyCompressed { fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { self.key.to_mut() } } -pub trait AutomorphismKeyCompressedToRef { - fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; -} - -impl AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed -where - GLWECompressed: GLWECompressedToRef, -{ - fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { - AutomorphismKeyCompressed { - key: self.key.to_ref(), - p: self.p, - } - } -} - -pub trait AutomorphismKeyCompressedToMut { - fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>; -} - -impl AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed -where - GLWECompressed: GLWECompressedToMut, -{ - fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { - AutomorphismKeyCompressed { - p: self.p, - key: self.key.to_mut(), - } - } -} - -impl GGLWECompressedSeedMut for AutomorphismKeyCompressed { +impl GGLWECompressedSeedMut for GLWEAutomorphismKeyCompressed { fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { &mut self.key.seed } } -impl SetAutomorphismGaloisElement for AutomorphismKeyCompressed { +impl SetGaloisElement for GLWEAutomorphismKeyCompressed { fn set_p(&mut self, p: i64) { self.p = p } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/gglwe_ksk.rs rename to poulpy-core/src/layouts/compressed/glwe_switching_key.rs diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs similarity index 81% rename from poulpy-core/src/layouts/compressed/gglwe_tsk.rs rename to poulpy-core/src/layouts/compressed/glwe_tensor_key.rs index 1e9e312..e8752d3 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs @@ -11,11 +11,11 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct TensorKeyCompressed { +pub struct GLWETensorKeyCompressed { pub(crate) keys: Vec>, } -impl LWEInfos for TensorKeyCompressed { +impl LWEInfos for GLWETensorKeyCompressed { fn n(&self) -> Degree { self.keys[0].n() } @@ -31,13 +31,13 @@ impl LWEInfos for TensorKeyCompressed { self.keys[0].size() } } -impl GLWEInfos for TensorKeyCompressed { +impl GLWEInfos for GLWETensorKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for TensorKeyCompressed { +impl GGLWEInfos for GLWETensorKeyCompressed { fn rank_in(&self) -> Rank { self.rank_out() } @@ -55,13 +55,13 @@ impl GGLWEInfos for TensorKeyCompressed { } } -impl fmt::Debug for TensorKeyCompressed { +impl fmt::Debug for GLWETensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for TensorKeyCompressed { +impl FillUniform for GLWETensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() @@ -69,7 +69,7 @@ impl FillUniform for TensorKeyCompressed { } } -impl fmt::Display for TensorKeyCompressed { +impl fmt::Display for GLWETensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKeyCompressed)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -79,7 +79,7 @@ impl fmt::Display for TensorKeyCompressed { } } -impl TensorKeyCompressed> { +impl GLWETensorKeyCompressed> { pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, @@ -96,7 +96,7 @@ impl TensorKeyCompressed> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); - TensorKeyCompressed { + GLWETensorKeyCompressed { keys: (0..pairs) .map(|_| GGLWECompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), @@ -123,7 +123,7 @@ impl TensorKeyCompressed> { } } -impl ReaderFrom for TensorKeyCompressed { +impl ReaderFrom for GLWETensorKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -139,7 +139,7 @@ impl ReaderFrom for TensorKeyCompressed { } } -impl WriterTo for TensorKeyCompressed { +impl WriterTo for GLWETensorKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -153,7 +153,7 @@ pub trait TensorKeyCompressedAtRef { fn at(&self, i: usize, j: usize) -> &GGLWECompressed; } -impl TensorKeyCompressedAtRef for TensorKeyCompressed { +impl TensorKeyCompressedAtRef for GLWETensorKeyCompressed { fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); @@ -167,7 +167,7 @@ pub trait TensorKeyCompressedAtMut { fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed; } -impl TensorKeyCompressedAtMut for TensorKeyCompressed { +impl TensorKeyCompressedAtMut for GLWETensorKeyCompressed { fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); @@ -187,7 +187,7 @@ where O: TensorKeyCompressedToRef, { let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); - let other: &TensorKeyCompressed<&[u8]> = &other.to_ref(); + let other: &GLWETensorKeyCompressed<&[u8]> = &other.to_ref(); assert_eq!( res.keys.len(), @@ -216,30 +216,30 @@ impl TensorKey { } pub trait TensorKeyCompressedToMut { - fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]>; + fn to_mut(&mut self) -> GLWETensorKeyCompressed<&mut [u8]>; } -impl TensorKeyCompressedToMut for TensorKeyCompressed +impl TensorKeyCompressedToMut for GLWETensorKeyCompressed where GGLWECompressed: GGLWECompressedToMut, { - fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { - TensorKeyCompressed { + fn to_mut(&mut self) -> GLWETensorKeyCompressed<&mut [u8]> { + GLWETensorKeyCompressed { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } } pub trait TensorKeyCompressedToRef { - fn to_ref(&self) -> TensorKeyCompressed<&[u8]>; + fn to_ref(&self) -> GLWETensorKeyCompressed<&[u8]>; } -impl TensorKeyCompressedToRef for TensorKeyCompressed +impl TensorKeyCompressedToRef for GLWETensorKeyCompressed where GGLWECompressed: GGLWECompressedToRef, { - fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { - TensorKeyCompressed { + fn to_ref(&self) -> GLWETensorKeyCompressed<&[u8]> { + GLWETensorKeyCompressed { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs rename to poulpy-core/src/layouts/compressed/glwe_to_lwe_switching_key.rs diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/lwe_ct.rs rename to poulpy-core/src/layouts/compressed/lwe.rs diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/lwe_ksk.rs rename to poulpy-core/src/layouts/compressed/lwe_switching_key.rs diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs rename to poulpy-core/src/layouts/compressed/lwe_to_glwe_switching_key.rs diff --git a/poulpy-core/src/layouts/compressed/mod.rs b/poulpy-core/src/layouts/compressed/mod.rs index cd7c459..b85d48d 100644 --- a/poulpy-core/src/layouts/compressed/mod.rs +++ b/poulpy-core/src/layouts/compressed/mod.rs @@ -1,21 +1,21 @@ -mod gglwe_atk; -mod gglwe_ct; -mod gglwe_ksk; -mod gglwe_tsk; -mod ggsw_ct; -mod glwe_ct; -mod glwe_to_lwe_ksk; -mod lwe_ct; -mod lwe_ksk; -mod lwe_to_glwe_ksk; +mod gglwe; +mod ggsw; +mod glwe; +mod glwe_automorphism_key; +mod glwe_switching_key; +mod glwe_tensor_key; +mod glwe_to_lwe_switching_key; +mod lwe; +mod lwe_switching_key; +mod lwe_to_glwe_switching_key; -pub use gglwe_atk::*; -pub use gglwe_ct::*; -pub use gglwe_ksk::*; -pub use gglwe_tsk::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; -pub use glwe_to_lwe_ksk::*; -pub use lwe_ct::*; -pub use lwe_ksk::*; -pub use lwe_to_glwe_ksk::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; +pub use glwe_automorphism_key::*; +pub use glwe_switching_key::*; +pub use glwe_tensor_key::*; +pub use glwe_to_lwe_switching_key::*; +pub use lwe::*; +pub use lwe_switching_key::*; +pub use lwe_to_glwe_switching_key::*; diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe.rs similarity index 100% rename from poulpy-core/src/layouts/gglwe_ct.rs rename to poulpy-core/src/layouts/gglwe.rs diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw.rs similarity index 100% rename from poulpy-core/src/layouts/ggsw_ct.rs rename to poulpy-core/src/layouts/ggsw.rs diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe.rs similarity index 100% rename from poulpy-core/src/layouts/glwe_ct.rs rename to poulpy-core/src/layouts/glwe.rs diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/glwe_automorphism_key.rs similarity index 95% rename from poulpy-core/src/layouts/gglwe_atk.rs rename to poulpy-core/src/layouts/glwe_automorphism_key.rs index b705056..c15c2db 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/glwe_automorphism_key.rs @@ -5,7 +5,6 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -27,13 +26,21 @@ pub struct AutomorphismKey { pub(crate) p: i64, } -impl SetAutomorphismGaloisElement for AutomorphismKey { +pub trait GetGaloisElement { + fn p(&self) -> i64; +} + +pub trait SetGaloisElement { + fn set_p(&mut self, p: i64); +} + +impl SetGaloisElement for AutomorphismKey { fn set_p(&mut self, p: i64) { self.p = p } } -impl GetAutomorphismGaloisElement for AutomorphismKey { +impl GetGaloisElement for AutomorphismKey { fn p(&self) -> i64 { self.p } diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_plaintext.rs similarity index 100% rename from poulpy-core/src/layouts/glwe_pt.rs rename to poulpy-core/src/layouts/glwe_plaintext.rs diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_public_key.rs similarity index 62% rename from poulpy-core/src/layouts/glwe_pk.rs rename to poulpy-core/src/layouts/glwe_public_key.rs index 2231389..f9c8c41 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_public_key.rs @@ -1,17 +1,14 @@ -use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo}; use crate::{ GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, TorusPrecision}, }; -use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; #[derive(PartialEq, Eq)] pub struct GLWEPublicKey { - pub(crate) data: VecZnx, - pub(crate) base2k: Base2K, - pub(crate) k: TorusPrecision, + pub(crate) key: GLWE, pub(crate) dist: Distribution, } @@ -37,25 +34,25 @@ pub struct GLWEPublicKeyLayout { impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { - self.base2k + self.key.base2k() } fn k(&self) -> TorusPrecision { - self.k + self.key.k() } fn n(&self) -> Degree { - Degree(self.data.n() as u32) + self.key.n() } fn size(&self) -> usize { - self.data.size() + self.key.size() } } impl GLWEInfos for GLWEPublicKey { fn rank(&self) -> Rank { - Rank(self.data.cols() as u32 - 1) + self.key.rank() } } @@ -93,9 +90,7 @@ impl GLWEPublicKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWEPublicKey { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, + key: GLWE::alloc(n, base2k, k, rank), dist: Distribution::NONE, } } @@ -114,54 +109,32 @@ impl GLWEPublicKey> { impl ReaderFrom for GLWEPublicKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.k = TorusPrecision(reader.read_u32::()?); - self.base2k = Base2K(reader.read_u32::()?); match Distribution::read_from(reader) { Ok(dist) => self.dist = dist, Err(e) => return Err(e), } - self.data.read_from(reader) + self.key.read_from(reader) } } impl WriterTo for GLWEPublicKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - writer.write_u32::(self.k.0)?; - writer.write_u32::(self.base2k.0)?; match self.dist.write_to(writer) { Ok(()) => {} Err(e) => return Err(e), } - self.data.write_to(writer) + self.key.write_to(writer) } } -pub trait GLWEPublicKeyToRef { - fn to_ref(&self) -> GLWEPublicKey<&[u8]>; -} - -impl GLWEPublicKeyToRef for GLWEPublicKey { - fn to_ref(&self) -> GLWEPublicKey<&[u8]> { - GLWEPublicKey { - data: self.data.to_ref(), - base2k: self.base2k, - k: self.k, - dist: self.dist, - } +impl GLWEToRef for GLWEPublicKey { + fn to_ref(&self) -> GLWE<&[u8]> { + self.key.to_ref() } } -pub trait GLWEPublicKeyToMut { - fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]>; -} - -impl GLWEPublicKeyToMut for GLWEPublicKey { - fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]> { - GLWEPublicKey { - base2k: self.base2k, - k: self.k, - dist: self.dist, - data: self.data.to_mut(), - } +impl GLWEToMut for GLWEPublicKey { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + self.key.to_mut() } } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_secret.rs similarity index 100% rename from poulpy-core/src/layouts/glwe_sk.rs rename to poulpy-core/src/layouts/glwe_secret.rs diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/glwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/gglwe_ksk.rs rename to poulpy-core/src/layouts/glwe_switching_key.rs diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/glwe_tensor_key.rs similarity index 100% rename from poulpy-core/src/layouts/gglwe_tsk.rs rename to poulpy-core/src/layouts/glwe_tensor_key.rs diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/glwe_to_lwe_ksk.rs rename to poulpy-core/src/layouts/glwe_to_lwe_switching_key.rs diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe.rs similarity index 100% rename from poulpy-core/src/layouts/lwe_ct.rs rename to poulpy-core/src/layouts/lwe.rs diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_plaintext.rs similarity index 100% rename from poulpy-core/src/layouts/lwe_pt.rs rename to poulpy-core/src/layouts/lwe_plaintext.rs diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_secret.rs similarity index 100% rename from poulpy-core/src/layouts/lwe_sk.rs rename to poulpy-core/src/layouts/lwe_secret.rs diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/lwe_ksk.rs rename to poulpy-core/src/layouts/lwe_switching_key.rs diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_switching_key.rs similarity index 100% rename from poulpy-core/src/layouts/lwe_to_glwe_ksk.rs rename to poulpy-core/src/layouts/lwe_to_glwe_switching_key.rs diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index abafe7e..6447fc5 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -1,38 +1,38 @@ -mod gglwe_atk; -mod gglwe_ct; -mod gglwe_ksk; -mod gglwe_tsk; -mod ggsw_ct; -mod glwe_ct; -mod glwe_pk; -mod glwe_pt; -mod glwe_sk; -mod glwe_to_lwe_ksk; -mod lwe_ct; -mod lwe_ksk; -mod lwe_pt; -mod lwe_sk; -mod lwe_to_glwe_ksk; +mod gglwe; +mod ggsw; +mod glwe; +mod glwe_automorphism_key; +mod glwe_plaintext; +mod glwe_public_key; +mod glwe_secret; +mod glwe_switching_key; +mod glwe_tensor_key; +mod glwe_to_lwe_switching_key; +mod lwe; +mod lwe_plaintext; +mod lwe_secret; +mod lwe_switching_key; +mod lwe_to_glwe_switching_key; pub mod compressed; pub mod prepared; pub use compressed::*; -pub use gglwe_atk::*; -pub use gglwe_ct::*; -pub use gglwe_ksk::*; -pub use gglwe_tsk::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; -pub use glwe_pk::*; -pub use glwe_pt::*; -pub use glwe_sk::*; -pub use glwe_to_lwe_ksk::*; -pub use lwe_ct::*; -pub use lwe_ksk::*; -pub use lwe_pt::*; -pub use lwe_sk::*; -pub use lwe_to_glwe_ksk::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; +pub use glwe_automorphism_key::*; +pub use glwe_plaintext::*; +pub use glwe_public_key::*; +pub use glwe_secret::*; +pub use glwe_switching_key::*; +pub use glwe_tensor_key::*; +pub use glwe_to_lwe_switching_key::*; +pub use lwe::*; +pub use lwe_plaintext::*; +pub use lwe_secret::*; +pub use lwe_switching_key::*; +pub use lwe_to_glwe_switching_key::*; pub use prepared::*; use poulpy_hal::layouts::{Backend, Module}; diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe.rs similarity index 90% rename from poulpy-core/src/layouts/prepared/gglwe_ct.rs rename to poulpy-core/src/layouts/prepared/gglwe.rs index 98d45b5..27e80ee 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe.rs @@ -57,9 +57,9 @@ impl GGLWEInfos for GGLWEPrepared { } } -pub trait GGLWEPreparedAlloc +pub trait GGLWEPreparedAlloc where - Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepare + VmpPrepareTmpBytes, { fn alloc_gglwe_prepared( &self, @@ -69,7 +69,7 @@ where rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> GGLWEPrepared, B> { + ) -> GGLWEPrepared, BE> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -92,7 +92,7 @@ where } } - fn alloc_gglwe_prepared_from_infos(&self, infos: &A) -> GGLWEPrepared, B> + fn alloc_gglwe_prepared_from_infos(&self, infos: &A) -> GGLWEPrepared, BE> where A: GGLWEInfos, { @@ -147,9 +147,41 @@ where infos.dsize(), ) } + + fn prepare_gglwe_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.vmp_prepare_tmp_bytes( + infos.dnum().into(), + infos.rank_in().into(), + (infos.rank() + 1).into(), + infos.size(), + ) + } + + fn prepare_gglwe(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut, + O: GGLWEToRef, + { + let mut res: GGLWEPrepared<&mut [u8], BE> = res.to_mut(); + let other: GGLWE<&[u8]> = other.to_ref(); + + assert_eq!(res.n(), self.ring_degree()); + assert_eq!(other.n(), self.ring_degree()); + assert_eq!(res.base2k, other.base2k); + assert_eq!(res.k, other.k); + assert_eq!(res.dsize, other.dsize); + + self.vmp_prepare(&mut res.data, &other.data, scratch); + } } -impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGLWEPreparedAlloc for Module where + Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepare + VmpPrepareTmpBytes +{ +} impl GGLWEPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -199,47 +231,11 @@ impl GGLWEPrepared, B> { } } -pub trait GGLWEPrepare -where - Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, -{ - fn prepare_gglwe_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.vmp_prepare_tmp_bytes( - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank() + 1).into(), - infos.size(), - ) - } - - fn prepare_gglwe(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut, - O: GGLWEToRef, - { - let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut(); - let other: GGLWE<&[u8]> = other.to_ref(); - - assert_eq!(res.n(), self.ring_degree()); - assert_eq!(other.n(), self.ring_degree()); - assert_eq!(res.base2k, other.base2k); - assert_eq!(res.k, other.k); - assert_eq!(res.dsize, other.dsize); - - self.vmp_prepare(&mut res.data, &other.data, scratch); - } -} - -impl GGLWEPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} - impl GGLWEPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef, - M: GGLWEPrepare, + M: GGLWEPreparedAlloc, { module.prepare_gglwe(self, other, scratch); } @@ -248,7 +244,7 @@ impl GGLWEPrepared { impl GGLWEPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: GGLWEPrepare, + M: GGLWEPreparedAlloc, { module.prepare_gglwe_tmp_bytes(self) } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw.rs similarity index 93% rename from poulpy-core/src/layouts/prepared/ggsw_ct.rs rename to poulpy-core/src/layouts/prepared/ggsw.rs index 551660b..c2c62a2 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw.rs @@ -51,7 +51,7 @@ impl GGSWInfos for GGSWPrepared { pub trait GGSWPreparedAlloc where - Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare, { fn alloc_ggsw_prepared( &self, @@ -133,9 +133,39 @@ where infos.rank(), ) } + + fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + assert_eq!(self.ring_degree(), infos.n()); + self.vmp_prepare_tmp_bytes( + infos.dnum().into(), + (infos.rank() + 1).into(), + (infos.rank() + 1).into(), + infos.size(), + ) + } + fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGSWPreparedToMut, + O: GGSWToRef, + { + let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); + let other: GGSW<&[u8]> = other.to_ref(); + assert_eq!(res.n(), self.ring_degree()); + assert_eq!(other.n(), self.ring_degree()); + assert_eq!(res.k, other.k); + assert_eq!(res.base2k, other.base2k); + assert_eq!(res.dsize, other.dsize); + self.vmp_prepare(&mut res.data, &other.data, scratch); + } } -impl GGSWPreparedAlloc for Module where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGSWPreparedAlloc for Module where + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare +{ +} impl GGSWPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -175,45 +205,11 @@ impl GGSWPrepared { } } -pub trait GGSWPrepare -where - Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, -{ - fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - assert_eq!(self.ring_degree(), infos.n()); - self.vmp_prepare_tmp_bytes( - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - ) - } - fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGSWPreparedToMut, - O: GGSWToRef, - { - let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); - let other: GGSW<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.ring_degree()); - assert_eq!(other.n(), self.ring_degree()); - assert_eq!(res.k, other.k); - assert_eq!(res.base2k, other.base2k); - assert_eq!(res.dsize, other.dsize); - self.vmp_prepare(&mut res.data, &other.data, scratch); - } -} - -impl GGSWPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} - impl GGSWPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGSWInfos, - M: GGSWPrepare, + M: GGSWPreparedAlloc, { module.ggsw_prepare_tmp_bytes(infos) } @@ -223,7 +219,7 @@ impl GGSWPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGSWToRef, - M: GGSWPrepare, + M: GGSWPreparedAlloc, { module.ggsw_prepare(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/glwe.rs b/poulpy-core/src/layouts/prepared/glwe.rs new file mode 100644 index 0000000..b09ca72 --- /dev/null +++ b/poulpy-core/src/layouts/prepared/glwe.rs @@ -0,0 +1,161 @@ +use poulpy_hal::{ + api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf}, + layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, +}; + +use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToRef, GetDegree, LWEInfos, Rank, TorusPrecision}; + +#[derive(PartialEq, Eq)] +pub struct GLWEPrepared { + pub(crate) data: VecZnxDft, + pub(crate) base2k: Base2K, + pub(crate) k: TorusPrecision, +} + +impl LWEInfos for GLWEPrepared { + fn base2k(&self) -> Base2K { + self.base2k + } + + fn k(&self) -> TorusPrecision { + self.k + } + + fn size(&self) -> usize { + self.data.size() + } + + fn n(&self) -> Degree { + Degree(self.data.n() as u32) + } +} + +impl GLWEInfos for GLWEPrepared { + fn rank(&self) -> Rank { + Rank(self.data.cols() as u32 - 1) + } +} + +pub trait GLWEPreparedAlloc +where + Self: GetDegree + VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply, +{ + fn alloc_glwe_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPrepared, B> { + GLWEPrepared { + data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), + base2k, + k, + } + } + + fn alloc_glwe_prepared_from_infos(&self, infos: &A) -> GLWEPrepared, B> + where + A: GLWEInfos, + { + self.alloc_glwe_prepared(infos.base2k(), infos.k(), infos.rank()) + } + + fn bytes_of_glwe_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_glwe_prepared_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_prepared(infos.base2k(), infos.k(), infos.rank()) + } + + fn prepare_glwe(&self, res: &mut R, other: &O) + where + R: GLWEPreparedToMut, + O: GLWEToRef, + { + { + let mut res: GLWEPrepared<&mut [u8], B> = res.to_mut(); + let other: GLWE<&[u8]> = other.to_ref(); + + assert_eq!(res.n(), self.ring_degree()); + assert_eq!(other.n(), self.ring_degree()); + assert_eq!(res.size(), other.size()); + assert_eq!(res.k(), other.k()); + assert_eq!(res.base2k(), other.base2k()); + + for i in 0..(res.rank() + 1).into() { + self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i); + } + } + } +} + +impl GLWEPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply {} + +impl GLWEPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GLWEInfos, + M: GLWEPreparedAlloc, + { + module.alloc_glwe_prepared_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + M: GLWEPreparedAlloc, + { + module.alloc_glwe_prepared(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GLWEInfos, + M: GLWEPreparedAlloc, + { + module.bytes_of_glwe_prepared_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + M: GLWEPreparedAlloc, + { + module.bytes_of_glwe_prepared(base2k, k, rank) + } +} + +impl GLWEPrepared { + pub fn prepare(&mut self, module: &M, other: &O) + where + O: GLWEToRef, + M: GLWEPreparedAlloc, + { + module.prepare_glwe(self, other); + } +} + +pub trait GLWEPreparedToMut { + fn to_mut(&mut self) -> GLWEPrepared<&mut [u8], B>; +} + +impl GLWEPreparedToMut for GLWEPrepared { + fn to_mut(&mut self) -> GLWEPrepared<&mut [u8], B> { + GLWEPrepared { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } + } +} + +pub trait GLWEPreparedToRef { + fn to_ref(&self) -> GLWEPrepared<&[u8], B>; +} + +impl GLWEPreparedToRef for GLWEPrepared { + fn to_ref(&self) -> GLWEPrepared<&[u8], B> { + GLWEPrepared { + data: self.data.to_ref(), + k: self.k, + base2k: self.base2k, + } + } +} diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs similarity index 67% rename from poulpy-core/src/layouts/prepared/gglwe_atk.rs rename to poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs index 244ab0a..5790491 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs @@ -1,17 +1,17 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, - GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef, + GGLWEToRef, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct AutomorphismKeyPrepared { +pub struct GLWEAutomorphismKeyPrepared { pub(crate) key: GGLWEPrepared, pub(crate) p: i64, } -impl LWEInfos for AutomorphismKeyPrepared { +impl LWEInfos for GLWEAutomorphismKeyPrepared { fn n(&self) -> Degree { self.key.n() } @@ -29,33 +29,25 @@ impl LWEInfos for AutomorphismKeyPrepared { } } -pub trait GetAutomorphismGaloisElement { - fn p(&self) -> i64; -} - -impl GetAutomorphismGaloisElement for AutomorphismKeyPrepared { +impl GetGaloisElement for GLWEAutomorphismKeyPrepared { fn p(&self) -> i64 { self.p } } -pub trait SetAutomorphismGaloisElement { - fn set_p(&mut self, p: i64); -} - -impl SetAutomorphismGaloisElement for AutomorphismKeyPrepared { +impl SetGaloisElement for GLWEAutomorphismKeyPrepared { fn set_p(&mut self, p: i64) { self.p = p } } -impl GLWEInfos for AutomorphismKeyPrepared { +impl GLWEInfos for GLWEAutomorphismKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for AutomorphismKeyPrepared { +impl GGLWEInfos for GLWEAutomorphismKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -73,7 +65,7 @@ impl GGLWEInfos for AutomorphismKeyPrepared { } } -pub trait AutomorphismKeyPreparedAlloc +pub trait GLWEAutomorphismKeyPreparedApi where Self: GGLWEPreparedAlloc, { @@ -84,14 +76,14 @@ where rank: Rank, dnum: Dnum, dsize: Dsize, - ) -> AutomorphismKeyPrepared, B> { - AutomorphismKeyPrepared::, B> { + ) -> GLWEAutomorphismKeyPrepared, B> { + GLWEAutomorphismKeyPrepared::, B> { key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize), p: 0, } } - fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> + fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> GLWEAutomorphismKeyPrepared, B> where A: GGLWEInfos, { @@ -137,46 +129,7 @@ where infos.dsize(), ) } -} -impl AutomorphismKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} - -impl AutomorphismKeyPrepared, B> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyPreparedAlloc, - { - module.alloc_automorphism_key_prepared_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: AutomorphismKeyPreparedAlloc, - { - module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyPreparedAlloc, - { - module.bytes_of_automorphism_key_prepared_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: AutomorphismKeyPreparedAlloc, - { - module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) - } -} - -pub trait AutomorphismKeyPrepare -where - Self: GGLWEPrepare, -{ fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, @@ -186,42 +139,74 @@ where fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GGLWEPreparedToMut + SetAutomorphismGaloisElement, - O: GGLWEToRef + GetAutomorphismGaloisElement, + R: GGLWEPreparedToMut + SetGaloisElement, + O: GGLWEToRef + GetGaloisElement, { self.prepare_gglwe(res, other, scratch); res.set_p(other.p()); } } -impl AutomorphismKeyPrepare for Module where Module: GGLWEPrepare {} +impl GLWEAutomorphismKeyPreparedApi for Module where Module: GGLWEPreparedAlloc {} -impl AutomorphismKeyPrepared, B> { +impl GLWEAutomorphismKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GLWEAutomorphismKeyPreparedApi, + { + module.alloc_automorphism_key_prepared_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: GLWEAutomorphismKeyPreparedApi, + { + module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GLWEAutomorphismKeyPreparedApi, + { + module.bytes_of_automorphism_key_prepared_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: GLWEAutomorphismKeyPreparedApi, + { + module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) + } +} + +impl GLWEAutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: AutomorphismKeyPrepare, + M: GLWEAutomorphismKeyPreparedApi, { module.prepare_automorphism_key_tmp_bytes(self) } } -impl AutomorphismKeyPrepared { +impl GLWEAutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: GGLWEToRef + GetAutomorphismGaloisElement, - M: AutomorphismKeyPrepare, + O: GGLWEToRef + GetGaloisElement, + M: GLWEAutomorphismKeyPreparedApi, { module.prepare_automorphism_key(self, other, scratch); } } -impl GGLWEPreparedToMut for AutomorphismKeyPrepared { +impl GGLWEPreparedToMut for GLWEAutomorphismKeyPrepared { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { self.key.to_mut() } } -impl GGLWEPreparedToRef for AutomorphismKeyPrepared { +impl GGLWEPreparedToRef for GLWEAutomorphismKeyPrepared { fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { self.key.to_ref() } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_public_key.rs similarity index 54% rename from poulpy-core/src/layouts/prepared/glwe_pk.rs rename to poulpy-core/src/layouts/prepared/glwe_public_key.rs index f186d22..8938c93 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_public_key.rs @@ -1,19 +1,20 @@ use poulpy_hal::{ api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf}, - layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module}, }; use crate::{ GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{ + Base2K, Degree, GLWEInfos, GLWEPrepared, GLWEPreparedAlloc, GLWEPreparedToMut, GLWEPreparedToRef, GLWEToRef, GetDegree, + LWEInfos, Rank, TorusPrecision, + }, }; #[derive(PartialEq, Eq)] pub struct GLWEPublicKeyPrepared { - pub(crate) data: VecZnxDft, - pub(crate) base2k: Base2K, - pub(crate) k: TorusPrecision, + pub(crate) key: GLWEPrepared, pub(crate) dist: Distribution, } @@ -31,37 +32,35 @@ impl GetDistributionMut for GLWEPublicKeyPrepared LWEInfos for GLWEPublicKeyPrepared { fn base2k(&self) -> Base2K { - self.base2k + self.key.base2k() } fn k(&self) -> TorusPrecision { - self.k + self.key.k() } fn size(&self) -> usize { - self.data.size() + self.key.size() } fn n(&self) -> Degree { - Degree(self.data.n() as u32) + self.key.n() } } impl GLWEInfos for GLWEPublicKeyPrepared { fn rank(&self) -> Rank { - Rank(self.data.cols() as u32 - 1) + self.key.rank() } } pub trait GLWEPublicKeyPreparedAlloc where - Self: GetDegree + VecZnxDftAlloc + VecZnxDftBytesOf, + Self: GetDegree + GLWEPreparedAlloc, { fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { GLWEPublicKeyPrepared { - data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, + key: self.alloc_glwe_prepared(base2k, k, rank), dist: Distribution::NONE, } } @@ -74,7 +73,7 @@ where } fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + self.bytes_of_glwe_prepared(base2k, k, rank) } fn bytes_of_glwe_public_key_prepared_from_infos(&self, infos: &A) -> usize @@ -83,9 +82,18 @@ where { self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } + + fn prepare_glwe_public_key(&self, res: &mut R, other: &O) + where + R: GLWEPreparedToMut + GetDistributionMut, + O: GLWEToRef + GetDistribution, + { + self.prepare_glwe(res, other); + *res.dist_mut() = *other.dist(); + } } -impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf {} +impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply {} impl GLWEPublicKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -119,72 +127,30 @@ impl GLWEPublicKeyPrepared, B> { } } -pub trait GLWEPublicKeyPrepare -where - Self: GetDegree + VecZnxDftApply, -{ - fn prepare_glwe_public_key(&self, res: &mut R, other: &O) - where - R: GLWEPublicKeyPreparedToMut + GetDistributionMut, - O: GLWEPublicKeyToRef + GetDistribution, - { - { - let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); - let other: GLWEPublicKey<&[u8]> = other.to_ref(); - - assert_eq!(res.n(), self.ring_degree()); - assert_eq!(other.n(), self.ring_degree()); - assert_eq!(res.size(), other.size()); - assert_eq!(res.k(), other.k()); - assert_eq!(res.base2k(), other.base2k()); - - for i in 0..(res.rank() + 1).into() { - self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i); - } - } - - *res.dist_mut() = *other.dist(); - } -} - -impl GLWEPublicKeyPrepare for Module where Self: GetDegree + VecZnxDftApply {} - impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) where - O: GLWEPublicKeyToRef + GetDistribution, - M: GLWEPublicKeyPrepare, + O: GLWEToRef + GetDistribution, + M: GLWEPublicKeyPreparedAlloc, { module.prepare_glwe_public_key(self, other); } } -pub trait GLWEPublicKeyPreparedToMut { - fn to_mut(&mut self) -> GLWEPublicKeyPrepared<&mut [u8], B>; -} - -impl GLWEPublicKeyPreparedToMut for GLWEPublicKeyPrepared { - fn to_mut(&mut self) -> GLWEPublicKeyPrepared<&mut [u8], B> { - GLWEPublicKeyPrepared { - dist: self.dist, - k: self.k, - base2k: self.base2k, - data: self.data.to_mut(), - } +impl GLWEPreparedToMut for GLWEPublicKeyPrepared +where + GLWEPrepared: GLWEPreparedToMut, +{ + fn to_mut(&mut self) -> GLWEPrepared<&mut [u8], B> { + self.key.to_mut() } } -pub trait GLWEPublicKeyPreparedToRef { - fn to_ref(&self) -> GLWEPublicKeyPrepared<&[u8], B>; -} - -impl GLWEPublicKeyPreparedToRef for GLWEPublicKeyPrepared { - fn to_ref(&self) -> GLWEPublicKeyPrepared<&[u8], B> { - GLWEPublicKeyPrepared { - data: self.data.to_ref(), - dist: self.dist, - k: self.k, - base2k: self.base2k, - } +impl GLWEPreparedToRef for GLWEPublicKeyPrepared +where + GLWEPrepared: GLWEPreparedToRef, +{ + fn to_ref(&self) -> GLWEPrepared<&[u8], B> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_secret.rs similarity index 88% rename from poulpy-core/src/layouts/prepared/glwe_sk.rs rename to poulpy-core/src/layouts/prepared/glwe_secret.rs index 3633e3f..98e8078 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_secret.rs @@ -49,9 +49,9 @@ impl GLWEInfos for GLWESecretPrepared { } } -pub trait GLWESecretPreparedAlloc +pub trait GLWESecretPreparedApi where - Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc, + Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc + SvpPrepare, { fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { GLWESecretPrepared { @@ -77,56 +77,7 @@ where assert_eq!(self.ring_degree(), infos.n()); self.bytes_of_glwe_secret_prepared(infos.rank()) } -} -impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc {} - -impl GLWESecretPrepared, B> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWESecretPreparedAlloc, - { - module.alloc_glwe_secret_prepared_from_infos(infos) - } - - pub fn alloc(module: &M, rank: Rank) -> Self - where - M: GLWESecretPreparedAlloc, - { - module.alloc_glwe_secret_prepared(rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWESecretPreparedAlloc, - { - module.bytes_of_glwe_secret_prepared_from_infos(infos) - } - - pub fn bytes_of(module: &M, rank: Rank) -> usize - where - M: GLWESecretPreparedAlloc, - { - module.bytes_of_glwe_secret_prepared(rank) - } -} - -impl GLWESecretPrepared { - pub fn n(&self) -> Degree { - Degree(self.data.n() as u32) - } - - pub fn rank(&self) -> Rank { - Rank(self.data.cols() as u32) - } -} - -pub trait GLWESecretPrepare -where - Self: SvpPrepare, -{ fn prepare_glwe_secret(&self, res: &mut R, other: &O) where R: GLWESecretPreparedToMut + GetDistributionMut, @@ -145,12 +96,54 @@ where } } -impl GLWESecretPrepare for Module where Self: SvpPrepare {} +impl GLWESecretPreparedApi for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc + SvpPrepare {} + +impl GLWESecretPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GLWEInfos, + M: GLWESecretPreparedApi, + { + module.alloc_glwe_secret_prepared_from_infos(infos) + } + + pub fn alloc(module: &M, rank: Rank) -> Self + where + M: GLWESecretPreparedApi, + { + module.alloc_glwe_secret_prepared(rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GLWEInfos, + M: GLWESecretPreparedApi, + { + module.bytes_of_glwe_secret_prepared_from_infos(infos) + } + + pub fn bytes_of(module: &M, rank: Rank) -> usize + where + M: GLWESecretPreparedApi, + { + module.bytes_of_glwe_secret_prepared(rank) + } +} + +impl GLWESecretPrepared { + pub fn n(&self) -> Degree { + Degree(self.data.n() as u32) + } + + pub fn rank(&self) -> Rank { + Rank(self.data.cols() as u32) + } +} impl GLWESecretPrepared { pub fn prepare(&mut self, module: &M, other: &O) where - M: GLWESecretPrepare, + M: GLWESecretPreparedApi, O: GLWESecretToRef + GetDistribution, { module.prepare_glwe_secret(self, other); diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_switching_key.rs similarity index 94% rename from poulpy-core/src/layouts/prepared/gglwe_ksk.rs rename to poulpy-core/src/layouts/prepared/glwe_switching_key.rs index 930f3e5..ce9cdfa 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_switching_key.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, + prepared::{GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] @@ -134,6 +134,23 @@ where infos.dsize(), ) } + + fn prepare_glwe_switching_key_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.prepare_gglwe_tmp_bytes(infos) + } + + fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, + { + self.prepare_gglwe(res, other, scratch); + *res.input_degree() = *other.input_degree(); + *res.output_degree() = *other.output_degree(); + } } impl GLWESwitchingKeyPreparedAlloc for Module where Self: GGLWEPreparedAlloc {} @@ -186,35 +203,11 @@ impl GLWESwitchingKeyPrepared, B> { } } -pub trait GLWESwitchingKeyPrepare -where - Self: GGLWEPrepare, -{ - fn prepare_glwe_switching_key_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.prepare_gglwe_tmp_bytes(infos) - } - - fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, - O: GGLWEToRef + GLWESwitchingKeyDegrees, - { - self.prepare_gglwe(res, other, scratch); - *res.input_degree() = *other.input_degree(); - *res.output_degree() = *other.output_degree(); - } -} - -impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} - impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: GLWESwitchingKeyPrepare, + M: GLWESwitchingKeyPreparedAlloc, { module.prepare_glwe_switching(self, other, scratch); } @@ -223,7 +216,7 @@ impl GLWESwitchingKeyPrepared { impl GLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: GLWESwitchingKeyPrepare, + M: GLWESwitchingKeyPreparedAlloc, { module.prepare_glwe_switching_key_tmp_bytes(self) } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs similarity index 93% rename from poulpy-core/src/layouts/prepared/gglwe_tsk.rs rename to poulpy-core/src/layouts/prepared/glwe_tensor_key.rs index 7b07ce7..d1287e8 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs @@ -1,8 +1,8 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, - GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef, + GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -107,6 +107,28 @@ where infos.dsize(), ) } + + fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.prepare_gglwe_tmp_bytes(infos) + } + + fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: TensorKeyPreparedToMut, + O: TensorKeyToRef, + { + let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut(); + let other: TensorKey<&[u8]> = other.to_ref(); + + assert_eq!(res.keys.len(), other.keys.len()); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.prepare_gglwe(a, b, scratch); + } + } } impl TensorKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} @@ -165,40 +187,11 @@ impl TensorKeyPrepared { } } -pub trait TensorKeyPrepare -where - Self: GGLWEPrepare, -{ - fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.prepare_gglwe_tmp_bytes(infos) - } - - fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef, - { - let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut(); - let other: TensorKey<&[u8]> = other.to_ref(); - - assert_eq!(res.keys.len(), other.keys.len()); - - for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.prepare_gglwe(a, b, scratch); - } - } -} - -impl TensorKeyPrepare for Module where Self: GGLWEPrepare {} - impl TensorKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPrepare, + M: TensorKeyPreparedAlloc, { module.prepare_tensor_key_tmp_bytes(infos) } @@ -208,7 +201,7 @@ impl TensorKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: TensorKeyToRef, - M: TensorKeyPrepare, + M: TensorKeyPreparedAlloc, { module.prepare_tensor_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs similarity index 94% rename from poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs rename to poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs index 26d617f..96a9907 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; #[derive(PartialEq, Eq)] @@ -101,6 +101,21 @@ where ); self.bytes_of_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } + + fn prepare_glwe_to_lwe_switching_key_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.prepare_glwe_switching_key_tmp_bytes(infos); + } + + fn prepare_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, + { + self.prepare_glwe_switching(res, other, scratch); + } } impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} @@ -137,33 +152,11 @@ impl GLWEToLWESwitchingKeyPrepared, B> { } } -pub trait GLWEToLWESwitchingKeyPrepare -where - Self: GLWESwitchingKeyPrepare, -{ - fn prepare_glwe_to_lwe_switching_key_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos, - { - self.prepare_glwe_switching_key_tmp_bytes(infos); - } - - fn prepare_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, - O: GGLWEToRef + GLWESwitchingKeyDegrees, - { - self.prepare_glwe_switching(res, other, scratch); - } -} - -impl GLWEToLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} - impl GLWEToLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyPrepare, + M: GLWEToLWESwitchingKeyPreparedAlloc, { module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos); } @@ -173,7 +166,7 @@ impl GLWEToLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: GLWEToLWESwitchingKeyPrepare, + M: GLWEToLWESwitchingKeyPreparedAlloc, { module.prepare_glwe_to_lwe_switching_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_switching_key.rs similarity index 94% rename from poulpy-core/src/layouts/prepared/lwe_ksk.rs rename to poulpy-core/src/layouts/prepared/lwe_switching_key.rs index 0319f77..6fc0ee9 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_switching_key.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; #[derive(PartialEq, Eq)] @@ -110,6 +110,20 @@ where ); self.bytes_of_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) } + + fn prepare_lwe_switching_key_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.prepare_glwe_switching_key_tmp_bytes(infos); + } + fn prepare_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, + { + self.prepare_glwe_switching(res, other, scratch); + } } impl LWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} @@ -146,32 +160,11 @@ impl LWESwitchingKeyPrepared, B> { } } -pub trait LWESwitchingKeyPrepare -where - Self: GLWESwitchingKeyPrepare, -{ - fn prepare_lwe_switching_key_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos, - { - self.prepare_glwe_switching_key_tmp_bytes(infos); - } - fn prepare_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, - O: GGLWEToRef + GLWESwitchingKeyDegrees, - { - self.prepare_glwe_switching(res, other, scratch); - } -} - -impl LWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} - impl LWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - M: LWESwitchingKeyPrepare, + M: LWESwitchingKeyPreparedAlloc, { module.prepare_lwe_switching_key_tmp_bytes(infos); } @@ -181,7 +174,7 @@ impl LWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: LWESwitchingKeyPrepare, + M: LWESwitchingKeyPreparedAlloc, { module.prepare_lwe_switching_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_switching_key.rs similarity index 94% rename from poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs rename to poulpy-core/src/layouts/prepared/lwe_to_glwe_switching_key.rs index c863c3f..bb0952c 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_switching_key.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. @@ -108,6 +108,21 @@ where ); self.bytes_of_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } + + fn prepare_lwe_to_glwe_switching_key_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.prepare_glwe_switching_key_tmp_bytes(infos); + } + + fn prepare_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, + { + self.prepare_glwe_switching(res, other, scratch); + } } impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} @@ -144,33 +159,11 @@ impl LWEToGLWESwitchingKeyPrepared, B> { } } -pub trait LWEToGLWESwitchingKeyPrepare -where - Self: GLWESwitchingKeyPrepare, -{ - fn prepare_lwe_to_glwe_switching_key_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos, - { - self.prepare_glwe_switching_key_tmp_bytes(infos); - } - - fn prepare_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, - O: GGLWEToRef + GLWESwitchingKeyDegrees, - { - self.prepare_glwe_switching(res, other, scratch); - } -} - -impl LWEToGLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} - impl LWEToGLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyPrepare, + M: LWEToGLWESwitchingKeyPreparedAlloc, { module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos); } @@ -180,7 +173,7 @@ impl LWEToGLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: LWEToGLWESwitchingKeyPrepare, + M: LWEToGLWESwitchingKeyPreparedAlloc, { module.prepare_lwe_to_glwe_switching_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/mod.rs b/poulpy-core/src/layouts/prepared/mod.rs index 296144a..8944b97 100644 --- a/poulpy-core/src/layouts/prepared/mod.rs +++ b/poulpy-core/src/layouts/prepared/mod.rs @@ -1,21 +1,23 @@ -mod gglwe_atk; -mod gglwe_ct; -mod gglwe_ksk; -mod gglwe_tsk; -mod ggsw_ct; -mod glwe_pk; -mod glwe_sk; -mod glwe_to_lwe_ksk; -mod lwe_ksk; -mod lwe_to_glwe_ksk; +mod gglwe; +mod ggsw; +mod glwe; +mod glwe_automorphism_key; +mod glwe_public_key; +mod glwe_secret; +mod glwe_switching_key; +mod glwe_tensor_key; +mod glwe_to_lwe_switching_key; +mod lwe_switching_key; +mod lwe_to_glwe_switching_key; -pub use gglwe_atk::*; -pub use gglwe_ct::*; -pub use gglwe_ksk::*; -pub use gglwe_tsk::*; -pub use ggsw_ct::*; -pub use glwe_pk::*; -pub use glwe_sk::*; -pub use glwe_to_lwe_ksk::*; -pub use lwe_ksk::*; -pub use lwe_to_glwe_ksk::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; +pub use glwe_automorphism_key::*; +pub use glwe_public_key::*; +pub use glwe_secret::*; +pub use glwe_switching_key::*; +pub use glwe_tensor_key::*; +pub use glwe_to_lwe_switching_key::*; +pub use lwe_switching_key::*; +pub use lwe_to_glwe_switching_key::*; diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe.rs similarity index 100% rename from poulpy-core/src/noise/gglwe_ct.rs rename to poulpy-core/src/noise/gglwe.rs diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw.rs similarity index 100% rename from poulpy-core/src/noise/ggsw_ct.rs rename to poulpy-core/src/noise/ggsw.rs diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe.rs similarity index 100% rename from poulpy-core/src/noise/glwe_ct.rs rename to poulpy-core/src/noise/glwe.rs diff --git a/poulpy-core/src/noise/mod.rs b/poulpy-core/src/noise/mod.rs index aace6ad..6f8882f 100644 --- a/poulpy-core/src/noise/mod.rs +++ b/poulpy-core/src/noise/mod.rs @@ -1,10 +1,10 @@ -mod gglwe_ct; -mod ggsw_ct; -mod glwe_ct; +mod gglwe; +mod ggsw; +mod glwe; -pub use gglwe_ct::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; +pub use gglwe::*; +pub use ggsw::*; +pub use glwe::*; #[allow(clippy::too_many_arguments)] #[allow(dead_code)] diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index a479080..7261410 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,10 +6,10 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, - GLWESecret, GLWESwitchingKey, Rank, TensorKey, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPrepared, + GLWEPublicKey, GLWESecret, GLWESwitchingKey, Rank, TensorKey, prepared::{ - AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + GGLWEPrepared, GGSWPrepared, GLWEAutomorphismKeyPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared, }, }, @@ -19,7 +19,7 @@ pub trait ScratchTakeCore where Self: ScratchTakeBasic + ScratchAvailable, { - fn take_glwe_ct(&mut self, module: &M, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) + fn take_glwe(&mut self, module: &M, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos, M: ModuleN, @@ -36,7 +36,7 @@ where ) } - fn take_glwe_ct_slice(&mut self, module: &M, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_slice(&mut self, module: &M, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, M: ModuleN, @@ -44,14 +44,14 @@ where let mut scratch: &mut Self = self; let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { - let (ct, new_scratch) = scratch.take_glwe_ct(module, infos); + let (ct, new_scratch) = scratch.take_glwe(module, infos); scratch = new_scratch; cts.push(ct); } (cts, scratch) } - fn take_glwe_pt(&mut self, module: &M, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) + fn take_glwe_plaintext(&mut self, module: &M, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) where A: GLWEInfos, M: ModuleN, @@ -184,25 +184,38 @@ where (cts, scratch) } - fn take_glwe_pk(&mut self, module: &M, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) + fn take_glwe_public_key(&mut self, module: &M, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) where A: GLWEInfos, M: ModuleN, { assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_vec_znx(module, (infos.rank() + 1).into(), infos.size()); + let (data, scratch) = self.take_glwe(module, infos); ( GLWEPublicKey { - k: infos.k(), dist: Distribution::NONE, - base2k: infos.base2k(), - data, + key: data, }, scratch, ) } - fn take_glwe_pk_prepared(&mut self, module: &M, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_public_key_prepared(&mut self, module: &M, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) + where + A: GLWEInfos, + M: ModuleN + VecZnxDftBytesOf, + { + let (data, scratch) = self.take_glwe_prepared(module, infos); + ( + GLWEPublicKeyPrepared { + dist: Distribution::NONE, + key: data, + }, + scratch, + ) + } + + fn take_glwe_prepared(&mut self, module: &M, infos: &A) -> (GLWEPrepared<&mut [u8], B>, &mut Self) where A: GLWEInfos, M: ModuleN + VecZnxDftBytesOf, @@ -210,9 +223,8 @@ where assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_vec_znx_dft(module, (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKeyPrepared { + GLWEPrepared { k: infos.k(), - dist: Distribution::NONE, base2k: infos.base2k(), data, }, @@ -265,7 +277,7 @@ where ) } - fn take_gglwe_switching_key_prepared( + fn take_glwe_switching_key_prepared( &mut self, module: &M, infos: &A, @@ -300,14 +312,14 @@ where &mut self, module: &M, infos: &A, - ) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + ) -> (GLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, M: ModuleN + VmpPMatBytesOf, { assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_gglwe_prepared(module, infos); - (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) + (GLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) } fn take_tensor_key(&mut self, module: &M, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index ddaaf9c..502a59b 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -4,9 +4,9 @@ use crate::layouts::{ AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, compressed::{ - AutomorphismKeyCompressed, GGLWECompressed, GGSWCompressed, GLWECompressed, GLWESwitchingKeyCompressed, - GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, - TensorKeyCompressed, + GGLWECompressed, GGSWCompressed, GLWEAutomorphismKeyCompressed, GLWECompressed, GLWESwitchingKeyCompressed, + GLWETensorKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, + LWEToGLWESwitchingKeyCompressed, }, }; @@ -75,7 +75,8 @@ fn test_automorphism_key_serialization() { #[test] fn test_automorphism_key_compressed_serialization() { - let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWEAutomorphismKeyCompressed> = + GLWEAutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } @@ -87,7 +88,7 @@ fn test_tensor_key_serialization() { #[test] fn test_tensor_key_compressed_serialization() { - let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWETensorKeyCompressed> = GLWETensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } diff --git a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs index 5a7bad6..fabd835 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyAutomorphism, AutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore, + AutomorphismKeyEncryptSk, GLWEAutomorphismKeyAutomorphism, GLWEDecrypt, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGLWEInfos, GLWEPlaintext, - GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEAutomorphismKeyPreparedApi, GLWEPlaintext, GLWESecret, + GLWESecretPreparedApi, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, }; @@ -19,14 +19,12 @@ use crate::{ pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where Module: AutomorphismKeyEncryptSk - + AutomorphismKeyPreparedAlloc - + AutomorphismKeyPrepare - + AutomorphismKeyAutomorphism + + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism + GaloisElement + VecZnxSubScalarInplace - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -117,8 +115,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared, BE> = - AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos); + let mut auto_key_apply_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -190,14 +188,12 @@ where pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where Module: AutomorphismKeyEncryptSk - + AutomorphismKeyPreparedAlloc - + AutomorphismKeyPrepare - + AutomorphismKeyAutomorphism + + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism + GaloisElement + VecZnxSubScalarInplace - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -271,8 +267,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared, BE> = - AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_layout); + let mut auto_key_apply_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_layout); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index 4a2b665..b8cd8d1 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -8,9 +8,9 @@ use crate::{ AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, - GLWESecretPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, TensorKeyPreparedAlloc, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, + AutomorphismKey, GGSW, GGSWLayout, GLWEAutomorphismKeyPreparedApi, GLWESecret, GLWESecretPreparedApi, TensorKey, + TensorKeyLayout, TensorKeyPreparedAlloc, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -19,14 +19,11 @@ pub fn test_ggsw_automorphism(module: &Module) where Module: GGSWEncryptSk + AutomorphismKeyEncryptSk - + AutomorphismKeyPreparedAlloc - + AutomorphismKeyPrepare + + GLWEAutomorphismKeyPreparedApi + GGSWAutomorphism - + TensorKeyPrepare + TensorKeyPreparedAlloc + TensorKeyEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxAutomorphismInplace + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -137,8 +134,8 @@ where scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared, BE> = - AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); + let mut auto_key_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); let mut tsk_prepared: TensorKeyPrepared, BE> = @@ -181,14 +178,11 @@ pub fn test_ggsw_automorphism_inplace(module: &Module) where Module: GGSWEncryptSk + AutomorphismKeyEncryptSk - + AutomorphismKeyPreparedAlloc - + AutomorphismKeyPrepare + + GLWEAutomorphismKeyPreparedApi + GGSWAutomorphism - + TensorKeyPrepare + TensorKeyPreparedAlloc + TensorKeyEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxAutomorphismInplace + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -286,8 +280,8 @@ where scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared, BE> = - AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); + let mut auto_key_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); let mut tsk_prepared: TensorKeyPrepared, BE> = diff --git a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs index 22d913d..64540cb 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -8,9 +8,9 @@ use crate::{ AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, - GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedApi, GLWELayout, GLWEPlaintext, GLWESecret, + GLWESecretPreparedApi, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, }; @@ -18,14 +18,12 @@ use crate::{ pub fn test_glwe_automorphism(module: &Module) where Module: GLWEEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism + AutomorphismKeyEncryptSk - + AutomorphismKeyPrepare - + AutomorphismKeyPreparedAlloc + + GLWEAutomorphismKeyPreparedApi + GLWENoise + VecZnxAutomorphismInplace, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -108,8 +106,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, BE> = - AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); + let mut autokey_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow()); @@ -138,14 +136,12 @@ where pub fn test_glwe_automorphism_inplace(module: &Module) where Module: GLWEEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism + AutomorphismKeyEncryptSk - + AutomorphismKeyPrepare - + AutomorphismKeyPreparedAlloc + + GLWEAutomorphismKeyPreparedApi + GLWENoise + VecZnxAutomorphismInplace, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -219,8 +215,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, BE> = - AutomorphismKeyPrepared::alloc_from_infos(module, &autokey); + let mut autokey_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &autokey); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 610b61e..0c55bad 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -8,10 +8,9 @@ use crate::{ GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore, layouts::{ - Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, - GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPrepare, GLWEToLWESwitchingKeyPreparedAlloc, LWE, - LWELayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPrepare, - LWEToGLWESwitchingKeyPreparedAlloc, Rank, TorusPrecision, + Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, GLWEToLWEKeyLayout, + GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPreparedAlloc, LWE, LWELayout, LWEPlaintext, LWESecret, + LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPreparedAlloc, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared}, }, }; @@ -21,11 +20,9 @@ where Module: GLWEFromLWE + LWEToGLWESwitchingKeyEncryptSk + GLWEDecrypt - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + LWEEncryptSk - + LWEToGLWESwitchingKeyPreparedAlloc - + LWEToGLWESwitchingKeyPrepare, + + LWEToGLWESwitchingKeyPreparedAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -115,10 +112,8 @@ where + GLWEEncryptSk + LWEDecrypt + GLWEDecrypt - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + GLWEToLWESwitchingKeyEncryptSk - + GLWEToLWESwitchingKeyPrepare + GLWEToLWESwitchingKeyPreparedAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index 4f84616..11d0c00 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -9,8 +9,8 @@ use crate::{ GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare, - GLWESecretPreparedAlloc, GLWESwitchingKeyDecompress, compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, + AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPreparedApi, + GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, prepared::GLWESecretPrepared, }, noise::GGLWENoise, }; @@ -19,8 +19,7 @@ pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where Module: AutomorphismKeyEncryptSk + GGLWEKeyswitch - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress @@ -92,8 +91,7 @@ pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &M where Module: AutomorphismKeyCompressedEncryptSk + GGLWEKeyswitch - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + AutomorphismKeyDecompress @@ -120,12 +118,13 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); + let mut atk_compressed: GLWEAutomorphismKeyCompressed> = + GLWEAutomorphismKeyCompressed::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKeyCompressed::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWEAutomorphismKeyCompressed::encrypt_sk_tmp_bytes( module, &atk_infos, )); diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs index 60eaf0d..1516684 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -9,9 +9,8 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyCompressed, - GLWESwitchingKeyDecompress, - prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, + GGLWELayout, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress, + prepared::{GGLWEPreparedAlloc, GLWESecretPrepared}, }, noise::GGLWENoise, }; @@ -19,12 +18,10 @@ use crate::{ pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) where Module: GGLWEEncryptSk - + GGLWEPrepare + GGLWEPreparedAlloc + GGLWEKeyswitch + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + GLWESwitchingKeyEncryptSk + VecZnxFillUniform + GGLWENoise, @@ -86,12 +83,10 @@ where pub fn test_gglwe_switching_key_compressed_encrypt_sk(module: &Module) where Module: GGLWEEncryptSk - + GGLWEPrepare + GGLWEPreparedAlloc + GGLWEKeyswitch + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress diff --git a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs index 529a42f..b315fef 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ GGSWCompressedEncryptSk, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, compressed::GGSWCompressed, + GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPreparedApi, compressed::GGSWCompressed, prepared::GLWESecretPrepared, }, }; @@ -17,7 +17,7 @@ pub fn test_ggsw_encrypt_sk(module: &Module) where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, - Module: GGSWEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + GGSWNoise, + Module: GGSWEncryptSk + GLWESecretPreparedApi + GGSWNoise, { let base2k: usize = 12; let k: usize = 54; @@ -74,8 +74,7 @@ pub fn test_ggsw_compressed_encrypt_sk(module: &Module) where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, - Module: - GGSWCompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + GGSWNoise + GGSWDecompress, + Module: GGSWCompressedEncryptSk + GLWESecretPreparedApi + GGSWNoise + GGSWDecompress, { let base2k: usize = 12; let k: usize = 54; diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs index 0f07c32..135561d 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -9,8 +9,8 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, GLWEPublicKeyPreparedAlloc, - GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPreparedAlloc, GLWESecret, + GLWESecretPreparedApi, LWEInfos, compressed::GLWECompressed, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, @@ -18,8 +18,7 @@ use crate::{ pub fn test_glwe_encrypt_sk(module: &Module) where - Module: - GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxFillUniform + GLWESub, + Module: GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -84,12 +83,7 @@ where pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: GLWECompressedEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + VecZnxFillUniform - + GLWESub, + Module: GLWECompressedEncryptSk + GLWEDecrypt + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -166,8 +160,7 @@ where pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: - GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxFillUniform + GLWESub, + Module: GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -217,12 +210,10 @@ where pub fn test_glwe_encrypt_pk(module: &Module) where Module: GLWEEncryptPk - + GLWEPublicKeyPrepare + GLWEPublicKeyPreparedAlloc + GLWEPublicKeyGenerate + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs index 48573db..e56d0fb 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -13,16 +13,15 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyCompressed, - TensorKeyLayout, prepared::GLWESecretPrepared, + Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, GLWETensorKeyCompressed, TensorKey, TensorKeyLayout, + prepared::GLWESecretPrepared, }, }; pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + GLWEDecrypt + VecZnxDftAlloc + VecZnxBigAlloc @@ -113,8 +112,7 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + GGLWETensorKeyCompressedEncryptSk + GLWEDecrypt + VecZnxDftAlloc @@ -144,12 +142,13 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); + let mut tensor_key_compressed: GLWETensorKeyCompressed> = + GLWETensorKeyCompressed::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWETensorKeyCompressed::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); diff --git a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs index d5f13fb..419da96 100644 --- a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs @@ -8,8 +8,7 @@ use crate::{ GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, - GLWESwitchingKey, GLWESwitchingKeyLayout, + GGSW, GGSWLayout, GGSWPreparedAlloc, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, @@ -21,10 +20,8 @@ where Module: GGLWEExternalProduct + GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxRotateInplace - + GGSWPrepare + GGSWPreparedAlloc + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -172,10 +169,8 @@ where Module: GGLWEExternalProduct + GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxRotateInplace - + GGSWPrepare + GGSWPreparedAlloc + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, diff --git a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs index de080a3..7e7535c 100644 --- a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ GGSWEncryptSk, GGSWExternalProduct, GGSWNoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + GGSW, GGSWLayout, GGSWPreparedAlloc, GLWESecret, GLWESecretPreparedApi, prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, @@ -19,9 +19,7 @@ pub fn test_ggsw_external_product(module: &Module) where Module: GGSWEncryptSk + GGSWExternalProduct - + GLWESecretPrepare - + GLWESecretPreparedAlloc - + GGSWPrepare + + GLWESecretPreparedApi + GGSWPreparedAlloc + VecZnxRotateInplace + GGSWNoise, @@ -155,9 +153,7 @@ pub fn test_ggsw_external_product_inplace(module: &Module) where Module: GGSWEncryptSk + GGSWExternalProduct - + GLWESecretPrepare - + GLWESecretPreparedAlloc - + GGSWPrepare + + GLWESecretPreparedApi + GGSWPreparedAlloc + VecZnxRotateInplace + GGSWNoise, diff --git a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs index 6c9de79..d501b9a 100644 --- a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs @@ -8,8 +8,7 @@ use crate::{ GGSWEncryptSk, GLWEEncryptSk, GLWEExternalProduct, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, - GLWESecretPreparedAlloc, + GGSW, GGSWLayout, GGSWPreparedAlloc, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, @@ -19,15 +18,13 @@ use crate::{ pub fn test_glwe_external_product(module: &Module) where Module: GGSWEncryptSk - + GGSWPrepare + GGSWPreparedAlloc + VecZnxFillUniform + GLWEExternalProduct + GLWEEncryptSk + GLWENoise + VecZnxRotateInplace - + GLWESecretPrepare - + GLWESecretPreparedAlloc, + + GLWESecretPreparedApi, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -151,15 +148,13 @@ where pub fn test_glwe_external_product_inplace(module: &Module) where Module: GGSWEncryptSk - + GGSWPrepare + GGSWPreparedAlloc + VecZnxFillUniform + GLWEExternalProduct + GLWEEncryptSk + GLWENoise + VecZnxRotateInplace - + GLWESecretPrepare - + GLWESecretPreparedAlloc, + + GLWESecretPreparedApi, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { diff --git a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs index dad69c0..baa1c34 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -8,8 +8,7 @@ use crate::{ GGLWEKeyswitch, GGLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, + GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -19,10 +18,8 @@ pub fn test_gglwe_switching_key_keyswitch(module: &Module) where Module: GLWESwitchingKeyEncryptSk + GGLWEKeyswitch - + GLWESecretPrepare + GLWESwitchingKeyPreparedAlloc - + GLWESecretPreparedAlloc - + GLWESwitchingKeyPrepare + + GLWESecretPreparedApi + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -165,9 +162,7 @@ pub fn test_gglwe_switching_key_keyswitch_inplace(module: &Module: GLWESwitchingKeyEncryptSk + GGLWEKeyswitch - + GLWESecretPrepare - + GLWESecretPreparedAlloc - + GLWESwitchingKeyPrepare + + GLWESecretPreparedApi + GGLWENoise + GLWESwitchingKeyPreparedAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, diff --git a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs index 9646909..8f5be61 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -8,9 +8,8 @@ use crate::{ GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, - TensorKeyPreparedAlloc, + GGSW, GGSWLayout, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, @@ -23,13 +22,10 @@ where + GLWESwitchingKeyEncryptSk + TensorKeyEncryptSk + GGSWKeyswitch - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + TensorKeyPreparedAlloc + GLWESwitchingKeyPreparedAlloc - + GGSWNoise - + GLWESwitchingKeyPrepare - + TensorKeyPrepare, + + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -191,13 +187,10 @@ where + GLWESwitchingKeyEncryptSk + TensorKeyEncryptSk + GGSWKeyswitch - + GLWESecretPreparedAlloc - + GLWESecretPrepare + + GLWESecretPreparedApi + TensorKeyPreparedAlloc + GLWESwitchingKeyPreparedAlloc - + GGSWNoise - + GLWESwitchingKeyPrepare - + TensorKeyPrepare, + + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { diff --git a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs index 146ea32..8619a7b 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -8,8 +8,8 @@ use crate::{ GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, - GLWESwitchingKeyLayout, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWESwitchingKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -22,9 +22,7 @@ where + GLWESwitchingKeyEncryptSk + GLWEEncryptSk + GLWEKeyswitch - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + GLWESwitchingKeyPrepare + + GLWESecretPreparedApi + GLWESwitchingKeyPreparedAlloc + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -145,9 +143,7 @@ where + GLWESwitchingKeyEncryptSk + GLWEEncryptSk + GLWEKeyswitch - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + GLWESwitchingKeyPrepare + + GLWESecretPreparedApi + GLWESwitchingKeyPreparedAlloc + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, diff --git a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs index a709747..cbda02f 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -7,19 +7,15 @@ use poulpy_hal::{ use crate::{ LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore, layouts::{ - LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPrepare, - LWESwitchingKeyPreparedAlloc, prepared::LWESwitchingKeyPrepared, + LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPreparedAlloc, + prepared::LWESwitchingKeyPrepared, }, }; pub fn test_lwe_keyswitch(module: &Module) where - Module: LWEKeySwitch - + LWESwitchingKeyEncrypt - + LWEEncryptSk - + LWESwitchingKeyPreparedAlloc - + LWEDecrypt - + LWESwitchingKeyPrepare, + Module: + LWEKeySwitch + LWESwitchingKeyEncrypt + LWEEncryptSk + LWESwitchingKeyPreparedAlloc + LWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index 7e132b2..a9e340e 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -9,9 +9,9 @@ use poulpy_hal::{ use crate::{ AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, - GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedApi, GLWELayout, GLWEPlaintext, GLWESecret, + GLWESecretPreparedApi, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, }; @@ -19,11 +19,9 @@ pub fn test_glwe_packing(module: &Module) where Module: GLWEEncryptSk + AutomorphismKeyEncryptSk - + AutomorphismKeyPrepare - + AutomorphismKeyPreparedAlloc + + GLWEAutomorphismKeyPreparedApi + GLWEPacking - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + GLWESub + GLWEDecrypt + GLWERotate, @@ -82,7 +80,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, BE>> = HashMap::new(); + let mut auto_keys: HashMap, BE>> = HashMap::new(); let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( @@ -93,7 +91,8 @@ where &mut source_xe, scratch.borrow(), ); - let mut atk_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp); + let mut atk_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp); atk_prepared.prepare(module, &tmp, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index 395a961..c15e584 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -11,9 +11,9 @@ use crate::{ encryption::SIGMA, glwe_trace::GLWETrace, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, - GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedApi, GLWELayout, GLWEPlaintext, GLWESecret, + GLWESecretPreparedApi, LWEInfos, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::var_noise_gglwe_product, }; @@ -24,11 +24,9 @@ where + GLWEEncryptSk + GLWEDecrypt + AutomorphismKeyEncryptSk - + AutomorphismKeyPrepare - + AutomorphismKeyPreparedAlloc + + GLWEAutomorphismKeyPreparedApi + VecZnxFillUniform - + GLWESecretPrepare - + GLWESecretPreparedAlloc + + GLWESecretPreparedApi + VecZnxSubInplace + VecZnxNormalizeInplace, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -98,7 +96,7 @@ where scratch.borrow(), ); - let mut auto_keys: HashMap, BE>> = HashMap::new(); + let mut auto_keys: HashMap, BE>> = HashMap::new(); let gal_els: Vec = GLWE::trace_galois_elements(module); let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { @@ -110,7 +108,8 @@ where &mut source_xe, scratch.borrow(), ); - let mut atk_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp); + let mut atk_prepared: GLWEAutomorphismKeyPrepared, BE> = + GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp); atk_prepared.prepare(module, &tmp, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index a3a779a..3a44e4f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -3,7 +3,7 @@ use poulpy_core::{ GLWEOperations, TakeGLWEPlaintext, TakeGLWESlice, glwe_packing, layouts::{ GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, }; use poulpy_hal::{ @@ -32,7 +32,7 @@ impl FheUintWord { &mut self, module: &Module, mut tmp_res: Vec>, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 1799127..a8b7212 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -20,7 +20,7 @@ use poulpy_core::{ }; use poulpy_core::glwe_packing; -use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::AutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ @@ -268,7 +268,7 @@ fn post_process( log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap, B>>, + auto_keys: &HashMap, B>>, scratch: &mut Scratch, ) where DataRes: DataMut, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 1e52a76..a471ffc 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, TensorKeyLayout, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, }; use std::collections::HashMap; @@ -155,7 +155,7 @@ where pub struct CircuitBootstrappingKeyPrepared { pub(crate) brk: BlindRotationKeyPrepared, pub(crate) tsk: TensorKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, + pub(crate) atk: HashMap, B>>, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { @@ -200,12 +200,12 @@ where Module: VmpPMatAlloc + VmpPrepare, BlindRotationKey: PrepareAlloc, BRA, B>>, TensorKey: PrepareAlloc, B>>, - AutomorphismKey: PrepareAlloc, B>>, + AutomorphismKey: PrepareAlloc, B>>, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> CircuitBootstrappingKeyPrepared, BRA, B> { let brk: BlindRotationKeyPrepared, BRA, B> = self.brk.prepare_alloc(module, scratch); let tsk: TensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); - let mut atk: HashMap, B>> = HashMap::new(); + let mut atk: HashMap, B>> = HashMap::new(); for (key, value) in &self.atk { atk.insert(*key, value.prepare_alloc(module, scratch)); }